2 * (SLIK) SimpLIstic sKin functions
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
23 #include <gdk/gdkkeysyms.h> /* for key values */
28 #include "ui_bookmark.h"
30 #include "ui_fileops.h"
33 #include "ui_utildlg.h"
34 #include "ui_tabcomp.h"
38 *-----------------------------------------------------------------------------
40 *-----------------------------------------------------------------------------
43 #define HISTORY_DEFAULT_KEY_COUNT 16
46 typedef struct _HistoryData HistoryData;
53 static GList *history_list = NULL;
56 static gchar *quoted_from_text(const gchar *text)
60 gint l = strlen(text);
62 if (l == 0) return NULL;
64 while (c < l && text[c] !='"') c++;
71 while (e < l && text[e] !='"') e++;
76 return g_strndup(ptr, e - c);
83 gint history_list_load(const gchar *path)
90 pathl = path_from_utf8(path);
91 f = fopen(pathl, "r");
95 /* first line must start with History comment */
96 if (!fgets(s_buf,1024,f) ||
97 strncmp(s_buf, "#History", 8) != 0)
103 while (fgets(s_buf,1024,f))
105 if (s_buf[0]=='#') continue;
113 while(ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
116 key = g_strndup(ptr, c);
122 value = quoted_from_text(s_buf);
125 history_list_add_to_key(key, value, 0);
138 gint history_list_save(const gchar *path)
144 pathl = path_from_utf8(path);
145 f = fopen(pathl, "w");
151 buf = g_strdup_printf(_("Unable to write history lists to: %s\n"), path);
158 fprintf(f, "#History lists\n");
161 list = g_list_last(history_list);
170 fprintf(f, "[%s]\n", hd->key);
172 /* save them inverted (oldest to newest)
173 * so that when reading they are added correctly
175 work = g_list_last(hd->list);
178 fprintf(f, "\"%s\"\n", (gchar *)work->data);
184 fprintf(f, "#end\n");
191 static void history_list_free(HistoryData *hd)
208 static HistoryData *history_list_find_by_key(const gchar* key)
210 GList *work = history_list;
212 if (!key) return NULL;
216 HistoryData *hd = work->data;
217 if (strcmp(hd->key, key) == 0) return hd;
223 const gchar *history_list_find_last_path_by_key(const gchar* key)
227 hd = history_list_find_by_key(key);
228 if (!hd || !hd->list) return NULL;
230 return hd->list->data;
233 void history_list_free_key(const gchar *key)
236 hd = history_list_find_by_key(key);
239 history_list = g_list_remove(history_list, hd);
240 history_list_free(hd);
243 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
248 if (!key || !path) return;
250 hd = history_list_find_by_key(key);
253 hd = g_new(HistoryData, 1);
254 hd->key = g_strdup(key);
256 history_list = g_list_prepend(history_list, hd);
259 /* if already in the list, simply move it to the top */
263 gchar *buf = work->data;
265 if (strcmp(buf, path) == 0)
267 hd->list = g_list_remove(hd->list, buf);
268 hd->list = g_list_prepend(hd->list, buf);
273 hd->list = g_list_prepend(hd->list, g_strdup(path));
275 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
278 while(hd->list && g_list_length(hd->list) > max)
280 GList *work = g_list_last(hd->list);
281 gchar *buf = work->data;
282 hd->list = g_list_remove(hd->list, buf);
288 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
293 if (!oldpath) return;
294 hd = history_list_find_by_key(key);
300 gchar *buf = work->data;
301 if (strcmp(buf, oldpath) == 0)
305 work->data = g_strdup(newpath);
309 hd->list = g_list_remove(hd->list, buf);
318 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
325 hd = history_list_find_by_key(key);
331 gchar *buf = work->data;
332 if (strcmp(buf, path) == 0)
336 hd->list = g_list_remove(hd->list, buf);
337 hd->list = g_list_insert(hd->list, buf, p);
345 void history_list_item_remove(const gchar *key, const gchar *path)
347 history_list_item_change(key, path, NULL);
350 GList *history_list_get_by_key(const gchar *key)
354 hd = history_list_find_by_key(key);
355 if (!hd) return NULL;
361 *-----------------------------------------------------------------------------
363 *-----------------------------------------------------------------------------
366 #define BOOKMARK_DATA_KEY "bookmarkdata"
367 #define MARKER_PATH "[path]"
368 #define MARKER_ICON "[icon]"
370 typedef struct _BookMarkData BookMarkData;
371 typedef struct _BookButtonData BookButtonData;
372 typedef struct _BookPropData BookPropData;
380 void (*select_func)(const gchar *path, gpointer data);
381 gpointer select_data;
386 BookButtonData *active_button;
389 struct _BookButtonData
404 GtkWidget *name_entry;
405 GtkWidget *path_entry;
406 GtkWidget *icon_entry;
417 static GtkTargetEntry bookmark_drop_types[] = {
418 { "text/uri-list", 0, TARGET_URI_LIST },
419 { "x-url/http", 0, TARGET_X_URL },
420 { "_NETSCAPE_URL", 0, TARGET_X_URL }
422 #define bookmark_drop_types_n 3
424 static GtkTargetEntry bookmark_drag_types[] = {
425 { "text/uri-list", 0, TARGET_URI_LIST },
426 { "text/plain", 0, TARGET_TEXT_PLAIN }
428 #define bookmark_drag_types_n 2
431 static GList *bookmark_widget_list = NULL;
432 static GList *bookmark_default_list = NULL;
435 static void bookmark_populate_all(const gchar *key);
438 static BookButtonData *bookmark_from_string(const gchar *text)
441 const gchar *path_ptr;
442 const gchar *icon_ptr;
444 b = g_new0(BookButtonData, 1);
448 b->name = g_strdup(_("New Bookmark"));
449 b->path = g_strdup(homedir());
454 b->key = g_strdup(text);
456 path_ptr = strstr(text, MARKER_PATH);
457 icon_ptr = strstr(text, MARKER_ICON);
459 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
461 printf("warning, bookmark icon must be after path\n");
470 b->name = g_strndup(text, l);
471 path_ptr += strlen(MARKER_PATH);
474 l = icon_ptr - path_ptr;
475 b->path = g_strndup(path_ptr, l);
479 b->path = g_strdup(path_ptr);
484 b->name = g_strdup(text);
485 b->path = g_strdup("");
490 icon_ptr += strlen(MARKER_ICON);
491 b->icon = g_strdup(icon_ptr);
497 static void bookmark_free(BookButtonData *b)
509 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
511 if (!name) name = _("New Bookmark");
512 if (icon && strncmp(icon, "/", 1) != 0) icon = NULL;
516 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
519 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
522 static void bookmark_select_cb(GtkWidget *button, gpointer data)
524 BookMarkData *bm = data;
527 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
530 if (bm->select_func) bm->select_func(b->path, bm->select_data);
533 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
535 BookPropData *p = data;
537 bookmark_free(p->bb);
541 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
545 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
547 BookPropData *p = data;
553 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
554 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
555 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
557 new = bookmark_string(name, path, icon);
561 history_list_item_change(p->bb->parent, p->bb->key, new);
565 history_list_add_to_key(p->bb->parent, new, 0);
568 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
569 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
574 bookmark_populate_all(p->bb->parent);
577 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
579 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
587 if (!key) key = "bookmarks";
589 p = g_new0(BookPropData, 1);
591 p->bb = bookmark_from_string(text);
592 p->bb->parent = g_strdup(key);
594 gd = generic_dialog_new(_("Edit Bookmark"), GQ_WMCLASS, "bookmark_edit",
596 bookmark_edit_cancel_cb, p);
597 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
598 G_CALLBACK(bookmark_edit_destroy_cb), p);
600 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
602 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
603 bookmark_edit_ok_cb, TRUE);
605 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
606 pref_table_label(table, 0, 0, _("Name:"), 1.0);
608 p->name_entry = gtk_entry_new();
609 gtk_widget_set_size_request(p->name_entry, 300, -1);
610 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
611 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
612 generic_dialog_attach_default(gd, p->name_entry);
613 gtk_widget_show(p->name_entry);
615 pref_table_label(table, 0, 1, _("Path:"), 1.0);
617 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
618 "bookmark_path", -1, NULL, NULL);
619 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
620 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
621 generic_dialog_attach_default(gd, p->path_entry);
622 gtk_widget_show(label);
624 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
627 if (!icon) icon = "";
628 label = tab_completion_new_with_history(&p->icon_entry, icon,
629 "bookmark_icons", -1, NULL, NULL);
630 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
631 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
632 generic_dialog_attach_default(gd, p->icon_entry);
633 gtk_widget_show(label);
635 gtk_widget_show(gd->dialog);
638 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
645 if (!bm->editable) return;
647 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
650 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
651 p = g_list_index(list, button);
654 if (p < 0 || p + direction < 0) return;
656 key_holder = bm->key;
657 bm->key = "_TEMPHOLDER";
658 history_list_item_move(key_holder, b->key, -direction);
659 bookmark_populate_all(key_holder);
660 bm->key = key_holder;
662 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
665 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
667 BookMarkData *bm = data;
669 if (!bm->active_button) return;
671 bookmark_edit(bm->key, bm->active_button->key, widget);
674 static void bookmark_menu_move(BookMarkData *bm, gint direction)
676 if (!bm->active_button) return;
678 bookmark_move(bm, bm->active_button->button, direction);
681 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
683 bookmark_menu_move(data, -1);
686 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
688 bookmark_menu_move(data, 1);
691 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
693 BookMarkData *bm = data;
695 if (!bm->active_button) return;
697 history_list_item_remove(bm->key, bm->active_button->key);
698 bookmark_populate_all(bm->key);
701 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
703 GtkWidget *button = data;
705 gdk_window_get_origin(button->window, x, y);
706 *y += button->allocation.y + button->allocation.height;
709 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
710 gint button_n, guint32 time, gint local)
715 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
718 bm->active_button = b;
720 menu = popup_menu_short_lived();
721 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
722 G_CALLBACK(bookmark_menu_prop_cb), bm);
723 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
724 G_CALLBACK(bookmark_menu_up_cb), bm);
725 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
726 G_CALLBACK(bookmark_menu_down_cb), bm);
727 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
728 G_CALLBACK(bookmark_menu_remove_cb), bm);
732 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
733 bookmark_menu_position_cb, button, button_n, time);
737 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
741 static gint bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
743 BookMarkData *bm = data;
745 if (event->button != 3) return FALSE;
747 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
752 static gint bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
754 BookMarkData *bm = data;
756 switch (event->keyval)
759 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
761 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
765 if (event->state & GDK_SHIFT_MASK)
767 bookmark_move(bm, button, -1);
772 if (event->state & GDK_SHIFT_MASK)
774 bookmark_move(bm, button, 1);
783 static void bookmark_drag_set_data(GtkWidget *button,
784 GdkDragContext *context, GtkSelectionData *selection_data,
785 guint info, guint time, gpointer data)
787 BookMarkData *bm = data;
789 gchar *uri_text = NULL;
793 if (context->dest_window == bm->widget->window) return;
795 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
798 list = g_list_append(list, b->path);
802 case TARGET_URI_LIST:
803 uri_text = uri_text_from_list(list, &length, FALSE);
805 case TARGET_TEXT_PLAIN:
806 uri_text = uri_text_from_list(list, &length, TRUE);
812 if (!uri_text) return;
814 gtk_selection_data_set(selection_data, selection_data->target,
815 8, (guchar *)uri_text, length);
819 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
822 GdkModifierType mask;
825 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
826 button->allocation.width, button->allocation.height);
827 gdk_pixbuf_get_from_drawable(pixbuf, button->window, NULL,
828 button->allocation.x, button->allocation.y,
829 0, 0, button->allocation.width, button->allocation.height);
831 gdk_window_get_pointer(button->window, &x, &y, &mask);
833 gtk_drag_set_icon_pixbuf(context, pixbuf,
834 x - button->allocation.x, y - button->allocation.y);
835 g_object_unref(pixbuf);
838 static void bookmark_populate(BookMarkData *bm)
844 box = GTK_BOX(bm->box);
845 children = gtk_container_get_children(GTK_CONTAINER(box));
849 GtkWidget *widget = GTK_WIDGET(work->data);
851 gtk_widget_destroy(widget);
854 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
859 if (!bookmark_default_list)
861 buf = bookmark_string(_("Home"), homedir(), NULL);
862 history_list_add_to_key(bm->key, buf, 0);
865 path = concat_dir_and_file(homedir(), "Desktop");
868 buf = bookmark_string(_("Desktop"), path, NULL);
869 history_list_add_to_key(bm->key, buf, 0);
875 work = bookmark_default_list;
876 while (work && work->next)
885 buf = bookmark_string(name, path, NULL);
886 history_list_add_to_key(bm->key, buf, 0);
891 work = history_list_get_by_key(bm->key);
892 work = g_list_last(work);
897 b = bookmark_from_string(work->data);
902 b->button = gtk_button_new();
903 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
904 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
905 gtk_widget_show(b->button);
907 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
908 b, (GDestroyNotify)bookmark_free);
910 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
911 gtk_container_add(GTK_CONTAINER(b->button), box);
912 gtk_widget_show(box);
919 iconl = path_from_utf8(b->icon);
920 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
928 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
930 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
931 GDK_INTERP_BILINEAR);
932 b->image = gtk_image_new_from_pixbuf(scaled);
933 g_object_unref(scaled);
934 g_object_unref(pixbuf);
938 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
939 GTK_ICON_SIZE_BUTTON);
944 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
946 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
947 gtk_widget_show(b->image);
949 b->label = gtk_label_new(b->name);
950 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
951 gtk_widget_show(b->label);
953 g_signal_connect(G_OBJECT(b->button), "clicked",
954 G_CALLBACK(bookmark_select_cb), bm);
955 g_signal_connect(G_OBJECT(b->button), "button_press_event",
956 G_CALLBACK(bookmark_press_cb), bm);
957 g_signal_connect(G_OBJECT(b->button), "key_press_event",
958 G_CALLBACK(bookmark_keypress_cb), bm);
960 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
961 bookmark_drag_types, bookmark_drag_types_n,
962 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
963 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
964 G_CALLBACK(bookmark_drag_set_data), bm);
965 g_signal_connect(G_OBJECT(b->button), "drag_begin",
966 G_CALLBACK(bookmark_drag_begin), bm);
973 static void bookmark_populate_all(const gchar *key)
979 work = bookmark_widget_list;
987 if (strcmp(bm->key, key) == 0)
989 bookmark_populate(bm);
994 static void bookmark_dnd_get_data(GtkWidget *widget,
995 GdkDragContext *context, gint x, gint y,
996 GtkSelectionData *selection_data, guint info,
997 guint time, gpointer data)
999 BookMarkData *bm = data;
1003 if (!bm->editable) return;
1007 case TARGET_URI_LIST:
1009 list = uri_list_from_text((gchar *)selection_data->data, FALSE);
1016 gchar *path = work->data;
1019 buf = bookmark_string(filename_from_path(path), path, NULL);
1020 history_list_add_to_key(bm->key, buf, 0);
1026 string_list_free(list);
1028 bookmark_populate_all(bm->key);
1031 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
1033 BookMarkData *bm = data;
1035 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
1041 GtkWidget *bookmark_list_new(const gchar *key,
1042 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
1044 GtkWidget *scrolled;
1047 if (!key) key = "bookmarks";
1049 bm = g_new0(BookMarkData, 1);
1050 bm->key = g_strdup(key);
1052 bm->select_func = select_func;
1053 bm->select_data = select_data;
1055 bm->no_defaults = FALSE;
1056 bm->editable = TRUE;
1058 scrolled = gtk_scrolled_window_new(NULL, NULL);
1059 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1060 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1062 bm->box = gtk_vbox_new(FALSE, 0);
1063 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1064 gtk_widget_show(bm->box);
1066 bookmark_populate(bm);
1068 g_signal_connect(G_OBJECT(bm->box), "destroy",
1069 G_CALLBACK(bookmark_list_destroy), bm);
1070 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1071 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1072 bm->widget = scrolled;
1074 gtk_drag_dest_set(scrolled,
1075 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1076 bookmark_drop_types, bookmark_drop_types_n,
1077 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1078 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1079 G_CALLBACK(bookmark_dnd_get_data), bm);
1081 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1086 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1090 if (!list || !key) return;
1092 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1095 if (bm->key && strcmp(bm->key, key) == 0) return;
1098 bm->key = g_strdup(key);
1100 bookmark_populate(bm);
1103 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1107 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1110 bm->no_defaults = no_defaults;
1113 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1117 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1120 bm->editable = editable;
1123 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1128 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1131 buf = bookmark_string(name, path, NULL);
1132 history_list_add_to_key(bm->key, buf, 0);
1135 bookmark_populate_all(bm->key);
1138 void bookmark_add_default(const gchar *name, const gchar *path)
1140 if (!name || !path) return;
1141 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1142 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1146 *-----------------------------------------------------------------------------
1147 * combo with history key
1148 *-----------------------------------------------------------------------------
1151 typedef struct _HistoryComboData HistoryComboData;
1152 struct _HistoryComboData
1157 gint history_levels;
1160 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1162 HistoryComboData *hc = data;
1164 g_free(hc->history_key);
1168 /* if text is NULL, entry is set to the most recent item */
1169 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1170 const gchar *history_key, gint max_levels)
1172 HistoryComboData *hc;
1176 hc = g_new0(HistoryComboData, 1);
1177 hc->history_key = g_strdup(history_key);
1178 hc->history_levels = max_levels;
1180 hc->combo = gtk_combo_box_entry_new_text();
1182 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1183 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1186 hc->entry = GTK_BIN(hc->combo)->child;
1188 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1189 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1190 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1191 G_CALLBACK(history_combo_destroy), hc);
1193 work = history_list_get_by_key(hc->history_key);
1196 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1203 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1207 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1210 if (entry) *entry = hc->entry;
1214 /* if text is NULL, current entry text is used
1215 * widget can be the combo or entry widget
1217 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1219 HistoryComboData *hc;
1222 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1225 printf("widget is not a history combo\n");
1231 new_text = g_strdup(text);
1235 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1238 if (new_text && strlen(new_text) > 0)
1240 GtkTreeModel *store;
1243 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1245 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1247 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1248 gtk_list_store_clear(GTK_LIST_STORE(store));
1250 work = history_list_get_by_key(hc->history_key);
1253 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1262 *-----------------------------------------------------------------------------
1263 * drag and drop uri utils
1264 *-----------------------------------------------------------------------------
1267 /* the following characters are allowed to be unencoded for pathnames:
1270 static gint escape_char_list[] = {
1271 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1272 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1273 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1274 /* spc ! " # $ % & ' */
1275 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1276 /* ( ) * + , - . / 0 1 */
1277 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1278 /* 2 3 4 5 6 7 8 9 : ; */
1279 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1280 /* < = > ? @ A B C D E */
1281 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1284 /* Z [ \ ] ^ _ ` a b c */
1285 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1286 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1288 /* x y z { | } ~ del */
1289 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1292 static gchar *hex_char = "0123456789ABCDEF";
1294 static gint escape_test(guchar c)
1296 if (c < 32 || c > 127) return TRUE;
1297 return (escape_char_list[c] != 0);
1300 static const gchar *escape_code(guchar c)
1302 static gchar text[4];
1305 text[1] = hex_char[c>>4];
1306 text[2] = hex_char[c%16];
1312 gchar *uri_text_escape(const gchar *text)
1318 if (!text) return NULL;
1320 string = g_string_new("");
1325 if (escape_test(*p))
1327 g_string_append(string, escape_code(*p));
1331 g_string_append_c(string, *p);
1336 result = string->str;
1337 g_string_free(string, FALSE);
1339 /* dropped filenames are expected to be utf-8 compatible */
1340 if (!g_utf8_validate(result, -1, NULL))
1344 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1355 /* this operates on the passed string, decoding escaped characters */
1356 void uri_text_decode(gchar *text)
1358 if (strchr(text, '%'))
1367 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1377 n = (gint)strtol(t, NULL, 16);
1378 if (n > 0 && n < 256)
1384 /* invalid number, rewind and ignore this escape */
1396 if (*w != '\0') *w = '\0';
1400 static void uri_list_parse_encoded_chars(GList *list)
1406 gchar *text = work->data;
1408 uri_text_decode(text);
1414 GList *uri_list_from_text(gchar *data, gint files_only)
1421 while (data[b] != '\0')
1423 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1424 if (strncmp(data + b, "file:", 5) == 0)
1428 while (data[b] == '/' && data[b+1] == '/') b++;
1429 path = g_strndup(data + b, e - b);
1430 list = g_list_append(list, path_to_utf8(path));
1433 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1435 list = g_list_append(list, g_strndup(data + b, e - b));
1437 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1439 list = g_list_append(list, g_strndup(data + b, e - b));
1441 while (data[e] == '\r' || data[e] == '\n') e++;
1445 uri_list_parse_encoded_chars(list);
1450 GList *uri_filelist_from_text(gchar *data, gint files_only)
1452 GList *path_list = uri_list_from_text(data, files_only);
1453 GList *filelist = filelist_from_path_list(path_list);
1454 string_list_free(path_list);
1458 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1460 gchar *uri_text = NULL;
1470 string = g_string_new("");
1475 const gchar *name8; /* dnd filenames are in utf-8 */
1483 escaped = uri_text_escape(name8);
1484 g_string_append(string, "file:");
1485 g_string_append(string, escaped);
1488 g_string_append(string, "\r\n");
1492 g_string_append(string, name8);
1493 if (work->next) g_string_append(string, "\n");
1499 uri_text = string->str;
1500 if (len) *len = string->len;
1501 g_string_free(string, FALSE);
1506 gchar *uri_text_from_filelist(GList *list, gint *len, gint plain_text)
1508 GList *path_list = filelist_to_path_list(list);
1509 gchar *ret = uri_text_from_list(path_list, len, plain_text);
1510 string_list_free(path_list);