]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
filechooserentry: Use completion for completion
[~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
23 #include "gtkfilechooserentry.h"
24
25 #include <string.h>
26
27 #include "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkentry.h"
30 #include "gtkfilesystemmodel.h"
31 #include "gtklabel.h"
32 #include "gtkmain.h"
33 #include "gtksizerequest.h"
34 #include "gtkwindow.h"
35 #include "gtkintl.h"
36
37 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
38
39 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
40 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
41 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
42
43 struct _GtkFileChooserEntryClass
44 {
45   GtkEntryClass parent_class;
46 };
47
48 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
49 typedef enum {
50   LOAD_COMPLETE_NOTHING,
51   LOAD_COMPLETE_EXPLICIT_COMPLETION
52 } LoadCompleteAction;
53
54 typedef enum
55 {
56   REFRESH_OK,
57   REFRESH_INVALID_INPUT,
58   REFRESH_INCOMPLETE_HOSTNAME,
59   REFRESH_NONEXISTENT,
60   REFRESH_NOT_LOCAL
61 } RefreshStatus;
62
63 struct _GtkFileChooserEntry
64 {
65   GtkEntry parent_instance;
66
67   GtkFileChooserAction action;
68
69   GFile *base_folder;
70   GFile *current_folder_file;
71   gchar *dir_part;
72   gchar *file_part;
73   gint file_part_pos;
74
75   LoadCompleteAction load_complete_action;
76
77   GtkTreeModel *completion_store;
78
79   guint current_folder_loaded : 1;
80   guint in_change      : 1;
81   guint eat_tabs       : 1;
82   guint local_only     : 1;
83 };
84
85 enum
86 {
87   DISPLAY_NAME_COLUMN,
88   FULL_PATH_COLUMN,
89   FILE_COLUMN,
90   N_COLUMNS
91 };
92
93 static void     gtk_file_chooser_entry_finalize       (GObject          *object);
94 static void     gtk_file_chooser_entry_dispose        (GObject          *object);
95 static void     gtk_file_chooser_entry_grab_focus     (GtkWidget        *widget);
96 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
97                                                         GdkEventKey *event);
98 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget       *widget,
99                                                         GdkEventFocus   *event);
100
101 #ifdef G_OS_WIN32
102 static gint     insert_text_callback      (GtkFileChooserEntry *widget,
103                                            const gchar         *new_text,
104                                            gint                 new_text_length,
105                                            gint                *position,
106                                            gpointer             user_data);
107 static void     delete_text_callback      (GtkFileChooserEntry *widget,
108                                            gint                 start_pos,
109                                            gint                 end_pos,
110                                            gpointer             user_data);
111 #endif
112
113 static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
114                                            GtkTreeModel        *model,
115                                            GtkTreeIter         *iter,
116                                            GtkFileChooserEntry *chooser_entry);
117 static gboolean completion_match_func     (GtkEntryCompletion  *comp,
118                                            const char          *key,
119                                            GtkTreeIter         *iter,
120                                            gpointer             data);
121
122 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
123                                                            const char          *text);
124 static void finished_loading_cb (GtkFileSystemModel  *model,
125                                  GError              *error,
126                                  GtkFileChooserEntry *chooser_entry);
127
128 G_DEFINE_TYPE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY)
129
130 static char *
131 gtk_file_chooser_entry_get_completion_text (GtkFileChooserEntry *chooser_entry)
132 {
133   GtkEditable *editable = GTK_EDITABLE (chooser_entry);
134   int start, end;
135
136   gtk_editable_get_selection_bounds (editable, &start, &end);
137   return gtk_editable_get_chars (editable, 0, MIN (start, end));
138 }
139
140 static void
141 gtk_file_chooser_entry_dispatch_properties_changed (GObject     *object,
142                                                     guint        n_pspecs,
143                                                     GParamSpec **pspecs)
144 {
145   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
146   guint i;
147
148   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
149
150   /* What we are after: The text in front of the cursor was modified.
151    * Unfortunately, there's no other way to catch this. */
152
153   if (chooser_entry->in_change)
154     return;
155
156   for (i = 0; i < n_pspecs; i++)
157     {
158       if (pspecs[i]->name == I_("cursor-position") ||
159           pspecs[i]->name == I_("selection-bound") ||
160           pspecs[i]->name == I_("text"))
161         {
162           char *text;
163
164           chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
165
166           text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
167           refresh_current_folder_and_file_part (chooser_entry, text);
168           g_free (text);
169
170           break;
171         }
172     }
173 }
174
175 static void
176 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
177 {
178   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
180
181   gobject_class->finalize = gtk_file_chooser_entry_finalize;
182   gobject_class->dispose = gtk_file_chooser_entry_dispose;
183   gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
184
185   widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
186   widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
187   widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
188 }
189
190 static void
191 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
192 {
193   GtkEntryCompletion *comp;
194   GtkCellRenderer *cell;
195
196   chooser_entry->local_only = TRUE;
197   chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
198
199   g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
200
201   comp = gtk_entry_completion_new ();
202   gtk_entry_completion_set_popup_single_match (comp, FALSE);
203   /* see docs for gtk_entry_completion_set_text_column() */
204   g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
205
206   gtk_entry_completion_set_match_func (comp,
207                                        completion_match_func,
208                                        chooser_entry,
209                                        NULL);
210
211   cell = gtk_cell_renderer_text_new ();
212   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
213                               cell, TRUE);
214   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
215                                  cell,
216                                  "text", DISPLAY_NAME_COLUMN);
217
218   g_signal_connect (comp, "match-selected",
219                     G_CALLBACK (match_selected_callback), chooser_entry);
220
221   gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
222   g_object_unref (comp);
223
224 #ifdef G_OS_WIN32
225   g_signal_connect (chooser_entry, "insert-text",
226                     G_CALLBACK (insert_text_callback), NULL);
227   g_signal_connect (chooser_entry, "delete-text",
228                     G_CALLBACK (delete_text_callback), NULL);
229 #endif
230 }
231
232 static void
233 gtk_file_chooser_entry_finalize (GObject *object)
234 {
235   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
236
237   g_object_unref (chooser_entry->base_folder);
238
239   if (chooser_entry->current_folder_file)
240     g_object_unref (chooser_entry->current_folder_file);
241
242   g_free (chooser_entry->dir_part);
243   g_free (chooser_entry->file_part);
244
245   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
246 }
247
248 static void
249 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
250 {
251   if (chooser_entry->current_folder_file)
252     {
253       g_object_unref (chooser_entry->current_folder_file);
254       chooser_entry->current_folder_file = NULL;
255     }
256 }
257
258 static void
259 gtk_file_chooser_entry_dispose (GObject *object)
260 {
261   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
262
263   discard_loading_and_current_folder_file (chooser_entry);
264
265   if (chooser_entry->completion_store)
266     {
267       g_object_unref (chooser_entry->completion_store);
268       chooser_entry->completion_store = NULL;
269     }
270
271   G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
272 }
273
274 /* Match functions for the GtkEntryCompletion */
275 static gboolean
276 match_selected_callback (GtkEntryCompletion  *completion,
277                          GtkTreeModel        *model,
278                          GtkTreeIter         *iter,
279                          GtkFileChooserEntry *chooser_entry)
280 {
281   char *path;
282   
283   gtk_tree_model_get (model, iter,
284                       FULL_PATH_COLUMN, &path,
285                       -1);
286
287   /* We don't set in_change here as we want to update the current_folder
288    * variable */
289   gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
290                             0,
291                             gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
292   gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
293                             path,
294                             0,
295                             NULL); 
296
297   g_free (path);
298
299   return TRUE;
300 }
301
302 /* Match function for the GtkEntryCompletion */
303 static gboolean
304 completion_match_func (GtkEntryCompletion *comp,
305                        const char         *key_unused,
306                        GtkTreeIter        *iter,
307                        gpointer            data)
308 {
309   GtkFileChooserEntry *chooser_entry;
310   char *name = NULL;
311   gboolean result;
312   char *norm_file_part;
313   char *norm_name;
314
315   chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
316
317   /* We ignore the key because it is the contents of the entry.  Instead, we
318    * just use our precomputed file_part.
319    */
320   if (!chooser_entry->file_part)
321     {
322       return FALSE;
323     }
324
325   gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
326   if (!name)
327     {
328       return FALSE; /* Uninitialized row, ugh */
329     }
330
331   /* If we have an empty file_part, then we're at the root of a directory.  In
332    * that case, we want to match all non-dot files.  We might want to match
333    * dot_files too if show_hidden is TRUE on the fileselector in the future.
334    */
335   /* Additionally, support for gnome .hidden files would be sweet, too */
336   if (chooser_entry->file_part[0] == '\000')
337     {
338       if (name[0] == '.')
339         result = FALSE;
340       else
341         result = TRUE;
342       g_free (name);
343
344       return result;
345     }
346
347
348   norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
349   norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
350
351 #ifdef G_PLATFORM_WIN32
352   {
353     gchar *temp;
354
355     temp = norm_file_part;
356     norm_file_part = g_utf8_casefold (norm_file_part, -1);
357     g_free (temp);
358
359     temp = norm_name;
360     norm_name = g_utf8_casefold (norm_name, -1);
361     g_free (temp);
362   }
363 #endif
364
365   result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
366
367   g_free (norm_file_part);
368   g_free (norm_name);
369   g_free (name);
370   
371   return result;
372 }
373
374 static void
375 clear_completions (GtkFileChooserEntry *chooser_entry)
376 {
377   chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
378 }
379
380 static void
381 beep (GtkFileChooserEntry *chooser_entry)
382 {
383   gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
384 }
385
386 static gboolean
387 is_valid_scheme_character (char c)
388 {
389   return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
390 }
391
392 static gboolean
393 has_uri_scheme (const char *str)
394 {
395   const char *p;
396
397   p = str;
398
399   if (!is_valid_scheme_character (*p))
400     return FALSE;
401
402   do
403     p++;
404   while (is_valid_scheme_character (*p));
405
406   return (strncmp (p, "://", 3) == 0);
407 }
408
409 static GFile *
410 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
411                                     const gchar         *str)
412 {
413   GFile *file;
414
415   if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
416     file = g_file_parse_name (str);
417   else
418     file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
419
420   return file;
421 }
422
423 static GFile *
424 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
425                                          const char *         text)
426 {
427   GFile *file, *parent;
428
429   file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
430
431   if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
432     return file;
433
434   parent = g_file_get_parent (file);
435   g_object_unref (file);
436
437   return parent;
438 }
439
440 static gboolean
441 gtk_file_chooser_entry_parse (GtkFileChooserEntry  *chooser_entry,
442                               const gchar          *str,
443                               GFile               **folder,
444                               gchar               **file_part,
445                               GError              **error)
446 {
447   GFile *file;
448   gboolean result = FALSE;
449   gboolean is_dir = FALSE;
450   gchar *last_slash = NULL;
451
452   if (str && *str)
453     is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
454
455   last_slash = strrchr (str, G_DIR_SEPARATOR);
456
457   file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
458
459   if (g_file_equal (chooser_entry->base_folder, file))
460     {
461       /* this is when user types '.', could be the
462        * beginning of a hidden file, ./ or ../
463        */
464       *folder = g_object_ref (file);
465       *file_part = g_strdup (str);
466       result = TRUE;
467     }
468   else if (is_dir)
469     {
470       /* it's a dir, or at least it ends with the dir separator */
471       *folder = g_object_ref (file);
472       *file_part = g_strdup ("");
473       result = TRUE;
474     }
475   else
476     {
477       GFile *parent_file;
478
479       parent_file = g_file_get_parent (file);
480
481       if (!parent_file)
482         {
483           g_set_error (error,
484                        GTK_FILE_CHOOSER_ERROR,
485                        GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
486                        "Could not get parent file");
487           *folder = NULL;
488           *file_part = NULL;
489         }
490       else
491         {
492           *folder = parent_file;
493           result = TRUE;
494
495           if (last_slash)
496             *file_part = g_strdup (last_slash + 1);
497           else
498             *file_part = g_strdup (str);
499         }
500     }
501
502   g_object_unref (file);
503
504   return result;
505 }
506
507 /* Finds a common prefix based on the contents of the entry
508  * and mandatorily appends it
509  */
510 static void
511 explicitly_complete (GtkFileChooserEntry *chooser_entry)
512 {
513   clear_completions (chooser_entry);
514
515   if (chooser_entry->completion_store)
516     {
517       char *completion, *text;
518       gsize completion_len, text_len;
519       
520       text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
521       text_len = strlen (text);
522       completion = gtk_entry_completion_compute_prefix (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), text);
523       completion_len = completion ? strlen (completion) : 0;
524
525       if (completion_len > text_len)
526         {
527           GtkEditable *editable = GTK_EDITABLE (chooser_entry);
528           int pos = gtk_editable_get_position (editable);
529
530           gtk_editable_insert_text (editable,
531                                     completion + text_len,
532                                     completion_len - text_len,
533                                     &pos);
534           gtk_editable_set_position (editable, pos);
535           return;
536         }
537     }
538
539   beep (chooser_entry);
540 }
541
542 static void
543 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
544 {
545   GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
546   _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
547 }
548
549 static void
550 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
551 {
552   if (chooser_entry->current_folder_loaded)
553     explicitly_complete (chooser_entry);
554   else
555     chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
556 }
557
558 static gboolean
559 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
560                                         GdkEventKey *event)
561 {
562   GtkFileChooserEntry *chooser_entry;
563   GtkEditable *editable;
564   GdkModifierType state;
565   gboolean control_pressed;
566
567   chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
568   editable = GTK_EDITABLE (widget);
569
570   if (!chooser_entry->eat_tabs)
571     return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
572
573   control_pressed = FALSE;
574
575   if (gtk_get_current_event_state (&state))
576     {
577       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
578         control_pressed = TRUE;
579     }
580
581   /* This is a bit evil -- it makes Tab never leave the entry. It basically
582    * makes it 'safe' for people to hit. */
583   if (event->keyval == GDK_KEY_Tab && !control_pressed)
584     {
585       gint start, end;
586
587       gtk_editable_get_selection_bounds (editable, &start, &end);
588       
589       if (start != end)
590         gtk_editable_set_position (editable, MAX (start, end));
591       else
592         start_explicit_completion (chooser_entry);
593
594       return TRUE;
595      }
596
597   return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
598
599 }
600
601 static gboolean
602 gtk_file_chooser_entry_focus_out_event (GtkWidget     *widget,
603                                         GdkEventFocus *event)
604 {
605   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
606
607   chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
608  
609   return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
610 }
611
612 static void
613 discard_completion_store (GtkFileChooserEntry *chooser_entry)
614 {
615   if (!chooser_entry->completion_store)
616     return;
617
618   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
619   gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
620   g_object_unref (chooser_entry->completion_store);
621   chooser_entry->completion_store = NULL;
622 }
623
624 static gboolean
625 completion_store_set (GtkFileSystemModel  *model,
626                       GFile               *file,
627                       GFileInfo           *info,
628                       int                  column,
629                       GValue              *value,
630                       gpointer             data)
631 {
632   GtkFileChooserEntry *chooser_entry = data;
633
634   const char *prefix = "";
635   const char *suffix = "";
636
637   switch (column)
638     {
639     case FILE_COLUMN:
640       g_value_set_object (value, file);
641       break;
642     case FULL_PATH_COLUMN:
643       prefix = chooser_entry->dir_part;
644       /* fall through */
645     case DISPLAY_NAME_COLUMN:
646       if (_gtk_file_info_consider_as_directory (info))
647         suffix = G_DIR_SEPARATOR_S;
648
649       g_value_take_string (value, g_strconcat (
650               prefix,
651               g_file_info_get_display_name (info),
652               suffix,
653               NULL));
654       break;
655     default:
656       g_assert_not_reached ();
657       break;
658     }
659
660   return TRUE;
661 }
662
663 /* Fills the completion store from the contents of the current folder */
664 static void
665 populate_completion_store (GtkFileChooserEntry *chooser_entry)
666 {
667   discard_completion_store (chooser_entry);
668
669   chooser_entry->completion_store = GTK_TREE_MODEL (
670       _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
671                                                 "standard::name,standard::display-name,standard::type",
672                                                 completion_store_set,
673                                                 chooser_entry,
674                                                 N_COLUMNS,
675                                                 G_TYPE_STRING,
676                                                 G_TYPE_STRING,
677                                                 G_TYPE_FILE));
678   g_signal_connect (chooser_entry->completion_store, "finished-loading",
679                     G_CALLBACK (finished_loading_cb), chooser_entry);
680
681   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
682                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
683
684   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
685                                   chooser_entry->completion_store);
686 }
687
688 /* When we finish loading the current folder, this function should get called to
689  * perform the deferred explicit completion.
690  */
691 static void
692 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
693 {
694   switch (chooser_entry->load_complete_action)
695     {
696     case LOAD_COMPLETE_NOTHING:
697       break;
698
699     case LOAD_COMPLETE_EXPLICIT_COMPLETION:
700       explicitly_complete (chooser_entry);
701       break;
702
703     default:
704       g_assert_not_reached ();
705     }
706
707 }
708
709 /* Callback when the current folder finishes loading */
710 static void
711 finished_loading_cb (GtkFileSystemModel  *model,
712                      GError              *error,
713                      GtkFileChooserEntry *chooser_entry)
714 {
715   GtkEntryCompletion *completion;
716
717   chooser_entry->current_folder_loaded = TRUE;
718
719   if (error)
720     {
721       LoadCompleteAction old_load_complete_action;
722
723       old_load_complete_action = chooser_entry->load_complete_action;
724
725       discard_completion_store (chooser_entry);
726       clear_completions (chooser_entry);
727
728       if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
729         {
730           /* Since this came from explicit user action (Tab completion), we'll present errors visually */
731
732           beep (chooser_entry);
733         }
734
735       return;
736     }
737
738   perform_load_complete_action (chooser_entry);
739
740   gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
741
742   completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
743   gtk_entry_completion_set_inline_completion (completion, TRUE);
744   gtk_entry_completion_complete (completion);
745   gtk_entry_completion_insert_prefix (completion);
746 }
747
748 static RefreshStatus
749 reload_current_folder (GtkFileChooserEntry *chooser_entry,
750                        GFile               *folder_file)
751 {
752   g_assert (folder_file != NULL);
753
754   if (chooser_entry->current_folder_file
755       && g_file_equal (folder_file, chooser_entry->current_folder_file))
756     return REFRESH_OK;
757
758   if (chooser_entry->current_folder_file)
759     {
760       discard_loading_and_current_folder_file (chooser_entry);
761     }
762   
763   if (chooser_entry->local_only
764       && !g_file_is_native (folder_file))
765     return REFRESH_NOT_LOCAL;
766
767   chooser_entry->current_folder_file = g_object_ref (folder_file);
768   chooser_entry->current_folder_loaded = FALSE;
769
770   populate_completion_store (chooser_entry);
771
772   return REFRESH_OK;
773 }
774
775 static RefreshStatus
776 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
777                                       const gchar *        text)
778 {
779   GFile *folder_file;
780   gchar *file_part;
781   gsize total_len, file_part_len;
782   gint file_part_pos;
783   GError *error;
784   RefreshStatus result;
785
786   error = NULL;
787   if (!gtk_file_chooser_entry_parse (chooser_entry,
788                                      text, &folder_file, &file_part, &error))
789     {
790       folder_file = g_object_ref (chooser_entry->base_folder);
791
792       if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
793         result = REFRESH_NONEXISTENT;
794       else
795         result = REFRESH_INVALID_INPUT;
796
797       if (error)
798         g_error_free (error);
799
800       file_part = g_strdup ("");
801       file_part_pos = -1;
802     }
803   else
804     {
805       g_assert (folder_file != NULL);
806
807       file_part_len = strlen (file_part);
808       total_len = strlen (text);
809       if (total_len > file_part_len)
810         file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
811       else
812         file_part_pos = 0;
813
814       result = REFRESH_OK;
815     }
816
817   g_free (chooser_entry->file_part);
818   g_free (chooser_entry->dir_part);
819
820   chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
821   chooser_entry->file_part = file_part;
822   chooser_entry->file_part_pos = file_part_pos;
823
824   if (result == REFRESH_OK)
825     {
826       result = reload_current_folder (chooser_entry, folder_file);
827     }
828   else
829     {
830       discard_loading_and_current_folder_file (chooser_entry);
831     }
832
833   if (folder_file)
834     g_object_unref (folder_file);
835
836   g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
837             ((result == REFRESH_OK)
838              && (chooser_entry->current_folder_file != NULL))
839             /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
840             || ((result != REFRESH_OK)
841                 && (chooser_entry->current_folder_file == NULL)));
842
843   return result;
844 }
845
846 #ifdef G_OS_WIN32
847 static gint
848 insert_text_callback (GtkFileChooserEntry *chooser_entry,
849                       const gchar         *new_text,
850                       gint                 new_text_length,
851                       gint                *position,
852                       gpointer             user_data)
853 {
854   const gchar *colon = memchr (new_text, ':', new_text_length);
855   gint i;
856
857   /* Disallow these characters altogether */
858   for (i = 0; i < new_text_length; i++)
859     {
860       if (new_text[i] == '<' ||
861           new_text[i] == '>' ||
862           new_text[i] == '"' ||
863           new_text[i] == '|' ||
864           new_text[i] == '*' ||
865           new_text[i] == '?')
866         break;
867     }
868
869   if (i < new_text_length ||
870       /* Disallow entering text that would cause a colon to be anywhere except
871        * after a drive letter.
872        */
873       (colon != NULL &&
874        *position + (colon - new_text) != 1) ||
875       (new_text_length > 0 &&
876        *position <= 1 &&
877        gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
878        gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
879     {
880       gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
881       g_signal_stop_emission_by_name (chooser_entry, "insert_text");
882       return FALSE;
883     }
884
885   return TRUE;
886 }
887
888 static void
889 delete_text_callback (GtkFileChooserEntry *chooser_entry,
890                       gint                 start_pos,
891                       gint                 end_pos,
892                       gpointer             user_data)
893 {
894   /* If deleting a drive letter, delete the colon, too */
895   if (start_pos == 0 && end_pos == 1 &&
896       gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
897       gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
898     {
899       g_signal_handlers_block_by_func (chooser_entry,
900                                        G_CALLBACK (delete_text_callback),
901                                        user_data);
902       gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
903       g_signal_handlers_unblock_by_func (chooser_entry,
904                                          G_CALLBACK (delete_text_callback),
905                                          user_data);
906     }
907 }
908 #endif
909
910 /**
911  * _gtk_file_chooser_entry_new:
912  * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
913  *
914  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
915  * is an internal implementation widget for the GTK+ file chooser
916  * which is an entry with completion with respect to a
917  * #GtkFileSystem object.
918  *
919  * Return value: the newly created #GtkFileChooserEntry
920  **/
921 GtkWidget *
922 _gtk_file_chooser_entry_new (gboolean       eat_tabs)
923 {
924   GtkFileChooserEntry *chooser_entry;
925
926   chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
927   chooser_entry->eat_tabs = (eat_tabs != FALSE);
928
929   return GTK_WIDGET (chooser_entry);
930 }
931
932 /**
933  * _gtk_file_chooser_entry_set_base_folder:
934  * @chooser_entry: a #GtkFileChooserEntry
935  * @file: file for a folder in the chooser entries current file system.
936  *
937  * Sets the folder with respect to which completions occur.
938  **/
939 void
940 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
941                                          GFile               *file)
942 {
943   if (file)
944     g_object_ref (file);
945   else
946     file = g_file_new_for_path (g_get_home_dir ());
947
948   if (g_file_equal (chooser_entry->base_folder, file))
949     {
950       g_object_unref (file);
951       return;
952     }
953
954   if (chooser_entry->base_folder)
955     g_object_unref (chooser_entry->base_folder);
956
957   chooser_entry->base_folder = file;
958
959   clear_completions (chooser_entry);
960 }
961
962 /**
963  * _gtk_file_chooser_entry_get_current_folder:
964  * @chooser_entry: a #GtkFileChooserEntry
965  *
966  * Gets the current folder for the #GtkFileChooserEntry. If the
967  * user has only entered a filename, this will be in the base folder
968  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
969  * user has entered a relative or absolute path, then it will
970  * be different.  If the user has entered unparsable text, or text which
971  * the entry cannot handle, this will return %NULL.
972  *
973  * Return value: the file for the current folder - you must g_object_unref()
974  *   the value after use.
975  **/
976 GFile *
977 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
978 {
979   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
980
981   return gtk_file_chooser_get_directory_for_text (chooser_entry,
982                                                   gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
983 }
984
985 /**
986  * _gtk_file_chooser_entry_get_file_part:
987  * @chooser_entry: a #GtkFileChooserEntry
988  *
989  * Gets the non-folder portion of whatever the user has entered
990  * into the file selector. What is returned is a UTF-8 string,
991  * and if a filename path is needed, g_file_get_child_for_display_name()
992  * must be used
993   *
994  * Return value: the entered filename - this value is owned by the
995  *  chooser entry and must not be modified or freed.
996  **/
997 const gchar *
998 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
999 {
1000   const char *last_slash, *text;
1001
1002   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1003
1004   text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1005   last_slash = strrchr (text, G_DIR_SEPARATOR);
1006   if (last_slash)
1007     return last_slash + 1;
1008   else
1009     return text;
1010 }
1011
1012 /**
1013  * _gtk_file_chooser_entry_set_file_part:
1014  * @chooser_entry: a #GtkFileChooserEntry
1015  * @file_part: text to display in the entry, in UTF-8
1016  *
1017  * Sets the current text shown in the file chooser entry.
1018  **/
1019 void
1020 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1021                                        const gchar         *file_part)
1022 {
1023   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1024
1025   chooser_entry->in_change = TRUE;
1026   clear_completions (chooser_entry);
1027   gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1028   chooser_entry->in_change = FALSE;
1029 }
1030
1031
1032 /**
1033  * _gtk_file_chooser_entry_set_action:
1034  * @chooser_entry: a #GtkFileChooserEntry
1035  * @action: the action which is performed by the file selector using this entry
1036  *
1037  * Sets action which is performed by the file selector using this entry. 
1038  * The #GtkFileChooserEntry will use different completion strategies for 
1039  * different actions.
1040  **/
1041 void
1042 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1043                                     GtkFileChooserAction action)
1044 {
1045   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1046   
1047   if (chooser_entry->action != action)
1048     {
1049       GtkEntryCompletion *comp;
1050
1051       chooser_entry->action = action;
1052
1053       comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1054
1055       /* FIXME: do we need to actually set the following? */
1056
1057       switch (action)
1058         {
1059         case GTK_FILE_CHOOSER_ACTION_OPEN:
1060         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1061           gtk_entry_completion_set_popup_single_match (comp, FALSE);
1062           break;
1063         case GTK_FILE_CHOOSER_ACTION_SAVE:
1064         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1065           gtk_entry_completion_set_popup_single_match (comp, TRUE);
1066           break;
1067         }
1068     }
1069 }
1070
1071
1072 /**
1073  * _gtk_file_chooser_entry_get_action:
1074  * @chooser_entry: a #GtkFileChooserEntry
1075  *
1076  * Gets the action for this entry. 
1077  *
1078  * Returns: the action
1079  **/
1080 GtkFileChooserAction
1081 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1082 {
1083   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1084                         GTK_FILE_CHOOSER_ACTION_OPEN);
1085   
1086   return chooser_entry->action;
1087 }
1088
1089 gboolean
1090 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1091                                        GFile               *file)
1092 {
1093   GtkTreeIter iter;
1094   GFileInfo *info;
1095
1096   if (chooser_entry->completion_store == NULL ||
1097       !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1098                                                  &iter,
1099                                                  file))
1100     return FALSE;
1101
1102   info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1103                                           &iter);
1104
1105   return _gtk_file_info_consider_as_directory (info);
1106 }
1107
1108
1109 /*
1110  * _gtk_file_chooser_entry_select_filename:
1111  * @chooser_entry: a #GtkFileChooserEntry
1112  *
1113  * Selects the filename (without the extension) for user edition.
1114  */
1115 void
1116 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1117 {
1118   const gchar *str, *ext;
1119   glong len = -1;
1120
1121   if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1122     {
1123       str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1124       ext = g_strrstr (str, ".");
1125
1126       if (ext)
1127        len = g_utf8_pointer_to_offset (str, ext);
1128     }
1129
1130   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1131 }
1132
1133 void
1134 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1135                                         gboolean             local_only)
1136 {
1137   chooser_entry->local_only = local_only;
1138   clear_completions (chooser_entry);
1139 }
1140
1141 gboolean
1142 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1143 {
1144   return chooser_entry->local_only;
1145 }