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 */
25 #include "ui_bookmark.h"
27 #include "ui_fileops.h"
30 #include "ui_utildlg.h"
31 #include "ui_tabcomp.h"
35 *-----------------------------------------------------------------------------
37 *-----------------------------------------------------------------------------
40 #define HISTORY_DEFAULT_KEY_COUNT 16
43 typedef struct _HistoryData HistoryData;
50 static GList *history_list = NULL;
53 static gchar *quoted_from_text(const gchar *text)
57 gint l = strlen(text);
59 if (l == 0) return NULL;
61 while (c < l && text[c] !='"') c++;
68 while (e < l && text[e] !='"') e++;
73 return g_strndup(ptr, e - c);
80 gint history_list_load(const gchar *path)
87 pathl = path_from_utf8(path);
88 f = fopen(pathl, "r");
92 /* first line must start with History comment */
93 if (!fgets(s_buf,1024,f) ||
94 strncmp(s_buf, "#History", 8) != 0)
100 while (fgets(s_buf,1024,f))
102 if (s_buf[0]=='#') continue;
110 while(ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
113 key = g_strndup(ptr, c);
119 value = quoted_from_text(s_buf);
122 history_list_add_to_key(key, value, 0);
135 gint history_list_save(const gchar *path)
141 pathl = path_from_utf8(path);
142 f = fopen(pathl, "w");
148 buf = g_strdup_printf(_("Unable to write history lists to: %s\n"), path);
155 fprintf(f, "#History lists\n");
158 list = g_list_last(history_list);
167 fprintf(f, "[%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);
175 fprintf(f, "\"%s\"\n", (gchar *)work->data);
181 fprintf(f, "#end\n");
188 static void history_list_free(HistoryData *hd)
205 static HistoryData *history_list_find_by_key(const gchar* key)
207 GList *work = history_list;
209 if (!key) return NULL;
213 HistoryData *hd = work->data;
214 if (strcmp(hd->key, key) == 0) return hd;
220 const gchar *history_list_find_last_path_by_key(const gchar* key)
224 hd = history_list_find_by_key(key);
225 if (!hd || !hd->list) return NULL;
227 return hd->list->data;
230 void history_list_free_key(const gchar *key)
233 hd = history_list_find_by_key(key);
236 history_list = g_list_remove(history_list, hd);
237 history_list_free(hd);
240 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
245 if (!key || !path) return;
247 hd = history_list_find_by_key(key);
250 hd = g_new(HistoryData, 1);
251 hd->key = g_strdup(key);
253 history_list = g_list_prepend(history_list, hd);
256 /* if already in the list, simply move it to the top */
260 gchar *buf = work->data;
262 if (strcmp(buf, path) == 0)
264 hd->list = g_list_remove(hd->list, buf);
265 hd->list = g_list_prepend(hd->list, buf);
270 hd->list = g_list_prepend(hd->list, g_strdup(path));
272 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
275 while(hd->list && g_list_length(hd->list) > max)
277 GList *work = g_list_last(hd->list);
278 gchar *buf = work->data;
279 hd->list = g_list_remove(hd->list, buf);
285 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
290 if (!oldpath) return;
291 hd = history_list_find_by_key(key);
297 gchar *buf = work->data;
298 if (strcmp(buf, oldpath) == 0)
302 work->data = g_strdup(newpath);
306 hd->list = g_list_remove(hd->list, buf);
315 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
322 hd = history_list_find_by_key(key);
328 gchar *buf = work->data;
329 if (strcmp(buf, path) == 0)
333 hd->list = g_list_remove(hd->list, buf);
334 hd->list = g_list_insert(hd->list, buf, p);
342 void history_list_item_remove(const gchar *key, const gchar *path)
344 history_list_item_change(key, path, NULL);
347 GList *history_list_get_by_key(const gchar *key)
351 hd = history_list_find_by_key(key);
352 if (!hd) return NULL;
358 *-----------------------------------------------------------------------------
360 *-----------------------------------------------------------------------------
363 #define BOOKMARK_DATA_KEY "bookmarkdata"
364 #define MARKER_PATH "[path]"
365 #define MARKER_ICON "[icon]"
367 typedef struct _BookMarkData BookMarkData;
368 typedef struct _BookButtonData BookButtonData;
369 typedef struct _BookPropData BookPropData;
377 void (*select_func)(const gchar *path, gpointer data);
378 gpointer select_data;
383 BookButtonData *active_button;
386 struct _BookButtonData
401 GtkWidget *name_entry;
402 GtkWidget *path_entry;
403 GtkWidget *icon_entry;
414 static GtkTargetEntry bookmark_drop_types[] = {
415 { "text/uri-list", 0, TARGET_URI_LIST },
416 { "x-url/http", 0, TARGET_X_URL },
417 { "_NETSCAPE_URL", 0, TARGET_X_URL }
419 #define bookmark_drop_types_n 3
421 static GtkTargetEntry bookmark_drag_types[] = {
422 { "text/uri-list", 0, TARGET_URI_LIST },
423 { "text/plain", 0, TARGET_TEXT_PLAIN }
425 #define bookmark_drag_types_n 2
428 static GList *bookmark_widget_list = NULL;
429 static GList *bookmark_default_list = NULL;
432 static void bookmark_populate_all(const gchar *key);
435 static BookButtonData *bookmark_from_string(const gchar *text)
438 const gchar *path_ptr;
439 const gchar *icon_ptr;
441 b = g_new0(BookButtonData, 1);
445 b->name = g_strdup(_("New Bookmark"));
446 b->path = g_strdup(homedir());
451 b->key = g_strdup(text);
453 path_ptr = strstr(text, MARKER_PATH);
454 icon_ptr = strstr(text, MARKER_ICON);
456 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
458 printf("warning, bookmark icon must be after path\n");
467 b->name = g_strndup(text, l);
468 path_ptr += strlen(MARKER_PATH);
471 l = icon_ptr - path_ptr;
472 b->path = g_strndup(path_ptr, l);
476 b->path = g_strdup(path_ptr);
481 b->name = g_strdup(text);
482 b->path = g_strdup("");
487 icon_ptr += strlen(MARKER_ICON);
488 b->icon = g_strdup(icon_ptr);
494 static void bookmark_free(BookButtonData *b)
506 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
508 if (!name) name = _("New Bookmark");
509 if (icon && strncmp(icon, "/", 1) != 0) icon = NULL;
513 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
516 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
519 static void bookmark_select_cb(GtkWidget *button, gpointer data)
521 BookMarkData *bm = data;
524 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
527 if (bm->select_func) bm->select_func(b->path, bm->select_data);
530 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
532 BookPropData *p = data;
534 bookmark_free(p->bb);
538 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
542 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
544 BookPropData *p = data;
550 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
551 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
552 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
554 new = bookmark_string(name, path, icon);
558 history_list_item_change(p->bb->parent, p->bb->key, new);
562 history_list_add_to_key(p->bb->parent, new, 0);
565 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
566 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
571 bookmark_populate_all(p->bb->parent);
574 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
576 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
584 if (!key) key = "bookmarks";
586 p = g_new0(BookPropData, 1);
588 p->bb = bookmark_from_string(text);
589 p->bb->parent = g_strdup(key);
591 gd = generic_dialog_new(_("Edit Bookmark"), PACKAGE, "bookmark_edit",
593 bookmark_edit_cancel_cb, p);
594 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
595 G_CALLBACK(bookmark_edit_destroy_cb), p);
597 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
599 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
600 bookmark_edit_ok_cb, TRUE);
602 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
603 pref_table_label(table, 0, 0, _("Name:"), 1.0);
605 p->name_entry = gtk_entry_new();
606 gtk_widget_set_size_request(p->name_entry, 300, -1);
607 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
608 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
609 generic_dialog_attach_default(gd, p->name_entry);
610 gtk_widget_show(p->name_entry);
612 pref_table_label(table, 0, 1, _("Path:"), 1.0);
614 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
615 "bookmark_path", -1, NULL, NULL);
616 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
617 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
618 generic_dialog_attach_default(gd, p->path_entry);
619 gtk_widget_show(label);
621 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
624 if (!icon) icon = "";
625 label = tab_completion_new_with_history(&p->icon_entry, icon,
626 "bookmark_icons", -1, NULL, NULL);
627 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
628 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
629 generic_dialog_attach_default(gd, p->icon_entry);
630 gtk_widget_show(label);
632 gtk_widget_show(gd->dialog);
635 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
642 if (!bm->editable) return;
644 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
647 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
648 p = g_list_index(list, button);
651 if (p < 0 || p + direction < 0) return;
653 key_holder = bm->key;
654 bm->key = "_TEMPHOLDER";
655 history_list_item_move(key_holder, b->key, -direction);
656 bookmark_populate_all(key_holder);
657 bm->key = key_holder;
659 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
662 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
664 BookMarkData *bm = data;
666 if (!bm->active_button) return;
668 bookmark_edit(bm->key, bm->active_button->key, widget);
671 static void bookmark_menu_move(BookMarkData *bm, gint direction)
673 if (!bm->active_button) return;
675 bookmark_move(bm, bm->active_button->button, direction);
678 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
680 bookmark_menu_move(data, -1);
683 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
685 bookmark_menu_move(data, 1);
688 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
690 BookMarkData *bm = data;
692 if (!bm->active_button) return;
694 history_list_item_remove(bm->key, bm->active_button->key);
695 bookmark_populate_all(bm->key);
698 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
700 GtkWidget *button = data;
702 gdk_window_get_origin(button->window, x, y);
703 *y += button->allocation.y + button->allocation.height;
706 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
707 gint button_n, guint32 time, gint local)
712 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
715 bm->active_button = b;
717 menu = popup_menu_short_lived();
718 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
719 G_CALLBACK(bookmark_menu_prop_cb), bm);
720 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
721 G_CALLBACK(bookmark_menu_up_cb), bm);
722 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
723 G_CALLBACK(bookmark_menu_down_cb), bm);
724 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
725 G_CALLBACK(bookmark_menu_remove_cb), bm);
729 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
730 bookmark_menu_position_cb, button, button_n, time);
734 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
738 static gint bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
740 BookMarkData *bm = data;
742 if (event->button != 3) return FALSE;
744 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
749 static gint bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
751 BookMarkData *bm = data;
753 switch (event->keyval)
756 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
758 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
762 if (event->state & GDK_SHIFT_MASK)
764 bookmark_move(bm, button, -1);
769 if (event->state & GDK_SHIFT_MASK)
771 bookmark_move(bm, button, 1);
780 static void bookmark_drag_set_data(GtkWidget *button,
781 GdkDragContext *context, GtkSelectionData *selection_data,
782 guint info, guint time, gpointer data)
784 BookMarkData *bm = data;
786 gchar *uri_text = NULL;
790 if (context->dest_window == bm->widget->window) return;
792 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
795 list = g_list_append(list, b->path);
799 case TARGET_URI_LIST:
800 uri_text = uri_text_from_list(list, &length, FALSE);
802 case TARGET_TEXT_PLAIN:
803 uri_text = uri_text_from_list(list, &length, TRUE);
809 if (!uri_text) return;
811 gtk_selection_data_set(selection_data, selection_data->target,
812 8, uri_text, length);
816 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
819 GdkModifierType mask;
822 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
823 button->allocation.width, button->allocation.height);
824 gdk_pixbuf_get_from_drawable(pixbuf, button->window, NULL,
825 button->allocation.x, button->allocation.y,
826 0, 0, button->allocation.width, button->allocation.height);
828 gdk_window_get_pointer(button->window, &x, &y, &mask);
830 gtk_drag_set_icon_pixbuf(context, pixbuf,
831 x - button->allocation.x, y - button->allocation.y);
832 g_object_unref(pixbuf);
835 static void bookmark_populate(BookMarkData *bm)
841 box = GTK_BOX(bm->box);
842 children = gtk_container_get_children(GTK_CONTAINER(box));
846 GtkWidget *widget = GTK_WIDGET(work->data);
848 gtk_widget_destroy(widget);
851 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
856 if (!bookmark_default_list)
858 buf = bookmark_string(_("Home"), homedir(), NULL);
859 history_list_add_to_key(bm->key, buf, 0);
862 path = concat_dir_and_file(homedir(), "Desktop");
865 buf = bookmark_string(_("Desktop"), path, NULL);
866 history_list_add_to_key(bm->key, buf, 0);
872 work = bookmark_default_list;
873 while (work && work->next)
882 buf = bookmark_string(name, path, NULL);
883 history_list_add_to_key(bm->key, buf, 0);
888 work = history_list_get_by_key(bm->key);
889 work = g_list_last(work);
894 b = bookmark_from_string(work->data);
899 b->button = gtk_button_new();
900 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
901 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
902 gtk_widget_show(b->button);
904 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
905 b, (GDestroyNotify)bookmark_free);
907 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
908 gtk_container_add(GTK_CONTAINER(b->button), box);
909 gtk_widget_show(box);
916 iconl = path_from_utf8(b->icon);
917 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
925 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
927 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
928 GDK_INTERP_BILINEAR);
929 b->image = gtk_image_new_from_pixbuf(scaled);
930 g_object_unref(scaled);
931 g_object_unref(pixbuf);
935 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
936 GTK_ICON_SIZE_BUTTON);
941 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
943 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
944 gtk_widget_show(b->image);
946 b->label = gtk_label_new(b->name);
947 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
948 gtk_widget_show(b->label);
950 g_signal_connect(G_OBJECT(b->button), "clicked",
951 G_CALLBACK(bookmark_select_cb), bm);
952 g_signal_connect(G_OBJECT(b->button), "button_press_event",
953 G_CALLBACK(bookmark_press_cb), bm);
954 g_signal_connect(G_OBJECT(b->button), "key_press_event",
955 G_CALLBACK(bookmark_keypress_cb), bm);
957 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
958 bookmark_drag_types, bookmark_drag_types_n,
959 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
960 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
961 G_CALLBACK(bookmark_drag_set_data), bm);
962 g_signal_connect(G_OBJECT(b->button), "drag_begin",
963 G_CALLBACK(bookmark_drag_begin), bm);
970 static void bookmark_populate_all(const gchar *key)
976 work = bookmark_widget_list;
984 if (strcmp(bm->key, key) == 0)
986 bookmark_populate(bm);
991 static void bookmark_dnd_get_data(GtkWidget *widget,
992 GdkDragContext *context, gint x, gint y,
993 GtkSelectionData *selection_data, guint info,
994 guint time, gpointer data)
996 BookMarkData *bm = data;
1000 if (!bm->editable) return;
1004 case TARGET_URI_LIST:
1006 list = uri_list_from_text(selection_data->data, FALSE);
1013 gchar *path = work->data;
1016 buf = bookmark_string(filename_from_path(path), path, NULL);
1017 history_list_add_to_key(bm->key, buf, 0);
1023 path_list_free(list);
1025 bookmark_populate_all(bm->key);
1028 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
1030 BookMarkData *bm = data;
1032 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
1038 GtkWidget *bookmark_list_new(const gchar *key,
1039 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
1041 GtkWidget *scrolled;
1044 if (!key) key = "bookmarks";
1046 bm = g_new0(BookMarkData, 1);
1047 bm->key = g_strdup(key);
1049 bm->select_func = select_func;
1050 bm->select_data = select_data;
1052 bm->no_defaults = FALSE;
1053 bm->editable = TRUE;
1055 scrolled = gtk_scrolled_window_new(NULL, NULL);
1056 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
1057 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1059 bm->box = gtk_vbox_new(FALSE, 0);
1060 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
1061 gtk_widget_show(bm->box);
1063 bookmark_populate(bm);
1065 g_signal_connect(G_OBJECT(bm->box), "destroy",
1066 G_CALLBACK(bookmark_list_destroy), bm);
1067 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
1068 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
1069 bm->widget = scrolled;
1071 gtk_drag_dest_set(scrolled,
1072 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
1073 bookmark_drop_types, bookmark_drop_types_n,
1074 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
1075 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
1076 G_CALLBACK(bookmark_dnd_get_data), bm);
1078 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
1083 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
1087 if (!list || !key) return;
1089 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1092 if (bm->key && strcmp(bm->key, key) == 0) return;
1095 bm->key = g_strdup(key);
1097 bookmark_populate(bm);
1100 void bookmark_list_set_no_defaults(GtkWidget *list, gint no_defaults)
1104 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1107 bm->no_defaults = no_defaults;
1110 void bookmark_list_set_editable(GtkWidget *list, gint editable)
1114 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1117 bm->editable = editable;
1120 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
1125 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
1128 buf = bookmark_string(name, path, NULL);
1129 history_list_add_to_key(bm->key, buf, 0);
1132 bookmark_populate_all(bm->key);
1135 void bookmark_add_default(const gchar *name, const gchar *path)
1137 if (!name || !path) return;
1138 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
1139 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
1143 *-----------------------------------------------------------------------------
1144 * combo with history key
1145 *-----------------------------------------------------------------------------
1148 typedef struct _HistoryComboData HistoryComboData;
1149 struct _HistoryComboData
1154 gint history_levels;
1157 static void history_combo_destroy(GtkWidget *widget, gpointer data)
1159 HistoryComboData *hc = data;
1161 g_free(hc->history_key);
1165 /* if text is NULL, entry is set to the most recent item */
1166 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
1167 const gchar *history_key, gint max_levels)
1169 HistoryComboData *hc;
1173 hc = g_new0(HistoryComboData, 1);
1174 hc->history_key = g_strdup(history_key);
1175 hc->history_levels = max_levels;
1177 hc->combo = gtk_combo_box_entry_new_text();
1179 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
1180 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
1183 hc->entry = GTK_BIN(hc->combo)->child;
1185 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
1186 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
1187 g_signal_connect(G_OBJECT(hc->combo), "destroy",
1188 G_CALLBACK(history_combo_destroy), hc);
1190 work = history_list_get_by_key(hc->history_key);
1193 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1200 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1204 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1207 if (entry) *entry = hc->entry;
1211 /* if text is NULL, current entry text is used
1212 * widget can be the combo or entry widget
1214 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1216 HistoryComboData *hc;
1219 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
1222 printf("widget is not a history combo\n");
1228 new_text = g_strdup(text);
1232 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1235 if (new_text && strlen(new_text) > 0)
1237 GtkTreeModel *store;
1240 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1242 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1244 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1245 gtk_list_store_clear(GTK_LIST_STORE(store));
1247 work = history_list_get_by_key(hc->history_key);
1250 gtk_combo_box_append_text(GTK_COMBO_BOX(hc->combo), (gchar *)work->data);
1259 *-----------------------------------------------------------------------------
1260 * drag and drop uri utils
1261 *-----------------------------------------------------------------------------
1264 /* the following characters are allowed to be unencoded for pathnames:
1267 static gint escape_char_list[] = {
1268 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0 */
1269 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 10 */
1270 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 */
1271 /* spc ! " # $ % & ' */
1272 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, /* 30 */
1273 /* ( ) * + , - . / 0 1 */
1274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40 */
1275 /* 2 3 4 5 6 7 8 9 : ; */
1276 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* 50 */
1277 /* < = > ? @ A B C D E */
1278 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, /* 60 */
1279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70 */
1280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */
1281 /* Z [ \ ] ^ _ ` a b c */
1282 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, /* 90 */
1283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100 */
1284 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110 */
1285 /* x y z { | } ~ del */
1286 0, 0, 0, 1, 1, 1, 0, 0 /* 120, 127 is end */
1289 static gchar *hex_char = "0123456789ABCDEF";
1291 static gint escape_test(guchar c)
1293 if (c < 32 || c > 127) return TRUE;
1294 return (escape_char_list[c] != 0);
1297 static const gchar *escape_code(guchar c)
1299 static gchar text[4];
1302 text[1] = hex_char[c>>4];
1303 text[2] = hex_char[c%16];
1309 gchar *uri_text_escape(const gchar *text)
1315 if (!text) return NULL;
1317 string = g_string_new("");
1322 if (escape_test(*p))
1324 g_string_append(string, escape_code(*p));
1328 g_string_append_c(string, *p);
1333 result = string->str;
1334 g_string_free(string, FALSE);
1336 /* dropped filenames are expected to be utf-8 compatible */
1337 if (!g_utf8_validate(result, -1, NULL))
1341 tmp = g_locale_to_utf8(result, -1, NULL, NULL, NULL);
1352 /* this operates on the passed string, decoding escaped characters */
1353 void uri_text_decode(gchar *text)
1355 if (strchr(text, '%'))
1364 if (*r == '%' && *(r + 1) != '\0' && *(r + 2) != '\0')
1374 n = (gint)strtol(t, NULL, 16);
1375 if (n > 0 && n < 256)
1381 /* invalid number, rewind and ignore this escape */
1393 if (*w != '\0') *w = '\0';
1397 static void uri_list_parse_encoded_chars(GList *list)
1403 gchar *text = work->data;
1405 uri_text_decode(text);
1411 GList *uri_list_from_text(gchar *data, gint files_only)
1418 while (data[b] != '\0')
1420 while (data[e] != '\r' && data[e] != '\n' && data[e] != '\0') e++;
1421 if (strncmp(data + b, "file:", 5) == 0)
1425 while (data[b] == '/' && data[b+1] == '/') b++;
1426 path = g_strndup(data + b, e - b);
1427 list = g_list_append(list, path_to_utf8(path));
1430 else if (!files_only && strncmp(data + b, "http:", 5) == 0)
1432 list = g_list_append(list, g_strndup(data + b, e - b));
1434 else if (!files_only && strncmp(data + b, "ftp:", 3) == 0)
1436 list = g_list_append(list, g_strndup(data + b, e - b));
1438 while (data[e] == '\r' || data[e] == '\n') e++;
1442 uri_list_parse_encoded_chars(list);
1447 gchar *uri_text_from_list(GList *list, gint *len, gint plain_text)
1449 gchar *uri_text = NULL;
1459 string = g_string_new("");
1464 const gchar *name8; /* dnd filenames are in utf-8 */
1472 escaped = uri_text_escape(name8);
1473 g_string_append(string, "file:");
1474 g_string_append(string, escaped);
1477 g_string_append(string, "\r\n");
1481 g_string_append(string, name8);
1482 if (work->next) g_string_append(string, "\n");
1488 uri_text = string->str;
1489 if (len) *len = string->len;
1490 g_string_free(string, FALSE);