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