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