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 void refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry);
122 static void finished_loading_cb (GtkFileSystemModel *model,
124 GtkFileChooserEntry *chooser_entry);
126 G_DEFINE_TYPE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY)
129 gtk_file_chooser_entry_get_completion_text (GtkFileChooserEntry *chooser_entry)
131 GtkEditable *editable = GTK_EDITABLE (chooser_entry);
134 gtk_editable_get_selection_bounds (editable, &start, &end);
135 return gtk_editable_get_chars (editable, 0, MIN (start, end));
139 gtk_file_chooser_entry_dispatch_properties_changed (GObject *object,
143 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
146 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
148 /* What we are after: The text in front of the cursor was modified.
149 * Unfortunately, there's no other way to catch this. */
151 for (i = 0; i < n_pspecs; i++)
153 if (pspecs[i]->name == I_("cursor-position") ||
154 pspecs[i]->name == I_("selection-bound") ||
155 pspecs[i]->name == I_("text"))
157 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
159 refresh_current_folder_and_file_part (chooser_entry);
167 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
169 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
170 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
172 gobject_class->finalize = gtk_file_chooser_entry_finalize;
173 gobject_class->dispose = gtk_file_chooser_entry_dispose;
174 gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
176 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
177 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
178 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
182 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
184 GtkEntryCompletion *comp;
185 GtkCellRenderer *cell;
187 chooser_entry->local_only = TRUE;
188 chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
190 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
192 comp = gtk_entry_completion_new ();
193 gtk_entry_completion_set_popup_single_match (comp, FALSE);
194 /* see docs for gtk_entry_completion_set_text_column() */
195 g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
197 gtk_entry_completion_set_match_func (comp,
198 completion_match_func,
202 cell = gtk_cell_renderer_text_new ();
203 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
205 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
207 "text", DISPLAY_NAME_COLUMN);
209 g_signal_connect (comp, "match-selected",
210 G_CALLBACK (match_selected_callback), chooser_entry);
212 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
213 g_object_unref (comp);
216 g_signal_connect (chooser_entry, "insert-text",
217 G_CALLBACK (insert_text_callback), NULL);
218 g_signal_connect (chooser_entry, "delete-text",
219 G_CALLBACK (delete_text_callback), NULL);
224 gtk_file_chooser_entry_finalize (GObject *object)
226 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
228 g_object_unref (chooser_entry->base_folder);
230 if (chooser_entry->current_folder_file)
231 g_object_unref (chooser_entry->current_folder_file);
233 g_free (chooser_entry->dir_part);
234 g_free (chooser_entry->file_part);
236 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
240 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
242 if (chooser_entry->current_folder_file)
244 g_object_unref (chooser_entry->current_folder_file);
245 chooser_entry->current_folder_file = NULL;
250 gtk_file_chooser_entry_dispose (GObject *object)
252 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
254 discard_loading_and_current_folder_file (chooser_entry);
256 if (chooser_entry->completion_store)
258 g_object_unref (chooser_entry->completion_store);
259 chooser_entry->completion_store = NULL;
262 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
265 /* Match functions for the GtkEntryCompletion */
267 match_selected_callback (GtkEntryCompletion *completion,
270 GtkFileChooserEntry *chooser_entry)
274 gtk_tree_model_get (model, iter,
275 FULL_PATH_COLUMN, &path,
278 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
280 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
281 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
291 /* Match function for the GtkEntryCompletion */
293 completion_match_func (GtkEntryCompletion *comp,
294 const char *key_unused,
298 GtkFileChooserEntry *chooser_entry;
301 char *norm_file_part;
304 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
306 /* We ignore the key because it is the contents of the entry. Instead, we
307 * just use our precomputed file_part.
309 if (!chooser_entry->file_part)
314 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
317 return FALSE; /* Uninitialized row, ugh */
320 /* If we have an empty file_part, then we're at the root of a directory. In
321 * that case, we want to match all non-dot files. We might want to match
322 * dot_files too if show_hidden is TRUE on the fileselector in the future.
324 /* Additionally, support for gnome .hidden files would be sweet, too */
325 if (chooser_entry->file_part[0] == '\000')
337 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
338 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
340 #ifdef G_PLATFORM_WIN32
344 temp = norm_file_part;
345 norm_file_part = g_utf8_casefold (norm_file_part, -1);
349 norm_name = g_utf8_casefold (norm_name, -1);
354 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
356 g_free (norm_file_part);
364 clear_completions (GtkFileChooserEntry *chooser_entry)
366 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
370 beep (GtkFileChooserEntry *chooser_entry)
372 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
376 is_valid_scheme_character (char c)
378 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
382 has_uri_scheme (const char *str)
388 if (!is_valid_scheme_character (*p))
393 while (is_valid_scheme_character (*p));
395 return (strncmp (p, "://", 3) == 0);
399 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
404 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
405 file = g_file_parse_name (str);
407 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
413 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
416 GFile *file, *parent;
418 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
420 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
423 parent = g_file_get_parent (file);
424 g_object_unref (file);
430 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
437 gboolean result = FALSE;
438 gboolean is_dir = FALSE;
439 gchar *last_slash = NULL;
442 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
444 last_slash = strrchr (str, G_DIR_SEPARATOR);
446 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
448 if (g_file_equal (chooser_entry->base_folder, file))
450 /* this is when user types '.', could be the
451 * beginning of a hidden file, ./ or ../
453 *folder = g_object_ref (file);
454 *file_part = g_strdup (str);
459 /* it's a dir, or at least it ends with the dir separator */
460 *folder = g_object_ref (file);
461 *file_part = g_strdup ("");
468 parent_file = g_file_get_parent (file);
473 GTK_FILE_CHOOSER_ERROR,
474 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
475 "Could not get parent file");
481 *folder = parent_file;
485 *file_part = g_strdup (last_slash + 1);
487 *file_part = g_strdup (str);
491 g_object_unref (file);
496 /* Finds a common prefix based on the contents of the entry
497 * and mandatorily appends it
500 explicitly_complete (GtkFileChooserEntry *chooser_entry)
502 clear_completions (chooser_entry);
504 if (chooser_entry->completion_store)
506 char *completion, *text;
507 gsize completion_len, text_len;
509 text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
510 text_len = strlen (text);
511 completion = gtk_entry_completion_compute_prefix (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), text);
512 completion_len = completion ? strlen (completion) : 0;
514 if (completion_len > text_len)
516 GtkEditable *editable = GTK_EDITABLE (chooser_entry);
517 int pos = gtk_editable_get_position (editable);
519 gtk_editable_insert_text (editable,
520 completion + text_len,
521 completion_len - text_len,
523 gtk_editable_set_position (editable, pos);
528 beep (chooser_entry);
532 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
534 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
535 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
539 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
541 if (chooser_entry->current_folder_loaded)
542 explicitly_complete (chooser_entry);
544 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
548 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
551 GtkFileChooserEntry *chooser_entry;
552 GtkEditable *editable;
553 GdkModifierType state;
554 gboolean control_pressed;
556 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
557 editable = GTK_EDITABLE (widget);
559 if (!chooser_entry->eat_tabs)
560 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
562 control_pressed = FALSE;
564 if (gtk_get_current_event_state (&state))
566 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
567 control_pressed = TRUE;
570 /* This is a bit evil -- it makes Tab never leave the entry. It basically
571 * makes it 'safe' for people to hit. */
572 if (event->keyval == GDK_KEY_Tab && !control_pressed)
576 gtk_editable_get_selection_bounds (editable, &start, &end);
579 gtk_editable_set_position (editable, MAX (start, end));
581 start_explicit_completion (chooser_entry);
586 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
591 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
592 GdkEventFocus *event)
594 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
596 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
598 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
602 discard_completion_store (GtkFileChooserEntry *chooser_entry)
604 if (!chooser_entry->completion_store)
607 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
608 gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
609 g_object_unref (chooser_entry->completion_store);
610 chooser_entry->completion_store = NULL;
614 completion_store_set (GtkFileSystemModel *model,
621 GtkFileChooserEntry *chooser_entry = data;
623 const char *prefix = "";
624 const char *suffix = "";
629 g_value_set_object (value, file);
631 case FULL_PATH_COLUMN:
632 prefix = chooser_entry->dir_part;
634 case DISPLAY_NAME_COLUMN:
635 if (_gtk_file_info_consider_as_directory (info))
636 suffix = G_DIR_SEPARATOR_S;
638 g_value_take_string (value, g_strconcat (
640 g_file_info_get_display_name (info),
645 g_assert_not_reached ();
652 /* Fills the completion store from the contents of the current folder */
654 populate_completion_store (GtkFileChooserEntry *chooser_entry)
656 discard_completion_store (chooser_entry);
658 chooser_entry->completion_store = GTK_TREE_MODEL (
659 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
660 "standard::name,standard::display-name,standard::type",
661 completion_store_set,
667 g_signal_connect (chooser_entry->completion_store, "finished-loading",
668 G_CALLBACK (finished_loading_cb), chooser_entry);
670 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
671 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
673 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
674 chooser_entry->completion_store);
677 /* When we finish loading the current folder, this function should get called to
678 * perform the deferred explicit completion.
681 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
683 switch (chooser_entry->load_complete_action)
685 case LOAD_COMPLETE_NOTHING:
688 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
689 explicitly_complete (chooser_entry);
693 g_assert_not_reached ();
698 /* Callback when the current folder finishes loading */
700 finished_loading_cb (GtkFileSystemModel *model,
702 GtkFileChooserEntry *chooser_entry)
704 GtkEntryCompletion *completion;
706 chooser_entry->current_folder_loaded = TRUE;
710 LoadCompleteAction old_load_complete_action;
712 old_load_complete_action = chooser_entry->load_complete_action;
714 discard_completion_store (chooser_entry);
715 clear_completions (chooser_entry);
717 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
719 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
721 beep (chooser_entry);
727 perform_load_complete_action (chooser_entry);
729 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
731 completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
732 gtk_entry_completion_set_inline_completion (completion, TRUE);
733 gtk_entry_completion_complete (completion);
734 gtk_entry_completion_insert_prefix (completion);
738 reload_current_folder (GtkFileChooserEntry *chooser_entry,
741 g_assert (folder_file != NULL);
743 if (chooser_entry->current_folder_file
744 && g_file_equal (folder_file, chooser_entry->current_folder_file))
747 if (chooser_entry->current_folder_file)
749 discard_loading_and_current_folder_file (chooser_entry);
752 if (chooser_entry->local_only
753 && !g_file_is_native (folder_file))
754 return REFRESH_NOT_LOCAL;
756 chooser_entry->current_folder_file = g_object_ref (folder_file);
757 chooser_entry->current_folder_loaded = FALSE;
759 populate_completion_store (chooser_entry);
765 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry)
769 gsize total_len, file_part_len;
772 RefreshStatus result;
775 text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
778 if (!gtk_file_chooser_entry_parse (chooser_entry,
779 text, &folder_file, &file_part, &error))
781 folder_file = g_object_ref (chooser_entry->base_folder);
783 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
784 result = REFRESH_NONEXISTENT;
786 result = REFRESH_INVALID_INPUT;
789 g_error_free (error);
791 file_part = g_strdup ("");
796 g_assert (folder_file != NULL);
798 file_part_len = strlen (file_part);
799 total_len = strlen (text);
800 if (total_len > file_part_len)
801 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
808 g_free (chooser_entry->file_part);
809 g_free (chooser_entry->dir_part);
811 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
812 chooser_entry->file_part = file_part;
813 chooser_entry->file_part_pos = file_part_pos;
815 if (result == REFRESH_OK)
817 reload_current_folder (chooser_entry, folder_file);
821 discard_loading_and_current_folder_file (chooser_entry);
825 g_object_unref (folder_file);
832 insert_text_callback (GtkFileChooserEntry *chooser_entry,
833 const gchar *new_text,
834 gint new_text_length,
838 const gchar *colon = memchr (new_text, ':', new_text_length);
841 /* Disallow these characters altogether */
842 for (i = 0; i < new_text_length; i++)
844 if (new_text[i] == '<' ||
845 new_text[i] == '>' ||
846 new_text[i] == '"' ||
847 new_text[i] == '|' ||
848 new_text[i] == '*' ||
853 if (i < new_text_length ||
854 /* Disallow entering text that would cause a colon to be anywhere except
855 * after a drive letter.
858 *position + (colon - new_text) != 1) ||
859 (new_text_length > 0 &&
861 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
862 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
864 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
865 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
873 delete_text_callback (GtkFileChooserEntry *chooser_entry,
878 /* If deleting a drive letter, delete the colon, too */
879 if (start_pos == 0 && end_pos == 1 &&
880 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
881 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
883 g_signal_handlers_block_by_func (chooser_entry,
884 G_CALLBACK (delete_text_callback),
886 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
887 g_signal_handlers_unblock_by_func (chooser_entry,
888 G_CALLBACK (delete_text_callback),
895 * _gtk_file_chooser_entry_new:
896 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
898 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
899 * is an internal implementation widget for the GTK+ file chooser
900 * which is an entry with completion with respect to a
901 * #GtkFileSystem object.
903 * Return value: the newly created #GtkFileChooserEntry
906 _gtk_file_chooser_entry_new (gboolean eat_tabs)
908 GtkFileChooserEntry *chooser_entry;
910 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
911 chooser_entry->eat_tabs = (eat_tabs != FALSE);
913 return GTK_WIDGET (chooser_entry);
917 * _gtk_file_chooser_entry_set_base_folder:
918 * @chooser_entry: a #GtkFileChooserEntry
919 * @file: file for a folder in the chooser entries current file system.
921 * Sets the folder with respect to which completions occur.
924 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
930 file = g_file_new_for_path (g_get_home_dir ());
932 if (g_file_equal (chooser_entry->base_folder, file))
934 g_object_unref (file);
938 if (chooser_entry->base_folder)
939 g_object_unref (chooser_entry->base_folder);
941 chooser_entry->base_folder = file;
943 clear_completions (chooser_entry);
947 * _gtk_file_chooser_entry_get_current_folder:
948 * @chooser_entry: a #GtkFileChooserEntry
950 * Gets the current folder for the #GtkFileChooserEntry. If the
951 * user has only entered a filename, this will be in the base folder
952 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
953 * user has entered a relative or absolute path, then it will
954 * be different. If the user has entered unparsable text, or text which
955 * the entry cannot handle, this will return %NULL.
957 * Return value: the file for the current folder - you must g_object_unref()
958 * the value after use.
961 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
963 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
965 return gtk_file_chooser_get_directory_for_text (chooser_entry,
966 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
970 * _gtk_file_chooser_entry_get_file_part:
971 * @chooser_entry: a #GtkFileChooserEntry
973 * Gets the non-folder portion of whatever the user has entered
974 * into the file selector. What is returned is a UTF-8 string,
975 * and if a filename path is needed, g_file_get_child_for_display_name()
978 * Return value: the entered filename - this value is owned by the
979 * chooser entry and must not be modified or freed.
982 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
984 const char *last_slash, *text;
986 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
988 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
989 last_slash = strrchr (text, G_DIR_SEPARATOR);
991 return last_slash + 1;
997 * _gtk_file_chooser_entry_set_action:
998 * @chooser_entry: a #GtkFileChooserEntry
999 * @action: the action which is performed by the file selector using this entry
1001 * Sets action which is performed by the file selector using this entry.
1002 * The #GtkFileChooserEntry will use different completion strategies for
1003 * different actions.
1006 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1007 GtkFileChooserAction action)
1009 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1011 if (chooser_entry->action != action)
1013 GtkEntryCompletion *comp;
1015 chooser_entry->action = action;
1017 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1019 /* FIXME: do we need to actually set the following? */
1023 case GTK_FILE_CHOOSER_ACTION_OPEN:
1024 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1025 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1027 case GTK_FILE_CHOOSER_ACTION_SAVE:
1028 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1029 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1037 * _gtk_file_chooser_entry_get_action:
1038 * @chooser_entry: a #GtkFileChooserEntry
1040 * Gets the action for this entry.
1042 * Returns: the action
1044 GtkFileChooserAction
1045 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1047 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1048 GTK_FILE_CHOOSER_ACTION_OPEN);
1050 return chooser_entry->action;
1054 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1060 if (chooser_entry->completion_store == NULL ||
1061 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1066 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1069 return _gtk_file_info_consider_as_directory (info);
1074 * _gtk_file_chooser_entry_select_filename:
1075 * @chooser_entry: a #GtkFileChooserEntry
1077 * Selects the filename (without the extension) for user edition.
1080 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1082 const gchar *str, *ext;
1085 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1087 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1088 ext = g_strrstr (str, ".");
1091 len = g_utf8_pointer_to_offset (str, ext);
1094 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1098 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1099 gboolean local_only)
1101 chooser_entry->local_only = local_only;
1102 clear_completions (chooser_entry);
1106 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1108 return chooser_entry->local_only;