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 "ui_fileops.h"
34 #include "ui_spinner.h"
35 #include "ui_utildlg.h"
37 #include <gdk/gdkkeysyms.h> /* for key values */
40 /* define this to enable a pop-up menu that shows possible matches
41 * #define TAB_COMPLETION_ENABLE_POPUP_MENU
43 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1
44 #define TAB_COMP_POPUP_MAX 500
46 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
51 /* ----------------------------------------------------------------
52 Tab completion routines, can be connected to any gtkentry widget
53 using the tab_completion_add_to_entry() function.
54 Use remove_trailing_slash() to strip the trailing G_DIR_SEPARATOR.
55 ----------------------------------------------------------------*/
57 typedef struct _TabCompData TabCompData;
63 void (*enter_func)(const gchar *, gpointer);
64 void (*tab_func)(const gchar *, gpointer);
80 static void tab_completion_select_show(TabCompData *td);
82 static void tab_completion_free_list(TabCompData *td)
97 g_list_free(td->file_list);
101 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
108 tab_completion_free_list(td);
110 pathl = path_from_utf8(path);
118 while ((dir = readdir(dp)) != NULL)
120 gchar *name = dir->d_name;
121 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
123 list = g_list_prepend(list, path_to_utf8(name));
128 td->dir_path = g_strdup(path);
129 td->file_list = list;
132 static void tab_completion_destroy(GtkWidget *widget, gpointer data)
134 TabCompData *td = data;
136 tab_completion_free_list(td);
137 g_free(td->history_key);
139 if (td->fd) file_dialog_close(td->fd);
140 g_free(td->fd_title);
145 static gchar *tab_completion_get_text(TabCompData *td)
149 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
154 text = expand_tilde(text);
161 static gint tab_completion_emit_enter_signal(TabCompData *td)
164 if (!td->enter_func) return FALSE;
166 text = tab_completion_get_text(td);
167 td->enter_func(text, td->enter_data);
173 static void tab_completion_emit_tab_signal(TabCompData *td)
176 if (!td->tab_func) return;
178 text = tab_completion_get_text(td);
179 td->tab_func(text, td->tab_data);
183 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
185 static gint tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
187 TabCompData *td = data;
189 if (event->keyval == GDK_Tab ||
190 event->keyval == GDK_BackSpace ||
191 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
193 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
198 buf[0] = event->keyval;
200 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
201 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
205 gtk_menu_popdown(GTK_MENU(widget));
206 /* doing this does not emit the "selection done" signal, unref it ourselves */
207 gtk_widget_unref(widget);
215 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
221 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
224 buf = g_build_filename(td->dir_path, name, NULL);
225 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
226 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
229 tab_completion_emit_tab_signal(td);
232 static void tab_completion_popup_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
234 TabCompData *td = data;
237 PangoRectangle strong_pos, weak_pos;
239 gint xoffset, yoffset;
243 GdkRectangle monitor;
245 gdk_window_get_origin(td->entry->window, x, y);
247 screen = gtk_widget_get_screen(GTK_WIDGET(menu));
248 monitor_num = gdk_screen_get_monitor_at_window(screen, td->entry->window);
249 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
251 gtk_widget_size_request(GTK_WIDGET(menu), &req);
253 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
254 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
256 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
257 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
259 *x += strong_pos.x / PANGO_SCALE + xoffset;
261 height = MIN(td->entry->requisition.height, td->entry->allocation.height);
263 if (req.height > monitor.y + monitor.height - *y - height &&
264 *y - monitor.y > monitor.y + monitor.height - *y)
266 height = MIN(*y - monitor.y, req.height);
267 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
276 static void tab_completion_popup_list(TabCompData *td, GList *list)
289 * well, the menu would be too long anyway...
290 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
291 * this is why menu popups are disabled, this really should be a popup scrollable listview.
293 if (g_list_length(list) > 200) return;
296 menu = popup_menu_short_lived();
299 while (work && count < TAB_COMP_POPUP_MAX)
301 gchar *name = work->data;
304 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
305 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
311 g_signal_connect(G_OBJECT(menu), "key_press_event",
312 G_CALLBACK(tab_completion_popup_key_press), td);
314 /* peek at the current event to get the time, etc. */
315 event = gtk_get_current_event();
317 if (event && event->type == GDK_BUTTON_RELEASE)
319 ebutton = event->button.button;
328 etime = gdk_event_get_time(event);
329 gdk_event_free(event);
336 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
337 tab_completion_popup_pos_cb, td, ebutton, etime);
341 #define CASE_SORT strcmp
344 static gint simple_sort(gconstpointer a, gconstpointer b)
346 return CASE_SORT((gchar *)a, (gchar *)b);
351 static gint tab_completion_do(TabCompData *td)
353 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
354 const gchar *entry_file;
357 gint home_exp = FALSE;
359 /* home dir expansion */
360 if (entry_text[0] == '~')
362 entry_dir = expand_tilde(entry_text);
367 entry_dir = g_strdup(entry_text);
370 entry_file = filename_from_path(entry_text);
372 if (isfile(entry_dir))
376 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
377 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
383 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
385 ptr = entry_dir + strlen(entry_dir) - 1;
386 if (ptr[0] == G_DIR_SEPARATOR)
390 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
391 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
394 tab_completion_read_dir(td, entry_dir);
395 td->file_list = g_list_sort(td->file_list, simple_sort);
396 if (td->file_list && !td->file_list->next)
401 file = td->file_list->data;
402 buf = g_build_filename(entry_dir, file, NULL);
405 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
409 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
410 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
414 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
418 tab_completion_popup_list(td, td->file_list);
427 gchar *buf = g_strconcat(entry_dir, 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));
436 ptr = (gchar *)filename_from_path(entry_dir);
437 if (ptr > entry_dir) ptr--;
440 if (strlen(entry_dir) == 0)
443 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: win32 */
446 if (isdir(entry_dir))
450 gint l = strlen(entry_file);
452 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
454 tab_completion_read_dir(td, entry_dir);
457 if (strcmp(entry_dir, G_DIR_SEPARATOR_S) == 0) entry_dir[0] = '\0'; /* FIXME: win32 */
459 list = td->file_list;
462 gchar *file = list->data;
463 if (strncmp(entry_file, file, l) == 0)
465 poss = g_list_prepend(poss, file);
474 gchar *file = poss->data;
477 buf = g_build_filename(entry_dir, file, NULL);
481 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
485 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
486 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
494 gsize c = strlen(entry_file);
496 gchar *test_file = poss->data;
501 if (!list) done = TRUE;
502 while (list && !done)
504 gchar *file = list->data;
505 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
518 file = g_strdup(test_file);
520 buf = g_build_filename(entry_dir, file, NULL);
521 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
522 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
524 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
526 poss = g_list_sort(poss, simple_sort);
527 tab_completion_popup_list(td, poss);
547 static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
549 TabCompData *td = data;
550 gint stop_signal = FALSE;
552 switch (event->keyval)
555 if (!(event->state & GDK_CONTROL_MASK))
557 if (tab_completion_do(td))
559 tab_completion_emit_tab_signal(td);
564 case GDK_Return: case GDK_KP_Enter:
566 (event->state & GDK_CONTROL_MASK))
568 tab_completion_select_show(td);
571 else if (tab_completion_emit_enter_signal(td))
580 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
582 return (stop_signal);
585 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
588 GtkWidget *entry = data;
590 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
594 if (!GTK_WIDGET_HAS_FOCUS(entry))
596 gtk_widget_grab_focus(entry);
599 if (tab_completion_do(td))
601 tab_completion_emit_tab_signal(td);
605 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
607 GtkWidget *parent = data;
609 if (allocation->height > parent->allocation.height)
611 GtkAllocation button_allocation;
613 button_allocation = button->allocation;
614 button_allocation.height = parent->allocation.height;
615 button_allocation.y = parent->allocation.y +
616 (parent->allocation.height - parent->allocation.height) / 2;
617 gtk_widget_size_allocate(button, &button_allocation);
621 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
627 button = gtk_button_new();
628 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
629 g_signal_connect(G_OBJECT(button), "size_allocate",
630 G_CALLBACK(tab_completion_button_size_allocate), parent);
631 g_signal_connect(G_OBJECT(button), "clicked",
632 G_CALLBACK(tab_completion_button_pressed), entry);
634 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
635 icon = gtk_image_new_from_pixbuf(pixbuf);
636 gdk_pixbuf_unref(pixbuf);
637 gtk_container_add(GTK_CONTAINER(button), icon);
638 gtk_widget_show(icon);
644 *----------------------------------------------------------------------------
646 *----------------------------------------------------------------------------
649 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
650 const gchar *history_key, gint max_levels,
651 void (*enter_func)(const gchar *, gpointer), gpointer data)
655 GtkWidget *combo_entry;
661 box = gtk_hbox_new(FALSE, 0);
663 combo = gtk_combo_box_entry_new_text();
664 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
665 gtk_widget_show(combo);
667 combo_entry = GTK_BIN(combo)->child;
669 gtk_combo_set_case_sensitive(GTK_COMBO(combo), TRUE);
670 gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE);
673 button = tab_completion_create_complete_button(combo_entry, combo);
674 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
675 gtk_widget_show(button);
677 tab_completion_add_to_entry(combo_entry, enter_func, data);
679 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
680 if (!td) return NULL; /* this should never happen! */
683 td->has_history = TRUE;
684 td->history_key = g_strdup(history_key);
685 td->history_levels = max_levels;
687 work = history_list_get_by_key(td->history_key);
689 work = history_list_get_by_key(history_key);
692 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (gchar *)work->data);
699 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
703 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
706 if (entry) *entry = combo_entry;
710 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
712 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
715 if (!td || !td->has_history) return NULL;
717 buf = history_list_find_last_path_by_key(td->history_key);
720 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
726 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
732 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
736 if (!td || !td->has_history) return;
738 history_list_add_to_key(td->history_key, path, td->history_levels);
740 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
742 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
743 gtk_list_store_clear(GTK_LIST_STORE(store));
745 work = history_list_get_by_key(td->history_key);
748 gtk_combo_box_append_text(GTK_COMBO_BOX(td->combo), (gchar *)work->data);
753 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
754 void (*enter_func)(const gchar *, gpointer), gpointer data)
760 hbox = gtk_hbox_new(FALSE, 0);
762 newentry = gtk_entry_new();
763 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
764 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
765 gtk_widget_show(newentry);
767 button = tab_completion_create_complete_button(newentry, newentry);
768 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
769 gtk_widget_show(button);
771 tab_completion_add_to_entry(newentry, enter_func, data);
773 if (entry) *entry = newentry;
777 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), gpointer data)
782 log_printf("Tab completion error: entry != NULL\n");
786 td = g_new0(TabCompData, 1);
789 td->file_list = NULL;
790 td->enter_func = enter_func;
791 td->enter_data = data;
795 td->has_history = FALSE;
796 td->history_key = NULL;
797 td->history_levels = 0;
799 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
801 g_signal_connect(G_OBJECT(entry), "key_press_event",
802 G_CALLBACK(tab_completion_key_pressed), td);
803 g_signal_connect(G_OBJECT(entry), "destroy",
804 G_CALLBACK(tab_completion_destroy), td);
807 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
809 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
813 td->tab_func = tab_func;
817 gchar *remove_trailing_slash(const gchar *path)
821 if (!path) return NULL;
824 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
826 return g_strndup(path, l);
829 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
831 TabCompData *td = data;
834 file_dialog_close(fd);
837 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
839 TabCompData *td = data;
841 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
843 tab_completion_select_cancel_cb(fd, data);
845 tab_completion_emit_enter_signal(td);
848 static void tab_completion_select_show(TabCompData *td)
855 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
859 title = (td->fd_title) ? td->fd_title : _("Select path");
860 td->fd = file_dialog_new(title, GQ_WMCLASS, "select_path", td->entry,
861 tab_completion_select_cancel_cb, td);
862 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
863 tab_completion_select_ok_cb, TRUE);
865 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL);
867 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
868 if (strlen(path) == 0) path = NULL;
869 if (td->fd_folders_only)
871 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
875 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, "*", _("All files"));
878 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
881 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data)
883 TabCompData *td = data;
885 tab_completion_select_show(td);
888 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gint folders_only)
894 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
898 g_free(td->fd_title);
899 td->fd_title = g_strdup(title);
900 td->fd_folders_only = folders_only;
902 if (td->fd_button) return;
904 parent = (td->combo) ? td->combo : td->entry;
906 hbox = gtk_widget_get_parent(parent);
907 if (!GTK_IS_BOX(hbox)) return;
909 td->fd_button = gtk_button_new_with_label("...");
910 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
911 G_CALLBACK(tab_completion_button_size_allocate), parent);
912 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
913 G_CALLBACK(tab_completion_select_pressed), td);
915 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
917 gtk_widget_show(td->fd_button);