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);
211 gtk_entry_completion_set_match_func (comp,
212 completion_match_func,
216 cell = gtk_cell_renderer_text_new ();
217 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
219 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
221 "text", DISPLAY_NAME_COLUMN);
223 g_signal_connect (comp, "match-selected",
224 G_CALLBACK (match_selected_callback), chooser_entry);
226 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
227 g_object_unref (comp);
230 g_signal_connect (chooser_entry, "insert-text",
231 G_CALLBACK (insert_text_callback), NULL);
232 g_signal_connect (chooser_entry, "delete-text",
233 G_CALLBACK (delete_text_callback), NULL);
238 gtk_file_chooser_entry_finalize (GObject *object)
240 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
242 g_object_unref (chooser_entry->base_folder);
244 if (chooser_entry->current_folder_file)
245 g_object_unref (chooser_entry->current_folder_file);
247 g_free (chooser_entry->dir_part);
248 g_free (chooser_entry->file_part);
250 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
254 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
256 if (chooser_entry->current_folder_file)
258 g_object_unref (chooser_entry->current_folder_file);
259 chooser_entry->current_folder_file = NULL;
264 gtk_file_chooser_entry_dispose (GObject *object)
266 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
268 remove_completion_feedback (chooser_entry);
269 discard_loading_and_current_folder_file (chooser_entry);
271 if (chooser_entry->completion_store)
273 g_object_unref (chooser_entry->completion_store);
274 chooser_entry->completion_store = NULL;
277 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
280 /* Match functions for the GtkEntryCompletion */
282 match_selected_callback (GtkEntryCompletion *completion,
285 GtkFileChooserEntry *chooser_entry)
289 gtk_tree_model_get (model, iter,
290 FULL_PATH_COLUMN, &path,
293 /* We don't set in_change here as we want to update the current_folder
295 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
297 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
298 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
308 /* Match function for the GtkEntryCompletion */
310 completion_match_func (GtkEntryCompletion *comp,
311 const char *key_unused,
315 GtkFileChooserEntry *chooser_entry;
318 char *norm_file_part;
321 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
323 /* We ignore the key because it is the contents of the entry. Instead, we
324 * just use our precomputed file_part.
326 if (!chooser_entry->file_part)
331 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
334 return FALSE; /* Uninitialized row, ugh */
337 /* If we have an empty file_part, then we're at the root of a directory. In
338 * that case, we want to match all non-dot files. We might want to match
339 * dot_files too if show_hidden is TRUE on the fileselector in the future.
341 /* Additionally, support for gnome .hidden files would be sweet, too */
342 if (chooser_entry->file_part[0] == '\000')
354 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
355 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
357 #ifdef G_PLATFORM_WIN32
361 temp = norm_file_part;
362 norm_file_part = g_utf8_casefold (norm_file_part, -1);
366 norm_name = g_utf8_casefold (norm_name, -1);
371 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
373 g_free (norm_file_part);
381 clear_completions (GtkFileChooserEntry *chooser_entry)
383 chooser_entry->has_completion = FALSE;
384 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
386 remove_completion_feedback (chooser_entry);
390 beep (GtkFileChooserEntry *chooser_entry)
392 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
396 is_valid_scheme_character (char c)
398 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
402 has_uri_scheme (const char *str)
408 if (!is_valid_scheme_character (*p))
413 while (is_valid_scheme_character (*p));
415 return (strncmp (p, "://", 3) == 0);
419 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
424 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
425 file = g_file_parse_name (str);
427 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
433 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
436 GFile *file, *parent;
438 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
440 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
443 parent = g_file_get_parent (file);
444 g_object_unref (file);
450 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
457 gboolean result = FALSE;
458 gboolean is_dir = FALSE;
459 gchar *last_slash = NULL;
462 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
464 last_slash = strrchr (str, G_DIR_SEPARATOR);
466 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
468 if (g_file_equal (chooser_entry->base_folder, file))
470 /* this is when user types '.', could be the
471 * beginning of a hidden file, ./ or ../
473 *folder = g_object_ref (file);
474 *file_part = g_strdup (str);
479 /* it's a dir, or at least it ends with the dir separator */
480 *folder = g_object_ref (file);
481 *file_part = g_strdup ("");
488 parent_file = g_file_get_parent (file);
493 GTK_FILE_CHOOSER_ERROR,
494 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
495 "Could not get parent file");
501 *folder = parent_file;
505 *file_part = g_strdup (last_slash + 1);
507 *file_part = g_strdup (str);
511 g_object_unref (file);
516 /* Determines if the completion model has entries with a common prefix relative
517 * to the current contents of the entry. Also, if there's one and only one such
518 * path, stores it in unique_path_ret.
521 find_common_prefix (GtkFileChooserEntry *chooser_entry,
522 gchar **common_prefix_ret,
523 GFile **unique_file_ret,
524 gboolean *is_complete_not_unique_ret,
525 gboolean *prefix_expands_the_file_part_ret,
528 GtkEditable *editable;
532 char *text_up_to_cursor;
533 GFile *parsed_folder_file;
534 char *parsed_file_part;
536 *common_prefix_ret = NULL;
537 *unique_file_ret = NULL;
538 *is_complete_not_unique_ret = FALSE;
539 *prefix_expands_the_file_part_ret = FALSE;
541 editable = GTK_EDITABLE (chooser_entry);
543 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
545 parsed = gtk_file_chooser_entry_parse (chooser_entry,
551 g_free (text_up_to_cursor);
556 g_assert (parsed_folder_file != NULL
557 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
559 g_object_unref (parsed_folder_file);
561 /* First pass: find the common prefix */
563 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
570 gtk_tree_model_get (chooser_entry->completion_store,
572 DISPLAY_NAME_COLUMN, &display_name,
576 if (g_str_has_prefix (display_name, parsed_file_part))
578 if (!*common_prefix_ret)
580 *common_prefix_ret = g_strdup (display_name);
581 *unique_file_ret = g_object_ref (file);
585 gchar *p = *common_prefix_ret;
586 const gchar *q = display_name;
588 while (*p && *p == *q)
596 if (*unique_file_ret)
598 g_object_unref (*unique_file_ret);
599 *unique_file_ret = NULL;
604 g_free (display_name);
605 g_object_unref (file);
606 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
609 /* Second pass: see if the prefix we found is a complete match */
611 if (*common_prefix_ret != NULL)
613 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
620 gtk_tree_model_get (chooser_entry->completion_store,
622 DISPLAY_NAME_COLUMN, &display_name,
624 len = strlen (display_name);
627 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
630 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
631 *is_complete_not_unique_ret = TRUE;
633 g_free (display_name);
634 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
637 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
639 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
642 g_free (parsed_file_part);
648 INVALID_INPUT, /* what the user typed is bogus */
649 NO_MATCH, /* no matches based on what the user typed */
650 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
651 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
652 COMPLETED, /* completion inserted (ambiguous suffix) */
653 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
654 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
655 } CommonPrefixResult;
657 /* Finds a common prefix based on the contents of the entry
658 * and mandatorily appends it
660 static CommonPrefixResult
661 append_common_prefix (GtkFileChooserEntry *chooser_entry,
663 gboolean show_errors)
665 gchar *common_prefix;
667 gboolean is_complete_not_unique;
668 gboolean prefix_expands_the_file_part;
670 CommonPrefixResult result = NO_MATCH;
671 gboolean have_result;
673 clear_completions (chooser_entry);
675 if (chooser_entry->completion_store == NULL)
679 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
681 g_error_free (error);
683 return INVALID_INPUT;
690 g_object_unref (unique_file);
692 if (prefix_expands_the_file_part)
693 result = COMPLETED_UNIQUE;
695 result = NOTHING_INSERTED_UNIQUE;
701 if (is_complete_not_unique)
703 result = COMPLETE_BUT_NOT_UNIQUE;
713 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
715 pos = chooser_entry->file_part_pos;
717 if (prefix_expands_the_file_part)
719 chooser_entry->in_change = TRUE;
720 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
722 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
725 chooser_entry->in_change = FALSE;
729 /* equivalent to cursor_pos + common_prefix_len); */
730 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
733 chooser_entry->has_completion = TRUE;
736 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
738 else if (!have_result)
740 result = NOTHING_INSERTED_COMPLETE;
744 g_free (common_prefix);
761 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
762 const gchar *new_text,
763 gint new_text_length,
766 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
770 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
771 insert_pos = *position;
773 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
775 if (chooser_entry->in_change)
778 remove_completion_feedback (chooser_entry);
780 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
781 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
782 && insert_pos == old_text_len)
783 start_autocompletion (chooser_entry);
787 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
789 if (chooser_entry->in_change)
792 clear_completions (chooser_entry);
796 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
800 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
802 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
804 clear_completions_if_not_in_change (chooser_entry);
808 gtk_file_chooser_entry_set_position (GtkEditable *editable,
811 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
813 parent_editable_iface->set_position (editable, position);
815 clear_completions_if_not_in_change (chooser_entry);
819 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
823 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
825 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
827 clear_completions_if_not_in_change (chooser_entry);
831 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
833 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
834 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
838 gtk_file_chooser_entry_unmap (GtkWidget *widget)
840 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
842 remove_completion_feedback (chooser_entry);
844 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
848 completion_feedback_window_draw_cb (GtkWidget *widget,
852 /* Stolen from gtk_tooltip_paint_window() */
854 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
855 GtkStyleContext *context;
857 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
859 gtk_render_background (context, cr, 0, 0,
860 gtk_widget_get_allocated_width (widget),
861 gtk_widget_get_allocated_height (widget));
862 gtk_render_frame (context, cr, 0, 0,
863 gtk_widget_get_allocated_width (widget),
864 gtk_widget_get_allocated_height (widget));
870 set_invisible_mouse_cursor (GdkWindow *window)
875 display = gdk_window_get_display (window);
876 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
878 gdk_window_set_cursor (window, cursor);
880 g_object_unref (cursor);
884 completion_feedback_window_realize_cb (GtkWidget *widget,
887 /* We hide the mouse cursor inside the completion feedback window, since
888 * GtkEntry hides the cursor when the user types. We don't want the cursor to
889 * come back if the completion feedback ends up where the mouse is.
891 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
895 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
897 /* Stolen from gtk_tooltip_init() */
898 GtkWidget *window, *label;
899 GtkStyleContext *context;
901 window = gtk_window_new (GTK_WINDOW_POPUP);
902 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
903 gtk_widget_set_app_paintable (window, TRUE);
904 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
905 gtk_widget_set_name (window, "gtk-tooltip");
907 context = gtk_widget_get_style_context (window);
908 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
910 g_signal_connect (window, "draw",
911 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
912 g_signal_connect (window, "realize",
913 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
914 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
916 label = gtk_label_new (NULL);
917 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
918 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
919 /* FIXME: don't hardcode this */
920 gtk_widget_set_margin_left (label, 6);
921 gtk_widget_set_margin_right (label, 6);
922 gtk_widget_set_margin_top (label, 6);
923 gtk_widget_set_margin_bottom (label, 6);
924 gtk_container_add (GTK_CONTAINER (window), label);
925 gtk_widget_show (label);
927 chooser_entry->completion_feedback_window = window;
928 chooser_entry->completion_feedback_label = label;
932 completion_feedback_timeout_cb (gpointer data)
934 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
936 chooser_entry->completion_feedback_timeout_id = 0;
938 remove_completion_feedback (chooser_entry);
943 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
945 if (chooser_entry->completion_feedback_timeout_id != 0)
946 g_source_remove (chooser_entry->completion_feedback_timeout_id);
948 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
949 completion_feedback_timeout_cb,
953 /* Gets the x position of the text cursor in the entry, in widget coordinates */
955 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
958 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
959 * the reader, you have to implement support for the entry's scroll offset and
960 * RTL layouts and all the fancy Pango stuff.
964 gint layout_x, layout_y;
966 PangoRectangle strong_pos;
967 gint start_pos, end_pos;
969 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
971 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
973 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
974 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
978 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
980 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
984 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
986 /* More or less stolen from gtk_tooltip_position() */
988 GtkRequisition feedback_req;
989 GtkWidget *widget = GTK_WIDGET (chooser_entry);
990 gint entry_x, entry_y;
992 GtkAllocation entry_allocation;
993 int feedback_x, feedback_y;
995 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
996 &feedback_req, NULL);
998 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
999 gtk_widget_get_allocation (widget, &entry_allocation);
1001 get_entry_cursor_x (chooser_entry, &cursor_x);
1003 /* FIXME: fit to the screen if we bump on the screen's edge */
1004 /* cheap "half M-width", use height as approximation of character em-size */
1005 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1006 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1008 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1009 gtk_widget_show (chooser_entry->completion_feedback_window);
1011 install_completion_feedback_timer (chooser_entry);
1015 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1016 const gchar *feedback)
1018 if (chooser_entry->completion_feedback_window == NULL)
1019 create_completion_feedback_window (chooser_entry);
1021 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1023 show_completion_feedback_window (chooser_entry);
1027 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1029 if (chooser_entry->completion_feedback_window)
1030 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1032 chooser_entry->completion_feedback_window = NULL;
1033 chooser_entry->completion_feedback_label = NULL;
1035 if (chooser_entry->completion_feedback_timeout_id != 0)
1037 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1038 chooser_entry->completion_feedback_timeout_id = 0;
1043 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1045 CommonPrefixResult result;
1047 g_assert (chooser_entry->current_folder_loaded);
1049 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1051 * - If there is a common prefix, insert it (done)
1052 * - If there is no common prefix, pop up the suggestion window
1053 * - If there are no matches at all, beep and bring up a tooltip (done)
1054 * - If the suggestion window is already up, scroll it
1056 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1061 /* We already beeped in append_common_prefix(); do nothing here */
1065 beep (chooser_entry);
1066 /* translators: this text is shown when there are no completions
1067 * for something the user typed in a file chooser entry
1069 pop_up_completion_feedback (chooser_entry, _("No match"));
1072 case NOTHING_INSERTED_COMPLETE:
1073 /* FIXME: pop up the suggestion window or scroll it */
1076 case NOTHING_INSERTED_UNIQUE:
1077 /* translators: this text is shown when there is exactly one completion
1078 * for something the user typed in a file chooser entry
1080 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1087 case COMPLETED_UNIQUE:
1091 case COMPLETE_BUT_NOT_UNIQUE:
1092 /* translators: this text is shown when the text in a file chooser
1093 * entry is a complete filename, but could be continued to find
1096 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1100 g_assert_not_reached ();
1105 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1107 RefreshStatus status;
1111 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1118 g_assert (chooser_entry->current_folder_file != NULL);
1120 if (chooser_entry->current_folder_loaded)
1121 explicitly_complete (chooser_entry);
1124 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1126 /* Translators: this text is shown while the system is searching
1127 * for possible completions for filenames in a file chooser entry. */
1128 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1133 case REFRESH_INVALID_INPUT:
1135 /* Translators: this is shown in the feedback for Tab-completion in a file
1136 * chooser's text entry, when the user enters an invalid path. */
1137 feedback_msg = _("Invalid path");
1140 case REFRESH_INCOMPLETE_HOSTNAME:
1143 if (chooser_entry->local_only)
1145 /* hostnames in a local_only file chooser? user error */
1147 /* Translators: this is shown in the feedback for Tab-completion in a
1148 * file chooser's text entry when the user enters something like
1149 * "sftp://blahblah" in an app that only supports local filenames. */
1150 feedback_msg = _("Only local files may be selected");
1154 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1156 /* Translators: this is shown in the feedback for Tab-completion in a
1157 * file chooser's text entry when the user hasn't entered the first '/'
1158 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1159 feedback_msg = _("Incomplete hostname; end it with '/'");
1164 case REFRESH_NONEXISTENT:
1167 /* Translators: this is shown in the feedback for Tab-completion in a file
1168 * chooser's text entry when the user enters a path that does not exist
1169 * and then hits Tab */
1170 feedback_msg = _("Path does not exist");
1173 case REFRESH_NOT_LOCAL:
1175 feedback_msg = _("Only local files may be selected");
1179 g_assert_not_reached ();
1185 g_assert (chooser_entry->current_folder_file == NULL);
1187 beep (chooser_entry);
1188 pop_up_completion_feedback (chooser_entry, feedback_msg);
1189 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1194 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1197 GtkFileChooserEntry *chooser_entry;
1198 GtkEditable *editable;
1200 GdkModifierType state;
1201 gboolean control_pressed;
1203 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1204 editable = GTK_EDITABLE (widget);
1205 entry = GTK_ENTRY (widget);
1207 if (!chooser_entry->eat_tabs)
1208 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1210 control_pressed = FALSE;
1212 if (gtk_get_current_event_state (&state))
1214 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1215 control_pressed = TRUE;
1218 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1219 * makes it 'safe' for people to hit. */
1220 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1222 if (chooser_entry->has_completion)
1223 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1225 start_explicit_completion (chooser_entry);
1230 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1235 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1236 GdkEventFocus *event)
1238 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1240 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1242 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1246 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1248 if (chooser_entry->has_completion)
1250 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1251 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1254 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1255 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1259 gtk_file_chooser_entry_activate (GtkEntry *entry)
1261 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1263 commit_completion_and_refresh (chooser_entry);
1264 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1268 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1270 if (!chooser_entry->completion_store)
1273 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1274 g_object_unref (chooser_entry->completion_store);
1275 chooser_entry->completion_store = NULL;
1279 completion_store_set (GtkFileSystemModel *model,
1286 GtkFileChooserEntry *chooser_entry = data;
1288 const char *prefix = "";
1289 const char *suffix = "";
1294 g_value_set_object (value, file);
1296 case FULL_PATH_COLUMN:
1297 prefix = chooser_entry->dir_part;
1299 case DISPLAY_NAME_COLUMN:
1300 if (_gtk_file_info_consider_as_directory (info))
1301 suffix = G_DIR_SEPARATOR_S;
1303 g_value_take_string (value, g_strconcat (
1305 g_file_info_get_display_name (info),
1310 g_assert_not_reached ();
1317 /* Fills the completion store from the contents of the current folder */
1319 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1321 discard_completion_store (chooser_entry);
1323 chooser_entry->completion_store = GTK_TREE_MODEL (
1324 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
1325 "standard::name,standard::display-name,standard::type",
1326 completion_store_set,
1332 g_signal_connect (chooser_entry->completion_store, "finished-loading",
1333 G_CALLBACK (finished_loading_cb), chooser_entry);
1335 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1336 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1338 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1339 chooser_entry->completion_store);
1342 /* When we finish loading the current folder, this function should get called to
1343 * perform the deferred autocompletion or explicit completion.
1346 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1348 switch (chooser_entry->load_complete_action)
1350 case LOAD_COMPLETE_NOTHING:
1353 case LOAD_COMPLETE_AUTOCOMPLETE:
1354 autocomplete (chooser_entry);
1357 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1358 explicitly_complete (chooser_entry);
1362 g_assert_not_reached ();
1365 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1369 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1371 perform_load_complete_action (chooser_entry);
1373 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1376 /* Callback when the current folder finishes loading */
1378 finished_loading_cb (GtkFileSystemModel *model,
1380 GtkFileChooserEntry *chooser_entry)
1382 chooser_entry->current_folder_loaded = TRUE;
1386 LoadCompleteAction old_load_complete_action;
1388 old_load_complete_action = chooser_entry->load_complete_action;
1390 discard_completion_store (chooser_entry);
1391 clear_completions (chooser_entry);
1393 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1395 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1397 beep (chooser_entry);
1398 pop_up_completion_feedback (chooser_entry, error->message);
1404 finish_folder_load (chooser_entry);
1407 static RefreshStatus
1408 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1411 g_assert (folder_file != NULL);
1413 if (chooser_entry->current_folder_file
1414 && g_file_equal (folder_file, chooser_entry->current_folder_file))
1417 if (chooser_entry->current_folder_file)
1419 discard_loading_and_current_folder_file (chooser_entry);
1422 if (chooser_entry->local_only
1423 && !g_file_is_native (folder_file))
1424 return REFRESH_NOT_LOCAL;
1426 chooser_entry->current_folder_file = g_object_ref (folder_file);
1427 chooser_entry->current_folder_loaded = FALSE;
1429 populate_completion_store (chooser_entry);
1434 static RefreshStatus
1435 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1436 RefreshMode refresh_mode)
1438 GtkEditable *editable;
1443 gsize total_len, file_part_len;
1446 RefreshStatus result;
1448 editable = GTK_EDITABLE (chooser_entry);
1450 switch (refresh_mode)
1452 case REFRESH_UP_TO_CURSOR_POSITION:
1453 end_pos = gtk_editable_get_position (editable);
1456 case REFRESH_WHOLE_TEXT:
1457 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1461 g_assert_not_reached ();
1462 return REFRESH_INVALID_INPUT;
1465 text = gtk_editable_get_chars (editable, 0, end_pos);
1468 if (!gtk_file_chooser_entry_parse (chooser_entry,
1469 text, &folder_file, &file_part, &error))
1471 folder_file = g_object_ref (chooser_entry->base_folder);
1473 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1474 result = REFRESH_NONEXISTENT;
1476 result = REFRESH_INVALID_INPUT;
1479 g_error_free (error);
1481 file_part = g_strdup ("");
1486 g_assert (folder_file != NULL);
1488 file_part_len = strlen (file_part);
1489 total_len = strlen (text);
1490 if (total_len > file_part_len)
1491 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1495 result = REFRESH_OK;
1498 g_free (chooser_entry->file_part);
1499 g_free (chooser_entry->dir_part);
1501 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
1502 chooser_entry->file_part = file_part;
1503 chooser_entry->file_part_pos = file_part_pos;
1507 if (result == REFRESH_OK)
1509 result = reload_current_folder (chooser_entry, folder_file);
1513 discard_loading_and_current_folder_file (chooser_entry);
1517 g_object_unref (folder_file);
1519 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1520 ((result == REFRESH_OK)
1521 && (chooser_entry->current_folder_file != NULL))
1522 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1523 || ((result != REFRESH_OK)
1524 && (chooser_entry->current_folder_file == NULL)));
1530 autocomplete (GtkFileChooserEntry *chooser_entry)
1532 if (!(chooser_entry->current_folder_loaded
1533 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1536 append_common_prefix (chooser_entry, TRUE, FALSE);
1540 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1542 RefreshStatus status;
1544 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1549 g_assert (chooser_entry->current_folder_file != NULL);
1551 if (chooser_entry->current_folder_loaded)
1552 autocomplete (chooser_entry);
1554 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1558 case REFRESH_INVALID_INPUT:
1559 case REFRESH_INCOMPLETE_HOSTNAME:
1560 case REFRESH_NONEXISTENT:
1561 case REFRESH_NOT_LOCAL:
1562 /* We don't beep or anything, since this is autocompletion - the user
1563 * didn't request any action explicitly.
1568 g_assert_not_reached ();
1574 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1575 const gchar *new_text,
1576 gint new_text_length,
1580 const gchar *colon = memchr (new_text, ':', new_text_length);
1583 /* Disallow these characters altogether */
1584 for (i = 0; i < new_text_length; i++)
1586 if (new_text[i] == '<' ||
1587 new_text[i] == '>' ||
1588 new_text[i] == '"' ||
1589 new_text[i] == '|' ||
1590 new_text[i] == '*' ||
1595 if (i < new_text_length ||
1596 /* Disallow entering text that would cause a colon to be anywhere except
1597 * after a drive letter.
1600 *position + (colon - new_text) != 1) ||
1601 (new_text_length > 0 &&
1603 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1604 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1606 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1607 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1615 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1620 /* If deleting a drive letter, delete the colon, too */
1621 if (start_pos == 0 && end_pos == 1 &&
1622 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1623 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1625 g_signal_handlers_block_by_func (chooser_entry,
1626 G_CALLBACK (delete_text_callback),
1628 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1629 g_signal_handlers_unblock_by_func (chooser_entry,
1630 G_CALLBACK (delete_text_callback),
1637 * _gtk_file_chooser_entry_new:
1638 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1640 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1641 * is an internal implementation widget for the GTK+ file chooser
1642 * which is an entry with completion with respect to a
1643 * #GtkFileSystem object.
1645 * Return value: the newly created #GtkFileChooserEntry
1648 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1650 GtkFileChooserEntry *chooser_entry;
1652 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1653 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1655 return GTK_WIDGET (chooser_entry);
1659 * _gtk_file_chooser_entry_set_base_folder:
1660 * @chooser_entry: a #GtkFileChooserEntry
1661 * @file: file for a folder in the chooser entries current file system.
1663 * Sets the folder with respect to which completions occur.
1666 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1670 g_object_ref (file);
1672 file = g_file_new_for_path (g_get_home_dir ());
1674 if (g_file_equal (chooser_entry->base_folder, file))
1676 g_object_unref (file);
1680 if (chooser_entry->base_folder)
1681 g_object_unref (chooser_entry->base_folder);
1683 chooser_entry->base_folder = file;
1685 clear_completions (chooser_entry);
1689 * _gtk_file_chooser_entry_get_current_folder:
1690 * @chooser_entry: a #GtkFileChooserEntry
1692 * Gets the current folder for the #GtkFileChooserEntry. If the
1693 * user has only entered a filename, this will be in the base folder
1694 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1695 * user has entered a relative or absolute path, then it will
1696 * be different. If the user has entered unparsable text, or text which
1697 * the entry cannot handle, this will return %NULL.
1699 * Return value: the file for the current folder - you must g_object_unref()
1700 * the value after use.
1703 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1705 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1707 return gtk_file_chooser_get_directory_for_text (chooser_entry,
1708 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
1712 * _gtk_file_chooser_entry_get_file_part:
1713 * @chooser_entry: a #GtkFileChooserEntry
1715 * Gets the non-folder portion of whatever the user has entered
1716 * into the file selector. What is returned is a UTF-8 string,
1717 * and if a filename path is needed, g_file_get_child_for_display_name()
1720 * Return value: the entered filename - this value is owned by the
1721 * chooser entry and must not be modified or freed.
1724 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1726 const char *last_slash, *text;
1728 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1730 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1731 last_slash = strrchr (text, G_DIR_SEPARATOR);
1733 return last_slash + 1;
1739 * _gtk_file_chooser_entry_set_file_part:
1740 * @chooser_entry: a #GtkFileChooserEntry
1741 * @file_part: text to display in the entry, in UTF-8
1743 * Sets the current text shown in the file chooser entry.
1746 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1747 const gchar *file_part)
1749 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1751 chooser_entry->in_change = TRUE;
1752 clear_completions (chooser_entry);
1753 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1754 chooser_entry->in_change = FALSE;
1759 * _gtk_file_chooser_entry_set_action:
1760 * @chooser_entry: a #GtkFileChooserEntry
1761 * @action: the action which is performed by the file selector using this entry
1763 * Sets action which is performed by the file selector using this entry.
1764 * The #GtkFileChooserEntry will use different completion strategies for
1765 * different actions.
1768 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1769 GtkFileChooserAction action)
1771 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1773 if (chooser_entry->action != action)
1775 GtkEntryCompletion *comp;
1777 chooser_entry->action = action;
1779 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1781 /* FIXME: do we need to actually set the following? */
1785 case GTK_FILE_CHOOSER_ACTION_OPEN:
1786 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1787 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1789 case GTK_FILE_CHOOSER_ACTION_SAVE:
1790 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1791 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1799 * _gtk_file_chooser_entry_get_action:
1800 * @chooser_entry: a #GtkFileChooserEntry
1802 * Gets the action for this entry.
1804 * Returns: the action
1806 GtkFileChooserAction
1807 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1809 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1810 GTK_FILE_CHOOSER_ACTION_OPEN);
1812 return chooser_entry->action;
1816 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1822 if (chooser_entry->completion_store == NULL ||
1823 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1828 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1831 return _gtk_file_info_consider_as_directory (info);
1836 * _gtk_file_chooser_entry_select_filename:
1837 * @chooser_entry: a #GtkFileChooserEntry
1839 * Selects the filename (without the extension) for user edition.
1842 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1844 const gchar *str, *ext;
1847 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1849 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1850 ext = g_strrstr (str, ".");
1853 len = g_utf8_pointer_to_offset (str, ext);
1856 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1860 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1861 gboolean local_only)
1863 chooser_entry->local_only = local_only;
1864 clear_completions (chooser_entry);
1868 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1870 return chooser_entry->local_only;