1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserentry.c: Entry with filename completion
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gtkfilechooserentry.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
30 #include "gtkfilesystemmodel.h"
33 #include "gtksizerequest.h"
34 #include "gtkwindow.h"
37 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
39 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
40 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
41 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
43 struct _GtkFileChooserEntryClass
45 GtkEntryClass parent_class;
48 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
50 LOAD_COMPLETE_NOTHING,
51 LOAD_COMPLETE_EXPLICIT_COMPLETION
57 REFRESH_INVALID_INPUT,
58 REFRESH_INCOMPLETE_HOSTNAME,
63 struct _GtkFileChooserEntry
65 GtkEntry parent_instance;
67 GtkFileChooserAction action;
70 GFile *current_folder_file;
75 LoadCompleteAction load_complete_action;
77 GtkTreeModel *completion_store;
79 guint current_folder_loaded : 1;
93 static void gtk_file_chooser_entry_finalize (GObject *object);
94 static void gtk_file_chooser_entry_dispose (GObject *object);
95 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
96 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
98 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
99 GdkEventFocus *event);
102 static gint insert_text_callback (GtkFileChooserEntry *widget,
103 const gchar *new_text,
104 gint new_text_length,
107 static void delete_text_callback (GtkFileChooserEntry *widget,
113 static gboolean match_selected_callback (GtkEntryCompletion *completion,
116 GtkFileChooserEntry *chooser_entry);
117 static gboolean completion_match_func (GtkEntryCompletion *comp,
122 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
124 static void finished_loading_cb (GtkFileSystemModel *model,
126 GtkFileChooserEntry *chooser_entry);
128 G_DEFINE_TYPE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY)
131 gtk_file_chooser_entry_get_completion_text (GtkFileChooserEntry *chooser_entry)
133 GtkEditable *editable = GTK_EDITABLE (chooser_entry);
136 gtk_editable_get_selection_bounds (editable, &start, &end);
137 return gtk_editable_get_chars (editable, 0, MIN (start, end));
141 gtk_file_chooser_entry_dispatch_properties_changed (GObject *object,
145 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
148 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
150 /* What we are after: The text in front of the cursor was modified.
151 * Unfortunately, there's no other way to catch this. */
153 if (chooser_entry->in_change)
156 for (i = 0; i < n_pspecs; i++)
158 if (pspecs[i]->name == I_("cursor-position") ||
159 pspecs[i]->name == I_("selection-bound") ||
160 pspecs[i]->name == I_("text"))
164 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
166 text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
167 refresh_current_folder_and_file_part (chooser_entry, text);
176 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
181 gobject_class->finalize = gtk_file_chooser_entry_finalize;
182 gobject_class->dispose = gtk_file_chooser_entry_dispose;
183 gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
185 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
186 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
187 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
191 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
193 GtkEntryCompletion *comp;
194 GtkCellRenderer *cell;
196 chooser_entry->local_only = TRUE;
197 chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
199 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
201 comp = gtk_entry_completion_new ();
202 gtk_entry_completion_set_popup_single_match (comp, FALSE);
203 /* see docs for gtk_entry_completion_set_text_column() */
204 g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
206 gtk_entry_completion_set_match_func (comp,
207 completion_match_func,
211 cell = gtk_cell_renderer_text_new ();
212 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
214 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
216 "text", DISPLAY_NAME_COLUMN);
218 g_signal_connect (comp, "match-selected",
219 G_CALLBACK (match_selected_callback), chooser_entry);
221 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
222 g_object_unref (comp);
225 g_signal_connect (chooser_entry, "insert-text",
226 G_CALLBACK (insert_text_callback), NULL);
227 g_signal_connect (chooser_entry, "delete-text",
228 G_CALLBACK (delete_text_callback), NULL);
233 gtk_file_chooser_entry_finalize (GObject *object)
235 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
237 g_object_unref (chooser_entry->base_folder);
239 if (chooser_entry->current_folder_file)
240 g_object_unref (chooser_entry->current_folder_file);
242 g_free (chooser_entry->dir_part);
243 g_free (chooser_entry->file_part);
245 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
249 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
251 if (chooser_entry->current_folder_file)
253 g_object_unref (chooser_entry->current_folder_file);
254 chooser_entry->current_folder_file = NULL;
259 gtk_file_chooser_entry_dispose (GObject *object)
261 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
263 discard_loading_and_current_folder_file (chooser_entry);
265 if (chooser_entry->completion_store)
267 g_object_unref (chooser_entry->completion_store);
268 chooser_entry->completion_store = NULL;
271 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
274 /* Match functions for the GtkEntryCompletion */
276 match_selected_callback (GtkEntryCompletion *completion,
279 GtkFileChooserEntry *chooser_entry)
283 gtk_tree_model_get (model, iter,
284 FULL_PATH_COLUMN, &path,
287 /* We don't set in_change here as we want to update the current_folder
289 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
291 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
292 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
302 /* Match function for the GtkEntryCompletion */
304 completion_match_func (GtkEntryCompletion *comp,
305 const char *key_unused,
309 GtkFileChooserEntry *chooser_entry;
312 char *norm_file_part;
315 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
317 /* We ignore the key because it is the contents of the entry. Instead, we
318 * just use our precomputed file_part.
320 if (!chooser_entry->file_part)
325 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
328 return FALSE; /* Uninitialized row, ugh */
331 /* If we have an empty file_part, then we're at the root of a directory. In
332 * that case, we want to match all non-dot files. We might want to match
333 * dot_files too if show_hidden is TRUE on the fileselector in the future.
335 /* Additionally, support for gnome .hidden files would be sweet, too */
336 if (chooser_entry->file_part[0] == '\000')
348 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
349 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
351 #ifdef G_PLATFORM_WIN32
355 temp = norm_file_part;
356 norm_file_part = g_utf8_casefold (norm_file_part, -1);
360 norm_name = g_utf8_casefold (norm_name, -1);
365 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
367 g_free (norm_file_part);
375 clear_completions (GtkFileChooserEntry *chooser_entry)
377 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
381 beep (GtkFileChooserEntry *chooser_entry)
383 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
387 is_valid_scheme_character (char c)
389 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
393 has_uri_scheme (const char *str)
399 if (!is_valid_scheme_character (*p))
404 while (is_valid_scheme_character (*p));
406 return (strncmp (p, "://", 3) == 0);
410 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
415 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
416 file = g_file_parse_name (str);
418 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
424 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
427 GFile *file, *parent;
429 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
431 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
434 parent = g_file_get_parent (file);
435 g_object_unref (file);
441 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
448 gboolean result = FALSE;
449 gboolean is_dir = FALSE;
450 gchar *last_slash = NULL;
453 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
455 last_slash = strrchr (str, G_DIR_SEPARATOR);
457 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
459 if (g_file_equal (chooser_entry->base_folder, file))
461 /* this is when user types '.', could be the
462 * beginning of a hidden file, ./ or ../
464 *folder = g_object_ref (file);
465 *file_part = g_strdup (str);
470 /* it's a dir, or at least it ends with the dir separator */
471 *folder = g_object_ref (file);
472 *file_part = g_strdup ("");
479 parent_file = g_file_get_parent (file);
484 GTK_FILE_CHOOSER_ERROR,
485 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
486 "Could not get parent file");
492 *folder = parent_file;
496 *file_part = g_strdup (last_slash + 1);
498 *file_part = g_strdup (str);
502 g_object_unref (file);
507 /* Finds a common prefix based on the contents of the entry
508 * and mandatorily appends it
511 explicitly_complete (GtkFileChooserEntry *chooser_entry)
513 clear_completions (chooser_entry);
515 if (chooser_entry->completion_store)
517 char *completion, *text;
518 gsize completion_len, text_len;
520 text = gtk_file_chooser_entry_get_completion_text (chooser_entry);
521 text_len = strlen (text);
522 completion = gtk_entry_completion_compute_prefix (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), text);
523 completion_len = completion ? strlen (completion) : 0;
525 if (completion_len > text_len)
527 GtkEditable *editable = GTK_EDITABLE (chooser_entry);
528 int pos = gtk_editable_get_position (editable);
530 gtk_editable_insert_text (editable,
531 completion + text_len,
532 completion_len - text_len,
534 gtk_editable_set_position (editable, pos);
539 beep (chooser_entry);
543 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
545 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
546 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
550 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
552 if (chooser_entry->current_folder_loaded)
553 explicitly_complete (chooser_entry);
555 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
559 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
562 GtkFileChooserEntry *chooser_entry;
563 GtkEditable *editable;
564 GdkModifierType state;
565 gboolean control_pressed;
567 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
568 editable = GTK_EDITABLE (widget);
570 if (!chooser_entry->eat_tabs)
571 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
573 control_pressed = FALSE;
575 if (gtk_get_current_event_state (&state))
577 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
578 control_pressed = TRUE;
581 /* This is a bit evil -- it makes Tab never leave the entry. It basically
582 * makes it 'safe' for people to hit. */
583 if (event->keyval == GDK_KEY_Tab && !control_pressed)
587 gtk_editable_get_selection_bounds (editable, &start, &end);
590 gtk_editable_set_position (editable, MAX (start, end));
592 start_explicit_completion (chooser_entry);
597 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
602 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
603 GdkEventFocus *event)
605 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
607 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
609 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
613 discard_completion_store (GtkFileChooserEntry *chooser_entry)
615 if (!chooser_entry->completion_store)
618 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
619 gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
620 g_object_unref (chooser_entry->completion_store);
621 chooser_entry->completion_store = NULL;
625 completion_store_set (GtkFileSystemModel *model,
632 GtkFileChooserEntry *chooser_entry = data;
634 const char *prefix = "";
635 const char *suffix = "";
640 g_value_set_object (value, file);
642 case FULL_PATH_COLUMN:
643 prefix = chooser_entry->dir_part;
645 case DISPLAY_NAME_COLUMN:
646 if (_gtk_file_info_consider_as_directory (info))
647 suffix = G_DIR_SEPARATOR_S;
649 g_value_take_string (value, g_strconcat (
651 g_file_info_get_display_name (info),
656 g_assert_not_reached ();
663 /* Fills the completion store from the contents of the current folder */
665 populate_completion_store (GtkFileChooserEntry *chooser_entry)
667 discard_completion_store (chooser_entry);
669 chooser_entry->completion_store = GTK_TREE_MODEL (
670 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
671 "standard::name,standard::display-name,standard::type",
672 completion_store_set,
678 g_signal_connect (chooser_entry->completion_store, "finished-loading",
679 G_CALLBACK (finished_loading_cb), chooser_entry);
681 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
682 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
684 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
685 chooser_entry->completion_store);
688 /* When we finish loading the current folder, this function should get called to
689 * perform the deferred explicit completion.
692 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
694 switch (chooser_entry->load_complete_action)
696 case LOAD_COMPLETE_NOTHING:
699 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
700 explicitly_complete (chooser_entry);
704 g_assert_not_reached ();
709 /* Callback when the current folder finishes loading */
711 finished_loading_cb (GtkFileSystemModel *model,
713 GtkFileChooserEntry *chooser_entry)
715 GtkEntryCompletion *completion;
717 chooser_entry->current_folder_loaded = TRUE;
721 LoadCompleteAction old_load_complete_action;
723 old_load_complete_action = chooser_entry->load_complete_action;
725 discard_completion_store (chooser_entry);
726 clear_completions (chooser_entry);
728 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
730 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
732 beep (chooser_entry);
738 perform_load_complete_action (chooser_entry);
740 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
742 completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
743 gtk_entry_completion_set_inline_completion (completion, TRUE);
744 gtk_entry_completion_complete (completion);
745 gtk_entry_completion_insert_prefix (completion);
749 reload_current_folder (GtkFileChooserEntry *chooser_entry,
752 g_assert (folder_file != NULL);
754 if (chooser_entry->current_folder_file
755 && g_file_equal (folder_file, chooser_entry->current_folder_file))
758 if (chooser_entry->current_folder_file)
760 discard_loading_and_current_folder_file (chooser_entry);
763 if (chooser_entry->local_only
764 && !g_file_is_native (folder_file))
765 return REFRESH_NOT_LOCAL;
767 chooser_entry->current_folder_file = g_object_ref (folder_file);
768 chooser_entry->current_folder_loaded = FALSE;
770 populate_completion_store (chooser_entry);
776 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
781 gsize total_len, file_part_len;
784 RefreshStatus result;
787 if (!gtk_file_chooser_entry_parse (chooser_entry,
788 text, &folder_file, &file_part, &error))
790 folder_file = g_object_ref (chooser_entry->base_folder);
792 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
793 result = REFRESH_NONEXISTENT;
795 result = REFRESH_INVALID_INPUT;
798 g_error_free (error);
800 file_part = g_strdup ("");
805 g_assert (folder_file != NULL);
807 file_part_len = strlen (file_part);
808 total_len = strlen (text);
809 if (total_len > file_part_len)
810 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
817 g_free (chooser_entry->file_part);
818 g_free (chooser_entry->dir_part);
820 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
821 chooser_entry->file_part = file_part;
822 chooser_entry->file_part_pos = file_part_pos;
824 if (result == REFRESH_OK)
826 result = reload_current_folder (chooser_entry, folder_file);
830 discard_loading_and_current_folder_file (chooser_entry);
834 g_object_unref (folder_file);
836 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
837 ((result == REFRESH_OK)
838 && (chooser_entry->current_folder_file != NULL))
839 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
840 || ((result != REFRESH_OK)
841 && (chooser_entry->current_folder_file == NULL)));
848 insert_text_callback (GtkFileChooserEntry *chooser_entry,
849 const gchar *new_text,
850 gint new_text_length,
854 const gchar *colon = memchr (new_text, ':', new_text_length);
857 /* Disallow these characters altogether */
858 for (i = 0; i < new_text_length; i++)
860 if (new_text[i] == '<' ||
861 new_text[i] == '>' ||
862 new_text[i] == '"' ||
863 new_text[i] == '|' ||
864 new_text[i] == '*' ||
869 if (i < new_text_length ||
870 /* Disallow entering text that would cause a colon to be anywhere except
871 * after a drive letter.
874 *position + (colon - new_text) != 1) ||
875 (new_text_length > 0 &&
877 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
878 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
880 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
881 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
889 delete_text_callback (GtkFileChooserEntry *chooser_entry,
894 /* If deleting a drive letter, delete the colon, too */
895 if (start_pos == 0 && end_pos == 1 &&
896 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
897 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
899 g_signal_handlers_block_by_func (chooser_entry,
900 G_CALLBACK (delete_text_callback),
902 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
903 g_signal_handlers_unblock_by_func (chooser_entry,
904 G_CALLBACK (delete_text_callback),
911 * _gtk_file_chooser_entry_new:
912 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
914 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
915 * is an internal implementation widget for the GTK+ file chooser
916 * which is an entry with completion with respect to a
917 * #GtkFileSystem object.
919 * Return value: the newly created #GtkFileChooserEntry
922 _gtk_file_chooser_entry_new (gboolean eat_tabs)
924 GtkFileChooserEntry *chooser_entry;
926 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
927 chooser_entry->eat_tabs = (eat_tabs != FALSE);
929 return GTK_WIDGET (chooser_entry);
933 * _gtk_file_chooser_entry_set_base_folder:
934 * @chooser_entry: a #GtkFileChooserEntry
935 * @file: file for a folder in the chooser entries current file system.
937 * Sets the folder with respect to which completions occur.
940 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
946 file = g_file_new_for_path (g_get_home_dir ());
948 if (g_file_equal (chooser_entry->base_folder, file))
950 g_object_unref (file);
954 if (chooser_entry->base_folder)
955 g_object_unref (chooser_entry->base_folder);
957 chooser_entry->base_folder = file;
959 clear_completions (chooser_entry);
963 * _gtk_file_chooser_entry_get_current_folder:
964 * @chooser_entry: a #GtkFileChooserEntry
966 * Gets the current folder for the #GtkFileChooserEntry. If the
967 * user has only entered a filename, this will be in the base folder
968 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
969 * user has entered a relative or absolute path, then it will
970 * be different. If the user has entered unparsable text, or text which
971 * the entry cannot handle, this will return %NULL.
973 * Return value: the file for the current folder - you must g_object_unref()
974 * the value after use.
977 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
979 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
981 return gtk_file_chooser_get_directory_for_text (chooser_entry,
982 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
986 * _gtk_file_chooser_entry_get_file_part:
987 * @chooser_entry: a #GtkFileChooserEntry
989 * Gets the non-folder portion of whatever the user has entered
990 * into the file selector. What is returned is a UTF-8 string,
991 * and if a filename path is needed, g_file_get_child_for_display_name()
994 * Return value: the entered filename - this value is owned by the
995 * chooser entry and must not be modified or freed.
998 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1000 const char *last_slash, *text;
1002 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1004 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1005 last_slash = strrchr (text, G_DIR_SEPARATOR);
1007 return last_slash + 1;
1013 * _gtk_file_chooser_entry_set_file_part:
1014 * @chooser_entry: a #GtkFileChooserEntry
1015 * @file_part: text to display in the entry, in UTF-8
1017 * Sets the current text shown in the file chooser entry.
1020 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1021 const gchar *file_part)
1023 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1025 chooser_entry->in_change = TRUE;
1026 clear_completions (chooser_entry);
1027 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1028 chooser_entry->in_change = FALSE;
1033 * _gtk_file_chooser_entry_set_action:
1034 * @chooser_entry: a #GtkFileChooserEntry
1035 * @action: the action which is performed by the file selector using this entry
1037 * Sets action which is performed by the file selector using this entry.
1038 * The #GtkFileChooserEntry will use different completion strategies for
1039 * different actions.
1042 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1043 GtkFileChooserAction action)
1045 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1047 if (chooser_entry->action != action)
1049 GtkEntryCompletion *comp;
1051 chooser_entry->action = action;
1053 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1055 /* FIXME: do we need to actually set the following? */
1059 case GTK_FILE_CHOOSER_ACTION_OPEN:
1060 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1061 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1063 case GTK_FILE_CHOOSER_ACTION_SAVE:
1064 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1065 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1073 * _gtk_file_chooser_entry_get_action:
1074 * @chooser_entry: a #GtkFileChooserEntry
1076 * Gets the action for this entry.
1078 * Returns: the action
1080 GtkFileChooserAction
1081 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1083 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1084 GTK_FILE_CHOOSER_ACTION_OPEN);
1086 return chooser_entry->action;
1090 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1096 if (chooser_entry->completion_store == NULL ||
1097 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1102 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1105 return _gtk_file_info_consider_as_directory (info);
1110 * _gtk_file_chooser_entry_select_filename:
1111 * @chooser_entry: a #GtkFileChooserEntry
1113 * Selects the filename (without the extension) for user edition.
1116 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1118 const gchar *str, *ext;
1121 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1123 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1124 ext = g_strrstr (str, ".");
1127 len = g_utf8_pointer_to_offset (str, ext);
1130 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1134 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1135 gboolean local_only)
1137 chooser_entry->local_only = local_only;
1138 clear_completions (chooser_entry);
1142 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1144 return chooser_entry->local_only;