1 /* Marco window menu */
2
3 /*
4 * Copyright (C) 2001 Havoc Pennington
5 * Copyright (C) 2004 Rob Adams
6 * Copyright (C) 2005 Elijah Newren
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24 #include <config.h>
25 #include <glib/gi18n-lib.h>
26
27 #include <gdk/gdkx.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include "menu.h"
31 #include "main.h"
32 #include "util.h"
33 #include "core.h"
34 #include "metaaccellabel.h"
35 #include "ui.h"
36
37 typedef struct _MenuItem MenuItem;
38 typedef struct _MenuData MenuData;
39
40 typedef enum {
41 MENU_ITEM_SEPARATOR = 0,
42 MENU_ITEM_NORMAL,
43 MENU_ITEM_IMAGE,
44 MENU_ITEM_CHECKBOX,
45 MENU_ITEM_RADIOBUTTON,
46 MENU_ITEM_WORKSPACE_LIST,
47 } MetaMenuItemType;
48
49 struct _MenuItem {
50 MetaMenuOp op;
51 MetaMenuItemType type;
52 const char* stock_id;
53 const gboolean checked;
54 const char* label;
55 };
56
57 struct _MenuData {
58 MetaWindowMenu* menu;
59 MetaMenuOp op;
60 };
61
62 static void activate_cb(GtkWidget* menuitem, gpointer data);
63
64 static MenuItem menuitems[] = {
65 /* Translators: Translate this string the same way as you do in libwnck! */
66 {META_MENU_OP_MINIMIZE, MENU_ITEM_IMAGE, MARCO_STOCK_MINIMIZE, FALSE, N_("Mi_nimize")},
67 /* Translators: Translate this string the same way as you do in libwnck! */
68 {META_MENU_OP_MAXIMIZE, MENU_ITEM_IMAGE, MARCO_STOCK_MAXIMIZE, FALSE, N_("Ma_ximize")},
69 /* Translators: Translate this string the same way as you do in libwnck! */
70 {META_MENU_OP_UNMAXIMIZE, MENU_ITEM_IMAGE, MARCO_STOCK_RESTORE, FALSE, N_("Unma_ximize")},
71 /* Translators: Translate this string the same way as you do in libwnck! */
72 {META_MENU_OP_SHADE, MENU_ITEM_NORMAL, NULL, FALSE, N_("Roll _Up")},
73 /* Translators: Translate this string the same way as you do in libwnck! */
74 {META_MENU_OP_UNSHADE, MENU_ITEM_NORMAL, NULL, FALSE, N_("_Unroll")},
75 /* Translators: Translate this string the same way as you do in libwnck! */
76 {META_MENU_OP_MOVE, MENU_ITEM_NORMAL, NULL, FALSE, N_("_Move") },
77 /* Translators: Translate this string the same way as you do in libwnck! */
78 {META_MENU_OP_RESIZE, MENU_ITEM_NORMAL, NULL, FALSE, N_("_Resize")},
79 /* Translators: Translate this string the same way as you do in libwnck! */
80 {META_MENU_OP_RECOVER, MENU_ITEM_NORMAL, NULL, FALSE, N_("Move Titlebar On_screen")},
81 {META_MENU_OP_WORKSPACES, MENU_ITEM_SEPARATOR, NULL, FALSE, NULL}, /* separator */
82 /* Translators: Translate this string the same way as you do in libwnck! */
83 {META_MENU_OP_ABOVE, MENU_ITEM_CHECKBOX, NULL, FALSE, N_("Always on _Top")},
84 /* Translators: Translate this string the same way as you do in libwnck! */
85 {META_MENU_OP_UNABOVE, MENU_ITEM_CHECKBOX, NULL, TRUE, N_("Always on _Top")},
86 /* Translators: Translate this string the same way as you do in libwnck! */
87 {META_MENU_OP_STICK, MENU_ITEM_RADIOBUTTON, NULL, FALSE, N_("_Always on Visible Workspace")},
88 /* Translators: Translate this string the same way as you do in libwnck! */
89 {META_MENU_OP_UNSTICK, MENU_ITEM_RADIOBUTTON, NULL, FALSE, N_("_Only on This Workspace")},
90 /* Translators: Translate this string the same way as you do in libwnck! */
91 {META_MENU_OP_MOVE_LEFT, MENU_ITEM_NORMAL, NULL, FALSE, N_("Move to Workspace _Left")},
92 /* Translators: Translate this string the same way as you do in libwnck! */
93 {META_MENU_OP_MOVE_RIGHT, MENU_ITEM_NORMAL, NULL, FALSE, N_("Move to Workspace R_ight")},
94 /* Translators: Translate this string the same way as you do in libwnck! */
95 {META_MENU_OP_MOVE_UP, MENU_ITEM_NORMAL, NULL, FALSE, N_("Move to Workspace _Up")},
96 /* Translators: Translate this string the same way as you do in libwnck! */
97 {META_MENU_OP_MOVE_DOWN, MENU_ITEM_NORMAL, NULL, FALSE, N_("Move to Workspace _Down")},
98 {0, MENU_ITEM_WORKSPACE_LIST, NULL, FALSE, NULL},
99 {0, MENU_ITEM_SEPARATOR, NULL, FALSE, NULL}, /* separator */
100 /* Translators: Translate this string the same way as you do in libwnck! */
101 {META_MENU_OP_DELETE, MENU_ITEM_IMAGE, MARCO_STOCK_DELETE, FALSE, N_("_Close")}
102 };
103
popup_position_func(GtkMenu * menu,gint * x,gint * y,gboolean * push_in,gpointer user_data)104 static void popup_position_func(GtkMenu* menu, gint* x, gint* y, gboolean* push_in, gpointer user_data)
105 {
106 GtkRequisition req;
107 GdkPoint* pos;
108
109 pos = user_data;
110
111 gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
112
113 *x = pos->x;
114 *y = pos->y;
115
116 if (meta_ui_get_direction() == META_UI_DIRECTION_RTL)
117 {
118 *x = MAX (0, *x - req.width);
119 }
120
121 /* Ensure onscreen */
122 *x = CLAMP (*x, 0, MAX(0, WidthOfScreen (gdk_x11_screen_get_xscreen (gdk_screen_get_default ())) - req.width));
123 *y = CLAMP (*y, 0, MAX(0, HeightOfScreen (gdk_x11_screen_get_xscreen (gdk_screen_get_default ())) - req.height));
124 }
125
menu_closed(GtkMenu * widget,gpointer data)126 static void menu_closed(GtkMenu* widget, gpointer data)
127 {
128 MetaWindowMenu *menu;
129
130 menu = data;
131
132 meta_frames_notify_menu_hide (menu->frames);
133
134 (*menu->func)(
135 menu,
136 GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
137 menu->client_xwindow,
138 gtk_get_current_event_time (),
139 0, 0,
140 menu->data);
141
142 /* menu may now be freed */
143 }
144
activate_cb(GtkWidget * menuitem,gpointer data)145 static void activate_cb(GtkWidget* menuitem, gpointer data)
146 {
147 MenuData* md;
148
149 g_return_if_fail (GTK_IS_WIDGET (menuitem));
150
151 md = data;
152
153 meta_frames_notify_menu_hide (md->menu->frames);
154
155 (*md->menu->func)(
156 md->menu,
157 GDK_DISPLAY_XDISPLAY (gdk_display_get_default()),
158 md->menu->client_xwindow,
159 gtk_get_current_event_time(),
160 md->op,
161 GPOINTER_TO_INT(g_object_get_data(G_OBJECT(menuitem), "workspace")),
162 md->menu->data);
163
164 /* menu may now be freed */
165 }
166
167 /*
168 * Given a Display and an index, get the workspace name and add any
169 * accelerators. At the moment this means adding a _ if the name is of
170 * the form "Workspace n" where n is less than 10, and escaping any
171 * other '_'s so they do not create inadvertant accelerators.
172 *
173 * The calling code owns the string, and is reponsible to free the
174 * memory after use.
175 *
176 * See also http://mail.gnome.org/archives/mate-i18n/2008-March/msg00380.html
177 * which discusses possible i18n concerns.
178 */
179 static char*
get_workspace_name_with_accel(Display * display,Window xroot,int index)180 get_workspace_name_with_accel (Display *display,
181 Window xroot,
182 int index)
183 {
184 const char *name;
185 int number;
186 int charcount=0;
187
188 name = meta_core_get_workspace_name_with_index (display, xroot, index);
189
190 g_assert (name != NULL);
191
192 /*
193 * If the name is of the form "Workspace x" where x is an unsigned
194 * integer, insert a '_' before the number if it is less than 10 and
195 * return it
196 */
197 number = 0;
198 if (sscanf (name, _("Workspace %d%n"), &number, &charcount) != 0 &&
199 *(name + charcount)=='\0')
200 {
201 char *new_name;
202
203 /*
204 * Above name is a pointer into the Workspace struct. Here we make
205 * a copy copy so we can have our wicked way with it.
206 */
207 if (number == 10)
208 new_name = g_strdup_printf (_("Workspace 1_0"));
209 else
210 new_name = g_strdup_printf (_("Workspace %s%d"),
211 number < 10 ? "_" : "",
212 number);
213 return new_name;
214 }
215 else
216 {
217 /*
218 * Otherwise this is just a normal name. Escape any _ characters so that
219 * the user's workspace names do not get mangled. If the number is less
220 * than 10 we provide an accelerator.
221 */
222 char *new_name;
223 const char *source;
224 char *dest;
225
226 /*
227 * Assume the worst case, that every character is a _. We also
228 * provide memory for " (_#)"
229 */
230 new_name = g_malloc0 (strlen (name) * 2 + 6 + 1);
231
232 /*
233 * Now iterate down the strings, adding '_' to escape as we go
234 */
235 dest = new_name;
236 source = name;
237 while (*source != '\0')
238 {
239 if (*source == '_')
240 *dest++ = '_';
241 *dest++ = *source++;
242 }
243
244 /* People don't start at workspace 0, but workspace 1 */
245 if (index < 9)
246 {
247 g_snprintf (dest, 6, " (_%d)", index + 1);
248 }
249 else if (index == 9)
250 {
251 g_snprintf (dest, 6, " (_0)");
252 }
253
254 return new_name;
255 }
256 }
257
menu_item_new(MenuItem * menuitem,int workspace_id)258 static GtkWidget* menu_item_new(MenuItem* menuitem, int workspace_id)
259 {
260 unsigned int key;
261 MetaVirtualModifier mods;
262 const char* i18n_label;
263 GtkWidget* mi;
264 GtkWidget* accel_label;
265
266 if (menuitem->type == MENU_ITEM_NORMAL)
267 {
268 mi = gtk_menu_item_new ();
269 }
270 else if (menuitem->type == MENU_ITEM_IMAGE)
271 {
272 GtkWidget* image = gtk_image_new_from_icon_name(menuitem->stock_id, GTK_ICON_SIZE_MENU);
273
274 mi = gtk_image_menu_item_new();
275
276 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(mi), image);
277 gtk_widget_show(image);
278 }
279 else if (menuitem->type == MENU_ITEM_CHECKBOX)
280 {
281 mi = gtk_check_menu_item_new ();
282
283 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(mi), menuitem->checked);
284 }
285 else if (menuitem->type == MENU_ITEM_RADIOBUTTON)
286 {
287 mi = gtk_check_menu_item_new ();
288
289 gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (mi), TRUE);
290 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mi), menuitem->checked);
291 }
292 else if (menuitem->type == MENU_ITEM_WORKSPACE_LIST)
293 {
294 return NULL;
295 }
296 else
297 {
298 return gtk_separator_menu_item_new();
299 }
300
301 i18n_label = _(menuitem->label);
302 meta_core_get_menu_accelerator (menuitem->op, workspace_id, &key, &mods);
303
304 accel_label = meta_accel_label_new_with_mnemonic (i18n_label);
305 gtk_widget_set_halign (accel_label, GTK_ALIGN_START);
306
307 gtk_container_add (GTK_CONTAINER (mi), accel_label);
308 gtk_widget_show (accel_label);
309
310 meta_accel_label_set_accelerator (META_ACCEL_LABEL (accel_label), key, mods);
311
312 return mi;
313 }
314
315 MetaWindowMenu*
meta_window_menu_new(MetaFrames * frames,MetaMenuOp ops,MetaMenuOp insensitive,Window client_xwindow,unsigned long active_workspace,int n_workspaces,MetaWindowMenuFunc func,gpointer data)316 meta_window_menu_new (MetaFrames *frames,
317 MetaMenuOp ops,
318 MetaMenuOp insensitive,
319 Window client_xwindow,
320 unsigned long active_workspace,
321 int n_workspaces,
322 MetaWindowMenuFunc func,
323 gpointer data)
324 {
325 int i;
326 MetaWindowMenu *menu;
327
328 /* FIXME: Modifications to 'ops' should happen in meta_window_show_menu */
329 if (n_workspaces < 2)
330 ops &= ~(META_MENU_OP_STICK | META_MENU_OP_UNSTICK | META_MENU_OP_WORKSPACES);
331 else if (n_workspaces == 2)
332 /* #151183: If we only have two workspaces, disable the menu listing them. */
333 ops &= ~(META_MENU_OP_WORKSPACES);
334
335 menu = g_new (MetaWindowMenu, 1);
336 menu->frames = frames;
337 menu->client_xwindow = client_xwindow;
338 menu->func = func;
339 menu->data = data;
340 menu->ops = ops;
341 menu->insensitive = insensitive;
342
343 menu->menu = gtk_menu_new ();
344
345 gtk_menu_set_screen (GTK_MENU (menu->menu),
346 gtk_widget_get_screen (GTK_WIDGET (frames)));
347
348 for (i = 0; i < (int) G_N_ELEMENTS (menuitems); i++)
349 {
350 MenuItem menuitem = menuitems[i];
351 if (ops & menuitem.op || menuitem.op == 0)
352 {
353 GtkWidget *mi;
354
355 mi = menu_item_new (&menuitem, -1);
356
357 /* Set the activeness of radiobuttons. */
358 switch (menuitem.op)
359 {
360 case META_MENU_OP_STICK:
361 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mi),
362 active_workspace == 0xFFFFFFFF);
363 break;
364 case META_MENU_OP_UNSTICK:
365 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (mi),
366 active_workspace != 0xFFFFFFFF);
367 break;
368 default:
369 break;
370 }
371
372 if (menuitem.type == MENU_ITEM_WORKSPACE_LIST)
373 {
374 if (ops & META_MENU_OP_WORKSPACES)
375 {
376 Display *display;
377 Window xroot;
378 GdkScreen *screen;
379 GdkWindow *window;
380 GtkWidget *submenu;
381 int j;
382
383 MenuItem to_another_workspace = {
384 0, MENU_ITEM_NORMAL,
385 NULL, FALSE,
386 N_("Move to Another _Workspace")
387 };
388
389 meta_verbose ("Creating %d-workspace menu current space %lu\n",
390 n_workspaces, active_workspace);
391
392 window = gtk_widget_get_window (GTK_WIDGET (frames));
393
394 display = GDK_WINDOW_XDISPLAY (window);
395
396 screen = gdk_window_get_screen (window);
397 xroot = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
398
399 submenu = gtk_menu_new ();
400
401 g_assert (mi==NULL);
402 mi = menu_item_new (&to_another_workspace, -1);
403 gtk_menu_item_set_submenu (GTK_MENU_ITEM (mi), submenu);
404
405 for (j = 0; j < n_workspaces; j++)
406 {
407 char *label;
408 MenuData *md;
409 unsigned int key;
410 MetaVirtualModifier mods;
411 MenuItem moveitem;
412 GtkWidget *submi;
413
414 meta_core_get_menu_accelerator (META_MENU_OP_WORKSPACES,
415 j + 1,
416 &key, &mods);
417
418 label = get_workspace_name_with_accel (display, xroot, j);
419
420 moveitem.type = MENU_ITEM_NORMAL;
421 moveitem.op = META_MENU_OP_WORKSPACES;
422 moveitem.label = label;
423 submi = menu_item_new (&moveitem, j + 1);
424
425 g_free (label);
426
427 if ((active_workspace == (unsigned)j) && (ops & META_MENU_OP_UNSTICK))
428 gtk_widget_set_sensitive (submi, FALSE);
429
430 md = g_new (MenuData, 1);
431
432 md->menu = menu;
433 md->op = META_MENU_OP_WORKSPACES;
434
435 g_object_set_data (G_OBJECT (submi),
436 "workspace",
437 GINT_TO_POINTER (j));
438
439 g_signal_connect_data (G_OBJECT (submi),
440 "activate",
441 G_CALLBACK (activate_cb),
442 md,
443 (GClosureNotify) g_free, 0);
444
445 gtk_menu_shell_append (GTK_MENU_SHELL (submenu), submi);
446
447 gtk_widget_show (submi);
448 }
449 }
450 else
451 meta_verbose ("not creating workspace menu\n");
452 }
453 else if (menuitem.type != MENU_ITEM_SEPARATOR)
454 {
455 MenuData *md;
456 unsigned int key;
457 MetaVirtualModifier mods;
458
459 meta_core_get_menu_accelerator (menuitems[i].op, -1,
460 &key, &mods);
461
462 if (insensitive & menuitem.op)
463 gtk_widget_set_sensitive (mi, FALSE);
464
465 md = g_new (MenuData, 1);
466
467 md->menu = menu;
468 md->op = menuitem.op;
469
470 g_signal_connect_data (G_OBJECT (mi),
471 "activate",
472 G_CALLBACK (activate_cb),
473 md,
474 (GClosureNotify) g_free, 0);
475 }
476
477 if (mi)
478 {
479 gtk_menu_shell_append (GTK_MENU_SHELL (menu->menu), mi);
480
481 gtk_widget_show (mi);
482 }
483 }
484 }
485
486 g_signal_connect (menu->menu, "selection_done",
487 G_CALLBACK (menu_closed), menu);
488
489 return menu;
490 }
491
meta_window_menu_popup(MetaWindowMenu * menu,int root_x,int root_y,int button,guint32 timestamp)492 void meta_window_menu_popup(MetaWindowMenu* menu, int root_x, int root_y, int button, guint32 timestamp)
493 {
494 GdkPoint* pt = g_new(GdkPoint, 1);
495 gint scale;
496
497 g_object_set_data_full(G_OBJECT(menu->menu), "destroy-point", pt, g_free);
498
499 scale = gtk_widget_get_scale_factor (menu->menu);
500 pt->x = root_x / scale;
501 pt->y = root_y / scale;
502
503 gtk_menu_popup(GTK_MENU (menu->menu), NULL, NULL, popup_position_func, pt, button, timestamp);
504
505 if (!gtk_widget_get_visible (menu->menu))
506 meta_warning("GtkMenu failed to grab the pointer\n");
507 }
508
meta_window_menu_free(MetaWindowMenu * menu)509 void meta_window_menu_free(MetaWindowMenu* menu)
510 {
511 gtk_widget_destroy(menu->menu);
512 g_free(menu);
513 }
514