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.
29 #include <sys/types.h>
34 #include <gdk-pixbuf/gdk-pixbuf.h>
37 #include "ui_tabcomp.h"
39 #include "history_list.h"
40 #include "misc.h" /* expand_tilde() */
41 #include "ui_fileops.h"
42 #include "ui_spinner.h"
43 #include "ui_utildlg.h"
45 #include <gdk/gdkkeysyms.h> /* for key values */
48 /* define this to enable a pop-up menu that shows possible matches
49 * #define TAB_COMPLETION_ENABLE_POPUP_MENU
51 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1
52 #define TAB_COMP_POPUP_MAX 1000
54 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
61 * ----------------------------------------------------------------
62 * Tab completion routines, can be connected to any gtkentry widget
63 * using the tab_completion_add_to_entry() function.
65 * Use remove_trailing_slash() to strip the trailing G_DIR_SEPARATOR.
67 * ----------------------------------------------------------------
70 typedef struct _TabCompData TabCompData;
76 void (*enter_func)(const gchar *, gpointer);
77 void (*tab_func)(const gchar *, gpointer);
78 void (*tab_append_func)(const gchar *, gpointer, gint);
82 gpointer tab_append_data;
91 gboolean fd_folders_only;
100 static void tab_completion_select_show(TabCompData *td);
101 static gint tab_completion_do(TabCompData *td);
103 static void tab_completion_free_list(TabCompData *td)
107 g_free(td->dir_path);
110 list = td->file_list;
118 g_list_free(td->file_list);
119 td->file_list = NULL;
122 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
129 tab_completion_free_list(td);
131 pathl = path_from_utf8(path);
139 while ((dir = readdir(dp)) != NULL)
141 gchar *name = dir->d_name;
142 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0 &&
143 (name[0] != '.' || options->file_filter.show_hidden_files))
145 gchar *abspath = g_build_filename(pathl, name, NULL);
147 if (g_file_test(abspath, G_FILE_TEST_IS_DIR))
149 gchar *dname = g_strconcat(name, G_DIR_SEPARATOR_S, NULL);
150 list = g_list_prepend(list, path_to_utf8(dname));
155 list = g_list_prepend(list, path_to_utf8(name));
162 td->dir_path = g_strdup(path);
163 td->file_list = list;
167 static void tab_completion_destroy(GtkWidget *UNUSED(widget), gpointer data)
169 TabCompData *td = data;
171 tab_completion_free_list(td);
172 g_free(td->history_key);
174 if (td->fd) file_dialog_close(td->fd);
175 g_free(td->fd_title);
178 g_free(td->filter_desc);
183 static gchar *tab_completion_get_text(TabCompData *td)
187 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
192 text = expand_tilde(text);
199 static gboolean tab_completion_emit_enter_signal(TabCompData *td)
202 if (!td->enter_func) return FALSE;
204 text = tab_completion_get_text(td);
205 td->enter_func(text, td->enter_data);
211 static void tab_completion_emit_tab_signal(TabCompData *td)
214 if (!td->tab_func) return;
216 text = tab_completion_get_text(td);
217 td->tab_func(text, td->tab_data);
221 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
222 void tab_completion_iter_menu_items(GtkWidget *widget, gpointer data)
224 TabCompData *td = data;
227 if (!gtk_widget_get_visible(widget)) return;
229 child = gtk_bin_get_child(GTK_BIN(widget));
230 if (GTK_IS_LABEL(child)) {
231 const gchar *text = gtk_label_get_text(GTK_LABEL(child));
232 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
233 const gchar *prefix = filename_from_path(entry_text);
234 guint prefix_len = strlen(prefix);
236 if (strlen(text) < prefix_len || strncmp(text, prefix, prefix_len))
238 /* Hide menu items not matching */
239 gtk_widget_hide(widget);
243 /* Count how many choices are left in the menu */
249 static gboolean tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
251 TabCompData *td = data;
253 if (event->keyval == GDK_KEY_Tab ||
254 event->keyval == GDK_KEY_BackSpace ||
255 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
257 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
262 buf[0] = event->keyval;
264 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
265 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
267 /* Reduce the number of entries in the menu */
269 gtk_container_foreach(GTK_CONTAINER(widget), tab_completion_iter_menu_items, (gpointer) td);
270 if (td->choices > 1) return TRUE; /* multiple choices */
271 if (td->choices > 0) tab_completion_do(td); /* one choice */
275 gtk_menu_popdown(GTK_MENU(widget));
276 /* doing this does not emit the "selection done" signal, unref it ourselves */
277 g_object_unref(widget);
284 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
290 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
293 buf = g_build_filename(td->dir_path, name, NULL);
294 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
295 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
298 tab_completion_emit_tab_signal(td);
301 static void tab_completion_popup_list(TabCompData *td, GList *list)
311 * well, the menu would be too long anyway...
312 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
313 * this is why menu popups are disabled, this really should be a popup scrollable listview.
315 if (g_list_length(list) > 200) return;
318 menu = popup_menu_short_lived();
321 while (work && count < TAB_COMP_POPUP_MAX)
323 gchar *name = work->data;
326 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
327 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
333 g_signal_connect(G_OBJECT(menu), "key_press_event",
334 G_CALLBACK(tab_completion_popup_key_press), td);
336 gtk_menu_popup_at_widget(GTK_MENU(menu), td->entry, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_NORTH, NULL);
340 #define CASE_SORT strcmp
343 static gint simple_sort(gconstpointer a, gconstpointer b)
345 return CASE_SORT((gchar *)a, (gchar *)b);
350 static gboolean tab_completion_do(TabCompData *td)
352 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
353 const gchar *entry_file;
356 gboolean home_exp = FALSE;
358 if (entry_text[0] == '\0')
360 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME root directory win32 */
361 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
362 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
367 /* home dir expansion */
368 if (entry_text[0] == '~')
370 entry_dir = expand_tilde(entry_text);
375 entry_dir = g_strdup(entry_text);
378 if (isfile(entry_dir))
382 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
383 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
389 entry_file = filename_from_path(entry_text);
391 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
393 ptr = entry_dir + strlen(entry_dir) - 1;
394 if (ptr[0] == G_DIR_SEPARATOR)
398 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
399 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
402 tab_completion_read_dir(td, entry_dir);
403 td->file_list = g_list_sort(td->file_list, simple_sort);
404 if (td->file_list && !td->file_list->next)
409 file = td->file_list->data;
410 buf = g_build_filename(entry_dir, file, NULL);
413 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
417 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
418 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
422 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
426 tab_completion_popup_list(td, td->file_list);
435 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
436 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
437 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
444 ptr = (gchar *)filename_from_path(entry_dir);
445 if (ptr > entry_dir) ptr--;
448 if (strlen(entry_dir) == 0)
451 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME win32 */
454 if (isdir(entry_dir))
458 gint l = strlen(entry_file);
460 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
462 tab_completion_read_dir(td, entry_dir);
465 list = td->file_list;
468 gchar *file = list->data;
469 if (strncmp(entry_file, file, l) == 0)
471 poss = g_list_prepend(poss, file);
480 gchar *file = poss->data;
483 buf = g_build_filename(entry_dir, file, NULL);
484 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
485 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
493 gsize c = strlen(entry_file);
494 gboolean done = FALSE;
495 gchar *test_file = poss->data;
500 if (!list) done = TRUE;
501 while (list && !done)
503 gchar *file = list->data;
504 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
517 file = g_strdup(test_file);
519 buf = g_build_filename(entry_dir, file, NULL);
520 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
521 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
523 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
525 poss = g_list_sort(poss, simple_sort);
526 tab_completion_popup_list(td, poss);
546 static gboolean tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
548 TabCompData *td = data;
549 gboolean stop_signal = FALSE;
551 switch (event->keyval)
554 if (!(event->state & GDK_CONTROL_MASK))
556 if (tab_completion_do(td))
558 tab_completion_emit_tab_signal(td);
563 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
565 (event->state & GDK_CONTROL_MASK))
567 tab_completion_select_show(td);
570 else if (tab_completion_emit_enter_signal(td))
579 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
581 return (stop_signal);
584 static void tab_completion_button_pressed(GtkWidget *UNUSED(widget), gpointer data)
587 GtkWidget *entry = data;
589 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
593 if (!gtk_widget_has_focus(entry))
595 gtk_widget_grab_focus(entry);
598 if (tab_completion_do(td))
600 tab_completion_emit_tab_signal(td);
604 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
606 GtkWidget *parent = data;
607 GtkAllocation parent_allocation;
608 gtk_widget_get_allocation(parent, &parent_allocation);
610 if (allocation->height > parent_allocation.height)
612 GtkAllocation button_allocation;
614 gtk_widget_get_allocation(button, &button_allocation);
615 button_allocation.height = parent_allocation.height;
616 button_allocation.y = parent_allocation.y +
617 (parent_allocation.height - parent_allocation.height) / 2;
618 gtk_widget_size_allocate(button, &button_allocation);
622 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
628 button = gtk_button_new();
629 gtk_widget_set_can_focus(button, FALSE);
630 g_signal_connect(G_OBJECT(button), "size_allocate",
631 G_CALLBACK(tab_completion_button_size_allocate), parent);
632 g_signal_connect(G_OBJECT(button), "clicked",
633 G_CALLBACK(tab_completion_button_pressed), entry);
635 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
636 icon = gtk_image_new_from_pixbuf(pixbuf);
637 g_object_unref(pixbuf);
639 gtk_container_add(GTK_CONTAINER(button), icon);
640 gtk_widget_show(icon);
646 *----------------------------------------------------------------------------
648 *----------------------------------------------------------------------------
651 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
652 const gchar *history_key, gint max_levels,
653 void (*enter_func)(const gchar *, gpointer), gpointer data)
657 GtkWidget *combo_entry;
663 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
665 combo = gtk_combo_box_text_new_with_entry();
666 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
667 gtk_widget_show(combo);
669 combo_entry = gtk_bin_get_child(GTK_BIN(combo));
671 button = tab_completion_create_complete_button(combo_entry, combo);
672 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
673 gtk_widget_show(button);
675 tab_completion_add_to_entry(combo_entry, enter_func, NULL, NULL, data);
677 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
678 if (!td) return NULL; /* this should never happen! */
681 td->has_history = TRUE;
682 td->history_key = g_strdup(history_key);
683 td->history_levels = max_levels;
685 work = history_list_get_by_key(td->history_key);
687 work = history_list_get_by_key(history_key);
690 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), (gchar *)work->data);
697 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
701 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
704 if (entry) *entry = combo_entry;
708 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
710 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
713 if (!td || !td->has_history) return NULL;
715 buf = history_list_find_last_path_by_key(td->history_key);
718 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
724 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
731 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
735 if (!td || !td->has_history) return;
737 history_list_add_to_key(td->history_key, path, td->history_levels);
739 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
741 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
742 gtk_list_store_clear(GTK_LIST_STORE(store));
744 work = history_list_get_by_key(td->history_key);
747 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(td->combo), (gchar *)work->data);
752 if (td->tab_append_func) {
753 td->tab_append_func(path, td->tab_append_data, n);
757 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
758 void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
764 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
766 newentry = gtk_entry_new();
767 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
768 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
769 gtk_widget_show(newentry);
771 button = tab_completion_create_complete_button(newentry, newentry);
772 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
773 gtk_widget_show(button);
775 tab_completion_add_to_entry(newentry, enter_func, filter, filter_desc, data);
776 if (entry) *entry = newentry;
780 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
785 log_printf("Tab completion error: entry != NULL\n");
789 td = g_new0(TabCompData, 1);
792 td->enter_func = enter_func;
793 td->enter_data = data;
794 td->filter = g_strdup(filter);
795 td->filter_desc = g_strdup(filter_desc);
797 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
799 g_signal_connect(G_OBJECT(entry), "key_press_event",
800 G_CALLBACK(tab_completion_key_pressed), td);
801 g_signal_connect(G_OBJECT(entry), "destroy",
802 G_CALLBACK(tab_completion_destroy), td);
805 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
807 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
811 td->tab_func = tab_func;
815 /* Add a callback function called when a new entry is appended to the list */
816 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
818 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
822 td->tab_append_func = tab_append_func;
823 td->tab_append_data = data;
826 gchar *remove_trailing_slash(const gchar *path)
830 if (!path) return NULL;
833 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
835 return g_strndup(path, l);
838 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
840 TabCompData *td = data;
843 file_dialog_close(fd);
846 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
848 TabCompData *td = data;
850 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
852 tab_completion_select_cancel_cb(fd, data);
854 tab_completion_emit_enter_signal(td);
857 static void tab_completion_select_show(TabCompData *td)
861 gchar *filter = NULL;
862 gchar *filter_desc = NULL;
866 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
870 title = (td->fd_title) ? td->fd_title : _("Select path");
871 td->fd = file_dialog_new(title, "select_path", td->entry,
872 tab_completion_select_cancel_cb, td);
873 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
874 tab_completion_select_ok_cb, TRUE);
876 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL, FALSE);
888 filter_desc = td->filter_desc;
892 filter_desc = _("All files");
895 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
896 if (strlen(path) == 0) path = NULL;
897 if (td->fd_folders_only)
899 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
903 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, filter, filter_desc);
906 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
909 static void tab_completion_select_pressed(GtkWidget *UNUSED(widget), gpointer data)
911 TabCompData *td = data;
913 tab_completion_select_show(td);
916 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gboolean folders_only)
922 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
926 g_free(td->fd_title);
927 td->fd_title = g_strdup(title);
928 td->fd_folders_only = folders_only;
930 if (td->fd_button) return;
932 parent = (td->combo) ? td->combo : td->entry;
934 hbox = gtk_widget_get_parent(parent);
935 if (!GTK_IS_BOX(hbox)) return;
937 td->fd_button = gtk_button_new_with_label("...");
938 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
939 G_CALLBACK(tab_completion_button_size_allocate), parent);
940 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
941 G_CALLBACK(tab_completion_select_pressed), td);
943 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
945 gtk_widget_show(td->fd_button);
947 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */