Convert GET_{LEFT,RIGHT}_PIXBUF_OFFSET macro to function
[geeqie.git] / src / ui-bookmark.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
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 "ui-bookmark.h"
23
24 #include <cstdlib>
25 #include <cstring>
26 #include <memory>
27
28 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <gdk/gdk.h>
30 #include <glib-object.h>
31 #include <pango/pango.h>
32
33 #include "compat.h"
34 #include "debug.h"
35 #include "history-list.h"
36 #include "intl.h"
37 #include "main-defines.h"
38 #include "misc.h"
39 #include "pixbuf-util.h"
40 #include "typedefs.h"
41 #include "ui-fileops.h"
42 #include "ui-menu.h"
43 #include "ui-misc.h"
44 #include "ui-tabcomp.h"
45 #include "ui-utildlg.h"
46 #include "uri-utils.h"
47
48 /*
49  *-----------------------------------------------------------------------------
50  * bookmarks
51  *-----------------------------------------------------------------------------
52  */
53
54 #define BOOKMARK_DATA_KEY "bookmarkdata"
55 #define MARKER_PATH "[path]"
56 #define MARKER_ICON "[icon]"
57
58 namespace
59 {
60
61 constexpr gint bookmark_drop_types_n = 3;
62 constexpr gint bookmark_drag_types_n = 2;
63
64 } // namespace
65
66 struct BookButtonData;
67
68 struct BookMarkData
69 {
70         GtkWidget *widget;
71         GtkWidget *box;
72         const gchar *key;
73
74         void (*select_func)(const gchar *path, gpointer data);
75         gpointer select_data;
76
77         gboolean no_defaults;
78         gboolean editable;
79         gboolean only_directories;
80
81         BookButtonData *active_button;
82 };
83
84 struct BookButtonData
85 {
86         GtkWidget *button;
87         GtkWidget *image;
88         GtkWidget *label;
89
90         gchar *key;
91         gchar *name;
92         gchar *path;
93         gchar *icon;
94         gchar *parent;
95 };
96
97 struct BookPropData
98 {
99         GtkWidget *name_entry;
100         GtkWidget *path_entry;
101         GtkWidget *icon_entry;
102
103         BookButtonData *bb;
104 };
105
106 enum {
107         TARGET_URI_LIST,
108         TARGET_X_URL,
109         TARGET_TEXT_PLAIN
110 };
111
112 static GtkTargetEntry bookmark_drop_types[] = {
113         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
114         { const_cast<gchar *>("x-url/http"),    0, TARGET_X_URL },
115         { const_cast<gchar *>("_NETSCAPE_URL"), 0, TARGET_X_URL }
116 };
117
118 static GtkTargetEntry bookmark_drag_types[] = {
119         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
120         { const_cast<gchar *>("text/plain"),    0, TARGET_TEXT_PLAIN }
121 };
122
123 static GList *bookmark_widget_list = nullptr;
124 static GList *bookmark_default_list = nullptr;
125
126
127 static void bookmark_populate_all(const gchar *key);
128
129
130 static BookButtonData *bookmark_from_string(const gchar *text)
131 {
132         BookButtonData *b;
133         const gchar *path_ptr;
134         const gchar *icon_ptr;
135
136         b = g_new0(BookButtonData, 1);
137
138         if (!text)
139                 {
140                 b->name = g_strdup(_("New Bookmark"));
141                 b->path = g_strdup(homedir());
142                 b->key = nullptr;
143                 return b;
144                 }
145
146         b->key = g_strdup(text);
147
148         path_ptr = strstr(text, MARKER_PATH);
149         icon_ptr = strstr(text, MARKER_ICON);
150
151         if (path_ptr && icon_ptr && icon_ptr < path_ptr)
152                 {
153                 log_printf("warning, bookmark icon must be after path\n");
154                 return nullptr;
155                 }
156
157         if (path_ptr)
158                 {
159                 gint l;
160
161                 l = path_ptr - text;
162                 b->name = g_strndup(text, l);
163                 path_ptr += strlen(MARKER_PATH);
164                 if (icon_ptr)
165                         {
166                         l = icon_ptr - path_ptr;
167                         b->path = g_strndup(path_ptr, l);
168                         }
169                 else
170                         {
171                         b->path = g_strdup(path_ptr);
172                         }
173                 }
174         else
175                 {
176                 b->name = g_strdup(text);
177                 b->path = g_strdup("");
178                 }
179
180         if (icon_ptr)
181                 {
182                 icon_ptr += strlen(MARKER_ICON);
183                 b->icon = g_strdup(icon_ptr);
184                 }
185
186         return b;
187 }
188
189 static void bookmark_free(BookButtonData *b)
190 {
191         if (!b) return;
192
193         g_free(b->name);
194         g_free(b->path);
195         g_free(b->icon);
196         g_free(b->key);
197         g_free(b->parent);
198         g_free(b);
199 }
200
201 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
202 {
203         if (!name) name = _("New Bookmark");
204
205         if (icon)
206                 {
207                 return g_strdup_printf("%s" MARKER_PATH "%s" MARKER_ICON "%s", name, path, icon);
208                 }
209
210         return g_strdup_printf("%s" MARKER_PATH "%s", name, path);
211 }
212
213 static void bookmark_select_cb(GtkWidget *button, gpointer data)
214 {
215         auto bm = static_cast<BookMarkData *>(data);
216         BookButtonData *b;
217
218         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
219         if (!b) return;
220
221         if (bm->select_func) bm->select_func(b->path, bm->select_data);
222 }
223
224 static void bookmark_edit_destroy_cb(GtkWidget *, gpointer data)
225 {
226         auto p = static_cast<BookPropData *>(data);
227
228         bookmark_free(p->bb);
229         g_free(p);
230 }
231
232 static void bookmark_edit_cancel_cb(GenericDialog *, gpointer)
233 {
234 }
235
236 static void bookmark_edit_ok_cb(GenericDialog *, gpointer data)
237 {
238         auto p = static_cast<BookPropData *>(data);
239         const gchar *name;
240         gchar *path;
241         const gchar *icon;
242         gchar *new_string;
243
244         name = gq_gtk_entry_get_text(GTK_ENTRY(p->name_entry));
245         path = remove_trailing_slash(gq_gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
246         icon = gq_gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
247
248         new_string = bookmark_string(name, path, icon);
249
250         if (p->bb->key)
251                 {
252                 history_list_item_change(p->bb->parent, p->bb->key, new_string);
253                 }
254         else
255                 {
256                 history_list_add_to_key(p->bb->parent, new_string, 0);
257                 }
258
259         if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
260         if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
261
262         g_free(path);
263         g_free(new_string);
264
265         bookmark_populate_all(p->bb->parent);
266 }
267
268 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
269
270 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
271 {
272         BookPropData *p;
273         GenericDialog *gd;
274         GtkWidget *table;
275         GtkWidget *label;
276         const gchar *icon;
277
278         if (!key) key = "bookmarks";
279
280         p = g_new0(BookPropData, 1);
281
282         p->bb = bookmark_from_string(text);
283         p->bb->parent = g_strdup(key);
284
285         gd = generic_dialog_new(_("Edit Bookmark"), "bookmark_edit",
286                                 parent, TRUE,
287                                 bookmark_edit_cancel_cb, p);
288         g_signal_connect(G_OBJECT(gd->dialog), "destroy",
289                          G_CALLBACK(bookmark_edit_destroy_cb), p);
290
291         generic_dialog_add_message(gd, nullptr, _("Edit Bookmark"), nullptr, FALSE);
292
293         generic_dialog_add_button(gd, GQ_ICON_OK, "OK",
294                                   bookmark_edit_ok_cb, TRUE);
295
296         table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
297         pref_table_label(table, 0, 0, _("Name:"), GTK_ALIGN_END);
298
299         p->name_entry = gtk_entry_new();
300         gtk_widget_set_size_request(p->name_entry, 300, -1);
301         if (p->bb->name) gq_gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
302         gq_gtk_grid_attach_default(GTK_GRID(table), p->name_entry, 1, 2, 0, 1);
303         generic_dialog_attach_default(gd, p->name_entry);
304         gtk_widget_show(p->name_entry);
305
306         pref_table_label(table, 0, 1, _("Path:"), GTK_ALIGN_END);
307
308         label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
309                                                 "bookmark_path", -1, nullptr, nullptr);
310         tab_completion_add_select_button(p->path_entry, nullptr, TRUE);
311         gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 1, 2);
312         generic_dialog_attach_default(gd, p->path_entry);
313         gtk_widget_show(label);
314
315         pref_table_label(table, 0, 2, _("Icon:"), GTK_ALIGN_END);
316
317         icon = p->bb->icon;
318         if (!icon) icon = "";
319         label = tab_completion_new_with_history(&p->icon_entry, icon,
320                                                 "bookmark_icons", -1, nullptr, nullptr);
321         tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
322         gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 2, 3);
323         generic_dialog_attach_default(gd, p->icon_entry);
324         gtk_widget_show(label);
325
326         gtk_widget_show(gd->dialog);
327 }
328
329 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
330 {
331         BookButtonData *b;
332         gint p;
333         GList *list;
334         const gchar *key_holder;
335
336         if (!bm->editable) return;
337
338         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
339         if (!b) return;
340
341         list = gtk_container_get_children(GTK_CONTAINER(bm->box));
342         p = g_list_index(list, button);
343         g_list_free(list);
344
345         if (p < 0 || p + direction < 0) return;
346
347         key_holder = bm->key;
348         bm->key = "_TEMPHOLDER";
349         history_list_item_move(key_holder, b->key, -direction);
350         bookmark_populate_all(key_holder);
351         bm->key = key_holder;
352
353         gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
354 }
355
356 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
357 {
358         auto bm = static_cast<BookMarkData *>(data);
359
360         if (!bm->active_button) return;
361
362         bookmark_edit(bm->key, bm->active_button->key, widget);
363 }
364
365 static void bookmark_menu_move(BookMarkData *bm, gint direction)
366 {
367         if (!bm->active_button) return;
368
369         bookmark_move(bm, bm->active_button->button, direction);
370 }
371
372 static void bookmark_menu_up_cb(GtkWidget *, gpointer data)
373 {
374         bookmark_menu_move(static_cast<BookMarkData *>(data), -1);
375 }
376
377 static void bookmark_menu_down_cb(GtkWidget *, gpointer data)
378 {
379         bookmark_menu_move(static_cast<BookMarkData *>(data), 1);
380 }
381
382 static void bookmark_menu_remove_cb(GtkWidget *, gpointer data)
383 {
384         auto bm = static_cast<BookMarkData *>(data);
385
386         if (!bm->active_button) return;
387
388         history_list_item_remove(bm->key, bm->active_button->key);
389         bookmark_populate_all(bm->key);
390 }
391
392 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button, gint, guint32, gboolean local)
393 {
394         GtkWidget *menu;
395         BookButtonData *b;
396
397         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
398         if (!b) return;
399
400         bm->active_button = b;
401
402         menu = popup_menu_short_lived();
403         menu_item_add_icon_sensitive(menu, _("_Properties..."), PIXBUF_INLINE_ICON_PROPERTIES, bm->editable,
404                       G_CALLBACK(bookmark_menu_prop_cb), bm);
405         menu_item_add_icon_sensitive(menu, _("Move _up"), GQ_ICON_GO_UP, bm->editable,
406                       G_CALLBACK(bookmark_menu_up_cb), bm);
407         menu_item_add_icon_sensitive(menu, _("Move _down"), GQ_ICON_GO_DOWN, bm->editable,
408                       G_CALLBACK(bookmark_menu_down_cb), bm);
409         menu_item_add_icon_sensitive(menu, _("_Remove"), GQ_ICON_REMOVE, bm->editable,
410                       G_CALLBACK(bookmark_menu_remove_cb), bm);
411
412         if (local)
413                 {
414                 gtk_menu_popup_at_widget(GTK_MENU(menu), button, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_CENTER, nullptr);
415                 }
416         else
417                 {
418                 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
419                 }
420 }
421
422 static gboolean bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
423 {
424         auto bm = static_cast<BookMarkData *>(data);
425
426         if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
427
428         bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
429
430         return TRUE;
431 }
432
433 static gboolean bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
434 {
435         auto bm = static_cast<BookMarkData *>(data);
436
437         switch (event->keyval)
438                 {
439                 case GDK_KEY_F10:
440                         if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
441                         /* fall through */
442                 case GDK_KEY_Menu:
443                         bookmark_menu_popup(bm, button, 0, event->time, TRUE);
444                         return TRUE;
445                         break;
446                 case GDK_KEY_Up:
447                         if (event->state & GDK_SHIFT_MASK)
448                                 {
449                                 bookmark_move(bm, button, -1);
450                                 return TRUE;
451                                 }
452                         break;
453                 case GDK_KEY_Down:
454                         if (event->state & GDK_SHIFT_MASK)
455                                 {
456                                 bookmark_move(bm, button, 1);
457                                 return TRUE;
458                                 }
459                         break;
460                 }
461
462         return FALSE;
463 }
464
465 static void bookmark_drag_set_data(GtkWidget *button,
466                                    GdkDragContext *context, GtkSelectionData *selection_data,
467                                    guint, guint, gpointer data)
468 {
469         auto bm = static_cast<BookMarkData *>(data);
470         BookButtonData *b;
471         GList *list = nullptr;
472
473         return;
474         if (gdk_drag_context_get_dest_window(context) == gtk_widget_get_window(bm->widget)) return;
475
476         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
477         if (!b) return;
478
479         list = g_list_append(list, b->path);
480
481         gchar **uris = uris_from_pathlist(list);
482         gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
483         if (!ret)
484                 {
485                 char *str = g_strjoinv("\r\n", uris);
486                 ret = gtk_selection_data_set_text(selection_data, str, -1);
487                 g_free(str);
488                 }
489
490         g_strfreev(uris);
491         g_list_free(list);
492 }
493
494 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer)
495 {
496         GdkPixbuf *pixbuf;
497         GdkModifierType mask;
498         gint x;
499         gint y;
500         GtkAllocation allocation;
501         GdkSeat *seat;
502         GdkDevice *device;
503
504         gtk_widget_get_allocation(button, &allocation);
505
506         pixbuf = gdk_pixbuf_get_from_window(gtk_widget_get_window(button),
507                                             allocation.x, allocation.y,
508                                             allocation.width, allocation.height);
509         seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(button)));
510         device = gdk_seat_get_pointer(seat);
511         gdk_window_get_device_position(gtk_widget_get_window(button), device, &x, &y, &mask);
512
513         gtk_drag_set_icon_pixbuf(context, pixbuf,
514                                  x - allocation.x, y - allocation.y);
515         g_object_unref(pixbuf);
516 }
517
518 static gboolean bookmark_path_tooltip_cb(GtkWidget *button, gpointer)
519 {
520         BookButtonData *b;
521
522         b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
523         gtk_widget_set_tooltip_text(GTK_WIDGET(button), b->path);
524
525         return FALSE;
526 }
527
528 static void bookmark_populate(BookMarkData *bm)
529 {
530         GtkBox *box;
531         GList *work;
532         GList *children;
533
534         box = GTK_BOX(bm->box);
535         children = gtk_container_get_children(GTK_CONTAINER(box));
536         work = children;
537         while (work)
538                 {
539                 GtkWidget *widget = GTK_WIDGET(work->data);
540                 work = work->next;
541                 gq_gtk_widget_destroy(widget);
542                 }
543
544         if (!bm->no_defaults && !history_list_get_by_key(bm->key))
545                 {
546                 gchar *buf;
547                 gchar *path;
548
549                 if (!bookmark_default_list)
550                         {
551                         buf = bookmark_string(_("Home"), homedir(), nullptr);
552                         history_list_add_to_key(bm->key, buf, 0);
553                         g_free(buf);
554
555                         if (g_strcmp0(bm->key, "shortcuts") != 0)
556                                 {
557                                 buf = bookmark_string(".", g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
558                                 history_list_add_to_key(bm->key, buf, 0);
559                                 g_free(buf);
560                                 }
561
562                         path = g_build_filename(homedir(), "Desktop", NULL);
563                         if (isname(path))
564                                 {
565                                 buf = bookmark_string(_("Desktop"), path, nullptr);
566                                 history_list_add_to_key(bm->key, buf, 0);
567                                 g_free(buf);
568                                 }
569                         g_free(path);
570                         }
571
572                 work = bookmark_default_list;
573                 while (work && work->next)
574                         {
575                         gchar *name;
576
577                         name = static_cast<gchar *>(work->data);
578                         work = work->next;
579                         path = static_cast<gchar *>(work->data);
580                         work = work->next;
581
582                         if (strcmp(name, ".") == 0)
583                                 {
584                                 if (g_strcmp0(bm->key, "shortcuts") != 0)
585                                         {
586                                         buf = bookmark_string(name, g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
587                                         }
588                                 else
589                                         {
590                                         continue;
591                                         }
592                                 }
593                         else
594                                 {
595                                 buf = bookmark_string(name, path, nullptr);
596                                 }
597                         history_list_add_to_key(bm->key, buf, 0);
598                         g_free(buf);
599                         }
600                 }
601
602         work = history_list_get_by_key(bm->key);
603         work = g_list_last(work);
604         while (work)
605                 {
606                 BookButtonData *b;
607
608                 b = bookmark_from_string(static_cast<const gchar *>(work->data));
609                 if (b)
610                         {
611                         if (strcmp(b->name, ".") == 0)
612                                 {
613                                 gchar *buf;
614
615                                 b->path = g_strdup(history_list_find_last_path_by_key("path_list"));
616                                 buf = bookmark_string(".", b->path, b->icon);
617                                 history_list_item_change("bookmarks", b->key, buf);
618                                 b->key = g_strdup(buf);
619                                 g_free(buf);
620                                 }
621                         GtkWidget *box;
622
623                         b->button = gtk_button_new();
624                         gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
625                         gq_gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
626                         gtk_widget_show(b->button);
627
628                         g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
629                                                b, reinterpret_cast<GDestroyNotify>(bookmark_free));
630
631                         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
632                         gq_gtk_container_add(GTK_WIDGET(b->button), box);
633                         gtk_widget_show(box);
634
635                         if (b->icon)
636                                 {
637                                 GdkPixbuf *pixbuf = nullptr;
638                                 gchar *iconl;
639
640                                 iconl = path_from_utf8(b->icon);
641                                 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
642
643                                 if (isfile(b->icon))
644                                         {
645                                         pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
646                                         }
647                                 else
648                                         {
649                                         gint w;
650                                         gint h;
651
652                                         w = h = 16;
653                                         gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
654
655                                         pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), b->icon, w, GTK_ICON_LOOKUP_NO_SVG, nullptr);
656                                         }
657
658                                 g_free(iconl);
659                                 if (pixbuf)
660                                         {
661                                         GdkPixbuf *scaled;
662                                         gint w;
663                                         gint h;
664
665                                         w = h = 16;
666                                         gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
667
668                                         scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
669                                                                          GDK_INTERP_BILINEAR);
670                                         b->image = gtk_image_new_from_pixbuf(scaled);
671                                         g_object_unref(scaled);
672                                         g_object_unref(pixbuf);
673                                         }
674                                 else
675                                         {
676                                         b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
677                                         }
678                                 }
679                         else
680                                 {
681                                 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
682                                 }
683                         gq_gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
684                         gtk_widget_show(b->image);
685
686                         b->label = gtk_label_new(b->name);
687                         gq_gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
688                         gtk_widget_show(b->label);
689
690                         g_signal_connect(G_OBJECT(b->button), "clicked",
691                                          G_CALLBACK(bookmark_select_cb), bm);
692                         g_signal_connect(G_OBJECT(b->button), "button_press_event",
693                                          G_CALLBACK(bookmark_press_cb), bm);
694                         g_signal_connect(G_OBJECT(b->button), "key_press_event",
695                                          G_CALLBACK(bookmark_keypress_cb), bm);
696
697                         gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
698                                             bookmark_drag_types, bookmark_drag_types_n,
699                                             static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
700                         g_signal_connect(G_OBJECT(b->button), "drag_data_get",
701                                          G_CALLBACK(bookmark_drag_set_data), bm);
702                         g_signal_connect(G_OBJECT(b->button), "drag_begin",
703                                          G_CALLBACK(bookmark_drag_begin), bm);
704
705                         gtk_widget_set_has_tooltip(GTK_WIDGET(b->button), TRUE);
706                         g_signal_connect(G_OBJECT(b->button), "query_tooltip", G_CALLBACK(bookmark_path_tooltip_cb), bm);
707                         }
708
709                 work = work->prev;
710                 }
711 }
712
713 static void bookmark_populate_all(const gchar *key)
714 {
715         GList *work;
716
717         if (!key) return;
718
719         work = bookmark_widget_list;
720         while (work)
721                 {
722                 BookMarkData *bm;
723
724                 bm = static_cast<BookMarkData *>(work->data);
725                 work = work->next;
726
727                 if (strcmp(bm->key, key) == 0)
728                         {
729                         bookmark_populate(bm);
730                         }
731                 }
732 }
733
734 static void bookmark_dnd_get_data(GtkWidget *, GdkDragContext *,
735                                   gint, gint,
736                                   GtkSelectionData *selection_data, guint,
737                                   guint, gpointer data)
738 {
739         auto bm = static_cast<BookMarkData *>(data);
740         GList *list = nullptr;
741         GList *errors = nullptr;
742         GList *work;
743         gchar *real_path;
744         gchar **uris;
745
746         if (!bm->editable) return;
747
748         uris = gtk_selection_data_get_uris(selection_data);
749         if (uris)
750                 {
751                 list = uri_pathlist_from_uris(uris, &errors);
752                 if(errors)
753                         {
754                         warning_dialog_dnd_uri_error(errors);
755                         g_list_free_full(errors, g_free);
756                         }
757                 g_strfreev(uris);
758
759                 work = list;
760                 while (work)
761                         {
762                         auto path = static_cast<gchar *>(work->data);
763                         gchar *buf;
764
765                         work = work->next;
766
767                         if (bm->only_directories && !isdir(path)) continue;
768
769                         real_path = realpath(path, nullptr);
770
771                         if (strstr(real_path, get_collections_dir()) && isfile(path))
772                                 {
773                                 buf = bookmark_string(filename_from_path(path), path, "gq-icon-collection");
774                                 }
775                         else if (isfile(path))
776                                 {
777                                 buf = bookmark_string(filename_from_path(path), path, GQ_ICON_FILE);
778                                 }
779                         else
780                                 {
781                                 buf = bookmark_string(filename_from_path(path), path, nullptr);
782                                 }
783                         history_list_add_to_key(bm->key, buf, 0);
784                         g_free(buf);
785                         g_free(real_path);
786                         }
787
788                 g_list_free_full(list, g_free);
789
790                 bookmark_populate_all(bm->key);
791                 }
792 }
793
794 static void bookmark_list_destroy(GtkWidget *, gpointer data)
795 {
796         auto bm = static_cast<BookMarkData *>(data);
797
798         bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
799
800         g_free(const_cast<gchar *>(bm->key));
801         g_free(bm);
802 }
803
804 GtkWidget *bookmark_list_new(const gchar *key,
805                              void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
806 {
807         GtkWidget *scrolled;
808         BookMarkData *bm;
809
810         if (!key) key = "bookmarks";
811
812         bm = g_new0(BookMarkData, 1);
813         bm->key = g_strdup(key);
814
815         bm->select_func = select_func;
816         bm->select_data = select_data;
817
818         bm->no_defaults = FALSE;
819         bm->editable = TRUE;
820         bm->only_directories = FALSE;
821
822         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
823
824         PangoLayout *layout;
825         gint width;
826         gint height;
827
828         layout = gtk_widget_create_pango_layout(GTK_WIDGET(scrolled), "reasonable width");
829         pango_layout_get_pixel_size(layout, &width, &height);
830         gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scrolled), width);
831         g_object_unref(layout);
832
833         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
834
835         bm->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
836         gq_gtk_container_add(GTK_WIDGET(scrolled), bm->box);
837         gtk_widget_show(bm->box);
838
839         bookmark_populate(bm);
840
841         g_signal_connect(G_OBJECT(bm->box), "destroy",
842                          G_CALLBACK(bookmark_list_destroy), bm);
843         g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
844         g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
845         bm->widget = scrolled;
846
847         gtk_drag_dest_set(scrolled,
848                           static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
849                           bookmark_drop_types, bookmark_drop_types_n,
850                           static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
851         g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
852                          G_CALLBACK(bookmark_dnd_get_data), bm);
853
854         bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
855
856         return scrolled;
857 }
858
859 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
860 {
861         BookMarkData *bm;
862
863         if (!list || !key) return;
864
865         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
866         if (!bm) return;
867
868         if (bm->key && strcmp(bm->key, key) == 0) return;
869
870         g_free(const_cast<gchar *>(bm->key));
871         bm->key = g_strdup(key);
872
873         bookmark_populate(bm);
874 }
875
876 void bookmark_list_set_no_defaults(GtkWidget *list, gboolean no_defaults)
877 {
878         BookMarkData *bm;
879
880         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
881         if (!bm) return;
882
883         bm->no_defaults = no_defaults;
884 }
885
886 void bookmark_list_set_editable(GtkWidget *list, gboolean editable)
887 {
888         BookMarkData *bm;
889
890         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
891         if (!bm) return;
892
893         bm->editable = editable;
894 }
895
896 void bookmark_list_set_only_directories(GtkWidget *list, gboolean only_directories)
897 {
898         BookMarkData *bm;
899
900         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
901         if (!bm) return;
902
903         bm->only_directories = only_directories;
904 }
905
906 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
907 {
908         BookMarkData *bm;
909         gchar *real_path;
910
911         bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
912         if (!bm) return;
913
914         std::unique_ptr<gchar, decltype(&g_free)> buf(bookmark_string(name, path, nullptr), g_free);
915         real_path = realpath(path, nullptr);
916
917         if (strstr(real_path, get_collections_dir()) && isfile(path))
918                 {
919                 buf.reset(bookmark_string(name, path, "gq-icon-collection"));
920                 }
921         else
922                 {
923                 if (isfile(path))
924                         {
925                         buf.reset(bookmark_string(name, path, GQ_ICON_FILE));
926                         }
927                 else
928                         {
929                         buf.reset(bookmark_string(name, path, nullptr));
930                         }
931                 }
932
933         history_list_add_to_key(bm->key, buf.get(), 0);
934         g_free(real_path);
935
936         bookmark_populate_all(bm->key);
937 }
938
939 void bookmark_add_default(const gchar *name, const gchar *path)
940 {
941         if (!name || !path) return;
942         bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
943         bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
944 }
945
946 /*
947  *-----------------------------------------------------------------------------
948  * combo with history key
949  *-----------------------------------------------------------------------------
950  */
951
952 struct HistoryComboData
953 {
954         GtkWidget *combo;
955         GtkWidget *entry;
956         gchar *history_key;
957         gint history_levels;
958 };
959
960 static void history_combo_destroy(GtkWidget *, gpointer data)
961 {
962         auto hc = static_cast<HistoryComboData *>(data);
963
964         g_free(hc->history_key);
965         g_free(data);
966 }
967
968 /* if text is NULL, entry is set to the most recent item */
969 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
970                              const gchar *history_key, gint max_levels)
971 {
972         HistoryComboData *hc;
973         GList *work;
974         gint n = 0;
975
976         hc = g_new0(HistoryComboData, 1);
977         hc->history_key = g_strdup(history_key);
978         hc->history_levels = max_levels;
979
980         hc->combo = gtk_combo_box_text_new_with_entry();
981
982         hc->entry = gtk_bin_get_child(GTK_BIN(hc->combo));
983
984         g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
985         g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
986         g_signal_connect(G_OBJECT(hc->combo), "destroy",
987                          G_CALLBACK(history_combo_destroy), hc);
988
989         work = history_list_get_by_key(hc->history_key);
990         while (work)
991                 {
992                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
993                 work = work->next;
994                 n++;
995                 }
996
997         if (text)
998                 {
999                 gq_gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1000                 }
1001         else if (n > 0)
1002                 {
1003                 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1004                 }
1005
1006         if (entry) *entry = hc->entry;
1007         return hc->combo;
1008 }
1009
1010 /* if text is NULL, current entry text is used
1011  * widget can be the combo or entry widget
1012  */
1013 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1014 {
1015         HistoryComboData *hc;
1016         gchar *new_text;
1017
1018         hc = static_cast<HistoryComboData *>(g_object_get_data(G_OBJECT(widget), "history_combo_data"));
1019         if (!hc)
1020                 {
1021                 log_printf("widget is not a history combo\n");
1022                 return;
1023                 }
1024
1025         if (text)
1026                 {
1027                 new_text = g_strdup(text);
1028                 }
1029         else
1030                 {
1031                 new_text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1032                 }
1033
1034         if (new_text && strlen(new_text) > 0)
1035                 {
1036                 GtkTreeModel *store;
1037                 GList *work;
1038
1039                 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1040
1041                 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1042
1043                 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1044                 gtk_list_store_clear(GTK_LIST_STORE(store));
1045
1046                 work = history_list_get_by_key(hc->history_key);
1047                 while (work)
1048                         {
1049                         gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
1050                         work = work->next;
1051                         }
1052                 }
1053
1054         g_free(new_text);
1055 }
1056 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */