OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/gtk/browser_toolbar_gtk.h" | 5 #include "chrome/browser/gtk/browser_toolbar_gtk.h" |
6 | 6 |
7 #include <gdk/gdkkeysyms.h> | 7 #include <gdk/gdkkeysyms.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/base_paths_linux.h" | 10 #include "base/base_paths_linux.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "chrome/app/chrome_dll_resource.h" | 12 #include "chrome/app/chrome_dll_resource.h" |
13 #include "chrome/browser/browser.h" | 13 #include "chrome/browser/browser.h" |
| 14 #include "chrome/browser/gtk/back_forward_menu_model_gtk.h" |
14 #include "chrome/browser/gtk/custom_button.h" | 15 #include "chrome/browser/gtk/custom_button.h" |
15 #include "chrome/browser/gtk/back_forward_menu_model_gtk.h" | 16 #include "chrome/browser/gtk/location_bar_view_gtk.h" |
16 #include "chrome/browser/gtk/standard_menus.h" | 17 #include "chrome/browser/gtk/standard_menus.h" |
17 #include "chrome/browser/net/url_fixer_upper.h" | 18 #include "chrome/browser/net/url_fixer_upper.h" |
18 #include "chrome/common/l10n_util.h" | 19 #include "chrome/common/l10n_util.h" |
19 #include "chrome/common/resource_bundle.h" | 20 #include "chrome/common/resource_bundle.h" |
20 #include "grit/chromium_strings.h" | 21 #include "grit/chromium_strings.h" |
21 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
22 #include "grit/theme_resources.h" | 23 #include "grit/theme_resources.h" |
23 | 24 |
24 // TODO(deanm): Remove this when the LocationBarView is used. | |
25 class LocationBar; | |
26 | |
27 const int BrowserToolbarGtk::kToolbarHeight = 38; | 25 const int BrowserToolbarGtk::kToolbarHeight = 38; |
28 // For the back/forward dropdown menus, the time in milliseconds between | 26 // For the back/forward dropdown menus, the time in milliseconds between |
29 // when the user clicks and the popup menu appears. | 27 // when the user clicks and the popup menu appears. |
30 static const int kMenuTimerDelay = 500; | 28 static const int kMenuTimerDelay = 500; |
31 | 29 |
| 30 static void OnGrabFocusThunk(GtkWidget* toolbar, gpointer self) { |
| 31 reinterpret_cast<BrowserToolbarGtk*>(self)->FocusLocationBar(); |
| 32 } |
| 33 |
32 BrowserToolbarGtk::BrowserToolbarGtk(Browser* browser) | 34 BrowserToolbarGtk::BrowserToolbarGtk(Browser* browser) |
33 : toolbar_(NULL), | 35 : toolbar_(NULL), |
34 entry_(NULL), | 36 location_bar_(new LocationBarViewGtk(browser->command_updater(), |
| 37 browser->toolbar_model())), |
35 model_(browser->toolbar_model()), | 38 model_(browser->toolbar_model()), |
36 browser_(browser), | 39 browser_(browser), |
37 profile_(NULL), | 40 profile_(NULL), |
38 show_menu_factory_(this) { | 41 show_menu_factory_(this) { |
39 browser_->command_updater()->AddCommandObserver(IDC_BACK, this); | 42 browser_->command_updater()->AddCommandObserver(IDC_BACK, this); |
40 browser_->command_updater()->AddCommandObserver(IDC_FORWARD, this); | 43 browser_->command_updater()->AddCommandObserver(IDC_FORWARD, this); |
41 browser_->command_updater()->AddCommandObserver(IDC_RELOAD, this); | 44 browser_->command_updater()->AddCommandObserver(IDC_RELOAD, this); |
42 browser_->command_updater()->AddCommandObserver(IDC_HOME, this); | 45 browser_->command_updater()->AddCommandObserver(IDC_HOME, this); |
43 browser_->command_updater()->AddCommandObserver(IDC_STAR, this); | 46 browser_->command_updater()->AddCommandObserver(IDC_STAR, this); |
44 | 47 |
45 back_menu_model_.reset(new BackForwardMenuModelGtk( | 48 back_menu_model_.reset(new BackForwardMenuModelGtk( |
46 browser, BackForwardMenuModel::BACKWARD_MENU_DELEGATE)); | 49 browser, BackForwardMenuModel::BACKWARD_MENU_DELEGATE)); |
47 forward_menu_model_.reset(new BackForwardMenuModelGtk( | 50 forward_menu_model_.reset(new BackForwardMenuModelGtk( |
48 browser, BackForwardMenuModel::FORWARD_MENU_DELEGATE)); | 51 browser, BackForwardMenuModel::FORWARD_MENU_DELEGATE)); |
49 } | 52 } |
50 | 53 |
51 BrowserToolbarGtk::~BrowserToolbarGtk() { | 54 BrowserToolbarGtk::~BrowserToolbarGtk() { |
52 } | 55 } |
53 | 56 |
54 void BrowserToolbarGtk::Init(Profile* profile, GtkAccelGroup* accel_group) { | 57 void BrowserToolbarGtk::Init(Profile* profile, GtkAccelGroup* accel_group) { |
| 58 // Make sure to tell the location bar the profile before calling its Init. |
| 59 SetProfile(profile); |
| 60 |
55 accel_group_ = accel_group; | 61 accel_group_ = accel_group; |
56 | 62 |
57 show_home_button_.Init(prefs::kShowHomeButton, profile->GetPrefs(), this); | 63 show_home_button_.Init(prefs::kShowHomeButton, profile->GetPrefs(), this); |
58 | 64 |
59 toolbar_ = gtk_hbox_new(FALSE, 0); | 65 toolbar_ = gtk_hbox_new(FALSE, 0); |
60 gtk_container_set_border_width(GTK_CONTAINER(toolbar_), 4); | 66 gtk_container_set_border_width(GTK_CONTAINER(toolbar_), 4); |
61 // Demand we're always at least kToolbarHeight tall. | 67 // Demand we're always at least kToolbarHeight tall. |
62 // -1 for width means "let GTK do its normal sizing". | 68 // -1 for width means "let GTK do its normal sizing". |
63 gtk_widget_set_size_request(toolbar_, -1, kToolbarHeight); | 69 gtk_widget_set_size_request(toolbar_, -1, kToolbarHeight); |
64 | 70 |
(...skipping 17 matching lines...) Expand all Loading... |
82 // TODO(port): we need to dynamically react to changes in show_home_button_ | 88 // TODO(port): we need to dynamically react to changes in show_home_button_ |
83 // and hide/show home appropriately. But we don't have a UI for it yet. | 89 // and hide/show home appropriately. But we don't have a UI for it yet. |
84 if (*show_home_button_) | 90 if (*show_home_button_) |
85 home_.reset(MakeHomeButton()); | 91 home_.reset(MakeHomeButton()); |
86 | 92 |
87 gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); | 93 gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); |
88 | 94 |
89 star_.reset(BuildToolbarButton(IDR_STAR, IDR_STAR_P, IDR_STAR_H, IDR_STAR_D, | 95 star_.reset(BuildToolbarButton(IDR_STAR, IDR_STAR_P, IDR_STAR_H, IDR_STAR_D, |
90 l10n_util::GetString(IDS_TOOLTIP_STAR))); | 96 l10n_util::GetString(IDS_TOOLTIP_STAR))); |
91 | 97 |
92 entry_ = gtk_entry_new(); | 98 location_bar_->Init(); |
93 gtk_widget_set_size_request(entry_, 0, 27); | 99 gtk_box_pack_start(GTK_BOX(toolbar_), location_bar_->widget(), TRUE, TRUE, 0); |
94 g_signal_connect(G_OBJECT(entry_), "activate", | 100 |
95 G_CALLBACK(OnEntryActivate), this); | 101 // We listen for ctrl-l which we have send a grab-focus action to the |
96 g_signal_connect(G_OBJECT(entry_), "focus", | 102 // toolbar. We want our callback to just call FocusLocationBar(). |
97 G_CALLBACK(OnEntryFocus), this); | 103 g_signal_connect(toolbar_, "grab-focus", |
98 g_signal_connect(G_OBJECT(entry_), "focus-in-event", | 104 G_CALLBACK(OnGrabFocusThunk), this); |
99 G_CALLBACK(OnEntryFocusIn), this); | |
100 g_signal_connect(G_OBJECT(entry_), "focus-out-event", | |
101 G_CALLBACK(OnEntryFocusOut), this); | |
102 gtk_widget_add_accelerator( | 105 gtk_widget_add_accelerator( |
103 entry_, "grab-focus", accel_group_, GDK_l, | 106 toolbar_, "grab-focus", accel_group_, GDK_l, |
104 GDK_CONTROL_MASK, GtkAccelFlags(0)); | 107 GDK_CONTROL_MASK, GtkAccelFlags(0)); |
105 | 108 |
106 gtk_box_pack_start(GTK_BOX(toolbar_), entry_, TRUE, TRUE, 0); | |
107 | |
108 go_.reset(BuildToolbarButton(IDR_GO, IDR_GO_P, IDR_GO_H, 0, L"")); | 109 go_.reset(BuildToolbarButton(IDR_GO, IDR_GO_P, IDR_GO_H, 0, L"")); |
109 | 110 |
110 gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); | 111 gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); |
111 | 112 |
112 page_menu_button_.reset(BuildToolbarMenuButton(IDR_MENU_PAGE, | 113 page_menu_button_.reset(BuildToolbarMenuButton(IDR_MENU_PAGE, |
113 l10n_util::GetString(IDS_PAGEMENU_TOOLTIP))); | 114 l10n_util::GetString(IDS_PAGEMENU_TOOLTIP))); |
114 page_menu_.reset(new MenuGtk(this, GetStandardPageMenu(), accel_group_)); | 115 page_menu_.reset(new MenuGtk(this, GetStandardPageMenu(), accel_group_)); |
115 | 116 |
116 app_menu_button_.reset(BuildToolbarMenuButton(IDR_MENU_CHROME, | 117 app_menu_button_.reset(BuildToolbarMenuButton(IDR_MENU_CHROME, |
117 l10n_util::GetStringF(IDS_APPMENU_TOOLTIP, | 118 l10n_util::GetStringF(IDS_APPMENU_TOOLTIP, |
118 l10n_util::GetString(IDS_PRODUCT_NAME)))); | 119 l10n_util::GetString(IDS_PRODUCT_NAME)))); |
119 app_menu_.reset(new MenuGtk(this, GetStandardAppMenu(), accel_group_)); | 120 app_menu_.reset(new MenuGtk(this, GetStandardAppMenu(), accel_group_)); |
120 | |
121 SetProfile(profile); | |
122 } | 121 } |
123 | 122 |
124 void BrowserToolbarGtk::AddToolbarToBox(GtkWidget* box) { | 123 void BrowserToolbarGtk::AddToolbarToBox(GtkWidget* box) { |
125 gtk_box_pack_start(GTK_BOX(box), toolbar_, FALSE, FALSE, 0); | 124 gtk_box_pack_start(GTK_BOX(box), toolbar_, FALSE, FALSE, 0); |
126 } | 125 } |
127 | 126 |
128 LocationBar* BrowserToolbarGtk::GetLocationBar() const { | 127 LocationBar* BrowserToolbarGtk::GetLocationBar() const { |
129 NOTIMPLEMENTED(); | 128 return location_bar_.get(); |
130 return NULL; | |
131 } | 129 } |
132 | 130 |
133 void BrowserToolbarGtk::FocusLocationBar() { | 131 void BrowserToolbarGtk::FocusLocationBar() { |
134 gtk_widget_grab_focus(entry_); | 132 location_bar_->FocusLocation(); |
135 } | 133 } |
136 | 134 |
137 void BrowserToolbarGtk::EnabledStateChangedForCommand(int id, bool enabled) { | 135 void BrowserToolbarGtk::EnabledStateChangedForCommand(int id, bool enabled) { |
138 GtkWidget* widget = NULL; | 136 GtkWidget* widget = NULL; |
139 switch (id) { | 137 switch (id) { |
140 case IDC_BACK: | 138 case IDC_BACK: |
141 widget = back_->widget(); | 139 widget = back_->widget(); |
142 break; | 140 break; |
143 case IDC_FORWARD: | 141 case IDC_FORWARD: |
144 widget = forward_->widget(); | 142 widget = forward_->widget(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 NOTIMPLEMENTED(); | 186 NOTIMPLEMENTED(); |
189 } | 187 } |
190 } | 188 } |
191 } | 189 } |
192 | 190 |
193 void BrowserToolbarGtk::SetProfile(Profile* profile) { | 191 void BrowserToolbarGtk::SetProfile(Profile* profile) { |
194 if (profile == profile_) | 192 if (profile == profile_) |
195 return; | 193 return; |
196 | 194 |
197 profile_ = profile; | 195 profile_ = profile; |
198 // TODO(erg): location_bar_ is a normal gtk text box right now. Change this | 196 location_bar_->SetProfile(profile); |
199 // when we get omnibox support. | |
200 // location_bar_->SetProfile(profile); | |
201 } | 197 } |
202 | 198 |
203 void BrowserToolbarGtk::UpdateTabContents(TabContents* contents, | 199 void BrowserToolbarGtk::UpdateTabContents(TabContents* contents, |
204 bool should_restore_state) { | 200 bool should_restore_state) { |
205 // Extract the UTF-8 representation of the URL. | 201 location_bar_->Update(should_restore_state ? contents : NULL); |
206 gtk_entry_set_text(GTK_ENTRY(entry_), | |
207 contents->GetURL().possibly_invalid_spec().c_str()); | |
208 } | 202 } |
209 | 203 |
210 CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton( | 204 CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton( |
211 int normal_id, int active_id, int highlight_id, int depressed_id, | 205 int normal_id, int active_id, int highlight_id, int depressed_id, |
212 const std::wstring& localized_tooltip) { | 206 const std::wstring& localized_tooltip) { |
213 CustomDrawButton* button = new CustomDrawButton(normal_id, active_id, | 207 CustomDrawButton* button = new CustomDrawButton(normal_id, active_id, |
214 highlight_id, depressed_id); | 208 highlight_id, depressed_id); |
215 | 209 |
216 gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tooltips_), | 210 gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tooltips_), |
217 GTK_WIDGET(button->widget()), | 211 GTK_WIDGET(button->widget()), |
(...skipping 24 matching lines...) Expand all Loading... |
242 g_signal_connect(G_OBJECT(button->widget()), "button-press-event", | 236 g_signal_connect(G_OBJECT(button->widget()), "button-press-event", |
243 G_CALLBACK(OnMenuButtonPressEvent), this); | 237 G_CALLBACK(OnMenuButtonPressEvent), this); |
244 GTK_WIDGET_UNSET_FLAGS(button->widget(), GTK_CAN_FOCUS); | 238 GTK_WIDGET_UNSET_FLAGS(button->widget(), GTK_CAN_FOCUS); |
245 | 239 |
246 gtk_box_pack_start(GTK_BOX(toolbar_), button->widget(), FALSE, FALSE, 0); | 240 gtk_box_pack_start(GTK_BOX(toolbar_), button->widget(), FALSE, FALSE, 0); |
247 | 241 |
248 return button; | 242 return button; |
249 } | 243 } |
250 | 244 |
251 // static | 245 // static |
252 void BrowserToolbarGtk::OnEntryActivate(GtkEntry *entry, | |
253 BrowserToolbarGtk* toolbar) { | |
254 GURL dest(URLFixerUpper::FixupURL(std::string(gtk_entry_get_text(entry)), | |
255 std::string())); | |
256 toolbar->browser_->GetSelectedTabContents()-> | |
257 OpenURL(dest, GURL(), CURRENT_TAB, PageTransition::TYPED); | |
258 } | |
259 | |
260 // static | |
261 gboolean BrowserToolbarGtk::OnEntryFocus(GtkWidget* widget, | |
262 GtkDirectionType direction, | |
263 BrowserToolbarGtk* host) { | |
264 if (!GTK_WIDGET_HAS_FOCUS(widget)) { | |
265 gtk_widget_grab_focus(widget); | |
266 return TRUE; | |
267 } | |
268 | |
269 return FALSE; | |
270 } | |
271 | |
272 // static | |
273 gboolean BrowserToolbarGtk::OnEntryFocusIn(GtkWidget* widget, | |
274 GdkEventFocus* focus, | |
275 BrowserToolbarGtk* host) { | |
276 // Set the caret at the end of the text. | |
277 gtk_editable_set_position(GTK_EDITABLE(widget), -1); | |
278 return FALSE; | |
279 } | |
280 | |
281 // static | |
282 gboolean BrowserToolbarGtk::OnEntryFocusOut(GtkWidget* widget, | |
283 GdkEventFocus* focus, | |
284 BrowserToolbarGtk* host) { | |
285 // Clear the selected text (if any). | |
286 gtk_editable_set_position(GTK_EDITABLE(widget), 0); | |
287 return FALSE; | |
288 } | |
289 | |
290 // static | |
291 void BrowserToolbarGtk::OnButtonClick(GtkWidget* button, | 246 void BrowserToolbarGtk::OnButtonClick(GtkWidget* button, |
292 BrowserToolbarGtk* toolbar) { | 247 BrowserToolbarGtk* toolbar) { |
293 int tag = -1; | 248 int tag = -1; |
294 if (button == toolbar->back_->widget()) | 249 if (button == toolbar->back_->widget()) |
295 tag = IDC_BACK; | 250 tag = IDC_BACK; |
296 else if (button == toolbar->forward_->widget()) | 251 else if (button == toolbar->forward_->widget()) |
297 tag = IDC_FORWARD; | 252 tag = IDC_FORWARD; |
298 else if (button == toolbar->reload_->widget()) | 253 else if (button == toolbar->reload_->widget()) |
299 tag = IDC_RELOAD; | 254 tag = IDC_RELOAD; |
300 else if (button == toolbar->go_->widget()) | 255 else if (button == toolbar->go_->widget()) |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 } | 357 } |
403 | 358 |
404 void BrowserToolbarGtk::RunAppMenu(GdkEvent* button_press_event) { | 359 void BrowserToolbarGtk::RunAppMenu(GdkEvent* button_press_event) { |
405 app_menu_->Popup(app_menu_button_->widget(), button_press_event); | 360 app_menu_->Popup(app_menu_button_->widget(), button_press_event); |
406 } | 361 } |
407 | 362 |
408 CustomDrawButton* BrowserToolbarGtk::MakeHomeButton() { | 363 CustomDrawButton* BrowserToolbarGtk::MakeHomeButton() { |
409 return BuildToolbarButton(IDR_HOME, IDR_HOME_P, IDR_HOME_H, 0, | 364 return BuildToolbarButton(IDR_HOME, IDR_HOME_P, IDR_HOME_H, 0, |
410 l10n_util::GetString(IDS_TOOLTIP_HOME)); | 365 l10n_util::GetString(IDS_TOOLTIP_HOME)); |
411 } | 366 } |
OLD | NEW |