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 }
100 bookmark_drop_types_n = 3
103 static GtkTargetEntry bookmark_drag_types[] = {
104 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
105 { const_cast<gchar *>("text/plain"), 0, TARGET_TEXT_PLAIN }
108 bookmark_drag_types_n = 2
112 static GList *bookmark_widget_list = nullptr;
113 static GList *bookmark_default_list = nullptr;
116 static void bookmark_populate_all(const gchar *key);
119 static BookButtonData *bookmark_from_string(const gchar *text)
122 const gchar *path_ptr;
123 const gchar *icon_ptr;
125 b = g_new0(BookButtonData, 1);
129 b->name = g_strdup(_("New Bookmark"));
130 b->path = g_strdup(homedir());
135 b->key = g_strdup(text);
137 path_ptr = strstr(text, MARKER_PATH);
138 icon_ptr = strstr(text, MARKER_ICON);
140 if (path_ptr && icon_ptr && icon_ptr < path_ptr)
142 log_printf("warning, bookmark icon must be after path\n");
151 b->name = g_strndup(text, l);
152 path_ptr += strlen(MARKER_PATH);
155 l = icon_ptr - path_ptr;
156 b->path = g_strndup(path_ptr, l);
160 b->path = g_strdup(path_ptr);
165 b->name = g_strdup(text);
166 b->path = g_strdup("");
171 icon_ptr += strlen(MARKER_ICON);
172 b->icon = g_strdup(icon_ptr);
178 static void bookmark_free(BookButtonData *b)
190 static gchar *bookmark_string(const gchar *name, const gchar *path, const gchar *icon)
192 if (!name) name = _("New Bookmark");
196 return g_strdup_printf("%s" MARKER_PATH "%s" MARKER_ICON "%s", name, path, icon);
199 return g_strdup_printf("%s" MARKER_PATH "%s", name, path);
202 static void bookmark_select_cb(GtkWidget *button, gpointer data)
204 auto bm = static_cast<BookMarkData *>(data);
207 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
210 if (bm->select_func) bm->select_func(b->path, bm->select_data);
213 static void bookmark_edit_destroy_cb(GtkWidget *, gpointer data)
215 auto p = static_cast<BookPropData *>(data);
217 bookmark_free(p->bb);
221 static void bookmark_edit_cancel_cb(GenericDialog *, gpointer)
225 static void bookmark_edit_ok_cb(GenericDialog *, gpointer data)
227 auto p = static_cast<BookPropData *>(data);
233 name = gq_gtk_entry_get_text(GTK_ENTRY(p->name_entry));
234 path = remove_trailing_slash(gq_gtk_entry_get_text(GTK_ENTRY(p->path_entry)));
235 icon = gq_gtk_entry_get_text(GTK_ENTRY(p->icon_entry));
237 new_string = bookmark_string(name, path, icon);
241 history_list_item_change(p->bb->parent, p->bb->key, new_string);
245 history_list_add_to_key(p->bb->parent, new_string, 0);
248 if (path && strlen(path) > 0) tab_completion_append_to_history(p->path_entry, path);
249 if (icon && strlen(icon) > 0) tab_completion_append_to_history(p->icon_entry, icon);
254 bookmark_populate_all(p->bb->parent);
257 /* simply pass NULL for text to turn this into a 'new bookmark' dialog */
259 static void bookmark_edit(const gchar *key, const gchar *text, GtkWidget *parent)
267 if (!key) key = "bookmarks";
269 p = g_new0(BookPropData, 1);
271 p->bb = bookmark_from_string(text);
272 p->bb->parent = g_strdup(key);
274 gd = generic_dialog_new(_("Edit Bookmark"), "bookmark_edit",
276 bookmark_edit_cancel_cb, p);
277 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
278 G_CALLBACK(bookmark_edit_destroy_cb), p);
280 generic_dialog_add_message(gd, nullptr, _("Edit Bookmark"), nullptr, FALSE);
282 generic_dialog_add_button(gd, GQ_ICON_OK, "OK",
283 bookmark_edit_ok_cb, TRUE);
285 table = pref_table_new(gd->vbox, 3, 2, FALSE, TRUE);
286 pref_table_label(table, 0, 0, _("Name:"), GTK_ALIGN_END);
288 p->name_entry = gtk_entry_new();
289 gtk_widget_set_size_request(p->name_entry, 300, -1);
290 if (p->bb->name) gq_gtk_entry_set_text(GTK_ENTRY(p->name_entry), p->bb->name);
291 gq_gtk_grid_attach_default(GTK_GRID(table), p->name_entry, 1, 2, 0, 1);
292 generic_dialog_attach_default(gd, p->name_entry);
293 gtk_widget_show(p->name_entry);
295 pref_table_label(table, 0, 1, _("Path:"), GTK_ALIGN_END);
297 label = tab_completion_new_with_history(&p->path_entry, p->bb->path,
298 "bookmark_path", -1, nullptr, nullptr);
299 tab_completion_add_select_button(p->path_entry, nullptr, TRUE);
300 gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 1, 2);
301 generic_dialog_attach_default(gd, p->path_entry);
302 gtk_widget_show(label);
304 pref_table_label(table, 0, 2, _("Icon:"), GTK_ALIGN_END);
307 if (!icon) icon = "";
308 label = tab_completion_new_with_history(&p->icon_entry, icon,
309 "bookmark_icons", -1, nullptr, nullptr);
310 tab_completion_add_select_button(p->icon_entry, _("Select icon"), FALSE);
311 gq_gtk_grid_attach_default(GTK_GRID(table), label, 1, 2, 2, 3);
312 generic_dialog_attach_default(gd, p->icon_entry);
313 gtk_widget_show(label);
315 gtk_widget_show(gd->dialog);
318 static void bookmark_move(BookMarkData *bm, GtkWidget *button, gint direction)
323 const gchar *key_holder;
325 if (!bm->editable) return;
327 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
330 list = gtk_container_get_children(GTK_CONTAINER(bm->box));
331 p = g_list_index(list, button);
334 if (p < 0 || p + direction < 0) return;
336 key_holder = bm->key;
337 bm->key = "_TEMPHOLDER";
338 history_list_item_move(key_holder, b->key, -direction);
339 bookmark_populate_all(key_holder);
340 bm->key = key_holder;
342 gtk_box_reorder_child(GTK_BOX(bm->box), button, p + direction);
345 static void bookmark_menu_prop_cb(GtkWidget *widget, gpointer data)
347 auto bm = static_cast<BookMarkData *>(data);
349 if (!bm->active_button) return;
351 bookmark_edit(bm->key, bm->active_button->key, widget);
354 static void bookmark_menu_move(BookMarkData *bm, gint direction)
356 if (!bm->active_button) return;
358 bookmark_move(bm, bm->active_button->button, direction);
361 static void bookmark_menu_up_cb(GtkWidget *, gpointer data)
363 bookmark_menu_move(static_cast<BookMarkData *>(data), -1);
366 static void bookmark_menu_down_cb(GtkWidget *, gpointer data)
368 bookmark_menu_move(static_cast<BookMarkData *>(data), 1);
371 static void bookmark_menu_remove_cb(GtkWidget *, gpointer data)
373 auto bm = static_cast<BookMarkData *>(data);
375 if (!bm->active_button) return;
377 history_list_item_remove(bm->key, bm->active_button->key);
378 bookmark_populate_all(bm->key);
381 static void bookmark_menu_popup(BookMarkData *bm, GtkWidget *button, gint, guint32, gboolean local)
386 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
389 bm->active_button = b;
391 menu = popup_menu_short_lived();
392 menu_item_add_icon_sensitive(menu, _("_Properties..."), PIXBUF_INLINE_ICON_PROPERTIES, bm->editable,
393 G_CALLBACK(bookmark_menu_prop_cb), bm);
394 menu_item_add_icon_sensitive(menu, _("Move _up"), GQ_ICON_GO_UP, bm->editable,
395 G_CALLBACK(bookmark_menu_up_cb), bm);
396 menu_item_add_icon_sensitive(menu, _("Move _down"), GQ_ICON_GO_DOWN, bm->editable,
397 G_CALLBACK(bookmark_menu_down_cb), bm);
398 menu_item_add_icon_sensitive(menu, _("_Remove"), GQ_ICON_REMOVE, bm->editable,
399 G_CALLBACK(bookmark_menu_remove_cb), bm);
403 gtk_menu_popup_at_widget(GTK_MENU(menu), button, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_CENTER, nullptr);
407 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
411 static gboolean bookmark_press_cb(GtkWidget *button, GdkEventButton *event, gpointer data)
413 auto bm = static_cast<BookMarkData *>(data);
415 if (event->button != MOUSE_BUTTON_RIGHT) return FALSE;
417 bookmark_menu_popup(bm, button, event->button, event->time, FALSE);
422 static gboolean bookmark_keypress_cb(GtkWidget *button, GdkEventKey *event, gpointer data)
424 auto bm = static_cast<BookMarkData *>(data);
426 switch (event->keyval)
429 if (!(event->state & GDK_CONTROL_MASK)) return FALSE;
432 bookmark_menu_popup(bm, button, 0, event->time, TRUE);
436 if (event->state & GDK_SHIFT_MASK)
438 bookmark_move(bm, button, -1);
443 if (event->state & GDK_SHIFT_MASK)
445 bookmark_move(bm, button, 1);
454 static void bookmark_drag_set_data(GtkWidget *button,
455 GdkDragContext *context, GtkSelectionData *selection_data,
456 guint, guint, gpointer data)
458 auto bm = static_cast<BookMarkData *>(data);
460 GList *list = nullptr;
463 if (gdk_drag_context_get_dest_window(context) == gtk_widget_get_window(bm->widget)) return;
465 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
468 list = g_list_append(list, b->path);
470 gchar **uris = uris_from_pathlist(list);
471 gboolean ret = gtk_selection_data_set_uris(selection_data, uris);
474 char *str = g_strjoinv("\r\n", uris);
475 ret = gtk_selection_data_set_text(selection_data, str, -1);
483 static void bookmark_drag_begin(GtkWidget *button, GdkDragContext *context, gpointer)
486 GdkModifierType mask;
489 GtkAllocation allocation;
493 gtk_widget_get_allocation(button, &allocation);
495 pixbuf = gdk_pixbuf_get_from_window(gtk_widget_get_window(button),
496 allocation.x, allocation.y,
497 allocation.width, allocation.height);
498 seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(button)));
499 device = gdk_seat_get_pointer(seat);
500 gdk_window_get_device_position(gtk_widget_get_window(button), device, &x, &y, &mask);
502 gtk_drag_set_icon_pixbuf(context, pixbuf,
503 x - allocation.x, y - allocation.y);
504 g_object_unref(pixbuf);
507 static gboolean bookmark_path_tooltip_cb(GtkWidget *button, gpointer)
511 b = static_cast<BookButtonData *>(g_object_get_data(G_OBJECT(button), "bookbuttondata"));
512 gtk_widget_set_tooltip_text(GTK_WIDGET(button), b->path);
517 static void bookmark_populate(BookMarkData *bm)
523 box = GTK_BOX(bm->box);
524 children = gtk_container_get_children(GTK_CONTAINER(box));
528 GtkWidget *widget = GTK_WIDGET(work->data);
530 gq_gtk_widget_destroy(widget);
533 if (!bm->no_defaults && !history_list_get_by_key(bm->key))
538 if (!bookmark_default_list)
540 buf = bookmark_string(_("Home"), homedir(), nullptr);
541 history_list_add_to_key(bm->key, buf, 0);
544 if (g_strcmp0(bm->key, "shortcuts") != 0)
546 buf = bookmark_string(".", g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
547 history_list_add_to_key(bm->key, buf, 0);
551 path = g_build_filename(homedir(), "Desktop", NULL);
554 buf = bookmark_string(_("Desktop"), path, nullptr);
555 history_list_add_to_key(bm->key, buf, 0);
561 work = bookmark_default_list;
562 while (work && work->next)
566 name = static_cast<gchar *>(work->data);
568 path = static_cast<gchar *>(work->data);
571 if (strcmp(name, ".") == 0)
573 if (g_strcmp0(bm->key, "shortcuts") != 0)
575 buf = bookmark_string(name, g_strdup(history_list_find_last_path_by_key("path_list")), nullptr);
584 buf = bookmark_string(name, path, nullptr);
586 history_list_add_to_key(bm->key, buf, 0);
591 work = history_list_get_by_key(bm->key);
592 work = g_list_last(work);
597 b = bookmark_from_string(static_cast<const gchar *>(work->data));
600 if (strcmp(b->name, ".") == 0)
604 b->path = g_strdup(history_list_find_last_path_by_key("path_list"));
605 buf = bookmark_string(".", b->path, b->icon);
606 history_list_item_change("bookmarks", b->key, buf);
607 b->key = g_strdup(buf);
612 b->button = gtk_button_new();
613 gtk_button_set_relief(GTK_BUTTON(b->button), GTK_RELIEF_NONE);
614 gq_gtk_box_pack_start(GTK_BOX(bm->box), b->button, FALSE, FALSE, 0);
615 gtk_widget_show(b->button);
617 g_object_set_data_full(G_OBJECT(b->button), "bookbuttondata",
618 b, reinterpret_cast<GDestroyNotify>(bookmark_free));
620 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_GAP);
621 gq_gtk_container_add(GTK_WIDGET(b->button), box);
622 gtk_widget_show(box);
626 GdkPixbuf *pixbuf = nullptr;
629 iconl = path_from_utf8(b->icon);
630 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
634 pixbuf = gdk_pixbuf_new_from_file(iconl, nullptr);
642 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
644 pixbuf = gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), b->icon, w, GTK_ICON_LOOKUP_NO_SVG, nullptr);
655 gtk_icon_size_lookup(GTK_ICON_SIZE_BUTTON, &w, &h);
657 scaled = gdk_pixbuf_scale_simple(pixbuf, w, h,
658 GDK_INTERP_BILINEAR);
659 b->image = gtk_image_new_from_pixbuf(scaled);
660 g_object_unref(scaled);
661 g_object_unref(pixbuf);
665 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
670 b->image = gtk_image_new_from_icon_name(GQ_ICON_DIRECTORY, GTK_ICON_SIZE_BUTTON);
672 gq_gtk_box_pack_start(GTK_BOX(box), b->image, FALSE, FALSE, 0);
673 gtk_widget_show(b->image);
675 b->label = gtk_label_new(b->name);
676 gq_gtk_box_pack_start(GTK_BOX(box), b->label, FALSE, FALSE, 0);
677 gtk_widget_show(b->label);
679 g_signal_connect(G_OBJECT(b->button), "clicked",
680 G_CALLBACK(bookmark_select_cb), bm);
681 g_signal_connect(G_OBJECT(b->button), "button_press_event",
682 G_CALLBACK(bookmark_press_cb), bm);
683 g_signal_connect(G_OBJECT(b->button), "key_press_event",
684 G_CALLBACK(bookmark_keypress_cb), bm);
686 gtk_drag_source_set(b->button, GDK_BUTTON1_MASK,
687 bookmark_drag_types, bookmark_drag_types_n,
688 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
689 g_signal_connect(G_OBJECT(b->button), "drag_data_get",
690 G_CALLBACK(bookmark_drag_set_data), bm);
691 g_signal_connect(G_OBJECT(b->button), "drag_begin",
692 G_CALLBACK(bookmark_drag_begin), bm);
694 gtk_widget_set_has_tooltip(GTK_WIDGET(b->button), TRUE);
695 g_signal_connect(G_OBJECT(b->button), "query_tooltip", G_CALLBACK(bookmark_path_tooltip_cb), bm);
702 static void bookmark_populate_all(const gchar *key)
708 work = bookmark_widget_list;
713 bm = static_cast<BookMarkData *>(work->data);
716 if (strcmp(bm->key, key) == 0)
718 bookmark_populate(bm);
723 static void bookmark_dnd_get_data(GtkWidget *, GdkDragContext *,
725 GtkSelectionData *selection_data, guint,
726 guint, gpointer data)
728 auto bm = static_cast<BookMarkData *>(data);
729 GList *list = nullptr;
730 GList *errors = nullptr;
735 if (!bm->editable) return;
737 uris = gtk_selection_data_get_uris(selection_data);
740 list = uri_pathlist_from_uris(uris, &errors);
743 warning_dialog_dnd_uri_error(errors);
744 g_list_free_full(errors, g_free);
751 auto path = static_cast<gchar *>(work->data);
756 if (bm->only_directories && !isdir(path)) continue;
758 real_path = realpath(path, nullptr);
760 if (strstr(real_path, get_collections_dir()) && isfile(path))
762 buf = bookmark_string(filename_from_path(path), path, "gq-icon-collection");
764 else if (isfile(path))
766 buf = bookmark_string(filename_from_path(path), path, GQ_ICON_FILE);
770 buf = bookmark_string(filename_from_path(path), path, nullptr);
772 history_list_add_to_key(bm->key, buf, 0);
777 g_list_free_full(list, g_free);
779 bookmark_populate_all(bm->key);
783 static void bookmark_list_destroy(GtkWidget *, gpointer data)
785 auto bm = static_cast<BookMarkData *>(data);
787 bookmark_widget_list = g_list_remove(bookmark_widget_list, bm);
789 g_free(const_cast<gchar *>(bm->key));
793 GtkWidget *bookmark_list_new(const gchar *key,
794 void (*select_func)(const gchar *path, gpointer data), gpointer select_data)
799 if (!key) key = "bookmarks";
801 bm = g_new0(BookMarkData, 1);
802 bm->key = g_strdup(key);
804 bm->select_func = select_func;
805 bm->select_data = select_data;
807 bm->no_defaults = FALSE;
809 bm->only_directories = FALSE;
811 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
817 layout = gtk_widget_create_pango_layout(GTK_WIDGET(scrolled), "reasonable width");
818 pango_layout_get_pixel_size(layout, &width, &height);
819 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scrolled), width);
820 g_object_unref(layout);
822 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
824 bm->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
825 gq_gtk_container_add(GTK_WIDGET(scrolled), bm->box);
826 gtk_widget_show(bm->box);
828 bookmark_populate(bm);
830 g_signal_connect(G_OBJECT(bm->box), "destroy",
831 G_CALLBACK(bookmark_list_destroy), bm);
832 g_object_set_data(G_OBJECT(bm->box), BOOKMARK_DATA_KEY, bm);
833 g_object_set_data(G_OBJECT(scrolled), BOOKMARK_DATA_KEY, bm);
834 bm->widget = scrolled;
836 gtk_drag_dest_set(scrolled,
837 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
838 bookmark_drop_types, bookmark_drop_types_n,
839 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK));
840 g_signal_connect(G_OBJECT(scrolled), "drag_data_received",
841 G_CALLBACK(bookmark_dnd_get_data), bm);
843 bookmark_widget_list = g_list_append(bookmark_widget_list, bm);
848 void bookmark_list_set_key(GtkWidget *list, const gchar *key)
852 if (!list || !key) return;
854 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
857 if (bm->key && strcmp(bm->key, key) == 0) return;
859 g_free(const_cast<gchar *>(bm->key));
860 bm->key = g_strdup(key);
862 bookmark_populate(bm);
865 void bookmark_list_set_no_defaults(GtkWidget *list, gboolean no_defaults)
869 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
872 bm->no_defaults = no_defaults;
875 void bookmark_list_set_editable(GtkWidget *list, gboolean editable)
879 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
882 bm->editable = editable;
885 void bookmark_list_set_only_directories(GtkWidget *list, gboolean only_directories)
889 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
892 bm->only_directories = only_directories;
895 void bookmark_list_add(GtkWidget *list, const gchar *name, const gchar *path)
900 bm = static_cast<BookMarkData *>(g_object_get_data(G_OBJECT(list), BOOKMARK_DATA_KEY));
903 std::unique_ptr<gchar, decltype(&g_free)> buf(bookmark_string(name, path, nullptr), g_free);
904 real_path = realpath(path, nullptr);
906 if (strstr(real_path, get_collections_dir()) && isfile(path))
908 buf.reset(bookmark_string(name, path, "gq-icon-collection"));
914 buf.reset(bookmark_string(name, path, GQ_ICON_FILE));
918 buf.reset(bookmark_string(name, path, nullptr));
922 history_list_add_to_key(bm->key, buf.get(), 0);
925 bookmark_populate_all(bm->key);
928 void bookmark_add_default(const gchar *name, const gchar *path)
930 if (!name || !path) return;
931 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(name));
932 bookmark_default_list = g_list_append(bookmark_default_list, g_strdup(path));
936 *-----------------------------------------------------------------------------
937 * combo with history key
938 *-----------------------------------------------------------------------------
941 struct HistoryComboData
949 static void history_combo_destroy(GtkWidget *, gpointer data)
951 auto hc = static_cast<HistoryComboData *>(data);
953 g_free(hc->history_key);
957 /* if text is NULL, entry is set to the most recent item */
958 GtkWidget *history_combo_new(GtkWidget **entry, const gchar *text,
959 const gchar *history_key, gint max_levels)
961 HistoryComboData *hc;
965 hc = g_new0(HistoryComboData, 1);
966 hc->history_key = g_strdup(history_key);
967 hc->history_levels = max_levels;
969 hc->combo = gtk_combo_box_text_new_with_entry();
971 hc->entry = gtk_bin_get_child(GTK_BIN(hc->combo));
973 g_object_set_data(G_OBJECT(hc->combo), "history_combo_data", hc);
974 g_object_set_data(G_OBJECT(hc->entry), "history_combo_data", hc);
975 g_signal_connect(G_OBJECT(hc->combo), "destroy",
976 G_CALLBACK(history_combo_destroy), hc);
978 work = history_list_get_by_key(hc->history_key);
981 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
988 gq_gtk_entry_set_text(GTK_ENTRY(hc->entry), text);
992 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), 0);
995 if (entry) *entry = hc->entry;
999 /* if text is NULL, current entry text is used
1000 * widget can be the combo or entry widget
1002 void history_combo_append_history(GtkWidget *widget, const gchar *text)
1004 HistoryComboData *hc;
1007 hc = static_cast<HistoryComboData *>(g_object_get_data(G_OBJECT(widget), "history_combo_data"));
1010 log_printf("widget is not a history combo\n");
1016 new_text = g_strdup(text);
1020 new_text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(hc->entry)));
1023 if (new_text && strlen(new_text) > 0)
1025 GtkTreeModel *store;
1028 history_list_add_to_key(hc->history_key, new_text, hc->history_levels);
1030 gtk_combo_box_set_active(GTK_COMBO_BOX(hc->combo), -1);
1032 store = gtk_combo_box_get_model(GTK_COMBO_BOX(hc->combo));
1033 gtk_list_store_clear(GTK_LIST_STORE(store));
1035 work = history_list_get_by_key(hc->history_key);
1038 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hc->combo), static_cast<gchar *>(work->data));
1045 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */