2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "ui-bookmark.h"
28 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #include <glib-object.h>
31 #include <pango/pango.h>
35 #include "history-list.h"
37 #include "main-defines.h"
39 #include "pixbuf-util.h"
41 #include "ui-fileops.h"
44 #include "ui-tabcomp.h"
45 #include "ui-utildlg.h"
46 #include "uri-utils.h"
49 *-----------------------------------------------------------------------------
51 *-----------------------------------------------------------------------------
54 #define BOOKMARK_DATA_KEY "bookmarkdata"
55 #define MARKER_PATH "[path]"
56 #define MARKER_ICON "[icon]"
58 struct BookButtonData;
66 void (*select_func)(const gchar *path, gpointer data);
71 gboolean only_directories;
73 BookButtonData *active_button;
91 GtkWidget *name_entry;
92 GtkWidget *path_entry;
93 GtkWidget *icon_entry;
104 static GtkTargetEntry bookmark_drop_types[] = {
105 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
106 { const_cast<gchar *>("x-url/http"), 0, TARGET_X_URL },
107 { const_cast<gchar *>("_NETSCAPE_URL"), 0, TARGET_X_URL }
110 bookmark_drop_types_n = 3
113 static GtkTargetEntry bookmark_drag_types[] = {
114 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
115 { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
118 bookmark_drag_types_n = 2
122 static GList *bookmark_widget_list = nullptr;
123 static GList *bookmark_default_list = nullptr;
126 static void bookmark_populate_all(const gchar *key);
129 static BookButtonData *bookmark_from_string(const gchar *text)
132 const gchar *path_ptr;
133 const gchar *icon_ptr;
135 b = g_new0(BookButtonData, 1);
139 b->name = g_strdup(_("New Bookmark"));
140 b->path = g_strdup(homedir());
145 b->key = g_strdup(text);
147 path_ptr = strstr(text, MARKER_PATH);
148 icon_ptr = strstr(text, MARKER_ICON);
150 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
152 log_printf("warning, bookmark icon must be after path\n");
161 b->name = g_strndup(text, l);
162 path_ptr += strlen(MARKER_PATH);
165 l = icon_ptr - path_ptr;
166 b->path = g_strndup(path_ptr, l);
170 b->path = g_strdup(path_ptr);
175 b->name = g_strdup(text);
176 b->path = g_strdup("");
181 icon_ptr += strlen(MARKER_ICON);
182 b->icon = g_strdup(icon_ptr);
188 static void bookmark_free(BookButtonData *b)
200 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
202 if (!name) name = _("New Bookmark");
206 return g_strdup_printf("%s" MARKER_PATH "%s" MARKER_ICON "%s", name, path, icon);
209 return g_strdup_printf("%s" MARKER_PATH "%s", name, path);
212 static void bookmark_select_cb(GtkWidget *button, gpointer data)
214 auto bm = static_cast<BookMarkData *>(data);
217 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
220 if (bm->select_func) bm->select_func(b->path, bm->select_data);
223 static void bookmark_edit_destroy_cb(GtkWidget *, gpointer data)
225 auto p = static_cast<BookPropData *>(data);
227 bookmark_free(p->bb);
231 static void bookmark_edit_cancel_cb(GenericDialog *, gpointer)
235 static void bookmark_edit_ok_cb(GenericDialog *, gpointer data)
237 auto p = static_cast<BookPropData *>(data);
243 name = gq_gtk_entry_get_text(GTK_ENTRY(p->name_entry));
244 path = remove_trailing_slash(gq_gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
245 icon = gq_gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
247 new_string = bookmark_string(name, path, icon);
251 history_list_item_change(p->bb->parent, p->bb->key, new_string);
255 history_list_add_to_key(p->bb->parent, new_string, 0);
258 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
259 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
264 bookmark_populate_all(p->bb->parent);
267 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
269 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
277 if (!key) key = "bookmarks";
279 p = g_new0(BookPropData, 1);
281 p->bb = bookmark_from_string(text);
282 p->bb->parent = g_strdup(key);
284 gd = generic_dialog_new(_("Edit Bookmark"), "bookmark_edit",
286 bookmark_edit_cancel_cb, p);
287 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
288 G_CALLBACK(bookmark_edit_destroy_cb), p);
290 generic_dialog_add_message(gd, nullptr, _("Edit Bookmark"), nullptr, FALSE);
292 generic_dialog_add_button(gd, GQ_ICON_OK, "OK",
293 bookmark_edit_ok_cb, TRUE);
295 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
296 pref_table_label(table, 0, 0, _("Name:"), GTK_ALIGN_END);
298 p->name_entry = gtk_entry_new();
299 gtk_widget_set_size_request(p->name_entry, 300, -1);
300 if (p->bb->name) gq_gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
301 gq_gtk_grid_attach_default(GTK_GRID(table), p->name_entry, 1, 2, 0, 1);
302 generic_dialog_attach_default(gd, p->name_entry);
303 gtk_widget_show(p->name_entry);
305 pref_table_label(table, 0, 1, _("Path:"), GTK_ALIGN_END);
307 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
308 "bookmark_path", -1, nullptr, nullptr);
309 tab_completion_add_select_button(p->path_entry, nullptr, TRUE);
310 gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 1, 2);
311 generic_dialog_attach_default(gd, p->path_entry);
312 gtk_widget_show(label);
314 pref_table_label(table, 0, 2, _("Icon:"), GTK_ALIGN_END);
317 if (!icon) icon = "";
318 label = tab_completion_new_with_history(&p->icon_entry, icon,
319 "bookmark_icons", -1, nullptr, nullptr);
320 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
321 gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 2, 3);
322 generic_dialog_attach_default(gd, p->icon_entry);
323 gtk_widget_show(label);
325 gtk_widget_show(gd->dialog);
328 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
333 const gchar *key_holder;
335 if (!bm->editable) return;
337 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
340 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
341 p = g_list_index(list, button);
344 if (p < 0 || p + direction < 0) return;
346 key_holder = bm->key;
347 bm->key = "_TEMPHOLDER";
348 history_list_item_move(key_holder, b->key, -direction);
349 bookmark_populate_all(key_holder);
350 bm->key = key_holder;
352 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
355 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
357 auto bm = static_cast<BookMarkData *>(data);
359 if (!bm->active_button) return;
361 bookmark_edit(bm->key, bm->active_button->key, widget);
364 static void bookmark_menu_move(BookMarkData *bm, gint direction)
366 if (!bm->active_button) return;
368 bookmark_move(bm, bm->active_button->button, direction);
371 static void bookmark_menu_up_cb(GtkWidget *, gpointer data)
373 bookmark_menu_move(static_cast<BookMarkData *>(data), -1);
376 static void bookmark_menu_down_cb(GtkWidget *, gpointer data)
378 bookmark_menu_move(static_cast<BookMarkData *>(data), 1);
381 static void bookmark_menu_remove_cb(GtkWidget *, gpointer data)
383 auto bm = static_cast<BookMarkData *>(data);
385 if (!bm->active_button) return;
387 history_list_item_remove(bm->key, bm->active_button->key);
388 bookmark_populate_all(bm->key);
391 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button, gint, guint32, gboolean local)
396 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
399 bm->active_button = b;
401 menu = popup_menu_short_lived();
402 menu_item_add_icon_sensitive(menu, _("_Properties..."), PIXBUF_INLINE_ICON_PROPERTIES, bm->editable,
403 G_CALLBACK(bookmark_menu_prop_cb), bm);
404 menu_item_add_icon_sensitive(menu, _("Move _up"), GQ_ICON_GO_UP, bm->editable,
405 G_CALLBACK(bookmark_menu_up_cb), bm);
406 menu_item_add_icon_sensitive(menu, _("Move _down"), GQ_ICON_GO_DOWN, bm->editable,
407 G_CALLBACK(bookmark_menu_down_cb), bm);
408 menu_item_add_icon_sensitive(menu, _("_Remove"), GQ_ICON_REMOVE, bm->editable,
409 G_CALLBACK(bookmark_menu_remove_cb), bm);
413 gtk_menu_popup_at_widget(GTK_MENU(menu), button, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_CENTER, nullptr);
417 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
421 static gboolean bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
423 auto bm = static_cast<BookMarkData *>(data);
425 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
427 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
432 static gboolean bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
434 auto bm = static_cast<BookMarkData *>(data);
436 switch (event->keyval)
439 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
442 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
446 if (event->state & GDK_SHIFT_MASK)
448 bookmark_move(bm, button, -1);
453 if (event->state & GDK_SHIFT_MASK)
455 bookmark_move(bm, button, 1);
464 static void bookmark_drag_set_data(GtkWidget *button,
465 GdkDragContext *context, GtkSelectionData *selection_data,
466 guint, guint, gpointer data)
468 auto bm = static_cast<BookMarkData *>(data);
470 GList *list = nullptr;
473 if (gdk_drag_context_get_dest_window(context) == gtk_widget_get_window(bm->widget)) return;
475 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
478 list = g_list_append(list, b->path);
480 gchar **uris = uris_from_pathlist(list);
481 gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
484 char *str = g_strjoinv("\r\n", uris);
485 ret = gtk_selection_data_set_text(selection_data, str, -1);
493 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer)
496 GdkModifierType mask;
499 GtkAllocation allocation;
503 gtk_widget_get_allocation(button, &allocation);
505 pixbuf = gdk_pixbuf_get_from_window(gtk_widget_get_window(button),
506 allocation.x, allocation.y,
507 allocation.width, allocation.height);
508 seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(button)));
509 device = gdk_seat_get_pointer(seat);
510 gdk_window_get_device_position(gtk_widget_get_window(button), device, &x, &y, &mask);
512 gtk_drag_set_icon_pixbuf(context, pixbuf,
513 x - allocation.x, y - allocation.y);
514 g_object_unref(pixbuf);
517 static gboolean bookmark_path_tooltip_cb(GtkWidget *button, gpointer)
521 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
522 gtk_widget_set_tooltip_text(GTK_WIDGET(button), b->path);
527 static void bookmark_populate(BookMarkData *bm)
533 box = GTK_BOX(bm->box);
534 children = gtk_container_get_children(GTK_CONTAINER(box));
538 GtkWidget *widget = GTK_WIDGET(work->data);
540 gq_gtk_widget_destroy(widget);
543 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
548 if (!bookmark_default_list)
550 buf = bookmark_string(_("Home"), homedir(), nullptr);
551 history_list_add_to_key(bm->key, buf, 0);
554 if (g_strcmp0(bm->key, "shortcuts") != 0)
556 buf = bookmark_string(".", g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
557 history_list_add_to_key(bm->key, buf, 0);
561 path = g_build_filename(homedir(), "Desktop", NULL);
564 buf = bookmark_string(_("Desktop"), path, nullptr);
565 history_list_add_to_key(bm->key, buf, 0);
571 work = bookmark_default_list;
572 while (work && work->next)
576 name = static_cast<gchar *>(work->data);
578 path = static_cast<gchar *>(work->data);
581 if (strcmp(name, ".") == 0)
583 if (g_strcmp0(bm->key, "shortcuts") != 0)
585 buf = bookmark_string(name, g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
594 buf = bookmark_string(name, path, nullptr);
596 history_list_add_to_key(bm->key, buf, 0);
601 work = history_list_get_by_key(bm->key);
602 work = g_list_last(work);
607 b = bookmark_from_string(static_cast<const gchar *>(work->data));
610 if (strcmp(b->name, ".") == 0)
614 b->path = g_strdup(history_list_find_last_path_by_key("path_list"));
615 buf = bookmark_string(".", b->path, b->icon);
616 history_list_item_change("bookmarks", b->key, buf);
617 b->key = g_strdup(buf);
622 b->button = gtk_button_new();
623 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
624 gq_gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
625 gtk_widget_show(b->button);
627 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
628 b, reinterpret_cast<GDestroyNotify>(bookmark_free));
630 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
631 gq_gtk_container_add(GTK_WIDGET(b->button), box);
632 gtk_widget_show(box);
636 GdkPixbuf *pixbuf = nullptr;
639 iconl = path_from_utf8(b->icon);
640 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
644 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
652 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
654 pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), b->icon, w, GTK_ICON_LOOKUP_NO_SVG, nullptr);
665 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
667 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
668 GDK_INTERP_BILINEAR);
669 b->image = gtk_image_new_from_pixbuf(scaled);
670 g_object_unref(scaled);
671 g_object_unref(pixbuf);
675 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
680 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
682 gq_gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
683 gtk_widget_show(b->image);
685 b->label = gtk_label_new(b->name);
686 gq_gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
687 gtk_widget_show(b->label);
689 g_signal_connect(G_OBJECT(b->button), "clicked",
690 G_CALLBACK(bookmark_select_cb), bm);
691 g_signal_connect(G_OBJECT(b->button), "button_press_event",
692 G_CALLBACK(bookmark_press_cb), bm);
693 g_signal_connect(G_OBJECT(b->button), "key_press_event",
694 G_CALLBACK(bookmark_keypress_cb), bm);
696 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
697 bookmark_drag_types, bookmark_drag_types_n,
698 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
699 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
700 G_CALLBACK(bookmark_drag_set_data), bm);
701 g_signal_connect(G_OBJECT(b->button), "drag_begin",
702 G_CALLBACK(bookmark_drag_begin), bm);
704 gtk_widget_set_has_tooltip(GTK_WIDGET(b->button), TRUE);
705 g_signal_connect(G_OBJECT(b->button), "query_tooltip", G_CALLBACK(bookmark_path_tooltip_cb), bm);
712 static void bookmark_populate_all(const gchar *key)
718 work = bookmark_widget_list;
723 bm = static_cast<BookMarkData *>(work->data);
726 if (strcmp(bm->key, key) == 0)
728 bookmark_populate(bm);
733 static void bookmark_dnd_get_data(GtkWidget *, GdkDragContext *,
735 GtkSelectionData *selection_data, guint,
736 guint, gpointer data)
738 auto bm = static_cast<BookMarkData *>(data);
739 GList *list = nullptr;
740 GList *errors = nullptr;
745 if (!bm->editable) return;
747 uris = gtk_selection_data_get_uris(selection_data);
750 list = uri_pathlist_from_uris(uris, &errors);
753 warning_dialog_dnd_uri_error(errors);
754 g_list_free_full(errors, g_free);
761 auto path = static_cast<gchar *>(work->data);
766 if (bm->only_directories && !isdir(path)) continue;
768 real_path = realpath(path, nullptr);
770 if (strstr(real_path, get_collections_dir()) && isfile(path))
772 buf = bookmark_string(filename_from_path(path), path, "gq-icon-collection");
774 else if (isfile(path))
776 buf = bookmark_string(filename_from_path(path), path, GQ_ICON_FILE);
780 buf = bookmark_string(filename_from_path(path), path, nullptr);
782 history_list_add_to_key(bm->key, buf, 0);
787 g_list_free_full(list, g_free);
789 bookmark_populate_all(bm->key);
793 static void bookmark_list_destroy(GtkWidget *, gpointer data)
795 auto bm = static_cast<BookMarkData *>(data);
797 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
799 g_free(const_cast<gchar *>(bm->key));
803 GtkWidget *bookmark_list_new(const gchar *key,
804 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
809 if (!key) key = "bookmarks";
811 bm = g_new0(BookMarkData, 1);
812 bm->key = g_strdup(key);
814 bm->select_func = select_func;
815 bm->select_data = select_data;
817 bm->no_defaults = FALSE;
819 bm->only_directories = FALSE;
821 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
827 layout = gtk_widget_create_pango_layout(GTK_WIDGET(scrolled), "reasonable width");
828 pango_layout_get_pixel_size(layout, &width, &height);
829 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scrolled), width);
830 g_object_unref(layout);
832 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
834 bm->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
835 gq_gtk_container_add(GTK_WIDGET(scrolled), bm->box);
836 gtk_widget_show(bm->box);
838 bookmark_populate(bm);
840 g_signal_connect(G_OBJECT(bm->box), "destroy",
841 G_CALLBACK(bookmark_list_destroy), bm);
842 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
843 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
844 bm->widget = scrolled;
846 gtk_drag_dest_set(scrolled,
847 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
848 bookmark_drop_types, bookmark_drop_types_n,
849 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
850 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
851 G_CALLBACK(bookmark_dnd_get_data), bm);
853 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
858 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
862 if (!list || !key) return;
864 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
867 if (bm->key && strcmp(bm->key, key) == 0) return;
869 g_free(const_cast<gchar *>(bm->key));
870 bm->key = g_strdup(key);
872 bookmark_populate(bm);
875 void bookmark_list_set_no_defaults(GtkWidget *list, gboolean no_defaults)
879 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
882 bm->no_defaults = no_defaults;
885 void bookmark_list_set_editable(GtkWidget *list, gboolean editable)
889 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
892 bm->editable = editable;
895 void bookmark_list_set_only_directories(GtkWidget *list, gboolean only_directories)
899 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
902 bm->only_directories = only_directories;
905 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
910 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
913 std::unique_ptr<gchar, decltype(&g_free)> buf(bookmark_string(name, path, nullptr), g_free);
914 real_path = realpath(path, nullptr);
916 if (strstr(real_path, get_collections_dir()) && isfile(path))
918 buf.reset(bookmark_string(name, path, "gq-icon-collection"));
924 buf.reset(bookmark_string(name, path, GQ_ICON_FILE));
928 buf.reset(bookmark_string(name, path, nullptr));
932 history_list_add_to_key(bm->key, buf.get(), 0);
935 bookmark_populate_all(bm->key);
938 void bookmark_add_default(const gchar *name, const gchar *path)
940 if (!name || !path) return;
941 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
942 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
946 *-----------------------------------------------------------------------------
947 * combo with history key
948 *-----------------------------------------------------------------------------
951 struct HistoryComboData
959 static void history_combo_destroy(GtkWidget *, gpointer data)
961 auto hc = static_cast<HistoryComboData *>(data);
963 g_free(hc->history_key);
967 /* if text is NULL, entry is set to the most recent item */
968 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
969 const gchar *history_key, gint max_levels)
971 HistoryComboData *hc;
975 hc = g_new0(HistoryComboData, 1);
976 hc->history_key = g_strdup(history_key);
977 hc->history_levels = max_levels;
979 hc->combo = gtk_combo_box_text_new_with_entry();
981 hc->entry = gtk_bin_get_child(GTK_BIN(hc->combo));
983 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
984 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
985 g_signal_connect(G_OBJECT(hc->combo), "destroy",
986 G_CALLBACK(history_combo_destroy), hc);
988 work = history_list_get_by_key(hc->history_key);
991 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
998 gq_gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1002 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1005 if (entry) *entry = hc->entry;
1009 /* if text is NULL, current entry text is used
1010 * widget can be the combo or entry widget
1012 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1014 HistoryComboData *hc;
1017 hc = static_cast<HistoryComboData *>(g_object_get_data(G_OBJECT(widget), "history_combo_data"));
1020 log_printf("widget is not a history combo\n");
1026 new_text = g_strdup(text);
1030 new_text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1033 if (new_text && strlen(new_text) > 0)
1035 GtkTreeModel *store;
1038 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1040 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1042 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1043 gtk_list_store_clear(GTK_LIST_STORE(store));
1045 work = history_list_get_by_key(hc->history_key);
1048 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
1055 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */