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_dispatch_properties_changed (GObject *object,
135 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
136 GtkEditable *editable = GTK_EDITABLE (object);
139 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
141 /* What we are after: The text in front of the cursor was modified.
142 * Unfortunately, there's no other way to catch this. */
144 if (chooser_entry->in_change)
147 for (i = 0; i < n_pspecs; i++)
149 if (pspecs[i]->name == I_("cursor-position") ||
150 pspecs[i]->name == I_("selection-bound") ||
151 pspecs[i]->name == I_("text"))
156 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
158 gtk_editable_get_selection_bounds (editable, &start, &end);
159 text = gtk_editable_get_chars (editable, 0, MIN (start, end));
160 refresh_current_folder_and_file_part (chooser_entry, text);
169 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
171 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
172 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
174 gobject_class->finalize = gtk_file_chooser_entry_finalize;
175 gobject_class->dispose = gtk_file_chooser_entry_dispose;
176 gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
178 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
179 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
180 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
184 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
186 GtkEntryCompletion *comp;
187 GtkCellRenderer *cell;
189 chooser_entry->local_only = TRUE;
190 chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
192 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
194 comp = gtk_entry_completion_new ();
195 gtk_entry_completion_set_popup_single_match (comp, FALSE);
196 /* see docs for gtk_entry_completion_set_text_column() */
197 g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
199 gtk_entry_completion_set_match_func (comp,
200 completion_match_func,
204 cell = gtk_cell_renderer_text_new ();
205 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
207 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
209 "text", DISPLAY_NAME_COLUMN);
211 g_signal_connect (comp, "match-selected",
212 G_CALLBACK (match_selected_callback), chooser_entry);
214 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
215 g_object_unref (comp);
218 g_signal_connect (chooser_entry, "insert-text",
219 G_CALLBACK (insert_text_callback), NULL);
220 g_signal_connect (chooser_entry, "delete-text",
221 G_CALLBACK (delete_text_callback), NULL);
226 gtk_file_chooser_entry_finalize (GObject *object)
228 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
230 g_object_unref (chooser_entry->base_folder);
232 if (chooser_entry->current_folder_file)
233 g_object_unref (chooser_entry->current_folder_file);
235 g_free (chooser_entry->dir_part);
236 g_free (chooser_entry->file_part);
238 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
242 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
244 if (chooser_entry->current_folder_file)
246 g_object_unref (chooser_entry->current_folder_file);
247 chooser_entry->current_folder_file = NULL;
252 gtk_file_chooser_entry_dispose (GObject *object)
254 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
256 discard_loading_and_current_folder_file (chooser_entry);
258 if (chooser_entry->completion_store)
260 g_object_unref (chooser_entry->completion_store);
261 chooser_entry->completion_store = NULL;
264 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
267 /* Match functions for the GtkEntryCompletion */
269 match_selected_callback (GtkEntryCompletion *completion,
272 GtkFileChooserEntry *chooser_entry)
276 gtk_tree_model_get (model, iter,
277 FULL_PATH_COLUMN, &path,
280 /* We don't set in_change here as we want to update the current_folder
282 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
284 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
285 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
295 /* Match function for the GtkEntryCompletion */
297 completion_match_func (GtkEntryCompletion *comp,
298 const char *key_unused,
302 GtkFileChooserEntry *chooser_entry;
305 char *norm_file_part;
308 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
310 /* We ignore the key because it is the contents of the entry. Instead, we
311 * just use our precomputed file_part.
313 if (!chooser_entry->file_part)
318 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
321 return FALSE; /* Uninitialized row, ugh */
324 /* If we have an empty file_part, then we're at the root of a directory. In
325 * that case, we want to match all non-dot files. We might want to match
326 * dot_files too if show_hidden is TRUE on the fileselector in the future.
328 /* Additionally, support for gnome .hidden files would be sweet, too */
329 if (chooser_entry->file_part[0] == '\000')
341 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
342 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
344 #ifdef G_PLATFORM_WIN32
348 temp = norm_file_part;
349 norm_file_part = g_utf8_casefold (norm_file_part, -1);
353 norm_name = g_utf8_casefold (norm_name, -1);
358 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
360 g_free (norm_file_part);
368 clear_completions (GtkFileChooserEntry *chooser_entry)
370 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
374 beep (GtkFileChooserEntry *chooser_entry)
376 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
380 is_valid_scheme_character (char c)
382 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
386 has_uri_scheme (const char *str)
392 if (!is_valid_scheme_character (*p))
397 while (is_valid_scheme_character (*p));
399 return (strncmp (p, "://", 3) == 0);
403 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
408 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
409 file = g_file_parse_name (str);
411 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
417 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
420 GFile *file, *parent;
422 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
424 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
427 parent = g_file_get_parent (file);
428 g_object_unref (file);
434 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
441 gboolean result = FALSE;
442 gboolean is_dir = FALSE;
443 gchar *last_slash = NULL;
446 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
448 last_slash = strrchr (str, G_DIR_SEPARATOR);
450 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
452 if (g_file_equal (chooser_entry->base_folder, file))
454 /* this is when user types '.', could be the
455 * beginning of a hidden file, ./ or ../
457 *folder = g_object_ref (file);
458 *file_part = g_strdup (str);
463 /* it's a dir, or at least it ends with the dir separator */
464 *folder = g_object_ref (file);
465 *file_part = g_strdup ("");
472 parent_file = g_file_get_parent (file);
477 GTK_FILE_CHOOSER_ERROR,
478 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
479 "Could not get parent file");
485 *folder = parent_file;
489 *file_part = g_strdup (last_slash + 1);
491 *file_part = g_strdup (str);
495 g_object_unref (file);
500 /* Determines if the completion model has entries with a common prefix relative
501 * to the current contents of the entry. Also, if there's one and only one such
502 * path, stores it in unique_path_ret.
505 find_common_prefix (GtkFileChooserEntry *chooser_entry,
506 gchar **common_prefix_ret,
507 GFile **unique_file_ret,
508 gboolean *is_complete_not_unique_ret,
509 gboolean *prefix_expands_the_file_part_ret,
512 GtkEditable *editable;
516 char *text_up_to_cursor;
517 GFile *parsed_folder_file;
518 char *parsed_file_part;
520 *common_prefix_ret = NULL;
521 *unique_file_ret = NULL;
522 *is_complete_not_unique_ret = FALSE;
523 *prefix_expands_the_file_part_ret = FALSE;
525 editable = GTK_EDITABLE (chooser_entry);
527 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
529 parsed = gtk_file_chooser_entry_parse (chooser_entry,
535 g_free (text_up_to_cursor);
540 g_assert (parsed_folder_file != NULL
541 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
543 g_object_unref (parsed_folder_file);
545 /* First pass: find the common prefix */
547 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
554 gtk_tree_model_get (chooser_entry->completion_store,
556 DISPLAY_NAME_COLUMN, &display_name,
560 if (g_str_has_prefix (display_name, parsed_file_part))
562 if (!*common_prefix_ret)
564 *common_prefix_ret = g_strdup (display_name);
565 *unique_file_ret = g_object_ref (file);
569 gchar *p = *common_prefix_ret;
570 const gchar *q = display_name;
572 while (*p && *p == *q)
580 if (*unique_file_ret)
582 g_object_unref (*unique_file_ret);
583 *unique_file_ret = NULL;
588 g_free (display_name);
589 g_object_unref (file);
590 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
593 /* Second pass: see if the prefix we found is a complete match */
595 if (*common_prefix_ret != NULL)
597 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
604 gtk_tree_model_get (chooser_entry->completion_store,
606 DISPLAY_NAME_COLUMN, &display_name,
608 len = strlen (display_name);
611 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
614 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
615 *is_complete_not_unique_ret = TRUE;
617 g_free (display_name);
618 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
621 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
623 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
626 g_free (parsed_file_part);
631 /* Finds a common prefix based on the contents of the entry
632 * and mandatorily appends it
635 explicitly_complete (GtkFileChooserEntry *chooser_entry)
637 gchar *common_prefix;
639 gboolean is_complete_not_unique;
640 gboolean prefix_expands_the_file_part;
644 clear_completions (chooser_entry);
646 if (chooser_entry->completion_store == NULL
647 || !find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, NULL)
649 || !prefix_expands_the_file_part)
651 beep (chooser_entry);
655 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
656 pos = chooser_entry->file_part_pos;
658 chooser_entry->in_change = TRUE;
659 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
661 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
664 chooser_entry->in_change = FALSE;
666 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
670 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
672 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
673 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
677 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
679 RefreshStatus status;
683 text = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry),
684 0, gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
685 status = refresh_current_folder_and_file_part (chooser_entry, text);
693 g_assert (chooser_entry->current_folder_file != NULL);
695 if (chooser_entry->current_folder_loaded)
696 explicitly_complete (chooser_entry);
699 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
704 case REFRESH_INVALID_INPUT:
708 case REFRESH_INCOMPLETE_HOSTNAME:
712 case REFRESH_NONEXISTENT:
716 case REFRESH_NOT_LOCAL:
721 g_assert_not_reached ();
727 g_assert (chooser_entry->current_folder_file == NULL);
729 beep (chooser_entry);
730 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
735 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
738 GtkFileChooserEntry *chooser_entry;
739 GtkEditable *editable;
740 GdkModifierType state;
741 gboolean control_pressed;
743 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
744 editable = GTK_EDITABLE (widget);
746 if (!chooser_entry->eat_tabs)
747 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
749 control_pressed = FALSE;
751 if (gtk_get_current_event_state (&state))
753 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
754 control_pressed = TRUE;
757 /* This is a bit evil -- it makes Tab never leave the entry. It basically
758 * makes it 'safe' for people to hit. */
759 if (event->keyval == GDK_KEY_Tab && !control_pressed)
763 gtk_editable_get_selection_bounds (editable, &start, &end);
766 gtk_editable_set_position (editable, MAX (start, end));
768 start_explicit_completion (chooser_entry);
773 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
778 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
779 GdkEventFocus *event)
781 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
783 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
785 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
789 discard_completion_store (GtkFileChooserEntry *chooser_entry)
791 if (!chooser_entry->completion_store)
794 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
795 gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
796 g_object_unref (chooser_entry->completion_store);
797 chooser_entry->completion_store = NULL;
801 completion_store_set (GtkFileSystemModel *model,
808 GtkFileChooserEntry *chooser_entry = data;
810 const char *prefix = "";
811 const char *suffix = "";
816 g_value_set_object (value, file);
818 case FULL_PATH_COLUMN:
819 prefix = chooser_entry->dir_part;
821 case DISPLAY_NAME_COLUMN:
822 if (_gtk_file_info_consider_as_directory (info))
823 suffix = G_DIR_SEPARATOR_S;
825 g_value_take_string (value, g_strconcat (
827 g_file_info_get_display_name (info),
832 g_assert_not_reached ();
839 /* Fills the completion store from the contents of the current folder */
841 populate_completion_store (GtkFileChooserEntry *chooser_entry)
843 discard_completion_store (chooser_entry);
845 chooser_entry->completion_store = GTK_TREE_MODEL (
846 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
847 "standard::name,standard::display-name,standard::type",
848 completion_store_set,
854 g_signal_connect (chooser_entry->completion_store, "finished-loading",
855 G_CALLBACK (finished_loading_cb), chooser_entry);
857 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
858 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
860 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
861 chooser_entry->completion_store);
864 /* When we finish loading the current folder, this function should get called to
865 * perform the deferred explicit completion.
868 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
870 switch (chooser_entry->load_complete_action)
872 case LOAD_COMPLETE_NOTHING:
875 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
876 explicitly_complete (chooser_entry);
880 g_assert_not_reached ();
885 /* Callback when the current folder finishes loading */
887 finished_loading_cb (GtkFileSystemModel *model,
889 GtkFileChooserEntry *chooser_entry)
891 GtkEntryCompletion *completion;
893 chooser_entry->current_folder_loaded = TRUE;
897 LoadCompleteAction old_load_complete_action;
899 old_load_complete_action = chooser_entry->load_complete_action;
901 discard_completion_store (chooser_entry);
902 clear_completions (chooser_entry);
904 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
906 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
908 beep (chooser_entry);
914 perform_load_complete_action (chooser_entry);
916 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
918 completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
919 gtk_entry_completion_set_inline_completion (completion, TRUE);
920 gtk_entry_completion_complete (completion);
921 gtk_entry_completion_insert_prefix (completion);
925 reload_current_folder (GtkFileChooserEntry *chooser_entry,
928 g_assert (folder_file != NULL);
930 if (chooser_entry->current_folder_file
931 && g_file_equal (folder_file, chooser_entry->current_folder_file))
934 if (chooser_entry->current_folder_file)
936 discard_loading_and_current_folder_file (chooser_entry);
939 if (chooser_entry->local_only
940 && !g_file_is_native (folder_file))
941 return REFRESH_NOT_LOCAL;
943 chooser_entry->current_folder_file = g_object_ref (folder_file);
944 chooser_entry->current_folder_loaded = FALSE;
946 populate_completion_store (chooser_entry);
952 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
957 gsize total_len, file_part_len;
960 RefreshStatus result;
963 if (!gtk_file_chooser_entry_parse (chooser_entry,
964 text, &folder_file, &file_part, &error))
966 folder_file = g_object_ref (chooser_entry->base_folder);
968 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
969 result = REFRESH_NONEXISTENT;
971 result = REFRESH_INVALID_INPUT;
974 g_error_free (error);
976 file_part = g_strdup ("");
981 g_assert (folder_file != NULL);
983 file_part_len = strlen (file_part);
984 total_len = strlen (text);
985 if (total_len > file_part_len)
986 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
993 g_free (chooser_entry->file_part);
994 g_free (chooser_entry->dir_part);
996 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
997 chooser_entry->file_part = file_part;
998 chooser_entry->file_part_pos = file_part_pos;
1000 if (result == REFRESH_OK)
1002 result = reload_current_folder (chooser_entry, folder_file);
1006 discard_loading_and_current_folder_file (chooser_entry);
1010 g_object_unref (folder_file);
1012 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1013 ((result == REFRESH_OK)
1014 && (chooser_entry->current_folder_file != NULL))
1015 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1016 || ((result != REFRESH_OK)
1017 && (chooser_entry->current_folder_file == NULL)));
1024 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1025 const gchar *new_text,
1026 gint new_text_length,
1030 const gchar *colon = memchr (new_text, ':', new_text_length);
1033 /* Disallow these characters altogether */
1034 for (i = 0; i < new_text_length; i++)
1036 if (new_text[i] == '<' ||
1037 new_text[i] == '>' ||
1038 new_text[i] == '"' ||
1039 new_text[i] == '|' ||
1040 new_text[i] == '*' ||
1045 if (i < new_text_length ||
1046 /* Disallow entering text that would cause a colon to be anywhere except
1047 * after a drive letter.
1050 *position + (colon - new_text) != 1) ||
1051 (new_text_length > 0 &&
1053 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1054 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1056 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1057 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1065 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1070 /* If deleting a drive letter, delete the colon, too */
1071 if (start_pos == 0 && end_pos == 1 &&
1072 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1073 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1075 g_signal_handlers_block_by_func (chooser_entry,
1076 G_CALLBACK (delete_text_callback),
1078 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1079 g_signal_handlers_unblock_by_func (chooser_entry,
1080 G_CALLBACK (delete_text_callback),
1087 * _gtk_file_chooser_entry_new:
1088 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1090 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1091 * is an internal implementation widget for the GTK+ file chooser
1092 * which is an entry with completion with respect to a
1093 * #GtkFileSystem object.
1095 * Return value: the newly created #GtkFileChooserEntry
1098 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1100 GtkFileChooserEntry *chooser_entry;
1102 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1103 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1105 return GTK_WIDGET (chooser_entry);
1109 * _gtk_file_chooser_entry_set_base_folder:
1110 * @chooser_entry: a #GtkFileChooserEntry
1111 * @file: file for a folder in the chooser entries current file system.
1113 * Sets the folder with respect to which completions occur.
1116 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1120 g_object_ref (file);
1122 file = g_file_new_for_path (g_get_home_dir ());
1124 if (g_file_equal (chooser_entry->base_folder, file))
1126 g_object_unref (file);
1130 if (chooser_entry->base_folder)
1131 g_object_unref (chooser_entry->base_folder);
1133 chooser_entry->base_folder = file;
1135 clear_completions (chooser_entry);
1139 * _gtk_file_chooser_entry_get_current_folder:
1140 * @chooser_entry: a #GtkFileChooserEntry
1142 * Gets the current folder for the #GtkFileChooserEntry. If the
1143 * user has only entered a filename, this will be in the base folder
1144 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1145 * user has entered a relative or absolute path, then it will
1146 * be different. If the user has entered unparsable text, or text which
1147 * the entry cannot handle, this will return %NULL.
1149 * Return value: the file for the current folder - you must g_object_unref()
1150 * the value after use.
1153 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1155 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1157 return gtk_file_chooser_get_directory_for_text (chooser_entry,
1158 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
1162 * _gtk_file_chooser_entry_get_file_part:
1163 * @chooser_entry: a #GtkFileChooserEntry
1165 * Gets the non-folder portion of whatever the user has entered
1166 * into the file selector. What is returned is a UTF-8 string,
1167 * and if a filename path is needed, g_file_get_child_for_display_name()
1170 * Return value: the entered filename - this value is owned by the
1171 * chooser entry and must not be modified or freed.
1174 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1176 const char *last_slash, *text;
1178 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1180 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1181 last_slash = strrchr (text, G_DIR_SEPARATOR);
1183 return last_slash + 1;
1189 * _gtk_file_chooser_entry_set_file_part:
1190 * @chooser_entry: a #GtkFileChooserEntry
1191 * @file_part: text to display in the entry, in UTF-8
1193 * Sets the current text shown in the file chooser entry.
1196 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1197 const gchar *file_part)
1199 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1201 chooser_entry->in_change = TRUE;
1202 clear_completions (chooser_entry);
1203 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1204 chooser_entry->in_change = FALSE;
1209 * _gtk_file_chooser_entry_set_action:
1210 * @chooser_entry: a #GtkFileChooserEntry
1211 * @action: the action which is performed by the file selector using this entry
1213 * Sets action which is performed by the file selector using this entry.
1214 * The #GtkFileChooserEntry will use different completion strategies for
1215 * different actions.
1218 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1219 GtkFileChooserAction action)
1221 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1223 if (chooser_entry->action != action)
1225 GtkEntryCompletion *comp;
1227 chooser_entry->action = action;
1229 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1231 /* FIXME: do we need to actually set the following? */
1235 case GTK_FILE_CHOOSER_ACTION_OPEN:
1236 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1237 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1239 case GTK_FILE_CHOOSER_ACTION_SAVE:
1240 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1241 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1249 * _gtk_file_chooser_entry_get_action:
1250 * @chooser_entry: a #GtkFileChooserEntry
1252 * Gets the action for this entry.
1254 * Returns: the action
1256 GtkFileChooserAction
1257 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1259 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1260 GTK_FILE_CHOOSER_ACTION_OPEN);
1262 return chooser_entry->action;
1266 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1272 if (chooser_entry->completion_store == NULL ||
1273 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1278 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1281 return _gtk_file_info_consider_as_directory (info);
1286 * _gtk_file_chooser_entry_select_filename:
1287 * @chooser_entry: a #GtkFileChooserEntry
1289 * Selects the filename (without the extension) for user edition.
1292 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1294 const gchar *str, *ext;
1297 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1299 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1300 ext = g_strrstr (str, ".");
1303 len = g_utf8_pointer_to_offset (str, ext);
1306 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1310 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1311 gboolean local_only)
1313 chooser_entry->local_only = local_only;
1314 clear_completions (chooser_entry);
1318 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1320 return chooser_entry->local_only;