]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
Revert the change on 2007-04-29 (Also check the default handler when
[~andy/gtk] / gtk / gtkfilechooserentry.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilechooserentry.c: Entry with filename completion
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22 #include <string.h>
23
24 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
26 #include "gtkentry.h"
27 #include "gtkfilechooserentry.h"
28 #include "gtkmain.h"
29 #include "gtkintl.h"
30 #include "gtkalias.h"
31
32 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
33
34 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
35 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
36 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
37
38 struct _GtkFileChooserEntryClass
39 {
40   GtkEntryClass parent_class;
41 };
42
43 struct _GtkFileChooserEntry
44 {
45   GtkEntry parent_instance;
46
47   GtkFileChooserAction action;
48
49   GtkFileSystem *file_system;
50   GtkFilePath *base_folder;
51   GtkFilePath *current_folder_path;
52   gchar *file_part;
53   gint file_part_pos;
54   guint check_completion_idle;
55   guint load_directory_idle;
56
57   GtkFileFolder *current_folder;
58   GtkFileSystemHandle *load_folder_handle;
59
60   GtkListStore *completion_store;
61
62   guint has_completion : 1;
63   guint in_change      : 1;
64   guint eat_tabs       : 1;
65 };
66
67 enum
68 {
69   DISPLAY_NAME_COLUMN,
70   PATH_COLUMN,
71   N_COLUMNS
72 };
73
74 static void     gtk_file_chooser_entry_iface_init     (GtkEditableClass *iface);
75
76 static void     gtk_file_chooser_entry_finalize       (GObject          *object);
77 static void     gtk_file_chooser_entry_dispose        (GObject          *object);
78 static gboolean gtk_file_chooser_entry_focus          (GtkWidget        *widget,
79                                                        GtkDirectionType  direction);
80 static void     gtk_file_chooser_entry_activate       (GtkEntry         *entry);
81 static void     gtk_file_chooser_entry_changed        (GtkEditable      *editable);
82 static void     gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
83                                                        const gchar *new_text,
84                                                        gint         new_text_length,
85                                                        gint        *position);
86
87 static void     clear_completion_callback (GtkFileChooserEntry *chooser_entry,
88                                            GParamSpec          *pspec);
89 static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
90                                            GtkTreeModel        *model,
91                                            GtkTreeIter         *iter,
92                                            GtkFileChooserEntry *chooser_entry);
93 static gboolean completion_match_func     (GtkEntryCompletion  *comp,
94                                            const char          *key,
95                                            GtkTreeIter         *iter,
96                                            gpointer             data);
97 static void     files_added_cb            (GtkFileSystem       *file_system,
98                                            GSList              *added_uris,
99                                            GtkFileChooserEntry *chooser_entry);
100 static void     files_deleted_cb          (GtkFileSystem       *file_system,
101                                            GSList              *deleted_uris,
102                                            GtkFileChooserEntry *chooser_entry);
103 static char    *maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
104                                                 GtkFilePath         *path,
105                                                 gchar               *display_name);
106
107 static GtkEditableClass *parent_editable_iface;
108
109 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
110                          G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
111                                                 gtk_file_chooser_entry_iface_init))
112
113 static void
114 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
115 {
116   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
117   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
118   GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
119
120   gobject_class->finalize = gtk_file_chooser_entry_finalize;
121   gobject_class->dispose = gtk_file_chooser_entry_dispose;
122
123   widget_class->focus = gtk_file_chooser_entry_focus;
124
125   entry_class->activate = gtk_file_chooser_entry_activate;
126 }
127
128 static void
129 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
130 {
131   parent_editable_iface = g_type_interface_peek_parent (iface);
132
133   iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
134   iface->changed = gtk_file_chooser_entry_changed;
135 }
136
137 static void
138 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
139 {
140   GtkEntryCompletion *comp;
141   GtkCellRenderer *cell;
142
143   g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
144
145   comp = gtk_entry_completion_new ();
146   gtk_entry_completion_set_popup_single_match (comp, FALSE);
147
148   gtk_entry_completion_set_match_func (comp,
149                                        completion_match_func,
150                                        chooser_entry,
151                                        NULL);
152
153   cell = gtk_cell_renderer_text_new ();
154   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
155                               cell, TRUE);
156   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
157                                  cell,
158                                  "text", 0);
159
160   g_signal_connect (comp, "match_selected",
161                     G_CALLBACK (match_selected_callback), chooser_entry);
162
163   gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
164   g_object_unref (comp);
165
166   g_signal_connect (chooser_entry, "notify::cursor-position",
167                     G_CALLBACK (clear_completion_callback), NULL);
168   g_signal_connect (chooser_entry, "notify::selection-bound",
169                     G_CALLBACK (clear_completion_callback), NULL);
170 }
171
172 static void
173 gtk_file_chooser_entry_finalize (GObject *object)
174 {
175   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
176
177   gtk_file_path_free (chooser_entry->base_folder);
178   gtk_file_path_free (chooser_entry->current_folder_path);
179   g_free (chooser_entry->file_part);
180
181   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
182 }
183
184 static void
185 gtk_file_chooser_entry_dispose (GObject *object)
186 {
187   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
188
189   if (chooser_entry->completion_store)
190     {
191       g_object_unref (chooser_entry->completion_store);
192       chooser_entry->completion_store = NULL;
193     }
194
195   if (chooser_entry->load_folder_handle)
196     {
197       gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
198       chooser_entry->load_folder_handle = NULL;
199     }
200
201   if (chooser_entry->current_folder)
202     {
203       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
204                                             G_CALLBACK (files_added_cb), chooser_entry);
205       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
206                                             G_CALLBACK (files_deleted_cb), chooser_entry);
207       g_object_unref (chooser_entry->current_folder);
208       chooser_entry->current_folder = NULL;
209     }
210
211   if (chooser_entry->file_system)
212     {
213       g_object_unref (chooser_entry->file_system);
214       chooser_entry->file_system = NULL;
215     }
216
217   if (chooser_entry->check_completion_idle)
218     {
219       g_source_remove (chooser_entry->check_completion_idle);
220       chooser_entry->check_completion_idle = 0;
221     }
222
223   if (chooser_entry->load_directory_idle)
224     {
225       g_source_remove (chooser_entry->load_directory_idle);
226       chooser_entry->load_directory_idle = 0;
227     }
228
229   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
230 }
231
232 /* Match functions for the GtkEntryCompletion */
233 static gboolean
234 match_selected_callback (GtkEntryCompletion  *completion,
235                          GtkTreeModel        *model,
236                          GtkTreeIter         *iter,
237                          GtkFileChooserEntry *chooser_entry)
238 {
239   char *display_name;
240   GtkFilePath *path;
241   gint pos;
242   
243   gtk_tree_model_get (model, iter,
244                       DISPLAY_NAME_COLUMN, &display_name,
245                       PATH_COLUMN, &path,
246                       -1);
247
248   if (!display_name || !path)
249     {
250       /* these shouldn't complain if passed NULL */
251       gtk_file_path_free (path);
252       g_free (display_name);
253       return FALSE;
254     }
255
256   display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
257
258   pos = chooser_entry->file_part_pos;
259
260   /* We don't set in_change here as we want to update the current_folder
261    * variable */
262   gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
263                             pos, -1);
264   gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
265                             display_name, -1, 
266                             &pos);
267   gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
268
269   gtk_file_path_free (path);
270   g_free (display_name);
271
272   return TRUE;
273 }
274
275 /* Match function for the GtkEntryCompletion */
276 static gboolean
277 completion_match_func (GtkEntryCompletion *comp,
278                        const char         *key_unused,
279                        GtkTreeIter        *iter,
280                        gpointer            data)
281 {
282   GtkFileChooserEntry *chooser_entry;
283   char *name;
284   gboolean result;
285   char *norm_file_part;
286   char *norm_name;
287
288   chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
289
290   /* We ignore the key because it is the contents of the entry.  Instead, we
291    * just use our precomputed file_part.
292    */
293   if (!chooser_entry->file_part)
294     {
295       return FALSE;
296     }
297
298   gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
299   if (!name)
300     {
301       return FALSE; /* Uninitialized row, ugh */
302     }
303
304   /* If we have an empty file_part, then we're at the root of a directory.  In
305    * that case, we want to match all non-dot files.  We might want to match
306    * dot_files too if show_hidden is TRUE on the fileselector in the future.
307    */
308   /* Additionally, support for gnome .hidden files would be sweet, too */
309   if (chooser_entry->file_part[0] == '\000')
310     {
311       if (name[0] == '.')
312         result = FALSE;
313       else
314         result = TRUE;
315       g_free (name);
316
317       return result;
318     }
319
320
321   norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
322   norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
323
324 #ifdef G_PLATFORM_WIN32
325   {
326     gchar *temp;
327
328     temp = norm_file_part;
329     norm_file_part = g_utf8_casefold (norm_file_part, -1);
330     g_free (temp);
331
332     temp = norm_name;
333     norm_name = g_utf8_casefold (norm_name, -1);
334     g_free (temp);
335   }
336 #endif
337
338   result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
339
340   g_free (norm_file_part);
341   g_free (norm_name);
342   g_free (name);
343   
344   return result;
345 }
346
347 /* This function will append a directory separator to paths to
348  * display_name iff the path associated with it is a directory.
349  * maybe_append_separator_to_path will g_free the display_name and
350  * return a new one if needed.  Otherwise, it will return the old one.
351  * You should be safe calling
352  *
353  * display_name = maybe_append_separator_to_path (entry, path, display_name);
354  * ...
355  * g_free (display_name);
356  */
357 static char *
358 maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
359                                 GtkFilePath         *path,
360                                 gchar               *display_name)
361 {
362   if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && path)
363     {
364       GtkFileInfo *info;
365             
366       info = gtk_file_folder_get_info (chooser_entry->current_folder,
367                                        path, NULL); /* NULL-GError */
368
369       if (info)
370         {
371           if (gtk_file_info_get_is_folder (info))
372             {
373               gchar *tmp = display_name;
374               display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
375               g_free (tmp);
376             }
377           
378           gtk_file_info_free (info);
379         }
380     }
381
382   return display_name;
383 }
384
385 /* Determines if the completion model has entries with a common prefix relative
386  * to the current contents of the entry.  Also, if there's one and only one such
387  * path, stores it in unique_path_ret.
388  */
389 static void
390 find_common_prefix (GtkFileChooserEntry *chooser_entry,
391                     gchar               **common_prefix_ret,
392                     GtkFilePath         **unique_path_ret)
393 {
394   GtkTreeIter iter;
395   gboolean valid;
396
397   *common_prefix_ret = NULL;
398   *unique_path_ret = NULL;
399
400   if (chooser_entry->completion_store == NULL)
401     return;
402
403   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
404
405   while (valid)
406     {
407       gchar *display_name;
408       GtkFilePath *path;
409
410       gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
411                           &iter,
412                           DISPLAY_NAME_COLUMN, &display_name,
413                           PATH_COLUMN, &path,
414                           -1);
415
416       if (g_str_has_prefix (display_name, chooser_entry->file_part))
417         {
418           if (!*common_prefix_ret)
419             {
420               *common_prefix_ret = g_strdup (display_name);
421               *unique_path_ret = gtk_file_path_copy (path);
422             }
423           else
424             {
425               gchar *p = *common_prefix_ret;
426               const gchar *q = display_name;
427                   
428               while (*p && *p == *q)
429                 {
430                   p++;
431                   q++;
432                 }
433                   
434               *p = '\0';
435
436               gtk_file_path_free (*unique_path_ret);
437               *unique_path_ret = NULL;
438             }
439         }
440
441       g_free (display_name);
442       gtk_file_path_free (path);
443       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
444     }
445 }
446
447 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
448 static void
449 append_common_prefix (GtkFileChooserEntry *chooser_entry,
450                       gboolean             highlight)
451 {
452   gchar *common_prefix;
453   GtkFilePath *unique_path;
454
455   find_common_prefix (chooser_entry, &common_prefix, &unique_path);
456
457   if (unique_path)
458     {
459       common_prefix = maybe_append_separator_to_path (chooser_entry,
460                                                       unique_path,
461                                                       common_prefix);
462       gtk_file_path_free (unique_path);
463     }
464
465   if (common_prefix)
466     {
467       gint file_part_len;
468       gint common_prefix_len;
469       gint pos;
470
471       file_part_len = g_utf8_strlen (chooser_entry->file_part, -1);
472       common_prefix_len = g_utf8_strlen (common_prefix, -1);
473
474       if (common_prefix_len > file_part_len)
475         {
476           pos = chooser_entry->file_part_pos;
477
478           chooser_entry->in_change = TRUE;
479           gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
480                                     pos, -1);
481           gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
482                                     common_prefix, -1, 
483                                     &pos);
484           chooser_entry->in_change = FALSE;
485
486           if (highlight)
487             {
488               gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
489                                           chooser_entry->file_part_pos + file_part_len,
490                                           chooser_entry->file_part_pos + common_prefix_len);
491               chooser_entry->has_completion = TRUE;
492             }
493         }
494
495       g_free (common_prefix);
496     }
497 }
498
499 static gboolean
500 check_completion_callback (GtkFileChooserEntry *chooser_entry)
501 {
502   GDK_THREADS_ENTER ();
503
504   g_assert (chooser_entry->file_part);
505
506   chooser_entry->check_completion_idle = 0;
507
508   if (strcmp (chooser_entry->file_part, "") == 0)
509     goto done;
510
511   /* We only insert the common prefix without requiring the user to hit Tab in
512    * the "open" modes.  For "save" modes, the user must hit Tab to cause the prefix
513    * to be inserted.  That happens in gtk_file_chooser_entry_focus().
514    */
515   if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
516       || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
517     append_common_prefix (chooser_entry, TRUE);
518
519  done:
520
521   GDK_THREADS_LEAVE ();
522
523   return FALSE;
524 }
525
526 static guint
527 idle_add (GtkFileChooserEntry *chooser_entry, 
528           GCallback            cb)
529 {
530   GSource *source;
531   guint id;
532
533   source = g_idle_source_new ();
534   g_source_set_priority (source, G_PRIORITY_HIGH);
535   g_source_set_closure (source,
536                         g_cclosure_new_object (cb, G_OBJECT (chooser_entry)));
537   id = g_source_attach (source, NULL);
538   g_source_unref (source);
539
540   return id;
541 }
542
543 static void
544 add_completion_idle (GtkFileChooserEntry *chooser_entry)
545 {
546   /* idle to update the selection based on the file list */
547   if (chooser_entry->check_completion_idle == 0)
548     chooser_entry->check_completion_idle = 
549       idle_add (chooser_entry, G_CALLBACK (check_completion_callback));
550 }
551
552
553 static void
554 update_current_folder_files (GtkFileChooserEntry *chooser_entry,
555                              GSList              *added_uris)
556 {
557   GSList *tmp_list;
558
559   g_assert (chooser_entry->completion_store != NULL);
560
561   /* Bah.  Need to turn off sorting */
562   for (tmp_list = added_uris; tmp_list; tmp_list = tmp_list->next)
563     {
564       GtkFileInfo *info;
565       GtkFilePath *path;
566
567       path = tmp_list->data;
568
569       info = gtk_file_folder_get_info (chooser_entry->current_folder,
570                                        path,
571                                        NULL); /* NULL-GError */
572       if (info)
573         {
574           gchar *display_name = g_strdup (gtk_file_info_get_display_name (info));
575           GtkTreeIter iter;
576
577           display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
578
579           gtk_list_store_append (chooser_entry->completion_store, &iter);
580           gtk_list_store_set (chooser_entry->completion_store, &iter,
581                               DISPLAY_NAME_COLUMN, display_name,
582                               PATH_COLUMN, path,
583                               -1);
584
585           gtk_file_info_free (info);
586           g_free (display_name);
587         }
588     }
589
590   /* FIXME: we want to turn off sorting temporarily.  I suck... */
591   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
592                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
593
594   add_completion_idle (chooser_entry);
595 }
596
597 static void
598 files_added_cb (GtkFileSystem       *file_system,
599                 GSList              *added_uris,
600                 GtkFileChooserEntry *chooser_entry)
601 {
602   update_current_folder_files (chooser_entry, added_uris);
603 }
604
605 static void
606 files_deleted_cb (GtkFileSystem       *file_system,
607                   GSList              *deleted_uris,
608                   GtkFileChooserEntry *chooser_entry)
609 {
610   /* FIXME: gravy... */
611 }
612
613 static void
614 load_directory_get_folder_callback (GtkFileSystemHandle *handle,
615                                     GtkFileFolder       *folder,
616                                     const GError        *error,
617                                     gpointer             data)
618 {
619   gboolean cancelled = handle->cancelled;
620   GtkFileChooserEntry *chooser_entry = data;
621
622   if (handle != chooser_entry->load_folder_handle)
623     goto out;
624
625   chooser_entry->load_folder_handle = NULL;
626
627   if (cancelled || error)
628     goto out;
629
630   chooser_entry->current_folder = folder;
631   g_signal_connect (chooser_entry->current_folder, "files-added",
632                     G_CALLBACK (files_added_cb), chooser_entry);
633   g_signal_connect (chooser_entry->current_folder, "files-removed",
634                     G_CALLBACK (files_deleted_cb), chooser_entry);
635   
636   chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
637                                                         G_TYPE_STRING,
638                                                         GTK_TYPE_FILE_PATH);
639
640   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
641                                   GTK_TREE_MODEL (chooser_entry->completion_store));
642
643 out:
644   g_object_unref (chooser_entry);
645   g_object_unref (handle);
646 }
647
648 static gboolean
649 load_directory_callback (GtkFileChooserEntry *chooser_entry)
650 {
651   GDK_THREADS_ENTER ();
652
653   chooser_entry->load_directory_idle = 0;
654
655   /* guard against bogus settings*/
656   if (chooser_entry->current_folder_path == NULL ||
657       chooser_entry->file_system == NULL)
658     goto done;
659
660   if (chooser_entry->current_folder != NULL)
661     {
662       g_warning ("idle activate multiple times without clearing the folder object first.");
663       goto done;
664     }
665   g_assert (chooser_entry->completion_store == NULL);
666
667   /* Load the folder */
668   if (chooser_entry->load_folder_handle)
669     gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
670
671   chooser_entry->load_folder_handle =
672     gtk_file_system_get_folder (chooser_entry->file_system,
673                                 chooser_entry->current_folder_path,
674                                 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
675                                 load_directory_get_folder_callback,
676                                 g_object_ref (chooser_entry));
677
678  done:
679   
680   GDK_THREADS_LEAVE ();
681
682   return FALSE;
683 }
684
685 static void
686 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
687                                        const gchar *new_text,
688                                        gint         new_text_length,
689                                        gint        *position)
690 {
691   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
692
693   parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
694
695   if (! chooser_entry->in_change)
696     add_completion_idle (GTK_FILE_CHOOSER_ENTRY (editable));
697 }
698
699 static gboolean
700 gtk_file_chooser_entry_focus (GtkWidget        *widget,
701                               GtkDirectionType  direction)
702 {
703   GtkFileChooserEntry *chooser_entry;
704   GtkEditable *editable;
705   GtkEntry *entry;
706   GdkModifierType state;
707   gboolean control_pressed;
708
709   chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
710   editable = GTK_EDITABLE (widget);
711   entry = GTK_ENTRY (widget);
712
713   if (!chooser_entry->eat_tabs)
714     return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
715
716   control_pressed = FALSE;
717
718   if (gtk_get_current_event_state (&state))
719     {
720       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
721         control_pressed = TRUE;
722     }
723
724   /* This is a bit evil -- it makes Tab never leave the entry. It basically
725    * makes it 'safe' for people to hit. */
726   if ((direction == GTK_DIR_TAB_FORWARD) &&
727       (GTK_WIDGET_HAS_FOCUS (widget)) &&
728       (! control_pressed))
729     {
730       gint pos = 0;
731
732       if (!chooser_entry->has_completion
733           && gtk_editable_get_position (editable) == entry->text_length)
734         append_common_prefix (chooser_entry, FALSE);
735
736       gtk_editable_set_position (editable, entry->text_length);
737
738       /* Trigger the completion window to pop up again by a 
739        * zero-length insertion, a bit of a hack.
740        */
741       gtk_editable_insert_text (editable, "", -1, &pos);
742
743       return TRUE;
744     }
745   else
746     return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
747 }
748
749 static void
750 gtk_file_chooser_entry_activate (GtkEntry *entry)
751 {
752   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
753
754   if (chooser_entry->has_completion)
755     {
756       gtk_editable_set_position (GTK_EDITABLE (entry),
757                                  entry->text_length);
758     }
759   
760   GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
761 }
762
763 /* This will see if a path typed by the user is new, and installs the loading
764  * idle if it is.
765  */
766 static void
767 gtk_file_chooser_entry_maybe_update_directory (GtkFileChooserEntry *chooser_entry,
768                                                GtkFilePath         *folder_path,
769                                                gboolean             force_reload)
770 {
771   gboolean queue_idle = FALSE;
772
773   if (chooser_entry->current_folder_path)
774     {
775       if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0 || force_reload)
776         {
777           /* We changed our current directory.  We need to clear out the old
778            * directory information.
779            */
780           if (chooser_entry->current_folder)
781             {
782               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
783                                                     G_CALLBACK (files_added_cb), chooser_entry);
784               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
785                                                     G_CALLBACK (files_deleted_cb), chooser_entry);
786
787               g_object_unref (chooser_entry->current_folder);
788               chooser_entry->current_folder = NULL;
789             }
790           if (chooser_entry->completion_store)
791             {
792               gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
793               g_object_unref (chooser_entry->completion_store);
794               chooser_entry->completion_store = NULL;
795             }
796
797           queue_idle = TRUE;
798         }
799
800       gtk_file_path_free (chooser_entry->current_folder_path);
801     }
802   else
803     {
804       queue_idle = TRUE;
805     }
806
807   chooser_entry->current_folder_path = folder_path;
808
809   if (queue_idle && chooser_entry->load_directory_idle == 0)
810     chooser_entry->load_directory_idle =
811       idle_add (chooser_entry, G_CALLBACK (load_directory_callback));
812 }
813
814
815
816 static void
817 gtk_file_chooser_entry_changed (GtkEditable *editable)
818 {
819   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
820   const gchar *text;
821   GtkFilePath *folder_path;
822   gchar *file_part;
823   gsize total_len, file_part_len;
824   gint file_part_pos;
825
826   if (chooser_entry->in_change)
827     return;
828
829   text = gtk_entry_get_text (GTK_ENTRY (editable));
830   
831   if (!chooser_entry->file_system ||
832       !chooser_entry->base_folder ||
833       !gtk_file_system_parse (chooser_entry->file_system,
834                               chooser_entry->base_folder, text,
835                               &folder_path, &file_part, NULL)) /* NULL-GError */
836     {
837       folder_path = gtk_file_path_copy (chooser_entry->base_folder);
838       file_part = g_strdup ("");
839       file_part_pos = -1;
840     }
841   else
842     {
843       file_part_len = strlen (file_part);
844       total_len = strlen (text);
845       if (total_len > file_part_len)
846         file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
847       else
848         file_part_pos = 0;
849     }
850
851   g_free (chooser_entry->file_part);
852
853   chooser_entry->file_part = file_part;
854   chooser_entry->file_part_pos = file_part_pos;
855
856   gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path, file_part_pos == -1);
857 }
858
859 static void
860 clear_completion_callback (GtkFileChooserEntry *chooser_entry,
861                            GParamSpec          *pspec)
862 {
863   if (chooser_entry->has_completion)
864     {
865       chooser_entry->has_completion = FALSE;
866       gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
867     }
868 }
869
870 /**
871  * _gtk_file_chooser_entry_new:
872  * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
873  *
874  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
875  * is an internal implementation widget for the GTK+ file chooser
876  * which is an entry with completion with respect to a
877  * #GtkFileSystem object.
878  *
879  * Return value: the newly created #GtkFileChooserEntry
880  **/
881 GtkWidget *
882 _gtk_file_chooser_entry_new (gboolean eat_tabs)
883 {
884   GtkFileChooserEntry *chooser_entry;
885
886   chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
887   chooser_entry->eat_tabs = (eat_tabs != FALSE);
888
889   return GTK_WIDGET (chooser_entry);
890 }
891
892 /**
893  * _gtk_file_chooser_entry_set_file_system:
894  * @chooser_entry: a #GtkFileChooser
895  * @file_system: an object implementing #GtkFileSystem
896  *
897  * Sets the file system for @chooser_entry.
898  **/
899 void
900 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
901                                          GtkFileSystem       *file_system)
902 {
903   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
904   g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
905
906   if (file_system != chooser_entry->file_system)
907     {
908       if (chooser_entry->file_system)
909         g_object_unref (chooser_entry->file_system);
910
911       chooser_entry->file_system = g_object_ref (file_system);
912     }
913 }
914
915 /**
916  * _gtk_file_chooser_entry_set_base_folder:
917  * @chooser_entry: a #GtkFileChooserEntry
918  * @path: path of a folder in the chooser entries current file system.
919  *
920  * Sets the folder with respect to which completions occur.
921  **/
922 void
923 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
924                                          const GtkFilePath   *path)
925 {
926   if (chooser_entry->base_folder)
927     gtk_file_path_free (chooser_entry->base_folder);
928
929   chooser_entry->base_folder = gtk_file_path_copy (path);
930
931   gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
932   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, -1);
933 }
934
935 /**
936  * _gtk_file_chooser_entry_get_current_folder:
937  * @chooser_entry: a #GtkFileChooserEntry
938  *
939  * Gets the current folder for the #GtkFileChooserEntry. If the
940  * user has only entered a filename, this will be the base folder
941  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
942  * user has entered a relative or absolute path, then it will
943  * be different. If the user has entered a relative or absolute
944  * path that doesn't point to a folder in the file system, it will
945  * be %NULL.
946  *
947  * Return value: the path of current folder - this value is owned by the
948  *  chooser entry and must not be modified or freed.
949  **/
950 const GtkFilePath *
951 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
952 {
953   if (chooser_entry->has_completion)
954     {
955       gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
956                                  GTK_ENTRY (chooser_entry)->text_length);
957     }
958   return chooser_entry->current_folder_path;
959 }
960
961 /**
962  * _gtk_file_chooser_entry_get_file_part:
963  * @chooser_entry: a #GtkFileChooserEntry
964  *
965  * Gets the non-folder portion of whatever the user has entered
966  * into the file selector. What is returned is a UTF-8 string,
967  * and if a filename path is needed, gtk_file_system_make_path()
968  * must be used
969   *
970  * Return value: the entered filename - this value is owned by the
971  *  chooser entry and must not be modified or freed.
972  **/
973 const gchar *
974 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
975 {
976   if (chooser_entry->has_completion)
977     {
978       gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
979                                  GTK_ENTRY (chooser_entry)->text_length);
980     }
981   return chooser_entry->file_part;
982 }
983
984 /**
985  * _gtk_file_chooser_entry_set_file_part:
986  * @chooser_entry: a #GtkFileChooserEntry
987  * @file_part: text to display in the entry, in UTF-8
988  *
989  * Sets the current text shown in the file chooser entry.
990  **/
991 void
992 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
993                                        const gchar         *file_part)
994 {
995   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
996
997   gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
998 }
999
1000
1001 /**
1002  * _gtk_file_chooser_entry_set_action:
1003  * @chooser_entry: a #GtkFileChooserEntry
1004  * @action: the action which is performed by the file selector using this entry
1005  *
1006  * Sets action which is performed by the file selector using this entry. 
1007  * The #GtkFileChooserEntry will use different completion strategies for 
1008  * different actions.
1009  **/
1010 void
1011 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1012                                     GtkFileChooserAction action)
1013 {
1014   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1015   
1016   if (chooser_entry->action != action)
1017     {
1018       GtkEntryCompletion *comp;
1019
1020       chooser_entry->action = action;
1021
1022       comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1023
1024       switch (action)
1025         {
1026         case GTK_FILE_CHOOSER_ACTION_OPEN:
1027         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1028           gtk_entry_completion_set_popup_single_match (comp, FALSE);
1029           break;
1030         case GTK_FILE_CHOOSER_ACTION_SAVE:
1031         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1032           gtk_entry_completion_set_popup_single_match (comp, TRUE);
1033           break;
1034         }
1035     }
1036 }
1037
1038
1039 /**
1040  * _gtk_file_chooser_entry_get_action:
1041  * @chooser_entry: a #GtkFileChooserEntry
1042  *
1043  * Gets the action for this entry. 
1044  *
1045  * Returns: the action
1046  **/
1047 GtkFileChooserAction
1048 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1049 {
1050   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1051                         GTK_FILE_CHOOSER_ACTION_OPEN);
1052   
1053   return chooser_entry->action;
1054 }
1055
1056 gboolean
1057 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1058                                        const GtkFilePath   *path)
1059 {
1060   gboolean retval = FALSE;
1061
1062   if (chooser_entry->current_folder)
1063     {
1064       GtkFileInfo *file_info;
1065
1066       file_info = gtk_file_folder_get_info (chooser_entry->current_folder,
1067                                             path, NULL);
1068       if (file_info)
1069         {
1070           retval = gtk_file_info_get_is_folder (file_info);
1071           gtk_file_info_free (file_info);
1072         }
1073     }
1074
1075   return retval;
1076 }