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