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_AUTOCOMPLETE,
52 LOAD_COMPLETE_EXPLICIT_COMPLETION
58 REFRESH_INVALID_INPUT,
59 REFRESH_INCOMPLETE_HOSTNAME,
64 struct _GtkFileChooserEntry
66 GtkEntry parent_instance;
68 GtkFileChooserAction action;
71 GFile *current_folder_file;
76 LoadCompleteAction load_complete_action;
78 GtkTreeModel *completion_store;
80 GtkWidget *completion_feedback_window;
81 GtkWidget *completion_feedback_label;
82 guint completion_feedback_timeout_id;
84 guint current_folder_loaded : 1;
85 guint has_completion : 1;
99 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
101 static void gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface);
103 static void gtk_file_chooser_entry_finalize (GObject *object);
104 static void gtk_file_chooser_entry_dispose (GObject *object);
105 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
106 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
107 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
109 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
110 GdkEventFocus *event);
111 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
112 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
113 const gchar *new_text,
114 gint new_text_length,
116 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
119 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
121 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
126 static gint insert_text_callback (GtkFileChooserEntry *widget,
127 const gchar *new_text,
128 gint new_text_length,
131 static void delete_text_callback (GtkFileChooserEntry *widget,
137 static gboolean match_selected_callback (GtkEntryCompletion *completion,
140 GtkFileChooserEntry *chooser_entry);
141 static gboolean completion_match_func (GtkEntryCompletion *comp,
147 REFRESH_UP_TO_CURSOR_POSITION,
151 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
152 RefreshMode refresh_mode);
153 static void finished_loading_cb (GtkFileSystemModel *model,
155 GtkFileChooserEntry *chooser_entry);
156 static void autocomplete (GtkFileChooserEntry *chooser_entry);
157 static void start_autocompletion (GtkFileChooserEntry *chooser_entry);
158 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
159 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
160 const gchar *feedback);
162 static GtkEditableInterface *parent_editable_iface;
164 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
165 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
166 gtk_file_chooser_entry_iface_init))
169 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
171 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
172 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
173 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
175 gobject_class->finalize = gtk_file_chooser_entry_finalize;
176 gobject_class->dispose = gtk_file_chooser_entry_dispose;
178 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
179 widget_class->unmap = gtk_file_chooser_entry_unmap;
180 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
181 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
183 entry_class->activate = gtk_file_chooser_entry_activate;
187 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
189 parent_editable_iface = g_type_interface_peek_parent (iface);
191 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
192 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
193 iface->set_position = gtk_file_chooser_entry_set_position;
194 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
198 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
200 GtkEntryCompletion *comp;
201 GtkCellRenderer *cell;
203 chooser_entry->local_only = TRUE;
204 chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
206 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
208 comp = gtk_entry_completion_new ();
209 gtk_entry_completion_set_popup_single_match (comp, FALSE);
210 /* see docs for gtk_entry_completion_set_text_column() */
211 g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
213 gtk_entry_completion_set_match_func (comp,
214 completion_match_func,
218 cell = gtk_cell_renderer_text_new ();
219 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
221 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
223 "text", DISPLAY_NAME_COLUMN);
225 g_signal_connect (comp, "match-selected",
226 G_CALLBACK (match_selected_callback), chooser_entry);
228 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
229 g_object_unref (comp);
232 g_signal_connect (chooser_entry, "insert-text",
233 G_CALLBACK (insert_text_callback), NULL);
234 g_signal_connect (chooser_entry, "delete-text",
235 G_CALLBACK (delete_text_callback), NULL);
240 gtk_file_chooser_entry_finalize (GObject *object)
242 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
244 g_object_unref (chooser_entry->base_folder);
246 if (chooser_entry->current_folder_file)
247 g_object_unref (chooser_entry->current_folder_file);
249 g_free (chooser_entry->dir_part);
250 g_free (chooser_entry->file_part);
252 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
256 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
258 if (chooser_entry->current_folder_file)
260 g_object_unref (chooser_entry->current_folder_file);
261 chooser_entry->current_folder_file = NULL;
266 gtk_file_chooser_entry_dispose (GObject *object)
268 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
270 remove_completion_feedback (chooser_entry);
271 discard_loading_and_current_folder_file (chooser_entry);
273 if (chooser_entry->completion_store)
275 g_object_unref (chooser_entry->completion_store);
276 chooser_entry->completion_store = NULL;
279 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
282 /* Match functions for the GtkEntryCompletion */
284 match_selected_callback (GtkEntryCompletion *completion,
287 GtkFileChooserEntry *chooser_entry)
291 gtk_tree_model_get (model, iter,
292 FULL_PATH_COLUMN, &path,
295 /* We don't set in_change here as we want to update the current_folder
297 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
299 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
300 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
310 /* Match function for the GtkEntryCompletion */
312 completion_match_func (GtkEntryCompletion *comp,
313 const char *key_unused,
317 GtkFileChooserEntry *chooser_entry;
320 char *norm_file_part;
323 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
325 /* We ignore the key because it is the contents of the entry. Instead, we
326 * just use our precomputed file_part.
328 if (!chooser_entry->file_part)
333 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
336 return FALSE; /* Uninitialized row, ugh */
339 /* If we have an empty file_part, then we're at the root of a directory. In
340 * that case, we want to match all non-dot files. We might want to match
341 * dot_files too if show_hidden is TRUE on the fileselector in the future.
343 /* Additionally, support for gnome .hidden files would be sweet, too */
344 if (chooser_entry->file_part[0] == '\000')
356 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
357 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
359 #ifdef G_PLATFORM_WIN32
363 temp = norm_file_part;
364 norm_file_part = g_utf8_casefold (norm_file_part, -1);
368 norm_name = g_utf8_casefold (norm_name, -1);
373 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
375 g_free (norm_file_part);
383 clear_completions (GtkFileChooserEntry *chooser_entry)
385 chooser_entry->has_completion = FALSE;
386 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
388 remove_completion_feedback (chooser_entry);
392 beep (GtkFileChooserEntry *chooser_entry)
394 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
398 is_valid_scheme_character (char c)
400 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
404 has_uri_scheme (const char *str)
410 if (!is_valid_scheme_character (*p))
415 while (is_valid_scheme_character (*p));
417 return (strncmp (p, "://", 3) == 0);
421 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
426 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
427 file = g_file_parse_name (str);
429 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
435 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
438 GFile *file, *parent;
440 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
442 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
445 parent = g_file_get_parent (file);
446 g_object_unref (file);
452 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
459 gboolean result = FALSE;
460 gboolean is_dir = FALSE;
461 gchar *last_slash = NULL;
464 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
466 last_slash = strrchr (str, G_DIR_SEPARATOR);
468 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
470 if (g_file_equal (chooser_entry->base_folder, file))
472 /* this is when user types '.', could be the
473 * beginning of a hidden file, ./ or ../
475 *folder = g_object_ref (file);
476 *file_part = g_strdup (str);
481 /* it's a dir, or at least it ends with the dir separator */
482 *folder = g_object_ref (file);
483 *file_part = g_strdup ("");
490 parent_file = g_file_get_parent (file);
495 GTK_FILE_CHOOSER_ERROR,
496 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
497 "Could not get parent file");
503 *folder = parent_file;
507 *file_part = g_strdup (last_slash + 1);
509 *file_part = g_strdup (str);
513 g_object_unref (file);
518 /* Determines if the completion model has entries with a common prefix relative
519 * to the current contents of the entry. Also, if there's one and only one such
520 * path, stores it in unique_path_ret.
523 find_common_prefix (GtkFileChooserEntry *chooser_entry,
524 gchar **common_prefix_ret,
525 GFile **unique_file_ret,
526 gboolean *is_complete_not_unique_ret,
527 gboolean *prefix_expands_the_file_part_ret,
530 GtkEditable *editable;
534 char *text_up_to_cursor;
535 GFile *parsed_folder_file;
536 char *parsed_file_part;
538 *common_prefix_ret = NULL;
539 *unique_file_ret = NULL;
540 *is_complete_not_unique_ret = FALSE;
541 *prefix_expands_the_file_part_ret = FALSE;
543 editable = GTK_EDITABLE (chooser_entry);
545 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
547 parsed = gtk_file_chooser_entry_parse (chooser_entry,
553 g_free (text_up_to_cursor);
558 g_assert (parsed_folder_file != NULL
559 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
561 g_object_unref (parsed_folder_file);
563 /* First pass: find the common prefix */
565 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
572 gtk_tree_model_get (chooser_entry->completion_store,
574 DISPLAY_NAME_COLUMN, &display_name,
578 if (g_str_has_prefix (display_name, parsed_file_part))
580 if (!*common_prefix_ret)
582 *common_prefix_ret = g_strdup (display_name);
583 *unique_file_ret = g_object_ref (file);
587 gchar *p = *common_prefix_ret;
588 const gchar *q = display_name;
590 while (*p && *p == *q)
598 if (*unique_file_ret)
600 g_object_unref (*unique_file_ret);
601 *unique_file_ret = NULL;
606 g_free (display_name);
607 g_object_unref (file);
608 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
611 /* Second pass: see if the prefix we found is a complete match */
613 if (*common_prefix_ret != NULL)
615 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
622 gtk_tree_model_get (chooser_entry->completion_store,
624 DISPLAY_NAME_COLUMN, &display_name,
626 len = strlen (display_name);
629 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
632 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
633 *is_complete_not_unique_ret = TRUE;
635 g_free (display_name);
636 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
639 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
641 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
644 g_free (parsed_file_part);
650 INVALID_INPUT, /* what the user typed is bogus */
651 NO_MATCH, /* no matches based on what the user typed */
652 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
653 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
654 COMPLETED, /* completion inserted (ambiguous suffix) */
655 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
656 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
657 } CommonPrefixResult;
659 /* Finds a common prefix based on the contents of the entry
660 * and mandatorily appends it
662 static CommonPrefixResult
663 append_common_prefix (GtkFileChooserEntry *chooser_entry,
665 gboolean show_errors)
667 gchar *common_prefix;
669 gboolean is_complete_not_unique;
670 gboolean prefix_expands_the_file_part;
672 CommonPrefixResult result = NO_MATCH;
673 gboolean have_result;
675 clear_completions (chooser_entry);
677 if (chooser_entry->completion_store == NULL)
681 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
683 g_error_free (error);
685 return INVALID_INPUT;
692 g_object_unref (unique_file);
694 if (prefix_expands_the_file_part)
695 result = COMPLETED_UNIQUE;
697 result = NOTHING_INSERTED_UNIQUE;
703 if (is_complete_not_unique)
705 result = COMPLETE_BUT_NOT_UNIQUE;
715 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
717 pos = chooser_entry->file_part_pos;
719 if (prefix_expands_the_file_part)
721 chooser_entry->in_change = TRUE;
722 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
724 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
727 chooser_entry->in_change = FALSE;
731 /* equivalent to cursor_pos + common_prefix_len); */
732 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
735 chooser_entry->has_completion = TRUE;
738 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
740 else if (!have_result)
742 result = NOTHING_INSERTED_COMPLETE;
746 g_free (common_prefix);
763 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
764 const gchar *new_text,
765 gint new_text_length,
768 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
772 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
773 insert_pos = *position;
775 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
777 if (chooser_entry->in_change)
780 remove_completion_feedback (chooser_entry);
782 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
783 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
784 && insert_pos == old_text_len)
785 start_autocompletion (chooser_entry);
789 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
791 if (chooser_entry->in_change)
794 clear_completions (chooser_entry);
798 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
802 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
804 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
806 clear_completions_if_not_in_change (chooser_entry);
810 gtk_file_chooser_entry_set_position (GtkEditable *editable,
813 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
815 parent_editable_iface->set_position (editable, position);
817 clear_completions_if_not_in_change (chooser_entry);
821 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
825 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
827 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
829 clear_completions_if_not_in_change (chooser_entry);
833 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
835 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
836 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
840 gtk_file_chooser_entry_unmap (GtkWidget *widget)
842 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
844 remove_completion_feedback (chooser_entry);
846 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
850 completion_feedback_window_draw_cb (GtkWidget *widget,
854 /* Stolen from gtk_tooltip_paint_window() */
856 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
857 GtkStyleContext *context;
859 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
861 gtk_render_background (context, cr, 0, 0,
862 gtk_widget_get_allocated_width (widget),
863 gtk_widget_get_allocated_height (widget));
864 gtk_render_frame (context, cr, 0, 0,
865 gtk_widget_get_allocated_width (widget),
866 gtk_widget_get_allocated_height (widget));
872 set_invisible_mouse_cursor (GdkWindow *window)
877 display = gdk_window_get_display (window);
878 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
880 gdk_window_set_cursor (window, cursor);
882 g_object_unref (cursor);
886 completion_feedback_window_realize_cb (GtkWidget *widget,
889 /* We hide the mouse cursor inside the completion feedback window, since
890 * GtkEntry hides the cursor when the user types. We don't want the cursor to
891 * come back if the completion feedback ends up where the mouse is.
893 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
897 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
899 /* Stolen from gtk_tooltip_init() */
900 GtkWidget *window, *label;
901 GtkStyleContext *context;
903 window = gtk_window_new (GTK_WINDOW_POPUP);
904 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
905 gtk_widget_set_app_paintable (window, TRUE);
906 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
907 gtk_widget_set_name (window, "gtk-tooltip");
909 context = gtk_widget_get_style_context (window);
910 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
912 g_signal_connect (window, "draw",
913 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
914 g_signal_connect (window, "realize",
915 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
916 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
918 label = gtk_label_new (NULL);
919 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
920 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
921 /* FIXME: don't hardcode this */
922 gtk_widget_set_margin_left (label, 6);
923 gtk_widget_set_margin_right (label, 6);
924 gtk_widget_set_margin_top (label, 6);
925 gtk_widget_set_margin_bottom (label, 6);
926 gtk_container_add (GTK_CONTAINER (window), label);
927 gtk_widget_show (label);
929 chooser_entry->completion_feedback_window = window;
930 chooser_entry->completion_feedback_label = label;
934 completion_feedback_timeout_cb (gpointer data)
936 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
938 chooser_entry->completion_feedback_timeout_id = 0;
940 remove_completion_feedback (chooser_entry);
945 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
947 if (chooser_entry->completion_feedback_timeout_id != 0)
948 g_source_remove (chooser_entry->completion_feedback_timeout_id);
950 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
951 completion_feedback_timeout_cb,
955 /* Gets the x position of the text cursor in the entry, in widget coordinates */
957 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
960 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
961 * the reader, you have to implement support for the entry's scroll offset and
962 * RTL layouts and all the fancy Pango stuff.
966 gint layout_x, layout_y;
968 PangoRectangle strong_pos;
969 gint start_pos, end_pos;
971 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
973 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
975 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
976 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
980 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
982 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
986 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
988 /* More or less stolen from gtk_tooltip_position() */
990 GtkRequisition feedback_req;
991 GtkWidget *widget = GTK_WIDGET (chooser_entry);
992 gint entry_x, entry_y;
994 GtkAllocation entry_allocation;
995 int feedback_x, feedback_y;
997 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
998 &feedback_req, NULL);
1000 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1001 gtk_widget_get_allocation (widget, &entry_allocation);
1003 get_entry_cursor_x (chooser_entry, &cursor_x);
1005 /* FIXME: fit to the screen if we bump on the screen's edge */
1006 /* cheap "half M-width", use height as approximation of character em-size */
1007 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1008 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1010 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1011 gtk_widget_show (chooser_entry->completion_feedback_window);
1013 install_completion_feedback_timer (chooser_entry);
1017 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1018 const gchar *feedback)
1020 if (chooser_entry->completion_feedback_window == NULL)
1021 create_completion_feedback_window (chooser_entry);
1023 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1025 show_completion_feedback_window (chooser_entry);
1029 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1031 if (chooser_entry->completion_feedback_window)
1032 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1034 chooser_entry->completion_feedback_window = NULL;
1035 chooser_entry->completion_feedback_label = NULL;
1037 if (chooser_entry->completion_feedback_timeout_id != 0)
1039 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1040 chooser_entry->completion_feedback_timeout_id = 0;
1045 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1047 CommonPrefixResult result;
1049 g_assert (chooser_entry->current_folder_loaded);
1051 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1053 * - If there is a common prefix, insert it (done)
1054 * - If there is no common prefix, pop up the suggestion window
1055 * - If there are no matches at all, beep and bring up a tooltip (done)
1056 * - If the suggestion window is already up, scroll it
1058 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1063 /* We already beeped in append_common_prefix(); do nothing here */
1067 beep (chooser_entry);
1068 /* translators: this text is shown when there are no completions
1069 * for something the user typed in a file chooser entry
1071 pop_up_completion_feedback (chooser_entry, _("No match"));
1074 case NOTHING_INSERTED_COMPLETE:
1075 /* FIXME: pop up the suggestion window or scroll it */
1078 case NOTHING_INSERTED_UNIQUE:
1079 /* translators: this text is shown when there is exactly one completion
1080 * for something the user typed in a file chooser entry
1082 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1089 case COMPLETED_UNIQUE:
1093 case COMPLETE_BUT_NOT_UNIQUE:
1094 /* translators: this text is shown when the text in a file chooser
1095 * entry is a complete filename, but could be continued to find
1098 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1102 g_assert_not_reached ();
1107 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1109 RefreshStatus status;
1113 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1120 g_assert (chooser_entry->current_folder_file != NULL);
1122 if (chooser_entry->current_folder_loaded)
1123 explicitly_complete (chooser_entry);
1126 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1128 /* Translators: this text is shown while the system is searching
1129 * for possible completions for filenames in a file chooser entry. */
1130 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1135 case REFRESH_INVALID_INPUT:
1137 /* Translators: this is shown in the feedback for Tab-completion in a file
1138 * chooser's text entry, when the user enters an invalid path. */
1139 feedback_msg = _("Invalid path");
1142 case REFRESH_INCOMPLETE_HOSTNAME:
1145 if (chooser_entry->local_only)
1147 /* hostnames in a local_only file chooser? user error */
1149 /* Translators: this is shown in the feedback for Tab-completion in a
1150 * file chooser's text entry when the user enters something like
1151 * "sftp://blahblah" in an app that only supports local filenames. */
1152 feedback_msg = _("Only local files may be selected");
1156 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1158 /* Translators: this is shown in the feedback for Tab-completion in a
1159 * file chooser's text entry when the user hasn't entered the first '/'
1160 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1161 feedback_msg = _("Incomplete hostname; end it with '/'");
1166 case REFRESH_NONEXISTENT:
1169 /* Translators: this is shown in the feedback for Tab-completion in a file
1170 * chooser's text entry when the user enters a path that does not exist
1171 * and then hits Tab */
1172 feedback_msg = _("Path does not exist");
1175 case REFRESH_NOT_LOCAL:
1177 feedback_msg = _("Only local files may be selected");
1181 g_assert_not_reached ();
1187 g_assert (chooser_entry->current_folder_file == NULL);
1189 beep (chooser_entry);
1190 pop_up_completion_feedback (chooser_entry, feedback_msg);
1191 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1196 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1199 GtkFileChooserEntry *chooser_entry;
1200 GtkEditable *editable;
1202 GdkModifierType state;
1203 gboolean control_pressed;
1205 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1206 editable = GTK_EDITABLE (widget);
1207 entry = GTK_ENTRY (widget);
1209 if (!chooser_entry->eat_tabs)
1210 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1212 control_pressed = FALSE;
1214 if (gtk_get_current_event_state (&state))
1216 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1217 control_pressed = TRUE;
1220 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1221 * makes it 'safe' for people to hit. */
1222 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1224 if (chooser_entry->has_completion)
1225 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1227 start_explicit_completion (chooser_entry);
1232 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1237 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1238 GdkEventFocus *event)
1240 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1242 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1244 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1248 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1250 if (chooser_entry->has_completion)
1252 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1253 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1256 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1257 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1261 gtk_file_chooser_entry_activate (GtkEntry *entry)
1263 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1265 commit_completion_and_refresh (chooser_entry);
1266 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1270 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1272 if (!chooser_entry->completion_store)
1275 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1276 g_object_unref (chooser_entry->completion_store);
1277 chooser_entry->completion_store = NULL;
1281 completion_store_set (GtkFileSystemModel *model,
1288 GtkFileChooserEntry *chooser_entry = data;
1290 const char *prefix = "";
1291 const char *suffix = "";
1296 g_value_set_object (value, file);
1298 case FULL_PATH_COLUMN:
1299 prefix = chooser_entry->dir_part;
1301 case DISPLAY_NAME_COLUMN:
1302 if (_gtk_file_info_consider_as_directory (info))
1303 suffix = G_DIR_SEPARATOR_S;
1305 g_value_take_string (value, g_strconcat (
1307 g_file_info_get_display_name (info),
1312 g_assert_not_reached ();
1319 /* Fills the completion store from the contents of the current folder */
1321 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1323 discard_completion_store (chooser_entry);
1325 chooser_entry->completion_store = GTK_TREE_MODEL (
1326 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
1327 "standard::name,standard::display-name,standard::type",
1328 completion_store_set,
1334 g_signal_connect (chooser_entry->completion_store, "finished-loading",
1335 G_CALLBACK (finished_loading_cb), chooser_entry);
1337 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1338 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1340 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1341 chooser_entry->completion_store);
1344 /* When we finish loading the current folder, this function should get called to
1345 * perform the deferred autocompletion or explicit completion.
1348 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1350 switch (chooser_entry->load_complete_action)
1352 case LOAD_COMPLETE_NOTHING:
1355 case LOAD_COMPLETE_AUTOCOMPLETE:
1356 autocomplete (chooser_entry);
1359 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1360 explicitly_complete (chooser_entry);
1364 g_assert_not_reached ();
1367 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1370 /* Callback when the current folder finishes loading */
1372 finished_loading_cb (GtkFileSystemModel *model,
1374 GtkFileChooserEntry *chooser_entry)
1376 chooser_entry->current_folder_loaded = TRUE;
1380 LoadCompleteAction old_load_complete_action;
1382 old_load_complete_action = chooser_entry->load_complete_action;
1384 discard_completion_store (chooser_entry);
1385 clear_completions (chooser_entry);
1387 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1389 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1391 beep (chooser_entry);
1392 pop_up_completion_feedback (chooser_entry, error->message);
1398 perform_load_complete_action (chooser_entry);
1400 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1403 static RefreshStatus
1404 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1407 g_assert (folder_file != NULL);
1409 if (chooser_entry->current_folder_file
1410 && g_file_equal (folder_file, chooser_entry->current_folder_file))
1413 if (chooser_entry->current_folder_file)
1415 discard_loading_and_current_folder_file (chooser_entry);
1418 if (chooser_entry->local_only
1419 && !g_file_is_native (folder_file))
1420 return REFRESH_NOT_LOCAL;
1422 chooser_entry->current_folder_file = g_object_ref (folder_file);
1423 chooser_entry->current_folder_loaded = FALSE;
1425 populate_completion_store (chooser_entry);
1430 static RefreshStatus
1431 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1432 RefreshMode refresh_mode)
1434 GtkEditable *editable;
1439 gsize total_len, file_part_len;
1442 RefreshStatus result;
1444 editable = GTK_EDITABLE (chooser_entry);
1446 switch (refresh_mode)
1448 case REFRESH_UP_TO_CURSOR_POSITION:
1449 end_pos = gtk_editable_get_position (editable);
1452 case REFRESH_WHOLE_TEXT:
1453 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1457 g_assert_not_reached ();
1458 return REFRESH_INVALID_INPUT;
1461 text = gtk_editable_get_chars (editable, 0, end_pos);
1464 if (!gtk_file_chooser_entry_parse (chooser_entry,
1465 text, &folder_file, &file_part, &error))
1467 folder_file = g_object_ref (chooser_entry->base_folder);
1469 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1470 result = REFRESH_NONEXISTENT;
1472 result = REFRESH_INVALID_INPUT;
1475 g_error_free (error);
1477 file_part = g_strdup ("");
1482 g_assert (folder_file != NULL);
1484 file_part_len = strlen (file_part);
1485 total_len = strlen (text);
1486 if (total_len > file_part_len)
1487 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1491 result = REFRESH_OK;
1494 g_free (chooser_entry->file_part);
1495 g_free (chooser_entry->dir_part);
1497 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
1498 chooser_entry->file_part = file_part;
1499 chooser_entry->file_part_pos = file_part_pos;
1503 if (result == REFRESH_OK)
1505 result = reload_current_folder (chooser_entry, folder_file);
1509 discard_loading_and_current_folder_file (chooser_entry);
1513 g_object_unref (folder_file);
1515 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1516 ((result == REFRESH_OK)
1517 && (chooser_entry->current_folder_file != NULL))
1518 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1519 || ((result != REFRESH_OK)
1520 && (chooser_entry->current_folder_file == NULL)));
1526 autocomplete (GtkFileChooserEntry *chooser_entry)
1528 if (!(chooser_entry->current_folder_loaded
1529 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1532 append_common_prefix (chooser_entry, TRUE, FALSE);
1536 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1538 RefreshStatus status;
1540 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1545 g_assert (chooser_entry->current_folder_file != NULL);
1547 if (chooser_entry->current_folder_loaded)
1548 autocomplete (chooser_entry);
1550 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1554 case REFRESH_INVALID_INPUT:
1555 case REFRESH_INCOMPLETE_HOSTNAME:
1556 case REFRESH_NONEXISTENT:
1557 case REFRESH_NOT_LOCAL:
1558 /* We don't beep or anything, since this is autocompletion - the user
1559 * didn't request any action explicitly.
1564 g_assert_not_reached ();
1570 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1571 const gchar *new_text,
1572 gint new_text_length,
1576 const gchar *colon = memchr (new_text, ':', new_text_length);
1579 /* Disallow these characters altogether */
1580 for (i = 0; i < new_text_length; i++)
1582 if (new_text[i] == '<' ||
1583 new_text[i] == '>' ||
1584 new_text[i] == '"' ||
1585 new_text[i] == '|' ||
1586 new_text[i] == '*' ||
1591 if (i < new_text_length ||
1592 /* Disallow entering text that would cause a colon to be anywhere except
1593 * after a drive letter.
1596 *position + (colon - new_text) != 1) ||
1597 (new_text_length > 0 &&
1599 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1600 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1602 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1603 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1611 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1616 /* If deleting a drive letter, delete the colon, too */
1617 if (start_pos == 0 && end_pos == 1 &&
1618 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1619 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1621 g_signal_handlers_block_by_func (chooser_entry,
1622 G_CALLBACK (delete_text_callback),
1624 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1625 g_signal_handlers_unblock_by_func (chooser_entry,
1626 G_CALLBACK (delete_text_callback),
1633 * _gtk_file_chooser_entry_new:
1634 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1636 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1637 * is an internal implementation widget for the GTK+ file chooser
1638 * which is an entry with completion with respect to a
1639 * #GtkFileSystem object.
1641 * Return value: the newly created #GtkFileChooserEntry
1644 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1646 GtkFileChooserEntry *chooser_entry;
1648 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1649 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1651 return GTK_WIDGET (chooser_entry);
1655 * _gtk_file_chooser_entry_set_base_folder:
1656 * @chooser_entry: a #GtkFileChooserEntry
1657 * @file: file for a folder in the chooser entries current file system.
1659 * Sets the folder with respect to which completions occur.
1662 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1666 g_object_ref (file);
1668 file = g_file_new_for_path (g_get_home_dir ());
1670 if (g_file_equal (chooser_entry->base_folder, file))
1672 g_object_unref (file);
1676 if (chooser_entry->base_folder)
1677 g_object_unref (chooser_entry->base_folder);
1679 chooser_entry->base_folder = file;
1681 clear_completions (chooser_entry);
1685 * _gtk_file_chooser_entry_get_current_folder:
1686 * @chooser_entry: a #GtkFileChooserEntry
1688 * Gets the current folder for the #GtkFileChooserEntry. If the
1689 * user has only entered a filename, this will be in the base folder
1690 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1691 * user has entered a relative or absolute path, then it will
1692 * be different. If the user has entered unparsable text, or text which
1693 * the entry cannot handle, this will return %NULL.
1695 * Return value: the file for the current folder - you must g_object_unref()
1696 * the value after use.
1699 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1701 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1703 return gtk_file_chooser_get_directory_for_text (chooser_entry,
1704 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
1708 * _gtk_file_chooser_entry_get_file_part:
1709 * @chooser_entry: a #GtkFileChooserEntry
1711 * Gets the non-folder portion of whatever the user has entered
1712 * into the file selector. What is returned is a UTF-8 string,
1713 * and if a filename path is needed, g_file_get_child_for_display_name()
1716 * Return value: the entered filename - this value is owned by the
1717 * chooser entry and must not be modified or freed.
1720 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1722 const char *last_slash, *text;
1724 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1726 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1727 last_slash = strrchr (text, G_DIR_SEPARATOR);
1729 return last_slash + 1;
1735 * _gtk_file_chooser_entry_set_file_part:
1736 * @chooser_entry: a #GtkFileChooserEntry
1737 * @file_part: text to display in the entry, in UTF-8
1739 * Sets the current text shown in the file chooser entry.
1742 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1743 const gchar *file_part)
1745 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1747 chooser_entry->in_change = TRUE;
1748 clear_completions (chooser_entry);
1749 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1750 chooser_entry->in_change = FALSE;
1755 * _gtk_file_chooser_entry_set_action:
1756 * @chooser_entry: a #GtkFileChooserEntry
1757 * @action: the action which is performed by the file selector using this entry
1759 * Sets action which is performed by the file selector using this entry.
1760 * The #GtkFileChooserEntry will use different completion strategies for
1761 * different actions.
1764 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1765 GtkFileChooserAction action)
1767 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1769 if (chooser_entry->action != action)
1771 GtkEntryCompletion *comp;
1773 chooser_entry->action = action;
1775 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1777 /* FIXME: do we need to actually set the following? */
1781 case GTK_FILE_CHOOSER_ACTION_OPEN:
1782 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1783 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1785 case GTK_FILE_CHOOSER_ACTION_SAVE:
1786 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1787 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1795 * _gtk_file_chooser_entry_get_action:
1796 * @chooser_entry: a #GtkFileChooserEntry
1798 * Gets the action for this entry.
1800 * Returns: the action
1802 GtkFileChooserAction
1803 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1805 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1806 GTK_FILE_CHOOSER_ACTION_OPEN);
1808 return chooser_entry->action;
1812 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1818 if (chooser_entry->completion_store == NULL ||
1819 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1824 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1827 return _gtk_file_info_consider_as_directory (info);
1832 * _gtk_file_chooser_entry_select_filename:
1833 * @chooser_entry: a #GtkFileChooserEntry
1835 * Selects the filename (without the extension) for user edition.
1838 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1840 const gchar *str, *ext;
1843 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1845 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1846 ext = g_strrstr (str, ".");
1849 len = g_utf8_pointer_to_offset (str, ext);
1852 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1856 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1857 gboolean local_only)
1859 chooser_entry->local_only = local_only;
1860 clear_completions (chooser_entry);
1864 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1866 return chooser_entry->local_only;