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.
23 #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-spinner.h"
33 #include "ui-utildlg.h"
35 /* define this to enable a pop-up menu that shows possible matches
36 * #define TAB_COMPLETION_ENABLE_POPUP_MENU
38 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1
39 #define TAB_COMP_POPUP_MAX 1000
41 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
48 * ----------------------------------------------------------------
49 * Tab completion routines, can be connected to any gtkentry widget
50 * using the tab_completion_add_to_entry() function.
52 * Use remove_trailing_slash() to strip the trailing G_DIR_SEPARATOR.
54 * ----------------------------------------------------------------
57 typedef struct _TabCompData TabCompData;
63 void (*enter_func)(const gchar *, gpointer);
64 void (*tab_func)(const gchar *, gpointer);
65 void (*tab_append_func)(const gchar *, gpointer, gint);
69 gpointer tab_append_data;
78 gboolean fd_folders_only;
87 static void tab_completion_select_show(TabCompData *td);
88 static gint tab_completion_do(TabCompData *td);
90 static void tab_completion_free_list(TabCompData *td)
105 g_list_free(td->file_list);
106 td->file_list = NULL;
109 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
116 tab_completion_free_list(td);
118 pathl = path_from_utf8(path);
126 while ((dir = readdir(dp)) != NULL)
128 gchar *name = dir->d_name;
129 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0 &&
130 (name[0] != '.' || options->file_filter.show_hidden_files))
132 gchar *abspath = g_build_filename(pathl, name, NULL);
134 if (g_file_test(abspath, G_FILE_TEST_IS_DIR))
136 gchar *dname = g_strconcat(name, G_DIR_SEPARATOR_S, NULL);
137 list = g_list_prepend(list, path_to_utf8(dname));
142 list = g_list_prepend(list, path_to_utf8(name));
149 td->dir_path = g_strdup(path);
150 td->file_list = list;
154 static void tab_completion_destroy(GtkWidget *UNUSED(widget), gpointer data)
156 TabCompData *td = static_cast<TabCompData *>(data);
158 tab_completion_free_list(td);
159 g_free(td->history_key);
161 if (td->fd) file_dialog_close(td->fd);
162 g_free(td->fd_title);
165 g_free(td->filter_desc);
170 static gchar *tab_completion_get_text(TabCompData *td)
174 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
179 text = expand_tilde(text);
186 static gboolean tab_completion_emit_enter_signal(TabCompData *td)
189 if (!td->enter_func) return FALSE;
191 text = tab_completion_get_text(td);
192 td->enter_func(text, td->enter_data);
198 static void tab_completion_emit_tab_signal(TabCompData *td)
201 if (!td->tab_func) return;
203 text = tab_completion_get_text(td);
204 td->tab_func(text, td->tab_data);
208 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
209 void tab_completion_iter_menu_items(GtkWidget *widget, gpointer data)
211 TabCompData *td = static_cast<TabCompData *>(data);
214 if (!gtk_widget_get_visible(widget)) return;
216 child = gtk_bin_get_child(GTK_BIN(widget));
217 if (GTK_IS_LABEL(child)) {
218 const gchar *text = gtk_label_get_text(GTK_LABEL(child));
219 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
220 const gchar *prefix = filename_from_path(entry_text);
221 guint prefix_len = strlen(prefix);
223 if (strlen(text) < prefix_len || strncmp(text, prefix, prefix_len))
225 /* Hide menu items not matching */
226 gtk_widget_hide(widget);
230 /* Count how many choices are left in the menu */
236 static gboolean tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
238 TabCompData *td = static_cast<TabCompData *>(data);
240 if (event->keyval == GDK_KEY_Tab ||
241 event->keyval == GDK_KEY_BackSpace ||
242 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
244 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
249 buf[0] = event->keyval;
251 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
252 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
254 /* Reduce the number of entries in the menu */
256 gtk_container_foreach(GTK_CONTAINER(widget), tab_completion_iter_menu_items, (gpointer) td);
257 if (td->choices > 1) return TRUE; /* multiple choices */
258 if (td->choices > 0) tab_completion_do(td); /* one choice */
262 gtk_menu_popdown(GTK_MENU(widget));
263 /* doing this does not emit the "selection done" signal, unref it ourselves */
264 g_object_unref(widget);
271 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
273 gchar *name = static_cast<gchar *>(data);
277 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(widget), "tab_completion_data"));
280 buf = g_build_filename(td->dir_path, name, NULL);
281 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
282 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
285 tab_completion_emit_tab_signal(td);
288 static void tab_completion_popup_list(TabCompData *td, GList *list)
298 * well, the menu would be too long anyway...
299 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
300 * this is why menu popups are disabled, this really should be a popup scrollable listview.
302 if (g_list_length(list) > 200) return;
305 menu = popup_menu_short_lived();
308 while (work && count < TAB_COMP_POPUP_MAX)
310 gchar *name = static_cast<gchar *>(work->data);
313 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
314 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
320 g_signal_connect(G_OBJECT(menu), "key_press_event",
321 G_CALLBACK(tab_completion_popup_key_press), td);
323 gtk_menu_popup_at_widget(GTK_MENU(menu), td->entry, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_NORTH, NULL);
327 #define CASE_SORT strcmp
330 static gint simple_sort(gconstpointer a, gconstpointer b)
332 return CASE_SORT((gchar *)a, (gchar *)b);
337 static gboolean tab_completion_do(TabCompData *td)
339 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
340 const gchar *entry_file;
343 gboolean home_exp = FALSE;
345 if (entry_text[0] == '\0')
347 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME root directory win32 */
348 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
349 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
354 /* home dir expansion */
355 if (entry_text[0] == '~')
357 entry_dir = expand_tilde(entry_text);
362 entry_dir = g_strdup(entry_text);
365 if (isfile(entry_dir))
369 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
370 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
376 entry_file = filename_from_path(entry_text);
378 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
380 ptr = entry_dir + strlen(entry_dir) - 1;
381 if (ptr[0] == G_DIR_SEPARATOR)
385 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
386 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
389 tab_completion_read_dir(td, entry_dir);
390 td->file_list = g_list_sort(td->file_list, simple_sort);
391 if (td->file_list && !td->file_list->next)
396 file = static_cast<const gchar *>(td->file_list->data);
397 buf = g_build_filename(entry_dir, file, NULL);
400 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
404 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
405 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
409 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
413 tab_completion_popup_list(td, td->file_list);
422 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
423 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
424 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
431 ptr = (gchar *)filename_from_path(entry_dir);
432 if (ptr > entry_dir) ptr--;
435 if (strlen(entry_dir) == 0)
438 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME win32 */
441 if (isdir(entry_dir))
445 gint l = strlen(entry_file);
447 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
449 tab_completion_read_dir(td, entry_dir);
452 list = td->file_list;
455 gchar *file = static_cast<gchar *>(list->data);
456 if (strncmp(entry_file, file, l) == 0)
458 poss = g_list_prepend(poss, file);
467 gchar *file = static_cast<gchar *>(poss->data);
470 buf = g_build_filename(entry_dir, file, NULL);
471 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
472 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
480 gsize c = strlen(entry_file);
481 gboolean done = FALSE;
482 gchar *test_file = static_cast<gchar *>(poss->data);
487 if (!list) done = TRUE;
488 while (list && !done)
490 gchar *file = static_cast<gchar *>(list->data);
491 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
504 file = g_strdup(test_file);
506 buf = g_build_filename(entry_dir, file, NULL);
507 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
508 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
510 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
512 poss = g_list_sort(poss, simple_sort);
513 tab_completion_popup_list(td, poss);
533 static gboolean tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
535 TabCompData *td = static_cast<TabCompData *>(data);
536 gboolean stop_signal = FALSE;
538 switch (event->keyval)
541 if (!(event->state & GDK_CONTROL_MASK))
543 if (tab_completion_do(td))
545 tab_completion_emit_tab_signal(td);
550 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
552 (event->state & GDK_CONTROL_MASK))
554 tab_completion_select_show(td);
557 else if (tab_completion_emit_enter_signal(td))
566 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
568 return (stop_signal);
571 static void tab_completion_button_pressed(GtkWidget *UNUSED(widget), gpointer data)
574 GtkWidget *entry = static_cast<GtkWidget *>(data);
576 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
580 if (!gtk_widget_has_focus(entry))
582 gtk_widget_grab_focus(entry);
585 if (tab_completion_do(td))
587 tab_completion_emit_tab_signal(td);
591 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
593 GtkWidget *parent = static_cast<GtkWidget *>(data);
594 GtkAllocation parent_allocation;
595 gtk_widget_get_allocation(parent, &parent_allocation);
597 if (allocation->height > parent_allocation.height)
599 GtkAllocation button_allocation;
601 gtk_widget_get_allocation(button, &button_allocation);
602 button_allocation.height = parent_allocation.height;
603 button_allocation.y = parent_allocation.y +
604 (parent_allocation.height - parent_allocation.height) / 2;
605 gtk_widget_size_allocate(button, &button_allocation);
609 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
615 button = gtk_button_new();
616 gtk_widget_set_can_focus(button, FALSE);
617 g_signal_connect(G_OBJECT(button), "size_allocate",
618 G_CALLBACK(tab_completion_button_size_allocate), parent);
619 g_signal_connect(G_OBJECT(button), "clicked",
620 G_CALLBACK(tab_completion_button_pressed), entry);
622 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
623 icon = gtk_image_new_from_pixbuf(pixbuf);
624 g_object_unref(pixbuf);
626 gtk_container_add(GTK_CONTAINER(button), icon);
627 gtk_widget_show(icon);
633 *----------------------------------------------------------------------------
635 *----------------------------------------------------------------------------
638 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
639 const gchar *history_key, gint max_levels,
640 void (*enter_func)(const gchar *, gpointer), gpointer data)
644 GtkWidget *combo_entry;
650 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
652 combo = gtk_combo_box_text_new_with_entry();
653 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
654 gtk_widget_show(combo);
656 combo_entry = gtk_bin_get_child(GTK_BIN(combo));
658 button = tab_completion_create_complete_button(combo_entry, combo);
659 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
660 gtk_widget_show(button);
662 tab_completion_add_to_entry(combo_entry, enter_func, NULL, NULL, data);
664 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data"));
665 if (!td) return NULL; /* this should never happen! */
668 td->has_history = TRUE;
669 td->history_key = g_strdup(history_key);
670 td->history_levels = max_levels;
672 work = history_list_get_by_key(td->history_key);
674 work = history_list_get_by_key(history_key);
677 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), (gchar *)work->data);
684 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
688 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
691 if (entry) *entry = combo_entry;
695 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
697 TabCompData *td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
700 if (!td || !td->has_history) return NULL;
702 buf = history_list_find_last_path_by_key(td->history_key);
705 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
711 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
718 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
722 if (!td || !td->has_history) return;
724 history_list_add_to_key(td->history_key, path, td->history_levels);
726 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
728 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
729 gtk_list_store_clear(GTK_LIST_STORE(store));
731 work = history_list_get_by_key(td->history_key);
734 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(td->combo), (gchar *)work->data);
739 if (td->tab_append_func) {
740 td->tab_append_func(path, td->tab_append_data, n);
744 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
745 void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
751 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
753 newentry = gtk_entry_new();
754 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
755 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
756 gtk_widget_show(newentry);
758 button = tab_completion_create_complete_button(newentry, newentry);
759 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
760 gtk_widget_show(button);
762 tab_completion_add_to_entry(newentry, enter_func, filter, filter_desc, data);
763 if (entry) *entry = newentry;
767 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
772 log_printf("Tab completion error: entry != NULL\n");
776 td = g_new0(TabCompData, 1);
779 td->enter_func = enter_func;
780 td->enter_data = data;
781 td->filter = g_strdup(filter);
782 td->filter_desc = g_strdup(filter_desc);
784 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
786 g_signal_connect(G_OBJECT(entry), "key_press_event",
787 G_CALLBACK(tab_completion_key_pressed), td);
788 g_signal_connect(G_OBJECT(entry), "destroy",
789 G_CALLBACK(tab_completion_destroy), td);
792 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
794 TabCompData *td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
798 td->tab_func = tab_func;
802 /* Add a callback function called when a new entry is appended to the list */
803 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
805 TabCompData *td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
809 td->tab_append_func = tab_append_func;
810 td->tab_append_data = data;
813 gchar *remove_trailing_slash(const gchar *path)
817 if (!path) return NULL;
820 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
822 return g_strndup(path, l);
825 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
827 TabCompData *td = static_cast<TabCompData *>(data);
830 file_dialog_close(fd);
833 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
835 TabCompData *td = static_cast<TabCompData *>(data);
837 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
839 tab_completion_select_cancel_cb(fd, data);
841 tab_completion_emit_enter_signal(td);
844 static void tab_completion_select_show(TabCompData *td)
848 const gchar *filter = NULL;
849 gchar *filter_desc = NULL;
853 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
857 title = (td->fd_title) ? td->fd_title : _("Select path");
858 td->fd = file_dialog_new(title, "select_path", td->entry,
859 tab_completion_select_cancel_cb, td);
860 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
861 tab_completion_select_ok_cb, TRUE);
863 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL, FALSE);
875 filter_desc = td->filter_desc;
879 filter_desc = _("All files");
882 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
883 if (strlen(path) == 0) path = NULL;
884 if (td->fd_folders_only)
886 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
890 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, filter, filter_desc);
893 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
896 static void tab_completion_select_pressed(GtkWidget *UNUSED(widget), gpointer data)
898 TabCompData *td = static_cast<TabCompData *>(data);
900 tab_completion_select_show(td);
903 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gboolean folders_only)
909 td = static_cast<TabCompData *>(g_object_get_data(G_OBJECT(entry), "tab_completion_data"));
913 g_free(td->fd_title);
914 td->fd_title = g_strdup(title);
915 td->fd_folders_only = folders_only;
917 if (td->fd_button) return;
919 parent = (td->combo) ? td->combo : td->entry;
921 hbox = gtk_widget_get_parent(parent);
922 if (!GTK_IS_BOX(hbox)) return;
924 td->fd_button = gtk_button_new_with_label("...");
925 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
926 G_CALLBACK(tab_completion_button_size_allocate), parent);
927 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
928 G_CALLBACK(tab_completion_select_pressed), td);
930 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
932 gtk_widget_show(td->fd_button);
934 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */