2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 The Geeqie Team
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!
24 #include <gdk/gdkkeysyms.h> /* for key values */
29 #include "secure_save.h"
30 #include "ui_bookmark.h"
31 #include "ui_fileops.h"
34 #include "ui_utildlg.h"
35 #include "ui_tabcomp.h"
39 *-----------------------------------------------------------------------------
41 *-----------------------------------------------------------------------------
44 #define HISTORY_DEFAULT_KEY_COUNT 16
47 typedef struct _HistoryData HistoryData;
54 static GList *history_list = NULL;
57 static gchar *quoted_from_text(const gchar *text)
61 gint l = strlen(text);
63 if (l == 0) return NULL;
65 while (c < l && text[c] !='"') c++;
72 while (e < l && text[e] !='"') e++;
77 return g_strndup(ptr, e - c);
84 gint history_list_load(const gchar *path)
91 pathl = path_from_utf8(path);
92 f = fopen(pathl, "r");
96 /* first line must start with History comment */
97 if (!fgets(s_buf, sizeof(s_buf), f) ||
98 strncmp(s_buf, "#History", 8) != 0)
104 while (fgets(s_buf, sizeof(s_buf), f))
106 if (s_buf[0]=='#') continue;
114 while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
117 key = g_strndup(ptr, c);
123 value = quoted_from_text(s_buf);
126 history_list_add_to_key(key, value, 0);
139 gint history_list_save(const gchar *path)
145 pathl = path_from_utf8(path);
146 ssi = secure_open(pathl);
150 printf_term(_("Unable to write history lists to: %s\n"), path);
154 secure_fprintf(ssi, "#History lists\n\n");
156 list = g_list_last(history_list);
157 while (list && secsave_errno == SS_ERR_NONE)
165 secure_fprintf(ssi, "[%s]\n", hd->key);
167 /* save them inverted (oldest to newest)
168 * so that when reading they are added correctly
170 work = g_list_last(hd->list);
171 while (work && secsave_errno == SS_ERR_NONE)
173 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
176 secure_fputc(ssi, '\n');
179 secure_fprintf(ssi, "#end\n");
181 return (secure_close(ssi) == 0);
184 static void history_list_free(HistoryData *hd)
201 static HistoryData *history_list_find_by_key(const gchar* key)
203 GList *work = history_list;
205 if (!key) return NULL;
209 HistoryData *hd = work->data;
210 if (strcmp(hd->key, key) == 0) return hd;
216 const gchar *history_list_find_last_path_by_key(const gchar* key)
220 hd = history_list_find_by_key(key);
221 if (!hd || !hd->list) return NULL;
223 return hd->list->data;
226 void history_list_free_key(const gchar *key)
229 hd = history_list_find_by_key(key);
232 history_list = g_list_remove(history_list, hd);
233 history_list_free(hd);
236 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
241 if (!key || !path) return;
243 hd = history_list_find_by_key(key);
246 hd = g_new(HistoryData, 1);
247 hd->key = g_strdup(key);
249 history_list = g_list_prepend(history_list, hd);
252 /* if already in the list, simply move it to the top */
256 gchar *buf = work->data;
258 if (strcmp(buf, path) == 0)
260 hd->list = g_list_remove(hd->list, buf);
261 hd->list = g_list_prepend(hd->list, buf);
266 hd->list = g_list_prepend(hd->list, g_strdup(path));
268 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
271 while (hd->list && g_list_length(hd->list) > max)
273 GList *work = g_list_last(hd->list);
274 gchar *buf = work->data;
275 hd->list = g_list_remove(hd->list, buf);
281 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
286 if (!oldpath) return;
287 hd = history_list_find_by_key(key);
293 gchar *buf = work->data;
294 if (strcmp(buf, oldpath) == 0)
298 work->data = g_strdup(newpath);
302 hd->list = g_list_remove(hd->list, buf);
311 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
318 hd = history_list_find_by_key(key);
324 gchar *buf = work->data;
325 if (strcmp(buf, path) == 0)
329 hd->list = g_list_remove(hd->list, buf);
330 hd->list = g_list_insert(hd->list, buf, p);
338 void history_list_item_remove(const gchar *key, const gchar *path)
340 history_list_item_change(key, path, NULL);
343 GList *history_list_get_by_key(const gchar *key)
347 hd = history_list_find_by_key(key);
348 if (!hd) return NULL;
354 *-----------------------------------------------------------------------------
356 *-----------------------------------------------------------------------------
359 #define BOOKMARK_DATA_KEY "bookmarkdata"
360 #define MARKER_PATH "[path]"
361 #define MARKER_ICON "[icon]"
363 typedef struct _BookMarkData BookMarkData;
364 typedef struct _BookButtonData BookButtonData;
365 typedef struct _BookPropData BookPropData;
373 void (*select_func)(const gchar *path, gpointer data);
374 gpointer select_data;
378 gint only_directories;
380 BookButtonData *active_button;
383 struct _BookButtonData
398 GtkWidget *name_entry;
399 GtkWidget *path_entry;
400 GtkWidget *icon_entry;
411 static GtkTargetEntry bookmark_drop_types[] = {
412 { "text/uri-list", 0, TARGET_URI_LIST },
413 { "x-url/http", 0, TARGET_X_URL },
414 { "_NETSCAPE_URL", 0, TARGET_X_URL }
416 #define bookmark_drop_types_n 3
418 static GtkTargetEntry bookmark_drag_types[] = {
419 { "text/uri-list", 0, TARGET_URI_LIST },
420 { "text/plain", 0, TARGET_TEXT_PLAIN }
422 #define bookmark_drag_types_n 2
425 static GList *bookmark_widget_list = NULL;
426 static GList *bookmark_default_list = NULL;
429 static void bookmark_populate_all(const gchar *key);
432 static BookButtonData *bookmark_from_string(const gchar *text)
435 const gchar *path_ptr;
436 const gchar *icon_ptr;
438 b = g_new0(BookButtonData, 1);
442 b->name = g_strdup(_("New Bookmark"));
443 b->path = g_strdup(homedir());
448 b->key = g_strdup(text);
450 path_ptr = strstr(text, MARKER_PATH);
451 icon_ptr = strstr(text, MARKER_ICON);
453 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
455 printf("warning, bookmark icon must be after path\n");
464 b->name = g_strndup(text, l);
465 path_ptr += strlen(MARKER_PATH);
468 l = icon_ptr - path_ptr;
469 b->path = g_strndup(path_ptr, l);
473 b->path = g_strdup(path_ptr);
478 b->name = g_strdup(text);
479 b->path = g_strdup("");
484 icon_ptr += strlen(MARKER_ICON);
485 b->icon = g_strdup(icon_ptr);
491 static void bookmark_free(BookButtonData *b)
503 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
505 if (!name) name = _("New Bookmark");
506 if (icon && strncmp(icon, "/", 1) != 0) icon = NULL;
510 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
513 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
516 static void bookmark_select_cb(GtkWidget *button, gpointer data)
518 BookMarkData *bm = data;
521 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
524 if (bm->select_func) bm->select_func(b->path, bm->select_data);
527 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
529 BookPropData *p = data;
531 bookmark_free(p->bb);
535 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
539 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
541 BookPropData *p = data;
547 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
548 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
549 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
551 new = bookmark_string(name, path, icon);
555 history_list_item_change(p->bb->parent, p->bb->key, new);
559 history_list_add_to_key(p->bb->parent, new, 0);
562 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
563 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
568 bookmark_populate_all(p->bb->parent);
571 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
573 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
581 if (!key) key = "bookmarks";
583 p = g_new0(BookPropData, 1);
585 p->bb = bookmark_from_string(text);
586 p->bb->parent = g_strdup(key);
588 gd = generic_dialog_new(_("Edit Bookmark"), GQ_WMCLASS, "bookmark_edit",
590 bookmark_edit_cancel_cb, p);
591 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
592 G_CALLBACK(bookmark_edit_destroy_cb), p);
594 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
596 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
597 bookmark_edit_ok_cb, TRUE);
599 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
600 pref_table_label(table, 0, 0, _("Name:"), 1.0);
602 p->name_entry = gtk_entry_new();
603 gtk_widget_set_size_request(p->name_entry, 300, -1);
604 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
605 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
606 generic_dialog_attach_default(gd, p->name_entry);
607 gtk_widget_show(p->name_entry);
609 pref_table_label(table, 0, 1, _("Path:"), 1.0);
611 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
612 "bookmark_path", -1, NULL, NULL);
613 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
614 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
615 generic_dialog_attach_default(gd, p->path_entry);
616 gtk_widget_show(label);
618 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
621 if (!icon) icon = "";
622 label = tab_completion_new_with_history(&p->icon_entry, icon,
623 "bookmark_icons", -1, NULL, NULL);
624 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
625 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
626 generic_dialog_attach_default(gd, p->icon_entry);
627 gtk_widget_show(label);
629 gtk_widget_show(gd->dialog);
632 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
639 if (!bm->editable) return;
641 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
644 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
645 p = g_list_index(list, button);
648 if (p < 0 || p + direction < 0) return;
650 key_holder = bm->key;
651 bm->key = "_TEMPHOLDER";
652 history_list_item_move(key_holder, b->key, -direction);
653 bookmark_populate_all(key_holder);
654 bm->key = key_holder;
656 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
659 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
661 BookMarkData *bm = data;
663 if (!bm->active_button) return;
665 bookmark_edit(bm->key, bm->active_button->key, widget);
668 static void bookmark_menu_move(BookMarkData *bm, gint direction)
670 if (!bm->active_button) return;
672 bookmark_move(bm, bm->active_button->button, direction);
675 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
677 bookmark_menu_move(data, -1);
680 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
682 bookmark_menu_move(data, 1);
685 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
687 BookMarkData *bm = data;
689 if (!bm->active_button) return;
691 history_list_item_remove(bm->key, bm->active_button->key);
692 bookmark_populate_all(bm->key);
695 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
697 GtkWidget *button = data;
699 gdk_window_get_origin(button->window, x, y);
700 *y += button->allocation.y + button->allocation.height;
703 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
704 gint button_n, guint32 time, gint local)
709 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
712 bm->active_button = b;
714 menu = popup_menu_short_lived();
715 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
716 G_CALLBACK(bookmark_menu_prop_cb), bm);
717 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
718 G_CALLBACK(bookmark_menu_up_cb), bm);
719 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
720 G_CALLBACK(bookmark_menu_down_cb), bm);
721 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
722 G_CALLBACK(bookmark_menu_remove_cb), bm);
726 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
727 bookmark_menu_position_cb, button, button_n, time);
731 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
735 static gint bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
737 BookMarkData *bm = data;
739 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
741 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
746 static gint bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
748 BookMarkData *bm = data;
750 switch (event->keyval)
753 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
755 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
759 if (event->state & GDK_SHIFT_MASK)
761 bookmark_move(bm, button, -1);
766 if (event->state & GDK_SHIFT_MASK)
768 bookmark_move(bm, button, 1);
777 static void bookmark_drag_set_data(GtkWidget *button,
778 GdkDragContext *context, GtkSelectionData *selection_data,
779 guint info, guint time, gpointer data)
781 BookMarkData *bm = data;
783 gchar *uri_text = NULL;
787 if (context->dest_window == bm->widget->window) return;
789 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
792 list = g_list_append(list, b->path);
796 case TARGET_URI_LIST:
797 uri_text = uri_text_from_list(list, &length, FALSE);
799 case TARGET_TEXT_PLAIN:
800 uri_text = uri_text_from_list(list, &length, TRUE);
806 if (!uri_text) return;
808 gtk_selection_data_set(selection_data, selection_data->target,
809 8, (guchar *)uri_text, length);
813 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
816 GdkModifierType mask;
819 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
820 button->allocation.width, button->allocation.height);
821 gdk_pixbuf_get_from_drawable(pixbuf, button->window, NULL,
822 button->allocation.x, button->allocation.y,
823 0, 0, button->allocation.width, button->allocation.height);
825 gdk_window_get_pointer(button->window, &x, &y, &mask);
827 gtk_drag_set_icon_pixbuf(context, pixbuf,
828 x - button->allocation.x, y - button->allocation.y);
829 g_object_unref(pixbuf);
832 static void bookmark_populate(BookMarkData *bm)
838 box = GTK_BOX(bm->box);
839 children = gtk_container_get_children(GTK_CONTAINER(box));
843 GtkWidget *widget = GTK_WIDGET(work->data);
845 gtk_widget_destroy(widget);
848 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
853 if (!bookmark_default_list)
855 buf = bookmark_string(_("Home"), homedir(), NULL);
856 history_list_add_to_key(bm->key, buf, 0);
859 path = concat_dir_and_file(homedir(), "Desktop");
862 buf = bookmark_string(_("Desktop"), path, NULL);
863 history_list_add_to_key(bm->key, buf, 0);
869 work = bookmark_default_list;
870 while (work && work->next)
879 buf = bookmark_string(name, path, NULL);
880 history_list_add_to_key(bm->key, buf, 0);
885 work = history_list_get_by_key(bm->key);
886 work = g_list_last(work);
891 b = bookmark_from_string(work->data);
896 b->button = gtk_button_new();
897 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
898 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
899 gtk_widget_show(b->button);
901 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
902 b, (GDestroyNotify)bookmark_free);
904 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
905 gtk_container_add(GTK_CONTAINER(b->button), box);
906 gtk_widget_show(box);
913 iconl = path_from_utf8(b->icon);
914 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
922 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
924 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
925 GDK_INTERP_BILINEAR);
926 b->image = gtk_image_new_from_pixbuf(scaled);
927 g_object_unref(scaled);
928 g_object_unref(pixbuf);
932 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
933 GTK_ICON_SIZE_BUTTON);
938 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
940 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
941 gtk_widget_show(b->image);
943 b->label = gtk_label_new(b->name);
944 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
945 gtk_widget_show(b->label);
947 g_signal_connect(G_OBJECT(b->button), "clicked",
948 G_CALLBACK(bookmark_select_cb), bm);
949 g_signal_connect(G_OBJECT(b->button), "button_press_event",
950 G_CALLBACK(bookmark_press_cb), bm);
951 g_signal_connect(G_OBJECT(b->button), "key_press_event",
952 G_CALLBACK(bookmark_keypress_cb), bm);
954 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
955 bookmark_drag_types, bookmark_drag_types_n,
956 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
957 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
958 G_CALLBACK(bookmark_drag_set_data), bm);
959 g_signal_connect(G_OBJECT(b->button), "drag_begin",
960 G_CALLBACK(bookmark_drag_begin), bm);
967 static void bookmark_populate_all(const gchar *key)
973 work = bookmark_widget_list;
981 if (strcmp(bm->key, key) == 0)
983 bookmark_populate(bm);
988 static void bookmark_dnd_get_data(GtkWidget *widget,
989 GdkDragContext *context, gint x, gint y,
990 GtkSelectionData *selection_data, guint info,
991 guint time, gpointer data)
993 BookMarkData *bm = data;
997 if (!bm->editable) return;
1001 case TARGET_URI_LIST:
1003 list = uri_list_from_text((gchar *)selection_data->data, FALSE);
1010 gchar *path = work->data;
1015 if (bm->only_directories && !isdir(path)) continue;
1016 buf = bookmark_string(filename_from_path(path), path, NULL);
1017 history_list_add_to_key(bm->key, buf, 0);
1021 string_list_free(list);
1023 bookmark_populate_all(bm->key);
1026 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
1028 BookMarkData *bm = data;
1030 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
1036 GtkWidget *bookmark_list_new(const gchar *key,
1037 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
1039 GtkWidget *scrolled;
1042 if (!key) key = "bookmarks";
1044 bm = g_new0(BookMarkData, 1);
1045 bm->key = g_strdup(key);
1047 bm->select_func = select_func;
1048 bm->select_data = select_data;
1050 bm->no_defaults = FALSE;
1051 bm->editable = TRUE;
1052 bm->only_directories = FALSE;
1054 scrolled = gtk_scrolled_window_new(NULL, NULL);
1055 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1056 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1058 bm->box = gtk_vbox_new(FALSE, 0);
1059 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1060 gtk_widget_show(bm->box);
1062 bookmark_populate(bm);
1064 g_signal_connect(G_OBJECT(bm->box), "destroy",
1065 G_CALLBACK(bookmark_list_destroy), bm);
1066 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1067 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1068 bm->widget = scrolled;
1070 gtk_drag_dest_set(scrolled,
1071 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1072 bookmark_drop_types, bookmark_drop_types_n,
1073 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1074 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1075 G_CALLBACK(bookmark_dnd_get_data), bm);
1077 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1082 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1086 if (!list || !key) return;
1088 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1091 if (bm->key && strcmp(bm->key, key) == 0) return;
1094 bm->key = g_strdup(key);
1096 bookmark_populate(bm);
1099 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1103 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1106 bm->no_defaults = no_defaults;
1109 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1113 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1116 bm->editable = editable;
1119 void bookmark_list_set_only_directories(GtkWidget *list, gint only_directories)
1123 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1126 bm->only_directories = only_directories;
1129 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1134 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1137 buf = bookmark_string(name, path, NULL);
1138 history_list_add_to_key(bm->key, buf, 0);
1141 bookmark_populate_all(bm->key);
1144 void bookmark_add_default(const gchar *name, const gchar *path)
1146 if (!name || !path) return;
1147 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1148 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1152 *-----------------------------------------------------------------------------
1153 * combo with history key
1154 *-----------------------------------------------------------------------------
1157 typedef struct _HistoryComboData HistoryComboData;
1158 struct _HistoryComboData
1163 gint history_levels;
1166 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1168 HistoryComboData *hc = data;
1170 g_free(hc->history_key);
1174 /* if text is NULL, entry is set to the most recent item */
1175 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1176 const gchar *history_key, gint max_levels)
1178 HistoryComboData *hc;
1182 hc = g_new0(HistoryComboData, 1);
1183 hc->history_key = g_strdup(history_key);
1184 hc->history_levels = max_levels;
1186 hc->combo = gtk_combo_box_entry_new_text();
1188 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1189 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1192 hc->entry = GTK_BIN(hc->combo)->child;
1194 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1195 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1196 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1197 G_CALLBACK(history_combo_destroy), hc);
1199 work = history_list_get_by_key(hc->history_key);
1202 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1209 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1213 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1216 if (entry) *entry = hc->entry;
1220 /* if text is NULL, current entry text is used
1221 * widget can be the combo or entry widget
1223 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1225 HistoryComboData *hc;
1228 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1231 printf("widget is not a history combo\n");
1237 new_text = g_strdup(text);
1241 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1244 if (new_text && strlen(new_text) > 0)
1246 GtkTreeModel *store;
1249 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1251 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1253 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1254 gtk_list_store_clear(GTK_LIST_STORE(store));
1256 work = history_list_get_by_key(hc->history_key);
1259 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1268 *-----------------------------------------------------------------------------
1269 * drag and drop uri utils
1270 *-----------------------------------------------------------------------------
1273 /* the following characters are allowed to be unencoded for pathnames:
1276 static gint escape_char_list[] = {
1277 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1278 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1279 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1280 /* spc ! " # $ % & ' */
1281 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1282 /* ( ) * + , - . / 0 1 */
1283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1284 /* 2 3 4 5 6 7 8 9 : ; */
1285 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1286 /* < = > ? @ A B C D E */
1287 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1288 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1289 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1290 /* Z [ \ ] ^ _ ` a b c */
1291 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1292 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1293 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1294 /* x y z { | } ~ del */
1295 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1298 static gchar *hex_char = "0123456789ABCDEF";
1300 static gint escape_test(guchar c)
1302 if (c < 32 || c > 127) return TRUE;
1303 return (escape_char_list[c] != 0);
1306 static const gchar *escape_code(guchar c)
1308 static gchar text[4];
1311 text[1] = hex_char[c>>4];
1312 text[2] = hex_char[c%16];
1318 gchar *uri_text_escape(const gchar *text)
1324 if (!text) return NULL;
1326 string = g_string_new("");
1331 if (escape_test(*p))
1333 g_string_append(string, escape_code(*p));
1337 g_string_append_c(string, *p);
1342 result = string->str;
1343 g_string_free(string, FALSE);
1345 /* dropped filenames are expected to be utf-8 compatible */
1346 if (!g_utf8_validate(result, -1, NULL))
1350 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1361 /* this operates on the passed string, decoding escaped characters */
1362 void uri_text_decode(gchar *text)
1364 if (strchr(text, '%'))
1373 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1383 n = (gint)strtol(t, NULL, 16);
1384 if (n > 0 && n < 256)
1390 /* invalid number, rewind and ignore this escape */
1402 if (*w != '\0') *w = '\0';
1406 static void uri_list_parse_encoded_chars(GList *list)
1412 gchar *text = work->data;
1414 uri_text_decode(text);
1420 GList *uri_list_from_text(gchar *data, gint files_only)
1427 while (data[b] != '\0')
1429 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1430 if (strncmp(data + b, "file:", 5) == 0)
1434 while (data[b] == '/' && data[b+1] == '/') b++;
1435 path = g_strndup(data + b, e - b);
1436 list = g_list_append(list, path_to_utf8(path));
1439 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1441 list = g_list_append(list, g_strndup(data + b, e - b));
1443 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1445 list = g_list_append(list, g_strndup(data + b, e - b));
1447 while (data[e] == '\r' || data[e] == '\n') e++;
1451 uri_list_parse_encoded_chars(list);
1456 GList *uri_filelist_from_text(gchar *data, gint files_only)
1458 GList *path_list = uri_list_from_text(data, files_only);
1459 GList *filelist = filelist_from_path_list(path_list);
1460 string_list_free(path_list);
1464 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1466 gchar *uri_text = NULL;
1476 string = g_string_new("");
1481 const gchar *name8; /* dnd filenames are in utf-8 */
1489 escaped = uri_text_escape(name8);
1490 g_string_append(string, "file:");
1491 g_string_append(string, escaped);
1494 g_string_append(string, "\r\n");
1498 g_string_append(string, name8);
1499 if (work->next) g_string_append(string, "\n");
1505 uri_text = string->str;
1506 if (len) *len = string->len;
1507 g_string_free(string, FALSE);
1512 gchar *uri_text_from_filelist(GList *list, gint *len, gint plain_text)
1514 GList *path_list = filelist_to_path_list(list);
1515 gchar *ret = uri_text_from_list(path_list, len, plain_text);
1516 string_list_free(path_list);