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 "gtkalignment.h"
28 #include "gtkcelllayout.h"
29 #include "gtkcellrenderertext.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 /* Folder being loaded or already loaded */
77 GtkFolder *current_folder;
78 GCancellable *load_folder_cancellable;
80 LoadCompleteAction load_complete_action;
82 GtkListStore *completion_store;
84 guint start_autocompletion_idle_id;
86 GtkWidget *completion_feedback_window;
87 GtkWidget *completion_feedback_label;
88 guint completion_feedback_timeout_id;
90 guint has_completion : 1;
103 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
105 static void gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface);
107 static void gtk_file_chooser_entry_finalize (GObject *object);
108 static void gtk_file_chooser_entry_dispose (GObject *object);
109 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
110 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
111 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
113 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
114 GdkEventFocus *event);
115 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
116 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
117 const gchar *new_text,
118 gint new_text_length,
120 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
123 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
125 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
130 static gint insert_text_callback (GtkFileChooserEntry *widget,
131 const gchar *new_text,
132 gint new_text_length,
135 static void delete_text_callback (GtkFileChooserEntry *widget,
141 static gboolean match_selected_callback (GtkEntryCompletion *completion,
144 GtkFileChooserEntry *chooser_entry);
145 static gboolean completion_match_func (GtkEntryCompletion *comp,
149 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
155 REFRESH_UP_TO_CURSOR_POSITION,
159 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
160 RefreshMode refresh_mode);
161 static void finished_loading_cb (GtkFolder *folder,
163 static void autocomplete (GtkFileChooserEntry *chooser_entry);
164 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
165 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
166 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
167 const gchar *feedback);
169 static GtkEditableInterface *parent_editable_iface;
171 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
172 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
173 gtk_file_chooser_entry_iface_init))
176 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
180 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
182 gobject_class->finalize = gtk_file_chooser_entry_finalize;
183 gobject_class->dispose = gtk_file_chooser_entry_dispose;
185 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
186 widget_class->unmap = gtk_file_chooser_entry_unmap;
187 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
188 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
190 entry_class->activate = gtk_file_chooser_entry_activate;
194 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
196 parent_editable_iface = g_type_interface_peek_parent (iface);
198 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
199 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
200 iface->set_position = gtk_file_chooser_entry_set_position;
201 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
205 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
207 GtkEntryCompletion *comp;
208 GtkCellRenderer *cell;
210 chooser_entry->local_only = TRUE;
212 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
214 comp = gtk_entry_completion_new ();
215 gtk_entry_completion_set_popup_single_match (comp, FALSE);
217 gtk_entry_completion_set_match_func (comp,
218 completion_match_func,
222 cell = gtk_cell_renderer_text_new ();
223 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
225 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
229 g_signal_connect (comp, "match-selected",
230 G_CALLBACK (match_selected_callback), chooser_entry);
232 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
233 g_object_unref (comp);
236 g_signal_connect (chooser_entry, "insert-text",
237 G_CALLBACK (insert_text_callback), NULL);
238 g_signal_connect (chooser_entry, "delete-text",
239 G_CALLBACK (delete_text_callback), NULL);
244 gtk_file_chooser_entry_finalize (GObject *object)
246 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
248 if (chooser_entry->base_folder)
249 g_object_unref (chooser_entry->base_folder);
251 if (chooser_entry->current_folder)
252 g_object_unref (chooser_entry->current_folder);
254 if (chooser_entry->current_folder_file)
255 g_object_unref (chooser_entry->current_folder_file);
257 g_free (chooser_entry->file_part);
259 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
263 discard_current_folder (GtkFileChooserEntry *chooser_entry)
265 if (chooser_entry->current_folder)
267 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
268 G_CALLBACK (finished_loading_cb), chooser_entry);
269 g_object_unref (chooser_entry->current_folder);
270 chooser_entry->current_folder = NULL;
275 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
277 if (chooser_entry->load_folder_cancellable)
279 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
280 chooser_entry->load_folder_cancellable = NULL;
283 if (chooser_entry->current_folder_file)
285 g_object_unref (chooser_entry->current_folder_file);
286 chooser_entry->current_folder_file = NULL;
291 gtk_file_chooser_entry_dispose (GObject *object)
293 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
295 remove_completion_feedback (chooser_entry);
296 discard_current_folder (chooser_entry);
297 discard_loading_and_current_folder_file (chooser_entry);
299 if (chooser_entry->start_autocompletion_idle_id != 0)
301 g_source_remove (chooser_entry->start_autocompletion_idle_id);
302 chooser_entry->start_autocompletion_idle_id = 0;
305 if (chooser_entry->completion_store)
307 g_object_unref (chooser_entry->completion_store);
308 chooser_entry->completion_store = NULL;
311 if (chooser_entry->file_system)
313 g_object_unref (chooser_entry->file_system);
314 chooser_entry->file_system = NULL;
317 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
320 /* Match functions for the GtkEntryCompletion */
322 match_selected_callback (GtkEntryCompletion *completion,
325 GtkFileChooserEntry *chooser_entry)
332 gtk_tree_model_get (model, iter,
333 DISPLAY_NAME_COLUMN, &display_name,
337 if (!display_name || !file)
340 g_object_unref (file);
342 g_free (display_name);
346 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
348 pos = chooser_entry->file_part_pos;
350 /* We don't set in_change here as we want to update the current_folder
352 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
354 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
357 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
359 g_object_unref (file);
360 g_free (display_name);
365 /* Match function for the GtkEntryCompletion */
367 completion_match_func (GtkEntryCompletion *comp,
368 const char *key_unused,
372 GtkFileChooserEntry *chooser_entry;
375 char *norm_file_part;
378 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
380 /* We ignore the key because it is the contents of the entry. Instead, we
381 * just use our precomputed file_part.
383 if (!chooser_entry->file_part)
388 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
391 return FALSE; /* Uninitialized row, ugh */
394 /* If we have an empty file_part, then we're at the root of a directory. In
395 * that case, we want to match all non-dot files. We might want to match
396 * dot_files too if show_hidden is TRUE on the fileselector in the future.
398 /* Additionally, support for gnome .hidden files would be sweet, too */
399 if (chooser_entry->file_part[0] == '\000')
411 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
412 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
414 #ifdef G_PLATFORM_WIN32
418 temp = norm_file_part;
419 norm_file_part = g_utf8_casefold (norm_file_part, -1);
423 norm_name = g_utf8_casefold (norm_name, -1);
428 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
430 g_free (norm_file_part);
438 clear_completions (GtkFileChooserEntry *chooser_entry)
440 chooser_entry->has_completion = FALSE;
441 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
443 remove_completion_feedback (chooser_entry);
447 beep (GtkFileChooserEntry *chooser_entry)
449 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
452 /* This function will append a directory separator to paths to
453 * display_name iff the path associated with it is a directory.
454 * maybe_append_separator_to_file will g_free the display_name and
455 * return a new one if needed. Otherwise, it will return the old one.
456 * You should be safe calling
458 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
460 * g_free (display_name);
463 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
470 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
474 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
478 if (_gtk_file_info_consider_as_directory (info))
480 gchar *tmp = display_name;
481 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
486 g_object_unref (info);
494 trim_dir_separator_suffix (const char *str)
499 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
500 return g_strndup (str, len - 1);
502 return g_strdup (str);
505 /* Determines if the completion model has entries with a common prefix relative
506 * to the current contents of the entry. Also, if there's one and only one such
507 * path, stores it in unique_path_ret.
510 find_common_prefix (GtkFileChooserEntry *chooser_entry,
511 gchar **common_prefix_ret,
512 GFile **unique_file_ret,
513 gboolean *is_complete_not_unique_ret,
514 gboolean *prefix_expands_the_file_part_ret,
517 GtkEditable *editable;
521 char *text_up_to_cursor;
522 GFile *parsed_folder_file;
523 char *parsed_file_part;
525 *common_prefix_ret = NULL;
526 *unique_file_ret = NULL;
527 *is_complete_not_unique_ret = FALSE;
528 *prefix_expands_the_file_part_ret = FALSE;
530 editable = GTK_EDITABLE (chooser_entry);
532 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
534 parsed = _gtk_file_system_parse (chooser_entry->file_system,
535 chooser_entry->base_folder,
541 g_free (text_up_to_cursor);
546 g_assert (parsed_folder_file != NULL
547 && chooser_entry->current_folder != NULL
548 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
550 g_object_unref (parsed_folder_file);
552 /* First pass: find the common prefix */
554 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
561 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
563 DISPLAY_NAME_COLUMN, &display_name,
567 if (g_str_has_prefix (display_name, parsed_file_part))
569 if (!*common_prefix_ret)
571 *common_prefix_ret = trim_dir_separator_suffix (display_name);
572 *unique_file_ret = g_object_ref (file);
576 gchar *p = *common_prefix_ret;
577 const gchar *q = display_name;
579 while (*p && *p == *q)
587 if (*unique_file_ret)
589 g_object_unref (*unique_file_ret);
590 *unique_file_ret = NULL;
595 g_free (display_name);
596 g_object_unref (file);
597 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
600 /* Second pass: see if the prefix we found is a complete match */
602 if (*common_prefix_ret != NULL)
604 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
611 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
613 DISPLAY_NAME_COLUMN, &display_name,
615 len = strlen (display_name);
618 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
621 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
622 *is_complete_not_unique_ret = TRUE;
624 g_free (display_name);
625 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
628 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
630 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
633 g_free (parsed_file_part);
639 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
646 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
647 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
651 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
652 if (G_IS_DIR_SEPARATOR (*next_char_str))
655 g_free (next_char_str);
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 /* If the user types an incomplete hostname ("http://foo" without
696 * a slash after that), it's not an error. We just don't want to
697 * pop up a meaningless completion window in that state.
699 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
702 beep (chooser_entry);
703 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
706 g_error_free (error);
708 return INVALID_INPUT;
715 if (!char_after_cursor_is_directory_separator (chooser_entry))
719 common_prefix = maybe_append_separator_to_file (chooser_entry,
724 prefix_expands_the_file_part = TRUE;
727 g_object_unref (unique_file);
729 if (prefix_expands_the_file_part)
730 result = COMPLETED_UNIQUE;
732 result = NOTHING_INSERTED_UNIQUE;
738 if (is_complete_not_unique)
740 result = COMPLETE_BUT_NOT_UNIQUE;
750 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
752 pos = chooser_entry->file_part_pos;
754 if (prefix_expands_the_file_part)
756 chooser_entry->in_change = TRUE;
757 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
759 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
762 chooser_entry->in_change = FALSE;
766 /* equivalent to cursor_pos + common_prefix_len); */
767 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
770 chooser_entry->has_completion = TRUE;
773 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
775 else if (!have_result)
777 result = NOTHING_INSERTED_COMPLETE;
781 g_free (common_prefix);
798 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
799 const gchar *new_text,
800 gint new_text_length,
803 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
807 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
808 insert_pos = *position;
810 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
812 if (chooser_entry->in_change)
815 remove_completion_feedback (chooser_entry);
817 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
818 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
819 && insert_pos == old_text_len)
820 install_start_autocompletion_idle (chooser_entry);
824 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
826 if (chooser_entry->in_change)
829 clear_completions (chooser_entry);
833 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
837 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
839 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
841 clear_completions_if_not_in_change (chooser_entry);
845 gtk_file_chooser_entry_set_position (GtkEditable *editable,
848 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
850 parent_editable_iface->set_position (editable, position);
852 clear_completions_if_not_in_change (chooser_entry);
856 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
860 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
862 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
864 clear_completions_if_not_in_change (chooser_entry);
868 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
870 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
871 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
875 gtk_file_chooser_entry_unmap (GtkWidget *widget)
877 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
879 remove_completion_feedback (chooser_entry);
881 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
885 completion_feedback_window_draw_cb (GtkWidget *widget,
889 /* Stolen from gtk_tooltip_paint_window() */
891 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
892 GtkStyleContext *context;
894 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
896 gtk_render_background (context, cr, 0, 0,
897 gtk_widget_get_allocated_width (widget),
898 gtk_widget_get_allocated_height (widget));
899 gtk_render_frame (context, cr, 0, 0,
900 gtk_widget_get_allocated_width (widget),
901 gtk_widget_get_allocated_height (widget));
907 set_invisible_mouse_cursor (GdkWindow *window)
912 display = gdk_window_get_display (window);
913 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
915 gdk_window_set_cursor (window, cursor);
917 g_object_unref (cursor);
921 completion_feedback_window_realize_cb (GtkWidget *widget,
924 /* We hide the mouse cursor inside the completion feedback window, since
925 * GtkEntry hides the cursor when the user types. We don't want the cursor to
926 * come back if the completion feedback ends up where the mouse is.
928 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
932 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
934 /* Stolen from gtk_tooltip_init() */
936 GtkStyleContext *context;
937 GtkWidget *alignment;
938 GtkBorder padding, border;
940 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
941 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
942 GDK_WINDOW_TYPE_HINT_TOOLTIP);
943 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
944 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
945 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
947 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
948 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
949 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
951 gtk_style_context_get_padding (context, 0, &padding);
952 gtk_style_context_get_border (context, 0, &border);
954 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
955 border.top + padding.top,
956 border.bottom + padding.bottom,
957 border.left + padding.left,
958 border.right + padding.right);
959 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
960 gtk_widget_show (alignment);
962 g_signal_connect (chooser_entry->completion_feedback_window, "draw",
963 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
964 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
965 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
966 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
968 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
969 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
970 gtk_widget_show (chooser_entry->completion_feedback_label);
974 completion_feedback_timeout_cb (gpointer data)
976 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
978 chooser_entry->completion_feedback_timeout_id = 0;
980 remove_completion_feedback (chooser_entry);
985 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
987 if (chooser_entry->completion_feedback_timeout_id != 0)
988 g_source_remove (chooser_entry->completion_feedback_timeout_id);
990 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
991 completion_feedback_timeout_cb,
995 /* Gets the x position of the text cursor in the entry, in widget coordinates */
997 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
1000 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
1001 * the reader, you have to implement support for the entry's scroll offset and
1002 * RTL layouts and all the fancy Pango stuff.
1005 PangoLayout *layout;
1006 gint layout_x, layout_y;
1008 PangoRectangle strong_pos;
1009 gint start_pos, end_pos;
1011 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1013 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1015 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
1016 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1020 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1022 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1026 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1028 /* More or less stolen from gtk_tooltip_position() */
1030 GtkRequisition feedback_req;
1031 GtkWidget *widget = GTK_WIDGET (chooser_entry);
1032 gint entry_x, entry_y;
1034 GtkAllocation entry_allocation;
1035 int feedback_x, feedback_y;
1037 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
1038 &feedback_req, NULL);
1040 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1041 gtk_widget_get_allocation (widget, &entry_allocation);
1043 get_entry_cursor_x (chooser_entry, &cursor_x);
1045 /* FIXME: fit to the screen if we bump on the screen's edge */
1046 /* cheap "half M-width", use height as approximation of character em-size */
1047 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1048 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1050 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1051 gtk_widget_show (chooser_entry->completion_feedback_window);
1053 install_completion_feedback_timer (chooser_entry);
1057 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1058 const gchar *feedback)
1060 if (chooser_entry->completion_feedback_window == NULL)
1061 create_completion_feedback_window (chooser_entry);
1063 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1065 show_completion_feedback_window (chooser_entry);
1069 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1071 if (chooser_entry->completion_feedback_window)
1072 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1074 chooser_entry->completion_feedback_window = NULL;
1075 chooser_entry->completion_feedback_label = NULL;
1077 if (chooser_entry->completion_feedback_timeout_id != 0)
1079 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1080 chooser_entry->completion_feedback_timeout_id = 0;
1085 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1087 CommonPrefixResult result;
1089 g_assert (chooser_entry->current_folder != NULL);
1090 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1092 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1094 * - If there is a common prefix, insert it (done)
1095 * - If there is no common prefix, pop up the suggestion window
1096 * - If there are no matches at all, beep and bring up a tooltip (done)
1097 * - If the suggestion window is already up, scroll it
1099 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1104 /* We already beeped in append_common_prefix(); do nothing here */
1108 beep (chooser_entry);
1109 /* translators: this text is shown when there are no completions
1110 * for something the user typed in a file chooser entry
1112 pop_up_completion_feedback (chooser_entry, _("No match"));
1115 case NOTHING_INSERTED_COMPLETE:
1116 /* FIXME: pop up the suggestion window or scroll it */
1119 case NOTHING_INSERTED_UNIQUE:
1120 /* translators: this text is shown when there is exactly one completion
1121 * for something the user typed in a file chooser entry
1123 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1130 case COMPLETED_UNIQUE:
1134 case COMPLETE_BUT_NOT_UNIQUE:
1135 /* translators: this text is shown when the text in a file chooser
1136 * entry is a complete filename, but could be continued to find
1139 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1143 g_assert_not_reached ();
1148 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1150 RefreshStatus status;
1154 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1161 g_assert (chooser_entry->current_folder_file != NULL);
1163 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1164 explicitly_complete (chooser_entry);
1167 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1169 /* Translators: this text is shown while the system is searching
1170 * for possible completions for filenames in a file chooser entry. */
1171 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1176 case REFRESH_INVALID_INPUT:
1178 /* Translators: this is shown in the feedback for Tab-completion in a file
1179 * chooser's text entry, when the user enters an invalid path. */
1180 feedback_msg = _("Invalid path");
1183 case REFRESH_INCOMPLETE_HOSTNAME:
1186 if (chooser_entry->local_only)
1188 /* hostnames in a local_only file chooser? user error */
1190 /* Translators: this is shown in the feedback for Tab-completion in a
1191 * file chooser's text entry when the user enters something like
1192 * "sftp://blahblah" in an app that only supports local filenames. */
1193 feedback_msg = _("Only local files may be selected");
1197 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1199 /* Translators: this is shown in the feedback for Tab-completion in a
1200 * file chooser's text entry when the user hasn't entered the first '/'
1201 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1202 feedback_msg = _("Incomplete hostname; end it with '/'");
1207 case REFRESH_NONEXISTENT:
1210 /* Translators: this is shown in the feedback for Tab-completion in a file
1211 * chooser's text entry when the user enters a path that does not exist
1212 * and then hits Tab */
1213 feedback_msg = _("Path does not exist");
1216 case REFRESH_NOT_LOCAL:
1218 feedback_msg = _("Only local files may be selected");
1222 g_assert_not_reached ();
1228 g_assert (chooser_entry->current_folder_file == NULL);
1230 beep (chooser_entry);
1231 pop_up_completion_feedback (chooser_entry, feedback_msg);
1232 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1237 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1240 GtkFileChooserEntry *chooser_entry;
1241 GtkEditable *editable;
1243 GdkModifierType state;
1244 gboolean control_pressed;
1246 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1247 editable = GTK_EDITABLE (widget);
1248 entry = GTK_ENTRY (widget);
1250 if (!chooser_entry->eat_tabs)
1251 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1253 control_pressed = FALSE;
1255 if (gtk_get_current_event_state (&state))
1257 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1258 control_pressed = TRUE;
1261 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1262 * makes it 'safe' for people to hit. */
1263 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1265 if (chooser_entry->has_completion)
1266 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1268 start_explicit_completion (chooser_entry);
1273 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1278 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1279 GdkEventFocus *event)
1281 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1283 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1285 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1289 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1291 if (chooser_entry->has_completion)
1293 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1294 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1297 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1298 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1302 gtk_file_chooser_entry_activate (GtkEntry *entry)
1304 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1306 commit_completion_and_refresh (chooser_entry);
1307 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1311 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1313 if (!chooser_entry->completion_store)
1316 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1317 g_object_unref (chooser_entry->completion_store);
1318 chooser_entry->completion_store = NULL;
1321 /* Fills the completion store from the contents of the current folder */
1323 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1328 discard_completion_store (chooser_entry);
1330 files = _gtk_folder_list_children (chooser_entry->current_folder);
1332 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1336 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1341 file = tmp_list->data;
1343 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1347 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1351 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1353 gtk_list_store_append (chooser_entry->completion_store, &iter);
1354 gtk_list_store_set (chooser_entry->completion_store, &iter,
1355 DISPLAY_NAME_COLUMN, display_name,
1359 g_object_unref (info);
1360 g_free (display_name);
1364 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1365 g_slist_free (files);
1367 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1368 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1370 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1371 GTK_TREE_MODEL (chooser_entry->completion_store));
1374 /* When we finish loading the current folder, this function should get called to
1375 * perform the deferred autocompletion or explicit completion.
1378 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1380 switch (chooser_entry->load_complete_action)
1382 case LOAD_COMPLETE_NOTHING:
1385 case LOAD_COMPLETE_AUTOCOMPLETE:
1386 autocomplete (chooser_entry);
1389 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1390 explicitly_complete (chooser_entry);
1394 g_assert_not_reached ();
1397 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1401 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1403 populate_completion_store (chooser_entry);
1404 perform_load_complete_action (chooser_entry);
1406 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1409 /* Callback when the current folder finishes loading */
1411 finished_loading_cb (GtkFolder *folder,
1414 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1416 finish_folder_load (chooser_entry);
1419 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1421 load_directory_get_folder_callback (GCancellable *cancellable,
1423 const GError *error,
1426 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1427 GtkFileChooserEntry *chooser_entry = data;
1429 if (cancellable != chooser_entry->load_folder_cancellable)
1432 chooser_entry->load_folder_cancellable = NULL;
1436 LoadCompleteAction old_load_complete_action;
1438 old_load_complete_action = chooser_entry->load_complete_action;
1440 discard_completion_store (chooser_entry);
1441 clear_completions (chooser_entry);
1443 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1445 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1447 beep (chooser_entry);
1448 pop_up_completion_feedback (chooser_entry, error->message);
1451 discard_current_folder (chooser_entry);
1454 if (cancelled || error)
1457 g_assert (folder != NULL);
1458 chooser_entry->current_folder = g_object_ref (folder);
1460 discard_completion_store (chooser_entry);
1462 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1463 finish_folder_load (chooser_entry);
1465 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1466 G_CALLBACK (finished_loading_cb), chooser_entry);
1469 g_object_unref (chooser_entry);
1470 g_object_unref (cancellable);
1473 static RefreshStatus
1474 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1476 if (chooser_entry->file_system == NULL)
1479 g_assert (chooser_entry->current_folder_file != NULL);
1480 g_assert (chooser_entry->current_folder == NULL);
1481 g_assert (chooser_entry->load_folder_cancellable == NULL);
1483 if (chooser_entry->local_only
1484 && !g_file_is_native (chooser_entry->current_folder_file))
1486 g_object_unref (chooser_entry->current_folder_file);
1487 chooser_entry->current_folder_file = NULL;
1489 return REFRESH_NOT_LOCAL;
1492 chooser_entry->load_folder_cancellable =
1493 _gtk_file_system_get_folder (chooser_entry->file_system,
1494 chooser_entry->current_folder_file,
1495 "standard::name,standard::display-name,standard::type",
1496 load_directory_get_folder_callback,
1497 g_object_ref (chooser_entry));
1502 static RefreshStatus
1503 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1505 gboolean force_reload)
1507 gboolean reload = FALSE;
1509 g_assert (folder_file != NULL);
1511 if (chooser_entry->current_folder_file)
1513 if ((!(g_file_equal (folder_file, chooser_entry->current_folder_file)
1514 && chooser_entry->load_folder_cancellable))
1519 discard_current_folder (chooser_entry);
1520 discard_loading_and_current_folder_file (chooser_entry);
1522 chooser_entry->current_folder_file = g_object_ref (folder_file);
1527 chooser_entry->current_folder_file = g_object_ref (folder_file);
1532 return start_loading_current_folder (chooser_entry);
1537 static RefreshStatus
1538 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1539 RefreshMode refresh_mode)
1541 GtkEditable *editable;
1546 gsize total_len, file_part_len;
1549 RefreshStatus result;
1551 editable = GTK_EDITABLE (chooser_entry);
1553 switch (refresh_mode)
1555 case REFRESH_UP_TO_CURSOR_POSITION:
1556 end_pos = gtk_editable_get_position (editable);
1559 case REFRESH_WHOLE_TEXT:
1560 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1564 g_assert_not_reached ();
1565 return REFRESH_INVALID_INPUT;
1568 text = gtk_editable_get_chars (editable, 0, end_pos);
1571 if (!chooser_entry->file_system ||
1572 !chooser_entry->base_folder ||
1573 !_gtk_file_system_parse (chooser_entry->file_system,
1574 chooser_entry->base_folder, text,
1575 &folder_file, &file_part, &error))
1577 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1580 result = REFRESH_INCOMPLETE_HOSTNAME;
1584 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1586 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1587 result = REFRESH_NONEXISTENT;
1589 result = REFRESH_INVALID_INPUT;
1593 g_error_free (error);
1595 file_part = g_strdup ("");
1600 g_assert (folder_file != NULL);
1602 file_part_len = strlen (file_part);
1603 total_len = strlen (text);
1604 if (total_len > file_part_len)
1605 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1609 result = REFRESH_OK;
1614 g_free (chooser_entry->file_part);
1616 chooser_entry->file_part = file_part;
1617 chooser_entry->file_part_pos = file_part_pos;
1619 if (result == REFRESH_OK)
1621 result = reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1625 discard_current_folder (chooser_entry);
1626 discard_loading_and_current_folder_file (chooser_entry);
1630 g_object_unref (folder_file);
1632 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1633 ((result == REFRESH_OK)
1634 && (chooser_entry->current_folder_file != NULL)
1635 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1636 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1637 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1638 || ((result != REFRESH_OK)
1639 && (chooser_entry->current_folder_file == NULL)
1640 && (chooser_entry->load_folder_cancellable == NULL)
1641 && (chooser_entry->current_folder == NULL)));
1647 autocomplete (GtkFileChooserEntry *chooser_entry)
1649 if (!(chooser_entry->current_folder != NULL
1650 && _gtk_folder_is_finished_loading (chooser_entry->current_folder)
1651 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1654 append_common_prefix (chooser_entry, TRUE, FALSE);
1658 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1660 RefreshStatus status;
1662 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1667 g_assert (chooser_entry->current_folder_file != NULL);
1669 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1670 autocomplete (chooser_entry);
1672 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1676 case REFRESH_INVALID_INPUT:
1677 case REFRESH_INCOMPLETE_HOSTNAME:
1678 case REFRESH_NONEXISTENT:
1679 case REFRESH_NOT_LOCAL:
1680 /* We don't beep or anything, since this is autocompletion - the user
1681 * didn't request any action explicitly.
1686 g_assert_not_reached ();
1691 start_autocompletion_idle_handler (gpointer data)
1693 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1695 start_autocompletion (chooser_entry);
1697 chooser_entry->start_autocompletion_idle_id = 0;
1703 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1705 if (chooser_entry->start_autocompletion_idle_id != 0)
1708 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1713 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1714 const gchar *new_text,
1715 gint new_text_length,
1719 const gchar *colon = memchr (new_text, ':', new_text_length);
1722 /* Disallow these characters altogether */
1723 for (i = 0; i < new_text_length; i++)
1725 if (new_text[i] == '<' ||
1726 new_text[i] == '>' ||
1727 new_text[i] == '"' ||
1728 new_text[i] == '|' ||
1729 new_text[i] == '*' ||
1734 if (i < new_text_length ||
1735 /* Disallow entering text that would cause a colon to be anywhere except
1736 * after a drive letter.
1739 *position + (colon - new_text) != 1) ||
1740 (new_text_length > 0 &&
1742 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1743 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1745 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1746 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1754 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1759 /* If deleting a drive letter, delete the colon, too */
1760 if (start_pos == 0 && end_pos == 1 &&
1761 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1762 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1764 g_signal_handlers_block_by_func (chooser_entry,
1765 G_CALLBACK (delete_text_callback),
1767 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1768 g_signal_handlers_unblock_by_func (chooser_entry,
1769 G_CALLBACK (delete_text_callback),
1776 * _gtk_file_chooser_entry_new:
1777 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1779 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1780 * is an internal implementation widget for the GTK+ file chooser
1781 * which is an entry with completion with respect to a
1782 * #GtkFileSystem object.
1784 * Return value: the newly created #GtkFileChooserEntry
1787 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1789 GtkFileChooserEntry *chooser_entry;
1791 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1792 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1794 return GTK_WIDGET (chooser_entry);
1798 * _gtk_file_chooser_entry_set_file_system:
1799 * @chooser_entry: a #GtkFileChooser
1800 * @file_system: an object implementing #GtkFileSystem
1802 * Sets the file system for @chooser_entry.
1805 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1806 GtkFileSystem *file_system)
1808 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1809 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1811 if (file_system != chooser_entry->file_system)
1813 if (chooser_entry->file_system)
1814 g_object_unref (chooser_entry->file_system);
1816 chooser_entry->file_system = g_object_ref (file_system);
1821 * _gtk_file_chooser_entry_set_base_folder:
1822 * @chooser_entry: a #GtkFileChooserEntry
1823 * @file: file for a folder in the chooser entries current file system.
1825 * Sets the folder with respect to which completions occur.
1828 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1831 if (chooser_entry->base_folder)
1832 g_object_unref (chooser_entry->base_folder);
1834 chooser_entry->base_folder = file;
1836 if (chooser_entry->base_folder)
1837 g_object_ref (chooser_entry->base_folder);
1839 clear_completions (chooser_entry);
1840 _gtk_file_chooser_entry_select_filename (chooser_entry);
1844 * _gtk_file_chooser_entry_get_current_folder:
1845 * @chooser_entry: a #GtkFileChooserEntry
1847 * Gets the current folder for the #GtkFileChooserEntry. If the
1848 * user has only entered a filename, this will be in the base folder
1849 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1850 * user has entered a relative or absolute path, then it will
1851 * be different. If the user has entered unparsable text, or text which
1852 * the entry cannot handle, this will return %NULL.
1854 * Return value: the file for the current folder - this value is owned by the
1855 * chooser entry and must not be modified or freed.
1858 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1860 commit_completion_and_refresh (chooser_entry);
1861 return chooser_entry->current_folder_file;
1865 * _gtk_file_chooser_entry_get_file_part:
1866 * @chooser_entry: a #GtkFileChooserEntry
1868 * Gets the non-folder portion of whatever the user has entered
1869 * into the file selector. What is returned is a UTF-8 string,
1870 * and if a filename path is needed, g_file_get_child_for_display_name()
1873 * Return value: the entered filename - this value is owned by the
1874 * chooser entry and must not be modified or freed.
1877 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1879 commit_completion_and_refresh (chooser_entry);
1880 return chooser_entry->file_part;
1884 * _gtk_file_chooser_entry_set_file_part:
1885 * @chooser_entry: a #GtkFileChooserEntry
1886 * @file_part: text to display in the entry, in UTF-8
1888 * Sets the current text shown in the file chooser entry.
1891 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1892 const gchar *file_part)
1894 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1896 chooser_entry->in_change = TRUE;
1897 clear_completions (chooser_entry);
1898 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1899 chooser_entry->in_change = FALSE;
1904 * _gtk_file_chooser_entry_set_action:
1905 * @chooser_entry: a #GtkFileChooserEntry
1906 * @action: the action which is performed by the file selector using this entry
1908 * Sets action which is performed by the file selector using this entry.
1909 * The #GtkFileChooserEntry will use different completion strategies for
1910 * different actions.
1913 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1914 GtkFileChooserAction action)
1916 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1918 if (chooser_entry->action != action)
1920 GtkEntryCompletion *comp;
1922 chooser_entry->action = action;
1924 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1926 /* FIXME: do we need to actually set the following? */
1930 case GTK_FILE_CHOOSER_ACTION_OPEN:
1931 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1932 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1934 case GTK_FILE_CHOOSER_ACTION_SAVE:
1935 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1936 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1944 * _gtk_file_chooser_entry_get_action:
1945 * @chooser_entry: a #GtkFileChooserEntry
1947 * Gets the action for this entry.
1949 * Returns: the action
1951 GtkFileChooserAction
1952 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1954 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1955 GTK_FILE_CHOOSER_ACTION_OPEN);
1957 return chooser_entry->action;
1961 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1964 gboolean retval = FALSE;
1966 if (chooser_entry->current_folder)
1968 GFileInfo *file_info;
1970 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1974 retval = _gtk_file_info_consider_as_directory (file_info);
1975 g_object_unref (file_info);
1984 * _gtk_file_chooser_entry_select_filename:
1985 * @chooser_entry: a #GtkFileChooserEntry
1987 * Selects the filename (without the extension) for user edition.
1990 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1992 const gchar *str, *ext;
1995 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1997 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1998 ext = g_strrstr (str, ".");
2001 len = g_utf8_pointer_to_offset (str, ext);
2004 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
2008 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
2009 gboolean local_only)
2011 chooser_entry->local_only = local_only;
2012 clear_completions (chooser_entry);
2016 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
2018 return chooser_entry->local_only;