| 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 "chrome/browser/ui/gtk/gtk_menu_creator.h" |
| 15 #include "ui/base/gtk/gtk_signal.h" | 16 #include "ui/base/gtk/gtk_signal.h" |
| 16 #include "ui/gfx/point.h" | 17 #include "ui/gfx/point.h" |
| 17 | 18 |
| 18 class SkBitmap; | 19 class SkBitmap; |
| 19 | 20 |
| 20 namespace ui { | 21 namespace ui { |
| 21 class ButtonMenuItemModel; | 22 class ButtonMenuItemModel; |
| 22 class MenuModel; | 23 class MenuModel; |
| 23 } | 24 } |
| 24 | 25 |
| 25 class MenuGtk { | 26 // MenuGtk both creates the menu. |
| 27 class MenuGtk : public GtkMenuCreator { |
| 26 public: | 28 public: |
| 27 // Delegate class that lets another class control the status of the menu. | 29 // Delegate class that lets another class control the status of the menu. |
| 28 class Delegate { | 30 class Delegate : public GtkMenuCreator::IconSource { |
| 29 public: | 31 public: |
| 30 virtual ~Delegate() { } | 32 virtual ~Delegate() { } |
| 31 | 33 |
| 32 // Called before a command is executed. This exists for the case where a | 34 // 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 | 35 // 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 | 36 // 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 | 37 // and not after the command is executed because its execution may delete |
| 36 // the menu and/or the delegate. | 38 // the menu and/or the delegate. |
| 37 virtual void CommandWillBeExecuted() {} | 39 virtual void CommandWillBeExecuted() {} |
| 38 | 40 |
| 39 // Called when the menu stops showing. This will be called before | 41 // 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 | 42 // ExecuteCommand if the user clicks an item, but will also be called when |
| 41 // the user clicks away from the menu. | 43 // the user clicks away from the menu. |
| 42 virtual void StoppedShowing() {} | 44 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 }; | 45 }; |
| 56 | 46 |
| 57 MenuGtk(MenuGtk::Delegate* delegate, ui::MenuModel* model); | 47 MenuGtk(MenuGtk::Delegate* delegate, ui::MenuModel* model); |
| 58 ~MenuGtk(); | 48 ~MenuGtk(); |
| 59 | 49 |
| 60 // Initialize GTK signal handlers. | 50 // Initialize GTK signal handlers. |
| 61 void ConnectSignalHandlers(); | 51 void ConnectSignalHandlers(); |
| 62 | 52 |
| 63 // These methods are used to build the menu dynamically. The return value | 53 // These methods are used to build the menu dynamically. The return value |
| 64 // is the new menu item. | 54 // is the new menu item. |
| 65 GtkWidget* AppendMenuItemWithLabel(int command_id, const std::string& label); | 55 GtkWidget* AppendMenuItemWithLabel(int command_id, const std::string& label); |
| 66 GtkWidget* AppendMenuItemWithIcon(int command_id, const std::string& label, | 56 GtkWidget* AppendMenuItemWithIcon(int command_id, const std::string& label, |
| 67 const SkBitmap& icon); | 57 const SkBitmap& icon); |
| 68 GtkWidget* AppendCheckMenuItemWithLabel(int command_id, | 58 GtkWidget* AppendCheckMenuItemWithLabel(int command_id, |
| 69 const std::string& label); | 59 const std::string& label); |
| 70 GtkWidget* AppendSeparator(); | 60 GtkWidget* AppendSeparator(); |
| 71 GtkWidget* AppendMenuItem(int command_id, GtkWidget* menu_item); | 61 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 | 62 |
| 78 // Displays the menu near a widget, as if the widget were a menu bar. | 63 // Displays the menu near a widget, as if the widget were a menu bar. |
| 79 // Example: the wrench menu button. | 64 // Example: the wrench menu button. |
| 80 // |button| is the mouse button that brought up the menu. | 65 // |button| is the mouse button that brought up the menu. |
| 81 // |event_time| is the time from the GdkEvent. | 66 // |event_time| is the time from the GdkEvent. |
| 82 void PopupForWidget(GtkWidget* widget, int button, guint32 event_time); | 67 void PopupForWidget(GtkWidget* widget, int button, guint32 event_time); |
| 83 | 68 |
| 84 // Displays the menu as a context menu, i.e. at the cursor location. | 69 // 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. | 70 // It is implicit that it was brought up using the right mouse button. |
| 86 // |point| is the point where to put the menu. | 71 // |point| is the point where to put the menu. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 116 int* y, | 101 int* y, |
| 117 gboolean* push_in, | 102 gboolean* push_in, |
| 118 gpointer userdata); | 103 gpointer userdata); |
| 119 | 104 |
| 120 GtkWidget* widget() const { return menu_; } | 105 GtkWidget* widget() const { return menu_; } |
| 121 | 106 |
| 122 // Updates all the enabled/checked states and the dynamic labels. | 107 // Updates all the enabled/checked states and the dynamic labels. |
| 123 void UpdateMenu(); | 108 void UpdateMenu(); |
| 124 | 109 |
| 125 private: | 110 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_|. | 111 // A function that creates a GtkMenu from |model_|. |
| 137 void BuildMenuFromModel(); | 112 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 | 113 |
| 144 void ExecuteCommand(ui::MenuModel* model, int id); | 114 void ExecuteCommand(ui::MenuModel* model, int id); |
| 145 | 115 |
| 116 // MenuCreator: |
| 117 virtual GtkWidget* AppendMenuItemToMenu(int index, |
| 118 ui::MenuModel* model, |
| 119 GtkWidget* menu_item, |
| 120 GtkWidget* menu); |
| 121 virtual GtkWidget* BuildButtonMenuItem(ui::ButtonMenuItemModel* model, |
| 122 GtkWidget* menu); |
| 123 |
| 146 // Callback for when a menu item is clicked. | 124 // Callback for when a menu item is clicked. |
| 147 CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuItemActivated); | 125 CHROMEGTK_CALLBACK_0(MenuGtk, void, OnMenuItemActivated); |
| 148 | 126 |
| 149 // Called when one of the buttons are pressed. | 127 // Called when one of the buttons are pressed. |
| 150 CHROMEGTK_CALLBACK_1(MenuGtk, void, OnMenuButtonPressed, int); | 128 CHROMEGTK_CALLBACK_1(MenuGtk, void, OnMenuButtonPressed, int); |
| 151 | 129 |
| 152 // Called to maybe activate a button if that button isn't supposed to dismiss | 130 // Called to maybe activate a button if that button isn't supposed to dismiss |
| 153 // the menu. | 131 // the menu. |
| 154 CHROMEGTK_CALLBACK_1(MenuGtk, gboolean, OnMenuTryButtonPressed, int); | 132 CHROMEGTK_CALLBACK_1(MenuGtk, gboolean, OnMenuTryButtonPressed, int); |
| 155 | 133 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 166 // items. | 144 // items. |
| 167 static void SetMenuItemInfo(GtkWidget* widget, void* raw_menu); | 145 static void SetMenuItemInfo(GtkWidget* widget, void* raw_menu); |
| 168 | 146 |
| 169 // Queries this object about the menu state. | 147 // Queries this object about the menu state. |
| 170 MenuGtk::Delegate* delegate_; | 148 MenuGtk::Delegate* delegate_; |
| 171 | 149 |
| 172 // If non-NULL, the MenuModel that we use to populate and control the GTK | 150 // If non-NULL, the MenuModel that we use to populate and control the GTK |
| 173 // menu (overriding the delegate as a controller). | 151 // menu (overriding the delegate as a controller). |
| 174 ui::MenuModel* model_; | 152 ui::MenuModel* model_; |
| 175 | 153 |
| 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 | 154 // gtk_menu_popup() does not appear to take ownership of popup menus, so |
| 182 // MenuGtk explicitly manages the lifetime of the menu. | 155 // MenuGtk explicitly manages the lifetime of the menu. |
| 183 GtkWidget* menu_; | 156 GtkWidget* menu_; |
| 184 | 157 |
| 185 // True when we should ignore "activate" signals. Used to prevent | 158 // True when we should ignore "activate" signals. Used to prevent |
| 186 // menu items from getting activated when we are setting up the | 159 // menu items from getting activated when we are setting up the |
| 187 // menu. | 160 // menu. |
| 188 static bool block_activation_; | 161 static bool block_activation_; |
| 189 | 162 |
| 190 // We must free these at shutdown. | 163 // We must free these at shutdown. |
| 191 std::vector<MenuGtk*> submenus_we_own_; | 164 std::vector<MenuGtk*> submenus_we_own_; |
| 192 | 165 |
| 193 ScopedRunnableMethodFactory<MenuGtk> factory_; | 166 ScopedRunnableMethodFactory<MenuGtk> factory_; |
| 194 }; | 167 }; |
| 195 | 168 |
| 196 #endif // CHROME_BROWSER_UI_GTK_MENU_GTK_H_ | 169 #endif // CHROME_BROWSER_UI_GTK_MENU_GTK_H_ |
| OLD | NEW |