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_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *UNUSED(push_in), gpointer data)
303 TabCompData *td = data;
306 PangoRectangle strong_pos, weak_pos;
308 gint xoffset, yoffset;
312 GdkRectangle monitor;
313 GtkRequisition requisition;
314 GtkAllocation allocation;
316 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(td->entry)), x, y);
318 screen = gtk_widget_get_screen(GTK_WIDGET(menu));
319 monitor_num = gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(GTK_WIDGET(td->entry)));
320 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
322 gtk_widget_size_request(GTK_WIDGET(menu), &req);
324 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
325 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
327 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
328 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
330 *x += strong_pos.x / PANGO_SCALE + xoffset;
332 gtk_widget_get_requisition(td->entry, &requisition);
333 gtk_widget_get_allocation(td->entry, &allocation);
335 height = MIN(requisition.height, allocation.height);
337 if (req.height > monitor.y + monitor.height - *y - height &&
338 *y - monitor.y > monitor.y + monitor.height - *y)
340 height = MIN(*y - monitor.y, req.height);
341 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
350 static void tab_completion_popup_list(TabCompData *td, GList *list)
363 * well, the menu would be too long anyway...
364 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
365 * this is why menu popups are disabled, this really should be a popup scrollable listview.
367 if (g_list_length(list) > 200) return;
370 menu = popup_menu_short_lived();
373 while (work && count < TAB_COMP_POPUP_MAX)
375 gchar *name = work->data;
378 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
379 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
385 g_signal_connect(G_OBJECT(menu), "key_press_event",
386 G_CALLBACK(tab_completion_popup_key_press), td);
388 /* peek at the current event to get the time, etc. */
389 event = gtk_get_current_event();
391 if (event && event->type == GDK_BUTTON_RELEASE)
393 ebutton = event->button.button;
402 etime = gdk_event_get_time(event);
403 gdk_event_free(event);
410 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
411 tab_completion_popup_pos_cb, td, ebutton, etime);
415 #define CASE_SORT strcmp
418 static gint simple_sort(gconstpointer a, gconstpointer b)
420 return CASE_SORT((gchar *)a, (gchar *)b);
425 static gboolean tab_completion_do(TabCompData *td)
427 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
428 const gchar *entry_file;
431 gboolean home_exp = FALSE;
433 if (entry_text[0] == '\0')
435 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME root directory win32 */
436 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
437 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
442 /* home dir expansion */
443 if (entry_text[0] == '~')
445 entry_dir = expand_tilde(entry_text);
450 entry_dir = g_strdup(entry_text);
453 if (isfile(entry_dir))
457 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
458 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
464 entry_file = filename_from_path(entry_text);
466 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
468 ptr = entry_dir + strlen(entry_dir) - 1;
469 if (ptr[0] == G_DIR_SEPARATOR)
473 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
474 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
477 tab_completion_read_dir(td, entry_dir);
478 td->file_list = g_list_sort(td->file_list, simple_sort);
479 if (td->file_list && !td->file_list->next)
484 file = td->file_list->data;
485 buf = g_build_filename(entry_dir, file, NULL);
488 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
492 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
493 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
497 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
501 tab_completion_popup_list(td, td->file_list);
510 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
511 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
512 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
519 ptr = (gchar *)filename_from_path(entry_dir);
520 if (ptr > entry_dir) ptr--;
523 if (strlen(entry_dir) == 0)
526 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME win32 */
529 if (isdir(entry_dir))
533 gint l = strlen(entry_file);
535 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
537 tab_completion_read_dir(td, entry_dir);
540 list = td->file_list;
543 gchar *file = list->data;
544 if (strncmp(entry_file, file, l) == 0)
546 poss = g_list_prepend(poss, file);
555 gchar *file = poss->data;
558 buf = g_build_filename(entry_dir, file, NULL);
559 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
560 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
568 gsize c = strlen(entry_file);
569 gboolean done = FALSE;
570 gchar *test_file = poss->data;
575 if (!list) done = TRUE;
576 while (list && !done)
578 gchar *file = list->data;
579 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
592 file = g_strdup(test_file);
594 buf = g_build_filename(entry_dir, file, NULL);
595 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
596 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
598 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
600 poss = g_list_sort(poss, simple_sort);
601 tab_completion_popup_list(td, poss);
621 static gboolean tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
623 TabCompData *td = data;
624 gboolean stop_signal = FALSE;
626 switch (event->keyval)
629 if (!(event->state & GDK_CONTROL_MASK))
631 if (tab_completion_do(td))
633 tab_completion_emit_tab_signal(td);
638 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
640 (event->state & GDK_CONTROL_MASK))
642 tab_completion_select_show(td);
645 else if (tab_completion_emit_enter_signal(td))
654 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
656 return (stop_signal);
659 static void tab_completion_button_pressed(GtkWidget *UNUSED(widget), gpointer data)
662 GtkWidget *entry = data;
664 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
668 if (!gtk_widget_has_focus(entry))
670 gtk_widget_grab_focus(entry);
673 if (tab_completion_do(td))
675 tab_completion_emit_tab_signal(td);
679 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
681 GtkWidget *parent = data;
682 GtkAllocation parent_allocation;
683 gtk_widget_get_allocation(parent, &parent_allocation);
685 if (allocation->height > parent_allocation.height)
687 GtkAllocation button_allocation;
689 gtk_widget_get_allocation(button, &button_allocation);
690 button_allocation.height = parent_allocation.height;
691 button_allocation.y = parent_allocation.y +
692 (parent_allocation.height - parent_allocation.height) / 2;
693 gtk_widget_size_allocate(button, &button_allocation);
697 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
703 button = gtk_button_new();
704 gtk_widget_set_can_focus(button, FALSE);
705 g_signal_connect(G_OBJECT(button), "size_allocate",
706 G_CALLBACK(tab_completion_button_size_allocate), parent);
707 g_signal_connect(G_OBJECT(button), "clicked",
708 G_CALLBACK(tab_completion_button_pressed), entry);
710 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
711 icon = gtk_image_new_from_pixbuf(pixbuf);
712 g_object_unref(pixbuf);
714 gtk_container_add(GTK_CONTAINER(button), icon);
715 gtk_widget_show(icon);
721 *----------------------------------------------------------------------------
723 *----------------------------------------------------------------------------
726 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
727 const gchar *history_key, gint max_levels,
728 void (*enter_func)(const gchar *, gpointer), gpointer data)
732 GtkWidget *combo_entry;
738 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
740 combo = gtk_combo_box_text_new_with_entry();
741 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
742 gtk_widget_show(combo);
744 combo_entry = gtk_bin_get_child(GTK_BIN(combo));
746 button = tab_completion_create_complete_button(combo_entry, combo);
747 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
748 gtk_widget_show(button);
750 tab_completion_add_to_entry(combo_entry, enter_func, NULL, NULL, data);
752 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
753 if (!td) return NULL; /* this should never happen! */
756 td->has_history = TRUE;
757 td->history_key = g_strdup(history_key);
758 td->history_levels = max_levels;
760 work = history_list_get_by_key(td->history_key);
762 work = history_list_get_by_key(history_key);
765 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), (gchar *)work->data);
772 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
776 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
779 if (entry) *entry = combo_entry;
783 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
785 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
788 if (!td || !td->has_history) return NULL;
790 buf = history_list_find_last_path_by_key(td->history_key);
793 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
799 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
806 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
810 if (!td || !td->has_history) return;
812 history_list_add_to_key(td->history_key, path, td->history_levels);
814 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
816 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
817 gtk_list_store_clear(GTK_LIST_STORE(store));
819 work = history_list_get_by_key(td->history_key);
822 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(td->combo), (gchar *)work->data);
827 if (td->tab_append_func) {
828 td->tab_append_func(path, td->tab_append_data, n);
832 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
833 void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
839 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
841 newentry = gtk_entry_new();
842 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
843 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
844 gtk_widget_show(newentry);
846 button = tab_completion_create_complete_button(newentry, newentry);
847 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
848 gtk_widget_show(button);
850 tab_completion_add_to_entry(newentry, enter_func, filter, filter_desc, data);
851 if (entry) *entry = newentry;
855 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
860 log_printf("Tab completion error: entry != NULL\n");
864 td = g_new0(TabCompData, 1);
867 td->enter_func = enter_func;
868 td->enter_data = data;
869 td->filter = g_strdup(filter);
870 td->filter_desc = g_strdup(filter_desc);
872 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
874 g_signal_connect(G_OBJECT(entry), "key_press_event",
875 G_CALLBACK(tab_completion_key_pressed), td);
876 g_signal_connect(G_OBJECT(entry), "destroy",
877 G_CALLBACK(tab_completion_destroy), td);
880 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
882 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
886 td->tab_func = tab_func;
890 /* Add a callback function called when a new entry is appended to the list */
891 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
893 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
897 td->tab_append_func = tab_append_func;
898 td->tab_append_data = data;
901 gchar *remove_trailing_slash(const gchar *path)
905 if (!path) return NULL;
908 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
910 return g_strndup(path, l);
913 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
915 TabCompData *td = data;
918 file_dialog_close(fd);
921 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
923 TabCompData *td = data;
925 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
927 tab_completion_select_cancel_cb(fd, data);
929 tab_completion_emit_enter_signal(td);
932 static void tab_completion_select_show(TabCompData *td)
936 gchar *filter = NULL;
937 gchar *filter_desc = NULL;
941 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
945 title = (td->fd_title) ? td->fd_title : _("Select path");
946 td->fd = file_dialog_new(title, "select_path", td->entry,
947 tab_completion_select_cancel_cb, td);
948 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
949 tab_completion_select_ok_cb, TRUE);
951 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL, FALSE);
963 filter_desc = td->filter_desc;
967 filter_desc = _("All files");
970 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
971 if (strlen(path) == 0) path = NULL;
972 if (td->fd_folders_only)
974 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
978 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, filter, filter_desc);
981 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
984 static void tab_completion_select_pressed(GtkWidget *UNUSED(widget), gpointer data)
986 TabCompData *td = data;
988 tab_completion_select_show(td);
991 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gboolean folders_only)
997 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
1001 g_free(td->fd_title);
1002 td->fd_title = g_strdup(title);
1003 td->fd_folders_only = folders_only;
1005 if (td->fd_button) return;
1007 parent = (td->combo) ? td->combo : td->entry;
1009 hbox = gtk_widget_get_parent(parent);
1010 if (!GTK_IS_BOX(hbox)) return;
1012 td->fd_button = gtk_button_new_with_label("...");
1013 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
1014 G_CALLBACK(tab_completion_button_size_allocate), parent);
1015 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
1016 G_CALLBACK(tab_completion_select_pressed), td);
1018 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
1020 gtk_widget_show(td->fd_button);
1022 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */