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