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