aboutsummaryrefslogtreecommitdiff
path: root/module/menuitem-proxy.c
blob: 6a3e5557d047aece1603684b16e9529621f55f81 (plain)
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
/* Contains code from GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "../libtopmenu-client/topmenu-monitor.h"

#include "menuitem-proxy.h"

static gboolean static_data_ok = FALSE;
static const gchar *pname_visible;
static const gchar *pname_sensitive;
static const gchar *pname_label;
static const gchar *pname_submenu;

static void init_static_data()
{
	if (!static_data_ok) {
		pname_visible = g_intern_string("visible");
		pname_sensitive = g_intern_string("sensitive");
		pname_label = g_intern_string("label");
		pname_submenu = g_intern_string("submenu");

		static_data_ok = TRUE;
	}
}

static void
free_timeval (GTimeVal *val)
{
	g_slice_free (GTimeVal, val);
}

static void
get_offsets (GtkMenu *menu,
             gint    *horizontal_offset,
             gint    *vertical_offset)
{
	gint vertical_padding;
	gint horizontal_padding;

	gtk_widget_style_get (GTK_WIDGET (menu),
	                      "horizontal-offset", horizontal_offset,
	                      "vertical-offset", vertical_offset,
	                      "horizontal-padding", &horizontal_padding,
	                      "vertical-padding", &vertical_padding,
	                      NULL);

	*vertical_offset -= GTK_WIDGET (menu)->style->ythickness;
	*vertical_offset -= vertical_padding;
	*horizontal_offset += horizontal_padding;
}

static void
menu_item_position_menu (GtkMenu  *menu,
                         gint     *x,
                         gint     *y,
                         gboolean *push_in,
                         gpointer  user_data)
{
	GtkMenuItem *menu_item;
	GtkWidget *widget;
	GtkMenuItem *parent_menu_item;
	GdkScreen *screen;
	gint twidth, theight;
	gint tx, ty;
	GtkTextDirection direction;
	GdkRectangle monitor;
	gint monitor_num;
	gint horizontal_offset;
	gint vertical_offset;
	gint parent_xthickness;
	gint available_left, available_right;

	g_return_if_fail (menu != NULL);
	g_return_if_fail (x != NULL);
	g_return_if_fail (y != NULL);

	menu_item = GTK_MENU_ITEM (user_data);
	widget = GTK_WIDGET (user_data);

	if (push_in)
		*push_in = FALSE;

	direction = gtk_widget_get_direction (widget);

	twidth = GTK_WIDGET (menu)->requisition.width;
	theight = GTK_WIDGET (menu)->requisition.height;

	screen = gtk_widget_get_screen (GTK_WIDGET (menu));
	monitor_num = gdk_screen_get_monitor_at_window (screen, menu_item->event_window);
	if (monitor_num < 0)
		monitor_num = 0;
	gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);

	if (!gdk_window_get_origin (widget->window, &tx, &ty))
	{
		g_warning ("Menu not on screen");
		return;
	}

	tx += widget->allocation.x;
	ty += widget->allocation.y;

	get_offsets (menu, &horizontal_offset, &vertical_offset);

	available_left = tx - monitor.x;
	available_right = monitor.x + monitor.width - (tx + widget->allocation.width);

	if (GTK_IS_MENU_BAR (widget->parent))
	{
		menu_item->from_menubar = TRUE;
	}
	else if (GTK_IS_MENU (widget->parent))
	{
		if (GTK_MENU (widget->parent)->parent_menu_item)
			menu_item->from_menubar = GTK_MENU_ITEM (GTK_MENU (widget->parent)->parent_menu_item)->from_menubar;
		else
			menu_item->from_menubar = FALSE;
	}
	else
	{
		menu_item->from_menubar = FALSE;
	}

	switch (menu_item->submenu_placement)
	{
	case GTK_TOP_BOTTOM:
		if (direction == GTK_TEXT_DIR_LTR)
			menu_item->submenu_direction = GTK_DIRECTION_RIGHT;
		else
		{
			menu_item->submenu_direction = GTK_DIRECTION_LEFT;
			tx += widget->allocation.width - twidth;
		}
		if ((ty + widget->allocation.height + theight) <= monitor.y + monitor.height)
			ty += widget->allocation.height;
		else if ((ty - theight) >= monitor.y)
			ty -= theight;
		else if (monitor.y + monitor.height - (ty + widget->allocation.height) > ty)
			ty += widget->allocation.height;
		else
			ty -= theight;
		break;

	case GTK_LEFT_RIGHT:
		if (GTK_IS_MENU (widget->parent))
			parent_menu_item = GTK_MENU_ITEM (GTK_MENU (widget->parent)->parent_menu_item);
		else
			parent_menu_item = NULL;

		parent_xthickness = widget->parent->style->xthickness;

		if (parent_menu_item && !GTK_MENU (widget->parent)->torn_off)
		{
			menu_item->submenu_direction = parent_menu_item->submenu_direction;
		}
		else
		{
			if (direction == GTK_TEXT_DIR_LTR)
				menu_item->submenu_direction = GTK_DIRECTION_RIGHT;
			else
				menu_item->submenu_direction = GTK_DIRECTION_LEFT;
		}

		switch (menu_item->submenu_direction)
		{
		case GTK_DIRECTION_LEFT:
			if (tx - twidth - parent_xthickness - horizontal_offset >= monitor.x ||
			        available_left >= available_right)
				tx -= twidth + parent_xthickness + horizontal_offset;
			else
			{
				menu_item->submenu_direction = GTK_DIRECTION_RIGHT;
				tx += widget->allocation.width + parent_xthickness + horizontal_offset;
			}
			break;

		case GTK_DIRECTION_RIGHT:
			if (tx + widget->allocation.width + parent_xthickness + horizontal_offset + twidth <= monitor.x + monitor.width ||
			        available_right >= available_left)
				tx += widget->allocation.width + parent_xthickness + horizontal_offset;
			else
			{
				menu_item->submenu_direction = GTK_DIRECTION_LEFT;
				tx -= twidth + parent_xthickness + horizontal_offset;
			}
			break;
		}

		ty += vertical_offset;

		/* If the height of the menu doesn't fit we move it upward. */
		ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight));
		break;
	}

	/* If we have negative, tx, here it is because we can't get
   * the menu all the way on screen. Favor the left portion.
   */
	*x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth));
	*y = ty;

	gtk_menu_set_monitor (menu, monitor_num);

	if (!gtk_widget_get_visible (menu->toplevel))
	{
		gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), menu_item->from_menubar?
		                              GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
	}
}

