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