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