static void handle_menuitem_notify(GtkMenuItem *item, GParamSpec *pspec, GtkMenuItem *proxy)
{
	// Note that it is OK to compare strings by pointer as they are all interned
	if (pspec->name == pname_submenu) {
		// Nothing to do!
	} else if (pspec->name == pname_label) {
		const gchar *label = gtk_menu_item_get_label(item);
		gtk_menu_item_set_label(proxy, label);
	} else if (pspec->name == pname_sensitive) {
		gtk_widget_set_sensitive(GTK_WIDGET(proxy),
		                         gtk_widget_get_sensitive(GTK_WIDGET(item)));
	} else if (pspec->name == pname_visible) {
		if (gtk_widget_get_visible(GTK_WIDGET(item))) {
			gtk_widget_show(GTK_WIDGET(proxy));
		} else {
			gtk_widget_hide(GTK_WIDGET(proxy));
		}
	}
}

static gboolean handle_menuitem_mnemonic_activate(GtkMenuItem *item, gboolean cycling, GtkMenuItem *proxy)
{
	TopMenuMonitor *monitor = topmenu_monitor_get_instance();
	GtkWidget *parent = gtk_widget_get_parent(GTK_WIDGET(proxy));

	if (parent && monitor->available) {
		GtkMenuShell *parent_shell = GTK_MENU_SHELL(parent);
		if (GTK_IS_MENU_BAR(parent_shell) || parent_shell->active) {
			gtk_widget_mnemonic_activate(GTK_WIDGET(proxy), cycling);
			return TRUE;
		}
	}

	return FALSE;
}

static gboolean handle_menu_leave_notify(GtkMenu *menu, GdkEvent *event, GtkMenuItem *item)
{
	return TRUE;
}

static void handle_parent_move_current(GtkMenuShell *shell, GtkMenuDirectionType dir, GtkMenuItem *item)
{
	if (dir == GTK_MENU_DIR_CHILD) {
		GtkWidget *submenu = gtk_menu_item_get_submenu(item);
		if (submenu) {
			gtk_menu_shell_select_first(GTK_MENU_SHELL(submenu), TRUE);
		}
	}
}

static void handle_proxy_select(GtkMenuItem *proxy, GtkMenuItem *item)
{
	GtkWidget *submenu = gtk_menu_item_get_submenu(item);

	if (submenu) {
		if (!gtk_widget_is_sensitive(GTK_WIDGET(submenu)))
			return;

		GtkMenuShell *parent_shell = GTK_MENU_SHELL(GTK_WIDGET(proxy)->parent);
		GTimeVal *popup_time = g_slice_new0(GTimeVal);

        g_get_current_time(popup_time);
		g_object_set_data_full(G_OBJECT(submenu),
		                                "gtk-menu-exact-popup-time", popup_time,
		                                (GDestroyNotify) free_timeval);

		g_signal_connect_object(submenu, "leave-notify-event",
		                        G_CALLBACK(handle_menu_leave_notify), item, 0);

		g_signal_connect_object(gtk_widget_get_parent(GTK_WIDGET(proxy)), "move-current",
		                        G_CALLBACK(handle_parent_move_current), item, 0);

		gtk_menu_popup(GTK_MENU(submenu),
		               GTK_WIDGET(parent_shell),
		               GTK_WIDGET(proxy),
		               menu_item_position_menu,
		               proxy,
		               parent_shell->button,
		               0);
	}
}

