1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserentry.c: Entry with filename completion
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gtkfilechooserentry.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
32 #include "gtksizerequest.h"
33 #include "gtkwindow.h"
36 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
38 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
39 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
40 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
42 struct _GtkFileChooserEntryClass
44 GtkEntryClass parent_class;
47 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
49 LOAD_COMPLETE_NOTHING,
50 LOAD_COMPLETE_AUTOCOMPLETE,
51 LOAD_COMPLETE_EXPLICIT_COMPLETION
57 REFRESH_INVALID_INPUT,
58 REFRESH_INCOMPLETE_HOSTNAME,
63 struct _GtkFileChooserEntry
65 GtkEntry parent_instance;
67 GtkFileChooserAction action;
69 GtkFileSystem *file_system;
71 GFile *current_folder_file;
75 /* Folder being loaded or already loaded */
76 GtkFolder *current_folder;
77 GCancellable *load_folder_cancellable;
79 LoadCompleteAction load_complete_action;
81 GtkTreeModel *completion_store;
83 guint start_autocompletion_idle_id;
85 GtkWidget *completion_feedback_window;
86 GtkWidget *completion_feedback_label;
87 guint completion_feedback_timeout_id;
89 guint current_folder_loaded : 1;
90 guint has_completion : 1;
103 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
105 static void gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface);
107 static void gtk_file_chooser_entry_finalize (GObject *object);
108 static void gtk_file_chooser_entry_dispose (GObject *object);
109 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
110 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
111 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
113 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
114 GdkEventFocus *event);
115 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
116 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
117 const gchar *new_text,
118 gint new_text_length,
120 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
123 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
125 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
130 static gint insert_text_callback (GtkFileChooserEntry *widget,
131 const gchar *new_text,
132 gint new_text_length,
135 static void delete_text_callback (GtkFileChooserEntry *widget,
141 static gboolean match_selected_callback (GtkEntryCompletion *completion,
144 GtkFileChooserEntry *chooser_entry);
145 static gboolean completion_match_func (GtkEntryCompletion *comp,
151 REFRESH_UP_TO_CURSOR_POSITION,
155 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
156 RefreshMode refresh_mode);
157 static void finished_loading_cb (GtkFolder *folder,
159 static void autocomplete (GtkFileChooserEntry *chooser_entry);
160 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
161 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
162 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
163 const gchar *feedback);
165 static GtkEditableInterface *parent_editable_iface;
167 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
168 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
169 gtk_file_chooser_entry_iface_init))
172 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
174 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
175 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
176 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
178 gobject_class->finalize = gtk_file_chooser_entry_finalize;
179 gobject_class->dispose = gtk_file_chooser_entry_dispose;
181 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
182 widget_class->unmap = gtk_file_chooser_entry_unmap;
183 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
184 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
186 entry_class->activate = gtk_file_chooser_entry_activate;
190 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
192 parent_editable_iface = g_type_interface_peek_parent (iface);
194 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
195 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
196 iface->set_position = gtk_file_chooser_entry_set_position;
197 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
201 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
203 GtkEntryCompletion *comp;
204 GtkCellRenderer *cell;
206 chooser_entry->local_only = TRUE;
208 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
210 comp = gtk_entry_completion_new ();
211 gtk_entry_completion_set_popup_single_match (comp, FALSE);
213 gtk_entry_completion_set_match_func (comp,
214 completion_match_func,
218 cell = gtk_cell_renderer_text_new ();
219 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
221 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
223 "text", DISPLAY_NAME_COLUMN);
225 g_signal_connect (comp, "match-selected",
226 G_CALLBACK (match_selected_callback), chooser_entry);
228 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
229 g_object_unref (comp);
232 g_signal_connect (chooser_entry, "insert-text",
233 G_CALLBACK (insert_text_callback), NULL);
234 g_signal_connect (chooser_entry, "delete-text",
235 G_CALLBACK (delete_text_callback), NULL);
240 gtk_file_chooser_entry_finalize (GObject *object)
242 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
244 if (chooser_entry->base_folder)
245 g_object_unref (chooser_entry->base_folder);
247 if (chooser_entry->current_folder)
248 g_object_unref (chooser_entry->current_folder);
250 if (chooser_entry->current_folder_file)
251 g_object_unref (chooser_entry->current_folder_file);
253 g_free (chooser_entry->file_part);
255 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
259 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
261 if (chooser_entry->current_folder)
263 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
264 G_CALLBACK (finished_loading_cb), chooser_entry);
265 g_object_unref (chooser_entry->current_folder);
266 chooser_entry->current_folder = NULL;
268 if (chooser_entry->load_folder_cancellable)
270 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
271 chooser_entry->load_folder_cancellable = NULL;
274 if (chooser_entry->current_folder_file)
276 g_object_unref (chooser_entry->current_folder_file);
277 chooser_entry->current_folder_file = NULL;
282 gtk_file_chooser_entry_dispose (GObject *object)
284 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
286 remove_completion_feedback (chooser_entry);
287 discard_loading_and_current_folder_file (chooser_entry);
289 if (chooser_entry->start_autocompletion_idle_id != 0)
291 g_source_remove (chooser_entry->start_autocompletion_idle_id);
292 chooser_entry->start_autocompletion_idle_id = 0;
295 if (chooser_entry->completion_store)
297 g_object_unref (chooser_entry->completion_store);
298 chooser_entry->completion_store = NULL;
301 if (chooser_entry->file_system)
303 g_object_unref (chooser_entry->file_system);
304 chooser_entry->file_system = NULL;
307 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
310 /* Match functions for the GtkEntryCompletion */
312 match_selected_callback (GtkEntryCompletion *completion,
315 GtkFileChooserEntry *chooser_entry)
321 gtk_tree_model_get (model, iter,
322 DISPLAY_NAME_COLUMN, &display_name,
326 if (!display_name || !file)
329 g_object_unref (file);
331 g_free (display_name);
335 pos = chooser_entry->file_part_pos;
337 /* We don't set in_change here as we want to update the current_folder
339 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
341 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
344 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
346 g_object_unref (file);
347 g_free (display_name);
352 /* Match function for the GtkEntryCompletion */
354 completion_match_func (GtkEntryCompletion *comp,
355 const char *key_unused,
359 GtkFileChooserEntry *chooser_entry;
362 char *norm_file_part;
365 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
367 /* We ignore the key because it is the contents of the entry. Instead, we
368 * just use our precomputed file_part.
370 if (!chooser_entry->file_part)
375 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
378 return FALSE; /* Uninitialized row, ugh */
381 /* If we have an empty file_part, then we're at the root of a directory. In
382 * that case, we want to match all non-dot files. We might want to match
383 * dot_files too if show_hidden is TRUE on the fileselector in the future.
385 /* Additionally, support for gnome .hidden files would be sweet, too */
386 if (chooser_entry->file_part[0] == '\000')
398 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
399 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
401 #ifdef G_PLATFORM_WIN32
405 temp = norm_file_part;
406 norm_file_part = g_utf8_casefold (norm_file_part, -1);
410 norm_name = g_utf8_casefold (norm_name, -1);
415 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
417 g_free (norm_file_part);
425 clear_completions (GtkFileChooserEntry *chooser_entry)
427 chooser_entry->has_completion = FALSE;
428 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
430 remove_completion_feedback (chooser_entry);
434 beep (GtkFileChooserEntry *chooser_entry)
436 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
439 /* Determines if the completion model has entries with a common prefix relative
440 * to the current contents of the entry. Also, if there's one and only one such
441 * path, stores it in unique_path_ret.
444 find_common_prefix (GtkFileChooserEntry *chooser_entry,
445 gchar **common_prefix_ret,
446 GFile **unique_file_ret,
447 gboolean *is_complete_not_unique_ret,
448 gboolean *prefix_expands_the_file_part_ret,
451 GtkEditable *editable;
455 char *text_up_to_cursor;
456 GFile *parsed_folder_file;
457 char *parsed_file_part;
459 *common_prefix_ret = NULL;
460 *unique_file_ret = NULL;
461 *is_complete_not_unique_ret = FALSE;
462 *prefix_expands_the_file_part_ret = FALSE;
464 editable = GTK_EDITABLE (chooser_entry);
466 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
468 parsed = _gtk_file_system_parse (chooser_entry->file_system,
469 chooser_entry->base_folder,
475 g_free (text_up_to_cursor);
480 g_assert (parsed_folder_file != NULL
481 && chooser_entry->current_folder != NULL
482 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
484 g_object_unref (parsed_folder_file);
486 /* First pass: find the common prefix */
488 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
495 gtk_tree_model_get (chooser_entry->completion_store,
497 DISPLAY_NAME_COLUMN, &display_name,
501 if (g_str_has_prefix (display_name, parsed_file_part))
503 if (!*common_prefix_ret)
505 *common_prefix_ret = g_strdup (display_name);
506 *unique_file_ret = g_object_ref (file);
510 gchar *p = *common_prefix_ret;
511 const gchar *q = display_name;
513 while (*p && *p == *q)
521 if (*unique_file_ret)
523 g_object_unref (*unique_file_ret);
524 *unique_file_ret = NULL;
529 g_free (display_name);
530 g_object_unref (file);
531 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
534 /* Second pass: see if the prefix we found is a complete match */
536 if (*common_prefix_ret != NULL)
538 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
545 gtk_tree_model_get (chooser_entry->completion_store,
547 DISPLAY_NAME_COLUMN, &display_name,
549 len = strlen (display_name);
552 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
555 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
556 *is_complete_not_unique_ret = TRUE;
558 g_free (display_name);
559 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
562 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
564 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
567 g_free (parsed_file_part);
573 INVALID_INPUT, /* what the user typed is bogus */
574 NO_MATCH, /* no matches based on what the user typed */
575 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
576 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
577 COMPLETED, /* completion inserted (ambiguous suffix) */
578 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
579 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
580 } CommonPrefixResult;
582 /* Finds a common prefix based on the contents of the entry
583 * and mandatorily appends it
585 static CommonPrefixResult
586 append_common_prefix (GtkFileChooserEntry *chooser_entry,
588 gboolean show_errors)
590 gchar *common_prefix;
592 gboolean is_complete_not_unique;
593 gboolean prefix_expands_the_file_part;
595 CommonPrefixResult result = NO_MATCH;
596 gboolean have_result;
598 clear_completions (chooser_entry);
600 if (chooser_entry->completion_store == NULL)
604 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
606 /* If the user types an incomplete hostname ("http://foo" without
607 * a slash after that), it's not an error. We just don't want to
608 * pop up a meaningless completion window in that state.
610 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
613 beep (chooser_entry);
614 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
617 g_error_free (error);
619 return INVALID_INPUT;
626 g_object_unref (unique_file);
628 if (prefix_expands_the_file_part)
629 result = COMPLETED_UNIQUE;
631 result = NOTHING_INSERTED_UNIQUE;
637 if (is_complete_not_unique)
639 result = COMPLETE_BUT_NOT_UNIQUE;
649 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
651 pos = chooser_entry->file_part_pos;
653 if (prefix_expands_the_file_part)
655 chooser_entry->in_change = TRUE;
656 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
658 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
661 chooser_entry->in_change = FALSE;
665 /* equivalent to cursor_pos + common_prefix_len); */
666 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
669 chooser_entry->has_completion = TRUE;
672 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
674 else if (!have_result)
676 result = NOTHING_INSERTED_COMPLETE;
680 g_free (common_prefix);
697 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
698 const gchar *new_text,
699 gint new_text_length,
702 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
706 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
707 insert_pos = *position;
709 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
711 if (chooser_entry->in_change)
714 remove_completion_feedback (chooser_entry);
716 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
717 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
718 && insert_pos == old_text_len)
719 install_start_autocompletion_idle (chooser_entry);
723 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
725 if (chooser_entry->in_change)
728 clear_completions (chooser_entry);
732 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
736 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
738 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
740 clear_completions_if_not_in_change (chooser_entry);
744 gtk_file_chooser_entry_set_position (GtkEditable *editable,
747 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
749 parent_editable_iface->set_position (editable, position);
751 clear_completions_if_not_in_change (chooser_entry);
755 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
759 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
761 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
763 clear_completions_if_not_in_change (chooser_entry);
767 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
769 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
770 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
774 gtk_file_chooser_entry_unmap (GtkWidget *widget)
776 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
778 remove_completion_feedback (chooser_entry);
780 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
784 completion_feedback_window_draw_cb (GtkWidget *widget,
788 /* Stolen from gtk_tooltip_paint_window() */
790 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
791 GtkStyleContext *context;
793 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
795 gtk_render_background (context, cr, 0, 0,
796 gtk_widget_get_allocated_width (widget),
797 gtk_widget_get_allocated_height (widget));
798 gtk_render_frame (context, cr, 0, 0,
799 gtk_widget_get_allocated_width (widget),
800 gtk_widget_get_allocated_height (widget));
806 set_invisible_mouse_cursor (GdkWindow *window)
811 display = gdk_window_get_display (window);
812 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
814 gdk_window_set_cursor (window, cursor);
816 g_object_unref (cursor);
820 completion_feedback_window_realize_cb (GtkWidget *widget,
823 /* We hide the mouse cursor inside the completion feedback window, since
824 * GtkEntry hides the cursor when the user types. We don't want the cursor to
825 * come back if the completion feedback ends up where the mouse is.
827 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
831 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
833 /* Stolen from gtk_tooltip_init() */
834 GtkWidget *window, *label;
835 GtkStyleContext *context;
837 window = gtk_window_new (GTK_WINDOW_POPUP);
838 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
839 gtk_widget_set_app_paintable (window, TRUE);
840 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
841 gtk_widget_set_name (window, "gtk-tooltip");
843 context = gtk_widget_get_style_context (window);
844 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
846 g_signal_connect (window, "draw",
847 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
848 g_signal_connect (window, "realize",
849 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
850 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
852 label = gtk_label_new (NULL);
853 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
854 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
855 /* FIXME: don't hardcode this */
856 gtk_widget_set_margin_left (label, 6);
857 gtk_widget_set_margin_right (label, 6);
858 gtk_widget_set_margin_top (label, 6);
859 gtk_widget_set_margin_bottom (label, 6);
860 gtk_container_add (GTK_CONTAINER (window), label);
861 gtk_widget_show (label);
863 chooser_entry->completion_feedback_window = window;
864 chooser_entry->completion_feedback_label = label;
868 completion_feedback_timeout_cb (gpointer data)
870 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
872 chooser_entry->completion_feedback_timeout_id = 0;
874 remove_completion_feedback (chooser_entry);
879 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
881 if (chooser_entry->completion_feedback_timeout_id != 0)
882 g_source_remove (chooser_entry->completion_feedback_timeout_id);
884 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
885 completion_feedback_timeout_cb,
889 /* Gets the x position of the text cursor in the entry, in widget coordinates */
891 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
894 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
895 * the reader, you have to implement support for the entry's scroll offset and
896 * RTL layouts and all the fancy Pango stuff.
900 gint layout_x, layout_y;
902 PangoRectangle strong_pos;
903 gint start_pos, end_pos;
905 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
907 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
909 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
910 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
914 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
916 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
920 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
922 /* More or less stolen from gtk_tooltip_position() */
924 GtkRequisition feedback_req;
925 GtkWidget *widget = GTK_WIDGET (chooser_entry);
926 gint entry_x, entry_y;
928 GtkAllocation entry_allocation;
929 int feedback_x, feedback_y;
931 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
932 &feedback_req, NULL);
934 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
935 gtk_widget_get_allocation (widget, &entry_allocation);
937 get_entry_cursor_x (chooser_entry, &cursor_x);
939 /* FIXME: fit to the screen if we bump on the screen's edge */
940 /* cheap "half M-width", use height as approximation of character em-size */
941 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
942 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
944 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
945 gtk_widget_show (chooser_entry->completion_feedback_window);
947 install_completion_feedback_timer (chooser_entry);
951 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
952 const gchar *feedback)
954 if (chooser_entry->completion_feedback_window == NULL)
955 create_completion_feedback_window (chooser_entry);
957 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
959 show_completion_feedback_window (chooser_entry);
963 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
965 if (chooser_entry->completion_feedback_window)
966 gtk_widget_destroy (chooser_entry->completion_feedback_window);
968 chooser_entry->completion_feedback_window = NULL;
969 chooser_entry->completion_feedback_label = NULL;
971 if (chooser_entry->completion_feedback_timeout_id != 0)
973 g_source_remove (chooser_entry->completion_feedback_timeout_id);
974 chooser_entry->completion_feedback_timeout_id = 0;
979 explicitly_complete (GtkFileChooserEntry *chooser_entry)
981 CommonPrefixResult result;
983 g_assert (chooser_entry->current_folder != NULL);
984 g_assert (chooser_entry->current_folder_loaded);
986 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
988 * - If there is a common prefix, insert it (done)
989 * - If there is no common prefix, pop up the suggestion window
990 * - If there are no matches at all, beep and bring up a tooltip (done)
991 * - If the suggestion window is already up, scroll it
993 result = append_common_prefix (chooser_entry, FALSE, TRUE);
998 /* We already beeped in append_common_prefix(); do nothing here */
1002 beep (chooser_entry);
1003 /* translators: this text is shown when there are no completions
1004 * for something the user typed in a file chooser entry
1006 pop_up_completion_feedback (chooser_entry, _("No match"));
1009 case NOTHING_INSERTED_COMPLETE:
1010 /* FIXME: pop up the suggestion window or scroll it */
1013 case NOTHING_INSERTED_UNIQUE:
1014 /* translators: this text is shown when there is exactly one completion
1015 * for something the user typed in a file chooser entry
1017 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1024 case COMPLETED_UNIQUE:
1028 case COMPLETE_BUT_NOT_UNIQUE:
1029 /* translators: this text is shown when the text in a file chooser
1030 * entry is a complete filename, but could be continued to find
1033 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1037 g_assert_not_reached ();
1042 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1044 RefreshStatus status;
1048 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1055 g_assert (chooser_entry->current_folder_file != NULL);
1057 if (chooser_entry->current_folder && chooser_entry->current_folder_loaded)
1058 explicitly_complete (chooser_entry);
1061 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1063 /* Translators: this text is shown while the system is searching
1064 * for possible completions for filenames in a file chooser entry. */
1065 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1070 case REFRESH_INVALID_INPUT:
1072 /* Translators: this is shown in the feedback for Tab-completion in a file
1073 * chooser's text entry, when the user enters an invalid path. */
1074 feedback_msg = _("Invalid path");
1077 case REFRESH_INCOMPLETE_HOSTNAME:
1080 if (chooser_entry->local_only)
1082 /* hostnames in a local_only file chooser? user error */
1084 /* Translators: this is shown in the feedback for Tab-completion in a
1085 * file chooser's text entry when the user enters something like
1086 * "sftp://blahblah" in an app that only supports local filenames. */
1087 feedback_msg = _("Only local files may be selected");
1091 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1093 /* Translators: this is shown in the feedback for Tab-completion in a
1094 * file chooser's text entry when the user hasn't entered the first '/'
1095 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1096 feedback_msg = _("Incomplete hostname; end it with '/'");
1101 case REFRESH_NONEXISTENT:
1104 /* Translators: this is shown in the feedback for Tab-completion in a file
1105 * chooser's text entry when the user enters a path that does not exist
1106 * and then hits Tab */
1107 feedback_msg = _("Path does not exist");
1110 case REFRESH_NOT_LOCAL:
1112 feedback_msg = _("Only local files may be selected");
1116 g_assert_not_reached ();
1122 g_assert (chooser_entry->current_folder_file == NULL);
1124 beep (chooser_entry);
1125 pop_up_completion_feedback (chooser_entry, feedback_msg);
1126 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1131 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1134 GtkFileChooserEntry *chooser_entry;
1135 GtkEditable *editable;
1137 GdkModifierType state;
1138 gboolean control_pressed;
1140 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1141 editable = GTK_EDITABLE (widget);
1142 entry = GTK_ENTRY (widget);
1144 if (!chooser_entry->eat_tabs)
1145 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1147 control_pressed = FALSE;
1149 if (gtk_get_current_event_state (&state))
1151 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1152 control_pressed = TRUE;
1155 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1156 * makes it 'safe' for people to hit. */
1157 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1159 if (chooser_entry->has_completion)
1160 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1162 start_explicit_completion (chooser_entry);
1167 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1172 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1173 GdkEventFocus *event)
1175 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1177 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1179 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1183 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1185 if (chooser_entry->has_completion)
1187 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1188 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1191 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1192 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1196 gtk_file_chooser_entry_activate (GtkEntry *entry)
1198 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1200 commit_completion_and_refresh (chooser_entry);
1201 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1205 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1207 if (!chooser_entry->completion_store)
1210 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1211 g_object_unref (chooser_entry->completion_store);
1212 chooser_entry->completion_store = NULL;
1215 /* Fills the completion store from the contents of the current folder */
1217 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1222 discard_completion_store (chooser_entry);
1224 files = _gtk_folder_list_children (chooser_entry->current_folder);
1226 chooser_entry->completion_store = GTK_TREE_MODEL (gtk_list_store_new (N_COLUMNS,
1230 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1235 file = tmp_list->data;
1237 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1241 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1244 if (_gtk_file_info_consider_as_directory (info))
1245 display_name = g_strconcat (g_file_info_get_display_name (info), G_DIR_SEPARATOR_S, NULL);
1247 display_name = g_strdup (g_file_info_get_display_name (info));
1249 gtk_list_store_append (GTK_LIST_STORE (chooser_entry->completion_store), &iter);
1250 gtk_list_store_set (GTK_LIST_STORE (chooser_entry->completion_store), &iter,
1251 DISPLAY_NAME_COLUMN, display_name,
1255 g_object_unref (info);
1256 g_free (display_name);
1260 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1261 g_slist_free (files);
1263 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1264 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1266 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1267 chooser_entry->completion_store);
1270 /* When we finish loading the current folder, this function should get called to
1271 * perform the deferred autocompletion or explicit completion.
1274 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1276 switch (chooser_entry->load_complete_action)
1278 case LOAD_COMPLETE_NOTHING:
1281 case LOAD_COMPLETE_AUTOCOMPLETE:
1282 autocomplete (chooser_entry);
1285 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1286 explicitly_complete (chooser_entry);
1290 g_assert_not_reached ();
1293 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1297 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1299 populate_completion_store (chooser_entry);
1300 perform_load_complete_action (chooser_entry);
1302 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1305 /* Callback when the current folder finishes loading */
1307 finished_loading_cb (GtkFolder *folder,
1310 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1312 chooser_entry->current_folder_loaded = TRUE;
1313 finish_folder_load (chooser_entry);
1316 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1318 load_directory_get_folder_callback (GCancellable *cancellable,
1320 const GError *error,
1323 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1324 GtkFileChooserEntry *chooser_entry = data;
1326 if (cancellable != chooser_entry->load_folder_cancellable)
1329 chooser_entry->load_folder_cancellable = NULL;
1333 LoadCompleteAction old_load_complete_action;
1335 old_load_complete_action = chooser_entry->load_complete_action;
1337 discard_completion_store (chooser_entry);
1338 clear_completions (chooser_entry);
1340 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1342 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1344 beep (chooser_entry);
1345 pop_up_completion_feedback (chooser_entry, error->message);
1349 if (cancelled || error)
1352 g_assert (folder != NULL);
1353 chooser_entry->current_folder = g_object_ref (folder);
1355 discard_completion_store (chooser_entry);
1357 chooser_entry->current_folder_loaded = _gtk_folder_is_finished_loading (chooser_entry->current_folder);
1358 if (chooser_entry->current_folder_loaded)
1359 finish_folder_load (chooser_entry);
1361 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1362 G_CALLBACK (finished_loading_cb), chooser_entry);
1365 g_object_unref (chooser_entry);
1366 g_object_unref (cancellable);
1369 static RefreshStatus
1370 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1373 g_assert (folder_file != NULL);
1375 if (chooser_entry->current_folder_file
1376 && g_file_equal (folder_file, chooser_entry->current_folder_file)
1377 && chooser_entry->load_folder_cancellable)
1380 if (chooser_entry->current_folder_file)
1382 discard_loading_and_current_folder_file (chooser_entry);
1385 if (chooser_entry->local_only
1386 && !g_file_is_native (folder_file))
1387 return REFRESH_NOT_LOCAL;
1389 chooser_entry->current_folder_file = g_object_ref (folder_file);
1391 chooser_entry->load_folder_cancellable =
1392 _gtk_file_system_get_folder (chooser_entry->file_system,
1393 chooser_entry->current_folder_file,
1394 "standard::name,standard::display-name,standard::type",
1395 load_directory_get_folder_callback,
1396 g_object_ref (chooser_entry));
1401 static RefreshStatus
1402 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1403 RefreshMode refresh_mode)
1405 GtkEditable *editable;
1410 gsize total_len, file_part_len;
1413 RefreshStatus result;
1415 editable = GTK_EDITABLE (chooser_entry);
1417 switch (refresh_mode)
1419 case REFRESH_UP_TO_CURSOR_POSITION:
1420 end_pos = gtk_editable_get_position (editable);
1423 case REFRESH_WHOLE_TEXT:
1424 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1428 g_assert_not_reached ();
1429 return REFRESH_INVALID_INPUT;
1432 text = gtk_editable_get_chars (editable, 0, end_pos);
1435 if (!_gtk_file_system_parse (chooser_entry->file_system,
1436 chooser_entry->base_folder, text,
1437 &folder_file, &file_part, &error))
1439 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1442 result = REFRESH_INCOMPLETE_HOSTNAME;
1446 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1448 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1449 result = REFRESH_NONEXISTENT;
1451 result = REFRESH_INVALID_INPUT;
1455 g_error_free (error);
1457 file_part = g_strdup ("");
1462 g_assert (folder_file != NULL);
1464 file_part_len = strlen (file_part);
1465 total_len = strlen (text);
1466 if (total_len > file_part_len)
1467 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1471 result = REFRESH_OK;
1476 g_free (chooser_entry->file_part);
1478 chooser_entry->file_part = file_part;
1479 chooser_entry->file_part_pos = file_part_pos;
1481 if (result == REFRESH_OK)
1483 result = reload_current_folder (chooser_entry, folder_file);
1487 discard_loading_and_current_folder_file (chooser_entry);
1491 g_object_unref (folder_file);
1493 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1494 ((result == REFRESH_OK)
1495 && (chooser_entry->current_folder_file != NULL)
1496 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1497 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1498 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1499 || ((result != REFRESH_OK)
1500 && (chooser_entry->current_folder_file == NULL)
1501 && (chooser_entry->load_folder_cancellable == NULL)
1502 && (chooser_entry->current_folder == NULL)));
1508 autocomplete (GtkFileChooserEntry *chooser_entry)
1510 if (!(chooser_entry->current_folder != NULL
1511 && chooser_entry->current_folder_loaded
1512 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1515 append_common_prefix (chooser_entry, TRUE, FALSE);
1519 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1521 RefreshStatus status;
1523 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1528 g_assert (chooser_entry->current_folder_file != NULL);
1530 if (chooser_entry->current_folder && chooser_entry->current_folder_loaded)
1531 autocomplete (chooser_entry);
1533 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1537 case REFRESH_INVALID_INPUT:
1538 case REFRESH_INCOMPLETE_HOSTNAME:
1539 case REFRESH_NONEXISTENT:
1540 case REFRESH_NOT_LOCAL:
1541 /* We don't beep or anything, since this is autocompletion - the user
1542 * didn't request any action explicitly.
1547 g_assert_not_reached ();
1552 start_autocompletion_idle_handler (gpointer data)
1554 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1556 start_autocompletion (chooser_entry);
1558 chooser_entry->start_autocompletion_idle_id = 0;
1564 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1566 if (chooser_entry->start_autocompletion_idle_id != 0)
1569 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1574 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1575 const gchar *new_text,
1576 gint new_text_length,
1580 const gchar *colon = memchr (new_text, ':', new_text_length);
1583 /* Disallow these characters altogether */
1584 for (i = 0; i < new_text_length; i++)
1586 if (new_text[i] == '<' ||
1587 new_text[i] == '>' ||
1588 new_text[i] == '"' ||
1589 new_text[i] == '|' ||
1590 new_text[i] == '*' ||
1595 if (i < new_text_length ||
1596 /* Disallow entering text that would cause a colon to be anywhere except
1597 * after a drive letter.
1600 *position + (colon - new_text) != 1) ||
1601 (new_text_length > 0 &&
1603 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1604 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1606 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1607 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1615 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1620 /* If deleting a drive letter, delete the colon, too */
1621 if (start_pos == 0 && end_pos == 1 &&
1622 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1623 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1625 g_signal_handlers_block_by_func (chooser_entry,
1626 G_CALLBACK (delete_text_callback),
1628 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1629 g_signal_handlers_unblock_by_func (chooser_entry,
1630 G_CALLBACK (delete_text_callback),
1637 * _gtk_file_chooser_entry_new:
1638 * @filesystem: The #GtkFileSystem to use
1639 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1641 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1642 * is an internal implementation widget for the GTK+ file chooser
1643 * which is an entry with completion with respect to a
1644 * #GtkFileSystem object.
1646 * Return value: the newly created #GtkFileChooserEntry
1649 _gtk_file_chooser_entry_new (GtkFileSystem *file_system,
1652 GtkFileChooserEntry *chooser_entry;
1654 g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
1656 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1657 chooser_entry->file_system = g_object_ref (file_system);
1658 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1660 return GTK_WIDGET (chooser_entry);
1664 * _gtk_file_chooser_entry_set_base_folder:
1665 * @chooser_entry: a #GtkFileChooserEntry
1666 * @file: file for a folder in the chooser entries current file system.
1668 * Sets the folder with respect to which completions occur.
1671 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1674 if (chooser_entry->base_folder)
1675 g_object_unref (chooser_entry->base_folder);
1677 chooser_entry->base_folder = file;
1679 if (chooser_entry->base_folder)
1680 g_object_ref (chooser_entry->base_folder);
1682 clear_completions (chooser_entry);
1686 * _gtk_file_chooser_entry_get_current_folder:
1687 * @chooser_entry: a #GtkFileChooserEntry
1689 * Gets the current folder for the #GtkFileChooserEntry. If the
1690 * user has only entered a filename, this will be in the base folder
1691 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1692 * user has entered a relative or absolute path, then it will
1693 * be different. If the user has entered unparsable text, or text which
1694 * the entry cannot handle, this will return %NULL.
1696 * Return value: the file for the current folder - this value is owned by the
1697 * chooser entry and must not be modified or freed.
1700 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1702 commit_completion_and_refresh (chooser_entry);
1703 return chooser_entry->current_folder_file;
1707 * _gtk_file_chooser_entry_get_file_part:
1708 * @chooser_entry: a #GtkFileChooserEntry
1710 * Gets the non-folder portion of whatever the user has entered
1711 * into the file selector. What is returned is a UTF-8 string,
1712 * and if a filename path is needed, g_file_get_child_for_display_name()
1715 * Return value: the entered filename - this value is owned by the
1716 * chooser entry and must not be modified or freed.
1719 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1721 commit_completion_and_refresh (chooser_entry);
1722 return chooser_entry->file_part;
1726 * _gtk_file_chooser_entry_set_file_part:
1727 * @chooser_entry: a #GtkFileChooserEntry
1728 * @file_part: text to display in the entry, in UTF-8
1730 * Sets the current text shown in the file chooser entry.
1733 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1734 const gchar *file_part)
1736 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1738 chooser_entry->in_change = TRUE;
1739 clear_completions (chooser_entry);
1740 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1741 chooser_entry->in_change = FALSE;
1746 * _gtk_file_chooser_entry_set_action:
1747 * @chooser_entry: a #GtkFileChooserEntry
1748 * @action: the action which is performed by the file selector using this entry
1750 * Sets action which is performed by the file selector using this entry.
1751 * The #GtkFileChooserEntry will use different completion strategies for
1752 * different actions.
1755 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1756 GtkFileChooserAction action)
1758 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1760 if (chooser_entry->action != action)
1762 GtkEntryCompletion *comp;
1764 chooser_entry->action = action;
1766 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1768 /* FIXME: do we need to actually set the following? */
1772 case GTK_FILE_CHOOSER_ACTION_OPEN:
1773 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1774 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1776 case GTK_FILE_CHOOSER_ACTION_SAVE:
1777 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1778 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1786 * _gtk_file_chooser_entry_get_action:
1787 * @chooser_entry: a #GtkFileChooserEntry
1789 * Gets the action for this entry.
1791 * Returns: the action
1793 GtkFileChooserAction
1794 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1796 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1797 GTK_FILE_CHOOSER_ACTION_OPEN);
1799 return chooser_entry->action;
1803 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1806 gboolean retval = FALSE;
1808 if (chooser_entry->current_folder)
1810 GFileInfo *file_info;
1812 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1816 retval = _gtk_file_info_consider_as_directory (file_info);
1817 g_object_unref (file_info);
1826 * _gtk_file_chooser_entry_select_filename:
1827 * @chooser_entry: a #GtkFileChooserEntry
1829 * Selects the filename (without the extension) for user edition.
1832 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1834 const gchar *str, *ext;
1837 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1839 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1840 ext = g_strrstr (str, ".");
1843 len = g_utf8_pointer_to_offset (str, ext);
1846 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1850 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1851 gboolean local_only)
1853 chooser_entry->local_only = local_only;
1854 clear_completions (chooser_entry);
1858 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1860 return chooser_entry->local_only;