| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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/ui/views/compact_nav/compact_location_bar_view.h" |
| 6 |
| 7 #if defined(TOOLKIT_USES_GTK) |
| 8 #include <gtk/gtk.h> |
| 9 #endif |
| 10 |
| 11 #include <algorithm> |
| 12 |
| 13 #include "chrome/app/chrome_command_ids.h" |
| 14 #include "chrome/app/chrome_dll_resource.h" |
| 15 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 17 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/themes/theme_service.h" |
| 20 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 21 #include "chrome/browser/ui/view_ids.h" |
| 22 #include "chrome/browser/ui/views/compact_nav/compact_location_bar_view_host.h" |
| 23 #include "chrome/browser/ui/views/event_utils.h" |
| 24 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 25 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 26 #include "chrome/browser/ui/views/reload_button.h" |
| 27 #include "content/browser/user_metrics.h" |
| 28 #include "grit/chromium_strings.h" |
| 29 #include "grit/generated_resources.h" |
| 30 #include "grit/theme_resources.h" |
| 31 #include "grit/theme_resources_standard.h" |
| 32 #include "ui/base/accessibility/accessible_view_state.h" |
| 33 #include "ui/base/l10n/l10n_util.h" |
| 34 #include "ui/base/resource/resource_bundle.h" |
| 35 #include "ui/base/theme_provider.h" |
| 36 #include "ui/gfx/canvas.h" |
| 37 #include "ui/gfx/point.h" |
| 38 #include "views/background.h" |
| 39 #include "views/controls/button/image_button.h" |
| 40 #include "views/controls/native/native_view_host.h" |
| 41 #include "views/widget/widget.h" |
| 42 #include "views/window/window.h" |
| 43 |
| 44 namespace { |
| 45 |
| 46 const int kDefaultLocationEntryWidth = 375; |
| 47 const int kCompactLocationLeftMargin = 7; |
| 48 const int kCompactLocationRightMargin = 8; |
| 49 const int kEntryPadding = 2; |
| 50 // TODO(oshima): ToolbarView gets this from background image's height; |
| 51 // Find out the right way, value for compact location bar. |
| 52 const int kDefaultLocationBarHeight = 34; |
| 53 |
| 54 } // namespace |
| 55 |
| 56 CompactLocationBarView::CompactLocationBarView(CompactLocationBarViewHost* host) |
| 57 : DropdownBarView(host), |
| 58 reload_button_(NULL), |
| 59 location_bar_view_(NULL), |
| 60 initialized_(false) { |
| 61 SetFocusable(true); |
| 62 } |
| 63 |
| 64 CompactLocationBarView::~CompactLocationBarView() { |
| 65 } |
| 66 |
| 67 //////////////////////////////////////////////////////////////////////////////// |
| 68 // CompactLocationBarView public: |
| 69 |
| 70 void CompactLocationBarView::SetFocusAndSelection(bool select_all) { |
| 71 location_bar_view_->FocusLocation(select_all); |
| 72 } |
| 73 |
| 74 void CompactLocationBarView::Update(const TabContents* contents) { |
| 75 location_bar_view_->Update(contents); |
| 76 } |
| 77 |
| 78 //////////////////////////////////////////////////////////////////////////////// |
| 79 // AccessiblePaneView overrides: |
| 80 |
| 81 bool CompactLocationBarView::SetPaneFocus( |
| 82 int view_storage_id, views::View* initial_focus) { |
| 83 if (!AccessiblePaneView::SetPaneFocus(view_storage_id, initial_focus)) |
| 84 return false; |
| 85 |
| 86 location_bar_view_->SetShowFocusRect(true); |
| 87 return true; |
| 88 } |
| 89 |
| 90 void CompactLocationBarView::GetAccessibleState( |
| 91 ui::AccessibleViewState* state) { |
| 92 state->role = ui::AccessibilityTypes::ROLE_TOOLBAR; |
| 93 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_TOOLBAR); |
| 94 } |
| 95 |
| 96 //////////////////////////////////////////////////////////////////////////////// |
| 97 // CompactLocationBarView private: |
| 98 |
| 99 Browser* CompactLocationBarView::browser() const { |
| 100 return host()->browser_view()->browser(); |
| 101 } |
| 102 |
| 103 void CompactLocationBarView::Init() { |
| 104 DCHECK(!initialized_); |
| 105 initialized_ = true; |
| 106 |
| 107 // Use a larger version of the system font. |
| 108 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 109 font_ = rb.GetFont(ResourceBundle::MediumFont); |
| 110 |
| 111 // Location bar. |
| 112 location_bar_view_ = new LocationBarView( |
| 113 browser()->profile(), |
| 114 browser()->command_updater(), |
| 115 browser()->toolbar_model(), |
| 116 clb_host(), |
| 117 LocationBarView::NORMAL); |
| 118 |
| 119 // Reload button. |
| 120 reload_button_ = new ReloadButton(location_bar_view_, browser()); |
| 121 reload_button_->set_triggerable_event_flags(ui::EF_LEFT_BUTTON_DOWN | |
| 122 ui::EF_MIDDLE_BUTTON_DOWN); |
| 123 reload_button_->set_tag(IDC_RELOAD); |
| 124 reload_button_->SetTooltipText( |
| 125 UTF16ToWide(l10n_util::GetStringUTF16(IDS_TOOLTIP_RELOAD))); |
| 126 reload_button_->SetAccessibleName( |
| 127 l10n_util::GetStringUTF16(IDS_ACCNAME_RELOAD)); |
| 128 reload_button_->SetID(VIEW_ID_RELOAD_BUTTON); |
| 129 |
| 130 ThemeProvider* tp = GetThemeProvider(); |
| 131 reload_button_->SetImage(views::CustomButton::BS_NORMAL, |
| 132 tp->GetBitmapNamed(IDR_RELOAD)); |
| 133 reload_button_->SetImage(views::CustomButton::BS_HOT, |
| 134 tp->GetBitmapNamed(IDR_RELOAD_H)); |
| 135 reload_button_->SetImage(views::CustomButton::BS_PUSHED, |
| 136 tp->GetBitmapNamed(IDR_RELOAD_P)); |
| 137 reload_button_->SetToggledImage(views::CustomButton::BS_NORMAL, |
| 138 tp->GetBitmapNamed(IDR_STOP)); |
| 139 reload_button_->SetToggledImage(views::CustomButton::BS_HOT, |
| 140 tp->GetBitmapNamed(IDR_STOP_H)); |
| 141 reload_button_->SetToggledImage(views::CustomButton::BS_PUSHED, |
| 142 tp->GetBitmapNamed(IDR_STOP_P)); |
| 143 reload_button_->SetToggledImage(views::CustomButton::BS_DISABLED, |
| 144 tp->GetBitmapNamed(IDR_STOP_D)); |
| 145 |
| 146 // Always add children in order from left to right, for accessibility. |
| 147 AddChildView(reload_button_); |
| 148 AddChildView(location_bar_view_); |
| 149 location_bar_view_->Init(); |
| 150 |
| 151 SetDialogBorderBitmaps(rb.GetBitmapNamed(IDR_CNAV_DIALOG_LEFT), |
| 152 rb.GetBitmapNamed(IDR_CNAV_DIALOG_MIDDLE), |
| 153 rb.GetBitmapNamed(IDR_CNAV_DIALOG_RIGHT)); |
| 154 } |
| 155 |
| 156 //////////////////////////////////////////////////////////////////////////////// |
| 157 // views::View overrides: |
| 158 |
| 159 gfx::Size CompactLocationBarView::GetPreferredSize() { |
| 160 if (!reload_button_) |
| 161 return gfx::Size(); // Not initialized yet, do nothing. |
| 162 |
| 163 gfx::Size reload_size = reload_button_->GetPreferredSize(); |
| 164 gfx::Size location_size = location_bar_view_->GetPreferredSize(); |
| 165 int width = kCompactLocationLeftMargin + reload_size.width() + |
| 166 std::max(kDefaultLocationEntryWidth, |
| 167 location_bar_view_->GetPreferredSize().width()) + |
| 168 kCompactLocationRightMargin; |
| 169 return gfx::Size(width, kDefaultLocationBarHeight); |
| 170 } |
| 171 |
| 172 void CompactLocationBarView::OnPaint(gfx::Canvas* canvas) { |
| 173 // TODO(stevet): A lot of this method is copied almost directly from |
| 174 // FindBarView. Perhaps we can share it in the common parent class. |
| 175 SkPaint paint; |
| 176 |
| 177 gfx::Rect bounds = PaintOffsetToolbarBackground(canvas); |
| 178 |
| 179 // Now flip the canvas for the rest of the graphics if in RTL mode. |
| 180 canvas->Save(); |
| 181 if (base::i18n::IsRTL()) { |
| 182 canvas->TranslateInt(width(), 0); |
| 183 canvas->ScaleInt(-1, 1); |
| 184 } |
| 185 |
| 186 PaintDialogBorder(canvas, bounds); |
| 187 |
| 188 PaintAnimatingEdges(canvas, bounds); |
| 189 |
| 190 canvas->Restore(); |
| 191 } |
| 192 |
| 193 void CompactLocationBarView::Layout() { |
| 194 if (!reload_button_) |
| 195 return; // Not initialized yet, do nothing. |
| 196 |
| 197 int cur_x = kCompactLocationLeftMargin; |
| 198 |
| 199 // Vertically center all items, basing off the reload button. |
| 200 gfx::Size reload_size = reload_button_->GetPreferredSize(); |
| 201 int y = (height() - reload_size.height()) / 2; |
| 202 reload_button_->SetBounds(cur_x, y, |
| 203 reload_size.width(), reload_size.height()); |
| 204 cur_x += reload_size.width() + kEntryPadding; |
| 205 |
| 206 int location_view_width = width() - cur_x - kCompactLocationRightMargin; |
| 207 |
| 208 // The location bar gets the rest of the space in the middle. We assume it has |
| 209 // the same height as the reload button. |
| 210 location_bar_view_->SetBounds(cur_x, y, location_view_width, |
| 211 reload_size.height()); |
| 212 } |
| 213 |
| 214 void CompactLocationBarView::Paint(gfx::Canvas* canvas) { |
| 215 // This paints the background behind the reload button all the way across to |
| 216 // the end of the CLB. Without this, everything comes up transparent. |
| 217 gfx::Rect bounds = GetLocalBounds(); |
| 218 ThemeProvider* tp = GetThemeProvider(); |
| 219 // Now convert from screen to parent coordinates. |
| 220 gfx::Point origin(bounds.origin()); |
| 221 BrowserView* browser_view = host()->browser_view(); |
| 222 ConvertPointToView(NULL, browser_view, &origin); |
| 223 bounds.set_origin(origin); |
| 224 // Finally, calculate the background image tiling offset. |
| 225 origin = browser_view->OffsetPointForToolbarBackgroundImage(origin); |
| 226 |
| 227 canvas->TileImageInt(*tp->GetBitmapNamed(IDR_THEME_TOOLBAR), |
| 228 origin.x(), origin.y(), 0, 0, |
| 229 bounds.width(), bounds.height()); |
| 230 View::Paint(canvas); |
| 231 } |
| 232 |
| 233 void CompactLocationBarView::ViewHierarchyChanged(bool is_add, |
| 234 View* parent, |
| 235 View* child) { |
| 236 if (is_add && child == this && !initialized_) { |
| 237 Init(); |
| 238 } |
| 239 } |
| 240 |
| 241 void CompactLocationBarView::Focus() { |
| 242 location_bar_view_->SetFocusAndSelection(false); |
| 243 } |
| 244 |
| 245 //////////////////////////////////////////////////////////////////////////////// |
| 246 // views::ButtonListener overrides: |
| 247 |
| 248 void CompactLocationBarView::ButtonPressed(views::Button* sender, |
| 249 const views::Event& event) { |
| 250 int id = sender->tag(); |
| 251 int flags = sender->mouse_event_flags(); |
| 252 // Shift-clicking or ctrl-clicking the reload button means we should |
| 253 // ignore any cached content. |
| 254 // TODO(avayvod): eliminate duplication of this logic in |
| 255 // CompactLocationBarView. |
| 256 if (id == IDC_RELOAD && (event.IsShiftDown() || event.IsControlDown())) { |
| 257 id = IDC_RELOAD_IGNORING_CACHE; |
| 258 // Mask off shift/ctrl so they aren't interpreted as affecting the |
| 259 // disposition below. |
| 260 flags &= ~(ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN); |
| 261 } |
| 262 browser()->ExecuteCommandWithDisposition( |
| 263 id, event_utils::DispositionFromEventFlags(flags)); |
| 264 } |
| 265 |
| 266 //////////////////////////////////////////////////////////////////////////////// |
| 267 // AutocompleteEditController overrides: |
| 268 |
| 269 void CompactLocationBarView::OnAutocompleteAccept( |
| 270 const GURL& url, |
| 271 WindowOpenDisposition disposition, |
| 272 PageTransition::Type transition, |
| 273 const GURL& alternate_nav_url) { |
| 274 browser()->OpenURL(url, GURL(), disposition, transition); |
| 275 clb_host()->StartAutoHideTimer(); |
| 276 } |
| 277 |
| 278 void CompactLocationBarView::OnChanged() { |
| 279 // TODO(stevet): Once we put in a location icon, we should resurrect this code |
| 280 // to update the icon. |
| 281 // location_icon_view_->SetImage( |
| 282 // ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 283 // location_entry_->GetIcon())); |
| 284 // location_icon_view_->ShowTooltip(!location_entry()->IsEditingOrEmpty()); |
| 285 |
| 286 Layout(); |
| 287 SchedulePaint(); |
| 288 } |
| 289 |
| 290 void CompactLocationBarView::OnSelectionBoundsChanged() { |
| 291 // TODO(stevet): LocationBarView, for OS_WIN, uses SuggestedTextView here. |
| 292 // We should implement this usage eventually, if appropriate. |
| 293 } |
| 294 |
| 295 void CompactLocationBarView::OnKillFocus() { |
| 296 host()->UnregisterAccelerators(); |
| 297 } |
| 298 |
| 299 void CompactLocationBarView::OnSetFocus() { |
| 300 clb_host()->CancelAutoHideTimer(); |
| 301 views::FocusManager* focus_manager = GetFocusManager(); |
| 302 if (!focus_manager) { |
| 303 NOTREACHED(); |
| 304 return; |
| 305 } |
| 306 focus_manager->SetFocusedView(this); |
| 307 host()->RegisterAccelerators(); |
| 308 } |
| 309 |
| 310 void CompactLocationBarView::OnInputInProgress(bool in_progress) { |
| 311 } |
| 312 |
| 313 SkBitmap CompactLocationBarView::GetFavicon() const { |
| 314 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper(); |
| 315 return wrapper ? wrapper->favicon_tab_helper()->GetFavicon() : SkBitmap(); |
| 316 } |
| 317 |
| 318 string16 CompactLocationBarView::GetTitle() const { |
| 319 const TabContentsWrapper* wrapper = |
| 320 browser()->GetSelectedTabContentsWrapper(); |
| 321 return wrapper ? wrapper->tab_contents()->GetTitle() : string16(); |
| 322 } |
| 323 |
| 324 InstantController* CompactLocationBarView::GetInstant() { |
| 325 // TODO(stevet): Re-enable instant for compact nav. |
| 326 // return browser()->instant(); |
| 327 return NULL; |
| 328 } |
| 329 |
| 330 TabContentsWrapper* CompactLocationBarView::GetTabContentsWrapper() const { |
| 331 return browser()->GetSelectedTabContentsWrapper(); |
| 332 } |
| OLD | NEW |