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;
92 static void gtk_file_chooser_entry_finalize (GObject *object);
93 static void gtk_file_chooser_entry_dispose (GObject *object);
94 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
95 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
97 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
98 GdkEventFocus *event);
101 static gint insert_text_callback (GtkFileChooserEntry *widget,
102 const gchar *new_text,
103 gint new_text_length,
106 static void delete_text_callback (GtkFileChooserEntry *widget,
112 static gboolean match_selected_callback (GtkEntryCompletion *completion,
115 GtkFileChooserEntry *chooser_entry);
116 static gboolean completion_match_func (GtkEntryCompletion *comp,
121 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
123 static void finished_loading_cb (GtkFileSystemModel *model,
125 GtkFileChooserEntry *chooser_entry);
127 G_DEFINE_TYPE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY)
130 gtk_file_chooser_entry_get_completion_text (GtkFileChooserEntry *chooser_entry)
132 GtkEditable *editable = GTK_EDITABLE (chooser_entry);
135 gtk_editable_get_selection_bounds (editable, &start, &end);
136 return gtk_editable_get_chars (editable, 0, MIN (start, end));
140 gtk_file_chooser_entry_dispatch_properties_changed (GObject *object,
144 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
147 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
149 /* What we are after: The text in front of the cursor was modified.
150 * Unfortunately, there's no other way to catch this. */
152 for (i = 0; i < n_pspecs; i++)
154 if (pspecs[i]->name == I_("cursor-position") ||
155 pspecs[i]->name == I_("selection-bound") ||
156 pspecs[i]->name == I_("text"))
160 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
162 text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
163 refresh_current_folder_and_file_part (chooser_entry, text);
172 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
174 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
175 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
177 gobject_class->finalize = gtk_file_chooser_entry_finalize;
178 gobject_class->dispose = gtk_file_chooser_entry_dispose;
179 gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
181 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
182 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
183 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
187 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
189 GtkEntryCompletion *comp;
190 GtkCellRenderer *cell;
192 chooser_entry->local_only = TRUE;
193 chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
195 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
197 comp = gtk_entry_completion_new ();
198 gtk_entry_completion_set_popup_single_match (comp, FALSE);
199 /* see docs for gtk_entry_completion_set_text_column() */
200 g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
202 gtk_entry_completion_set_match_func (comp,
203 completion_match_func,
207 cell = gtk_cell_renderer_text_new ();
208 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
210 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
212 "text", DISPLAY_NAME_COLUMN);
214 g_signal_connect (comp, "match-selected",
215 G_CALLBACK (match_selected_callback), chooser_entry);
217 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
218 g_object_unref (comp);
221 g_signal_connect (chooser_entry, "insert-text",
222 G_CALLBACK (insert_text_callback), NULL);
223 g_signal_connect (chooser_entry, "delete-text",
224 G_CALLBACK (delete_text_callback), NULL);
229 gtk_file_chooser_entry_finalize (GObject *object)
231 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
233 g_object_unref (chooser_entry->base_folder);
235 if (chooser_entry->current_folder_file)
236 g_object_unref (chooser_entry->current_folder_file);
238 g_free (chooser_entry->dir_part);
239 g_free (chooser_entry->file_part);
241 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
245 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
247 if (chooser_entry->current_folder_file)
249 g_object_unref (chooser_entry->current_folder_file);
250 chooser_entry->current_folder_file = NULL;
255 gtk_file_chooser_entry_dispose (GObject *object)
257 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
259 discard_loading_and_current_folder_file (chooser_entry);
261 if (chooser_entry->completion_store)
263 g_object_unref (chooser_entry->completion_store);
264 chooser_entry->completion_store = NULL;
267 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
270 /* Match functions for the GtkEntryCompletion */
272 match_selected_callback (GtkEntryCompletion *completion,
275 GtkFileChooserEntry *chooser_entry)
279 gtk_tree_model_get (model, iter,
280 FULL_PATH_COLUMN, &path,
283 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
285 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
286 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
296 /* Match function for the GtkEntryCompletion */
298 completion_match_func (GtkEntryCompletion *comp,
299 const char *key_unused,
303 GtkFileChooserEntry *chooser_entry;
306 char *norm_file_part;
309 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
311 /* We ignore the key because it is the contents of the entry. Instead, we
312 * just use our precomputed file_part.
314 if (!chooser_entry->file_part)
319 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
322 return FALSE; /* Uninitialized row, ugh */
325 /* If we have an empty file_part, then we're at the root of a directory. In
326 * that case, we want to match all non-dot files. We might want to match
327 * dot_files too if show_hidden is TRUE on the fileselector in the future.
329 /* Additionally, support for gnome .hidden files would be sweet, too */
330 if (chooser_entry->file_part[0] == '\000')
342 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
343 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
345 #ifdef G_PLATFORM_WIN32
349 temp = norm_file_part;
350 norm_file_part = g_utf8_casefold (norm_file_part, -1);
354 norm_name = g_utf8_casefold (norm_name, -1);
359 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
361 g_free (norm_file_part);
369 clear_completions (GtkFileChooserEntry *chooser_entry)
371 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
375 beep (GtkFileChooserEntry *chooser_entry)
377 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
381 is_valid_scheme_character (char c)
383 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
387 has_uri_scheme (const char *str)
393 if (!is_valid_scheme_character (*p))
398 while (is_valid_scheme_character (*p));
400 return (strncmp (p, "://", 3) == 0);
404 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
409 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
410 file = g_file_parse_name (str);
412 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
418 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
421 GFile *file, *parent;
423 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
425 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
428 parent = g_file_get_parent (file);
429 g_object_unref (file);
435 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
442 gboolean result = FALSE;
443 gboolean is_dir = FALSE;
444 gchar *last_slash = NULL;
447 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
449 last_slash = strrchr (str, G_DIR_SEPARATOR);
451 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
453 if (g_file_equal (chooser_entry->base_folder, file))
455 /* this is when user types '.', could be the
456 * beginning of a hidden file, ./ or ../
458 *folder = g_object_ref (file);
459 *file_part = g_strdup (str);
464 /* it's a dir, or at least it ends with the dir separator */
465 *folder = g_object_ref (file);
466 *file_part = g_strdup ("");
473 parent_file = g_file_get_parent (file);
478 GTK_FILE_CHOOSER_ERROR,
479 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
480 "Could not get parent file");
486 *folder = parent_file;
490 *file_part = g_strdup (last_slash + 1);
492 *file_part = g_strdup (str);
496 g_object_unref (file);
501 /* Finds a common prefix based on the contents of the entry
502 * and mandatorily appends it
505 explicitly_complete (GtkFileChooserEntry *chooser_entry)
507 clear_completions (chooser_entry);
509 if (chooser_entry->completion_store)
511 char *completion, *text;
512 gsize completion_len, text_len;
514 text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
515 text_len = strlen (text);
516 completion = gtk_entry_completion_compute_prefix (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), text);
517 completion_len = completion ? strlen (completion) : 0;
519 if (completion_len > text_len)
521 GtkEditable *editable = GTK_EDITABLE (chooser_entry);
522 int pos = gtk_editable_get_position (editable);
524 gtk_editable_insert_text (editable,
525 completion + text_len,
526 completion_len - text_len,
528 gtk_editable_set_position (editable, pos);
533 beep (chooser_entry);
537 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
539 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
540 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
544 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
546 if (chooser_entry->current_folder_loaded)
547 explicitly_complete (chooser_entry);
549 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
553 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
556 GtkFileChooserEntry *chooser_entry;
557 GtkEditable *editable;
558 GdkModifierType state;
559 gboolean control_pressed;
561 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
562 editable = GTK_EDITABLE (widget);
564 if (!chooser_entry->eat_tabs)
565 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
567 control_pressed = FALSE;
569 if (gtk_get_current_event_state (&state))
571 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
572 control_pressed = TRUE;
575 /* This is a bit evil -- it makes Tab never leave the entry. It basically
576 * makes it 'safe' for people to hit. */
577 if (event->keyval == GDK_KEY_Tab && !control_pressed)
581 gtk_editable_get_selection_bounds (editable, &start, &end);
584 gtk_editable_set_position (editable, MAX (start, end));
586 start_explicit_completion (chooser_entry);
591 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
596 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
597 GdkEventFocus *event)
599 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
601 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
603 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
607 discard_completion_store (GtkFileChooserEntry *chooser_entry)
609 if (!chooser_entry->completion_store)
612 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
613 gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
614 g_object_unref (chooser_entry->completion_store);
615 chooser_entry->completion_store = NULL;
619 completion_store_set (GtkFileSystemModel *model,
626 GtkFileChooserEntry *chooser_entry = data;
628 const char *prefix = "";
629 const char *suffix = "";
634 g_value_set_object (value, file);
636 case FULL_PATH_COLUMN:
637 prefix = chooser_entry->dir_part;
639 case DISPLAY_NAME_COLUMN:
640 if (_gtk_file_info_consider_as_directory (info))
641 suffix = G_DIR_SEPARATOR_S;
643 g_value_take_string (value, g_strconcat (
645 g_file_info_get_display_name (info),
650 g_assert_not_reached ();
657 /* Fills the completion store from the contents of the current folder */
659 populate_completion_store (GtkFileChooserEntry *chooser_entry)
661 discard_completion_store (chooser_entry);
663 chooser_entry->completion_store = GTK_TREE_MODEL (
664 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
665 "standard::name,standard::display-name,standard::type",
666 completion_store_set,
672 g_signal_connect (chooser_entry->completion_store, "finished-loading",
673 G_CALLBACK (finished_loading_cb), chooser_entry);
675 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
676 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
678 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
679 chooser_entry->completion_store);
682 /* When we finish loading the current folder, this function should get called to
683 * perform the deferred explicit completion.
686 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
688 switch (chooser_entry->load_complete_action)
690 case LOAD_COMPLETE_NOTHING:
693 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
694 explicitly_complete (chooser_entry);
698 g_assert_not_reached ();
703 /* Callback when the current folder finishes loading */
705 finished_loading_cb (GtkFileSystemModel *model,
707 GtkFileChooserEntry *chooser_entry)
709 GtkEntryCompletion *completion;
711 chooser_entry->current_folder_loaded = TRUE;
715 LoadCompleteAction old_load_complete_action;
717 old_load_complete_action = chooser_entry->load_complete_action;
719 discard_completion_store (chooser_entry);
720 clear_completions (chooser_entry);
722 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
724 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
726 beep (chooser_entry);
732 perform_load_complete_action (chooser_entry);
734 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
736 completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
737 gtk_entry_completion_set_inline_completion (completion, TRUE);
738 gtk_entry_completion_complete (completion);
739 gtk_entry_completion_insert_prefix (completion);
743 reload_current_folder (GtkFileChooserEntry *chooser_entry,
746 g_assert (folder_file != NULL);
748 if (chooser_entry->current_folder_file
749 && g_file_equal (folder_file, chooser_entry->current_folder_file))
752 if (chooser_entry->current_folder_file)
754 discard_loading_and_current_folder_file (chooser_entry);
757 if (chooser_entry->local_only
758 && !g_file_is_native (folder_file))
759 return REFRESH_NOT_LOCAL;
761 chooser_entry->current_folder_file = g_object_ref (folder_file);
762 chooser_entry->current_folder_loaded = FALSE;
764 populate_completion_store (chooser_entry);
770 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
775 gsize total_len, file_part_len;
778 RefreshStatus result;
781 if (!gtk_file_chooser_entry_parse (chooser_entry,
782 text, &folder_file, &file_part, &error))
784 folder_file = g_object_ref (chooser_entry->base_folder);
786 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
787 result = REFRESH_NONEXISTENT;
789 result = REFRESH_INVALID_INPUT;
792 g_error_free (error);
794 file_part = g_strdup ("");
799 g_assert (folder_file != NULL);
801 file_part_len = strlen (file_part);
802 total_len = strlen (text);
803 if (total_len > file_part_len)
804 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
811 g_free (chooser_entry->file_part);
812 g_free (chooser_entry->dir_part);
814 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
815 chooser_entry->file_part = file_part;
816 chooser_entry->file_part_pos = file_part_pos;
818 if (result == REFRESH_OK)
820 result = reload_current_folder (chooser_entry, folder_file);
824 discard_loading_and_current_folder_file (chooser_entry);
828 g_object_unref (folder_file);
830 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
831 ((result == REFRESH_OK)
832 && (chooser_entry->current_folder_file != NULL))
833 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
834 || ((result != REFRESH_OK)
835 && (chooser_entry->current_folder_file == NULL)));
842 insert_text_callback (GtkFileChooserEntry *chooser_entry,
843 const gchar *new_text,
844 gint new_text_length,
848 const gchar *colon = memchr (new_text, ':', new_text_length);
851 /* Disallow these characters altogether */
852 for (i = 0; i < new_text_length; i++)
854 if (new_text[i] == '<' ||
855 new_text[i] == '>' ||
856 new_text[i] == '"' ||
857 new_text[i] == '|' ||
858 new_text[i] == '*' ||
863 if (i < new_text_length ||
864 /* Disallow entering text that would cause a colon to be anywhere except
865 * after a drive letter.
868 *position + (colon - new_text) != 1) ||
869 (new_text_length > 0 &&
871 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
872 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
874 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
875 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
883 delete_text_callback (GtkFileChooserEntry *chooser_entry,
888 /* If deleting a drive letter, delete the colon, too */
889 if (start_pos == 0 && end_pos == 1 &&
890 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
891 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
893 g_signal_handlers_block_by_func (chooser_entry,
894 G_CALLBACK (delete_text_callback),
896 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
897 g_signal_handlers_unblock_by_func (chooser_entry,
898 G_CALLBACK (delete_text_callback),
905 * _gtk_file_chooser_entry_new:
906 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
908 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
909 * is an internal implementation widget for the GTK+ file chooser
910 * which is an entry with completion with respect to a
911 * #GtkFileSystem object.
913 * Return value: the newly created #GtkFileChooserEntry
916 _gtk_file_chooser_entry_new (gboolean eat_tabs)
918 GtkFileChooserEntry *chooser_entry;
920 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
921 chooser_entry->eat_tabs = (eat_tabs != FALSE);
923 return GTK_WIDGET (chooser_entry);
927 * _gtk_file_chooser_entry_set_base_folder:
928 * @chooser_entry: a #GtkFileChooserEntry
929 * @file: file for a folder in the chooser entries current file system.
931 * Sets the folder with respect to which completions occur.
934 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
940 file = g_file_new_for_path (g_get_home_dir ());
942 if (g_file_equal (chooser_entry->base_folder, file))
944 g_object_unref (file);
948 if (chooser_entry->base_folder)
949 g_object_unref (chooser_entry->base_folder);
951 chooser_entry->base_folder = file;
953 clear_completions (chooser_entry);
957 * _gtk_file_chooser_entry_get_current_folder:
958 * @chooser_entry: a #GtkFileChooserEntry
960 * Gets the current folder for the #GtkFileChooserEntry. If the
961 * user has only entered a filename, this will be in the base folder
962 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
963 * user has entered a relative or absolute path, then it will
964 * be different. If the user has entered unparsable text, or text which
965 * the entry cannot handle, this will return %NULL.
967 * Return value: the file for the current folder - you must g_object_unref()
968 * the value after use.
971 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
973 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
975 return gtk_file_chooser_get_directory_for_text (chooser_entry,
976 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
980 * _gtk_file_chooser_entry_get_file_part:
981 * @chooser_entry: a #GtkFileChooserEntry
983 * Gets the non-folder portion of whatever the user has entered
984 * into the file selector. What is returned is a UTF-8 string,
985 * and if a filename path is needed, g_file_get_child_for_display_name()
988 * Return value: the entered filename - this value is owned by the
989 * chooser entry and must not be modified or freed.
992 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
994 const char *last_slash, *text;
996 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
998 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
999 last_slash = strrchr (text, G_DIR_SEPARATOR);
1001 return last_slash + 1;
1007 * _gtk_file_chooser_entry_set_action:
1008 * @chooser_entry: a #GtkFileChooserEntry
1009 * @action: the action which is performed by the file selector using this entry
1011 * Sets action which is performed by the file selector using this entry.
1012 * The #GtkFileChooserEntry will use different completion strategies for
1013 * different actions.
1016 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1017 GtkFileChooserAction action)
1019 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1021 if (chooser_entry->action != action)
1023 GtkEntryCompletion *comp;
1025 chooser_entry->action = action;
1027 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1029 /* FIXME: do we need to actually set the following? */
1033 case GTK_FILE_CHOOSER_ACTION_OPEN:
1034 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1035 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1037 case GTK_FILE_CHOOSER_ACTION_SAVE:
1038 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1039 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1047 * _gtk_file_chooser_entry_get_action:
1048 * @chooser_entry: a #GtkFileChooserEntry
1050 * Gets the action for this entry.
1052 * Returns: the action
1054 GtkFileChooserAction
1055 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1057 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1058 GTK_FILE_CHOOSER_ACTION_OPEN);
1060 return chooser_entry->action;
1064 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1070 if (chooser_entry->completion_store == NULL ||
1071 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1076 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1079 return _gtk_file_info_consider_as_directory (info);
1084 * _gtk_file_chooser_entry_select_filename:
1085 * @chooser_entry: a #GtkFileChooserEntry
1087 * Selects the filename (without the extension) for user edition.
1090 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1092 const gchar *str, *ext;
1095 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1097 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1098 ext = g_strrstr (str, ".");
1101 len = g_utf8_pointer_to_offset (str, ext);
1104 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1108 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1109 gboolean local_only)
1111 chooser_entry->local_only = local_only;
1112 clear_completions (chooser_entry);
1116 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1118 return chooser_entry->local_only;