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]"
61 constexpr gint bookmark_drop_types_n = 3;
62 constexpr gint bookmark_drag_types_n = 2;
66 struct BookButtonData;
74 void (*select_func)(const gchar *path, gpointer data);
79 gboolean only_directories;
81 BookButtonData *active_button;
99 GtkWidget *name_entry;
100 GtkWidget *path_entry;
101 GtkWidget *icon_entry;
112 static GtkTargetEntry bookmark_drop_types[] = {
113 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
114 { const_cast<gchar *>("x-url/http"), 0, TARGET_X_URL },
115 { const_cast<gchar *>("_NETSCAPE_URL"), 0, TARGET_X_URL }
118 static GtkTargetEntry bookmark_drag_types[] = {
119 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
120 { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
123 static GList *bookmark_widget_list = nullptr;
124 static GList *bookmark_default_list = nullptr;
127 static void bookmark_populate_all(const gchar *key);
130 static BookButtonData *bookmark_from_string(const gchar *text)
133 const gchar *path_ptr;
134 const gchar *icon_ptr;
136 b = g_new0(BookButtonData, 1);
140 b->name = g_strdup(_("New Bookmark"));
141 b->path = g_strdup(homedir());
146 b->key = g_strdup(text);
148 path_ptr = strstr(text, MARKER_PATH);
149 icon_ptr = strstr(text, MARKER_ICON);
151 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
153 log_printf("warning, bookmark icon must be after path\n");
162 b->name = g_strndup(text, l);
163 path_ptr += strlen(MARKER_PATH);
166 l = icon_ptr - path_ptr;
167 b->path = g_strndup(path_ptr, l);
171 b->path = g_strdup(path_ptr);
176 b->name = g_strdup(text);
177 b->path = g_strdup("");
182 icon_ptr += strlen(MARKER_ICON);
183 b->icon = g_strdup(icon_ptr);
189 static void bookmark_free(BookButtonData *b)
201 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
203 if (!name) name = _("New Bookmark");
207 return g_strdup_printf("%s" MARKER_PATH "%s" MARKER_ICON "%s", name, path, icon);
210 return g_strdup_printf("%s" MARKER_PATH "%s", name, path);
213 static void bookmark_select_cb(GtkWidget *button, gpointer data)
215 auto bm = static_cast<BookMarkData *>(data);
218 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
221 if (bm->select_func) bm->select_func(b->path, bm->select_data);
224 static void bookmark_edit_destroy_cb(GtkWidget *, gpointer data)
226 auto p = static_cast<BookPropData *>(data);
228 bookmark_free(p->bb);
232 static void bookmark_edit_cancel_cb(GenericDialog *, gpointer)
236 static void bookmark_edit_ok_cb(GenericDialog *, gpointer data)
238 auto p = static_cast<BookPropData *>(data);
244 name = gq_gtk_entry_get_text(GTK_ENTRY(p->name_entry));
245 path = remove_trailing_slash(gq_gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
246 icon = gq_gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
248 new_string = bookmark_string(name, path, icon);
252 history_list_item_change(p->bb->parent, p->bb->key, new_string);
256 history_list_add_to_key(p->bb->parent, new_string, 0);
259 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
260 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
265 bookmark_populate_all(p->bb->parent);
268 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
270 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
278 if (!key) key = "bookmarks";
280 p = g_new0(BookPropData, 1);
282 p->bb = bookmark_from_string(text);
283 p->bb->parent = g_strdup(key);
285 gd = generic_dialog_new(_("Edit Bookmark"), "bookmark_edit",
287 bookmark_edit_cancel_cb, p);
288 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
289 G_CALLBACK(bookmark_edit_destroy_cb), p);
291 generic_dialog_add_message(gd, nullptr, _("Edit Bookmark"), nullptr, FALSE);
293 generic_dialog_add_button(gd, GQ_ICON_OK, "OK",
294 bookmark_edit_ok_cb, TRUE);
296 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
297 pref_table_label(table, 0, 0, _("Name:"), GTK_ALIGN_END);
299 p->name_entry = gtk_entry_new();
300 gtk_widget_set_size_request(p->name_entry, 300, -1);
301 if (p->bb->name) gq_gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
302 gq_gtk_grid_attach_default(GTK_GRID(table), p->name_entry, 1, 2, 0, 1);
303 generic_dialog_attach_default(gd, p->name_entry);
304 gtk_widget_show(p->name_entry);
306 pref_table_label(table, 0, 1, _("Path:"), GTK_ALIGN_END);
308 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
309 "bookmark_path", -1, nullptr, nullptr);
310 tab_completion_add_select_button(p->path_entry, nullptr, TRUE);
311 gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 1, 2);
312 generic_dialog_attach_default(gd, p->path_entry);
313 gtk_widget_show(label);
315 pref_table_label(table, 0, 2, _("Icon:"), GTK_ALIGN_END);
318 if (!icon) icon = "";
319 label = tab_completion_new_with_history(&p->icon_entry, icon,
320 "bookmark_icons", -1, nullptr, nullptr);
321 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
322 gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 2, 3);
323 generic_dialog_attach_default(gd, p->icon_entry);
324 gtk_widget_show(label);
326 gtk_widget_show(gd->dialog);
329 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
334 const gchar *key_holder;
336 if (!bm->editable) return;
338 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
341 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
342 p = g_list_index(list, button);
345 if (p < 0 || p + direction < 0) return;
347 key_holder = bm->key;
348 bm->key = "_TEMPHOLDER";
349 history_list_item_move(key_holder, b->key, -direction);
350 bookmark_populate_all(key_holder);
351 bm->key = key_holder;
353 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
356 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
358 auto bm = static_cast<BookMarkData *>(data);
360 if (!bm->active_button) return;
362 bookmark_edit(bm->key, bm->active_button->key, widget);
365 static void bookmark_menu_move(BookMarkData *bm, gint direction)
367 if (!bm->active_button) return;
369 bookmark_move(bm, bm->active_button->button, direction);
372 static void bookmark_menu_up_cb(GtkWidget *, gpointer data)
374 bookmark_menu_move(static_cast<BookMarkData *>(data), -1);
377 static void bookmark_menu_down_cb(GtkWidget *, gpointer data)
379 bookmark_menu_move(static_cast<BookMarkData *>(data), 1);
382 static void bookmark_menu_remove_cb(GtkWidget *, gpointer data)
384 auto bm = static_cast<BookMarkData *>(data);
386 if (!bm->active_button) return;
388 history_list_item_remove(bm->key, bm->active_button->key);
389 bookmark_populate_all(bm->key);
392 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button, gint, guint32, gboolean local)
397 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
400 bm->active_button = b;
402 menu = popup_menu_short_lived();
403 menu_item_add_icon_sensitive(menu, _("_Properties..."), PIXBUF_INLINE_ICON_PROPERTIES, bm->editable,
404 G_CALLBACK(bookmark_menu_prop_cb), bm);
405 menu_item_add_icon_sensitive(menu, _("Move _up"), GQ_ICON_GO_UP, bm->editable,
406 G_CALLBACK(bookmark_menu_up_cb), bm);
407 menu_item_add_icon_sensitive(menu, _("Move _down"), GQ_ICON_GO_DOWN, bm->editable,
408 G_CALLBACK(bookmark_menu_down_cb), bm);
409 menu_item_add_icon_sensitive(menu, _("_Remove"), GQ_ICON_REMOVE, bm->editable,
410 G_CALLBACK(bookmark_menu_remove_cb), bm);
414 gtk_menu_popup_at_widget(GTK_MENU(menu), button, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_CENTER, nullptr);
418 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
422 static gboolean bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
424 auto bm = static_cast<BookMarkData *>(data);
426 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
428 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
433 static gboolean bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
435 auto bm = static_cast<BookMarkData *>(data);
437 switch (event->keyval)
440 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
443 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
447 if (event->state & GDK_SHIFT_MASK)
449 bookmark_move(bm, button, -1);
454 if (event->state & GDK_SHIFT_MASK)
456 bookmark_move(bm, button, 1);
465 static void bookmark_drag_set_data(GtkWidget *button,
466 GdkDragContext *context, GtkSelectionData *selection_data,
467 guint, guint, gpointer data)
469 auto bm = static_cast<BookMarkData *>(data);
471 GList *list = nullptr;
474 if (gdk_drag_context_get_dest_window(context) == gtk_widget_get_window(bm->widget)) return;
476 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
479 list = g_list_append(list, b->path);
481 gchar **uris = uris_from_pathlist(list);
482 gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
485 char *str = g_strjoinv("\r\n", uris);
486 ret = gtk_selection_data_set_text(selection_data, str, -1);
494 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer)
497 GdkModifierType mask;
500 GtkAllocation allocation;
504 gtk_widget_get_allocation(button, &allocation);
506 pixbuf = gdk_pixbuf_get_from_window(gtk_widget_get_window(button),
507 allocation.x, allocation.y,
508 allocation.width, allocation.height);
509 seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(button)));
510 device = gdk_seat_get_pointer(seat);
511 gdk_window_get_device_position(gtk_widget_get_window(button), device, &x, &y, &mask);
513 gtk_drag_set_icon_pixbuf(context, pixbuf,
514 x - allocation.x, y - allocation.y);
515 g_object_unref(pixbuf);
518 static gboolean bookmark_path_tooltip_cb(GtkWidget *button, gpointer)
522 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
523 gtk_widget_set_tooltip_text(GTK_WIDGET(button), b->path);
528 static void bookmark_populate(BookMarkData *bm)
534 box = GTK_BOX(bm->box);
535 children = gtk_container_get_children(GTK_CONTAINER(box));
539 GtkWidget *widget = GTK_WIDGET(work->data);
541 gq_gtk_widget_destroy(widget);
544 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
549 if (!bookmark_default_list)
551 buf = bookmark_string(_("Home"), homedir(), nullptr);
552 history_list_add_to_key(bm->key, buf, 0);
555 if (g_strcmp0(bm->key, "shortcuts") != 0)
557 buf = bookmark_string(".", g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
558 history_list_add_to_key(bm->key, buf, 0);
562 path = g_build_filename(homedir(), "Desktop", NULL);
565 buf = bookmark_string(_("Desktop"), path, nullptr);
566 history_list_add_to_key(bm->key, buf, 0);
572 work = bookmark_default_list;
573 while (work && work->next)
577 name = static_cast<gchar *>(work->data);
579 path = static_cast<gchar *>(work->data);
582 if (strcmp(name, ".") == 0)
584 if (g_strcmp0(bm->key, "shortcuts") != 0)
586 buf = bookmark_string(name, g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
595 buf = bookmark_string(name, path, nullptr);
597 history_list_add_to_key(bm->key, buf, 0);
602 work = history_list_get_by_key(bm->key);
603 work = g_list_last(work);
608 b = bookmark_from_string(static_cast<const gchar *>(work->data));
611 if (strcmp(b->name, ".") == 0)
615 b->path = g_strdup(history_list_find_last_path_by_key("path_list"));
616 buf = bookmark_string(".", b->path, b->icon);
617 history_list_item_change("bookmarks", b->key, buf);
618 b->key = g_strdup(buf);
623 b->button = gtk_button_new();
624 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
625 gq_gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
626 gtk_widget_show(b->button);
628 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
629 b, reinterpret_cast<GDestroyNotify>(bookmark_free));
631 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
632 gq_gtk_container_add(GTK_WIDGET(b->button), box);
633 gtk_widget_show(box);
637 GdkPixbuf *pixbuf = nullptr;
640 iconl = path_from_utf8(b->icon);
641 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
645 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
653 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
655 pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), b->icon, w, GTK_ICON_LOOKUP_NO_SVG, nullptr);
666 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
668 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
669 GDK_INTERP_BILINEAR);
670 b->image = gtk_image_new_from_pixbuf(scaled);
671 g_object_unref(scaled);
672 g_object_unref(pixbuf);
676 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
681 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
683 gq_gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
684 gtk_widget_show(b->image);
686 b->label = gtk_label_new(b->name);
687 gq_gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
688 gtk_widget_show(b->label);
690 g_signal_connect(G_OBJECT(b->button), "clicked",
691 G_CALLBACK(bookmark_select_cb), bm);
692 g_signal_connect(G_OBJECT(b->button), "button_press_event",
693 G_CALLBACK(bookmark_press_cb), bm);
694 g_signal_connect(G_OBJECT(b->button), "key_press_event",
695 G_CALLBACK(bookmark_keypress_cb), bm);
697 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
698 bookmark_drag_types, bookmark_drag_types_n,
699 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
700 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
701 G_CALLBACK(bookmark_drag_set_data), bm);
702 g_signal_connect(G_OBJECT(b->button), "drag_begin",
703 G_CALLBACK(bookmark_drag_begin), bm);
705 gtk_widget_set_has_tooltip(GTK_WIDGET(b->button), TRUE);
706 g_signal_connect(G_OBJECT(b->button), "query_tooltip", G_CALLBACK(bookmark_path_tooltip_cb), bm);
713 static void bookmark_populate_all(const gchar *key)
719 work = bookmark_widget_list;
724 bm = static_cast<BookMarkData *>(work->data);
727 if (strcmp(bm->key, key) == 0)
729 bookmark_populate(bm);
734 static void bookmark_dnd_get_data(GtkWidget *, GdkDragContext *,
736 GtkSelectionData *selection_data, guint,
737 guint, gpointer data)
739 auto bm = static_cast<BookMarkData *>(data);
740 GList *list = nullptr;
741 GList *errors = nullptr;
746 if (!bm->editable) return;
748 uris = gtk_selection_data_get_uris(selection_data);
751 list = uri_pathlist_from_uris(uris, &errors);
754 warning_dialog_dnd_uri_error(errors);
755 g_list_free_full(errors, g_free);
762 auto path = static_cast<gchar *>(work->data);
767 if (bm->only_directories && !isdir(path)) continue;
769 real_path = realpath(path, nullptr);
771 if (strstr(real_path, get_collections_dir()) && isfile(path))
773 buf = bookmark_string(filename_from_path(path), path, "gq-icon-collection");
775 else if (isfile(path))
777 buf = bookmark_string(filename_from_path(path), path, GQ_ICON_FILE);
781 buf = bookmark_string(filename_from_path(path), path, nullptr);
783 history_list_add_to_key(bm->key, buf, 0);
788 g_list_free_full(list, g_free);
790 bookmark_populate_all(bm->key);
794 static void bookmark_list_destroy(GtkWidget *, gpointer data)
796 auto bm = static_cast<BookMarkData *>(data);
798 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
800 g_free(const_cast<gchar *>(bm->key));
804 GtkWidget *bookmark_list_new(const gchar *key,
805 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
810 if (!key) key = "bookmarks";
812 bm = g_new0(BookMarkData, 1);
813 bm->key = g_strdup(key);
815 bm->select_func = select_func;
816 bm->select_data = select_data;
818 bm->no_defaults = FALSE;
820 bm->only_directories = FALSE;
822 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
828 layout = gtk_widget_create_pango_layout(GTK_WIDGET(scrolled), "reasonable width");
829 pango_layout_get_pixel_size(layout, &width, &height);
830 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scrolled), width);
831 g_object_unref(layout);
833 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
835 bm->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
836 gq_gtk_container_add(GTK_WIDGET(scrolled), bm->box);
837 gtk_widget_show(bm->box);
839 bookmark_populate(bm);
841 g_signal_connect(G_OBJECT(bm->box), "destroy",
842 G_CALLBACK(bookmark_list_destroy), bm);
843 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
844 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
845 bm->widget = scrolled;
847 gtk_drag_dest_set(scrolled,
848 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
849 bookmark_drop_types, bookmark_drop_types_n,
850 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
851 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
852 G_CALLBACK(bookmark_dnd_get_data), bm);
854 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
859 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
863 if (!list || !key) return;
865 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
868 if (bm->key && strcmp(bm->key, key) == 0) return;
870 g_free(const_cast<gchar *>(bm->key));
871 bm->key = g_strdup(key);
873 bookmark_populate(bm);
876 void bookmark_list_set_no_defaults(GtkWidget *list, gboolean no_defaults)
880 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
883 bm->no_defaults = no_defaults;
886 void bookmark_list_set_editable(GtkWidget *list, gboolean editable)
890 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
893 bm->editable = editable;
896 void bookmark_list_set_only_directories(GtkWidget *list, gboolean only_directories)
900 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
903 bm->only_directories = only_directories;
906 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
911 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
914 std::unique_ptr<gchar, decltype(&g_free)> buf(bookmark_string(name, path, nullptr), g_free);
915 real_path = realpath(path, nullptr);
917 if (strstr(real_path, get_collections_dir()) && isfile(path))
919 buf.reset(bookmark_string(name, path, "gq-icon-collection"));
925 buf.reset(bookmark_string(name, path, GQ_ICON_FILE));
929 buf.reset(bookmark_string(name, path, nullptr));
933 history_list_add_to_key(bm->key, buf.get(), 0);
936 bookmark_populate_all(bm->key);
939 void bookmark_add_default(const gchar *name, const gchar *path)
941 if (!name || !path) return;
942 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
943 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
947 *-----------------------------------------------------------------------------
948 * combo with history key
949 *-----------------------------------------------------------------------------
952 struct HistoryComboData
960 static void history_combo_destroy(GtkWidget *, gpointer data)
962 auto hc = static_cast<HistoryComboData *>(data);
964 g_free(hc->history_key);
968 /* if text is NULL, entry is set to the most recent item */
969 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
970 const gchar *history_key, gint max_levels)
972 HistoryComboData *hc;
976 hc = g_new0(HistoryComboData, 1);
977 hc->history_key = g_strdup(history_key);
978 hc->history_levels = max_levels;
980 hc->combo = gtk_combo_box_text_new_with_entry();
982 hc->entry = gtk_bin_get_child(GTK_BIN(hc->combo));
984 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
985 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
986 g_signal_connect(G_OBJECT(hc->combo), "destroy",
987 G_CALLBACK(history_combo_destroy), hc);
989 work = history_list_get_by_key(hc->history_key);
992 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
999 gq_gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
1003 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
1006 if (entry) *entry = hc->entry;
1010 /* if text is NULL, current entry text is used
1011 * widget can be the combo or entry widget
1013 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1015 HistoryComboData *hc;
1018 hc = static_cast<HistoryComboData *>(g_object_get_data(G_OBJECT(widget), "history_combo_data"));
1021 log_printf("widget is not a history combo\n");
1027 new_text = g_strdup(text);
1031 new_text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1034 if (new_text && strlen(new_text) > 0)
1036 GtkTreeModel *store;
1039 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1041 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1043 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1044 gtk_list_store_clear(GTK_LIST_STORE(store));
1046 work = history_list_get_by_key(hc->history_key);
1049 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
1056 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */