| 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_view_gtk.h" | 5 #include "chrome/browser/gtk/browser_toolbar_view_gtk.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/base_paths_linux.h" | 8 #include "base/base_paths_linux.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "chrome/app/chrome_dll_resource.h" | 10 #include "chrome/app/chrome_dll_resource.h" |
| 11 #include "chrome/browser/browser.h" | 11 #include "chrome/browser/browser.h" |
| 12 #include "chrome/browser/gtk/menu_gtk.h" |
| 13 #include "chrome/browser/gtk/standard_menus.h" |
| 12 #include "chrome/common/l10n_util.h" | 14 #include "chrome/common/l10n_util.h" |
| 13 | 15 |
| 14 #include "chromium_strings.h" | 16 #include "chromium_strings.h" |
| 15 #include "generated_resources.h" | 17 #include "generated_resources.h" |
| 16 | 18 |
| 17 // CustomDrawButton manages the lifetimes of some resources used to make a | 19 // CustomDrawButton manages the lifetimes of some resources used to make a |
| 18 // custom-drawn Gtk button. We use them on the toolbar. | 20 // custom-drawn Gtk button. We use them on the toolbar. |
| 19 class BrowserToolbarGtk::CustomDrawButton { | 21 class BrowserToolbarGtk::CustomDrawButton { |
| 20 public: | 22 public: |
| 21 // The constructor takes a filename, which is used as the base filename | 23 // The constructor takes a filename, which is used as the base filename |
| 22 // in loading the theme graphics pngs. This will be replaced by the | 24 // in loading the theme graphics pngs. This will be replaced by the |
| 23 // ResourceBundle graphics soon. | 25 // ResourceBundle graphics soon. |
| 24 CustomDrawButton(const std::string& filename); | 26 explicit CustomDrawButton(const std::string& filename); |
| 25 ~CustomDrawButton(); | 27 ~CustomDrawButton(); |
| 26 | 28 |
| 27 GtkWidget* widget() const { return widget_; } | 29 GtkWidget* widget() const { return widget_; } |
| 28 | 30 |
| 29 private: | 31 private: |
| 30 // Load an image from a path. | 32 // Load an image from a path. |
| 31 // TODO(port): Temporary until ResourceBundle works. | 33 // TODO(port): Temporary until ResourceBundle works. |
| 32 GdkPixbuf* LoadImage(const std::string& filename); | 34 GdkPixbuf* LoadImage(const std::string& filename); |
| 33 | 35 |
| 34 // Load all the button images from a base theme filename. | 36 // Load all the button images from a base theme filename. |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 BrowserToolbarGtk::~BrowserToolbarGtk() { | 135 BrowserToolbarGtk::~BrowserToolbarGtk() { |
| 134 } | 136 } |
| 135 | 137 |
| 136 void BrowserToolbarGtk::Init(Profile* profile) { | 138 void BrowserToolbarGtk::Init(Profile* profile) { |
| 137 toolbar_ = gtk_hbox_new(FALSE, 0); | 139 toolbar_ = gtk_hbox_new(FALSE, 0); |
| 138 gtk_container_set_border_width(GTK_CONTAINER(toolbar_), 6); | 140 gtk_container_set_border_width(GTK_CONTAINER(toolbar_), 6); |
| 139 | 141 |
| 140 toolbar_tooltips_ = gtk_tooltips_new(); | 142 toolbar_tooltips_ = gtk_tooltips_new(); |
| 141 | 143 |
| 142 back_.reset(BuildToolbarButton("back", | 144 back_.reset(BuildToolbarButton("back", |
| 143 l10n_util::GetString(IDS_TOOLTIP_BACK))); | 145 l10n_util::GetString(IDS_TOOLTIP_BACK), |
| 146 false)); |
| 144 forward_.reset(BuildToolbarButton("forward", | 147 forward_.reset(BuildToolbarButton("forward", |
| 145 l10n_util::GetString(IDS_TOOLTIP_FORWARD))); | 148 l10n_util::GetString(IDS_TOOLTIP_FORWARD), |
| 149 false)); |
| 146 | 150 |
| 147 gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); | 151 gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); |
| 148 | 152 |
| 149 reload_.reset(BuildToolbarButton("reload", | 153 reload_.reset(BuildToolbarButton("reload", |
| 150 l10n_util::GetString(IDS_TOOLTIP_RELOAD))); | 154 l10n_util::GetString(IDS_TOOLTIP_RELOAD), |
| 155 false)); |
| 151 home_.reset(BuildToolbarButton("home", | 156 home_.reset(BuildToolbarButton("home", |
| 152 l10n_util::GetString(IDS_TOOLTIP_HOME))); | 157 l10n_util::GetString(IDS_TOOLTIP_HOME), |
| 158 false)); |
| 153 | 159 |
| 154 gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); | 160 gtk_box_pack_start(GTK_BOX(toolbar_), gtk_label_new(" "), FALSE, FALSE, 0); |
| 155 | 161 |
| 156 star_.reset(BuildToolbarButton("star", | 162 star_.reset(BuildToolbarButton("star", |
| 157 l10n_util::GetString(IDS_TOOLTIP_STAR))); | 163 l10n_util::GetString(IDS_TOOLTIP_STAR), |
| 164 false)); |
| 158 | 165 |
| 159 GtkWidget* entry = gtk_entry_new(); | 166 GtkWidget* entry = gtk_entry_new(); |
| 160 gtk_widget_set_size_request(entry, 0, 27); | 167 gtk_widget_set_size_request(entry, 0, 27); |
| 161 gtk_box_pack_start(GTK_BOX(toolbar_), entry, TRUE, TRUE, 0); | 168 gtk_box_pack_start(GTK_BOX(toolbar_), entry, TRUE, TRUE, 0); |
| 162 | 169 |
| 163 go_.reset(BuildToolbarButton("go", L"")); | 170 go_.reset(BuildToolbarButton("go", L"", false)); |
| 164 | 171 |
| 165 // TODO(port): these buttons need even stranger drawing than the others. | 172 // TODO(port): these buttons need even stranger drawing than the others. |
| 166 page_menu_.reset(BuildToolbarButton("menu_page", | 173 page_menu_button_.reset(BuildToolbarButton("menu_page", |
| 167 l10n_util::GetString(IDS_PAGEMENU_TOOLTIP))); | 174 l10n_util::GetString(IDS_PAGEMENU_TOOLTIP), true)); |
| 168 | 175 |
| 169 // TODO(port): Need to get l10n_util::GetStringF working under linux to get | 176 // TODO(port): Need to get l10n_util::GetStringF working under linux to get |
| 170 // the correct string here. | 177 // the correct string here. |
| 171 app_menu_.reset(BuildToolbarButton("menu_chrome", | 178 app_menu_button_.reset(BuildToolbarButton("menu_chrome", |
| 172 l10n_util::GetString(IDS_APPMENU_TOOLTIP))); | 179 l10n_util::GetString(IDS_APPMENU_TOOLTIP), true)); |
| 173 | 180 |
| 174 // TODO(erg): wchar_t mismatch on linux. Fix later. | 181 // TODO(erg): wchar_t mismatch on linux. Fix later. |
| 175 // show_home_button_.Init(prefs::kShowHomeButton, profile->GetPrefs(), this); | 182 // show_home_button_.Init(prefs::kShowHomeButton, profile->GetPrefs(), this); |
| 176 | 183 |
| 177 SetProfile(profile); | 184 SetProfile(profile); |
| 178 } | 185 } |
| 179 | 186 |
| 180 void BrowserToolbarGtk::AddToolbarToBox(GtkWidget* box) { | 187 void BrowserToolbarGtk::AddToolbarToBox(GtkWidget* box) { |
| 181 gtk_box_pack_start(GTK_BOX(box), toolbar_, FALSE, FALSE, 0); | 188 gtk_box_pack_start(GTK_BOX(box), toolbar_, FALSE, FALSE, 0); |
| 182 } | 189 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 197 widget = home_->widget(); | 204 widget = home_->widget(); |
| 198 break; | 205 break; |
| 199 case IDC_STAR: | 206 case IDC_STAR: |
| 200 widget = star_->widget(); | 207 widget = star_->widget(); |
| 201 break; | 208 break; |
| 202 } | 209 } |
| 203 if (widget) | 210 if (widget) |
| 204 gtk_widget_set_sensitive(widget, enabled); | 211 gtk_widget_set_sensitive(widget, enabled); |
| 205 } | 212 } |
| 206 | 213 |
| 214 bool BrowserToolbarGtk::IsCommandEnabled(int command_id) const { |
| 215 return browser_->command_updater()->IsCommandEnabled(command_id); |
| 216 } |
| 217 |
| 218 bool BrowserToolbarGtk::IsItemChecked(int id) const { |
| 219 if (!profile_) |
| 220 return false; |
| 221 if (id == IDC_SHOW_BOOKMARK_BAR) |
| 222 return profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); |
| 223 // TODO(port): Fix this when we get some items that want checking! |
| 224 return false; |
| 225 } |
| 226 |
| 227 void BrowserToolbarGtk::ExecuteCommand(int id) { |
| 228 browser_->ExecuteCommand(id); |
| 229 } |
| 230 |
| 207 void BrowserToolbarGtk::SetProfile(Profile* profile) { | 231 void BrowserToolbarGtk::SetProfile(Profile* profile) { |
| 208 if (profile == profile_) | 232 if (profile == profile_) |
| 209 return; | 233 return; |
| 210 | 234 |
| 211 profile_ = profile; | 235 profile_ = profile; |
| 212 // TODO(erg): location_bar_ is a normal gtk text box right now. Change this | 236 // TODO(erg): location_bar_ is a normal gtk text box right now. Change this |
| 213 // when we get omnibox support. | 237 // when we get omnibox support. |
| 214 // location_bar_->SetProfile(profile); | 238 // location_bar_->SetProfile(profile); |
| 215 } | 239 } |
| 216 | 240 |
| 217 // TODO(port): This needs to deal with our styled pixmaps. | 241 // TODO(port): This needs to deal with our styled pixmaps. |
| 218 BrowserToolbarGtk::CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton( | 242 BrowserToolbarGtk::CustomDrawButton* BrowserToolbarGtk::BuildToolbarButton( |
| 219 const std::string& filename, | 243 const std::string& filename, |
| 220 const std::wstring& localized_tooltip) { | 244 const std::wstring& localized_tooltip, |
| 245 bool menu_button) { |
| 221 CustomDrawButton* button = new CustomDrawButton(filename); | 246 CustomDrawButton* button = new CustomDrawButton(filename); |
| 222 | 247 |
| 223 // TODO(erg): Mismatch between wstring and string. | 248 // TODO(erg): Mismatch between wstring and string. |
| 224 // gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tooltips_), | 249 // gtk_tooltips_set_tip(GTK_TOOLTIPS(toolbar_tooltips_), |
| 225 // GTK_WIDGET(back_), | 250 // GTK_WIDGET(back_), |
| 226 // localized_tooltip, localized_tooltip); | 251 // localized_tooltip, localized_tooltip); |
| 227 g_signal_connect(G_OBJECT(button->widget()), "clicked", | 252 if (menu_button) { |
| 228 G_CALLBACK(ButtonClickCallback), this); | 253 g_signal_connect(G_OBJECT(button->widget()), "button_press_event", |
| 254 G_CALLBACK(OnMenuButtonPressEvent), this); |
| 255 } else { |
| 256 g_signal_connect(G_OBJECT(button->widget()), "clicked", |
| 257 G_CALLBACK(OnButtonClick), this); |
| 258 } |
| 259 |
| 229 gtk_box_pack_start(GTK_BOX(toolbar_), button->widget(), FALSE, FALSE, 0); | 260 gtk_box_pack_start(GTK_BOX(toolbar_), button->widget(), FALSE, FALSE, 0); |
| 230 | |
| 231 return button; | 261 return button; |
| 232 } | 262 } |
| 233 | 263 |
| 234 /* static */ | 264 /* static */ |
| 235 void BrowserToolbarGtk::ButtonClickCallback(GtkWidget* button, | 265 void BrowserToolbarGtk::OnButtonClick(GtkWidget* button, |
| 236 BrowserToolbarGtk* toolbar) { | 266 BrowserToolbarGtk* toolbar) { |
| 237 int tag = -1; | 267 int tag = -1; |
| 238 if (button == toolbar->back_->widget()) | 268 if (button == toolbar->back_->widget()) |
| 239 tag = IDC_BACK; | 269 tag = IDC_BACK; |
| 240 else if (button == toolbar->forward_->widget()) | 270 else if (button == toolbar->forward_->widget()) |
| 241 tag = IDC_FORWARD; | 271 tag = IDC_FORWARD; |
| 242 else if (button == toolbar->reload_->widget()) | 272 else if (button == toolbar->reload_->widget()) |
| 243 tag = IDC_RELOAD; | 273 tag = IDC_RELOAD; |
| 244 else if (button == toolbar->home_->widget()) | 274 else if (button == toolbar->home_->widget()) |
| 245 tag = IDC_HOME; | 275 tag = IDC_HOME; |
| 246 else if (button == toolbar->star_->widget()) | 276 else if (button == toolbar->star_->widget()) |
| 247 tag = IDC_STAR; | 277 tag = IDC_STAR; |
| 248 | 278 |
| 249 if (tag != -1) { | 279 DCHECK(tag != -1) << "Impossible button click callback"; |
| 250 toolbar->browser_->ExecuteCommand(tag); | 280 toolbar->browser_->ExecuteCommand(tag); |
| 251 } else { | 281 } |
| 252 // TODO(erg): The menu buttons are special; they need to spawn menus. | 282 |
| 283 /* static */ |
| 284 gint BrowserToolbarGtk::OnMenuButtonPressEvent(GtkWidget* button, |
| 285 GdkEvent* event, |
| 286 BrowserToolbarGtk* toolbar) { |
| 287 if (event->type == GDK_BUTTON_PRESS) { |
| 288 GdkEventButton* event_button = reinterpret_cast<GdkEventButton*>(event); |
| 289 if (event_button->button == 1) { |
| 290 // We have a button press we should respond to. |
| 291 if (button == toolbar->page_menu_button_->widget()) { |
| 292 toolbar->RunPageMenu(event); |
| 293 return TRUE; |
| 294 } else if (button == toolbar->app_menu_button_->widget()) { |
| 295 toolbar->RunAppMenu(event); |
| 296 return TRUE; |
| 297 } |
| 298 } |
| 253 } | 299 } |
| 300 |
| 301 return FALSE; |
| 254 } | 302 } |
| 303 |
| 304 void BrowserToolbarGtk::RunPageMenu(GdkEvent* button_press_event) { |
| 305 if (page_menu_ == NULL) { |
| 306 page_menu_.reset(new MenuGtk(this, GetStandardPageMenu())); |
| 307 } |
| 308 |
| 309 page_menu_->Popup(page_menu_button_->widget(), button_press_event); |
| 310 } |
| 311 |
| 312 void BrowserToolbarGtk::RunAppMenu(GdkEvent* button_press_event) { |
| 313 if (app_menu_ == NULL) { |
| 314 app_menu_.reset(new MenuGtk(this, GetStandardAppMenu())); |
| 315 } |
| 316 |
| 317 app_menu_->Popup(app_menu_button_->widget(), button_press_event); |
| 318 } |
| OLD | NEW |