| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_GTK_MENU_GTK_H_ | 5 #ifndef CHROME_BROWSER_GTK_MENU_GTK_H_ |
| 6 #define CHROME_BROWSER_GTK_MENU_GTK_H_ | 6 #define CHROME_BROWSER_GTK_MENU_GTK_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <gtk/gtk.h> | 9 #include "chrome/browser/ui/gtk/menu_gtk.h" |
| 10 | 10 // TODO(msw): remove this file once all includes have been updated. |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "app/gtk_signal.h" | |
| 15 #include "base/task.h" | |
| 16 #include "gfx/point.h" | |
| 17 | |
| 18 class SkBitmap; | |
| 19 | |
| 20 namespace menus { | |
| 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: | |
| 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. | |
| 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. | |
| 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. | |
| 46 virtual bool AlwaysShowIconForCmd(int command_id) const { return false; } | |
| 47 | |
| 48 // Returns a tinted image used in button in a menu. | |
| 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, menus::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 menus::MenuModel* model, | |
| 74 GtkWidget* menu_item, | |
| 75 GtkWidget* menu, | |
| 76 bool connect_to_activate); | |
| 77 | |
| 78 // Displays the menu. |timestamp| is the time of activation. The popup is | |
| 79 // statically positioned at |widget|. | |
| 80 void Popup(GtkWidget* widget, gint button_type, guint32 timestamp); | |
| 81 | |
| 82 // Displays the menu using the button type and timestamp of |event|. The popup | |
| 83 // is statically positioned at |widget|. | |
| 84 void Popup(GtkWidget* widget, GdkEvent* event); | |
| 85 | |
| 86 // Displays the menu as a context menu, i.e. at the current cursor location. | |
| 87 // |event_time| is the time of the event that triggered the menu's display. | |
| 88 void PopupAsContext(guint32 event_time); | |
| 89 | |
| 90 // Displays the menu at the given coords. |point| is intentionally not const. | |
| 91 void PopupAsContextAt(guint32 event_time, gfx::Point point); | |
| 92 | |
| 93 // Displays the menu as a context menu for the passed status icon. | |
| 94 void PopupAsContextForStatusIcon(guint32 event_time, guint32 button, | |
| 95 GtkStatusIcon* icon); | |
| 96 | |
| 97 // Displays the menu following a keyboard event (such as selecting |widget| | |
| 98 // and pressing "enter"). | |
| 99 void PopupAsFromKeyEvent(GtkWidget* widget); | |
| 100 | |
| 101 // Closes the menu. | |
| 102 void Cancel(); | |
| 103 | |
| 104 // Repositions the menu to be right under the button. Alignment is set as | |
| 105 // object data on |void_widget| with the tag "left_align". If "left_align" | |
| 106 // is true, it aligns the left side of the menu with the left side of the | |
| 107 // button. Otherwise it aligns the right side of the menu with the right side | |
| 108 // of the button. Public since some menus have odd requirements that don't | |
| 109 // belong in a public class. | |
| 110 static void WidgetMenuPositionFunc(GtkMenu* menu, | |
| 111 int* x, | |
| 112 int* y, | |
| 113 gboolean* push_in, | |
| 114 void* void_widget); | |
| 115 | |
| 116 // Positions the menu to appear at the gfx::Point represented by |userdata|. | |
| 117 static void PointMenuPositionFunc(GtkMenu* menu, | |
| 118 int* x, | |
| 119 int* y, | |
| 120 gboolean* push_in, | |
| 121 gpointer userdata); | |
| 122 | |
| 123 GtkWidget* widget() const { return menu_; } | |
| 124 | |
| 125 // Updates all the enabled/checked states and the dynamic labels. | |
| 126 void UpdateMenu(); | |
| 127 | |
| 128 private: | |
| 129 // Builds a GtkImageMenuItem. | |
| 130 GtkWidget* BuildMenuItemWithImage(const std::string& label, | |
| 131 const SkBitmap& icon); | |
| 132 | |
| 133 GtkWidget* BuildMenuItemWithImage(const std::string& label, | |
| 134 GtkWidget* image); | |
| 135 | |
| 136 GtkWidget* BuildMenuItemWithLabel(const std::string& label, | |
| 137 int command_id); | |
| 138 | |
| 139 // A function that creates a GtkMenu from |model_|. | |
| 140 void BuildMenuFromModel(); | |
| 141 // Implementation of the above; called recursively. | |
| 142 void BuildSubmenuFromModel(menus::MenuModel* model, GtkWidget* menu); | |
| 143 // Builds a menu item with buttons in it from the data in the model. | |
| 144 GtkWidget* BuildButtomMenuItem(menus::ButtonMenuItemModel* model, | |
| 145 GtkWidget* menu); | |
| 146 | |
| 147 void ExecuteCommand(menus::MenuModel* model, int id); | |
| 148 | |
| 149 // Callback for when a menu item is clicked. | |
| 150 CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuItemActivated); | |
| 151 | |
| 152 // Called when one of the buttons are pressed. | |
| 153 CHROMEGTK_CALLBACK_1(MenuGtk, void, OnMenuButtonPressed, int); | |
| 154 | |
| 155 // Called to maybe activate a button if that button isn't supposed to dismiss | |
| 156 // the menu. | |
| 157 CHROMEGTK_CALLBACK_1(MenuGtk, gboolean, OnMenuTryButtonPressed, int); | |
| 158 | |
| 159 // Updates all the menu items' state. | |
| 160 CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuShow); | |
| 161 | |
| 162 // Sets the activating widget back to a normal appearance. | |
| 163 CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuHidden); | |
| 164 | |
| 165 // Sets the enable/disabled state and dynamic labels on our menu items. | |
| 166 static void SetButtonItemInfo(GtkWidget* button, gpointer userdata); | |
| 167 | |
| 168 // Sets the check mark, enabled/disabled state and dynamic labels on our menu | |
| 169 // items. | |
| 170 static void SetMenuItemInfo(GtkWidget* widget, void* raw_menu); | |
| 171 | |
| 172 // Queries this object about the menu state. | |
| 173 MenuGtk::Delegate* delegate_; | |
| 174 | |
| 175 // If non-NULL, the MenuModel that we use to populate and control the GTK | |
| 176 // menu (overriding the delegate as a controller). | |
| 177 menus::MenuModel* model_; | |
| 178 | |
| 179 // For some menu items, we want to show the accelerator, but not actually | |
| 180 // explicitly handle it. To this end we connect those menu items' accelerators | |
| 181 // to this group, but don't attach this group to any top level window. | |
| 182 GtkAccelGroup* dummy_accel_group_; | |
| 183 | |
| 184 // gtk_menu_popup() does not appear to take ownership of popup menus, so | |
| 185 // MenuGtk explicitly manages the lifetime of the menu. | |
| 186 GtkWidget* menu_; | |
| 187 | |
| 188 // True when we should ignore "activate" signals. Used to prevent | |
| 189 // menu items from getting activated when we are setting up the | |
| 190 // menu. | |
| 191 static bool block_activation_; | |
| 192 | |
| 193 // We must free these at shutdown. | |
| 194 std::vector<MenuGtk*> submenus_we_own_; | |
| 195 | |
| 196 ScopedRunnableMethodFactory<MenuGtk> factory_; | |
| 197 }; | |
| 198 | 11 |
| 199 #endif // CHROME_BROWSER_GTK_MENU_GTK_H_ | 12 #endif // CHROME_BROWSER_GTK_MENU_GTK_H_ |
| OLD | NEW |