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.
5 #ifndef CHROME_BROWSER_UI_GTK_BROWSER_ACTIONS_TOOLBAR_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_BROWSER_ACTIONS_TOOLBAR_GTK_H_
11 #include "base/compiler_specific.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/extensions/extension_toolbar_model.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/overflow_button.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "ui/base/gtk/gtk_signal.h"
21 #include "ui/base/gtk/gtk_signal_registrar.h"
22 #include "ui/base/gtk/owned_widget_gtk.h"
23 #include "ui/base/models/simple_menu_model.h"
24 #include "ui/gfx/animation/animation_delegate.h"
25 #include "ui/gfx/animation/slide_animation.h"
28 class BrowserActionButton;
29 class GtkThemeService;
32 namespace extensions {
36 typedef struct _GdkDragContext GdkDragContext;
37 typedef struct _GtkWidget GtkWidget;
39 class BrowserActionsToolbarGtk
40 : public extensions::ExtensionToolbarModel::Observer,
41 public gfx::AnimationDelegate,
42 public MenuGtk::Delegate,
43 public ui::SimpleMenuModel::Delegate,
44 public content::NotificationObserver {
46 explicit BrowserActionsToolbarGtk(Browser* browser);
47 virtual ~BrowserActionsToolbarGtk();
49 GtkWidget* widget() { return hbox_.get(); }
50 GtkWidget* chevron() { return overflow_button_->widget(); }
52 // Returns the widget in use by the BrowserActionButton corresponding to
53 // |extension|. Used in positioning the ExtensionInstalledBubble for
55 GtkWidget* GetBrowserActionWidget(const extensions::Extension* extension);
56 BrowserActionButton* GetBrowserActionButton(
57 const extensions::Extension* extension);
59 int button_count() { return extension_button_map_.size(); }
61 Browser* browser() { return browser_; }
63 extensions::ExtensionToolbarModel* model() { return model_; }
65 // Returns the currently selected tab ID, or -1 if there is none.
66 int GetCurrentTabId() const;
68 // Update the display of all buttons.
71 // content::NotificationObserver implementation.
72 virtual void Observe(int type,
73 const content::NotificationSource& source,
74 const content::NotificationDetails& details) OVERRIDE;
77 return resize_animation_.is_animating();
81 friend class BrowserActionButton;
83 // Initialize drag and drop.
86 // Query the extensions service for all extensions with browser actions,
87 // and create the UI for them.
88 void CreateAllButtons();
90 // Sets the width of the container and overflow state according to the model.
91 void SetContainerWidth();
93 // Create the UI for a single browser action. This will stick the button
94 // at the end of the toolbar.
95 void CreateButtonForExtension(const extensions::Extension* extension,
98 // Delete resources associated with UI for a browser action.
99 void RemoveButtonForExtension(const extensions::Extension* extension);
101 // Change the visibility of widget() based on whether we have any buttons
103 void UpdateVisibility();
105 // Hide the extension popup, if any.
108 // Animate the toolbar to show the given number of icons. This assumes the
109 // visibility of the overflow button will not change.
110 void AnimateToShowNIcons(int count);
112 // Returns true if this extension should be shown in this toolbar. This can
113 // return false if we are in an incognito window and the extension is disabled
115 bool ShouldDisplayBrowserAction(const extensions::Extension* extension);
117 // extensions::ExtensionToolbarModel::Observer implementation.
118 virtual void BrowserActionAdded(const extensions::Extension* extension,
120 virtual void BrowserActionRemoved(
121 const extensions::Extension* extension) OVERRIDE;
122 virtual void BrowserActionMoved(const extensions::Extension* extension,
124 virtual bool BrowserActionShowPopup(
125 const extensions::Extension* extension) OVERRIDE;
126 virtual void VisibleCountChanged() OVERRIDE;
128 // gfx::AnimationDelegate implementation.
129 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
130 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
132 // SimpleMenuModel::Delegate implementation.
133 // In our case, |command_id| is be the index into the model's extension list.
134 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
135 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
136 virtual bool GetAcceleratorForCommandId(
138 ui::Accelerator* accelerator) OVERRIDE;
139 virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
141 // MenuGtk::Delegate implementation.
142 virtual void StoppedShowing() OVERRIDE;
143 virtual bool AlwaysShowIconForCmd(int command_id) const OVERRIDE;
145 // Called by the BrowserActionButton in response to drag-begin.
146 void DragStarted(BrowserActionButton* button, GdkDragContext* drag_context);
148 // Sets the width of the button area of the toolbar to |new_width|, clamping
149 // it to appropriate values.
150 void SetButtonHBoxWidth(int new_width);
152 // Shows or hides the chevron as appropriate.
153 void UpdateChevronVisibility();
155 CHROMEGTK_CALLBACK_4(BrowserActionsToolbarGtk, gboolean, OnDragMotion,
156 GdkDragContext*, gint, gint, guint);
157 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, void, OnDragEnd,
159 CHROMEGTK_CALLBACK_2(BrowserActionsToolbarGtk, gboolean, OnDragFailed,
160 GdkDragContext*, GtkDragResult);
161 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, void, OnHierarchyChanged,
163 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, void, OnSetFocus, GtkWidget*);
164 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, gboolean,
165 OnGripperMotionNotify, GdkEventMotion*);
166 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, gboolean, OnGripperExpose,
168 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, gboolean,
169 OnGripperEnterNotify, GdkEventCrossing*);
170 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, gboolean,
171 OnGripperLeaveNotify, GdkEventCrossing*);
172 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, gboolean,
173 OnGripperButtonRelease, GdkEventButton*);
174 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, gboolean,
175 OnGripperButtonPress, GdkEventButton*);
176 // The overflow button is pressed.
177 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, gboolean,
178 OnOverflowButtonPress, GdkEventButton*);
179 // The user presses a mouse button over the popped up overflow menu.
180 CHROMEGTK_CALLBACK_1(BrowserActionsToolbarGtk, gboolean,
181 OnOverflowMenuButtonPress, GdkEventButton*);
182 CHROMEGTK_CALLBACK_0(BrowserActionsToolbarGtk, void, OnButtonShowOrHide);
187 GtkThemeService* theme_service_;
189 extensions::ExtensionToolbarModel* model_;
191 // Contains the drag gripper, browser action buttons, and overflow chevron.
192 ui::OwnedWidgetGtk hbox_;
194 // Contains the browser action buttons.
195 ui::OwnedWidgetGtk button_hbox_;
197 // The overflow button for chrome theme mode.
198 scoped_ptr<CustomDrawButton> overflow_button_;
199 // The separator just next to the overflow button. Only shown in GTK+ theme
200 // mode. In Chrome theme mode, the overflow button has a separator built in.
201 ui::OwnedWidgetGtk separator_;
202 scoped_ptr<MenuGtk> overflow_menu_;
203 scoped_ptr<ui::SimpleMenuModel> overflow_menu_model_;
204 ui::OwnedWidgetGtk overflow_area_;
205 // A widget for adding extra padding to the left of the overflow button.
206 ui::OwnedWidgetGtk overflow_alignment_;
208 // The button that is currently being dragged, or NULL.
209 BrowserActionButton* drag_button_;
211 // The new position of the button in the drag, or -1.
214 // Map from extension ID to BrowserActionButton, which is a wrapper for
215 // a chrome button and related functionality. There should be one entry
216 // for every extension that has a browser action.
217 typedef std::map<std::string, linked_ptr<BrowserActionButton> >
219 ExtensionButtonMap extension_button_map_;
221 // We use this animation for the smart resizing of the toolbar.
222 gfx::SlideAnimation resize_animation_;
223 // This is the final width we are animating towards.
225 // This is the width we were at when we started animating.
228 ui::GtkSignalRegistrar signals_;
230 content::NotificationRegistrar registrar_;
232 base::WeakPtrFactory<BrowserActionsToolbarGtk> weak_factory_;
234 DISALLOW_COPY_AND_ASSIGN(BrowserActionsToolbarGtk);
237 #endif // CHROME_BROWSER_UI_GTK_BROWSER_ACTIONS_TOOLBAR_GTK_H_