2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
39 *-----------------------------------------------------------------------------
41 *-----------------------------------------------------------------------------
45 * @brief Add accelerator key to a window popup menu
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.
54 static void menu_item_add_accelerator(GtkWidget *menu, GtkAccelGroup *accel_group, hard_coded_window_keys *window_keys)
58 gchar **label_stripped;
61 label = g_strdup(gtk_menu_item_get_label(GTK_MENU_ITEM(menu)));
63 pango_parse_markup(label, -1, '_', NULL, &label_text, NULL, NULL);
65 label_stripped = g_strsplit(label_text, "...", 2);
67 while (window_keys[i].text != NULL)
69 if (g_strcmp0(window_keys[i].text, label_stripped[0]) == 0)
71 gtk_widget_add_accelerator(menu, "activate", accel_group, window_keys[i].key_value, (GdkModifierType)window_keys[i].mask, GTK_ACCEL_VISIBLE);
80 g_strfreev(label_stripped);
84 * @brief Callback for the actions GList sort function
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.
93 static gint actions_sort_cb(gconstpointer a, gconstpointer b)
95 const gchar *accel_path_a;
97 const gchar *accel_path_b;
100 accel_path_a = gtk_action_get_accel_path(GTK_ACTION(a));
101 accel_path_b = gtk_action_get_accel_path(GTK_ACTION(b));
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))
105 if (key_a.accel_mods < key_b.accel_mods) return -1;
106 if (key_a.accel_mods > key_b.accel_mods) return 1;
113 * @brief Add accelerator key to main window popup menu
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.
121 static void menu_item_add_main_window_accelerator(GtkWidget *menu, GtkAccelGroup *accel_group)
124 gchar *menu_label_text;
126 gchar *action_label_text;
131 const gchar *accel_path;
134 menu_label = g_strdup(gtk_menu_item_get_label(GTK_MENU_ITEM(menu)));
136 pango_parse_markup(menu_label, -1, '_', NULL, &menu_label_text, NULL, NULL);
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 */
140 g_assert(lw && lw->ui_manager);
141 groups = gtk_ui_manager_get_action_groups(lw->ui_manager);
145 actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
146 actions = g_list_sort(actions, actions_sort_cb);
150 action = GTK_ACTION(actions->data);
151 accel_path = gtk_action_get_accel_path(action);
152 if (accel_path && gtk_accel_map_lookup_entry(accel_path, &key))
154 g_object_get(action, "label", &action_label, NULL);
156 pango_parse_markup(action_label, -1, '_', NULL, &action_label_text, NULL, NULL);
158 if (g_strcmp0(action_label_text, menu_label_text) == 0)
160 if (key.accel_key != 0)
162 gtk_widget_add_accelerator(menu, "activate", accel_group, key.accel_key, key.accel_mods, GTK_ACCEL_VISIBLE);
167 g_free(action_label_text);
168 g_free(action_label);
170 actions = actions->next;
172 groups = groups->next;
176 g_free(menu_label_text);
179 static void menu_item_finish(GtkWidget *menu, GtkWidget *item, GCallback func, gpointer data)
181 if (func) g_signal_connect(G_OBJECT(item), "activate", func, data);
182 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
183 gtk_widget_show(item);
186 GtkWidget *menu_item_add(GtkWidget *menu, const gchar *label,
187 GCallback func, gpointer data)
190 GtkAccelGroup *accel_group;
191 hard_coded_window_keys *window_keys;
193 item = gtk_menu_item_new_with_mnemonic(label);
194 window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
195 accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
197 if (accel_group && window_keys)
199 menu_item_add_accelerator(item, accel_group, window_keys);
201 else if (accel_group)
203 menu_item_add_main_window_accelerator(item, accel_group);
206 menu_item_finish(menu, item, func, data);
211 GtkWidget *menu_item_add_stock(GtkWidget *menu, const gchar *label, const gchar *stock_id,
212 GCallback func, gpointer data)
216 GtkAccelGroup *accel_group;
217 hard_coded_window_keys *window_keys;
219 item = gtk_image_menu_item_new_with_mnemonic(label);
220 window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
221 accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
223 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
224 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
226 if (accel_group && window_keys)
228 menu_item_add_accelerator(item, accel_group, window_keys);
230 else if (accel_group)
232 menu_item_add_main_window_accelerator(item, accel_group);
235 gtk_widget_show(image);
236 menu_item_finish(menu, item, func, data);
241 GtkWidget *menu_item_add_sensitive(GtkWidget *menu, const gchar *label, gboolean sensitive,
242 GCallback func, gpointer data)
245 GtkAccelGroup *accel_group;
246 hard_coded_window_keys *window_keys;
248 item = menu_item_add(menu, label, func, data);
249 gtk_widget_set_sensitive(item, sensitive);
250 window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
251 accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
252 if (accel_group && window_keys)
254 menu_item_add_accelerator(item, accel_group, window_keys);
256 else if (accel_group)
258 menu_item_add_main_window_accelerator(item, accel_group);
264 GtkWidget *menu_item_add_stock_sensitive(GtkWidget *menu, const gchar *label, const gchar *stock_id, gboolean sensitive,
265 GCallback func, gpointer data)
268 GtkAccelGroup *accel_group;
269 hard_coded_window_keys *window_keys;
271 item = menu_item_add_stock(menu, label, stock_id, func, data);
272 gtk_widget_set_sensitive(item, sensitive);
273 window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
274 accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
275 if (accel_group && window_keys)
277 menu_item_add_accelerator(item, accel_group, window_keys);
279 else if (accel_group)
281 menu_item_add_main_window_accelerator(item, accel_group);
287 GtkWidget *menu_item_add_check(GtkWidget *menu, const gchar *label, gboolean active,
288 GCallback func, gpointer data)
291 GtkAccelGroup *accel_group;
292 hard_coded_window_keys *window_keys;
294 item = gtk_check_menu_item_new_with_mnemonic(label);
295 window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
296 accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
298 if (accel_group && window_keys)
300 menu_item_add_accelerator(item, accel_group, window_keys);
302 else if (accel_group)
304 menu_item_add_main_window_accelerator(item, accel_group);
307 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), active);
308 menu_item_finish(menu, item, func, data);
313 GtkWidget *menu_item_add_radio(GtkWidget *menu, const gchar *label, gpointer item_data, gboolean active,
314 GCallback func, gpointer data)
316 GtkAccelGroup *accel_group;
317 hard_coded_window_keys *window_keys;
319 GtkWidget *item = menu_item_add_check(menu, label, active, func, data);
320 g_object_set_data(G_OBJECT(item), "menu_item_radio_data", item_data);
321 g_object_set(G_OBJECT(item), "draw-as-radio", TRUE, NULL);
323 window_keys = g_object_get_data(G_OBJECT(menu), "window_keys");
324 accel_group = g_object_get_data(G_OBJECT(menu), "accel_group");
325 if (accel_group && window_keys)
327 menu_item_add_accelerator(item, accel_group, window_keys);
329 else if (accel_group)
331 menu_item_add_main_window_accelerator(item, accel_group);
337 void menu_item_add_divider(GtkWidget *menu)
339 GtkWidget *item = gtk_separator_menu_item_new();
340 gtk_widget_set_sensitive(item, FALSE);
341 gtk_menu_shell_append(GTK_MENU_SHELL(menu),item);
342 gtk_widget_show(item);
345 GtkWidget *menu_item_add_simple(GtkWidget *menu, const gchar *label,
346 GCallback func, gpointer data)
348 GtkWidget *item = gtk_menu_item_new_with_label(label);
349 menu_item_finish(menu, item, func, data);
355 *-----------------------------------------------------------------------------
357 *-----------------------------------------------------------------------------
360 static void popup_menu_short_lived_cb(GtkWidget *widget, gpointer data)
362 /* destroy the menu */
363 g_object_unref(G_OBJECT(data));
366 GtkWidget *popup_menu_short_lived(void)
370 menu = gtk_menu_new();
372 /* take ownership of menu */
373 #ifdef GTK_OBJECT_FLOATING
375 g_object_ref(G_OBJECT(menu));
376 gtk_object_sink(GTK_OBJECT(menu));
379 g_object_ref_sink(G_OBJECT(menu));
382 g_signal_connect(G_OBJECT(menu), "selection_done",
383 G_CALLBACK(popup_menu_short_lived_cb), menu);
387 gboolean popup_menu_position_clamp(GtkMenu *menu, gint *x, gint *y, gint height)
389 gboolean adjusted = FALSE;
392 GtkRequisition requisition;
394 gtk_widget_get_requisition(GTK_WIDGET(menu), &requisition);
395 w = requisition.width;
396 h = requisition.height;
397 xw = gdk_screen_width();
398 xh = gdk_screen_height();
409 *y = MAX(0, *y - h - height);
431 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */