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(pathl, name, NULL);
129 if (g_file_test(abspath, G_FILE_TEST_IS_DIR))
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;
149 static void tab_completion_destroy(GtkWidget *widget, gpointer data)
151 TabCompData *td = data;
153 tab_completion_free_list(td);
154 g_free(td->history_key);
156 if (td->fd) file_dialog_close(td->fd);
157 g_free(td->fd_title);
162 static gchar *tab_completion_get_text(TabCompData *td)
166 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
171 text = expand_tilde(text);
178 static gint tab_completion_emit_enter_signal(TabCompData *td)
181 if (!td->enter_func) return FALSE;
183 text = tab_completion_get_text(td);
184 td->enter_func(text, td->enter_data);
190 static void tab_completion_emit_tab_signal(TabCompData *td)
193 if (!td->tab_func) return;
195 text = tab_completion_get_text(td);
196 td->tab_func(text, td->tab_data);
200 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
202 static gint tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
204 TabCompData *td = data;
206 if (event->keyval == GDK_Tab ||
207 event->keyval == GDK_BackSpace ||
208 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
210 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
215 buf[0] = event->keyval;
217 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
218 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
222 gtk_menu_popdown(GTK_MENU(widget));
223 /* doing this does not emit the "selection done" signal, unref it ourselves */
224 #if GTK_CHECK_VERSION(2,12,0)
225 g_object_unref(widget);
227 gtk_widget_unref(widget);
235 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
241 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
244 buf = g_build_filename(td->dir_path, name, NULL);
245 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
246 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
249 tab_completion_emit_tab_signal(td);
252 static void tab_completion_popup_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
254 TabCompData *td = data;
257 PangoRectangle strong_pos, weak_pos;
259 gint xoffset, yoffset;
263 GdkRectangle monitor;
265 gdk_window_get_origin(td->entry->window, x, y);
267 screen = gtk_widget_get_screen(GTK_WIDGET(menu));
268 monitor_num = gdk_screen_get_monitor_at_window(screen, td->entry->window);
269 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
271 gtk_widget_size_request(GTK_WIDGET(menu), &req);
273 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
274 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
276 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
277 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
279 *x += strong_pos.x / PANGO_SCALE + xoffset;
281 height = MIN(td->entry->requisition.height, td->entry->allocation.height);
283 if (req.height > monitor.y + monitor.height - *y - height &&
284 *y - monitor.y > monitor.y + monitor.height - *y)
286 height = MIN(*y - monitor.y, req.height);
287 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
296 static void tab_completion_popup_list(TabCompData *td, GList *list)
309 * well, the menu would be too long anyway...
310 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
311 * this is why menu popups are disabled, this really should be a popup scrollable listview.
313 if (g_list_length(list) > 200) return;
316 menu = popup_menu_short_lived();
319 while (work && count < TAB_COMP_POPUP_MAX)
321 gchar *name = work->data;
324 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
325 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
331 g_signal_connect(G_OBJECT(menu), "key_press_event",
332 G_CALLBACK(tab_completion_popup_key_press), td);
334 /* peek at the current event to get the time, etc. */
335 event = gtk_get_current_event();
337 if (event && event->type == GDK_BUTTON_RELEASE)
339 ebutton = event->button.button;
348 etime = gdk_event_get_time(event);
349 gdk_event_free(event);
356 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
357 tab_completion_popup_pos_cb, td, ebutton, etime);
361 #define CASE_SORT strcmp
364 static gint simple_sort(gconstpointer a, gconstpointer b)
366 return CASE_SORT((gchar *)a, (gchar *)b);
371 static gint tab_completion_do(TabCompData *td)
373 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
374 const gchar *entry_file;
377 gint home_exp = FALSE;
379 /* home dir expansion */
380 if (entry_text[0] == '~')
382 entry_dir = expand_tilde(entry_text);
387 entry_dir = g_strdup(entry_text);
390 if (isfile(entry_dir))
394 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
395 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
401 entry_file = filename_from_path(entry_text);
403 if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
405 ptr = entry_dir + strlen(entry_dir) - 1;
406 if (ptr[0] == G_DIR_SEPARATOR)
410 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
411 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
414 tab_completion_read_dir(td, entry_dir);
415 td->file_list = g_list_sort(td->file_list, simple_sort);
416 if (td->file_list && !td->file_list->next)
421 file = td->file_list->data;
422 buf = g_build_filename(entry_dir, file, NULL);
425 gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
429 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
430 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
434 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
438 tab_completion_popup_list(td, td->file_list);
447 gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
448 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
449 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
456 ptr = (gchar *)filename_from_path(entry_dir);
457 if (ptr > entry_dir) ptr--;
460 if (strlen(entry_dir) == 0)
463 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /* FIXME: win32 */
466 if (isdir(entry_dir))
470 gint l = strlen(entry_file);
472 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
474 tab_completion_read_dir(td, entry_dir);
477 list = td->file_list;
480 gchar *file = list->data;
481 if (strncmp(entry_file, file, l) == 0)
483 poss = g_list_prepend(poss, file);
492 gchar *file = poss->data;
495 buf = g_build_filename(entry_dir, file, NULL);
496 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
497 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
505 gsize c = strlen(entry_file);
507 gchar *test_file = poss->data;
512 if (!list) done = TRUE;
513 while (list && !done)
515 gchar *file = list->data;
516 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
529 file = g_strdup(test_file);
531 buf = g_build_filename(entry_dir, file, NULL);
532 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
533 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
535 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
537 poss = g_list_sort(poss, simple_sort);
538 tab_completion_popup_list(td, poss);
558 static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
560 TabCompData *td = data;
561 gint stop_signal = FALSE;
563 switch (event->keyval)
566 if (!(event->state & GDK_CONTROL_MASK))
568 if (tab_completion_do(td))
570 tab_completion_emit_tab_signal(td);
575 case GDK_Return: case GDK_KP_Enter:
577 (event->state & GDK_CONTROL_MASK))
579 tab_completion_select_show(td);
582 else if (tab_completion_emit_enter_signal(td))
591 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
593 return (stop_signal);
596 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
599 GtkWidget *entry = data;
601 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
605 if (!GTK_WIDGET_HAS_FOCUS(entry))
607 gtk_widget_grab_focus(entry);
610 if (tab_completion_do(td))
612 tab_completion_emit_tab_signal(td);
616 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
618 GtkWidget *parent = data;
620 if (allocation->height > parent->allocation.height)
622 GtkAllocation button_allocation;
624 button_allocation = button->allocation;
625 button_allocation.height = parent->allocation.height;
626 button_allocation.y = parent->allocation.y +
627 (parent->allocation.height - parent->allocation.height) / 2;
628 gtk_widget_size_allocate(button, &button_allocation);
632 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
638 button = gtk_button_new();
639 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
640 g_signal_connect(G_OBJECT(button), "size_allocate",
641 G_CALLBACK(tab_completion_button_size_allocate), parent);
642 g_signal_connect(G_OBJECT(button), "clicked",
643 G_CALLBACK(tab_completion_button_pressed), entry);
645 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
646 icon = gtk_image_new_from_pixbuf(pixbuf);
647 g_object_unref(pixbuf);
649 gtk_container_add(GTK_CONTAINER(button), icon);
650 gtk_widget_show(icon);
656 *----------------------------------------------------------------------------
658 *----------------------------------------------------------------------------
661 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
662 const gchar *history_key, gint max_levels,
663 void (*enter_func)(const gchar *, gpointer), gpointer data)
667 GtkWidget *combo_entry;
673 box = gtk_hbox_new(FALSE, 0);
675 combo = gtk_combo_box_entry_new_text();
676 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
677 gtk_widget_show(combo);
679 combo_entry = GTK_BIN(combo)->child;
681 gtk_combo_set_case_sensitive(GTK_COMBO(combo), TRUE);
682 gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE);
685 button = tab_completion_create_complete_button(combo_entry, combo);
686 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
687 gtk_widget_show(button);
689 tab_completion_add_to_entry(combo_entry, enter_func, data);
691 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
692 if (!td) return NULL; /* this should never happen! */
695 td->has_history = TRUE;
696 td->history_key = g_strdup(history_key);
697 td->history_levels = max_levels;
699 work = history_list_get_by_key(td->history_key);
701 work = history_list_get_by_key(history_key);
704 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (gchar *)work->data);
711 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
715 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
718 if (entry) *entry = combo_entry;
722 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
724 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
727 if (!td || !td->has_history) return NULL;
729 buf = history_list_find_last_path_by_key(td->history_key);
732 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
738 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
745 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
749 if (!td || !td->has_history) return;
751 history_list_add_to_key(td->history_key, path, td->history_levels);
753 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
755 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
756 gtk_list_store_clear(GTK_LIST_STORE(store));
758 work = history_list_get_by_key(td->history_key);
761 gtk_combo_box_append_text(GTK_COMBO_BOX(td->combo), (gchar *)work->data);
766 if (td->tab_append_func) {
767 td->tab_append_func(path, td->tab_append_data, n);
771 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
772 void (*enter_func)(const gchar *, gpointer), gpointer data)
778 hbox = gtk_hbox_new(FALSE, 0);
780 newentry = gtk_entry_new();
781 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
782 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
783 gtk_widget_show(newentry);
785 button = tab_completion_create_complete_button(newentry, newentry);
786 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
787 gtk_widget_show(button);
789 tab_completion_add_to_entry(newentry, enter_func, data);
791 if (entry) *entry = newentry;
795 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), gpointer data)
800 log_printf("Tab completion error: entry != NULL\n");
804 td = g_new0(TabCompData, 1);
807 td->enter_func = enter_func;
808 td->enter_data = data;
810 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
812 g_signal_connect(G_OBJECT(entry), "key_press_event",
813 G_CALLBACK(tab_completion_key_pressed), td);
814 g_signal_connect(G_OBJECT(entry), "destroy",
815 G_CALLBACK(tab_completion_destroy), td);
818 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
820 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
824 td->tab_func = tab_func;
828 /* Add a callback function called when a new entry is appended to the list */
829 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
831 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
835 td->tab_append_func = tab_append_func;
836 td->tab_append_data = data;
839 gchar *remove_trailing_slash(const gchar *path)
843 if (!path) return NULL;
846 while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
848 return g_strndup(path, l);
851 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
853 TabCompData *td = data;
856 file_dialog_close(fd);
859 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
861 TabCompData *td = data;
863 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
865 tab_completion_select_cancel_cb(fd, data);
867 tab_completion_emit_enter_signal(td);
870 static void tab_completion_select_show(TabCompData *td)
877 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
881 title = (td->fd_title) ? td->fd_title : _("Select path");
882 td->fd = file_dialog_new(title, "select_path", td->entry,
883 tab_completion_select_cancel_cb, td);
884 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
885 tab_completion_select_ok_cb, TRUE);
887 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL);
889 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
890 if (strlen(path) == 0) path = NULL;
891 if (td->fd_folders_only)
893 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
897 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, "*", _("All files"));
900 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
903 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data)
905 TabCompData *td = data;
907 tab_completion_select_show(td);
910 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gint folders_only)
916 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
920 g_free(td->fd_title);
921 td->fd_title = g_strdup(title);
922 td->fd_folders_only = folders_only;
924 if (td->fd_button) return;
926 parent = (td->combo) ? td->combo : td->entry;
928 hbox = gtk_widget_get_parent(parent);
929 if (!GTK_IS_BOX(hbox)) return;
931 td->fd_button = gtk_button_new_with_label("...");
932 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
933 G_CALLBACK(tab_completion_button_size_allocate), parent);
934 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
935 G_CALLBACK(tab_completion_select_pressed), td);
937 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
939 gtk_widget_show(td->fd_button);
941 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */