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 "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
32 #include "gtksizerequest.h"
33 #include "gtkwindow.h"
36 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
38 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
39 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
40 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
42 struct _GtkFileChooserEntryClass
44 GtkEntryClass parent_class;
47 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
49 LOAD_COMPLETE_NOTHING,
50 LOAD_COMPLETE_AUTOCOMPLETE,
51 LOAD_COMPLETE_EXPLICIT_COMPLETION
57 REFRESH_INVALID_INPUT,
58 REFRESH_INCOMPLETE_HOSTNAME,
63 struct _GtkFileChooserEntry
65 GtkEntry parent_instance;
67 GtkFileChooserAction action;
69 GtkFileSystem *file_system;
71 GFile *current_folder_file;
75 /* Folder being loaded or already loaded */
76 GtkFolder *current_folder;
77 GCancellable *load_folder_cancellable;
79 LoadCompleteAction load_complete_action;
81 GtkListStore *completion_store;
83 guint start_autocompletion_idle_id;
85 GtkWidget *completion_feedback_window;
86 GtkWidget *completion_feedback_label;
87 guint completion_feedback_timeout_id;
89 guint current_folder_loaded : 1;
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),
227 "text", DISPLAY_NAME_COLUMN);
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_loading_and_current_folder_file (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;
272 if (chooser_entry->load_folder_cancellable)
274 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
275 chooser_entry->load_folder_cancellable = NULL;
278 if (chooser_entry->current_folder_file)
280 g_object_unref (chooser_entry->current_folder_file);
281 chooser_entry->current_folder_file = NULL;
286 gtk_file_chooser_entry_dispose (GObject *object)
288 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
290 remove_completion_feedback (chooser_entry);
291 discard_loading_and_current_folder_file (chooser_entry);
293 if (chooser_entry->start_autocompletion_idle_id != 0)
295 g_source_remove (chooser_entry->start_autocompletion_idle_id);
296 chooser_entry->start_autocompletion_idle_id = 0;
299 if (chooser_entry->completion_store)
301 g_object_unref (chooser_entry->completion_store);
302 chooser_entry->completion_store = NULL;
305 if (chooser_entry->file_system)
307 g_object_unref (chooser_entry->file_system);
308 chooser_entry->file_system = NULL;
311 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
314 /* Match functions for the GtkEntryCompletion */
316 match_selected_callback (GtkEntryCompletion *completion,
319 GtkFileChooserEntry *chooser_entry)
326 gtk_tree_model_get (model, iter,
327 DISPLAY_NAME_COLUMN, &display_name,
331 if (!display_name || !file)
334 g_object_unref (file);
336 g_free (display_name);
340 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
342 pos = chooser_entry->file_part_pos;
344 /* We don't set in_change here as we want to update the current_folder
346 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
348 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
351 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
353 g_object_unref (file);
354 g_free (display_name);
359 /* Match function for the GtkEntryCompletion */
361 completion_match_func (GtkEntryCompletion *comp,
362 const char *key_unused,
366 GtkFileChooserEntry *chooser_entry;
369 char *norm_file_part;
372 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
374 /* We ignore the key because it is the contents of the entry. Instead, we
375 * just use our precomputed file_part.
377 if (!chooser_entry->file_part)
382 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
385 return FALSE; /* Uninitialized row, ugh */
388 /* If we have an empty file_part, then we're at the root of a directory. In
389 * that case, we want to match all non-dot files. We might want to match
390 * dot_files too if show_hidden is TRUE on the fileselector in the future.
392 /* Additionally, support for gnome .hidden files would be sweet, too */
393 if (chooser_entry->file_part[0] == '\000')
405 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
406 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
408 #ifdef G_PLATFORM_WIN32
412 temp = norm_file_part;
413 norm_file_part = g_utf8_casefold (norm_file_part, -1);
417 norm_name = g_utf8_casefold (norm_name, -1);
422 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
424 g_free (norm_file_part);
432 clear_completions (GtkFileChooserEntry *chooser_entry)
434 chooser_entry->has_completion = FALSE;
435 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
437 remove_completion_feedback (chooser_entry);
441 beep (GtkFileChooserEntry *chooser_entry)
443 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
446 /* This function will append a directory separator to paths to
447 * display_name iff the path associated with it is a directory.
448 * maybe_append_separator_to_file will g_free the display_name and
449 * return a new one if needed. Otherwise, it will return the old one.
450 * You should be safe calling
452 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
454 * g_free (display_name);
457 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
464 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
468 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
472 if (_gtk_file_info_consider_as_directory (info))
474 gchar *tmp = display_name;
475 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
480 g_object_unref (info);
488 trim_dir_separator_suffix (const char *str)
493 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
494 return g_strndup (str, len - 1);
496 return g_strdup (str);
499 /* Determines if the completion model has entries with a common prefix relative
500 * to the current contents of the entry. Also, if there's one and only one such
501 * path, stores it in unique_path_ret.
504 find_common_prefix (GtkFileChooserEntry *chooser_entry,
505 gchar **common_prefix_ret,
506 GFile **unique_file_ret,
507 gboolean *is_complete_not_unique_ret,
508 gboolean *prefix_expands_the_file_part_ret,
511 GtkEditable *editable;
515 char *text_up_to_cursor;
516 GFile *parsed_folder_file;
517 char *parsed_file_part;
519 *common_prefix_ret = NULL;
520 *unique_file_ret = NULL;
521 *is_complete_not_unique_ret = FALSE;
522 *prefix_expands_the_file_part_ret = FALSE;
524 editable = GTK_EDITABLE (chooser_entry);
526 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
528 parsed = _gtk_file_system_parse (chooser_entry->file_system,
529 chooser_entry->base_folder,
535 g_free (text_up_to_cursor);
540 g_assert (parsed_folder_file != NULL
541 && chooser_entry->current_folder != NULL
542 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
544 g_object_unref (parsed_folder_file);
546 /* First pass: find the common prefix */
548 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
555 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
557 DISPLAY_NAME_COLUMN, &display_name,
561 if (g_str_has_prefix (display_name, parsed_file_part))
563 if (!*common_prefix_ret)
565 *common_prefix_ret = trim_dir_separator_suffix (display_name);
566 *unique_file_ret = g_object_ref (file);
570 gchar *p = *common_prefix_ret;
571 const gchar *q = display_name;
573 while (*p && *p == *q)
581 if (*unique_file_ret)
583 g_object_unref (*unique_file_ret);
584 *unique_file_ret = NULL;
589 g_free (display_name);
590 g_object_unref (file);
591 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
594 /* Second pass: see if the prefix we found is a complete match */
596 if (*common_prefix_ret != NULL)
598 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
605 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
607 DISPLAY_NAME_COLUMN, &display_name,
609 len = strlen (display_name);
612 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
615 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
616 *is_complete_not_unique_ret = TRUE;
618 g_free (display_name);
619 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
622 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
624 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
627 g_free (parsed_file_part);
633 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
640 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
641 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
645 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
646 if (G_IS_DIR_SEPARATOR (*next_char_str))
649 g_free (next_char_str);
656 INVALID_INPUT, /* what the user typed is bogus */
657 NO_MATCH, /* no matches based on what the user typed */
658 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
659 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
660 COMPLETED, /* completion inserted (ambiguous suffix) */
661 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
662 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
663 } CommonPrefixResult;
665 /* Finds a common prefix based on the contents of the entry
666 * 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
690 * a slash after that), it's not an error. We just don't want to
691 * pop up a 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;
744 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
746 pos = chooser_entry->file_part_pos;
748 if (prefix_expands_the_file_part)
750 chooser_entry->in_change = TRUE;
751 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
753 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
756 chooser_entry->in_change = FALSE;
760 /* equivalent to cursor_pos + common_prefix_len); */
761 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
764 chooser_entry->has_completion = TRUE;
767 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
769 else if (!have_result)
771 result = NOTHING_INSERTED_COMPLETE;
775 g_free (common_prefix);
792 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
793 const gchar *new_text,
794 gint new_text_length,
797 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
801 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
802 insert_pos = *position;
804 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
806 if (chooser_entry->in_change)
809 remove_completion_feedback (chooser_entry);
811 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
812 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
813 && insert_pos == old_text_len)
814 install_start_autocompletion_idle (chooser_entry);
818 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
820 if (chooser_entry->in_change)
823 clear_completions (chooser_entry);
827 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
831 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
833 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
835 clear_completions_if_not_in_change (chooser_entry);
839 gtk_file_chooser_entry_set_position (GtkEditable *editable,
842 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
844 parent_editable_iface->set_position (editable, position);
846 clear_completions_if_not_in_change (chooser_entry);
850 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
854 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
856 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
858 clear_completions_if_not_in_change (chooser_entry);
862 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
864 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
865 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
869 gtk_file_chooser_entry_unmap (GtkWidget *widget)
871 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
873 remove_completion_feedback (chooser_entry);
875 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
879 completion_feedback_window_draw_cb (GtkWidget *widget,
883 /* Stolen from gtk_tooltip_paint_window() */
885 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
886 GtkStyleContext *context;
888 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
890 gtk_render_background (context, cr, 0, 0,
891 gtk_widget_get_allocated_width (widget),
892 gtk_widget_get_allocated_height (widget));
893 gtk_render_frame (context, cr, 0, 0,
894 gtk_widget_get_allocated_width (widget),
895 gtk_widget_get_allocated_height (widget));
901 set_invisible_mouse_cursor (GdkWindow *window)
906 display = gdk_window_get_display (window);
907 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
909 gdk_window_set_cursor (window, cursor);
911 g_object_unref (cursor);
915 completion_feedback_window_realize_cb (GtkWidget *widget,
918 /* We hide the mouse cursor inside the completion feedback window, since
919 * GtkEntry hides the cursor when the user types. We don't want the cursor to
920 * come back if the completion feedback ends up where the mouse is.
922 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
926 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
928 /* Stolen from gtk_tooltip_init() */
929 GtkWidget *window, *label;
930 GtkStyleContext *context;
932 window = gtk_window_new (GTK_WINDOW_POPUP);
933 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
934 gtk_widget_set_app_paintable (window, TRUE);
935 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
936 gtk_widget_set_name (window, "gtk-tooltip");
938 context = gtk_widget_get_style_context (window);
939 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
941 g_signal_connect (window, "draw",
942 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
943 g_signal_connect (window, "realize",
944 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
945 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
947 label = gtk_label_new (NULL);
948 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
949 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
950 /* FIXME: don't hardcode this */
951 gtk_widget_set_margin_left (label, 6);
952 gtk_widget_set_margin_right (label, 6);
953 gtk_widget_set_margin_top (label, 6);
954 gtk_widget_set_margin_bottom (label, 6);
955 gtk_container_add (GTK_CONTAINER (window), label);
956 gtk_widget_show (label);
958 chooser_entry->completion_feedback_window = window;
959 chooser_entry->completion_feedback_label = label;
963 completion_feedback_timeout_cb (gpointer data)
965 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
967 chooser_entry->completion_feedback_timeout_id = 0;
969 remove_completion_feedback (chooser_entry);
974 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
976 if (chooser_entry->completion_feedback_timeout_id != 0)
977 g_source_remove (chooser_entry->completion_feedback_timeout_id);
979 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
980 completion_feedback_timeout_cb,
984 /* Gets the x position of the text cursor in the entry, in widget coordinates */
986 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
989 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
990 * the reader, you have to implement support for the entry's scroll offset and
991 * RTL layouts and all the fancy Pango stuff.
995 gint layout_x, layout_y;
997 PangoRectangle strong_pos;
998 gint start_pos, end_pos;
1000 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1002 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1004 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
1005 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1009 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1011 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1015 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1017 /* More or less stolen from gtk_tooltip_position() */
1019 GtkRequisition feedback_req;
1020 GtkWidget *widget = GTK_WIDGET (chooser_entry);
1021 gint entry_x, entry_y;
1023 GtkAllocation entry_allocation;
1024 int feedback_x, feedback_y;
1026 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
1027 &feedback_req, NULL);
1029 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1030 gtk_widget_get_allocation (widget, &entry_allocation);
1032 get_entry_cursor_x (chooser_entry, &cursor_x);
1034 /* FIXME: fit to the screen if we bump on the screen's edge */
1035 /* cheap "half M-width", use height as approximation of character em-size */
1036 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1037 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1039 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1040 gtk_widget_show (chooser_entry->completion_feedback_window);
1042 install_completion_feedback_timer (chooser_entry);
1046 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1047 const gchar *feedback)
1049 if (chooser_entry->completion_feedback_window == NULL)
1050 create_completion_feedback_window (chooser_entry);
1052 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1054 show_completion_feedback_window (chooser_entry);
1058 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1060 if (chooser_entry->completion_feedback_window)
1061 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1063 chooser_entry->completion_feedback_window = NULL;
1064 chooser_entry->completion_feedback_label = NULL;
1066 if (chooser_entry->completion_feedback_timeout_id != 0)
1068 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1069 chooser_entry->completion_feedback_timeout_id = 0;
1074 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1076 CommonPrefixResult result;
1078 g_assert (chooser_entry->current_folder != NULL);
1079 g_assert (chooser_entry->current_folder_loaded);
1081 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1083 * - If there is a common prefix, insert it (done)
1084 * - If there is no common prefix, pop up the suggestion window
1085 * - If there are no matches at all, beep and bring up a tooltip (done)
1086 * - If the suggestion window is already up, scroll it
1088 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1093 /* We already beeped in append_common_prefix(); do nothing here */
1097 beep (chooser_entry);
1098 /* translators: this text is shown when there are no completions
1099 * for something the user typed in a file chooser entry
1101 pop_up_completion_feedback (chooser_entry, _("No match"));
1104 case NOTHING_INSERTED_COMPLETE:
1105 /* FIXME: pop up the suggestion window or scroll it */
1108 case NOTHING_INSERTED_UNIQUE:
1109 /* translators: this text is shown when there is exactly one completion
1110 * for something the user typed in a file chooser entry
1112 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1119 case COMPLETED_UNIQUE:
1123 case COMPLETE_BUT_NOT_UNIQUE:
1124 /* translators: this text is shown when the text in a file chooser
1125 * entry is a complete filename, but could be continued to find
1128 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1132 g_assert_not_reached ();
1137 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1139 RefreshStatus status;
1143 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1150 g_assert (chooser_entry->current_folder_file != NULL);
1152 if (chooser_entry->current_folder && chooser_entry->current_folder_loaded)
1153 explicitly_complete (chooser_entry);
1156 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1158 /* Translators: this text is shown while the system is searching
1159 * for possible completions for filenames in a file chooser entry. */
1160 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1165 case REFRESH_INVALID_INPUT:
1167 /* Translators: this is shown in the feedback for Tab-completion in a file
1168 * chooser's text entry, when the user enters an invalid path. */
1169 feedback_msg = _("Invalid path");
1172 case REFRESH_INCOMPLETE_HOSTNAME:
1175 if (chooser_entry->local_only)
1177 /* hostnames in a local_only file chooser? user error */
1179 /* Translators: this is shown in the feedback for Tab-completion in a
1180 * file chooser's text entry when the user enters something like
1181 * "sftp://blahblah" in an app that only supports local filenames. */
1182 feedback_msg = _("Only local files may be selected");
1186 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1188 /* Translators: this is shown in the feedback for Tab-completion in a
1189 * file chooser's text entry when the user hasn't entered the first '/'
1190 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1191 feedback_msg = _("Incomplete hostname; end it with '/'");
1196 case REFRESH_NONEXISTENT:
1199 /* Translators: this is shown in the feedback for Tab-completion in a file
1200 * chooser's text entry when the user enters a path that does not exist
1201 * and then hits Tab */
1202 feedback_msg = _("Path does not exist");
1205 case REFRESH_NOT_LOCAL:
1207 feedback_msg = _("Only local files may be selected");
1211 g_assert_not_reached ();
1217 g_assert (chooser_entry->current_folder_file == NULL);
1219 beep (chooser_entry);
1220 pop_up_completion_feedback (chooser_entry, feedback_msg);
1221 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1226 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1229 GtkFileChooserEntry *chooser_entry;
1230 GtkEditable *editable;
1232 GdkModifierType state;
1233 gboolean control_pressed;
1235 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1236 editable = GTK_EDITABLE (widget);
1237 entry = GTK_ENTRY (widget);
1239 if (!chooser_entry->eat_tabs)
1240 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1242 control_pressed = FALSE;
1244 if (gtk_get_current_event_state (&state))
1246 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1247 control_pressed = TRUE;
1250 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1251 * makes it 'safe' for people to hit. */
1252 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1254 if (chooser_entry->has_completion)
1255 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1257 start_explicit_completion (chooser_entry);
1262 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1267 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1268 GdkEventFocus *event)
1270 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1272 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1274 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1278 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1280 if (chooser_entry->has_completion)
1282 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1283 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1286 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1287 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1291 gtk_file_chooser_entry_activate (GtkEntry *entry)
1293 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1295 commit_completion_and_refresh (chooser_entry);
1296 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1300 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1302 if (!chooser_entry->completion_store)
1305 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1306 g_object_unref (chooser_entry->completion_store);
1307 chooser_entry->completion_store = NULL;
1310 /* Fills the completion store from the contents of the current folder */
1312 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1317 discard_completion_store (chooser_entry);
1319 files = _gtk_folder_list_children (chooser_entry->current_folder);
1321 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1325 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1330 file = tmp_list->data;
1332 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1336 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1340 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1342 gtk_list_store_append (chooser_entry->completion_store, &iter);
1343 gtk_list_store_set (chooser_entry->completion_store, &iter,
1344 DISPLAY_NAME_COLUMN, display_name,
1348 g_object_unref (info);
1349 g_free (display_name);
1353 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1354 g_slist_free (files);
1356 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1357 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1359 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1360 GTK_TREE_MODEL (chooser_entry->completion_store));
1363 /* When we finish loading the current folder, this function should get called to
1364 * perform the deferred autocompletion or explicit completion.
1367 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1369 switch (chooser_entry->load_complete_action)
1371 case LOAD_COMPLETE_NOTHING:
1374 case LOAD_COMPLETE_AUTOCOMPLETE:
1375 autocomplete (chooser_entry);
1378 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1379 explicitly_complete (chooser_entry);
1383 g_assert_not_reached ();
1386 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1390 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1392 populate_completion_store (chooser_entry);
1393 perform_load_complete_action (chooser_entry);
1395 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1398 /* Callback when the current folder finishes loading */
1400 finished_loading_cb (GtkFolder *folder,
1403 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1405 chooser_entry->current_folder_loaded = TRUE;
1406 finish_folder_load (chooser_entry);
1409 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1411 load_directory_get_folder_callback (GCancellable *cancellable,
1413 const GError *error,
1416 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1417 GtkFileChooserEntry *chooser_entry = data;
1419 if (cancellable != chooser_entry->load_folder_cancellable)
1422 chooser_entry->load_folder_cancellable = NULL;
1426 LoadCompleteAction old_load_complete_action;
1428 old_load_complete_action = chooser_entry->load_complete_action;
1430 discard_completion_store (chooser_entry);
1431 clear_completions (chooser_entry);
1433 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1435 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1437 beep (chooser_entry);
1438 pop_up_completion_feedback (chooser_entry, error->message);
1442 if (cancelled || error)
1445 g_assert (folder != NULL);
1446 chooser_entry->current_folder = g_object_ref (folder);
1448 discard_completion_store (chooser_entry);
1450 chooser_entry->current_folder_loaded = _gtk_folder_is_finished_loading (chooser_entry->current_folder);
1451 if (chooser_entry->current_folder_loaded)
1452 finish_folder_load (chooser_entry);
1454 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1455 G_CALLBACK (finished_loading_cb), chooser_entry);
1458 g_object_unref (chooser_entry);
1459 g_object_unref (cancellable);
1462 static RefreshStatus
1463 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1465 if (chooser_entry->file_system == NULL)
1468 g_assert (chooser_entry->current_folder_file != NULL);
1469 g_assert (chooser_entry->current_folder == NULL);
1470 g_assert (chooser_entry->load_folder_cancellable == NULL);
1472 if (chooser_entry->local_only
1473 && !g_file_is_native (chooser_entry->current_folder_file))
1475 discard_loading_and_current_folder_file (chooser_entry);
1477 return REFRESH_NOT_LOCAL;
1480 chooser_entry->load_folder_cancellable =
1481 _gtk_file_system_get_folder (chooser_entry->file_system,
1482 chooser_entry->current_folder_file,
1483 "standard::name,standard::display-name,standard::type",
1484 load_directory_get_folder_callback,
1485 g_object_ref (chooser_entry));
1490 static RefreshStatus
1491 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1494 g_assert (folder_file != NULL);
1496 if (chooser_entry->current_folder_file
1497 && g_file_equal (folder_file, chooser_entry->current_folder_file)
1498 && chooser_entry->load_folder_cancellable)
1501 if (chooser_entry->current_folder_file)
1503 discard_loading_and_current_folder_file (chooser_entry);
1506 chooser_entry->current_folder_file = g_object_ref (folder_file);
1508 return start_loading_current_folder (chooser_entry);
1511 static RefreshStatus
1512 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1513 RefreshMode refresh_mode)
1515 GtkEditable *editable;
1520 gsize total_len, file_part_len;
1523 RefreshStatus result;
1525 editable = GTK_EDITABLE (chooser_entry);
1527 switch (refresh_mode)
1529 case REFRESH_UP_TO_CURSOR_POSITION:
1530 end_pos = gtk_editable_get_position (editable);
1533 case REFRESH_WHOLE_TEXT:
1534 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1538 g_assert_not_reached ();
1539 return REFRESH_INVALID_INPUT;
1542 text = gtk_editable_get_chars (editable, 0, end_pos);
1545 if (!chooser_entry->file_system ||
1546 !_gtk_file_system_parse (chooser_entry->file_system,
1547 chooser_entry->base_folder, text,
1548 &folder_file, &file_part, &error))
1550 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1553 result = REFRESH_INCOMPLETE_HOSTNAME;
1557 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1559 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1560 result = REFRESH_NONEXISTENT;
1562 result = REFRESH_INVALID_INPUT;
1566 g_error_free (error);
1568 file_part = g_strdup ("");
1573 g_assert (folder_file != NULL);
1575 file_part_len = strlen (file_part);
1576 total_len = strlen (text);
1577 if (total_len > file_part_len)
1578 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1582 result = REFRESH_OK;
1587 g_free (chooser_entry->file_part);
1589 chooser_entry->file_part = file_part;
1590 chooser_entry->file_part_pos = file_part_pos;
1592 if (result == REFRESH_OK)
1594 result = reload_current_folder (chooser_entry, folder_file);
1598 discard_loading_and_current_folder_file (chooser_entry);
1602 g_object_unref (folder_file);
1604 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1605 ((result == REFRESH_OK)
1606 && (chooser_entry->current_folder_file != NULL)
1607 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1608 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1609 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1610 || ((result != REFRESH_OK)
1611 && (chooser_entry->current_folder_file == NULL)
1612 && (chooser_entry->load_folder_cancellable == NULL)
1613 && (chooser_entry->current_folder == NULL)));
1619 autocomplete (GtkFileChooserEntry *chooser_entry)
1621 if (!(chooser_entry->current_folder != NULL
1622 && chooser_entry->current_folder_loaded
1623 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1626 append_common_prefix (chooser_entry, TRUE, FALSE);
1630 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1632 RefreshStatus status;
1634 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1639 g_assert (chooser_entry->current_folder_file != NULL);
1641 if (chooser_entry->current_folder && chooser_entry->current_folder_loaded)
1642 autocomplete (chooser_entry);
1644 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1648 case REFRESH_INVALID_INPUT:
1649 case REFRESH_INCOMPLETE_HOSTNAME:
1650 case REFRESH_NONEXISTENT:
1651 case REFRESH_NOT_LOCAL:
1652 /* We don't beep or anything, since this is autocompletion - the user
1653 * didn't request any action explicitly.
1658 g_assert_not_reached ();
1663 start_autocompletion_idle_handler (gpointer data)
1665 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1667 start_autocompletion (chooser_entry);
1669 chooser_entry->start_autocompletion_idle_id = 0;
1675 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1677 if (chooser_entry->start_autocompletion_idle_id != 0)
1680 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1685 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1686 const gchar *new_text,
1687 gint new_text_length,
1691 const gchar *colon = memchr (new_text, ':', new_text_length);
1694 /* Disallow these characters altogether */
1695 for (i = 0; i < new_text_length; i++)
1697 if (new_text[i] == '<' ||
1698 new_text[i] == '>' ||
1699 new_text[i] == '"' ||
1700 new_text[i] == '|' ||
1701 new_text[i] == '*' ||
1706 if (i < new_text_length ||
1707 /* Disallow entering text that would cause a colon to be anywhere except
1708 * after a drive letter.
1711 *position + (colon - new_text) != 1) ||
1712 (new_text_length > 0 &&
1714 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1715 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1717 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1718 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1726 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1731 /* If deleting a drive letter, delete the colon, too */
1732 if (start_pos == 0 && end_pos == 1 &&
1733 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1734 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1736 g_signal_handlers_block_by_func (chooser_entry,
1737 G_CALLBACK (delete_text_callback),
1739 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1740 g_signal_handlers_unblock_by_func (chooser_entry,
1741 G_CALLBACK (delete_text_callback),
1748 * _gtk_file_chooser_entry_new:
1749 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1751 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1752 * is an internal implementation widget for the GTK+ file chooser
1753 * which is an entry with completion with respect to a
1754 * #GtkFileSystem object.
1756 * Return value: the newly created #GtkFileChooserEntry
1759 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1761 GtkFileChooserEntry *chooser_entry;
1763 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1764 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1766 return GTK_WIDGET (chooser_entry);
1770 * _gtk_file_chooser_entry_set_file_system:
1771 * @chooser_entry: a #GtkFileChooser
1772 * @file_system: an object implementing #GtkFileSystem
1774 * Sets the file system for @chooser_entry.
1777 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1778 GtkFileSystem *file_system)
1780 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1781 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1783 if (file_system != chooser_entry->file_system)
1785 if (chooser_entry->file_system)
1786 g_object_unref (chooser_entry->file_system);
1788 chooser_entry->file_system = g_object_ref (file_system);
1793 * _gtk_file_chooser_entry_set_base_folder:
1794 * @chooser_entry: a #GtkFileChooserEntry
1795 * @file: file for a folder in the chooser entries current file system.
1797 * Sets the folder with respect to which completions occur.
1800 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1803 if (chooser_entry->base_folder)
1804 g_object_unref (chooser_entry->base_folder);
1806 chooser_entry->base_folder = file;
1808 if (chooser_entry->base_folder)
1809 g_object_ref (chooser_entry->base_folder);
1811 clear_completions (chooser_entry);
1815 * _gtk_file_chooser_entry_get_current_folder:
1816 * @chooser_entry: a #GtkFileChooserEntry
1818 * Gets the current folder for the #GtkFileChooserEntry. If the
1819 * user has only entered a filename, this will be in the base folder
1820 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1821 * user has entered a relative or absolute path, then it will
1822 * be different. If the user has entered unparsable text, or text which
1823 * the entry cannot handle, this will return %NULL.
1825 * Return value: the file for the current folder - this value is owned by the
1826 * chooser entry and must not be modified or freed.
1829 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1831 commit_completion_and_refresh (chooser_entry);
1832 return chooser_entry->current_folder_file;
1836 * _gtk_file_chooser_entry_get_file_part:
1837 * @chooser_entry: a #GtkFileChooserEntry
1839 * Gets the non-folder portion of whatever the user has entered
1840 * into the file selector. What is returned is a UTF-8 string,
1841 * and if a filename path is needed, g_file_get_child_for_display_name()
1844 * Return value: the entered filename - this value is owned by the
1845 * chooser entry and must not be modified or freed.
1848 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1850 commit_completion_and_refresh (chooser_entry);
1851 return chooser_entry->file_part;
1855 * _gtk_file_chooser_entry_set_file_part:
1856 * @chooser_entry: a #GtkFileChooserEntry
1857 * @file_part: text to display in the entry, in UTF-8
1859 * Sets the current text shown in the file chooser entry.
1862 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1863 const gchar *file_part)
1865 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1867 chooser_entry->in_change = TRUE;
1868 clear_completions (chooser_entry);
1869 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1870 chooser_entry->in_change = FALSE;
1875 * _gtk_file_chooser_entry_set_action:
1876 * @chooser_entry: a #GtkFileChooserEntry
1877 * @action: the action which is performed by the file selector using this entry
1879 * Sets action which is performed by the file selector using this entry.
1880 * The #GtkFileChooserEntry will use different completion strategies for
1881 * different actions.
1884 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1885 GtkFileChooserAction action)
1887 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1889 if (chooser_entry->action != action)
1891 GtkEntryCompletion *comp;
1893 chooser_entry->action = action;
1895 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1897 /* FIXME: do we need to actually set the following? */
1901 case GTK_FILE_CHOOSER_ACTION_OPEN:
1902 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1903 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1905 case GTK_FILE_CHOOSER_ACTION_SAVE:
1906 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1907 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1915 * _gtk_file_chooser_entry_get_action:
1916 * @chooser_entry: a #GtkFileChooserEntry
1918 * Gets the action for this entry.
1920 * Returns: the action
1922 GtkFileChooserAction
1923 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1925 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1926 GTK_FILE_CHOOSER_ACTION_OPEN);
1928 return chooser_entry->action;
1932 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1935 gboolean retval = FALSE;
1937 if (chooser_entry->current_folder)
1939 GFileInfo *file_info;
1941 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1945 retval = _gtk_file_info_consider_as_directory (file_info);
1946 g_object_unref (file_info);
1955 * _gtk_file_chooser_entry_select_filename:
1956 * @chooser_entry: a #GtkFileChooserEntry
1958 * Selects the filename (without the extension) for user edition.
1961 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1963 const gchar *str, *ext;
1966 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1968 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1969 ext = g_strrstr (str, ".");
1972 len = g_utf8_pointer_to_offset (str, ext);
1975 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1979 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1980 gboolean local_only)
1982 chooser_entry->local_only = local_only;
1983 clear_completions (chooser_entry);
1987 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1989 return chooser_entry->local_only;