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