]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
Really handle a NULL model, fixes #137211 for real.
[~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 "gtkalias.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkentry.h"
28 #include "gtkfilechooserentry.h"
29 #include "gtkmain.h"
30
31 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
32
33 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
34 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
35 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
36
37 struct _GtkFileChooserEntryClass
38 {
39   GtkEntryClass parent_class;
40 };
41
42 struct _GtkFileChooserEntry
43 {
44   GtkEntry parent_instance;
45
46   GtkFileChooserAction action;
47
48   GtkFileSystem *file_system;
49   GtkFilePath *base_folder;
50   GtkFilePath *current_folder_path;
51   gchar *file_part;
52   gint file_part_pos;
53   GSource *check_completion_idle;
54   GSource *load_directory_idle;
55
56   GtkFileFolder *current_folder;
57
58   GtkListStore *completion_store;
59
60   guint has_completion : 1;
61   guint in_change      : 1;
62   guint eat_tabs       : 1;
63 };
64
65 enum
66 {
67   DISPLAY_NAME_COLUMN,
68   PATH_COLUMN,
69   N_COLUMNS
70 };
71
72 static void gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class);
73 static void gtk_file_chooser_entry_iface_init (GtkEditableClass         *iface);
74 static void gtk_file_chooser_entry_init       (GtkFileChooserEntry      *chooser_entry);
75
76 static void     gtk_file_chooser_entry_finalize       (GObject          *object);
77 static gboolean gtk_file_chooser_entry_focus          (GtkWidget        *widget,
78                                                        GtkDirectionType  direction);
79 static void     gtk_file_chooser_entry_activate       (GtkEntry         *entry);
80 static void     gtk_file_chooser_entry_changed        (GtkEditable      *editable);
81 static void     gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
82                                                        const gchar *new_text,
83                                                        gint         new_text_length,
84                                                        gint        *position);
85
86 static void     clear_completion_callback (GtkFileChooserEntry *chooser_entry,
87                                            GParamSpec          *pspec);
88 static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
89                                            GtkTreeModel        *model,
90                                            GtkTreeIter         *iter,
91                                            GtkFileChooserEntry *chooser_entry);
92 static gboolean completion_match_func     (GtkEntryCompletion  *comp,
93                                            const char          *key,
94                                            GtkTreeIter         *iter,
95                                            gpointer             data);
96 static void     files_added_cb            (GtkFileSystem       *file_system,
97                                            GSList              *added_uris,
98                                            GtkFileChooserEntry *chooser_entry);
99 static void     files_deleted_cb          (GtkFileSystem       *file_system,
100                                            GSList              *deleted_uris,
101                                            GtkFileChooserEntry *chooser_entry);
102 static char    *maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
103                                                 GtkFilePath         *path,
104                                                 gchar               *display_name);
105
106 static GObjectClass *parent_class;
107 static GtkEditableClass *parent_editable_iface;
108
109 GType
110 _gtk_file_chooser_entry_get_type (void)
111 {
112   static GType file_chooser_entry_type = 0;
113
114   if (!file_chooser_entry_type)
115     {
116       static const GTypeInfo file_chooser_entry_info =
117       {
118         sizeof (GtkFileChooserEntryClass),
119         NULL,           /* base_init */
120         NULL,           /* base_finalize */
121         (GClassInitFunc) gtk_file_chooser_entry_class_init,
122         NULL,           /* class_finalize */
123         NULL,           /* class_data */
124         sizeof (GtkFileChooserEntry),
125         0,              /* n_preallocs */
126         (GInstanceInitFunc) gtk_file_chooser_entry_init,
127       };
128
129       static const GInterfaceInfo editable_info =
130       {
131         (GInterfaceInitFunc) gtk_file_chooser_entry_iface_init, /* interface_init */
132         NULL,                                                 /* interface_finalize */
133         NULL                                                  /* interface_data */
134       };
135
136
137       file_chooser_entry_type = g_type_register_static (GTK_TYPE_ENTRY, "GtkFileChooserEntry",
138                                                         &file_chooser_entry_info, 0);
139       g_type_add_interface_static (file_chooser_entry_type,
140                                    GTK_TYPE_EDITABLE,
141                                    &editable_info);
142     }
143
144
145   return file_chooser_entry_type;
146 }
147
148 static void
149 gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
150 {
151   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
152   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
153   GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
154
155   parent_class = g_type_class_peek_parent (class);
156
157   gobject_class->finalize = gtk_file_chooser_entry_finalize;
158
159   widget_class->focus = gtk_file_chooser_entry_focus;
160
161   entry_class->activate = gtk_file_chooser_entry_activate;
162 }
163
164 static void
165 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
166 {
167   parent_editable_iface = g_type_interface_peek_parent (iface);
168
169   iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
170   iface->changed = gtk_file_chooser_entry_changed;
171 }
172
173 static void
174 gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
175 {
176   GtkEntryCompletion *comp;
177   GtkCellRenderer *cell;
178
179   comp = gtk_entry_completion_new ();
180
181   gtk_entry_completion_set_match_func (comp,
182                                        completion_match_func,
183                                        chooser_entry,
184                                        NULL);
185
186   cell = gtk_cell_renderer_text_new ();
187   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
188                               cell, TRUE);
189   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
190                                  cell,
191                                  "text", 0);
192
193   g_signal_connect (comp, "match-selected",
194                     G_CALLBACK (match_selected_callback), chooser_entry);
195
196   gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
197   g_object_unref (comp);
198
199   g_signal_connect (chooser_entry, "notify::cursor-position",
200                     G_CALLBACK (clear_completion_callback), NULL);
201   g_signal_connect (chooser_entry, "notify::selection-bound",
202                     G_CALLBACK (clear_completion_callback), NULL);
203 }
204
205 static void
206 gtk_file_chooser_entry_finalize (GObject *object)
207 {
208   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
209
210   if (chooser_entry->completion_store)
211     g_object_unref (chooser_entry->completion_store);
212
213   if (chooser_entry->current_folder)
214     {
215       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
216                                             G_CALLBACK (files_added_cb), chooser_entry);
217       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
218                                             G_CALLBACK (files_deleted_cb), chooser_entry);
219       g_object_unref (chooser_entry->current_folder);
220     }
221
222   if (chooser_entry->file_system)
223     g_object_unref (chooser_entry->file_system);
224
225   gtk_file_path_free (chooser_entry->base_folder);
226   gtk_file_path_free (chooser_entry->current_folder_path);
227   g_free (chooser_entry->file_part);
228
229   parent_class->finalize (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 (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
383   return display_name;
384 }
385
386 static gboolean
387 check_completion_callback (GtkFileChooserEntry *chooser_entry)
388 {
389   GtkTreeIter iter;
390   gchar *common_prefix = NULL;
391   GtkFilePath *unique_path = NULL;
392   gboolean valid;
393
394   g_assert (chooser_entry->file_part);
395
396   chooser_entry->check_completion_idle = NULL;
397
398   if (strcmp (chooser_entry->file_part, "") == 0)
399     return FALSE;
400
401   if (chooser_entry->completion_store == NULL)
402     return FALSE;
403
404   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store),
405                                          &iter);
406
407   while (valid)
408     {
409       gchar *display_name;
410       GtkFilePath *path;
411
412       gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
413                           &iter,
414                           DISPLAY_NAME_COLUMN, &display_name,
415                           PATH_COLUMN, &path,
416                           -1);
417
418       if (g_str_has_prefix (display_name, chooser_entry->file_part))
419         {
420           if (!common_prefix)
421             {
422               common_prefix = g_strdup (display_name);
423               unique_path = gtk_file_path_copy (path);
424             }
425           else
426             {
427               gchar *p = common_prefix;
428               const gchar *q = display_name;
429                   
430               while (*p && *p == *q)
431                 {
432                   p++;
433                   q++;
434                 }
435                   
436               *p = '\0';
437
438               gtk_file_path_free (unique_path);
439               unique_path = NULL;
440             }
441         }
442
443       g_free (display_name);
444       gtk_file_path_free (path);
445       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store),
446                                         &iter);
447     }
448
449   if (unique_path)
450     {
451       common_prefix = maybe_append_separator_to_path (chooser_entry,
452                                                       unique_path,
453                                                       common_prefix);
454       gtk_file_path_free (unique_path);
455     }
456
457   switch (chooser_entry->action)
458     {
459     case GTK_FILE_CHOOSER_ACTION_SAVE:
460     case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
461       if (common_prefix && !g_str_has_suffix (common_prefix, "/"))
462         {
463           g_free (common_prefix);
464           common_prefix = NULL;
465         }
466       break;
467     default: ;
468     }
469
470   if (common_prefix)
471     {
472       gint file_part_len;
473       gint common_prefix_len;
474       gint pos;
475
476       file_part_len = g_utf8_strlen (chooser_entry->file_part, -1);
477       common_prefix_len = g_utf8_strlen (common_prefix, -1);
478
479       if (common_prefix_len > file_part_len)
480         {
481           pos = chooser_entry->file_part_pos;
482
483           chooser_entry->in_change = TRUE;
484           gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
485                                     pos, -1);
486           gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
487                                     common_prefix, -1, 
488                                     &pos);
489           gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
490                                       chooser_entry->file_part_pos + file_part_len,
491                                       chooser_entry->file_part_pos + common_prefix_len);
492           chooser_entry->in_change = FALSE;
493
494           chooser_entry->has_completion = TRUE;
495         }
496           
497       g_free (common_prefix);
498     }
499
500   return FALSE;
501 }
502
503 static void
504 add_completion_idle (GtkFileChooserEntry *chooser_entry)
505 {
506   /* idle to update the selection based on the file list */
507   if (chooser_entry->check_completion_idle == NULL)
508     {
509       chooser_entry->check_completion_idle = g_idle_source_new ();
510       g_source_set_priority (chooser_entry->check_completion_idle, G_PRIORITY_HIGH);
511       g_source_set_closure (chooser_entry->check_completion_idle,
512                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
513                                                    G_OBJECT (chooser_entry)));
514       g_source_attach (chooser_entry->check_completion_idle, NULL);
515     }
516 }
517
518
519 static void
520 update_current_folder_files (GtkFileChooserEntry *chooser_entry,
521                              GSList              *added_uris)
522 {
523   GSList *tmp_list;
524
525   g_assert (chooser_entry->completion_store != NULL);
526
527   /* Bah.  Need to turn off sorting */
528   for (tmp_list = added_uris; tmp_list; tmp_list = tmp_list->next)
529     {
530       GtkFileInfo *info;
531       GtkFilePath *path;
532
533       path = tmp_list->data;
534
535       info = gtk_file_folder_get_info (chooser_entry->current_folder,
536                                        path,
537                                        NULL); /* NULL-GError */
538       if (info)
539         {
540           const gchar *display_name = gtk_file_info_get_display_name (info);
541           GtkTreeIter iter;
542
543           gtk_list_store_append (chooser_entry->completion_store, &iter);
544           gtk_list_store_set (chooser_entry->completion_store, &iter,
545                               DISPLAY_NAME_COLUMN, display_name,
546                               PATH_COLUMN, path,
547                               -1);
548
549           gtk_file_info_free (info);
550         }
551     }
552
553   /* FIXME: we want to turn off sorting temporarily.  I suck... */
554   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
555                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
556
557   add_completion_idle (chooser_entry);
558 }
559
560 static void
561 files_added_cb (GtkFileSystem       *file_system,
562                 GSList              *added_uris,
563                 GtkFileChooserEntry *chooser_entry)
564 {
565   update_current_folder_files (chooser_entry, added_uris);
566 }
567
568 static void
569 files_deleted_cb (GtkFileSystem       *file_system,
570                   GSList              *deleted_uris,
571                   GtkFileChooserEntry *chooser_entry)
572 {
573   /* FIXME: gravy... */
574 }
575
576 static gboolean
577 load_directory_callback (GtkFileChooserEntry *chooser_entry)
578 {
579   GSList *child_paths = NULL;
580
581   chooser_entry->load_directory_idle = NULL;
582
583   /* guard against bogus settings*/
584   if (chooser_entry->current_folder_path == NULL ||
585       chooser_entry->file_system == NULL)
586     return FALSE;
587
588   if (chooser_entry->current_folder != NULL)
589     {
590       g_warning ("idle activate multiple times without clearing the folder object first.");
591       return FALSE;
592     }
593   g_assert (chooser_entry->completion_store == NULL);
594
595   /* Load the folder */
596   chooser_entry->current_folder = gtk_file_system_get_folder (chooser_entry->file_system,
597                                                               chooser_entry->current_folder_path,
598                                                               GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
599                                                               NULL); /* NULL-GError */
600
601   /* There is no folder by that name */
602   if (!chooser_entry->current_folder)
603     return FALSE;
604   g_signal_connect (chooser_entry->current_folder, "files-added",
605                     G_CALLBACK (files_added_cb), chooser_entry);
606   g_signal_connect (chooser_entry->current_folder, "files-removed",
607                     G_CALLBACK (files_deleted_cb), chooser_entry);
608   
609   chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
610                                                         G_TYPE_STRING,
611                                                         GTK_TYPE_FILE_PATH);
612
613   if (chooser_entry->file_part_pos != -1)
614     {
615       gtk_file_folder_list_children (chooser_entry->current_folder,
616                                      &child_paths,
617                                      NULL); /* NULL-GError */
618       if (child_paths)
619         {
620           update_current_folder_files (chooser_entry, child_paths);
621           add_completion_idle (chooser_entry);
622           gtk_file_paths_free (child_paths);
623         }
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 (!chooser_entry->eat_tabs)
654     return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
655
656   if (gtk_get_current_event_state (&state))
657     {
658       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
659         control_pressed = TRUE;
660     }
661
662   /* This is a bit evil -- it makes Tab never leave the entry. It basically
663    * makes it 'safe' for people to hit. */
664   if ((direction == GTK_DIR_TAB_FORWARD) &&
665       (GTK_WIDGET_HAS_FOCUS (widget)) &&
666       (! control_pressed))
667     {
668       gint pos = 0;
669
670       if (chooser_entry->has_completion)
671         gtk_editable_set_position (GTK_EDITABLE (widget),
672                                    GTK_ENTRY (widget)->text_length);
673       /* Trigger the completion window to pop up again by a 
674        * zero-length insertion, a bit of a hack.
675        */
676       gtk_editable_insert_text (GTK_EDITABLE (widget), "", -1, &pos);
677
678       return TRUE;
679     }
680   else
681     return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
682 }
683
684 static void
685 gtk_file_chooser_entry_activate (GtkEntry *entry)
686 {
687   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
688
689   if (chooser_entry->has_completion)
690     {
691       gtk_editable_set_position (GTK_EDITABLE (entry),
692                                  entry->text_length);
693     }
694   
695   GTK_ENTRY_CLASS (parent_class)->activate (entry);
696 }
697
698 /* This will see if a path typed by the user is new, and installs the loading
699  * idle if it is.
700  */
701 static void
702 gtk_file_chooser_entry_maybe_update_directory (GtkFileChooserEntry *chooser_entry,
703                                                GtkFilePath         *folder_path,
704                                                gboolean             force_reload)
705 {
706   gboolean queue_idle = FALSE;
707
708   if (chooser_entry->current_folder_path)
709     {
710       if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0 || force_reload)
711         {
712           /* We changed our current directory.  We need to clear out the old
713            * directory information.
714            */
715           if (chooser_entry->current_folder)
716             {
717               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
718                                                     G_CALLBACK (files_added_cb), chooser_entry);
719               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
720                                                     G_CALLBACK (files_deleted_cb), chooser_entry);
721
722               g_object_unref (chooser_entry->current_folder);
723               chooser_entry->current_folder = NULL;
724             }
725           if (chooser_entry->completion_store)
726             {
727               gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
728               g_object_unref (chooser_entry->completion_store);
729               chooser_entry->completion_store = NULL;
730             }
731
732           queue_idle = TRUE;
733         }
734
735       gtk_file_path_free (chooser_entry->current_folder_path);
736     }
737   else
738     {
739       queue_idle = TRUE;
740     }
741
742   chooser_entry->current_folder_path = folder_path;
743
744   if (queue_idle && chooser_entry->load_directory_idle == NULL)
745     {
746       chooser_entry->load_directory_idle = g_idle_source_new ();
747       g_source_set_priority (chooser_entry->load_directory_idle, G_PRIORITY_HIGH);
748       g_source_set_closure (chooser_entry->load_directory_idle,
749                             g_cclosure_new_object (G_CALLBACK (load_directory_callback),
750                                                    G_OBJECT (chooser_entry)));
751       g_source_attach (chooser_entry->load_directory_idle, NULL);
752     }
753 }
754
755
756
757 static void
758 gtk_file_chooser_entry_changed (GtkEditable *editable)
759 {
760   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
761   const gchar *text;
762   GtkFilePath *folder_path;
763   gchar *file_part;
764   gsize total_len, file_part_len;
765   gint file_part_pos;
766
767   if (chooser_entry->in_change)
768     return;
769
770   text = gtk_entry_get_text (GTK_ENTRY (editable));
771   
772   if (!chooser_entry->file_system ||
773       !chooser_entry->base_folder ||
774       !gtk_file_system_parse (chooser_entry->file_system,
775                               chooser_entry->base_folder, text,
776                               &folder_path, &file_part, NULL)) /* NULL-GError */
777     {
778       folder_path = gtk_file_path_copy (chooser_entry->base_folder);
779       file_part = g_strdup ("");
780       file_part_pos = -1;
781     }
782   else
783     {
784       file_part_len = strlen (file_part);
785       total_len = strlen (text);
786       if (total_len > file_part_len)
787         file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
788       else
789         file_part_pos = 0;
790     }
791
792   if (chooser_entry->file_part)
793     g_free (chooser_entry->file_part);
794
795   chooser_entry->file_part = file_part;
796   chooser_entry->file_part_pos = file_part_pos;
797
798   gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path, file_part_pos == -1);
799 }
800
801 static void
802 clear_completion_callback (GtkFileChooserEntry *chooser_entry,
803                            GParamSpec          *pspec)
804 {
805   if (chooser_entry->has_completion)
806     {
807       chooser_entry->has_completion = FALSE;
808       gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
809     }
810 }
811
812 /**
813  * _gtk_file_chooser_entry_new:
814  * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
815  *
816  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
817  * is an internal implementation widget for the GTK+ file chooser
818  * which is an entry with completion with respect to a
819  * #GtkFileSystem object.
820  *
821  * Return value: the newly created #GtkFileChooserEntry
822  **/
823 GtkWidget *
824 _gtk_file_chooser_entry_new (gboolean eat_tabs)
825 {
826   GtkFileChooserEntry *chooser_entry;
827
828   chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
829   chooser_entry->eat_tabs = (eat_tabs != FALSE);
830
831   return GTK_WIDGET (chooser_entry);
832 }
833
834 /**
835  * _gtk_file_chooser_entry_set_file_system:
836  * @chooser_entry: a #GtkFileChooser
837  * @file_system: an object implementing #GtkFileSystem
838  *
839  * Sets the file system for @chooser_entry.
840  **/
841 void
842 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
843                                          GtkFileSystem       *file_system)
844 {
845   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
846   g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
847
848   if (file_system != chooser_entry->file_system)
849     {
850       if (chooser_entry->file_system)
851         g_object_unref (chooser_entry->file_system);
852
853       chooser_entry->file_system = g_object_ref (file_system);
854     }
855 }
856
857 /**
858  * _gtk_file_chooser_entry_set_base_folder:
859  * @chooser_entry: a #GtkFileChooserEntry
860  * @path: path of a folder in the chooser entries current file system.
861  *
862  * Sets the folder with respect to which completions occur.
863  **/
864 void
865 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
866                                          const GtkFilePath   *path)
867 {
868   if (chooser_entry->base_folder)
869     gtk_file_path_free (chooser_entry->base_folder);
870
871   chooser_entry->base_folder = gtk_file_path_copy (path);
872
873   gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
874   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, -1);
875 }
876
877 /**
878  * _gtk_file_chooser_entry_get_current_folder:
879  * @chooser_entry: a #GtkFileChooserEntry
880  *
881  * Gets the current folder for the #GtkFileChooserEntry. If the
882  * user has only entered a filename, this will be the base folder
883  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
884  * user has entered a relative or absolute path, then it will
885  * be different. If the user has entered a relative or absolute
886  * path that doesn't point to a folder in the file system, it will
887  * be %NULL.
888  *
889  * Return value: the path of current folder - this value is owned by the
890  *  chooser entry and must not be modified or freed.
891  **/
892 const GtkFilePath *
893 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
894 {
895   if (chooser_entry->has_completion)
896     {
897       gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
898                                  GTK_ENTRY (chooser_entry)->text_length);
899     }
900   return chooser_entry->current_folder_path;
901 }
902
903 /**
904  * _gtk_file_chooser_entry_get_file_part:
905  * @chooser_entry: a #GtkFileChooserEntry
906  *
907  * Gets the non-folder portion of whatever the user has entered
908  * into the file selector. What is returned is a UTF-8 string,
909  * and if a filename path is needed, gtk_file_system_make_path()
910  * must be used
911   *
912  * Return value: the entered filename - this value is owned by the
913  *  chooser entry and must not be modified or freed.
914  **/
915 const gchar *
916 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
917 {
918   if (chooser_entry->has_completion)
919     {
920       gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
921                                  GTK_ENTRY (chooser_entry)->text_length);
922     }
923   return chooser_entry->file_part;
924 }
925
926 /**
927  * _gtk_file_chooser_entry_set_file_part:
928  * @chooser_entry: a #GtkFileChooserEntry
929  * @file_part: text to display in the entry, in UTF-8
930  *
931  * Sets the current text shown in the file chooser entry.
932  **/
933 void
934 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
935                                        const gchar         *file_part)
936 {
937   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
938
939   gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
940 }
941
942
943 /**
944  * _gtk_file_chooser_entry_set_action:
945  * @chooser_entry: a #GtkFileChooserEntry
946  * @action: the action which is performed by the file selector using this entry
947  *
948  * Sets action which is performed by the file selector using this entry. 
949  * The #GtkFileChooserEntry will use different completion strategies for 
950  * different actions.
951  **/
952 void
953 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
954                                     GtkFileChooserAction action)
955 {
956   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
957   
958   if (  chooser_entry->action != action)
959     {
960       chooser_entry->action = action;
961     }
962 }
963
964
965 /**
966  * _gtk_file_chooser_entry_get_action:
967  * @chooser_entry: a #GtkFileChooserEntry
968  *
969  * Gets the action for this entry. 
970  *
971  * Returns: the action
972  **/
973 GtkFileChooserAction
974 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
975 {
976   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
977                         GTK_FILE_CHOOSER_ACTION_OPEN);
978   
979   return chooser_entry->action;
980 }