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.
26 #include "ui-bookmark.h"
28 #include "history-list.h"
30 #include "pixbuf-util.h"
31 #include "ui-fileops.h"
34 #include "ui-tabcomp.h"
35 #include "ui-utildlg.h"
36 #include "uri-utils.h"
39 *-----------------------------------------------------------------------------
41 *-----------------------------------------------------------------------------
44 #define BOOKMARK_DATA_KEY "bookmarkdata"
45 #define MARKER_PATH "[path]"
46 #define MARKER_ICON "[icon]"
48 struct BookButtonData;
56 void (*select_func)(const gchar *path, gpointer data);
61 gboolean only_directories;
63 BookButtonData *active_button;
81 GtkWidget *name_entry;
82 GtkWidget *path_entry;
83 GtkWidget *icon_entry;
94 static GtkTargetEntry bookmark_drop_types[] = {
95 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
96 { const_cast<gchar *>("x-url/http"), 0, TARGET_X_URL },
97 { const_cast<gchar *>("_NETSCAPE_URL"), 0, TARGET_X_URL }
99 #define bookmark_drop_types_n 3
101 static GtkTargetEntry bookmark_drag_types[] = {
102 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
103 { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
105 #define bookmark_drag_types_n 2
108 static GList *bookmark_widget_list = nullptr;
109 static GList *bookmark_default_list = nullptr;
112 static void bookmark_populate_all(const gchar *key);
115 static BookButtonData *bookmark_from_string(const gchar *text)
118 const gchar *path_ptr;
119 const gchar *icon_ptr;
121 b = g_new0(BookButtonData, 1);
125 b->name = g_strdup(_("New Bookmark"));
126 b->path = g_strdup(homedir());
131 b->key = g_strdup(text);
133 path_ptr = strstr(text, MARKER_PATH);
134 icon_ptr = strstr(text, MARKER_ICON);
136 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
138 log_printf("warning, bookmark icon must be after path\n");
147 b->name = g_strndup(text, l);
148 path_ptr += strlen(MARKER_PATH);
151 l = icon_ptr - path_ptr;
152 b->path = g_strndup(path_ptr, l);
156 b->path = g_strdup(path_ptr);
161 b->name = g_strdup(text);
162 b->path = g_strdup("");
167 icon_ptr += strlen(MARKER_ICON);
168 b->icon = g_strdup(icon_ptr);
174 static void bookmark_free(BookButtonData *b)
186 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
188 if (!name) name = _("New Bookmark");
192 return g_strdup_printf("%s" MARKER_PATH "%s" MARKER_ICON "%s", name, path, icon);
195 return g_strdup_printf("%s" MARKER_PATH "%s", name, path);
198 static void bookmark_select_cb(GtkWidget *button, gpointer data)
200 auto bm = static_cast<BookMarkData *>(data);
203 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
206 if (bm->select_func) bm->select_func(b->path, bm->select_data);
209 static void bookmark_edit_destroy_cb(GtkWidget *, gpointer data)
211 auto p = static_cast<BookPropData *>(data);
213 bookmark_free(p->bb);
217 static void bookmark_edit_cancel_cb(GenericDialog *, gpointer)
221 static void bookmark_edit_ok_cb(GenericDialog *, gpointer data)
223 auto p = static_cast<BookPropData *>(data);
229 name = gq_gtk_entry_get_text(GTK_ENTRY(p->name_entry));
230 path = remove_trailing_slash(gq_gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
231 icon = gq_gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
233 new_string = bookmark_string(name, path, icon);
237 history_list_item_change(p->bb->parent, p->bb->key, new_string);
241 history_list_add_to_key(p->bb->parent, new_string, 0);
244 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
245 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
250 bookmark_populate_all(p->bb->parent);
253 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
255 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
263 if (!key) key = "bookmarks";
265 p = g_new0(BookPropData, 1);
267 p->bb = bookmark_from_string(text);
268 p->bb->parent = g_strdup(key);
270 gd = generic_dialog_new(_("Edit Bookmark"), "bookmark_edit",
272 bookmark_edit_cancel_cb, p);
273 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
274 G_CALLBACK(bookmark_edit_destroy_cb), p);
276 generic_dialog_add_message(gd, nullptr, _("Edit Bookmark"), nullptr, FALSE);
278 generic_dialog_add_button(gd, GQ_ICON_OK, "OK",
279 bookmark_edit_ok_cb, TRUE);
281 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
282 pref_table_label(table, 0, 0, _("Name:"), GTK_ALIGN_END);
284 p->name_entry = gtk_entry_new();
285 gtk_widget_set_size_request(p->name_entry, 300, -1);
286 if (p->bb->name) gq_gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
287 gq_gtk_grid_attach_default(GTK_GRID(table), p->name_entry, 1, 2, 0, 1);
288 generic_dialog_attach_default(gd, p->name_entry);
289 gtk_widget_show(p->name_entry);
291 pref_table_label(table, 0, 1, _("Path:"), GTK_ALIGN_END);
293 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
294 "bookmark_path", -1, nullptr, nullptr);
295 tab_completion_add_select_button(p->path_entry, nullptr, TRUE);
296 gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 1, 2);
297 generic_dialog_attach_default(gd, p->path_entry);
298 gtk_widget_show(label);
300 pref_table_label(table, 0, 2, _("Icon:"), GTK_ALIGN_END);
303 if (!icon) icon = "";
304 label = tab_completion_new_with_history(&p->icon_entry, icon,
305 "bookmark_icons", -1, nullptr, nullptr);
306 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
307 gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 2, 3);
308 generic_dialog_attach_default(gd, p->icon_entry);
309 gtk_widget_show(label);
311 gtk_widget_show(gd->dialog);
314 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
319 const gchar *key_holder;
321 if (!bm->editable) return;
323 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
326 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
327 p = g_list_index(list, button);
330 if (p < 0 || p + direction < 0) return;
332 key_holder = bm->key;
333 bm->key = "_TEMPHOLDER";
334 history_list_item_move(key_holder, b->key, -direction);
335 bookmark_populate_all(key_holder);
336 bm->key = key_holder;
338 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
341 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
343 auto bm = static_cast<BookMarkData *>(data);
345 if (!bm->active_button) return;
347 bookmark_edit(bm->key, bm->active_button->key, widget);
350 static void bookmark_menu_move(BookMarkData *bm, gint direction)
352 if (!bm->active_button) return;
354 bookmark_move(bm, bm->active_button->button, direction);
357 static void bookmark_menu_up_cb(GtkWidget *, gpointer data)
359 bookmark_menu_move(static_cast<BookMarkData *>(data), -1);
362 static void bookmark_menu_down_cb(GtkWidget *, gpointer data)
364 bookmark_menu_move(static_cast<BookMarkData *>(data), 1);
367 static void bookmark_menu_remove_cb(GtkWidget *, gpointer data)
369 auto bm = static_cast<BookMarkData *>(data);
371 if (!bm->active_button) return;
373 history_list_item_remove(bm->key, bm->active_button->key);
374 bookmark_populate_all(bm->key);
377 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button, gint, guint32, gboolean local)
382 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
385 bm->active_button = b;
387 menu = popup_menu_short_lived();
388 menu_item_add_icon_sensitive(menu, _("_Properties..."), PIXBUF_INLINE_ICON_PROPERTIES, bm->editable,
389 G_CALLBACK(bookmark_menu_prop_cb), bm);
390 menu_item_add_icon_sensitive(menu, _("Move _up"), GQ_ICON_GO_UP, bm->editable,
391 G_CALLBACK(bookmark_menu_up_cb), bm);
392 menu_item_add_icon_sensitive(menu, _("Move _down"), GQ_ICON_GO_DOWN, bm->editable,
393 G_CALLBACK(bookmark_menu_down_cb), bm);
394 menu_item_add_icon_sensitive(menu, _("_Remove"), GQ_ICON_REMOVE, bm->editable,
395 G_CALLBACK(bookmark_menu_remove_cb), bm);
399 gtk_menu_popup_at_widget(GTK_MENU(menu), button, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_CENTER, nullptr);
403 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
407 static gboolean bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
409 auto bm = static_cast<BookMarkData *>(data);
411 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
413 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
418 static gboolean bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
420 auto bm = static_cast<BookMarkData *>(data);
422 switch (event->keyval)
425 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
428 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
432 if (event->state & GDK_SHIFT_MASK)
434 bookmark_move(bm, button, -1);
439 if (event->state & GDK_SHIFT_MASK)
441 bookmark_move(bm, button, 1);
450 static void bookmark_drag_set_data(GtkWidget *button,
451 GdkDragContext *context, GtkSelectionData *selection_data,
452 guint, guint, gpointer data)
454 auto bm = static_cast<BookMarkData *>(data);
456 GList *list = nullptr;
459 if (gdk_drag_context_get_dest_window(context) == gtk_widget_get_window(bm->widget)) return;
461 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
464 list = g_list_append(list, b->path);
466 gchar **uris = uris_from_pathlist(list);
467 gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
470 char *str = g_strjoinv("\r\n", uris);
471 ret = gtk_selection_data_set_text(selection_data, str, -1);
479 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer)
482 GdkModifierType mask;
484 GtkAllocation allocation;
488 gtk_widget_get_allocation(button, &allocation);
490 pixbuf = gdk_pixbuf_get_from_window(gtk_widget_get_window(button),
491 allocation.x, allocation.y,
492 allocation.width, allocation.height);
493 seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(button)));
494 device = gdk_seat_get_pointer(seat);
495 gdk_window_get_device_position(gtk_widget_get_window(button), device, &x, &y, &mask);
497 gtk_drag_set_icon_pixbuf(context, pixbuf,
498 x - allocation.x, y - allocation.y);
499 g_object_unref(pixbuf);
502 static gboolean bookmark_path_tooltip_cb(GtkWidget *button, gpointer)
506 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
507 gtk_widget_set_tooltip_text(GTK_WIDGET(button), b->path);
512 static void bookmark_populate(BookMarkData *bm)
518 box = GTK_BOX(bm->box);
519 children = gtk_container_get_children(GTK_CONTAINER(box));
523 GtkWidget *widget = GTK_WIDGET(work->data);
525 gq_gtk_widget_destroy(widget);
528 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
533 if (!bookmark_default_list)
535 buf = bookmark_string(_("Home"), homedir(), nullptr);
536 history_list_add_to_key(bm->key, buf, 0);
539 if (g_strcmp0(bm->key, "shortcuts") != 0)
541 buf = bookmark_string(".", g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
542 history_list_add_to_key(bm->key, buf, 0);
546 path = g_build_filename(homedir(), "Desktop", NULL);
549 buf = bookmark_string(_("Desktop"), path, nullptr);
550 history_list_add_to_key(bm->key, buf, 0);
556 work = bookmark_default_list;
557 while (work && work->next)
561 name = static_cast<gchar *>(work->data);
563 path = static_cast<gchar *>(work->data);
566 if (strcmp(name, ".") == 0)
568 if (g_strcmp0(bm->key, "shortcuts") != 0)
570 buf = bookmark_string(name, g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
579 buf = bookmark_string(name, path, nullptr);
581 history_list_add_to_key(bm->key, buf, 0);
586 work = history_list_get_by_key(bm->key);
587 work = g_list_last(work);
592 b = bookmark_from_string(static_cast<const gchar *>(work->data));
595 if (strcmp(b->name, ".") == 0)
599 b->path = g_strdup(history_list_find_last_path_by_key("path_list"));
600 buf = bookmark_string(".", b->path, b->icon);
601 history_list_item_change("bookmarks", b->key, buf);
602 b->key = g_strdup(buf);
607 b->button = gtk_button_new();
608 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
609 gq_gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
610 gtk_widget_show(b->button);
612 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
613 b, reinterpret_cast<GDestroyNotify>(bookmark_free));
615 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
616 gq_gtk_container_add(GTK_WIDGET(b->button), box);
617 gtk_widget_show(box);
621 GdkPixbuf *pixbuf = nullptr;
624 iconl = path_from_utf8(b->icon);
625 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
629 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
636 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
638 pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), b->icon, w, GTK_ICON_LOOKUP_NO_SVG, nullptr);
648 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
650 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
651 GDK_INTERP_BILINEAR);
652 b->image = gtk_image_new_from_pixbuf(scaled);
653 g_object_unref(scaled);
654 g_object_unref(pixbuf);
658 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
663 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
665 gq_gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
666 gtk_widget_show(b->image);
668 b->label = gtk_label_new(b->name);
669 gq_gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
670 gtk_widget_show(b->label);
672 g_signal_connect(G_OBJECT(b->button), "clicked",
673 G_CALLBACK(bookmark_select_cb), bm);
674 g_signal_connect(G_OBJECT(b->button), "button_press_event",
675 G_CALLBACK(bookmark_press_cb), bm);
676 g_signal_connect(G_OBJECT(b->button), "key_press_event",
677 G_CALLBACK(bookmark_keypress_cb), bm);
679 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
680 bookmark_drag_types, bookmark_drag_types_n,
681 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
682 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
683 G_CALLBACK(bookmark_drag_set_data), bm);
684 g_signal_connect(G_OBJECT(b->button), "drag_begin",
685 G_CALLBACK(bookmark_drag_begin), bm);
687 gtk_widget_set_has_tooltip(GTK_WIDGET(b->button), TRUE);
688 g_signal_connect(G_OBJECT(b->button), "query_tooltip", G_CALLBACK(bookmark_path_tooltip_cb), bm);
695 static void bookmark_populate_all(const gchar *key)
701 work = bookmark_widget_list;
706 bm = static_cast<BookMarkData *>(work->data);
709 if (strcmp(bm->key, key) == 0)
711 bookmark_populate(bm);
716 static void bookmark_dnd_get_data(GtkWidget *, GdkDragContext *,
718 GtkSelectionData *selection_data, guint,
719 guint, gpointer data)
721 auto bm = static_cast<BookMarkData *>(data);
722 GList *list = nullptr;
723 GList *errors = nullptr;
728 if (!bm->editable) return;
730 uris = gtk_selection_data_get_uris(selection_data);
733 list = uri_pathlist_from_uris(uris, &errors);
736 warning_dialog_dnd_uri_error(errors);
737 g_list_free_full(errors, g_free);
744 auto path = static_cast<gchar *>(work->data);
749 if (bm->only_directories && !isdir(path)) continue;
751 real_path = realpath(path, nullptr);
753 if (strstr(real_path, get_collections_dir()) && isfile(path))
755 buf = bookmark_string(filename_from_path(path), path, "gq-icon-collection");
757 else if (isfile(path))
759 buf = bookmark_string(filename_from_path(path), path, GQ_ICON_FILE);
763 buf = bookmark_string(filename_from_path(path), path, nullptr);
765 history_list_add_to_key(bm->key, buf, 0);
770 g_list_free_full(list, g_free);
772 bookmark_populate_all(bm->key);
776 static void bookmark_list_destroy(GtkWidget *, gpointer data)
778 auto bm = static_cast<BookMarkData *>(data);
780 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
782 g_free(const_cast<gchar *>(bm->key));
786 GtkWidget *bookmark_list_new(const gchar *key,
787 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
792 if (!key) key = "bookmarks";
794 bm = g_new0(BookMarkData, 1);
795 bm->key = g_strdup(key);
797 bm->select_func = select_func;
798 bm->select_data = select_data;
800 bm->no_defaults = FALSE;
802 bm->only_directories = FALSE;
804 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
809 layout = gtk_widget_create_pango_layout(GTK_WIDGET(scrolled), "reasonable width");
810 pango_layout_get_pixel_size(layout, &width, &height);
811 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scrolled), width);
812 g_object_unref(layout);
814 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
816 bm->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
817 gq_gtk_container_add(GTK_WIDGET(scrolled), bm->box);
818 gtk_widget_show(bm->box);
820 bookmark_populate(bm);
822 g_signal_connect(G_OBJECT(bm->box), "destroy",
823 G_CALLBACK(bookmark_list_destroy), bm);
824 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
825 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
826 bm->widget = scrolled;
828 gtk_drag_dest_set(scrolled,
829 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
830 bookmark_drop_types, bookmark_drop_types_n,
831 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
832 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
833 G_CALLBACK(bookmark_dnd_get_data), bm);
835 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
840 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
844 if (!list || !key) return;
846 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
849 if (bm->key && strcmp(bm->key, key) == 0) return;
851 g_free(const_cast<gchar *>(bm->key));
852 bm->key = g_strdup(key);
854 bookmark_populate(bm);
857 void bookmark_list_set_no_defaults(GtkWidget *list, gboolean no_defaults)
861 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
864 bm->no_defaults = no_defaults;
867 void bookmark_list_set_editable(GtkWidget *list, gboolean editable)
871 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
874 bm->editable = editable;
877 void bookmark_list_set_only_directories(GtkWidget *list, gboolean only_directories)
881 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
884 bm->only_directories = only_directories;
887 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
892 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
895 std::unique_ptr<gchar, decltype(&g_free)> buf(bookmark_string(name, path, nullptr), g_free);
896 real_path = realpath(path, nullptr);
898 if (strstr(real_path, get_collections_dir()) && isfile(path))
900 buf.reset(bookmark_string(name, path, "gq-icon-collection"));
906 buf.reset(bookmark_string(name, path, GQ_ICON_FILE));
910 buf.reset(bookmark_string(name, path, nullptr));
914 history_list_add_to_key(bm->key, buf.get(), 0);
917 bookmark_populate_all(bm->key);
920 void bookmark_add_default(const gchar *name, const gchar *path)
922 if (!name || !path) return;
923 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
924 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
928 *-----------------------------------------------------------------------------
929 * combo with history key
930 *-----------------------------------------------------------------------------
933 struct HistoryComboData
941 static void history_combo_destroy(GtkWidget *, gpointer data)
943 auto hc = static_cast<HistoryComboData *>(data);
945 g_free(hc->history_key);
949 /* if text is NULL, entry is set to the most recent item */
950 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
951 const gchar *history_key, gint max_levels)
953 HistoryComboData *hc;
957 hc = g_new0(HistoryComboData, 1);
958 hc->history_key = g_strdup(history_key);
959 hc->history_levels = max_levels;
961 hc->combo = gtk_combo_box_text_new_with_entry();
963 hc->entry = gtk_bin_get_child(GTK_BIN(hc->combo));
965 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
966 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
967 g_signal_connect(G_OBJECT(hc->combo), "destroy",
968 G_CALLBACK(history_combo_destroy), hc);
970 work = history_list_get_by_key(hc->history_key);
973 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
980 gq_gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
984 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
987 if (entry) *entry = hc->entry;
991 /* if text is NULL, current entry text is used
992 * widget can be the combo or entry widget
994 void history_combo_append_history(GtkWidget *widget, const gchar *text)
996 HistoryComboData *hc;
999 hc = static_cast<HistoryComboData *>(g_object_get_data(G_OBJECT(widget), "history_combo_data"));
1002 log_printf("widget is not a history combo\n");
1008 new_text = g_strdup(text);
1012 new_text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1015 if (new_text && strlen(new_text) > 0)
1017 GtkTreeModel *store;
1020 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1022 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1024 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1025 gtk_list_store_clear(GTK_LIST_STORE(store));
1027 work = history_list_get_by_key(hc->history_key);
1030 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
1037 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */