4efaa4e9da1f05dfb0a74e37b52a1caf5c5f9e44
[geeqie.git] / src / ui_tabcomp.c
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <config.h>
23 #include "intl.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <dirent.h>
31
32 #include <gdk/gdk.h>
33 #include <gtk/gtk.h>
34 #include <gdk-pixbuf/gdk-pixbuf.h>
35
36 #include "main.h"
37 #include "ui_tabcomp.h"
38
39 #include "history_list.h"
40 #include "misc.h"       /* expand_tilde() */
41 #include "ui_fileops.h"
42 #include "ui_spinner.h"
43 #include "ui_utildlg.h"
44
45 #include <gdk/gdkkeysyms.h> /* for key values */
46
47
48 /* define this to enable a pop-up menu that shows possible matches
49  * #define TAB_COMPLETION_ENABLE_POPUP_MENU
50  */
51 #define TAB_COMPLETION_ENABLE_POPUP_MENU 1
52 #define TAB_COMP_POPUP_MAX 1000
53
54 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
55 #include "ui_menu.h"
56 #endif
57
58
59 /**
60  * @file
61  * ----------------------------------------------------------------
62  * Tab completion routines, can be connected to any gtkentry widget
63  * using the tab_completion_add_to_entry() function.
64  * 
65  * Use remove_trailing_slash() to strip the trailing G_DIR_SEPARATOR.
66  * 
67  * ----------------------------------------------------------------
68  */
69
70 typedef struct _TabCompData TabCompData;
71 struct _TabCompData
72 {
73         GtkWidget *entry;
74         gchar *dir_path;
75         GList *file_list;
76         void (*enter_func)(const gchar *, gpointer);
77         void (*tab_func)(const gchar *, gpointer);
78         void (*tab_append_func)(const gchar *, gpointer, gint);
79
80         gpointer enter_data;
81         gpointer tab_data;
82         gpointer tab_append_data;
83
84         GtkWidget *combo;
85         gboolean has_history;
86         gchar *history_key;
87         gint history_levels;
88
89         FileDialog *fd;
90         gchar *fd_title;
91         gboolean fd_folders_only;
92         GtkWidget *fd_button;
93         gchar *filter;
94         gchar *filter_desc;
95
96         guint choices;
97 };
98
99
100 static void tab_completion_select_show(TabCompData *td);
101 static gint tab_completion_do(TabCompData *td);
102
103 static void tab_completion_free_list(TabCompData *td)
104 {
105         GList *list;
106
107         g_free(td->dir_path);
108         td->dir_path = NULL;
109
110         list = td->file_list;
111
112         while (list)
113                 {
114                 g_free(list->data);
115                 list = list->next;
116                 }
117
118         g_list_free(td->file_list);
119         td->file_list = NULL;
120 }
121
122 static void tab_completion_read_dir(TabCompData *td, const gchar *path)
123 {
124         DIR *dp;
125         struct dirent *dir;
126         GList *list = NULL;
127         gchar *pathl;
128
129         tab_completion_free_list(td);
130
131         pathl = path_from_utf8(path);
132         dp = opendir(pathl);
133         if (!dp)
134                 {
135                 /* dir not found */
136                 g_free(pathl);
137                 return;
138                 }
139         while ((dir = readdir(dp)) != NULL)
140                 {
141                 gchar *name = dir->d_name;
142                 if (strcmp(name, ".") != 0 && strcmp(name, "..") != 0 &&
143                                                 (name[0] != '.' || options->file_filter.show_hidden_files))
144                         {
145                         gchar *abspath = g_build_filename(pathl, name, NULL);
146
147                         if (g_file_test(abspath, G_FILE_TEST_IS_DIR))
148                                 {
149                                 gchar *dname = g_strconcat(name, G_DIR_SEPARATOR_S, NULL);
150                                 list = g_list_prepend(list, path_to_utf8(dname));
151                                 g_free(dname);
152                                 }
153                         else
154                                 {
155                                 list = g_list_prepend(list, path_to_utf8(name));
156                                 }
157                         g_free(abspath);
158                         }
159                 }
160         closedir(dp);
161
162         td->dir_path = g_strdup(path);
163         td->file_list = list;
164         g_free(pathl);
165 }
166
167 static void tab_completion_destroy(GtkWidget *UNUSED(widget), gpointer data)
168 {
169         TabCompData *td = data;
170
171         tab_completion_free_list(td);
172         g_free(td->history_key);
173
174         if (td->fd) file_dialog_close(td->fd);
175         g_free(td->fd_title);
176
177         g_free(td->filter);
178         g_free(td->filter_desc);
179
180         g_free(td);
181 }
182
183 static gchar *tab_completion_get_text(TabCompData *td)
184 {
185         gchar *text;
186
187         text = g_strdup(gtk_entry_get_text(GTK_ENTRY(td->entry)));
188
189         if (text[0] == '~')
190                 {
191                 gchar *t = text;
192                 text = expand_tilde(text);
193                 g_free(t);
194                 }
195
196         return text;
197 }
198
199 static gboolean tab_completion_emit_enter_signal(TabCompData *td)
200 {
201         gchar *text;
202         if (!td->enter_func) return FALSE;
203
204         text = tab_completion_get_text(td);
205         td->enter_func(text, td->enter_data);
206         g_free(text);
207
208         return TRUE;
209 }
210
211 static void tab_completion_emit_tab_signal(TabCompData *td)
212 {
213         gchar *text;
214         if (!td->tab_func) return;
215
216         text = tab_completion_get_text(td);
217         td->tab_func(text, td->tab_data);
218         g_free(text);
219 }
220
221 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
222 void tab_completion_iter_menu_items(GtkWidget *widget, gpointer data)
223 {
224         TabCompData *td = data;
225         GtkWidget *child;
226
227         if (!gtk_widget_get_visible(widget)) return;
228
229         child = gtk_bin_get_child(GTK_BIN(widget));
230         if (GTK_IS_LABEL(child)) {
231                 const gchar *text = gtk_label_get_text(GTK_LABEL(child));
232                 const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
233                 const gchar *prefix = filename_from_path(entry_text);
234                 guint prefix_len = strlen(prefix);
235
236                 if (strlen(text) < prefix_len || strncmp(text, prefix, prefix_len))
237                         {
238                         /* Hide menu items not matching */
239                         gtk_widget_hide(widget);
240                         }
241                 else
242                         {
243                         /* Count how many choices are left in the menu */
244                         td->choices++;
245                         }
246         }
247 }
248
249 static gboolean tab_completion_popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
250 {
251         TabCompData *td = data;
252
253         if (event->keyval == GDK_KEY_Tab ||
254             event->keyval == GDK_KEY_BackSpace ||
255             (event->keyval >= 0x20 && event->keyval <= 0xFF) )
256                 {
257                 if (event->keyval >= 0x20 && event->keyval <= 0xFF)
258                         {
259                         gchar buf[2];
260                         gint p = -1;
261
262                         buf[0] = event->keyval;
263                         buf[1] = '\0';
264                         gtk_editable_insert_text(GTK_EDITABLE(td->entry), buf, 1, &p);
265                         gtk_editable_set_position(GTK_EDITABLE(td->entry), -1);
266
267                         /* Reduce the number of entries in the menu */
268                         td->choices = 0;
269                         gtk_container_foreach(GTK_CONTAINER(widget), tab_completion_iter_menu_items, (gpointer) td);
270                         if (td->choices > 1) return TRUE; /* multiple choices */
271                         if (td->choices > 0) tab_completion_do(td); /* one choice */
272                         }
273
274                 /* close the menu */
275                 gtk_menu_popdown(GTK_MENU(widget));
276                 /* doing this does not emit the "selection done" signal, unref it ourselves */
277                 g_object_unref(widget);
278                 return TRUE;
279                 }
280
281         return FALSE;
282 }
283
284 static void tab_completion_popup_cb(GtkWidget *widget, gpointer data)
285 {
286         gchar *name = data;
287         TabCompData *td;
288         gchar *buf;
289
290         td = g_object_get_data(G_OBJECT(widget), "tab_completion_data");
291         if (!td) return;
292
293         buf = g_build_filename(td->dir_path, name, NULL);
294         gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
295         gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
296         g_free(buf);
297
298         tab_completion_emit_tab_signal(td);
299 }
300
301 static void tab_completion_popup_list(TabCompData *td, GList *list)
302 {
303         GtkWidget *menu;
304         GList *work;
305         gint count = 0;
306
307         if (!list) return;
308
309 #if 0
310         /*
311          * well, the menu would be too long anyway...
312          * (listing /dev causes gtk+ window allocation errors, -> too big a window)
313          * this is why menu popups are disabled, this really should be a popup scrollable listview.
314          */
315         if (g_list_length(list) > 200) return;
316 #endif
317
318         menu = popup_menu_short_lived();
319
320         work = list;
321         while (work && count < TAB_COMP_POPUP_MAX)
322                 {
323                 gchar *name = work->data;
324                 GtkWidget *item;
325
326                 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
327                 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
328
329                 work = work->next;
330                 count++;
331                 }
332
333         g_signal_connect(G_OBJECT(menu), "key_press_event",
334                          G_CALLBACK(tab_completion_popup_key_press), td);
335
336         gtk_menu_popup_at_widget(GTK_MENU(menu), td->entry, GDK_GRAVITY_NORTH_EAST, GDK_GRAVITY_NORTH, NULL);
337 }
338
339 #ifndef CASE_SORT
340 #define CASE_SORT strcmp
341 #endif
342
343 static gint simple_sort(gconstpointer a, gconstpointer b)
344 {
345         return CASE_SORT((gchar *)a, (gchar *)b);
346 }
347
348 #endif
349
350 static gboolean tab_completion_do(TabCompData *td)
351 {
352         const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
353         const gchar *entry_file;
354         gchar *entry_dir;
355         gchar *ptr;
356         gboolean home_exp = FALSE;
357
358         if (entry_text[0] == '\0')
359                 {
360                 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME root directory win32 */
361                 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
362                 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
363                 g_free(entry_dir);
364                 return FALSE;
365                 }
366
367         /* home dir expansion */
368         if (entry_text[0] == '~')
369                 {
370                 entry_dir = expand_tilde(entry_text);
371                 home_exp = TRUE;
372                 }
373         else
374                 {
375                 entry_dir = g_strdup(entry_text);
376                 }
377
378         if (isfile(entry_dir))
379                 {
380                 if (home_exp)
381                         {
382                         gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
383                         gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
384                         }
385                 g_free(entry_dir);
386                 return home_exp;
387                 }
388
389         entry_file = filename_from_path(entry_text);
390
391         if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
392                 {
393                 ptr = entry_dir + strlen(entry_dir) - 1;
394                 if (ptr[0] == G_DIR_SEPARATOR)
395                         {
396                         if (home_exp)
397                                 {
398                                 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
399                                 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
400                                 }
401
402                         tab_completion_read_dir(td, entry_dir);
403                         td->file_list = g_list_sort(td->file_list, simple_sort);
404                         if (td->file_list && !td->file_list->next)
405                                 {
406                                 gchar *buf;
407                                 const gchar *file;
408
409                                 file = td->file_list->data;
410                                 buf = g_build_filename(entry_dir, file, NULL);
411                                 if (isdir(buf))
412                                         {
413                                         gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
414                                         g_free(buf);
415                                         buf = tmp;
416                                         }
417                                 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
418                                 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
419                                 g_free(buf);
420                                 }
421
422 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
423
424                         else
425                                 {
426                                 tab_completion_popup_list(td, td->file_list);
427                                 }
428 #endif
429
430                         g_free(entry_dir);
431                         return home_exp;
432                         }
433                 else
434                         {
435                         gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
436                         gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
437                         gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
438                         g_free(buf);
439                         g_free(entry_dir);
440                         return TRUE;
441                         }
442                 }
443
444         ptr = (gchar *)filename_from_path(entry_dir);
445         if (ptr > entry_dir) ptr--;
446         ptr[0] = '\0';
447
448         if (strlen(entry_dir) == 0)
449                 {
450                 g_free(entry_dir);
451                 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME win32 */
452                 }
453
454         if (isdir(entry_dir))
455                 {
456                 GList *list;
457                 GList *poss = NULL;
458                 gint l = strlen(entry_file);
459
460                 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
461                         {
462                         tab_completion_read_dir(td, entry_dir);
463                         }
464
465                 list = td->file_list;
466                 while (list)
467                         {
468                         gchar *file = list->data;
469                         if (strncmp(entry_file, file, l) == 0)
470                                 {
471                                 poss = g_list_prepend(poss, file);
472                                 }
473                         list = list->next;
474                         }
475
476                 if (poss)
477                         {
478                         if (!poss->next)
479                                 {
480                                 gchar *file = poss->data;
481                                 gchar *buf;
482
483                                 buf = g_build_filename(entry_dir, file, NULL);
484                                 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
485                                 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
486                                 g_free(buf);
487                                 g_list_free(poss);
488                                 g_free(entry_dir);
489                                 return TRUE;
490                                 }
491                         else
492                                 {
493                                 gsize c = strlen(entry_file);
494                                 gboolean done = FALSE;
495                                 gchar *test_file = poss->data;
496
497                                 while (!done)
498                                         {
499                                         list = poss;
500                                         if (!list) done = TRUE;
501                                         while (list && !done)
502                                                 {
503                                                 gchar *file = list->data;
504                                                 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
505                                                         {
506                                                         done = TRUE;
507                                                         }
508                                                 list = list->next;
509                                                 }
510                                         c++;
511                                         }
512                                 c -= 2;
513                                 if (c > 0)
514                                         {
515                                         gchar *file;
516                                         gchar *buf;
517                                         file = g_strdup(test_file);
518                                         file[c] = '\0';
519                                         buf = g_build_filename(entry_dir, file, NULL);
520                                         gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
521                                         gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
522
523 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
524
525                                         poss = g_list_sort(poss, simple_sort);
526                                         tab_completion_popup_list(td, poss);
527
528 #endif
529
530                                         g_free(file);
531                                         g_free(buf);
532                                         g_list_free(poss);
533                                         g_free(entry_dir);
534                                         return TRUE;
535                                         }
536                                 }
537                         g_list_free(poss);
538                         }
539                 }
540
541         g_free(entry_dir);
542
543         return FALSE;
544 }
545
546 static gboolean tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
547 {
548         TabCompData *td = data;
549         gboolean stop_signal = FALSE;
550
551         switch (event->keyval)
552                 {
553                 case GDK_KEY_Tab:
554                         if (!(event->state & GDK_CONTROL_MASK))
555                                 {
556                                 if (tab_completion_do(td))
557                                         {
558                                         tab_completion_emit_tab_signal(td);
559                                         }
560                                 stop_signal = TRUE;
561                                 }
562                         break;
563                 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
564                         if (td->fd_button &&
565                             (event->state & GDK_CONTROL_MASK))
566                                 {
567                                 tab_completion_select_show(td);
568                                 stop_signal = TRUE;
569                                 }
570                         else if (tab_completion_emit_enter_signal(td))
571                                 {
572                                 stop_signal = TRUE;
573                                 }
574                         break;
575                 default:
576                         break;
577                 }
578
579         if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
580
581         return (stop_signal);
582 }
583
584 static void tab_completion_button_pressed(GtkWidget *UNUSED(widget), gpointer data)
585 {
586         TabCompData *td;
587         GtkWidget *entry = data;
588
589         td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
590
591         if (!td) return;
592
593         if (!gtk_widget_has_focus(entry))
594                 {
595                 gtk_widget_grab_focus(entry);
596                 }
597
598         if (tab_completion_do(td))
599                 {
600                 tab_completion_emit_tab_signal(td);
601                 }
602 }
603
604 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
605 {
606         GtkWidget *parent = data;
607         GtkAllocation parent_allocation;
608         gtk_widget_get_allocation(parent, &parent_allocation);
609
610         if (allocation->height > parent_allocation.height)
611                 {
612                 GtkAllocation button_allocation;
613
614                 gtk_widget_get_allocation(button, &button_allocation);
615                 button_allocation.height = parent_allocation.height;
616                 button_allocation.y = parent_allocation.y +
617                         (parent_allocation.height - parent_allocation.height) / 2;
618                 gtk_widget_size_allocate(button, &button_allocation);
619                 }
620 }
621
622 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
623 {
624         GtkWidget *button;
625         GtkWidget *icon;
626         GdkPixbuf *pixbuf;
627
628         button = gtk_button_new();
629         gtk_widget_set_can_focus(button, FALSE);
630         g_signal_connect(G_OBJECT(button), "size_allocate",
631                          G_CALLBACK(tab_completion_button_size_allocate), parent);
632         g_signal_connect(G_OBJECT(button), "clicked",
633                          G_CALLBACK(tab_completion_button_pressed), entry);
634
635         pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
636         icon = gtk_image_new_from_pixbuf(pixbuf);
637         g_object_unref(pixbuf);
638
639         gtk_container_add(GTK_CONTAINER(button), icon);
640         gtk_widget_show(icon);
641
642         return button;
643 }
644
645 /*
646  *----------------------------------------------------------------------------
647  * public interface
648  *----------------------------------------------------------------------------
649  */
650
651 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
652                                            const gchar *history_key, gint max_levels,
653                                            void (*enter_func)(const gchar *, gpointer), gpointer data)
654 {
655         GtkWidget *box;
656         GtkWidget *combo;
657         GtkWidget *combo_entry;
658         GtkWidget *button;
659         GList *work;
660         TabCompData *td;
661         gint n = 0;
662
663         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
664
665         combo = gtk_combo_box_text_new_with_entry();
666         gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
667         gtk_widget_show(combo);
668
669         combo_entry = gtk_bin_get_child(GTK_BIN(combo));
670
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);
674
675         tab_completion_add_to_entry(combo_entry, enter_func, NULL, NULL, data);
676
677         td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
678         if (!td) return NULL; /* this should never happen! */
679
680         td->combo = combo;
681         td->has_history = TRUE;
682         td->history_key = g_strdup(history_key);
683         td->history_levels = max_levels;
684
685         work = history_list_get_by_key(td->history_key);
686
687         work = history_list_get_by_key(history_key);
688         while (work)
689                 {
690                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), (gchar *)work->data);
691                 work = work->next;
692                 n++;
693                 }
694
695         if (text)
696                 {
697                 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
698                 }
699         else if (n > 0)
700                 {
701                 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
702                 }
703
704         if (entry) *entry = combo_entry;
705         return box;
706 }
707
708 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
709 {
710         TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
711         const gchar *buf;
712
713         if (!td || !td->has_history) return NULL;
714
715         buf = history_list_find_last_path_by_key(td->history_key);
716         if (buf)
717                 {
718                 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
719                 }
720
721         return buf;
722 }
723
724 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
725 {
726         TabCompData *td;
727         GtkTreeModel *store;
728         GList *work;
729         gint n = 0;
730
731         td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
732
733         if (!path) return;
734
735         if (!td || !td->has_history) return;
736
737         history_list_add_to_key(td->history_key, path, td->history_levels);
738
739         gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
740
741         store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
742         gtk_list_store_clear(GTK_LIST_STORE(store));
743
744         work = history_list_get_by_key(td->history_key);
745         while (work)
746                 {
747                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(td->combo), (gchar *)work->data);
748                 work = work->next;
749                 n++;
750                 }
751
752         if (td->tab_append_func) {
753                 td->tab_append_func(path, td->tab_append_data, n);
754         }
755 }
756
757 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
758                               void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
759 {
760         GtkWidget *hbox;
761         GtkWidget *button;
762         GtkWidget *newentry;
763
764         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
765
766         newentry = gtk_entry_new();
767         if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
768         gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
769         gtk_widget_show(newentry);
770
771         button = tab_completion_create_complete_button(newentry, newentry);
772         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
773         gtk_widget_show(button);
774
775         tab_completion_add_to_entry(newentry, enter_func, filter, filter_desc, data);
776         if (entry) *entry = newentry;
777         return hbox;
778 }
779
780 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
781 {
782         TabCompData *td;
783         if (!entry)
784                 {
785                 log_printf("Tab completion error: entry != NULL\n");
786                 return;
787                 }
788
789         td = g_new0(TabCompData, 1);
790
791         td->entry = entry;
792         td->enter_func = enter_func;
793         td->enter_data = data;
794         td->filter = g_strdup(filter);
795         td->filter_desc = g_strdup(filter_desc);
796
797         g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
798
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);
803 }
804
805 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
806 {
807         TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
808
809         if (!td) return;
810
811         td->tab_func = tab_func;
812         td->tab_data = data;
813 }
814
815 /* Add a callback function called when a new entry is appended to the list */
816 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
817 {
818         TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
819
820         if (!td) return;
821
822         td->tab_append_func = tab_append_func;
823         td->tab_append_data = data;
824 }
825
826 gchar *remove_trailing_slash(const gchar *path)
827 {
828         gint l;
829
830         if (!path) return NULL;
831
832         l = strlen(path);
833         while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
834
835         return g_strndup(path, l);
836 }
837
838 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
839 {
840         TabCompData *td = data;
841
842         td->fd = NULL;
843         file_dialog_close(fd);
844 }
845
846 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
847 {
848         TabCompData *td = data;
849
850         gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
851
852         tab_completion_select_cancel_cb(fd, data);
853
854         tab_completion_emit_enter_signal(td);
855 }
856
857 static void tab_completion_select_show(TabCompData *td)
858 {
859         const gchar *title;
860         const gchar *path;
861         gchar *filter = NULL;
862         gchar *filter_desc = NULL;
863
864         if (td->fd)
865                 {
866                 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
867                 return;
868                 }
869
870         title = (td->fd_title) ? td->fd_title : _("Select path");
871         td->fd = file_dialog_new(title, "select_path", td->entry,
872                                  tab_completion_select_cancel_cb, td);
873         file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
874                                  tab_completion_select_ok_cb, TRUE);
875
876         generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL, FALSE);
877
878         if (td->filter)
879                 {
880                 filter = td->filter;
881                 }
882         else
883                 {
884                 filter = "*";
885                 }
886         if (td->filter_desc)
887                 {
888                 filter_desc = td->filter_desc;
889                 }
890         else
891                 {
892                 filter_desc = _("All files");
893                 }
894
895         path = gtk_entry_get_text(GTK_ENTRY(td->entry));
896         if (strlen(path) == 0) path = NULL;
897         if (td->fd_folders_only)
898                 {
899                 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
900                 }
901         else
902                 {
903                 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, filter, filter_desc);
904                 }
905
906         gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
907 }
908
909 static void tab_completion_select_pressed(GtkWidget *UNUSED(widget), gpointer data)
910 {
911         TabCompData *td = data;
912
913         tab_completion_select_show(td);
914 }
915
916 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gboolean folders_only)
917 {
918         TabCompData *td;
919         GtkWidget *parent;
920         GtkWidget *hbox;
921
922         td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
923
924         if (!td) return;
925
926         g_free(td->fd_title);
927         td->fd_title = g_strdup(title);
928         td->fd_folders_only = folders_only;
929
930         if (td->fd_button) return;
931
932         parent = (td->combo) ? td->combo : td->entry;
933
934         hbox = gtk_widget_get_parent(parent);
935         if (!GTK_IS_BOX(hbox)) return;
936
937         td->fd_button = gtk_button_new_with_label("...");
938         g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
939                          G_CALLBACK(tab_completion_button_size_allocate), parent);
940         g_signal_connect(G_OBJECT(td->fd_button), "clicked",
941                          G_CALLBACK(tab_completion_select_pressed), td);
942
943         gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
944
945         gtk_widget_show(td->fd_button);
946 }
947 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */