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