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