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