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.
28 *-----------------------------------------------------------------------------
30 *-----------------------------------------------------------------------------
34 * @brief Add accelerator key to a window popup menu
39 * This is used only so that the user can see the applicable
40 * shortcut key displayed in the menu. The actual handling of
41 * the keystroke is done elsewhere in the code.
43 static void menu_item_add_accelerator(GtkWidget *menu, GtkAccelGroup *accel_group, hard_coded_window_keys *window_keys)
47 gchar **label_stripped;
50 label = g_strdup(gtk_menu_item_get_label(GTK_MENU_ITEM(menu)));
52 pango_parse_markup(label, -1, '_', NULL, &label_text, NULL, NULL);
54 label_stripped = g_strsplit(label_text, "...", 2);
56 while (window_keys[i].text != NULL)
58 if (g_strcmp0(window_keys[i].text, label_stripped[0]) == 0)
60 gtk_widget_add_accelerator(menu, "activate", accel_group, window_keys[i].key_value, (GdkModifierType)window_keys[i].mask, GTK_ACCEL_VISIBLE);
69 g_strfreev(label_stripped);
73 * @brief Callback for the actions GList sort function
78 * Sort the action entries so that the non-shifted and non-control
79 * entries are at the start of the list. The user then sees the basic
80 * non-modified key shortcuts displayed in the menus.
82 static gint actions_sort_cb(gconstpointer a, gconstpointer b)
84 const gchar *accel_path_a;
86 const gchar *accel_path_b;
89 accel_path_a = gtk_action_get_accel_path(GTK_ACTION(a));
90 accel_path_b = gtk_action_get_accel_path(GTK_ACTION(b));
92 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))
94 if (key_a.accel_mods < key_b.accel_mods) return -1;
95 if (key_a.accel_mods > key_b.accel_mods) return 1;
102 * @brief Add accelerator key to main window popup menu
106 * This is used only so that the user can see the applicable
107 * shortcut key displayed in the menu. The actual handling of
108 * the keystroke is done elsewhere in the code.
110 static void menu_item_add_main_window_accelerator(GtkWidget *menu, GtkAccelGroup *accel_group)
113 gchar *menu_label_text;
115 gchar *action_label_text;
120 const gchar *accel_path;
123 menu_label = g_strdup(gtk_menu_item_get_label(GTK_MENU_ITEM(menu)));
125 pango_parse_markup(menu_label, -1, '_', NULL, &menu_label_text, NULL, NULL);
127 lw = static_cast<LayoutWindow *>(layout_window_list->data); /* get the actions from the first window, it should not matter, they should be the same in all windows */
129 g_assert(lw && lw->ui_manager);
130 groups = gtk_ui_manager_get_action_groups(lw->ui_manager);
134 actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
135 actions = g_list_sort(actions, actions_sort_cb);
139 action = GTK_ACTION(actions->data);
140 accel_path = gtk_action_get_accel_path(action);
141 if (accel_path && gtk_accel_map_lookup_entry(accel_path, &key))
143 g_object_get(action, "label", &action_label, NULL);
145 pango_parse_markup(action_label, -1, '_', NULL, &action_label_text, NULL, NULL);
147 if (g_strcmp0(action_label_text, menu_label_text) == 0)
149 if (key.accel_key != 0)
151 gtk_widget_add_accelerator(menu, "activate", accel_group, key.accel_key, key.accel_mods, GTK_ACCEL_VISIBLE);
156 g_free(action_label_text);
157 g_free(action_label);
159 actions = actions->next;
161 groups = groups->next;
165 g_free(menu_label_text);
168 static void menu_item_finish(GtkWidget *menu, GtkWidget *item, GCallback func, gpointer data)
170 if (func) g_signal_connect(G_OBJECT(item), "activate", func, data);
171 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
172 gtk_widget_show(item);
175 GtkWidget *menu_item_add(GtkWidget *menu, const gchar *label,
176 GCallback func, gpointer data)
179 GtkAccelGroup *accel_group;
180 hard_coded_window_keys *window_keys;
182 item = gtk_menu_item_new_with_mnemonic(label);
183 window_keys = static_cast<hard_coded_window_keys *>(g_object_get_data(G_OBJECT(menu), "window_keys"));
184 accel_group = static_cast<GtkAccelGroup *>(g_object_get_data(G_OBJECT(menu), "accel_group"));
186 if (accel_group && window_keys)
188 menu_item_add_accelerator(item, accel_group, window_keys);
190 else if (accel_group)
192 menu_item_add_main_window_accelerator(item, accel_group);
195 menu_item_finish(menu, item, func, data);
200 GtkWidget *menu_item_add_stock(GtkWidget *menu, const gchar *label, const gchar *stock_id,
201 GCallback func, gpointer data)
205 GtkAccelGroup *accel_group;
206 hard_coded_window_keys *window_keys;
208 item = gtk_image_menu_item_new_with_mnemonic(label);
209 window_keys = static_cast<hard_coded_window_keys *>(g_object_get_data(G_OBJECT(menu), "window_keys"));
210 accel_group = static_cast<GtkAccelGroup *>(g_object_get_data(G_OBJECT(menu), "accel_group"));
212 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_MENU);
213 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
215 if (accel_group && window_keys)
217 menu_item_add_accelerator(item, accel_group, window_keys);
219 else if (accel_group)
221 menu_item_add_main_window_accelerator(item, accel_group);
224 gtk_widget_show(image);
225 menu_item_finish(menu, item, func, data);
230 GtkWidget *menu_item_add_sensitive(GtkWidget *menu, const gchar *label, gboolean sensitive,
231 GCallback func, gpointer data)
234 GtkAccelGroup *accel_group;
235 hard_coded_window_keys *window_keys;
237 item = menu_item_add(menu, label, func, data);
238 gtk_widget_set_sensitive(item, sensitive);
239 window_keys = static_cast<hard_coded_window_keys *>(g_object_get_data(G_OBJECT(menu), "window_keys"));
240 accel_group = static_cast<GtkAccelGroup *>(g_object_get_data(G_OBJECT(menu), "accel_group"));
241 if (accel_group && window_keys)
243 menu_item_add_accelerator(item, accel_group, window_keys);
245 else if (accel_group)
247 menu_item_add_main_window_accelerator(item, accel_group);
253 GtkWidget *menu_item_add_stock_sensitive(GtkWidget *menu, const gchar *label, const gchar *stock_id, gboolean sensitive,
254 GCallback func, gpointer data)
257 GtkAccelGroup *accel_group;
258 hard_coded_window_keys *window_keys;
260 item = menu_item_add_stock(menu, label, stock_id, func, data);
261 gtk_widget_set_sensitive(item, sensitive);
262 window_keys = static_cast<hard_coded_window_keys *>(g_object_get_data(G_OBJECT(menu), "window_keys"));
263 accel_group = static_cast<GtkAccelGroup *>(g_object_get_data(G_OBJECT(menu), "accel_group"));
264 if (accel_group && window_keys)
266 menu_item_add_accelerator(item, accel_group, window_keys);
268 else if (accel_group)
270 menu_item_add_main_window_accelerator(item, accel_group);
276 GtkWidget *menu_item_add_check(GtkWidget *menu, const gchar *label, gboolean active,
277 GCallback func, gpointer data)
280 GtkAccelGroup *accel_group;
281 hard_coded_window_keys *window_keys;
283 item = gtk_check_menu_item_new_with_mnemonic(label);
284 window_keys = static_cast<hard_coded_window_keys *>(g_object_get_data(G_OBJECT(menu), "window_keys"));
285 accel_group = static_cast<GtkAccelGroup *>(g_object_get_data(G_OBJECT(menu), "accel_group"));
287 if (accel_group && window_keys)
289 menu_item_add_accelerator(item, accel_group, window_keys);
291 else if (accel_group)
293 menu_item_add_main_window_accelerator(item, accel_group);
296 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), active);
297 menu_item_finish(menu, item, func, data);
302 GtkWidget *menu_item_add_radio(GtkWidget *menu, const gchar *label, gpointer item_data, gboolean active,
303 GCallback func, gpointer data)
305 GtkAccelGroup *accel_group;
306 hard_coded_window_keys *window_keys;
308 GtkWidget *item = menu_item_add_check(menu, label, active, func, data);
309 g_object_set_data(G_OBJECT(item), "menu_item_radio_data", item_data);
310 g_object_set(G_OBJECT(item), "draw-as-radio", TRUE, NULL);
312 window_keys = static_cast<hard_coded_window_keys *>(g_object_get_data(G_OBJECT(menu), "window_keys"));
313 accel_group = static_cast<GtkAccelGroup *>(g_object_get_data(G_OBJECT(menu), "accel_group"));
314 if (accel_group && window_keys)
316 menu_item_add_accelerator(item, accel_group, window_keys);
318 else if (accel_group)
320 menu_item_add_main_window_accelerator(item, accel_group);
326 void menu_item_add_divider(GtkWidget *menu)
328 GtkWidget *item = gtk_separator_menu_item_new();
329 gtk_widget_set_sensitive(item, FALSE);
330 gtk_menu_shell_append(GTK_MENU_SHELL(menu),item);
331 gtk_widget_show(item);
334 GtkWidget *menu_item_add_simple(GtkWidget *menu, const gchar *label,
335 GCallback func, gpointer data)
337 GtkWidget *item = gtk_menu_item_new_with_label(label);
338 menu_item_finish(menu, item, func, data);
344 *-----------------------------------------------------------------------------
346 *-----------------------------------------------------------------------------
349 static void popup_menu_short_lived_cb(GtkWidget *UNUSED(widget), gpointer data)
351 /* destroy the menu */
352 g_object_unref(G_OBJECT(data));
355 GtkWidget *popup_menu_short_lived(void)
359 menu = gtk_menu_new();
361 /* take ownership of menu */
362 #ifdef GTK_OBJECT_FLOATING
364 g_object_ref(G_OBJECT(menu));
365 gtk_object_sink(GTK_OBJECT(menu));
368 g_object_ref_sink(G_OBJECT(menu));
371 g_signal_connect(G_OBJECT(menu), "selection_done",
372 G_CALLBACK(popup_menu_short_lived_cb), menu);
376 gboolean popup_menu_position_clamp(GtkMenu *menu, gint *x, gint *y, gint height)
378 gboolean adjusted = FALSE;
381 GtkRequisition requisition;
383 gtk_widget_get_requisition(GTK_WIDGET(menu), &requisition);
384 w = requisition.width;
385 h = requisition.height;
386 xw = gdk_screen_width();
387 xh = gdk_screen_height();
398 *y = MAX(0, *y - h - height);
420 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */