]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
filechooserentry: Allow NULL as base folder again
[~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
175   g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
176
177   comp = gtk_entry_completion_new ();
178   gtk_entry_completion_set_popup_single_match (comp, FALSE);
179   gtk_entry_completion_set_minimum_key_length (comp, 0);
180   /* see docs for gtk_entry_completion_set_text_column() */
181   g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
182
183   /* Need a match func here or entry completion uses a wrong one.
184    * We do our own filtering after all. */
185   gtk_entry_completion_set_match_func (comp,
186                                        (GtkEntryCompletionMatchFunc) gtk_true,
187                                        chooser_entry,
188                                        NULL);
189
190   cell = gtk_cell_renderer_text_new ();
191   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
192                               cell, TRUE);
193   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
194                                  cell,
195                                  "text", DISPLAY_NAME_COLUMN);
196
197   g_signal_connect (comp, "match-selected",
198                     G_CALLBACK (match_selected_callback), chooser_entry);
199
200   gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
201   g_object_unref (comp);
202
203 #ifdef G_OS_WIN32
204   g_signal_connect (chooser_entry, "insert-text",
205                     G_CALLBACK (insert_text_callback), NULL);
206   g_signal_connect (chooser_entry, "delete-text",
207                     G_CALLBACK (delete_text_callback), NULL);
208 #endif
209 }
210
211 static void
212 gtk_file_chooser_entry_finalize (GObject *object)
213 {
214   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
215
216   if (chooser_entry->base_folder)
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 if (chooser_entry->base_folder != NULL)
308     file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
309   else
310     file = NULL;
311
312   return file;
313 }
314
315 static GFile *
316 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
317                                          const char *         text)
318 {
319   GFile *file, *parent;
320
321   file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
322
323   if (file == NULL)
324     return NULL;
325
326   if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
327     return file;
328
329   parent = g_file_get_parent (file);
330   g_object_unref (file);
331
332   return parent;
333 }
334
335 /* Finds a common prefix based on the contents of the entry
336  * and mandatorily appends it
337  */
338 static void
339 explicitly_complete (GtkFileChooserEntry *chooser_entry)
340 {
341   chooser_entry->complete_on_load = FALSE;
342
343   if (chooser_entry->completion_store)
344     {
345       char *completion, *text;
346       gsize completion_len, text_len;
347       
348       text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
349       text_len = strlen (text);
350       completion = gtk_entry_completion_compute_prefix (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), text);
351       completion_len = completion ? strlen (completion) : 0;
352
353       if (completion_len > text_len)
354         {
355           GtkEditable *editable = GTK_EDITABLE (chooser_entry);
356           int pos = gtk_editable_get_position (editable);
357
358           gtk_editable_insert_text (editable,
359                                     completion + text_len,
360                                     completion_len - text_len,
361                                     &pos);
362           gtk_editable_set_position (editable, pos);
363           return;
364         }
365     }
366
367   gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
368 }
369
370 static void
371 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
372 {
373   GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
374   _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
375 }
376
377 static void
378 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
379 {
380   if (chooser_entry->current_folder_loaded)
381     explicitly_complete (chooser_entry);
382   else
383     set_complete_on_load (chooser_entry, TRUE);
384 }
385
386 static gboolean
387 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
388                                         GdkEventKey *event)
389 {
390   GtkFileChooserEntry *chooser_entry;
391   GtkEditable *editable;
392   GdkModifierType state;
393   gboolean control_pressed;
394
395   chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
396   editable = GTK_EDITABLE (widget);
397
398   if (!chooser_entry->eat_tabs)
399     return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
400
401   control_pressed = FALSE;
402
403   if (gtk_get_current_event_state (&state))
404     {
405       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
406         control_pressed = TRUE;
407     }
408
409   /* This is a bit evil -- it makes Tab never leave the entry. It basically
410    * makes it 'safe' for people to hit. */
411   if (event->keyval == GDK_KEY_Tab && !control_pressed)
412     {
413       gint start, end;
414
415       gtk_editable_get_selection_bounds (editable, &start, &end);
416       
417       if (start != end)
418         gtk_editable_set_position (editable, MAX (start, end));
419       else
420         start_explicit_completion (chooser_entry);
421
422       return TRUE;
423      }
424
425   return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
426
427 }
428
429 static gboolean
430 gtk_file_chooser_entry_focus_out_event (GtkWidget     *widget,
431                                         GdkEventFocus *event)
432 {
433   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
434
435   set_complete_on_load (chooser_entry, FALSE);
436  
437   return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
438 }
439
440 static void
441 discard_completion_store (GtkFileChooserEntry *chooser_entry)
442 {
443   if (!chooser_entry->completion_store)
444     return;
445
446   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
447   gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
448   g_object_unref (chooser_entry->completion_store);
449   chooser_entry->completion_store = NULL;
450 }
451
452 static gboolean
453 completion_store_set (GtkFileSystemModel  *model,
454                       GFile               *file,
455                       GFileInfo           *info,
456                       int                  column,
457                       GValue              *value,
458                       gpointer             data)
459 {
460   GtkFileChooserEntry *chooser_entry = data;
461
462   const char *prefix = "";
463   const char *suffix = "";
464
465   switch (column)
466     {
467     case FULL_PATH_COLUMN:
468       prefix = chooser_entry->dir_part;
469       /* fall through */
470     case DISPLAY_NAME_COLUMN:
471       if (_gtk_file_info_consider_as_directory (info))
472         suffix = G_DIR_SEPARATOR_S;
473
474       g_value_take_string (value, g_strconcat (
475               prefix,
476               g_file_info_get_display_name (info),
477               suffix,
478               NULL));
479       break;
480     default:
481       g_assert_not_reached ();
482       break;
483     }
484
485   return TRUE;
486 }
487
488 /* Fills the completion store from the contents of the current folder */
489 static void
490 populate_completion_store (GtkFileChooserEntry *chooser_entry)
491 {
492   chooser_entry->completion_store = GTK_TREE_MODEL (
493       _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
494                                                 "standard::name,standard::display-name,standard::type",
495                                                 completion_store_set,
496                                                 chooser_entry,
497                                                 N_COLUMNS,
498                                                 G_TYPE_STRING,
499                                                 G_TYPE_STRING));
500   g_signal_connect (chooser_entry->completion_store, "finished-loading",
501                     G_CALLBACK (finished_loading_cb), chooser_entry);
502
503   _gtk_file_system_model_set_filter_folders (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
504                                              TRUE);
505   _gtk_file_system_model_set_show_files (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
506                                          chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN ||
507                                          chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE);
508   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
509                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
510
511   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
512                                   chooser_entry->completion_store);
513 }
514
515 /* Callback when the current folder finishes loading */
516 static void
517 finished_loading_cb (GtkFileSystemModel  *model,
518                      GError              *error,
519                      GtkFileChooserEntry *chooser_entry)
520 {
521   GtkEntryCompletion *completion;
522
523   chooser_entry->current_folder_loaded = TRUE;
524
525   if (error)
526     {
527       discard_completion_store (chooser_entry);
528       set_complete_on_load (chooser_entry, FALSE);
529       return;
530     }
531
532   if (chooser_entry->complete_on_load)
533     explicitly_complete (chooser_entry);
534
535   gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
536
537   completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
538   gtk_entry_completion_set_inline_completion (completion, TRUE);
539   gtk_entry_completion_complete (completion);
540   gtk_entry_completion_insert_prefix (completion);
541 }
542
543 static void
544 set_completion_folder (GtkFileChooserEntry *chooser_entry,
545                        GFile               *folder_file)
546 {
547   if (folder_file &&
548       chooser_entry->local_only
549       && !g_file_is_native (folder_file))
550     folder_file = NULL;
551
552   if ((chooser_entry->current_folder_file
553        && folder_file
554        && g_file_equal (folder_file, chooser_entry->current_folder_file))
555       || chooser_entry->current_folder_file == folder_file)
556     return;
557
558   if (chooser_entry->current_folder_file)
559     {
560       g_object_unref (chooser_entry->current_folder_file);
561       chooser_entry->current_folder_file = NULL;
562     }
563   
564   chooser_entry->current_folder_loaded = FALSE;
565
566   discard_completion_store (chooser_entry);
567
568   if (folder_file)
569     {
570       chooser_entry->current_folder_file = g_object_ref (folder_file);
571       populate_completion_store (chooser_entry);
572     }
573 }
574
575 static void
576 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry)
577 {
578   GFile *folder_file;
579   char *text, *last_slash, *old_file_part;
580
581   old_file_part = chooser_entry->file_part;
582   g_free (chooser_entry->dir_part);
583
584   text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
585
586   last_slash = strrchr (text, G_DIR_SEPARATOR);
587   if (last_slash)
588     {
589       chooser_entry->dir_part = g_strndup (text, last_slash - text + 1);
590       chooser_entry->file_part = g_strdup (last_slash + 1);
591     }
592   else
593     {
594       chooser_entry->dir_part = g_strdup ("");
595       chooser_entry->file_part = g_strdup (text);
596     }
597
598   folder_file = gtk_file_chooser_get_directory_for_text (chooser_entry, text);
599   set_completion_folder (chooser_entry, folder_file);
600   if (folder_file)
601     g_object_unref (folder_file);
602
603   if (chooser_entry->completion_store &&
604       (g_strcmp0 (old_file_part, chooser_entry->file_part) != 0))
605     {
606       GtkFileFilter *filter;
607       char *pattern;
608
609       filter = gtk_file_filter_new ();
610       pattern = g_strconcat (chooser_entry->file_part, "*", NULL);
611       gtk_file_filter_add_pattern (filter, pattern);
612
613       _gtk_file_system_model_set_filter (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
614                                          filter);
615
616       g_free (pattern);
617       g_object_unref (filter);
618     }
619
620   g_free (text);
621   g_free (old_file_part);
622 }
623
624 #ifdef G_OS_WIN32
625 static gint
626 insert_text_callback (GtkFileChooserEntry *chooser_entry,
627                       const gchar         *new_text,
628                       gint                 new_text_length,
629                       gint                *position,
630                       gpointer             user_data)
631 {
632   const gchar *colon = memchr (new_text, ':', new_text_length);
633   gint i;
634
635   /* Disallow these characters altogether */
636   for (i = 0; i < new_text_length; i++)
637     {
638       if (new_text[i] == '<' ||
639           new_text[i] == '>' ||
640           new_text[i] == '"' ||
641           new_text[i] == '|' ||
642           new_text[i] == '*' ||
643           new_text[i] == '?')
644         break;
645     }
646
647   if (i < new_text_length ||
648       /* Disallow entering text that would cause a colon to be anywhere except
649        * after a drive letter.
650        */
651       (colon != NULL &&
652        *position + (colon - new_text) != 1) ||
653       (new_text_length > 0 &&
654        *position <= 1 &&
655        gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
656        gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
657     {
658       gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
659       g_signal_stop_emission_by_name (chooser_entry, "insert_text");
660       return FALSE;
661     }
662
663   return TRUE;
664 }
665
666 static void
667 delete_text_callback (GtkFileChooserEntry *chooser_entry,
668                       gint                 start_pos,
669                       gint                 end_pos,
670                       gpointer             user_data)
671 {
672   /* If deleting a drive letter, delete the colon, too */
673   if (start_pos == 0 && end_pos == 1 &&
674       gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
675       gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
676     {
677       g_signal_handlers_block_by_func (chooser_entry,
678                                        G_CALLBACK (delete_text_callback),
679                                        user_data);
680       gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
681       g_signal_handlers_unblock_by_func (chooser_entry,
682                                          G_CALLBACK (delete_text_callback),
683                                          user_data);
684     }
685 }
686 #endif
687
688 /**
689  * _gtk_file_chooser_entry_new:
690  * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
691  *
692  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
693  * is an internal implementation widget for the GTK+ file chooser
694  * which is an entry with completion with respect to a
695  * #GtkFileSystem object.
696  *
697  * Return value: the newly created #GtkFileChooserEntry
698  **/
699 GtkWidget *
700 _gtk_file_chooser_entry_new (gboolean       eat_tabs)
701 {
702   GtkFileChooserEntry *chooser_entry;
703
704   chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
705   chooser_entry->eat_tabs = (eat_tabs != FALSE);
706
707   return GTK_WIDGET (chooser_entry);
708 }
709
710 /**
711  * _gtk_file_chooser_entry_set_base_folder:
712  * @chooser_entry: a #GtkFileChooserEntry
713  * @file: file for a folder in the chooser entries current file system.
714  *
715  * Sets the folder with respect to which completions occur.
716  **/
717 void
718 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
719                                          GFile               *file)
720 {
721   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
722   g_return_if_fail (file == NULL || G_IS_FILE (file));
723
724   if (chooser_entry->base_folder == file ||
725       (file != NULL && chooser_entry->base_folder != NULL 
726        && g_file_equal (chooser_entry->base_folder, file)))
727     return;
728
729   if (file)
730     g_object_ref (file);
731
732   if (chooser_entry->base_folder)
733     g_object_unref (chooser_entry->base_folder);
734
735   chooser_entry->base_folder = file;
736
737   refresh_current_folder_and_file_part (chooser_entry);
738 }
739
740 /**
741  * _gtk_file_chooser_entry_get_current_folder:
742  * @chooser_entry: a #GtkFileChooserEntry
743  *
744  * Gets the current folder for the #GtkFileChooserEntry. If the
745  * user has only entered a filename, this will be in the base folder
746  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
747  * user has entered a relative or absolute path, then it will
748  * be different.  If the user has entered unparsable text, or text which
749  * the entry cannot handle, this will return %NULL.
750  *
751  * Return value: the file for the current folder - you must g_object_unref()
752  *   the value after use.
753  **/
754 GFile *
755 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
756 {
757   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
758
759   return gtk_file_chooser_get_directory_for_text (chooser_entry,
760                                                   gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
761 }
762
763 /**
764  * _gtk_file_chooser_entry_get_file_part:
765  * @chooser_entry: a #GtkFileChooserEntry
766  *
767  * Gets the non-folder portion of whatever the user has entered
768  * into the file selector. What is returned is a UTF-8 string,
769  * and if a filename path is needed, g_file_get_child_for_display_name()
770  * must be used
771   *
772  * Return value: the entered filename - this value is owned by the
773  *  chooser entry and must not be modified or freed.
774  **/
775 const gchar *
776 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
777 {
778   const char *last_slash, *text;
779
780   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
781
782   text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
783   last_slash = strrchr (text, G_DIR_SEPARATOR);
784   if (last_slash)
785     return last_slash + 1;
786   else
787     return text;
788 }
789
790 /**
791  * _gtk_file_chooser_entry_set_action:
792  * @chooser_entry: a #GtkFileChooserEntry
793  * @action: the action which is performed by the file selector using this entry
794  *
795  * Sets action which is performed by the file selector using this entry. 
796  * The #GtkFileChooserEntry will use different completion strategies for 
797  * different actions.
798  **/
799 void
800 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
801                                     GtkFileChooserAction action)
802 {
803   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
804   
805   if (chooser_entry->action != action)
806     {
807       GtkEntryCompletion *comp;
808
809       chooser_entry->action = action;
810
811       comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
812
813       /* FIXME: do we need to actually set the following? */
814
815       switch (action)
816         {
817         case GTK_FILE_CHOOSER_ACTION_OPEN:
818         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
819           gtk_entry_completion_set_popup_single_match (comp, FALSE);
820           break;
821         case GTK_FILE_CHOOSER_ACTION_SAVE:
822         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
823           gtk_entry_completion_set_popup_single_match (comp, TRUE);
824           break;
825         }
826
827       if (chooser_entry->completion_store)
828         _gtk_file_system_model_set_show_files (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
829                                                action == GTK_FILE_CHOOSER_ACTION_OPEN ||
830                                                action == GTK_FILE_CHOOSER_ACTION_SAVE);
831     }
832 }
833
834
835 /**
836  * _gtk_file_chooser_entry_get_action:
837  * @chooser_entry: a #GtkFileChooserEntry
838  *
839  * Gets the action for this entry. 
840  *
841  * Returns: the action
842  **/
843 GtkFileChooserAction
844 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
845 {
846   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
847                         GTK_FILE_CHOOSER_ACTION_OPEN);
848   
849   return chooser_entry->action;
850 }
851
852 gboolean
853 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
854                                        GFile               *file)
855 {
856   GtkTreeIter iter;
857   GFileInfo *info;
858
859   if (chooser_entry->completion_store == NULL ||
860       !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
861                                                  &iter,
862                                                  file))
863     return FALSE;
864
865   info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
866                                           &iter);
867
868   return _gtk_file_info_consider_as_directory (info);
869 }
870
871
872 /*
873  * _gtk_file_chooser_entry_select_filename:
874  * @chooser_entry: a #GtkFileChooserEntry
875  *
876  * Selects the filename (without the extension) for user edition.
877  */
878 void
879 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
880 {
881   const gchar *str, *ext;
882   glong len = -1;
883
884   if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
885     {
886       str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
887       ext = g_strrstr (str, ".");
888
889       if (ext)
890        len = g_utf8_pointer_to_offset (str, ext);
891     }
892
893   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
894 }
895
896 void
897 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
898                                         gboolean             local_only)
899 {
900   chooser_entry->local_only = local_only;
901   refresh_current_folder_and_file_part (chooser_entry);
902 }
903
904 gboolean
905 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
906 {
907   return chooser_entry->local_only;
908 }