1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
#include "views/controls/menu/native_menu_gtk.h"
#include <map>
#include <string>
#include "app/gfx/gtk_util.h"
#include "app/menus/menu_model.h"
#include "base/keyboard_codes.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/time.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "views/accelerator.h"
#include "views/controls/menu/menu_2.h"
namespace {
const char kPositionString[] = "position";
// Data passed to the MenuPositionFunc from gtk_menu_popup
struct Position {
// The point to run the menu at.
gfx::Point point;
// The alignment of the menu at that point.
views::Menu2::Alignment alignment;
};
std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) {
std::string ret;
ret.reserve(label.length());
for (size_t i = 0; i < label.length(); ++i) {
if ('&' == label[i]) {
if (i + 1 < label.length() && '&' == label[i + 1]) {
ret.push_back(label[i]);
++i;
} else {
ret.push_back('_');
}
} else {
ret.push_back(label[i]);
}
}
return ret;
}
// Returns true if the menu item type specified can be executed as a command.
bool MenuTypeCanExecute(menus::MenuModel::ItemType type) {
return type == menus::MenuModel::TYPE_COMMAND ||
type == menus::MenuModel::TYPE_CHECK ||
type == menus::MenuModel::TYPE_RADIO;
}
} // namespace
namespace views {
////////////////////////////////////////////////////////////////////////////////
// NativeMenuGtk, public:
NativeMenuGtk::NativeMenuGtk(menus::MenuModel* model)
: model_(model),
menu_(NULL),
menu_shown_(false),
suppress_activate_signal_(false),
menu_activated_(false),
activated_index_(-1) {
}
NativeMenuGtk::~NativeMenuGtk() {
gtk_widget_destroy(menu_);
}
////////////////////////////////////////////////////////////////////////////////
// NativeMenuGtk, MenuWrapper implementation:
void NativeMenuGtk::RunMenuAt(const gfx::Point& point, int alignment) {
menu_activated_ = false;
UpdateStates();
Position position = { point, static_cast<Menu2::Alignment>(alignment) };
// TODO(beng): value of '1' will not work for context menus!
gtk_menu_popup(GTK_MENU(menu_), NULL, NULL, MenuPositionFunc, &position, 1,
gtk_get_current_event_time());
DCHECK(!menu_shown_);
menu_shown_ = true;
// Listen for "hide" signal so that we know when to return from the blocking
// RunMenuAt call.
gint handle_id =
g_signal_connect(G_OBJECT(menu_), "hide", G_CALLBACK(OnMenuHidden), this);
// Block until menu is no longer shown by running a nested message loop.
MessageLoopForUI::current()->Run(NULL);
g_signal_handler_disconnect(G_OBJECT(menu_), handle_id);
menu_shown_ = false;
// Call into the model after the nested message loop quits. This way if the
// model ends up deleting us, or MessageLoop::Quit takes a while, there aren't
// any problems.
if (menu_activated_ && model_->IsEnabledAt(activated_index_) &&
MenuTypeCanExecute(model_->GetTypeAt(activated_index_))) {
model_->ActivatedAt(activated_index_);
}
}
void NativeMenuGtk::CancelMenu() {
NOTIMPLEMENTED();
}
void NativeMenuGtk::Rebuild() {
ResetMenu();
std::map<int, GtkRadioMenuItem*> radio_groups_;
for (int i = 0; i < model_->GetItemCount(); ++i) {
menus::MenuModel::ItemType type = model_->GetTypeAt(i);
if (type == menus::MenuModel::TYPE_SEPARATOR) {
AddSeparatorAt(i);
} else if (type == menus::MenuModel::TYPE_RADIO) {
const int radio_group_id = model_->GetGroupIdAt(i);
std::map<int, GtkRadioMenuItem*>::const_iterator iter
= radio_groups_.find(radio_group_id);
if (iter == radio_groups_.end()) {
GtkWidget* new_menu_item = AddMenuItemAt(i, NULL);
// |new_menu_item| is the first menu item for |radio_group_id| group.
radio_groups_.insert(
std::make_pair(radio_group_id, GTK_RADIO_MENU_ITEM(new_menu_item)));
} else {
AddMenuItemAt(i, iter->second);
}
} else {
AddMenuItemAt(i, NULL);
}
}
}
void NativeMenuGtk::UpdateStates() {
gtk_container_foreach(GTK_CONTAINER(menu_), &UpdateStateCallback, this);
}
gfx::NativeMenu NativeMenuGtk::GetNativeMenu() const {
return menu_;
}
////////////////////////////////////////////////////////////////////////////////
// NativeMenuGtk, private:
// static
void NativeMenuGtk::OnMenuHidden(GtkWidget* widget, NativeMenuGtk* menu) {
if (!menu->menu_shown_) {
// This indicates we don't have a menu open, and should never happen.
NOTREACHED();
return;
}
// Quit the nested message loop we spawned in RunMenuAt.
MessageLoop::current()->Quit();
}
void NativeMenuGtk::AddSeparatorAt(int index) {
GtkWidget* separator = gtk_separator_menu_item_new();
gtk_widget_show(separator);
gtk_menu_append(menu_, separator);
}
GtkWidget* NativeMenuGtk::AddMenuItemAt(int index,
GtkRadioMenuItem* radio_group) {
GtkWidget* menu_item = NULL;
std::string label = ConvertAcceleratorsFromWindowsStyle(UTF16ToUTF8(
model_->GetLabelAt(index)));
menus::MenuModel::ItemType type = model_->GetTypeAt(index);
switch (type) {
case menus::MenuModel::TYPE_CHECK:
menu_item = gtk_check_menu_item_new_with_mnemonic(label.c_str());
break;
case menus::MenuModel::TYPE_RADIO:
if (radio_group) {
menu_item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
radio_group, label.c_str());
} else {
// The item does not belong to any existing radio button groups.
menu_item = gtk_radio_menu_item_new_with_mnemonic(NULL, label.c_str());
}
break;
case menus::MenuModel::TYPE_SUBMENU:
case menus::MenuModel::TYPE_COMMAND: {
SkBitmap icon;
// Create menu item with icon if icon exists.
if (model_->HasIcons() && model_->GetIconAt(index, &icon)) {
menu_item = gtk_image_menu_item_new_with_mnemonic(label.c_str());
GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(&icon);
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item),
gtk_image_new_from_pixbuf(pixbuf));
} else {
menu_item = gtk_menu_item_new_with_mnemonic(label.c_str());
}
break;
}
default:
NOTREACHED();
break;
}
if (type == menus::MenuModel::TYPE_SUBMENU) {
// TODO(beng): we're leaking these objects right now... consider some other
// arrangement.
Menu2* submenu = new Menu2(model_->GetSubmenuModelAt(index));
g_object_set_data(G_OBJECT(menu_item), "submenu", submenu);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item),
submenu->GetNativeMenu());
}
views::Accelerator accelerator(base::VKEY_UNKNOWN, false, false, false);
if (model_->GetAcceleratorAt(index, &accelerator)) {
// TODO(beng): accelerators w/gtk_widget_add_accelerator.
}
g_object_set_data(G_OBJECT(menu_item), kPositionString,
reinterpret_cast<void*>(index));
g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(CallActivate),
this);
gtk_widget_show(menu_item);
gtk_menu_append(menu_, menu_item);
return menu_item;
}
void NativeMenuGtk::ResetMenu() {
if (menu_)
gtk_widget_destroy(menu_);
menu_ = gtk_menu_new();
}
void NativeMenuGtk::UpdateMenuItemState(GtkWidget* menu_item) {
int index = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu_item),
kPositionString));
gtk_widget_set_sensitive(menu_item, model_->IsEnabledAt(index));
if (GTK_IS_CHECK_MENU_ITEM(menu_item)) {
suppress_activate_signal_ = true;
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item),
model_->IsItemCheckedAt(index));
suppress_activate_signal_ = false;
}
// Recurse into submenus, too.
if (GTK_IS_MENU_ITEM(menu_item)) {
if (gtk_menu_item_get_submenu(GTK_MENU_ITEM(menu_item))) {
Menu2* submenu =
reinterpret_cast<Menu2*>(g_object_get_data(G_OBJECT(menu_item),
"submenu"));
if (submenu)
submenu->UpdateStates();
}
}
}
// static
void NativeMenuGtk::UpdateStateCallback(GtkWidget* menu_item, gpointer data) {
NativeMenuGtk* menu = reinterpret_cast<NativeMenuGtk*>(data);
menu->UpdateMenuItemState(menu_item);
}
// static
void NativeMenuGtk::MenuPositionFunc(GtkMenu* menu,
int* x,
int* y,
gboolean* push_in,
void* data) {
Position* position = reinterpret_cast<Position*>(data);
GtkRequisition menu_req;
gtk_widget_size_request(GTK_WIDGET(menu), &menu_req);
// TODO(beng): RTL
*x = position->point.x();
*y = position->point.y();
if (position->alignment == Menu2::ALIGN_TOPRIGHT)
*x -= menu_req.width;
// Make sure the popup fits on screen.
GdkScreen* screen = gtk_widget_get_screen(GTK_WIDGET(menu));
*x = std::max(0, std::min(gdk_screen_get_width(screen) - menu_req.width, *x));
*y = std::max(0, std::min(gdk_screen_get_height(screen) - menu_req.height,
*y));
*push_in = FALSE;
}
void NativeMenuGtk::OnActivate(GtkMenuItem* menu_item) {
if (suppress_activate_signal_)
return;
int position = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menu_item),
kPositionString));
// Ignore the signal if it's sent to an inactive checked radio item.
//
// Suppose there are three radio items A, B, C, and A is now being
// checked. If you click C, "activate" signal will be sent to A and C.
// Here, we ignore the signal sent to A.
if (GTK_IS_RADIO_MENU_ITEM(menu_item) &&
!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menu_item))) {
return;
}
menu_activated_ = true;
activated_index_ = position;
}
// static
void NativeMenuGtk::CallActivate(GtkMenuItem* menu_item,
NativeMenuGtk* native_menu) {
native_menu->OnActivate(menu_item);
}
////////////////////////////////////////////////////////////////////////////////
// MenuWrapper, public:
// static
MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) {
return new NativeMenuGtk(menu->model());
}
} // namespace views
|