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 // This class replicates some menubar behaviors that are tricky to get right. 6 // It is used to create a more native feel for the bookmark bar. 7 8 #ifndef CHROME_BROWSER_UI_GTK_MENU_BAR_HELPER_H_ 9 #define CHROME_BROWSER_UI_GTK_MENU_BAR_HELPER_H_ 10 11 #include <gtk/gtk.h> 12 13 #include <vector> 14 15 #include "base/memory/scoped_ptr.h" 16 #include "ui/base/gtk/gtk_signal.h" 17 18 namespace ui { 19 class GtkSignalRegistrar; 20 } 21 22 class MenuBarHelper { 23 public: 24 class Delegate { 25 public: 26 virtual ~Delegate() {} 27 28 // Called when a the menu for a button ought to be triggered. 29 virtual void PopupForButton(GtkWidget* button) = 0; 30 virtual void PopupForButtonNextTo(GtkWidget* button, 31 GtkMenuDirectionType dir) = 0; 32 }; 33 34 // |delegate| cannot be null. 35 explicit MenuBarHelper(Delegate* delegate); 36 ~MenuBarHelper(); 37 38 // Must be called whenever a button's menu starts showing. It triggers the 39 // MenuBarHelper to start listening for certain events. 40 void MenuStartedShowing(GtkWidget* button, GtkWidget* menu); 41 42 // Add |button| to the set of buttons we care about. 43 void Add(GtkWidget* button); 44 45 // Remove |button| from the set of buttons we care about. 46 void Remove(GtkWidget* button); 47 48 // Clear all buttons from the set. 49 void Clear(); 50 51 private: 52 CHROMEGTK_CALLBACK_0(MenuBarHelper, void, OnMenuHiddenOrDestroyed); 53 CHROMEGTK_CALLBACK_1(MenuBarHelper, gboolean, OnMenuMotionNotify, 54 GdkEventMotion*); 55 CHROMEGTK_CALLBACK_1(MenuBarHelper, void, OnMenuMoveCurrent, 56 GtkMenuDirectionType); 57 58 // The buttons for which we pop up menus. We do not own these, or even add 59 // refs to them. 60 std::vector<GtkWidget*> buttons_; 61 62 // The button that is currently showing a menu, or NULL. 63 GtkWidget* button_showing_menu_; 64 65 // The highest level menu that is currently showing, or NULL. 66 GtkWidget* showing_menu_; 67 68 // All the submenus of |showing_menu_|. We connect to motion events on all 69 // of them. 70 std::vector<GtkWidget*> submenus_; 71 72 // Signal handlers that are attached only between the "show" and "hide" events 73 // for the menu. 74 scoped_ptr<ui::GtkSignalRegistrar> signal_handlers_; 75 76 Delegate* delegate_; 77 }; 78 79 #endif // CHROME_BROWSER_UI_GTK_MENU_BAR_HELPER_H_ 80