Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(560)

Side by Side Diff: chrome/browser/ui/views/compact_nav/compact_location_bar_view.cc

Issue 6913026: Compact Navigation prototype (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Added missing DropdownBarHostDelegate header file. Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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))
sky 2011/05/09 14:27:22 spacing is off
SteveT 2011/05/09 19:40:04 Done.
84 return false;
85
86 location_bar_view_->SetShowFocusRect(true);
87 return true;
88 }
89
90 void CompactLocationBarView::GetAccessibleState(
91 ui::AccessibleViewState* state) {
sky 2011/05/09 14:27:22 spacing of 91-93 is off
SteveT 2011/05/09 19:40:04 Done.
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 // 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 SetDialogBorderBitmaps(rb.GetBitmapNamed(IDR_CNAV_DIALOG_LEFT),
149 rb.GetBitmapNamed(IDR_CNAV_DIALOG_MIDDLE),
150 rb.GetBitmapNamed(IDR_CNAV_DIALOG_RIGHT));
151 }
152
153 ////////////////////////////////////////////////////////////////////////////////
154 // views::View overrides:
155
156 gfx::Size CompactLocationBarView::GetPreferredSize() {
157 if (!reload_button_)
158 return gfx::Size(); // Not initialized yet, do nothing.
159
160 gfx::Size reload_size = reload_button_->GetPreferredSize();
161 gfx::Size location_size = location_bar_view_->GetPreferredSize();
162 int width = kCompactLocationLeftMargin + reload_size.width() +
163 std::max(kDefaultLocationEntryWidth,
164 location_bar_view_->GetPreferredSize().width()) +
165 kCompactLocationRightMargin;
166 return gfx::Size(width, kDefaultLocationBarHeight);
167 }
168
169 void CompactLocationBarView::OnPaint(gfx::Canvas* canvas) {
170 // TODO(stevet): A lot of this method is copied almost directly from
171 // FindBarView. Perhaps we can share it in the common parent class.
172 SkPaint paint;
173
174 gfx::Rect bounds = PaintOffsetToolbarBackground(canvas);
175
176 // Now flip the canvas for the rest of the graphics if in RTL mode.
177 canvas->Save();
178 if (base::i18n::IsRTL()) {
179 canvas->TranslateInt(width(), 0);
180 canvas->ScaleInt(-1, 1);
181 }
182
183 PaintDialogBorder(canvas, bounds);
184
185 PaintAnimatingEdges(canvas, bounds);
186
187 canvas->Restore();
188 }
189
190 void CompactLocationBarView::Layout() {
191 if (!reload_button_)
192 return; // Not initialized yet, do nothing.
193
194 int cur_x = kCompactLocationLeftMargin;
195
196 // Vertically center all items, basing off the reload button.
197 gfx::Size reload_size = reload_button_->GetPreferredSize();
198 int y = (height() - reload_size.height()) / 2;
199 reload_button_->SetBounds(cur_x, y,
200 reload_size.width(), reload_size.height());
201 cur_x += reload_size.width() + kEntryPadding;
202
203 int location_view_width = width() - cur_x - kCompactLocationRightMargin;
204
205 // The location bar gets the rest of the space in the middle. We assume it has
206 // the same height as the reload button.
207 location_bar_view_->SetBounds(cur_x, y, location_view_width,
208 reload_size.height());
209 }
210
211 void CompactLocationBarView::Paint(gfx::Canvas* canvas) {
212 // This paints the background behind the reload button all the way across to
213 // the end of the CLB. Without this, everything comes up transparent.
214 gfx::Rect bounds = GetLocalBounds();
215 ThemeProvider* tp = GetThemeProvider();
216 // Now convert from screen to parent coordinates.
217 gfx::Point origin(bounds.origin());
218 BrowserView* browser_view = host()->browser_view();
219 ConvertPointToView(NULL, browser_view, &origin);
220 bounds.set_origin(origin);
221 // Finally, calculate the background image tiling offset.
222 origin = browser_view->OffsetPointForToolbarBackgroundImage(origin);
223
224 canvas->TileImageInt(*tp->GetBitmapNamed(IDR_THEME_TOOLBAR),
225 origin.x(), origin.y(), 0, 0,
226 bounds.width(), bounds.height());
227 View::Paint(canvas);
228 }
229
230 void CompactLocationBarView::ViewHierarchyChanged(bool is_add,
231 View* parent,
232 View* child) {
233 if (is_add && child == this && !initialized_) {
234 Init();
235 initialized_ = true;
sky 2011/05/09 14:27:22 Set this in Init, not here.
SteveT 2011/05/09 19:40:04 Done - and added DCHECK.
236 }
237 }
238
239 void CompactLocationBarView::Focus() {
240 location_bar_view_->SetFocusAndSelection(false);
241 }
242
243 ////////////////////////////////////////////////////////////////////////////////
244 // views::ButtonListener overrides:
245
246 void CompactLocationBarView::ButtonPressed(views::Button* sender,
247 const views::Event& event) {
248 int id = sender->tag();
249 int flags = sender->mouse_event_flags();
250 // Shift-clicking or ctrl-clicking the reload button means we should
251 // ignore any cached content.
252 // TODO(avayvod): eliminate duplication of this logic in
253 // CompactLocationBarView.
254 if (id == IDC_RELOAD && (event.IsShiftDown() || event.IsControlDown())) {
255 id = IDC_RELOAD_IGNORING_CACHE;
256 // Mask off shift/ctrl so they aren't interpreted as affecting the
257 // disposition below.
258 flags &= ~(ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
259 }
260 browser()->ExecuteCommandWithDisposition(
261 id, event_utils::DispositionFromEventFlags(flags));
262 }
263
264 ////////////////////////////////////////////////////////////////////////////////
265 // AutocompleteEditController overrides:
266
267 void CompactLocationBarView::OnAutocompleteAccept(
268 const GURL& url,
269 WindowOpenDisposition disposition,
270 PageTransition::Type transition,
271 const GURL& alternate_nav_url) {
272 browser()->OpenURL(url, GURL(), disposition, transition);
273 clb_host()->StartAutoHideTimer();
274 }
275
276 void CompactLocationBarView::OnChanged() {
277 // TODO(stevet): Once we put in a location icon, we should resurrect this code
278 // to update the icon.
279 // location_icon_view_->SetImage(
280 // ResourceBundle::GetSharedInstance().GetBitmapNamed(
281 // location_entry_->GetIcon()));
282 // location_icon_view_->ShowTooltip(!location_entry()->IsEditingOrEmpty());
283
284 Layout();
285 SchedulePaint();
286 }
287
288 void CompactLocationBarView::OnSelectionBoundsChanged() {
289 // TODO(stevet): LocationBarView, for OS_WIN, uses SuggestedTextView here.
290 // We should implement this usage eventually, if appropriate.
291 }
292
293 void CompactLocationBarView::OnKillFocus() {
294 host()->UnregisterAccelerators();
295 }
296
297 void CompactLocationBarView::OnSetFocus() {
298 clb_host()->CancelAutoHideTimer();
299 views::FocusManager* focus_manager = GetFocusManager();
300 if (!focus_manager) {
301 NOTREACHED();
302 return;
303 }
304 focus_manager->SetFocusedView(this);
305 host()->RegisterAccelerators();
306 }
307
308 void CompactLocationBarView::OnInputInProgress(bool in_progress) {
309 }
310
311 SkBitmap CompactLocationBarView::GetFavicon() const {
312 TabContentsWrapper* wrapper = browser()->GetSelectedTabContentsWrapper();
313 return wrapper ? wrapper->favicon_tab_helper()->GetFavicon() : SkBitmap();
314 }
315
316 string16 CompactLocationBarView::GetTitle() const {
317 const TabContentsWrapper* wrapper =
318 browser()->GetSelectedTabContentsWrapper();
319 return wrapper ? wrapper->tab_contents()->GetTitle() : string16();
320 }
321
322 InstantController* CompactLocationBarView::GetInstant() {
323 // TODO(stevet): Re-enable instant for compact nav.
324 // return browser()->instant();
325 return NULL;
326 }
327
328 TabContentsWrapper* CompactLocationBarView::GetTabContentsWrapper() const {
329 return browser()->GetSelectedTabContentsWrapper();
330 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698