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.
31 #include <sys/types.h>
36 #include <gdk-pixbuf/gdk-pixbuf.h>
39 #include "ui_tabcomp.h"
41 #include "history_list.h"
42 #include "misc.h" /* expand_tilde() */
43 #include "ui_fileops.h"
44 #include "ui_spinner.h"
45 #include "ui_utildlg.h"
47 #include <gdk/gdkkeysyms.h> /* for key values */
50 /* define this to enable a pop-up menu that shows possible matches
51 * #define TAB_COMPLETION_ENABLE_POPUP_MENU
53 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1
54 #define TAB_COMP_POPUP_MAX 1000
56 #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.
64 Use remove_trailing_slash() to strip the trailing G_DIR_SEPARATOR.
65 ----------------------------------------------------------------*/
67 typedef struct _TabCompData TabCompData;
73 void (*enter_func)(const gchar *, gpointer);
74 void (*tab_func)(const gchar *, gpointer);
75 void (*tab_append_func)(const gchar *, gpointer, gint);
79 gpointer tab_append_data;
88 gboolean fd_folders_only;
97 static void tab_completion_select_show(TabCompData *td);
98 static gint tab_completion_do(TabCompData *td);
100 static void tab_completion_free_list(TabCompData *td)
104 g_free(td->dir_path);
107 list = td->file_list;
115 g_list_free(td->file_list);
116 td->file_list = NULL;
119 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
126 tab_completion_free_list(td);
128 pathl = path_from_utf8(path);
136 while ((dir = readdir(dp)) != NULL)
138 gchar *name = dir->d_name;
139 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
141 gchar *abspath = g_build_filename(pathl, name, NULL);
143 if (g_file_test(abspath, G_FILE_TEST_IS_DIR))
145 gchar *dname = g_strconcat(name, G_DIR_SEPARATOR_S, NULL);
146 list = g_list_prepend(list, path_to_utf8(dname));
151 list = g_list_prepend(list, path_to_utf8(name));
158 td->dir_path = g_strdup(path);
159 td->file_list = list;
163 static void tab_completion_destroy(GtkWidget *widget, gpointer data)
165 TabCompData *td = data;
167 tab_completion_free_list(td);
168 g_free(td->history_key);
170 if (td->fd) file_dialog_close(td->fd);
171 g_free(td->fd_title);
174 g_free(td->filter_desc);
179 static gchar *tab_completion_get_text(TabCompData *td)
183 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
188 text = expand_tilde(text);
195 static gboolean tab_completion_emit_enter_signal(TabCompData *td)
198 if (!td->enter_func) return FALSE;
200 text = tab_completion_get_text(td);
201 td->enter_func(text, td->enter_data);
207 static void tab_completion_emit_tab_signal(TabCompData *td)
210 if (!td->tab_func) return;
212 text = tab_completion_get_text(td);
213 td->tab_func(text, td->tab_data);
217 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
218 void tab_completion_iter_menu_items(GtkWidget *widget, gpointer data)
220 TabCompData *td = data;
223 if (!gtk_widget_get_visible(widget)) return;
225 child = gtk_bin_get_child(GTK_BIN(widget));
226 if (GTK_IS_LABEL(child)) {
227 const gchar *text = gtk_label_get_text(GTK_LABEL(child));
228 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
229 const gchar *prefix = filename_from_path(entry_text);
230 guint prefix_len = strlen(prefix);
232 if (strlen(text) < prefix_len || strncmp(text, prefix, prefix_len))
234 /* Hide menu items not matching */
235 gtk_widget_hide(widget);
239 /* Count how many choices are left in the menu */
245 static gboolean tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
247 TabCompData *td = data;
249 if (event->keyval == GDK_KEY_Tab ||
250 event->keyval == GDK_KEY_BackSpace ||
251 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
253 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
258 buf[0] = event->keyval;
260 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
261 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
263 /* Reduce the number of entries in the menu */
265 gtk_container_foreach(GTK_CONTAINER(widget), tab_completion_iter_menu_items, (gpointer) td);
266 if (td->choices > 1) return TRUE; /* multiple choices */
267 if (td->choices > 0) tab_completion_do(td); /* one choice */
271 gtk_menu_popdown(GTK_MENU(widget));
272 /* doing this does not emit the "selection done" signal, unref it ourselves */
273 g_object_unref(widget);
280 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
286 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
289 buf = g_build_filename(td->dir_path, name, NULL);
290 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
291 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
294 tab_completion_emit_tab_signal(td);
297 static void tab_completion_popup_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
299 TabCompData *td = data;
302 PangoRectangle strong_pos, weak_pos;
304 gint xoffset, yoffset;
308 GdkRectangle monitor;
309 GtkRequisition requisition;
310 GtkAllocation allocation;
312 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(td->entry)), x, y);
314 screen = gtk_widget_get_screen(GTK_WIDGET(menu));
315 monitor_num = gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(GTK_WIDGET(td->entry)));
316 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
318 gtk_widget_size_request(GTK_WIDGET(menu), &req);
320 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
321 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
323 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
324 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
326 *x += strong_pos.x / PANGO_SCALE + xoffset;
328 gtk_widget_get_requisition(td->entry, &requisition);
329 gtk_widget_get_allocation(td->entry, &allocation);
331 height = MIN(requisition.height, allocation.height);
333 if (req.height > monitor.y + monitor.height - *y - height &&
334 *y - monitor.y > monitor.y + monitor.height - *y)
336 height = MIN(*y - monitor.y, req.height);
337 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
346 static void tab_completion_popup_list(TabCompData *td, GList *list)
359 * well, the menu would be too long anyway...
360 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
361 * this is why menu popups are disabled, this really should be a popup scrollable listview.
363 if (g_list_length(list) > 200) return;
366 menu = popup_menu_short_lived();
369 while (work && count < TAB_COMP_POPUP_MAX)
371 gchar *name = work->data;
374 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
375 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
381 g_signal_connect(G_OBJECT(menu), "key_press_event",
382 G_CALLBACK(tab_completion_popup_key_press), td);
384 /* peek at the current event to get the time, etc. */
385 event = gtk_get_current_event();
387 if (event && event->type == GDK_BUTTON_RELEASE)
389 ebutton = event->button.button;
398 etime = gdk_event_get_time(event);
399 gdk_event_free(event);
406 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
407 tab_completion_popup_pos_cb, td, ebutton, etime);
411 #define CASE_SORT strcmp
414 static gint simple_sort(gconstpointer a, gconstpointer b)
416 return CASE_SORT((gchar *)a, (gchar *)b);
421 static gboolean tab_completion_do(TabCompData *td)
423 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
424 const gchar *entry_file;
427 gboolean home_exp = FALSE;
429 if (entry_text[0] == '\0')
431 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: root directory win32 */
432 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
433 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
438 /* home dir expansion */
439 if (entry_text[0] == '~')
441 entry_dir = expand_tilde(entry_text);
446 entry_dir = g_strdup(entry_text);
449 if (isfile(entry_dir))
453 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
454 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
460 entry_file = filename_from_path(entry_text);
462 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
464 ptr = entry_dir + strlen(entry_dir) - 1;
465 if (ptr[0] == G_DIR_SEPARATOR)
469 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
470 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
473 tab_completion_read_dir(td, entry_dir);
474 td->file_list = g_list_sort(td->file_list, simple_sort);
475 if (td->file_list && !td->file_list->next)
480 file = td->file_list->data;
481 buf = g_build_filename(entry_dir, file, NULL);
484 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
488 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
489 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
493 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
497 tab_completion_popup_list(td, td->file_list);
506 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
507 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
508 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
515 ptr = (gchar *)filename_from_path(entry_dir);
516 if (ptr > entry_dir) ptr--;
519 if (strlen(entry_dir) == 0)
522 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: win32 */
525 if (isdir(entry_dir))
529 gint l = strlen(entry_file);
531 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
533 tab_completion_read_dir(td, entry_dir);
536 list = td->file_list;
539 gchar *file = list->data;
540 if (strncmp(entry_file, file, l) == 0)
542 poss = g_list_prepend(poss, file);
551 gchar *file = poss->data;
554 buf = g_build_filename(entry_dir, file, NULL);
555 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
556 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
564 gsize c = strlen(entry_file);
565 gboolean done = FALSE;
566 gchar *test_file = poss->data;
571 if (!list) done = TRUE;
572 while (list && !done)
574 gchar *file = list->data;
575 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
588 file = g_strdup(test_file);
590 buf = g_build_filename(entry_dir, file, NULL);
591 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
592 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
594 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
596 poss = g_list_sort(poss, simple_sort);
597 tab_completion_popup_list(td, poss);
617 static gboolean tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
619 TabCompData *td = data;
620 gboolean stop_signal = FALSE;
622 switch (event->keyval)
625 if (!(event->state & GDK_CONTROL_MASK))
627 if (tab_completion_do(td))
629 tab_completion_emit_tab_signal(td);
634 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
636 (event->state & GDK_CONTROL_MASK))
638 tab_completion_select_show(td);
641 else if (tab_completion_emit_enter_signal(td))
650 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
652 return (stop_signal);
655 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
658 GtkWidget *entry = data;
660 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
664 if (!gtk_widget_has_focus(entry))
666 gtk_widget_grab_focus(entry);
669 if (tab_completion_do(td))
671 tab_completion_emit_tab_signal(td);
675 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
677 GtkWidget *parent = data;
678 GtkAllocation parent_allocation;
679 gtk_widget_get_allocation(parent, &parent_allocation);
681 if (allocation->height > parent_allocation.height)
683 GtkAllocation button_allocation;
685 gtk_widget_get_allocation(button, &button_allocation);
686 button_allocation.height = parent_allocation.height;
687 button_allocation.y = parent_allocation.y +
688 (parent_allocation.height - parent_allocation.height) / 2;
689 gtk_widget_size_allocate(button, &button_allocation);
693 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
699 button = gtk_button_new();
700 gtk_widget_set_can_focus(button, FALSE);
701 g_signal_connect(G_OBJECT(button), "size_allocate",
702 G_CALLBACK(tab_completion_button_size_allocate), parent);
703 g_signal_connect(G_OBJECT(button), "clicked",
704 G_CALLBACK(tab_completion_button_pressed), entry);
706 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
707 icon = gtk_image_new_from_pixbuf(pixbuf);
708 g_object_unref(pixbuf);
710 gtk_container_add(GTK_CONTAINER(button), icon);
711 gtk_widget_show(icon);
717 *----------------------------------------------------------------------------
719 *----------------------------------------------------------------------------
722 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
723 const gchar *history_key, gint max_levels,
724 void (*enter_func)(const gchar *, gpointer), gpointer data)
728 GtkWidget *combo_entry;
734 box = gtk_hbox_new(FALSE, 0);
736 combo = gtk_combo_box_text_new_with_entry();
737 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
738 gtk_widget_show(combo);
740 combo_entry = gtk_bin_get_child(GTK_BIN(combo));
742 button = tab_completion_create_complete_button(combo_entry, combo);
743 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
744 gtk_widget_show(button);
746 tab_completion_add_to_entry(combo_entry, enter_func, NULL, NULL, data);
748 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
749 if (!td) return NULL; /* this should never happen! */
752 td->has_history = TRUE;
753 td->history_key = g_strdup(history_key);
754 td->history_levels = max_levels;
756 work = history_list_get_by_key(td->history_key);
758 work = history_list_get_by_key(history_key);
761 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), (gchar *)work->data);
768 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
772 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
775 if (entry) *entry = combo_entry;
779 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
781 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
784 if (!td || !td->has_history) return NULL;
786 buf = history_list_find_last_path_by_key(td->history_key);
789 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
795 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
802 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
806 if (!td || !td->has_history) return;
808 history_list_add_to_key(td->history_key, path, td->history_levels);
810 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
812 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
813 gtk_list_store_clear(GTK_LIST_STORE(store));
815 work = history_list_get_by_key(td->history_key);
818 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(td->combo), (gchar *)work->data);
823 if (td->tab_append_func) {
824 td->tab_append_func(path, td->tab_append_data, n);
828 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
829 void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
835 hbox = gtk_hbox_new(FALSE, 0);
837 newentry = gtk_entry_new();
838 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
839 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
840 gtk_widget_show(newentry);
842 button = tab_completion_create_complete_button(newentry, newentry);
843 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
844 gtk_widget_show(button);
846 tab_completion_add_to_entry(newentry, enter_func, filter, filter_desc, data);
847 if (entry) *entry = newentry;
851 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
856 log_printf("Tab completion error: entry != NULL\n");
860 td = g_new0(TabCompData, 1);
863 td->enter_func = enter_func;
864 td->enter_data = data;
865 td->filter = g_strdup(filter);
866 td->filter_desc = g_strdup(filter_desc);
868 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
870 g_signal_connect(G_OBJECT(entry), "key_press_event",
871 G_CALLBACK(tab_completion_key_pressed), td);
872 g_signal_connect(G_OBJECT(entry), "destroy",
873 G_CALLBACK(tab_completion_destroy), td);
876 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
878 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
882 td->tab_func = tab_func;
886 /* Add a callback function called when a new entry is appended to the list */
887 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
889 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
893 td->tab_append_func = tab_append_func;
894 td->tab_append_data = data;
897 gchar *remove_trailing_slash(const gchar *path)
901 if (!path) return NULL;
904 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
906 return g_strndup(path, l);
909 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
911 TabCompData *td = data;
914 file_dialog_close(fd);
917 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
919 TabCompData *td = data;
921 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
923 tab_completion_select_cancel_cb(fd, data);
925 tab_completion_emit_enter_signal(td);
928 static void tab_completion_select_show(TabCompData *td)
932 gchar *filter = NULL;
933 gchar *filter_desc = NULL;
937 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
941 title = (td->fd_title) ? td->fd_title : _("Select path");
942 td->fd = file_dialog_new(title, "select_path", td->entry,
943 tab_completion_select_cancel_cb, td);
944 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
945 tab_completion_select_ok_cb, TRUE);
947 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL, FALSE);
959 filter_desc = td->filter_desc;
963 filter_desc = _("All files");
966 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
967 if (strlen(path) == 0) path = NULL;
968 if (td->fd_folders_only)
970 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
974 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, filter, filter_desc);
977 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
980 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data)
982 TabCompData *td = data;
984 tab_completion_select_show(td);
987 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gboolean folders_only)
993 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
997 g_free(td->fd_title);
998 td->fd_title = g_strdup(title);
999 td->fd_folders_only = folders_only;
1001 if (td->fd_button) return;
1003 parent = (td->combo) ? td->combo : td->entry;
1005 hbox = gtk_widget_get_parent(parent);
1006 if (!GTK_IS_BOX(hbox)) return;
1008 td->fd_button = gtk_button_new_with_label("...");
1009 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
1010 G_CALLBACK(tab_completion_button_size_allocate), parent);
1011 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
1012 G_CALLBACK(tab_completion_select_pressed), td);
1014 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
1016 gtk_widget_show(td->fd_button);
1018 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */