2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 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 list = g_list_prepend(list, path_to_utf8(name));
132 td->dir_path = g_strdup(path);
133 td->file_list = list;
136 static void tab_completion_destroy(GtkWidget *widget, gpointer data)
138 TabCompData *td = data;
140 tab_completion_free_list(td);
141 g_free(td->history_key);
143 if (td->fd) file_dialog_close(td->fd);
144 g_free(td->fd_title);
149 static gchar *tab_completion_get_text(TabCompData *td)
153 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
158 text = expand_tilde(text);
165 static gint tab_completion_emit_enter_signal(TabCompData *td)
168 if (!td->enter_func) return FALSE;
170 text = tab_completion_get_text(td);
171 td->enter_func(text, td->enter_data);
177 static void tab_completion_emit_tab_signal(TabCompData *td)
180 if (!td->tab_func) return;
182 text = tab_completion_get_text(td);
183 td->tab_func(text, td->tab_data);
187 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
189 static gint tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
191 TabCompData *td = data;
193 if (event->keyval == GDK_Tab ||
194 event->keyval == GDK_BackSpace ||
195 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
197 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
202 buf[0] = event->keyval;
204 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
205 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
209 gtk_menu_popdown(GTK_MENU(widget));
210 /* doing this does not emit the "selection done" signal, unref it ourselves */
211 #if GTK_CHECK_VERSION(2,12,0)
212 g_object_unref(widget);
214 gtk_widget_unref(widget);
222 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
228 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
231 buf = g_build_filename(td->dir_path, name, NULL);
232 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
233 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
236 tab_completion_emit_tab_signal(td);
239 static void tab_completion_popup_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
241 TabCompData *td = data;
244 PangoRectangle strong_pos, weak_pos;
246 gint xoffset, yoffset;
250 GdkRectangle monitor;
252 gdk_window_get_origin(td->entry->window, x, y);
254 screen = gtk_widget_get_screen(GTK_WIDGET(menu));
255 monitor_num = gdk_screen_get_monitor_at_window(screen, td->entry->window);
256 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
258 gtk_widget_size_request(GTK_WIDGET(menu), &req);
260 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
261 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
263 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
264 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
266 *x += strong_pos.x / PANGO_SCALE + xoffset;
268 height = MIN(td->entry->requisition.height, td->entry->allocation.height);
270 if (req.height > monitor.y + monitor.height - *y - height &&
271 *y - monitor.y > monitor.y + monitor.height - *y)
273 height = MIN(*y - monitor.y, req.height);
274 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
283 static void tab_completion_popup_list(TabCompData *td, GList *list)
296 * well, the menu would be too long anyway...
297 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
298 * this is why menu popups are disabled, this really should be a popup scrollable listview.
300 if (g_list_length(list) > 200) return;
303 menu = popup_menu_short_lived();
306 while (work && count < TAB_COMP_POPUP_MAX)
308 gchar *name = work->data;
311 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
312 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
318 g_signal_connect(G_OBJECT(menu), "key_press_event",
319 G_CALLBACK(tab_completion_popup_key_press), td);
321 /* peek at the current event to get the time, etc. */
322 event = gtk_get_current_event();
324 if (event && event->type == GDK_BUTTON_RELEASE)
326 ebutton = event->button.button;
335 etime = gdk_event_get_time(event);
336 gdk_event_free(event);
343 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
344 tab_completion_popup_pos_cb, td, ebutton, etime);
348 #define CASE_SORT strcmp
351 static gint simple_sort(gconstpointer a, gconstpointer b)
353 return CASE_SORT((gchar *)a, (gchar *)b);
358 static gint tab_completion_do(TabCompData *td)
360 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
361 const gchar *entry_file;
364 gint home_exp = FALSE;
366 /* home dir expansion */
367 if (entry_text[0] == '~')
369 entry_dir = expand_tilde(entry_text);
374 entry_dir = g_strdup(entry_text);
377 entry_file = filename_from_path(entry_text);
379 if (isfile(entry_dir))
383 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
384 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
390 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
392 ptr = entry_dir + strlen(entry_dir) - 1;
393 if (ptr[0] == G_DIR_SEPARATOR)
397 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
398 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
401 tab_completion_read_dir(td, entry_dir);
402 td->file_list = g_list_sort(td->file_list, simple_sort);
403 if (td->file_list && !td->file_list->next)
408 file = td->file_list->data;
409 buf = g_build_filename(entry_dir, file, NULL);
412 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
416 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
417 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
421 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
425 tab_completion_popup_list(td, td->file_list);
434 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
435 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
436 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
443 ptr = (gchar *)filename_from_path(entry_dir);
444 if (ptr > entry_dir) ptr--;
447 if (strlen(entry_dir) == 0)
450 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: win32 */
453 if (isdir(entry_dir))
457 gint l = strlen(entry_file);
459 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
461 tab_completion_read_dir(td, entry_dir);
464 if (strcmp(entry_dir, G_DIR_SEPARATOR_S) == 0) entry_dir[0] = '\0'; /* FIXME: win32 */
466 list = td->file_list;
469 gchar *file = list->data;
470 if (strncmp(entry_file, file, l) == 0)
472 poss = g_list_prepend(poss, file);
481 gchar *file = poss->data;
484 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));
501 gsize c = strlen(entry_file);
503 gchar *test_file = poss->data;
508 if (!list) done = TRUE;
509 while (list && !done)
511 gchar *file = list->data;
512 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
525 file = g_strdup(test_file);
527 buf = g_build_filename(entry_dir, file, NULL);
528 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
529 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
531 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
533 poss = g_list_sort(poss, simple_sort);
534 tab_completion_popup_list(td, poss);
554 static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
556 TabCompData *td = data;
557 gint stop_signal = FALSE;
559 switch (event->keyval)
562 if (!(event->state & GDK_CONTROL_MASK))
564 if (tab_completion_do(td))
566 tab_completion_emit_tab_signal(td);
571 case GDK_Return: case GDK_KP_Enter:
573 (event->state & GDK_CONTROL_MASK))
575 tab_completion_select_show(td);
578 else if (tab_completion_emit_enter_signal(td))
587 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
589 return (stop_signal);
592 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
595 GtkWidget *entry = data;
597 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
601 if (!GTK_WIDGET_HAS_FOCUS(entry))
603 gtk_widget_grab_focus(entry);
606 if (tab_completion_do(td))
608 tab_completion_emit_tab_signal(td);
612 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
614 GtkWidget *parent = data;
616 if (allocation->height > parent->allocation.height)
618 GtkAllocation button_allocation;
620 button_allocation = button->allocation;
621 button_allocation.height = parent->allocation.height;
622 button_allocation.y = parent->allocation.y +
623 (parent->allocation.height - parent->allocation.height) / 2;
624 gtk_widget_size_allocate(button, &button_allocation);
628 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
634 button = gtk_button_new();
635 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
636 g_signal_connect(G_OBJECT(button), "size_allocate",
637 G_CALLBACK(tab_completion_button_size_allocate), parent);
638 g_signal_connect(G_OBJECT(button), "clicked",
639 G_CALLBACK(tab_completion_button_pressed), entry);
641 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
642 icon = gtk_image_new_from_pixbuf(pixbuf);
643 g_object_unref(pixbuf);
645 gtk_container_add(GTK_CONTAINER(button), icon);
646 gtk_widget_show(icon);
652 *----------------------------------------------------------------------------
654 *----------------------------------------------------------------------------
657 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
658 const gchar *history_key, gint max_levels,
659 void (*enter_func)(const gchar *, gpointer), gpointer data)
663 GtkWidget *combo_entry;
669 box = gtk_hbox_new(FALSE, 0);
671 combo = gtk_combo_box_entry_new_text();
672 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
673 gtk_widget_show(combo);
675 combo_entry = GTK_BIN(combo)->child;
677 gtk_combo_set_case_sensitive(GTK_COMBO(combo), TRUE);
678 gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE);
681 button = tab_completion_create_complete_button(combo_entry, combo);
682 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
683 gtk_widget_show(button);
685 tab_completion_add_to_entry(combo_entry, enter_func, data);
687 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
688 if (!td) return NULL; /* this should never happen! */
691 td->has_history = TRUE;
692 td->history_key = g_strdup(history_key);
693 td->history_levels = max_levels;
695 work = history_list_get_by_key(td->history_key);
697 work = history_list_get_by_key(history_key);
700 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (gchar *)work->data);
707 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
711 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
714 if (entry) *entry = combo_entry;
718 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
720 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
723 if (!td || !td->has_history) return NULL;
725 buf = history_list_find_last_path_by_key(td->history_key);
728 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
734 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
741 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
745 if (!td || !td->has_history) return;
747 history_list_add_to_key(td->history_key, path, td->history_levels);
749 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
751 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
752 gtk_list_store_clear(GTK_LIST_STORE(store));
754 work = history_list_get_by_key(td->history_key);
757 gtk_combo_box_append_text(GTK_COMBO_BOX(td->combo), (gchar *)work->data);
762 if (td->tab_append_func) {
763 td->tab_append_func(path, td->tab_append_data, n);
767 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
768 void (*enter_func)(const gchar *, gpointer), gpointer data)
774 hbox = gtk_hbox_new(FALSE, 0);
776 newentry = gtk_entry_new();
777 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
778 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
779 gtk_widget_show(newentry);
781 button = tab_completion_create_complete_button(newentry, newentry);
782 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
783 gtk_widget_show(button);
785 tab_completion_add_to_entry(newentry, enter_func, data);
787 if (entry) *entry = newentry;
791 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), gpointer data)
796 log_printf("Tab completion error: entry != NULL\n");
800 td = g_new0(TabCompData, 1);
803 td->file_list = NULL;
804 td->enter_func = enter_func;
805 td->enter_data = data;
809 td->has_history = FALSE;
810 td->history_key = NULL;
811 td->history_levels = 0;
813 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
815 g_signal_connect(G_OBJECT(entry), "key_press_event",
816 G_CALLBACK(tab_completion_key_pressed), td);
817 g_signal_connect(G_OBJECT(entry), "destroy",
818 G_CALLBACK(tab_completion_destroy), td);
821 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
823 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
827 td->tab_func = tab_func;
831 /* Add a callback function called when a new entry is appended to the list */
832 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
834 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
838 td->tab_append_func = tab_append_func;
839 td->tab_append_data = data;
842 gchar *remove_trailing_slash(const gchar *path)
846 if (!path) return NULL;
849 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
851 return g_strndup(path, l);
854 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
856 TabCompData *td = data;
859 file_dialog_close(fd);
862 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
864 TabCompData *td = data;
866 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
868 tab_completion_select_cancel_cb(fd, data);
870 tab_completion_emit_enter_signal(td);
873 static void tab_completion_select_show(TabCompData *td)
880 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
884 title = (td->fd_title) ? td->fd_title : _("Select path");
885 td->fd = file_dialog_new(title, "select_path", td->entry,
886 tab_completion_select_cancel_cb, td);
887 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
888 tab_completion_select_ok_cb, TRUE);
890 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL);
892 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
893 if (strlen(path) == 0) path = NULL;
894 if (td->fd_folders_only)
896 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
900 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, "*", _("All files"));
903 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
906 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data)
908 TabCompData *td = data;
910 tab_completion_select_show(td);
913 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gint folders_only)
919 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
923 g_free(td->fd_title);
924 td->fd_title = g_strdup(title);
925 td->fd_folders_only = folders_only;
927 if (td->fd_button) return;
929 parent = (td->combo) ? td->combo : td->entry;
931 hbox = gtk_widget_get_parent(parent);
932 if (!GTK_IS_BOX(hbox)) return;
934 td->fd_button = gtk_button_new_with_label("...");
935 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
936 G_CALLBACK(tab_completion_button_size_allocate), parent);
937 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
938 G_CALLBACK(tab_completion_select_pressed), td);
940 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
942 gtk_widget_show(td->fd_button);
944 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */