Update test flags
[geeqie.git] / src / toolbar.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2017 The Geeqie Team
4  *
5  * Author: Colin Clark
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 #include "toolbar.h"
23
24 #include <cstddef>
25
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <glib-object.h>
28 #include <glib.h>
29
30 #include <config.h>
31
32 #include "compat.h"
33 #include "editors.h"
34 #include "intl.h"
35 #include "layout-util.h"
36 #include "layout.h"
37 #include "main-defines.h"
38 #include "ui-fileops.h"
39 #include "ui-menu.h"
40 #include "ui-misc.h"
41
42 /** Implements the user-definable toolbar function
43  * Called from the Preferences/toolbar tab
44  **/
45
46 namespace
47 {
48
49 struct ToolbarData
50 {
51         GtkWidget *vbox;
52 };
53
54 const gchar *action_name_key = "action_name";
55
56 ToolbarData *toolbarlist[2];
57
58 } // namespace
59
60 /**
61  * @brief
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
66  *
67  */
68 static void toolbar_item_move(GtkWidget *, gpointer data, gboolean up, gboolean single_step)
69 {
70         auto list_item = static_cast<GtkWidget *>(data);
71         GtkWidget *box;
72         gint pos = 0;
73
74         if (!list_item) return;
75         box = gtk_widget_get_ancestor(list_item, GTK_TYPE_BOX);
76         if (!box) return;
77
78         gtk_container_child_get(GTK_CONTAINER(box), list_item, "position", &pos, NULL);
79
80         if (single_step)
81                 {
82                 pos = up ? (pos - 1) : (pos + 1);
83                 if (pos < 0) pos = 0;
84                 }
85         else
86                 {
87                 pos = up ? 0 : -1;
88                 }
89
90         gtk_box_reorder_child(GTK_BOX(box), list_item, pos);
91 }
92
93 static void toolbar_item_move_up_cb(GtkWidget *widget, gpointer data)
94 {
95         toolbar_item_move(widget, data, TRUE, TRUE);
96 }
97
98 static void toolbar_item_move_down_cb(GtkWidget *widget, gpointer data)
99 {
100         toolbar_item_move(widget, data, FALSE, TRUE);
101 }
102
103 static void toolbar_item_move_top_cb(GtkWidget *widget, gpointer data)
104 {
105         toolbar_item_move(widget, data, TRUE, FALSE);
106 }
107
108 static void toolbar_item_move_bottom_cb(GtkWidget *widget, gpointer data)
109 {
110         toolbar_item_move(widget, data, FALSE, FALSE);
111 }
112
113 static void toolbar_item_delete_cb(GtkWidget *, gpointer data)
114 {
115         gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(GTK_WIDGET(data))), GTK_WIDGET(data));
116 }
117
118 static void toolbar_menu_popup(GtkWidget *widget)
119 {
120         GtkWidget *menu;
121
122         menu = popup_menu_short_lived();
123
124         if (widget)
125                 {
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);
133                 }
134
135         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
136 }
137
138 static gboolean toolbar_press_cb(GtkGesture *, int, double, double, gpointer data)
139 {
140         auto *button = static_cast<GtkWidget *>(data);
141
142         toolbar_menu_popup(button);
143
144         return TRUE;
145 }
146
147 static void get_toolbar_item(const gchar *name, gchar **label, gchar **stock_id)
148 {
149         GList *list;
150         GList *work;
151         *label = nullptr;
152         *stock_id = nullptr;
153
154         list = get_action_items();
155
156         const auto action_item_compare_name = [](gconstpointer data, gconstpointer user_data)
157         {
158                 return g_strcmp0(static_cast<const ActionItem *>(data)->name, static_cast<const gchar *>(user_data));
159         };
160         work = g_list_find_custom(list, name, action_item_compare_name);
161         if (work)
162                 {
163                 auto *action_item = static_cast<ActionItem *>(work->data);
164
165                 *label = g_strdup(action_item->label);
166                 *stock_id = g_strdup(action_item->icon_name);
167                 }
168
169         action_items_free(list);
170 }
171
172 static void toolbar_button_free(GtkWidget *widget)
173 {
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"));
177 }
178
179 static void toolbarlist_add_button(const gchar *name, const gchar *label,
180                                                                         const gchar *stock_id, GtkBox *box)
181 {
182         GtkWidget *hbox;
183         GtkGesture *gesture;
184
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);
189
190         g_object_set_data_full(G_OBJECT(button), action_name_key, g_strdup(name), g_free);
191
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);
195
196 #if HAVE_GTK4
197         gesture = gtk_gesture_click_new();
198         gtk_widget_add_controller(button, GTK_EVENT_CONTROLLER(gesture));
199 #else
200         gesture = gtk_gesture_multi_press_new(button);
201 #endif
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);
204
205         GtkWidget *image;
206         if (stock_id)
207                 {
208                 GdkPixbuf *pixbuf;
209                 gchar *iconl;
210                 iconl = path_from_utf8(stock_id);
211                 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
212                 g_free(iconl);
213                 if (pixbuf)
214                         {
215                         GdkPixbuf *scaled;
216                         gint w;
217                         gint h;
218
219                         w = h = 16;
220                         gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
221
222                         scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
223                                                          GDK_INTERP_BILINEAR);
224                         image = gtk_image_new_from_pixbuf(scaled);
225
226                         g_object_unref(scaled);
227                         g_object_unref(pixbuf);
228                         }
229                 else
230                         {
231                         image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
232                         }
233                 }
234         else
235                 {
236                 image = gtk_image_new_from_icon_name(GQ_ICON_GO_JUMP, GTK_ICON_SIZE_BUTTON);
237                 }
238         gq_gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
239         gtk_widget_show(image);
240
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);
244 }
245
246 static void toolbarlist_add_cb(GtkWidget *widget, gpointer data)
247 {
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);
252
253         toolbarlist_add_button(name, label, stock_id, GTK_BOX(tbbd->vbox));
254 }
255
256 static void get_desktop_data(const gchar *name, gchar **label, gchar **stock_id)
257 {
258         GList *editors_list;
259         GList *work;
260         *label = nullptr;
261         *stock_id = nullptr;
262
263         editors_list = editor_list_get();
264         const auto editor_compare_key = [](gconstpointer data, gconstpointer user_data)
265         {
266                 return g_strcmp0(static_cast<const EditorDescription *>(data)->key, static_cast<const gchar *>(user_data));
267         };
268         work = g_list_find_custom(editors_list, name, editor_compare_key);
269         if (work)
270                 {
271                 auto editor = static_cast<const EditorDescription *>(work->data);
272
273                 *label = g_strdup(editor->name);
274                 *stock_id = g_strconcat(editor->icon, ".desktop", NULL);
275                 }
276         g_list_free(editors_list);
277 }
278
279 // toolbar_menu_add_popup
280 static gboolean toolbar_menu_add_cb(GtkWidget *, gpointer data)
281 {
282         ActionItem *action_item;
283         GList *list;
284         GList *work;
285         GtkWidget *item;
286         GtkWidget *menu;
287
288         menu = popup_menu_short_lived();
289
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);
295
296         list = get_action_items();
297
298         work = list;
299         while (work)
300                 {
301                 action_item = static_cast<ActionItem *>(work->data);
302
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);
308
309                 work = work->next;
310                 }
311
312         action_items_free(list);
313
314         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
315
316         return TRUE;
317 }
318
319 /**
320  * @brief For each layoutwindow, clear toolbar and reload with current selection
321  * @param bar Main or Status toolbar
322  *
323  */
324 void toolbar_apply(ToolbarType bar)
325 {
326         const auto layout_toolbar_apply = [](gpointer data, gpointer user_data)
327         {
328                 auto *lw = static_cast<LayoutWindow *>(data);
329                 auto bar = static_cast<ToolbarType>(GPOINTER_TO_INT(user_data));
330
331                 layout_toolbar_clear(lw, bar);
332
333                 GList *work_toolbar = gtk_container_get_children(GTK_CONTAINER(toolbarlist[bar]->vbox));
334                 for (GList *work = work_toolbar; work; work = work->next)
335                         {
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));
338
339                         layout_toolbar_add(lw, bar, action_name);
340                         }
341                 g_list_free(work_toolbar);
342         };
343
344         g_list_foreach(layout_window_list, layout_toolbar_apply, GINT_TO_POINTER(bar));
345 }
346
347 /**
348  * @brief Load the current toolbar items into the vbox
349  * @param lw
350  * @param box The vbox displayed in the preferences Toolbar tab
351  * @param bar Main or Status toolbar
352  *
353  * Get the current contents of the toolbar, both menu items
354  * and desktop items, and load them into the vbox
355  */
356 static void toolbarlist_populate(LayoutWindow *lw, GtkBox *box, ToolbarType bar)
357 {
358         GList *work = g_list_first(lw->toolbar_actions[bar]);
359
360         while (work)
361                 {
362                 auto name = static_cast<gchar *>(work->data);
363                 gchar *label;
364                 gchar *icon;
365                 work = work->next;
366
367                 if (file_extension_match(name, ".desktop"))
368                         {
369                         get_desktop_data(name, &label, &icon);
370                         }
371                 else
372                         {
373                         get_toolbar_item(name, &label, &icon);
374                         }
375
376                 if (g_strcmp0(name, "Separator") != 0)
377                         {
378                         toolbarlist_add_button(name, label, icon, box);
379                         }
380                 else
381                         {
382                         toolbarlist_add_button(name, name, "no-icon", box);
383                         }
384                 }
385 }
386
387 GtkWidget *toolbar_select_new(LayoutWindow *lw, ToolbarType bar)
388 {
389         GtkWidget *scrolled;
390         GtkWidget *tbar;
391         GtkWidget *add_box;
392
393         if (!lw) return nullptr;
394
395         if (!toolbarlist[bar])
396                 {
397                 toolbarlist[bar] = g_new0(ToolbarData, 1);
398                 }
399
400         GtkWidget *widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
401         gtk_widget_show(widget);
402
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);
409
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))),
414                                                                                                                                 GTK_SHADOW_NONE);
415
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);
420
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);
425
426         toolbarlist_populate(lw,GTK_BOX(toolbarlist[bar]->vbox), bar);
427
428         return widget;
429 }
430
431 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */