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 #ifndef CHROME_BROWSER_UI_GTK_BROWSER_TOOLBAR_GTK_H_ 6 #define CHROME_BROWSER_UI_GTK_BROWSER_TOOLBAR_GTK_H_ 7 #pragma once 8 9 #include <gtk/gtk.h> 10 #include <string> 11 12 #include "base/memory/scoped_ptr.h" 13 #include "chrome/browser/command_updater.h" 14 #include "chrome/browser/prefs/pref_member.h" 15 #include "chrome/browser/ui/gtk/custom_button.h" 16 #include "chrome/browser/ui/gtk/menu_gtk.h" 17 #include "chrome/browser/ui/gtk/owned_widget_gtk.h" 18 #include "chrome/browser/ui/toolbar/wrench_menu_model.h" 19 #include "content/common/notification_observer.h" 20 #include "content/common/notification_registrar.h" 21 #include "ui/base/gtk/gtk_signal.h" 22 #include "ui/base/gtk/gtk_signal_registrar.h" 23 #include "ui/base/models/accelerator.h" 24 #include "ui/base/models/simple_menu_model.h" 25 26 class BackForwardButtonGtk; 27 class Browser; 28 class BrowserActionsToolbarGtk; 29 class BrowserWindowGtk; 30 class CustomDrawButton; 31 class GtkThemeService; 32 class LocationBar; 33 class LocationBarViewGtk; 34 class Profile; 35 class ReloadButtonGtk; 36 class TabContents; 37 class ToolbarModel; 38 39 // View class that displays the GTK version of the toolbar and routes gtk 40 // events back to the Browser. 41 class BrowserToolbarGtk : public CommandUpdater::CommandObserver, 42 public ui::AcceleratorProvider, 43 public MenuGtk::Delegate, 44 public NotificationObserver { 45 public: 46 explicit BrowserToolbarGtk(Browser* browser, BrowserWindowGtk* window); 47 virtual ~BrowserToolbarGtk(); 48 49 // Create the contents of the toolbar. |top_level_window| is the GtkWindow 50 // to which we attach our accelerators. 51 void Init(Profile* profile, GtkWindow* top_level_window); 52 53 // Set the various widgets' ViewIDs. 54 void SetViewIDs(); 55 56 void Show(); 57 void Hide(); 58 59 // Getter for the containing widget. 60 GtkWidget* widget() { 61 return event_box_; 62 } 63 64 // Getter for associated browser object. 65 Browser* browser() { 66 return browser_; 67 } 68 69 virtual LocationBar* GetLocationBar() const; 70 71 ReloadButtonGtk* GetReloadButton() { return reload_.get(); } 72 73 GtkWidget* GetAppMenuButton() { return wrench_menu_button_->widget(); } 74 75 BrowserActionsToolbarGtk* GetBrowserActionsToolbar() { 76 return actions_toolbar_.get(); 77 } 78 79 LocationBarViewGtk* GetLocationBarView() { return location_bar_.get(); } 80 81 // We have to show padding on the bottom of the toolbar when the bookmark 82 // is in floating mode. Otherwise the bookmark bar will paint it for us. 83 void UpdateForBookmarkBarVisibility(bool show_bottom_padding); 84 85 void ShowAppMenu(); 86 87 // Overridden from CommandUpdater::CommandObserver: 88 virtual void EnabledStateChangedForCommand(int id, bool enabled); 89 90 // Overridden from MenuGtk::Delegate: 91 virtual void StoppedShowing(); 92 virtual GtkIconSet* GetIconSetForId(int idr); 93 virtual bool AlwaysShowIconForCmd(int command_id) const; 94 95 // Overridden from ui::AcceleratorProvider: 96 virtual bool GetAcceleratorForCommandId(int id, 97 ui::Accelerator* accelerator); 98 99 // NotificationObserver implementation. 100 virtual void Observe(NotificationType type, 101 const NotificationSource& source, 102 const NotificationDetails& details); 103 104 Profile* profile() { return profile_; } 105 void SetProfile(Profile* profile); 106 107 // Message that we should react to a state change. 108 void UpdateTabContents(TabContents* contents, bool should_restore_state); 109 110 private: 111 // Connect/Disconnect signals for dragging a url onto the home button. 112 void SetUpDragForHomeButton(bool enable); 113 114 // Sets the top corners of the toolbar to rounded, or sets them to normal, 115 // depending on the state of the browser window. Returns false if no action 116 // was taken (the roundedness was already correct), true otherwise. 117 bool UpdateRoundedness(); 118 119 // Gtk callback for the "expose-event" signal. 120 // The alignment contains the toolbar. 121 CHROMEGTK_CALLBACK_1(BrowserToolbarGtk, gboolean, OnAlignmentExpose, 122 GdkEventExpose*); 123 CHROMEGTK_CALLBACK_1(BrowserToolbarGtk, gboolean, OnLocationHboxExpose, 124 GdkEventExpose*); 125 126 // Gtk callback for the "clicked" signal. 127 CHROMEGTK_CALLBACK_0(BrowserToolbarGtk, void, OnButtonClick); 128 129 // Gtk callback to intercept mouse clicks to the menu buttons. 130 CHROMEGTK_CALLBACK_1(BrowserToolbarGtk, gboolean, OnMenuButtonPressEvent, 131 GdkEventButton*); 132 133 // Used for drags onto home button. 134 CHROMEGTK_CALLBACK_6(BrowserToolbarGtk, void, OnDragDataReceived, 135 GdkDragContext*, gint, gint, GtkSelectionData*, 136 guint, guint); 137 138 // Used to draw the upgrade notification badge. 139 CHROMEGTK_CALLBACK_1(BrowserToolbarGtk, gboolean, OnWrenchMenuButtonExpose, 140 GdkEventExpose*); 141 142 // Updates preference-dependent state. 143 void NotifyPrefChanged(const std::string* pref); 144 145 static void SetSyncMenuLabel(GtkWidget* widget, gpointer userdata); 146 147 // Sometimes we only want to show the location w/o the toolbar buttons (e.g., 148 // in a popup window). 149 bool ShouldOnlyShowLocation() const; 150 151 // An event box that holds |toolbar_|. We need the toolbar to have its own 152 // GdkWindow when we use the GTK drawing because otherwise the color from our 153 // parent GdkWindow will leak through with some theme engines (such as 154 // Clearlooks). 155 GtkWidget* event_box_; 156 157 // This widget handles padding around the outside of the toolbar. 158 GtkWidget* alignment_; 159 160 // Gtk widgets. The toolbar is an hbox with each of the other pieces of the 161 // toolbar placed side by side. 162 GtkWidget* toolbar_; 163 164 // All widgets to the left or right of the |location_hbox_|. We put the 165 // widgets on either side of location_hbox_ in their own toolbar so we can 166 // set their minimum sizes independently of |location_hbox_| which needs to 167 // grow/shrink in GTK+ mode. 168 GtkWidget* toolbar_left_; 169 170 // Contains all the widgets of the location bar. 171 GtkWidget* location_hbox_; 172 173 // The location bar view. 174 scoped_ptr<LocationBarViewGtk> location_bar_; 175 176 // All the buttons in the toolbar. 177 scoped_ptr<BackForwardButtonGtk> back_, forward_; 178 scoped_ptr<CustomDrawButton> home_; 179 scoped_ptr<ReloadButtonGtk> reload_; 180 scoped_ptr<BrowserActionsToolbarGtk> actions_toolbar_; 181 scoped_ptr<CustomDrawButton> wrench_menu_button_; 182 183 // The image shown in GTK+ mode in the wrench button. 184 GtkWidget* wrench_menu_image_; 185 186 // The model that contains the security level, text, icon to display... 187 ToolbarModel* model_; 188 189 GtkThemeService* theme_service_; 190 191 scoped_ptr<MenuGtk> wrench_menu_; 192 193 WrenchMenuModel wrench_menu_model_; 194 195 Browser* browser_; 196 BrowserWindowGtk* window_; 197 Profile* profile_; 198 199 // Controls whether or not a home button should be shown on the toolbar. 200 BooleanPrefMember show_home_button_; 201 202 // Preferences controlling the configured home page. 203 StringPrefMember home_page_; 204 BooleanPrefMember home_page_is_new_tab_page_; 205 206 NotificationRegistrar registrar_; 207 208 // A GtkEntry that isn't part of the hierarchy. We keep this for native 209 // rendering. 210 OwnedWidgetGtk offscreen_entry_; 211 212 // Manages the home button drop signal handler. 213 scoped_ptr<ui::GtkSignalRegistrar> drop_handler_; 214 215 DISALLOW_COPY_AND_ASSIGN(BrowserToolbarGtk); 216 }; 217 218 #endif // CHROME_BROWSER_UI_GTK_BROWSER_TOOLBAR_GTK_H_ 219