]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
s/seperator/separator/.
[~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   GSource *check_completion_idle;
50   GSource *load_directory_idle;
51
52   GtkFileFolder *current_folder;
53
54   GtkListStore *completion_store;
55
56   guint has_completion : 1;
57   guint in_change      : 1;
58   guint no_pop_down    : 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   gint total_len;
237   gint file_part_len;
238   gint selected_part_len;
239   GtkFilePath *path;
240   gint pos;
241   const gchar *text;
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   /* We have to jump through hoops to figure out where to append this to */
259   text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
260   total_len = g_utf8_strlen (text, -1);
261   file_part_len = g_utf8_strlen (chooser_entry->file_part, -1);
262
263   selected_part_len = 0;
264
265   if (chooser_entry->has_completion)
266     {
267       gint sel_start, sel_end;
268
269       /* Gosh, there should be a more efficient way of doing this... */
270       if (gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry),
271                                              &sel_start, &sel_end))
272         {
273           gchar *str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry),
274                                                sel_start, sel_end);
275           selected_part_len = g_utf8_strlen (str, -1);
276           g_free (str);
277         }
278     }
279
280   pos = total_len - (file_part_len + selected_part_len); 
281
282   /* We don't set in_change here as we want to update the current_folder
283    * variable */
284   gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
285                             pos, -1);
286   gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
287                             display_name, -1, 
288                             &pos);
289   gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
290
291   gtk_file_path_free (path);
292   g_free (display_name);
293
294   /* We surpress the completion for a second.  It's confusing to click on an
295    * entry and have it pop back up immediately */
296   chooser_entry->no_pop_down = TRUE;
297
298   return TRUE;
299 }
300
301 /* Match function for the GtkEntryCompletion */
302 static gboolean
303 completion_match_func (GtkEntryCompletion *comp,
304                        const char         *key_unused,
305                        GtkTreeIter        *iter,
306                        gpointer            data)
307 {
308   GtkFileChooserEntry *chooser_entry;
309   char *name;
310   gboolean result;
311   char *norm_file_part;
312   char *norm_name;
313
314   chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
315
316   /* We ignore the key because it is the contents of the entry.  Instead, we
317    * just use our precomputed file_part.
318    */
319   if (!chooser_entry->file_part)
320     {
321       return FALSE;
322     }
323
324   /* If this is set, than someone is surpressing the popdown temporarily */
325   if (chooser_entry->no_pop_down)
326     return FALSE;
327
328   gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, 0, &name, -1);
329   if (!name)
330     {
331       return FALSE; /* Uninitialized row, ugh */
332     }
333
334   /* If we have an empty file_part, then we're at the root of a directory.  In
335    * that case, we want to match all non-dot files.  We might want to match
336    * dot_files too if show_hidden is TRUE on the fileselector in the future.
337    */
338   /* Additionally, support for gnome .hidden files would be sweet, too */
339   if (chooser_entry->file_part[0] == '\000')
340     {
341       if (name[0] == '.')
342         result = FALSE;
343       else
344         result = TRUE;
345       g_free (name);
346
347       return result;
348     }
349
350
351   norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
352   norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
353
354   result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
355
356   g_free (norm_file_part);
357   g_free (norm_name);
358   g_free (name);
359   
360   return result;
361 }
362
363 /* This function will append a '/' character to paths to display_name iff the
364  * path associated with it is a directory.  maybe_append_separator_to_path will
365  * g_free the display_name and return a new one if needed.  Otherwise, it will
366  * return the old one.  You should be safe calling
367  *
368  * display_name = maybe_append_separator_to_path (entry, path, display_name);
369  * ...
370  * g_free (display_name);
371  */
372 static char *
373 maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
374                                 GtkFilePath         *path,
375                                 gchar               *display_name)
376 {
377   if (path)
378     {
379       GtkFileInfo *info;
380             
381       info = gtk_file_folder_get_info (chooser_entry->current_folder,
382                                        path, NULL); /* NULL-GError */
383
384       if (info)
385         {
386           if (gtk_file_info_get_is_folder (info))
387             {
388               gchar *tmp = display_name;
389               display_name = g_strconcat (tmp, "/", NULL);
390               g_free (tmp);
391             }
392           
393           gtk_file_info_free (info);
394         }
395
396     }
397
398   return display_name;
399 }
400
401 static gboolean
402 check_completion_callback (GtkFileChooserEntry *chooser_entry)
403 {
404   GtkTreeIter iter;
405   gchar *common_prefix = NULL;
406   GtkFilePath *unique_path = NULL;
407   gboolean valid;
408
409   g_assert (chooser_entry->file_part);
410
411   chooser_entry->check_completion_idle = NULL;
412
413   if (strcmp (chooser_entry->file_part, "") == 0)
414     return FALSE;
415
416   if (chooser_entry->completion_store == NULL)
417     return FALSE;
418
419   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store),
420                                          &iter);
421
422   while (valid)
423     {
424       gchar *display_name;
425       GtkFilePath *path;
426
427       gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
428                           &iter,
429                           DISPLAY_NAME_COLUMN, &display_name,
430                           PATH_COLUMN, &path,
431                           -1);
432
433       if (g_str_has_prefix (display_name, chooser_entry->file_part))
434         {
435           if (!common_prefix)
436             {
437               common_prefix = g_strdup (display_name);
438               unique_path = gtk_file_path_copy (path);
439             }
440           else
441             {
442               gchar *p = common_prefix;
443               const gchar *q = display_name;
444                   
445               while (*p && *p == *q)
446                 {
447                   p++;
448                   q++;
449                 }
450                   
451               *p = '\0';
452
453               gtk_file_path_free (unique_path);
454               unique_path = NULL;
455             }
456         }
457
458       g_free (display_name);
459       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store),
460                                         &iter);
461     }
462
463   if (unique_path)
464     {
465       common_prefix = maybe_append_separator_to_path (chooser_entry,
466                                                       unique_path,
467                                                       common_prefix);
468       gtk_file_path_free (unique_path);
469     }
470
471   if (common_prefix)
472     {
473       gint total_len;
474       gint file_part_len;
475       gint common_prefix_len;
476       gint pos;
477
478       total_len = g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry)), -1);
479       file_part_len = g_utf8_strlen (chooser_entry->file_part, -1);
480       common_prefix_len = g_utf8_strlen (common_prefix, -1);
481
482       if (common_prefix_len > file_part_len)
483         {
484           pos = total_len - file_part_len;
485
486           chooser_entry->in_change = TRUE;
487           gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
488                                     pos, -1);
489           gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
490                                     common_prefix, -1, 
491                                     &pos);
492           gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
493                                       total_len,
494                                       total_len - file_part_len + common_prefix_len);
495           chooser_entry->in_change = FALSE;
496
497           chooser_entry->has_completion = TRUE;
498         }
499           
500       g_free (common_prefix);
501     }
502
503   return FALSE;
504 }
505
506 static void
507 add_completion_idle (GtkFileChooserEntry *chooser_entry)
508 {
509   /* idle to update the selection based on the file list */
510   if (chooser_entry->check_completion_idle == NULL)
511     {
512       chooser_entry->check_completion_idle = g_idle_source_new ();
513       g_source_set_priority (chooser_entry->check_completion_idle, G_PRIORITY_HIGH);
514       g_source_set_closure (chooser_entry->check_completion_idle,
515                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
516                                                    G_OBJECT (chooser_entry)));
517       g_source_attach (chooser_entry->check_completion_idle, NULL);
518     }
519 }
520
521
522 static void
523 update_current_folder_files (GtkFileChooserEntry *chooser_entry,
524                              GSList              *added_uris)
525 {
526   GSList *tmp_list;
527
528   g_assert (chooser_entry->completion_store != NULL);
529
530   /* Bah.  Need to turn off sorting */
531   for (tmp_list = added_uris; tmp_list; tmp_list = tmp_list->next)
532     {
533       GtkFileInfo *info;
534       GtkFilePath *path;
535
536       path = tmp_list->data;
537
538       info = gtk_file_folder_get_info (chooser_entry->current_folder,
539                                        path,
540                                        NULL); /* NULL-GError */
541       if (info)
542         {
543           const gchar *display_name = gtk_file_info_get_display_name (info);
544           GtkTreeIter iter;
545
546           gtk_list_store_append (chooser_entry->completion_store, &iter);
547           gtk_list_store_set (chooser_entry->completion_store, &iter,
548                               DISPLAY_NAME_COLUMN, display_name,
549                               PATH_COLUMN, path,
550                               -1);
551
552           gtk_file_info_free (info);
553         }
554     }
555
556   /* FIXME: we want to turn off sorting temporarily.  I suck... */
557   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
558                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
559
560   add_completion_idle (chooser_entry);
561 }
562
563 static void
564 files_added_cb (GtkFileSystem       *file_system,
565                 GSList              *added_uris,
566                 GtkFileChooserEntry *chooser_entry)
567 {
568   update_current_folder_files (chooser_entry, added_uris);
569 }
570
571 static void
572 files_deleted_cb (GtkFileSystem       *file_system,
573                   GSList              *deleted_uris,
574                   GtkFileChooserEntry *chooser_entry)
575 {
576   /* FIXME: gravy... */
577 }
578
579 static gboolean
580 load_directory_callback (GtkFileChooserEntry *chooser_entry)
581 {
582   GSList *child_paths = NULL;
583
584   chooser_entry->load_directory_idle = NULL;
585
586   /* guard against bogus settings*/
587   if (chooser_entry->current_folder_path == NULL ||
588       chooser_entry->file_system == NULL)
589     return FALSE;
590
591   if (chooser_entry->current_folder != NULL)
592     {
593       g_warning ("idle activate multiple times without clearing the folder object first.");
594       return FALSE;
595     }
596   g_assert (chooser_entry->completion_store == NULL);
597
598   /* Load the folder */
599   chooser_entry->current_folder = gtk_file_system_get_folder (chooser_entry->file_system,
600                                                               chooser_entry->current_folder_path,
601                                                               GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
602                                                               NULL); /* NULL-GError */
603
604   /* There is no folder by that name */
605   if (!chooser_entry->current_folder)
606     return FALSE;
607   g_signal_connect (chooser_entry->current_folder, "files-added",
608                     G_CALLBACK (files_added_cb), chooser_entry);
609   g_signal_connect (chooser_entry->current_folder, "files-removed",
610                     G_CALLBACK (files_deleted_cb), chooser_entry);
611   
612   gtk_file_folder_list_children (chooser_entry->current_folder,
613                                  &child_paths,
614                                  NULL); /* NULL-GError */
615   chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
616                                                         G_TYPE_STRING,
617                                                         GTK_TYPE_FILE_PATH);
618
619   if (child_paths)
620     {
621       update_current_folder_files (chooser_entry, child_paths);
622       add_completion_idle (chooser_entry);
623       gtk_file_paths_free (child_paths);
624   }
625
626   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
627                                   GTK_TREE_MODEL (chooser_entry->completion_store));
628   return FALSE;
629 }
630
631 static void
632 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
633                                        const gchar *new_text,
634                                        gint         new_text_length,
635                                        gint        *position)
636 {
637   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
638
639   parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
640
641   if (! chooser_entry->in_change)
642     add_completion_idle (GTK_FILE_CHOOSER_ENTRY (editable));
643 }
644
645 static gboolean
646 gtk_file_chooser_entry_focus (GtkWidget        *widget,
647                               GtkDirectionType  direction)
648 {
649   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
650   GdkModifierType state;
651   gboolean control_pressed = FALSE;
652
653   if (gtk_get_current_event_state (&state))
654     {
655       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
656         control_pressed = TRUE;
657     }
658
659   /* This is a bit evil -- it makes Tab never leave the entry. It basically
660    * makes it 'safe' for people to hit. */
661   if ((direction == GTK_DIR_TAB_FORWARD) &&
662       (GTK_WIDGET_HAS_FOCUS (widget)) &&
663       (! control_pressed))
664     {
665       if (chooser_entry->has_completion)
666         {
667           gtk_editable_set_position (GTK_EDITABLE (widget),
668                                      GTK_ENTRY (widget)->text_length);
669         }
670       return TRUE;
671     }
672   else
673     return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
674 }
675
676 static void
677 gtk_file_chooser_entry_activate (GtkEntry *entry)
678 {
679   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
680
681   if (chooser_entry->has_completion)
682     {
683       gtk_editable_set_position (GTK_EDITABLE (entry),
684                                  entry->text_length);
685     }
686   
687   GTK_ENTRY_CLASS (parent_class)->activate (entry);
688 }
689
690 /* This will see if a path typed by the user is new, and installs the loading
691  * idle if it is.
692  */
693 static void
694 gtk_file_chooser_entry_maybe_update_directory (GtkFileChooserEntry *chooser_entry,
695                                                GtkFilePath         *folder_path)
696 {
697   gboolean queue_idle = FALSE;
698
699   if (chooser_entry->current_folder_path)
700     {
701       if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0)
702         {
703           /* We changed our current directory.  We need to clear out the old
704            * directory information.
705            */
706           if (chooser_entry->current_folder)
707             {
708               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
709                                                     G_CALLBACK (files_added_cb), chooser_entry);
710               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
711                                                     G_CALLBACK (files_deleted_cb), chooser_entry);
712
713               g_object_unref (chooser_entry->current_folder);
714               chooser_entry->current_folder = NULL;
715             }
716           if (chooser_entry->completion_store)
717             {
718               gtk_list_store_clear (GTK_LIST_STORE (chooser_entry->completion_store));
719               /* FIXME: Uncomment this line and get rid of the _clear above
720                * after #137211 is fixed */
721               /* gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);*/
722               g_object_unref (chooser_entry->completion_store);
723               chooser_entry->completion_store = NULL;
724             }
725
726           queue_idle = TRUE;
727         }
728
729       gtk_file_path_free (chooser_entry->current_folder_path);
730     }
731   else
732     {
733       queue_idle = TRUE;
734     }
735
736   chooser_entry->current_folder_path = folder_path;
737
738   if (queue_idle && chooser_entry->load_directory_idle == NULL)
739     {
740       chooser_entry->load_directory_idle = g_idle_source_new ();
741       g_source_set_priority (chooser_entry->load_directory_idle, G_PRIORITY_HIGH);
742       g_source_set_closure (chooser_entry->load_directory_idle,
743                             g_cclosure_new_object (G_CALLBACK (load_directory_callback),
744                                                    G_OBJECT (chooser_entry)));
745       g_source_attach (chooser_entry->load_directory_idle, NULL);
746     }
747 }
748
749
750
751 static void
752 gtk_file_chooser_entry_changed (GtkEditable *editable)
753 {
754   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
755   const gchar *text;
756   GtkFilePath *folder_path;
757   gchar *file_part;
758
759   if (chooser_entry->in_change)
760     return;
761
762   text = gtk_entry_get_text (GTK_ENTRY (editable));
763   
764   if (!chooser_entry->file_system ||
765       !chooser_entry->base_folder ||
766       !gtk_file_system_parse (chooser_entry->file_system,
767                               chooser_entry->base_folder, text,
768                               &folder_path, &file_part, NULL)) /* NULL-GError */
769     {
770       folder_path = gtk_file_path_copy (chooser_entry->base_folder);
771       file_part = g_strdup ("");
772     }
773
774   gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path);
775
776   if (chooser_entry->file_part)
777     g_free (chooser_entry->file_part);
778
779   chooser_entry->file_part = file_part;
780 }
781
782 static void
783 clear_completion_callback (GtkFileChooserEntry *chooser_entry,
784                            GParamSpec          *pspec)
785 {
786   if (chooser_entry->has_completion)
787     {
788       chooser_entry->has_completion = FALSE;
789       gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
790     }
791   chooser_entry->no_pop_down = FALSE;
792 }
793
794 /**
795  * _gtk_file_chooser_entry_new:
796  *
797  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
798  * is an internal implementation widget for the GTK+ file chooser
799  * which is an entry with completion with respect to a
800  * #GtkFileSystem object.
801  *
802  * Return value: the newly created #GtkFileChooserEntry
803  **/
804 GtkWidget *
805 _gtk_file_chooser_entry_new (void)
806 {
807   return g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
808 }
809
810 /**
811  * _gtk_file_chooser_entry_set_file_system:
812  * @chooser_entry: a #GtkFileChooser
813  * @file_system: an object implementing #GtkFileSystem
814  *
815  * Sets the file system for @chooser_entry.
816  **/
817 void
818 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
819                                          GtkFileSystem       *file_system)
820 {
821   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
822   g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
823
824   if (file_system != chooser_entry->file_system)
825     {
826       if (chooser_entry->file_system)
827         g_object_unref (chooser_entry->file_system);
828
829       chooser_entry->file_system = g_object_ref (file_system);
830     }
831 }
832
833 /**
834  * _gtk_file_chooser_entry_set_base_folder:
835  * @chooser_entry: a #GtkFileChooserEntry
836  * @path: path of a folder in the chooser entries current file system.
837  *
838  * Sets the folder with respect to which completions occur.
839  **/
840 void
841 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
842                                          const GtkFilePath   *path)
843 {
844   if (chooser_entry->base_folder)
845     gtk_file_path_free (chooser_entry->base_folder);
846
847   chooser_entry->base_folder = gtk_file_path_copy (path);
848
849   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, -1);
850 }
851
852 /**
853  * _gtk_file_chooser_entry_get_current_folder:
854  * @chooser_entry: a #GtkFileChooserEntry
855  *
856  * Gets the current folder for the #GtkFileChooserEntry. If the
857  * user has only entered a filename, this will be the base folder
858  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
859  * user has entered a relative or absolute path, then it will
860  * be different. If the user has entered a relative or absolute
861  * path that doesn't point to a folder in the file system, it will
862  * be %NULL.
863  *
864  * Return value: the path of current folder - this value is owned by the
865  *  chooser entry and must not be modified or freed.
866  **/
867 const GtkFilePath *
868 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
869 {
870   return chooser_entry->current_folder_path;
871 }
872
873 /**
874  * _gtk_file_chooser_entry_get_file_part:
875  * @chooser_entry: a #GtkFileChooserEntry
876  *
877  * Gets the non-folder portion of whatever the user has entered
878  * into the file selector. What is returned is a UTF-8 string,
879  * and if a filename path is needed, gtk_file_system_make_path()
880  * must be used
881   *
882  * Return value: the entered filename - this value is owned by the
883  *  chooser entry and must not be modified or freed.
884  **/
885 const gchar *
886 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
887 {
888   return chooser_entry->file_part;
889 }
890
891 /**
892  * _gtk_file_chooser_entry_set_file_part:
893  * @chooser_entry: a #GtkFileChooserEntry
894  * @file_part: text to display in the entry, in UTF-8
895  *
896  * Sets the current text shown in the file chooser entry.
897  **/
898 void
899 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
900                                        const gchar         *file_part)
901 {
902   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
903
904   gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
905 }