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
53 struct _GtkFileChooserEntry
55 GtkEntry parent_instance;
57 GtkFileChooserAction action;
59 GtkFileSystem *file_system;
61 GFile *current_folder_file;
65 /* Folder being loaded or already loaded */
66 GtkFolder *current_folder;
67 GCancellable *load_folder_cancellable;
69 LoadCompleteAction load_complete_action;
71 GtkListStore *completion_store;
73 guint start_autocompletion_idle_id;
75 GtkWidget *completion_feedback_window;
76 GtkWidget *completion_feedback_label;
77 guint completion_feedback_timeout_id;
79 guint has_completion : 1;
92 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
94 static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface);
96 static void gtk_file_chooser_entry_finalize (GObject *object);
97 static void gtk_file_chooser_entry_dispose (GObject *object);
98 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
99 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
100 static gboolean gtk_file_chooser_entry_focus (GtkWidget *widget,
101 GtkDirectionType direction);
102 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
103 GdkEventFocus *event);
104 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
105 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
106 const gchar *new_text,
107 gint new_text_length,
109 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
112 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
114 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
119 static gint insert_text_callback (GtkFileChooserEntry *widget,
120 const gchar *new_text,
121 gint new_text_length,
124 static void delete_text_callback (GtkFileChooserEntry *widget,
130 static gboolean match_selected_callback (GtkEntryCompletion *completion,
133 GtkFileChooserEntry *chooser_entry);
134 static gboolean completion_match_func (GtkEntryCompletion *comp,
138 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
140 gchar *display_name);
143 REFRESH_UP_TO_CURSOR_POSITION,
147 static void refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
148 RefreshMode refresh_mode);
149 static void finished_loading_cb (GtkFolder *folder,
151 static void autocomplete (GtkFileChooserEntry *chooser_entry);
152 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
153 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
154 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
155 const gchar *feedback);
157 static GtkEditableClass *parent_editable_iface;
159 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
160 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
161 gtk_file_chooser_entry_iface_init))
164 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
166 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
167 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
168 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
170 gobject_class->finalize = gtk_file_chooser_entry_finalize;
171 gobject_class->dispose = gtk_file_chooser_entry_dispose;
173 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
174 widget_class->unmap = gtk_file_chooser_entry_unmap;
175 widget_class->focus = gtk_file_chooser_entry_focus;
176 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
178 entry_class->activate = gtk_file_chooser_entry_activate;
182 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
184 parent_editable_iface = g_type_interface_peek_parent (iface);
186 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
187 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
188 iface->set_position = gtk_file_chooser_entry_set_position;
189 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
193 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
195 GtkEntryCompletion *comp;
196 GtkCellRenderer *cell;
198 chooser_entry->local_only = TRUE;
200 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
202 comp = gtk_entry_completion_new ();
203 gtk_entry_completion_set_popup_single_match (comp, FALSE);
205 gtk_entry_completion_set_match_func (comp,
206 completion_match_func,
210 cell = gtk_cell_renderer_text_new ();
211 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
213 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
217 g_signal_connect (comp, "match-selected",
218 G_CALLBACK (match_selected_callback), chooser_entry);
220 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
221 g_object_unref (comp);
224 g_signal_connect (chooser_entry, "insert-text",
225 G_CALLBACK (insert_text_callback), NULL);
226 g_signal_connect (chooser_entry, "delete-text",
227 G_CALLBACK (delete_text_callback), NULL);
232 gtk_file_chooser_entry_finalize (GObject *object)
234 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
236 if (chooser_entry->base_folder)
237 g_object_unref (chooser_entry->base_folder);
239 if (chooser_entry->current_folder)
240 g_object_unref (chooser_entry->current_folder);
242 if (chooser_entry->current_folder_file)
243 g_object_unref (chooser_entry->current_folder_file);
245 g_free (chooser_entry->file_part);
247 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
251 discard_current_folder (GtkFileChooserEntry *chooser_entry)
253 if (chooser_entry->current_folder)
255 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
256 G_CALLBACK (finished_loading_cb), chooser_entry);
257 g_object_unref (chooser_entry->current_folder);
258 chooser_entry->current_folder = NULL;
263 gtk_file_chooser_entry_dispose (GObject *object)
265 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
267 remove_completion_feedback (chooser_entry);
268 discard_current_folder (chooser_entry);
270 if (chooser_entry->start_autocompletion_idle_id != 0)
272 g_source_remove (chooser_entry->start_autocompletion_idle_id);
273 chooser_entry->start_autocompletion_idle_id = 0;
276 if (chooser_entry->completion_store)
278 g_object_unref (chooser_entry->completion_store);
279 chooser_entry->completion_store = NULL;
282 if (chooser_entry->load_folder_cancellable)
284 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
285 chooser_entry->load_folder_cancellable = NULL;
288 if (chooser_entry->file_system)
290 g_object_unref (chooser_entry->file_system);
291 chooser_entry->file_system = NULL;
294 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
297 /* Match functions for the GtkEntryCompletion */
299 match_selected_callback (GtkEntryCompletion *completion,
302 GtkFileChooserEntry *chooser_entry)
308 gtk_tree_model_get (model, iter,
309 DISPLAY_NAME_COLUMN, &display_name,
313 if (!display_name || !file)
315 /* these shouldn't complain if passed NULL */
316 g_object_unref (file);
317 g_free (display_name);
321 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
323 pos = chooser_entry->file_part_pos;
325 /* We don't set in_change here as we want to update the current_folder
327 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
329 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
332 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
334 g_object_unref (file);
335 g_free (display_name);
340 /* Match function for the GtkEntryCompletion */
342 completion_match_func (GtkEntryCompletion *comp,
343 const char *key_unused,
347 GtkFileChooserEntry *chooser_entry;
350 char *norm_file_part;
353 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
355 /* We ignore the key because it is the contents of the entry. Instead, we
356 * just use our precomputed file_part.
358 if (!chooser_entry->file_part)
363 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
366 return FALSE; /* Uninitialized row, ugh */
369 /* If we have an empty file_part, then we're at the root of a directory. In
370 * that case, we want to match all non-dot files. We might want to match
371 * dot_files too if show_hidden is TRUE on the fileselector in the future.
373 /* Additionally, support for gnome .hidden files would be sweet, too */
374 if (chooser_entry->file_part[0] == '\000')
386 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
387 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
389 #ifdef G_PLATFORM_WIN32
393 temp = norm_file_part;
394 norm_file_part = g_utf8_casefold (norm_file_part, -1);
398 norm_name = g_utf8_casefold (norm_name, -1);
403 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
405 g_free (norm_file_part);
413 clear_completions (GtkFileChooserEntry *chooser_entry)
415 chooser_entry->has_completion = FALSE;
416 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
418 remove_completion_feedback (chooser_entry);
422 beep (GtkFileChooserEntry *chooser_entry)
424 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
427 /* This function will append a directory separator to paths to
428 * display_name iff the path associated with it is a directory.
429 * maybe_append_separator_to_file will g_free the display_name and
430 * return a new one if needed. Otherwise, it will return the old one.
431 * You should be safe calling
433 * display_name = maybe_append_separator_to_file (entry, file, display_name);
435 * g_free (display_name);
438 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
442 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
446 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
450 if (_gtk_file_info_consider_as_directory (info))
452 gchar *tmp = display_name;
453 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
457 g_object_unref (info);
465 trim_dir_separator_suffix (const char *str)
470 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
471 return g_strndup (str, len - 1);
473 return g_strdup (str);
476 /* Determines if the completion model has entries with a common prefix relative
477 * to the current contents of the entry. Also, if there's one and only one such
478 * path, stores it in unique_path_ret.
481 find_common_prefix (GtkFileChooserEntry *chooser_entry,
482 gchar **common_prefix_ret,
483 GFile **unique_file_ret,
484 gboolean *is_complete_not_unique_ret,
485 gboolean *prefix_expands_the_file_part_ret,
488 GtkEditable *editable;
492 char *text_up_to_cursor;
493 GFile *parsed_folder_file;
494 char *parsed_file_part;
496 *common_prefix_ret = NULL;
497 *unique_file_ret = NULL;
498 *is_complete_not_unique_ret = FALSE;
499 *prefix_expands_the_file_part_ret = FALSE;
501 editable = GTK_EDITABLE (chooser_entry);
503 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
505 parsed = _gtk_file_system_parse (chooser_entry->file_system,
506 chooser_entry->base_folder,
512 g_free (text_up_to_cursor);
517 g_assert (parsed_folder_file != NULL
518 && chooser_entry->current_folder != NULL
519 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
521 g_object_unref (parsed_folder_file);
523 /* First pass: find the common prefix */
525 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
532 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
534 DISPLAY_NAME_COLUMN, &display_name,
538 if (g_str_has_prefix (display_name, parsed_file_part))
540 if (!*common_prefix_ret)
542 *common_prefix_ret = trim_dir_separator_suffix (display_name);
543 *unique_file_ret = g_object_ref (file);
547 gchar *p = *common_prefix_ret;
548 const gchar *q = display_name;
550 while (*p && *p == *q)
558 if (*unique_file_ret)
560 g_object_unref (*unique_file_ret);
561 *unique_file_ret = NULL;
566 g_free (display_name);
567 g_object_unref (file);
568 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
571 /* Second pass: see if the prefix we found is a complete match */
573 if (*common_prefix_ret != NULL)
575 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
582 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
584 DISPLAY_NAME_COLUMN, &display_name,
586 len = strlen (display_name);
589 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
592 if (strncmp (*common_prefix_ret, display_name, len) == 0)
593 *is_complete_not_unique_ret = TRUE;
595 g_free (display_name);
596 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
599 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
601 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
604 g_free (parsed_file_part);
610 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
617 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
618 if (cursor_pos < GTK_ENTRY (chooser_entry)->text_length)
622 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
623 if (G_IS_DIR_SEPARATOR (*next_char_str))
626 g_free (next_char_str);
633 INVALID_INPUT, /* what the user typed is bogus */
634 NO_MATCH, /* no matches based on what the user typed */
635 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
636 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
637 COMPLETED, /* completion inserted (ambiguous suffix) */
638 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
639 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
640 } CommonPrefixResult;
642 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
643 static CommonPrefixResult
644 append_common_prefix (GtkFileChooserEntry *chooser_entry,
646 gboolean show_errors)
648 gchar *common_prefix;
650 gboolean is_complete_not_unique;
651 gboolean prefix_expands_the_file_part;
653 CommonPrefixResult result;
654 gboolean have_result;
656 clear_completions (chooser_entry);
658 if (chooser_entry->completion_store == NULL)
662 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
664 /* If the user types an incomplete hostname ("http://foo" without a slash
665 * after that), it's not an error. We just don't want to pop up a
666 * meaningless completion window in that state.
668 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
671 beep (chooser_entry);
672 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
675 g_error_free (error);
677 return INVALID_INPUT;
684 if (!char_after_cursor_is_directory_separator (chooser_entry))
685 common_prefix = maybe_append_separator_to_file (chooser_entry,
689 g_object_unref (unique_file);
693 if (prefix_expands_the_file_part)
694 result = COMPLETED_UNIQUE;
696 result = NOTHING_INSERTED_UNIQUE;
699 result = INVALID_INPUT;
705 if (is_complete_not_unique)
707 result = COMPLETE_BUT_NOT_UNIQUE;
715 gint common_prefix_len;
718 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
719 common_prefix_len = g_utf8_strlen (common_prefix, -1);
721 pos = chooser_entry->file_part_pos;
723 if (prefix_expands_the_file_part)
725 chooser_entry->in_change = TRUE;
726 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
728 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
731 chooser_entry->in_change = FALSE;
735 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
737 pos); /* equivalent to cursor_pos + common_prefix_len); */
738 chooser_entry->has_completion = TRUE;
741 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
743 else if (!have_result)
745 result = NOTHING_INSERTED_COMPLETE;
749 g_free (common_prefix);
766 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
767 const gchar *new_text,
768 gint new_text_length,
771 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
775 old_text_len = GTK_ENTRY (chooser_entry)->text_length;
776 insert_pos = *position;
778 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
780 if (chooser_entry->in_change)
783 remove_completion_feedback (chooser_entry);
785 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
786 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
787 && insert_pos == old_text_len)
788 install_start_autocompletion_idle (chooser_entry);
792 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
794 if (chooser_entry->in_change)
797 clear_completions (chooser_entry);
801 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
805 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
807 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
809 clear_completions_if_not_in_change (chooser_entry);
813 gtk_file_chooser_entry_set_position (GtkEditable *editable,
816 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
818 parent_editable_iface->set_position (editable, position);
820 clear_completions_if_not_in_change (chooser_entry);
824 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
828 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
830 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
832 clear_completions_if_not_in_change (chooser_entry);
836 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
838 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
839 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
843 gtk_file_chooser_entry_unmap (GtkWidget *widget)
845 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
847 remove_completion_feedback (chooser_entry);
849 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
853 completion_feedback_window_expose_event_cb (GtkWidget *widget,
854 GdkEventExpose *event,
857 /* Stolen from gtk_tooltip_paint_window() */
859 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
861 gtk_paint_flat_box (chooser_entry->completion_feedback_window->style,
862 chooser_entry->completion_feedback_window->window,
866 chooser_entry->completion_feedback_window,
869 chooser_entry->completion_feedback_window->allocation.width,
870 chooser_entry->completion_feedback_window->allocation.height);
876 set_invisible_mouse_cursor (GdkWindow *window)
881 display = gdk_drawable_get_display (window);
882 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
884 gdk_window_set_cursor (window, cursor);
886 gdk_cursor_unref (cursor);
890 completion_feedback_window_realize_cb (GtkWidget *widget,
893 /* We hide the mouse cursor inside the completion feedback window, since
894 * GtkEntry hides the cursor when the user types. We don't want the cursor to
895 * come back if the completion feedback ends up where the mouse is.
897 set_invisible_mouse_cursor (widget->window);
901 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
903 /* Stolen from gtk_tooltip_init() */
905 GtkWidget *alignment;
907 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
908 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
909 GDK_WINDOW_TYPE_HINT_TOOLTIP);
910 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
911 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
912 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
914 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
915 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
916 chooser_entry->completion_feedback_window->style->ythickness,
917 chooser_entry->completion_feedback_window->style->ythickness,
918 chooser_entry->completion_feedback_window->style->xthickness,
919 chooser_entry->completion_feedback_window->style->xthickness);
920 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
921 gtk_widget_show (alignment);
923 g_signal_connect (chooser_entry->completion_feedback_window, "expose-event",
924 G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
925 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
926 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
927 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
929 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
930 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
931 gtk_widget_show (chooser_entry->completion_feedback_label);
935 completion_feedback_timeout_cb (gpointer data)
937 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
939 chooser_entry->completion_feedback_timeout_id = 0;
941 remove_completion_feedback (chooser_entry);
946 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
948 if (chooser_entry->completion_feedback_timeout_id != 0)
949 g_source_remove (chooser_entry->completion_feedback_timeout_id);
951 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
952 completion_feedback_timeout_cb,
956 /* Gets the x position of the text cursor in the entry, in widget coordinates */
958 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
961 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
962 * the reader, you have to implement support for the entry's scroll offset and
963 * RTL layouts and all the fancy Pango stuff.
967 gint layout_x, layout_y;
969 PangoRectangle strong_pos;
971 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
973 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
975 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
976 GTK_ENTRY (chooser_entry)->current_pos);
978 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
980 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
984 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
986 /* More or less stolen from gtk_tooltip_position() */
988 GtkRequisition feedback_req;
989 gint entry_x, entry_y;
991 GtkAllocation *entry_allocation;
992 int feedback_x, feedback_y;
994 gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);
996 gdk_window_get_origin (GTK_WIDGET (chooser_entry)->window, &entry_x, &entry_y);
997 entry_allocation = &(GTK_WIDGET (chooser_entry)->allocation);
999 get_entry_cursor_x (chooser_entry, &cursor_x);
1001 /* FIXME: fit to the screen if we bump on the screen's edge */
1002 feedback_x = entry_x + cursor_x + entry_allocation->height / 2; /* cheap "half M-width" */
1003 feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;
1005 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1006 gtk_widget_show (chooser_entry->completion_feedback_window);
1008 install_completion_feedback_timer (chooser_entry);
1012 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1013 const gchar *feedback)
1015 if (chooser_entry->completion_feedback_window == NULL)
1016 create_completion_feedback_window (chooser_entry);
1018 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1020 show_completion_feedback_window (chooser_entry);
1024 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1026 if (chooser_entry->completion_feedback_window)
1027 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1029 chooser_entry->completion_feedback_window = NULL;
1030 chooser_entry->completion_feedback_label = NULL;
1032 if (chooser_entry->completion_feedback_timeout_id != 0)
1034 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1035 chooser_entry->completion_feedback_timeout_id = 0;
1040 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1042 CommonPrefixResult result;
1044 g_assert (chooser_entry->current_folder != NULL);
1045 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1047 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1049 * - If there is a common prefix, insert it (done)
1050 * - If there is no common prefix, pop up the suggestion window
1051 * - If there are no matches at all, beep and bring up a tooltip (done)
1052 * - If the suggestion window is already up, scroll it
1054 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1059 /* We already beeped in append_common_prefix(); do nothing here */
1063 beep (chooser_entry);
1064 /* translators: this text is shown when there are no completions
1065 * for something the user typed in a file chooser entry
1067 pop_up_completion_feedback (chooser_entry, _("No match"));
1070 case NOTHING_INSERTED_COMPLETE:
1071 /* FIXME: pop up the suggestion window or scroll it */
1074 case NOTHING_INSERTED_UNIQUE:
1075 /* translators: this text is shown when there is exactly one completion
1076 * for something the user typed in a file chooser entry
1078 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1085 case COMPLETED_UNIQUE:
1089 case COMPLETE_BUT_NOT_UNIQUE:
1090 /* translators: this text is shown when the text in a file chooser
1091 * entry is a complete filename, but could be continued to find
1094 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1098 g_assert_not_reached ();
1103 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1105 refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1107 if (!chooser_entry->current_folder_file)
1109 /* Here, no folder path means we couldn't parse what the user typed. */
1111 beep (chooser_entry);
1112 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
1114 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1118 if (chooser_entry->local_only
1119 && !g_file_is_native (chooser_entry->current_folder_file))
1121 beep (chooser_entry);
1122 pop_up_completion_feedback (chooser_entry, _("Only local files can be selected"));
1124 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1128 if (chooser_entry->current_folder
1129 && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1131 explicitly_complete (chooser_entry);
1135 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1137 /* translators: this text is shown while the system is searching
1138 * for possible completions for text in a file chooser entry
1140 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1145 gtk_file_chooser_entry_focus (GtkWidget *widget,
1146 GtkDirectionType direction)
1148 GtkFileChooserEntry *chooser_entry;
1149 GtkEditable *editable;
1151 GdkModifierType state;
1152 gboolean control_pressed;
1154 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1155 editable = GTK_EDITABLE (widget);
1156 entry = GTK_ENTRY (widget);
1158 if (!chooser_entry->eat_tabs)
1159 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1161 control_pressed = FALSE;
1163 if (gtk_get_current_event_state (&state))
1165 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1166 control_pressed = TRUE;
1169 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1170 * makes it 'safe' for people to hit. */
1171 if ((direction == GTK_DIR_TAB_FORWARD) &&
1172 (GTK_WIDGET_HAS_FOCUS (widget)) &&
1173 (! control_pressed))
1175 if (chooser_entry->has_completion)
1176 gtk_editable_set_position (editable, entry->text_length);
1178 start_explicit_completion (chooser_entry);
1183 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1187 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1188 GdkEventFocus *event)
1190 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1192 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1194 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1198 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1200 if (chooser_entry->has_completion)
1202 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1203 GTK_ENTRY (chooser_entry)->text_length);
1206 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1210 gtk_file_chooser_entry_activate (GtkEntry *entry)
1212 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1214 commit_completion_and_refresh (chooser_entry);
1215 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1219 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1221 if (!chooser_entry->completion_store)
1224 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1225 g_object_unref (chooser_entry->completion_store);
1226 chooser_entry->completion_store = NULL;
1229 /* Fills the completion store from the contents of the current folder */
1231 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1236 discard_completion_store (chooser_entry);
1238 files = _gtk_folder_list_children (chooser_entry->current_folder);
1240 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1244 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1249 file = tmp_list->data;
1251 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1255 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1258 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
1260 gtk_list_store_append (chooser_entry->completion_store, &iter);
1261 gtk_list_store_set (chooser_entry->completion_store, &iter,
1262 DISPLAY_NAME_COLUMN, display_name,
1266 g_object_unref (info);
1267 g_free (display_name);
1271 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1272 g_slist_free (files);
1274 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1275 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1277 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1278 GTK_TREE_MODEL (chooser_entry->completion_store));
1281 /* When we finish loading the current folder, this function should get called to
1282 * perform the deferred autocompletion or explicit completion.
1285 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1287 switch (chooser_entry->load_complete_action)
1289 case LOAD_COMPLETE_NOTHING:
1292 case LOAD_COMPLETE_AUTOCOMPLETE:
1293 autocomplete (chooser_entry);
1296 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1297 explicitly_complete (chooser_entry);
1301 g_assert_not_reached ();
1304 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1308 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1310 populate_completion_store (chooser_entry);
1311 perform_load_complete_action (chooser_entry);
1313 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1316 /* Callback when the current folder finishes loading */
1318 finished_loading_cb (GtkFolder *folder,
1321 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1323 finish_folder_load (chooser_entry);
1326 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1328 load_directory_get_folder_callback (GCancellable *cancellable,
1330 const GError *error,
1333 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1334 GtkFileChooserEntry *chooser_entry = data;
1336 if (cancellable != chooser_entry->load_folder_cancellable)
1339 chooser_entry->load_folder_cancellable = NULL;
1343 LoadCompleteAction old_load_complete_action;
1345 old_load_complete_action = chooser_entry->load_complete_action;
1347 discard_completion_store (chooser_entry);
1348 clear_completions (chooser_entry);
1350 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1352 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1354 beep (chooser_entry);
1355 pop_up_completion_feedback (chooser_entry, error->message);
1358 discard_current_folder (chooser_entry);
1361 if (cancelled || error)
1364 g_assert (folder != NULL);
1365 chooser_entry->current_folder = g_object_ref (folder);
1367 discard_completion_store (chooser_entry);
1369 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1370 finish_folder_load (chooser_entry);
1372 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1373 G_CALLBACK (finished_loading_cb), chooser_entry);
1376 g_object_unref (chooser_entry);
1377 g_object_unref (cancellable);
1381 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1383 if (chooser_entry->current_folder_file == NULL ||
1384 chooser_entry->file_system == NULL)
1387 if (chooser_entry->local_only
1388 && !g_file_is_native (chooser_entry->current_folder_file))
1391 g_assert (chooser_entry->current_folder == NULL);
1392 g_assert (chooser_entry->load_folder_cancellable == NULL);
1394 chooser_entry->load_folder_cancellable =
1395 _gtk_file_system_get_folder (chooser_entry->file_system,
1396 chooser_entry->current_folder_file,
1397 "standard::name,standard::display-name,standard::type",
1398 load_directory_get_folder_callback,
1399 g_object_ref (chooser_entry));
1403 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1405 gboolean force_reload)
1407 gboolean reload = FALSE;
1409 if (chooser_entry->current_folder_file)
1411 if ((folder_file && !(g_file_equal (folder_file, chooser_entry->current_folder_file)
1412 && chooser_entry->load_folder_cancellable))
1417 /* We changed our current directory. We need to clear out the old
1418 * directory information.
1420 if (chooser_entry->load_folder_cancellable)
1422 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
1423 chooser_entry->load_folder_cancellable = NULL;
1426 discard_current_folder (chooser_entry);
1427 g_object_unref (chooser_entry->current_folder_file);
1428 chooser_entry->current_folder_file = (folder_file) ? g_object_ref (folder_file) : NULL;
1433 chooser_entry->current_folder_file = (folder_file) ? g_object_ref (folder_file) : NULL;
1438 start_loading_current_folder (chooser_entry);
1442 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1443 RefreshMode refresh_mode)
1445 GtkEditable *editable;
1450 gsize total_len, file_part_len;
1454 editable = GTK_EDITABLE (chooser_entry);
1456 switch (refresh_mode)
1458 case REFRESH_UP_TO_CURSOR_POSITION:
1459 end_pos = gtk_editable_get_position (editable);
1462 case REFRESH_WHOLE_TEXT:
1463 end_pos = GTK_ENTRY (chooser_entry)->text_length;
1467 g_assert_not_reached ();
1471 text = gtk_editable_get_chars (editable, 0, end_pos);
1474 if (!chooser_entry->file_system ||
1475 !chooser_entry->base_folder ||
1476 !_gtk_file_system_parse (chooser_entry->file_system,
1477 chooser_entry->base_folder, text,
1478 &folder_file, &file_part, &error))
1480 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1483 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1486 g_error_free (error);
1488 file_part = g_strdup ("");
1493 file_part_len = strlen (file_part);
1494 total_len = strlen (text);
1495 if (total_len > file_part_len)
1496 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1503 g_free (chooser_entry->file_part);
1505 chooser_entry->file_part = file_part;
1506 chooser_entry->file_part_pos = file_part_pos;
1508 reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1511 g_object_unref (folder_file);
1515 autocomplete (GtkFileChooserEntry *chooser_entry)
1517 g_assert (chooser_entry->current_folder != NULL);
1518 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1519 g_assert (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length);
1521 append_common_prefix (chooser_entry, TRUE, FALSE);
1525 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1527 refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1529 if (!chooser_entry->current_folder)
1531 /* We don't beep or anything, since this is autocompletion - the user
1532 * didn't request any action explicitly.
1537 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1538 autocomplete (chooser_entry);
1540 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1544 start_autocompletion_idle_handler (gpointer data)
1546 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1548 start_autocompletion (chooser_entry);
1550 chooser_entry->start_autocompletion_idle_id = 0;
1556 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1558 if (chooser_entry->start_autocompletion_idle_id != 0)
1561 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1566 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1567 const gchar *new_text,
1568 gint new_text_length,
1572 const gchar *colon = memchr (new_text, ':', new_text_length);
1575 /* Disallow these characters altogether */
1576 for (i = 0; i < new_text_length; i++)
1578 if (new_text[i] == '<' ||
1579 new_text[i] == '>' ||
1580 new_text[i] == '"' ||
1581 new_text[i] == '|' ||
1582 new_text[i] == '*' ||
1587 if (i < new_text_length ||
1588 /* Disallow entering text that would cause a colon to be anywhere except
1589 * after a drive letter.
1592 *position + (colon - new_text) != 1) ||
1593 (new_text_length > 0 &&
1595 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1596 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1598 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1599 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1607 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1612 /* If deleting a drive letter, delete the colon, too */
1613 if (start_pos == 0 && end_pos == 1 &&
1614 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1615 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1617 g_signal_handlers_block_by_func (chooser_entry,
1618 G_CALLBACK (delete_text_callback),
1620 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1621 g_signal_handlers_unblock_by_func (chooser_entry,
1622 G_CALLBACK (delete_text_callback),
1629 * _gtk_file_chooser_entry_new:
1630 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1632 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1633 * is an internal implementation widget for the GTK+ file chooser
1634 * which is an entry with completion with respect to a
1635 * #GtkFileSystem object.
1637 * Return value: the newly created #GtkFileChooserEntry
1640 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1642 GtkFileChooserEntry *chooser_entry;
1644 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1645 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1647 return GTK_WIDGET (chooser_entry);
1651 * _gtk_file_chooser_entry_set_file_system:
1652 * @chooser_entry: a #GtkFileChooser
1653 * @file_system: an object implementing #GtkFileSystem
1655 * Sets the file system for @chooser_entry.
1658 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1659 GtkFileSystem *file_system)
1661 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1662 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1664 if (file_system != chooser_entry->file_system)
1666 if (chooser_entry->file_system)
1667 g_object_unref (chooser_entry->file_system);
1669 chooser_entry->file_system = g_object_ref (file_system);
1674 * _gtk_file_chooser_entry_set_base_folder:
1675 * @chooser_entry: a #GtkFileChooserEntry
1676 * @file: file for a folder in the chooser entries current file system.
1678 * Sets the folder with respect to which completions occur.
1681 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1684 if (chooser_entry->base_folder)
1685 g_object_unref (chooser_entry->base_folder);
1687 chooser_entry->base_folder = file;
1689 if (chooser_entry->base_folder)
1690 g_object_ref (chooser_entry->base_folder);
1692 clear_completions (chooser_entry);
1693 _gtk_file_chooser_entry_select_filename (chooser_entry);
1697 * _gtk_file_chooser_entry_get_current_folder:
1698 * @chooser_entry: a #GtkFileChooserEntry
1700 * Gets the current folder for the #GtkFileChooserEntry. If the
1701 * user has only entered a filename, this will be the base folder
1702 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1703 * user has entered a relative or absolute path, then it will
1704 * be different. If the user has entered a relative or absolute
1705 * path that doesn't point to a folder in the file system, it will
1708 * Return value: the file for the current folder - this value is owned by the
1709 * chooser entry and must not be modified or freed.
1712 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1714 commit_completion_and_refresh (chooser_entry);
1715 return chooser_entry->current_folder_file;
1719 * _gtk_file_chooser_entry_get_file_part:
1720 * @chooser_entry: a #GtkFileChooserEntry
1722 * Gets the non-folder portion of whatever the user has entered
1723 * into the file selector. What is returned is a UTF-8 string,
1724 * and if a filename path is needed, g_file_get_child_for_display_name()
1727 * Return value: the entered filename - this value is owned by the
1728 * chooser entry and must not be modified or freed.
1731 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1733 commit_completion_and_refresh (chooser_entry);
1734 return chooser_entry->file_part;
1738 * _gtk_file_chooser_entry_set_file_part:
1739 * @chooser_entry: a #GtkFileChooserEntry
1740 * @file_part: text to display in the entry, in UTF-8
1742 * Sets the current text shown in the file chooser entry.
1745 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1746 const gchar *file_part)
1748 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1750 chooser_entry->in_change = TRUE;
1751 clear_completions (chooser_entry);
1752 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1753 chooser_entry->in_change = FALSE;
1758 * _gtk_file_chooser_entry_set_action:
1759 * @chooser_entry: a #GtkFileChooserEntry
1760 * @action: the action which is performed by the file selector using this entry
1762 * Sets action which is performed by the file selector using this entry.
1763 * The #GtkFileChooserEntry will use different completion strategies for
1764 * different actions.
1767 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1768 GtkFileChooserAction action)
1770 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1772 if (chooser_entry->action != action)
1774 GtkEntryCompletion *comp;
1776 chooser_entry->action = action;
1778 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1780 /* FIXME: do we need to actually set the following? */
1784 case GTK_FILE_CHOOSER_ACTION_OPEN:
1785 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1786 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1788 case GTK_FILE_CHOOSER_ACTION_SAVE:
1789 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1790 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1798 * _gtk_file_chooser_entry_get_action:
1799 * @chooser_entry: a #GtkFileChooserEntry
1801 * Gets the action for this entry.
1803 * Returns: the action
1805 GtkFileChooserAction
1806 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1808 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1809 GTK_FILE_CHOOSER_ACTION_OPEN);
1811 return chooser_entry->action;
1815 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1818 gboolean retval = FALSE;
1820 if (chooser_entry->current_folder)
1822 GFileInfo *file_info;
1824 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1828 retval = _gtk_file_info_consider_as_directory (file_info);
1829 g_object_unref (file_info);
1838 * _gtk_file_chooser_entry_select_filename:
1839 * @chooser_entry: a #GtkFileChooserEntry
1841 * Selects the filename (without the extension) for user edition.
1844 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1846 const gchar *str, *ext;
1849 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1851 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1852 ext = g_strrstr (str, ".");
1855 len = g_utf8_pointer_to_offset (str, ext);
1858 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1862 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1863 gboolean local_only)
1865 chooser_entry->local_only = local_only;
1866 clear_completions (chooser_entry);
1870 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1872 return chooser_entry->local_only;