95cc6916158e79b111c01140db61e54e877e34c8
[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 <config.h>
25
26 #include "compat.h"
27 #include "intl.h"
28 #include "layout-util.h"
29 #include "main-defines.h"
30 #include "ui-fileops.h"
31 #include "ui-misc.h"
32 #include "pixbuf-util.h"
33 #include "ui-menu.h"
34 #include "editors.h"
35
36 /** Implements the user-definable toolbar function
37  * Called from the Preferences/toolbar tab
38  **/
39
40 struct ToolbarData
41 {
42         GtkWidget *widget;
43         GtkWidget *vbox;
44         GtkWidget *add_button;
45
46         LayoutWindow *lw;
47 };
48
49 struct ToolbarButtonData
50 {
51         GtkWidget *button;
52         GtkWidget *button_label;
53         GtkWidget *image;
54
55         const gchar *name; /* GtkActionEntry terminology */
56         const gchar *stock_id;
57 };
58
59 static ToolbarData *toolbarlist[2];
60
61 struct UseableToolbarItems
62 {
63         const gchar *name; /* GtkActionEntry terminology */
64         const gchar *label;
65         const gchar *stock_id;
66 };
67
68 /**
69  * @brief
70  * @param widget Not used
71  * @param data Pointer to vbox list item
72  * @param up Up/Down movement
73  * @param single_step Move up/down one step, or to top/bottom
74  *
75  */
76 static void toolbar_item_move(GtkWidget *, gpointer data, gboolean up, gboolean single_step)
77 {
78         auto list_item = static_cast<GtkWidget *>(data);
79         GtkWidget *box;
80         gint pos = 0;
81
82         if (!list_item) return;
83         box = gtk_widget_get_ancestor(list_item, GTK_TYPE_BOX);
84         if (!box) return;
85
86         gtk_container_child_get(GTK_CONTAINER(box), list_item, "position", &pos, NULL);
87
88         if (single_step)
89                 {
90                 pos = up ? (pos - 1) : (pos + 1);
91                 if (pos < 0) pos = 0;
92                 }
93         else
94                 {
95                 pos = up ? 0 : -1;
96                 }
97
98         gtk_box_reorder_child(GTK_BOX(box), list_item, pos);
99 }
100
101 static void toolbar_item_move_up_cb(GtkWidget *widget, gpointer data)
102 {
103         toolbar_item_move(widget, data, TRUE, TRUE);
104 }
105
106 static void toolbar_item_move_down_cb(GtkWidget *widget, gpointer data)
107 {
108         toolbar_item_move(widget, data, FALSE, TRUE);
109 }
110
111 static void toolbar_item_move_top_cb(GtkWidget *widget, gpointer data)
112 {
113         toolbar_item_move(widget, data, TRUE, FALSE);
114 }
115
116 static void toolbar_item_move_bottom_cb(GtkWidget *widget, gpointer data)
117 {
118         toolbar_item_move(widget, data, FALSE, FALSE);
119 }
120
121 static void toolbar_item_delete_cb(GtkWidget *, gpointer data)
122 {
123         gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(GTK_WIDGET(data))), GTK_WIDGET(data));
124 }
125
126 static void toolbar_menu_popup(GtkWidget *widget)
127 {
128         GtkWidget *menu;
129
130         menu = popup_menu_short_lived();
131
132         if (widget)
133                 {
134                 menu_item_add_icon(menu, _("Move to _top"), GQ_ICON_GO_TOP, G_CALLBACK(toolbar_item_move_top_cb), widget);
135                 menu_item_add_icon(menu, _("Move _up"), GQ_ICON_GO_UP, G_CALLBACK(toolbar_item_move_up_cb), widget);
136                 menu_item_add_icon(menu, _("Move _down"), GQ_ICON_GO_DOWN, G_CALLBACK(toolbar_item_move_down_cb), widget);
137                 menu_item_add_icon(menu, _("Move to _bottom"), GQ_ICON_GO_BOTTOM, G_CALLBACK(toolbar_item_move_bottom_cb), widget);
138                 menu_item_add_divider(menu);
139                 menu_item_add_icon(menu, _("Remove"), GQ_ICON_DELETE, G_CALLBACK(toolbar_item_delete_cb), widget);
140                 menu_item_add_divider(menu);
141                 }
142
143         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
144 }
145
146 static gboolean toolbar_press_cb(GtkGesture *, int, double, double, gpointer data)
147 {
148         auto button_data = static_cast<ToolbarButtonData *>(data);
149
150         toolbar_menu_popup(button_data->button);
151
152         return TRUE;
153 }
154
155 static void get_toolbar_item(const gchar *name, gchar **label, gchar **stock_id)
156 {
157         ActionItem *action_item;
158         GList *list;
159         GList *work;
160         *label = nullptr;
161         *stock_id = nullptr;
162
163         list = get_action_items();
164
165         work = list;
166         while (work)
167                 {
168                 action_item = static_cast<ActionItem *>(work->data);
169                 if (g_strcmp0(action_item->name, name) == 0)
170                         {
171                         *label = g_strdup(action_item->label);
172                         *stock_id = g_strdup(action_item->icon_name);
173                         break;
174                         }
175
176                 work = work->next;
177                 }
178
179         action_items_free(list);
180 }
181
182 static void toolbar_item_free(ToolbarButtonData *tbbd)
183 {
184         if (!tbbd) return;
185
186         g_free(const_cast<gchar *>(tbbd->name));
187         g_free(const_cast<gchar *>(tbbd->stock_id));
188         g_free(const_cast<ToolbarButtonData *>(tbbd));
189 }
190
191 static void toolbar_button_free(GtkWidget *widget)
192 {
193         g_free(g_object_get_data(G_OBJECT(widget), "toolbar_add_name"));
194         g_free(g_object_get_data(G_OBJECT(widget), "toolbar_add_label"));
195         g_free(g_object_get_data(G_OBJECT(widget), "toolbar_add_stock_id"));
196 }
197
198 static void toolbarlist_add_button(const gchar *name, const gchar *label,
199                                                                         const gchar *stock_id, GtkBox *box)
200 {
201         ToolbarButtonData *toolbar_entry;
202         GtkWidget *hbox;
203         GtkGesture *gesture;
204
205         toolbar_entry = g_new(ToolbarButtonData,1);
206         toolbar_entry->button = gtk_button_new();
207         gtk_button_set_relief(GTK_BUTTON(toolbar_entry->button), GTK_RELIEF_NONE);
208         gq_gtk_box_pack_start(GTK_BOX(box), toolbar_entry->button, FALSE, FALSE, 0);
209         gtk_widget_show(toolbar_entry->button);
210
211         g_object_set_data_full(G_OBJECT(toolbar_entry->button), "toolbarbuttondata",
212         toolbar_entry, reinterpret_cast<GDestroyNotify>(toolbar_item_free));
213
214         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
215         gq_gtk_container_add(GTK_WIDGET(toolbar_entry->button), hbox);
216         gtk_widget_show(hbox);
217
218         toolbar_entry->button_label = gtk_label_new(label);
219         toolbar_entry->name = g_strdup(name);
220         toolbar_entry->stock_id = g_strdup(stock_id);
221
222 #ifdef HAVE_GTK4
223         gesture = gtk_gesture_click_new();
224         gtk_widget_add_controller(toolbar_entry->button, GTK_EVENT_CONTROLLER(gesture));
225 #else
226         gesture = gtk_gesture_multi_press_new(toolbar_entry->button);
227 #endif
228         gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), MOUSE_BUTTON_RIGHT);
229         g_signal_connect(gesture, "released", G_CALLBACK(toolbar_press_cb), toolbar_entry);
230
231         if (toolbar_entry->stock_id)
232                 {
233                 GdkPixbuf *pixbuf;
234                 gchar *iconl;
235                 iconl = path_from_utf8(toolbar_entry->stock_id);
236                 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
237                 g_free(iconl);
238                 if (pixbuf)
239                         {
240                         GdkPixbuf *scaled;
241                         gint w;
242                         gint h;
243
244                         w = h = 16;
245                         gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
246
247                         scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
248                                                          GDK_INTERP_BILINEAR);
249                         toolbar_entry->image = gtk_image_new_from_pixbuf(scaled);
250
251                         g_object_unref(scaled);
252                         g_object_unref(pixbuf);
253                         }
254                 else
255                         {
256                         toolbar_entry->image = gtk_image_new_from_stock(toolbar_entry->stock_id,
257                                                                                                                 GTK_ICON_SIZE_BUTTON);
258                         }
259                 }
260         else
261                 {
262                 toolbar_entry->image = gtk_image_new_from_icon_name(GQ_ICON_GO_JUMP,
263                                                                                                                 GTK_ICON_SIZE_BUTTON);
264                 }
265         gq_gtk_box_pack_start(GTK_BOX(hbox), toolbar_entry->image, FALSE, FALSE, 0);
266         gtk_widget_show(toolbar_entry->image);
267         gq_gtk_box_pack_start(GTK_BOX(hbox), toolbar_entry->button_label, FALSE, FALSE, 0);
268         gtk_widget_show(toolbar_entry->button_label);
269 }
270
271 static void toolbarlist_add_cb(GtkWidget *widget, gpointer data)
272 {
273         auto name = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_name"));
274         auto label = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_label"));
275         auto stock_id = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_stock_id"));
276         auto tbbd = static_cast<ToolbarData *>(data);
277
278         toolbarlist_add_button(name, label, stock_id, GTK_BOX(tbbd->vbox));
279 }
280
281 static void get_desktop_data(const gchar *name, gchar **label, gchar **stock_id)
282 {
283         GList *editors_list;
284         GList *work;
285         *label = nullptr;
286         *stock_id = nullptr;
287
288         editors_list = editor_list_get();
289         work = editors_list;
290         while (work)
291                 {
292                 auto editor = static_cast<const EditorDescription *>(work->data);
293
294                 if (g_strcmp0(name, editor->key) == 0)
295                         {
296                         *label = g_strdup(editor->name);
297                         *stock_id = g_strconcat(editor->icon, ".desktop", NULL);
298                         break;
299                         }
300                 work = work->next;
301                 }
302         g_list_free(editors_list);
303 }
304
305 static void toolbar_menu_add_popup(GtkWidget *, gpointer data)
306 {
307         ActionItem *action_item;
308         auto toolbarlist = static_cast<ToolbarData *>(data);
309         GList *list;
310         GList *work;
311         GtkWidget *item;
312         GtkWidget *menu;
313
314         menu = popup_menu_short_lived();
315
316         item = menu_item_add_stock(menu, "Separator", "Separator", G_CALLBACK(toolbarlist_add_cb), toolbarlist);
317         g_object_set_data(G_OBJECT(item), "toolbar_add_name", g_strdup("Separator"));
318         g_object_set_data(G_OBJECT(item), "toolbar_add_label", g_strdup("Separator"));
319         g_object_set_data(G_OBJECT(item), "toolbar_add_stock_id", g_strdup("no-icon"));
320         g_signal_connect(G_OBJECT(item), "destroy", G_CALLBACK(toolbar_button_free), item);
321
322         list = get_action_items();
323
324         work = list;
325         while (work)
326                 {
327                 action_item = static_cast<ActionItem *>(work->data);
328
329                 item = menu_item_add_stock(menu, action_item->label, action_item->icon_name, G_CALLBACK(toolbarlist_add_cb), toolbarlist);
330                 g_object_set_data(G_OBJECT(item), "toolbar_add_name", g_strdup(action_item->name));
331                 g_object_set_data(G_OBJECT(item), "toolbar_add_label", g_strdup(action_item->label));
332                 g_object_set_data(G_OBJECT(item), "toolbar_add_stock_id", g_strdup(action_item->icon_name));
333                 g_signal_connect(G_OBJECT(item), "destroy", G_CALLBACK(toolbar_button_free), item);
334
335                 work = work->next;
336                 }
337
338         action_items_free(list);
339
340         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
341 }
342
343 static gboolean toolbar_menu_add_cb(GtkWidget *widget, gpointer data)
344 {
345         auto toolbarlist = static_cast<ToolbarData *>(data);
346
347         toolbar_menu_add_popup(widget, toolbarlist);
348         return TRUE;
349 }
350
351 /**
352  * @brief For each layoutwindow, clear toolbar and reload with current selection
353  * @param bar Main or Status toolbar
354  *
355  */
356 void toolbar_apply(ToolbarType bar)
357 {
358         LayoutWindow *lw;
359         GList *work_windows;
360         GList *work_toolbar;
361
362         work_windows = layout_window_list;
363         while (work_windows)
364                 {
365                 lw = static_cast<LayoutWindow *>(work_windows->data);
366
367                 layout_toolbar_clear(lw, bar);
368
369                 work_toolbar = gtk_container_get_children(GTK_CONTAINER(toolbarlist[bar]->vbox));
370                 while (work_toolbar)
371                         {
372                         auto button = static_cast<GtkButton *>(work_toolbar->data);
373                         ToolbarButtonData *tbbd;
374
375                         tbbd = static_cast<ToolbarButtonData *>(g_object_get_data(G_OBJECT(button),"toolbarbuttondata"));
376                         layout_toolbar_add(lw, bar, tbbd->name);
377
378                         work_toolbar = work_toolbar->next;
379                         }
380                 g_list_free(work_toolbar);
381
382                 work_windows = work_windows->next;
383                 }
384
385 }
386
387 /**
388  * @brief Load the current toolbar items into the vbox
389  * @param lw
390  * @param box The vbox displayed in the preferences Toolbar tab
391  * @param bar Main or Status toolbar
392  *
393  * Get the current contents of the toolbar, both menu items
394  * and desktop items, and load them into the vbox
395  */
396 static void toolbarlist_populate(LayoutWindow *lw, GtkBox *box, ToolbarType bar)
397 {
398         GList *work = g_list_first(lw->toolbar_actions[bar]);
399
400         while (work)
401                 {
402                 auto name = static_cast<gchar *>(work->data);
403                 gchar *label;
404                 gchar *icon;
405                 work = work->next;
406
407                 if (file_extension_match(name, ".desktop"))
408                         {
409                         get_desktop_data(name, &label, &icon);
410                         }
411                 else
412                         {
413                         get_toolbar_item(name, &label, &icon);
414                         }
415
416                 if (g_strcmp0(name, "Separator") != 0)
417                         {
418                         toolbarlist_add_button(name, label, icon, box);
419                         }
420                 else
421                         {
422                         toolbarlist_add_button(name, name, "no-icon", box);
423                         }
424                 }
425 }
426
427 GtkWidget *toolbar_select_new(LayoutWindow *lw, ToolbarType bar)
428 {
429         GtkWidget *scrolled;
430         GtkWidget *tbar;
431         GtkWidget *add_box;
432
433         if (!lw) return nullptr;
434
435         if (!toolbarlist[bar])
436                 {
437                 toolbarlist[bar] = g_new0(ToolbarData, 1);
438                 }
439         toolbarlist[bar]->lw = lw;
440
441         toolbarlist[bar]->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
442         gtk_widget_show(toolbarlist[bar]->widget);
443
444         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
445         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
446                                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
447         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_NONE);
448         gq_gtk_box_pack_start(GTK_BOX(toolbarlist[bar]->widget), scrolled, TRUE, TRUE, 0);
449         gtk_widget_show(scrolled);
450
451         toolbarlist[bar]->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
452         gtk_widget_show(toolbarlist[bar]->vbox);
453         gq_gtk_container_add(GTK_WIDGET(scrolled), toolbarlist[bar]->vbox);
454         gtk_viewport_set_shadow_type(GTK_VIEWPORT(gtk_bin_get_child(GTK_BIN(scrolled))),
455                                                                                                                                 GTK_SHADOW_NONE);
456
457         add_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
458         gtk_widget_show(add_box);
459         gq_gtk_box_pack_end(GTK_BOX(toolbarlist[bar]->widget), add_box, FALSE, FALSE, 0);
460         tbar = pref_toolbar_new(add_box);
461         toolbarlist[bar]->add_button = pref_toolbar_button(tbar, GQ_ICON_ADD, _("Add"), FALSE,
462                                                                                         _("Add Toolbar Item"),
463                                                                                         G_CALLBACK(toolbar_menu_add_cb), toolbarlist[bar]);
464         gtk_widget_show(toolbarlist[bar]->add_button);
465
466         toolbarlist_populate(lw,GTK_BOX(toolbarlist[bar]->vbox), bar);
467
468         return toolbarlist[bar]->widget;
469 }
470
471 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */