Show shortcut keys in pop-up menus
[geeqie.git] / src / ui_menu.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 #include "intl.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <gtk/gtk.h>
32
33 #include "main.h"
34 #include "layout.h"
35 #include "ui_menu.h"
36
37
38 /*
39  *-----------------------------------------------------------------------------
40  * menu items
41  *-----------------------------------------------------------------------------
42  */
43
44 /**
45  * @brief Add accelerator key to a window popup menu
46  * @param menu
47  * @param accel_group
48  * @param window_keys
49  *
50  * This is used only so that the user can see the applicable
51  * shortcut key displayed in the menu. The actual handling of
52  * the keystroke is done elsewhere in the code.
53  */
54 static void menu_item_add_accelerator(GtkWidget *menu, GtkAccelGroup *accel_group, hard_coded_window_keys *window_keys)
55 {
56         gchar *label;
57         gchar *label_text;
58         gchar **label_stripped;
59         gint i = 0;
60
61         label = g_strdup(gtk_menu_item_get_label(GTK_MENU_ITEM(menu)));
62
63         pango_parse_markup(label, -1, '_', NULL, &label_text, NULL, NULL);
64
65         label_stripped = g_strsplit(label_text, "...", 2);
66
67         while (window_keys[i].text != NULL)
68                 {
69                 if (g_strcmp0(window_keys[i].text, label_stripped[0]) == 0)
70                         {
71                         gtk_widget_add_accelerator(menu, "activate", accel_group, window_keys[i].key_value, (GdkModifierType)window_keys[i].mask, GTK_ACCEL_VISIBLE);
72
73                         break;
74                         }
75                 i++;
76                 }
77
78         g_free(label);
79         g_free(label_text);
80         g_strfreev(label_stripped);
81 }
82
83 /**
84  * @brief Callback for the actions GList sort function
85  * @param a
86  * @param b
87  * @returns
88  *
89  * Sort the action entries so that the non-shifted and non-control
90  * entries are at the start of the list. The user then sees the basic
91  * non-modified key shortcuts displayed in the menus.
92  */
93 static gint actions_sort_cb(gconstpointer a, gconstpointer b)
94 {
95         const gchar *accel_path_a;
96         GtkAccelKey key_a;
97         const gchar *accel_path_b;
98         GtkAccelKey key_b;
99
100         accel_path_a = gtk_action_get_accel_path(GTK_ACTION(a));
101         accel_path_b = gtk_action_get_accel_path(GTK_ACTION(b));
102
103         if (accel_path_a && gtk_accel_map_lookup_entry(accel_path_a, &key_a) && accel_path_b && gtk_accel_map_lookup_entry(accel_path_b, &key_b))
104                 {
105                 if (key_a.accel_mods < key_b.accel_mods) return -1;
106                 if (key_a.accel_mods > key_b.accel_mods) return 1;
107                 }
108
109         return 0;
110 }
111
112 /**
113  * @brief Add accelerator key to main window popup menu
114  * @param menu
115  * @param accel_group
116  *
117  * This is used only so that the user can see the applicable
118  * shortcut key displayed in the menu. The actual handling of
119  * the keystroke is done elsewhere in the code.
120  */
121 static void menu_item_add_main_window_accelerator(GtkWidget *menu, GtkAccelGroup *accel_group)
122 {
123         gchar *menu_label;
124         gchar *menu_label_text;
125         gchar *action_label;
126         gchar *action_label_text;
127         LayoutWindow *lw;
128         GList *groups;
129         GList *actions;
130         GtkAction *action;
131         const gchar *accel_path;
132         GtkAccelKey key;
133
134         menu_label = g_strdup(gtk_menu_item_get_label(GTK_MENU_ITEM(menu)));
135
136         pango_parse_markup(menu_label, -1, '_', NULL, &menu_label_text, NULL, NULL);
137
138         lw = layout_window_list->data; /* get the actions from the first window, it should not matter, they should be the same in all windows */
139
140         g_assert(lw && lw->ui_manager);
141         groups = gtk_ui_manager_get_action_groups(lw->ui_manager);
142         actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data)); // Only the first group required
143         actions = g_list_sort(actions, actions_sort_cb);
144
145         while (actions)
146                 {
147                 action = GTK_ACTION(actions->data);
148                 accel_path = gtk_action_get_accel_path(action);
149                 if (accel_path && gtk_accel_map_lookup_entry(accel_path, &key))
150                         {
151                         g_object_get(action, "label", &action_label, NULL);
152
153                         pango_parse_markup(action_label, -1, '_', NULL, &action_label_text, NULL, NULL);
154
155                         if (g_strcmp0(action_label_text, menu_label_text) == 0)
156                                 {
157                                 if (key.accel_key != 0)
158                                         {
159                                         gtk_widget_add_accelerator(menu, "activate", accel_group, key.accel_key, key.accel_mods, GTK_ACCEL_VISIBLE);
160
161                                         break;
162                                         }
163                                 }
164
165                         g_free(action_label_text);
166                         g_free(action_label);
167                         }
168                 actions = actions->next;
169                 }
170
171         g_free(menu_label);
172         g_free(menu_label_text);
173 }
174
175 static void menu_item_finish(GtkWidget *menu, GtkWidget *item, GCallback func, gpointer data)
176 {
177         if (func) g_signal_connect(G_OBJECT(item), "activate", func, data);
178         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
179         gtk_widget_show(item);
180 }
181
182 GtkWidget *menu_item_add(GtkWidget *menu, const gchar *label,
183                          GCallback func, gpointer data)
184 {
185         GtkWidget *item;
186         GtkAccelGroup *accel_group;
187         hard_coded_window_keys *window_keys;
188
189         item = gtk_menu_item_new_with_mnemonic(label);
190         window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
191         accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
192
193         if (accel_group && window_keys)
194                 {
195                 menu_item_add_accelerator(item, accel_group, window_keys);
196                 }
197         else if (accel_group)
198                 {
199                 menu_item_add_main_window_accelerator(item, accel_group);
200                 }
201
202         menu_item_finish(menu, item, func, data);
203
204         return item;
205 }
206
207 GtkWidget *menu_item_add_stock(GtkWidget *menu, const gchar *label, const gchar *stock_id,
208                                GCallback func, gpointer data)
209 {
210         GtkWidget *item;
211         GtkWidget *image;
212         GtkAccelGroup *accel_group;
213         hard_coded_window_keys *window_keys;
214
215         item = gtk_image_menu_item_new_with_mnemonic(label);
216         window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
217         accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
218
219         image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
220         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
221
222         if (accel_group && window_keys)
223                 {
224                 menu_item_add_accelerator(item, accel_group, window_keys);
225                 }
226         else if (accel_group)
227                 {
228                 menu_item_add_main_window_accelerator(item, accel_group);
229                 }
230
231         gtk_widget_show(image);
232         menu_item_finish(menu, item, func, data);
233
234         return item;
235 }
236
237 GtkWidget *menu_item_add_sensitive(GtkWidget *menu, const gchar *label, gboolean sensitive,
238                                    GCallback func, gpointer data)
239 {
240         GtkWidget *item;
241         GtkAccelGroup *accel_group;
242         hard_coded_window_keys *window_keys;
243
244         item = menu_item_add(menu, label, func, data);
245         gtk_widget_set_sensitive(item, sensitive);
246         window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
247         accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
248         if (accel_group && window_keys)
249                 {
250                 menu_item_add_accelerator(item, accel_group, window_keys);
251                 }
252         else if (accel_group)
253                 {
254                 menu_item_add_main_window_accelerator(item, accel_group);
255                 }
256
257         return item;
258 }
259
260 GtkWidget *menu_item_add_stock_sensitive(GtkWidget *menu, const gchar *label, const gchar *stock_id, gboolean sensitive,
261                                          GCallback func, gpointer data)
262 {
263         GtkWidget *item;
264         GtkAccelGroup *accel_group;
265         hard_coded_window_keys *window_keys;
266
267         item = menu_item_add_stock(menu, label, stock_id, func, data);
268         gtk_widget_set_sensitive(item, sensitive);
269         window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
270         accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
271         if (accel_group && window_keys)
272                 {
273                 menu_item_add_accelerator(item, accel_group, window_keys);
274                 }
275         else if (accel_group)
276                 {
277                 menu_item_add_main_window_accelerator(item, accel_group);
278                 }
279
280         return item;
281 }
282
283 GtkWidget *menu_item_add_check(GtkWidget *menu, const gchar *label, gboolean active,
284                                GCallback func, gpointer data)
285 {
286         GtkWidget *item;
287         GtkAccelGroup *accel_group;
288         hard_coded_window_keys *window_keys;
289
290         item = gtk_check_menu_item_new_with_mnemonic(label);
291         window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
292         accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
293
294         if (accel_group && window_keys)
295                 {
296                 menu_item_add_accelerator(item, accel_group, window_keys);
297                 }
298         else if (accel_group)
299                 {
300                 menu_item_add_main_window_accelerator(item, accel_group);
301                 }
302
303         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), active);
304         menu_item_finish(menu, item, func, data);
305
306         return item;
307 }
308
309 GtkWidget *menu_item_add_radio(GtkWidget *menu, const gchar *label, gpointer item_data, gboolean active,
310                                GCallback func, gpointer data)
311 {
312         GtkAccelGroup *accel_group;
313         hard_coded_window_keys *window_keys;
314
315         GtkWidget *item = menu_item_add_check(menu, label, active, func, data);
316         g_object_set_data(G_OBJECT(item), "menu_item_radio_data", item_data);
317         g_object_set(G_OBJECT(item), "draw-as-radio", TRUE, NULL);
318
319         window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
320         accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
321         if (accel_group && window_keys)
322                 {
323                 menu_item_add_accelerator(item, accel_group, window_keys);
324                 }
325         else if (accel_group)
326                 {
327                 menu_item_add_main_window_accelerator(item, accel_group);
328                 }
329
330         return item;
331 }
332
333 void menu_item_add_divider(GtkWidget *menu)
334 {
335         GtkWidget *item = gtk_separator_menu_item_new();
336         gtk_widget_set_sensitive(item, FALSE);
337         gtk_menu_shell_append(GTK_MENU_SHELL(menu),item);
338         gtk_widget_show(item);
339 }
340
341 GtkWidget *menu_item_add_simple(GtkWidget *menu, const gchar *label,
342                                 GCallback func, gpointer data)
343 {
344         GtkWidget *item = gtk_menu_item_new_with_label(label);
345         menu_item_finish(menu, item, func, data);
346
347         return item;
348 }
349
350 /*
351  *-----------------------------------------------------------------------------
352  * popup menus
353  *-----------------------------------------------------------------------------
354  */
355
356 static void popup_menu_short_lived_cb(GtkWidget *widget, gpointer data)
357 {
358         /* destroy the menu */
359         g_object_unref(G_OBJECT(data));
360 }
361
362 GtkWidget *popup_menu_short_lived(void)
363 {
364         GtkWidget *menu;
365
366         menu = gtk_menu_new();
367
368         /* take ownership of menu */
369 #ifdef GTK_OBJECT_FLOATING
370         /* GTK+ < 2.10 */
371         g_object_ref(G_OBJECT(menu));
372         gtk_object_sink(GTK_OBJECT(menu));
373 #else
374         /* GTK+ >= 2.10 */
375         g_object_ref_sink(G_OBJECT(menu));
376 #endif
377
378         g_signal_connect(G_OBJECT(menu), "selection_done",
379                          G_CALLBACK(popup_menu_short_lived_cb), menu);
380         return menu;
381 }
382
383 gboolean popup_menu_position_clamp(GtkMenu *menu, gint *x, gint *y, gint height)
384 {
385         gboolean adjusted = FALSE;
386         gint w, h;
387         gint xw, xh;
388         GtkRequisition requisition;
389
390         gtk_widget_get_requisition(GTK_WIDGET(menu), &requisition);
391         w = requisition.width;
392         h = requisition.height;
393         xw = gdk_screen_width();
394         xh = gdk_screen_height();
395
396         if (*x + w > xw)
397                 {
398                 *x = xw - w;
399                 adjusted = TRUE;
400                 }
401         if (*y + h > xh)
402                 {
403                 if (height)
404                         {
405                         *y = MAX(0, *y - h - height);
406                         }
407                 else
408                         {
409                         *y = xh - h;
410                         }
411                 adjusted = TRUE;
412                 };
413
414         if (*x < 0)
415                 {
416                 *x = 0;
417                 adjusted = TRUE;
418                 }
419         if (*y < 0)
420                 {
421                 *y = 0;
422                 adjusted = TRUE;
423                 }
424
425         return adjusted;
426 }
427 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */