2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 - 2009 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
22 #include <sys/types.h>
27 #include <gdk-pixbuf/gdk-pixbuf.h>
30 #include "ui_tabcomp.h"
32 #include "history_list.h"
33 #include "misc.h" /* expand_tilde() */
34 #include "ui_fileops.h"
35 #include "ui_spinner.h"
36 #include "ui_utildlg.h"
38 #include <gdk/gdkkeysyms.h> /* for key values */
41 /* define this to enable a pop-up menu that shows possible matches
42 * #define TAB_COMPLETION_ENABLE_POPUP_MENU
44 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1
45 #define TAB_COMP_POPUP_MAX 1000
47 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
52 /* ----------------------------------------------------------------
53 Tab completion routines, can be connected to any gtkentry widget
54 using the tab_completion_add_to_entry() function.
55 Use remove_trailing_slash() to strip the trailing G_DIR_SEPARATOR.
56 ----------------------------------------------------------------*/
58 typedef struct _TabCompData TabCompData;
64 void (*enter_func)(const gchar *, gpointer);
65 void (*tab_func)(const gchar *, gpointer);
66 void (*tab_append_func)(const gchar *, gpointer, gint);
70 gpointer tab_append_data;
79 gboolean fd_folders_only;
86 static void tab_completion_select_show(TabCompData *td);
87 static gint tab_completion_do(TabCompData *td);
89 static void tab_completion_free_list(TabCompData *td)
104 g_list_free(td->file_list);
105 td->file_list = NULL;
108 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
115 tab_completion_free_list(td);
117 pathl = path_from_utf8(path);
125 while ((dir = readdir(dp)) != NULL)
127 gchar *name = dir->d_name;
128 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
130 gchar *abspath = g_build_filename(pathl, name, NULL);
132 if (g_file_test(abspath, G_FILE_TEST_IS_DIR))
134 gchar *dname = g_strconcat(name, G_DIR_SEPARATOR_S, NULL);
135 list = g_list_prepend(list, path_to_utf8(dname));
140 list = g_list_prepend(list, path_to_utf8(name));
147 td->dir_path = g_strdup(path);
148 td->file_list = list;
152 static void tab_completion_destroy(GtkWidget *widget, gpointer data)
154 TabCompData *td = data;
156 tab_completion_free_list(td);
157 g_free(td->history_key);
159 if (td->fd) file_dialog_close(td->fd);
160 g_free(td->fd_title);
165 static gchar *tab_completion_get_text(TabCompData *td)
169 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
174 text = expand_tilde(text);
181 static gboolean tab_completion_emit_enter_signal(TabCompData *td)
184 if (!td->enter_func) return FALSE;
186 text = tab_completion_get_text(td);
187 td->enter_func(text, td->enter_data);
193 static void tab_completion_emit_tab_signal(TabCompData *td)
196 if (!td->tab_func) return;
198 text = tab_completion_get_text(td);
199 td->tab_func(text, td->tab_data);
203 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
204 void tab_completion_iter_menu_items(GtkWidget *widget, gpointer data)
206 TabCompData *td = data;
209 if (!GTK_WIDGET_VISIBLE(widget)) return;
211 child = gtk_bin_get_child(GTK_BIN(widget));
212 if (GTK_IS_LABEL(child)) {
213 const gchar *text = gtk_label_get_text(GTK_LABEL(child));
214 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
215 const gchar *prefix = filename_from_path(entry_text);
216 guint prefix_len = strlen(prefix);
218 if (strlen(text) < prefix_len || strncmp(text, prefix, prefix_len))
220 /* Hide menu items not matching */
221 gtk_widget_hide(widget);
225 /* Count how many choices are left in the menu */
231 static gboolean tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
233 TabCompData *td = data;
235 if (event->keyval == GDK_Tab ||
236 event->keyval == GDK_BackSpace ||
237 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
239 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
244 buf[0] = event->keyval;
246 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
247 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
249 /* Reduce the number of entries in the menu */
251 gtk_container_foreach(GTK_CONTAINER(widget), tab_completion_iter_menu_items, (gpointer) td);
252 if (td->choices > 1) return TRUE; /* multiple choices */
253 if (td->choices > 0) tab_completion_do(td); /* one choice */
257 gtk_menu_popdown(GTK_MENU(widget));
258 /* doing this does not emit the "selection done" signal, unref it ourselves */
259 #if GTK_CHECK_VERSION(2,12,0)
260 g_object_unref(widget);
262 gtk_widget_unref(widget);
270 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
276 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
279 buf = g_build_filename(td->dir_path, name, NULL);
280 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
281 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
284 tab_completion_emit_tab_signal(td);
287 static void tab_completion_popup_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
289 TabCompData *td = data;
292 PangoRectangle strong_pos, weak_pos;
294 gint xoffset, yoffset;
298 GdkRectangle monitor;
300 gdk_window_get_origin(td->entry->window, x, y);
302 screen = gtk_widget_get_screen(GTK_WIDGET(menu));
303 monitor_num = gdk_screen_get_monitor_at_window(screen, td->entry->window);
304 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
306 gtk_widget_size_request(GTK_WIDGET(menu), &req);
308 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
309 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
311 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
312 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
314 *x += strong_pos.x / PANGO_SCALE + xoffset;
316 height = MIN(td->entry->requisition.height, td->entry->allocation.height);
318 if (req.height > monitor.y + monitor.height - *y - height &&
319 *y - monitor.y > monitor.y + monitor.height - *y)
321 height = MIN(*y - monitor.y, req.height);
322 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
331 static void tab_completion_popup_list(TabCompData *td, GList *list)
344 * well, the menu would be too long anyway...
345 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
346 * this is why menu popups are disabled, this really should be a popup scrollable listview.
348 if (g_list_length(list) > 200) return;
351 menu = popup_menu_short_lived();
354 while (work && count < TAB_COMP_POPUP_MAX)
356 gchar *name = work->data;
359 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
360 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
366 g_signal_connect(G_OBJECT(menu), "key_press_event",
367 G_CALLBACK(tab_completion_popup_key_press), td);
369 /* peek at the current event to get the time, etc. */
370 event = gtk_get_current_event();
372 if (event && event->type == GDK_BUTTON_RELEASE)
374 ebutton = event->button.button;
383 etime = gdk_event_get_time(event);
384 gdk_event_free(event);
391 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
392 tab_completion_popup_pos_cb, td, ebutton, etime);
396 #define CASE_SORT strcmp
399 static gint simple_sort(gconstpointer a, gconstpointer b)
401 return CASE_SORT((gchar *)a, (gchar *)b);
406 static gboolean tab_completion_do(TabCompData *td)
408 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
409 const gchar *entry_file;
412 gboolean home_exp = FALSE;
414 if (entry_text[0] == '\0')
416 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: root directory win32 */
417 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
418 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
423 /* home dir expansion */
424 if (entry_text[0] == '~')
426 entry_dir = expand_tilde(entry_text);
431 entry_dir = g_strdup(entry_text);
434 if (isfile(entry_dir))
438 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
439 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
445 entry_file = filename_from_path(entry_text);
447 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
449 ptr = entry_dir + strlen(entry_dir) - 1;
450 if (ptr[0] == G_DIR_SEPARATOR)
454 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
455 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
458 tab_completion_read_dir(td, entry_dir);
459 td->file_list = g_list_sort(td->file_list, simple_sort);
460 if (td->file_list && !td->file_list->next)
465 file = td->file_list->data;
466 buf = g_build_filename(entry_dir, file, NULL);
469 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
473 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
474 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
478 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
482 tab_completion_popup_list(td, td->file_list);
491 gchar *buf = g_strconcat(entry_dir, 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));
500 ptr = (gchar *)filename_from_path(entry_dir);
501 if (ptr > entry_dir) ptr--;
504 if (strlen(entry_dir) == 0)
507 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: win32 */
510 if (isdir(entry_dir))
514 gint l = strlen(entry_file);
516 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
518 tab_completion_read_dir(td, entry_dir);
521 list = td->file_list;
524 gchar *file = list->data;
525 if (strncmp(entry_file, file, l) == 0)
527 poss = g_list_prepend(poss, file);
536 gchar *file = poss->data;
539 buf = g_build_filename(entry_dir, file, NULL);
540 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
541 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
549 gsize c = strlen(entry_file);
550 gboolean done = FALSE;
551 gchar *test_file = poss->data;
556 if (!list) done = TRUE;
557 while (list && !done)
559 gchar *file = list->data;
560 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
573 file = g_strdup(test_file);
575 buf = g_build_filename(entry_dir, file, NULL);
576 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
577 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
579 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
581 poss = g_list_sort(poss, simple_sort);
582 tab_completion_popup_list(td, poss);
602 static gboolean tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
604 TabCompData *td = data;
605 gboolean stop_signal = FALSE;
607 switch (event->keyval)
610 if (!(event->state & GDK_CONTROL_MASK))
612 if (tab_completion_do(td))
614 tab_completion_emit_tab_signal(td);
619 case GDK_Return: case GDK_KP_Enter:
621 (event->state & GDK_CONTROL_MASK))
623 tab_completion_select_show(td);
626 else if (tab_completion_emit_enter_signal(td))
635 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
637 return (stop_signal);
640 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
643 GtkWidget *entry = data;
645 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
649 if (!GTK_WIDGET_HAS_FOCUS(entry))
651 gtk_widget_grab_focus(entry);
654 if (tab_completion_do(td))
656 tab_completion_emit_tab_signal(td);
660 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
662 GtkWidget *parent = data;
664 if (allocation->height > parent->allocation.height)
666 GtkAllocation button_allocation;
668 button_allocation = button->allocation;
669 button_allocation.height = parent->allocation.height;
670 button_allocation.y = parent->allocation.y +
671 (parent->allocation.height - parent->allocation.height) / 2;
672 gtk_widget_size_allocate(button, &button_allocation);
676 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
682 button = gtk_button_new();
683 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
684 g_signal_connect(G_OBJECT(button), "size_allocate",
685 G_CALLBACK(tab_completion_button_size_allocate), parent);
686 g_signal_connect(G_OBJECT(button), "clicked",
687 G_CALLBACK(tab_completion_button_pressed), entry);
689 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
690 icon = gtk_image_new_from_pixbuf(pixbuf);
691 g_object_unref(pixbuf);
693 gtk_container_add(GTK_CONTAINER(button), icon);
694 gtk_widget_show(icon);
700 *----------------------------------------------------------------------------
702 *----------------------------------------------------------------------------
705 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
706 const gchar *history_key, gint max_levels,
707 void (*enter_func)(const gchar *, gpointer), gpointer data)
711 GtkWidget *combo_entry;
717 box = gtk_hbox_new(FALSE, 0);
719 combo = gtk_combo_box_entry_new_text();
720 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
721 gtk_widget_show(combo);
723 combo_entry = GTK_BIN(combo)->child;
725 gtk_combo_set_case_sensitive(GTK_COMBO(combo), TRUE);
726 gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE);
729 button = tab_completion_create_complete_button(combo_entry, combo);
730 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
731 gtk_widget_show(button);
733 tab_completion_add_to_entry(combo_entry, enter_func, data);
735 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
736 if (!td) return NULL; /* this should never happen! */
739 td->has_history = TRUE;
740 td->history_key = g_strdup(history_key);
741 td->history_levels = max_levels;
743 work = history_list_get_by_key(td->history_key);
745 work = history_list_get_by_key(history_key);
748 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (gchar *)work->data);
755 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
759 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
762 if (entry) *entry = combo_entry;
766 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
768 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
771 if (!td || !td->has_history) return NULL;
773 buf = history_list_find_last_path_by_key(td->history_key);
776 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
782 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
789 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
793 if (!td || !td->has_history) return;
795 history_list_add_to_key(td->history_key, path, td->history_levels);
797 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
799 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
800 gtk_list_store_clear(GTK_LIST_STORE(store));
802 work = history_list_get_by_key(td->history_key);
805 gtk_combo_box_append_text(GTK_COMBO_BOX(td->combo), (gchar *)work->data);
810 if (td->tab_append_func) {
811 td->tab_append_func(path, td->tab_append_data, n);
815 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
816 void (*enter_func)(const gchar *, gpointer), gpointer data)
822 hbox = gtk_hbox_new(FALSE, 0);
824 newentry = gtk_entry_new();
825 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
826 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
827 gtk_widget_show(newentry);
829 button = tab_completion_create_complete_button(newentry, newentry);
830 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
831 gtk_widget_show(button);
833 tab_completion_add_to_entry(newentry, enter_func, data);
835 if (entry) *entry = newentry;
839 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), gpointer data)
844 log_printf("Tab completion error: entry != NULL\n");
848 td = g_new0(TabCompData, 1);
851 td->enter_func = enter_func;
852 td->enter_data = data;
854 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
856 g_signal_connect(G_OBJECT(entry), "key_press_event",
857 G_CALLBACK(tab_completion_key_pressed), td);
858 g_signal_connect(G_OBJECT(entry), "destroy",
859 G_CALLBACK(tab_completion_destroy), td);
862 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
864 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
868 td->tab_func = tab_func;
872 /* Add a callback function called when a new entry is appended to the list */
873 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
875 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
879 td->tab_append_func = tab_append_func;
880 td->tab_append_data = data;
883 gchar *remove_trailing_slash(const gchar *path)
887 if (!path) return NULL;
890 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
892 return g_strndup(path, l);
895 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
897 TabCompData *td = data;
900 file_dialog_close(fd);
903 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
905 TabCompData *td = data;
907 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
909 tab_completion_select_cancel_cb(fd, data);
911 tab_completion_emit_enter_signal(td);
914 static void tab_completion_select_show(TabCompData *td)
921 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
925 title = (td->fd_title) ? td->fd_title : _("Select path");
926 td->fd = file_dialog_new(title, "select_path", td->entry,
927 tab_completion_select_cancel_cb, td);
928 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
929 tab_completion_select_ok_cb, TRUE);
931 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL);
933 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
934 if (strlen(path) == 0) path = NULL;
935 if (td->fd_folders_only)
937 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
941 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, "*", _("All files"));
944 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
947 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data)
949 TabCompData *td = data;
951 tab_completion_select_show(td);
954 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gboolean folders_only)
960 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
964 g_free(td->fd_title);
965 td->fd_title = g_strdup(title);
966 td->fd_folders_only = folders_only;
968 if (td->fd_button) return;
970 parent = (td->combo) ? td->combo : td->entry;
972 hbox = gtk_widget_get_parent(parent);
973 if (!GTK_IS_BOX(hbox)) return;
975 td->fd_button = gtk_button_new_with_label("...");
976 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
977 G_CALLBACK(tab_completion_button_size_allocate), parent);
978 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
979 G_CALLBACK(tab_completion_select_pressed), td);
981 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
983 gtk_widget_show(td->fd_button);
985 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */