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