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