2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 - 2012 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
24 #include <gdk/gdkkeysyms.h> /* for key values */
29 #include "history_list.h"
31 #include "ui_bookmark.h"
32 #include "ui_fileops.h"
35 #include "ui_utildlg.h"
36 #include "ui_tabcomp.h"
37 #include "uri_utils.h"
42 *-----------------------------------------------------------------------------
44 *-----------------------------------------------------------------------------
47 #define BOOKMARK_DATA_KEY "bookmarkdata"
48 #define MARKER_PATH "[path]"
49 #define MARKER_ICON "[icon]"
51 typedef struct _BookMarkData BookMarkData;
52 typedef struct _BookButtonData BookButtonData;
53 typedef struct _BookPropData BookPropData;
61 void (*select_func)(const gchar *path, gpointer data);
66 gboolean only_directories;
68 BookButtonData *active_button;
71 struct _BookButtonData
86 GtkWidget *name_entry;
87 GtkWidget *path_entry;
88 GtkWidget *icon_entry;
99 static GtkTargetEntry bookmark_drop_types[] = {
100 { "text/uri-list", 0, TARGET_URI_LIST },
101 { "x-url/http", 0, TARGET_X_URL },
102 { "_NETSCAPE_URL", 0, TARGET_X_URL }
104 #define bookmark_drop_types_n 3
106 static GtkTargetEntry bookmark_drag_types[] = {
107 { "text/uri-list", 0, TARGET_URI_LIST },
108 { "text/plain", 0, TARGET_TEXT_PLAIN }
110 #define bookmark_drag_types_n 2
113 static GList *bookmark_widget_list = NULL;
114 static GList *bookmark_default_list = NULL;
117 static void bookmark_populate_all(const gchar *key);
120 static BookButtonData *bookmark_from_string(const gchar *text)
123 const gchar *path_ptr;
124 const gchar *icon_ptr;
126 b = g_new0(BookButtonData, 1);
130 b->name = g_strdup(_("New Bookmark"));
131 b->path = g_strdup(homedir());
136 b->key = g_strdup(text);
138 path_ptr = strstr(text, MARKER_PATH);
139 icon_ptr = strstr(text, MARKER_ICON);
141 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
143 log_printf("warning, bookmark icon must be after path\n");
152 b->name = g_strndup(text, l);
153 path_ptr += strlen(MARKER_PATH);
156 l = icon_ptr - path_ptr;
157 b->path = g_strndup(path_ptr, l);
161 b->path = g_strdup(path_ptr);
166 b->name = g_strdup(text);
167 b->path = g_strdup("");
172 icon_ptr += strlen(MARKER_ICON);
173 b->icon = g_strdup(icon_ptr);
179 static void bookmark_free(BookButtonData *b)
191 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
193 if (!name) name = _("New Bookmark");
194 if (icon && strncmp(icon, G_DIR_SEPARATOR_S, 1) != 0) icon = NULL;
198 return g_strdup_printf("%s"MARKER_PATH"%s"MARKER_ICON"%s", name, path, icon);
201 return g_strdup_printf("%s"MARKER_PATH"%s", name, path);
204 static void bookmark_select_cb(GtkWidget *button, gpointer data)
206 BookMarkData *bm = data;
209 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
212 if (bm->select_func) bm->select_func(b->path, bm->select_data);
215 static void bookmark_edit_destroy_cb(GtkWidget *widget, gpointer data)
217 BookPropData *p = data;
219 bookmark_free(p->bb);
223 static void bookmark_edit_cancel_cb(GenericDialog *gd, gpointer data)
227 static void bookmark_edit_ok_cb(GenericDialog *gd, gpointer data)
229 BookPropData *p = data;
235 name = gtk_entry_get_text(GTK_ENTRY(p->name_entry));
236 path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
237 icon = gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
239 new = bookmark_string(name, path, icon);
243 history_list_item_change(p->bb->parent, p->bb->key, new);
247 history_list_add_to_key(p->bb->parent, new, 0);
250 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
251 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
256 bookmark_populate_all(p->bb->parent);
259 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
261 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
269 if (!key) key = "bookmarks";
271 p = g_new0(BookPropData, 1);
273 p->bb = bookmark_from_string(text);
274 p->bb->parent = g_strdup(key);
276 gd = generic_dialog_new(_("Edit Bookmark"), "bookmark_edit",
278 bookmark_edit_cancel_cb, p);
279 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
280 G_CALLBACK(bookmark_edit_destroy_cb), p);
282 generic_dialog_add_message(gd, NULL, _("Edit Bookmark"), NULL);
284 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
285 bookmark_edit_ok_cb, TRUE);
287 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
288 pref_table_label(table, 0, 0, _("Name:"), 1.0);
290 p->name_entry = gtk_entry_new();
291 gtk_widget_set_size_request(p->name_entry, 300, -1);
292 if (p->bb->name) gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
293 gtk_table_attach_defaults(GTK_TABLE(table), p->name_entry, 1, 2, 0, 1);
294 generic_dialog_attach_default(gd, p->name_entry);
295 gtk_widget_show(p->name_entry);
297 pref_table_label(table, 0, 1, _("Path:"), 1.0);
299 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
300 "bookmark_path", -1, NULL, NULL);
301 tab_completion_add_select_button(p->path_entry, NULL, TRUE);
302 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
303 generic_dialog_attach_default(gd, p->path_entry);
304 gtk_widget_show(label);
306 pref_table_label(table, 0, 2, _("Icon:"), 1.0);
309 if (!icon) icon = "";
310 label = tab_completion_new_with_history(&p->icon_entry, icon,
311 "bookmark_icons", -1, NULL, NULL);
312 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
313 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
314 generic_dialog_attach_default(gd, p->icon_entry);
315 gtk_widget_show(label);
317 gtk_widget_show(gd->dialog);
320 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
327 if (!bm->editable) return;
329 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
332 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
333 p = g_list_index(list, button);
336 if (p < 0 || p + direction < 0) return;
338 key_holder = bm->key;
339 bm->key = "_TEMPHOLDER";
340 history_list_item_move(key_holder, b->key, -direction);
341 bookmark_populate_all(key_holder);
342 bm->key = key_holder;
344 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
347 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
349 BookMarkData *bm = data;
351 if (!bm->active_button) return;
353 bookmark_edit(bm->key, bm->active_button->key, widget);
356 static void bookmark_menu_move(BookMarkData *bm, gint direction)
358 if (!bm->active_button) return;
360 bookmark_move(bm, bm->active_button->button, direction);
363 static void bookmark_menu_up_cb(GtkWidget *widget, gpointer data)
365 bookmark_menu_move(data, -1);
368 static void bookmark_menu_down_cb(GtkWidget *widget, gpointer data)
370 bookmark_menu_move(data, 1);
373 static void bookmark_menu_remove_cb(GtkWidget *widget, gpointer data)
375 BookMarkData *bm = data;
377 if (!bm->active_button) return;
379 history_list_item_remove(bm->key, bm->active_button->key);
380 bookmark_populate_all(bm->key);
383 static void bookmark_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gint *pushed_in, gpointer data)
385 GtkWidget *button = data;
386 GtkAllocation allocation;
388 gtk_widget_set_allocation(button, &allocation);
389 gdk_window_get_origin(gtk_widget_get_window(button), x, y);
390 *y += allocation.y + allocation.height;
393 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button,
394 gint button_n, guint32 time, gboolean local)
399 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
402 bm->active_button = b;
404 menu = popup_menu_short_lived();
405 menu_item_add_stock_sensitive(menu, _("_Properties..."), GTK_STOCK_PROPERTIES, bm->editable,
406 G_CALLBACK(bookmark_menu_prop_cb), bm);
407 menu_item_add_stock_sensitive(menu, _("Move _up"), GTK_STOCK_GO_UP, bm->editable,
408 G_CALLBACK(bookmark_menu_up_cb), bm);
409 menu_item_add_stock_sensitive(menu, _("Move _down"), GTK_STOCK_GO_DOWN, bm->editable,
410 G_CALLBACK(bookmark_menu_down_cb), bm);
411 menu_item_add_stock_sensitive(menu, _("_Remove"), GTK_STOCK_REMOVE, bm->editable,
412 G_CALLBACK(bookmark_menu_remove_cb), bm);
416 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
417 bookmark_menu_position_cb, button, button_n, time);
421 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, button_n, time);
425 static gboolean bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
427 BookMarkData *bm = data;
429 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
431 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
436 static gboolean bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
438 BookMarkData *bm = data;
440 switch (event->keyval)
443 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
445 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
449 if (event->state & GDK_SHIFT_MASK)
451 bookmark_move(bm, button, -1);
456 if (event->state & GDK_SHIFT_MASK)
458 bookmark_move(bm, button, 1);
467 static void bookmark_drag_set_data(GtkWidget *button,
468 GdkDragContext *context, GtkSelectionData *selection_data,
469 guint info, guint time, gpointer data)
471 BookMarkData *bm = data;
473 gchar *uri_text = NULL;
477 // if (context->dest_window == bm->widget->window) return;
479 b = g_object_get_data(G_OBJECT(button), "bookbuttondata");
482 list = g_list_append(list, b->path);
486 case TARGET_URI_LIST:
487 uri_text = uri_text_from_list(list, &length, FALSE);
489 case TARGET_TEXT_PLAIN:
490 uri_text = uri_text_from_list(list, &length, TRUE);
496 if (!uri_text) return;
498 gtk_selection_data_set_text(selection_data, uri_text, length);
502 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer data)
505 GdkModifierType mask;
507 GtkAllocation allocation;
509 gtk_widget_get_allocation(button, &allocation);
511 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
512 allocation.width, allocation.height);
513 #if !GTK_CHECK_VERSION(3,0,0)
514 gdk_pixbuf_get_from_drawable(pixbuf, gtk_widget_get_window(button), NULL,
515 allocation.x, allocation.y,
516 0, 0, allocation.width, allocation.height);
518 gdk_window_get_pointer(gtk_widget_get_window(button), &x, &y, &mask);
520 gtk_drag_set_icon_pixbuf(context, pixbuf,
521 x - allocation.x, y - allocation.y);
522 g_object_unref(pixbuf);
525 static void bookmark_populate(BookMarkData *bm)
531 box = GTK_BOX(bm->box);
532 children = gtk_container_get_children(GTK_CONTAINER(box));
536 GtkWidget *widget = GTK_WIDGET(work->data);
538 gtk_widget_destroy(widget);
541 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
546 if (!bookmark_default_list)
548 buf = bookmark_string(_("Home"), homedir(), NULL);
549 history_list_add_to_key(bm->key, buf, 0);
552 path = g_build_filename(homedir(), "Desktop", NULL);
555 buf = bookmark_string(_("Desktop"), path, NULL);
556 history_list_add_to_key(bm->key, buf, 0);
562 work = bookmark_default_list;
563 while (work && work->next)
572 buf = bookmark_string(name, path, NULL);
573 history_list_add_to_key(bm->key, buf, 0);
578 work = history_list_get_by_key(bm->key);
579 work = g_list_last(work);
584 b = bookmark_from_string(work->data);
589 b->button = gtk_button_new();
590 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
591 gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
592 gtk_widget_show(b->button);
594 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
595 b, (GDestroyNotify)bookmark_free);
597 box = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_GAP);
598 gtk_container_add(GTK_CONTAINER(b->button), box);
599 gtk_widget_show(box);
606 iconl = path_from_utf8(b->icon);
607 pixbuf = gdk_pixbuf_new_from_file(iconl, NULL);
615 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
617 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
618 GDK_INTERP_BILINEAR);
619 b->image = gtk_image_new_from_pixbuf(scaled);
620 g_object_unref(scaled);
621 g_object_unref(pixbuf);
625 b->image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE,
626 GTK_ICON_SIZE_BUTTON);
631 b->image = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
633 gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
634 gtk_widget_show(b->image);
636 b->label = gtk_label_new(b->name);
637 gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
638 gtk_widget_show(b->label);
640 g_signal_connect(G_OBJECT(b->button), "clicked",
641 G_CALLBACK(bookmark_select_cb), bm);
642 g_signal_connect(G_OBJECT(b->button), "button_press_event",
643 G_CALLBACK(bookmark_press_cb), bm);
644 g_signal_connect(G_OBJECT(b->button), "key_press_event",
645 G_CALLBACK(bookmark_keypress_cb), bm);
647 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
648 bookmark_drag_types, bookmark_drag_types_n,
649 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
650 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
651 G_CALLBACK(bookmark_drag_set_data), bm);
652 g_signal_connect(G_OBJECT(b->button), "drag_begin",
653 G_CALLBACK(bookmark_drag_begin), bm);
660 static void bookmark_populate_all(const gchar *key)
666 work = bookmark_widget_list;
674 if (strcmp(bm->key, key) == 0)
676 bookmark_populate(bm);
681 static void bookmark_dnd_get_data(GtkWidget *widget,
682 GdkDragContext *context, gint x, gint y,
683 GtkSelectionData *selection_data, guint info,
684 guint time, gpointer data)
686 BookMarkData *bm = data;
690 if (!bm->editable) return;
694 case TARGET_URI_LIST:
696 list = uri_list_from_text((gchar *)gtk_selection_data_get_data(selection_data), FALSE);
703 gchar *path = work->data;
708 if (bm->only_directories && !isdir(path)) continue;
709 buf = bookmark_string(filename_from_path(path), path, NULL);
710 history_list_add_to_key(bm->key, buf, 0);
714 string_list_free(list);
716 bookmark_populate_all(bm->key);
719 static void bookmark_list_destroy(GtkWidget *widget, gpointer data)
721 BookMarkData *bm = data;
723 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
729 GtkWidget *bookmark_list_new(const gchar *key,
730 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
735 if (!key) key = "bookmarks";
737 bm = g_new0(BookMarkData, 1);
738 bm->key = g_strdup(key);
740 bm->select_func = select_func;
741 bm->select_data = select_data;
743 bm->no_defaults = FALSE;
745 bm->only_directories = FALSE;
747 scrolled = gtk_scrolled_window_new(NULL, NULL);
748 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
749 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
751 bm->box = gtk_vbox_new(FALSE, 0);
752 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolled), bm->box);
753 gtk_widget_show(bm->box);
755 bookmark_populate(bm);
757 g_signal_connect(G_OBJECT(bm->box), "destroy",
758 G_CALLBACK(bookmark_list_destroy), bm);
759 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
760 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
761 bm->widget = scrolled;
763 gtk_drag_dest_set(scrolled,
764 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
765 bookmark_drop_types, bookmark_drop_types_n,
766 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
767 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
768 G_CALLBACK(bookmark_dnd_get_data), bm);
770 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
775 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
779 if (!list || !key) return;
781 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
784 if (bm->key && strcmp(bm->key, key) == 0) return;
787 bm->key = g_strdup(key);
789 bookmark_populate(bm);
792 void bookmark_list_set_no_defaults(GtkWidget *list, gboolean no_defaults)
796 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
799 bm->no_defaults = no_defaults;
802 void bookmark_list_set_editable(GtkWidget *list, gboolean editable)
806 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
809 bm->editable = editable;
812 void bookmark_list_set_only_directories(GtkWidget *list, gboolean only_directories)
816 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
819 bm->only_directories = only_directories;
822 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
827 bm = g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY);
830 buf = bookmark_string(name, path, NULL);
831 history_list_add_to_key(bm->key, buf, 0);
834 bookmark_populate_all(bm->key);
837 void bookmark_add_default(const gchar *name, const gchar *path)
839 if (!name || !path) return;
840 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
841 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
845 *-----------------------------------------------------------------------------
846 * combo with history key
847 *-----------------------------------------------------------------------------
850 typedef struct _HistoryComboData HistoryComboData;
851 struct _HistoryComboData
859 static void history_combo_destroy(GtkWidget *widget, gpointer data)
861 HistoryComboData *hc = data;
863 g_free(hc->history_key);
867 /* if text is NULL, entry is set to the most recent item */
868 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
869 const gchar *history_key, gint max_levels)
871 HistoryComboData *hc;
875 hc = g_new0(HistoryComboData, 1);
876 hc->history_key = g_strdup(history_key);
877 hc->history_levels = max_levels;
879 hc->combo = gtk_combo_box_text_new_with_entry();
881 gtk_combo_set_case_sensitive(GTK_COMBO(hc->combo), TRUE);
882 gtk_combo_set_use_arrows(GTK_COMBO(hc->combo), FALSE);
885 hc->entry = gtk_bin_get_child(GTK_BIN(hc->combo));
887 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
888 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
889 g_signal_connect(G_OBJECT(hc->combo), "destroy",
890 G_CALLBACK(history_combo_destroy), hc);
892 work = history_list_get_by_key(hc->history_key);
895 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), (gchar *)work->data);
902 gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
906 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
909 if (entry) *entry = hc->entry;
913 /* if text is NULL, current entry text is used
914 * widget can be the combo or entry widget
916 void history_combo_append_history(GtkWidget *widget, const gchar *text)
918 HistoryComboData *hc;
921 hc = g_object_get_data(G_OBJECT(widget), "history_combo_data");
924 log_printf("widget is not a history combo\n");
930 new_text = g_strdup(text);
934 new_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(hc->entry)));
937 if (new_text && strlen(new_text) > 0)
942 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
944 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
946 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
947 gtk_list_store_clear(GTK_LIST_STORE(store));
949 work = history_list_get_by_key(hc->history_key);
952 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), (gchar *)work->data);
959 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */