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