]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserentry.c
Add a property to suppress the popup for single matches. (#154711)
[~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 #include <string.h>
23
24 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
26 #include "gtkentry.h"
27 #include "gtkfilechooserentry.h"
28 #include "gtkmain.h"
29 #include "gtkalias.h"
30
31 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
32
33 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
34 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
35 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
36
37 struct _GtkFileChooserEntryClass
38 {
39   GtkEntryClass parent_class;
40 };
41
42 struct _GtkFileChooserEntry
43 {
44   GtkEntry parent_instance;
45
46   GtkFileChooserAction action;
47
48   GtkFileSystem *file_system;
49   GtkFilePath *base_folder;
50   GtkFilePath *current_folder_path;
51   gchar *file_part;
52   gint file_part_pos;
53   GSource *check_completion_idle;
54   GSource *load_directory_idle;
55
56   GtkFileFolder *current_folder;
57
58   GtkListStore *completion_store;
59
60   guint has_completion : 1;
61   guint in_change      : 1;
62   guint eat_tabs       : 1;
63 };
64
65 enum
66 {
67   DISPLAY_NAME_COLUMN,
68   PATH_COLUMN,
69   N_COLUMNS
70 };
71
72 static void gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class);
73 static void gtk_file_chooser_entry_iface_init (GtkEditableClass         *iface);
74 static void gtk_file_chooser_entry_init       (GtkFileChooserEntry      *chooser_entry);
75
76 static void     gtk_file_chooser_entry_finalize       (GObject          *object);
77 static gboolean gtk_file_chooser_entry_focus          (GtkWidget        *widget,
78                                                        GtkDirectionType  direction);
79 static void     gtk_file_chooser_entry_activate       (GtkEntry         *entry);
80 static void     gtk_file_chooser_entry_changed        (GtkEditable      *editable);
81 static void     gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
82                                                        const gchar *new_text,
83                                                        gint         new_text_length,
84                                                        gint        *position);
85
86 static void     clear_completion_callback (GtkFileChooserEntry *chooser_entry,
87                                            GParamSpec          *pspec);
88 static gboolean match_selected_callback   (GtkEntryCompletion  *completion,
89                                            GtkTreeModel        *model,
90                                            GtkTreeIter         *iter,
91                                            GtkFileChooserEntry *chooser_entry);
92 static gboolean completion_match_func     (GtkEntryCompletion  *comp,
93                                            const char          *key,
94                                            GtkTreeIter         *iter,
95                                            gpointer             data);
96 static void     files_added_cb            (GtkFileSystem       *file_system,
97                                            GSList              *added_uris,
98                                            GtkFileChooserEntry *chooser_entry);
99 static void     files_deleted_cb          (GtkFileSystem       *file_system,
100                                            GSList              *deleted_uris,
101                                            GtkFileChooserEntry *chooser_entry);
102 static char    *maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
103                                                 GtkFilePath         *path,
104                                                 gchar               *display_name);
105
106 static GObjectClass *parent_class;
107 static GtkEditableClass *parent_editable_iface;
108
109 GType
110 _gtk_file_chooser_entry_get_type (void)
111 {
112   static GType file_chooser_entry_type = 0;
113
114   if (!file_chooser_entry_type)
115     {
116       static const GTypeInfo file_chooser_entry_info =
117       {
118         sizeof (GtkFileChooserEntryClass),
119         NULL,           /* base_init */
120         NULL,           /* base_finalize */
121         (GClassInitFunc) gtk_file_chooser_entry_class_init,
122         NULL,           /* class_finalize */
123         NULL,           /* class_data */
124         sizeof (GtkFileChooserEntry),
125         0,              /* n_preallocs */
126         (GInstanceInitFunc) gtk_file_chooser_entry_init,
127       };
128
129       static const GInterfaceInfo editable_info =
130       {
131         (GInterfaceInitFunc) gtk_file_chooser_entry_iface_init, /* interface_init */
132         NULL,                                                 /* interface_finalize */
133         NULL                                                  /* interface_data */
134       };
135
136
137       file_chooser_entry_type = g_type_register_static (GTK_TYPE_ENTRY, "GtkFileChooserEntry",
138                                                         &file_chooser_entry_info, 0);
139       g_type_add_interface_static (file_chooser_entry_type,
140                                    GTK_TYPE_EDITABLE,
141                                    &editable_info);
142     }
143
144
145   return file_chooser_entry_type;
146 }
147
148 static void
149 gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
150 {
151   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
152   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
153   GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
154
155   parent_class = g_type_class_peek_parent (class);
156
157   gobject_class->finalize = gtk_file_chooser_entry_finalize;
158
159   widget_class->focus = gtk_file_chooser_entry_focus;
160
161   entry_class->activate = gtk_file_chooser_entry_activate;
162 }
163
164 static void
165 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
166 {
167   parent_editable_iface = g_type_interface_peek_parent (iface);
168
169   iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
170   iface->changed = gtk_file_chooser_entry_changed;
171 }
172
173 static void
174 gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
175 {
176   GtkEntryCompletion *comp;
177   GtkCellRenderer *cell;
178
179   comp = gtk_entry_completion_new ();
180   gtk_entry_completion_set_popup_single_match (comp, FALSE);
181
182   gtk_entry_completion_set_match_func (comp,
183                                        completion_match_func,
184                                        chooser_entry,
185                                        NULL);
186
187   cell = gtk_cell_renderer_text_new ();
188   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
189                               cell, TRUE);
190   gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
191                                  cell,
192                                  "text", 0);
193
194   g_signal_connect (comp, "match-selected",
195                     G_CALLBACK (match_selected_callback), chooser_entry);
196
197   gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
198   g_object_unref (comp);
199
200   g_signal_connect (chooser_entry, "notify::cursor-position",
201                     G_CALLBACK (clear_completion_callback), NULL);
202   g_signal_connect (chooser_entry, "notify::selection-bound",
203                     G_CALLBACK (clear_completion_callback), NULL);
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->completion_store)
212     g_object_unref (chooser_entry->completion_store);
213
214   if (chooser_entry->current_folder)
215     {
216       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
217                                             G_CALLBACK (files_added_cb), chooser_entry);
218       g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
219                                             G_CALLBACK (files_deleted_cb), chooser_entry);
220       g_object_unref (chooser_entry->current_folder);
221     }
222
223   if (chooser_entry->file_system)
224     g_object_unref (chooser_entry->file_system);
225
226   gtk_file_path_free (chooser_entry->base_folder);
227   gtk_file_path_free (chooser_entry->current_folder_path);
228   g_free (chooser_entry->file_part);
229
230   parent_class->finalize (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 *display_name;
241   GtkFilePath *path;
242   gint pos;
243   
244   gtk_tree_model_get (model, iter,
245                       DISPLAY_NAME_COLUMN, &display_name,
246                       PATH_COLUMN, &path,
247                       -1);
248
249   if (!display_name || !path)
250     {
251       /* these shouldn't complain if passed NULL */
252       gtk_file_path_free (path);
253       g_free (display_name);
254       return FALSE;
255     }
256
257   display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
258
259   pos = chooser_entry->file_part_pos;
260
261   /* We don't set in_change here as we want to update the current_folder
262    * variable */
263   gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
264                             pos, -1);
265   gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
266                             display_name, -1, 
267                             &pos);
268   gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
269
270   gtk_file_path_free (path);
271   g_free (display_name);
272
273   return TRUE;
274 }
275
276 /* Match function for the GtkEntryCompletion */
277 static gboolean
278 completion_match_func (GtkEntryCompletion *comp,
279                        const char         *key_unused,
280                        GtkTreeIter        *iter,
281                        gpointer            data)
282 {
283   GtkFileChooserEntry *chooser_entry;
284   char *name;
285   gboolean result;
286   char *norm_file_part;
287   char *norm_name;
288
289   chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
290
291   /* We ignore the key because it is the contents of the entry.  Instead, we
292    * just use our precomputed file_part.
293    */
294   if (!chooser_entry->file_part)
295     {
296       return FALSE;
297     }
298
299   gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
300   if (!name)
301     {
302       return FALSE; /* Uninitialized row, ugh */
303     }
304
305   /* If we have an empty file_part, then we're at the root of a directory.  In
306    * that case, we want to match all non-dot files.  We might want to match
307    * dot_files too if show_hidden is TRUE on the fileselector in the future.
308    */
309   /* Additionally, support for gnome .hidden files would be sweet, too */
310   if (chooser_entry->file_part[0] == '\000')
311     {
312       if (name[0] == '.')
313         result = FALSE;
314       else
315         result = TRUE;
316       g_free (name);
317
318       return result;
319     }
320
321
322   norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
323   norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
324
325 #ifdef G_PLATFORM_WIN32
326   {
327     gchar *temp;
328
329     temp = norm_file_part;
330     norm_file_part = g_utf8_casefold (norm_file_part, -1);
331     g_free (temp);
332
333     temp = norm_name;
334     norm_name = g_utf8_casefold (norm_name, -1);
335     g_free (temp);
336   }
337 #endif
338
339   result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
340
341   g_free (norm_file_part);
342   g_free (norm_name);
343   g_free (name);
344   
345   return result;
346 }
347
348 /* This function will append a directory separator to paths to
349  * display_name iff the path associated with it is a directory.
350  * maybe_append_separator_to_path will g_free the display_name and
351  * return a new one if needed.  Otherwise, it will return the old one.
352  * You should be safe calling
353  *
354  * display_name = maybe_append_separator_to_path (entry, path, display_name);
355  * ...
356  * g_free (display_name);
357  */
358 static char *
359 maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
360                                 GtkFilePath         *path,
361                                 gchar               *display_name)
362 {
363   if (path)
364     {
365       GtkFileInfo *info;
366             
367       info = gtk_file_folder_get_info (chooser_entry->current_folder,
368                                        path, NULL); /* NULL-GError */
369
370       if (info)
371         {
372           if (gtk_file_info_get_is_folder (info))
373             {
374               gchar *tmp = display_name;
375               display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
376               g_free (tmp);
377             }
378           
379           gtk_file_info_free (info);
380         }
381
382     }
383
384   return display_name;
385 }
386
387 static gboolean
388 check_completion_callback (GtkFileChooserEntry *chooser_entry)
389 {
390   GtkTreeIter iter;
391   gchar *common_prefix = NULL;
392   GtkFilePath *unique_path = NULL;
393   gboolean valid;
394
395   GDK_THREADS_ENTER ();
396
397   g_assert (chooser_entry->file_part);
398
399   chooser_entry->check_completion_idle = NULL;
400
401   if (strcmp (chooser_entry->file_part, "") == 0)
402     goto done;
403
404   if (chooser_entry->completion_store == NULL)
405     goto done;
406
407   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store),
408                                          &iter);
409
410   while (valid)
411     {
412       gchar *display_name;
413       GtkFilePath *path;
414
415       gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
416                           &iter,
417                           DISPLAY_NAME_COLUMN, &display_name,
418                           PATH_COLUMN, &path,
419                           -1);
420
421       if (g_str_has_prefix (display_name, chooser_entry->file_part))
422         {
423           if (!common_prefix)
424             {
425               common_prefix = g_strdup (display_name);
426               unique_path = gtk_file_path_copy (path);
427             }
428           else
429             {
430               gchar *p = common_prefix;
431               const gchar *q = display_name;
432                   
433               while (*p && *p == *q)
434                 {
435                   p++;
436                   q++;
437                 }
438                   
439               *p = '\0';
440
441               gtk_file_path_free (unique_path);
442               unique_path = NULL;
443             }
444         }
445
446       g_free (display_name);
447       gtk_file_path_free (path);
448       valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store),
449                                         &iter);
450     }
451
452   if (unique_path)
453     {
454       common_prefix = maybe_append_separator_to_path (chooser_entry,
455                                                       unique_path,
456                                                       common_prefix);
457       gtk_file_path_free (unique_path);
458     }
459
460   switch (chooser_entry->action)
461     {
462     case GTK_FILE_CHOOSER_ACTION_SAVE:
463     case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
464       if (common_prefix && !g_str_has_suffix (common_prefix, "/"))
465         {
466           g_free (common_prefix);
467           common_prefix = NULL;
468         }
469       break;
470     default: ;
471     }
472
473   if (common_prefix)
474     {
475       gint file_part_len;
476       gint common_prefix_len;
477       gint pos;
478
479       file_part_len = g_utf8_strlen (chooser_entry->file_part, -1);
480       common_prefix_len = g_utf8_strlen (common_prefix, -1);
481
482       if (common_prefix_len > file_part_len)
483         {
484           pos = chooser_entry->file_part_pos;
485
486           chooser_entry->in_change = TRUE;
487           gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
488                                     pos, -1);
489           gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
490                                     common_prefix, -1, 
491                                     &pos);
492           gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
493                                       chooser_entry->file_part_pos + file_part_len,
494                                       chooser_entry->file_part_pos + common_prefix_len);
495           chooser_entry->in_change = FALSE;
496
497           chooser_entry->has_completion = TRUE;
498         }
499           
500       g_free (common_prefix);
501     }
502
503  done:
504
505   GDK_THREADS_LEAVE ();
506
507   return FALSE;
508 }
509
510 static void
511 add_completion_idle (GtkFileChooserEntry *chooser_entry)
512 {
513   /* idle to update the selection based on the file list */
514   if (chooser_entry->check_completion_idle == NULL)
515     {
516       chooser_entry->check_completion_idle = g_idle_source_new ();
517       g_source_set_priority (chooser_entry->check_completion_idle, G_PRIORITY_HIGH);
518       g_source_set_closure (chooser_entry->check_completion_idle,
519                             g_cclosure_new_object (G_CALLBACK (check_completion_callback),
520                                                    G_OBJECT (chooser_entry)));
521       g_source_attach (chooser_entry->check_completion_idle, NULL);
522     }
523 }
524
525
526 static void
527 update_current_folder_files (GtkFileChooserEntry *chooser_entry,
528                              GSList              *added_uris)
529 {
530   GSList *tmp_list;
531
532   g_assert (chooser_entry->completion_store != NULL);
533
534   /* Bah.  Need to turn off sorting */
535   for (tmp_list = added_uris; tmp_list; tmp_list = tmp_list->next)
536     {
537       GtkFileInfo *info;
538       GtkFilePath *path;
539
540       path = tmp_list->data;
541
542       info = gtk_file_folder_get_info (chooser_entry->current_folder,
543                                        path,
544                                        NULL); /* NULL-GError */
545       if (info)
546         {
547           const gchar *display_name = gtk_file_info_get_display_name (info);
548           GtkTreeIter iter;
549
550           gtk_list_store_append (chooser_entry->completion_store, &iter);
551           gtk_list_store_set (chooser_entry->completion_store, &iter,
552                               DISPLAY_NAME_COLUMN, display_name,
553                               PATH_COLUMN, path,
554                               -1);
555
556           gtk_file_info_free (info);
557         }
558     }
559
560   /* FIXME: we want to turn off sorting temporarily.  I suck... */
561   gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
562                                         DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
563
564   add_completion_idle (chooser_entry);
565 }
566
567 static void
568 files_added_cb (GtkFileSystem       *file_system,
569                 GSList              *added_uris,
570                 GtkFileChooserEntry *chooser_entry)
571 {
572   update_current_folder_files (chooser_entry, added_uris);
573 }
574
575 static void
576 files_deleted_cb (GtkFileSystem       *file_system,
577                   GSList              *deleted_uris,
578                   GtkFileChooserEntry *chooser_entry)
579 {
580   /* FIXME: gravy... */
581 }
582
583 static gboolean
584 load_directory_callback (GtkFileChooserEntry *chooser_entry)
585 {
586   GSList *child_paths = NULL;
587
588   GDK_THREADS_ENTER ();
589
590   chooser_entry->load_directory_idle = NULL;
591
592   /* guard against bogus settings*/
593   if (chooser_entry->current_folder_path == NULL ||
594       chooser_entry->file_system == NULL)
595     goto done;
596
597   if (chooser_entry->current_folder != NULL)
598     {
599       g_warning ("idle activate multiple times without clearing the folder object first.");
600       goto done;
601     }
602   g_assert (chooser_entry->completion_store == NULL);
603
604   /* Load the folder */
605   chooser_entry->current_folder = gtk_file_system_get_folder (chooser_entry->file_system,
606                                                               chooser_entry->current_folder_path,
607                                                               GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
608                                                               NULL); /* NULL-GError */
609
610   /* There is no folder by that name */
611   if (!chooser_entry->current_folder)
612     goto done;
613   g_signal_connect (chooser_entry->current_folder, "files-added",
614                     G_CALLBACK (files_added_cb), chooser_entry);
615   g_signal_connect (chooser_entry->current_folder, "files-removed",
616                     G_CALLBACK (files_deleted_cb), chooser_entry);
617   
618   chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
619                                                         G_TYPE_STRING,
620                                                         GTK_TYPE_FILE_PATH);
621
622   if (chooser_entry->file_part_pos != -1)
623     {
624       gtk_file_folder_list_children (chooser_entry->current_folder,
625                                      &child_paths,
626                                      NULL); /* NULL-GError */
627       if (child_paths)
628         {
629           update_current_folder_files (chooser_entry, child_paths);
630           add_completion_idle (chooser_entry);
631           gtk_file_paths_free (child_paths);
632         }
633     }
634
635   gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
636                                   GTK_TREE_MODEL (chooser_entry->completion_store));
637
638  done:
639   
640   GDK_THREADS_LEAVE ();
641
642   return FALSE;
643 }
644
645 static void
646 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
647                                        const gchar *new_text,
648                                        gint         new_text_length,
649                                        gint        *position)
650 {
651   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
652
653   parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
654
655   if (! chooser_entry->in_change)
656     add_completion_idle (GTK_FILE_CHOOSER_ENTRY (editable));
657 }
658
659 static gboolean
660 gtk_file_chooser_entry_focus (GtkWidget        *widget,
661                               GtkDirectionType  direction)
662 {
663   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
664   GdkModifierType state;
665   gboolean control_pressed = FALSE;
666
667   if (!chooser_entry->eat_tabs)
668     return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
669
670   if (gtk_get_current_event_state (&state))
671     {
672       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
673         control_pressed = TRUE;
674     }
675
676   /* This is a bit evil -- it makes Tab never leave the entry. It basically
677    * makes it 'safe' for people to hit. */
678   if ((direction == GTK_DIR_TAB_FORWARD) &&
679       (GTK_WIDGET_HAS_FOCUS (widget)) &&
680       (! control_pressed))
681     {
682       gint pos = 0;
683
684       if (chooser_entry->has_completion)
685         gtk_editable_set_position (GTK_EDITABLE (widget),
686                                    GTK_ENTRY (widget)->text_length);
687       /* Trigger the completion window to pop up again by a 
688        * zero-length insertion, a bit of a hack.
689        */
690       gtk_editable_insert_text (GTK_EDITABLE (widget), "", -1, &pos);
691
692       return TRUE;
693     }
694   else
695     return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction);
696 }
697
698 static void
699 gtk_file_chooser_entry_activate (GtkEntry *entry)
700 {
701   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
702
703   if (chooser_entry->has_completion)
704     {
705       gtk_editable_set_position (GTK_EDITABLE (entry),
706                                  entry->text_length);
707     }
708   
709   GTK_ENTRY_CLASS (parent_class)->activate (entry);
710 }
711
712 /* This will see if a path typed by the user is new, and installs the loading
713  * idle if it is.
714  */
715 static void
716 gtk_file_chooser_entry_maybe_update_directory (GtkFileChooserEntry *chooser_entry,
717                                                GtkFilePath         *folder_path,
718                                                gboolean             force_reload)
719 {
720   gboolean queue_idle = FALSE;
721
722   if (chooser_entry->current_folder_path)
723     {
724       if (gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0 || force_reload)
725         {
726           /* We changed our current directory.  We need to clear out the old
727            * directory information.
728            */
729           if (chooser_entry->current_folder)
730             {
731               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
732                                                     G_CALLBACK (files_added_cb), chooser_entry);
733               g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
734                                                     G_CALLBACK (files_deleted_cb), chooser_entry);
735
736               g_object_unref (chooser_entry->current_folder);
737               chooser_entry->current_folder = NULL;
738             }
739           if (chooser_entry->completion_store)
740             {
741               gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
742               g_object_unref (chooser_entry->completion_store);
743               chooser_entry->completion_store = NULL;
744             }
745
746           queue_idle = TRUE;
747         }
748
749       gtk_file_path_free (chooser_entry->current_folder_path);
750     }
751   else
752     {
753       queue_idle = TRUE;
754     }
755
756   chooser_entry->current_folder_path = folder_path;
757
758   if (queue_idle && chooser_entry->load_directory_idle == NULL)
759     {
760       chooser_entry->load_directory_idle = g_idle_source_new ();
761       g_source_set_priority (chooser_entry->load_directory_idle, G_PRIORITY_HIGH);
762       g_source_set_closure (chooser_entry->load_directory_idle,
763                             g_cclosure_new_object (G_CALLBACK (load_directory_callback),
764                                                    G_OBJECT (chooser_entry)));
765       g_source_attach (chooser_entry->load_directory_idle, NULL);
766     }
767 }
768
769
770
771 static void
772 gtk_file_chooser_entry_changed (GtkEditable *editable)
773 {
774   GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
775   const gchar *text;
776   GtkFilePath *folder_path;
777   gchar *file_part;
778   gsize total_len, file_part_len;
779   gint file_part_pos;
780
781   if (chooser_entry->in_change)
782     return;
783
784   text = gtk_entry_get_text (GTK_ENTRY (editable));
785   
786   if (!chooser_entry->file_system ||
787       !chooser_entry->base_folder ||
788       !gtk_file_system_parse (chooser_entry->file_system,
789                               chooser_entry->base_folder, text,
790                               &folder_path, &file_part, NULL)) /* NULL-GError */
791     {
792       folder_path = gtk_file_path_copy (chooser_entry->base_folder);
793       file_part = g_strdup ("");
794       file_part_pos = -1;
795     }
796   else
797     {
798       file_part_len = strlen (file_part);
799       total_len = strlen (text);
800       if (total_len > file_part_len)
801         file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
802       else
803         file_part_pos = 0;
804     }
805
806   if (chooser_entry->file_part)
807     g_free (chooser_entry->file_part);
808
809   chooser_entry->file_part = file_part;
810   chooser_entry->file_part_pos = file_part_pos;
811
812   gtk_file_chooser_entry_maybe_update_directory (chooser_entry, folder_path, file_part_pos == -1);
813 }
814
815 static void
816 clear_completion_callback (GtkFileChooserEntry *chooser_entry,
817                            GParamSpec          *pspec)
818 {
819   if (chooser_entry->has_completion)
820     {
821       chooser_entry->has_completion = FALSE;
822       gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
823     }
824 }
825
826 /**
827  * _gtk_file_chooser_entry_new:
828  * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
829  *
830  * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
831  * is an internal implementation widget for the GTK+ file chooser
832  * which is an entry with completion with respect to a
833  * #GtkFileSystem object.
834  *
835  * Return value: the newly created #GtkFileChooserEntry
836  **/
837 GtkWidget *
838 _gtk_file_chooser_entry_new (gboolean eat_tabs)
839 {
840   GtkFileChooserEntry *chooser_entry;
841
842   chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
843   chooser_entry->eat_tabs = (eat_tabs != FALSE);
844
845   return GTK_WIDGET (chooser_entry);
846 }
847
848 /**
849  * _gtk_file_chooser_entry_set_file_system:
850  * @chooser_entry: a #GtkFileChooser
851  * @file_system: an object implementing #GtkFileSystem
852  *
853  * Sets the file system for @chooser_entry.
854  **/
855 void
856 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
857                                          GtkFileSystem       *file_system)
858 {
859   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
860   g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
861
862   if (file_system != chooser_entry->file_system)
863     {
864       if (chooser_entry->file_system)
865         g_object_unref (chooser_entry->file_system);
866
867       chooser_entry->file_system = g_object_ref (file_system);
868     }
869 }
870
871 /**
872  * _gtk_file_chooser_entry_set_base_folder:
873  * @chooser_entry: a #GtkFileChooserEntry
874  * @path: path of a folder in the chooser entries current file system.
875  *
876  * Sets the folder with respect to which completions occur.
877  **/
878 void
879 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
880                                          const GtkFilePath   *path)
881 {
882   if (chooser_entry->base_folder)
883     gtk_file_path_free (chooser_entry->base_folder);
884
885   chooser_entry->base_folder = gtk_file_path_copy (path);
886
887   gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
888   gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, -1);
889 }
890
891 /**
892  * _gtk_file_chooser_entry_get_current_folder:
893  * @chooser_entry: a #GtkFileChooserEntry
894  *
895  * Gets the current folder for the #GtkFileChooserEntry. If the
896  * user has only entered a filename, this will be the base folder
897  * (see _gtk_file_chooser_entry_set_base_folder()), but if the
898  * user has entered a relative or absolute path, then it will
899  * be different. If the user has entered a relative or absolute
900  * path that doesn't point to a folder in the file system, it will
901  * be %NULL.
902  *
903  * Return value: the path of current folder - this value is owned by the
904  *  chooser entry and must not be modified or freed.
905  **/
906 const GtkFilePath *
907 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
908 {
909   if (chooser_entry->has_completion)
910     {
911       gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
912                                  GTK_ENTRY (chooser_entry)->text_length);
913     }
914   return chooser_entry->current_folder_path;
915 }
916
917 /**
918  * _gtk_file_chooser_entry_get_file_part:
919  * @chooser_entry: a #GtkFileChooserEntry
920  *
921  * Gets the non-folder portion of whatever the user has entered
922  * into the file selector. What is returned is a UTF-8 string,
923  * and if a filename path is needed, gtk_file_system_make_path()
924  * must be used
925   *
926  * Return value: the entered filename - this value is owned by the
927  *  chooser entry and must not be modified or freed.
928  **/
929 const gchar *
930 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
931 {
932   if (chooser_entry->has_completion)
933     {
934       gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
935                                  GTK_ENTRY (chooser_entry)->text_length);
936     }
937   return chooser_entry->file_part;
938 }
939
940 /**
941  * _gtk_file_chooser_entry_set_file_part:
942  * @chooser_entry: a #GtkFileChooserEntry
943  * @file_part: text to display in the entry, in UTF-8
944  *
945  * Sets the current text shown in the file chooser entry.
946  **/
947 void
948 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
949                                        const gchar         *file_part)
950 {
951   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
952
953   gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
954 }
955
956
957 /**
958  * _gtk_file_chooser_entry_set_action:
959  * @chooser_entry: a #GtkFileChooserEntry
960  * @action: the action which is performed by the file selector using this entry
961  *
962  * Sets action which is performed by the file selector using this entry. 
963  * The #GtkFileChooserEntry will use different completion strategies for 
964  * different actions.
965  **/
966 void
967 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
968                                     GtkFileChooserAction action)
969 {
970   g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
971   
972   if (chooser_entry->action != action)
973     {
974       GtkEntryCompletion *comp;
975
976       chooser_entry->action = action;
977
978       comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
979
980       switch (action)
981         {
982         case GTK_FILE_CHOOSER_ACTION_OPEN:
983         case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
984           gtk_entry_completion_set_popup_single_match (comp, FALSE);
985           break;
986         case GTK_FILE_CHOOSER_ACTION_SAVE:
987         case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
988           gtk_entry_completion_set_popup_single_match (comp, TRUE);
989           break;
990         }
991     }
992 }
993
994
995 /**
996  * _gtk_file_chooser_entry_get_action:
997  * @chooser_entry: a #GtkFileChooserEntry
998  *
999  * Gets the action for this entry. 
1000  *
1001  * Returns: the action
1002  **/
1003 GtkFileChooserAction
1004 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1005 {
1006   g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1007                         GTK_FILE_CHOOSER_ACTION_OPEN);
1008   
1009   return chooser_entry->action;
1010 }
1011
1012 #define __GTK_FILE_CHOOSER_ENTRY_C__
1013 #include "gtkaliasdef.c"