OLD | NEW |
| (Empty) |
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 UI_VIEWS_CONTROLS_MENU_NATIVE_MENU_GTK_H_ | |
6 #define UI_VIEWS_CONTROLS_MENU_NATIVE_MENU_GTK_H_ | |
7 #pragma once | |
8 | |
9 #include <gtk/gtk.h> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/message_loop.h" | |
15 #include "base/observer_list.h" | |
16 #include "ui/base/gtk/gtk_signal.h" | |
17 #include "ui/views/controls/menu/menu_wrapper.h" | |
18 | |
19 namespace ui { | |
20 class MenuModel; | |
21 } | |
22 | |
23 namespace views { | |
24 | |
25 class NestedDispatcherGtk; | |
26 | |
27 // A Gtk implementation of MenuWrapper. | |
28 // | |
29 // NOTE: On windows the activate message is not sent immediately when an item | |
30 // is selected. Instead a message is added to the queue that is processed later. | |
31 // To simulate that (and avoid having to deal with different behavior between | |
32 // the platforms) we mimick that by posting a task after a menu item is selected | |
33 // then notify. | |
34 // | |
35 // TODO(beng): rename to MenuGtk once the old class is dead. | |
36 class NativeMenuGtk : public MenuWrapper, | |
37 public MessageLoopForUI::Dispatcher { | |
38 public: | |
39 explicit NativeMenuGtk(Menu2* menu); | |
40 virtual ~NativeMenuGtk(); | |
41 | |
42 // Overridden from MenuWrapper: | |
43 virtual void RunMenuAt(const gfx::Point& point, int alignment) OVERRIDE; | |
44 virtual void CancelMenu() OVERRIDE; | |
45 virtual void Rebuild() OVERRIDE; | |
46 virtual void UpdateStates() OVERRIDE; | |
47 virtual gfx::NativeMenu GetNativeMenu() const OVERRIDE; | |
48 virtual MenuAction GetMenuAction() const OVERRIDE; | |
49 virtual void AddMenuListener(MenuListener* listener) OVERRIDE; | |
50 virtual void RemoveMenuListener(MenuListener* listener) OVERRIDE; | |
51 virtual void SetMinimumWidth(int width) OVERRIDE; | |
52 | |
53 // Overriden from MessageLoopForUI::Dispatcher: | |
54 virtual bool Dispatch(GdkEvent* event) OVERRIDE; | |
55 | |
56 private: | |
57 CHROMEGTK_CALLBACK_0(NativeMenuGtk, void, OnMenuHidden); | |
58 CHROMEGTK_CALLBACK_1(NativeMenuGtk, void, OnMenuMoveCurrent, | |
59 GtkMenuDirectionType); | |
60 CHROMEGTK_CALLBACK_1(NativeMenuGtk, void, AfterMenuMoveCurrent, | |
61 GtkMenuDirectionType); | |
62 CHROMEGTK_CALLBACK_1(NativeMenuGtk, gboolean, OnExpose, GdkEventExpose*); | |
63 | |
64 void AddSeparatorAt(int index); | |
65 GtkWidget* AddMenuItemAt(int index, GtkRadioMenuItem* radio_group, | |
66 GtkAccelGroup* accel_group); | |
67 | |
68 void ResetMenu(); | |
69 | |
70 // Updates the menu item's state. | |
71 void UpdateMenuItemState(GtkWidget* menu_item, bool recurse); | |
72 | |
73 static void UpdateStateCallback(GtkWidget* menu_item, gpointer data); | |
74 | |
75 // Callback for gtk_menu_popup to position the menu. | |
76 static void MenuPositionFunc(GtkMenu* menu, int* x, int* y, gboolean* push_in, | |
77 void* data); | |
78 | |
79 // Event handlers: | |
80 void OnActivate(GtkMenuItem* menu_item); | |
81 | |
82 // Gtk signal handlers. | |
83 static void CallActivate(GtkMenuItem* menu_item, NativeMenuGtk* native_menu); | |
84 | |
85 // Sets the parent of this menu. | |
86 void set_parent(NativeMenuGtk* parent) { parent_ = parent; } | |
87 | |
88 // Returns the root of the menu tree. | |
89 NativeMenuGtk* GetAncestor(); | |
90 | |
91 // Callback that we should really process the menu activation. | |
92 // See description above class for why we delay processing activation. | |
93 void ProcessActivate(); | |
94 | |
95 // Notifies the model the user selected an item. | |
96 void Activate(); | |
97 | |
98 void SendAccessibilityEvent(); | |
99 | |
100 // A callback to delete menu2 object when the native widget is | |
101 // destroyed first. | |
102 static void MenuDestroyed(GtkWidget* widget, Menu2* menu2); | |
103 | |
104 // If we're a submenu, this is the parent. | |
105 NativeMenuGtk* parent_; | |
106 | |
107 ui::MenuModel* model_; | |
108 | |
109 GtkWidget* menu_; | |
110 | |
111 // Has the menu been hidden? | |
112 // NOTE: this is maintained by us and do to the asynchronous nature of X may | |
113 // be out of sync with whether the window is actually hidden. None-the-less if | |
114 // true the menu is either truly hidden or in the process of hiding. | |
115 bool menu_hidden_; | |
116 | |
117 // A flag used to avoid misfiring ActivateAt call on the menu model. | |
118 // This is necessary as GTK menu fires an activate signal even when the | |
119 // state is changed by |UpdateStates()| API. | |
120 bool suppress_activate_signal_; | |
121 | |
122 // If the user selects something from the menu this is the menu they selected | |
123 // it from. When an item is selected menu_activated_ on the root ancestor is | |
124 // set to the menu the user selected and after the nested message loop exits | |
125 // Activate is invoked on this menu. | |
126 NativeMenuGtk* activated_menu_; | |
127 | |
128 // The index of the item the user selected. This is set on the actual menu the | |
129 // user selects and not the root. | |
130 int activated_index_; | |
131 | |
132 // Used when a menu item is selected. See description above class as to why | |
133 // we do this. | |
134 base::WeakPtrFactory<NativeMenuGtk> activate_factory_; | |
135 | |
136 // A eference to the hosting menu2 object and signal handler id | |
137 // used to delete the menu2 when its native menu gtk is destroyed first. | |
138 Menu2* host_menu_; | |
139 gulong destroy_handler_id_; | |
140 gulong expose_handler_id_; | |
141 | |
142 // The action that took place during the call to RunMenuAt. | |
143 MenuAction menu_action_; | |
144 | |
145 // A list of listeners to call when the menu opens. | |
146 ObserverList<MenuListener> listeners_; | |
147 | |
148 // Nested dispatcher object that can outlive this object. | |
149 // This is to deal with the menu being deleted while the nested | |
150 // message loop is handled. see http://crosbug.com/7228 . | |
151 NestedDispatcherGtk* nested_dispatcher_; | |
152 | |
153 // A flag used to detect a button release event without button press or move. | |
154 // see http://crosbug.com/8718. | |
155 bool ignore_button_release_; | |
156 | |
157 DISALLOW_COPY_AND_ASSIGN(NativeMenuGtk); | |
158 }; | |
159 | |
160 } // namespace views | |
161 | |
162 #endif // UI_VIEWS_CONTROLS_MENU_NATIVE_MENU_GTK_H_ | |
OLD | NEW |