1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserentry.c: Entry with filename completion
3 * Copyright (C) 2003, Red Hat, Inc.
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.
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.
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.
24 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
27 #include "gtkfilechooserentry.h"
32 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
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))
38 struct _GtkFileChooserEntryClass
40 GtkEntryClass parent_class;
43 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
45 LOAD_COMPLETE_NOTHING,
46 LOAD_COMPLETE_AUTOCOMPLETE,
47 LOAD_COMPLETE_INSERT_PREFIX
50 struct _GtkFileChooserEntry
52 GtkEntry parent_instance;
54 GtkFileChooserAction action;
56 GtkFileSystem *file_system;
57 GtkFilePath *base_folder;
60 guint check_completion_idle;
62 /* Folder being loaded or already loaded */
63 GtkFilePath *current_folder_path;
64 GtkFileFolder *current_folder;
65 GtkFileSystemHandle *load_folder_handle;
67 LoadCompleteAction load_complete_action;
69 GtkListStore *completion_store;
71 guint start_autocompletion_idle_id;
73 guint has_completion : 1;
85 static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface);
87 static void gtk_file_chooser_entry_finalize (GObject *object);
88 static void gtk_file_chooser_entry_dispose (GObject *object);
89 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
90 static gboolean gtk_file_chooser_entry_focus (GtkWidget *widget,
91 GtkDirectionType direction);
92 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
93 static void gtk_file_chooser_entry_changed (GtkEditable *editable);
94 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
95 const gchar *new_text,
99 static void clear_completion_callback (GtkFileChooserEntry *chooser_entry,
103 static gint insert_text_callback (GtkFileChooserEntry *widget,
104 const gchar *new_text,
105 gint new_text_length,
108 static void delete_text_callback (GtkFileChooserEntry *widget,
114 static gboolean match_selected_callback (GtkEntryCompletion *completion,
117 GtkFileChooserEntry *chooser_entry);
118 static gboolean completion_match_func (GtkEntryCompletion *comp,
122 static char *maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
124 gchar *display_name);
126 static void finished_loading_cb (GtkFileFolder *folder,
128 static void autocomplete (GtkFileChooserEntry *chooser_entry);
130 static GtkEditableClass *parent_editable_iface;
132 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
133 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
134 gtk_file_chooser_entry_iface_init))
137 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
139 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
140 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
141 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
143 gobject_class->finalize = gtk_file_chooser_entry_finalize;
144 gobject_class->dispose = gtk_file_chooser_entry_dispose;
146 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
147 widget_class->focus = gtk_file_chooser_entry_focus;
149 entry_class->activate = gtk_file_chooser_entry_activate;
153 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
155 parent_editable_iface = g_type_interface_peek_parent (iface);
157 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
158 iface->changed = gtk_file_chooser_entry_changed;
162 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
164 GtkEntryCompletion *comp;
165 GtkCellRenderer *cell;
167 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
169 comp = gtk_entry_completion_new ();
170 gtk_entry_completion_set_popup_single_match (comp, FALSE);
172 gtk_entry_completion_set_match_func (comp,
173 completion_match_func,
177 cell = gtk_cell_renderer_text_new ();
178 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
180 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
184 g_signal_connect (comp, "match_selected",
185 G_CALLBACK (match_selected_callback), chooser_entry);
187 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
188 g_object_unref (comp);
190 g_signal_connect (chooser_entry, "notify::cursor-position",
191 G_CALLBACK (clear_completion_callback), NULL);
192 g_signal_connect (chooser_entry, "notify::selection-bound",
193 G_CALLBACK (clear_completion_callback), NULL);
196 g_signal_connect (chooser_entry, "insert_text",
197 G_CALLBACK (insert_text_callback), NULL);
198 g_signal_connect (chooser_entry, "delete_text",
199 G_CALLBACK (delete_text_callback), NULL);
204 gtk_file_chooser_entry_finalize (GObject *object)
206 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
208 gtk_file_path_free (chooser_entry->base_folder);
209 gtk_file_path_free (chooser_entry->current_folder_path);
210 g_free (chooser_entry->file_part);
212 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
216 gtk_file_chooser_entry_dispose (GObject *object)
218 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
220 if (chooser_entry->start_autocompletion_idle_id != 0)
222 g_source_remove (chooser_entry->start_autocompletion_idle_id);
223 chooser_entry->start_autocompletion_idle_id = 0;
226 if (chooser_entry->completion_store)
228 g_object_unref (chooser_entry->completion_store);
229 chooser_entry->completion_store = NULL;
232 if (chooser_entry->load_folder_handle)
234 gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
235 chooser_entry->load_folder_handle = NULL;
238 if (chooser_entry->current_folder)
240 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
241 G_CALLBACK (finished_loading_cb), chooser_entry);
242 g_object_unref (chooser_entry->current_folder);
243 chooser_entry->current_folder = NULL;
246 if (chooser_entry->file_system)
248 g_object_unref (chooser_entry->file_system);
249 chooser_entry->file_system = NULL;
252 if (chooser_entry->check_completion_idle)
254 g_source_remove (chooser_entry->check_completion_idle);
255 chooser_entry->check_completion_idle = 0;
258 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
261 /* Match functions for the GtkEntryCompletion */
263 match_selected_callback (GtkEntryCompletion *completion,
266 GtkFileChooserEntry *chooser_entry)
272 gtk_tree_model_get (model, iter,
273 DISPLAY_NAME_COLUMN, &display_name,
277 if (!display_name || !path)
279 /* these shouldn't complain if passed NULL */
280 gtk_file_path_free (path);
281 g_free (display_name);
285 display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
287 pos = chooser_entry->file_part_pos;
289 /* We don't set in_change here as we want to update the current_folder
291 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
293 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
296 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
298 gtk_file_path_free (path);
299 g_free (display_name);
304 /* Match function for the GtkEntryCompletion */
306 completion_match_func (GtkEntryCompletion *comp,
307 const char *key_unused,
311 GtkFileChooserEntry *chooser_entry;
314 char *norm_file_part;
317 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
319 /* We ignore the key because it is the contents of the entry. Instead, we
320 * just use our precomputed file_part.
322 if (!chooser_entry->file_part)
327 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
330 return FALSE; /* Uninitialized row, ugh */
333 /* If we have an empty file_part, then we're at the root of a directory. In
334 * that case, we want to match all non-dot files. We might want to match
335 * dot_files too if show_hidden is TRUE on the fileselector in the future.
337 /* Additionally, support for gnome .hidden files would be sweet, too */
338 if (chooser_entry->file_part[0] == '\000')
350 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
351 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
353 #ifdef G_PLATFORM_WIN32
357 temp = norm_file_part;
358 norm_file_part = g_utf8_casefold (norm_file_part, -1);
362 norm_name = g_utf8_casefold (norm_name, -1);
367 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
369 g_free (norm_file_part);
376 /* This function will append a directory separator to paths to
377 * display_name iff the path associated with it is a directory.
378 * maybe_append_separator_to_path will g_free the display_name and
379 * return a new one if needed. Otherwise, it will return the old one.
380 * You should be safe calling
382 * display_name = maybe_append_separator_to_path (entry, path, display_name);
384 * g_free (display_name);
387 maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
391 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && path)
395 info = gtk_file_folder_get_info (chooser_entry->current_folder,
396 path, NULL); /* NULL-GError */
400 if (gtk_file_info_get_is_folder (info))
402 gchar *tmp = display_name;
403 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
407 gtk_file_info_free (info);
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.
419 find_common_prefix (GtkFileChooserEntry *chooser_entry,
420 gchar **common_prefix_ret,
421 GtkFilePath **unique_path_ret)
423 GtkEditable *editable;
427 char *text_up_to_cursor;
428 GtkFilePath *parsed_folder_path;
429 char *parsed_file_part;
431 *common_prefix_ret = NULL;
432 *unique_path_ret = NULL;
434 if (chooser_entry->completion_store == NULL)
437 editable = GTK_EDITABLE (chooser_entry);
439 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
441 parsed = gtk_file_system_parse (chooser_entry->file_system,
442 chooser_entry->base_folder,
446 NULL); /* NULL-GError */
448 printf ("Text up to cursor: \"%s\"\n", text_up_to_cursor);
449 printf ("parsed_folder_path: \"%s\"\nparsed_file_part: \"%s\"\n",
450 parsed_folder_path ? (char *) parsed_folder_path : "<NONE>",
451 parsed_file_part ? parsed_file_part : "<NONE>");
453 g_free (text_up_to_cursor);
458 /* FIXME: assert that the current folder path is the same as the parsed path? */
460 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
467 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
469 DISPLAY_NAME_COLUMN, &display_name,
473 if (g_str_has_prefix (display_name, parsed_file_part))
475 if (!*common_prefix_ret)
477 *common_prefix_ret = g_strdup (display_name);
478 *unique_path_ret = gtk_file_path_copy (path);
482 gchar *p = *common_prefix_ret;
483 const gchar *q = display_name;
485 while (*p && *p == *q)
493 gtk_file_path_free (*unique_path_ret);
494 *unique_path_ret = NULL;
498 g_free (display_name);
499 gtk_file_path_free (path);
500 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
503 gtk_file_path_free (parsed_folder_path);
504 g_free (parsed_file_part);
507 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
509 append_common_prefix (GtkFileChooserEntry *chooser_entry,
512 gchar *common_prefix;
513 GtkFilePath *unique_path;
515 find_common_prefix (chooser_entry, &common_prefix, &unique_path);
519 common_prefix = maybe_append_separator_to_path (chooser_entry,
522 gtk_file_path_free (unique_path);
525 printf ("common prefix: \"%s\"\n",
526 common_prefix ? common_prefix : "<NONE>");
531 gint common_prefix_len;
534 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
535 common_prefix_len = g_utf8_strlen (common_prefix, -1);
537 pos = chooser_entry->file_part_pos;
539 chooser_entry->in_change = TRUE;
540 printf ("Deleting range (%d, %d)\n", pos, cursor_pos);
541 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
543 printf ("Inserting common prefix at %d\n", pos);
544 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
547 chooser_entry->in_change = FALSE;
551 printf ("Selecting range (%d, %d)\n", cursor_pos, pos);
552 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
554 pos); /* cursor_pos + common_prefix_len); */
555 chooser_entry->has_completion = TRUE;
558 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
560 g_free (common_prefix);
565 check_completion_callback (GtkFileChooserEntry *chooser_entry)
567 GDK_THREADS_ENTER ();
569 g_assert (chooser_entry->file_part);
571 chooser_entry->check_completion_idle = 0;
573 if (strcmp (chooser_entry->file_part, "") == 0)
576 /* We only insert the common prefix without requiring the user to hit Tab in
577 * the "open" modes. For "save" modes, the user must hit Tab to cause the prefix
578 * to be inserted. That happens in gtk_file_chooser_entry_focus().
580 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
581 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
582 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length)
583 append_common_prefix (chooser_entry, TRUE);
587 GDK_THREADS_LEAVE ();
593 idle_add (GtkFileChooserEntry *chooser_entry,
599 source = g_idle_source_new ();
600 g_source_set_priority (source, G_PRIORITY_HIGH);
601 g_source_set_closure (source,
602 g_cclosure_new_object (cb, G_OBJECT (chooser_entry)));
603 id = g_source_attach (source, NULL);
604 g_source_unref (source);
610 add_completion_idle (GtkFileChooserEntry *chooser_entry)
612 /* idle to update the selection based on the file list */
613 if (chooser_entry->check_completion_idle == 0)
614 chooser_entry->check_completion_idle =
615 idle_add (chooser_entry, G_CALLBACK (check_completion_callback));
619 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
620 const gchar *new_text,
621 gint new_text_length,
624 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
626 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
628 if (! chooser_entry->in_change)
629 add_completion_idle (GTK_FILE_CHOOSER_ENTRY (editable));
633 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
635 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
636 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
640 gtk_file_chooser_entry_focus (GtkWidget *widget,
641 GtkDirectionType direction)
643 GtkFileChooserEntry *chooser_entry;
644 GtkEditable *editable;
646 GdkModifierType state;
647 gboolean control_pressed;
649 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
650 editable = GTK_EDITABLE (widget);
651 entry = GTK_ENTRY (widget);
653 if (!chooser_entry->eat_tabs)
654 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
656 control_pressed = FALSE;
658 if (gtk_get_current_event_state (&state))
660 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
661 control_pressed = TRUE;
664 /* This is a bit evil -- it makes Tab never leave the entry. It basically
665 * makes it 'safe' for people to hit. */
666 if ((direction == GTK_DIR_TAB_FORWARD) &&
667 (GTK_WIDGET_HAS_FOCUS (widget)) &&
672 if (chooser_entry->has_completion)
676 if (gtk_editable_get_selection_bounds (editable, NULL, &sel_end))
677 gtk_editable_set_position (editable, sel_end);
680 append_common_prefix (chooser_entry, FALSE);
682 /* Trigger the completion window to pop up again by a
683 * zero-length insertion, a bit of a hack.
685 gtk_editable_insert_text (editable, "", -1, &pos);
690 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
694 gtk_file_chooser_entry_activate (GtkEntry *entry)
696 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
698 if (chooser_entry->has_completion)
700 gtk_editable_set_position (GTK_EDITABLE (entry),
704 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
707 /* Fills the completion store from the contents of the current folder */
709 populate_completion_store (GtkFileChooserEntry *chooser_entry)
714 if (!gtk_file_folder_list_children (chooser_entry->current_folder, &paths, NULL)) /* NULL-GError */
717 if (chooser_entry->completion_store)
719 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
720 g_object_unref (chooser_entry->completion_store);
723 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
727 /* Bah. Need to turn off sorting */
728 for (tmp_list = paths; tmp_list; tmp_list = tmp_list->next)
733 path = tmp_list->data;
735 info = gtk_file_folder_get_info (chooser_entry->current_folder,
737 NULL); /* NULL-GError */
740 gchar *display_name = g_strdup (gtk_file_info_get_display_name (info));
743 display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
745 gtk_list_store_append (chooser_entry->completion_store, &iter);
746 gtk_list_store_set (chooser_entry->completion_store, &iter,
747 DISPLAY_NAME_COLUMN, display_name,
751 gtk_file_info_free (info);
752 g_free (display_name);
756 gtk_file_paths_free (paths);
758 /* FIXME: we want to turn off sorting temporarily. I suck... */
759 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
760 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
762 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
763 GTK_TREE_MODEL (chooser_entry->completion_store));
766 /* When we finish loading the current folder, this function should get called to
767 * perform the deferred autocompletion or explicit completion.
770 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
772 switch (chooser_entry->load_complete_action)
774 case LOAD_COMPLETE_NOTHING:
777 case LOAD_COMPLETE_AUTOCOMPLETE:
778 printf ("Load is complete; will autocomplete immediately\n");
779 autocomplete (chooser_entry);
782 case LOAD_COMPLETE_INSERT_PREFIX:
787 g_assert_not_reached ();
790 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
794 finish_folder_load (GtkFileChooserEntry *chooser_entry)
796 populate_completion_store (chooser_entry);
797 perform_load_complete_action (chooser_entry);
800 /* Callback when the current folder finishes loading */
802 finished_loading_cb (GtkFileFolder *folder,
805 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
807 finish_folder_load (chooser_entry);
810 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
812 load_directory_get_folder_callback (GtkFileSystemHandle *handle,
813 GtkFileFolder *folder,
817 gboolean cancelled = handle->cancelled;
818 GtkFileChooserEntry *chooser_entry = data;
820 if (handle != chooser_entry->load_folder_handle)
823 chooser_entry->load_folder_handle = NULL;
825 if (cancelled || error)
828 g_assert (folder != NULL);
829 chooser_entry->current_folder = folder;
831 if (gtk_file_folder_is_finished_loading (chooser_entry->current_folder))
832 finish_folder_load (chooser_entry);
834 g_signal_connect (chooser_entry->current_folder, "finished-loading",
835 G_CALLBACK (finished_loading_cb), chooser_entry);
838 g_object_unref (chooser_entry);
839 g_object_unref (handle);
843 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
845 if (chooser_entry->current_folder_path == NULL ||
846 chooser_entry->file_system == NULL)
849 g_assert (chooser_entry->current_folder == NULL);
850 g_assert (chooser_entry->load_folder_handle == NULL);
852 printf ("Starting async load of folder %s\n", (char *) chooser_entry->current_folder_path);
854 chooser_entry->load_folder_handle =
855 gtk_file_system_get_folder (chooser_entry->file_system,
856 chooser_entry->current_folder_path,
857 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
858 load_directory_get_folder_callback,
859 g_object_ref (chooser_entry));
863 reload_current_folder (GtkFileChooserEntry *chooser_entry,
864 GtkFilePath *folder_path,
865 gboolean force_reload)
867 gboolean reload = FALSE;
869 if (chooser_entry->current_folder_path)
871 if ((folder_path && gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0)
876 /* We changed our current directory. We need to clear out the old
877 * directory information.
879 if (chooser_entry->current_folder)
881 if (chooser_entry->load_folder_handle)
883 printf ("Cancelling folder load\n");
884 gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
885 chooser_entry->load_folder_handle = NULL;
888 g_object_unref (chooser_entry->current_folder);
889 chooser_entry->current_folder = NULL;
892 gtk_file_path_free (chooser_entry->current_folder_path);
893 chooser_entry->current_folder_path = gtk_file_path_copy (folder_path);
898 chooser_entry->current_folder_path = gtk_file_path_copy (folder_path);
903 start_loading_current_folder (chooser_entry);
907 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry)
909 GtkEditable *editable;
910 gchar *text_up_to_cursor;
911 GtkFilePath *folder_path;
913 gsize total_len, file_part_len;
916 editable = GTK_EDITABLE (chooser_entry);
918 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
920 if (!chooser_entry->file_system ||
921 !chooser_entry->base_folder ||
922 !gtk_file_system_parse (chooser_entry->file_system,
923 chooser_entry->base_folder, text_up_to_cursor,
924 &folder_path, &file_part, NULL)) /* NULL-GError */
926 folder_path = gtk_file_path_copy (chooser_entry->base_folder);
927 file_part = g_strdup ("");
932 file_part_len = strlen (file_part);
933 total_len = strlen (text_up_to_cursor);
934 if (total_len > file_part_len)
935 file_part_pos = g_utf8_strlen (text_up_to_cursor, total_len - file_part_len);
940 printf ("Parsed text \"%s\", file_part=\"%s\", file_part_pos=%d, folder_path=\"%s\"\n",
944 folder_path ? (char *) folder_path : "(NULL)");
946 g_free (text_up_to_cursor);
948 g_free (chooser_entry->file_part);
950 chooser_entry->file_part = file_part;
951 chooser_entry->file_part_pos = file_part_pos;
953 reload_current_folder (chooser_entry, folder_path, file_part_pos == -1);
954 gtk_file_path_free (folder_path);
958 autocomplete (GtkFileChooserEntry *chooser_entry)
960 g_assert (chooser_entry->current_folder != NULL);
961 g_assert (gtk_file_folder_is_finished_loading (chooser_entry->current_folder));
962 g_assert (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length);
964 printf ("Doing autocompletion since our folder is finished loading\n");
966 append_common_prefix (chooser_entry, TRUE);
970 start_autocompletion (GtkFileChooserEntry *chooser_entry)
972 printf ("Starting autocompletion\n");
974 refresh_current_folder_and_file_part (chooser_entry);
976 if (!chooser_entry->current_folder)
978 /* We don't beep or anything, since this is autocompletion - the user
979 * didn't request any action explicitly.
981 printf ("No current_folder; not doing autocompletion after all\n");
985 if (gtk_file_folder_is_finished_loading (chooser_entry->current_folder))
987 printf ("File folder is finished loading; doing autocompletion immediately\n");
988 autocomplete (chooser_entry);
992 printf ("File folder is not yet loaded; will do autocompletion later\n");
993 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
998 start_autocompletion_idle_handler (gpointer data)
1000 GtkFileChooserEntry *chooser_entry;
1002 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1004 if (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length)
1005 start_autocompletion (chooser_entry);
1007 chooser_entry->start_autocompletion_idle_id = 0;
1013 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1015 /* We set up an idle handler because we must test the cursor position. However,
1016 * the cursor is not updated in GtkEntry::changed, so we wait for the idle loop.
1019 if (chooser_entry->start_autocompletion_idle_id != 0)
1022 chooser_entry->start_autocompletion_idle_id = g_idle_add (start_autocompletion_idle_handler, chooser_entry);
1026 gtk_file_chooser_entry_changed (GtkEditable *editable)
1028 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
1030 if (chooser_entry->in_change)
1033 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1035 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
1036 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER))
1037 install_start_autocompletion_idle (chooser_entry);
1039 /* FIXME: see when the cursor changes (see GtkEntry's cursor_position property, or GtkEditable::set_position and GtkEntry::move_cursor).
1040 * When the cursor changes, cancel the load_complete_action.
1041 * When the entry is activated, cancel the load_complete_action.
1042 * In general, cancel the load_complete_action when the entry loses the focus.
1047 clear_completion_callback (GtkFileChooserEntry *chooser_entry,
1050 if (chooser_entry->has_completion)
1052 chooser_entry->has_completion = FALSE;
1053 gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
1059 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1060 const gchar *new_text,
1061 gint new_text_length,
1065 const gchar *colon = memchr (new_text, ':', new_text_length);
1068 /* Disallow these characters altogether */
1069 for (i = 0; i < new_text_length; i++)
1071 if (new_text[i] == '<' ||
1072 new_text[i] == '>' ||
1073 new_text[i] == '"' ||
1074 new_text[i] == '|' ||
1075 new_text[i] == '*' ||
1080 if (i < new_text_length ||
1081 /* Disallow entering text that would cause a colon to be anywhere except
1082 * after a drive letter.
1085 *position + (colon - new_text) != 1) ||
1086 (new_text_length > 0 &&
1088 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1089 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1091 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1092 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1100 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1105 /* If deleting a drive letter, delete the colon, too */
1106 if (start_pos == 0 && end_pos == 1 &&
1107 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1108 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1110 g_signal_handlers_block_by_func (chooser_entry,
1111 G_CALLBACK (delete_text_callback),
1113 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1114 g_signal_handlers_unblock_by_func (chooser_entry,
1115 G_CALLBACK (delete_text_callback),
1122 * _gtk_file_chooser_entry_new:
1123 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1125 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1126 * is an internal implementation widget for the GTK+ file chooser
1127 * which is an entry with completion with respect to a
1128 * #GtkFileSystem object.
1130 * Return value: the newly created #GtkFileChooserEntry
1133 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1135 GtkFileChooserEntry *chooser_entry;
1137 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1138 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1140 return GTK_WIDGET (chooser_entry);
1144 * _gtk_file_chooser_entry_set_file_system:
1145 * @chooser_entry: a #GtkFileChooser
1146 * @file_system: an object implementing #GtkFileSystem
1148 * Sets the file system for @chooser_entry.
1151 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1152 GtkFileSystem *file_system)
1154 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1155 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1157 if (file_system != chooser_entry->file_system)
1159 if (chooser_entry->file_system)
1160 g_object_unref (chooser_entry->file_system);
1162 chooser_entry->file_system = g_object_ref (file_system);
1167 * _gtk_file_chooser_entry_set_base_folder:
1168 * @chooser_entry: a #GtkFileChooserEntry
1169 * @path: path of a folder in the chooser entries current file system.
1171 * Sets the folder with respect to which completions occur.
1174 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1175 const GtkFilePath *path)
1177 if (chooser_entry->base_folder)
1178 gtk_file_path_free (chooser_entry->base_folder);
1180 chooser_entry->base_folder = gtk_file_path_copy (path);
1182 gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
1183 _gtk_file_chooser_entry_select_filename (chooser_entry);
1187 * _gtk_file_chooser_entry_get_current_folder:
1188 * @chooser_entry: a #GtkFileChooserEntry
1190 * Gets the current folder for the #GtkFileChooserEntry. If the
1191 * user has only entered a filename, this will be the base folder
1192 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1193 * user has entered a relative or absolute path, then it will
1194 * be different. If the user has entered a relative or absolute
1195 * path that doesn't point to a folder in the file system, it will
1198 * Return value: the path of current folder - this value is owned by the
1199 * chooser entry and must not be modified or freed.
1202 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1204 if (chooser_entry->has_completion)
1206 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1207 GTK_ENTRY (chooser_entry)->text_length);
1209 return chooser_entry->current_folder_path;
1213 * _gtk_file_chooser_entry_get_file_part:
1214 * @chooser_entry: a #GtkFileChooserEntry
1216 * Gets the non-folder portion of whatever the user has entered
1217 * into the file selector. What is returned is a UTF-8 string,
1218 * and if a filename path is needed, gtk_file_system_make_path()
1221 * Return value: the entered filename - this value is owned by the
1222 * chooser entry and must not be modified or freed.
1225 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1227 if (chooser_entry->has_completion)
1229 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1230 GTK_ENTRY (chooser_entry)->text_length);
1232 return chooser_entry->file_part;
1236 * _gtk_file_chooser_entry_set_file_part:
1237 * @chooser_entry: a #GtkFileChooserEntry
1238 * @file_part: text to display in the entry, in UTF-8
1240 * Sets the current text shown in the file chooser entry.
1243 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1244 const gchar *file_part)
1246 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1248 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1253 * _gtk_file_chooser_entry_set_action:
1254 * @chooser_entry: a #GtkFileChooserEntry
1255 * @action: the action which is performed by the file selector using this entry
1257 * Sets action which is performed by the file selector using this entry.
1258 * The #GtkFileChooserEntry will use different completion strategies for
1259 * different actions.
1262 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1263 GtkFileChooserAction action)
1265 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1267 if (chooser_entry->action != action)
1269 GtkEntryCompletion *comp;
1271 chooser_entry->action = action;
1273 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1277 case GTK_FILE_CHOOSER_ACTION_OPEN:
1278 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1279 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1281 case GTK_FILE_CHOOSER_ACTION_SAVE:
1282 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1283 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1291 * _gtk_file_chooser_entry_get_action:
1292 * @chooser_entry: a #GtkFileChooserEntry
1294 * Gets the action for this entry.
1296 * Returns: the action
1298 GtkFileChooserAction
1299 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1301 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1302 GTK_FILE_CHOOSER_ACTION_OPEN);
1304 return chooser_entry->action;
1308 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1309 const GtkFilePath *path)
1311 gboolean retval = FALSE;
1313 if (chooser_entry->current_folder)
1315 GtkFileInfo *file_info;
1317 file_info = gtk_file_folder_get_info (chooser_entry->current_folder,
1321 retval = gtk_file_info_get_is_folder (file_info);
1322 gtk_file_info_free (file_info);
1331 * _gtk_file_chooser_entry_select_filename:
1332 * @chooser_entry: a #GtkFileChooserEntry
1334 * Selects the filename (without the extension) for user edition.
1337 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1339 const gchar *str, *ext;
1342 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1344 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1345 ext = g_strrstr (str, ".");
1348 len = g_utf8_pointer_to_offset (str, ext);
1351 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);