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