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)
291 gtk_tree_model_get (model, iter,
292 DISPLAY_NAME_COLUMN, &display_name,
296 if (!display_name || !file)
299 g_object_unref (file);
301 g_free (display_name);
305 pos = chooser_entry->file_part_pos;
307 /* We don't set in_change here as we want to update the current_folder
309 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
311 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
314 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
316 g_object_unref (file);
317 g_free (display_name);
322 /* Match function for the GtkEntryCompletion */
324 completion_match_func (GtkEntryCompletion *comp,
325 const char *key_unused,
329 GtkFileChooserEntry *chooser_entry;
332 char *norm_file_part;
335 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
337 /* We ignore the key because it is the contents of the entry. Instead, we
338 * just use our precomputed file_part.
340 if (!chooser_entry->file_part)
345 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
348 return FALSE; /* Uninitialized row, ugh */
351 /* If we have an empty file_part, then we're at the root of a directory. In
352 * that case, we want to match all non-dot files. We might want to match
353 * dot_files too if show_hidden is TRUE on the fileselector in the future.
355 /* Additionally, support for gnome .hidden files would be sweet, too */
356 if (chooser_entry->file_part[0] == '\000')
368 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
369 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
371 #ifdef G_PLATFORM_WIN32
375 temp = norm_file_part;
376 norm_file_part = g_utf8_casefold (norm_file_part, -1);
380 norm_name = g_utf8_casefold (norm_name, -1);
385 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
387 g_free (norm_file_part);
395 clear_completions (GtkFileChooserEntry *chooser_entry)
397 chooser_entry->has_completion = FALSE;
398 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
400 remove_completion_feedback (chooser_entry);
404 beep (GtkFileChooserEntry *chooser_entry)
406 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
410 is_valid_scheme_character (char c)
412 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
416 has_uri_scheme (const char *str)
422 if (!is_valid_scheme_character (*p))
427 while (is_valid_scheme_character (*p));
429 return (strncmp (p, "://", 3) == 0);
433 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
438 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
439 file = g_file_parse_name (str);
441 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
447 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
450 GFile *file, *parent;
452 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
454 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
457 parent = g_file_get_parent (file);
458 g_object_unref (file);
464 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
471 gboolean result = FALSE;
472 gboolean is_dir = FALSE;
473 gchar *last_slash = NULL;
476 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
478 last_slash = strrchr (str, G_DIR_SEPARATOR);
480 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
482 if (g_file_equal (chooser_entry->base_folder, file))
484 /* this is when user types '.', could be the
485 * beginning of a hidden file, ./ or ../
487 *folder = g_object_ref (file);
488 *file_part = g_strdup (str);
493 /* it's a dir, or at least it ends with the dir separator */
494 *folder = g_object_ref (file);
495 *file_part = g_strdup ("");
502 parent_file = g_file_get_parent (file);
507 GTK_FILE_CHOOSER_ERROR,
508 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
509 "Could not get parent file");
515 *folder = parent_file;
519 *file_part = g_strdup (last_slash + 1);
521 *file_part = g_strdup (str);
525 g_object_unref (file);
530 /* Determines if the completion model has entries with a common prefix relative
531 * to the current contents of the entry. Also, if there's one and only one such
532 * path, stores it in unique_path_ret.
535 find_common_prefix (GtkFileChooserEntry *chooser_entry,
536 gchar **common_prefix_ret,
537 GFile **unique_file_ret,
538 gboolean *is_complete_not_unique_ret,
539 gboolean *prefix_expands_the_file_part_ret,
542 GtkEditable *editable;
546 char *text_up_to_cursor;
547 GFile *parsed_folder_file;
548 char *parsed_file_part;
550 *common_prefix_ret = NULL;
551 *unique_file_ret = NULL;
552 *is_complete_not_unique_ret = FALSE;
553 *prefix_expands_the_file_part_ret = FALSE;
555 editable = GTK_EDITABLE (chooser_entry);
557 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
559 parsed = gtk_file_chooser_entry_parse (chooser_entry,
565 g_free (text_up_to_cursor);
570 g_assert (parsed_folder_file != NULL
571 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
573 g_object_unref (parsed_folder_file);
575 /* First pass: find the common prefix */
577 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
584 gtk_tree_model_get (chooser_entry->completion_store,
586 DISPLAY_NAME_COLUMN, &display_name,
590 if (g_str_has_prefix (display_name, parsed_file_part))
592 if (!*common_prefix_ret)
594 *common_prefix_ret = g_strdup (display_name);
595 *unique_file_ret = g_object_ref (file);
599 gchar *p = *common_prefix_ret;
600 const gchar *q = display_name;
602 while (*p && *p == *q)
610 if (*unique_file_ret)
612 g_object_unref (*unique_file_ret);
613 *unique_file_ret = NULL;
618 g_free (display_name);
619 g_object_unref (file);
620 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
623 /* Second pass: see if the prefix we found is a complete match */
625 if (*common_prefix_ret != NULL)
627 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
634 gtk_tree_model_get (chooser_entry->completion_store,
636 DISPLAY_NAME_COLUMN, &display_name,
638 len = strlen (display_name);
641 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
644 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
645 *is_complete_not_unique_ret = TRUE;
647 g_free (display_name);
648 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
651 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
653 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
656 g_free (parsed_file_part);
662 INVALID_INPUT, /* what the user typed is bogus */
663 NO_MATCH, /* no matches based on what the user typed */
664 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
665 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
666 COMPLETED, /* completion inserted (ambiguous suffix) */
667 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
668 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
669 } CommonPrefixResult;
671 /* Finds a common prefix based on the contents of the entry
672 * and mandatorily appends it
674 static CommonPrefixResult
675 append_common_prefix (GtkFileChooserEntry *chooser_entry,
677 gboolean show_errors)
679 gchar *common_prefix;
681 gboolean is_complete_not_unique;
682 gboolean prefix_expands_the_file_part;
684 CommonPrefixResult result = NO_MATCH;
685 gboolean have_result;
687 clear_completions (chooser_entry);
689 if (chooser_entry->completion_store == NULL)
693 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
695 g_error_free (error);
697 return INVALID_INPUT;
704 g_object_unref (unique_file);
706 if (prefix_expands_the_file_part)
707 result = COMPLETED_UNIQUE;
709 result = NOTHING_INSERTED_UNIQUE;
715 if (is_complete_not_unique)
717 result = COMPLETE_BUT_NOT_UNIQUE;
727 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
729 pos = chooser_entry->file_part_pos;
731 if (prefix_expands_the_file_part)
733 chooser_entry->in_change = TRUE;
734 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
736 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
739 chooser_entry->in_change = FALSE;
743 /* equivalent to cursor_pos + common_prefix_len); */
744 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
747 chooser_entry->has_completion = TRUE;
750 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
752 else if (!have_result)
754 result = NOTHING_INSERTED_COMPLETE;
758 g_free (common_prefix);
775 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
776 const gchar *new_text,
777 gint new_text_length,
780 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
784 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
785 insert_pos = *position;
787 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
789 if (chooser_entry->in_change)
792 remove_completion_feedback (chooser_entry);
794 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
795 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
796 && insert_pos == old_text_len)
797 start_autocompletion (chooser_entry);
801 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
803 if (chooser_entry->in_change)
806 clear_completions (chooser_entry);
810 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
814 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
816 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
818 clear_completions_if_not_in_change (chooser_entry);
822 gtk_file_chooser_entry_set_position (GtkEditable *editable,
825 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
827 parent_editable_iface->set_position (editable, position);
829 clear_completions_if_not_in_change (chooser_entry);
833 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
837 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
839 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
841 clear_completions_if_not_in_change (chooser_entry);
845 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
847 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
848 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
852 gtk_file_chooser_entry_unmap (GtkWidget *widget)
854 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
856 remove_completion_feedback (chooser_entry);
858 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
862 completion_feedback_window_draw_cb (GtkWidget *widget,
866 /* Stolen from gtk_tooltip_paint_window() */
868 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
869 GtkStyleContext *context;
871 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
873 gtk_render_background (context, cr, 0, 0,
874 gtk_widget_get_allocated_width (widget),
875 gtk_widget_get_allocated_height (widget));
876 gtk_render_frame (context, cr, 0, 0,
877 gtk_widget_get_allocated_width (widget),
878 gtk_widget_get_allocated_height (widget));
884 set_invisible_mouse_cursor (GdkWindow *window)
889 display = gdk_window_get_display (window);
890 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
892 gdk_window_set_cursor (window, cursor);
894 g_object_unref (cursor);
898 completion_feedback_window_realize_cb (GtkWidget *widget,
901 /* We hide the mouse cursor inside the completion feedback window, since
902 * GtkEntry hides the cursor when the user types. We don't want the cursor to
903 * come back if the completion feedback ends up where the mouse is.
905 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
909 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
911 /* Stolen from gtk_tooltip_init() */
912 GtkWidget *window, *label;
913 GtkStyleContext *context;
915 window = gtk_window_new (GTK_WINDOW_POPUP);
916 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
917 gtk_widget_set_app_paintable (window, TRUE);
918 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
919 gtk_widget_set_name (window, "gtk-tooltip");
921 context = gtk_widget_get_style_context (window);
922 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
924 g_signal_connect (window, "draw",
925 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
926 g_signal_connect (window, "realize",
927 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
928 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
930 label = gtk_label_new (NULL);
931 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
932 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
933 /* FIXME: don't hardcode this */
934 gtk_widget_set_margin_left (label, 6);
935 gtk_widget_set_margin_right (label, 6);
936 gtk_widget_set_margin_top (label, 6);
937 gtk_widget_set_margin_bottom (label, 6);
938 gtk_container_add (GTK_CONTAINER (window), label);
939 gtk_widget_show (label);
941 chooser_entry->completion_feedback_window = window;
942 chooser_entry->completion_feedback_label = label;
946 completion_feedback_timeout_cb (gpointer data)
948 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
950 chooser_entry->completion_feedback_timeout_id = 0;
952 remove_completion_feedback (chooser_entry);
957 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
959 if (chooser_entry->completion_feedback_timeout_id != 0)
960 g_source_remove (chooser_entry->completion_feedback_timeout_id);
962 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
963 completion_feedback_timeout_cb,
967 /* Gets the x position of the text cursor in the entry, in widget coordinates */
969 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
972 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
973 * the reader, you have to implement support for the entry's scroll offset and
974 * RTL layouts and all the fancy Pango stuff.
978 gint layout_x, layout_y;
980 PangoRectangle strong_pos;
981 gint start_pos, end_pos;
983 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
985 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
987 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
988 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
992 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
994 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
998 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1000 /* More or less stolen from gtk_tooltip_position() */
1002 GtkRequisition feedback_req;
1003 GtkWidget *widget = GTK_WIDGET (chooser_entry);
1004 gint entry_x, entry_y;
1006 GtkAllocation entry_allocation;
1007 int feedback_x, feedback_y;
1009 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
1010 &feedback_req, NULL);
1012 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1013 gtk_widget_get_allocation (widget, &entry_allocation);
1015 get_entry_cursor_x (chooser_entry, &cursor_x);
1017 /* FIXME: fit to the screen if we bump on the screen's edge */
1018 /* cheap "half M-width", use height as approximation of character em-size */
1019 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1020 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1022 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1023 gtk_widget_show (chooser_entry->completion_feedback_window);
1025 install_completion_feedback_timer (chooser_entry);
1029 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1030 const gchar *feedback)
1032 if (chooser_entry->completion_feedback_window == NULL)
1033 create_completion_feedback_window (chooser_entry);
1035 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1037 show_completion_feedback_window (chooser_entry);
1041 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1043 if (chooser_entry->completion_feedback_window)
1044 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1046 chooser_entry->completion_feedback_window = NULL;
1047 chooser_entry->completion_feedback_label = NULL;
1049 if (chooser_entry->completion_feedback_timeout_id != 0)
1051 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1052 chooser_entry->completion_feedback_timeout_id = 0;
1057 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1059 CommonPrefixResult result;
1061 g_assert (chooser_entry->current_folder_loaded);
1063 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1065 * - If there is a common prefix, insert it (done)
1066 * - If there is no common prefix, pop up the suggestion window
1067 * - If there are no matches at all, beep and bring up a tooltip (done)
1068 * - If the suggestion window is already up, scroll it
1070 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1075 /* We already beeped in append_common_prefix(); do nothing here */
1079 beep (chooser_entry);
1080 /* translators: this text is shown when there are no completions
1081 * for something the user typed in a file chooser entry
1083 pop_up_completion_feedback (chooser_entry, _("No match"));
1086 case NOTHING_INSERTED_COMPLETE:
1087 /* FIXME: pop up the suggestion window or scroll it */
1090 case NOTHING_INSERTED_UNIQUE:
1091 /* translators: this text is shown when there is exactly one completion
1092 * for something the user typed in a file chooser entry
1094 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1101 case COMPLETED_UNIQUE:
1105 case COMPLETE_BUT_NOT_UNIQUE:
1106 /* translators: this text is shown when the text in a file chooser
1107 * entry is a complete filename, but could be continued to find
1110 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1114 g_assert_not_reached ();
1119 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1121 RefreshStatus status;
1125 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1132 g_assert (chooser_entry->current_folder_file != NULL);
1134 if (chooser_entry->current_folder_loaded)
1135 explicitly_complete (chooser_entry);
1138 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1140 /* Translators: this text is shown while the system is searching
1141 * for possible completions for filenames in a file chooser entry. */
1142 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1147 case REFRESH_INVALID_INPUT:
1149 /* Translators: this is shown in the feedback for Tab-completion in a file
1150 * chooser's text entry, when the user enters an invalid path. */
1151 feedback_msg = _("Invalid path");
1154 case REFRESH_INCOMPLETE_HOSTNAME:
1157 if (chooser_entry->local_only)
1159 /* hostnames in a local_only file chooser? user error */
1161 /* Translators: this is shown in the feedback for Tab-completion in a
1162 * file chooser's text entry when the user enters something like
1163 * "sftp://blahblah" in an app that only supports local filenames. */
1164 feedback_msg = _("Only local files may be selected");
1168 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1170 /* Translators: this is shown in the feedback for Tab-completion in a
1171 * file chooser's text entry when the user hasn't entered the first '/'
1172 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1173 feedback_msg = _("Incomplete hostname; end it with '/'");
1178 case REFRESH_NONEXISTENT:
1181 /* Translators: this is shown in the feedback for Tab-completion in a file
1182 * chooser's text entry when the user enters a path that does not exist
1183 * and then hits Tab */
1184 feedback_msg = _("Path does not exist");
1187 case REFRESH_NOT_LOCAL:
1189 feedback_msg = _("Only local files may be selected");
1193 g_assert_not_reached ();
1199 g_assert (chooser_entry->current_folder_file == NULL);
1201 beep (chooser_entry);
1202 pop_up_completion_feedback (chooser_entry, feedback_msg);
1203 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1208 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1211 GtkFileChooserEntry *chooser_entry;
1212 GtkEditable *editable;
1214 GdkModifierType state;
1215 gboolean control_pressed;
1217 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1218 editable = GTK_EDITABLE (widget);
1219 entry = GTK_ENTRY (widget);
1221 if (!chooser_entry->eat_tabs)
1222 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1224 control_pressed = FALSE;
1226 if (gtk_get_current_event_state (&state))
1228 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1229 control_pressed = TRUE;
1232 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1233 * makes it 'safe' for people to hit. */
1234 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1236 if (chooser_entry->has_completion)
1237 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1239 start_explicit_completion (chooser_entry);
1244 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1249 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1250 GdkEventFocus *event)
1252 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1254 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1256 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1260 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1262 if (chooser_entry->has_completion)
1264 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1265 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1268 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1269 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1273 gtk_file_chooser_entry_activate (GtkEntry *entry)
1275 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1277 commit_completion_and_refresh (chooser_entry);
1278 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1282 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1284 if (!chooser_entry->completion_store)
1287 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1288 g_object_unref (chooser_entry->completion_store);
1289 chooser_entry->completion_store = NULL;
1293 completion_store_set (GtkFileSystemModel *model,
1300 GtkFileChooserEntry *chooser_entry = data;
1302 const char *prefix = "";
1303 const char *suffix = "";
1308 g_value_set_object (value, file);
1310 case FULL_PATH_COLUMN:
1311 prefix = chooser_entry->dir_part;
1313 case DISPLAY_NAME_COLUMN:
1314 if (_gtk_file_info_consider_as_directory (info))
1315 suffix = G_DIR_SEPARATOR_S;
1317 g_value_take_string (value, g_strconcat (
1319 g_file_info_get_display_name (info),
1324 g_assert_not_reached ();
1331 /* Fills the completion store from the contents of the current folder */
1333 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1335 discard_completion_store (chooser_entry);
1337 chooser_entry->completion_store = GTK_TREE_MODEL (
1338 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
1339 "standard::name,standard::display-name,standard::type",
1340 completion_store_set,
1346 g_signal_connect (chooser_entry->completion_store, "finished-loading",
1347 G_CALLBACK (finished_loading_cb), chooser_entry);
1349 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1350 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1352 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1353 chooser_entry->completion_store);
1356 /* When we finish loading the current folder, this function should get called to
1357 * perform the deferred autocompletion or explicit completion.
1360 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1362 switch (chooser_entry->load_complete_action)
1364 case LOAD_COMPLETE_NOTHING:
1367 case LOAD_COMPLETE_AUTOCOMPLETE:
1368 autocomplete (chooser_entry);
1371 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1372 explicitly_complete (chooser_entry);
1376 g_assert_not_reached ();
1379 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1383 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1385 perform_load_complete_action (chooser_entry);
1387 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1390 /* Callback when the current folder finishes loading */
1392 finished_loading_cb (GtkFileSystemModel *model,
1394 GtkFileChooserEntry *chooser_entry)
1396 chooser_entry->current_folder_loaded = TRUE;
1400 LoadCompleteAction old_load_complete_action;
1402 old_load_complete_action = chooser_entry->load_complete_action;
1404 discard_completion_store (chooser_entry);
1405 clear_completions (chooser_entry);
1407 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1409 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1411 beep (chooser_entry);
1412 pop_up_completion_feedback (chooser_entry, error->message);
1418 finish_folder_load (chooser_entry);
1421 static RefreshStatus
1422 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1425 g_assert (folder_file != NULL);
1427 if (chooser_entry->current_folder_file
1428 && g_file_equal (folder_file, chooser_entry->current_folder_file))
1431 if (chooser_entry->current_folder_file)
1433 discard_loading_and_current_folder_file (chooser_entry);
1436 if (chooser_entry->local_only
1437 && !g_file_is_native (folder_file))
1438 return REFRESH_NOT_LOCAL;
1440 chooser_entry->current_folder_file = g_object_ref (folder_file);
1441 chooser_entry->current_folder_loaded = FALSE;
1443 populate_completion_store (chooser_entry);
1448 static RefreshStatus
1449 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1450 RefreshMode refresh_mode)
1452 GtkEditable *editable;
1457 gsize total_len, file_part_len;
1460 RefreshStatus result;
1462 editable = GTK_EDITABLE (chooser_entry);
1464 switch (refresh_mode)
1466 case REFRESH_UP_TO_CURSOR_POSITION:
1467 end_pos = gtk_editable_get_position (editable);
1470 case REFRESH_WHOLE_TEXT:
1471 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1475 g_assert_not_reached ();
1476 return REFRESH_INVALID_INPUT;
1479 text = gtk_editable_get_chars (editable, 0, end_pos);
1482 if (!gtk_file_chooser_entry_parse (chooser_entry,
1483 text, &folder_file, &file_part, &error))
1485 folder_file = g_object_ref (chooser_entry->base_folder);
1487 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1488 result = REFRESH_NONEXISTENT;
1490 result = REFRESH_INVALID_INPUT;
1493 g_error_free (error);
1495 file_part = g_strdup ("");
1500 g_assert (folder_file != NULL);
1502 file_part_len = strlen (file_part);
1503 total_len = strlen (text);
1504 if (total_len > file_part_len)
1505 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1509 result = REFRESH_OK;
1512 g_free (chooser_entry->file_part);
1513 g_free (chooser_entry->dir_part);
1515 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
1516 chooser_entry->file_part = file_part;
1517 chooser_entry->file_part_pos = file_part_pos;
1521 if (result == REFRESH_OK)
1523 result = reload_current_folder (chooser_entry, folder_file);
1527 discard_loading_and_current_folder_file (chooser_entry);
1531 g_object_unref (folder_file);
1533 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1534 ((result == REFRESH_OK)
1535 && (chooser_entry->current_folder_file != NULL))
1536 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1537 || ((result != REFRESH_OK)
1538 && (chooser_entry->current_folder_file == NULL)));
1544 autocomplete (GtkFileChooserEntry *chooser_entry)
1546 if (!(chooser_entry->current_folder_loaded
1547 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1550 append_common_prefix (chooser_entry, TRUE, FALSE);
1554 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1556 RefreshStatus status;
1558 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1563 g_assert (chooser_entry->current_folder_file != NULL);
1565 if (chooser_entry->current_folder_loaded)
1566 autocomplete (chooser_entry);
1568 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1572 case REFRESH_INVALID_INPUT:
1573 case REFRESH_INCOMPLETE_HOSTNAME:
1574 case REFRESH_NONEXISTENT:
1575 case REFRESH_NOT_LOCAL:
1576 /* We don't beep or anything, since this is autocompletion - the user
1577 * didn't request any action explicitly.
1582 g_assert_not_reached ();
1588 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1589 const gchar *new_text,
1590 gint new_text_length,
1594 const gchar *colon = memchr (new_text, ':', new_text_length);
1597 /* Disallow these characters altogether */
1598 for (i = 0; i < new_text_length; i++)
1600 if (new_text[i] == '<' ||
1601 new_text[i] == '>' ||
1602 new_text[i] == '"' ||
1603 new_text[i] == '|' ||
1604 new_text[i] == '*' ||
1609 if (i < new_text_length ||
1610 /* Disallow entering text that would cause a colon to be anywhere except
1611 * after a drive letter.
1614 *position + (colon - new_text) != 1) ||
1615 (new_text_length > 0 &&
1617 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1618 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1620 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1621 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1629 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1634 /* If deleting a drive letter, delete the colon, too */
1635 if (start_pos == 0 && end_pos == 1 &&
1636 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1637 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1639 g_signal_handlers_block_by_func (chooser_entry,
1640 G_CALLBACK (delete_text_callback),
1642 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1643 g_signal_handlers_unblock_by_func (chooser_entry,
1644 G_CALLBACK (delete_text_callback),
1651 * _gtk_file_chooser_entry_new:
1652 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1654 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1655 * is an internal implementation widget for the GTK+ file chooser
1656 * which is an entry with completion with respect to a
1657 * #GtkFileSystem object.
1659 * Return value: the newly created #GtkFileChooserEntry
1662 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1664 GtkFileChooserEntry *chooser_entry;
1666 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1667 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1669 return GTK_WIDGET (chooser_entry);
1673 * _gtk_file_chooser_entry_set_base_folder:
1674 * @chooser_entry: a #GtkFileChooserEntry
1675 * @file: file for a folder in the chooser entries current file system.
1677 * Sets the folder with respect to which completions occur.
1680 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1684 g_object_ref (file);
1686 file = g_file_new_for_path (g_get_home_dir ());
1688 if (g_file_equal (chooser_entry->base_folder, file))
1690 g_object_unref (file);
1694 if (chooser_entry->base_folder)
1695 g_object_unref (chooser_entry->base_folder);
1697 chooser_entry->base_folder = file;
1699 clear_completions (chooser_entry);
1703 * _gtk_file_chooser_entry_get_current_folder:
1704 * @chooser_entry: a #GtkFileChooserEntry
1706 * Gets the current folder for the #GtkFileChooserEntry. If the
1707 * user has only entered a filename, this will be in the base folder
1708 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1709 * user has entered a relative or absolute path, then it will
1710 * be different. If the user has entered unparsable text, or text which
1711 * the entry cannot handle, this will return %NULL.
1713 * Return value: the file for the current folder - you must g_object_unref()
1714 * the value after use.
1717 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1719 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1721 return gtk_file_chooser_get_directory_for_text (chooser_entry,
1722 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
1726 * _gtk_file_chooser_entry_get_file_part:
1727 * @chooser_entry: a #GtkFileChooserEntry
1729 * Gets the non-folder portion of whatever the user has entered
1730 * into the file selector. What is returned is a UTF-8 string,
1731 * and if a filename path is needed, g_file_get_child_for_display_name()
1734 * Return value: the entered filename - this value is owned by the
1735 * chooser entry and must not be modified or freed.
1738 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1740 const char *last_slash, *text;
1742 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1744 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1745 last_slash = strrchr (text, G_DIR_SEPARATOR);
1747 return last_slash + 1;
1753 * _gtk_file_chooser_entry_set_file_part:
1754 * @chooser_entry: a #GtkFileChooserEntry
1755 * @file_part: text to display in the entry, in UTF-8
1757 * Sets the current text shown in the file chooser entry.
1760 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1761 const gchar *file_part)
1763 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1765 chooser_entry->in_change = TRUE;
1766 clear_completions (chooser_entry);
1767 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1768 chooser_entry->in_change = FALSE;
1773 * _gtk_file_chooser_entry_set_action:
1774 * @chooser_entry: a #GtkFileChooserEntry
1775 * @action: the action which is performed by the file selector using this entry
1777 * Sets action which is performed by the file selector using this entry.
1778 * The #GtkFileChooserEntry will use different completion strategies for
1779 * different actions.
1782 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1783 GtkFileChooserAction action)
1785 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1787 if (chooser_entry->action != action)
1789 GtkEntryCompletion *comp;
1791 chooser_entry->action = action;
1793 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1795 /* FIXME: do we need to actually set the following? */
1799 case GTK_FILE_CHOOSER_ACTION_OPEN:
1800 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1801 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1803 case GTK_FILE_CHOOSER_ACTION_SAVE:
1804 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1805 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1813 * _gtk_file_chooser_entry_get_action:
1814 * @chooser_entry: a #GtkFileChooserEntry
1816 * Gets the action for this entry.
1818 * Returns: the action
1820 GtkFileChooserAction
1821 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1823 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1824 GTK_FILE_CHOOSER_ACTION_OPEN);
1826 return chooser_entry->action;
1830 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1836 if (chooser_entry->completion_store == NULL ||
1837 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1842 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1845 return _gtk_file_info_consider_as_directory (info);
1850 * _gtk_file_chooser_entry_select_filename:
1851 * @chooser_entry: a #GtkFileChooserEntry
1853 * Selects the filename (without the extension) for user edition.
1856 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1858 const gchar *str, *ext;
1861 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1863 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1864 ext = g_strrstr (str, ".");
1867 len = g_utf8_pointer_to_offset (str, ext);
1870 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1874 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1875 gboolean local_only)
1877 chooser_entry->local_only = local_only;
1878 clear_completions (chooser_entry);
1882 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1884 return chooser_entry->local_only;