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
|
// 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 "chrome/browser/gtk/gtk_custom_menu.h"
#include "chrome/browser/gtk/gtk_custom_menu_item.h"
G_DEFINE_TYPE(GtkCustomMenu, gtk_custom_menu, GTK_TYPE_MENU)
// Stolen directly from gtkmenushell.c. I'd love to call the library version
// instead, but it's static and isn't exported. :(
static gint gtk_menu_shell_is_item(GtkMenuShell* menu_shell,
GtkWidget* child) {
GtkWidget *parent;
g_return_val_if_fail(GTK_IS_MENU_SHELL(menu_shell), FALSE);
g_return_val_if_fail(child != NULL, FALSE);
parent = child->parent;
while (GTK_IS_MENU_SHELL(parent)) {
if (parent == reinterpret_cast<GtkWidget*>(menu_shell))
return TRUE;
parent = GTK_MENU_SHELL(parent)->parent_menu_shell;
}
return FALSE;
}
// Stolen directly from gtkmenushell.c. I'd love to call the library version
// instead, but it's static and isn't exported. :(
static GtkWidget* gtk_menu_shell_get_item(GtkMenuShell* menu_shell,
GdkEvent* event) {
GtkWidget* menu_item = gtk_get_event_widget(event);
while (menu_item && !GTK_IS_MENU_ITEM(menu_item))
menu_item = menu_item->parent;
if (menu_item && gtk_menu_shell_is_item(menu_shell, menu_item))
return menu_item;
else
return NULL;
}
// When processing a button event, abort processing if the cursor isn't in a
// clickable region.
static gboolean gtk_custom_menu_button_press(GtkWidget* widget,
GdkEventButton* event) {
GtkWidget* menu_item = gtk_menu_shell_get_item(
GTK_MENU_SHELL(widget), reinterpret_cast<GdkEvent*>(event));
if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) {
if (!gtk_custom_menu_item_is_in_clickable_region(
GTK_CUSTOM_MENU_ITEM(menu_item))) {
return TRUE;
}
}
return GTK_WIDGET_CLASS(gtk_custom_menu_parent_class)->
button_press_event(widget, event);
}
// When processing a button event, abort processing if the cursor isn't in a
// clickable region. If it's in a button that doesn't dismiss the menu, fire
// that event and abort having the normal GtkMenu code run.
static gboolean gtk_custom_menu_button_release(GtkWidget* widget,
GdkEventButton* event) {
GtkWidget* menu_item = gtk_menu_shell_get_item(
GTK_MENU_SHELL(widget), reinterpret_cast<GdkEvent*>(event));
if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) {
if (!gtk_custom_menu_item_is_in_clickable_region(
GTK_CUSTOM_MENU_ITEM(menu_item))) {
// Stop processing this event. This isn't a clickable region.
return TRUE;
}
if (gtk_custom_menu_item_try_no_dismiss_command(
GTK_CUSTOM_MENU_ITEM(menu_item))) {
return TRUE;
}
}
return GTK_WIDGET_CLASS(gtk_custom_menu_parent_class)->
button_release_event(widget, event);
}
// Manually forward button press events to the menu item (and then do what we'd
// do normally).
static gboolean gtk_custom_menu_motion_notify(GtkWidget* widget,
GdkEventMotion* event) {
GtkWidget* menu_item = gtk_menu_shell_get_item(
GTK_MENU_SHELL(widget), (GdkEvent*)event);
if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) {
gtk_custom_menu_item_receive_motion_event(GTK_CUSTOM_MENU_ITEM(menu_item),
event->x, event->y);
}
return GTK_WIDGET_CLASS(gtk_custom_menu_parent_class)->
motion_notify_event(widget, event);
}
static void gtk_custom_menu_move_current(GtkMenuShell* menu_shell,
GtkMenuDirectionType direction) {
// If the currently selected item is custom, we give it first chance to catch
// up/down events.
// TODO(erg): We are breaking a GSEAL by directly accessing this. We'll need
// to fix this by the time gtk3 comes out.
GtkWidget* menu_item = GTK_MENU_SHELL(menu_shell)->active_menu_item;
if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) {
switch (direction) {
case GTK_MENU_DIR_PREV:
case GTK_MENU_DIR_NEXT:
if (gtk_custom_menu_item_handle_move(GTK_CUSTOM_MENU_ITEM(menu_item),
direction))
return;
break;
default:
break;
}
}
GTK_MENU_SHELL_CLASS(gtk_custom_menu_parent_class)->
move_current(menu_shell, direction);
// In the case of hitting PREV and transitioning to a custom menu, we want to
// make sure we're selecting the final item in the list, not the first one.
menu_item = GTK_MENU_SHELL(menu_shell)->active_menu_item;
if (GTK_IS_CUSTOM_MENU_ITEM(menu_item)) {
gtk_custom_menu_item_select_item_by_direction(
GTK_CUSTOM_MENU_ITEM(menu_item), direction);
}
}
static void gtk_custom_menu_init(GtkCustomMenu* menu) {
}
static void gtk_custom_menu_class_init(GtkCustomMenuClass* klass) {
GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(klass);
GtkMenuShellClass* menu_shell_class = GTK_MENU_SHELL_CLASS(klass);
widget_class->button_press_event = gtk_custom_menu_button_press;
widget_class->button_release_event = gtk_custom_menu_button_release;
widget_class->motion_notify_event = gtk_custom_menu_motion_notify;
menu_shell_class->move_current = gtk_custom_menu_move_current;
}
GtkWidget* gtk_custom_menu_new() {
return GTK_WIDGET(g_object_new(GTK_TYPE_CUSTOM_MENU, NULL));
}
|