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