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