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.
23 #include "gtkfilechooserentry.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
30 #include "gtkfilesystemmodel.h"
33 #include "gtksizerequest.h"
34 #include "gtkwindow.h"
37 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
39 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
40 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
41 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
43 struct _GtkFileChooserEntryClass
45 GtkEntryClass parent_class;
48 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
50 LOAD_COMPLETE_NOTHING,
51 LOAD_COMPLETE_EXPLICIT_COMPLETION
57 REFRESH_INVALID_INPUT,
58 REFRESH_INCOMPLETE_HOSTNAME,
63 struct _GtkFileChooserEntry
65 GtkEntry parent_instance;
67 GtkFileChooserAction action;
70 GFile *current_folder_file;
75 LoadCompleteAction load_complete_action;
77 GtkTreeModel *completion_store;
79 guint current_folder_loaded : 1;
93 static void gtk_file_chooser_entry_finalize (GObject *object);
94 static void gtk_file_chooser_entry_dispose (GObject *object);
95 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
96 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
98 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
99 GdkEventFocus *event);
102 static gint insert_text_callback (GtkFileChooserEntry *widget,
103 const gchar *new_text,
104 gint new_text_length,
107 static void delete_text_callback (GtkFileChooserEntry *widget,
113 static gboolean match_selected_callback (GtkEntryCompletion *completion,
116 GtkFileChooserEntry *chooser_entry);
117 static gboolean completion_match_func (GtkEntryCompletion *comp,
122 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
124 static void finished_loading_cb (GtkFileSystemModel *model,
126 GtkFileChooserEntry *chooser_entry);
128 G_DEFINE_TYPE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY)
131 gtk_file_chooser_entry_get_completion_text (GtkFileChooserEntry *chooser_entry)
133 GtkEditable *editable = GTK_EDITABLE (chooser_entry);
136 gtk_editable_get_selection_bounds (editable, &start, &end);
137 return gtk_editable_get_chars (editable, 0, MIN (start, end));
141 gtk_file_chooser_entry_dispatch_properties_changed (GObject *object,
145 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
148 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
150 /* What we are after: The text in front of the cursor was modified.
151 * Unfortunately, there's no other way to catch this. */
153 if (chooser_entry->in_change)
156 for (i = 0; i < n_pspecs; i++)
158 if (pspecs[i]->name == I_("cursor-position") ||
159 pspecs[i]->name == I_("selection-bound") ||
160 pspecs[i]->name == I_("text"))
164 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
166 text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
167 refresh_current_folder_and_file_part (chooser_entry, text);
176 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
181 gobject_class->finalize = gtk_file_chooser_entry_finalize;
182 gobject_class->dispose = gtk_file_chooser_entry_dispose;
183 gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
185 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
186 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
187 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
191 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
193 GtkEntryCompletion *comp;
194 GtkCellRenderer *cell;
196 chooser_entry->local_only = TRUE;
197 chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
199 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
201 comp = gtk_entry_completion_new ();
202 gtk_entry_completion_set_popup_single_match (comp, FALSE);
203 /* see docs for gtk_entry_completion_set_text_column() */
204 g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
206 gtk_entry_completion_set_match_func (comp,
207 completion_match_func,
211 cell = gtk_cell_renderer_text_new ();
212 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
214 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
216 "text", DISPLAY_NAME_COLUMN);
218 g_signal_connect (comp, "match-selected",
219 G_CALLBACK (match_selected_callback), chooser_entry);
221 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
222 g_object_unref (comp);
225 g_signal_connect (chooser_entry, "insert-text",
226 G_CALLBACK (insert_text_callback), NULL);
227 g_signal_connect (chooser_entry, "delete-text",
228 G_CALLBACK (delete_text_callback), NULL);
233 gtk_file_chooser_entry_finalize (GObject *object)
235 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
237 g_object_unref (chooser_entry->base_folder);
239 if (chooser_entry->current_folder_file)
240 g_object_unref (chooser_entry->current_folder_file);
242 g_free (chooser_entry->dir_part);
243 g_free (chooser_entry->file_part);
245 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
249 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
251 if (chooser_entry->current_folder_file)
253 g_object_unref (chooser_entry->current_folder_file);
254 chooser_entry->current_folder_file = NULL;
259 gtk_file_chooser_entry_dispose (GObject *object)
261 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
263 discard_loading_and_current_folder_file (chooser_entry);
265 if (chooser_entry->completion_store)
267 g_object_unref (chooser_entry->completion_store);
268 chooser_entry->completion_store = NULL;
271 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
274 /* Match functions for the GtkEntryCompletion */
276 match_selected_callback (GtkEntryCompletion *completion,
279 GtkFileChooserEntry *chooser_entry)
283 gtk_tree_model_get (model, iter,
284 FULL_PATH_COLUMN, &path,
287 /* We don't set in_change here as we want to update the current_folder
289 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
291 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
292 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
302 /* Match function for the GtkEntryCompletion */
304 completion_match_func (GtkEntryCompletion *comp,
305 const char *key_unused,
309 GtkFileChooserEntry *chooser_entry;
312 char *norm_file_part;
315 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
317 /* We ignore the key because it is the contents of the entry. Instead, we
318 * just use our precomputed file_part.
320 if (!chooser_entry->file_part)
325 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
328 return FALSE; /* Uninitialized row, ugh */
331 /* If we have an empty file_part, then we're at the root of a directory. In
332 * that case, we want to match all non-dot files. We might want to match
333 * dot_files too if show_hidden is TRUE on the fileselector in the future.
335 /* Additionally, support for gnome .hidden files would be sweet, too */
336 if (chooser_entry->file_part[0] == '\000')
348 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
349 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
351 #ifdef G_PLATFORM_WIN32
355 temp = norm_file_part;
356 norm_file_part = g_utf8_casefold (norm_file_part, -1);
360 norm_name = g_utf8_casefold (norm_name, -1);
365 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
367 g_free (norm_file_part);
375 clear_completions (GtkFileChooserEntry *chooser_entry)
377 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
381 beep (GtkFileChooserEntry *chooser_entry)
383 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
387 is_valid_scheme_character (char c)
389 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
393 has_uri_scheme (const char *str)
399 if (!is_valid_scheme_character (*p))
404 while (is_valid_scheme_character (*p));
406 return (strncmp (p, "://", 3) == 0);
410 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
415 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
416 file = g_file_parse_name (str);
418 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
424 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
427 GFile *file, *parent;
429 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
431 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
434 parent = g_file_get_parent (file);
435 g_object_unref (file);
441 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
448 gboolean result = FALSE;
449 gboolean is_dir = FALSE;
450 gchar *last_slash = NULL;
453 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
455 last_slash = strrchr (str, G_DIR_SEPARATOR);
457 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
459 if (g_file_equal (chooser_entry->base_folder, file))
461 /* this is when user types '.', could be the
462 * beginning of a hidden file, ./ or ../
464 *folder = g_object_ref (file);
465 *file_part = g_strdup (str);
470 /* it's a dir, or at least it ends with the dir separator */
471 *folder = g_object_ref (file);
472 *file_part = g_strdup ("");
479 parent_file = g_file_get_parent (file);
484 GTK_FILE_CHOOSER_ERROR,
485 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
486 "Could not get parent file");
492 *folder = parent_file;
496 *file_part = g_strdup (last_slash + 1);
498 *file_part = g_strdup (str);
502 g_object_unref (file);
507 /* Determines if the completion model has entries with a common prefix relative
508 * to the current contents of the entry. Also, if there's one and only one such
509 * path, stores it in unique_path_ret.
512 find_common_prefix (GtkFileChooserEntry *chooser_entry,
513 gchar **common_prefix_ret,
514 GFile **unique_file_ret,
515 gboolean *is_complete_not_unique_ret,
516 gboolean *prefix_expands_the_file_part_ret,
522 char *text_up_to_cursor;
523 GFile *parsed_folder_file;
524 char *parsed_file_part;
526 *common_prefix_ret = NULL;
527 *unique_file_ret = NULL;
528 *is_complete_not_unique_ret = FALSE;
529 *prefix_expands_the_file_part_ret = FALSE;
531 text_up_to_cursor = gtk_file_chooser_entry_get_completion_text (chooser_entry);
533 parsed = gtk_file_chooser_entry_parse (chooser_entry,
539 g_free (text_up_to_cursor);
544 g_assert (parsed_folder_file != NULL
545 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
547 g_object_unref (parsed_folder_file);
549 /* First pass: find the common prefix */
551 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
558 gtk_tree_model_get (chooser_entry->completion_store,
560 DISPLAY_NAME_COLUMN, &display_name,
564 if (g_str_has_prefix (display_name, parsed_file_part))
566 if (!*common_prefix_ret)
568 *common_prefix_ret = g_strdup (display_name);
569 *unique_file_ret = g_object_ref (file);
573 gchar *p = *common_prefix_ret;
574 const gchar *q = display_name;
576 while (*p && *p == *q)
584 if (*unique_file_ret)
586 g_object_unref (*unique_file_ret);
587 *unique_file_ret = NULL;
592 g_free (display_name);
593 g_object_unref (file);
594 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
597 /* Second pass: see if the prefix we found is a complete match */
599 if (*common_prefix_ret != NULL)
601 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
608 gtk_tree_model_get (chooser_entry->completion_store,
610 DISPLAY_NAME_COLUMN, &display_name,
612 len = strlen (display_name);
615 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
618 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
619 *is_complete_not_unique_ret = TRUE;
621 g_free (display_name);
622 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
625 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
627 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
630 g_free (parsed_file_part);
635 /* Finds a common prefix based on the contents of the entry
636 * and mandatorily appends it
639 explicitly_complete (GtkFileChooserEntry *chooser_entry)
641 gchar *common_prefix;
643 gboolean is_complete_not_unique;
644 gboolean prefix_expands_the_file_part;
648 clear_completions (chooser_entry);
650 if (chooser_entry->completion_store == NULL
651 || !find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, NULL)
653 || !prefix_expands_the_file_part)
655 beep (chooser_entry);
659 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
660 pos = chooser_entry->file_part_pos;
662 chooser_entry->in_change = TRUE;
663 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
665 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
668 chooser_entry->in_change = FALSE;
670 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
674 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
676 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
677 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
681 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
683 if (chooser_entry->current_folder_loaded)
684 explicitly_complete (chooser_entry);
686 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
690 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
693 GtkFileChooserEntry *chooser_entry;
694 GtkEditable *editable;
695 GdkModifierType state;
696 gboolean control_pressed;
698 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
699 editable = GTK_EDITABLE (widget);
701 if (!chooser_entry->eat_tabs)
702 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
704 control_pressed = FALSE;
706 if (gtk_get_current_event_state (&state))
708 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
709 control_pressed = TRUE;
712 /* This is a bit evil -- it makes Tab never leave the entry. It basically
713 * makes it 'safe' for people to hit. */
714 if (event->keyval == GDK_KEY_Tab && !control_pressed)
718 gtk_editable_get_selection_bounds (editable, &start, &end);
721 gtk_editable_set_position (editable, MAX (start, end));
723 start_explicit_completion (chooser_entry);
728 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
733 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
734 GdkEventFocus *event)
736 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
738 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
740 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
744 discard_completion_store (GtkFileChooserEntry *chooser_entry)
746 if (!chooser_entry->completion_store)
749 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
750 gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
751 g_object_unref (chooser_entry->completion_store);
752 chooser_entry->completion_store = NULL;
756 completion_store_set (GtkFileSystemModel *model,
763 GtkFileChooserEntry *chooser_entry = data;
765 const char *prefix = "";
766 const char *suffix = "";
771 g_value_set_object (value, file);
773 case FULL_PATH_COLUMN:
774 prefix = chooser_entry->dir_part;
776 case DISPLAY_NAME_COLUMN:
777 if (_gtk_file_info_consider_as_directory (info))
778 suffix = G_DIR_SEPARATOR_S;
780 g_value_take_string (value, g_strconcat (
782 g_file_info_get_display_name (info),
787 g_assert_not_reached ();
794 /* Fills the completion store from the contents of the current folder */
796 populate_completion_store (GtkFileChooserEntry *chooser_entry)
798 discard_completion_store (chooser_entry);
800 chooser_entry->completion_store = GTK_TREE_MODEL (
801 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
802 "standard::name,standard::display-name,standard::type",
803 completion_store_set,
809 g_signal_connect (chooser_entry->completion_store, "finished-loading",
810 G_CALLBACK (finished_loading_cb), chooser_entry);
812 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
813 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
815 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
816 chooser_entry->completion_store);
819 /* When we finish loading the current folder, this function should get called to
820 * perform the deferred explicit completion.
823 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
825 switch (chooser_entry->load_complete_action)
827 case LOAD_COMPLETE_NOTHING:
830 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
831 explicitly_complete (chooser_entry);
835 g_assert_not_reached ();
840 /* Callback when the current folder finishes loading */
842 finished_loading_cb (GtkFileSystemModel *model,
844 GtkFileChooserEntry *chooser_entry)
846 GtkEntryCompletion *completion;
848 chooser_entry->current_folder_loaded = TRUE;
852 LoadCompleteAction old_load_complete_action;
854 old_load_complete_action = chooser_entry->load_complete_action;
856 discard_completion_store (chooser_entry);
857 clear_completions (chooser_entry);
859 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
861 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
863 beep (chooser_entry);
869 perform_load_complete_action (chooser_entry);
871 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
873 completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
874 gtk_entry_completion_set_inline_completion (completion, TRUE);
875 gtk_entry_completion_complete (completion);
876 gtk_entry_completion_insert_prefix (completion);
880 reload_current_folder (GtkFileChooserEntry *chooser_entry,
883 g_assert (folder_file != NULL);
885 if (chooser_entry->current_folder_file
886 && g_file_equal (folder_file, chooser_entry->current_folder_file))
889 if (chooser_entry->current_folder_file)
891 discard_loading_and_current_folder_file (chooser_entry);
894 if (chooser_entry->local_only
895 && !g_file_is_native (folder_file))
896 return REFRESH_NOT_LOCAL;
898 chooser_entry->current_folder_file = g_object_ref (folder_file);
899 chooser_entry->current_folder_loaded = FALSE;
901 populate_completion_store (chooser_entry);
907 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
912 gsize total_len, file_part_len;
915 RefreshStatus result;
918 if (!gtk_file_chooser_entry_parse (chooser_entry,
919 text, &folder_file, &file_part, &error))
921 folder_file = g_object_ref (chooser_entry->base_folder);
923 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
924 result = REFRESH_NONEXISTENT;
926 result = REFRESH_INVALID_INPUT;
929 g_error_free (error);
931 file_part = g_strdup ("");
936 g_assert (folder_file != NULL);
938 file_part_len = strlen (file_part);
939 total_len = strlen (text);
940 if (total_len > file_part_len)
941 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
948 g_free (chooser_entry->file_part);
949 g_free (chooser_entry->dir_part);
951 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
952 chooser_entry->file_part = file_part;
953 chooser_entry->file_part_pos = file_part_pos;
955 if (result == REFRESH_OK)
957 result = reload_current_folder (chooser_entry, folder_file);
961 discard_loading_and_current_folder_file (chooser_entry);
965 g_object_unref (folder_file);
967 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
968 ((result == REFRESH_OK)
969 && (chooser_entry->current_folder_file != NULL))
970 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
971 || ((result != REFRESH_OK)
972 && (chooser_entry->current_folder_file == NULL)));
979 insert_text_callback (GtkFileChooserEntry *chooser_entry,
980 const gchar *new_text,
981 gint new_text_length,
985 const gchar *colon = memchr (new_text, ':', new_text_length);
988 /* Disallow these characters altogether */
989 for (i = 0; i < new_text_length; i++)
991 if (new_text[i] == '<' ||
992 new_text[i] == '>' ||
993 new_text[i] == '"' ||
994 new_text[i] == '|' ||
995 new_text[i] == '*' ||
1000 if (i < new_text_length ||
1001 /* Disallow entering text that would cause a colon to be anywhere except
1002 * after a drive letter.
1005 *position + (colon - new_text) != 1) ||
1006 (new_text_length > 0 &&
1008 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1009 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1011 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1012 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1020 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1025 /* If deleting a drive letter, delete the colon, too */
1026 if (start_pos == 0 && end_pos == 1 &&
1027 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1028 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1030 g_signal_handlers_block_by_func (chooser_entry,
1031 G_CALLBACK (delete_text_callback),
1033 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1034 g_signal_handlers_unblock_by_func (chooser_entry,
1035 G_CALLBACK (delete_text_callback),
1042 * _gtk_file_chooser_entry_new:
1043 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1045 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1046 * is an internal implementation widget for the GTK+ file chooser
1047 * which is an entry with completion with respect to a
1048 * #GtkFileSystem object.
1050 * Return value: the newly created #GtkFileChooserEntry
1053 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1055 GtkFileChooserEntry *chooser_entry;
1057 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1058 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1060 return GTK_WIDGET (chooser_entry);
1064 * _gtk_file_chooser_entry_set_base_folder:
1065 * @chooser_entry: a #GtkFileChooserEntry
1066 * @file: file for a folder in the chooser entries current file system.
1068 * Sets the folder with respect to which completions occur.
1071 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1075 g_object_ref (file);
1077 file = g_file_new_for_path (g_get_home_dir ());
1079 if (g_file_equal (chooser_entry->base_folder, file))
1081 g_object_unref (file);
1085 if (chooser_entry->base_folder)
1086 g_object_unref (chooser_entry->base_folder);
1088 chooser_entry->base_folder = file;
1090 clear_completions (chooser_entry);
1094 * _gtk_file_chooser_entry_get_current_folder:
1095 * @chooser_entry: a #GtkFileChooserEntry
1097 * Gets the current folder for the #GtkFileChooserEntry. If the
1098 * user has only entered a filename, this will be in the base folder
1099 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1100 * user has entered a relative or absolute path, then it will
1101 * be different. If the user has entered unparsable text, or text which
1102 * the entry cannot handle, this will return %NULL.
1104 * Return value: the file for the current folder - you must g_object_unref()
1105 * the value after use.
1108 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1110 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1112 return gtk_file_chooser_get_directory_for_text (chooser_entry,
1113 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
1117 * _gtk_file_chooser_entry_get_file_part:
1118 * @chooser_entry: a #GtkFileChooserEntry
1120 * Gets the non-folder portion of whatever the user has entered
1121 * into the file selector. What is returned is a UTF-8 string,
1122 * and if a filename path is needed, g_file_get_child_for_display_name()
1125 * Return value: the entered filename - this value is owned by the
1126 * chooser entry and must not be modified or freed.
1129 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1131 const char *last_slash, *text;
1133 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1135 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1136 last_slash = strrchr (text, G_DIR_SEPARATOR);
1138 return last_slash + 1;
1144 * _gtk_file_chooser_entry_set_file_part:
1145 * @chooser_entry: a #GtkFileChooserEntry
1146 * @file_part: text to display in the entry, in UTF-8
1148 * Sets the current text shown in the file chooser entry.
1151 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1152 const gchar *file_part)
1154 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1156 chooser_entry->in_change = TRUE;
1157 clear_completions (chooser_entry);
1158 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1159 chooser_entry->in_change = FALSE;
1164 * _gtk_file_chooser_entry_set_action:
1165 * @chooser_entry: a #GtkFileChooserEntry
1166 * @action: the action which is performed by the file selector using this entry
1168 * Sets action which is performed by the file selector using this entry.
1169 * The #GtkFileChooserEntry will use different completion strategies for
1170 * different actions.
1173 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1174 GtkFileChooserAction action)
1176 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1178 if (chooser_entry->action != action)
1180 GtkEntryCompletion *comp;
1182 chooser_entry->action = action;
1184 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1186 /* FIXME: do we need to actually set the following? */
1190 case GTK_FILE_CHOOSER_ACTION_OPEN:
1191 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1192 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1194 case GTK_FILE_CHOOSER_ACTION_SAVE:
1195 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1196 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1204 * _gtk_file_chooser_entry_get_action:
1205 * @chooser_entry: a #GtkFileChooserEntry
1207 * Gets the action for this entry.
1209 * Returns: the action
1211 GtkFileChooserAction
1212 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1214 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1215 GTK_FILE_CHOOSER_ACTION_OPEN);
1217 return chooser_entry->action;
1221 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1227 if (chooser_entry->completion_store == NULL ||
1228 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1233 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1236 return _gtk_file_info_consider_as_directory (info);
1241 * _gtk_file_chooser_entry_select_filename:
1242 * @chooser_entry: a #GtkFileChooserEntry
1244 * Selects the filename (without the extension) for user edition.
1247 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1249 const gchar *str, *ext;
1252 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1254 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1255 ext = g_strrstr (str, ".");
1258 len = g_utf8_pointer_to_offset (str, ext);
1261 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1265 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1266 gboolean local_only)
1268 chooser_entry->local_only = local_only;
1269 clear_completions (chooser_entry);
1273 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1275 return chooser_entry->local_only;