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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
// Copyright (c) 2010 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 <algorithm>
#include <map>
#include <string>
#include "app/gfx/font.h"
#include "app/menus/menu_model.h"
#include "base/i18n/rtl.h"
#include "base/keyboard_code_conversion_gtk.h"
#include "base/keyboard_codes.h"
#include "base/message_loop.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "gfx/gtk_util.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";
const char kAccelGroupString[] = "accel_group";
// 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(Menu2* menu)
: parent_(NULL),
model_(menu->model()),
menu_(NULL),
menu_shown_(false),
suppress_activate_signal_(false),
activated_menu_(NULL),
activated_index_(-1),
activate_factory_(this),
host_menu_(menu) {
}
NativeMenuGtk::~NativeMenuGtk() {
if (menu_) {
// Don't call MenuDestroyed because menu2 has already been destroyed.
g_signal_handler_disconnect(menu_, destroy_handler_id_);
gtk_widget_destroy(menu_);
}
}
////////////////////////////////////////////////////////////////////////////////
// NativeMenuGtk, MenuWrapper implementation:
void NativeMenuGtk::RunMenuAt(const gfx::Point& point, int alignment) {
activated_menu_ = NULL;
activated_index_ = -1;
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(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;
if (activated_menu_) {
MessageLoop::current()->PostTask(FROM_HERE,
activate_factory_.NewRunnableMethod(
&NativeMenuGtk::ProcessActivate));
}
}
void NativeMenuGtk::CancelMenu() {
NOTIMPLEMENTED();
}
void NativeMenuGtk::Rebuild() {
activated_menu_ = NULL;
ResetMenu();
// Try to retrieve accelerator group as data from menu_; if null, create new
// one and store it as data into menu_.
// We store it as data so as to use the destroy notifier to get rid of initial
// reference count. For some reason, when we unref it ourselves (even in
// destructor), it would cause random crashes, depending on when gtk tries to
// access it.
GtkAccelGroup* accel_group = static_cast<GtkAccelGroup*>(
g_object_get_data(G_OBJECT(menu_), kAccelGroupString));
if (!accel_group) {
accel_group = gtk_accel_group_new();
g_object_set_data_full(G_OBJECT(menu_), kAccelGroupString, accel_group,
g_object_unref);
}
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, accel_group);
// |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, accel_group);
}
} else {
AddMenuItemAt(i, NULL, accel_group);
}
}
}
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,
GtkAccelGroup* accel_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;
}
// Label font.
const gfx::Font* font = model_->GetLabelFontAt(index);
if (font) {
// The label item is the first child of the menu item.
GtkWidget* label_widget = GTK_BIN(menu_item)->child;
DCHECK(label_widget && GTK_IS_LABEL(label_widget));
gtk_widget_modify_font(label_widget,
gfx::Font::PangoFontFromGfxFont(*font));
}
if (type == menus::MenuModel::TYPE_SUBMENU) {
Menu2* submenu = new Menu2(model_->GetSubmenuModelAt(index));
static_cast<NativeMenuGtk*>(submenu->wrapper_.get())->set_parent(this);
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 (accel_group && model_->GetAcceleratorAt(index, &accelerator)) {
int gdk_modifiers = 0;
if (accelerator.IsShiftDown())
gdk_modifiers |= GDK_SHIFT_MASK;
if (accelerator.IsCtrlDown())
gdk_modifiers |= GDK_CONTROL_MASK;
if (accelerator.IsAltDown())
gdk_modifiers |= GDK_MOD1_MASK;
gtk_widget_add_accelerator(menu_item, "activate", accel_group,
base::GdkKeyCodeForWindowsKeyCode(accelerator.GetKeyCode(), false),
static_cast<GdkModifierType>(gdk_modifiers), GTK_ACCEL_VISIBLE);
}
g_object_set_data(G_OBJECT(menu_item), kPositionString,
reinterpret_cast<void*>(index));
g_signal_connect(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_) {
g_signal_handler_disconnect(menu_, destroy_handler_id_);
gtk_widget_destroy(menu_);
}
menu_ = gtk_menu_new();
destroy_handler_id_ = g_signal_connect(
menu_, "destroy", G_CALLBACK(NativeMenuGtk::MenuDestroyed), host_menu_);
}
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);
*x = position->point.x();
*y = position->point.y();
views::Menu2::Alignment alignment = position->alignment;
if (base::i18n::IsRTL()) {
switch (alignment) {
case Menu2::ALIGN_TOPRIGHT:
alignment = Menu2::ALIGN_TOPLEFT;
break;
case Menu2::ALIGN_TOPLEFT:
alignment = Menu2::ALIGN_TOPRIGHT;
break;
default:
NOTREACHED();
break;
}
}
if (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;
}
// NOTE: we get activate messages for submenus when first shown.
if (model_->IsEnabledAt(position) &&
MenuTypeCanExecute(model_->GetTypeAt(position))) {
NativeMenuGtk* ancestor = GetAncestor();
ancestor->activated_menu_ = this;
activated_index_ = position;
}
}
// static
void NativeMenuGtk::CallActivate(GtkMenuItem* menu_item,
NativeMenuGtk* native_menu) {
native_menu->OnActivate(menu_item);
}
NativeMenuGtk* NativeMenuGtk::GetAncestor() {
NativeMenuGtk* ancestor = this;
while (ancestor->parent_)
ancestor = ancestor->parent_;
return ancestor;
}
void NativeMenuGtk::ProcessActivate() {
if (activated_menu_)
activated_menu_->Activate();
}
void NativeMenuGtk::Activate() {
if (model_->IsEnabledAt(activated_index_) &&
MenuTypeCanExecute(model_->GetTypeAt(activated_index_))) {
model_->ActivatedAt(activated_index_);
}
}
// static
void NativeMenuGtk::MenuDestroyed(GtkWidget* widget, Menu2* menu2) {
NativeMenuGtk* native_menu =
static_cast<NativeMenuGtk*>(menu2->wrapper_.get());
// The native gtk widget has already been destroyed.
native_menu->menu_ = NULL;
delete menu2;
}
////////////////////////////////////////////////////////////////////////////////
// MenuWrapper, public:
// static
MenuWrapper* MenuWrapper::CreateWrapper(Menu2* menu) {
return new NativeMenuGtk(menu);
}
} // namespace views
|