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);
525 gint common_prefix_len;
528 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
529 common_prefix_len = g_utf8_strlen (common_prefix, -1);
531 pos = chooser_entry->file_part_pos;
533 chooser_entry->in_change = TRUE;
534 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
536 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
539 chooser_entry->in_change = FALSE;
543 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
545 pos); /* cursor_pos + common_prefix_len); */
546 chooser_entry->has_completion = TRUE;
549 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
551 g_free (common_prefix);
556 check_completion_callback (GtkFileChooserEntry *chooser_entry)
558 GDK_THREADS_ENTER ();
560 g_assert (chooser_entry->file_part);
562 chooser_entry->check_completion_idle = 0;
564 if (strcmp (chooser_entry->file_part, "") == 0)
567 /* We only insert the common prefix without requiring the user to hit Tab in
568 * the "open" modes. For "save" modes, the user must hit Tab to cause the prefix
569 * to be inserted. That happens in gtk_file_chooser_entry_focus().
571 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
572 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
573 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length)
574 append_common_prefix (chooser_entry, TRUE);
578 GDK_THREADS_LEAVE ();
584 idle_add (GtkFileChooserEntry *chooser_entry,
590 source = g_idle_source_new ();
591 g_source_set_priority (source, G_PRIORITY_HIGH);
592 g_source_set_closure (source,
593 g_cclosure_new_object (cb, G_OBJECT (chooser_entry)));
594 id = g_source_attach (source, NULL);
595 g_source_unref (source);
601 add_completion_idle (GtkFileChooserEntry *chooser_entry)
603 /* idle to update the selection based on the file list */
604 if (chooser_entry->check_completion_idle == 0)
605 chooser_entry->check_completion_idle =
606 idle_add (chooser_entry, G_CALLBACK (check_completion_callback));
611 update_current_folder_files (GtkFileChooserEntry *chooser_entry,
616 g_assert (chooser_entry->completion_store != NULL);
618 /* Bah. Need to turn off sorting */
619 for (tmp_list = added_uris; tmp_list; tmp_list = tmp_list->next)
624 path = tmp_list->data;
626 info = gtk_file_folder_get_info (chooser_entry->current_folder,
628 NULL); /* NULL-GError */
631 gchar *display_name = g_strdup (gtk_file_info_get_display_name (info));
634 display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
636 gtk_list_store_append (chooser_entry->completion_store, &iter);
637 gtk_list_store_set (chooser_entry->completion_store, &iter,
638 DISPLAY_NAME_COLUMN, display_name,
642 gtk_file_info_free (info);
643 g_free (display_name);
647 /* FIXME: we want to turn off sorting temporarily. I suck... */
648 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
649 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
651 add_completion_idle (chooser_entry);
655 files_added_cb (GtkFileSystem *file_system,
657 GtkFileChooserEntry *chooser_entry)
659 update_current_folder_files (chooser_entry, added_uris);
663 files_deleted_cb (GtkFileSystem *file_system,
664 GSList *deleted_uris,
665 GtkFileChooserEntry *chooser_entry)
667 /* FIXME: gravy... */
671 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
672 const gchar *new_text,
673 gint new_text_length,
676 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
678 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
680 if (! chooser_entry->in_change)
681 add_completion_idle (GTK_FILE_CHOOSER_ENTRY (editable));
685 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
687 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
688 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
692 gtk_file_chooser_entry_focus (GtkWidget *widget,
693 GtkDirectionType direction)
695 GtkFileChooserEntry *chooser_entry;
696 GtkEditable *editable;
698 GdkModifierType state;
699 gboolean control_pressed;
701 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
702 editable = GTK_EDITABLE (widget);
703 entry = GTK_ENTRY (widget);
705 if (!chooser_entry->eat_tabs)
706 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
708 control_pressed = FALSE;
710 if (gtk_get_current_event_state (&state))
712 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
713 control_pressed = TRUE;
716 /* This is a bit evil -- it makes Tab never leave the entry. It basically
717 * makes it 'safe' for people to hit. */
718 if ((direction == GTK_DIR_TAB_FORWARD) &&
719 (GTK_WIDGET_HAS_FOCUS (widget)) &&
724 if (chooser_entry->has_completion)
728 if (gtk_editable_get_selection_bounds (editable, NULL, &sel_end))
729 gtk_editable_set_position (editable, sel_end);
732 append_common_prefix (chooser_entry, FALSE);
734 /* Trigger the completion window to pop up again by a
735 * zero-length insertion, a bit of a hack.
737 gtk_editable_insert_text (editable, "", -1, &pos);
742 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
746 gtk_file_chooser_entry_activate (GtkEntry *entry)
748 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
750 if (chooser_entry->has_completion)
752 gtk_editable_set_position (GTK_EDITABLE (entry),
756 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
760 load_directory_get_folder_callback (GtkFileSystemHandle *handle,
761 GtkFileFolder *folder,
765 gboolean cancelled = handle->cancelled;
766 GtkFileChooserEntry *chooser_entry = data;
768 if (handle != chooser_entry->load_folder_handle)
771 chooser_entry->load_folder_handle = NULL;
773 if (cancelled || error)
776 /* FIXME: connect to "finished-loading". In that callback, see our
777 * load_complete_action and do the appropriate thing.
779 * Do we need to populate the completion store here? (O(n^2))
780 * Maybe we should wait until the folder is finished loading.
783 chooser_entry->current_folder = folder;
784 g_signal_connect (chooser_entry->current_folder, "files-added",
785 G_CALLBACK (files_added_cb), chooser_entry);
786 g_signal_connect (chooser_entry->current_folder, "files-removed",
787 G_CALLBACK (files_deleted_cb), chooser_entry);
789 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
793 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
794 GTK_TREE_MODEL (chooser_entry->completion_store));
797 g_object_unref (chooser_entry);
798 g_object_unref (handle);
802 load_current_folder (GtkFileChooserEntry *chooser_entry)
804 if (chooser_entry->current_folder_path == NULL ||
805 chooser_entry->file_system == NULL)
808 g_assert (chooser_entry->current_folder == NULL);
809 g_assert (chooser_entry->completion_store == NULL);
810 g_assert (chooser_entry->load_folder_handle == NULL);
812 chooser_entry->load_folder_handle =
813 gtk_file_system_get_folder (chooser_entry->file_system,
814 chooser_entry->current_folder_path,
815 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
816 load_directory_get_folder_callback,
817 g_object_ref (chooser_entry));
821 reload_current_folder (GtkFileChooserEntry *chooser_entry,
822 GtkFilePath *folder_path,
823 gboolean force_reload)
825 gboolean reload = FALSE;
827 if (chooser_entry->current_folder_path)
829 if ((folder_path && gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0)
834 /* We changed our current directory. We need to clear out the old
835 * directory information.
837 if (chooser_entry->current_folder)
839 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
840 G_CALLBACK (files_added_cb), chooser_entry);
841 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
842 G_CALLBACK (files_deleted_cb), chooser_entry);
844 if (chooser_entry->load_folder_handle)
846 gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
847 chooser_entry->load_folder_handle = NULL;
850 g_object_unref (chooser_entry->current_folder);
851 chooser_entry->current_folder = NULL;
854 if (chooser_entry->completion_store)
856 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
857 g_object_unref (chooser_entry->completion_store);
858 chooser_entry->completion_store = NULL;
861 gtk_file_path_free (chooser_entry->current_folder_path);
862 chooser_entry->current_folder_path = gtk_file_path_copy (folder_path);
867 chooser_entry->current_folder_path = gtk_file_path_copy (folder_path);
872 load_current_folder (chooser_entry);
876 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry)
878 GtkEditable *editable;
879 gchar *text_up_to_cursor;
880 GtkFilePath *folder_path;
882 gsize total_len, file_part_len;
885 editable = GTK_EDITABLE (chooser_entry);
887 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
889 if (!chooser_entry->file_system ||
890 !chooser_entry->base_folder ||
891 !gtk_file_system_parse (chooser_entry->file_system,
892 chooser_entry->base_folder, text_up_to_cursor,
893 &folder_path, &file_part, NULL)) /* NULL-GError */
895 folder_path = gtk_file_path_copy (chooser_entry->base_folder);
896 file_part = g_strdup ("");
901 file_part_len = strlen (file_part);
902 total_len = strlen (text_up_to_cursor);
903 if (total_len > file_part_len)
904 file_part_pos = g_utf8_strlen (text_up_to_cursor, total_len - file_part_len);
909 g_free (text_up_to_cursor);
911 g_free (chooser_entry->file_part);
913 chooser_entry->file_part = file_part;
914 chooser_entry->file_part_pos = file_part_pos;
916 reload_current_folder (chooser_entry, folder_path, file_part_pos == -1);
917 gtk_file_path_free (folder_path);
921 autocomplete (GtkFileChooserEntry *chooser_entry)
923 g_assert (chooser_entry->current_folder != NULL);
924 g_assert (gtk_file_folder_is_finished_loading (chooser_entry->current_folder));
930 start_autocompletion (GtkFileChooserEntry *chooser_entry)
932 refresh_current_folder_and_file_part (chooser_entry);
934 if (!chooser_entry->current_folder)
936 /* We don't beep or anything, since this is autocompletion - the user
937 * didn't request any action explicitly.
942 if (gtk_file_folder_is_finished_loading (chooser_entry->current_folder))
943 autocomplete (chooser_entry);
945 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
949 start_autocompletion_idle_handler (gpointer data)
951 GtkFileChooserEntry *chooser_entry;
953 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
955 if (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length)
956 start_autocompletion (chooser_entry);
958 chooser_entry->start_autocompletion_idle_id = 0;
964 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
966 /* We set up an idle handler because we must test the cursor position. However,
967 * the cursor is not updated in GtkEntry::changed, so we wait for the idle loop.
970 if (chooser_entry->start_autocompletion_idle_id != 0)
973 chooser_entry->start_autocompletion_idle_id = g_idle_add (start_autocompletion_idle_handler, chooser_entry);
977 gtk_file_chooser_entry_changed (GtkEditable *editable)
979 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
981 if (chooser_entry->in_change)
984 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
986 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
987 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER))
988 install_start_autocompletion_idle (chooser_entry);
990 /* FIXME: see when the cursor changes (see GtkEntry's cursor_position property, or GtkEditable::set_position and GtkEntry::move_cursor).
991 * When the cursor changes, cancel the load_complete_action.
992 * When the entry is activated, cancel the load_complete_action.
993 * In general, cancel the load_complete_action when the entry loses the focus.
998 clear_completion_callback (GtkFileChooserEntry *chooser_entry,
1001 if (chooser_entry->has_completion)
1003 chooser_entry->has_completion = FALSE;
1004 gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
1010 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1011 const gchar *new_text,
1012 gint new_text_length,
1016 const gchar *colon = memchr (new_text, ':', new_text_length);
1019 /* Disallow these characters altogether */
1020 for (i = 0; i < new_text_length; i++)
1022 if (new_text[i] == '<' ||
1023 new_text[i] == '>' ||
1024 new_text[i] == '"' ||
1025 new_text[i] == '|' ||
1026 new_text[i] == '*' ||
1031 if (i < new_text_length ||
1032 /* Disallow entering text that would cause a colon to be anywhere except
1033 * after a drive letter.
1036 *position + (colon - new_text) != 1) ||
1037 (new_text_length > 0 &&
1039 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1040 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1042 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1043 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1051 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1056 /* If deleting a drive letter, delete the colon, too */
1057 if (start_pos == 0 && end_pos == 1 &&
1058 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1059 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1061 g_signal_handlers_block_by_func (chooser_entry,
1062 G_CALLBACK (delete_text_callback),
1064 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1065 g_signal_handlers_unblock_by_func (chooser_entry,
1066 G_CALLBACK (delete_text_callback),
1073 * _gtk_file_chooser_entry_new:
1074 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1076 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1077 * is an internal implementation widget for the GTK+ file chooser
1078 * which is an entry with completion with respect to a
1079 * #GtkFileSystem object.
1081 * Return value: the newly created #GtkFileChooserEntry
1084 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1086 GtkFileChooserEntry *chooser_entry;
1088 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1089 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1091 return GTK_WIDGET (chooser_entry);
1095 * _gtk_file_chooser_entry_set_file_system:
1096 * @chooser_entry: a #GtkFileChooser
1097 * @file_system: an object implementing #GtkFileSystem
1099 * Sets the file system for @chooser_entry.
1102 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1103 GtkFileSystem *file_system)
1105 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1106 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1108 if (file_system != chooser_entry->file_system)
1110 if (chooser_entry->file_system)
1111 g_object_unref (chooser_entry->file_system);
1113 chooser_entry->file_system = g_object_ref (file_system);
1118 * _gtk_file_chooser_entry_set_base_folder:
1119 * @chooser_entry: a #GtkFileChooserEntry
1120 * @path: path of a folder in the chooser entries current file system.
1122 * Sets the folder with respect to which completions occur.
1125 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1126 const GtkFilePath *path)
1128 if (chooser_entry->base_folder)
1129 gtk_file_path_free (chooser_entry->base_folder);
1131 chooser_entry->base_folder = gtk_file_path_copy (path);
1133 gtk_file_chooser_entry_changed (GTK_EDITABLE (chooser_entry));
1134 _gtk_file_chooser_entry_select_filename (chooser_entry);
1138 * _gtk_file_chooser_entry_get_current_folder:
1139 * @chooser_entry: a #GtkFileChooserEntry
1141 * Gets the current folder for the #GtkFileChooserEntry. If the
1142 * user has only entered a filename, this will be the base folder
1143 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1144 * user has entered a relative or absolute path, then it will
1145 * be different. If the user has entered a relative or absolute
1146 * path that doesn't point to a folder in the file system, it will
1149 * Return value: the path of current folder - this value is owned by the
1150 * chooser entry and must not be modified or freed.
1153 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1155 if (chooser_entry->has_completion)
1157 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1158 GTK_ENTRY (chooser_entry)->text_length);
1160 return chooser_entry->current_folder_path;
1164 * _gtk_file_chooser_entry_get_file_part:
1165 * @chooser_entry: a #GtkFileChooserEntry
1167 * Gets the non-folder portion of whatever the user has entered
1168 * into the file selector. What is returned is a UTF-8 string,
1169 * and if a filename path is needed, gtk_file_system_make_path()
1172 * Return value: the entered filename - this value is owned by the
1173 * chooser entry and must not be modified or freed.
1176 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1178 if (chooser_entry->has_completion)
1180 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1181 GTK_ENTRY (chooser_entry)->text_length);
1183 return chooser_entry->file_part;
1187 * _gtk_file_chooser_entry_set_file_part:
1188 * @chooser_entry: a #GtkFileChooserEntry
1189 * @file_part: text to display in the entry, in UTF-8
1191 * Sets the current text shown in the file chooser entry.
1194 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1195 const gchar *file_part)
1197 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1199 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1204 * _gtk_file_chooser_entry_set_action:
1205 * @chooser_entry: a #GtkFileChooserEntry
1206 * @action: the action which is performed by the file selector using this entry
1208 * Sets action which is performed by the file selector using this entry.
1209 * The #GtkFileChooserEntry will use different completion strategies for
1210 * different actions.
1213 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1214 GtkFileChooserAction action)
1216 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1218 if (chooser_entry->action != action)
1220 GtkEntryCompletion *comp;
1222 chooser_entry->action = action;
1224 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1228 case GTK_FILE_CHOOSER_ACTION_OPEN:
1229 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1230 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1232 case GTK_FILE_CHOOSER_ACTION_SAVE:
1233 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1234 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1242 * _gtk_file_chooser_entry_get_action:
1243 * @chooser_entry: a #GtkFileChooserEntry
1245 * Gets the action for this entry.
1247 * Returns: the action
1249 GtkFileChooserAction
1250 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1252 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1253 GTK_FILE_CHOOSER_ACTION_OPEN);
1255 return chooser_entry->action;
1259 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1260 const GtkFilePath *path)
1262 gboolean retval = FALSE;
1264 if (chooser_entry->current_folder)
1266 GtkFileInfo *file_info;
1268 file_info = gtk_file_folder_get_info (chooser_entry->current_folder,
1272 retval = gtk_file_info_get_is_folder (file_info);
1273 gtk_file_info_free (file_info);
1282 * _gtk_file_chooser_entry_select_filename:
1283 * @chooser_entry: a #GtkFileChooserEntry
1285 * Selects the filename (without the extension) for user edition.
1288 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1290 const gchar *str, *ext;
1293 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1295 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1296 ext = g_strrstr (str, ".");
1299 len = g_utf8_pointer_to_offset (str, ext);
1302 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);