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