static void handle_proxy_deselect(GtkMenuItem *proxy, GtkMenuItem *item)
{
	GtkWidget *submenu = gtk_menu_item_get_submenu(item);

	if (submenu) {
		g_signal_handlers_disconnect_by_func(submenu, handle_menu_leave_notify, item);
		g_signal_handlers_disconnect_by_func(gtk_widget_get_parent(GTK_WIDGET(proxy)),
		                                     handle_parent_move_current, item);
		gtk_menu_popdown(GTK_MENU(submenu));
	}
}

static void handle_proxy_activate(GtkMenuItem *proxy, GtkMenuItem *item)
{
	GtkWidget *submenu = gtk_menu_item_get_submenu(item);

	if (submenu) {
		// Do nothing!
	} else {
		gtk_menu_item_activate(item);
	}
}

static void handle_proxy_activate_item(GtkMenuItem *proxy, GtkMenuItem *item)
{
	GtkWidget *submenu = gtk_menu_item_get_submenu(item);

	if (submenu) {
		GtkMenuShell *parent = GTK_MENU_SHELL(gtk_widget_get_parent(GTK_WIDGET(proxy)));
		if (parent) {
			if (!parent->active) {
				//gtk_grab_add(GTK_WIDGET(parent));
				//parent->have_grab = TRUE;
				parent->active = TRUE;
			}
			gtk_menu_shell_select_item(parent, GTK_WIDGET(proxy));
			gtk_menu_shell_select_first(GTK_MENU_SHELL(submenu), TRUE);
		}
	}
}

static GtkWidget *construct_image_widget_proxy(GtkImage *widget)
{
	GtkImageType itype = gtk_image_get_storage_type(widget);
	gchar *icon_name;
	GtkIconSize icon_size;
	switch (itype) {
	case GTK_IMAGE_STOCK:
		gtk_image_get_stock(widget, &icon_name, &icon_size);
		return gtk_image_new_from_stock(icon_name, icon_size);
	case GTK_IMAGE_EMPTY:
		return gtk_image_new();
	default:
		return gtk_image_new();
	}
}

GtkMenuItem *topmenu_create_proxy_menu_item(GtkMenuItem *item)
{
	init_static_data();

	GtkMenuItem *proxy = NULL;

	const gchar *label = gtk_menu_item_get_label(item);

	if (GTK_IS_IMAGE_MENU_ITEM(item)) {
		GtkImageMenuItem *iitem = GTK_IMAGE_MENU_ITEM(item);
		if (gtk_image_menu_item_get_use_stock(iitem)) {
			proxy = GTK_MENU_ITEM(gtk_image_menu_item_new_from_stock(label, NULL));
		} else {
			GtkWidget *iwidget = gtk_image_menu_item_get_image(iitem);
			proxy = GTK_MENU_ITEM(gtk_image_menu_item_new_with_mnemonic(label));
			if (iwidget) {
				// Let's suppport some common widget types
				if (GTK_IS_IMAGE(iwidget)) {
					gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(proxy),
					                              construct_image_widget_proxy(GTK_IMAGE(iwidget)));
				}
			}
		}
		gtk_image_menu_item_set_always_show_image(GTK_IMAGE_MENU_ITEM(proxy),
		                                          gtk_image_menu_item_get_always_show_image(iitem));
	}
	else if (GTK_IS_SEPARATOR_MENU_ITEM(item)) {
		proxy = GTK_MENU_ITEM(gtk_separator_menu_item_new());
	} else {
		proxy = GTK_MENU_ITEM(gtk_menu_item_new_with_mnemonic(label));
	}

	gtk_widget_set_sensitive(GTK_WIDGET(proxy),
	                         gtk_widget_get_sensitive(GTK_WIDGET(item)));

	if (gtk_widget_get_visible(GTK_WIDGET(item))) {
		gtk_widget_show(GTK_WIDGET(proxy));
	}

	g_signal_connect_object(item, "notify",
	                        G_CALLBACK(handle_menuitem_notify), proxy, 0);
	g_signal_connect_object(item, "mnemonic-activate",
	                        G_CALLBACK(handle_menuitem_mnemonic_activate), proxy, 0);

	g_signal_connect_object(proxy, "select",
	                        G_CALLBACK(handle_proxy_select), item, 0);
	g_signal_connect_object(proxy, "deselect",
	                        G_CALLBACK(handle_proxy_deselect), item, 0);
	g_signal_connect_object(proxy, "activate",
	                        G_CALLBACK(handle_proxy_activate), item, 0);
	g_signal_connect_object(proxy, "activate-item",
	                        G_CALLBACK(handle_proxy_activate_item), item, 0);

	return proxy;
}