Home | History | Annotate | Download | only in gtk
      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_MENU_GTK_H_
      6 #define CHROME_BROWSER_UI_GTK_MENU_GTK_H_
      7 
      8 #include <gtk/gtk.h>
      9 
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/memory/weak_ptr.h"
     14 #include "ui/base/gtk/gtk_signal.h"
     15 #include "ui/base/gtk/gtk_signal_registrar.h"
     16 #include "ui/gfx/point.h"
     17 
     18 namespace gfx {
     19 class Image;
     20 }
     21 
     22 namespace ui {
     23 class ButtonMenuItemModel;
     24 class MenuModel;
     25 }
     26 
     27 class MenuGtk {
     28  public:
     29   // Delegate class that lets another class control the status of the menu.
     30   class Delegate {
     31    public:
     32     virtual ~Delegate() {}
     33 
     34     // Called before a command is executed. This exists for the case where a
     35     // model is handling the actual execution of commands, but the delegate
     36     // still needs to know that some command got executed. This is called before
     37     // and not after the command is executed because its execution may delete
     38     // the menu and/or the delegate.
     39     virtual void CommandWillBeExecuted() {}
     40 
     41     // Called when the menu stops showing. This will be called before
     42     // ExecuteCommand if the user clicks an item, but will also be called when
     43     // the user clicks away from the menu.
     44     virtual void StoppedShowing() {}
     45 
     46     // Return true if we should override the "gtk-menu-images" system setting
     47     // when showing image menu items for this menu.
     48     virtual bool AlwaysShowIconForCmd(int command_id) const;
     49 
     50     // Returns a tinted image used in button in a menu.
     51     virtual GtkIconSet* GetIconSetForId(int idr);
     52 
     53     // Returns an icon for the menu item, if available.
     54     virtual GtkWidget* GetImageForCommandId(int command_id) const;
     55 
     56     static GtkWidget* GetDefaultImageForCommandId(int command_id);
     57   };
     58 
     59   MenuGtk(MenuGtk::Delegate* delegate, ui::MenuModel* model);
     60   virtual ~MenuGtk();
     61 
     62   // Initialize GTK signal handlers.
     63   void ConnectSignalHandlers();
     64 
     65   // These methods are used to build the menu dynamically. The return value
     66   // is the new menu item.
     67   GtkWidget* AppendMenuItemWithLabel(int command_id, const std::string& label);
     68   GtkWidget* AppendMenuItemWithIcon(int command_id, const std::string& label,
     69                                     const gfx::Image& icon);
     70   GtkWidget* AppendCheckMenuItemWithLabel(int command_id,
     71                                           const std::string& label);
     72   GtkWidget* AppendSeparator();
     73   GtkWidget* AppendMenuItem(int command_id, GtkWidget* menu_item);
     74   GtkWidget* AppendMenuItemToMenu(int index,
     75                                   ui::MenuModel* model,
     76                                   GtkWidget* menu_item,
     77                                   GtkWidget* menu,
     78                                   bool connect_to_activate);
     79 
     80   // Displays the menu near a widget, as if the widget were a menu bar.
     81   // Example: the wrench menu button.
     82   // |button| is the mouse button that brought up the menu.
     83   // |event_time| is the time from the GdkEvent.
     84   void PopupForWidget(GtkWidget* widget, int button, guint32 event_time);
     85 
     86   // Displays the menu as a context menu, i.e. at the cursor location.
     87   // It is implicit that it was brought up using the right mouse button.
     88   // |point| is the point where to put the menu.
     89   // |event_time| is the time of the event that triggered the menu's display.
     90   void PopupAsContext(const gfx::Point& point, guint32 event_time);
     91 
     92   // Displays the menu as a context menu for the passed status icon.
     93   void PopupAsContextForStatusIcon(guint32 event_time, guint32 button,
     94                                    GtkStatusIcon* icon);
     95 
     96   // Displays the menu following a keyboard event (such as selecting |widget|
     97   // and pressing "enter").
     98   void PopupAsFromKeyEvent(GtkWidget* widget);
     99 
    100   // Closes the menu.
    101   void Cancel();
    102 
    103   // Repositions the menu to be right under the button.  Alignment is set as
    104   // object data on |void_widget| with the tag "left_align".  If "left_align"
    105   // is true, it aligns the left side of the menu with the left side of the
    106   // button. Otherwise it aligns the right side of the menu with the right side
    107   // of the button. Public since some menus have odd requirements that don't
    108   // belong in a public class.
    109   static void WidgetMenuPositionFunc(GtkMenu* menu,
    110                                      int* x,
    111                                      int* y,
    112                                      gboolean* push_in,
    113                                      void* void_widget);
    114 
    115   // Positions the menu to appear at the gfx::Point represented by |userdata|.
    116   static void PointMenuPositionFunc(GtkMenu* menu,
    117                                     int* x,
    118                                     int* y,
    119                                     gboolean* push_in,
    120                                     gpointer userdata);
    121 
    122   GtkWidget* widget() const { return menu_; }
    123 
    124   // Updates all the enabled/checked states and the dynamic labels.
    125   void UpdateMenu();
    126 
    127  private:
    128   // Builds a GtkImageMenuItem.
    129   GtkWidget* BuildMenuItemWithImage(const std::string& label,
    130                                     const gfx::Image& icon);
    131 
    132   GtkWidget* BuildMenuItemWithImage(const std::string& label,
    133                                     GtkWidget* image);
    134 
    135   GtkWidget* BuildMenuItemWithLabel(const std::string& label,
    136                                     int command_id);
    137 
    138   // A function that creates a GtkMenu from |model_|.
    139   void BuildMenuFromModel();
    140   // Implementation of the above; called recursively.
    141   void BuildSubmenuFromModel(ui::MenuModel* model, GtkWidget* menu);
    142   // Builds a menu item with buttons in it from the data in the model.
    143   GtkWidget* BuildButtonMenuItem(ui::ButtonMenuItemModel* model,
    144                                  GtkWidget* menu);
    145 
    146   void ExecuteCommand(ui::MenuModel* model, int id);
    147 
    148   // Callback for when a menu item is clicked.
    149   CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuItemActivated);
    150 
    151   // Called when one of the buttons is pressed.
    152   CHROMEGTK_CALLBACK_1(MenuGtk, void, OnMenuButtonPressed, int);
    153 
    154   // Called to maybe activate a button if that button isn't supposed to dismiss
    155   // the menu.
    156   CHROMEGTK_CALLBACK_1(MenuGtk, gboolean, OnMenuTryButtonPressed, int);
    157 
    158   // Updates all the menu items' state.
    159   CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuShow);
    160 
    161   // Sets the activating widget back to a normal appearance.
    162   CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuHidden);
    163 
    164   // Focus out event handler for the menu.
    165   CHROMEGTK_CALLBACK_1(MenuGtk, gboolean, OnMenuFocusOut, GdkEventFocus*);
    166 
    167   // Handles building dynamic submenus on demand when they are shown.
    168   CHROMEGTK_CALLBACK_0(MenuGtk, void, OnSubMenuShow);
    169 
    170   // Handles trearing down dynamic submenus when they have been closed.
    171   CHROMEGTK_CALLBACK_0(MenuGtk, void, OnSubMenuHidden);
    172 
    173   // Scheduled by OnSubMenuHidden() to avoid deleting submenus when hidden
    174   // before pending activations within them are delivered.
    175   static void OnSubMenuHiddenCallback(GtkWidget* submenu);
    176 
    177   // Sets the enable/disabled state and dynamic labels on our menu items.
    178   static void SetButtonItemInfo(GtkWidget* button, gpointer userdata);
    179 
    180   // Sets the check mark, enabled/disabled state and dynamic labels on our menu
    181   // items.
    182   static void SetMenuItemInfo(GtkWidget* widget, void* raw_menu);
    183 
    184   // Queries this object about the menu state.
    185   MenuGtk::Delegate* delegate_;
    186 
    187   // If non-NULL, the MenuModel that we use to populate and control the GTK
    188   // menu (overriding the delegate as a controller).
    189   ui::MenuModel* model_;
    190 
    191   // For some menu items, we want to show the accelerator, but not actually
    192   // explicitly handle it. To this end we connect those menu items' accelerators
    193   // to this group, but don't attach this group to any top level window.
    194   GtkAccelGroup* dummy_accel_group_;
    195 
    196   // gtk_menu_popup() does not appear to take ownership of popup menus, so
    197   // MenuGtk explicitly manages the lifetime of the menu.
    198   GtkWidget* menu_;
    199 
    200   // True when we should ignore "activate" signals.  Used to prevent
    201   // menu items from getting activated when we are setting up the
    202   // menu.
    203   static bool block_activation_;
    204 
    205   ui::GtkSignalRegistrar signal_;
    206 
    207   base::WeakPtrFactory<MenuGtk> weak_factory_;
    208 };
    209 
    210 #endif  // CHROME_BROWSER_UI_GTK_MENU_GTK_H_
    211