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);
100 static void gtk_file_chooser_entry_activate (GtkEntry *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,
123 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
125 static void finished_loading_cb (GtkFileSystemModel *model,
127 GtkFileChooserEntry *chooser_entry);
129 G_DEFINE_TYPE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY)
132 gtk_file_chooser_entry_dispatch_properties_changed (GObject *object,
136 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
137 GtkEditable *editable = GTK_EDITABLE (object);
140 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
142 /* What we are after: The text in front of the cursor was modified.
143 * Unfortunately, there's no other way to catch this. */
145 if (chooser_entry->in_change)
148 for (i = 0; i < n_pspecs; i++)
150 if (pspecs[i]->name == I_("cursor-position") ||
151 pspecs[i]->name == I_("selection-bound") ||
152 pspecs[i]->name == I_("text"))
157 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
159 gtk_editable_get_selection_bounds (editable, &start, &end);
160 text = gtk_editable_get_chars (editable, 0, MIN (start, end));
161 refresh_current_folder_and_file_part (chooser_entry, text);
170 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
172 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
173 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
174 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
176 gobject_class->finalize = gtk_file_chooser_entry_finalize;
177 gobject_class->dispose = gtk_file_chooser_entry_dispose;
178 gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
180 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
181 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
182 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
184 entry_class->activate = gtk_file_chooser_entry_activate;
188 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
190 GtkEntryCompletion *comp;
191 GtkCellRenderer *cell;
193 chooser_entry->local_only = TRUE;
194 chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
196 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
198 comp = gtk_entry_completion_new ();
199 gtk_entry_completion_set_popup_single_match (comp, FALSE);
200 /* see docs for gtk_entry_completion_set_text_column() */
201 g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
203 gtk_entry_completion_set_match_func (comp,
204 completion_match_func,
208 cell = gtk_cell_renderer_text_new ();
209 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
211 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
213 "text", DISPLAY_NAME_COLUMN);
215 g_signal_connect (comp, "match-selected",
216 G_CALLBACK (match_selected_callback), chooser_entry);
218 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
219 g_object_unref (comp);
222 g_signal_connect (chooser_entry, "insert-text",
223 G_CALLBACK (insert_text_callback), NULL);
224 g_signal_connect (chooser_entry, "delete-text",
225 G_CALLBACK (delete_text_callback), NULL);
230 gtk_file_chooser_entry_finalize (GObject *object)
232 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
234 g_object_unref (chooser_entry->base_folder);
236 if (chooser_entry->current_folder_file)
237 g_object_unref (chooser_entry->current_folder_file);
239 g_free (chooser_entry->dir_part);
240 g_free (chooser_entry->file_part);
242 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
246 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
248 if (chooser_entry->current_folder_file)
250 g_object_unref (chooser_entry->current_folder_file);
251 chooser_entry->current_folder_file = NULL;
256 gtk_file_chooser_entry_dispose (GObject *object)
258 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
260 discard_loading_and_current_folder_file (chooser_entry);
262 if (chooser_entry->completion_store)
264 g_object_unref (chooser_entry->completion_store);
265 chooser_entry->completion_store = NULL;
268 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
271 /* Match functions for the GtkEntryCompletion */
273 match_selected_callback (GtkEntryCompletion *completion,
276 GtkFileChooserEntry *chooser_entry)
280 gtk_tree_model_get (model, iter,
281 FULL_PATH_COLUMN, &path,
284 /* We don't set in_change here as we want to update the current_folder
286 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
288 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
289 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
299 /* Match function for the GtkEntryCompletion */
301 completion_match_func (GtkEntryCompletion *comp,
302 const char *key_unused,
306 GtkFileChooserEntry *chooser_entry;
309 char *norm_file_part;
312 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
314 /* We ignore the key because it is the contents of the entry. Instead, we
315 * just use our precomputed file_part.
317 if (!chooser_entry->file_part)
322 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
325 return FALSE; /* Uninitialized row, ugh */
328 /* If we have an empty file_part, then we're at the root of a directory. In
329 * that case, we want to match all non-dot files. We might want to match
330 * dot_files too if show_hidden is TRUE on the fileselector in the future.
332 /* Additionally, support for gnome .hidden files would be sweet, too */
333 if (chooser_entry->file_part[0] == '\000')
345 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
346 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
348 #ifdef G_PLATFORM_WIN32
352 temp = norm_file_part;
353 norm_file_part = g_utf8_casefold (norm_file_part, -1);
357 norm_name = g_utf8_casefold (norm_name, -1);
362 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
364 g_free (norm_file_part);
372 clear_completions (GtkFileChooserEntry *chooser_entry)
374 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
378 beep (GtkFileChooserEntry *chooser_entry)
380 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
384 is_valid_scheme_character (char c)
386 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
390 has_uri_scheme (const char *str)
396 if (!is_valid_scheme_character (*p))
401 while (is_valid_scheme_character (*p));
403 return (strncmp (p, "://", 3) == 0);
407 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
412 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
413 file = g_file_parse_name (str);
415 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
421 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
424 GFile *file, *parent;
426 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
428 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
431 parent = g_file_get_parent (file);
432 g_object_unref (file);
438 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
445 gboolean result = FALSE;
446 gboolean is_dir = FALSE;
447 gchar *last_slash = NULL;
450 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
452 last_slash = strrchr (str, G_DIR_SEPARATOR);
454 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
456 if (g_file_equal (chooser_entry->base_folder, file))
458 /* this is when user types '.', could be the
459 * beginning of a hidden file, ./ or ../
461 *folder = g_object_ref (file);
462 *file_part = g_strdup (str);
467 /* it's a dir, or at least it ends with the dir separator */
468 *folder = g_object_ref (file);
469 *file_part = g_strdup ("");
476 parent_file = g_file_get_parent (file);
481 GTK_FILE_CHOOSER_ERROR,
482 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
483 "Could not get parent file");
489 *folder = parent_file;
493 *file_part = g_strdup (last_slash + 1);
495 *file_part = g_strdup (str);
499 g_object_unref (file);
504 /* Determines if the completion model has entries with a common prefix relative
505 * to the current contents of the entry. Also, if there's one and only one such
506 * path, stores it in unique_path_ret.
509 find_common_prefix (GtkFileChooserEntry *chooser_entry,
510 gchar **common_prefix_ret,
511 GFile **unique_file_ret,
512 gboolean *is_complete_not_unique_ret,
513 gboolean *prefix_expands_the_file_part_ret,
516 GtkEditable *editable;
520 char *text_up_to_cursor;
521 GFile *parsed_folder_file;
522 char *parsed_file_part;
524 *common_prefix_ret = NULL;
525 *unique_file_ret = NULL;
526 *is_complete_not_unique_ret = FALSE;
527 *prefix_expands_the_file_part_ret = FALSE;
529 editable = GTK_EDITABLE (chooser_entry);
531 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
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 RefreshStatus status;
687 text = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry),
688 0, gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
689 status = refresh_current_folder_and_file_part (chooser_entry, text);
697 g_assert (chooser_entry->current_folder_file != NULL);
699 if (chooser_entry->current_folder_loaded)
700 explicitly_complete (chooser_entry);
703 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
708 case REFRESH_INVALID_INPUT:
712 case REFRESH_INCOMPLETE_HOSTNAME:
716 case REFRESH_NONEXISTENT:
720 case REFRESH_NOT_LOCAL:
725 g_assert_not_reached ();
731 g_assert (chooser_entry->current_folder_file == NULL);
733 beep (chooser_entry);
734 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
739 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
742 GtkFileChooserEntry *chooser_entry;
743 GtkEditable *editable;
744 GdkModifierType state;
745 gboolean control_pressed;
747 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
748 editable = GTK_EDITABLE (widget);
750 if (!chooser_entry->eat_tabs)
751 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
753 control_pressed = FALSE;
755 if (gtk_get_current_event_state (&state))
757 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
758 control_pressed = TRUE;
761 /* This is a bit evil -- it makes Tab never leave the entry. It basically
762 * makes it 'safe' for people to hit. */
763 if (event->keyval == GDK_KEY_Tab && !control_pressed)
767 gtk_editable_get_selection_bounds (editable, &start, &end);
770 gtk_editable_set_position (editable, MAX (start, end));
772 start_explicit_completion (chooser_entry);
777 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
782 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
783 GdkEventFocus *event)
785 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
787 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
789 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
793 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
795 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
796 refresh_current_folder_and_file_part (chooser_entry, gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
800 gtk_file_chooser_entry_activate (GtkEntry *entry)
802 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
804 commit_completion_and_refresh (chooser_entry);
805 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
809 discard_completion_store (GtkFileChooserEntry *chooser_entry)
811 if (!chooser_entry->completion_store)
814 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
815 gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
816 g_object_unref (chooser_entry->completion_store);
817 chooser_entry->completion_store = NULL;
821 completion_store_set (GtkFileSystemModel *model,
828 GtkFileChooserEntry *chooser_entry = data;
830 const char *prefix = "";
831 const char *suffix = "";
836 g_value_set_object (value, file);
838 case FULL_PATH_COLUMN:
839 prefix = chooser_entry->dir_part;
841 case DISPLAY_NAME_COLUMN:
842 if (_gtk_file_info_consider_as_directory (info))
843 suffix = G_DIR_SEPARATOR_S;
845 g_value_take_string (value, g_strconcat (
847 g_file_info_get_display_name (info),
852 g_assert_not_reached ();
859 /* Fills the completion store from the contents of the current folder */
861 populate_completion_store (GtkFileChooserEntry *chooser_entry)
863 discard_completion_store (chooser_entry);
865 chooser_entry->completion_store = GTK_TREE_MODEL (
866 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
867 "standard::name,standard::display-name,standard::type",
868 completion_store_set,
874 g_signal_connect (chooser_entry->completion_store, "finished-loading",
875 G_CALLBACK (finished_loading_cb), chooser_entry);
877 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
878 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
880 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
881 chooser_entry->completion_store);
884 /* When we finish loading the current folder, this function should get called to
885 * perform the deferred explicit completion.
888 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
890 switch (chooser_entry->load_complete_action)
892 case LOAD_COMPLETE_NOTHING:
895 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
896 explicitly_complete (chooser_entry);
900 g_assert_not_reached ();
905 /* Callback when the current folder finishes loading */
907 finished_loading_cb (GtkFileSystemModel *model,
909 GtkFileChooserEntry *chooser_entry)
911 GtkEntryCompletion *completion;
913 chooser_entry->current_folder_loaded = TRUE;
917 LoadCompleteAction old_load_complete_action;
919 old_load_complete_action = chooser_entry->load_complete_action;
921 discard_completion_store (chooser_entry);
922 clear_completions (chooser_entry);
924 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
926 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
928 beep (chooser_entry);
934 perform_load_complete_action (chooser_entry);
936 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
938 completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
939 gtk_entry_completion_set_inline_completion (completion, TRUE);
940 gtk_entry_completion_complete (completion);
941 gtk_entry_completion_insert_prefix (completion);
945 reload_current_folder (GtkFileChooserEntry *chooser_entry,
948 g_assert (folder_file != NULL);
950 if (chooser_entry->current_folder_file
951 && g_file_equal (folder_file, chooser_entry->current_folder_file))
954 if (chooser_entry->current_folder_file)
956 discard_loading_and_current_folder_file (chooser_entry);
959 if (chooser_entry->local_only
960 && !g_file_is_native (folder_file))
961 return REFRESH_NOT_LOCAL;
963 chooser_entry->current_folder_file = g_object_ref (folder_file);
964 chooser_entry->current_folder_loaded = FALSE;
966 populate_completion_store (chooser_entry);
972 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
977 gsize total_len, file_part_len;
980 RefreshStatus result;
983 if (!gtk_file_chooser_entry_parse (chooser_entry,
984 text, &folder_file, &file_part, &error))
986 folder_file = g_object_ref (chooser_entry->base_folder);
988 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
989 result = REFRESH_NONEXISTENT;
991 result = REFRESH_INVALID_INPUT;
994 g_error_free (error);
996 file_part = g_strdup ("");
1001 g_assert (folder_file != NULL);
1003 file_part_len = strlen (file_part);
1004 total_len = strlen (text);
1005 if (total_len > file_part_len)
1006 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1010 result = REFRESH_OK;
1013 g_free (chooser_entry->file_part);
1014 g_free (chooser_entry->dir_part);
1016 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
1017 chooser_entry->file_part = file_part;
1018 chooser_entry->file_part_pos = file_part_pos;
1020 if (result == REFRESH_OK)
1022 result = reload_current_folder (chooser_entry, folder_file);
1026 discard_loading_and_current_folder_file (chooser_entry);
1030 g_object_unref (folder_file);
1032 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1033 ((result == REFRESH_OK)
1034 && (chooser_entry->current_folder_file != NULL))
1035 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1036 || ((result != REFRESH_OK)
1037 && (chooser_entry->current_folder_file == NULL)));
1044 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1045 const gchar *new_text,
1046 gint new_text_length,
1050 const gchar *colon = memchr (new_text, ':', new_text_length);
1053 /* Disallow these characters altogether */
1054 for (i = 0; i < new_text_length; i++)
1056 if (new_text[i] == '<' ||
1057 new_text[i] == '>' ||
1058 new_text[i] == '"' ||
1059 new_text[i] == '|' ||
1060 new_text[i] == '*' ||
1065 if (i < new_text_length ||
1066 /* Disallow entering text that would cause a colon to be anywhere except
1067 * after a drive letter.
1070 *position + (colon - new_text) != 1) ||
1071 (new_text_length > 0 &&
1073 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1074 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1076 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1077 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1085 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1090 /* If deleting a drive letter, delete the colon, too */
1091 if (start_pos == 0 && end_pos == 1 &&
1092 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1093 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1095 g_signal_handlers_block_by_func (chooser_entry,
1096 G_CALLBACK (delete_text_callback),
1098 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1099 g_signal_handlers_unblock_by_func (chooser_entry,
1100 G_CALLBACK (delete_text_callback),
1107 * _gtk_file_chooser_entry_new:
1108 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1110 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1111 * is an internal implementation widget for the GTK+ file chooser
1112 * which is an entry with completion with respect to a
1113 * #GtkFileSystem object.
1115 * Return value: the newly created #GtkFileChooserEntry
1118 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1120 GtkFileChooserEntry *chooser_entry;
1122 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1123 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1125 return GTK_WIDGET (chooser_entry);
1129 * _gtk_file_chooser_entry_set_base_folder:
1130 * @chooser_entry: a #GtkFileChooserEntry
1131 * @file: file for a folder in the chooser entries current file system.
1133 * Sets the folder with respect to which completions occur.
1136 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1140 g_object_ref (file);
1142 file = g_file_new_for_path (g_get_home_dir ());
1144 if (g_file_equal (chooser_entry->base_folder, file))
1146 g_object_unref (file);
1150 if (chooser_entry->base_folder)
1151 g_object_unref (chooser_entry->base_folder);
1153 chooser_entry->base_folder = file;
1155 clear_completions (chooser_entry);
1159 * _gtk_file_chooser_entry_get_current_folder:
1160 * @chooser_entry: a #GtkFileChooserEntry
1162 * Gets the current folder for the #GtkFileChooserEntry. If the
1163 * user has only entered a filename, this will be in the base folder
1164 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1165 * user has entered a relative or absolute path, then it will
1166 * be different. If the user has entered unparsable text, or text which
1167 * the entry cannot handle, this will return %NULL.
1169 * Return value: the file for the current folder - you must g_object_unref()
1170 * the value after use.
1173 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1175 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1177 return gtk_file_chooser_get_directory_for_text (chooser_entry,
1178 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
1182 * _gtk_file_chooser_entry_get_file_part:
1183 * @chooser_entry: a #GtkFileChooserEntry
1185 * Gets the non-folder portion of whatever the user has entered
1186 * into the file selector. What is returned is a UTF-8 string,
1187 * and if a filename path is needed, g_file_get_child_for_display_name()
1190 * Return value: the entered filename - this value is owned by the
1191 * chooser entry and must not be modified or freed.
1194 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1196 const char *last_slash, *text;
1198 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1200 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1201 last_slash = strrchr (text, G_DIR_SEPARATOR);
1203 return last_slash + 1;
1209 * _gtk_file_chooser_entry_set_file_part:
1210 * @chooser_entry: a #GtkFileChooserEntry
1211 * @file_part: text to display in the entry, in UTF-8
1213 * Sets the current text shown in the file chooser entry.
1216 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1217 const gchar *file_part)
1219 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1221 chooser_entry->in_change = TRUE;
1222 clear_completions (chooser_entry);
1223 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1224 chooser_entry->in_change = FALSE;
1229 * _gtk_file_chooser_entry_set_action:
1230 * @chooser_entry: a #GtkFileChooserEntry
1231 * @action: the action which is performed by the file selector using this entry
1233 * Sets action which is performed by the file selector using this entry.
1234 * The #GtkFileChooserEntry will use different completion strategies for
1235 * different actions.
1238 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1239 GtkFileChooserAction action)
1241 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1243 if (chooser_entry->action != action)
1245 GtkEntryCompletion *comp;
1247 chooser_entry->action = action;
1249 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1251 /* FIXME: do we need to actually set the following? */
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,
1292 if (chooser_entry->completion_store == NULL ||
1293 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1298 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1301 return _gtk_file_info_consider_as_directory (info);
1306 * _gtk_file_chooser_entry_select_filename:
1307 * @chooser_entry: a #GtkFileChooserEntry
1309 * Selects the filename (without the extension) for user edition.
1312 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1314 const gchar *str, *ext;
1317 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1319 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1320 ext = g_strrstr (str, ".");
1323 len = g_utf8_pointer_to_offset (str, ext);
1326 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1330 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1331 gboolean local_only)
1333 chooser_entry->local_only = local_only;
1334 clear_completions (chooser_entry);
1338 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1340 return chooser_entry->local_only;