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