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.
24 #include "gtkalignment.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
28 #include "gtkfilechooserentry.h"
31 #include "gtkwindow.h"
35 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
37 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
38 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
39 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
41 struct _GtkFileChooserEntryClass
43 GtkEntryClass parent_class;
46 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
48 LOAD_COMPLETE_NOTHING,
49 LOAD_COMPLETE_AUTOCOMPLETE,
50 LOAD_COMPLETE_EXPLICIT_COMPLETION
56 REFRESH_INVALID_INPUT,
57 REFRESH_INCOMPLETE_HOSTNAME,
62 struct _GtkFileChooserEntry
64 GtkEntry parent_instance;
66 GtkFileChooserAction action;
68 GtkFileSystem *file_system;
70 GFile *current_folder_file;
74 /* Folder being loaded or already loaded */
75 GtkFolder *current_folder;
76 GCancellable *load_folder_cancellable;
78 LoadCompleteAction load_complete_action;
80 GtkListStore *completion_store;
82 guint start_autocompletion_idle_id;
84 GtkWidget *completion_feedback_window;
85 GtkWidget *completion_feedback_label;
86 guint completion_feedback_timeout_id;
88 guint has_completion : 1;
101 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
103 static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface);
105 static void gtk_file_chooser_entry_finalize (GObject *object);
106 static void gtk_file_chooser_entry_dispose (GObject *object);
107 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
108 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
109 static gboolean gtk_file_chooser_entry_focus (GtkWidget *widget,
110 GtkDirectionType direction);
111 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
112 GdkEventFocus *event);
113 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
114 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
115 const gchar *new_text,
116 gint new_text_length,
118 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
121 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
123 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
128 static gint insert_text_callback (GtkFileChooserEntry *widget,
129 const gchar *new_text,
130 gint new_text_length,
133 static void delete_text_callback (GtkFileChooserEntry *widget,
139 static gboolean match_selected_callback (GtkEntryCompletion *completion,
142 GtkFileChooserEntry *chooser_entry);
143 static gboolean completion_match_func (GtkEntryCompletion *comp,
147 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
153 REFRESH_UP_TO_CURSOR_POSITION,
157 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
158 RefreshMode refresh_mode);
159 static void finished_loading_cb (GtkFolder *folder,
161 static void autocomplete (GtkFileChooserEntry *chooser_entry);
162 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
163 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
164 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
165 const gchar *feedback);
167 static GtkEditableClass *parent_editable_iface;
169 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
170 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
171 gtk_file_chooser_entry_iface_init))
174 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
176 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
177 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
178 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
180 gobject_class->finalize = gtk_file_chooser_entry_finalize;
181 gobject_class->dispose = gtk_file_chooser_entry_dispose;
183 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
184 widget_class->unmap = gtk_file_chooser_entry_unmap;
185 widget_class->focus = gtk_file_chooser_entry_focus;
186 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
188 entry_class->activate = gtk_file_chooser_entry_activate;
192 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
194 parent_editable_iface = g_type_interface_peek_parent (iface);
196 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
197 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
198 iface->set_position = gtk_file_chooser_entry_set_position;
199 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
203 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
205 GtkEntryCompletion *comp;
206 GtkCellRenderer *cell;
208 chooser_entry->local_only = TRUE;
210 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
212 comp = gtk_entry_completion_new ();
213 gtk_entry_completion_set_popup_single_match (comp, FALSE);
215 gtk_entry_completion_set_match_func (comp,
216 completion_match_func,
220 cell = gtk_cell_renderer_text_new ();
221 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
223 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
227 g_signal_connect (comp, "match-selected",
228 G_CALLBACK (match_selected_callback), chooser_entry);
230 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
231 g_object_unref (comp);
234 g_signal_connect (chooser_entry, "insert-text",
235 G_CALLBACK (insert_text_callback), NULL);
236 g_signal_connect (chooser_entry, "delete-text",
237 G_CALLBACK (delete_text_callback), NULL);
242 gtk_file_chooser_entry_finalize (GObject *object)
244 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
246 if (chooser_entry->base_folder)
247 g_object_unref (chooser_entry->base_folder);
249 if (chooser_entry->current_folder)
250 g_object_unref (chooser_entry->current_folder);
252 if (chooser_entry->current_folder_file)
253 g_object_unref (chooser_entry->current_folder_file);
255 g_free (chooser_entry->file_part);
257 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
261 discard_current_folder (GtkFileChooserEntry *chooser_entry)
263 if (chooser_entry->current_folder)
265 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
266 G_CALLBACK (finished_loading_cb), chooser_entry);
267 g_object_unref (chooser_entry->current_folder);
268 chooser_entry->current_folder = NULL;
273 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
275 if (chooser_entry->load_folder_cancellable)
277 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
278 chooser_entry->load_folder_cancellable = NULL;
281 if (chooser_entry->current_folder_file)
283 g_object_unref (chooser_entry->current_folder_file);
284 chooser_entry->current_folder_file = NULL;
289 gtk_file_chooser_entry_dispose (GObject *object)
291 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
293 remove_completion_feedback (chooser_entry);
294 discard_current_folder (chooser_entry);
295 discard_loading_and_current_folder_file (chooser_entry);
297 if (chooser_entry->start_autocompletion_idle_id != 0)
299 g_source_remove (chooser_entry->start_autocompletion_idle_id);
300 chooser_entry->start_autocompletion_idle_id = 0;
303 if (chooser_entry->completion_store)
305 g_object_unref (chooser_entry->completion_store);
306 chooser_entry->completion_store = NULL;
309 if (chooser_entry->file_system)
311 g_object_unref (chooser_entry->file_system);
312 chooser_entry->file_system = NULL;
315 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
318 /* Match functions for the GtkEntryCompletion */
320 match_selected_callback (GtkEntryCompletion *completion,
323 GtkFileChooserEntry *chooser_entry)
330 gtk_tree_model_get (model, iter,
331 DISPLAY_NAME_COLUMN, &display_name,
335 if (!display_name || !file)
337 /* these shouldn't complain if passed NULL */
338 g_object_unref (file);
339 g_free (display_name);
343 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
345 pos = chooser_entry->file_part_pos;
347 /* We don't set in_change here as we want to update the current_folder
349 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
351 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
354 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
356 g_object_unref (file);
357 g_free (display_name);
362 /* Match function for the GtkEntryCompletion */
364 completion_match_func (GtkEntryCompletion *comp,
365 const char *key_unused,
369 GtkFileChooserEntry *chooser_entry;
372 char *norm_file_part;
375 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
377 /* We ignore the key because it is the contents of the entry. Instead, we
378 * just use our precomputed file_part.
380 if (!chooser_entry->file_part)
385 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
388 return FALSE; /* Uninitialized row, ugh */
391 /* If we have an empty file_part, then we're at the root of a directory. In
392 * that case, we want to match all non-dot files. We might want to match
393 * dot_files too if show_hidden is TRUE on the fileselector in the future.
395 /* Additionally, support for gnome .hidden files would be sweet, too */
396 if (chooser_entry->file_part[0] == '\000')
408 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
409 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
411 #ifdef G_PLATFORM_WIN32
415 temp = norm_file_part;
416 norm_file_part = g_utf8_casefold (norm_file_part, -1);
420 norm_name = g_utf8_casefold (norm_name, -1);
425 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
427 g_free (norm_file_part);
435 clear_completions (GtkFileChooserEntry *chooser_entry)
437 chooser_entry->has_completion = FALSE;
438 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
440 remove_completion_feedback (chooser_entry);
444 beep (GtkFileChooserEntry *chooser_entry)
446 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
449 /* This function will append a directory separator to paths to
450 * display_name iff the path associated with it is a directory.
451 * maybe_append_separator_to_file will g_free the display_name and
452 * return a new one if needed. Otherwise, it will return the old one.
453 * You should be safe calling
455 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
457 * g_free (display_name);
460 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
467 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
471 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
475 if (_gtk_file_info_consider_as_directory (info))
477 gchar *tmp = display_name;
478 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
483 g_object_unref (info);
491 trim_dir_separator_suffix (const char *str)
496 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
497 return g_strndup (str, len - 1);
499 return g_strdup (str);
502 /* Determines if the completion model has entries with a common prefix relative
503 * to the current contents of the entry. Also, if there's one and only one such
504 * path, stores it in unique_path_ret.
507 find_common_prefix (GtkFileChooserEntry *chooser_entry,
508 gchar **common_prefix_ret,
509 GFile **unique_file_ret,
510 gboolean *is_complete_not_unique_ret,
511 gboolean *prefix_expands_the_file_part_ret,
514 GtkEditable *editable;
518 char *text_up_to_cursor;
519 GFile *parsed_folder_file;
520 char *parsed_file_part;
522 *common_prefix_ret = NULL;
523 *unique_file_ret = NULL;
524 *is_complete_not_unique_ret = FALSE;
525 *prefix_expands_the_file_part_ret = FALSE;
527 editable = GTK_EDITABLE (chooser_entry);
529 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
531 parsed = _gtk_file_system_parse (chooser_entry->file_system,
532 chooser_entry->base_folder,
538 g_free (text_up_to_cursor);
543 g_assert (parsed_folder_file != NULL
544 && chooser_entry->current_folder != NULL
545 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
547 g_object_unref (parsed_folder_file);
549 /* First pass: find the common prefix */
551 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
558 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
560 DISPLAY_NAME_COLUMN, &display_name,
564 if (g_str_has_prefix (display_name, parsed_file_part))
566 if (!*common_prefix_ret)
568 *common_prefix_ret = trim_dir_separator_suffix (display_name);
569 *unique_file_ret = g_object_ref (file);
573 gchar *p = *common_prefix_ret;
574 const gchar *q = display_name;
576 while (*p && *p == *q)
584 if (*unique_file_ret)
586 g_object_unref (*unique_file_ret);
587 *unique_file_ret = NULL;
592 g_free (display_name);
593 g_object_unref (file);
594 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
597 /* Second pass: see if the prefix we found is a complete match */
599 if (*common_prefix_ret != NULL)
601 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
608 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
610 DISPLAY_NAME_COLUMN, &display_name,
612 len = strlen (display_name);
615 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
618 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
619 *is_complete_not_unique_ret = TRUE;
621 g_free (display_name);
622 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
625 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
627 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
630 g_free (parsed_file_part);
636 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
643 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
644 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
648 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
649 if (G_IS_DIR_SEPARATOR (*next_char_str))
652 g_free (next_char_str);
659 INVALID_INPUT, /* what the user typed is bogus */
660 NO_MATCH, /* no matches based on what the user typed */
661 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
662 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
663 COMPLETED, /* completion inserted (ambiguous suffix) */
664 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
665 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
666 } CommonPrefixResult;
668 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
669 static CommonPrefixResult
670 append_common_prefix (GtkFileChooserEntry *chooser_entry,
672 gboolean show_errors)
674 gchar *common_prefix;
676 gboolean is_complete_not_unique;
677 gboolean prefix_expands_the_file_part;
679 CommonPrefixResult result;
680 gboolean have_result;
682 clear_completions (chooser_entry);
684 if (chooser_entry->completion_store == NULL)
688 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
690 /* If the user types an incomplete hostname ("http://foo" without a slash
691 * after that), it's not an error. We just don't want to pop up a
692 * meaningless completion window in that state.
694 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
697 beep (chooser_entry);
698 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
701 g_error_free (error);
703 return INVALID_INPUT;
710 if (!char_after_cursor_is_directory_separator (chooser_entry))
714 common_prefix = maybe_append_separator_to_file (chooser_entry,
719 prefix_expands_the_file_part = TRUE;
722 g_object_unref (unique_file);
724 if (prefix_expands_the_file_part)
725 result = COMPLETED_UNIQUE;
727 result = NOTHING_INSERTED_UNIQUE;
733 if (is_complete_not_unique)
735 result = COMPLETE_BUT_NOT_UNIQUE;
743 gint common_prefix_len;
746 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
747 common_prefix_len = g_utf8_strlen (common_prefix, -1);
749 pos = chooser_entry->file_part_pos;
751 if (prefix_expands_the_file_part)
753 chooser_entry->in_change = TRUE;
754 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
756 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
759 chooser_entry->in_change = FALSE;
763 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
765 pos); /* equivalent to cursor_pos + common_prefix_len); */
766 chooser_entry->has_completion = TRUE;
769 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
771 else if (!have_result)
773 result = NOTHING_INSERTED_COMPLETE;
777 g_free (common_prefix);
794 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
795 const gchar *new_text,
796 gint new_text_length,
799 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
803 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
804 insert_pos = *position;
806 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
808 if (chooser_entry->in_change)
811 remove_completion_feedback (chooser_entry);
813 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
814 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
815 && insert_pos == old_text_len)
816 install_start_autocompletion_idle (chooser_entry);
820 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
822 if (chooser_entry->in_change)
825 clear_completions (chooser_entry);
829 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
833 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
835 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
837 clear_completions_if_not_in_change (chooser_entry);
841 gtk_file_chooser_entry_set_position (GtkEditable *editable,
844 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
846 parent_editable_iface->set_position (editable, position);
848 clear_completions_if_not_in_change (chooser_entry);
852 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
856 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
858 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
860 clear_completions_if_not_in_change (chooser_entry);
864 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
866 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
867 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
871 gtk_file_chooser_entry_unmap (GtkWidget *widget)
873 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
875 remove_completion_feedback (chooser_entry);
877 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
881 completion_feedback_window_expose_event_cb (GtkWidget *widget,
882 GdkEventExpose *event,
885 /* Stolen from gtk_tooltip_paint_window() */
887 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
889 gtk_paint_flat_box (chooser_entry->completion_feedback_window->style,
890 chooser_entry->completion_feedback_window->window,
894 chooser_entry->completion_feedback_window,
897 chooser_entry->completion_feedback_window->allocation.width,
898 chooser_entry->completion_feedback_window->allocation.height);
904 set_invisible_mouse_cursor (GdkWindow *window)
909 display = gdk_drawable_get_display (window);
910 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
912 gdk_window_set_cursor (window, cursor);
914 gdk_cursor_unref (cursor);
918 completion_feedback_window_realize_cb (GtkWidget *widget,
921 /* We hide the mouse cursor inside the completion feedback window, since
922 * GtkEntry hides the cursor when the user types. We don't want the cursor to
923 * come back if the completion feedback ends up where the mouse is.
925 set_invisible_mouse_cursor (widget->window);
929 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
931 /* Stolen from gtk_tooltip_init() */
933 GtkWidget *alignment;
935 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
936 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
937 GDK_WINDOW_TYPE_HINT_TOOLTIP);
938 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
939 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
940 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
942 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
943 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
944 chooser_entry->completion_feedback_window->style->ythickness,
945 chooser_entry->completion_feedback_window->style->ythickness,
946 chooser_entry->completion_feedback_window->style->xthickness,
947 chooser_entry->completion_feedback_window->style->xthickness);
948 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
949 gtk_widget_show (alignment);
951 g_signal_connect (chooser_entry->completion_feedback_window, "expose-event",
952 G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
953 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
954 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
955 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
957 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
958 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
959 gtk_widget_show (chooser_entry->completion_feedback_label);
963 completion_feedback_timeout_cb (gpointer data)
965 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
967 chooser_entry->completion_feedback_timeout_id = 0;
969 remove_completion_feedback (chooser_entry);
974 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
976 if (chooser_entry->completion_feedback_timeout_id != 0)
977 g_source_remove (chooser_entry->completion_feedback_timeout_id);
979 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
980 completion_feedback_timeout_cb,
984 /* Gets the x position of the text cursor in the entry, in widget coordinates */
986 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
989 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
990 * the reader, you have to implement support for the entry's scroll offset and
991 * RTL layouts and all the fancy Pango stuff.
995 gint layout_x, layout_y;
997 PangoRectangle strong_pos;
999 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1001 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1003 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1004 GTK_ENTRY (chooser_entry)->current_pos);
1006 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1008 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1012 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1014 /* More or less stolen from gtk_tooltip_position() */
1016 GtkRequisition feedback_req;
1017 gint entry_x, entry_y;
1019 GtkAllocation *entry_allocation;
1020 int feedback_x, feedback_y;
1022 gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);
1024 gdk_window_get_origin (GTK_WIDGET (chooser_entry)->window, &entry_x, &entry_y);
1025 entry_allocation = &(GTK_WIDGET (chooser_entry)->allocation);
1027 get_entry_cursor_x (chooser_entry, &cursor_x);
1029 /* FIXME: fit to the screen if we bump on the screen's edge */
1030 feedback_x = entry_x + cursor_x + entry_allocation->height / 2; /* cheap "half M-width" */
1031 feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;
1033 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1034 gtk_widget_show (chooser_entry->completion_feedback_window);
1036 install_completion_feedback_timer (chooser_entry);
1040 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1041 const gchar *feedback)
1043 if (chooser_entry->completion_feedback_window == NULL)
1044 create_completion_feedback_window (chooser_entry);
1046 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1048 show_completion_feedback_window (chooser_entry);
1052 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1054 if (chooser_entry->completion_feedback_window)
1055 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1057 chooser_entry->completion_feedback_window = NULL;
1058 chooser_entry->completion_feedback_label = NULL;
1060 if (chooser_entry->completion_feedback_timeout_id != 0)
1062 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1063 chooser_entry->completion_feedback_timeout_id = 0;
1068 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1070 CommonPrefixResult result;
1072 g_assert (chooser_entry->current_folder != NULL);
1073 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1075 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1077 * - If there is a common prefix, insert it (done)
1078 * - If there is no common prefix, pop up the suggestion window
1079 * - If there are no matches at all, beep and bring up a tooltip (done)
1080 * - If the suggestion window is already up, scroll it
1082 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1087 /* We already beeped in append_common_prefix(); do nothing here */
1091 beep (chooser_entry);
1092 /* translators: this text is shown when there are no completions
1093 * for something the user typed in a file chooser entry
1095 pop_up_completion_feedback (chooser_entry, _("No match"));
1098 case NOTHING_INSERTED_COMPLETE:
1099 /* FIXME: pop up the suggestion window or scroll it */
1102 case NOTHING_INSERTED_UNIQUE:
1103 /* translators: this text is shown when there is exactly one completion
1104 * for something the user typed in a file chooser entry
1106 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1113 case COMPLETED_UNIQUE:
1117 case COMPLETE_BUT_NOT_UNIQUE:
1118 /* translators: this text is shown when the text in a file chooser
1119 * entry is a complete filename, but could be continued to find
1122 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1126 g_assert_not_reached ();
1131 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1133 RefreshStatus status;
1137 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1144 g_assert (chooser_entry->current_folder_file != NULL);
1146 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1147 explicitly_complete (chooser_entry);
1150 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1152 /* Translators: this text is shown while the system is searching
1153 * for possible completions for filenames in a file chooser entry. */
1154 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1159 case REFRESH_INVALID_INPUT:
1161 /* Translators: this is shown in the feedback for Tab-completion in a file
1162 * chooser's text entry, when the user enters an invalid path. */
1163 feedback_msg = _("Invalid path");
1166 case REFRESH_INCOMPLETE_HOSTNAME:
1169 if (chooser_entry->local_only)
1171 /* hostnames in a local_only file chooser? user error */
1173 /* Translators: this is shown in the feedback for Tab-completion in a
1174 * file chooser's text entry when the user enters something like
1175 * "sftp://blahblah" in an app that only supports local filenames. */
1176 feedback_msg = _("Only local files may be selected");
1180 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1182 /* Translators: this is shown in the feedback for Tab-completion in a
1183 * file chooser's text entry when the user hasn't entered the first '/'
1184 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1185 feedback_msg = _("Incomplete hostname; end it with '/'");
1190 case REFRESH_NONEXISTENT:
1193 /* Translators: this is shown in the feedback for Tab-completion in a file
1194 * chooser's text entry when the user enters a path that does not exist
1195 * and then hits Tab */
1196 feedback_msg = _("Path does not exist");
1199 case REFRESH_NOT_LOCAL:
1201 feedback_msg = _("Only local files may be selected");
1205 g_assert_not_reached ();
1211 g_assert (chooser_entry->current_folder_file == NULL);
1213 beep (chooser_entry);
1214 pop_up_completion_feedback (chooser_entry, feedback_msg);
1215 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1220 gtk_file_chooser_entry_focus (GtkWidget *widget,
1221 GtkDirectionType direction)
1223 GtkFileChooserEntry *chooser_entry;
1224 GtkEditable *editable;
1226 GdkModifierType state;
1227 gboolean control_pressed;
1229 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1230 editable = GTK_EDITABLE (widget);
1231 entry = GTK_ENTRY (widget);
1233 if (!chooser_entry->eat_tabs)
1234 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1236 control_pressed = FALSE;
1238 if (gtk_get_current_event_state (&state))
1240 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1241 control_pressed = TRUE;
1244 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1245 * makes it 'safe' for people to hit. */
1246 if ((direction == GTK_DIR_TAB_FORWARD) &&
1247 (GTK_WIDGET_HAS_FOCUS (widget)) &&
1248 (! control_pressed))
1250 if (chooser_entry->has_completion)
1251 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1253 start_explicit_completion (chooser_entry);
1258 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1262 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1263 GdkEventFocus *event)
1265 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1267 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1269 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1273 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1275 if (chooser_entry->has_completion)
1277 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1278 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1281 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1282 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1286 gtk_file_chooser_entry_activate (GtkEntry *entry)
1288 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1290 commit_completion_and_refresh (chooser_entry);
1291 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1295 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1297 if (!chooser_entry->completion_store)
1300 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1301 g_object_unref (chooser_entry->completion_store);
1302 chooser_entry->completion_store = NULL;
1305 /* Fills the completion store from the contents of the current folder */
1307 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1312 discard_completion_store (chooser_entry);
1314 files = _gtk_folder_list_children (chooser_entry->current_folder);
1316 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1320 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1325 file = tmp_list->data;
1327 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1331 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1335 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1337 gtk_list_store_append (chooser_entry->completion_store, &iter);
1338 gtk_list_store_set (chooser_entry->completion_store, &iter,
1339 DISPLAY_NAME_COLUMN, display_name,
1343 g_object_unref (info);
1344 g_free (display_name);
1348 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1349 g_slist_free (files);
1351 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1352 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1354 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1355 GTK_TREE_MODEL (chooser_entry->completion_store));
1358 /* When we finish loading the current folder, this function should get called to
1359 * perform the deferred autocompletion or explicit completion.
1362 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1364 switch (chooser_entry->load_complete_action)
1366 case LOAD_COMPLETE_NOTHING:
1369 case LOAD_COMPLETE_AUTOCOMPLETE:
1370 autocomplete (chooser_entry);
1373 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1374 explicitly_complete (chooser_entry);
1378 g_assert_not_reached ();
1381 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1385 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1387 populate_completion_store (chooser_entry);
1388 perform_load_complete_action (chooser_entry);
1390 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1393 /* Callback when the current folder finishes loading */
1395 finished_loading_cb (GtkFolder *folder,
1398 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1400 finish_folder_load (chooser_entry);
1403 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1405 load_directory_get_folder_callback (GCancellable *cancellable,
1407 const GError *error,
1410 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1411 GtkFileChooserEntry *chooser_entry = data;
1413 if (cancellable != chooser_entry->load_folder_cancellable)
1416 chooser_entry->load_folder_cancellable = NULL;
1420 LoadCompleteAction old_load_complete_action;
1422 old_load_complete_action = chooser_entry->load_complete_action;
1424 discard_completion_store (chooser_entry);
1425 clear_completions (chooser_entry);
1427 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1429 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1431 beep (chooser_entry);
1432 pop_up_completion_feedback (chooser_entry, error->message);
1435 discard_current_folder (chooser_entry);
1438 if (cancelled || error)
1441 g_assert (folder != NULL);
1442 chooser_entry->current_folder = g_object_ref (folder);
1444 discard_completion_store (chooser_entry);
1446 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1447 finish_folder_load (chooser_entry);
1449 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1450 G_CALLBACK (finished_loading_cb), chooser_entry);
1453 g_object_unref (chooser_entry);
1454 g_object_unref (cancellable);
1457 static RefreshStatus
1458 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1460 if (chooser_entry->file_system == NULL)
1463 g_assert (chooser_entry->current_folder_file != NULL);
1464 g_assert (chooser_entry->current_folder == NULL);
1465 g_assert (chooser_entry->load_folder_cancellable == NULL);
1467 if (chooser_entry->local_only
1468 && !g_file_is_native (chooser_entry->current_folder_file))
1470 g_object_unref (chooser_entry->current_folder_file);
1471 chooser_entry->current_folder_file = NULL;
1473 return REFRESH_NOT_LOCAL;
1476 chooser_entry->load_folder_cancellable =
1477 _gtk_file_system_get_folder (chooser_entry->file_system,
1478 chooser_entry->current_folder_file,
1479 "standard::name,standard::display-name,standard::type",
1480 load_directory_get_folder_callback,
1481 g_object_ref (chooser_entry));
1486 static RefreshStatus
1487 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1489 gboolean force_reload)
1491 gboolean reload = FALSE;
1493 g_assert (folder_file != NULL);
1495 if (chooser_entry->current_folder_file)
1497 if ((!(g_file_equal (folder_file, chooser_entry->current_folder_file)
1498 && chooser_entry->load_folder_cancellable))
1503 discard_current_folder (chooser_entry);
1504 discard_loading_and_current_folder_file (chooser_entry);
1506 chooser_entry->current_folder_file = g_object_ref (folder_file);
1511 chooser_entry->current_folder_file = g_object_ref (folder_file);
1516 return start_loading_current_folder (chooser_entry);
1521 static RefreshStatus
1522 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1523 RefreshMode refresh_mode)
1525 GtkEditable *editable;
1530 gsize total_len, file_part_len;
1533 RefreshStatus result;
1535 editable = GTK_EDITABLE (chooser_entry);
1537 switch (refresh_mode)
1539 case REFRESH_UP_TO_CURSOR_POSITION:
1540 end_pos = gtk_editable_get_position (editable);
1543 case REFRESH_WHOLE_TEXT:
1544 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1548 g_assert_not_reached ();
1549 return REFRESH_INVALID_INPUT;
1552 text = gtk_editable_get_chars (editable, 0, end_pos);
1555 if (!chooser_entry->file_system ||
1556 !chooser_entry->base_folder ||
1557 !_gtk_file_system_parse (chooser_entry->file_system,
1558 chooser_entry->base_folder, text,
1559 &folder_file, &file_part, &error))
1561 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1564 result = REFRESH_INCOMPLETE_HOSTNAME;
1568 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1570 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1571 result = REFRESH_NONEXISTENT;
1573 result = REFRESH_INVALID_INPUT;
1577 g_error_free (error);
1579 file_part = g_strdup ("");
1584 g_assert (folder_file != NULL);
1586 file_part_len = strlen (file_part);
1587 total_len = strlen (text);
1588 if (total_len > file_part_len)
1589 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1593 result = REFRESH_OK;
1598 g_free (chooser_entry->file_part);
1600 chooser_entry->file_part = file_part;
1601 chooser_entry->file_part_pos = file_part_pos;
1603 if (result == REFRESH_OK)
1605 result = reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1609 discard_current_folder (chooser_entry);
1610 discard_loading_and_current_folder_file (chooser_entry);
1614 g_object_unref (folder_file);
1616 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1617 ((result == REFRESH_OK)
1618 && (chooser_entry->current_folder_file != NULL)
1619 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1620 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1621 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1622 || ((result != REFRESH_OK)
1623 && (chooser_entry->current_folder_file == NULL)
1624 && (chooser_entry->load_folder_cancellable == NULL)
1625 && (chooser_entry->current_folder == NULL)));
1631 autocomplete (GtkFileChooserEntry *chooser_entry)
1633 if (!(chooser_entry->current_folder != NULL
1634 && _gtk_folder_is_finished_loading (chooser_entry->current_folder)
1635 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1638 append_common_prefix (chooser_entry, TRUE, FALSE);
1642 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1644 RefreshStatus status;
1646 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1651 g_assert (chooser_entry->current_folder_file != NULL);
1653 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1654 autocomplete (chooser_entry);
1656 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1660 case REFRESH_INVALID_INPUT:
1661 case REFRESH_INCOMPLETE_HOSTNAME:
1662 case REFRESH_NONEXISTENT:
1663 case REFRESH_NOT_LOCAL:
1664 /* We don't beep or anything, since this is autocompletion - the user
1665 * didn't request any action explicitly.
1670 g_assert_not_reached ();
1675 start_autocompletion_idle_handler (gpointer data)
1677 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1679 start_autocompletion (chooser_entry);
1681 chooser_entry->start_autocompletion_idle_id = 0;
1687 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1689 if (chooser_entry->start_autocompletion_idle_id != 0)
1692 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1697 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1698 const gchar *new_text,
1699 gint new_text_length,
1703 const gchar *colon = memchr (new_text, ':', new_text_length);
1706 /* Disallow these characters altogether */
1707 for (i = 0; i < new_text_length; i++)
1709 if (new_text[i] == '<' ||
1710 new_text[i] == '>' ||
1711 new_text[i] == '"' ||
1712 new_text[i] == '|' ||
1713 new_text[i] == '*' ||
1718 if (i < new_text_length ||
1719 /* Disallow entering text that would cause a colon to be anywhere except
1720 * after a drive letter.
1723 *position + (colon - new_text) != 1) ||
1724 (new_text_length > 0 &&
1726 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1727 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1729 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1730 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1738 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1743 /* If deleting a drive letter, delete the colon, too */
1744 if (start_pos == 0 && end_pos == 1 &&
1745 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1746 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1748 g_signal_handlers_block_by_func (chooser_entry,
1749 G_CALLBACK (delete_text_callback),
1751 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1752 g_signal_handlers_unblock_by_func (chooser_entry,
1753 G_CALLBACK (delete_text_callback),
1760 * _gtk_file_chooser_entry_new:
1761 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1763 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1764 * is an internal implementation widget for the GTK+ file chooser
1765 * which is an entry with completion with respect to a
1766 * #GtkFileSystem object.
1768 * Return value: the newly created #GtkFileChooserEntry
1771 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1773 GtkFileChooserEntry *chooser_entry;
1775 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1776 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1778 return GTK_WIDGET (chooser_entry);
1782 * _gtk_file_chooser_entry_set_file_system:
1783 * @chooser_entry: a #GtkFileChooser
1784 * @file_system: an object implementing #GtkFileSystem
1786 * Sets the file system for @chooser_entry.
1789 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1790 GtkFileSystem *file_system)
1792 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1793 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1795 if (file_system != chooser_entry->file_system)
1797 if (chooser_entry->file_system)
1798 g_object_unref (chooser_entry->file_system);
1800 chooser_entry->file_system = g_object_ref (file_system);
1805 * _gtk_file_chooser_entry_set_base_folder:
1806 * @chooser_entry: a #GtkFileChooserEntry
1807 * @file: file for a folder in the chooser entries current file system.
1809 * Sets the folder with respect to which completions occur.
1812 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1815 if (chooser_entry->base_folder)
1816 g_object_unref (chooser_entry->base_folder);
1818 chooser_entry->base_folder = file;
1820 if (chooser_entry->base_folder)
1821 g_object_ref (chooser_entry->base_folder);
1823 clear_completions (chooser_entry);
1824 _gtk_file_chooser_entry_select_filename (chooser_entry);
1828 * _gtk_file_chooser_entry_get_current_folder:
1829 * @chooser_entry: a #GtkFileChooserEntry
1831 * Gets the current folder for the #GtkFileChooserEntry. If the
1832 * user has only entered a filename, this will be in the base folder
1833 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1834 * user has entered a relative or absolute path, then it will
1835 * be different. If the user has entered unparsable text, or text which
1836 * the entry cannot handle, this will return %NULL.
1838 * Return value: the file for the current folder - this value is owned by the
1839 * chooser entry and must not be modified or freed.
1842 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1844 commit_completion_and_refresh (chooser_entry);
1845 return chooser_entry->current_folder_file;
1849 * _gtk_file_chooser_entry_get_file_part:
1850 * @chooser_entry: a #GtkFileChooserEntry
1852 * Gets the non-folder portion of whatever the user has entered
1853 * into the file selector. What is returned is a UTF-8 string,
1854 * and if a filename path is needed, g_file_get_child_for_display_name()
1857 * Return value: the entered filename - this value is owned by the
1858 * chooser entry and must not be modified or freed.
1861 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1863 commit_completion_and_refresh (chooser_entry);
1864 return chooser_entry->file_part;
1868 * _gtk_file_chooser_entry_set_file_part:
1869 * @chooser_entry: a #GtkFileChooserEntry
1870 * @file_part: text to display in the entry, in UTF-8
1872 * Sets the current text shown in the file chooser entry.
1875 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1876 const gchar *file_part)
1878 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1880 chooser_entry->in_change = TRUE;
1881 clear_completions (chooser_entry);
1882 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1883 chooser_entry->in_change = FALSE;
1888 * _gtk_file_chooser_entry_set_action:
1889 * @chooser_entry: a #GtkFileChooserEntry
1890 * @action: the action which is performed by the file selector using this entry
1892 * Sets action which is performed by the file selector using this entry.
1893 * The #GtkFileChooserEntry will use different completion strategies for
1894 * different actions.
1897 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1898 GtkFileChooserAction action)
1900 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1902 if (chooser_entry->action != action)
1904 GtkEntryCompletion *comp;
1906 chooser_entry->action = action;
1908 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1910 /* FIXME: do we need to actually set the following? */
1914 case GTK_FILE_CHOOSER_ACTION_OPEN:
1915 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1916 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1918 case GTK_FILE_CHOOSER_ACTION_SAVE:
1919 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1920 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1928 * _gtk_file_chooser_entry_get_action:
1929 * @chooser_entry: a #GtkFileChooserEntry
1931 * Gets the action for this entry.
1933 * Returns: the action
1935 GtkFileChooserAction
1936 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1938 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1939 GTK_FILE_CHOOSER_ACTION_OPEN);
1941 return chooser_entry->action;
1945 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1948 gboolean retval = FALSE;
1950 if (chooser_entry->current_folder)
1952 GFileInfo *file_info;
1954 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1958 retval = _gtk_file_info_consider_as_directory (file_info);
1959 g_object_unref (file_info);
1968 * _gtk_file_chooser_entry_select_filename:
1969 * @chooser_entry: a #GtkFileChooserEntry
1971 * Selects the filename (without the extension) for user edition.
1974 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1976 const gchar *str, *ext;
1979 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1981 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1982 ext = g_strrstr (str, ".");
1985 len = g_utf8_pointer_to_offset (str, ext);
1988 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1992 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1993 gboolean local_only)
1995 chooser_entry->local_only = local_only;
1996 clear_completions (chooser_entry);
2000 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
2002 return chooser_entry->local_only;