| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 | 7 |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/gfx/point.h" | 13 #include "base/gfx/point.h" |
| 14 #include "base/task.h" | 14 #include "base/task.h" |
| 15 #include "chrome/common/owned_widget_gtk.h" | 15 #include "chrome/common/owned_widget_gtk.h" |
| 16 | 16 |
| 17 class SkBitmap; | 17 class SkBitmap; |
| 18 | 18 |
| 19 namespace menus { |
| 20 class MenuModel; |
| 21 } |
| 22 |
| 19 struct MenuCreateMaterial; | 23 struct MenuCreateMaterial; |
| 20 | 24 |
| 21 class MenuGtk { | 25 class MenuGtk { |
| 22 public: | 26 public: |
| 23 // Delegate class that lets another class control the status of the menu. | 27 // Delegate class that lets another class control the status of the menu. |
| 24 class Delegate { | 28 class Delegate { |
| 25 public: | 29 public: |
| 26 virtual ~Delegate() { } | 30 virtual ~Delegate() { } |
| 27 | 31 |
| 28 // Returns whether the menu item for this command should be enabled. | 32 // Returns whether the menu item for this command should be enabled. |
| 29 virtual bool IsCommandEnabled(int command_id) const = 0; | 33 virtual bool IsCommandEnabled(int command_id) const = 0; |
| 30 | 34 |
| 31 // Returns whether this command is checked (for checkbox menu items only). | 35 // Returns whether this command is checked (for checkbox menu items only). |
| 32 virtual bool IsItemChecked(int command_id) const { return false; } | 36 virtual bool IsItemChecked(int command_id) const { return false; } |
| 33 | 37 |
| 34 // Executes the command. | 38 // Executes the command. |
| 35 virtual void ExecuteCommand(int command_id) = 0; | 39 virtual void ExecuteCommand(int command_id) = 0; |
| 36 | 40 |
| 37 // Called when the menu stops showing. This will be called along with | 41 // Called when the menu stops showing. This will be called along with |
| 38 // 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 |
| 39 // the user clicks away from the menu. | 43 // the user clicks away from the menu. |
| 40 virtual void StoppedShowing() {} | 44 virtual void StoppedShowing() {} |
| 41 | 45 |
| 42 // Functions needed for creation of non-static menus. | |
| 43 virtual int GetItemCount() const { return 0; } | |
| 44 virtual bool IsItemSeparator(int command_id) const { return false; } | |
| 45 virtual std::string GetLabel(int command_id) const { return std::string(); } | |
| 46 virtual bool HasIcon(int command_id) const { return false; } | |
| 47 virtual const SkBitmap* GetIcon(int command_id) const { return NULL; } | |
| 48 // Return true if we should override the "gtk-menu-images" system setting | 46 // Return true if we should override the "gtk-menu-images" system setting |
| 49 // when showing image menu items for this menu. | 47 // when showing image menu items for this menu. |
| 50 virtual bool AlwaysShowImages() const { return false; } | 48 virtual bool AlwaysShowImages() const { return false; } |
| 51 }; | 49 }; |
| 52 | 50 |
| 53 // Builds a MenuGtk that uses |delegate| to perform actions and |menu_data| | 51 // Builds a MenuGtk that uses |delegate| to perform actions and |menu_data| |
| 54 // to create the menu. | 52 // to create the menu. |
| 55 MenuGtk(MenuGtk::Delegate* delegate, const MenuCreateMaterial* menu_data, | 53 MenuGtk(MenuGtk::Delegate* delegate, const MenuCreateMaterial* menu_data, |
| 56 GtkAccelGroup* accel_group); | 54 GtkAccelGroup* accel_group); |
| 57 // Creates a MenuGtk that uses |delegate| to perform actions. Builds the | 55 // Creates a MenuGtk that uses |delegate| to perform actions. Builds the |
| 58 // menu using |delegate| if |load| is true. | 56 // menu using |model_| if non-NULL. |
| 59 MenuGtk(MenuGtk::Delegate* delegate, bool load); | 57 // TODO(estade): MenuModel support is only partial. Only TYPE_SEPARATOR and |
| 58 // TYPE_COMMAND are currently implemented. |
| 59 MenuGtk(MenuGtk::Delegate* delegate, menus::MenuModel* model); |
| 60 ~MenuGtk(); | 60 ~MenuGtk(); |
| 61 | 61 |
| 62 // Initialize GTK signal handlers. | 62 // Initialize GTK signal handlers. |
| 63 void ConnectSignalHandlers(); | 63 void ConnectSignalHandlers(); |
| 64 | 64 |
| 65 // These methods are used to build the menu dynamically. | 65 // These methods are used to build the menu dynamically. |
| 66 void AppendMenuItemWithLabel(int command_id, const std::string& label); | 66 void AppendMenuItemWithLabel(int command_id, const std::string& label); |
| 67 void AppendMenuItemWithIcon(int command_id, const std::string& label, | 67 void AppendMenuItemWithIcon(int command_id, const std::string& label, |
| 68 const SkBitmap& icon); | 68 const SkBitmap& icon); |
| 69 void AppendCheckMenuItemWithLabel(int command_id, const std::string& label); | 69 void AppendCheckMenuItemWithLabel(int command_id, const std::string& label); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 // A recursive function that transforms a MenuCreateMaterial tree into a set | 117 // A recursive function that transforms a MenuCreateMaterial tree into a set |
| 118 // of GtkMenuItems. | 118 // of GtkMenuItems. |
| 119 void BuildMenuIn(GtkWidget* menu, | 119 void BuildMenuIn(GtkWidget* menu, |
| 120 const MenuCreateMaterial* menu_data, | 120 const MenuCreateMaterial* menu_data, |
| 121 GtkAccelGroup* accel_group); | 121 GtkAccelGroup* accel_group); |
| 122 | 122 |
| 123 // Builds a GtkImageMenuItem. | 123 // Builds a GtkImageMenuItem. |
| 124 GtkWidget* BuildMenuItemWithImage(const std::string& label, | 124 GtkWidget* BuildMenuItemWithImage(const std::string& label, |
| 125 const SkBitmap& icon); | 125 const SkBitmap& icon); |
| 126 | 126 |
| 127 // A function that creates a GtkMenu from |delegate_|. This function is not | 127 // A function that creates a GtkMenu from |model_|. |
| 128 // recursive and does not support sub-menus. | 128 void BuildMenuFromModel(); |
| 129 void BuildMenuFromDelegate(); | |
| 130 | 129 |
| 131 // Contains implementation for OnMenuShow. | 130 // Contains implementation for OnMenuShow. |
| 132 void UpdateMenu(); | 131 void UpdateMenu(); |
| 133 | 132 |
| 134 // Callback for when a menu item is clicked. | 133 // Callback for when a menu item is clicked. |
| 135 static void OnMenuItemActivated(GtkMenuItem* menuitem, MenuGtk* menu); | 134 static void OnMenuItemActivated(GtkMenuItem* menuitem, MenuGtk* menu); |
| 136 | 135 |
| 137 // Sets the check mark and enabled/disabled state on our menu items. | 136 // Sets the check mark and enabled/disabled state on our menu items. |
| 138 static void SetMenuItemInfo(GtkWidget* widget, void* raw_menu); | 137 static void SetMenuItemInfo(GtkWidget* widget, void* raw_menu); |
| 139 | 138 |
| 140 // Updates all the menu items' state. | 139 // Updates all the menu items' state. |
| 141 static void OnMenuShow(GtkWidget* widget, MenuGtk* menu); | 140 static void OnMenuShow(GtkWidget* widget, MenuGtk* menu); |
| 142 | 141 |
| 143 // Sets the activating widget back to a normal appearance. | 142 // Sets the activating widget back to a normal appearance. |
| 144 static void OnMenuHidden(GtkWidget* widget, MenuGtk* menu); | 143 static void OnMenuHidden(GtkWidget* widget, MenuGtk* menu); |
| 145 | 144 |
| 146 // Queries this object about the menu state. | 145 // Queries this object about the menu state. |
| 147 MenuGtk::Delegate* delegate_; | 146 MenuGtk::Delegate* delegate_; |
| 148 | 147 |
| 148 // If non-NULL, the MenuModel that we use to populate the GTK menu. |
| 149 menus::MenuModel* model_; |
| 150 |
| 149 // For some menu items, we want to show the accelerator, but not actually | 151 // For some menu items, we want to show the accelerator, but not actually |
| 150 // explicitly handle it. To this end we connect those menu items' accelerators | 152 // explicitly handle it. To this end we connect those menu items' accelerators |
| 151 // to this group, but don't attach this group to any top level window. | 153 // to this group, but don't attach this group to any top level window. |
| 152 GtkAccelGroup* dummy_accel_group_; | 154 GtkAccelGroup* dummy_accel_group_; |
| 153 | 155 |
| 154 // gtk_menu_popup() does not appear to take ownership of popup menus, so | 156 // gtk_menu_popup() does not appear to take ownership of popup menus, so |
| 155 // MenuGtk explicitly manages the lifetime of the menu. | 157 // MenuGtk explicitly manages the lifetime of the menu. |
| 156 OwnedWidgetGtk menu_; | 158 OwnedWidgetGtk menu_; |
| 157 | 159 |
| 158 // True when we should ignore "activate" signals. Used to prevent | 160 // True when we should ignore "activate" signals. Used to prevent |
| 159 // menu items from getting activated when we are setting up the | 161 // menu items from getting activated when we are setting up the |
| 160 // menu. | 162 // menu. |
| 161 static bool block_activation_; | 163 static bool block_activation_; |
| 162 | 164 |
| 163 // We must free these at shutdown. | 165 // We must free these at shutdown. |
| 164 std::vector<MenuGtk*> submenus_we_own_; | 166 std::vector<MenuGtk*> submenus_we_own_; |
| 165 | 167 |
| 166 ScopedRunnableMethodFactory<MenuGtk> factory_; | 168 ScopedRunnableMethodFactory<MenuGtk> factory_; |
| 167 }; | 169 }; |
| 168 | 170 |
| 169 #endif // CHROME_BROWSER_GTK_MENU_GTK_H_ | 171 #endif // CHROME_BROWSER_GTK_MENU_GTK_H_ |
| OLD | NEW |