]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
Block the ::changed handler during the emission of ::match-selected.
[~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
30 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
31
32 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
33 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
34 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
35
36 struct _GtkFileChooserEntryClass
37 {
38   GtkEntryClass parent_class;
39 };
40
41 struct _GtkFileChooserEntry
42 {
43   GtkEntry parent_instance;
44
45   GtkFileSystem *file_system;
46   GtkFilePath *base_folder;
47   GtkFilePath *current_folder_path;
48   gchar *file_part;
49   gint file_part_pos;
50   GSource *check_completion_idle;
51   GSource *load_directory_idle;
52
53   GtkFileFolder *current_folder;
54
55   GtkListStore *completion_store;
56
57   guint has_completion : 1;
58   guint in_change      : 1;
59 };
60
61 enum
62 {
63   DISPLAY_NAME_COLUMN,
64   PATH_COLUMN,
65   N_COLUMNS
66 };
67
68 static void gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class);
69 static void gtk_file_chooser_entry_iface_init (GtkEditableClass         *iface);
70 static void gtk_file_chooser_entry_init       (GtkFileChooserEntry      *chooser_entry);
71
72 static void     gtk_file_chooser_entry_finalize       (GObject          *object);
73 static gboolean gtk_file_chooser_entry_focus          (GtkWidget        *widget,
74                                                        GtkDirectionType  direction);
75 static void     gtk_file_chooser_entry_activate       (GtkEntry         *entry);
76 static void     gtk_file_chooser_entry_changed        (GtkEditable      *editable);
77 static void     gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
78                                                        const gchar *new_text,
79                                                        gint         new_text_length,
80                                                        gint        *position);
81
82 static void     clear_completion_callback (GtkFileChooserEntry *chooser_entry,
83                                            GParamSpec          *pspec);
84 static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
85                                            GtkTreeModel        *model,
86                                            GtkTreeIter         *iter,
87                                            GtkFileChooserEntry *chooser_entry);
88 static gboolean completion_match_func     (GtkEntryCompletion  *comp,
89                                            const char          *key,
90                                            GtkTreeIter         *iter,
91                                            gpointer             data);
92 static void     files_added_cb            (GtkFileSystem       *file_system,
93                                            GSList              *added_uris,
94                                            GtkFileChooserEntry *chooser_entry);
95 static void     files_deleted_cb          (GtkFileSystem       *file_system,
96                                            GSList              *deleted_uris,
97                                            GtkFileChooserEntry *chooser_entry);
98 static char    *maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
99                                                 GtkFilePath         *path,
100                                                 gchar               *display_name);
101
102
103 static GObjectClass *parent_class;
104 static GtkEditableClass *parent_editable_iface;
105
106 GType
107 _gtk_file_chooser_entry_get_type (void)
108 {
109   static GType file_chooser_entry_type = 0;
110
111   if (!file_chooser_entry_type)
112     {
113       static const GTypeInfo file_chooser_entry_info =
114       {
115         sizeof (GtkFileChooserEntryClass),
116         NULL,           /* base_init */
117         NULL,           /* base_finalize */
118         (GClassInitFunc) gtk_file_chooser_entry_class_init,
119         NULL,           /* class_finalize */
120         NULL,           /* class_data */
121         sizeof (GtkFileChooserEntry),
122         0,              /* n_preallocs */
123         (GInstanceInitFunc) gtk_file_chooser_entry_init,
124       };
125
126       static const GInterfaceInfo editable_info =
127       {
128         (GInterfaceInitFunc) gtk_file_chooser_entry_iface_init, /* interface_init */
129         NULL,                                                 /* interface_finalize */
130         NULL                                                  /* interface_data */
131       };
132
133
134       file_chooser_entry_type = g_type_register_static (GTK_TYPE_ENTRY, "GtkFileChooserEntry",
135                                                         &file_chooser_entry_info, 0);
136       g_type_add_interface_static (file_chooser_entry_type,
137                                    GTK_TYPE_EDITABLE,
138                                    &editable_info);
139     }
140
141
142   return file_chooser_entry_type;
143 }
144
145 static void
146 gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
147 {
148   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
149   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
150   GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
151
152   parent_class = g_type_class_peek_parent (class);
153
154   gobject_class->finalize = gtk_file_chooser_entry_finalize;
155
156   widget_class->focus = gtk_file_chooser_entry_focus;
157
158   entry_class->activate = gtk_file_chooser_entry_activate;
159 }
160
161 static void
162 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
163 {
164   parent_editable_iface = g_type_interface_peek_parent (iface);
165
166   iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
167   iface->changed = gtk_file_chooser_entry_changed;
168 }
169
170 static void
171 gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
172 {
173   GtkEntryCompletion *comp;
174   GtkCellRenderer *cell;
175
176   comp = gtk_entry_completion_new ();
177   gtk_entry_completion_set_match_func (comp,
178                                        completion_match_func,
179                                        chooser_entry,
180                                        NULL);
181
182   cell = gtk_cell_renderer_text_new ();
183   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
184                               cell, TRUE);
185   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
186                                  cell,
187                                  "text", 0);
188
189   g_signal_connect (comp, "match-selected",
190                     G_CALLBACK (match_selected_callback), chooser_entry);
191
192   gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
193   g_object_unref (comp);
194
195   g_signal_connect (chooser_entry, "notify::cursor-position",
196                     G_CALLBACK (clear_completion_callback), NULL);
197   g_signal_connect (chooser_entry, "notify::selection-bound",
198                     G_CALLBACK (clear_completion_callback), NULL);
199 }
200
201 static void
202 gtk_file_chooser_entry_finalize (GObject *object)
203 {
204   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
205
206   if (chooser_entry->completion_store)
207     g_object_unref (chooser_entry->completion_store);
208
209   if (chooser_entry->current_folder)
210     {
211       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
212                                             G_CALLBACK (files_added_cb), chooser_entry);
213       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
214                                             G_CALLBACK (files_deleted_cb), chooser_entry);
215       g_object_unref (chooser_entry->current_folder);
216     }
217
218   if (chooser_entry->file_system)
219     g_object_unref (chooser_entry->file_system);
220
221   gtk_file_path_free (chooser_entry->base_folder);
222   gtk_file_path_free (chooser_entry->current_folder_path);
223   g_free (chooser_entry->file_part);
224
225   parent_class->finalize (object);
226 }
227
228 /* Match functions for the GtkEntryCompletion */
229 static gboolean
230 match_selected_callback (GtkEntryCompletion  *completion,
231                          GtkTreeModel        *model,
232                          GtkTreeIter         *iter,
233                          GtkFileChooserEntry *chooser_entry)
234 {
235   char *display_name;
236   GtkFilePath *path;
237   gint pos;
238   
239   gtk_tree_model_get (model, iter,
240                       DISPLAY_NAME_COLUMN, &display_name,
241                       PATH_COLUMN, &path,
242                       -1);
243
244   if (!display_name || !path)
245     {
246       /* these shouldn't complain if passed NULL */
247       gtk_file_path_free (path);
248       g_free (display_name);
249       return FALSE;
250     }
251
252   display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
253
254   pos = chooser_entry->file_part_pos;
255
256   /* We don't set in_change here as we want to update the current_folder
257    * variable */
258   gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
259                             pos, -1);
260   gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
261                             display_name, -1, 
262                             &pos);
263   gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
264
265   gtk_file_path_free (path);
266   g_free (display_name);
267
268   return TRUE;
269 }
270
271 /* Match function for the GtkEntryCompletion */
272 static gboolean
273 completion_match_func (GtkEntryCompletion *comp,
274                        const char         *key_unused,
275                        GtkTreeIter        *iter,
276                        gpointer            data)
277 {
278   GtkFileChooserEntry *chooser_entry;
279   char *name;
280   gboolean result;
281   char *norm_file_part;
282   char *norm_name;
283
284   chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
285
286   /* We ignore the key because it is the contents of the entry.  Instead, we
287    * just use our precomputed file_part.
288    */
289   if (!chooser_entry->file_part)
290     {
291       return FALSE;
292     }
293
294   gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
295   if (!name)
296     {
297       return FALSE; /* Uninitialized row, ugh */
298     }
299
300   /* If we have an empty file_part, then we're at the root of a directory.  In
301    * that case, we want to match all non-dot files.  We might want to match
302    * dot_files too if show_hidden is TRUE on the fileselector in the future.
303    */
304   /* Additionally, support for gnome .hidden files would be sweet, too */
305   if (chooser_entry->file_part[0] == '\000')
306     {
307       if (name[0] == '.')
308         result = FALSE;
309       else
310         result = TRUE;
311       g_free (name);
312
313       return result;
314     }
315
316
317   norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
318   norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
319
320   result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
321
322   g_free (norm_file_part);
323   g_free (norm_name);
324   g_free (name);
325   
326   return result;
327 }
328
329 /* This function will append a '/' character to paths to display_name iff the
330  * path associated with it is a directory.  maybe_append_separator_to_path will
331  * g_free the display_name and return a new one if needed.  Otherwise, it will
332  * return the old one.  You should be safe calling
333  *
334  * display_name = maybe_append_separator_to_path (entry, path, display_name);
335  * ...
336  * g_free (display_name);
337  */
338 static char *
339 maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
340                                 GtkFilePath         *path,
341                                 gchar               *display_name)
342 {
343   if (path)
344     {
345       GtkFileInfo *info;
346             
347       info = gtk_file_folder_get_info (chooser_entry->current_folder,
348                                        path, NULL); /* NULL-GError */
349
350       if (info)
351         {
352           if (gtk_file_info_get_is_folder (info))
353             {
354               gchar *tmp = display_name;
355               display_name = g_strconcat (tmp, "/", NULL);
356               g_free (tmp);
357             }
358           
359           gtk_file_info_free (info);
360         }
361
362     }
363
364   return display_name;
365 }
366
367 static gboolean
368 check_completion_callback (GtkFileChooserEntry *chooser_entry)
369 {
370   GtkTreeIter iter;
371   gchar *common_prefix = NULL;
372   GtkFilePath *unique_path = NULL;
373   gboolean valid;
374
375   g_assert (chooser_entry->file_part);
376
377   chooser_entry->check_completion_idle = NULL;
378
379   if (strcmp (chooser_entry->file_part, "") == 0)
380     return FALSE;
381
382   if (chooser_entry->completion_store == NULL)
383     return FALSE;
384
385   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store),
386                                          &iter);
387
388   while (valid)
389     {
390       gchar *display_name;
391       GtkFilePath *path;
392
393       gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
394                           &iter,
395                           DISPLAY_NAME_COLUMN, &display_name,
396                           PATH_COLUMN, &path,
397                           -1);
398
399       if (g_str_has_prefix (display_name, chooser_entry->file_part))
400         {
401           if (!common_prefix)
402             {
403               common_prefix = g_strdup (display_name);
404               unique_path = gtk_file_path_copy (path);
405             }
406           else
407             {
408               gchar *p = common_prefix;
409               const gchar *q = display_name;
410                   
411               while (*p && *p == *q)
412                 {
413                   p++;
414                   q++;
415                 }
416                   
417               *p = '\0';
418
419               gtk_file_path_free (unique_path);
420               unique_path = NULL;
421             }
422         }
423
424       g_free (display_name);
425       gtk_file_path_free (path);
426       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store),
427                                         &iter);
428     }
429
430   if (unique_path)
431     {
432       common_prefix = maybe_append_separator_to_path (chooser_entry,
433                                                       unique_path,
434                                                       common_prefix);
435       gtk_file_path_free (unique_path);
436     }
437
438   if (common_prefix)
439     {
440       gint file_part_len;
441       gint common_prefix_len;
442       gint pos;
443
444       file_part_len = g_utf8_strlen (chooser_entry->file_part, -1);
445       common_prefix_len = g_utf8_strlen (common_prefix, -1);
446
447       if (common_prefix_len > file_part_len)
448         {
449           pos = chooser_entry->file_part_pos;
450
451           chooser_entry->in_change = TRUE;
452           gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
453                                     pos, -1);
454           gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
455                                     common_prefix, -1, 
456                                     &pos);
457           gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
458                                       chooser_entry->file_part_pos + file_part_len,
459                                       chooser_entry->file_part_pos + common_prefix_len);
460           chooser_entry->in_change = FALSE;
461
462           chooser_entry->has_completion = TRUE;
463         }
464           
465       g_free (common_prefix);
466     }
467
468   return FALSE;
469 }
470
471 static void
472 add_completion_idle (GtkFileChooserEntry *chooser_entry)
473 {
474   /* idle to update the selection based on the file list */
475   if (chooser_entry->check_completion_idle == NULL)
476     {
477       chooser_entry->check_completion_idle = g_idle_source_new ();
478       g_source_set_priority (chooser_entry->check_completion_idle, G_PRIORITY_HIGH);
479       g_source_set_closure (chooser_entry->check_completion_idle,
480                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
481                                                    G_OBJECT (chooser_entry)));
482       g_source_attach (chooser_entry->check_completion_idle, NULL);
483     }
484 }
485
486
487 static void
488 update_current_folder_files (GtkFileChooserEntry *chooser_entry,
489                              GSList              *added_uris)
490 {
491   GSList *tmp_list;
492
493   g_assert (chooser_entry->completion_store != NULL);
494
495   /* Bah.  Need to turn off sorting */
496   for (tmp_list = added_uris; tmp_list; tmp_list = tmp_list->next)
497     {
498       GtkFileInfo *info;
499       GtkFilePath *path;
500
501       path = tmp_list->data;
502
503       info = gtk_file_folder_get_info (chooser_entry->current_folder,
504                                        path,
505                                        NULL); /* NULL-GError */
506       if (info)
507         {
508           const gchar *display_name = gtk_file_info_get_display_name (info);
509           GtkTreeIter iter;
510
511           gtk_list_store_append (chooser_entry->completion_store, &iter);
512           gtk_list_store_set (chooser_entry->completion_store, &iter,
513                               DISPLAY_NAME_COLUMN, display_name,
514                               PATH_COLUMN, path,
515                               -1);
516
517           gtk_file_info_free (info);
518         }
519     }
520
521   /* FIXME: we want to turn off sorting temporarily.  I suck... */
522   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
523                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
524
525   add_completion_idle (chooser_entry);
526 }
527
528 static void
529 files_added_cb (GtkFileSystem       *file_system,
530                 GSList              *added_uris,
531                 GtkFileChooserEntry *chooser_entry)
532 {
533   update_current_folder_files (chooser_entry, added_uris);
534 }
535
536 static void
537 files_deleted_cb (GtkFileSystem       *file_system,
538                   GSList              *deleted_uris,
539                   GtkFileChooserEntry *chooser_entry)
540 {
541   /* FIXME: gravy... */
542 }
543
544 static gboolean
545 load_directory_callback (GtkFileChooserEntry *chooser_entry)
546 {
547   GSList *child_paths = NULL;
548
549   chooser_entry->load_directory_idle = NULL;
550
551   /* guard against bogus settings*/
552   if (chooser_entry->current_folder_path == NULL ||
553       chooser_entry->file_system == NULL)
554     return FALSE;
555
556   if (chooser_entry->current_folder != NULL)
557     {
558       g_warning ("idle activate multiple times without clearing the folder object first.");
559       return FALSE;
560     }
561   g_assert (chooser_entry->completion_store == NULL);
562
563   /* Load the folder */
564   chooser_entry->current_folder = gtk_file_system_get_folder (chooser_entry->file_system,
565                                                               chooser_entry->current_folder_path,
566                                                               GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
567                                                               NULL); /* NULL-GError */
568
569   /* There is no folder by that name */
570   if (!chooser_entry->current_folder)
571     return FALSE;
572   g_signal_connect (chooser_entry->current_folder, "files-added",
573                     G_CALLBACK (files_added_cb), chooser_entry);
574   g_signal_connect (chooser_entry->current_folder, "files-removed",
575                     G_CALLBACK (files_deleted_cb), chooser_entry);
576   
577   gtk_file_folder_list_children (chooser_entry->current_folder,
578                                  &child_paths,
579                                  NULL); /* NULL-GError */
580   chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
581                                                         G_TYPE_STRING,
582                                                         GTK_TYPE_FILE_PATH);
583
584   if (child_paths)
585     {
586       update_current_folder_files (chooser_entry, child_paths);
587       add_completion_idle (chooser_entry);
588       gtk_file_paths_free (child_paths);
589   }
590
591   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
592                                   GTK_TREE_MODEL (chooser_entry->completion_store));
593   return FALSE;
594 }
595
596 static void
597 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
598                                        const gchar *new_text,
599                                        gint         new_text_length,
600                                        gint        *position)
601 {
602   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
603
604   parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
605
606   if (! chooser_entry->in_change)
607     add_completion_idle (GTK_FILE_CHOOSER_ENTRY (editable));
608 }
609
610 static gboolean
611 gtk_file_chooser_entry_focus (GtkWidget        *widget,
612                               GtkDirectionType  direction)
613 {
614   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
615   GdkModifierType state;
616   gboolean control_pressed = FALSE;
617
618   if (gtk_get_current_event_state (&state))
619     {
620       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
621         control_pressed = TRUE;
622     }
623
624   /* This is a bit evil -- it makes Tab never leave the entry. It basically
625    * makes it 'safe' for people to hit. */
626   if ((direction == GTK_DIR_TAB_FORWARD) &&
627       (GTK_WIDGET_HAS_FOCUS (widget)) &&
628       (! control_pressed))
629     {
630       if (chooser_entry->has_completion)
631         {
632           gtk_editable_set_position (GTK_EDITABLE (widget),
633                                      GTK_ENTRY (widget)->text_length);
634         }
635       return TRUE;
636     }
637   else
638     return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
639 }
640
641 static void
642 gtk_file_chooser_entry_activate (GtkEntry *entry)
643 {
644   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
645
646   if (chooser_entry->has_completion)
647     {
648       gtk_editable_set_position (GTK_EDITABLE (entry),
649                                  entry->text_length);
650     }
651   
652   GTK_ENTRY_CLASS (parent_class)->activate (entry);
653 }
654
655 /* This will see if a path typed by the user is new, and installs the loading
656  * idle if it is.
657  */
658 static void
659 gtk_file_chooser_entry_maybe_update_directory (GtkFileChooserEntry *chooser_entry,
660                                                GtkFilePath         *folder_path)
661 {
662   gboolean queue_idle = FALSE;
663
664   if (chooser_entry->current_folder_path)
665     {
666       if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0)
667         {
668           /* We changed our current directory.  We need to clear out the old
669            * directory information.
670            */
671           if (chooser_entry->current_folder)
672             {
673               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
674                                                     G_CALLBACK (files_added_cb), chooser_entry);
675               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
676                                                     G_CALLBACK (files_deleted_cb), chooser_entry);
677
678               g_object_unref (chooser_entry->current_folder);
679               chooser_entry->current_folder = NULL;
680             }
681           if (chooser_entry->completion_store)
682             {
683               gtk_list_store_clear (GTK_LIST_STORE (chooser_entry->completion_store));
684               /* FIXME: Uncomment this line and get rid of the _clear above
685                * after #137211 is fixed */
686               /* gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);*/
687               g_object_unref (chooser_entry->completion_store);
688               chooser_entry->completion_store = NULL;
689             }
690
691           queue_idle = TRUE;
692         }
693
694       gtk_file_path_free (chooser_entry->current_folder_path);
695     }
696   else
697     {
698       queue_idle = TRUE;
699     }
700
701   chooser_entry->current_folder_path = folder_path;
702
703   if (queue_idle && chooser_entry->load_directory_idle == NULL)
704     {
705       chooser_entry->load_directory_idle = g_idle_source_new ();
706       g_source_set_priority (chooser_entry->load_directory_idle, G_PRIORITY_HIGH);
707       g_source_set_closure (chooser_entry->load_directory_idle,
708                             g_cclosure_new_object (G_CALLBACK (load_directory_callback),
709                                                    G_OBJECT (chooser_entry)));
710       g_source_attach (chooser_entry->load_directory_idle, NULL);
711     }
712 }
713
714
715
716 static void
717 gtk_file_chooser_entry_changed (GtkEditable *editable)
718 {
719   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
720   const gchar *text;
721   GtkFilePath *folder_path;
722   gchar *file_part;
723   gsize total_len, file_part_len;
724   gint file_part_pos;
725
726   if (chooser_entry->in_change)
727     return;
728
729   text = gtk_entry_get_text (GTK_ENTRY (editable));
730   
731   if (!chooser_entry->file_system ||
732       !chooser_entry->base_folder ||
733       !gtk_file_system_parse (chooser_entry->file_system,
734                               chooser_entry->base_folder, text,
735                               &folder_path, &file_part, NULL)) /* NULL-GError */
736     {
737       folder_path = gtk_file_path_copy (chooser_entry->base_folder);
738       file_part = g_strdup ("");
739     }
740
741   file_part_len = strlen (file_part);
742   total_len = strlen (text);
743   if (total_len > file_part_len)
744     file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
745   else
746     file_part_pos = 0;
747
748   gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path);
749
750   if (chooser_entry->file_part)
751     g_free (chooser_entry->file_part);
752
753   chooser_entry->file_part = file_part;
754   chooser_entry->file_part_pos = file_part_pos;
755 }
756
757 static void
758 clear_completion_callback (GtkFileChooserEntry *chooser_entry,
759                            GParamSpec          *pspec)
760 {
761   if (chooser_entry->has_completion)
762     {
763       chooser_entry->has_completion = FALSE;
764       gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
765     }
766 }
767
768 /**
769  * _gtk_file_chooser_entry_new:
770  *
771  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
772  * is an internal implementation widget for the GTK+ file chooser
773  * which is an entry with completion with respect to a
774  * #GtkFileSystem object.
775  *
776  * Return value: the newly created #GtkFileChooserEntry
777  **/
778 GtkWidget *
779 _gtk_file_chooser_entry_new (void)
780 {
781   return g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
782 }
783
784 /**
785  * _gtk_file_chooser_entry_set_file_system:
786  * @chooser_entry: a #GtkFileChooser
787  * @file_system: an object implementing #GtkFileSystem
788  *
789  * Sets the file system for @chooser_entry.
790  **/
791 void
792 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
793                                          GtkFileSystem       *file_system)
794 {
795   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
796   g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
797
798   if (file_system != chooser_entry->file_system)
799     {
800       if (chooser_entry->file_system)
801         g_object_unref (chooser_entry->file_system);
802
803       chooser_entry->file_system = g_object_ref (file_system);
804     }
805 }
806
807 /**
808  * _gtk_file_chooser_entry_set_base_folder:
809  * @chooser_entry: a #GtkFileChooserEntry
810  * @path: path of a folder in the chooser entries current file system.
811  *
812  * Sets the folder with respect to which completions occur.
813  **/
814 void
815 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
816                                          const GtkFilePath   *path)
817 {
818   if (chooser_entry->base_folder)
819     gtk_file_path_free (chooser_entry->base_folder);
820
821   chooser_entry->base_folder = gtk_file_path_copy (path);
822
823   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, -1);
824 }
825
826 /**
827  * _gtk_file_chooser_entry_get_current_folder:
828  * @chooser_entry: a #GtkFileChooserEntry
829  *
830  * Gets the current folder for the #GtkFileChooserEntry. If the
831  * user has only entered a filename, this will be the base folder
832  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
833  * user has entered a relative or absolute path, then it will
834  * be different. If the user has entered a relative or absolute
835  * path that doesn't point to a folder in the file system, it will
836  * be %NULL.
837  *
838  * Return value: the path of current folder - this value is owned by the
839  *  chooser entry and must not be modified or freed.
840  **/
841 const GtkFilePath *
842 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
843 {
844   return chooser_entry->current_folder_path;
845 }
846
847 /**
848  * _gtk_file_chooser_entry_get_file_part:
849  * @chooser_entry: a #GtkFileChooserEntry
850  *
851  * Gets the non-folder portion of whatever the user has entered
852  * into the file selector. What is returned is a UTF-8 string,
853  * and if a filename path is needed, gtk_file_system_make_path()
854  * must be used
855   *
856  * Return value: the entered filename - this value is owned by the
857  *  chooser entry and must not be modified or freed.
858  **/
859 const gchar *
860 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
861 {
862   return chooser_entry->file_part;
863 }
864
865 /**
866  * _gtk_file_chooser_entry_set_file_part:
867  * @chooser_entry: a #GtkFileChooserEntry
868  * @file_part: text to display in the entry, in UTF-8
869  *
870  * Sets the current text shown in the file chooser entry.
871  **/
872 void
873 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
874                                        const gchar         *file_part)
875 {
876   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
877
878   gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
879 }