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