Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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_UI_GTK_MENU_GTK_H_ | 5 #ifndef CHROME_BROWSER_UI_GTK_MENU_GTK_H_ |
| 6 #define CHROME_BROWSER_UI_GTK_MENU_GTK_H_ | 6 #define CHROME_BROWSER_UI_GTK_MENU_GTK_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <gtk/gtk.h> | 9 #include <gtk/gtk.h> |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/task.h" | 14 #include "base/task.h" |
| 15 #include "ui/base/gtk/gtk_signal.h" | 15 #include "ui/base/gtk/gtk_signal.h" |
| 16 #include "ui/gfx/point.h" | 16 #include "ui/gfx/point.h" |
| 17 | 17 |
| 18 class SkBitmap; | 18 class SkBitmap; |
| 19 | 19 |
| 20 namespace ui { | 20 namespace ui { |
| 21 class ButtonMenuItemModel; | 21 class ButtonMenuItemModel; |
| 22 class MenuModel; | 22 class MenuModel; |
| 23 } | 23 } |
| 24 | 24 |
| 25 class MenuGtk { | 25 // We have multiple places in the code that actually build menus and have |
| 26 // wildly different requirements. | |
| 27 class MenuCreator { | |
|
Evan Stade
2011/04/20 22:52:10
can we call it GtkMenuCreator to make it more expl
| |
| 28 public: | |
| 29 class Delegate { | |
|
Evan Stade
2011/04/20 22:52:10
this should not be called Delegate. I think GtkMen
| |
| 30 public: | |
| 31 virtual ~Delegate() {} | |
| 32 | |
| 33 // Return true if we should override the "gtk-menu-images" system setting | |
| 34 // when showing image menu items for this menu. | |
| 35 virtual bool AlwaysShowIconForCmd(int command_id) const { return false; } | |
| 36 | |
| 37 // Returns an icon for the menu item, if available. | |
| 38 virtual GtkWidget* GetImageForCommandId(int command_id) const; | |
| 39 | |
| 40 static GtkWidget* GetDefaultImageForCommandId(int command_id); | |
|
Evan Stade
2011/04/20 22:52:10
I think this belongs on MenuCreator.
| |
| 41 }; | |
| 42 | |
| 43 protected: | |
| 44 explicit MenuCreator(Delegate* delegate); | |
| 45 virtual ~MenuCreator(); | |
| 46 | |
| 47 // Creates the menu items specified in |model| and places them into |menu|. | |
| 48 void BuildSubmenuFromModel(ui::MenuModel* model, GtkWidget* menu); | |
| 49 | |
| 50 // Builds a GtkImageMenuItem. | |
| 51 GtkWidget* BuildMenuItemWithImage(const std::string& label, | |
| 52 const SkBitmap& icon) const; | |
| 53 | |
| 54 GtkWidget* BuildMenuItemWithImage(const std::string& label, | |
| 55 GtkWidget* image) const; | |
| 56 | |
| 57 GtkWidget* BuildMenuItemWithLabel(const std::string& label, | |
| 58 int command_id) const; | |
| 59 | |
| 60 // Called to build one of our custom menu button items. Some menus we | |
| 61 // construct can't support this type (due to tight integration with our | |
| 62 // GtkCustomMenu); in that case always return NULL. | |
| 63 virtual GtkWidget* BuildButtonMenuItem(ui::ButtonMenuItemModel* model, | |
| 64 GtkWidget* menu) = 0; | |
| 65 | |
| 66 // Called after every |menu_item| created. It's this method's responsibility | |
| 67 // to add the |menu_item| to |menu|, to hook up signals, etc. | |
| 68 virtual GtkWidget* AppendMenuItemToMenu(int index, | |
| 69 ui::MenuModel* model, | |
| 70 GtkWidget* menu_item, | |
| 71 GtkWidget* menu, | |
| 72 bool connect_to_activate) = 0; | |
| 73 | |
| 74 private: | |
| 75 Delegate* delegate_; | |
| 76 | |
| 77 // For some menu items, we want to show the accelerator, but not actually | |
| 78 // explicitly handle it. To this end we connect those menu items' accelerators | |
| 79 // to this group, but don't attach this group to any top level window. | |
| 80 GtkAccelGroup* dummy_accel_group_; | |
| 81 }; | |
| 82 | |
| 83 // MenuGtk both creates the menu. | |
| 84 class MenuGtk : public MenuCreator { | |
| 26 public: | 85 public: |
| 27 // Delegate class that lets another class control the status of the menu. | 86 // Delegate class that lets another class control the status of the menu. |
| 28 class Delegate { | 87 class Delegate : public MenuCreator::Delegate { |
|
Evan Stade
2011/04/20 22:52:10
I don't think MenuGtk::Delegate needs to be a subc
| |
| 29 public: | 88 public: |
| 30 virtual ~Delegate() { } | 89 virtual ~Delegate() { } |
| 31 | 90 |
| 32 // Called before a command is executed. This exists for the case where a | 91 // 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 | 92 // 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 | 93 // 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 | 94 // and not after the command is executed because its execution may delete |
| 36 // the menu and/or the delegate. | 95 // the menu and/or the delegate. |
| 37 virtual void CommandWillBeExecuted() {} | 96 virtual void CommandWillBeExecuted() {} |
| 38 | 97 |
| 39 // Called when the menu stops showing. This will be called before | 98 // 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 | 99 // ExecuteCommand if the user clicks an item, but will also be called when |
| 41 // the user clicks away from the menu. | 100 // the user clicks away from the menu. |
| 42 virtual void StoppedShowing() {} | 101 virtual void StoppedShowing() {} |
| 43 | 102 |
| 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. | 103 // Returns a tinted image used in button in a menu. |
| 49 virtual GtkIconSet* GetIconSetForId(int idr) { return NULL; } | 104 virtual GtkIconSet* GetIconSetForId(int idr) { return NULL; } |
|
Evan Stade
2011/04/20 22:52:10
also belongs in ModelExtras
| |
| 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 }; | 105 }; |
| 56 | 106 |
| 57 MenuGtk(MenuGtk::Delegate* delegate, ui::MenuModel* model); | 107 MenuGtk(MenuGtk::Delegate* delegate, ui::MenuModel* model); |
| 58 ~MenuGtk(); | 108 ~MenuGtk(); |
| 59 | 109 |
| 60 // Initialize GTK signal handlers. | 110 // Initialize GTK signal handlers. |
| 61 void ConnectSignalHandlers(); | 111 void ConnectSignalHandlers(); |
| 62 | 112 |
| 63 // These methods are used to build the menu dynamically. The return value | 113 // These methods are used to build the menu dynamically. The return value |
| 64 // is the new menu item. | 114 // is the new menu item. |
| 65 GtkWidget* AppendMenuItemWithLabel(int command_id, const std::string& label); | 115 GtkWidget* AppendMenuItemWithLabel(int command_id, const std::string& label); |
| 66 GtkWidget* AppendMenuItemWithIcon(int command_id, const std::string& label, | 116 GtkWidget* AppendMenuItemWithIcon(int command_id, const std::string& label, |
| 67 const SkBitmap& icon); | 117 const SkBitmap& icon); |
| 68 GtkWidget* AppendCheckMenuItemWithLabel(int command_id, | 118 GtkWidget* AppendCheckMenuItemWithLabel(int command_id, |
| 69 const std::string& label); | 119 const std::string& label); |
| 70 GtkWidget* AppendSeparator(); | 120 GtkWidget* AppendSeparator(); |
| 71 GtkWidget* AppendMenuItem(int command_id, GtkWidget* menu_item); | 121 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 | 122 |
| 78 // Displays the menu near a widget, as if the widget were a menu bar. | 123 // Displays the menu near a widget, as if the widget were a menu bar. |
| 79 // Example: the wrench menu button. | 124 // Example: the wrench menu button. |
| 80 // |button| is the mouse button that brought up the menu. | 125 // |button| is the mouse button that brought up the menu. |
| 81 // |event_time| is the time from the GdkEvent. | 126 // |event_time| is the time from the GdkEvent. |
| 82 void PopupForWidget(GtkWidget* widget, int button, guint32 event_time); | 127 void PopupForWidget(GtkWidget* widget, int button, guint32 event_time); |
| 83 | 128 |
| 84 // Displays the menu as a context menu, i.e. at the cursor location. | 129 // 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. | 130 // It is implicit that it was brought up using the right mouse button. |
| 86 // |point| is the point where to put the menu. | 131 // |point| is the point where to put the menu. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 116 int* y, | 161 int* y, |
| 117 gboolean* push_in, | 162 gboolean* push_in, |
| 118 gpointer userdata); | 163 gpointer userdata); |
| 119 | 164 |
| 120 GtkWidget* widget() const { return menu_; } | 165 GtkWidget* widget() const { return menu_; } |
| 121 | 166 |
| 122 // Updates all the enabled/checked states and the dynamic labels. | 167 // Updates all the enabled/checked states and the dynamic labels. |
| 123 void UpdateMenu(); | 168 void UpdateMenu(); |
| 124 | 169 |
| 125 private: | 170 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_|. | 171 // A function that creates a GtkMenu from |model_|. |
| 137 void BuildMenuFromModel(); | 172 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 | 173 |
| 144 void ExecuteCommand(ui::MenuModel* model, int id); | 174 void ExecuteCommand(ui::MenuModel* model, int id); |
| 145 | 175 |
| 176 // MenuCreator: | |
| 177 virtual GtkWidget* AppendMenuItemToMenu(int index, | |
| 178 ui::MenuModel* model, | |
| 179 GtkWidget* menu_item, | |
| 180 GtkWidget* menu, | |
| 181 bool connect_to_activate); | |
| 182 virtual GtkWidget* BuildButtonMenuItem(ui::ButtonMenuItemModel* model, | |
| 183 GtkWidget* menu); | |
| 184 | |
| 146 // Callback for when a menu item is clicked. | 185 // Callback for when a menu item is clicked. |
| 147 CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuItemActivated); | 186 CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuItemActivated); |
| 148 | 187 |
| 149 // Called when one of the buttons are pressed. | 188 // Called when one of the buttons are pressed. |
| 150 CHROMEGTK_CALLBACK_1(MenuGtk, void, OnMenuButtonPressed, int); | 189 CHROMEGTK_CALLBACK_1(MenuGtk, void, OnMenuButtonPressed, int); |
| 151 | 190 |
| 152 // Called to maybe activate a button if that button isn't supposed to dismiss | 191 // Called to maybe activate a button if that button isn't supposed to dismiss |
| 153 // the menu. | 192 // the menu. |
| 154 CHROMEGTK_CALLBACK_1(MenuGtk, gboolean, OnMenuTryButtonPressed, int); | 193 CHROMEGTK_CALLBACK_1(MenuGtk, gboolean, OnMenuTryButtonPressed, int); |
| 155 | 194 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 166 // items. | 205 // items. |
| 167 static void SetMenuItemInfo(GtkWidget* widget, void* raw_menu); | 206 static void SetMenuItemInfo(GtkWidget* widget, void* raw_menu); |
| 168 | 207 |
| 169 // Queries this object about the menu state. | 208 // Queries this object about the menu state. |
| 170 MenuGtk::Delegate* delegate_; | 209 MenuGtk::Delegate* delegate_; |
| 171 | 210 |
| 172 // If non-NULL, the MenuModel that we use to populate and control the GTK | 211 // If non-NULL, the MenuModel that we use to populate and control the GTK |
| 173 // menu (overriding the delegate as a controller). | 212 // menu (overriding the delegate as a controller). |
| 174 ui::MenuModel* model_; | 213 ui::MenuModel* model_; |
| 175 | 214 |
| 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 | 215 // gtk_menu_popup() does not appear to take ownership of popup menus, so |
| 182 // MenuGtk explicitly manages the lifetime of the menu. | 216 // MenuGtk explicitly manages the lifetime of the menu. |
| 183 GtkWidget* menu_; | 217 GtkWidget* menu_; |
| 184 | 218 |
| 185 // True when we should ignore "activate" signals. Used to prevent | 219 // True when we should ignore "activate" signals. Used to prevent |
| 186 // menu items from getting activated when we are setting up the | 220 // menu items from getting activated when we are setting up the |
| 187 // menu. | 221 // menu. |
| 188 static bool block_activation_; | 222 static bool block_activation_; |
| 189 | 223 |
| 190 // We must free these at shutdown. | 224 // We must free these at shutdown. |
| 191 std::vector<MenuGtk*> submenus_we_own_; | 225 std::vector<MenuGtk*> submenus_we_own_; |
| 192 | 226 |
| 193 ScopedRunnableMethodFactory<MenuGtk> factory_; | 227 ScopedRunnableMethodFactory<MenuGtk> factory_; |
| 194 }; | 228 }; |
| 195 | 229 |
| 196 #endif // CHROME_BROWSER_UI_GTK_MENU_GTK_H_ | 230 #endif // CHROME_BROWSER_UI_GTK_MENU_GTK_H_ |
| OLD | NEW |