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"
34 #include "gdkkeysyms.h"
36 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
38 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
39 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
40 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
42 struct _GtkFileChooserEntryClass
44 GtkEntryClass parent_class;
47 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
49 LOAD_COMPLETE_NOTHING,
50 LOAD_COMPLETE_AUTOCOMPLETE,
51 LOAD_COMPLETE_EXPLICIT_COMPLETION
57 REFRESH_INVALID_INPUT,
58 REFRESH_INCOMPLETE_HOSTNAME,
63 struct _GtkFileChooserEntry
65 GtkEntry parent_instance;
67 GtkFileChooserAction action;
69 GtkFileSystem *file_system;
71 GFile *current_folder_file;
75 /* Folder being loaded or already loaded */
76 GtkFolder *current_folder;
77 GCancellable *load_folder_cancellable;
79 LoadCompleteAction load_complete_action;
81 GtkListStore *completion_store;
83 guint start_autocompletion_idle_id;
85 GtkWidget *completion_feedback_window;
86 GtkWidget *completion_feedback_label;
87 guint completion_feedback_timeout_id;
89 guint has_completion : 1;
102 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
104 static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface);
106 static void gtk_file_chooser_entry_finalize (GObject *object);
107 static void gtk_file_chooser_entry_dispose (GObject *object);
108 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
109 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
110 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
112 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
113 GdkEventFocus *event);
114 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
115 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
116 const gchar *new_text,
117 gint new_text_length,
119 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
122 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
124 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
129 static gint insert_text_callback (GtkFileChooserEntry *widget,
130 const gchar *new_text,
131 gint new_text_length,
134 static void delete_text_callback (GtkFileChooserEntry *widget,
140 static gboolean match_selected_callback (GtkEntryCompletion *completion,
143 GtkFileChooserEntry *chooser_entry);
144 static gboolean completion_match_func (GtkEntryCompletion *comp,
148 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
154 REFRESH_UP_TO_CURSOR_POSITION,
158 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
159 RefreshMode refresh_mode);
160 static void finished_loading_cb (GtkFolder *folder,
162 static void autocomplete (GtkFileChooserEntry *chooser_entry);
163 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
164 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
165 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
166 const gchar *feedback);
168 static GtkEditableClass *parent_editable_iface;
170 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
171 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
172 gtk_file_chooser_entry_iface_init))
175 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
177 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
178 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
179 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
181 gobject_class->finalize = gtk_file_chooser_entry_finalize;
182 gobject_class->dispose = gtk_file_chooser_entry_dispose;
184 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
185 widget_class->unmap = gtk_file_chooser_entry_unmap;
186 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
187 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
189 entry_class->activate = gtk_file_chooser_entry_activate;
193 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
195 parent_editable_iface = g_type_interface_peek_parent (iface);
197 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
198 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
199 iface->set_position = gtk_file_chooser_entry_set_position;
200 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
204 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
206 GtkEntryCompletion *comp;
207 GtkCellRenderer *cell;
209 chooser_entry->local_only = TRUE;
211 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
213 comp = gtk_entry_completion_new ();
214 gtk_entry_completion_set_popup_single_match (comp, FALSE);
216 gtk_entry_completion_set_match_func (comp,
217 completion_match_func,
221 cell = gtk_cell_renderer_text_new ();
222 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
224 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
228 g_signal_connect (comp, "match-selected",
229 G_CALLBACK (match_selected_callback), chooser_entry);
231 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
232 g_object_unref (comp);
235 g_signal_connect (chooser_entry, "insert-text",
236 G_CALLBACK (insert_text_callback), NULL);
237 g_signal_connect (chooser_entry, "delete-text",
238 G_CALLBACK (delete_text_callback), NULL);
243 gtk_file_chooser_entry_finalize (GObject *object)
245 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
247 if (chooser_entry->base_folder)
248 g_object_unref (chooser_entry->base_folder);
250 if (chooser_entry->current_folder)
251 g_object_unref (chooser_entry->current_folder);
253 if (chooser_entry->current_folder_file)
254 g_object_unref (chooser_entry->current_folder_file);
256 g_free (chooser_entry->file_part);
258 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
262 discard_current_folder (GtkFileChooserEntry *chooser_entry)
264 if (chooser_entry->current_folder)
266 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
267 G_CALLBACK (finished_loading_cb), chooser_entry);
268 g_object_unref (chooser_entry->current_folder);
269 chooser_entry->current_folder = NULL;
274 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
276 if (chooser_entry->load_folder_cancellable)
278 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
279 chooser_entry->load_folder_cancellable = NULL;
282 if (chooser_entry->current_folder_file)
284 g_object_unref (chooser_entry->current_folder_file);
285 chooser_entry->current_folder_file = NULL;
290 gtk_file_chooser_entry_dispose (GObject *object)
292 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
294 remove_completion_feedback (chooser_entry);
295 discard_current_folder (chooser_entry);
296 discard_loading_and_current_folder_file (chooser_entry);
298 if (chooser_entry->start_autocompletion_idle_id != 0)
300 g_source_remove (chooser_entry->start_autocompletion_idle_id);
301 chooser_entry->start_autocompletion_idle_id = 0;
304 if (chooser_entry->completion_store)
306 g_object_unref (chooser_entry->completion_store);
307 chooser_entry->completion_store = NULL;
310 if (chooser_entry->file_system)
312 g_object_unref (chooser_entry->file_system);
313 chooser_entry->file_system = NULL;
316 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
319 /* Match functions for the GtkEntryCompletion */
321 match_selected_callback (GtkEntryCompletion *completion,
324 GtkFileChooserEntry *chooser_entry)
331 gtk_tree_model_get (model, iter,
332 DISPLAY_NAME_COLUMN, &display_name,
336 if (!display_name || !file)
338 /* these shouldn't complain if passed NULL */
339 g_object_unref (file);
340 g_free (display_name);
344 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
346 pos = chooser_entry->file_part_pos;
348 /* We don't set in_change here as we want to update the current_folder
350 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
352 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
355 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
357 g_object_unref (file);
358 g_free (display_name);
363 /* Match function for the GtkEntryCompletion */
365 completion_match_func (GtkEntryCompletion *comp,
366 const char *key_unused,
370 GtkFileChooserEntry *chooser_entry;
373 char *norm_file_part;
376 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
378 /* We ignore the key because it is the contents of the entry. Instead, we
379 * just use our precomputed file_part.
381 if (!chooser_entry->file_part)
386 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
389 return FALSE; /* Uninitialized row, ugh */
392 /* If we have an empty file_part, then we're at the root of a directory. In
393 * that case, we want to match all non-dot files. We might want to match
394 * dot_files too if show_hidden is TRUE on the fileselector in the future.
396 /* Additionally, support for gnome .hidden files would be sweet, too */
397 if (chooser_entry->file_part[0] == '\000')
409 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
410 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
412 #ifdef G_PLATFORM_WIN32
416 temp = norm_file_part;
417 norm_file_part = g_utf8_casefold (norm_file_part, -1);
421 norm_name = g_utf8_casefold (norm_name, -1);
426 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
428 g_free (norm_file_part);
436 clear_completions (GtkFileChooserEntry *chooser_entry)
438 chooser_entry->has_completion = FALSE;
439 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
441 remove_completion_feedback (chooser_entry);
445 beep (GtkFileChooserEntry *chooser_entry)
447 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
450 /* This function will append a directory separator to paths to
451 * display_name iff the path associated with it is a directory.
452 * maybe_append_separator_to_file will g_free the display_name and
453 * return a new one if needed. Otherwise, it will return the old one.
454 * You should be safe calling
456 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
458 * g_free (display_name);
461 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
468 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
472 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
476 if (_gtk_file_info_consider_as_directory (info))
478 gchar *tmp = display_name;
479 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
484 g_object_unref (info);
492 trim_dir_separator_suffix (const char *str)
497 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
498 return g_strndup (str, len - 1);
500 return g_strdup (str);
503 /* Determines if the completion model has entries with a common prefix relative
504 * to the current contents of the entry. Also, if there's one and only one such
505 * path, stores it in unique_path_ret.
508 find_common_prefix (GtkFileChooserEntry *chooser_entry,
509 gchar **common_prefix_ret,
510 GFile **unique_file_ret,
511 gboolean *is_complete_not_unique_ret,
512 gboolean *prefix_expands_the_file_part_ret,
515 GtkEditable *editable;
519 char *text_up_to_cursor;
520 GFile *parsed_folder_file;
521 char *parsed_file_part;
523 *common_prefix_ret = NULL;
524 *unique_file_ret = NULL;
525 *is_complete_not_unique_ret = FALSE;
526 *prefix_expands_the_file_part_ret = FALSE;
528 editable = GTK_EDITABLE (chooser_entry);
530 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
532 parsed = _gtk_file_system_parse (chooser_entry->file_system,
533 chooser_entry->base_folder,
539 g_free (text_up_to_cursor);
544 g_assert (parsed_folder_file != NULL
545 && chooser_entry->current_folder != NULL
546 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
548 g_object_unref (parsed_folder_file);
550 /* First pass: find the common prefix */
552 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
559 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
561 DISPLAY_NAME_COLUMN, &display_name,
565 if (g_str_has_prefix (display_name, parsed_file_part))
567 if (!*common_prefix_ret)
569 *common_prefix_ret = trim_dir_separator_suffix (display_name);
570 *unique_file_ret = g_object_ref (file);
574 gchar *p = *common_prefix_ret;
575 const gchar *q = display_name;
577 while (*p && *p == *q)
585 if (*unique_file_ret)
587 g_object_unref (*unique_file_ret);
588 *unique_file_ret = NULL;
593 g_free (display_name);
594 g_object_unref (file);
595 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
598 /* Second pass: see if the prefix we found is a complete match */
600 if (*common_prefix_ret != NULL)
602 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
609 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
611 DISPLAY_NAME_COLUMN, &display_name,
613 len = strlen (display_name);
616 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
619 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
620 *is_complete_not_unique_ret = TRUE;
622 g_free (display_name);
623 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
626 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
628 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
631 g_free (parsed_file_part);
637 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
644 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
645 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
649 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
650 if (G_IS_DIR_SEPARATOR (*next_char_str))
653 g_free (next_char_str);
660 INVALID_INPUT, /* what the user typed is bogus */
661 NO_MATCH, /* no matches based on what the user typed */
662 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
663 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
664 COMPLETED, /* completion inserted (ambiguous suffix) */
665 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
666 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
667 } CommonPrefixResult;
669 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
670 static CommonPrefixResult
671 append_common_prefix (GtkFileChooserEntry *chooser_entry,
673 gboolean show_errors)
675 gchar *common_prefix;
677 gboolean is_complete_not_unique;
678 gboolean prefix_expands_the_file_part;
680 CommonPrefixResult result = NO_MATCH;
681 gboolean have_result;
683 clear_completions (chooser_entry);
685 if (chooser_entry->completion_store == NULL)
689 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
691 /* If the user types an incomplete hostname ("http://foo" without a slash
692 * after that), it's not an error. We just don't want to pop up a
693 * meaningless completion window in that state.
695 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
698 beep (chooser_entry);
699 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
702 g_error_free (error);
704 return INVALID_INPUT;
711 if (!char_after_cursor_is_directory_separator (chooser_entry))
715 common_prefix = maybe_append_separator_to_file (chooser_entry,
720 prefix_expands_the_file_part = TRUE;
723 g_object_unref (unique_file);
725 if (prefix_expands_the_file_part)
726 result = COMPLETED_UNIQUE;
728 result = NOTHING_INSERTED_UNIQUE;
734 if (is_complete_not_unique)
736 result = COMPLETE_BUT_NOT_UNIQUE;
744 gint common_prefix_len;
747 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
748 common_prefix_len = g_utf8_strlen (common_prefix, -1);
750 pos = chooser_entry->file_part_pos;
752 if (prefix_expands_the_file_part)
754 chooser_entry->in_change = TRUE;
755 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
757 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
760 chooser_entry->in_change = FALSE;
764 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
766 pos); /* equivalent to cursor_pos + common_prefix_len); */
767 chooser_entry->has_completion = TRUE;
770 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
772 else if (!have_result)
774 result = NOTHING_INSERTED_COMPLETE;
778 g_free (common_prefix);
795 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
796 const gchar *new_text,
797 gint new_text_length,
800 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
804 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
805 insert_pos = *position;
807 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
809 if (chooser_entry->in_change)
812 remove_completion_feedback (chooser_entry);
814 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
815 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
816 && insert_pos == old_text_len)
817 install_start_autocompletion_idle (chooser_entry);
821 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
823 if (chooser_entry->in_change)
826 clear_completions (chooser_entry);
830 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
834 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
836 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
838 clear_completions_if_not_in_change (chooser_entry);
842 gtk_file_chooser_entry_set_position (GtkEditable *editable,
845 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
847 parent_editable_iface->set_position (editable, position);
849 clear_completions_if_not_in_change (chooser_entry);
853 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
857 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
859 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
861 clear_completions_if_not_in_change (chooser_entry);
865 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
867 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
868 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
872 gtk_file_chooser_entry_unmap (GtkWidget *widget)
874 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
876 remove_completion_feedback (chooser_entry);
878 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
882 completion_feedback_window_expose_event_cb (GtkWidget *widget,
883 GdkEventExpose *event,
886 /* Stolen from gtk_tooltip_paint_window() */
888 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
890 gtk_paint_flat_box (chooser_entry->completion_feedback_window->style,
891 chooser_entry->completion_feedback_window->window,
895 chooser_entry->completion_feedback_window,
898 chooser_entry->completion_feedback_window->allocation.width,
899 chooser_entry->completion_feedback_window->allocation.height);
905 set_invisible_mouse_cursor (GdkWindow *window)
910 display = gdk_drawable_get_display (window);
911 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
913 gdk_window_set_cursor (window, cursor);
915 gdk_cursor_unref (cursor);
919 completion_feedback_window_realize_cb (GtkWidget *widget,
922 /* We hide the mouse cursor inside the completion feedback window, since
923 * GtkEntry hides the cursor when the user types. We don't want the cursor to
924 * come back if the completion feedback ends up where the mouse is.
926 set_invisible_mouse_cursor (widget->window);
930 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
932 /* Stolen from gtk_tooltip_init() */
934 GtkWidget *alignment;
936 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
937 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
938 GDK_WINDOW_TYPE_HINT_TOOLTIP);
939 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
940 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
941 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
943 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
944 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
945 chooser_entry->completion_feedback_window->style->ythickness,
946 chooser_entry->completion_feedback_window->style->ythickness,
947 chooser_entry->completion_feedback_window->style->xthickness,
948 chooser_entry->completion_feedback_window->style->xthickness);
949 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
950 gtk_widget_show (alignment);
952 g_signal_connect (chooser_entry->completion_feedback_window, "expose-event",
953 G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
954 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
955 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
956 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
958 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
959 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
960 gtk_widget_show (chooser_entry->completion_feedback_label);
964 completion_feedback_timeout_cb (gpointer data)
966 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
968 chooser_entry->completion_feedback_timeout_id = 0;
970 remove_completion_feedback (chooser_entry);
975 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
977 if (chooser_entry->completion_feedback_timeout_id != 0)
978 g_source_remove (chooser_entry->completion_feedback_timeout_id);
980 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
981 completion_feedback_timeout_cb,
985 /* Gets the x position of the text cursor in the entry, in widget coordinates */
987 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
990 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
991 * the reader, you have to implement support for the entry's scroll offset and
992 * RTL layouts and all the fancy Pango stuff.
996 gint layout_x, layout_y;
998 PangoRectangle strong_pos;
1000 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1002 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1004 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1005 GTK_ENTRY (chooser_entry)->current_pos);
1007 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1009 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1013 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1015 /* More or less stolen from gtk_tooltip_position() */
1017 GtkRequisition feedback_req;
1018 gint entry_x, entry_y;
1020 GtkAllocation *entry_allocation;
1021 int feedback_x, feedback_y;
1023 gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);
1025 gdk_window_get_origin (GTK_WIDGET (chooser_entry)->window, &entry_x, &entry_y);
1026 entry_allocation = &(GTK_WIDGET (chooser_entry)->allocation);
1028 get_entry_cursor_x (chooser_entry, &cursor_x);
1030 /* FIXME: fit to the screen if we bump on the screen's edge */
1031 /* cheap "half M-width", use height as approximation of character em-size */
1032 feedback_x = entry_x + cursor_x + entry_allocation->height / 2;
1033 feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;
1035 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1036 gtk_widget_show (chooser_entry->completion_feedback_window);
1038 install_completion_feedback_timer (chooser_entry);
1042 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1043 const gchar *feedback)
1045 if (chooser_entry->completion_feedback_window == NULL)
1046 create_completion_feedback_window (chooser_entry);
1048 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1050 show_completion_feedback_window (chooser_entry);
1054 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1056 if (chooser_entry->completion_feedback_window)
1057 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1059 chooser_entry->completion_feedback_window = NULL;
1060 chooser_entry->completion_feedback_label = NULL;
1062 if (chooser_entry->completion_feedback_timeout_id != 0)
1064 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1065 chooser_entry->completion_feedback_timeout_id = 0;
1070 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1072 CommonPrefixResult result;
1074 g_assert (chooser_entry->current_folder != NULL);
1075 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1077 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1079 * - If there is a common prefix, insert it (done)
1080 * - If there is no common prefix, pop up the suggestion window
1081 * - If there are no matches at all, beep and bring up a tooltip (done)
1082 * - If the suggestion window is already up, scroll it
1084 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1089 /* We already beeped in append_common_prefix(); do nothing here */
1093 beep (chooser_entry);
1094 /* translators: this text is shown when there are no completions
1095 * for something the user typed in a file chooser entry
1097 pop_up_completion_feedback (chooser_entry, _("No match"));
1100 case NOTHING_INSERTED_COMPLETE:
1101 /* FIXME: pop up the suggestion window or scroll it */
1104 case NOTHING_INSERTED_UNIQUE:
1105 /* translators: this text is shown when there is exactly one completion
1106 * for something the user typed in a file chooser entry
1108 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1115 case COMPLETED_UNIQUE:
1119 case COMPLETE_BUT_NOT_UNIQUE:
1120 /* translators: this text is shown when the text in a file chooser
1121 * entry is a complete filename, but could be continued to find
1124 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1128 g_assert_not_reached ();
1133 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1135 RefreshStatus status;
1139 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1146 g_assert (chooser_entry->current_folder_file != NULL);
1148 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1149 explicitly_complete (chooser_entry);
1152 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1154 /* Translators: this text is shown while the system is searching
1155 * for possible completions for filenames in a file chooser entry. */
1156 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1161 case REFRESH_INVALID_INPUT:
1163 /* Translators: this is shown in the feedback for Tab-completion in a file
1164 * chooser's text entry, when the user enters an invalid path. */
1165 feedback_msg = _("Invalid path");
1168 case REFRESH_INCOMPLETE_HOSTNAME:
1171 if (chooser_entry->local_only)
1173 /* hostnames in a local_only file chooser? user error */
1175 /* Translators: this is shown in the feedback for Tab-completion in a
1176 * file chooser's text entry when the user enters something like
1177 * "sftp://blahblah" in an app that only supports local filenames. */
1178 feedback_msg = _("Only local files may be selected");
1182 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1184 /* Translators: this is shown in the feedback for Tab-completion in a
1185 * file chooser's text entry when the user hasn't entered the first '/'
1186 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1187 feedback_msg = _("Incomplete hostname; end it with '/'");
1192 case REFRESH_NONEXISTENT:
1195 /* Translators: this is shown in the feedback for Tab-completion in a file
1196 * chooser's text entry when the user enters a path that does not exist
1197 * and then hits Tab */
1198 feedback_msg = _("Path does not exist");
1201 case REFRESH_NOT_LOCAL:
1203 feedback_msg = _("Only local files may be selected");
1207 g_assert_not_reached ();
1213 g_assert (chooser_entry->current_folder_file == NULL);
1215 beep (chooser_entry);
1216 pop_up_completion_feedback (chooser_entry, feedback_msg);
1217 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1222 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1225 GtkFileChooserEntry *chooser_entry;
1226 GtkEditable *editable;
1228 GdkModifierType state;
1229 gboolean control_pressed;
1231 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1232 editable = GTK_EDITABLE (widget);
1233 entry = GTK_ENTRY (widget);
1235 if (!chooser_entry->eat_tabs)
1236 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1238 control_pressed = FALSE;
1240 if (gtk_get_current_event_state (&state))
1242 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1243 control_pressed = TRUE;
1246 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1247 * makes it 'safe' for people to hit. */
1248 if (event->keyval == GDK_Tab && !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)->key_press_event (widget, event);
1263 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1264 GdkEventFocus *event)
1266 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1268 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1270 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1274 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1276 if (chooser_entry->has_completion)
1278 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1279 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1282 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1283 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1287 gtk_file_chooser_entry_activate (GtkEntry *entry)
1289 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1291 commit_completion_and_refresh (chooser_entry);
1292 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1296 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1298 if (!chooser_entry->completion_store)
1301 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1302 g_object_unref (chooser_entry->completion_store);
1303 chooser_entry->completion_store = NULL;
1306 /* Fills the completion store from the contents of the current folder */
1308 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1313 discard_completion_store (chooser_entry);
1315 files = _gtk_folder_list_children (chooser_entry->current_folder);
1317 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1321 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1326 file = tmp_list->data;
1328 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1332 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1336 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1338 gtk_list_store_append (chooser_entry->completion_store, &iter);
1339 gtk_list_store_set (chooser_entry->completion_store, &iter,
1340 DISPLAY_NAME_COLUMN, display_name,
1344 g_object_unref (info);
1345 g_free (display_name);
1349 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1350 g_slist_free (files);
1352 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1353 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1355 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1356 GTK_TREE_MODEL (chooser_entry->completion_store));
1359 /* When we finish loading the current folder, this function should get called to
1360 * perform the deferred autocompletion or explicit completion.
1363 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1365 switch (chooser_entry->load_complete_action)
1367 case LOAD_COMPLETE_NOTHING:
1370 case LOAD_COMPLETE_AUTOCOMPLETE:
1371 autocomplete (chooser_entry);
1374 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1375 explicitly_complete (chooser_entry);
1379 g_assert_not_reached ();
1382 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1386 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1388 populate_completion_store (chooser_entry);
1389 perform_load_complete_action (chooser_entry);
1391 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1394 /* Callback when the current folder finishes loading */
1396 finished_loading_cb (GtkFolder *folder,
1399 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1401 finish_folder_load (chooser_entry);
1404 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1406 load_directory_get_folder_callback (GCancellable *cancellable,
1408 const GError *error,
1411 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1412 GtkFileChooserEntry *chooser_entry = data;
1414 if (cancellable != chooser_entry->load_folder_cancellable)
1417 chooser_entry->load_folder_cancellable = NULL;
1421 LoadCompleteAction old_load_complete_action;
1423 old_load_complete_action = chooser_entry->load_complete_action;
1425 discard_completion_store (chooser_entry);
1426 clear_completions (chooser_entry);
1428 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1430 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1432 beep (chooser_entry);
1433 pop_up_completion_feedback (chooser_entry, error->message);
1436 discard_current_folder (chooser_entry);
1439 if (cancelled || error)
1442 g_assert (folder != NULL);
1443 chooser_entry->current_folder = g_object_ref (folder);
1445 discard_completion_store (chooser_entry);
1447 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1448 finish_folder_load (chooser_entry);
1450 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1451 G_CALLBACK (finished_loading_cb), chooser_entry);
1454 g_object_unref (chooser_entry);
1455 g_object_unref (cancellable);
1458 static RefreshStatus
1459 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1461 if (chooser_entry->file_system == NULL)
1464 g_assert (chooser_entry->current_folder_file != NULL);
1465 g_assert (chooser_entry->current_folder == NULL);
1466 g_assert (chooser_entry->load_folder_cancellable == NULL);
1468 if (chooser_entry->local_only
1469 && !g_file_is_native (chooser_entry->current_folder_file))
1471 g_object_unref (chooser_entry->current_folder_file);
1472 chooser_entry->current_folder_file = NULL;
1474 return REFRESH_NOT_LOCAL;
1477 chooser_entry->load_folder_cancellable =
1478 _gtk_file_system_get_folder (chooser_entry->file_system,
1479 chooser_entry->current_folder_file,
1480 "standard::name,standard::display-name,standard::type",
1481 load_directory_get_folder_callback,
1482 g_object_ref (chooser_entry));
1487 static RefreshStatus
1488 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1490 gboolean force_reload)
1492 gboolean reload = FALSE;
1494 g_assert (folder_file != NULL);
1496 if (chooser_entry->current_folder_file)
1498 if ((!(g_file_equal (folder_file, chooser_entry->current_folder_file)
1499 && chooser_entry->load_folder_cancellable))
1504 discard_current_folder (chooser_entry);
1505 discard_loading_and_current_folder_file (chooser_entry);
1507 chooser_entry->current_folder_file = g_object_ref (folder_file);
1512 chooser_entry->current_folder_file = g_object_ref (folder_file);
1517 return start_loading_current_folder (chooser_entry);
1522 static RefreshStatus
1523 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1524 RefreshMode refresh_mode)
1526 GtkEditable *editable;
1531 gsize total_len, file_part_len;
1534 RefreshStatus result;
1536 editable = GTK_EDITABLE (chooser_entry);
1538 switch (refresh_mode)
1540 case REFRESH_UP_TO_CURSOR_POSITION:
1541 end_pos = gtk_editable_get_position (editable);
1544 case REFRESH_WHOLE_TEXT:
1545 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1549 g_assert_not_reached ();
1550 return REFRESH_INVALID_INPUT;
1553 text = gtk_editable_get_chars (editable, 0, end_pos);
1556 if (!chooser_entry->file_system ||
1557 !chooser_entry->base_folder ||
1558 !_gtk_file_system_parse (chooser_entry->file_system,
1559 chooser_entry->base_folder, text,
1560 &folder_file, &file_part, &error))
1562 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1565 result = REFRESH_INCOMPLETE_HOSTNAME;
1569 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1571 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1572 result = REFRESH_NONEXISTENT;
1574 result = REFRESH_INVALID_INPUT;
1578 g_error_free (error);
1580 file_part = g_strdup ("");
1585 g_assert (folder_file != NULL);
1587 file_part_len = strlen (file_part);
1588 total_len = strlen (text);
1589 if (total_len > file_part_len)
1590 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1594 result = REFRESH_OK;
1599 g_free (chooser_entry->file_part);
1601 chooser_entry->file_part = file_part;
1602 chooser_entry->file_part_pos = file_part_pos;
1604 if (result == REFRESH_OK)
1606 result = reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1610 discard_current_folder (chooser_entry);
1611 discard_loading_and_current_folder_file (chooser_entry);
1615 g_object_unref (folder_file);
1617 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1618 ((result == REFRESH_OK)
1619 && (chooser_entry->current_folder_file != NULL)
1620 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1621 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1622 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1623 || ((result != REFRESH_OK)
1624 && (chooser_entry->current_folder_file == NULL)
1625 && (chooser_entry->load_folder_cancellable == NULL)
1626 && (chooser_entry->current_folder == NULL)));
1632 autocomplete (GtkFileChooserEntry *chooser_entry)
1634 if (!(chooser_entry->current_folder != NULL
1635 && _gtk_folder_is_finished_loading (chooser_entry->current_folder)
1636 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1639 append_common_prefix (chooser_entry, TRUE, FALSE);
1643 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1645 RefreshStatus status;
1647 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1652 g_assert (chooser_entry->current_folder_file != NULL);
1654 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1655 autocomplete (chooser_entry);
1657 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1661 case REFRESH_INVALID_INPUT:
1662 case REFRESH_INCOMPLETE_HOSTNAME:
1663 case REFRESH_NONEXISTENT:
1664 case REFRESH_NOT_LOCAL:
1665 /* We don't beep or anything, since this is autocompletion - the user
1666 * didn't request any action explicitly.
1671 g_assert_not_reached ();
1676 start_autocompletion_idle_handler (gpointer data)
1678 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1680 start_autocompletion (chooser_entry);
1682 chooser_entry->start_autocompletion_idle_id = 0;
1688 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1690 if (chooser_entry->start_autocompletion_idle_id != 0)
1693 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1698 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1699 const gchar *new_text,
1700 gint new_text_length,
1704 const gchar *colon = memchr (new_text, ':', new_text_length);
1707 /* Disallow these characters altogether */
1708 for (i = 0; i < new_text_length; i++)
1710 if (new_text[i] == '<' ||
1711 new_text[i] == '>' ||
1712 new_text[i] == '"' ||
1713 new_text[i] == '|' ||
1714 new_text[i] == '*' ||
1719 if (i < new_text_length ||
1720 /* Disallow entering text that would cause a colon to be anywhere except
1721 * after a drive letter.
1724 *position + (colon - new_text) != 1) ||
1725 (new_text_length > 0 &&
1727 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1728 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1730 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1731 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1739 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1744 /* If deleting a drive letter, delete the colon, too */
1745 if (start_pos == 0 && end_pos == 1 &&
1746 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1747 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1749 g_signal_handlers_block_by_func (chooser_entry,
1750 G_CALLBACK (delete_text_callback),
1752 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1753 g_signal_handlers_unblock_by_func (chooser_entry,
1754 G_CALLBACK (delete_text_callback),
1761 * _gtk_file_chooser_entry_new:
1762 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1764 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1765 * is an internal implementation widget for the GTK+ file chooser
1766 * which is an entry with completion with respect to a
1767 * #GtkFileSystem object.
1769 * Return value: the newly created #GtkFileChooserEntry
1772 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1774 GtkFileChooserEntry *chooser_entry;
1776 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1777 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1779 return GTK_WIDGET (chooser_entry);
1783 * _gtk_file_chooser_entry_set_file_system:
1784 * @chooser_entry: a #GtkFileChooser
1785 * @file_system: an object implementing #GtkFileSystem
1787 * Sets the file system for @chooser_entry.
1790 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1791 GtkFileSystem *file_system)
1793 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1794 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1796 if (file_system != chooser_entry->file_system)
1798 if (chooser_entry->file_system)
1799 g_object_unref (chooser_entry->file_system);
1801 chooser_entry->file_system = g_object_ref (file_system);
1806 * _gtk_file_chooser_entry_set_base_folder:
1807 * @chooser_entry: a #GtkFileChooserEntry
1808 * @file: file for a folder in the chooser entries current file system.
1810 * Sets the folder with respect to which completions occur.
1813 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1816 if (chooser_entry->base_folder)
1817 g_object_unref (chooser_entry->base_folder);
1819 chooser_entry->base_folder = file;
1821 if (chooser_entry->base_folder)
1822 g_object_ref (chooser_entry->base_folder);
1824 clear_completions (chooser_entry);
1825 _gtk_file_chooser_entry_select_filename (chooser_entry);
1829 * _gtk_file_chooser_entry_get_current_folder:
1830 * @chooser_entry: a #GtkFileChooserEntry
1832 * Gets the current folder for the #GtkFileChooserEntry. If the
1833 * user has only entered a filename, this will be in the base folder
1834 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1835 * user has entered a relative or absolute path, then it will
1836 * be different. If the user has entered unparsable text, or text which
1837 * the entry cannot handle, this will return %NULL.
1839 * Return value: the file for the current folder - this value is owned by the
1840 * chooser entry and must not be modified or freed.
1843 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1845 commit_completion_and_refresh (chooser_entry);
1846 return chooser_entry->current_folder_file;
1850 * _gtk_file_chooser_entry_get_file_part:
1851 * @chooser_entry: a #GtkFileChooserEntry
1853 * Gets the non-folder portion of whatever the user has entered
1854 * into the file selector. What is returned is a UTF-8 string,
1855 * and if a filename path is needed, g_file_get_child_for_display_name()
1858 * Return value: the entered filename - this value is owned by the
1859 * chooser entry and must not be modified or freed.
1862 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1864 commit_completion_and_refresh (chooser_entry);
1865 return chooser_entry->file_part;
1869 * _gtk_file_chooser_entry_set_file_part:
1870 * @chooser_entry: a #GtkFileChooserEntry
1871 * @file_part: text to display in the entry, in UTF-8
1873 * Sets the current text shown in the file chooser entry.
1876 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1877 const gchar *file_part)
1879 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1881 chooser_entry->in_change = TRUE;
1882 clear_completions (chooser_entry);
1883 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1884 chooser_entry->in_change = FALSE;
1889 * _gtk_file_chooser_entry_set_action:
1890 * @chooser_entry: a #GtkFileChooserEntry
1891 * @action: the action which is performed by the file selector using this entry
1893 * Sets action which is performed by the file selector using this entry.
1894 * The #GtkFileChooserEntry will use different completion strategies for
1895 * different actions.
1898 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1899 GtkFileChooserAction action)
1901 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1903 if (chooser_entry->action != action)
1905 GtkEntryCompletion *comp;
1907 chooser_entry->action = action;
1909 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1911 /* FIXME: do we need to actually set the following? */
1915 case GTK_FILE_CHOOSER_ACTION_OPEN:
1916 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1917 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1919 case GTK_FILE_CHOOSER_ACTION_SAVE:
1920 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1921 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1929 * _gtk_file_chooser_entry_get_action:
1930 * @chooser_entry: a #GtkFileChooserEntry
1932 * Gets the action for this entry.
1934 * Returns: the action
1936 GtkFileChooserAction
1937 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1939 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1940 GTK_FILE_CHOOSER_ACTION_OPEN);
1942 return chooser_entry->action;
1946 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1949 gboolean retval = FALSE;
1951 if (chooser_entry->current_folder)
1953 GFileInfo *file_info;
1955 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1959 retval = _gtk_file_info_consider_as_directory (file_info);
1960 g_object_unref (file_info);
1969 * _gtk_file_chooser_entry_select_filename:
1970 * @chooser_entry: a #GtkFileChooserEntry
1972 * Selects the filename (without the extension) for user edition.
1975 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1977 const gchar *str, *ext;
1980 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1982 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1983 ext = g_strrstr (str, ".");
1986 len = g_utf8_pointer_to_offset (str, ext);
1989 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1993 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1994 gboolean local_only)
1996 chooser_entry->local_only = local_only;
1997 clear_completions (chooser_entry);
2001 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
2003 return chooser_entry->local_only;