1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserentry.c: Entry with filename completion
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gtkfilechooserentry.h"
27 #include "gtkalignment.h"
28 #include "gtkcelllayout.h"
29 #include "gtkcellrenderertext.h"
33 #include "gtksizerequest.h"
34 #include "gtkwindow.h"
37 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
39 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
40 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
41 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
43 struct _GtkFileChooserEntryClass
45 GtkEntryClass parent_class;
48 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
50 LOAD_COMPLETE_NOTHING,
51 LOAD_COMPLETE_AUTOCOMPLETE,
52 LOAD_COMPLETE_EXPLICIT_COMPLETION
58 REFRESH_INVALID_INPUT,
59 REFRESH_INCOMPLETE_HOSTNAME,
64 struct _GtkFileChooserEntry
66 GtkEntry parent_instance;
68 GtkFileChooserAction action;
70 GtkFileSystem *file_system;
72 GFile *current_folder_file;
76 /* Folder being loaded or already loaded */
77 GtkFolder *current_folder;
78 GCancellable *load_folder_cancellable;
80 LoadCompleteAction load_complete_action;
82 GtkListStore *completion_store;
84 guint start_autocompletion_idle_id;
86 GtkWidget *completion_feedback_window;
87 GtkWidget *completion_feedback_label;
88 guint completion_feedback_timeout_id;
90 guint has_completion : 1;
103 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
105 static void gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface);
107 static void gtk_file_chooser_entry_finalize (GObject *object);
108 static void gtk_file_chooser_entry_dispose (GObject *object);
109 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
110 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
111 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
113 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
114 GdkEventFocus *event);
115 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
116 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
117 const gchar *new_text,
118 gint new_text_length,
120 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
123 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
125 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
130 static gint insert_text_callback (GtkFileChooserEntry *widget,
131 const gchar *new_text,
132 gint new_text_length,
135 static void delete_text_callback (GtkFileChooserEntry *widget,
141 static gboolean match_selected_callback (GtkEntryCompletion *completion,
144 GtkFileChooserEntry *chooser_entry);
145 static gboolean completion_match_func (GtkEntryCompletion *comp,
149 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
155 REFRESH_UP_TO_CURSOR_POSITION,
159 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
160 RefreshMode refresh_mode);
161 static void finished_loading_cb (GtkFolder *folder,
163 static void autocomplete (GtkFileChooserEntry *chooser_entry);
164 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
165 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
166 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
167 const gchar *feedback);
169 static GtkEditableInterface *parent_editable_iface;
171 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
172 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
173 gtk_file_chooser_entry_iface_init))
176 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
180 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
182 gobject_class->finalize = gtk_file_chooser_entry_finalize;
183 gobject_class->dispose = gtk_file_chooser_entry_dispose;
185 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
186 widget_class->unmap = gtk_file_chooser_entry_unmap;
187 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
188 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
190 entry_class->activate = gtk_file_chooser_entry_activate;
194 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
196 parent_editable_iface = g_type_interface_peek_parent (iface);
198 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
199 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
200 iface->set_position = gtk_file_chooser_entry_set_position;
201 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
205 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
207 GtkEntryCompletion *comp;
208 GtkCellRenderer *cell;
210 chooser_entry->local_only = TRUE;
212 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
214 comp = gtk_entry_completion_new ();
215 gtk_entry_completion_set_popup_single_match (comp, FALSE);
217 gtk_entry_completion_set_match_func (comp,
218 completion_match_func,
222 cell = gtk_cell_renderer_text_new ();
223 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
225 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
229 g_signal_connect (comp, "match-selected",
230 G_CALLBACK (match_selected_callback), chooser_entry);
232 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
233 g_object_unref (comp);
236 g_signal_connect (chooser_entry, "insert-text",
237 G_CALLBACK (insert_text_callback), NULL);
238 g_signal_connect (chooser_entry, "delete-text",
239 G_CALLBACK (delete_text_callback), NULL);
244 gtk_file_chooser_entry_finalize (GObject *object)
246 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
248 if (chooser_entry->base_folder)
249 g_object_unref (chooser_entry->base_folder);
251 if (chooser_entry->current_folder)
252 g_object_unref (chooser_entry->current_folder);
254 if (chooser_entry->current_folder_file)
255 g_object_unref (chooser_entry->current_folder_file);
257 g_free (chooser_entry->file_part);
259 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
263 discard_current_folder (GtkFileChooserEntry *chooser_entry)
265 if (chooser_entry->current_folder)
267 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
268 G_CALLBACK (finished_loading_cb), chooser_entry);
269 g_object_unref (chooser_entry->current_folder);
270 chooser_entry->current_folder = NULL;
275 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
277 if (chooser_entry->load_folder_cancellable)
279 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
280 chooser_entry->load_folder_cancellable = NULL;
283 if (chooser_entry->current_folder_file)
285 g_object_unref (chooser_entry->current_folder_file);
286 chooser_entry->current_folder_file = NULL;
291 gtk_file_chooser_entry_dispose (GObject *object)
293 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
295 remove_completion_feedback (chooser_entry);
296 discard_current_folder (chooser_entry);
297 discard_loading_and_current_folder_file (chooser_entry);
299 if (chooser_entry->start_autocompletion_idle_id != 0)
301 g_source_remove (chooser_entry->start_autocompletion_idle_id);
302 chooser_entry->start_autocompletion_idle_id = 0;
305 if (chooser_entry->completion_store)
307 g_object_unref (chooser_entry->completion_store);
308 chooser_entry->completion_store = NULL;
311 if (chooser_entry->file_system)
313 g_object_unref (chooser_entry->file_system);
314 chooser_entry->file_system = NULL;
317 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
320 /* Match functions for the GtkEntryCompletion */
322 match_selected_callback (GtkEntryCompletion *completion,
325 GtkFileChooserEntry *chooser_entry)
332 gtk_tree_model_get (model, iter,
333 DISPLAY_NAME_COLUMN, &display_name,
337 if (!display_name || !file)
339 /* these shouldn't complain if passed NULL */
340 g_object_unref (file);
341 g_free (display_name);
345 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
347 pos = chooser_entry->file_part_pos;
349 /* We don't set in_change here as we want to update the current_folder
351 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
353 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
356 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
358 g_object_unref (file);
359 g_free (display_name);
364 /* Match function for the GtkEntryCompletion */
366 completion_match_func (GtkEntryCompletion *comp,
367 const char *key_unused,
371 GtkFileChooserEntry *chooser_entry;
374 char *norm_file_part;
377 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
379 /* We ignore the key because it is the contents of the entry. Instead, we
380 * just use our precomputed file_part.
382 if (!chooser_entry->file_part)
387 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
390 return FALSE; /* Uninitialized row, ugh */
393 /* If we have an empty file_part, then we're at the root of a directory. In
394 * that case, we want to match all non-dot files. We might want to match
395 * dot_files too if show_hidden is TRUE on the fileselector in the future.
397 /* Additionally, support for gnome .hidden files would be sweet, too */
398 if (chooser_entry->file_part[0] == '\000')
410 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
411 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
413 #ifdef G_PLATFORM_WIN32
417 temp = norm_file_part;
418 norm_file_part = g_utf8_casefold (norm_file_part, -1);
422 norm_name = g_utf8_casefold (norm_name, -1);
427 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
429 g_free (norm_file_part);
437 clear_completions (GtkFileChooserEntry *chooser_entry)
439 chooser_entry->has_completion = FALSE;
440 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
442 remove_completion_feedback (chooser_entry);
446 beep (GtkFileChooserEntry *chooser_entry)
448 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
451 /* This function will append a directory separator to paths to
452 * display_name iff the path associated with it is a directory.
453 * maybe_append_separator_to_file will g_free the display_name and
454 * return a new one if needed. Otherwise, it will return the old one.
455 * You should be safe calling
457 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
459 * g_free (display_name);
462 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
469 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
473 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
477 if (_gtk_file_info_consider_as_directory (info))
479 gchar *tmp = display_name;
480 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
485 g_object_unref (info);
493 trim_dir_separator_suffix (const char *str)
498 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
499 return g_strndup (str, len - 1);
501 return g_strdup (str);
504 /* Determines if the completion model has entries with a common prefix relative
505 * to the current contents of the entry. Also, if there's one and only one such
506 * path, stores it in unique_path_ret.
509 find_common_prefix (GtkFileChooserEntry *chooser_entry,
510 gchar **common_prefix_ret,
511 GFile **unique_file_ret,
512 gboolean *is_complete_not_unique_ret,
513 gboolean *prefix_expands_the_file_part_ret,
516 GtkEditable *editable;
520 char *text_up_to_cursor;
521 GFile *parsed_folder_file;
522 char *parsed_file_part;
524 *common_prefix_ret = NULL;
525 *unique_file_ret = NULL;
526 *is_complete_not_unique_ret = FALSE;
527 *prefix_expands_the_file_part_ret = FALSE;
529 editable = GTK_EDITABLE (chooser_entry);
531 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
533 parsed = _gtk_file_system_parse (chooser_entry->file_system,
534 chooser_entry->base_folder,
540 g_free (text_up_to_cursor);
545 g_assert (parsed_folder_file != NULL
546 && chooser_entry->current_folder != NULL
547 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
549 g_object_unref (parsed_folder_file);
551 /* First pass: find the common prefix */
553 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
560 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
562 DISPLAY_NAME_COLUMN, &display_name,
566 if (g_str_has_prefix (display_name, parsed_file_part))
568 if (!*common_prefix_ret)
570 *common_prefix_ret = trim_dir_separator_suffix (display_name);
571 *unique_file_ret = g_object_ref (file);
575 gchar *p = *common_prefix_ret;
576 const gchar *q = display_name;
578 while (*p && *p == *q)
586 if (*unique_file_ret)
588 g_object_unref (*unique_file_ret);
589 *unique_file_ret = NULL;
594 g_free (display_name);
595 g_object_unref (file);
596 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
599 /* Second pass: see if the prefix we found is a complete match */
601 if (*common_prefix_ret != NULL)
603 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
610 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
612 DISPLAY_NAME_COLUMN, &display_name,
614 len = strlen (display_name);
617 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
620 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
621 *is_complete_not_unique_ret = TRUE;
623 g_free (display_name);
624 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
627 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
629 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
632 g_free (parsed_file_part);
638 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
645 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
646 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
650 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
651 if (G_IS_DIR_SEPARATOR (*next_char_str))
654 g_free (next_char_str);
661 INVALID_INPUT, /* what the user typed is bogus */
662 NO_MATCH, /* no matches based on what the user typed */
663 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
664 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
665 COMPLETED, /* completion inserted (ambiguous suffix) */
666 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
667 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
668 } CommonPrefixResult;
670 /* Finds a common prefix based on the contents of the entry
671 * and mandatorily appends it
673 static CommonPrefixResult
674 append_common_prefix (GtkFileChooserEntry *chooser_entry,
676 gboolean show_errors)
678 gchar *common_prefix;
680 gboolean is_complete_not_unique;
681 gboolean prefix_expands_the_file_part;
683 CommonPrefixResult result = NO_MATCH;
684 gboolean have_result;
686 clear_completions (chooser_entry);
688 if (chooser_entry->completion_store == NULL)
692 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
694 /* If the user types an incomplete hostname ("http://foo" without
695 * a slash after that), it's not an error. We just don't want to
696 * pop up a meaningless completion window in that state.
698 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
701 beep (chooser_entry);
702 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
705 g_error_free (error);
707 return INVALID_INPUT;
714 if (!char_after_cursor_is_directory_separator (chooser_entry))
718 common_prefix = maybe_append_separator_to_file (chooser_entry,
723 prefix_expands_the_file_part = TRUE;
726 g_object_unref (unique_file);
728 if (prefix_expands_the_file_part)
729 result = COMPLETED_UNIQUE;
731 result = NOTHING_INSERTED_UNIQUE;
737 if (is_complete_not_unique)
739 result = COMPLETE_BUT_NOT_UNIQUE;
749 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
751 pos = chooser_entry->file_part_pos;
753 if (prefix_expands_the_file_part)
755 chooser_entry->in_change = TRUE;
756 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
758 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
761 chooser_entry->in_change = FALSE;
765 /* equivalent to cursor_pos + common_prefix_len); */
766 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
769 chooser_entry->has_completion = TRUE;
772 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
774 else if (!have_result)
776 result = NOTHING_INSERTED_COMPLETE;
780 g_free (common_prefix);
797 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
798 const gchar *new_text,
799 gint new_text_length,
802 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
806 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
807 insert_pos = *position;
809 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
811 if (chooser_entry->in_change)
814 remove_completion_feedback (chooser_entry);
816 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
817 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
818 && insert_pos == old_text_len)
819 install_start_autocompletion_idle (chooser_entry);
823 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
825 if (chooser_entry->in_change)
828 clear_completions (chooser_entry);
832 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
836 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
838 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
840 clear_completions_if_not_in_change (chooser_entry);
844 gtk_file_chooser_entry_set_position (GtkEditable *editable,
847 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
849 parent_editable_iface->set_position (editable, position);
851 clear_completions_if_not_in_change (chooser_entry);
855 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
859 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
861 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
863 clear_completions_if_not_in_change (chooser_entry);
867 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
869 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
870 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
874 gtk_file_chooser_entry_unmap (GtkWidget *widget)
876 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
878 remove_completion_feedback (chooser_entry);
880 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
884 completion_feedback_window_draw_cb (GtkWidget *widget,
888 /* Stolen from gtk_tooltip_paint_window() */
890 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
891 GtkStyleContext *context;
893 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
895 gtk_render_background (context, cr, 0, 0,
896 gtk_widget_get_allocated_width (widget),
897 gtk_widget_get_allocated_height (widget));
898 gtk_render_frame (context, cr, 0, 0,
899 gtk_widget_get_allocated_width (widget),
900 gtk_widget_get_allocated_height (widget));
906 set_invisible_mouse_cursor (GdkWindow *window)
911 display = gdk_window_get_display (window);
912 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
914 gdk_window_set_cursor (window, cursor);
916 g_object_unref (cursor);
920 completion_feedback_window_realize_cb (GtkWidget *widget,
923 /* We hide the mouse cursor inside the completion feedback window, since
924 * GtkEntry hides the cursor when the user types. We don't want the cursor to
925 * come back if the completion feedback ends up where the mouse is.
927 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
931 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
933 /* Stolen from gtk_tooltip_init() */
935 GtkStyleContext *context;
936 GtkWidget *alignment;
937 GtkBorder padding, border;
939 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
940 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
941 GDK_WINDOW_TYPE_HINT_TOOLTIP);
942 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
943 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
944 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
946 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
947 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
948 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
950 gtk_style_context_get_padding (context, 0, &padding);
951 gtk_style_context_get_border (context, 0, &border);
953 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
954 border.top + padding.top,
955 border.bottom + padding.bottom,
956 border.left + padding.left,
957 border.right + padding.right);
958 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
959 gtk_widget_show (alignment);
961 g_signal_connect (chooser_entry->completion_feedback_window, "draw",
962 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
963 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
964 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
965 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
967 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
968 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
969 gtk_widget_show (chooser_entry->completion_feedback_label);
973 completion_feedback_timeout_cb (gpointer data)
975 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
977 chooser_entry->completion_feedback_timeout_id = 0;
979 remove_completion_feedback (chooser_entry);
984 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
986 if (chooser_entry->completion_feedback_timeout_id != 0)
987 g_source_remove (chooser_entry->completion_feedback_timeout_id);
989 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
990 completion_feedback_timeout_cb,
994 /* Gets the x position of the text cursor in the entry, in widget coordinates */
996 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
999 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
1000 * the reader, you have to implement support for the entry's scroll offset and
1001 * RTL layouts and all the fancy Pango stuff.
1004 PangoLayout *layout;
1005 gint layout_x, layout_y;
1007 PangoRectangle strong_pos;
1008 gint start_pos, end_pos;
1010 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1012 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1014 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
1015 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1019 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1021 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1025 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1027 /* More or less stolen from gtk_tooltip_position() */
1029 GtkRequisition feedback_req;
1030 GtkWidget *widget = GTK_WIDGET (chooser_entry);
1031 gint entry_x, entry_y;
1033 GtkAllocation entry_allocation;
1034 int feedback_x, feedback_y;
1036 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
1037 &feedback_req, NULL);
1039 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1040 gtk_widget_get_allocation (widget, &entry_allocation);
1042 get_entry_cursor_x (chooser_entry, &cursor_x);
1044 /* FIXME: fit to the screen if we bump on the screen's edge */
1045 /* cheap "half M-width", use height as approximation of character em-size */
1046 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1047 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1049 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1050 gtk_widget_show (chooser_entry->completion_feedback_window);
1052 install_completion_feedback_timer (chooser_entry);
1056 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1057 const gchar *feedback)
1059 if (chooser_entry->completion_feedback_window == NULL)
1060 create_completion_feedback_window (chooser_entry);
1062 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1064 show_completion_feedback_window (chooser_entry);
1068 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1070 if (chooser_entry->completion_feedback_window)
1071 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1073 chooser_entry->completion_feedback_window = NULL;
1074 chooser_entry->completion_feedback_label = NULL;
1076 if (chooser_entry->completion_feedback_timeout_id != 0)
1078 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1079 chooser_entry->completion_feedback_timeout_id = 0;
1084 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1086 CommonPrefixResult result;
1088 g_assert (chooser_entry->current_folder != NULL);
1089 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1091 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1093 * - If there is a common prefix, insert it (done)
1094 * - If there is no common prefix, pop up the suggestion window
1095 * - If there are no matches at all, beep and bring up a tooltip (done)
1096 * - If the suggestion window is already up, scroll it
1098 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1103 /* We already beeped in append_common_prefix(); do nothing here */
1107 beep (chooser_entry);
1108 /* translators: this text is shown when there are no completions
1109 * for something the user typed in a file chooser entry
1111 pop_up_completion_feedback (chooser_entry, _("No match"));
1114 case NOTHING_INSERTED_COMPLETE:
1115 /* FIXME: pop up the suggestion window or scroll it */
1118 case NOTHING_INSERTED_UNIQUE:
1119 /* translators: this text is shown when there is exactly one completion
1120 * for something the user typed in a file chooser entry
1122 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1129 case COMPLETED_UNIQUE:
1133 case COMPLETE_BUT_NOT_UNIQUE:
1134 /* translators: this text is shown when the text in a file chooser
1135 * entry is a complete filename, but could be continued to find
1138 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1142 g_assert_not_reached ();
1147 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1149 RefreshStatus status;
1153 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1160 g_assert (chooser_entry->current_folder_file != NULL);
1162 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1163 explicitly_complete (chooser_entry);
1166 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1168 /* Translators: this text is shown while the system is searching
1169 * for possible completions for filenames in a file chooser entry. */
1170 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1175 case REFRESH_INVALID_INPUT:
1177 /* Translators: this is shown in the feedback for Tab-completion in a file
1178 * chooser's text entry, when the user enters an invalid path. */
1179 feedback_msg = _("Invalid path");
1182 case REFRESH_INCOMPLETE_HOSTNAME:
1185 if (chooser_entry->local_only)
1187 /* hostnames in a local_only file chooser? user error */
1189 /* Translators: this is shown in the feedback for Tab-completion in a
1190 * file chooser's text entry when the user enters something like
1191 * "sftp://blahblah" in an app that only supports local filenames. */
1192 feedback_msg = _("Only local files may be selected");
1196 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1198 /* Translators: this is shown in the feedback for Tab-completion in a
1199 * file chooser's text entry when the user hasn't entered the first '/'
1200 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1201 feedback_msg = _("Incomplete hostname; end it with '/'");
1206 case REFRESH_NONEXISTENT:
1209 /* Translators: this is shown in the feedback for Tab-completion in a file
1210 * chooser's text entry when the user enters a path that does not exist
1211 * and then hits Tab */
1212 feedback_msg = _("Path does not exist");
1215 case REFRESH_NOT_LOCAL:
1217 feedback_msg = _("Only local files may be selected");
1221 g_assert_not_reached ();
1227 g_assert (chooser_entry->current_folder_file == NULL);
1229 beep (chooser_entry);
1230 pop_up_completion_feedback (chooser_entry, feedback_msg);
1231 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1236 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1239 GtkFileChooserEntry *chooser_entry;
1240 GtkEditable *editable;
1242 GdkModifierType state;
1243 gboolean control_pressed;
1245 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1246 editable = GTK_EDITABLE (widget);
1247 entry = GTK_ENTRY (widget);
1249 if (!chooser_entry->eat_tabs)
1250 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1252 control_pressed = FALSE;
1254 if (gtk_get_current_event_state (&state))
1256 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1257 control_pressed = TRUE;
1260 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1261 * makes it 'safe' for people to hit. */
1262 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1264 if (chooser_entry->has_completion)
1265 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1267 start_explicit_completion (chooser_entry);
1272 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1277 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1278 GdkEventFocus *event)
1280 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1282 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1284 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1288 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1290 if (chooser_entry->has_completion)
1292 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1293 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1296 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1297 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1301 gtk_file_chooser_entry_activate (GtkEntry *entry)
1303 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1305 commit_completion_and_refresh (chooser_entry);
1306 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1310 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1312 if (!chooser_entry->completion_store)
1315 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1316 g_object_unref (chooser_entry->completion_store);
1317 chooser_entry->completion_store = NULL;
1320 /* Fills the completion store from the contents of the current folder */
1322 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1327 discard_completion_store (chooser_entry);
1329 files = _gtk_folder_list_children (chooser_entry->current_folder);
1331 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1335 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1340 file = tmp_list->data;
1342 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1346 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1350 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1352 gtk_list_store_append (chooser_entry->completion_store, &iter);
1353 gtk_list_store_set (chooser_entry->completion_store, &iter,
1354 DISPLAY_NAME_COLUMN, display_name,
1358 g_object_unref (info);
1359 g_free (display_name);
1363 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1364 g_slist_free (files);
1366 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1367 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1369 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1370 GTK_TREE_MODEL (chooser_entry->completion_store));
1373 /* When we finish loading the current folder, this function should get called to
1374 * perform the deferred autocompletion or explicit completion.
1377 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1379 switch (chooser_entry->load_complete_action)
1381 case LOAD_COMPLETE_NOTHING:
1384 case LOAD_COMPLETE_AUTOCOMPLETE:
1385 autocomplete (chooser_entry);
1388 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1389 explicitly_complete (chooser_entry);
1393 g_assert_not_reached ();
1396 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1400 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1402 populate_completion_store (chooser_entry);
1403 perform_load_complete_action (chooser_entry);
1405 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1408 /* Callback when the current folder finishes loading */
1410 finished_loading_cb (GtkFolder *folder,
1413 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1415 finish_folder_load (chooser_entry);
1418 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1420 load_directory_get_folder_callback (GCancellable *cancellable,
1422 const GError *error,
1425 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1426 GtkFileChooserEntry *chooser_entry = data;
1428 if (cancellable != chooser_entry->load_folder_cancellable)
1431 chooser_entry->load_folder_cancellable = NULL;
1435 LoadCompleteAction old_load_complete_action;
1437 old_load_complete_action = chooser_entry->load_complete_action;
1439 discard_completion_store (chooser_entry);
1440 clear_completions (chooser_entry);
1442 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1444 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1446 beep (chooser_entry);
1447 pop_up_completion_feedback (chooser_entry, error->message);
1450 discard_current_folder (chooser_entry);
1453 if (cancelled || error)
1456 g_assert (folder != NULL);
1457 chooser_entry->current_folder = g_object_ref (folder);
1459 discard_completion_store (chooser_entry);
1461 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1462 finish_folder_load (chooser_entry);
1464 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1465 G_CALLBACK (finished_loading_cb), chooser_entry);
1468 g_object_unref (chooser_entry);
1469 g_object_unref (cancellable);
1472 static RefreshStatus
1473 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1475 if (chooser_entry->file_system == NULL)
1478 g_assert (chooser_entry->current_folder_file != NULL);
1479 g_assert (chooser_entry->current_folder == NULL);
1480 g_assert (chooser_entry->load_folder_cancellable == NULL);
1482 if (chooser_entry->local_only
1483 && !g_file_is_native (chooser_entry->current_folder_file))
1485 g_object_unref (chooser_entry->current_folder_file);
1486 chooser_entry->current_folder_file = NULL;
1488 return REFRESH_NOT_LOCAL;
1491 chooser_entry->load_folder_cancellable =
1492 _gtk_file_system_get_folder (chooser_entry->file_system,
1493 chooser_entry->current_folder_file,
1494 "standard::name,standard::display-name,standard::type",
1495 load_directory_get_folder_callback,
1496 g_object_ref (chooser_entry));
1501 static RefreshStatus
1502 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1504 gboolean force_reload)
1506 gboolean reload = FALSE;
1508 g_assert (folder_file != NULL);
1510 if (chooser_entry->current_folder_file)
1512 if ((!(g_file_equal (folder_file, chooser_entry->current_folder_file)
1513 && chooser_entry->load_folder_cancellable))
1518 discard_current_folder (chooser_entry);
1519 discard_loading_and_current_folder_file (chooser_entry);
1521 chooser_entry->current_folder_file = g_object_ref (folder_file);
1526 chooser_entry->current_folder_file = g_object_ref (folder_file);
1531 return start_loading_current_folder (chooser_entry);
1536 static RefreshStatus
1537 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1538 RefreshMode refresh_mode)
1540 GtkEditable *editable;
1545 gsize total_len, file_part_len;
1548 RefreshStatus result;
1550 editable = GTK_EDITABLE (chooser_entry);
1552 switch (refresh_mode)
1554 case REFRESH_UP_TO_CURSOR_POSITION:
1555 end_pos = gtk_editable_get_position (editable);
1558 case REFRESH_WHOLE_TEXT:
1559 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1563 g_assert_not_reached ();
1564 return REFRESH_INVALID_INPUT;
1567 text = gtk_editable_get_chars (editable, 0, end_pos);
1570 if (!chooser_entry->file_system ||
1571 !chooser_entry->base_folder ||
1572 !_gtk_file_system_parse (chooser_entry->file_system,
1573 chooser_entry->base_folder, text,
1574 &folder_file, &file_part, &error))
1576 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1579 result = REFRESH_INCOMPLETE_HOSTNAME;
1583 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1585 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1586 result = REFRESH_NONEXISTENT;
1588 result = REFRESH_INVALID_INPUT;
1592 g_error_free (error);
1594 file_part = g_strdup ("");
1599 g_assert (folder_file != NULL);
1601 file_part_len = strlen (file_part);
1602 total_len = strlen (text);
1603 if (total_len > file_part_len)
1604 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1608 result = REFRESH_OK;
1613 g_free (chooser_entry->file_part);
1615 chooser_entry->file_part = file_part;
1616 chooser_entry->file_part_pos = file_part_pos;
1618 if (result == REFRESH_OK)
1620 result = reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1624 discard_current_folder (chooser_entry);
1625 discard_loading_and_current_folder_file (chooser_entry);
1629 g_object_unref (folder_file);
1631 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1632 ((result == REFRESH_OK)
1633 && (chooser_entry->current_folder_file != NULL)
1634 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1635 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1636 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1637 || ((result != REFRESH_OK)
1638 && (chooser_entry->current_folder_file == NULL)
1639 && (chooser_entry->load_folder_cancellable == NULL)
1640 && (chooser_entry->current_folder == NULL)));
1646 autocomplete (GtkFileChooserEntry *chooser_entry)
1648 if (!(chooser_entry->current_folder != NULL
1649 && _gtk_folder_is_finished_loading (chooser_entry->current_folder)
1650 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1653 append_common_prefix (chooser_entry, TRUE, FALSE);
1657 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1659 RefreshStatus status;
1661 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1666 g_assert (chooser_entry->current_folder_file != NULL);
1668 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1669 autocomplete (chooser_entry);
1671 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1675 case REFRESH_INVALID_INPUT:
1676 case REFRESH_INCOMPLETE_HOSTNAME:
1677 case REFRESH_NONEXISTENT:
1678 case REFRESH_NOT_LOCAL:
1679 /* We don't beep or anything, since this is autocompletion - the user
1680 * didn't request any action explicitly.
1685 g_assert_not_reached ();
1690 start_autocompletion_idle_handler (gpointer data)
1692 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1694 start_autocompletion (chooser_entry);
1696 chooser_entry->start_autocompletion_idle_id = 0;
1702 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1704 if (chooser_entry->start_autocompletion_idle_id != 0)
1707 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1712 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1713 const gchar *new_text,
1714 gint new_text_length,
1718 const gchar *colon = memchr (new_text, ':', new_text_length);
1721 /* Disallow these characters altogether */
1722 for (i = 0; i < new_text_length; i++)
1724 if (new_text[i] == '<' ||
1725 new_text[i] == '>' ||
1726 new_text[i] == '"' ||
1727 new_text[i] == '|' ||
1728 new_text[i] == '*' ||
1733 if (i < new_text_length ||
1734 /* Disallow entering text that would cause a colon to be anywhere except
1735 * after a drive letter.
1738 *position + (colon - new_text) != 1) ||
1739 (new_text_length > 0 &&
1741 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1742 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1744 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1745 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1753 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1758 /* If deleting a drive letter, delete the colon, too */
1759 if (start_pos == 0 && end_pos == 1 &&
1760 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1761 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1763 g_signal_handlers_block_by_func (chooser_entry,
1764 G_CALLBACK (delete_text_callback),
1766 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1767 g_signal_handlers_unblock_by_func (chooser_entry,
1768 G_CALLBACK (delete_text_callback),
1775 * _gtk_file_chooser_entry_new:
1776 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1778 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1779 * is an internal implementation widget for the GTK+ file chooser
1780 * which is an entry with completion with respect to a
1781 * #GtkFileSystem object.
1783 * Return value: the newly created #GtkFileChooserEntry
1786 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1788 GtkFileChooserEntry *chooser_entry;
1790 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1791 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1793 return GTK_WIDGET (chooser_entry);
1797 * _gtk_file_chooser_entry_set_file_system:
1798 * @chooser_entry: a #GtkFileChooser
1799 * @file_system: an object implementing #GtkFileSystem
1801 * Sets the file system for @chooser_entry.
1804 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1805 GtkFileSystem *file_system)
1807 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1808 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1810 if (file_system != chooser_entry->file_system)
1812 if (chooser_entry->file_system)
1813 g_object_unref (chooser_entry->file_system);
1815 chooser_entry->file_system = g_object_ref (file_system);
1820 * _gtk_file_chooser_entry_set_base_folder:
1821 * @chooser_entry: a #GtkFileChooserEntry
1822 * @file: file for a folder in the chooser entries current file system.
1824 * Sets the folder with respect to which completions occur.
1827 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1830 if (chooser_entry->base_folder)
1831 g_object_unref (chooser_entry->base_folder);
1833 chooser_entry->base_folder = file;
1835 if (chooser_entry->base_folder)
1836 g_object_ref (chooser_entry->base_folder);
1838 clear_completions (chooser_entry);
1839 _gtk_file_chooser_entry_select_filename (chooser_entry);
1843 * _gtk_file_chooser_entry_get_current_folder:
1844 * @chooser_entry: a #GtkFileChooserEntry
1846 * Gets the current folder for the #GtkFileChooserEntry. If the
1847 * user has only entered a filename, this will be in the base folder
1848 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1849 * user has entered a relative or absolute path, then it will
1850 * be different. If the user has entered unparsable text, or text which
1851 * the entry cannot handle, this will return %NULL.
1853 * Return value: the file for the current folder - this value is owned by the
1854 * chooser entry and must not be modified or freed.
1857 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1859 commit_completion_and_refresh (chooser_entry);
1860 return chooser_entry->current_folder_file;
1864 * _gtk_file_chooser_entry_get_file_part:
1865 * @chooser_entry: a #GtkFileChooserEntry
1867 * Gets the non-folder portion of whatever the user has entered
1868 * into the file selector. What is returned is a UTF-8 string,
1869 * and if a filename path is needed, g_file_get_child_for_display_name()
1872 * Return value: the entered filename - this value is owned by the
1873 * chooser entry and must not be modified or freed.
1876 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1878 commit_completion_and_refresh (chooser_entry);
1879 return chooser_entry->file_part;
1883 * _gtk_file_chooser_entry_set_file_part:
1884 * @chooser_entry: a #GtkFileChooserEntry
1885 * @file_part: text to display in the entry, in UTF-8
1887 * Sets the current text shown in the file chooser entry.
1890 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1891 const gchar *file_part)
1893 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1895 chooser_entry->in_change = TRUE;
1896 clear_completions (chooser_entry);
1897 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1898 chooser_entry->in_change = FALSE;
1903 * _gtk_file_chooser_entry_set_action:
1904 * @chooser_entry: a #GtkFileChooserEntry
1905 * @action: the action which is performed by the file selector using this entry
1907 * Sets action which is performed by the file selector using this entry.
1908 * The #GtkFileChooserEntry will use different completion strategies for
1909 * different actions.
1912 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1913 GtkFileChooserAction action)
1915 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1917 if (chooser_entry->action != action)
1919 GtkEntryCompletion *comp;
1921 chooser_entry->action = action;
1923 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1925 /* FIXME: do we need to actually set the following? */
1929 case GTK_FILE_CHOOSER_ACTION_OPEN:
1930 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1931 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1933 case GTK_FILE_CHOOSER_ACTION_SAVE:
1934 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1935 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1943 * _gtk_file_chooser_entry_get_action:
1944 * @chooser_entry: a #GtkFileChooserEntry
1946 * Gets the action for this entry.
1948 * Returns: the action
1950 GtkFileChooserAction
1951 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1953 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1954 GTK_FILE_CHOOSER_ACTION_OPEN);
1956 return chooser_entry->action;
1960 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1963 gboolean retval = FALSE;
1965 if (chooser_entry->current_folder)
1967 GFileInfo *file_info;
1969 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1973 retval = _gtk_file_info_consider_as_directory (file_info);
1974 g_object_unref (file_info);
1983 * _gtk_file_chooser_entry_select_filename:
1984 * @chooser_entry: a #GtkFileChooserEntry
1986 * Selects the filename (without the extension) for user edition.
1989 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1991 const gchar *str, *ext;
1994 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1996 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1997 ext = g_strrstr (str, ".");
2000 len = g_utf8_pointer_to_offset (str, ext);
2003 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
2007 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
2008 gboolean local_only)
2010 chooser_entry->local_only = local_only;
2011 clear_completions (chooser_entry);
2015 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
2017 return chooser_entry->local_only;