Change configuration system from Autotools to Meson
[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 *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_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
302 {
303         TabCompData *td = data;
304         gint height;
305         PangoLayout *layout;
306         PangoRectangle strong_pos, weak_pos;
307         gint length;
308         gint xoffset, yoffset;
309         GtkRequisition req;
310         GdkScreen *screen;
311         gint monitor_num;
312         GdkRectangle monitor;
313         GtkRequisition requisition;
314         GtkAllocation allocation;
315
316         gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(td->entry)), x, y);
317
318         screen = gtk_widget_get_screen(GTK_WIDGET(menu));
319         monitor_num = gdk_screen_get_monitor_at_window(screen, gtk_widget_get_window(GTK_WIDGET(td->entry)));
320         gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
321
322         gtk_widget_size_request(GTK_WIDGET(menu), &req);
323
324         length = strlen(gtk_entry_get_text(GTK_ENTRY(td->entry)));
325         gtk_entry_get_layout_offsets(GTK_ENTRY(td->entry), &xoffset, &yoffset);
326
327         layout = gtk_entry_get_layout(GTK_ENTRY(td->entry));
328         pango_layout_get_cursor_pos(layout, length, &strong_pos, &weak_pos);
329
330         *x += strong_pos.x / PANGO_SCALE + xoffset;
331
332         gtk_widget_get_requisition(td->entry, &requisition);
333         gtk_widget_get_allocation(td->entry, &allocation);
334
335         height = MIN(requisition.height, allocation.height);
336
337         if (req.height > monitor.y + monitor.height - *y - height &&
338             *y - monitor.y >  monitor.y + monitor.height - *y)
339                 {
340                 height = MIN(*y - monitor.y, req.height);
341                 gtk_widget_set_size_request(GTK_WIDGET(menu), -1, height);
342                 *y -= height;
343                 }
344         else
345                 {
346                 *y += height;
347                 }
348 }
349
350 static void tab_completion_popup_list(TabCompData *td, GList *list)
351 {
352         GtkWidget *menu;
353         GList *work;
354         GdkEvent *event;
355         guint32 etime;
356         gint ebutton;
357         gint count = 0;
358
359         if (!list) return;
360
361 #if 0
362         /*
363          * well, the menu would be too long anyway...
364          * (listing /dev causes gtk+ window allocation errors, -> too big a window)
365          * this is why menu popups are disabled, this really should be a popup scrollable listview.
366          */
367         if (g_list_length(list) > 200) return;
368 #endif
369
370         menu = popup_menu_short_lived();
371
372         work = list;
373         while (work && count < TAB_COMP_POPUP_MAX)
374                 {
375                 gchar *name = work->data;
376                 GtkWidget *item;
377
378                 item = menu_item_add_simple(menu, name, G_CALLBACK(tab_completion_popup_cb), name);
379                 g_object_set_data(G_OBJECT(item), "tab_completion_data", td);
380
381                 work = work->next;
382                 count++;
383                 }
384
385         g_signal_connect(G_OBJECT(menu), "key_press_event",
386                          G_CALLBACK(tab_completion_popup_key_press), td);
387
388         /* peek at the current event to get the time, etc. */
389         event = gtk_get_current_event();
390
391         if (event && event->type == GDK_BUTTON_RELEASE)
392                 {
393                 ebutton = event->button.button;
394                 }
395         else
396                 {
397                 ebutton = 0;
398                 }
399
400         if (event)
401                 {
402                 etime = gdk_event_get_time(event);
403                 gdk_event_free(event);
404                 }
405         else
406                 {
407                 etime = 0;
408                 }
409
410         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
411                        tab_completion_popup_pos_cb, td, ebutton, etime);
412 }
413
414 #ifndef CASE_SORT
415 #define CASE_SORT strcmp
416 #endif
417
418 static gint simple_sort(gconstpointer a, gconstpointer b)
419 {
420         return CASE_SORT((gchar *)a, (gchar *)b);
421 }
422
423 #endif
424
425 static gboolean tab_completion_do(TabCompData *td)
426 {
427         const gchar *entry_text = gtk_entry_get_text(GTK_ENTRY(td->entry));
428         const gchar *entry_file;
429         gchar *entry_dir;
430         gchar *ptr;
431         gboolean home_exp = FALSE;
432
433         if (entry_text[0] == '\0')
434                 {
435                 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME root directory win32 */
436                 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
437                 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
438                 g_free(entry_dir);
439                 return FALSE;
440                 }
441
442         /* home dir expansion */
443         if (entry_text[0] == '~')
444                 {
445                 entry_dir = expand_tilde(entry_text);
446                 home_exp = TRUE;
447                 }
448         else
449                 {
450                 entry_dir = g_strdup(entry_text);
451                 }
452
453         if (isfile(entry_dir))
454                 {
455                 if (home_exp)
456                         {
457                         gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
458                         gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
459                         }
460                 g_free(entry_dir);
461                 return home_exp;
462                 }
463
464         entry_file = filename_from_path(entry_text);
465
466         if (isdir(entry_dir) && strcmp(entry_file, ".") != 0 && strcmp(entry_file, "..") != 0)
467                 {
468                 ptr = entry_dir + strlen(entry_dir) - 1;
469                 if (ptr[0] == G_DIR_SEPARATOR)
470                         {
471                         if (home_exp)
472                                 {
473                                 gtk_entry_set_text(GTK_ENTRY(td->entry), entry_dir);
474                                 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(entry_dir));
475                                 }
476
477                         tab_completion_read_dir(td, entry_dir);
478                         td->file_list = g_list_sort(td->file_list, simple_sort);
479                         if (td->file_list && !td->file_list->next)
480                                 {
481                                 gchar *buf;
482                                 const gchar *file;
483
484                                 file = td->file_list->data;
485                                 buf = g_build_filename(entry_dir, file, NULL);
486                                 if (isdir(buf))
487                                         {
488                                         gchar *tmp = g_strconcat(buf, G_DIR_SEPARATOR_S, NULL);
489                                         g_free(buf);
490                                         buf = tmp;
491                                         }
492                                 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
493                                 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
494                                 g_free(buf);
495                                 }
496
497 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
498
499                         else
500                                 {
501                                 tab_completion_popup_list(td, td->file_list);
502                                 }
503 #endif
504
505                         g_free(entry_dir);
506                         return home_exp;
507                         }
508                 else
509                         {
510                         gchar *buf = g_strconcat(entry_dir, G_DIR_SEPARATOR_S, NULL);
511                         gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
512                         gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
513                         g_free(buf);
514                         g_free(entry_dir);
515                         return TRUE;
516                         }
517                 }
518
519         ptr = (gchar *)filename_from_path(entry_dir);
520         if (ptr > entry_dir) ptr--;
521         ptr[0] = '\0';
522
523         if (strlen(entry_dir) == 0)
524                 {
525                 g_free(entry_dir);
526                 entry_dir = g_strdup(G_DIR_SEPARATOR_S); /** @FIXME win32 */
527                 }
528
529         if (isdir(entry_dir))
530                 {
531                 GList *list;
532                 GList *poss = NULL;
533                 gint l = strlen(entry_file);
534
535                 if (!td->dir_path || !td->file_list || strcmp(td->dir_path, entry_dir) != 0)
536                         {
537                         tab_completion_read_dir(td, entry_dir);
538                         }
539
540                 list = td->file_list;
541                 while (list)
542                         {
543                         gchar *file = list->data;
544                         if (strncmp(entry_file, file, l) == 0)
545                                 {
546                                 poss = g_list_prepend(poss, file);
547                                 }
548                         list = list->next;
549                         }
550
551                 if (poss)
552                         {
553                         if (!poss->next)
554                                 {
555                                 gchar *file = poss->data;
556                                 gchar *buf;
557
558                                 buf = g_build_filename(entry_dir, file, NULL);
559                                 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
560                                 gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
561                                 g_free(buf);
562                                 g_list_free(poss);
563                                 g_free(entry_dir);
564                                 return TRUE;
565                                 }
566                         else
567                                 {
568                                 gsize c = strlen(entry_file);
569                                 gboolean done = FALSE;
570                                 gchar *test_file = poss->data;
571
572                                 while (!done)
573                                         {
574                                         list = poss;
575                                         if (!list) done = TRUE;
576                                         while (list && !done)
577                                                 {
578                                                 gchar *file = list->data;
579                                                 if (strlen(file) < c || strncmp(test_file, file, c) != 0)
580                                                         {
581                                                         done = TRUE;
582                                                         }
583                                                 list = list->next;
584                                                 }
585                                         c++;
586                                         }
587                                 c -= 2;
588                                 if (c > 0)
589                                         {
590                                         gchar *file;
591                                         gchar *buf;
592                                         file = g_strdup(test_file);
593                                         file[c] = '\0';
594                                         buf = g_build_filename(entry_dir, file, NULL);
595                                         gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
596                                         gtk_editable_set_position(GTK_EDITABLE(td->entry), strlen(buf));
597
598 #ifdef TAB_COMPLETION_ENABLE_POPUP_MENU
599
600                                         poss = g_list_sort(poss, simple_sort);
601                                         tab_completion_popup_list(td, poss);
602
603 #endif
604
605                                         g_free(file);
606                                         g_free(buf);
607                                         g_list_free(poss);
608                                         g_free(entry_dir);
609                                         return TRUE;
610                                         }
611                                 }
612                         g_list_free(poss);
613                         }
614                 }
615
616         g_free(entry_dir);
617
618         return FALSE;
619 }
620
621 static gboolean tab_completion_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
622 {
623         TabCompData *td = data;
624         gboolean stop_signal = FALSE;
625
626         switch (event->keyval)
627                 {
628                 case GDK_KEY_Tab:
629                         if (!(event->state & GDK_CONTROL_MASK))
630                                 {
631                                 if (tab_completion_do(td))
632                                         {
633                                         tab_completion_emit_tab_signal(td);
634                                         }
635                                 stop_signal = TRUE;
636                                 }
637                         break;
638                 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
639                         if (td->fd_button &&
640                             (event->state & GDK_CONTROL_MASK))
641                                 {
642                                 tab_completion_select_show(td);
643                                 stop_signal = TRUE;
644                                 }
645                         else if (tab_completion_emit_enter_signal(td))
646                                 {
647                                 stop_signal = TRUE;
648                                 }
649                         break;
650                 default:
651                         break;
652                 }
653
654         if (stop_signal) g_signal_stop_emission_by_name(G_OBJECT(widget), "key_press_event");
655
656         return (stop_signal);
657 }
658
659 static void tab_completion_button_pressed(GtkWidget *widget, gpointer data)
660 {
661         TabCompData *td;
662         GtkWidget *entry = data;
663
664         td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
665
666         if (!td) return;
667
668         if (!gtk_widget_has_focus(entry))
669                 {
670                 gtk_widget_grab_focus(entry);
671                 }
672
673         if (tab_completion_do(td))
674                 {
675                 tab_completion_emit_tab_signal(td);
676                 }
677 }
678
679 static void tab_completion_button_size_allocate(GtkWidget *button, GtkAllocation *allocation, gpointer data)
680 {
681         GtkWidget *parent = data;
682         GtkAllocation parent_allocation;
683         gtk_widget_get_allocation(parent, &parent_allocation);
684
685         if (allocation->height > parent_allocation.height)
686                 {
687                 GtkAllocation button_allocation;
688
689                 gtk_widget_get_allocation(button, &button_allocation);
690                 button_allocation.height = parent_allocation.height;
691                 button_allocation.y = parent_allocation.y +
692                         (parent_allocation.height - parent_allocation.height) / 2;
693                 gtk_widget_size_allocate(button, &button_allocation);
694                 }
695 }
696
697 static GtkWidget *tab_completion_create_complete_button(GtkWidget *entry, GtkWidget *parent)
698 {
699         GtkWidget *button;
700         GtkWidget *icon;
701         GdkPixbuf *pixbuf;
702
703         button = gtk_button_new();
704         gtk_widget_set_can_focus(button, FALSE);
705         g_signal_connect(G_OBJECT(button), "size_allocate",
706                          G_CALLBACK(tab_completion_button_size_allocate), parent);
707         g_signal_connect(G_OBJECT(button), "clicked",
708                          G_CALLBACK(tab_completion_button_pressed), entry);
709
710         pixbuf = gdk_pixbuf_new_from_inline(-1, icon_tabcomp, FALSE, NULL);
711         icon = gtk_image_new_from_pixbuf(pixbuf);
712         g_object_unref(pixbuf);
713
714         gtk_container_add(GTK_CONTAINER(button), icon);
715         gtk_widget_show(icon);
716
717         return button;
718 }
719
720 /*
721  *----------------------------------------------------------------------------
722  * public interface
723  *----------------------------------------------------------------------------
724  */
725
726 GtkWidget *tab_completion_new_with_history(GtkWidget **entry, const gchar *text,
727                                            const gchar *history_key, gint max_levels,
728                                            void (*enter_func)(const gchar *, gpointer), gpointer data)
729 {
730         GtkWidget *box;
731         GtkWidget *combo;
732         GtkWidget *combo_entry;
733         GtkWidget *button;
734         GList *work;
735         TabCompData *td;
736         gint n = 0;
737
738         box = gtk_hbox_new(FALSE, 0);
739
740         combo = gtk_combo_box_text_new_with_entry();
741         gtk_box_pack_start(GTK_BOX(box), combo, TRUE, TRUE, 0);
742         gtk_widget_show(combo);
743
744         combo_entry = gtk_bin_get_child(GTK_BIN(combo));
745
746         button = tab_completion_create_complete_button(combo_entry, combo);
747         gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0);
748         gtk_widget_show(button);
749
750         tab_completion_add_to_entry(combo_entry, enter_func, NULL, NULL, data);
751
752         td = g_object_get_data(G_OBJECT(combo_entry), "tab_completion_data");
753         if (!td) return NULL; /* this should never happen! */
754
755         td->combo = combo;
756         td->has_history = TRUE;
757         td->history_key = g_strdup(history_key);
758         td->history_levels = max_levels;
759
760         work = history_list_get_by_key(td->history_key);
761
762         work = history_list_get_by_key(history_key);
763         while (work)
764                 {
765                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), (gchar *)work->data);
766                 work = work->next;
767                 n++;
768                 }
769
770         if (text)
771                 {
772                 gtk_entry_set_text(GTK_ENTRY(combo_entry), text);
773                 }
774         else if (n > 0)
775                 {
776                 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
777                 }
778
779         if (entry) *entry = combo_entry;
780         return box;
781 }
782
783 const gchar *tab_completion_set_to_last_history(GtkWidget *entry)
784 {
785         TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
786         const gchar *buf;
787
788         if (!td || !td->has_history) return NULL;
789
790         buf = history_list_find_last_path_by_key(td->history_key);
791         if (buf)
792                 {
793                 gtk_entry_set_text(GTK_ENTRY(td->entry), buf);
794                 }
795
796         return buf;
797 }
798
799 void tab_completion_append_to_history(GtkWidget *entry, const gchar *path)
800 {
801         TabCompData *td;
802         GtkTreeModel *store;
803         GList *work;
804         gint n = 0;
805
806         td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
807
808         if (!path) return;
809
810         if (!td || !td->has_history) return;
811
812         history_list_add_to_key(td->history_key, path, td->history_levels);
813
814         gtk_combo_box_set_active(GTK_COMBO_BOX(td->combo), -1);
815
816         store = gtk_combo_box_get_model(GTK_COMBO_BOX(td->combo));
817         gtk_list_store_clear(GTK_LIST_STORE(store));
818
819         work = history_list_get_by_key(td->history_key);
820         while (work)
821                 {
822                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(td->combo), (gchar *)work->data);
823                 work = work->next;
824                 n++;
825                 }
826
827         if (td->tab_append_func) {
828                 td->tab_append_func(path, td->tab_append_data, n);
829         }
830 }
831
832 GtkWidget *tab_completion_new(GtkWidget **entry, const gchar *text,
833                               void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
834 {
835         GtkWidget *hbox;
836         GtkWidget *button;
837         GtkWidget *newentry;
838
839         hbox = gtk_hbox_new(FALSE, 0);
840
841         newentry = gtk_entry_new();
842         if (text) gtk_entry_set_text(GTK_ENTRY(newentry), text);
843         gtk_box_pack_start(GTK_BOX(hbox), newentry, TRUE, TRUE, 0);
844         gtk_widget_show(newentry);
845
846         button = tab_completion_create_complete_button(newentry, newentry);
847         gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
848         gtk_widget_show(button);
849
850         tab_completion_add_to_entry(newentry, enter_func, filter, filter_desc, data);
851         if (entry) *entry = newentry;
852         return hbox;
853 }
854
855 void tab_completion_add_to_entry(GtkWidget *entry, void (*enter_func)(const gchar *, gpointer), const gchar *filter, const gchar *filter_desc, gpointer data)
856 {
857         TabCompData *td;
858         if (!entry)
859                 {
860                 log_printf("Tab completion error: entry != NULL\n");
861                 return;
862                 }
863
864         td = g_new0(TabCompData, 1);
865
866         td->entry = entry;
867         td->enter_func = enter_func;
868         td->enter_data = data;
869         td->filter = g_strdup(filter);
870         td->filter_desc = g_strdup(filter_desc);
871
872         g_object_set_data(G_OBJECT(td->entry), "tab_completion_data", td);
873
874         g_signal_connect(G_OBJECT(entry), "key_press_event",
875                          G_CALLBACK(tab_completion_key_pressed), td);
876         g_signal_connect(G_OBJECT(entry), "destroy",
877                          G_CALLBACK(tab_completion_destroy), td);
878 }
879
880 void tab_completion_add_tab_func(GtkWidget *entry, void (*tab_func)(const gchar *, gpointer), gpointer data)
881 {
882         TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
883
884         if (!td) return;
885
886         td->tab_func = tab_func;
887         td->tab_data = data;
888 }
889
890 /* Add a callback function called when a new entry is appended to the list */
891 void tab_completion_add_append_func(GtkWidget *entry, void (*tab_append_func)(const gchar *, gpointer, gint), gpointer data)
892 {
893         TabCompData *td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
894
895         if (!td) return;
896
897         td->tab_append_func = tab_append_func;
898         td->tab_append_data = data;
899 }
900
901 gchar *remove_trailing_slash(const gchar *path)
902 {
903         gint l;
904
905         if (!path) return NULL;
906
907         l = strlen(path);
908         while (l > 1 && path[l - 1] == G_DIR_SEPARATOR) l--;
909
910         return g_strndup(path, l);
911 }
912
913 static void tab_completion_select_cancel_cb(FileDialog *fd, gpointer data)
914 {
915         TabCompData *td = data;
916
917         td->fd = NULL;
918         file_dialog_close(fd);
919 }
920
921 static void tab_completion_select_ok_cb(FileDialog *fd, gpointer data)
922 {
923         TabCompData *td = data;
924
925         gtk_entry_set_text(GTK_ENTRY(td->entry), gtk_entry_get_text(GTK_ENTRY(fd->entry)));
926
927         tab_completion_select_cancel_cb(fd, data);
928
929         tab_completion_emit_enter_signal(td);
930 }
931
932 static void tab_completion_select_show(TabCompData *td)
933 {
934         const gchar *title;
935         const gchar *path;
936         gchar *filter = NULL;
937         gchar *filter_desc = NULL;
938
939         if (td->fd)
940                 {
941                 gtk_window_present(GTK_WINDOW(GENERIC_DIALOG(td->fd)->dialog));
942                 return;
943                 }
944
945         title = (td->fd_title) ? td->fd_title : _("Select path");
946         td->fd = file_dialog_new(title, "select_path", td->entry,
947                                  tab_completion_select_cancel_cb, td);
948         file_dialog_add_button(td->fd, GTK_STOCK_OK, NULL,
949                                  tab_completion_select_ok_cb, TRUE);
950
951         generic_dialog_add_message(GENERIC_DIALOG(td->fd), NULL, title, NULL, FALSE);
952
953         if (td->filter)
954                 {
955                 filter = td->filter;
956                 }
957         else
958                 {
959                 filter = "*";
960                 }
961         if (td->filter_desc)
962                 {
963                 filter_desc = td->filter_desc;
964                 }
965         else
966                 {
967                 filter_desc = _("All files");
968                 }
969
970         path = gtk_entry_get_text(GTK_ENTRY(td->entry));
971         if (strlen(path) == 0) path = NULL;
972         if (td->fd_folders_only)
973                 {
974                 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, NULL, NULL);
975                 }
976         else
977                 {
978                 file_dialog_add_path_widgets(td->fd, NULL, path, td->history_key, filter, filter_desc);
979                 }
980
981         gtk_widget_show(GENERIC_DIALOG(td->fd)->dialog);
982 }
983
984 static void tab_completion_select_pressed(GtkWidget *widget, gpointer data)
985 {
986         TabCompData *td = data;
987
988         tab_completion_select_show(td);
989 }
990
991 void tab_completion_add_select_button(GtkWidget *entry, const gchar *title, gboolean folders_only)
992 {
993         TabCompData *td;
994         GtkWidget *parent;
995         GtkWidget *hbox;
996
997         td = g_object_get_data(G_OBJECT(entry), "tab_completion_data");
998
999         if (!td) return;
1000
1001         g_free(td->fd_title);
1002         td->fd_title = g_strdup(title);
1003         td->fd_folders_only = folders_only;
1004
1005         if (td->fd_button) return;
1006
1007         parent = (td->combo) ? td->combo : td->entry;
1008
1009         hbox = gtk_widget_get_parent(parent);
1010         if (!GTK_IS_BOX(hbox)) return;
1011
1012         td->fd_button = gtk_button_new_with_label("...");
1013         g_signal_connect(G_OBJECT(td->fd_button), "size_allocate",
1014                          G_CALLBACK(tab_completion_button_size_allocate), parent);
1015         g_signal_connect(G_OBJECT(td->fd_button), "clicked",
1016                          G_CALLBACK(tab_completion_select_pressed), td);
1017
1018         gtk_box_pack_start(GTK_BOX(hbox), td->fd_button, FALSE, FALSE, 0);
1019
1020         gtk_widget_show(td->fd_button);
1021 }
1022 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */