1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserentry.c: Entry with filename completion
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include "gtkalignment.h"
25 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
28 #include "gtkfilechooserentry.h"
31 #include "gtkwindow.h"
35 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
37 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
38 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
39 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
41 struct _GtkFileChooserEntryClass
43 GtkEntryClass parent_class;
46 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
48 LOAD_COMPLETE_NOTHING,
49 LOAD_COMPLETE_AUTOCOMPLETE,
50 LOAD_COMPLETE_EXPLICIT_COMPLETION
56 REFRESH_INVALID_INPUT,
57 REFRESH_INCOMPLETE_HOSTNAME,
62 struct _GtkFileChooserEntry
64 GtkEntry parent_instance;
66 GtkFileChooserAction action;
68 GtkFileSystem *file_system;
70 GFile *current_folder_file;
74 /* Folder being loaded or already loaded */
75 GtkFolder *current_folder;
76 GCancellable *load_folder_cancellable;
78 LoadCompleteAction load_complete_action;
80 GtkListStore *completion_store;
82 guint start_autocompletion_idle_id;
84 GtkWidget *completion_feedback_window;
85 GtkWidget *completion_feedback_label;
86 guint completion_feedback_timeout_id;
88 guint has_completion : 1;
101 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
103 static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface);
105 static void gtk_file_chooser_entry_finalize (GObject *object);
106 static void gtk_file_chooser_entry_dispose (GObject *object);
107 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
108 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
109 static gboolean gtk_file_chooser_entry_focus (GtkWidget *widget,
110 GtkDirectionType direction);
111 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
112 GdkEventFocus *event);
113 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
114 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
115 const gchar *new_text,
116 gint new_text_length,
118 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
121 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
123 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
128 static gint insert_text_callback (GtkFileChooserEntry *widget,
129 const gchar *new_text,
130 gint new_text_length,
133 static void delete_text_callback (GtkFileChooserEntry *widget,
139 static gboolean match_selected_callback (GtkEntryCompletion *completion,
142 GtkFileChooserEntry *chooser_entry);
143 static gboolean completion_match_func (GtkEntryCompletion *comp,
147 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
149 gchar *display_name);
152 REFRESH_UP_TO_CURSOR_POSITION,
156 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
157 RefreshMode refresh_mode);
158 static void finished_loading_cb (GtkFolder *folder,
160 static void autocomplete (GtkFileChooserEntry *chooser_entry);
161 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
162 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
163 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
164 const gchar *feedback);
166 static GtkEditableClass *parent_editable_iface;
168 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
169 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
170 gtk_file_chooser_entry_iface_init))
173 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
175 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
176 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
177 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
179 gobject_class->finalize = gtk_file_chooser_entry_finalize;
180 gobject_class->dispose = gtk_file_chooser_entry_dispose;
182 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
183 widget_class->unmap = gtk_file_chooser_entry_unmap;
184 widget_class->focus = gtk_file_chooser_entry_focus;
185 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
187 entry_class->activate = gtk_file_chooser_entry_activate;
191 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
193 parent_editable_iface = g_type_interface_peek_parent (iface);
195 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
196 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
197 iface->set_position = gtk_file_chooser_entry_set_position;
198 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
202 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
204 GtkEntryCompletion *comp;
205 GtkCellRenderer *cell;
207 chooser_entry->local_only = TRUE;
209 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
211 comp = gtk_entry_completion_new ();
212 gtk_entry_completion_set_popup_single_match (comp, FALSE);
214 gtk_entry_completion_set_match_func (comp,
215 completion_match_func,
219 cell = gtk_cell_renderer_text_new ();
220 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
222 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
226 g_signal_connect (comp, "match-selected",
227 G_CALLBACK (match_selected_callback), chooser_entry);
229 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
230 g_object_unref (comp);
233 g_signal_connect (chooser_entry, "insert-text",
234 G_CALLBACK (insert_text_callback), NULL);
235 g_signal_connect (chooser_entry, "delete-text",
236 G_CALLBACK (delete_text_callback), NULL);
241 gtk_file_chooser_entry_finalize (GObject *object)
243 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
245 if (chooser_entry->base_folder)
246 g_object_unref (chooser_entry->base_folder);
248 if (chooser_entry->current_folder)
249 g_object_unref (chooser_entry->current_folder);
251 if (chooser_entry->current_folder_file)
252 g_object_unref (chooser_entry->current_folder_file);
254 g_free (chooser_entry->file_part);
256 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
260 discard_current_folder (GtkFileChooserEntry *chooser_entry)
262 if (chooser_entry->current_folder)
264 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
265 G_CALLBACK (finished_loading_cb), chooser_entry);
266 g_object_unref (chooser_entry->current_folder);
267 chooser_entry->current_folder = NULL;
272 gtk_file_chooser_entry_dispose (GObject *object)
274 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
276 remove_completion_feedback (chooser_entry);
277 discard_current_folder (chooser_entry);
279 if (chooser_entry->start_autocompletion_idle_id != 0)
281 g_source_remove (chooser_entry->start_autocompletion_idle_id);
282 chooser_entry->start_autocompletion_idle_id = 0;
285 if (chooser_entry->completion_store)
287 g_object_unref (chooser_entry->completion_store);
288 chooser_entry->completion_store = NULL;
291 if (chooser_entry->load_folder_cancellable)
293 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
294 chooser_entry->load_folder_cancellable = NULL;
297 if (chooser_entry->file_system)
299 g_object_unref (chooser_entry->file_system);
300 chooser_entry->file_system = NULL;
303 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
306 /* Match functions for the GtkEntryCompletion */
308 match_selected_callback (GtkEntryCompletion *completion,
311 GtkFileChooserEntry *chooser_entry)
317 gtk_tree_model_get (model, iter,
318 DISPLAY_NAME_COLUMN, &display_name,
322 if (!display_name || !file)
324 /* these shouldn't complain if passed NULL */
325 g_object_unref (file);
326 g_free (display_name);
330 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
332 pos = chooser_entry->file_part_pos;
334 /* We don't set in_change here as we want to update the current_folder
336 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
338 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
341 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
343 g_object_unref (file);
344 g_free (display_name);
349 /* Match function for the GtkEntryCompletion */
351 completion_match_func (GtkEntryCompletion *comp,
352 const char *key_unused,
356 GtkFileChooserEntry *chooser_entry;
359 char *norm_file_part;
362 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
364 /* We ignore the key because it is the contents of the entry. Instead, we
365 * just use our precomputed file_part.
367 if (!chooser_entry->file_part)
372 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
375 return FALSE; /* Uninitialized row, ugh */
378 /* If we have an empty file_part, then we're at the root of a directory. In
379 * that case, we want to match all non-dot files. We might want to match
380 * dot_files too if show_hidden is TRUE on the fileselector in the future.
382 /* Additionally, support for gnome .hidden files would be sweet, too */
383 if (chooser_entry->file_part[0] == '\000')
395 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
396 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
398 #ifdef G_PLATFORM_WIN32
402 temp = norm_file_part;
403 norm_file_part = g_utf8_casefold (norm_file_part, -1);
407 norm_name = g_utf8_casefold (norm_name, -1);
412 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
414 g_free (norm_file_part);
422 clear_completions (GtkFileChooserEntry *chooser_entry)
424 chooser_entry->has_completion = FALSE;
425 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
427 remove_completion_feedback (chooser_entry);
431 beep (GtkFileChooserEntry *chooser_entry)
433 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
436 /* This function will append a directory separator to paths to
437 * display_name iff the path associated with it is a directory.
438 * maybe_append_separator_to_file will g_free the display_name and
439 * return a new one if needed. Otherwise, it will return the old one.
440 * You should be safe calling
442 * display_name = maybe_append_separator_to_file (entry, file, display_name);
444 * g_free (display_name);
447 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
451 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
455 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
459 if (_gtk_file_info_consider_as_directory (info))
461 gchar *tmp = display_name;
462 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
466 g_object_unref (info);
474 trim_dir_separator_suffix (const char *str)
479 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
480 return g_strndup (str, len - 1);
482 return g_strdup (str);
485 /* Determines if the completion model has entries with a common prefix relative
486 * to the current contents of the entry. Also, if there's one and only one such
487 * path, stores it in unique_path_ret.
490 find_common_prefix (GtkFileChooserEntry *chooser_entry,
491 gchar **common_prefix_ret,
492 GFile **unique_file_ret,
493 gboolean *is_complete_not_unique_ret,
494 gboolean *prefix_expands_the_file_part_ret,
497 GtkEditable *editable;
501 char *text_up_to_cursor;
502 GFile *parsed_folder_file;
503 char *parsed_file_part;
505 *common_prefix_ret = NULL;
506 *unique_file_ret = NULL;
507 *is_complete_not_unique_ret = FALSE;
508 *prefix_expands_the_file_part_ret = FALSE;
510 editable = GTK_EDITABLE (chooser_entry);
512 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
514 parsed = _gtk_file_system_parse (chooser_entry->file_system,
515 chooser_entry->base_folder,
521 g_free (text_up_to_cursor);
526 g_assert (parsed_folder_file != NULL
527 && chooser_entry->current_folder != NULL
528 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
530 g_object_unref (parsed_folder_file);
532 /* First pass: find the common prefix */
534 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
541 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
543 DISPLAY_NAME_COLUMN, &display_name,
547 if (g_str_has_prefix (display_name, parsed_file_part))
549 if (!*common_prefix_ret)
551 *common_prefix_ret = trim_dir_separator_suffix (display_name);
552 *unique_file_ret = g_object_ref (file);
556 gchar *p = *common_prefix_ret;
557 const gchar *q = display_name;
559 while (*p && *p == *q)
567 if (*unique_file_ret)
569 g_object_unref (*unique_file_ret);
570 *unique_file_ret = NULL;
575 g_free (display_name);
576 g_object_unref (file);
577 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
580 /* Second pass: see if the prefix we found is a complete match */
582 if (*common_prefix_ret != NULL)
584 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
591 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
593 DISPLAY_NAME_COLUMN, &display_name,
595 len = strlen (display_name);
598 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
601 if (strncmp (*common_prefix_ret, display_name, len) == 0)
602 *is_complete_not_unique_ret = TRUE;
604 g_free (display_name);
605 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
608 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
610 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
613 g_free (parsed_file_part);
619 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
626 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
627 if (cursor_pos < GTK_ENTRY (chooser_entry)->text_length)
631 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
632 if (G_IS_DIR_SEPARATOR (*next_char_str))
635 g_free (next_char_str);
642 INVALID_INPUT, /* what the user typed is bogus */
643 NO_MATCH, /* no matches based on what the user typed */
644 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
645 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
646 COMPLETED, /* completion inserted (ambiguous suffix) */
647 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
648 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
649 } CommonPrefixResult;
651 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
652 static CommonPrefixResult
653 append_common_prefix (GtkFileChooserEntry *chooser_entry,
655 gboolean show_errors)
657 gchar *common_prefix;
659 gboolean is_complete_not_unique;
660 gboolean prefix_expands_the_file_part;
662 CommonPrefixResult result;
663 gboolean have_result;
665 clear_completions (chooser_entry);
667 if (chooser_entry->completion_store == NULL)
671 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
673 /* If the user types an incomplete hostname ("http://foo" without a slash
674 * after that), it's not an error. We just don't want to pop up a
675 * meaningless completion window in that state.
677 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
680 beep (chooser_entry);
681 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
684 g_error_free (error);
686 return INVALID_INPUT;
693 if (!char_after_cursor_is_directory_separator (chooser_entry))
694 common_prefix = maybe_append_separator_to_file (chooser_entry,
698 g_object_unref (unique_file);
702 if (prefix_expands_the_file_part)
703 result = COMPLETED_UNIQUE;
705 result = NOTHING_INSERTED_UNIQUE;
708 result = INVALID_INPUT;
714 if (is_complete_not_unique)
716 result = COMPLETE_BUT_NOT_UNIQUE;
724 gint common_prefix_len;
727 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
728 common_prefix_len = g_utf8_strlen (common_prefix, -1);
730 pos = chooser_entry->file_part_pos;
732 if (prefix_expands_the_file_part)
734 chooser_entry->in_change = TRUE;
735 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
737 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
740 chooser_entry->in_change = FALSE;
744 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
746 pos); /* equivalent to cursor_pos + common_prefix_len); */
747 chooser_entry->has_completion = TRUE;
750 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
752 else if (!have_result)
754 result = NOTHING_INSERTED_COMPLETE;
758 g_free (common_prefix);
775 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
776 const gchar *new_text,
777 gint new_text_length,
780 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
784 old_text_len = GTK_ENTRY (chooser_entry)->text_length;
785 insert_pos = *position;
787 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
789 if (chooser_entry->in_change)
792 remove_completion_feedback (chooser_entry);
794 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
795 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
796 && insert_pos == old_text_len)
797 install_start_autocompletion_idle (chooser_entry);
801 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
803 if (chooser_entry->in_change)
806 clear_completions (chooser_entry);
810 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
814 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
816 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
818 clear_completions_if_not_in_change (chooser_entry);
822 gtk_file_chooser_entry_set_position (GtkEditable *editable,
825 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
827 parent_editable_iface->set_position (editable, position);
829 clear_completions_if_not_in_change (chooser_entry);
833 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
837 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
839 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
841 clear_completions_if_not_in_change (chooser_entry);
845 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
847 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
848 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
852 gtk_file_chooser_entry_unmap (GtkWidget *widget)
854 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
856 remove_completion_feedback (chooser_entry);
858 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
862 completion_feedback_window_expose_event_cb (GtkWidget *widget,
863 GdkEventExpose *event,
866 /* Stolen from gtk_tooltip_paint_window() */
868 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
870 gtk_paint_flat_box (chooser_entry->completion_feedback_window->style,
871 chooser_entry->completion_feedback_window->window,
875 chooser_entry->completion_feedback_window,
878 chooser_entry->completion_feedback_window->allocation.width,
879 chooser_entry->completion_feedback_window->allocation.height);
885 set_invisible_mouse_cursor (GdkWindow *window)
890 display = gdk_drawable_get_display (window);
891 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
893 gdk_window_set_cursor (window, cursor);
895 gdk_cursor_unref (cursor);
899 completion_feedback_window_realize_cb (GtkWidget *widget,
902 /* We hide the mouse cursor inside the completion feedback window, since
903 * GtkEntry hides the cursor when the user types. We don't want the cursor to
904 * come back if the completion feedback ends up where the mouse is.
906 set_invisible_mouse_cursor (widget->window);
910 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
912 /* Stolen from gtk_tooltip_init() */
914 GtkWidget *alignment;
916 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
917 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
918 GDK_WINDOW_TYPE_HINT_TOOLTIP);
919 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
920 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
921 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
923 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
924 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
925 chooser_entry->completion_feedback_window->style->ythickness,
926 chooser_entry->completion_feedback_window->style->ythickness,
927 chooser_entry->completion_feedback_window->style->xthickness,
928 chooser_entry->completion_feedback_window->style->xthickness);
929 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
930 gtk_widget_show (alignment);
932 g_signal_connect (chooser_entry->completion_feedback_window, "expose-event",
933 G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
934 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
935 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
936 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
938 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
939 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
940 gtk_widget_show (chooser_entry->completion_feedback_label);
944 completion_feedback_timeout_cb (gpointer data)
946 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
948 chooser_entry->completion_feedback_timeout_id = 0;
950 remove_completion_feedback (chooser_entry);
955 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
957 if (chooser_entry->completion_feedback_timeout_id != 0)
958 g_source_remove (chooser_entry->completion_feedback_timeout_id);
960 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
961 completion_feedback_timeout_cb,
965 /* Gets the x position of the text cursor in the entry, in widget coordinates */
967 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
970 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
971 * the reader, you have to implement support for the entry's scroll offset and
972 * RTL layouts and all the fancy Pango stuff.
976 gint layout_x, layout_y;
978 PangoRectangle strong_pos;
980 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
982 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
984 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
985 GTK_ENTRY (chooser_entry)->current_pos);
987 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
989 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
993 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
995 /* More or less stolen from gtk_tooltip_position() */
997 GtkRequisition feedback_req;
998 gint entry_x, entry_y;
1000 GtkAllocation *entry_allocation;
1001 int feedback_x, feedback_y;
1003 gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);
1005 gdk_window_get_origin (GTK_WIDGET (chooser_entry)->window, &entry_x, &entry_y);
1006 entry_allocation = &(GTK_WIDGET (chooser_entry)->allocation);
1008 get_entry_cursor_x (chooser_entry, &cursor_x);
1010 /* FIXME: fit to the screen if we bump on the screen's edge */
1011 feedback_x = entry_x + cursor_x + entry_allocation->height / 2; /* cheap "half M-width" */
1012 feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;
1014 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1015 gtk_widget_show (chooser_entry->completion_feedback_window);
1017 install_completion_feedback_timer (chooser_entry);
1021 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1022 const gchar *feedback)
1024 if (chooser_entry->completion_feedback_window == NULL)
1025 create_completion_feedback_window (chooser_entry);
1027 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1029 show_completion_feedback_window (chooser_entry);
1033 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1035 if (chooser_entry->completion_feedback_window)
1036 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1038 chooser_entry->completion_feedback_window = NULL;
1039 chooser_entry->completion_feedback_label = NULL;
1041 if (chooser_entry->completion_feedback_timeout_id != 0)
1043 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1044 chooser_entry->completion_feedback_timeout_id = 0;
1049 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1051 CommonPrefixResult result;
1053 g_assert (chooser_entry->current_folder != NULL);
1054 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1056 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1058 * - If there is a common prefix, insert it (done)
1059 * - If there is no common prefix, pop up the suggestion window
1060 * - If there are no matches at all, beep and bring up a tooltip (done)
1061 * - If the suggestion window is already up, scroll it
1063 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1068 /* We already beeped in append_common_prefix(); do nothing here */
1072 beep (chooser_entry);
1073 /* translators: this text is shown when there are no completions
1074 * for something the user typed in a file chooser entry
1076 pop_up_completion_feedback (chooser_entry, _("No match"));
1079 case NOTHING_INSERTED_COMPLETE:
1080 /* FIXME: pop up the suggestion window or scroll it */
1083 case NOTHING_INSERTED_UNIQUE:
1084 /* translators: this text is shown when there is exactly one completion
1085 * for something the user typed in a file chooser entry
1087 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1094 case COMPLETED_UNIQUE:
1098 case COMPLETE_BUT_NOT_UNIQUE:
1099 /* translators: this text is shown when the text in a file chooser
1100 * entry is a complete filename, but could be continued to find
1103 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1107 g_assert_not_reached ();
1112 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1114 RefreshStatus status;
1118 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1125 g_assert (chooser_entry->current_folder_file != NULL);
1127 if (chooser_entry->current_folder
1128 && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1130 explicitly_complete (chooser_entry);
1134 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1136 /* translators: this text is shown while the system is searching
1137 * for possible completions for text in a file chooser entry
1139 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1144 case REFRESH_INVALID_INPUT:
1146 feedback_msg = _("Invalid path");
1149 case REFRESH_INCOMPLETE_HOSTNAME:
1152 if (chooser_entry->local_only)
1153 feedback_msg = _("Only local files may be selected"); /* hostnames in a local_only file chooser? user error */
1156 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1157 feedback_msg = _("Incomplete hostname; end it with '/'");
1162 case REFRESH_NONEXISTENT:
1164 feedback_msg = _("Path does not exist");
1167 case REFRESH_NOT_LOCAL:
1169 feedback_msg = _("Only local files may be selected");
1173 g_assert_not_reached ();
1179 g_assert (chooser_entry->current_folder_file == NULL);
1181 beep (chooser_entry);
1182 pop_up_completion_feedback (chooser_entry, feedback_msg);
1183 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1188 gtk_file_chooser_entry_focus (GtkWidget *widget,
1189 GtkDirectionType direction)
1191 GtkFileChooserEntry *chooser_entry;
1192 GtkEditable *editable;
1194 GdkModifierType state;
1195 gboolean control_pressed;
1197 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1198 editable = GTK_EDITABLE (widget);
1199 entry = GTK_ENTRY (widget);
1201 if (!chooser_entry->eat_tabs)
1202 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1204 control_pressed = FALSE;
1206 if (gtk_get_current_event_state (&state))
1208 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1209 control_pressed = TRUE;
1212 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1213 * makes it 'safe' for people to hit. */
1214 if ((direction == GTK_DIR_TAB_FORWARD) &&
1215 (GTK_WIDGET_HAS_FOCUS (widget)) &&
1216 (! control_pressed))
1218 if (chooser_entry->has_completion)
1219 gtk_editable_set_position (editable, entry->text_length);
1221 start_explicit_completion (chooser_entry);
1226 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1230 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1231 GdkEventFocus *event)
1233 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1235 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1237 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1241 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1243 if (chooser_entry->has_completion)
1245 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1246 GTK_ENTRY (chooser_entry)->text_length);
1249 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1250 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1254 gtk_file_chooser_entry_activate (GtkEntry *entry)
1256 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1258 commit_completion_and_refresh (chooser_entry);
1259 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1263 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1265 if (!chooser_entry->completion_store)
1268 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1269 g_object_unref (chooser_entry->completion_store);
1270 chooser_entry->completion_store = NULL;
1273 /* Fills the completion store from the contents of the current folder */
1275 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1280 discard_completion_store (chooser_entry);
1282 files = _gtk_folder_list_children (chooser_entry->current_folder);
1284 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1288 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1293 file = tmp_list->data;
1295 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1299 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1302 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
1304 gtk_list_store_append (chooser_entry->completion_store, &iter);
1305 gtk_list_store_set (chooser_entry->completion_store, &iter,
1306 DISPLAY_NAME_COLUMN, display_name,
1310 g_object_unref (info);
1311 g_free (display_name);
1315 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1316 g_slist_free (files);
1318 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1319 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1321 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1322 GTK_TREE_MODEL (chooser_entry->completion_store));
1325 /* When we finish loading the current folder, this function should get called to
1326 * perform the deferred autocompletion or explicit completion.
1329 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1331 switch (chooser_entry->load_complete_action)
1333 case LOAD_COMPLETE_NOTHING:
1336 case LOAD_COMPLETE_AUTOCOMPLETE:
1337 autocomplete (chooser_entry);
1340 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1341 explicitly_complete (chooser_entry);
1345 g_assert_not_reached ();
1348 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1352 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1354 populate_completion_store (chooser_entry);
1355 perform_load_complete_action (chooser_entry);
1357 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1360 /* Callback when the current folder finishes loading */
1362 finished_loading_cb (GtkFolder *folder,
1365 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1367 finish_folder_load (chooser_entry);
1370 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1372 load_directory_get_folder_callback (GCancellable *cancellable,
1374 const GError *error,
1377 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1378 GtkFileChooserEntry *chooser_entry = data;
1380 if (cancellable != chooser_entry->load_folder_cancellable)
1383 chooser_entry->load_folder_cancellable = NULL;
1387 LoadCompleteAction old_load_complete_action;
1389 old_load_complete_action = chooser_entry->load_complete_action;
1391 discard_completion_store (chooser_entry);
1392 clear_completions (chooser_entry);
1394 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1396 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1398 beep (chooser_entry);
1399 pop_up_completion_feedback (chooser_entry, error->message);
1402 discard_current_folder (chooser_entry);
1405 if (cancelled || error)
1408 g_assert (folder != NULL);
1409 chooser_entry->current_folder = g_object_ref (folder);
1411 discard_completion_store (chooser_entry);
1413 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1414 finish_folder_load (chooser_entry);
1416 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1417 G_CALLBACK (finished_loading_cb), chooser_entry);
1420 g_object_unref (chooser_entry);
1421 g_object_unref (cancellable);
1425 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1427 if (chooser_entry->current_folder_file == NULL ||
1428 chooser_entry->file_system == NULL)
1431 if (chooser_entry->local_only
1432 && !g_file_is_native (chooser_entry->current_folder_file))
1435 g_assert (chooser_entry->current_folder == NULL);
1436 g_assert (chooser_entry->load_folder_cancellable == NULL);
1438 chooser_entry->load_folder_cancellable =
1439 _gtk_file_system_get_folder (chooser_entry->file_system,
1440 chooser_entry->current_folder_file,
1441 "standard::name,standard::display-name,standard::type",
1442 load_directory_get_folder_callback,
1443 g_object_ref (chooser_entry));
1447 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1449 gboolean force_reload)
1451 gboolean reload = FALSE;
1453 if (chooser_entry->current_folder_file)
1455 if ((folder_file && !(g_file_equal (folder_file, chooser_entry->current_folder_file)
1456 && chooser_entry->load_folder_cancellable))
1461 /* We changed our current directory. We need to clear out the old
1462 * directory information.
1464 if (chooser_entry->load_folder_cancellable)
1466 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
1467 chooser_entry->load_folder_cancellable = NULL;
1470 discard_current_folder (chooser_entry);
1471 g_object_unref (chooser_entry->current_folder_file);
1472 chooser_entry->current_folder_file = (folder_file) ? g_object_ref (folder_file) : NULL;
1477 chooser_entry->current_folder_file = (folder_file) ? g_object_ref (folder_file) : NULL;
1482 start_loading_current_folder (chooser_entry);
1485 static RefreshStatus
1486 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1487 RefreshMode refresh_mode)
1489 GtkEditable *editable;
1494 gsize total_len, file_part_len;
1497 RefreshStatus result;
1499 editable = GTK_EDITABLE (chooser_entry);
1501 switch (refresh_mode)
1503 case REFRESH_UP_TO_CURSOR_POSITION:
1504 end_pos = gtk_editable_get_position (editable);
1507 case REFRESH_WHOLE_TEXT:
1508 end_pos = GTK_ENTRY (chooser_entry)->text_length;
1512 g_assert_not_reached ();
1513 return REFRESH_INVALID_INPUT;
1516 text = gtk_editable_get_chars (editable, 0, end_pos);
1519 if (!chooser_entry->file_system ||
1520 !chooser_entry->base_folder ||
1521 !_gtk_file_system_parse (chooser_entry->file_system,
1522 chooser_entry->base_folder, text,
1523 &folder_file, &file_part, &error))
1525 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1528 result = REFRESH_INCOMPLETE_HOSTNAME;
1532 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1534 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1535 result = REFRESH_NONEXISTENT;
1537 result = REFRESH_INVALID_INPUT;
1541 g_error_free (error);
1543 file_part = g_strdup ("");
1548 file_part_len = strlen (file_part);
1549 total_len = strlen (text);
1550 if (total_len > file_part_len)
1551 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1555 result = REFRESH_OK;
1560 g_free (chooser_entry->file_part);
1562 chooser_entry->file_part = file_part;
1563 chooser_entry->file_part_pos = file_part_pos;
1565 /* FMQ: this needs to return an error if the folder is not local */
1566 reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1569 g_object_unref (folder_file);
1575 autocomplete (GtkFileChooserEntry *chooser_entry)
1577 g_assert (chooser_entry->current_folder != NULL);
1578 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1579 g_assert (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length);
1581 append_common_prefix (chooser_entry, TRUE, FALSE);
1585 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1587 /* FMQ: get result from the function below */
1588 refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1590 if (!chooser_entry->current_folder)
1592 /* We don't beep or anything, since this is autocompletion - the user
1593 * didn't request any action explicitly.
1598 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1599 autocomplete (chooser_entry);
1601 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1605 start_autocompletion_idle_handler (gpointer data)
1607 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1609 start_autocompletion (chooser_entry);
1611 chooser_entry->start_autocompletion_idle_id = 0;
1617 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1619 if (chooser_entry->start_autocompletion_idle_id != 0)
1622 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1627 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1628 const gchar *new_text,
1629 gint new_text_length,
1633 const gchar *colon = memchr (new_text, ':', new_text_length);
1636 /* Disallow these characters altogether */
1637 for (i = 0; i < new_text_length; i++)
1639 if (new_text[i] == '<' ||
1640 new_text[i] == '>' ||
1641 new_text[i] == '"' ||
1642 new_text[i] == '|' ||
1643 new_text[i] == '*' ||
1648 if (i < new_text_length ||
1649 /* Disallow entering text that would cause a colon to be anywhere except
1650 * after a drive letter.
1653 *position + (colon - new_text) != 1) ||
1654 (new_text_length > 0 &&
1656 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1657 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1659 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1660 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1668 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1673 /* If deleting a drive letter, delete the colon, too */
1674 if (start_pos == 0 && end_pos == 1 &&
1675 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1676 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1678 g_signal_handlers_block_by_func (chooser_entry,
1679 G_CALLBACK (delete_text_callback),
1681 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1682 g_signal_handlers_unblock_by_func (chooser_entry,
1683 G_CALLBACK (delete_text_callback),
1690 * _gtk_file_chooser_entry_new:
1691 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1693 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1694 * is an internal implementation widget for the GTK+ file chooser
1695 * which is an entry with completion with respect to a
1696 * #GtkFileSystem object.
1698 * Return value: the newly created #GtkFileChooserEntry
1701 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1703 GtkFileChooserEntry *chooser_entry;
1705 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1706 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1708 return GTK_WIDGET (chooser_entry);
1712 * _gtk_file_chooser_entry_set_file_system:
1713 * @chooser_entry: a #GtkFileChooser
1714 * @file_system: an object implementing #GtkFileSystem
1716 * Sets the file system for @chooser_entry.
1719 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1720 GtkFileSystem *file_system)
1722 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1723 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1725 if (file_system != chooser_entry->file_system)
1727 if (chooser_entry->file_system)
1728 g_object_unref (chooser_entry->file_system);
1730 chooser_entry->file_system = g_object_ref (file_system);
1735 * _gtk_file_chooser_entry_set_base_folder:
1736 * @chooser_entry: a #GtkFileChooserEntry
1737 * @file: file for a folder in the chooser entries current file system.
1739 * Sets the folder with respect to which completions occur.
1742 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1745 if (chooser_entry->base_folder)
1746 g_object_unref (chooser_entry->base_folder);
1748 chooser_entry->base_folder = file;
1750 if (chooser_entry->base_folder)
1751 g_object_ref (chooser_entry->base_folder);
1753 clear_completions (chooser_entry);
1754 _gtk_file_chooser_entry_select_filename (chooser_entry);
1758 * _gtk_file_chooser_entry_get_current_folder:
1759 * @chooser_entry: a #GtkFileChooserEntry
1761 * Gets the current folder for the #GtkFileChooserEntry. If the
1762 * user has only entered a filename, this will be in the base folder
1763 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1764 * user has entered a relative or absolute path, then it will
1765 * be different. If the user has entered unparsable text, or text which
1766 * the entry cannot handle, this will return %NULL.
1768 * Return value: the file for the current folder - this value is owned by the
1769 * chooser entry and must not be modified or freed.
1772 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1774 commit_completion_and_refresh (chooser_entry);
1775 return chooser_entry->current_folder_file;
1779 * _gtk_file_chooser_entry_get_file_part:
1780 * @chooser_entry: a #GtkFileChooserEntry
1782 * Gets the non-folder portion of whatever the user has entered
1783 * into the file selector. What is returned is a UTF-8 string,
1784 * and if a filename path is needed, g_file_get_child_for_display_name()
1787 * Return value: the entered filename - this value is owned by the
1788 * chooser entry and must not be modified or freed.
1791 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1793 commit_completion_and_refresh (chooser_entry);
1794 return chooser_entry->file_part;
1798 * _gtk_file_chooser_entry_set_file_part:
1799 * @chooser_entry: a #GtkFileChooserEntry
1800 * @file_part: text to display in the entry, in UTF-8
1802 * Sets the current text shown in the file chooser entry.
1805 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1806 const gchar *file_part)
1808 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1810 chooser_entry->in_change = TRUE;
1811 clear_completions (chooser_entry);
1812 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1813 chooser_entry->in_change = FALSE;
1818 * _gtk_file_chooser_entry_set_action:
1819 * @chooser_entry: a #GtkFileChooserEntry
1820 * @action: the action which is performed by the file selector using this entry
1822 * Sets action which is performed by the file selector using this entry.
1823 * The #GtkFileChooserEntry will use different completion strategies for
1824 * different actions.
1827 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1828 GtkFileChooserAction action)
1830 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1832 if (chooser_entry->action != action)
1834 GtkEntryCompletion *comp;
1836 chooser_entry->action = action;
1838 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1840 /* FIXME: do we need to actually set the following? */
1844 case GTK_FILE_CHOOSER_ACTION_OPEN:
1845 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1846 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1848 case GTK_FILE_CHOOSER_ACTION_SAVE:
1849 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1850 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1858 * _gtk_file_chooser_entry_get_action:
1859 * @chooser_entry: a #GtkFileChooserEntry
1861 * Gets the action for this entry.
1863 * Returns: the action
1865 GtkFileChooserAction
1866 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1868 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1869 GTK_FILE_CHOOSER_ACTION_OPEN);
1871 return chooser_entry->action;
1875 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1878 gboolean retval = FALSE;
1880 if (chooser_entry->current_folder)
1882 GFileInfo *file_info;
1884 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1888 retval = _gtk_file_info_consider_as_directory (file_info);
1889 g_object_unref (file_info);
1898 * _gtk_file_chooser_entry_select_filename:
1899 * @chooser_entry: a #GtkFileChooserEntry
1901 * Selects the filename (without the extension) for user edition.
1904 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1906 const gchar *str, *ext;
1909 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1911 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1912 ext = g_strrstr (str, ".");
1915 len = g_utf8_pointer_to_offset (str, ext);
1918 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1922 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1923 gboolean local_only)
1925 chooser_entry->local_only = local_only;
1926 clear_completions (chooser_entry);
1930 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1932 return chooser_entry->local_only;