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 #include "gdkkeysyms.h"
39 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
41 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
42 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
43 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
45 struct _GtkFileChooserEntryClass
47 GtkEntryClass parent_class;
50 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
52 LOAD_COMPLETE_NOTHING,
53 LOAD_COMPLETE_AUTOCOMPLETE,
54 LOAD_COMPLETE_EXPLICIT_COMPLETION
60 REFRESH_INVALID_INPUT,
61 REFRESH_INCOMPLETE_HOSTNAME,
66 struct _GtkFileChooserEntry
68 GtkEntry parent_instance;
70 GtkFileChooserAction action;
72 GtkFileSystem *file_system;
74 GFile *current_folder_file;
78 /* Folder being loaded or already loaded */
79 GtkFolder *current_folder;
80 GCancellable *load_folder_cancellable;
82 LoadCompleteAction load_complete_action;
84 GtkListStore *completion_store;
86 guint start_autocompletion_idle_id;
88 GtkWidget *completion_feedback_window;
89 GtkWidget *completion_feedback_label;
90 guint completion_feedback_timeout_id;
92 guint has_completion : 1;
105 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
107 static void gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface);
109 static void gtk_file_chooser_entry_finalize (GObject *object);
110 static void gtk_file_chooser_entry_dispose (GObject *object);
111 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
112 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
113 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
115 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
116 GdkEventFocus *event);
117 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
118 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
119 const gchar *new_text,
120 gint new_text_length,
122 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
125 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
127 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
132 static gint insert_text_callback (GtkFileChooserEntry *widget,
133 const gchar *new_text,
134 gint new_text_length,
137 static void delete_text_callback (GtkFileChooserEntry *widget,
143 static gboolean match_selected_callback (GtkEntryCompletion *completion,
146 GtkFileChooserEntry *chooser_entry);
147 static gboolean completion_match_func (GtkEntryCompletion *comp,
151 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
157 REFRESH_UP_TO_CURSOR_POSITION,
161 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
162 RefreshMode refresh_mode);
163 static void finished_loading_cb (GtkFolder *folder,
165 static void autocomplete (GtkFileChooserEntry *chooser_entry);
166 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
167 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
168 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
169 const gchar *feedback);
171 static GtkEditableInterface *parent_editable_iface;
173 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
174 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
175 gtk_file_chooser_entry_iface_init))
178 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
180 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
181 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
182 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
184 gobject_class->finalize = gtk_file_chooser_entry_finalize;
185 gobject_class->dispose = gtk_file_chooser_entry_dispose;
187 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
188 widget_class->unmap = gtk_file_chooser_entry_unmap;
189 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
190 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
192 entry_class->activate = gtk_file_chooser_entry_activate;
196 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
198 parent_editable_iface = g_type_interface_peek_parent (iface);
200 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
201 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
202 iface->set_position = gtk_file_chooser_entry_set_position;
203 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
207 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
209 GtkEntryCompletion *comp;
210 GtkCellRenderer *cell;
212 chooser_entry->local_only = TRUE;
214 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
216 comp = gtk_entry_completion_new ();
217 gtk_entry_completion_set_popup_single_match (comp, FALSE);
219 gtk_entry_completion_set_match_func (comp,
220 completion_match_func,
224 cell = gtk_cell_renderer_text_new ();
225 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
227 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
231 g_signal_connect (comp, "match-selected",
232 G_CALLBACK (match_selected_callback), chooser_entry);
234 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
235 g_object_unref (comp);
238 g_signal_connect (chooser_entry, "insert-text",
239 G_CALLBACK (insert_text_callback), NULL);
240 g_signal_connect (chooser_entry, "delete-text",
241 G_CALLBACK (delete_text_callback), NULL);
246 gtk_file_chooser_entry_finalize (GObject *object)
248 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
250 if (chooser_entry->base_folder)
251 g_object_unref (chooser_entry->base_folder);
253 if (chooser_entry->current_folder)
254 g_object_unref (chooser_entry->current_folder);
256 if (chooser_entry->current_folder_file)
257 g_object_unref (chooser_entry->current_folder_file);
259 g_free (chooser_entry->file_part);
261 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
265 discard_current_folder (GtkFileChooserEntry *chooser_entry)
267 if (chooser_entry->current_folder)
269 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
270 G_CALLBACK (finished_loading_cb), chooser_entry);
271 g_object_unref (chooser_entry->current_folder);
272 chooser_entry->current_folder = NULL;
277 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
279 if (chooser_entry->load_folder_cancellable)
281 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
282 chooser_entry->load_folder_cancellable = NULL;
285 if (chooser_entry->current_folder_file)
287 g_object_unref (chooser_entry->current_folder_file);
288 chooser_entry->current_folder_file = NULL;
293 gtk_file_chooser_entry_dispose (GObject *object)
295 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
297 remove_completion_feedback (chooser_entry);
298 discard_current_folder (chooser_entry);
299 discard_loading_and_current_folder_file (chooser_entry);
301 if (chooser_entry->start_autocompletion_idle_id != 0)
303 g_source_remove (chooser_entry->start_autocompletion_idle_id);
304 chooser_entry->start_autocompletion_idle_id = 0;
307 if (chooser_entry->completion_store)
309 g_object_unref (chooser_entry->completion_store);
310 chooser_entry->completion_store = NULL;
313 if (chooser_entry->file_system)
315 g_object_unref (chooser_entry->file_system);
316 chooser_entry->file_system = NULL;
319 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
322 /* Match functions for the GtkEntryCompletion */
324 match_selected_callback (GtkEntryCompletion *completion,
327 GtkFileChooserEntry *chooser_entry)
334 gtk_tree_model_get (model, iter,
335 DISPLAY_NAME_COLUMN, &display_name,
339 if (!display_name || !file)
341 /* these shouldn't complain if passed NULL */
342 g_object_unref (file);
343 g_free (display_name);
347 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
349 pos = chooser_entry->file_part_pos;
351 /* We don't set in_change here as we want to update the current_folder
353 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
355 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
358 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
360 g_object_unref (file);
361 g_free (display_name);
366 /* Match function for the GtkEntryCompletion */
368 completion_match_func (GtkEntryCompletion *comp,
369 const char *key_unused,
373 GtkFileChooserEntry *chooser_entry;
376 char *norm_file_part;
379 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
381 /* We ignore the key because it is the contents of the entry. Instead, we
382 * just use our precomputed file_part.
384 if (!chooser_entry->file_part)
389 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
392 return FALSE; /* Uninitialized row, ugh */
395 /* If we have an empty file_part, then we're at the root of a directory. In
396 * that case, we want to match all non-dot files. We might want to match
397 * dot_files too if show_hidden is TRUE on the fileselector in the future.
399 /* Additionally, support for gnome .hidden files would be sweet, too */
400 if (chooser_entry->file_part[0] == '\000')
412 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
413 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
415 #ifdef G_PLATFORM_WIN32
419 temp = norm_file_part;
420 norm_file_part = g_utf8_casefold (norm_file_part, -1);
424 norm_name = g_utf8_casefold (norm_name, -1);
429 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
431 g_free (norm_file_part);
439 clear_completions (GtkFileChooserEntry *chooser_entry)
441 chooser_entry->has_completion = FALSE;
442 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
444 remove_completion_feedback (chooser_entry);
448 beep (GtkFileChooserEntry *chooser_entry)
450 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
453 /* This function will append a directory separator to paths to
454 * display_name iff the path associated with it is a directory.
455 * maybe_append_separator_to_file will g_free the display_name and
456 * return a new one if needed. Otherwise, it will return the old one.
457 * You should be safe calling
459 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
461 * g_free (display_name);
464 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
471 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
475 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
479 if (_gtk_file_info_consider_as_directory (info))
481 gchar *tmp = display_name;
482 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
487 g_object_unref (info);
495 trim_dir_separator_suffix (const char *str)
500 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
501 return g_strndup (str, len - 1);
503 return g_strdup (str);
506 /* Determines if the completion model has entries with a common prefix relative
507 * to the current contents of the entry. Also, if there's one and only one such
508 * path, stores it in unique_path_ret.
511 find_common_prefix (GtkFileChooserEntry *chooser_entry,
512 gchar **common_prefix_ret,
513 GFile **unique_file_ret,
514 gboolean *is_complete_not_unique_ret,
515 gboolean *prefix_expands_the_file_part_ret,
518 GtkEditable *editable;
522 char *text_up_to_cursor;
523 GFile *parsed_folder_file;
524 char *parsed_file_part;
526 *common_prefix_ret = NULL;
527 *unique_file_ret = NULL;
528 *is_complete_not_unique_ret = FALSE;
529 *prefix_expands_the_file_part_ret = FALSE;
531 editable = GTK_EDITABLE (chooser_entry);
533 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
535 parsed = _gtk_file_system_parse (chooser_entry->file_system,
536 chooser_entry->base_folder,
542 g_free (text_up_to_cursor);
547 g_assert (parsed_folder_file != NULL
548 && chooser_entry->current_folder != NULL
549 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
551 g_object_unref (parsed_folder_file);
553 /* First pass: find the common prefix */
555 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
562 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
564 DISPLAY_NAME_COLUMN, &display_name,
568 if (g_str_has_prefix (display_name, parsed_file_part))
570 if (!*common_prefix_ret)
572 *common_prefix_ret = trim_dir_separator_suffix (display_name);
573 *unique_file_ret = g_object_ref (file);
577 gchar *p = *common_prefix_ret;
578 const gchar *q = display_name;
580 while (*p && *p == *q)
588 if (*unique_file_ret)
590 g_object_unref (*unique_file_ret);
591 *unique_file_ret = NULL;
596 g_free (display_name);
597 g_object_unref (file);
598 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
601 /* Second pass: see if the prefix we found is a complete match */
603 if (*common_prefix_ret != NULL)
605 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
612 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
614 DISPLAY_NAME_COLUMN, &display_name,
616 len = strlen (display_name);
619 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
622 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
623 *is_complete_not_unique_ret = TRUE;
625 g_free (display_name);
626 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
629 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
631 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
634 g_free (parsed_file_part);
640 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
647 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
648 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
652 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
653 if (G_IS_DIR_SEPARATOR (*next_char_str))
656 g_free (next_char_str);
663 INVALID_INPUT, /* what the user typed is bogus */
664 NO_MATCH, /* no matches based on what the user typed */
665 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
666 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
667 COMPLETED, /* completion inserted (ambiguous suffix) */
668 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
669 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
670 } CommonPrefixResult;
672 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
673 static CommonPrefixResult
674 append_common_prefix (GtkFileChooserEntry *chooser_entry,
676 gboolean show_errors)
678 gchar *common_prefix;
680 gboolean is_complete_not_unique;
681 gboolean prefix_expands_the_file_part;
683 CommonPrefixResult result = NO_MATCH;
684 gboolean have_result;
686 clear_completions (chooser_entry);
688 if (chooser_entry->completion_store == NULL)
692 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
694 /* If the user types an incomplete hostname ("http://foo" without a slash
695 * after that), it's not an error. We just don't want to pop up a
696 * meaningless completion window in that state.
698 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
701 beep (chooser_entry);
702 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
705 g_error_free (error);
707 return INVALID_INPUT;
714 if (!char_after_cursor_is_directory_separator (chooser_entry))
718 common_prefix = maybe_append_separator_to_file (chooser_entry,
723 prefix_expands_the_file_part = TRUE;
726 g_object_unref (unique_file);
728 if (prefix_expands_the_file_part)
729 result = COMPLETED_UNIQUE;
731 result = NOTHING_INSERTED_UNIQUE;
737 if (is_complete_not_unique)
739 result = COMPLETE_BUT_NOT_UNIQUE;
747 gint common_prefix_len;
750 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
751 common_prefix_len = g_utf8_strlen (common_prefix, -1);
753 pos = chooser_entry->file_part_pos;
755 if (prefix_expands_the_file_part)
757 chooser_entry->in_change = TRUE;
758 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
760 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
763 chooser_entry->in_change = FALSE;
767 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
769 pos); /* equivalent to cursor_pos + common_prefix_len); */
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);
893 gtk_paint_flat_box (gtk_widget_get_style (chooser_entry->completion_feedback_window),
897 chooser_entry->completion_feedback_window,
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 gdk_cursor_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() */
937 GtkWidget *alignment;
939 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
940 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
941 GDK_WINDOW_TYPE_HINT_TOOLTIP);
942 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
943 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
944 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
946 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
947 style = gtk_widget_get_style (chooser_entry->completion_feedback_window);
948 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
949 style->ythickness, style->ythickness,
950 style->xthickness, style->xthickness);
951 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
952 gtk_widget_show (alignment);
954 g_signal_connect (chooser_entry->completion_feedback_window, "draw",
955 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
956 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
957 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
958 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
960 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
961 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
962 gtk_widget_show (chooser_entry->completion_feedback_label);
966 completion_feedback_timeout_cb (gpointer data)
968 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
970 chooser_entry->completion_feedback_timeout_id = 0;
972 remove_completion_feedback (chooser_entry);
977 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
979 if (chooser_entry->completion_feedback_timeout_id != 0)
980 g_source_remove (chooser_entry->completion_feedback_timeout_id);
982 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
983 completion_feedback_timeout_cb,
987 /* Gets the x position of the text cursor in the entry, in widget coordinates */
989 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
992 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
993 * the reader, you have to implement support for the entry's scroll offset and
994 * RTL layouts and all the fancy Pango stuff.
998 gint layout_x, layout_y;
1000 PangoRectangle strong_pos;
1002 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1004 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1006 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1007 GTK_ENTRY (chooser_entry)->current_pos);
1009 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1011 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1015 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1017 /* More or less stolen from gtk_tooltip_position() */
1019 GtkRequisition feedback_req;
1020 GtkWidget *widget = GTK_WIDGET (chooser_entry);
1021 gint entry_x, entry_y;
1023 GtkAllocation entry_allocation;
1024 int feedback_x, feedback_y;
1026 gtk_size_request_get_size (GTK_SIZE_REQUEST (chooser_entry->completion_feedback_window),
1027 &feedback_req, NULL);
1029 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1030 gtk_widget_get_allocation (widget, &entry_allocation);
1032 get_entry_cursor_x (chooser_entry, &cursor_x);
1034 /* FIXME: fit to the screen if we bump on the screen's edge */
1035 /* cheap "half M-width", use height as approximation of character em-size */
1036 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1037 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1039 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1040 gtk_widget_show (chooser_entry->completion_feedback_window);
1042 install_completion_feedback_timer (chooser_entry);
1046 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1047 const gchar *feedback)
1049 if (chooser_entry->completion_feedback_window == NULL)
1050 create_completion_feedback_window (chooser_entry);
1052 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1054 show_completion_feedback_window (chooser_entry);
1058 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1060 if (chooser_entry->completion_feedback_window)
1061 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1063 chooser_entry->completion_feedback_window = NULL;
1064 chooser_entry->completion_feedback_label = NULL;
1066 if (chooser_entry->completion_feedback_timeout_id != 0)
1068 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1069 chooser_entry->completion_feedback_timeout_id = 0;
1074 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1076 CommonPrefixResult result;
1078 g_assert (chooser_entry->current_folder != NULL);
1079 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1081 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1083 * - If there is a common prefix, insert it (done)
1084 * - If there is no common prefix, pop up the suggestion window
1085 * - If there are no matches at all, beep and bring up a tooltip (done)
1086 * - If the suggestion window is already up, scroll it
1088 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1093 /* We already beeped in append_common_prefix(); do nothing here */
1097 beep (chooser_entry);
1098 /* translators: this text is shown when there are no completions
1099 * for something the user typed in a file chooser entry
1101 pop_up_completion_feedback (chooser_entry, _("No match"));
1104 case NOTHING_INSERTED_COMPLETE:
1105 /* FIXME: pop up the suggestion window or scroll it */
1108 case NOTHING_INSERTED_UNIQUE:
1109 /* translators: this text is shown when there is exactly one completion
1110 * for something the user typed in a file chooser entry
1112 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1119 case COMPLETED_UNIQUE:
1123 case COMPLETE_BUT_NOT_UNIQUE:
1124 /* translators: this text is shown when the text in a file chooser
1125 * entry is a complete filename, but could be continued to find
1128 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1132 g_assert_not_reached ();
1137 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1139 RefreshStatus status;
1143 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1150 g_assert (chooser_entry->current_folder_file != NULL);
1152 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1153 explicitly_complete (chooser_entry);
1156 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1158 /* Translators: this text is shown while the system is searching
1159 * for possible completions for filenames in a file chooser entry. */
1160 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1165 case REFRESH_INVALID_INPUT:
1167 /* Translators: this is shown in the feedback for Tab-completion in a file
1168 * chooser's text entry, when the user enters an invalid path. */
1169 feedback_msg = _("Invalid path");
1172 case REFRESH_INCOMPLETE_HOSTNAME:
1175 if (chooser_entry->local_only)
1177 /* hostnames in a local_only file chooser? user error */
1179 /* Translators: this is shown in the feedback for Tab-completion in a
1180 * file chooser's text entry when the user enters something like
1181 * "sftp://blahblah" in an app that only supports local filenames. */
1182 feedback_msg = _("Only local files may be selected");
1186 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1188 /* Translators: this is shown in the feedback for Tab-completion in a
1189 * file chooser's text entry when the user hasn't entered the first '/'
1190 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1191 feedback_msg = _("Incomplete hostname; end it with '/'");
1196 case REFRESH_NONEXISTENT:
1199 /* Translators: this is shown in the feedback for Tab-completion in a file
1200 * chooser's text entry when the user enters a path that does not exist
1201 * and then hits Tab */
1202 feedback_msg = _("Path does not exist");
1205 case REFRESH_NOT_LOCAL:
1207 feedback_msg = _("Only local files may be selected");
1211 g_assert_not_reached ();
1217 g_assert (chooser_entry->current_folder_file == NULL);
1219 beep (chooser_entry);
1220 pop_up_completion_feedback (chooser_entry, feedback_msg);
1221 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1226 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1229 GtkFileChooserEntry *chooser_entry;
1230 GtkEditable *editable;
1232 GdkModifierType state;
1233 gboolean control_pressed;
1235 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1236 editable = GTK_EDITABLE (widget);
1237 entry = GTK_ENTRY (widget);
1239 if (!chooser_entry->eat_tabs)
1240 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1242 control_pressed = FALSE;
1244 if (gtk_get_current_event_state (&state))
1246 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1247 control_pressed = TRUE;
1250 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1251 * makes it 'safe' for people to hit. */
1252 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1254 if (chooser_entry->has_completion)
1255 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1257 start_explicit_completion (chooser_entry);
1262 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1267 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1268 GdkEventFocus *event)
1270 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1272 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1274 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1278 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1280 if (chooser_entry->has_completion)
1282 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1283 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1286 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1287 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1291 gtk_file_chooser_entry_activate (GtkEntry *entry)
1293 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1295 commit_completion_and_refresh (chooser_entry);
1296 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1300 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1302 if (!chooser_entry->completion_store)
1305 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1306 g_object_unref (chooser_entry->completion_store);
1307 chooser_entry->completion_store = NULL;
1310 /* Fills the completion store from the contents of the current folder */
1312 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1317 discard_completion_store (chooser_entry);
1319 files = _gtk_folder_list_children (chooser_entry->current_folder);
1321 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1325 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1330 file = tmp_list->data;
1332 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1336 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1340 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1342 gtk_list_store_append (chooser_entry->completion_store, &iter);
1343 gtk_list_store_set (chooser_entry->completion_store, &iter,
1344 DISPLAY_NAME_COLUMN, display_name,
1348 g_object_unref (info);
1349 g_free (display_name);
1353 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1354 g_slist_free (files);
1356 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1357 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1359 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1360 GTK_TREE_MODEL (chooser_entry->completion_store));
1363 /* When we finish loading the current folder, this function should get called to
1364 * perform the deferred autocompletion or explicit completion.
1367 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1369 switch (chooser_entry->load_complete_action)
1371 case LOAD_COMPLETE_NOTHING:
1374 case LOAD_COMPLETE_AUTOCOMPLETE:
1375 autocomplete (chooser_entry);
1378 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1379 explicitly_complete (chooser_entry);
1383 g_assert_not_reached ();
1386 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1390 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1392 populate_completion_store (chooser_entry);
1393 perform_load_complete_action (chooser_entry);
1395 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1398 /* Callback when the current folder finishes loading */
1400 finished_loading_cb (GtkFolder *folder,
1403 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1405 finish_folder_load (chooser_entry);
1408 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1410 load_directory_get_folder_callback (GCancellable *cancellable,
1412 const GError *error,
1415 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1416 GtkFileChooserEntry *chooser_entry = data;
1418 if (cancellable != chooser_entry->load_folder_cancellable)
1421 chooser_entry->load_folder_cancellable = NULL;
1425 LoadCompleteAction old_load_complete_action;
1427 old_load_complete_action = chooser_entry->load_complete_action;
1429 discard_completion_store (chooser_entry);
1430 clear_completions (chooser_entry);
1432 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1434 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1436 beep (chooser_entry);
1437 pop_up_completion_feedback (chooser_entry, error->message);
1440 discard_current_folder (chooser_entry);
1443 if (cancelled || error)
1446 g_assert (folder != NULL);
1447 chooser_entry->current_folder = g_object_ref (folder);
1449 discard_completion_store (chooser_entry);
1451 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1452 finish_folder_load (chooser_entry);
1454 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1455 G_CALLBACK (finished_loading_cb), chooser_entry);
1458 g_object_unref (chooser_entry);
1459 g_object_unref (cancellable);
1462 static RefreshStatus
1463 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1465 if (chooser_entry->file_system == NULL)
1468 g_assert (chooser_entry->current_folder_file != NULL);
1469 g_assert (chooser_entry->current_folder == NULL);
1470 g_assert (chooser_entry->load_folder_cancellable == NULL);
1472 if (chooser_entry->local_only
1473 && !g_file_is_native (chooser_entry->current_folder_file))
1475 g_object_unref (chooser_entry->current_folder_file);
1476 chooser_entry->current_folder_file = NULL;
1478 return REFRESH_NOT_LOCAL;
1481 chooser_entry->load_folder_cancellable =
1482 _gtk_file_system_get_folder (chooser_entry->file_system,
1483 chooser_entry->current_folder_file,
1484 "standard::name,standard::display-name,standard::type",
1485 load_directory_get_folder_callback,
1486 g_object_ref (chooser_entry));
1491 static RefreshStatus
1492 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1494 gboolean force_reload)
1496 gboolean reload = FALSE;
1498 g_assert (folder_file != NULL);
1500 if (chooser_entry->current_folder_file)
1502 if ((!(g_file_equal (folder_file, chooser_entry->current_folder_file)
1503 && chooser_entry->load_folder_cancellable))
1508 discard_current_folder (chooser_entry);
1509 discard_loading_and_current_folder_file (chooser_entry);
1511 chooser_entry->current_folder_file = g_object_ref (folder_file);
1516 chooser_entry->current_folder_file = g_object_ref (folder_file);
1521 return start_loading_current_folder (chooser_entry);
1526 static RefreshStatus
1527 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1528 RefreshMode refresh_mode)
1530 GtkEditable *editable;
1535 gsize total_len, file_part_len;
1538 RefreshStatus result;
1540 editable = GTK_EDITABLE (chooser_entry);
1542 switch (refresh_mode)
1544 case REFRESH_UP_TO_CURSOR_POSITION:
1545 end_pos = gtk_editable_get_position (editable);
1548 case REFRESH_WHOLE_TEXT:
1549 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1553 g_assert_not_reached ();
1554 return REFRESH_INVALID_INPUT;
1557 text = gtk_editable_get_chars (editable, 0, end_pos);
1560 if (!chooser_entry->file_system ||
1561 !chooser_entry->base_folder ||
1562 !_gtk_file_system_parse (chooser_entry->file_system,
1563 chooser_entry->base_folder, text,
1564 &folder_file, &file_part, &error))
1566 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1569 result = REFRESH_INCOMPLETE_HOSTNAME;
1573 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1575 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1576 result = REFRESH_NONEXISTENT;
1578 result = REFRESH_INVALID_INPUT;
1582 g_error_free (error);
1584 file_part = g_strdup ("");
1589 g_assert (folder_file != NULL);
1591 file_part_len = strlen (file_part);
1592 total_len = strlen (text);
1593 if (total_len > file_part_len)
1594 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1598 result = REFRESH_OK;
1603 g_free (chooser_entry->file_part);
1605 chooser_entry->file_part = file_part;
1606 chooser_entry->file_part_pos = file_part_pos;
1608 if (result == REFRESH_OK)
1610 result = reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1614 discard_current_folder (chooser_entry);
1615 discard_loading_and_current_folder_file (chooser_entry);
1619 g_object_unref (folder_file);
1621 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1622 ((result == REFRESH_OK)
1623 && (chooser_entry->current_folder_file != NULL)
1624 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1625 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1626 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1627 || ((result != REFRESH_OK)
1628 && (chooser_entry->current_folder_file == NULL)
1629 && (chooser_entry->load_folder_cancellable == NULL)
1630 && (chooser_entry->current_folder == NULL)));
1636 autocomplete (GtkFileChooserEntry *chooser_entry)
1638 if (!(chooser_entry->current_folder != NULL
1639 && _gtk_folder_is_finished_loading (chooser_entry->current_folder)
1640 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1643 append_common_prefix (chooser_entry, TRUE, FALSE);
1647 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1649 RefreshStatus status;
1651 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1656 g_assert (chooser_entry->current_folder_file != NULL);
1658 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1659 autocomplete (chooser_entry);
1661 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1665 case REFRESH_INVALID_INPUT:
1666 case REFRESH_INCOMPLETE_HOSTNAME:
1667 case REFRESH_NONEXISTENT:
1668 case REFRESH_NOT_LOCAL:
1669 /* We don't beep or anything, since this is autocompletion - the user
1670 * didn't request any action explicitly.
1675 g_assert_not_reached ();
1680 start_autocompletion_idle_handler (gpointer data)
1682 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1684 start_autocompletion (chooser_entry);
1686 chooser_entry->start_autocompletion_idle_id = 0;
1692 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1694 if (chooser_entry->start_autocompletion_idle_id != 0)
1697 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1702 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1703 const gchar *new_text,
1704 gint new_text_length,
1708 const gchar *colon = memchr (new_text, ':', new_text_length);
1711 /* Disallow these characters altogether */
1712 for (i = 0; i < new_text_length; i++)
1714 if (new_text[i] == '<' ||
1715 new_text[i] == '>' ||
1716 new_text[i] == '"' ||
1717 new_text[i] == '|' ||
1718 new_text[i] == '*' ||
1723 if (i < new_text_length ||
1724 /* Disallow entering text that would cause a colon to be anywhere except
1725 * after a drive letter.
1728 *position + (colon - new_text) != 1) ||
1729 (new_text_length > 0 &&
1731 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1732 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1734 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1735 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1743 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1748 /* If deleting a drive letter, delete the colon, too */
1749 if (start_pos == 0 && end_pos == 1 &&
1750 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1751 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1753 g_signal_handlers_block_by_func (chooser_entry,
1754 G_CALLBACK (delete_text_callback),
1756 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1757 g_signal_handlers_unblock_by_func (chooser_entry,
1758 G_CALLBACK (delete_text_callback),
1765 * _gtk_file_chooser_entry_new:
1766 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1768 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1769 * is an internal implementation widget for the GTK+ file chooser
1770 * which is an entry with completion with respect to a
1771 * #GtkFileSystem object.
1773 * Return value: the newly created #GtkFileChooserEntry
1776 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1778 GtkFileChooserEntry *chooser_entry;
1780 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1781 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1783 return GTK_WIDGET (chooser_entry);
1787 * _gtk_file_chooser_entry_set_file_system:
1788 * @chooser_entry: a #GtkFileChooser
1789 * @file_system: an object implementing #GtkFileSystem
1791 * Sets the file system for @chooser_entry.
1794 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1795 GtkFileSystem *file_system)
1797 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1798 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1800 if (file_system != chooser_entry->file_system)
1802 if (chooser_entry->file_system)
1803 g_object_unref (chooser_entry->file_system);
1805 chooser_entry->file_system = g_object_ref (file_system);
1810 * _gtk_file_chooser_entry_set_base_folder:
1811 * @chooser_entry: a #GtkFileChooserEntry
1812 * @file: file for a folder in the chooser entries current file system.
1814 * Sets the folder with respect to which completions occur.
1817 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1820 if (chooser_entry->base_folder)
1821 g_object_unref (chooser_entry->base_folder);
1823 chooser_entry->base_folder = file;
1825 if (chooser_entry->base_folder)
1826 g_object_ref (chooser_entry->base_folder);
1828 clear_completions (chooser_entry);
1829 _gtk_file_chooser_entry_select_filename (chooser_entry);
1833 * _gtk_file_chooser_entry_get_current_folder:
1834 * @chooser_entry: a #GtkFileChooserEntry
1836 * Gets the current folder for the #GtkFileChooserEntry. If the
1837 * user has only entered a filename, this will be in the base folder
1838 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1839 * user has entered a relative or absolute path, then it will
1840 * be different. If the user has entered unparsable text, or text which
1841 * the entry cannot handle, this will return %NULL.
1843 * Return value: the file for the current folder - this value is owned by the
1844 * chooser entry and must not be modified or freed.
1847 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1849 commit_completion_and_refresh (chooser_entry);
1850 return chooser_entry->current_folder_file;
1854 * _gtk_file_chooser_entry_get_file_part:
1855 * @chooser_entry: a #GtkFileChooserEntry
1857 * Gets the non-folder portion of whatever the user has entered
1858 * into the file selector. What is returned is a UTF-8 string,
1859 * and if a filename path is needed, g_file_get_child_for_display_name()
1862 * Return value: the entered filename - this value is owned by the
1863 * chooser entry and must not be modified or freed.
1866 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1868 commit_completion_and_refresh (chooser_entry);
1869 return chooser_entry->file_part;
1873 * _gtk_file_chooser_entry_set_file_part:
1874 * @chooser_entry: a #GtkFileChooserEntry
1875 * @file_part: text to display in the entry, in UTF-8
1877 * Sets the current text shown in the file chooser entry.
1880 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1881 const gchar *file_part)
1883 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1885 chooser_entry->in_change = TRUE;
1886 clear_completions (chooser_entry);
1887 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1888 chooser_entry->in_change = FALSE;
1893 * _gtk_file_chooser_entry_set_action:
1894 * @chooser_entry: a #GtkFileChooserEntry
1895 * @action: the action which is performed by the file selector using this entry
1897 * Sets action which is performed by the file selector using this entry.
1898 * The #GtkFileChooserEntry will use different completion strategies for
1899 * different actions.
1902 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1903 GtkFileChooserAction action)
1905 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1907 if (chooser_entry->action != action)
1909 GtkEntryCompletion *comp;
1911 chooser_entry->action = action;
1913 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1915 /* FIXME: do we need to actually set the following? */
1919 case GTK_FILE_CHOOSER_ACTION_OPEN:
1920 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1921 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1923 case GTK_FILE_CHOOSER_ACTION_SAVE:
1924 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1925 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1933 * _gtk_file_chooser_entry_get_action:
1934 * @chooser_entry: a #GtkFileChooserEntry
1936 * Gets the action for this entry.
1938 * Returns: the action
1940 GtkFileChooserAction
1941 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1943 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1944 GTK_FILE_CHOOSER_ACTION_OPEN);
1946 return chooser_entry->action;
1950 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1953 gboolean retval = FALSE;
1955 if (chooser_entry->current_folder)
1957 GFileInfo *file_info;
1959 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1963 retval = _gtk_file_info_consider_as_directory (file_info);
1964 g_object_unref (file_info);
1973 * _gtk_file_chooser_entry_select_filename:
1974 * @chooser_entry: a #GtkFileChooserEntry
1976 * Selects the filename (without the extension) for user edition.
1979 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1981 const gchar *str, *ext;
1984 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1986 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1987 ext = g_strrstr (str, ".");
1990 len = g_utf8_pointer_to_offset (str, ext);
1993 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1997 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1998 gboolean local_only)
2000 chooser_entry->local_only = local_only;
2001 clear_completions (chooser_entry);
2005 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
2007 return chooser_entry->local_only;