2 * Copyright (C) 2006 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.
24 #include <sys/types.h>
27 #include "ui-tabcomp.h"
29 #include "history-list.h"
30 #include "misc.h" /* expand_tilde() */
31 #include "ui-fileops.h"
32 #include "ui-utildlg.h"
34 /* define this to enable a pop-up menu that shows possible matches
35 * #define TAB_COMPLETION_ENABLE_POPUP_MENU
37 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1
38 #define TAB_COMP_POPUP_MAX 1000
40 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
47 * ----------------------------------------------------------------
48 * Tab completion routines, can be connected to any gtkentry widget
49 * using the tab_completion_add_to_entry() function.
51 * Use remove_trailing_slash() to strip the trailing G_DIR_SEPARATOR.
53 * ----------------------------------------------------------------
61 void (*enter_func)(const gchar *, gpointer);
62 void (*tab_func)(const gchar *, gpointer);
63 void (*tab_append_func)(const gchar *, gpointer, gint);
67 gpointer tab_append_data;
76 gboolean fd_folders_only;
85 static void tab_completion_select_show(TabCompData *td);
86 static gint tab_completion_do(TabCompData *td);
88 static void tab_completion_free_list(TabCompData *td)
91 td->dir_path = nullptr;
93 g_list_free_full(td->file_list, g_free);
94 td->file_list = nullptr;
97 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
101 GList *list = nullptr;
104 tab_completion_free_list(td);
106 pathl = path_from_utf8(path);
114 while ((dir = readdir(dp)) != nullptr)
116 gchar *name = dir->d_name;
117 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0 &&
118 (name[0] != '.' || options->file_filter.show_hidden_files))
120 gchar *abspath = g_build_filename(pathl, name, NULL);
122 if (g_file_test(abspath, G_FILE_TEST_IS_DIR))
124 gchar *dname = g_strconcat(name, G_DIR_SEPARATOR_S, NULL);
125 list = g_list_prepend(list, path_to_utf8(dname));
130 list = g_list_prepend(list, path_to_utf8(name));
137 td->dir_path = g_strdup(path);
138 td->file_list = list;
142 static void tab_completion_destroy(GtkWidget *, gpointer data)
144 auto td = static_cast<TabCompData *>(data);
146 tab_completion_free_list(td);
147 g_free(td->history_key);
149 if (td->fd) file_dialog_close(td->fd);
150 g_free(td->fd_title);
153 g_free(td->filter_desc);
158 static gchar *tab_completion_get_text(TabCompData *td)
162 text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(td->entry)));
167 text = expand_tilde(text);
174 static gboolean tab_completion_emit_enter_signal(TabCompData *td)
177 if (!td->enter_func) return FALSE;
179 text = tab_completion_get_text(td);
180 td->enter_func(text, td->enter_data);
186 static void tab_completion_emit_tab_signal(TabCompData *td)
189 if (!td->tab_func) return;
191 text = tab_completion_get_text(td);
192 td->tab_func(text, td->tab_data);
196 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
197 void tab_completion_iter_menu_items(GtkWidget *widget, gpointer data)
199 auto td = static_cast<TabCompData *>(data);
202 if (!gtk_widget_get_visible(widget)) return;
204 child = gtk_bin_get_child(GTK_BIN(widget));
205 if (GTK_IS_LABEL(child)) {
206 const gchar *text = gtk_label_get_text(GTK_LABEL(child));
207 const gchar *entry_text = gq_gtk_entry_get_text(GTK_ENTRY(td->entry));
208 const gchar *prefix = filename_from_path(entry_text);
209 guint prefix_len = strlen(prefix);
211 if (strlen(text) < prefix_len || strncmp(text, prefix, prefix_len))
213 /* Hide menu items not matching */
214 gtk_widget_hide(widget);
218 /* Count how many choices are left in the menu */
224 static gboolean tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
226 auto td = static_cast<TabCompData *>(data);
228 if (event->keyval == GDK_KEY_Tab ||
229 event->keyval == GDK_KEY_BackSpace ||
230 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
232 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
237 buf[0] = event->keyval;
239 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
240 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
242 /* Reduce the number of entries in the menu */
244 gtk_container_foreach(GTK_CONTAINER(widget), tab_completion_iter_menu_items, td);
245 if (td->choices > 1) return TRUE; /* multiple choices */
246 if (td->choices > 0) tab_completion_do(td); /* one choice */
250 gtk_menu_popdown(GTK_MENU(widget));
251 /* doing this does not emit the "selection done" signal, unref it ourselves */
252 g_object_unref(widget);
259 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
261 auto name = static_cast<gchar *>(data);
265 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(widget), "tab_completion_data"));
268 buf = g_build_filename(td->dir_path, name, NULL);
269 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
270 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
273 tab_completion_emit_tab_signal(td);
276 static void tab_completion_popup_list(TabCompData *td, GList *list)
286 * well, the menu would be too long anyway...
287 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
288 * this is why menu popups are disabled, this really should be a popup scrollable listview.
290 if (g_list_length(list) > 200) return;
293 menu = popup_menu_short_lived();
296 while (work && count < TAB_COMP_POPUP_MAX)
298 auto name = static_cast<gchar *>(work->data);
301 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
302 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
308 g_signal_connect(G_OBJECT(menu), "key_press_event",
309 G_CALLBACK(tab_completion_popup_key_press), td);
311 gtk_menu_popup_at_widget(GTK_MENU(menu), td->entry, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_NORTH, nullptr);
315 #define CASE_SORT strcmp
318 static gint simple_sort(gconstpointer a, gconstpointer b)
320 return CASE_SORT((gchar *)a, (gchar *)b);
325 static gboolean tab_completion_do(TabCompData *td)
327 const gchar *entry_text = gq_gtk_entry_get_text(GTK_ENTRY(td->entry));
328 const gchar *entry_file;
331 gboolean home_exp = FALSE;
333 if (entry_text[0] == '\0')
335 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME root directory win32 */
336 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
337 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
342 /* home dir expansion */
343 if (entry_text[0] == '~')
345 entry_dir = expand_tilde(entry_text);
350 entry_dir = g_strdup(entry_text);
353 if (isfile(entry_dir))
357 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
358 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
364 entry_file = filename_from_path(entry_text);
366 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
368 ptr = entry_dir + strlen(entry_dir) - 1;
369 if (ptr[0] == G_DIR_SEPARATOR)
373 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
374 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
377 tab_completion_read_dir(td, entry_dir);
378 td->file_list = g_list_sort(td->file_list, simple_sort);
379 if (td->file_list && !td->file_list->next)
384 file = static_cast<const gchar *>(td->file_list->data);
385 buf = g_build_filename(entry_dir, file, NULL);
388 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
392 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
393 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
397 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
401 tab_completion_popup_list(td, td->file_list);
410 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
411 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
412 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
419 ptr = const_cast<gchar *>(filename_from_path(entry_dir));
420 if (ptr > entry_dir) ptr--;
423 if (strlen(entry_dir) == 0)
426 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME win32 */
429 if (isdir(entry_dir))
432 GList *poss = nullptr;
433 gint l = strlen(entry_file);
435 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
437 tab_completion_read_dir(td, entry_dir);
440 list = td->file_list;
443 auto file = static_cast<gchar *>(list->data);
444 if (strncmp(entry_file, file, l) == 0)
446 poss = g_list_prepend(poss, file);
455 auto file = static_cast<gchar *>(poss->data);
458 buf = g_build_filename(entry_dir, file, NULL);
459 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
460 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
468 gsize c = strlen(entry_file);
469 gboolean done = FALSE;
470 auto test_file = static_cast<gchar *>(poss->data);
475 if (!list) done = TRUE;
476 while (list && !done)
478 auto file = static_cast<gchar *>(list->data);
479 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
492 file = g_strdup(test_file);
494 buf = g_build_filename(entry_dir, file, NULL);
495 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
496 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
498 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
500 poss = g_list_sort(poss, simple_sort);
501 tab_completion_popup_list(td, poss);
521 static gboolean tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
523 auto td = static_cast<TabCompData *>(data);
524 gboolean stop_signal = FALSE;
526 switch (event->keyval)
529 if (!(event->state & GDK_CONTROL_MASK))
531 if (tab_completion_do(td))
533 tab_completion_emit_tab_signal(td);
538 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
540 (event->state & GDK_CONTROL_MASK))
542 tab_completion_select_show(td);
545 else if (tab_completion_emit_enter_signal(td))
554 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
556 return (stop_signal);
559 static void tab_completion_button_pressed(GtkWidget *, gpointer data)
562 auto entry = static_cast<GtkWidget *>(data);
564 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
568 if (!gtk_widget_has_focus(entry))
570 gtk_widget_grab_focus(entry);
573 if (tab_completion_do(td))
575 tab_completion_emit_tab_signal(td);
579 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
581 auto parent = static_cast<GtkWidget *>(data);
582 GtkAllocation parent_allocation;
583 gtk_widget_get_allocation(parent, &parent_allocation);
585 if (allocation->height > parent_allocation.height)
587 GtkAllocation button_allocation;
589 gtk_widget_get_allocation(button, &button_allocation);
590 button_allocation.height = parent_allocation.height;
591 button_allocation.y = parent_allocation.y +
592 (parent_allocation.height - parent_allocation.height) / 2;
593 gtk_widget_size_allocate(button, &button_allocation);
597 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
601 button = gtk_button_new_from_icon_name(GQ_ICON_GO_LAST, GTK_ICON_SIZE_BUTTON);
602 gtk_widget_set_can_focus(button, FALSE);
603 g_signal_connect(G_OBJECT(button), "size_allocate",
604 G_CALLBACK(tab_completion_button_size_allocate), parent);
605 g_signal_connect(G_OBJECT(button), "clicked",
606 G_CALLBACK(tab_completion_button_pressed), entry);
612 *----------------------------------------------------------------------------
614 *----------------------------------------------------------------------------
617 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
618 const gchar *history_key, gint max_levels,
619 void (*enter_func)(const gchar *, gpointer), gpointer data)
623 GtkWidget *combo_entry;
629 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
631 combo = gtk_combo_box_text_new_with_entry();
632 gq_gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
633 gtk_widget_show(combo);
635 combo_entry = gtk_bin_get_child(GTK_BIN(combo));
637 button = tab_completion_create_complete_button(combo_entry, combo);
638 gq_gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
639 gtk_widget_show(button);
641 tab_completion_add_to_entry(combo_entry, enter_func, nullptr, nullptr, data);
643 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data"));
644 if (!td) return nullptr; /* this should never happen! */
647 td->has_history = TRUE;
648 td->history_key = g_strdup(history_key);
649 td->history_levels = max_levels;
651 work = history_list_get_by_key(td->history_key);
653 work = history_list_get_by_key(history_key);
656 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), static_cast<gchar *>(work->data));
663 gq_gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
667 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
670 if (entry) *entry = combo_entry;
674 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
676 auto td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
679 if (!td || !td->has_history) return nullptr;
681 buf = history_list_find_last_path_by_key(td->history_key);
684 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
690 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
697 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
701 if (!td || !td->has_history) return;
703 history_list_add_to_key(td->history_key, path, td->history_levels);
705 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
707 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
708 gtk_list_store_clear(GTK_LIST_STORE(store));
710 work = history_list_get_by_key(td->history_key);
713 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(td->combo), static_cast<gchar *>(work->data));
718 if (td->tab_append_func) {
719 td->tab_append_func(path, td->tab_append_data, n);
723 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
724 void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
730 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
732 newentry = gtk_entry_new();
733 if (text) gq_gtk_entry_set_text(GTK_ENTRY(newentry), text);
734 gq_gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
735 gtk_widget_show(newentry);
737 button = tab_completion_create_complete_button(newentry, newentry);
738 gq_gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
739 gtk_widget_show(button);
741 tab_completion_add_to_entry(newentry, enter_func, filter, filter_desc, data);
742 if (entry) *entry = newentry;
746 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
751 log_printf("Tab completion error: entry != NULL\n");
755 td = g_new0(TabCompData, 1);
758 td->enter_func = enter_func;
759 td->enter_data = data;
760 td->filter = g_strdup(filter);
761 td->filter_desc = g_strdup(filter_desc);
763 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
765 g_signal_connect(G_OBJECT(entry), "key_press_event",
766 G_CALLBACK(tab_completion_key_pressed), td);
767 g_signal_connect(G_OBJECT(entry), "destroy",
768 G_CALLBACK(tab_completion_destroy), td);
771 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
773 auto td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
777 td->tab_func = tab_func;
781 /* Add a callback function called when a new entry is appended to the list */
782 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
784 auto td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
788 td->tab_append_func = tab_append_func;
789 td->tab_append_data = data;
792 gchar *remove_trailing_slash(const gchar *path)
796 if (!path) return nullptr;
799 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
801 return g_strndup(path, l);
804 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
806 auto td = static_cast<TabCompData *>(data);
809 file_dialog_close(fd);
812 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
814 auto td = static_cast<TabCompData *>(data);
816 gq_gtk_entry_set_text(GTK_ENTRY(td->entry), gq_gtk_entry_get_text(GTK_ENTRY(fd->entry)));
818 tab_completion_select_cancel_cb(fd, data);
820 tab_completion_emit_enter_signal(td);
823 static void tab_completion_select_show(TabCompData *td)
827 const gchar *filter = nullptr;
828 gchar *filter_desc = nullptr;
832 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
836 title = (td->fd_title) ? td->fd_title : _("Select path");
837 td->fd = file_dialog_new(title, "select_path", td->entry,
838 tab_completion_select_cancel_cb, td);
839 file_dialog_add_button(td->fd, GQ_ICON_OK, "OK",
840 tab_completion_select_ok_cb, TRUE);
842 generic_dialog_add_message(GENERIC_DIALOG(td->fd), nullptr, title, nullptr, FALSE);
854 filter_desc = td->filter_desc;
858 filter_desc = _("All files");
861 path = gq_gtk_entry_get_text(GTK_ENTRY(td->entry));
862 if (strlen(path) == 0) path = nullptr;
863 if (td->fd_folders_only)
865 file_dialog_add_path_widgets(td->fd, nullptr, path, td->history_key, nullptr, nullptr);
869 file_dialog_add_path_widgets(td->fd, nullptr, path, td->history_key, filter, filter_desc);
872 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
875 static void tab_completion_select_pressed(GtkWidget *, gpointer data)
877 auto td = static_cast<TabCompData *>(data);
879 tab_completion_select_show(td);
882 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gboolean folders_only)
888 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
892 g_free(td->fd_title);
893 td->fd_title = g_strdup(title);
894 td->fd_folders_only = folders_only;
896 if (td->fd_button) return;
898 parent = (td->combo) ? td->combo : td->entry;
900 hbox = gtk_widget_get_parent(parent);
901 if (!GTK_IS_BOX(hbox)) return;
903 td->fd_button = gtk_button_new_with_label("...");
904 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
905 G_CALLBACK(tab_completion_button_size_allocate), parent);
906 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
907 G_CALLBACK(tab_completion_select_pressed), td);
909 gq_gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
911 gtk_widget_show(td->fd_button);
913 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */