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\n");
157 list = g_list_last(history_list);
158 while (list && secsave_errno == SS_ERR_NONE)
166 secure_fprintf(ssi, "[%s]\n", hd->key);
168 /* save them inverted (oldest to newest)
169 * so that when reading they are added correctly
171 work = g_list_last(hd->list);
172 while (work && secsave_errno == SS_ERR_NONE)
174 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
177 secure_fputc(ssi, '\n');
180 secure_fprintf(ssi, "#end\n");
182 return (secure_close(ssi) == 0);
185 static void history_list_free(HistoryData *hd)
202 static HistoryData *history_list_find_by_key(const gchar* key)
204 GList *work = history_list;
206 if (!key) return NULL;
210 HistoryData *hd = work->data;
211 if (strcmp(hd->key, key) == 0) return hd;
217 const gchar *history_list_find_last_path_by_key(const gchar* key)
221 hd = history_list_find_by_key(key);
222 if (!hd || !hd->list) return NULL;
224 return hd->list->data;
227 void history_list_free_key(const gchar *key)
230 hd = history_list_find_by_key(key);
233 history_list = g_list_remove(history_list, hd);
234 history_list_free(hd);
237 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
242 if (!key || !path) return;
244 hd = history_list_find_by_key(key);
247 hd = g_new(HistoryData, 1);
248 hd->key = g_strdup(key);
250 history_list = g_list_prepend(history_list, hd);
253 /* if already in the list, simply move it to the top */
257 gchar *buf = work->data;
259 if (strcmp(buf, path) == 0)
261 hd->list = g_list_remove(hd->list, buf);
262 hd->list = g_list_prepend(hd->list, buf);
267 hd->list = g_list_prepend(hd->list, g_strdup(path));
269 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
272 while (hd->list && g_list_length(hd->list) > max)
274 GList *work = g_list_last(hd->list);
275 gchar *buf = work->data;
276 hd->list = g_list_remove(hd->list, buf);
282 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
287 if (!oldpath) return;
288 hd = history_list_find_by_key(key);
294 gchar *buf = work->data;
295 if (strcmp(buf, oldpath) == 0)
299 work->data = g_strdup(newpath);
303 hd->list = g_list_remove(hd->list, buf);
312 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
319 hd = history_list_find_by_key(key);
325 gchar *buf = work->data;
326 if (strcmp(buf, path) == 0)
330 hd->list = g_list_remove(hd->list, buf);
331 hd->list = g_list_insert(hd->list, buf, p);
339 void history_list_item_remove(const gchar *key, const gchar *path)
341 history_list_item_change(key, path, NULL);
344 GList *history_list_get_by_key(const gchar *key)
348 hd = history_list_find_by_key(key);
349 if (!hd) return NULL;
355 *-----------------------------------------------------------------------------
357 *-----------------------------------------------------------------------------
360 #define BOOKMARK_DATA_KEY "bookmarkdata"
361 #define MARKER_PATH "[path]"
362 #define MARKER_ICON "[icon]"
364 typedef struct _BookMarkData BookMarkData;
365 typedef struct _BookButtonData BookButtonData;
366 typedef struct _BookPropData BookPropData;
374 void (*select_func)(const gchar *path, gpointer data);
375 gpointer select_data;
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;
1013 buf = bookmark_string(filename_from_path(path), path, NULL);
1014 history_list_add_to_key(bm->key, buf, 0);
1020 string_list_free(list);
1022 bookmark_populate_all(bm->key);
1025 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
1027 BookMarkData *bm = data;
1029 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
1035 GtkWidget *bookmark_list_new(const gchar *key,
1036 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
1038 GtkWidget *scrolled;
1041 if (!key) key = "bookmarks";
1043 bm = g_new0(BookMarkData, 1);
1044 bm->key = g_strdup(key);
1046 bm->select_func = select_func;
1047 bm->select_data = select_data;
1049 bm->no_defaults = FALSE;
1050 bm->editable = TRUE;
1052 scrolled = gtk_scrolled_window_new(NULL, NULL);
1053 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1054 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1056 bm->box = gtk_vbox_new(FALSE, 0);
1057 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1058 gtk_widget_show(bm->box);
1060 bookmark_populate(bm);
1062 g_signal_connect(G_OBJECT(bm->box), "destroy",
1063 G_CALLBACK(bookmark_list_destroy), bm);
1064 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1065 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1066 bm->widget = scrolled;
1068 gtk_drag_dest_set(scrolled,
1069 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1070 bookmark_drop_types, bookmark_drop_types_n,
1071 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1072 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1073 G_CALLBACK(bookmark_dnd_get_data), bm);
1075 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1080 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1084 if (!list || !key) return;
1086 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1089 if (bm->key && strcmp(bm->key, key) == 0) return;
1092 bm->key = g_strdup(key);
1094 bookmark_populate(bm);
1097 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1101 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1104 bm->no_defaults = no_defaults;
1107 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1111 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1114 bm->editable = editable;
1117 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1122 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1125 buf = bookmark_string(name, path, NULL);
1126 history_list_add_to_key(bm->key, buf, 0);
1129 bookmark_populate_all(bm->key);
1132 void bookmark_add_default(const gchar *name, const gchar *path)
1134 if (!name || !path) return;
1135 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1136 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1140 *-----------------------------------------------------------------------------
1141 * combo with history key
1142 *-----------------------------------------------------------------------------
1145 typedef struct _HistoryComboData HistoryComboData;
1146 struct _HistoryComboData
1151 gint history_levels;
1154 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1156 HistoryComboData *hc = data;
1158 g_free(hc->history_key);
1162 /* if text is NULL, entry is set to the most recent item */
1163 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1164 const gchar *history_key, gint max_levels)
1166 HistoryComboData *hc;
1170 hc = g_new0(HistoryComboData, 1);
1171 hc->history_key = g_strdup(history_key);
1172 hc->history_levels = max_levels;
1174 hc->combo = gtk_combo_box_entry_new_text();
1176 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1177 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1180 hc->entry = GTK_BIN(hc->combo)->child;
1182 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1183 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1184 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1185 G_CALLBACK(history_combo_destroy), hc);
1187 work = history_list_get_by_key(hc->history_key);
1190 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1197 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1201 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1204 if (entry) *entry = hc->entry;
1208 /* if text is NULL, current entry text is used
1209 * widget can be the combo or entry widget
1211 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1213 HistoryComboData *hc;
1216 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1219 printf("widget is not a history combo\n");
1225 new_text = g_strdup(text);
1229 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1232 if (new_text && strlen(new_text) > 0)
1234 GtkTreeModel *store;
1237 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1239 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1241 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1242 gtk_list_store_clear(GTK_LIST_STORE(store));
1244 work = history_list_get_by_key(hc->history_key);
1247 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1256 *-----------------------------------------------------------------------------
1257 * drag and drop uri utils
1258 *-----------------------------------------------------------------------------
1261 /* the following characters are allowed to be unencoded for pathnames:
1264 static gint escape_char_list[] = {
1265 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1266 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1267 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1268 /* spc ! " # $ % & ' */
1269 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1270 /* ( ) * + , - . / 0 1 */
1271 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1272 /* 2 3 4 5 6 7 8 9 : ; */
1273 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1274 /* < = > ? @ A B C D E */
1275 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1276 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1278 /* Z [ \ ] ^ _ ` a b c */
1279 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1282 /* x y z { | } ~ del */
1283 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1286 static gchar *hex_char = "0123456789ABCDEF";
1288 static gint escape_test(guchar c)
1290 if (c < 32 || c > 127) return TRUE;
1291 return (escape_char_list[c] != 0);
1294 static const gchar *escape_code(guchar c)
1296 static gchar text[4];
1299 text[1] = hex_char[c>>4];
1300 text[2] = hex_char[c%16];
1306 gchar *uri_text_escape(const gchar *text)
1312 if (!text) return NULL;
1314 string = g_string_new("");
1319 if (escape_test(*p))
1321 g_string_append(string, escape_code(*p));
1325 g_string_append_c(string, *p);
1330 result = string->str;
1331 g_string_free(string, FALSE);
1333 /* dropped filenames are expected to be utf-8 compatible */
1334 if (!g_utf8_validate(result, -1, NULL))
1338 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1349 /* this operates on the passed string, decoding escaped characters */
1350 void uri_text_decode(gchar *text)
1352 if (strchr(text, '%'))
1361 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1371 n = (gint)strtol(t, NULL, 16);
1372 if (n > 0 && n < 256)
1378 /* invalid number, rewind and ignore this escape */
1390 if (*w != '\0') *w = '\0';
1394 static void uri_list_parse_encoded_chars(GList *list)
1400 gchar *text = work->data;
1402 uri_text_decode(text);
1408 GList *uri_list_from_text(gchar *data, gint files_only)
1415 while (data[b] != '\0')
1417 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1418 if (strncmp(data + b, "file:", 5) == 0)
1422 while (data[b] == '/' && data[b+1] == '/') b++;
1423 path = g_strndup(data + b, e - b);
1424 list = g_list_append(list, path_to_utf8(path));
1427 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1429 list = g_list_append(list, g_strndup(data + b, e - b));
1431 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1433 list = g_list_append(list, g_strndup(data + b, e - b));
1435 while (data[e] == '\r' || data[e] == '\n') e++;
1439 uri_list_parse_encoded_chars(list);
1444 GList *uri_filelist_from_text(gchar *data, gint files_only)
1446 GList *path_list = uri_list_from_text(data, files_only);
1447 GList *filelist = filelist_from_path_list(path_list);
1448 string_list_free(path_list);
1452 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1454 gchar *uri_text = NULL;
1464 string = g_string_new("");
1469 const gchar *name8; /* dnd filenames are in utf-8 */
1477 escaped = uri_text_escape(name8);
1478 g_string_append(string, "file:");
1479 g_string_append(string, escaped);
1482 g_string_append(string, "\r\n");
1486 g_string_append(string, name8);
1487 if (work->next) g_string_append(string, "\n");
1493 uri_text = string->str;
1494 if (len) *len = string->len;
1495 g_string_free(string, FALSE);
1500 gchar *uri_text_from_filelist(GList *list, gint *len, gint plain_text)
1502 GList *path_list = filelist_to_path_list(list);
1503 gchar *ret = uri_text_from_list(path_list, len, plain_text);
1504 string_list_free(path_list);