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