2 * (SLIK) SimpLIstic sKin functions
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
21 #include <sys/types.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include "ui_tabcomp.h"
31 #include "ui_bookmark.h"
32 #include "ui_fileops.h"
33 #include "ui_spinner.h"
34 #include "ui_utildlg.h"
36 #include <gdk/gdkkeysyms.h> /* for key values */
39 /* define this to enable a pop-up menu that shows possible matches
40 * #define TAB_COMPLETION_ENABLE_POPUP_MENU
42 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1
43 #define TAB_COMP_POPUP_MAX 500
45 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
50 /* ----------------------------------------------------------------
51 Tab completion routines, can be connected to any gtkentry widget
52 using the tab_completion_add_to_entry() function.
53 Use remove_trailing_slash() to strip the trailing '/'.
54 ----------------------------------------------------------------*/
56 typedef struct _TabCompData TabCompData;
62 void (*enter_func)(const gchar *, gpointer);
63 void (*tab_func)(const gchar *, gpointer);
79 static void tab_completion_select_show(TabCompData *td);
81 static void tab_completion_free_list(TabCompData *td)
96 g_list_free(td->file_list);
100 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
107 tab_completion_free_list(td);
109 pathl = path_from_utf8(path);
117 while ((dir = readdir(dp)) != NULL)
119 gchar *name = dir->d_name;
120 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0)
122 list = g_list_prepend(list, path_to_utf8(name));
127 td->dir_path = g_strdup(path);
128 td->file_list = list;
131 static void tab_completion_destroy(GtkWidget *widget, gpointer data)
133 TabCompData *td = data;
135 tab_completion_free_list(td);
136 g_free(td->history_key);
138 if (td->fd) file_dialog_close(td->fd);
139 g_free(td->fd_title);
144 static gint tab_completion_emit_enter_signal(TabCompData *td)
147 if (!td->enter_func) return FALSE;
149 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
154 text = g_strconcat(homedir(), t + 1, NULL);
158 td->enter_func(text, td->enter_data);
164 static void tab_completion_emit_tab_signal(TabCompData *td)
167 if (!td->tab_func) return;
169 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
174 text = g_strconcat(homedir(), t + 1, NULL);
178 td->tab_func(text, td->tab_data);
182 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
184 static gint tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
186 TabCompData *td = data;
188 if (event->keyval == GDK_Tab ||
189 event->keyval == GDK_BackSpace ||
190 (event->keyval >= 0x20 && event->keyval <= 0xFF) )
192 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
197 buf[0] = event->keyval;
199 gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
200 gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
204 gtk_menu_popdown(GTK_MENU(widget));
205 /* doing this does not emit the "selection done" signal, unref it ourselves */
206 gtk_widget_unref(widget);
214 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
221 td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
224 ptr = td->dir_path + strlen(td->dir_path) - 1;
225 buf = g_strconcat(td->dir_path, (ptr[0] == '/') ? "" : "/", name, NULL);
226 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
227 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
230 tab_completion_emit_tab_signal(td);
233 static void tab_completion_popup_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
235 TabCompData *td = data;
238 PangoRectangle strong_pos, weak_pos;
240 gint xoffset, yoffset;
244 GdkRectangle monitor;
246 gdk_window_get_origin(td->entry->window, x, y);
248 screen = gtk_widget_get_screen(GTK_WIDGET(menu));
249 monitor_num = gdk_screen_get_monitor_at_window(screen, td->entry->window);
250 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
252 gtk_widget_size_request(GTK_WIDGET(menu), &req);
254 length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
255 gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
257 layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
258 pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
260 *x += strong_pos.x / PANGO_SCALE + xoffset;
262 height = MIN(td->entry->requisition.height, td->entry->allocation.height);
264 if (req.height > monitor.y + monitor.height - *y - height &&
265 *y - monitor.y > monitor.y + monitor.height - *y)
267 height = MIN(*y - monitor.y, req.height);
268 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
277 static void tab_completion_popup_list(TabCompData *td, GList *list)
290 * well, the menu would be too long anyway...
291 * (listing /dev causes gtk+ window allocation errors, -> too big a window)
292 * this is why menu popups are disabled, this really should be a popup scrollable listview.
294 if (g_list_length(list) > 200) return;
297 menu = popup_menu_short_lived();
300 while (work && count < TAB_COMP_POPUP_MAX)
302 gchar *name = work->data;
305 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
306 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
312 g_signal_connect(G_OBJECT(menu), "key_press_event",
313 G_CALLBACK(tab_completion_popup_key_press), td);
315 /* peek at the current event to get the time, etc. */
316 event = gtk_get_current_event();
318 if (event && event->type == GDK_BUTTON_RELEASE)
320 ebutton = event->button.button;
329 etime = gdk_event_get_time(event);
330 gdk_event_free(event);
337 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
338 tab_completion_popup_pos_cb, td, ebutton, etime);
342 #define CASE_SORT strcmp
345 static gint simple_sort(gconstpointer a, gconstpointer b)
347 return CASE_SORT((gchar *)a, (gchar *)b);
352 static gint tab_completion_do(TabCompData *td)
354 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
355 const gchar *entry_file;
358 gint home_exp = FALSE;
360 /* home dir expansion */
361 if (entry_text[0] == '~')
363 entry_dir = g_strconcat(homedir(), entry_text + 1, NULL);
368 entry_dir = g_strdup(entry_text);
371 entry_file = filename_from_path(entry_text);
373 if (isfile(entry_dir))
377 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
378 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;
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_strconcat(entry_dir, file, NULL);
406 buf = g_strconcat(entry_dir, file, "/", NULL);
408 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
409 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
413 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
417 tab_completion_popup_list(td, td->file_list);
426 gchar *buf = g_strconcat(entry_dir, "/", NULL);
427 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
428 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
435 ptr = (gchar *)filename_from_path(entry_dir);
436 if (ptr > entry_dir) ptr--;
439 if (strlen(entry_dir) == 0)
442 entry_dir = g_strdup("/");
445 if (isdir(entry_dir))
449 gint l = strlen(entry_file);
451 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
453 tab_completion_read_dir(td, entry_dir);
456 if (strcmp(entry_dir, "/") == 0) entry_dir[0] = '\0';
458 list = td->file_list;
461 gchar *file = list->data;
462 if (strncmp(entry_file, file, l) == 0)
464 poss = g_list_prepend(poss, file);
473 gchar *file = poss->data;
476 buf = g_strconcat(entry_dir, "/", file, NULL);
481 buf = g_strconcat(entry_dir, "/", file, "/", NULL);
483 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
484 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
492 gint c = strlen(entry_file);
494 gchar *test_file = poss->data;
499 if (!list) done = TRUE;
502 gchar *file = list->data;
503 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
516 file = g_strdup(test_file);
518 buf = g_strconcat(entry_dir, "/", file, NULL);
519 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
520 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
522 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
524 poss = g_list_sort(poss, simple_sort);
525 tab_completion_popup_list(td, poss);
545 static gint tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
547 TabCompData *td = data;
548 gint stop_signal = FALSE;
550 switch (event->keyval)
553 if (!(event->state & GDK_CONTROL_MASK))
555 if (tab_completion_do(td))
557 tab_completion_emit_tab_signal(td);
562 case GDK_Return: case GDK_KP_Enter:
564 (event->state & GDK_CONTROL_MASK))
566 tab_completion_select_show(td);
569 else if (tab_completion_emit_enter_signal(td))
578 if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
580 return (stop_signal);
583 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
586 GtkWidget *entry = data;
588 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
592 if (!GTK_WIDGET_HAS_FOCUS(entry))
594 gtk_widget_grab_focus(entry);
597 if (tab_completion_do(td))
599 tab_completion_emit_tab_signal(td);
603 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
605 GtkWidget *parent = data;
607 if (allocation->height > parent->allocation.height)
609 GtkAllocation button_allocation;
611 button_allocation = button->allocation;
612 button_allocation.height = parent->allocation.height;
613 button_allocation.y = parent->allocation.y +
614 (parent->allocation.height - parent->allocation.height) / 2;
615 gtk_widget_size_allocate(button, &button_allocation);
619 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
625 button = gtk_button_new();
626 GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
627 g_signal_connect(G_OBJECT(button), "size_allocate",
628 G_CALLBACK(tab_completion_button_size_allocate), parent);
629 g_signal_connect(G_OBJECT(button), "clicked",
630 G_CALLBACK(tab_completion_button_pressed), entry);
632 pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
633 icon = gtk_image_new_from_pixbuf(pixbuf);
634 gdk_pixbuf_unref(pixbuf);
635 gtk_container_add(GTK_CONTAINER(button), icon);
636 gtk_widget_show(icon);
642 *----------------------------------------------------------------------------
644 *----------------------------------------------------------------------------
647 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
648 const gchar *history_key, gint max_levels,
649 void (*enter_func)(const gchar *, gpointer), gpointer data)
653 GtkWidget *combo_entry;
659 box = gtk_hbox_new(FALSE, 0);
661 combo = gtk_combo_box_entry_new_text();
662 gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
663 gtk_widget_show(combo);
665 combo_entry = GTK_BIN(combo)->child;
667 gtk_combo_set_case_sensitive(GTK_COMBO(combo), TRUE);
668 gtk_combo_set_use_arrows(GTK_COMBO(combo), FALSE);
671 button = tab_completion_create_complete_button(combo_entry, combo);
672 gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
673 gtk_widget_show(button);
675 tab_completion_add_to_entry(combo_entry, enter_func, data);
677 td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
678 if (!td) return NULL; /* this should never happen! */
681 td->has_history = TRUE;
682 td->history_key = g_strdup(history_key);
683 td->history_levels = max_levels;
685 work = history_list_get_by_key(td->history_key);
687 work = history_list_get_by_key(history_key);
690 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), (gchar *)work->data);
697 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
701 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
704 if (entry) *entry = combo_entry;
708 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
710 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
713 if (!td || !td->has_history) return NULL;
715 buf = history_list_find_last_path_by_key(td->history_key);
718 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
724 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
730 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
734 if (!td || !td->has_history) return;
736 history_list_add_to_key(td->history_key, path, td->history_levels);
738 gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
740 store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
741 gtk_list_store_clear(GTK_LIST_STORE(store));
743 work = history_list_get_by_key(td->history_key);
746 gtk_combo_box_append_text(GTK_COMBO_BOX(td->combo), (gchar *)work->data);
751 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
752 void (*enter_func)(const gchar *, gpointer), gpointer data)
758 hbox = gtk_hbox_new(FALSE, 0);
760 newentry = gtk_entry_new();
761 if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
762 gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
763 gtk_widget_show(newentry);
765 button = tab_completion_create_complete_button(newentry, newentry);
766 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
767 gtk_widget_show(button);
769 tab_completion_add_to_entry(newentry, enter_func, data);
771 if (entry) *entry = newentry;
775 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), gpointer data)
780 printf("Tab completion error: entry != NULL\n");
784 td = g_new0(TabCompData, 1);
787 td->file_list = NULL;
788 td->enter_func = enter_func;
789 td->enter_data = data;
793 td->has_history = FALSE;
794 td->history_key = NULL;
795 td->history_levels = 0;
797 g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
799 g_signal_connect(G_OBJECT(entry), "key_press_event",
800 G_CALLBACK(tab_completion_key_pressed), td);
801 g_signal_connect(G_OBJECT(entry), "destroy",
802 G_CALLBACK(tab_completion_destroy), td);
805 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
807 TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
811 td->tab_func = tab_func;
815 gchar *remove_trailing_slash(const gchar *path)
819 if (!path) return NULL;
822 while (l > 1 && path[l - 1] == '/') l--;
824 return g_strndup(path, l);
827 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
829 TabCompData *td = data;
832 file_dialog_close(fd);
835 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
837 TabCompData *td = data;
839 gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
841 tab_completion_select_cancel_cb(fd, data);
843 tab_completion_emit_enter_signal(td);
846 static void tab_completion_select_show(TabCompData *td)
853 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
857 title = (td->fd_title) ? td->fd_title : _("Select path");
858 td->fd = file_dialog_new(title, GQ_WMCLASS, "select_path", td->entry,
859 tab_completion_select_cancel_cb, td);
860 file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
861 tab_completion_select_ok_cb, TRUE);
863 generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL);
865 path = gtk_entry_get_text(GTK_ENTRY(td->entry));
866 if (strlen(path) == 0) path = NULL;
867 if (td->fd_folders_only)
869 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
873 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, "*", _("All files"));
876 gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
879 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data)
881 TabCompData *td = data;
883 tab_completion_select_show(td);
886 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gint folders_only)
892 td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
896 g_free(td->fd_title);
897 td->fd_title = g_strdup(title);
898 td->fd_folders_only = folders_only;
900 if (td->fd_button) return;
902 parent = (td->combo) ? td->combo : td->entry;
904 hbox = gtk_widget_get_parent(parent);
905 if (!GTK_IS_BOX(hbox)) return;
907 td->fd_button = gtk_button_new_with_label("...");
908 g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
909 G_CALLBACK(tab_completion_button_size_allocate), parent);
910 g_signal_connect(G_OBJECT(td->fd_button), "clicked",
911 G_CALLBACK(tab_completion_select_pressed), td);
913 gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
915 gtk_widget_show(td->fd_button);