OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/compact_location_bar.h" |
| 6 |
| 7 #include <gtk/gtk.h> |
| 8 #include <algorithm> |
| 9 |
| 10 #include "app/l10n_util.h" |
| 11 #include "base/gfx/point.h" |
| 12 #include "chrome/app/chrome_dll_resource.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_edit_view_gtk.h" |
| 14 #include "chrome/browser/browser_list.h" |
| 15 #include "chrome/browser/browser_theme_provider.h" |
| 16 #include "chrome/browser/profile.h" |
| 17 #include "chrome/browser/view_ids.h" |
| 18 #include "chrome/browser/views/event_utils.h" |
| 19 #include "chrome/browser/views/frame/browser_view.h" |
| 20 #include "chrome/browser/views/tabs/tab.h" |
| 21 #include "chrome/browser/views/tabs/tab_strip.h" |
| 22 #include "grit/chromium_strings.h" |
| 23 #include "grit/generated_resources.h" |
| 24 #include "grit/theme_resources.h" |
| 25 #include "views/background.h" |
| 26 #include "views/controls/button/image_button.h" |
| 27 #include "views/controls/native/native_view_host.h" |
| 28 #include "views/widget/widget.h" |
| 29 |
| 30 const int kDefaultLocationBarWidth = 300; |
| 31 const int kHideTimeoutInSeconds = 2; |
| 32 |
| 33 CompactLocationBar::CompactLocationBar(BrowserView* browser_view) |
| 34 : browser_view_(browser_view), |
| 35 current_contents_(NULL), |
| 36 reload_(NULL) { |
| 37 popup_timer_.reset(new base::OneShotTimer<CompactLocationBar>()); |
| 38 set_background(views::Background::CreateStandardPanelBackground()); |
| 39 } |
| 40 |
| 41 CompactLocationBar::~CompactLocationBar() { |
| 42 if (popup_) { |
| 43 // This is a hack to avoid deleting this twice. |
| 44 // This problem will be gone once we eliminate a popup. |
| 45 GetParent()->RemoveAllChildViews(false); |
| 46 popup_->Close(); |
| 47 popup_ = NULL; |
| 48 } |
| 49 } |
| 50 |
| 51 //////////////////////////////////////////////////////////////////////////////// |
| 52 // CompactLocationBar public: |
| 53 |
| 54 void CompactLocationBar::StartPopupTimer() { |
| 55 if (popup_ == NULL || !popup_->IsVisible()) |
| 56 return; |
| 57 if (popup_timer_->IsRunning()) { |
| 58 // Restart the timer. |
| 59 popup_timer_->Reset(); |
| 60 } else { |
| 61 popup_timer_->Start(base::TimeDelta::FromSeconds(kHideTimeoutInSeconds), |
| 62 this, &CompactLocationBar::HidePopup); |
| 63 } |
| 64 } |
| 65 |
| 66 gfx::Rect CompactLocationBar::GetBoundsUnderTab(const Tab* tab) const { |
| 67 // Get the position of the left-bottom corner of the tab on the screen |
| 68 gfx::Point tab_left_bottom(0, tab->height()); |
| 69 views::View::ConvertPointToScreen(tab, &tab_left_bottom); |
| 70 // Get the position of the left edge of the window. |
| 71 gfx::Point browser_left_top(0, 0); |
| 72 views::View::ConvertPointToScreen(browser_view_, |
| 73 &browser_left_top); |
| 74 |
| 75 // The compact location bar must be smaller than browser_width. |
| 76 int width = std::min(browser_view_->width(), kDefaultLocationBarWidth); |
| 77 |
| 78 // Try to center around the tab, or align to the left of the window. |
| 79 // TODO(oshima): handle RTL |
| 80 int x = std::max(tab_left_bottom.x() - ((width - tab->width()) / 2), |
| 81 browser_left_top.x()); |
| 82 return gfx::Rect(x, tab_left_bottom.y(), width, 28); |
| 83 } |
| 84 |
| 85 void CompactLocationBar::UpdateBounds(const Tab* tab) { |
| 86 if (popup_ != NULL) |
| 87 popup_->SetBounds(GetBoundsUnderTab(tab)); |
| 88 } |
| 89 |
| 90 void CompactLocationBar::Update(const Tab* tab, const TabContents* contents) { |
| 91 DCHECK(tab != NULL && contents != NULL); |
| 92 if (current_contents_ == contents) { |
| 93 StartPopupTimer(); |
| 94 return; |
| 95 } |
| 96 CancelPopupTimer(); |
| 97 HidePopup(); |
| 98 |
| 99 if (!popup_) { |
| 100 popup_ = views::Widget::CreatePopupWidget( |
| 101 views::Widget::Transparent, |
| 102 views::Widget::AcceptEvents, |
| 103 views::Widget::DeleteOnDestroy); |
| 104 popup_->Init(NULL, GetBoundsUnderTab(tab)); |
| 105 popup_->SetContentsView(this); |
| 106 } else { |
| 107 UpdateBounds(tab); |
| 108 } |
| 109 current_contents_ = contents; |
| 110 location_entry_->Update(contents); |
| 111 popup_->Show(); |
| 112 // Set focus to the location entry. |
| 113 location_entry_->SetFocus(); |
| 114 |
| 115 // Start popup timer. |
| 116 StartPopupTimer(); |
| 117 } |
| 118 |
| 119 //////////////////////////////////////////////////////////////////////////////// |
| 120 // CompactLocationBar private: |
| 121 |
| 122 Browser* CompactLocationBar::browser() const { |
| 123 return browser_view_->browser(); |
| 124 } |
| 125 |
| 126 void CompactLocationBar::CancelPopupTimer() { |
| 127 popup_timer_->Stop(); |
| 128 } |
| 129 |
| 130 void CompactLocationBar::HidePopup() { |
| 131 current_contents_ = NULL; |
| 132 if (popup_) { |
| 133 CancelPopupTimer(); |
| 134 popup_->Hide(); |
| 135 } |
| 136 } |
| 137 |
| 138 void CompactLocationBar::Init() { |
| 139 ThemeProvider* tp = browser()->profile()->GetThemeProvider(); |
| 140 SkColor color = tp->GetColor(BrowserThemeProvider::COLOR_BUTTON_BACKGROUND); |
| 141 SkBitmap* background = tp->GetBitmapNamed(IDR_THEME_BUTTON_BACKGROUND); |
| 142 |
| 143 // Reload button. |
| 144 reload_ = new views::ImageButton(this); |
| 145 reload_->set_tag(IDC_RELOAD); |
| 146 reload_->SetTooltipText(l10n_util::GetString(IDS_TOOLTIP_RELOAD)); |
| 147 reload_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_RELOAD)); |
| 148 reload_->SetID(VIEW_ID_RELOAD_BUTTON); |
| 149 |
| 150 reload_->SetImage(views::CustomButton::BS_NORMAL, |
| 151 tp->GetBitmapNamed(IDR_RELOAD)); |
| 152 reload_->SetImage(views::CustomButton::BS_HOT, |
| 153 tp->GetBitmapNamed(IDR_RELOAD_H)); |
| 154 reload_->SetImage(views::CustomButton::BS_PUSHED, |
| 155 tp->GetBitmapNamed(IDR_RELOAD_P)); |
| 156 reload_->SetBackground(color, background, |
| 157 tp->GetBitmapNamed(IDR_BUTTON_MASK)); |
| 158 |
| 159 AddChildView(reload_); |
| 160 |
| 161 // Location bar. |
| 162 location_entry_.reset(new AutocompleteEditViewGtk( |
| 163 this, browser()->toolbar_model(), browser()->profile(), |
| 164 browser()->command_updater(), false, this)); |
| 165 |
| 166 |
| 167 location_entry_->Init(); |
| 168 location_entry_->Update(browser()->GetSelectedTabContents()); |
| 169 gtk_widget_show_all(location_entry_->widget()); |
| 170 gtk_widget_hide(location_entry_->widget()); |
| 171 |
| 172 location_entry_view_ = new views::NativeViewHost; |
| 173 AddChildView(location_entry_view_); |
| 174 location_entry_view_->set_focus_view(this); |
| 175 location_entry_view_->Attach(location_entry_->widget()); |
| 176 |
| 177 // TODO(oshima): Add Star Button |
| 178 location_entry_->Update(browser()->GetSelectedTabContents()); |
| 179 } |
| 180 |
| 181 //////////////////////////////////////////////////////////////////////////////// |
| 182 // views::View overrides: |
| 183 |
| 184 gfx::Size CompactLocationBar::GetPreferredSize() { |
| 185 if (!reload_) |
| 186 return gfx::Size(); // Not initialized yet, do nothing. |
| 187 |
| 188 gfx::Size sz = reload_->GetPreferredSize(); |
| 189 |
| 190 return gfx::Size(500, sz.height()); |
| 191 } |
| 192 |
| 193 void CompactLocationBar::Layout() { |
| 194 if (!reload_) |
| 195 return; // Not initialized yet, do nothing. |
| 196 |
| 197 int cur_x = 0; |
| 198 |
| 199 gfx::Size sz = reload_->GetPreferredSize(); |
| 200 reload_->SetBounds(cur_x, 0, sz.width(), sz.height()); |
| 201 cur_x += sz.width(); |
| 202 |
| 203 cur_x += 2; |
| 204 |
| 205 // The location bar gets the rest of the space in the middle. |
| 206 location_entry_view_->SetBounds(cur_x, 0, width() - cur_x * 2 - 2, height()); |
| 207 |
| 208 cur_x = width() - sz.width(); |
| 209 } |
| 210 |
| 211 void CompactLocationBar::Paint(gfx::Canvas* canvas) { |
| 212 View::Paint(canvas); |
| 213 } |
| 214 |
| 215 void CompactLocationBar::ViewHierarchyChanged(bool is_add, View* parent, |
| 216 View* child) { |
| 217 if (is_add && child == this) |
| 218 Init(); |
| 219 } |
| 220 |
| 221 void CompactLocationBar::OnMouseEntered(const views::MouseEvent& event) { |
| 222 CancelPopupTimer(); |
| 223 } |
| 224 |
| 225 void CompactLocationBar::OnMouseExited(const views::MouseEvent& event) { |
| 226 StartPopupTimer(); |
| 227 } |
| 228 |
| 229 //////////////////////////////////////////////////////////////////////////////// |
| 230 // views::ButtonListener overrides: |
| 231 |
| 232 void CompactLocationBar::ButtonPressed(views::Button* sender, |
| 233 const views::Event& event) { |
| 234 int id = sender->tag(); |
| 235 browser()->ExecuteCommandWithDisposition( |
| 236 id, event_utils::DispositionFromEventFlags(sender->mouse_event_flags())); |
| 237 } |
| 238 |
| 239 //////////////////////////////////////////////////////////////////////////////// |
| 240 // AutocompleteEditController overrides: |
| 241 |
| 242 void CompactLocationBar::OnAutocompleteAccept( |
| 243 const GURL& url, |
| 244 WindowOpenDisposition disposition, |
| 245 PageTransition::Type transition, |
| 246 const GURL& alternate_nav_url) { |
| 247 browser()->OpenURL(url, GURL(), disposition, transition); |
| 248 } |
| 249 |
| 250 void CompactLocationBar::OnChanged() { |
| 251 // Other one does "DoLayout" here. |
| 252 } |
| 253 |
| 254 void CompactLocationBar::OnInputInProgress(bool in_progress) { |
| 255 } |
| 256 |
| 257 SkBitmap CompactLocationBar::GetFavIcon() const { |
| 258 return SkBitmap(); |
| 259 } |
| 260 |
| 261 std::wstring CompactLocationBar::GetTitle() const { |
| 262 return std::wstring(); |
| 263 } |
| 264 |
| 265 //////////////////////////////////////////////////////////////////////////////// |
| 266 // BubblePositioner overrides: |
| 267 |
| 268 gfx::Rect CompactLocationBar::GetLocationStackBounds() const { |
| 269 gfx::Point lower_left(0, height()); |
| 270 ConvertPointToScreen(this, &lower_left); |
| 271 return gfx::Rect(lower_left.x(), lower_left.y(), 700, 100); |
| 272 } |
| 273 |
OLD | NEW |