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 500
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;
84 static void tab_completion_select_show(TabCompData *td);
86 static void tab_completion_free_list(TabCompData *td)
101 g_list_free(td->file_list);
102 td->file_list = NULL;
105 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
112 tab_completion_free_list(td);
114 pathl = path_from_utf8(path);
122 while ((dir = readdir(dp)) != NULL)
124 gchar *name = dir->d_name;
125 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
127 gchar *abspath = g_build_filename(path, name, NULL);
131 gchar *dname = g_strconcat(name, G_DIR_SEPARATOR_S, NULL);
132 list = g_list_prepend(list, path_to_utf8(dname));
137 list = g_list_prepend(list, path_to_utf8(name));
144 td->dir_path = g_strdup(path);
145 td->file_list = list;
148 static void tab_completion_destroy(GtkWidget *widget, gpointer data)
150 TabCompData *td = data;
152 tab_completion_free_list(td);
153 g_free(td->history_key);
155 if (td->fd) file_dialog_close(td->fd);
156 g_free(td->fd_title);
161 static gchar *tab_completion_get_text(TabCompData *td)
165 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
170 text = expand_tilde(text);
177 static gint tab_completion_emit_enter_signal(TabCompData *td)
180 if (!td->enter_func) return FALSE;
182 text = tab_completion_get_text(td);
183 td->enter_func(text, td->enter_data);
189 static void tab_completion_emit_tab_signal(TabCompData *td)
192 if (!td->tab_func) return;
194 text = tab_completion_get_text(td);
195 td->tab_func(text, td->tab_data);
199 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
201 static gint tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
203 TabCompData *td = data;
205 if (event->keyval == GDK_Tab ||
206 event->keyval == GDK_BackSpace ||
207 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
209 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
214 buf[0] = event->keyval;
216 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
217 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
221 gtk_menu_popdown(GTK_MENU(widget));
222 /* doing this does not emit the "selection done" signal, unref it ourselves */
223 #if GTK_CHECK_VERSION(2,12,0)
224 g_object_unref(widget);
226 gtk_widget_unref(widget);
234 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
240 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
243 buf = g_build_filename(td->dir_path, name, NULL);
244 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
245 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
248 tab_completion_emit_tab_signal(td);
251 static void tab_completion_popup_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
253 TabCompData *td = data;
256 PangoRectangle strong_pos, weak_pos;
258 gint xoffset, yoffset;
262 GdkRectangle monitor;
264 gdk_window_get_origin(td->entry->window, x, y);
266 screen = gtk_widget_get_screen(GTK_WIDGET(menu));
267 monitor_num = gdk_screen_get_monitor_at_window(screen, td->entry->window);
268 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
270 gtk_widget_size_request(GTK_WIDGET(menu), &req);
272 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
273 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
275 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
276 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
278 *x += strong_pos.x / PANGO_SCALE + xoffset;
280 height = MIN(td->entry->requisition.height, td->entry->allocation.height);
282 if (req.height > monitor.y + monitor.height - *y - height &&
283 *y - monitor.y > monitor.y + monitor.height - *y)
285 height = MIN(*y - monitor.y, req.height);
286 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
295 static void tab_completion_popup_list(TabCompData *td, GList *list)
308 * well, the menu would be too long anyway...
309 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
310 * this is why menu popups are disabled, this really should be a popup scrollable listview.
312 if (g_list_length(list) > 200) return;
315 menu = popup_menu_short_lived();
318 while (work && count < TAB_COMP_POPUP_MAX)
320 gchar *name = work->data;
323 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
324 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
330 g_signal_connect(G_OBJECT(menu), "key_press_event",
331 G_CALLBACK(tab_completion_popup_key_press), td);
333 /* peek at the current event to get the time, etc. */
334 event = gtk_get_current_event();
336 if (event && event->type == GDK_BUTTON_RELEASE)
338 ebutton = event->button.button;
347 etime = gdk_event_get_time(event);
348 gdk_event_free(event);
355 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
356 tab_completion_popup_pos_cb, td, ebutton, etime);
360 #define CASE_SORT strcmp
363 static gint simple_sort(gconstpointer a, gconstpointer b)
365 return CASE_SORT((gchar *)a, (gchar *)b);
370 static gint tab_completion_do(TabCompData *td)
372 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
373 const gchar *entry_file;
376 gint home_exp = FALSE;
378 /* home dir expansion */
379 if (entry_text[0] == '~')
381 entry_dir = expand_tilde(entry_text);
386 entry_dir = g_strdup(entry_text);
389 entry_file = filename_from_path(entry_text);
391 if (isfile(entry_dir))
395 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
396 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
402 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
404 ptr = entry_dir + strlen(entry_dir) - 1;
405 if (ptr[0] == G_DIR_SEPARATOR)
409 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
410 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
413 tab_completion_read_dir(td, entry_dir);
414 td->file_list = g_list_sort(td->file_list, simple_sort);
415 if (td->file_list && !td->file_list->next)
420 file = td->file_list->data;
421 buf = g_build_filename(entry_dir, file, NULL);
424 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
428 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
429 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
433 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
437 tab_completion_popup_list(td, td->file_list);
446 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
447 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
448 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
455 ptr = (gchar *)filename_from_path(entry_dir);
456 if (ptr > entry_dir) ptr--;
459 if (strlen(entry_dir) == 0)
462 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: win32 */
465 if (isdir(entry_dir))
469 gint l = strlen(entry_file);
471 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
473 tab_completion_read_dir(td, entry_dir);
476 if (strcmp(entry_dir, G_DIR_SEPARATOR_S) == 0) entry_dir[0] = '\0'; /* FIXME: win32 */
478 list = td->file_list;
481 gchar *file = list->data;
482 if (strncmp(entry_file, file, l) == 0)
484 poss = g_list_prepend(poss, file);
493 gchar *file = poss->data;
496 buf = g_build_filename(entry_dir, file, NULL);
500 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
504 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
505 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
513 gsize c = strlen(entry_file);
515 gchar *test_file = poss->data;
520 if (!list) done = TRUE;
521 while (list && !done)
523 gchar *file = list->data;
524 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
537 file = g_strdup(test_file);
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));
543 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
545 poss = g_list_sort(poss, simple_sort);
546 tab_completion_popup_list(td, poss);
566 static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
568 TabCompData *td = data;
569 gint stop_signal = FALSE;
571 switch (event->keyval)
574 if (!(event->state & GDK_CONTROL_MASK))
576 if (tab_completion_do(td))
578 tab_completion_emit_tab_signal(td);
583 case GDK_Return: case GDK_KP_Enter:
585 (event->state & GDK_CONTROL_MASK))
587 tab_completion_select_show(td);
590 else if (tab_completion_emit_enter_signal(td))
599 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
601 return (stop_signal);
604 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
607 GtkWidget *entry = data;
609 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
613 if (!GTK_WIDGET_HAS_FOCUS(entry))
615 gtk_widget_grab_focus(entry);
618 if (tab_completion_do(td))
620 tab_completion_emit_tab_signal(td);
624 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
626 GtkWidget *parent = data;
628 if (allocation->height > parent->allocation.height)
630 GtkAllocation button_allocation;
632 button_allocation = button->allocation;
633 button_allocation.height = parent->allocation.height;
634 button_allocation.y = parent->allocation.y +
635 (parent->allocation.height - parent->allocation.height) / 2;
636 gtk_widget_size_allocate(button, &button_allocation);
640 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
646 button = gtk_button_new();
647 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
648 g_signal_connect(G_OBJECT(button), "size_allocate",
649 G_CALLBACK(tab_completion_button_size_allocate), parent);
650 g_signal_connect(G_OBJECT(button), "clicked",
651 G_CALLBACK(tab_completion_button_pressed), entry);
653 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
654 icon = gtk_image_new_from_pixbuf(pixbuf);
655 g_object_unref(pixbuf);
657 gtk_container_add(GTK_CONTAINER(button), icon);
658 gtk_widget_show(icon);
664 *----------------------------------------------------------------------------
666 *----------------------------------------------------------------------------
669 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
670 const gchar *history_key, gint max_levels,
671 void (*enter_func)(const gchar *, gpointer), gpointer data)
675 GtkWidget *combo_entry;
681 box = gtk_hbox_new(FALSE, 0);
683 combo = gtk_combo_box_entry_new_text();
684 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
685 gtk_widget_show(combo);
687 combo_entry = GTK_BIN(combo)->child;
689 gtk_combo_set_case_sensitive(GTK_COMBO(combo), TRUE);
690 gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE);
693 button = tab_completion_create_complete_button(combo_entry, combo);
694 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
695 gtk_widget_show(button);
697 tab_completion_add_to_entry(combo_entry, enter_func, data);
699 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
700 if (!td) return NULL; /* this should never happen! */
703 td->has_history = TRUE;
704 td->history_key = g_strdup(history_key);
705 td->history_levels = max_levels;
707 work = history_list_get_by_key(td->history_key);
709 work = history_list_get_by_key(history_key);
712 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (gchar *)work->data);
719 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
723 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
726 if (entry) *entry = combo_entry;
730 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
732 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
735 if (!td || !td->has_history) return NULL;
737 buf = history_list_find_last_path_by_key(td->history_key);
740 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
746 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
753 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
757 if (!td || !td->has_history) return;
759 history_list_add_to_key(td->history_key, path, td->history_levels);
761 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
763 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
764 gtk_list_store_clear(GTK_LIST_STORE(store));
766 work = history_list_get_by_key(td->history_key);
769 gtk_combo_box_append_text(GTK_COMBO_BOX(td->combo), (gchar *)work->data);
774 if (td->tab_append_func) {
775 td->tab_append_func(path, td->tab_append_data, n);
779 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
780 void (*enter_func)(const gchar *, gpointer), gpointer data)
786 hbox = gtk_hbox_new(FALSE, 0);
788 newentry = gtk_entry_new();
789 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
790 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
791 gtk_widget_show(newentry);
793 button = tab_completion_create_complete_button(newentry, newentry);
794 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
795 gtk_widget_show(button);
797 tab_completion_add_to_entry(newentry, enter_func, data);
799 if (entry) *entry = newentry;
803 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), gpointer data)
808 log_printf("Tab completion error: entry != NULL\n");
812 td = g_new0(TabCompData, 1);
815 td->enter_func = enter_func;
816 td->enter_data = data;
818 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
820 g_signal_connect(G_OBJECT(entry), "key_press_event",
821 G_CALLBACK(tab_completion_key_pressed), td);
822 g_signal_connect(G_OBJECT(entry), "destroy",
823 G_CALLBACK(tab_completion_destroy), td);
826 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
828 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
832 td->tab_func = tab_func;
836 /* Add a callback function called when a new entry is appended to the list */
837 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
839 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
843 td->tab_append_func = tab_append_func;
844 td->tab_append_data = data;
847 gchar *remove_trailing_slash(const gchar *path)
851 if (!path) return NULL;
854 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
856 return g_strndup(path, l);
859 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
861 TabCompData *td = data;
864 file_dialog_close(fd);
867 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
869 TabCompData *td = data;
871 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
873 tab_completion_select_cancel_cb(fd, data);
875 tab_completion_emit_enter_signal(td);
878 static void tab_completion_select_show(TabCompData *td)
885 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
889 title = (td->fd_title) ? td->fd_title : _("Select path");
890 td->fd = file_dialog_new(title, "select_path", td->entry,
891 tab_completion_select_cancel_cb, td);
892 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
893 tab_completion_select_ok_cb, TRUE);
895 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL);
897 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
898 if (strlen(path) == 0) path = NULL;
899 if (td->fd_folders_only)
901 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
905 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, "*", _("All files"));
908 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
911 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data)
913 TabCompData *td = data;
915 tab_completion_select_show(td);
918 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gint folders_only)
924 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
928 g_free(td->fd_title);
929 td->fd_title = g_strdup(title);
930 td->fd_folders_only = folders_only;
932 if (td->fd_button) return;
934 parent = (td->combo) ? td->combo : td->entry;
936 hbox = gtk_widget_get_parent(parent);
937 if (!GTK_IS_BOX(hbox)) return;
939 td->fd_button = gtk_button_new_with_label("...");
940 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
941 G_CALLBACK(tab_completion_button_size_allocate), parent);
942 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
943 G_CALLBACK(tab_completion_select_pressed), td);
945 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
947 gtk_widget_show(td->fd_button);
949 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */