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 */
30 #include "secure_save.h"
31 #include "ui_bookmark.h"
32 #include "ui_fileops.h"
35 #include "ui_utildlg.h"
36 #include "ui_tabcomp.h"
40 *-----------------------------------------------------------------------------
42 *-----------------------------------------------------------------------------
45 #define HISTORY_DEFAULT_KEY_COUNT 16
48 typedef struct _HistoryData HistoryData;
55 static GList *history_list = NULL;
58 static gchar *quoted_from_text(const gchar *text)
62 gint l = strlen(text);
64 if (l == 0) return NULL;
66 while (c < l && text[c] !='"') c++;
73 while (e < l && text[e] !='"') e++;
78 return g_strndup(ptr, e - c);
85 gint history_list_load(const gchar *path)
92 pathl = path_from_utf8(path);
93 f = fopen(pathl, "r");
97 /* first line must start with History comment */
98 if (!fgets(s_buf, sizeof(s_buf), f) ||
99 strncmp(s_buf, "#History", 8) != 0)
105 while (fgets(s_buf, sizeof(s_buf), f))
107 if (s_buf[0]=='#') continue;
115 while(ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
118 key = g_strndup(ptr, c);
124 value = quoted_from_text(s_buf);
127 history_list_add_to_key(key, value, 0);
140 gint history_list_save(const gchar *path)
146 pathl = path_from_utf8(path);
147 ssi = secure_open(pathl);
151 printf_term(_("Unable to write history lists to: %s\n"), path);
155 secure_fprintf(ssi, "#History lists\n");
156 secure_fprintf(ssi, "\n");
158 list = g_list_last(history_list);
159 while(list && secsave_errno == SS_ERR_NONE)
167 secure_fprintf(ssi, "[%s]\n", hd->key);
169 /* save them inverted (oldest to newest)
170 * so that when reading they are added correctly
172 work = g_list_last(hd->list);
173 while(work && secsave_errno == SS_ERR_NONE)
175 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
178 secure_fputc(ssi, '\n');
181 secure_fprintf(ssi, "#end\n");
183 return (secure_close(ssi) == 0);
186 static void history_list_free(HistoryData *hd)
203 static HistoryData *history_list_find_by_key(const gchar* key)
205 GList *work = history_list;
207 if (!key) return NULL;
211 HistoryData *hd = work->data;
212 if (strcmp(hd->key, key) == 0) return hd;
218 const gchar *history_list_find_last_path_by_key(const gchar* key)
222 hd = history_list_find_by_key(key);
223 if (!hd || !hd->list) return NULL;
225 return hd->list->data;
228 void history_list_free_key(const gchar *key)
231 hd = history_list_find_by_key(key);
234 history_list = g_list_remove(history_list, hd);
235 history_list_free(hd);
238 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
243 if (!key || !path) return;
245 hd = history_list_find_by_key(key);
248 hd = g_new(HistoryData, 1);
249 hd->key = g_strdup(key);
251 history_list = g_list_prepend(history_list, hd);
254 /* if already in the list, simply move it to the top */
258 gchar *buf = work->data;
260 if (strcmp(buf, path) == 0)
262 hd->list = g_list_remove(hd->list, buf);
263 hd->list = g_list_prepend(hd->list, buf);
268 hd->list = g_list_prepend(hd->list, g_strdup(path));
270 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
273 while(hd->list && g_list_length(hd->list) > max)
275 GList *work = g_list_last(hd->list);
276 gchar *buf = work->data;
277 hd->list = g_list_remove(hd->list, buf);
283 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
288 if (!oldpath) return;
289 hd = history_list_find_by_key(key);
295 gchar *buf = work->data;
296 if (strcmp(buf, oldpath) == 0)
300 work->data = g_strdup(newpath);
304 hd->list = g_list_remove(hd->list, buf);
313 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
320 hd = history_list_find_by_key(key);
326 gchar *buf = work->data;
327 if (strcmp(buf, path) == 0)
331 hd->list = g_list_remove(hd->list, buf);
332 hd->list = g_list_insert(hd->list, buf, p);
340 void history_list_item_remove(const gchar *key, const gchar *path)
342 history_list_item_change(key, path, NULL);
345 GList *history_list_get_by_key(const gchar *key)
349 hd = history_list_find_by_key(key);
350 if (!hd) return NULL;
356 *-----------------------------------------------------------------------------
358 *-----------------------------------------------------------------------------
361 #define BOOKMARK_DATA_KEY "bookmarkdata"
362 #define MARKER_PATH "[path]"
363 #define MARKER_ICON "[icon]"
365 typedef struct _BookMarkData BookMarkData;
366 typedef struct _BookButtonData BookButtonData;
367 typedef struct _BookPropData BookPropData;
375 void (*select_func)(const gchar *path, gpointer data);
376 gpointer select_data;
381 BookButtonData *active_button;
384 struct _BookButtonData
399 GtkWidget *name_entry;
400 GtkWidget *path_entry;
401 GtkWidget *icon_entry;
412 static GtkTargetEntry bookmark_drop_types[] = {
413 { "text/uri-list", 0, TARGET_URI_LIST },
414 { "x-url/http", 0, TARGET_X_URL },
415 { "_NETSCAPE_URL", 0, TARGET_X_URL }
417 #define bookmark_drop_types_n 3
419 static GtkTargetEntry bookmark_drag_types[] = {
420 { "text/uri-list", 0, TARGET_URI_LIST },
421 { "text/plain", 0, TARGET_TEXT_PLAIN }
423 #define bookmark_drag_types_n 2
426 static GList *bookmark_widget_list = NULL;
427 static GList *bookmark_default_list = NULL;
430 static void bookmark_populate_all(const gchar *key);
433 static BookButtonData *bookmark_from_string(const gchar *text)
436 const gchar *path_ptr;
437 const gchar *icon_ptr;
439 b = g_new0(BookButtonData, 1);
443 b->name = g_strdup(_("New Bookmark"));
444 b->path = g_strdup(homedir());
449 b->key = g_strdup(text);
451 path_ptr = strstr(text, MARKER_PATH);
452 icon_ptr = strstr(text, MARKER_ICON);
454 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
456 printf("warning, bookmark icon must be after path\n");
465 b->name = g_strndup(text, l);
466 path_ptr += strlen(MARKER_PATH);
469 l = icon_ptr - path_ptr;
470 b->path = g_strndup(path_ptr, l);
474 b->path = g_strdup(path_ptr);
479 b->name = g_strdup(text);
480 b->path = g_strdup("");
485 icon_ptr += strlen(MARKER_ICON);
486 b->icon = g_strdup(icon_ptr);
492 static void bookmark_free(BookButtonData *b)
504 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
506 if (!name) name = _("New Bookmark");
507 if (icon && strncmp(icon, "/", 1) != 0) icon = NULL;
511 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
514 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
517 static void bookmark_select_cb(GtkWidget *button, gpointer data)
519 BookMarkData *bm = data;
522 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
525 if (bm->select_func) bm->select_func(b->path, bm->select_data);
528 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
530 BookPropData *p = data;
532 bookmark_free(p->bb);
536 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
540 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
542 BookPropData *p = data;
548 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
549 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
550 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
552 new = bookmark_string(name, path, icon);
556 history_list_item_change(p->bb->parent, p->bb->key, new);
560 history_list_add_to_key(p->bb->parent, new, 0);
563 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
564 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
569 bookmark_populate_all(p->bb->parent);
572 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
574 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
582 if (!key) key = "bookmarks";
584 p = g_new0(BookPropData, 1);
586 p->bb = bookmark_from_string(text);
587 p->bb->parent = g_strdup(key);
589 gd = generic_dialog_new(_("Edit Bookmark"), GQ_WMCLASS, "bookmark_edit",
591 bookmark_edit_cancel_cb, p);
592 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
593 G_CALLBACK(bookmark_edit_destroy_cb), p);
595 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
597 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
598 bookmark_edit_ok_cb, TRUE);
600 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
601 pref_table_label(table, 0, 0, _("Name:"), 1.0);
603 p->name_entry = gtk_entry_new();
604 gtk_widget_set_size_request(p->name_entry, 300, -1);
605 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
606 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
607 generic_dialog_attach_default(gd, p->name_entry);
608 gtk_widget_show(p->name_entry);
610 pref_table_label(table, 0, 1, _("Path:"), 1.0);
612 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
613 "bookmark_path", -1, NULL, NULL);
614 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
615 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
616 generic_dialog_attach_default(gd, p->path_entry);
617 gtk_widget_show(label);
619 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
622 if (!icon) icon = "";
623 label = tab_completion_new_with_history(&p->icon_entry, icon,
624 "bookmark_icons", -1, NULL, NULL);
625 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
626 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
627 generic_dialog_attach_default(gd, p->icon_entry);
628 gtk_widget_show(label);
630 gtk_widget_show(gd->dialog);
633 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
640 if (!bm->editable) return;
642 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
645 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
646 p = g_list_index(list, button);
649 if (p < 0 || p + direction < 0) return;
651 key_holder = bm->key;
652 bm->key = "_TEMPHOLDER";
653 history_list_item_move(key_holder, b->key, -direction);
654 bookmark_populate_all(key_holder);
655 bm->key = key_holder;
657 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
660 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
662 BookMarkData *bm = data;
664 if (!bm->active_button) return;
666 bookmark_edit(bm->key, bm->active_button->key, widget);
669 static void bookmark_menu_move(BookMarkData *bm, gint direction)
671 if (!bm->active_button) return;
673 bookmark_move(bm, bm->active_button->button, direction);
676 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
678 bookmark_menu_move(data, -1);
681 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
683 bookmark_menu_move(data, 1);
686 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
688 BookMarkData *bm = data;
690 if (!bm->active_button) return;
692 history_list_item_remove(bm->key, bm->active_button->key);
693 bookmark_populate_all(bm->key);
696 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
698 GtkWidget *button = data;
700 gdk_window_get_origin(button->window, x, y);
701 *y += button->allocation.y + button->allocation.height;
704 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
705 gint button_n, guint32 time, gint local)
710 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
713 bm->active_button = b;
715 menu = popup_menu_short_lived();
716 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
717 G_CALLBACK(bookmark_menu_prop_cb), bm);
718 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
719 G_CALLBACK(bookmark_menu_up_cb), bm);
720 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
721 G_CALLBACK(bookmark_menu_down_cb), bm);
722 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
723 G_CALLBACK(bookmark_menu_remove_cb), bm);
727 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
728 bookmark_menu_position_cb, button, button_n, time);
732 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
736 static gint bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
738 BookMarkData *bm = data;
740 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
742 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
747 static gint bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
749 BookMarkData *bm = data;
751 switch (event->keyval)
754 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
756 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
760 if (event->state & GDK_SHIFT_MASK)
762 bookmark_move(bm, button, -1);
767 if (event->state & GDK_SHIFT_MASK)
769 bookmark_move(bm, button, 1);
778 static void bookmark_drag_set_data(GtkWidget *button,
779 GdkDragContext *context, GtkSelectionData *selection_data,
780 guint info, guint time, gpointer data)
782 BookMarkData *bm = data;
784 gchar *uri_text = NULL;
788 if (context->dest_window == bm->widget->window) return;
790 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
793 list = g_list_append(list, b->path);
797 case TARGET_URI_LIST:
798 uri_text = uri_text_from_list(list, &length, FALSE);
800 case TARGET_TEXT_PLAIN:
801 uri_text = uri_text_from_list(list, &length, TRUE);
807 if (!uri_text) return;
809 gtk_selection_data_set(selection_data, selection_data->target,
810 8, (guchar *)uri_text, length);
814 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
817 GdkModifierType mask;
820 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
821 button->allocation.width, button->allocation.height);
822 gdk_pixbuf_get_from_drawable(pixbuf, button->window, NULL,
823 button->allocation.x, button->allocation.y,
824 0, 0, button->allocation.width, button->allocation.height);
826 gdk_window_get_pointer(button->window, &x, &y, &mask);
828 gtk_drag_set_icon_pixbuf(context, pixbuf,
829 x - button->allocation.x, y - button->allocation.y);
830 g_object_unref(pixbuf);
833 static void bookmark_populate(BookMarkData *bm)
839 box = GTK_BOX(bm->box);
840 children = gtk_container_get_children(GTK_CONTAINER(box));
844 GtkWidget *widget = GTK_WIDGET(work->data);
846 gtk_widget_destroy(widget);
849 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
854 if (!bookmark_default_list)
856 buf = bookmark_string(_("Home"), homedir(), NULL);
857 history_list_add_to_key(bm->key, buf, 0);
860 path = concat_dir_and_file(homedir(), "Desktop");
863 buf = bookmark_string(_("Desktop"), path, NULL);
864 history_list_add_to_key(bm->key, buf, 0);
870 work = bookmark_default_list;
871 while (work && work->next)
880 buf = bookmark_string(name, path, NULL);
881 history_list_add_to_key(bm->key, buf, 0);
886 work = history_list_get_by_key(bm->key);
887 work = g_list_last(work);
892 b = bookmark_from_string(work->data);
897 b->button = gtk_button_new();
898 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
899 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
900 gtk_widget_show(b->button);
902 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
903 b, (GDestroyNotify)bookmark_free);
905 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
906 gtk_container_add(GTK_CONTAINER(b->button), box);
907 gtk_widget_show(box);
914 iconl = path_from_utf8(b->icon);
915 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
923 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
925 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
926 GDK_INTERP_BILINEAR);
927 b->image = gtk_image_new_from_pixbuf(scaled);
928 g_object_unref(scaled);
929 g_object_unref(pixbuf);
933 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
934 GTK_ICON_SIZE_BUTTON);
939 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
941 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
942 gtk_widget_show(b->image);
944 b->label = gtk_label_new(b->name);
945 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
946 gtk_widget_show(b->label);
948 g_signal_connect(G_OBJECT(b->button), "clicked",
949 G_CALLBACK(bookmark_select_cb), bm);
950 g_signal_connect(G_OBJECT(b->button), "button_press_event",
951 G_CALLBACK(bookmark_press_cb), bm);
952 g_signal_connect(G_OBJECT(b->button), "key_press_event",
953 G_CALLBACK(bookmark_keypress_cb), bm);
955 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
956 bookmark_drag_types, bookmark_drag_types_n,
957 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
958 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
959 G_CALLBACK(bookmark_drag_set_data), bm);
960 g_signal_connect(G_OBJECT(b->button), "drag_begin",
961 G_CALLBACK(bookmark_drag_begin), bm);
968 static void bookmark_populate_all(const gchar *key)
974 work = bookmark_widget_list;
982 if (strcmp(bm->key, key) == 0)
984 bookmark_populate(bm);
989 static void bookmark_dnd_get_data(GtkWidget *widget,
990 GdkDragContext *context, gint x, gint y,
991 GtkSelectionData *selection_data, guint info,
992 guint time, gpointer data)
994 BookMarkData *bm = data;
998 if (!bm->editable) return;
1002 case TARGET_URI_LIST:
1004 list = uri_list_from_text((gchar *)selection_data->data, FALSE);
1011 gchar *path = work->data;
1014 buf = bookmark_string(filename_from_path(path), path, NULL);
1015 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;
1053 scrolled = gtk_scrolled_window_new(NULL, NULL);
1054 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1055 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1057 bm->box = gtk_vbox_new(FALSE, 0);
1058 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1059 gtk_widget_show(bm->box);
1061 bookmark_populate(bm);
1063 g_signal_connect(G_OBJECT(bm->box), "destroy",
1064 G_CALLBACK(bookmark_list_destroy), bm);
1065 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1066 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1067 bm->widget = scrolled;
1069 gtk_drag_dest_set(scrolled,
1070 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1071 bookmark_drop_types, bookmark_drop_types_n,
1072 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1073 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1074 G_CALLBACK(bookmark_dnd_get_data), bm);
1076 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1081 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1085 if (!list || !key) return;
1087 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1090 if (bm->key && strcmp(bm->key, key) == 0) return;
1093 bm->key = g_strdup(key);
1095 bookmark_populate(bm);
1098 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1102 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1105 bm->no_defaults = no_defaults;
1108 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1112 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1115 bm->editable = editable;
1118 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1123 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1126 buf = bookmark_string(name, path, NULL);
1127 history_list_add_to_key(bm->key, buf, 0);
1130 bookmark_populate_all(bm->key);
1133 void bookmark_add_default(const gchar *name, const gchar *path)
1135 if (!name || !path) return;
1136 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1137 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1141 *-----------------------------------------------------------------------------
1142 * combo with history key
1143 *-----------------------------------------------------------------------------
1146 typedef struct _HistoryComboData HistoryComboData;
1147 struct _HistoryComboData
1152 gint history_levels;
1155 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1157 HistoryComboData *hc = data;
1159 g_free(hc->history_key);
1163 /* if text is NULL, entry is set to the most recent item */
1164 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1165 const gchar *history_key, gint max_levels)
1167 HistoryComboData *hc;
1171 hc = g_new0(HistoryComboData, 1);
1172 hc->history_key = g_strdup(history_key);
1173 hc->history_levels = max_levels;
1175 hc->combo = gtk_combo_box_entry_new_text();
1177 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1178 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1181 hc->entry = GTK_BIN(hc->combo)->child;
1183 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1184 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1185 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1186 G_CALLBACK(history_combo_destroy), hc);
1188 work = history_list_get_by_key(hc->history_key);
1191 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1198 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1202 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1205 if (entry) *entry = hc->entry;
1209 /* if text is NULL, current entry text is used
1210 * widget can be the combo or entry widget
1212 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1214 HistoryComboData *hc;
1217 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1220 printf("widget is not a history combo\n");
1226 new_text = g_strdup(text);
1230 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1233 if (new_text && strlen(new_text) > 0)
1235 GtkTreeModel *store;
1238 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1240 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1242 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1243 gtk_list_store_clear(GTK_LIST_STORE(store));
1245 work = history_list_get_by_key(hc->history_key);
1248 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1257 *-----------------------------------------------------------------------------
1258 * drag and drop uri utils
1259 *-----------------------------------------------------------------------------
1262 /* the following characters are allowed to be unencoded for pathnames:
1265 static gint escape_char_list[] = {
1266 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1267 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1268 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1269 /* spc ! " # $ % & ' */
1270 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1271 /* ( ) * + , - . / 0 1 */
1272 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1273 /* 2 3 4 5 6 7 8 9 : ; */
1274 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1275 /* < = > ? @ A B C D E */
1276 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1278 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1279 /* Z [ \ ] ^ _ ` a b c */
1280 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1283 /* x y z { | } ~ del */
1284 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1287 static gchar *hex_char = "0123456789ABCDEF";
1289 static gint escape_test(guchar c)
1291 if (c < 32 || c > 127) return TRUE;
1292 return (escape_char_list[c] != 0);
1295 static const gchar *escape_code(guchar c)
1297 static gchar text[4];
1300 text[1] = hex_char[c>>4];
1301 text[2] = hex_char[c%16];
1307 gchar *uri_text_escape(const gchar *text)
1313 if (!text) return NULL;
1315 string = g_string_new("");
1320 if (escape_test(*p))
1322 g_string_append(string, escape_code(*p));
1326 g_string_append_c(string, *p);
1331 result = string->str;
1332 g_string_free(string, FALSE);
1334 /* dropped filenames are expected to be utf-8 compatible */
1335 if (!g_utf8_validate(result, -1, NULL))
1339 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1350 /* this operates on the passed string, decoding escaped characters */
1351 void uri_text_decode(gchar *text)
1353 if (strchr(text, '%'))
1362 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1372 n = (gint)strtol(t, NULL, 16);
1373 if (n > 0 && n < 256)
1379 /* invalid number, rewind and ignore this escape */
1391 if (*w != '\0') *w = '\0';
1395 static void uri_list_parse_encoded_chars(GList *list)
1401 gchar *text = work->data;
1403 uri_text_decode(text);
1409 GList *uri_list_from_text(gchar *data, gint files_only)
1416 while (data[b] != '\0')
1418 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1419 if (strncmp(data + b, "file:", 5) == 0)
1423 while (data[b] == '/' && data[b+1] == '/') b++;
1424 path = g_strndup(data + b, e - b);
1425 list = g_list_append(list, path_to_utf8(path));
1428 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1430 list = g_list_append(list, g_strndup(data + b, e - b));
1432 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1434 list = g_list_append(list, g_strndup(data + b, e - b));
1436 while (data[e] == '\r' || data[e] == '\n') e++;
1440 uri_list_parse_encoded_chars(list);
1445 GList *uri_filelist_from_text(gchar *data, gint files_only)
1447 GList *path_list = uri_list_from_text(data, files_only);
1448 GList *filelist = filelist_from_path_list(path_list);
1449 string_list_free(path_list);
1453 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1455 gchar *uri_text = NULL;
1465 string = g_string_new("");
1470 const gchar *name8; /* dnd filenames are in utf-8 */
1478 escaped = uri_text_escape(name8);
1479 g_string_append(string, "file:");
1480 g_string_append(string, escaped);
1483 g_string_append(string, "\r\n");
1487 g_string_append(string, name8);
1488 if (work->next) g_string_append(string, "\n");
1494 uri_text = string->str;
1495 if (len) *len = string->len;
1496 g_string_free(string, FALSE);
1501 gchar *uri_text_from_filelist(GList *list, gint *len, gint plain_text)
1503 GList *path_list = filelist_to_path_list(list);
1504 gchar *ret = uri_text_from_list(path_list, len, plain_text);
1505 string_list_free(path_list);