Open With feature
[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         g_object_unref(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, h;
238
239                         w = h = 16;
240                         gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
241
242                         scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
243                                                          GDK_INTERP_BILINEAR);
244                         toolbar_entry->image = gtk_image_new_from_pixbuf(scaled);
245
246                         g_object_unref(scaled);
247                         g_object_unref(pixbuf);
248                         }
249                 else
250                         {
251                         toolbar_entry->image = gtk_image_new_from_stock(toolbar_entry->stock_id,
252                                                                                                                 GTK_ICON_SIZE_BUTTON);
253                         }
254                 }
255         else
256                 {
257                 toolbar_entry->image = gtk_image_new_from_icon_name(GQ_ICON_GO_JUMP,
258                                                                                                                 GTK_ICON_SIZE_BUTTON);
259                 }
260         gq_gtk_box_pack_start(GTK_BOX(hbox), toolbar_entry->image, FALSE, FALSE, 0);
261         gtk_widget_show(toolbar_entry->image);
262         gq_gtk_box_pack_start(GTK_BOX(hbox), toolbar_entry->button_label, FALSE, FALSE, 0);
263         gtk_widget_show(toolbar_entry->button_label);
264 }
265
266 static void toolbarlist_add_cb(GtkWidget *widget, gpointer data)
267 {
268         auto name = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_name"));
269         auto label = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_label"));
270         auto stock_id = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "toolbar_add_stock_id"));
271         auto tbbd = static_cast<ToolbarData *>(data);
272
273         toolbarlist_add_button(name, label, stock_id, GTK_BOX(tbbd->vbox));
274 }
275
276 static void get_desktop_data(const gchar *name, gchar **label, gchar **stock_id)
277 {
278         GList *editors_list;
279         GList *work;
280         *label = nullptr;
281         *stock_id = nullptr;
282
283         editors_list = editor_list_get();
284         work = editors_list;
285         while (work)
286                 {
287                 auto editor = static_cast<const EditorDescription *>(work->data);
288
289                 if (g_strcmp0(name, editor->key) == 0)
290                         {
291                         *label = g_strdup(editor->name);
292                         *stock_id = g_strconcat(editor->icon, ".desktop", NULL);
293                         break;
294                         }
295                 work = work->next;
296                 }
297         g_list_free(editors_list);
298 }
299
300 static void toolbar_menu_add_popup(GtkWidget *, gpointer data)
301 {
302         ActionItem *action_item;
303         auto toolbarlist = static_cast<ToolbarData *>(data);
304         GList *list;
305         GList *work;
306         GtkWidget *item;
307         GtkWidget *menu;
308
309         menu = popup_menu_short_lived();
310
311         item = menu_item_add_stock(menu, "Separator", "Separator", G_CALLBACK(toolbarlist_add_cb), toolbarlist);
312         g_object_set_data(G_OBJECT(item), "toolbar_add_name", g_strdup("Separator"));
313         g_object_set_data(G_OBJECT(item), "toolbar_add_label", g_strdup("Separator"));
314         g_object_set_data(G_OBJECT(item), "toolbar_add_stock_id", g_strdup("no-icon"));
315         g_signal_connect(G_OBJECT(item), "destroy", G_CALLBACK(toolbar_button_free), item);
316
317         list = get_action_items();
318
319         work = list;
320         while (work)
321                 {
322                 action_item = static_cast<ActionItem *>(work->data);
323
324                 item = menu_item_add_stock(menu, action_item->label, action_item->icon_name, G_CALLBACK(toolbarlist_add_cb), toolbarlist);
325                 g_object_set_data(G_OBJECT(item), "toolbar_add_name", g_strdup(action_item->name));
326                 g_object_set_data(G_OBJECT(item), "toolbar_add_label", g_strdup(action_item->label));
327                 g_object_set_data(G_OBJECT(item), "toolbar_add_stock_id", g_strdup(action_item->icon_name));
328                 g_signal_connect(G_OBJECT(item), "destroy", G_CALLBACK(toolbar_button_free), item);
329
330                 work = work->next;
331                 }
332
333         action_items_free(list);
334
335         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
336 }
337
338 static gboolean toolbar_menu_add_cb(GtkWidget *widget, gpointer data)
339 {
340         auto toolbarlist = static_cast<ToolbarData *>(data);
341
342         toolbar_menu_add_popup(widget, toolbarlist);
343         return TRUE;
344 }
345
346 /**
347  * @brief For each layoutwindow, clear toolbar and reload with current selection
348  * @param bar Main or Status toolbar
349  *
350  */
351 void toolbar_apply(ToolbarType bar)
352 {
353         LayoutWindow *lw;
354         GList *work_windows;
355         GList *work_toolbar;
356
357         work_windows = layout_window_list;
358         while (work_windows)
359                 {
360                 lw = static_cast<LayoutWindow *>(work_windows->data);
361
362                 layout_toolbar_clear(lw, bar);
363
364                 work_toolbar = gtk_container_get_children(GTK_CONTAINER(toolbarlist[bar]->vbox));
365                 while (work_toolbar)
366                         {
367                         auto button = static_cast<GtkButton *>(work_toolbar->data);
368                         ToolbarButtonData *tbbd;
369
370                         tbbd = static_cast<ToolbarButtonData *>(g_object_get_data(G_OBJECT(button),"toolbarbuttondata"));
371                         layout_toolbar_add(lw, bar, tbbd->name);
372
373                         work_toolbar = work_toolbar->next;
374                         }
375                 g_list_free(work_toolbar);
376
377                 work_windows = work_windows->next;
378                 }
379
380 }
381
382 /**
383  * @brief Load the current toolbar items into the vbox
384  * @param lw
385  * @param box The vbox displayed in the preferences Toolbar tab
386  * @param bar Main or Status toolbar
387  *
388  * Get the current contents of the toolbar, both menu items
389  * and desktop items, and load them into the vbox
390  */
391 static void toolbarlist_populate(LayoutWindow *lw, GtkBox *box, ToolbarType bar)
392 {
393         GList *work = g_list_first(lw->toolbar_actions[bar]);
394
395         while (work)
396                 {
397                 auto name = static_cast<gchar *>(work->data);
398                 gchar *label;
399                 gchar *icon;
400                 work = work->next;
401
402                 if (file_extension_match(name, ".desktop"))
403                         {
404                         get_desktop_data(name, &label, &icon);
405                         }
406                 else
407                         {
408                         get_toolbar_item(name, &label, &icon);
409                         }
410
411                 if (g_strcmp0(name, "Separator") != 0)
412                         {
413                         toolbarlist_add_button(name, label, icon, box);
414                         }
415                 else
416                         {
417                         toolbarlist_add_button(name, name, "no-icon", box);
418                         }
419                 }
420 }
421
422 GtkWidget *toolbar_select_new(LayoutWindow *lw, ToolbarType bar)
423 {
424         GtkWidget *scrolled;
425         GtkWidget *tbar;
426         GtkWidget *add_box;
427
428         if (!lw) return nullptr;
429
430         if (!toolbarlist[bar])
431                 {
432                 toolbarlist[bar] = g_new0(ToolbarData, 1);
433                 }
434         toolbarlist[bar]->lw = lw;
435
436         toolbarlist[bar]->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
437         gtk_widget_show(toolbarlist[bar]->widget);
438
439         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
440         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
441                                                         GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
442         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_NONE);
443         gq_gtk_box_pack_start(GTK_BOX(toolbarlist[bar]->widget), scrolled, TRUE, TRUE, 0);
444         gtk_widget_show(scrolled);
445
446         toolbarlist[bar]->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
447         gtk_widget_show(toolbarlist[bar]->vbox);
448         gq_gtk_container_add(GTK_WIDGET(scrolled), toolbarlist[bar]->vbox);
449         gtk_viewport_set_shadow_type(GTK_VIEWPORT(gtk_bin_get_child(GTK_BIN(scrolled))),
450                                                                                                                                 GTK_SHADOW_NONE);
451
452         add_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
453         gtk_widget_show(add_box);
454         gq_gtk_box_pack_end(GTK_BOX(toolbarlist[bar]->widget), add_box, FALSE, FALSE, 0);
455         tbar = pref_toolbar_new(add_box);
456         toolbarlist[bar]->add_button = pref_toolbar_button(tbar, GQ_ICON_ADD, _("Add"), FALSE,
457                                                                                         _("Add Toolbar Item"),
458                                                                                         G_CALLBACK(toolbar_menu_add_cb), toolbarlist[bar]);
459         gtk_widget_show(toolbarlist[bar]->add_button);
460
461         toolbarlist_populate(lw,GTK_BOX(toolbarlist[bar]->vbox), bar);
462
463         return toolbarlist[bar]->widget;
464 }
465
466 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */