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 void files_added_cb (GtkFileSystem *file_system,
124 GtkFileChooserEntry *chooser_entry);
125 static void files_deleted_cb (GtkFileSystem *file_system,
126 GSList *deleted_uris,
127 GtkFileChooserEntry *chooser_entry);
128 static char *maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
130 gchar *display_name);
132 static GtkEditableClass *parent_editable_iface;
134 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
135 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
136 gtk_file_chooser_entry_iface_init))
139 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
141 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
142 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
143 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
145 gobject_class->finalize = gtk_file_chooser_entry_finalize;
146 gobject_class->dispose = gtk_file_chooser_entry_dispose;
148 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
149 widget_class->focus = gtk_file_chooser_entry_focus;
151 entry_class->activate = gtk_file_chooser_entry_activate;
155 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
157 parent_editable_iface = g_type_interface_peek_parent (iface);
159 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
160 iface->changed = gtk_file_chooser_entry_changed;
164 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
166 GtkEntryCompletion *comp;
167 GtkCellRenderer *cell;
169 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
171 comp = gtk_entry_completion_new ();
172 gtk_entry_completion_set_popup_single_match (comp, FALSE);
174 gtk_entry_completion_set_match_func (comp,
175 completion_match_func,
179 cell = gtk_cell_renderer_text_new ();
180 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
182 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
186 g_signal_connect (comp, "match_selected",
187 G_CALLBACK (match_selected_callback), chooser_entry);
189 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
190 g_object_unref (comp);
192 g_signal_connect (chooser_entry, "notify::cursor-position",
193 G_CALLBACK (clear_completion_callback), NULL);
194 g_signal_connect (chooser_entry, "notify::selection-bound",
195 G_CALLBACK (clear_completion_callback), NULL);
198 g_signal_connect (chooser_entry, "insert_text",
199 G_CALLBACK (insert_text_callback), NULL);
200 g_signal_connect (chooser_entry, "delete_text",
201 G_CALLBACK (delete_text_callback), NULL);
206 gtk_file_chooser_entry_finalize (GObject *object)
208 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
210 gtk_file_path_free (chooser_entry->base_folder);
211 gtk_file_path_free (chooser_entry->current_folder_path);
212 g_free (chooser_entry->file_part);
214 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
218 gtk_file_chooser_entry_dispose (GObject *object)
220 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
222 if (chooser_entry->start_autocompletion_idle_id != 0)
224 g_source_remove (chooser_entry->start_autocompletion_idle_id);
225 chooser_entry->start_autocompletion_idle_id = 0;
228 if (chooser_entry->completion_store)
230 g_object_unref (chooser_entry->completion_store);
231 chooser_entry->completion_store = NULL;
234 if (chooser_entry->load_folder_handle)
236 gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
237 chooser_entry->load_folder_handle = NULL;
240 if (chooser_entry->current_folder)
242 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
243 G_CALLBACK (files_added_cb), chooser_entry);
244 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
245 G_CALLBACK (files_deleted_cb), chooser_entry);
246 g_object_unref (chooser_entry->current_folder);
247 chooser_entry->current_folder = NULL;
250 if (chooser_entry->file_system)
252 g_object_unref (chooser_entry->file_system);
253 chooser_entry->file_system = NULL;
256 if (chooser_entry->check_completion_idle)
258 g_source_remove (chooser_entry->check_completion_idle);
259 chooser_entry->check_completion_idle = 0;
262 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
265 /* Match functions for the GtkEntryCompletion */
267 match_selected_callback (GtkEntryCompletion *completion,
270 GtkFileChooserEntry *chooser_entry)
276 gtk_tree_model_get (model, iter,
277 DISPLAY_NAME_COLUMN, &display_name,
281 if (!display_name || !path)
283 /* these shouldn't complain if passed NULL */
284 gtk_file_path_free (path);
285 g_free (display_name);
289 display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
291 pos = chooser_entry->file_part_pos;
293 /* We don't set in_change here as we want to update the current_folder
295 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
297 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
300 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
302 gtk_file_path_free (path);
303 g_free (display_name);
308 /* Match function for the GtkEntryCompletion */
310 completion_match_func (GtkEntryCompletion *comp,
311 const char *key_unused,
315 GtkFileChooserEntry *chooser_entry;
318 char *norm_file_part;
321 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
323 /* We ignore the key because it is the contents of the entry. Instead, we
324 * just use our precomputed file_part.
326 if (!chooser_entry->file_part)
331 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
334 return FALSE; /* Uninitialized row, ugh */
337 /* If we have an empty file_part, then we're at the root of a directory. In
338 * that case, we want to match all non-dot files. We might want to match
339 * dot_files too if show_hidden is TRUE on the fileselector in the future.
341 /* Additionally, support for gnome .hidden files would be sweet, too */
342 if (chooser_entry->file_part[0] == '\000')
354 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
355 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
357 #ifdef G_PLATFORM_WIN32
361 temp = norm_file_part;
362 norm_file_part = g_utf8_casefold (norm_file_part, -1);
366 norm_name = g_utf8_casefold (norm_name, -1);
371 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
373 g_free (norm_file_part);
380 /* This function will append a directory separator to paths to
381 * display_name iff the path associated with it is a directory.
382 * maybe_append_separator_to_path will g_free the display_name and
383 * return a new one if needed. Otherwise, it will return the old one.
384 * You should be safe calling
386 * display_name = maybe_append_separator_to_path (entry, path, display_name);
388 * g_free (display_name);
391 maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
395 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && path)
399 info = gtk_file_folder_get_info (chooser_entry->current_folder,
400 path, NULL); /* NULL-GError */
404 if (gtk_file_info_get_is_folder (info))
406 gchar *tmp = display_name;
407 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
411 gtk_file_info_free (info);
418 /* Determines if the completion model has entries with a common prefix relative
419 * to the current contents of the entry. Also, if there's one and only one such
420 * path, stores it in unique_path_ret.
423 find_common_prefix (GtkFileChooserEntry *chooser_entry,
424 gchar **common_prefix_ret,
425 GtkFilePath **unique_path_ret)
427 GtkEditable *editable;
431 char *text_up_to_cursor;
432 GtkFilePath *parsed_folder_path;
433 char *parsed_file_part;
435 *common_prefix_ret = NULL;
436 *unique_path_ret = NULL;
438 if (chooser_entry->completion_store == NULL)
441 editable = GTK_EDITABLE (chooser_entry);
443 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
445 parsed = gtk_file_system_parse (chooser_entry->file_system,
446 chooser_entry->base_folder,
450 NULL); /* NULL-GError */
452 g_free (text_up_to_cursor);
457 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
464 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
466 DISPLAY_NAME_COLUMN, &display_name,
470 if (g_str_has_prefix (display_name, parsed_file_part))
472 if (!*common_prefix_ret)
474 *common_prefix_ret = g_strdup (display_name);
475 *unique_path_ret = gtk_file_path_copy (path);
479 gchar *p = *common_prefix_ret;
480 const gchar *q = display_name;
482 while (*p && *p == *q)
490 gtk_file_path_free (*unique_path_ret);
491 *unique_path_ret = NULL;
495 g_free (display_name);
496 gtk_file_path_free (path);
497 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
500 gtk_file_path_free (parsed_folder_path);
501 g_free (parsed_file_part);
504 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
506 append_common_prefix (GtkFileChooserEntry *chooser_entry,
509 gchar *common_prefix;
510 GtkFilePath *unique_path;
512 find_common_prefix (chooser_entry, &common_prefix, &unique_path);
516 common_prefix = maybe_append_separator_to_path (chooser_entry,
519 gtk_file_path_free (unique_path);
522 printf ("common prefix: \"%s\"\n",
523 common_prefix ? common_prefix : "<NONE>");
528 gint common_prefix_len;
531 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
532 common_prefix_len = g_utf8_strlen (common_prefix, -1);
534 pos = chooser_entry->file_part_pos;
536 chooser_entry->in_change = TRUE;
537 printf ("Deleting range (%d, %d)\n", pos, cursor_pos);
538 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
540 printf ("Inserting common prefix at %d\n", pos);
541 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
544 chooser_entry->in_change = FALSE;
548 printf ("Selecting range (%d, %d)\n", cursor_pos, pos);
549 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
551 pos); /* cursor_pos + common_prefix_len); */
552 chooser_entry->has_completion = TRUE;
555 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
557 g_free (common_prefix);
562 check_completion_callback (GtkFileChooserEntry *chooser_entry)
564 GDK_THREADS_ENTER ();
566 g_assert (chooser_entry->file_part);
568 chooser_entry->check_completion_idle = 0;
570 if (strcmp (chooser_entry->file_part, "") == 0)
573 /* We only insert the common prefix without requiring the user to hit Tab in
574 * the "open" modes. For "save" modes, the user must hit Tab to cause the prefix
575 * to be inserted. That happens in gtk_file_chooser_entry_focus().
577 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
578 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
579 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length)
580 append_common_prefix (chooser_entry, TRUE);
584 GDK_THREADS_LEAVE ();
590 idle_add (GtkFileChooserEntry *chooser_entry,
596 source = g_idle_source_new ();
597 g_source_set_priority (source, G_PRIORITY_HIGH);
598 g_source_set_closure (source,
599 g_cclosure_new_object (cb, G_OBJECT (chooser_entry)));
600 id = g_source_attach (source, NULL);
601 g_source_unref (source);
607 add_completion_idle (GtkFileChooserEntry *chooser_entry)
609 /* idle to update the selection based on the file list */
610 if (chooser_entry->check_completion_idle == 0)
611 chooser_entry->check_completion_idle =
612 idle_add (chooser_entry, G_CALLBACK (check_completion_callback));
617 update_current_folder_files (GtkFileChooserEntry *chooser_entry,
622 g_assert (chooser_entry->completion_store != NULL);
624 /* Bah. Need to turn off sorting */
625 for (tmp_list = added_uris; tmp_list; tmp_list = tmp_list->next)
630 path = tmp_list->data;
632 info = gtk_file_folder_get_info (chooser_entry->current_folder,
634 NULL); /* NULL-GError */
637 gchar *display_name = g_strdup (gtk_file_info_get_display_name (info));
640 display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
642 gtk_list_store_append (chooser_entry->completion_store, &iter);
643 gtk_list_store_set (chooser_entry->completion_store, &iter,
644 DISPLAY_NAME_COLUMN, display_name,
648 gtk_file_info_free (info);
649 g_free (display_name);
653 /* FIXME: we want to turn off sorting temporarily. I suck... */
654 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
655 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
657 add_completion_idle (chooser_entry);
661 files_added_cb (GtkFileSystem *file_system,
663 GtkFileChooserEntry *chooser_entry)
665 update_current_folder_files (chooser_entry, added_uris);
669 files_deleted_cb (GtkFileSystem *file_system,
670 GSList *deleted_uris,
671 GtkFileChooserEntry *chooser_entry)
673 /* FIXME: gravy... */
677 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
678 const gchar *new_text,
679 gint new_text_length,
682 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
684 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
686 if (! chooser_entry->in_change)
687 add_completion_idle (GTK_FILE_CHOOSER_ENTRY (editable));
691 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
693 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
694 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
698 gtk_file_chooser_entry_focus (GtkWidget *widget,
699 GtkDirectionType direction)
701 GtkFileChooserEntry *chooser_entry;
702 GtkEditable *editable;
704 GdkModifierType state;
705 gboolean control_pressed;
707 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
708 editable = GTK_EDITABLE (widget);
709 entry = GTK_ENTRY (widget);
711 if (!chooser_entry->eat_tabs)
712 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
714 control_pressed = FALSE;
716 if (gtk_get_current_event_state (&state))
718 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
719 control_pressed = TRUE;
722 /* This is a bit evil -- it makes Tab never leave the entry. It basically
723 * makes it 'safe' for people to hit. */
724 if ((direction == GTK_DIR_TAB_FORWARD) &&
725 (GTK_WIDGET_HAS_FOCUS (widget)) &&
730 if (chooser_entry->has_completion)
734 if (gtk_editable_get_selection_bounds (editable, NULL, &sel_end))
735 gtk_editable_set_position (editable, sel_end);
738 append_common_prefix (chooser_entry, FALSE);
740 /* Trigger the completion window to pop up again by a
741 * zero-length insertion, a bit of a hack.
743 gtk_editable_insert_text (editable, "", -1, &pos);
748 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
752 gtk_file_chooser_entry_activate (GtkEntry *entry)
754 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
756 if (chooser_entry->has_completion)
758 gtk_editable_set_position (GTK_EDITABLE (entry),
762 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
766 load_directory_get_folder_callback (GtkFileSystemHandle *handle,
767 GtkFileFolder *folder,
771 gboolean cancelled = handle->cancelled;
772 GtkFileChooserEntry *chooser_entry = data;
774 if (handle != chooser_entry->load_folder_handle)
777 chooser_entry->load_folder_handle = NULL;
779 if (cancelled || error)
782 /* FIXME: connect to "finished-loading". In that callback, see our
783 * load_complete_action and do the appropriate thing.
785 * Do we need to populate the completion store here? (O(n^2))
786 * Maybe we should wait until the folder is finished loading.
789 chooser_entry->current_folder = folder;
790 g_signal_connect (chooser_entry->current_folder, "files-added",
791 G_CALLBACK (files_added_cb), chooser_entry);
792 g_signal_connect (chooser_entry->current_folder, "files-removed",
793 G_CALLBACK (files_deleted_cb), chooser_entry);
795 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
799 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
800 GTK_TREE_MODEL (chooser_entry->completion_store));
803 g_object_unref (chooser_entry);
804 g_object_unref (handle);
808 load_current_folder (GtkFileChooserEntry *chooser_entry)
810 if (chooser_entry->current_folder_path == NULL ||
811 chooser_entry->file_system == NULL)
814 g_assert (chooser_entry->current_folder == NULL);
815 g_assert (chooser_entry->completion_store == NULL);
816 g_assert (chooser_entry->load_folder_handle == NULL);
818 printf ("Starting async load of folder %s\n", (char *) chooser_entry->current_folder_path);
820 chooser_entry->load_folder_handle =
821 gtk_file_system_get_folder (chooser_entry->file_system,
822 chooser_entry->current_folder_path,
823 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
824 load_directory_get_folder_callback,
825 g_object_ref (chooser_entry));
829 reload_current_folder (GtkFileChooserEntry *chooser_entry,
830 GtkFilePath *folder_path,
831 gboolean force_reload)
833 gboolean reload = FALSE;
835 if (chooser_entry->current_folder_path)
837 if ((folder_path && gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0)
842 /* We changed our current directory. We need to clear out the old
843 * directory information.
845 if (chooser_entry->current_folder)
847 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
848 G_CALLBACK (files_added_cb), chooser_entry);
849 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
850 G_CALLBACK (files_deleted_cb), chooser_entry);
852 if (chooser_entry->load_folder_handle)
854 printf ("Cancelling folder load\n");
855 gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
856 chooser_entry->load_folder_handle = NULL;
859 g_object_unref (chooser_entry->current_folder);
860 chooser_entry->current_folder = NULL;
863 if (chooser_entry->completion_store)
865 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
866 g_object_unref (chooser_entry->completion_store);
867 chooser_entry->completion_store = NULL;
870 gtk_file_path_free (chooser_entry->current_folder_path);
871 chooser_entry->current_folder_path = gtk_file_path_copy (folder_path);
876 chooser_entry->current_folder_path = gtk_file_path_copy (folder_path);
881 load_current_folder (chooser_entry);
885 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry)
887 GtkEditable *editable;
888 gchar *text_up_to_cursor;
889 GtkFilePath *folder_path;
891 gsize total_len, file_part_len;
894 editable = GTK_EDITABLE (chooser_entry);
896 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
898 if (!chooser_entry->file_system ||
899 !chooser_entry->base_folder ||
900 !gtk_file_system_parse (chooser_entry->file_system,
901 chooser_entry->base_folder, text_up_to_cursor,
902 &folder_path, &file_part, NULL)) /* NULL-GError */
904 folder_path = gtk_file_path_copy (chooser_entry->base_folder);
905 file_part = g_strdup ("");
910 file_part_len = strlen (file_part);
911 total_len = strlen (text_up_to_cursor);
912 if (total_len > file_part_len)
913 file_part_pos = g_utf8_strlen (text_up_to_cursor, total_len - file_part_len);
918 printf ("Parsed text \"%s\", file_part=\"%s\", file_part_pos=%d, folder_path=\"%s\"\n",
922 folder_path ? (char *) folder_path : "(NULL)");
924 g_free (text_up_to_cursor);
926 g_free (chooser_entry->file_part);
928 chooser_entry->file_part = file_part;
929 chooser_entry->file_part_pos = file_part_pos;
931 reload_current_folder (chooser_entry, folder_path, file_part_pos == -1);
932 gtk_file_path_free (folder_path);
936 autocomplete (GtkFileChooserEntry *chooser_entry)
938 g_assert (chooser_entry->current_folder != NULL);
939 g_assert (gtk_file_folder_is_finished_loading (chooser_entry->current_folder));
940 g_assert (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length);
942 printf ("Doing autocompletion since our folder is finished loading\n");
944 append_common_prefix (chooser_entry, TRUE);
948 start_autocompletion (GtkFileChooserEntry *chooser_entry)
950 printf ("Starting autocompletion\n");
952 refresh_current_folder_and_file_part (chooser_entry);
954 if (!chooser_entry->current_folder)
956 /* We don't beep or anything, since this is autocompletion - the user
957 * didn't request any action explicitly.
959 printf ("No current_folder; not doing autocompletion after all\n");
963 if (gtk_file_folder_is_finished_loading (chooser_entry->current_folder))
965 printf ("File folder is finished loading; doing autocompletion immediately\n");
966 autocomplete (chooser_entry);
970 printf ("File folder is not yet loaded; will do autocompletion later\n");
971 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
976 start_autocompletion_idle_handler (gpointer data)
978 GtkFileChooserEntry *chooser_entry;
980 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
982 if (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length)
983 start_autocompletion (chooser_entry);
985 chooser_entry->start_autocompletion_idle_id = 0;
991 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
993 /* We set up an idle handler because we must test the cursor position. However,
994 * the cursor is not updated in GtkEntry::changed, so we wait for the idle loop.
997 if (chooser_entry->start_autocompletion_idle_id != 0)
1000 chooser_entry->start_autocompletion_idle_id = g_idle_add (start_autocompletion_idle_handler, chooser_entry);
1004 gtk_file_chooser_entry_changed (GtkEditable *editable)
1006 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
1008 if (chooser_entry->in_change)
1011 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1013 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
1014 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER))
1015 install_start_autocompletion_idle (chooser_entry);
1017 /* FIXME: see when the cursor changes (see GtkEntry's cursor_position property, or GtkEditable::set_position and GtkEntry::move_cursor).
1018 * When the cursor changes, cancel the load_complete_action.
1019 * When the entry is activated, cancel the load_complete_action.
1020 * In general, cancel the load_complete_action when the entry loses the focus.
1025 clear_completion_callback (GtkFileChooserEntry *chooser_entry,
1028 if (chooser_entry->has_completion)
1030 chooser_entry->has_completion = FALSE;
1031 gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
1037 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1038 const gchar *new_text,
1039 gint new_text_length,
1043 const gchar *colon = memchr (new_text, ':', new_text_length);
1046 /* Disallow these characters altogether */
1047 for (i = 0; i < new_text_length; i++)
1049 if (new_text[i] == '<' ||
1050 new_text[i] == '>' ||
1051 new_text[i] == '"' ||
1052 new_text[i] == '|' ||
1053 new_text[i] == '*' ||
1058 if (i < new_text_length ||
1059 /* Disallow entering text that would cause a colon to be anywhere except
1060 * after a drive letter.
1063 *position + (colon - new_text) != 1) ||
1064 (new_text_length > 0 &&
1066 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1067 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1069 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1070 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1078 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1083 /* If deleting a drive letter, delete the colon, too */
1084 if (start_pos == 0 && end_pos == 1 &&
1085 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1086 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1088 g_signal_handlers_block_by_func (chooser_entry,
1089 G_CALLBACK (delete_text_callback),
1091 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1092 g_signal_handlers_unblock_by_func (chooser_entry,
1093 G_CALLBACK (delete_text_callback),
1100 * _gtk_file_chooser_entry_new:
1101 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1103 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1104 * is an internal implementation widget for the GTK+ file chooser
1105 * which is an entry with completion with respect to a
1106 * #GtkFileSystem object.
1108 * Return value: the newly created #GtkFileChooserEntry
1111 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1113 GtkFileChooserEntry *chooser_entry;
1115 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1116 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1118 return GTK_WIDGET (chooser_entry);
1122 * _gtk_file_chooser_entry_set_file_system:
1123 * @chooser_entry: a #GtkFileChooser
1124 * @file_system: an object implementing #GtkFileSystem
1126 * Sets the file system for @chooser_entry.
1129 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1130 GtkFileSystem *file_system)
1132 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1133 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1135 if (file_system != chooser_entry->file_system)
1137 if (chooser_entry->file_system)
1138 g_object_unref (chooser_entry->file_system);
1140 chooser_entry->file_system = g_object_ref (file_system);
1145 * _gtk_file_chooser_entry_set_base_folder:
1146 * @chooser_entry: a #GtkFileChooserEntry
1147 * @path: path of a folder in the chooser entries current file system.
1149 * Sets the folder with respect to which completions occur.
1152 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1153 const GtkFilePath *path)
1155 if (chooser_entry->base_folder)
1156 gtk_file_path_free (chooser_entry->base_folder);
1158 chooser_entry->base_folder = gtk_file_path_copy (path);
1160 gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
1161 _gtk_file_chooser_entry_select_filename (chooser_entry);
1165 * _gtk_file_chooser_entry_get_current_folder:
1166 * @chooser_entry: a #GtkFileChooserEntry
1168 * Gets the current folder for the #GtkFileChooserEntry. If the
1169 * user has only entered a filename, this will be the base folder
1170 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1171 * user has entered a relative or absolute path, then it will
1172 * be different. If the user has entered a relative or absolute
1173 * path that doesn't point to a folder in the file system, it will
1176 * Return value: the path of current folder - this value is owned by the
1177 * chooser entry and must not be modified or freed.
1180 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1182 if (chooser_entry->has_completion)
1184 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1185 GTK_ENTRY (chooser_entry)->text_length);
1187 return chooser_entry->current_folder_path;
1191 * _gtk_file_chooser_entry_get_file_part:
1192 * @chooser_entry: a #GtkFileChooserEntry
1194 * Gets the non-folder portion of whatever the user has entered
1195 * into the file selector. What is returned is a UTF-8 string,
1196 * and if a filename path is needed, gtk_file_system_make_path()
1199 * Return value: the entered filename - this value is owned by the
1200 * chooser entry and must not be modified or freed.
1203 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1205 if (chooser_entry->has_completion)
1207 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1208 GTK_ENTRY (chooser_entry)->text_length);
1210 return chooser_entry->file_part;
1214 * _gtk_file_chooser_entry_set_file_part:
1215 * @chooser_entry: a #GtkFileChooserEntry
1216 * @file_part: text to display in the entry, in UTF-8
1218 * Sets the current text shown in the file chooser entry.
1221 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1222 const gchar *file_part)
1224 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1226 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1231 * _gtk_file_chooser_entry_set_action:
1232 * @chooser_entry: a #GtkFileChooserEntry
1233 * @action: the action which is performed by the file selector using this entry
1235 * Sets action which is performed by the file selector using this entry.
1236 * The #GtkFileChooserEntry will use different completion strategies for
1237 * different actions.
1240 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1241 GtkFileChooserAction action)
1243 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1245 if (chooser_entry->action != action)
1247 GtkEntryCompletion *comp;
1249 chooser_entry->action = action;
1251 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1255 case GTK_FILE_CHOOSER_ACTION_OPEN:
1256 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1257 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1259 case GTK_FILE_CHOOSER_ACTION_SAVE:
1260 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1261 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1269 * _gtk_file_chooser_entry_get_action:
1270 * @chooser_entry: a #GtkFileChooserEntry
1272 * Gets the action for this entry.
1274 * Returns: the action
1276 GtkFileChooserAction
1277 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1279 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1280 GTK_FILE_CHOOSER_ACTION_OPEN);
1282 return chooser_entry->action;
1286 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1287 const GtkFilePath *path)
1289 gboolean retval = FALSE;
1291 if (chooser_entry->current_folder)
1293 GtkFileInfo *file_info;
1295 file_info = gtk_file_folder_get_info (chooser_entry->current_folder,
1299 retval = gtk_file_info_get_is_folder (file_info);
1300 gtk_file_info_free (file_info);
1309 * _gtk_file_chooser_entry_select_filename:
1310 * @chooser_entry: a #GtkFileChooserEntry
1312 * Selects the filename (without the extension) for user edition.
1315 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1317 const gchar *str, *ext;
1320 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1322 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1323 ext = g_strrstr (str, ".");
1326 len = g_utf8_pointer_to_offset (str, ext);
1329 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);