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 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
36 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
37 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
38 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
40 struct _GtkFileChooserEntryClass
42 GtkEntryClass parent_class;
45 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
47 LOAD_COMPLETE_NOTHING,
48 LOAD_COMPLETE_AUTOCOMPLETE,
49 LOAD_COMPLETE_EXPLICIT_COMPLETION
55 REFRESH_INVALID_INPUT,
56 REFRESH_INCOMPLETE_HOSTNAME,
61 struct _GtkFileChooserEntry
63 GtkEntry parent_instance;
65 GtkFileChooserAction action;
67 GtkFileSystem *file_system;
69 GFile *current_folder_file;
73 /* Folder being loaded or already loaded */
74 GtkFolder *current_folder;
75 GCancellable *load_folder_cancellable;
77 LoadCompleteAction load_complete_action;
79 GtkListStore *completion_store;
81 guint start_autocompletion_idle_id;
83 GtkWidget *completion_feedback_window;
84 GtkWidget *completion_feedback_label;
85 guint completion_feedback_timeout_id;
87 guint has_completion : 1;
100 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
102 static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface);
104 static void gtk_file_chooser_entry_finalize (GObject *object);
105 static void gtk_file_chooser_entry_dispose (GObject *object);
106 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
107 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
108 static gboolean gtk_file_chooser_entry_focus (GtkWidget *widget,
109 GtkDirectionType direction);
110 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
111 GdkEventFocus *event);
112 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
113 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
114 const gchar *new_text,
115 gint new_text_length,
117 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
120 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
122 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
127 static gint insert_text_callback (GtkFileChooserEntry *widget,
128 const gchar *new_text,
129 gint new_text_length,
132 static void delete_text_callback (GtkFileChooserEntry *widget,
138 static gboolean match_selected_callback (GtkEntryCompletion *completion,
141 GtkFileChooserEntry *chooser_entry);
142 static gboolean completion_match_func (GtkEntryCompletion *comp,
146 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
152 REFRESH_UP_TO_CURSOR_POSITION,
156 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
157 RefreshMode refresh_mode);
158 static void finished_loading_cb (GtkFolder *folder,
160 static void autocomplete (GtkFileChooserEntry *chooser_entry);
161 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
162 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
163 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
164 const gchar *feedback);
166 static GtkEditableClass *parent_editable_iface;
168 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
169 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
170 gtk_file_chooser_entry_iface_init))
173 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
175 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
176 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
177 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
179 gobject_class->finalize = gtk_file_chooser_entry_finalize;
180 gobject_class->dispose = gtk_file_chooser_entry_dispose;
182 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
183 widget_class->unmap = gtk_file_chooser_entry_unmap;
184 widget_class->focus = gtk_file_chooser_entry_focus;
185 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
187 entry_class->activate = gtk_file_chooser_entry_activate;
191 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
193 parent_editable_iface = g_type_interface_peek_parent (iface);
195 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
196 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
197 iface->set_position = gtk_file_chooser_entry_set_position;
198 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
202 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
204 GtkEntryCompletion *comp;
205 GtkCellRenderer *cell;
207 chooser_entry->local_only = TRUE;
209 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
211 comp = gtk_entry_completion_new ();
212 gtk_entry_completion_set_popup_single_match (comp, FALSE);
214 gtk_entry_completion_set_match_func (comp,
215 completion_match_func,
219 cell = gtk_cell_renderer_text_new ();
220 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
222 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
226 g_signal_connect (comp, "match-selected",
227 G_CALLBACK (match_selected_callback), chooser_entry);
229 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
230 g_object_unref (comp);
233 g_signal_connect (chooser_entry, "insert-text",
234 G_CALLBACK (insert_text_callback), NULL);
235 g_signal_connect (chooser_entry, "delete-text",
236 G_CALLBACK (delete_text_callback), NULL);
241 gtk_file_chooser_entry_finalize (GObject *object)
243 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
245 if (chooser_entry->base_folder)
246 g_object_unref (chooser_entry->base_folder);
248 if (chooser_entry->current_folder)
249 g_object_unref (chooser_entry->current_folder);
251 if (chooser_entry->current_folder_file)
252 g_object_unref (chooser_entry->current_folder_file);
254 g_free (chooser_entry->file_part);
256 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
260 discard_current_folder (GtkFileChooserEntry *chooser_entry)
262 if (chooser_entry->current_folder)
264 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
265 G_CALLBACK (finished_loading_cb), chooser_entry);
266 g_object_unref (chooser_entry->current_folder);
267 chooser_entry->current_folder = NULL;
272 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
274 if (chooser_entry->load_folder_cancellable)
276 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
277 chooser_entry->load_folder_cancellable = NULL;
280 if (chooser_entry->current_folder_file)
282 g_object_unref (chooser_entry->current_folder_file);
283 chooser_entry->current_folder_file = NULL;
288 gtk_file_chooser_entry_dispose (GObject *object)
290 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
292 remove_completion_feedback (chooser_entry);
293 discard_current_folder (chooser_entry);
294 discard_loading_and_current_folder_file (chooser_entry);
296 if (chooser_entry->start_autocompletion_idle_id != 0)
298 g_source_remove (chooser_entry->start_autocompletion_idle_id);
299 chooser_entry->start_autocompletion_idle_id = 0;
302 if (chooser_entry->completion_store)
304 g_object_unref (chooser_entry->completion_store);
305 chooser_entry->completion_store = NULL;
308 if (chooser_entry->file_system)
310 g_object_unref (chooser_entry->file_system);
311 chooser_entry->file_system = NULL;
314 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
317 /* Match functions for the GtkEntryCompletion */
319 match_selected_callback (GtkEntryCompletion *completion,
322 GtkFileChooserEntry *chooser_entry)
329 gtk_tree_model_get (model, iter,
330 DISPLAY_NAME_COLUMN, &display_name,
334 if (!display_name || !file)
336 /* these shouldn't complain if passed NULL */
337 g_object_unref (file);
338 g_free (display_name);
342 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
344 pos = chooser_entry->file_part_pos;
346 /* We don't set in_change here as we want to update the current_folder
348 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
350 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
353 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
355 g_object_unref (file);
356 g_free (display_name);
361 /* Match function for the GtkEntryCompletion */
363 completion_match_func (GtkEntryCompletion *comp,
364 const char *key_unused,
368 GtkFileChooserEntry *chooser_entry;
371 char *norm_file_part;
374 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
376 /* We ignore the key because it is the contents of the entry. Instead, we
377 * just use our precomputed file_part.
379 if (!chooser_entry->file_part)
384 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
387 return FALSE; /* Uninitialized row, ugh */
390 /* If we have an empty file_part, then we're at the root of a directory. In
391 * that case, we want to match all non-dot files. We might want to match
392 * dot_files too if show_hidden is TRUE on the fileselector in the future.
394 /* Additionally, support for gnome .hidden files would be sweet, too */
395 if (chooser_entry->file_part[0] == '\000')
407 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
408 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
410 #ifdef G_PLATFORM_WIN32
414 temp = norm_file_part;
415 norm_file_part = g_utf8_casefold (norm_file_part, -1);
419 norm_name = g_utf8_casefold (norm_name, -1);
424 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
426 g_free (norm_file_part);
434 clear_completions (GtkFileChooserEntry *chooser_entry)
436 chooser_entry->has_completion = FALSE;
437 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
439 remove_completion_feedback (chooser_entry);
443 beep (GtkFileChooserEntry *chooser_entry)
445 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
448 /* This function will append a directory separator to paths to
449 * display_name iff the path associated with it is a directory.
450 * maybe_append_separator_to_file will g_free the display_name and
451 * return a new one if needed. Otherwise, it will return the old one.
452 * You should be safe calling
454 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
456 * g_free (display_name);
459 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
466 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
470 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
474 if (_gtk_file_info_consider_as_directory (info))
476 gchar *tmp = display_name;
477 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
482 g_object_unref (info);
490 trim_dir_separator_suffix (const char *str)
495 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
496 return g_strndup (str, len - 1);
498 return g_strdup (str);
501 /* Determines if the completion model has entries with a common prefix relative
502 * to the current contents of the entry. Also, if there's one and only one such
503 * path, stores it in unique_path_ret.
506 find_common_prefix (GtkFileChooserEntry *chooser_entry,
507 gchar **common_prefix_ret,
508 GFile **unique_file_ret,
509 gboolean *is_complete_not_unique_ret,
510 gboolean *prefix_expands_the_file_part_ret,
513 GtkEditable *editable;
517 char *text_up_to_cursor;
518 GFile *parsed_folder_file;
519 char *parsed_file_part;
521 *common_prefix_ret = NULL;
522 *unique_file_ret = NULL;
523 *is_complete_not_unique_ret = FALSE;
524 *prefix_expands_the_file_part_ret = FALSE;
526 editable = GTK_EDITABLE (chooser_entry);
528 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
530 parsed = _gtk_file_system_parse (chooser_entry->file_system,
531 chooser_entry->base_folder,
537 g_free (text_up_to_cursor);
542 g_assert (parsed_folder_file != NULL
543 && chooser_entry->current_folder != NULL
544 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
546 g_object_unref (parsed_folder_file);
548 /* First pass: find the common prefix */
550 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
557 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
559 DISPLAY_NAME_COLUMN, &display_name,
563 if (g_str_has_prefix (display_name, parsed_file_part))
565 if (!*common_prefix_ret)
567 *common_prefix_ret = trim_dir_separator_suffix (display_name);
568 *unique_file_ret = g_object_ref (file);
572 gchar *p = *common_prefix_ret;
573 const gchar *q = display_name;
575 while (*p && *p == *q)
583 if (*unique_file_ret)
585 g_object_unref (*unique_file_ret);
586 *unique_file_ret = NULL;
591 g_free (display_name);
592 g_object_unref (file);
593 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
596 /* Second pass: see if the prefix we found is a complete match */
598 if (*common_prefix_ret != NULL)
600 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
607 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
609 DISPLAY_NAME_COLUMN, &display_name,
611 len = strlen (display_name);
614 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
617 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
618 *is_complete_not_unique_ret = TRUE;
620 g_free (display_name);
621 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
624 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
626 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
629 g_free (parsed_file_part);
635 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
642 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
643 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
647 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
648 if (G_IS_DIR_SEPARATOR (*next_char_str))
651 g_free (next_char_str);
658 INVALID_INPUT, /* what the user typed is bogus */
659 NO_MATCH, /* no matches based on what the user typed */
660 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
661 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
662 COMPLETED, /* completion inserted (ambiguous suffix) */
663 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
664 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
665 } CommonPrefixResult;
667 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
668 static CommonPrefixResult
669 append_common_prefix (GtkFileChooserEntry *chooser_entry,
671 gboolean show_errors)
673 gchar *common_prefix;
675 gboolean is_complete_not_unique;
676 gboolean prefix_expands_the_file_part;
678 CommonPrefixResult result = NO_MATCH;
679 gboolean have_result;
681 clear_completions (chooser_entry);
683 if (chooser_entry->completion_store == NULL)
687 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
689 /* If the user types an incomplete hostname ("http://foo" without a slash
690 * after that), it's not an error. We just don't want to pop up a
691 * meaningless completion window in that state.
693 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
696 beep (chooser_entry);
697 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
700 g_error_free (error);
702 return INVALID_INPUT;
709 if (!char_after_cursor_is_directory_separator (chooser_entry))
713 common_prefix = maybe_append_separator_to_file (chooser_entry,
718 prefix_expands_the_file_part = TRUE;
721 g_object_unref (unique_file);
723 if (prefix_expands_the_file_part)
724 result = COMPLETED_UNIQUE;
726 result = NOTHING_INSERTED_UNIQUE;
732 if (is_complete_not_unique)
734 result = COMPLETE_BUT_NOT_UNIQUE;
742 gint common_prefix_len;
745 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
746 common_prefix_len = g_utf8_strlen (common_prefix, -1);
748 pos = chooser_entry->file_part_pos;
750 if (prefix_expands_the_file_part)
752 chooser_entry->in_change = TRUE;
753 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
755 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
758 chooser_entry->in_change = FALSE;
762 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
764 pos); /* equivalent to cursor_pos + common_prefix_len); */
765 chooser_entry->has_completion = TRUE;
768 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
770 else if (!have_result)
772 result = NOTHING_INSERTED_COMPLETE;
776 g_free (common_prefix);
793 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
794 const gchar *new_text,
795 gint new_text_length,
798 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
802 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
803 insert_pos = *position;
805 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
807 if (chooser_entry->in_change)
810 remove_completion_feedback (chooser_entry);
812 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
813 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
814 && insert_pos == old_text_len)
815 install_start_autocompletion_idle (chooser_entry);
819 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
821 if (chooser_entry->in_change)
824 clear_completions (chooser_entry);
828 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
832 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
834 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
836 clear_completions_if_not_in_change (chooser_entry);
840 gtk_file_chooser_entry_set_position (GtkEditable *editable,
843 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
845 parent_editable_iface->set_position (editable, position);
847 clear_completions_if_not_in_change (chooser_entry);
851 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
855 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
857 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
859 clear_completions_if_not_in_change (chooser_entry);
863 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
865 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
866 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
870 gtk_file_chooser_entry_unmap (GtkWidget *widget)
872 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
874 remove_completion_feedback (chooser_entry);
876 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
880 completion_feedback_window_expose_event_cb (GtkWidget *widget,
881 GdkEventExpose *event,
884 /* Stolen from gtk_tooltip_paint_window() */
886 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
888 gtk_paint_flat_box (chooser_entry->completion_feedback_window->style,
889 chooser_entry->completion_feedback_window->window,
893 chooser_entry->completion_feedback_window,
896 chooser_entry->completion_feedback_window->allocation.width,
897 chooser_entry->completion_feedback_window->allocation.height);
903 set_invisible_mouse_cursor (GdkWindow *window)
908 display = gdk_drawable_get_display (window);
909 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
911 gdk_window_set_cursor (window, cursor);
913 gdk_cursor_unref (cursor);
917 completion_feedback_window_realize_cb (GtkWidget *widget,
920 /* We hide the mouse cursor inside the completion feedback window, since
921 * GtkEntry hides the cursor when the user types. We don't want the cursor to
922 * come back if the completion feedback ends up where the mouse is.
924 set_invisible_mouse_cursor (widget->window);
928 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
930 /* Stolen from gtk_tooltip_init() */
932 GtkWidget *alignment;
934 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
935 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
936 GDK_WINDOW_TYPE_HINT_TOOLTIP);
937 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
938 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
939 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
941 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
942 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
943 chooser_entry->completion_feedback_window->style->ythickness,
944 chooser_entry->completion_feedback_window->style->ythickness,
945 chooser_entry->completion_feedback_window->style->xthickness,
946 chooser_entry->completion_feedback_window->style->xthickness);
947 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
948 gtk_widget_show (alignment);
950 g_signal_connect (chooser_entry->completion_feedback_window, "expose-event",
951 G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
952 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
953 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
954 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
956 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
957 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
958 gtk_widget_show (chooser_entry->completion_feedback_label);
962 completion_feedback_timeout_cb (gpointer data)
964 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
966 chooser_entry->completion_feedback_timeout_id = 0;
968 remove_completion_feedback (chooser_entry);
973 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
975 if (chooser_entry->completion_feedback_timeout_id != 0)
976 g_source_remove (chooser_entry->completion_feedback_timeout_id);
978 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
979 completion_feedback_timeout_cb,
983 /* Gets the x position of the text cursor in the entry, in widget coordinates */
985 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
988 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
989 * the reader, you have to implement support for the entry's scroll offset and
990 * RTL layouts and all the fancy Pango stuff.
994 gint layout_x, layout_y;
996 PangoRectangle strong_pos;
998 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1000 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1002 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1003 GTK_ENTRY (chooser_entry)->current_pos);
1005 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1007 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1011 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1013 /* More or less stolen from gtk_tooltip_position() */
1015 GtkRequisition feedback_req;
1016 gint entry_x, entry_y;
1018 GtkAllocation *entry_allocation;
1019 int feedback_x, feedback_y;
1021 gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);
1023 gdk_window_get_origin (GTK_WIDGET (chooser_entry)->window, &entry_x, &entry_y);
1024 entry_allocation = &(GTK_WIDGET (chooser_entry)->allocation);
1026 get_entry_cursor_x (chooser_entry, &cursor_x);
1028 /* FIXME: fit to the screen if we bump on the screen's edge */
1029 feedback_x = entry_x + cursor_x + entry_allocation->height / 2; /* cheap "half M-width" */
1030 feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;
1032 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1033 gtk_widget_show (chooser_entry->completion_feedback_window);
1035 install_completion_feedback_timer (chooser_entry);
1039 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1040 const gchar *feedback)
1042 if (chooser_entry->completion_feedback_window == NULL)
1043 create_completion_feedback_window (chooser_entry);
1045 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1047 show_completion_feedback_window (chooser_entry);
1051 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1053 if (chooser_entry->completion_feedback_window)
1054 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1056 chooser_entry->completion_feedback_window = NULL;
1057 chooser_entry->completion_feedback_label = NULL;
1059 if (chooser_entry->completion_feedback_timeout_id != 0)
1061 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1062 chooser_entry->completion_feedback_timeout_id = 0;
1067 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1069 CommonPrefixResult result;
1071 g_assert (chooser_entry->current_folder != NULL);
1072 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1074 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1076 * - If there is a common prefix, insert it (done)
1077 * - If there is no common prefix, pop up the suggestion window
1078 * - If there are no matches at all, beep and bring up a tooltip (done)
1079 * - If the suggestion window is already up, scroll it
1081 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1086 /* We already beeped in append_common_prefix(); do nothing here */
1090 beep (chooser_entry);
1091 /* translators: this text is shown when there are no completions
1092 * for something the user typed in a file chooser entry
1094 pop_up_completion_feedback (chooser_entry, _("No match"));
1097 case NOTHING_INSERTED_COMPLETE:
1098 /* FIXME: pop up the suggestion window or scroll it */
1101 case NOTHING_INSERTED_UNIQUE:
1102 /* translators: this text is shown when there is exactly one completion
1103 * for something the user typed in a file chooser entry
1105 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1112 case COMPLETED_UNIQUE:
1116 case COMPLETE_BUT_NOT_UNIQUE:
1117 /* translators: this text is shown when the text in a file chooser
1118 * entry is a complete filename, but could be continued to find
1121 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1125 g_assert_not_reached ();
1130 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1132 RefreshStatus status;
1136 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1143 g_assert (chooser_entry->current_folder_file != NULL);
1145 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1146 explicitly_complete (chooser_entry);
1149 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1151 /* Translators: this text is shown while the system is searching
1152 * for possible completions for filenames in a file chooser entry. */
1153 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1158 case REFRESH_INVALID_INPUT:
1160 /* Translators: this is shown in the feedback for Tab-completion in a file
1161 * chooser's text entry, when the user enters an invalid path. */
1162 feedback_msg = _("Invalid path");
1165 case REFRESH_INCOMPLETE_HOSTNAME:
1168 if (chooser_entry->local_only)
1170 /* hostnames in a local_only file chooser? user error */
1172 /* Translators: this is shown in the feedback for Tab-completion in a
1173 * file chooser's text entry when the user enters something like
1174 * "sftp://blahblah" in an app that only supports local filenames. */
1175 feedback_msg = _("Only local files may be selected");
1179 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1181 /* Translators: this is shown in the feedback for Tab-completion in a
1182 * file chooser's text entry when the user hasn't entered the first '/'
1183 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1184 feedback_msg = _("Incomplete hostname; end it with '/'");
1189 case REFRESH_NONEXISTENT:
1192 /* Translators: this is shown in the feedback for Tab-completion in a file
1193 * chooser's text entry when the user enters a path that does not exist
1194 * and then hits Tab */
1195 feedback_msg = _("Path does not exist");
1198 case REFRESH_NOT_LOCAL:
1200 feedback_msg = _("Only local files may be selected");
1204 g_assert_not_reached ();
1210 g_assert (chooser_entry->current_folder_file == NULL);
1212 beep (chooser_entry);
1213 pop_up_completion_feedback (chooser_entry, feedback_msg);
1214 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1219 gtk_file_chooser_entry_focus (GtkWidget *widget,
1220 GtkDirectionType direction)
1222 GtkFileChooserEntry *chooser_entry;
1223 GtkEditable *editable;
1225 GdkModifierType state;
1226 gboolean control_pressed;
1228 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1229 editable = GTK_EDITABLE (widget);
1230 entry = GTK_ENTRY (widget);
1232 if (!chooser_entry->eat_tabs)
1233 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1235 control_pressed = FALSE;
1237 if (gtk_get_current_event_state (&state))
1239 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1240 control_pressed = TRUE;
1243 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1244 * makes it 'safe' for people to hit. */
1245 if ((direction == GTK_DIR_TAB_FORWARD) &&
1246 (gtk_widget_has_focus (widget)) &&
1247 (! control_pressed))
1249 if (chooser_entry->has_completion)
1250 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1252 start_explicit_completion (chooser_entry);
1257 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1261 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1262 GdkEventFocus *event)
1264 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1266 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1268 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1272 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1274 if (chooser_entry->has_completion)
1276 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1277 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1280 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1281 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1285 gtk_file_chooser_entry_activate (GtkEntry *entry)
1287 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1289 commit_completion_and_refresh (chooser_entry);
1290 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1294 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1296 if (!chooser_entry->completion_store)
1299 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1300 g_object_unref (chooser_entry->completion_store);
1301 chooser_entry->completion_store = NULL;
1304 /* Fills the completion store from the contents of the current folder */
1306 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1311 discard_completion_store (chooser_entry);
1313 files = _gtk_folder_list_children (chooser_entry->current_folder);
1315 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1319 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1324 file = tmp_list->data;
1326 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1330 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1334 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1336 gtk_list_store_append (chooser_entry->completion_store, &iter);
1337 gtk_list_store_set (chooser_entry->completion_store, &iter,
1338 DISPLAY_NAME_COLUMN, display_name,
1342 g_object_unref (info);
1343 g_free (display_name);
1347 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1348 g_slist_free (files);
1350 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1351 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1353 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1354 GTK_TREE_MODEL (chooser_entry->completion_store));
1357 /* When we finish loading the current folder, this function should get called to
1358 * perform the deferred autocompletion or explicit completion.
1361 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1363 switch (chooser_entry->load_complete_action)
1365 case LOAD_COMPLETE_NOTHING:
1368 case LOAD_COMPLETE_AUTOCOMPLETE:
1369 autocomplete (chooser_entry);
1372 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1373 explicitly_complete (chooser_entry);
1377 g_assert_not_reached ();
1380 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1384 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1386 populate_completion_store (chooser_entry);
1387 perform_load_complete_action (chooser_entry);
1389 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1392 /* Callback when the current folder finishes loading */
1394 finished_loading_cb (GtkFolder *folder,
1397 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1399 finish_folder_load (chooser_entry);
1402 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1404 load_directory_get_folder_callback (GCancellable *cancellable,
1406 const GError *error,
1409 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1410 GtkFileChooserEntry *chooser_entry = data;
1412 if (cancellable != chooser_entry->load_folder_cancellable)
1415 chooser_entry->load_folder_cancellable = NULL;
1419 LoadCompleteAction old_load_complete_action;
1421 old_load_complete_action = chooser_entry->load_complete_action;
1423 discard_completion_store (chooser_entry);
1424 clear_completions (chooser_entry);
1426 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1428 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1430 beep (chooser_entry);
1431 pop_up_completion_feedback (chooser_entry, error->message);
1434 discard_current_folder (chooser_entry);
1437 if (cancelled || error)
1440 g_assert (folder != NULL);
1441 chooser_entry->current_folder = g_object_ref (folder);
1443 discard_completion_store (chooser_entry);
1445 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1446 finish_folder_load (chooser_entry);
1448 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1449 G_CALLBACK (finished_loading_cb), chooser_entry);
1452 g_object_unref (chooser_entry);
1453 g_object_unref (cancellable);
1456 static RefreshStatus
1457 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1459 if (chooser_entry->file_system == NULL)
1462 g_assert (chooser_entry->current_folder_file != NULL);
1463 g_assert (chooser_entry->current_folder == NULL);
1464 g_assert (chooser_entry->load_folder_cancellable == NULL);
1466 if (chooser_entry->local_only
1467 && !g_file_is_native (chooser_entry->current_folder_file))
1469 g_object_unref (chooser_entry->current_folder_file);
1470 chooser_entry->current_folder_file = NULL;
1472 return REFRESH_NOT_LOCAL;
1475 chooser_entry->load_folder_cancellable =
1476 _gtk_file_system_get_folder (chooser_entry->file_system,
1477 chooser_entry->current_folder_file,
1478 "standard::name,standard::display-name,standard::type",
1479 load_directory_get_folder_callback,
1480 g_object_ref (chooser_entry));
1485 static RefreshStatus
1486 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1488 gboolean force_reload)
1490 gboolean reload = FALSE;
1492 g_assert (folder_file != NULL);
1494 if (chooser_entry->current_folder_file)
1496 if ((!(g_file_equal (folder_file, chooser_entry->current_folder_file)
1497 && chooser_entry->load_folder_cancellable))
1502 discard_current_folder (chooser_entry);
1503 discard_loading_and_current_folder_file (chooser_entry);
1505 chooser_entry->current_folder_file = g_object_ref (folder_file);
1510 chooser_entry->current_folder_file = g_object_ref (folder_file);
1515 return start_loading_current_folder (chooser_entry);
1520 static RefreshStatus
1521 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1522 RefreshMode refresh_mode)
1524 GtkEditable *editable;
1529 gsize total_len, file_part_len;
1532 RefreshStatus result;
1534 editable = GTK_EDITABLE (chooser_entry);
1536 switch (refresh_mode)
1538 case REFRESH_UP_TO_CURSOR_POSITION:
1539 end_pos = gtk_editable_get_position (editable);
1542 case REFRESH_WHOLE_TEXT:
1543 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1547 g_assert_not_reached ();
1548 return REFRESH_INVALID_INPUT;
1551 text = gtk_editable_get_chars (editable, 0, end_pos);
1554 if (!chooser_entry->file_system ||
1555 !chooser_entry->base_folder ||
1556 !_gtk_file_system_parse (chooser_entry->file_system,
1557 chooser_entry->base_folder, text,
1558 &folder_file, &file_part, &error))
1560 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1563 result = REFRESH_INCOMPLETE_HOSTNAME;
1567 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1569 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1570 result = REFRESH_NONEXISTENT;
1572 result = REFRESH_INVALID_INPUT;
1576 g_error_free (error);
1578 file_part = g_strdup ("");
1583 g_assert (folder_file != NULL);
1585 file_part_len = strlen (file_part);
1586 total_len = strlen (text);
1587 if (total_len > file_part_len)
1588 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1592 result = REFRESH_OK;
1597 g_free (chooser_entry->file_part);
1599 chooser_entry->file_part = file_part;
1600 chooser_entry->file_part_pos = file_part_pos;
1602 if (result == REFRESH_OK)
1604 result = reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1608 discard_current_folder (chooser_entry);
1609 discard_loading_and_current_folder_file (chooser_entry);
1613 g_object_unref (folder_file);
1615 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1616 ((result == REFRESH_OK)
1617 && (chooser_entry->current_folder_file != NULL)
1618 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1619 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1620 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1621 || ((result != REFRESH_OK)
1622 && (chooser_entry->current_folder_file == NULL)
1623 && (chooser_entry->load_folder_cancellable == NULL)
1624 && (chooser_entry->current_folder == NULL)));
1630 autocomplete (GtkFileChooserEntry *chooser_entry)
1632 if (!(chooser_entry->current_folder != NULL
1633 && _gtk_folder_is_finished_loading (chooser_entry->current_folder)
1634 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1637 append_common_prefix (chooser_entry, TRUE, FALSE);
1641 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1643 RefreshStatus status;
1645 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1650 g_assert (chooser_entry->current_folder_file != NULL);
1652 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1653 autocomplete (chooser_entry);
1655 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1659 case REFRESH_INVALID_INPUT:
1660 case REFRESH_INCOMPLETE_HOSTNAME:
1661 case REFRESH_NONEXISTENT:
1662 case REFRESH_NOT_LOCAL:
1663 /* We don't beep or anything, since this is autocompletion - the user
1664 * didn't request any action explicitly.
1669 g_assert_not_reached ();
1674 start_autocompletion_idle_handler (gpointer data)
1676 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1678 start_autocompletion (chooser_entry);
1680 chooser_entry->start_autocompletion_idle_id = 0;
1686 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1688 if (chooser_entry->start_autocompletion_idle_id != 0)
1691 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1696 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1697 const gchar *new_text,
1698 gint new_text_length,
1702 const gchar *colon = memchr (new_text, ':', new_text_length);
1705 /* Disallow these characters altogether */
1706 for (i = 0; i < new_text_length; i++)
1708 if (new_text[i] == '<' ||
1709 new_text[i] == '>' ||
1710 new_text[i] == '"' ||
1711 new_text[i] == '|' ||
1712 new_text[i] == '*' ||
1717 if (i < new_text_length ||
1718 /* Disallow entering text that would cause a colon to be anywhere except
1719 * after a drive letter.
1722 *position + (colon - new_text) != 1) ||
1723 (new_text_length > 0 &&
1725 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1726 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1728 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1729 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1737 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1742 /* If deleting a drive letter, delete the colon, too */
1743 if (start_pos == 0 && end_pos == 1 &&
1744 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1745 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1747 g_signal_handlers_block_by_func (chooser_entry,
1748 G_CALLBACK (delete_text_callback),
1750 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1751 g_signal_handlers_unblock_by_func (chooser_entry,
1752 G_CALLBACK (delete_text_callback),
1759 * _gtk_file_chooser_entry_new:
1760 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1762 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1763 * is an internal implementation widget for the GTK+ file chooser
1764 * which is an entry with completion with respect to a
1765 * #GtkFileSystem object.
1767 * Return value: the newly created #GtkFileChooserEntry
1770 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1772 GtkFileChooserEntry *chooser_entry;
1774 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1775 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1777 return GTK_WIDGET (chooser_entry);
1781 * _gtk_file_chooser_entry_set_file_system:
1782 * @chooser_entry: a #GtkFileChooser
1783 * @file_system: an object implementing #GtkFileSystem
1785 * Sets the file system for @chooser_entry.
1788 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1789 GtkFileSystem *file_system)
1791 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1792 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1794 if (file_system != chooser_entry->file_system)
1796 if (chooser_entry->file_system)
1797 g_object_unref (chooser_entry->file_system);
1799 chooser_entry->file_system = g_object_ref (file_system);
1804 * _gtk_file_chooser_entry_set_base_folder:
1805 * @chooser_entry: a #GtkFileChooserEntry
1806 * @file: file for a folder in the chooser entries current file system.
1808 * Sets the folder with respect to which completions occur.
1811 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1814 if (chooser_entry->base_folder)
1815 g_object_unref (chooser_entry->base_folder);
1817 chooser_entry->base_folder = file;
1819 if (chooser_entry->base_folder)
1820 g_object_ref (chooser_entry->base_folder);
1822 clear_completions (chooser_entry);
1823 _gtk_file_chooser_entry_select_filename (chooser_entry);
1827 * _gtk_file_chooser_entry_get_current_folder:
1828 * @chooser_entry: a #GtkFileChooserEntry
1830 * Gets the current folder for the #GtkFileChooserEntry. If the
1831 * user has only entered a filename, this will be in the base folder
1832 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1833 * user has entered a relative or absolute path, then it will
1834 * be different. If the user has entered unparsable text, or text which
1835 * the entry cannot handle, this will return %NULL.
1837 * Return value: the file for the current folder - this value is owned by the
1838 * chooser entry and must not be modified or freed.
1841 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1843 commit_completion_and_refresh (chooser_entry);
1844 return chooser_entry->current_folder_file;
1848 * _gtk_file_chooser_entry_get_file_part:
1849 * @chooser_entry: a #GtkFileChooserEntry
1851 * Gets the non-folder portion of whatever the user has entered
1852 * into the file selector. What is returned is a UTF-8 string,
1853 * and if a filename path is needed, g_file_get_child_for_display_name()
1856 * Return value: the entered filename - this value is owned by the
1857 * chooser entry and must not be modified or freed.
1860 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1862 commit_completion_and_refresh (chooser_entry);
1863 return chooser_entry->file_part;
1867 * _gtk_file_chooser_entry_set_file_part:
1868 * @chooser_entry: a #GtkFileChooserEntry
1869 * @file_part: text to display in the entry, in UTF-8
1871 * Sets the current text shown in the file chooser entry.
1874 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1875 const gchar *file_part)
1877 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1879 chooser_entry->in_change = TRUE;
1880 clear_completions (chooser_entry);
1881 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1882 chooser_entry->in_change = FALSE;
1887 * _gtk_file_chooser_entry_set_action:
1888 * @chooser_entry: a #GtkFileChooserEntry
1889 * @action: the action which is performed by the file selector using this entry
1891 * Sets action which is performed by the file selector using this entry.
1892 * The #GtkFileChooserEntry will use different completion strategies for
1893 * different actions.
1896 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1897 GtkFileChooserAction action)
1899 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1901 if (chooser_entry->action != action)
1903 GtkEntryCompletion *comp;
1905 chooser_entry->action = action;
1907 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1909 /* FIXME: do we need to actually set the following? */
1913 case GTK_FILE_CHOOSER_ACTION_OPEN:
1914 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1915 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1917 case GTK_FILE_CHOOSER_ACTION_SAVE:
1918 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1919 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1927 * _gtk_file_chooser_entry_get_action:
1928 * @chooser_entry: a #GtkFileChooserEntry
1930 * Gets the action for this entry.
1932 * Returns: the action
1934 GtkFileChooserAction
1935 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1937 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1938 GTK_FILE_CHOOSER_ACTION_OPEN);
1940 return chooser_entry->action;
1944 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1947 gboolean retval = FALSE;
1949 if (chooser_entry->current_folder)
1951 GFileInfo *file_info;
1953 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1957 retval = _gtk_file_info_consider_as_directory (file_info);
1958 g_object_unref (file_info);
1967 * _gtk_file_chooser_entry_select_filename:
1968 * @chooser_entry: a #GtkFileChooserEntry
1970 * Selects the filename (without the extension) for user edition.
1973 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1975 const gchar *str, *ext;
1978 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1980 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1981 ext = g_strrstr (str, ".");
1984 len = g_utf8_pointer_to_offset (str, ext);
1987 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1991 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1992 gboolean local_only)
1994 chooser_entry->local_only = local_only;
1995 clear_completions (chooser_entry);
1999 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
2001 return chooser_entry->local_only;