2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2017 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.
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <glib-object.h>
35 #include "layout-util.h"
37 #include "main-defines.h"
38 #include "ui-fileops.h"
42 /** Implements the user-definable toolbar function
43 * Called from the Preferences/toolbar tab
54 const gchar *action_name_key = "action_name";
56 ToolbarData *toolbarlist[2];
62 * @param widget Not used
63 * @param data Pointer to vbox list item
64 * @param up Up/Down movement
65 * @param single_step Move up/down one step, or to top/bottom
68 static void toolbar_item_move(GtkWidget *, gpointer data, gboolean up, gboolean single_step)
70 auto list_item = static_cast<GtkWidget *>(data);
74 if (!list_item) return;
75 box = gtk_widget_get_ancestor(list_item, GTK_TYPE_BOX);
78 gtk_container_child_get(GTK_CONTAINER(box), list_item, "position", &pos, NULL);
82 pos = up ? (pos - 1) : (pos + 1);
90 gtk_box_reorder_child(GTK_BOX(box), list_item, pos);
93 static void toolbar_item_move_up_cb(GtkWidget *widget, gpointer data)
95 toolbar_item_move(widget, data, TRUE, TRUE);
98 static void toolbar_item_move_down_cb(GtkWidget *widget, gpointer data)
100 toolbar_item_move(widget, data, FALSE, TRUE);
103 static void toolbar_item_move_top_cb(GtkWidget *widget, gpointer data)
105 toolbar_item_move(widget, data, TRUE, FALSE);
108 static void toolbar_item_move_bottom_cb(GtkWidget *widget, gpointer data)
110 toolbar_item_move(widget, data, FALSE, FALSE);
113 static void toolbar_item_delete_cb(GtkWidget *, gpointer data)
115 gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(GTK_WIDGET(data))), GTK_WIDGET(data));
118 static void toolbar_menu_popup(GtkWidget *widget)
122 menu = popup_menu_short_lived();
126 menu_item_add_icon(menu, _("Move to _top"), GQ_ICON_GO_TOP, G_CALLBACK(toolbar_item_move_top_cb), widget);
127 menu_item_add_icon(menu, _("Move _up"), GQ_ICON_GO_UP, G_CALLBACK(toolbar_item_move_up_cb), widget);
128 menu_item_add_icon(menu, _("Move _down"), GQ_ICON_GO_DOWN, G_CALLBACK(toolbar_item_move_down_cb), widget);
129 menu_item_add_icon(menu, _("Move to _bottom"), GQ_ICON_GO_BOTTOM, G_CALLBACK(toolbar_item_move_bottom_cb), widget);
130 menu_item_add_divider(menu);
131 menu_item_add_icon(menu, _("Remove"), GQ_ICON_DELETE, G_CALLBACK(toolbar_item_delete_cb), widget);
132 menu_item_add_divider(menu);
135 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
138 static gboolean toolbar_press_cb(GtkGesture *, int, double, double, gpointer data)
140 auto *button = static_cast<GtkWidget *>(data);
142 toolbar_menu_popup(button);
147 static void get_toolbar_item(const gchar *name, gchar **label, gchar **stock_id)
154 list = get_action_items();
156 const auto action_item_compare_name = [](gconstpointer data, gconstpointer user_data)
158 return g_strcmp0(static_cast<const ActionItem *>(data)->name, static_cast<const gchar *>(user_data));
160 work = g_list_find_custom(list, name, action_item_compare_name);
163 auto *action_item = static_cast<ActionItem *>(work->data);
165 *label = g_strdup(action_item->label);
166 *stock_id = g_strdup(action_item->icon_name);
169 action_items_free(list);
172 static void toolbar_button_free(GtkWidget *widget)
174 g_free(g_object_get_data(G_OBJECT(widget), "toolbar_add_name"));
175 g_free(g_object_get_data(G_OBJECT(widget), "toolbar_add_label"));
176 g_free(g_object_get_data(G_OBJECT(widget), "toolbar_add_stock_id"));
179 static void toolbarlist_add_button(const gchar *name, const gchar *label,
180 const gchar *stock_id, GtkBox *box)
185 GtkWidget *button = gtk_button_new();
186 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
187 gq_gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
188 gtk_widget_show(button);
190 g_object_set_data_full(G_OBJECT(button), action_name_key, g_strdup(name), g_free);
192 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
193 gq_gtk_container_add(GTK_WIDGET(button), hbox);
194 gtk_widget_show(hbox);
197 gesture = gtk_gesture_click_new();
198 gtk_widget_add_controller(button, GTK_EVENT_CONTROLLER(gesture));
200 gesture = gtk_gesture_multi_press_new(button);
202 gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), MOUSE_BUTTON_RIGHT);
203 g_signal_connect(gesture, "released", G_CALLBACK(toolbar_press_cb), button);
210 iconl = path_from_utf8(stock_id);
211 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
220 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
222 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
223 GDK_INTERP_BILINEAR);
224 image = gtk_image_new_from_pixbuf(scaled);
226 g_object_unref(scaled);
227 g_object_unref(pixbuf);
231 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
236 image = gtk_image_new_from_icon_name(GQ_ICON_GO_JUMP, GTK_ICON_SIZE_BUTTON);
238 gq_gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
239 gtk_widget_show(image);
241 GtkWidget *button_label = gtk_label_new(label);
242 gq_gtk_box_pack_start(GTK_BOX(hbox), button_label, FALSE, FALSE, 0);
243 gtk_widget_show(button_label);
246 static void toolbarlist_add_cb(GtkWidget *widget, gpointer data)
248 auto name = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_name"));
249 auto label = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_label"));
250 auto stock_id = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_stock_id"));
251 auto tbbd = static_cast<ToolbarData *>(data);
253 toolbarlist_add_button(name, label, stock_id, GTK_BOX(tbbd->vbox));
256 static void get_desktop_data(const gchar *name, gchar **label, gchar **stock_id)
263 editors_list = editor_list_get();
264 const auto editor_compare_key = [](gconstpointer data, gconstpointer user_data)
266 return g_strcmp0(static_cast<const EditorDescription *>(data)->key, static_cast<const gchar *>(user_data));
268 work = g_list_find_custom(editors_list, name, editor_compare_key);
271 auto editor = static_cast<const EditorDescription *>(work->data);
273 *label = g_strdup(editor->name);
274 *stock_id = g_strconcat(editor->icon, ".desktop", NULL);
276 g_list_free(editors_list);
279 // toolbar_menu_add_popup
280 static gboolean toolbar_menu_add_cb(GtkWidget *, gpointer data)
282 ActionItem *action_item;
288 menu = popup_menu_short_lived();
290 item = menu_item_add_stock(menu, "Separator", "Separator", G_CALLBACK(toolbarlist_add_cb), data);
291 g_object_set_data(G_OBJECT(item), "toolbar_add_name", g_strdup("Separator"));
292 g_object_set_data(G_OBJECT(item), "toolbar_add_label", g_strdup("Separator"));
293 g_object_set_data(G_OBJECT(item), "toolbar_add_stock_id", g_strdup("no-icon"));
294 g_signal_connect(G_OBJECT(item), "destroy", G_CALLBACK(toolbar_button_free), item);
296 list = get_action_items();
301 action_item = static_cast<ActionItem *>(work->data);
303 item = menu_item_add_stock(menu, action_item->label, action_item->icon_name, G_CALLBACK(toolbarlist_add_cb), data);
304 g_object_set_data(G_OBJECT(item), "toolbar_add_name", g_strdup(action_item->name));
305 g_object_set_data(G_OBJECT(item), "toolbar_add_label", g_strdup(action_item->label));
306 g_object_set_data(G_OBJECT(item), "toolbar_add_stock_id", g_strdup(action_item->icon_name));
307 g_signal_connect(G_OBJECT(item), "destroy", G_CALLBACK(toolbar_button_free), item);
312 action_items_free(list);
314 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
320 * @brief For each layoutwindow, clear toolbar and reload with current selection
321 * @param bar Main or Status toolbar
324 void toolbar_apply(ToolbarType bar)
326 const auto layout_toolbar_apply = [](gpointer data, gpointer user_data)
328 auto *lw = static_cast<LayoutWindow *>(data);
329 auto bar = static_cast<ToolbarType>(GPOINTER_TO_INT(user_data));
331 layout_toolbar_clear(lw, bar);
333 GList *work_toolbar = gtk_container_get_children(GTK_CONTAINER(toolbarlist[bar]->vbox));
334 for (GList *work = work_toolbar; work; work = work->next)
336 auto button = static_cast<GtkButton *>(work->data);
337 auto *action_name = static_cast<gchar *>(g_object_get_data(G_OBJECT(button), action_name_key));
339 layout_toolbar_add(lw, bar, action_name);
341 g_list_free(work_toolbar);
344 g_list_foreach(layout_window_list, layout_toolbar_apply, GINT_TO_POINTER(bar));
348 * @brief Load the current toolbar items into the vbox
350 * @param box The vbox displayed in the preferences Toolbar tab
351 * @param bar Main or Status toolbar
353 * Get the current contents of the toolbar, both menu items
354 * and desktop items, and load them into the vbox
356 static void toolbarlist_populate(LayoutWindow *lw, GtkBox *box, ToolbarType bar)
358 GList *work = g_list_first(lw->toolbar_actions[bar]);
362 auto name = static_cast<gchar *>(work->data);
367 if (file_extension_match(name, ".desktop"))
369 get_desktop_data(name, &label, &icon);
373 get_toolbar_item(name, &label, &icon);
376 if (g_strcmp0(name, "Separator") != 0)
378 toolbarlist_add_button(name, label, icon, box);
382 toolbarlist_add_button(name, name, "no-icon", box);
387 GtkWidget *toolbar_select_new(LayoutWindow *lw, ToolbarType bar)
393 if (!lw) return nullptr;
395 if (!toolbarlist[bar])
397 toolbarlist[bar] = g_new0(ToolbarData, 1);
400 GtkWidget *widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
401 gtk_widget_show(widget);
403 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
404 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
405 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
406 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_NONE);
407 gq_gtk_box_pack_start(GTK_BOX(widget), scrolled, TRUE, TRUE, 0);
408 gtk_widget_show(scrolled);
410 toolbarlist[bar]->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
411 gtk_widget_show(toolbarlist[bar]->vbox);
412 gq_gtk_container_add(GTK_WIDGET(scrolled), toolbarlist[bar]->vbox);
413 gtk_viewport_set_shadow_type(GTK_VIEWPORT(gtk_bin_get_child(GTK_BIN(scrolled))),
416 add_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
417 gtk_widget_show(add_box);
418 gq_gtk_box_pack_end(GTK_BOX(widget), add_box, FALSE, FALSE, 0);
419 tbar = pref_toolbar_new(add_box);
421 GtkWidget *add_button = pref_toolbar_button(tbar, GQ_ICON_ADD, _("Add"), FALSE,
422 _("Add Toolbar Item"),
423 G_CALLBACK(toolbar_menu_add_cb), toolbarlist[bar]);
424 gtk_widget_show(add_button);
426 toolbarlist_populate(lw,GTK_BOX(toolbarlist[bar]->vbox), bar);
431 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */