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;
70 GtkFileSystem *file_system;
72 GFile *current_folder_file;
76 LoadCompleteAction load_complete_action;
78 GtkTreeModel *completion_store;
80 guint start_autocompletion_idle_id;
82 GtkWidget *completion_feedback_window;
83 GtkWidget *completion_feedback_label;
84 guint completion_feedback_timeout_id;
86 guint current_folder_loaded : 1;
87 guint has_completion : 1;
100 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
102 static void gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface);
104 static void gtk_file_chooser_entry_finalize (GObject *object);
105 static void gtk_file_chooser_entry_dispose (GObject *object);
106 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
107 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
108 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
110 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
111 GdkEventFocus *event);
112 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
113 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
114 const gchar *new_text,
115 gint new_text_length,
117 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
120 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
122 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
127 static gint insert_text_callback (GtkFileChooserEntry *widget,
128 const gchar *new_text,
129 gint new_text_length,
132 static void delete_text_callback (GtkFileChooserEntry *widget,
138 static gboolean match_selected_callback (GtkEntryCompletion *completion,
141 GtkFileChooserEntry *chooser_entry);
142 static gboolean completion_match_func (GtkEntryCompletion *comp,
148 REFRESH_UP_TO_CURSOR_POSITION,
152 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
153 RefreshMode refresh_mode);
154 static void finished_loading_cb (GtkFileSystemModel *model,
156 GtkFileChooserEntry *chooser_entry);
157 static void autocomplete (GtkFileChooserEntry *chooser_entry);
158 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
159 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
160 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
161 const gchar *feedback);
163 static GtkEditableInterface *parent_editable_iface;
165 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
166 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
167 gtk_file_chooser_entry_iface_init))
170 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
172 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
173 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
174 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
176 gobject_class->finalize = gtk_file_chooser_entry_finalize;
177 gobject_class->dispose = gtk_file_chooser_entry_dispose;
179 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
180 widget_class->unmap = gtk_file_chooser_entry_unmap;
181 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
182 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
184 entry_class->activate = gtk_file_chooser_entry_activate;
188 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
190 parent_editable_iface = g_type_interface_peek_parent (iface);
192 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
193 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
194 iface->set_position = gtk_file_chooser_entry_set_position;
195 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
199 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
201 GtkEntryCompletion *comp;
202 GtkCellRenderer *cell;
204 chooser_entry->local_only = TRUE;
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 if (chooser_entry->base_folder)
243 g_object_unref (chooser_entry->base_folder);
245 if (chooser_entry->current_folder_file)
246 g_object_unref (chooser_entry->current_folder_file);
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->start_autocompletion_idle_id != 0)
273 g_source_remove (chooser_entry->start_autocompletion_idle_id);
274 chooser_entry->start_autocompletion_idle_id = 0;
277 if (chooser_entry->completion_store)
279 g_object_unref (chooser_entry->completion_store);
280 chooser_entry->completion_store = NULL;
283 if (chooser_entry->file_system)
285 g_object_unref (chooser_entry->file_system);
286 chooser_entry->file_system = NULL;
289 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
292 /* Match functions for the GtkEntryCompletion */
294 match_selected_callback (GtkEntryCompletion *completion,
297 GtkFileChooserEntry *chooser_entry)
303 gtk_tree_model_get (model, iter,
304 DISPLAY_NAME_COLUMN, &display_name,
308 if (!display_name || !file)
311 g_object_unref (file);
313 g_free (display_name);
317 pos = chooser_entry->file_part_pos;
319 /* We don't set in_change here as we want to update the current_folder
321 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
323 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
326 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
328 g_object_unref (file);
329 g_free (display_name);
334 /* Match function for the GtkEntryCompletion */
336 completion_match_func (GtkEntryCompletion *comp,
337 const char *key_unused,
341 GtkFileChooserEntry *chooser_entry;
344 char *norm_file_part;
347 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
349 /* We ignore the key because it is the contents of the entry. Instead, we
350 * just use our precomputed file_part.
352 if (!chooser_entry->file_part)
357 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
360 return FALSE; /* Uninitialized row, ugh */
363 /* If we have an empty file_part, then we're at the root of a directory. In
364 * that case, we want to match all non-dot files. We might want to match
365 * dot_files too if show_hidden is TRUE on the fileselector in the future.
367 /* Additionally, support for gnome .hidden files would be sweet, too */
368 if (chooser_entry->file_part[0] == '\000')
380 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
381 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
383 #ifdef G_PLATFORM_WIN32
387 temp = norm_file_part;
388 norm_file_part = g_utf8_casefold (norm_file_part, -1);
392 norm_name = g_utf8_casefold (norm_name, -1);
397 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
399 g_free (norm_file_part);
407 clear_completions (GtkFileChooserEntry *chooser_entry)
409 chooser_entry->has_completion = FALSE;
410 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
412 remove_completion_feedback (chooser_entry);
416 beep (GtkFileChooserEntry *chooser_entry)
418 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
422 is_valid_scheme_character (char c)
424 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
428 has_uri_scheme (const char *str)
434 if (!is_valid_scheme_character (*p))
439 while (is_valid_scheme_character (*p));
441 return (strncmp (p, "://", 3) == 0);
445 _gtk_file_system_parse (GtkFileSystem *file_system,
453 gboolean result = FALSE;
454 gboolean is_dir = FALSE;
455 gchar *last_slash = NULL;
459 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
461 last_slash = strrchr (str, G_DIR_SEPARATOR);
463 is_uri = has_uri_scheme (str);
468 const char *slash_after_hostname;
470 colon = strchr (str, ':');
471 g_assert (colon != NULL);
472 g_assert (strncmp (colon, "://", 3) == 0);
474 slash_after_hostname = strchr (colon + 3, '/');
476 if (slash_after_hostname == NULL)
478 /* We don't have a full hostname yet. So, don't switch the folder
479 * until we have seen a full hostname. Otherwise, completion will
480 * happen for every character the user types for the hostname.
486 GTK_FILE_CHOOSER_ERROR,
487 GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME,
488 "Incomplete hostname");
493 if (str[0] == '~' || g_path_is_absolute (str) || is_uri)
494 file = g_file_parse_name (str);
498 file = g_file_resolve_relative_path (base_file, str);
504 GTK_FILE_CHOOSER_ERROR,
505 GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
511 if (base_file && g_file_equal (base_file, file))
513 /* this is when user types '.', could be the
514 * beginning of a hidden file, ./ or ../
516 *folder = g_object_ref (file);
517 *file_part = g_strdup (str);
522 /* it's a dir, or at least it ends with the dir separator */
523 *folder = g_object_ref (file);
524 *file_part = g_strdup ("");
531 parent_file = g_file_get_parent (file);
536 GTK_FILE_CHOOSER_ERROR,
537 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
538 "Could not get parent file");
544 *folder = parent_file;
548 *file_part = g_strdup (last_slash + 1);
550 *file_part = g_strdup (str);
554 g_object_unref (file);
559 /* Determines if the completion model has entries with a common prefix relative
560 * to the current contents of the entry. Also, if there's one and only one such
561 * path, stores it in unique_path_ret.
564 find_common_prefix (GtkFileChooserEntry *chooser_entry,
565 gchar **common_prefix_ret,
566 GFile **unique_file_ret,
567 gboolean *is_complete_not_unique_ret,
568 gboolean *prefix_expands_the_file_part_ret,
571 GtkEditable *editable;
575 char *text_up_to_cursor;
576 GFile *parsed_folder_file;
577 char *parsed_file_part;
579 *common_prefix_ret = NULL;
580 *unique_file_ret = NULL;
581 *is_complete_not_unique_ret = FALSE;
582 *prefix_expands_the_file_part_ret = FALSE;
584 editable = GTK_EDITABLE (chooser_entry);
586 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
588 parsed = _gtk_file_system_parse (chooser_entry->file_system,
589 chooser_entry->base_folder,
595 g_free (text_up_to_cursor);
600 g_assert (parsed_folder_file != NULL
601 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
603 g_object_unref (parsed_folder_file);
605 /* First pass: find the common prefix */
607 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
614 gtk_tree_model_get (chooser_entry->completion_store,
616 DISPLAY_NAME_COLUMN, &display_name,
620 if (g_str_has_prefix (display_name, parsed_file_part))
622 if (!*common_prefix_ret)
624 *common_prefix_ret = g_strdup (display_name);
625 *unique_file_ret = g_object_ref (file);
629 gchar *p = *common_prefix_ret;
630 const gchar *q = display_name;
632 while (*p && *p == *q)
640 if (*unique_file_ret)
642 g_object_unref (*unique_file_ret);
643 *unique_file_ret = NULL;
648 g_free (display_name);
649 g_object_unref (file);
650 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
653 /* Second pass: see if the prefix we found is a complete match */
655 if (*common_prefix_ret != NULL)
657 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
664 gtk_tree_model_get (chooser_entry->completion_store,
666 DISPLAY_NAME_COLUMN, &display_name,
668 len = strlen (display_name);
671 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
674 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
675 *is_complete_not_unique_ret = TRUE;
677 g_free (display_name);
678 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
681 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
683 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
686 g_free (parsed_file_part);
692 INVALID_INPUT, /* what the user typed is bogus */
693 NO_MATCH, /* no matches based on what the user typed */
694 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
695 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
696 COMPLETED, /* completion inserted (ambiguous suffix) */
697 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
698 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
699 } CommonPrefixResult;
701 /* Finds a common prefix based on the contents of the entry
702 * and mandatorily appends it
704 static CommonPrefixResult
705 append_common_prefix (GtkFileChooserEntry *chooser_entry,
707 gboolean show_errors)
709 gchar *common_prefix;
711 gboolean is_complete_not_unique;
712 gboolean prefix_expands_the_file_part;
714 CommonPrefixResult result = NO_MATCH;
715 gboolean have_result;
717 clear_completions (chooser_entry);
719 if (chooser_entry->completion_store == NULL)
723 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
725 /* If the user types an incomplete hostname ("http://foo" without
726 * a slash after that), it's not an error. We just don't want to
727 * pop up a meaningless completion window in that state.
729 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
732 beep (chooser_entry);
733 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
736 g_error_free (error);
738 return INVALID_INPUT;
745 g_object_unref (unique_file);
747 if (prefix_expands_the_file_part)
748 result = COMPLETED_UNIQUE;
750 result = NOTHING_INSERTED_UNIQUE;
756 if (is_complete_not_unique)
758 result = COMPLETE_BUT_NOT_UNIQUE;
768 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
770 pos = chooser_entry->file_part_pos;
772 if (prefix_expands_the_file_part)
774 chooser_entry->in_change = TRUE;
775 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
777 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
780 chooser_entry->in_change = FALSE;
784 /* equivalent to cursor_pos + common_prefix_len); */
785 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
788 chooser_entry->has_completion = TRUE;
791 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
793 else if (!have_result)
795 result = NOTHING_INSERTED_COMPLETE;
799 g_free (common_prefix);
816 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
817 const gchar *new_text,
818 gint new_text_length,
821 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
825 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
826 insert_pos = *position;
828 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
830 if (chooser_entry->in_change)
833 remove_completion_feedback (chooser_entry);
835 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
836 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
837 && insert_pos == old_text_len)
838 install_start_autocompletion_idle (chooser_entry);
842 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
844 if (chooser_entry->in_change)
847 clear_completions (chooser_entry);
851 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
855 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
857 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
859 clear_completions_if_not_in_change (chooser_entry);
863 gtk_file_chooser_entry_set_position (GtkEditable *editable,
866 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
868 parent_editable_iface->set_position (editable, position);
870 clear_completions_if_not_in_change (chooser_entry);
874 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
878 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
880 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
882 clear_completions_if_not_in_change (chooser_entry);
886 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
888 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
889 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
893 gtk_file_chooser_entry_unmap (GtkWidget *widget)
895 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
897 remove_completion_feedback (chooser_entry);
899 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
903 completion_feedback_window_draw_cb (GtkWidget *widget,
907 /* Stolen from gtk_tooltip_paint_window() */
909 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
910 GtkStyleContext *context;
912 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
914 gtk_render_background (context, cr, 0, 0,
915 gtk_widget_get_allocated_width (widget),
916 gtk_widget_get_allocated_height (widget));
917 gtk_render_frame (context, cr, 0, 0,
918 gtk_widget_get_allocated_width (widget),
919 gtk_widget_get_allocated_height (widget));
925 set_invisible_mouse_cursor (GdkWindow *window)
930 display = gdk_window_get_display (window);
931 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
933 gdk_window_set_cursor (window, cursor);
935 g_object_unref (cursor);
939 completion_feedback_window_realize_cb (GtkWidget *widget,
942 /* We hide the mouse cursor inside the completion feedback window, since
943 * GtkEntry hides the cursor when the user types. We don't want the cursor to
944 * come back if the completion feedback ends up where the mouse is.
946 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
950 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
952 /* Stolen from gtk_tooltip_init() */
953 GtkWidget *window, *label;
954 GtkStyleContext *context;
956 window = gtk_window_new (GTK_WINDOW_POPUP);
957 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
958 gtk_widget_set_app_paintable (window, TRUE);
959 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
960 gtk_widget_set_name (window, "gtk-tooltip");
962 context = gtk_widget_get_style_context (window);
963 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
965 g_signal_connect (window, "draw",
966 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
967 g_signal_connect (window, "realize",
968 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
969 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
971 label = gtk_label_new (NULL);
972 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
973 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
974 /* FIXME: don't hardcode this */
975 gtk_widget_set_margin_left (label, 6);
976 gtk_widget_set_margin_right (label, 6);
977 gtk_widget_set_margin_top (label, 6);
978 gtk_widget_set_margin_bottom (label, 6);
979 gtk_container_add (GTK_CONTAINER (window), label);
980 gtk_widget_show (label);
982 chooser_entry->completion_feedback_window = window;
983 chooser_entry->completion_feedback_label = label;
987 completion_feedback_timeout_cb (gpointer data)
989 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
991 chooser_entry->completion_feedback_timeout_id = 0;
993 remove_completion_feedback (chooser_entry);
998 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
1000 if (chooser_entry->completion_feedback_timeout_id != 0)
1001 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1003 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
1004 completion_feedback_timeout_cb,
1008 /* Gets the x position of the text cursor in the entry, in widget coordinates */
1010 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
1013 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
1014 * the reader, you have to implement support for the entry's scroll offset and
1015 * RTL layouts and all the fancy Pango stuff.
1018 PangoLayout *layout;
1019 gint layout_x, layout_y;
1021 PangoRectangle strong_pos;
1022 gint start_pos, end_pos;
1024 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1026 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1028 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
1029 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1033 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1035 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1039 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1041 /* More or less stolen from gtk_tooltip_position() */
1043 GtkRequisition feedback_req;
1044 GtkWidget *widget = GTK_WIDGET (chooser_entry);
1045 gint entry_x, entry_y;
1047 GtkAllocation entry_allocation;
1048 int feedback_x, feedback_y;
1050 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
1051 &feedback_req, NULL);
1053 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1054 gtk_widget_get_allocation (widget, &entry_allocation);
1056 get_entry_cursor_x (chooser_entry, &cursor_x);
1058 /* FIXME: fit to the screen if we bump on the screen's edge */
1059 /* cheap "half M-width", use height as approximation of character em-size */
1060 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1061 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1063 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1064 gtk_widget_show (chooser_entry->completion_feedback_window);
1066 install_completion_feedback_timer (chooser_entry);
1070 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1071 const gchar *feedback)
1073 if (chooser_entry->completion_feedback_window == NULL)
1074 create_completion_feedback_window (chooser_entry);
1076 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1078 show_completion_feedback_window (chooser_entry);
1082 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1084 if (chooser_entry->completion_feedback_window)
1085 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1087 chooser_entry->completion_feedback_window = NULL;
1088 chooser_entry->completion_feedback_label = NULL;
1090 if (chooser_entry->completion_feedback_timeout_id != 0)
1092 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1093 chooser_entry->completion_feedback_timeout_id = 0;
1098 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1100 CommonPrefixResult result;
1102 g_assert (chooser_entry->current_folder_loaded);
1104 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1106 * - If there is a common prefix, insert it (done)
1107 * - If there is no common prefix, pop up the suggestion window
1108 * - If there are no matches at all, beep and bring up a tooltip (done)
1109 * - If the suggestion window is already up, scroll it
1111 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1116 /* We already beeped in append_common_prefix(); do nothing here */
1120 beep (chooser_entry);
1121 /* translators: this text is shown when there are no completions
1122 * for something the user typed in a file chooser entry
1124 pop_up_completion_feedback (chooser_entry, _("No match"));
1127 case NOTHING_INSERTED_COMPLETE:
1128 /* FIXME: pop up the suggestion window or scroll it */
1131 case NOTHING_INSERTED_UNIQUE:
1132 /* translators: this text is shown when there is exactly one completion
1133 * for something the user typed in a file chooser entry
1135 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1142 case COMPLETED_UNIQUE:
1146 case COMPLETE_BUT_NOT_UNIQUE:
1147 /* translators: this text is shown when the text in a file chooser
1148 * entry is a complete filename, but could be continued to find
1151 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1155 g_assert_not_reached ();
1160 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1162 RefreshStatus status;
1166 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1173 g_assert (chooser_entry->current_folder_file != NULL);
1175 if (chooser_entry->current_folder_loaded)
1176 explicitly_complete (chooser_entry);
1179 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1181 /* Translators: this text is shown while the system is searching
1182 * for possible completions for filenames in a file chooser entry. */
1183 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1188 case REFRESH_INVALID_INPUT:
1190 /* Translators: this is shown in the feedback for Tab-completion in a file
1191 * chooser's text entry, when the user enters an invalid path. */
1192 feedback_msg = _("Invalid path");
1195 case REFRESH_INCOMPLETE_HOSTNAME:
1198 if (chooser_entry->local_only)
1200 /* hostnames in a local_only file chooser? user error */
1202 /* Translators: this is shown in the feedback for Tab-completion in a
1203 * file chooser's text entry when the user enters something like
1204 * "sftp://blahblah" in an app that only supports local filenames. */
1205 feedback_msg = _("Only local files may be selected");
1209 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1211 /* Translators: this is shown in the feedback for Tab-completion in a
1212 * file chooser's text entry when the user hasn't entered the first '/'
1213 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1214 feedback_msg = _("Incomplete hostname; end it with '/'");
1219 case REFRESH_NONEXISTENT:
1222 /* Translators: this is shown in the feedback for Tab-completion in a file
1223 * chooser's text entry when the user enters a path that does not exist
1224 * and then hits Tab */
1225 feedback_msg = _("Path does not exist");
1228 case REFRESH_NOT_LOCAL:
1230 feedback_msg = _("Only local files may be selected");
1234 g_assert_not_reached ();
1240 g_assert (chooser_entry->current_folder_file == NULL);
1242 beep (chooser_entry);
1243 pop_up_completion_feedback (chooser_entry, feedback_msg);
1244 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1249 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1252 GtkFileChooserEntry *chooser_entry;
1253 GtkEditable *editable;
1255 GdkModifierType state;
1256 gboolean control_pressed;
1258 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1259 editable = GTK_EDITABLE (widget);
1260 entry = GTK_ENTRY (widget);
1262 if (!chooser_entry->eat_tabs)
1263 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1265 control_pressed = FALSE;
1267 if (gtk_get_current_event_state (&state))
1269 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1270 control_pressed = TRUE;
1273 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1274 * makes it 'safe' for people to hit. */
1275 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1277 if (chooser_entry->has_completion)
1278 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1280 start_explicit_completion (chooser_entry);
1285 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1290 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1291 GdkEventFocus *event)
1293 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1295 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1297 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1301 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1303 if (chooser_entry->has_completion)
1305 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1306 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1309 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1310 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1314 gtk_file_chooser_entry_activate (GtkEntry *entry)
1316 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1318 commit_completion_and_refresh (chooser_entry);
1319 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1323 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1325 if (!chooser_entry->completion_store)
1328 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1329 g_object_unref (chooser_entry->completion_store);
1330 chooser_entry->completion_store = NULL;
1334 completion_store_set (GtkFileSystemModel *model,
1344 g_value_set_object (value, file);
1346 case DISPLAY_NAME_COLUMN:
1347 if (_gtk_file_info_consider_as_directory (info))
1348 g_value_take_string (value, g_strconcat (g_file_info_get_display_name (info), G_DIR_SEPARATOR_S, NULL));
1350 g_value_set_string (value, g_file_info_get_display_name (info));
1353 g_assert_not_reached ();
1360 /* Fills the completion store from the contents of the current folder */
1362 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1364 discard_completion_store (chooser_entry);
1366 chooser_entry->completion_store = GTK_TREE_MODEL (
1367 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
1368 "standard::name,standard::display-name,standard::type",
1369 completion_store_set,
1374 g_signal_connect (chooser_entry->completion_store, "finished-loading",
1375 G_CALLBACK (finished_loading_cb), chooser_entry);
1377 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1378 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1380 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1381 chooser_entry->completion_store);
1384 /* When we finish loading the current folder, this function should get called to
1385 * perform the deferred autocompletion or explicit completion.
1388 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1390 switch (chooser_entry->load_complete_action)
1392 case LOAD_COMPLETE_NOTHING:
1395 case LOAD_COMPLETE_AUTOCOMPLETE:
1396 autocomplete (chooser_entry);
1399 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1400 explicitly_complete (chooser_entry);
1404 g_assert_not_reached ();
1407 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1411 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1413 perform_load_complete_action (chooser_entry);
1415 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1418 /* Callback when the current folder finishes loading */
1420 finished_loading_cb (GtkFileSystemModel *model,
1422 GtkFileChooserEntry *chooser_entry)
1424 chooser_entry->current_folder_loaded = TRUE;
1428 LoadCompleteAction old_load_complete_action;
1430 old_load_complete_action = chooser_entry->load_complete_action;
1432 discard_completion_store (chooser_entry);
1433 clear_completions (chooser_entry);
1435 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1437 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1439 beep (chooser_entry);
1440 pop_up_completion_feedback (chooser_entry, error->message);
1446 finish_folder_load (chooser_entry);
1449 static RefreshStatus
1450 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1453 g_assert (folder_file != NULL);
1455 if (chooser_entry->current_folder_file
1456 && g_file_equal (folder_file, chooser_entry->current_folder_file))
1459 if (chooser_entry->current_folder_file)
1461 discard_loading_and_current_folder_file (chooser_entry);
1464 if (chooser_entry->local_only
1465 && !g_file_is_native (folder_file))
1466 return REFRESH_NOT_LOCAL;
1468 chooser_entry->current_folder_file = g_object_ref (folder_file);
1469 chooser_entry->current_folder_loaded = FALSE;
1471 populate_completion_store (chooser_entry);
1476 static RefreshStatus
1477 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1478 RefreshMode refresh_mode)
1480 GtkEditable *editable;
1485 gsize total_len, file_part_len;
1488 RefreshStatus result;
1490 editable = GTK_EDITABLE (chooser_entry);
1492 switch (refresh_mode)
1494 case REFRESH_UP_TO_CURSOR_POSITION:
1495 end_pos = gtk_editable_get_position (editable);
1498 case REFRESH_WHOLE_TEXT:
1499 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1503 g_assert_not_reached ();
1504 return REFRESH_INVALID_INPUT;
1507 text = gtk_editable_get_chars (editable, 0, end_pos);
1510 if (!_gtk_file_system_parse (chooser_entry->file_system,
1511 chooser_entry->base_folder, text,
1512 &folder_file, &file_part, &error))
1514 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1517 result = REFRESH_INCOMPLETE_HOSTNAME;
1521 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1523 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1524 result = REFRESH_NONEXISTENT;
1526 result = REFRESH_INVALID_INPUT;
1530 g_error_free (error);
1532 file_part = g_strdup ("");
1537 g_assert (folder_file != NULL);
1539 file_part_len = strlen (file_part);
1540 total_len = strlen (text);
1541 if (total_len > file_part_len)
1542 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1546 result = REFRESH_OK;
1551 g_free (chooser_entry->file_part);
1553 chooser_entry->file_part = file_part;
1554 chooser_entry->file_part_pos = file_part_pos;
1556 if (result == REFRESH_OK)
1558 result = reload_current_folder (chooser_entry, folder_file);
1562 discard_loading_and_current_folder_file (chooser_entry);
1566 g_object_unref (folder_file);
1568 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1569 ((result == REFRESH_OK)
1570 && (chooser_entry->current_folder_file != NULL))
1571 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1572 || ((result != REFRESH_OK)
1573 && (chooser_entry->current_folder_file == NULL)));
1579 autocomplete (GtkFileChooserEntry *chooser_entry)
1581 if (!(chooser_entry->current_folder_loaded
1582 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1585 append_common_prefix (chooser_entry, TRUE, FALSE);
1589 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1591 RefreshStatus status;
1593 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1598 g_assert (chooser_entry->current_folder_file != NULL);
1600 if (chooser_entry->current_folder_loaded)
1601 autocomplete (chooser_entry);
1603 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1607 case REFRESH_INVALID_INPUT:
1608 case REFRESH_INCOMPLETE_HOSTNAME:
1609 case REFRESH_NONEXISTENT:
1610 case REFRESH_NOT_LOCAL:
1611 /* We don't beep or anything, since this is autocompletion - the user
1612 * didn't request any action explicitly.
1617 g_assert_not_reached ();
1622 start_autocompletion_idle_handler (gpointer data)
1624 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1626 start_autocompletion (chooser_entry);
1628 chooser_entry->start_autocompletion_idle_id = 0;
1634 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1636 if (chooser_entry->start_autocompletion_idle_id != 0)
1639 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1644 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1645 const gchar *new_text,
1646 gint new_text_length,
1650 const gchar *colon = memchr (new_text, ':', new_text_length);
1653 /* Disallow these characters altogether */
1654 for (i = 0; i < new_text_length; i++)
1656 if (new_text[i] == '<' ||
1657 new_text[i] == '>' ||
1658 new_text[i] == '"' ||
1659 new_text[i] == '|' ||
1660 new_text[i] == '*' ||
1665 if (i < new_text_length ||
1666 /* Disallow entering text that would cause a colon to be anywhere except
1667 * after a drive letter.
1670 *position + (colon - new_text) != 1) ||
1671 (new_text_length > 0 &&
1673 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1674 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1676 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1677 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1685 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1690 /* If deleting a drive letter, delete the colon, too */
1691 if (start_pos == 0 && end_pos == 1 &&
1692 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1693 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1695 g_signal_handlers_block_by_func (chooser_entry,
1696 G_CALLBACK (delete_text_callback),
1698 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1699 g_signal_handlers_unblock_by_func (chooser_entry,
1700 G_CALLBACK (delete_text_callback),
1707 * _gtk_file_chooser_entry_new:
1708 * @filesystem: The #GtkFileSystem to use
1709 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1711 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1712 * is an internal implementation widget for the GTK+ file chooser
1713 * which is an entry with completion with respect to a
1714 * #GtkFileSystem object.
1716 * Return value: the newly created #GtkFileChooserEntry
1719 _gtk_file_chooser_entry_new (GtkFileSystem *file_system,
1722 GtkFileChooserEntry *chooser_entry;
1724 g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
1726 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1727 chooser_entry->file_system = g_object_ref (file_system);
1728 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1730 return GTK_WIDGET (chooser_entry);
1734 * _gtk_file_chooser_entry_set_base_folder:
1735 * @chooser_entry: a #GtkFileChooserEntry
1736 * @file: file for a folder in the chooser entries current file system.
1738 * Sets the folder with respect to which completions occur.
1741 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1744 if (chooser_entry->base_folder)
1745 g_object_unref (chooser_entry->base_folder);
1747 chooser_entry->base_folder = file;
1749 if (chooser_entry->base_folder)
1750 g_object_ref (chooser_entry->base_folder);
1752 clear_completions (chooser_entry);
1756 * _gtk_file_chooser_entry_get_current_folder:
1757 * @chooser_entry: a #GtkFileChooserEntry
1759 * Gets the current folder for the #GtkFileChooserEntry. If the
1760 * user has only entered a filename, this will be in the base folder
1761 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1762 * user has entered a relative or absolute path, then it will
1763 * be different. If the user has entered unparsable text, or text which
1764 * the entry cannot handle, this will return %NULL.
1766 * Return value: the file for the current folder - this value is owned by the
1767 * chooser entry and must not be modified or freed.
1770 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1772 commit_completion_and_refresh (chooser_entry);
1773 return chooser_entry->current_folder_file;
1777 * _gtk_file_chooser_entry_get_file_part:
1778 * @chooser_entry: a #GtkFileChooserEntry
1780 * Gets the non-folder portion of whatever the user has entered
1781 * into the file selector. What is returned is a UTF-8 string,
1782 * and if a filename path is needed, g_file_get_child_for_display_name()
1785 * Return value: the entered filename - this value is owned by the
1786 * chooser entry and must not be modified or freed.
1789 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1791 commit_completion_and_refresh (chooser_entry);
1792 return chooser_entry->file_part;
1796 * _gtk_file_chooser_entry_set_file_part:
1797 * @chooser_entry: a #GtkFileChooserEntry
1798 * @file_part: text to display in the entry, in UTF-8
1800 * Sets the current text shown in the file chooser entry.
1803 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1804 const gchar *file_part)
1806 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1808 chooser_entry->in_change = TRUE;
1809 clear_completions (chooser_entry);
1810 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1811 chooser_entry->in_change = FALSE;
1816 * _gtk_file_chooser_entry_set_action:
1817 * @chooser_entry: a #GtkFileChooserEntry
1818 * @action: the action which is performed by the file selector using this entry
1820 * Sets action which is performed by the file selector using this entry.
1821 * The #GtkFileChooserEntry will use different completion strategies for
1822 * different actions.
1825 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1826 GtkFileChooserAction action)
1828 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1830 if (chooser_entry->action != action)
1832 GtkEntryCompletion *comp;
1834 chooser_entry->action = action;
1836 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1838 /* FIXME: do we need to actually set the following? */
1842 case GTK_FILE_CHOOSER_ACTION_OPEN:
1843 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1844 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1846 case GTK_FILE_CHOOSER_ACTION_SAVE:
1847 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1848 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1856 * _gtk_file_chooser_entry_get_action:
1857 * @chooser_entry: a #GtkFileChooserEntry
1859 * Gets the action for this entry.
1861 * Returns: the action
1863 GtkFileChooserAction
1864 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1866 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1867 GTK_FILE_CHOOSER_ACTION_OPEN);
1869 return chooser_entry->action;
1873 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1879 if (chooser_entry->completion_store == NULL ||
1880 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1885 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1888 return _gtk_file_info_consider_as_directory (info);
1893 * _gtk_file_chooser_entry_select_filename:
1894 * @chooser_entry: a #GtkFileChooserEntry
1896 * Selects the filename (without the extension) for user edition.
1899 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1901 const gchar *str, *ext;
1904 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1906 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1907 ext = g_strrstr (str, ".");
1910 len = g_utf8_pointer_to_offset (str, ext);
1913 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1917 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1918 gboolean local_only)
1920 chooser_entry->local_only = local_only;
1921 clear_completions (chooser_entry);
1925 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1927 return chooser_entry->local_only;