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;
91 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
93 static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface);
95 static void gtk_file_chooser_entry_finalize (GObject *object);
96 static void gtk_file_chooser_entry_dispose (GObject *object);
97 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
98 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
99 static gboolean gtk_file_chooser_entry_focus (GtkWidget *widget,
100 GtkDirectionType direction);
101 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
102 GdkEventFocus *event);
103 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
104 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
105 const gchar *new_text,
106 gint new_text_length,
108 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
111 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
113 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
118 static gint insert_text_callback (GtkFileChooserEntry *widget,
119 const gchar *new_text,
120 gint new_text_length,
123 static void delete_text_callback (GtkFileChooserEntry *widget,
129 static gboolean match_selected_callback (GtkEntryCompletion *completion,
132 GtkFileChooserEntry *chooser_entry);
133 static gboolean completion_match_func (GtkEntryCompletion *comp,
137 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
139 gchar *display_name);
142 REFRESH_UP_TO_CURSOR_POSITION,
146 static void refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
147 RefreshMode refresh_mode);
148 static void finished_loading_cb (GFile *file,
150 static void autocomplete (GtkFileChooserEntry *chooser_entry);
151 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
152 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
153 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
154 const gchar *feedback);
156 static GtkEditableClass *parent_editable_iface;
158 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
159 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
160 gtk_file_chooser_entry_iface_init))
163 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
165 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
166 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
167 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
169 gobject_class->finalize = gtk_file_chooser_entry_finalize;
170 gobject_class->dispose = gtk_file_chooser_entry_dispose;
172 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
173 widget_class->unmap = gtk_file_chooser_entry_unmap;
174 widget_class->focus = gtk_file_chooser_entry_focus;
175 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
177 entry_class->activate = gtk_file_chooser_entry_activate;
181 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
183 parent_editable_iface = g_type_interface_peek_parent (iface);
185 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
186 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
187 iface->set_position = gtk_file_chooser_entry_set_position;
188 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
192 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
194 GtkEntryCompletion *comp;
195 GtkCellRenderer *cell;
197 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
199 comp = gtk_entry_completion_new ();
200 gtk_entry_completion_set_popup_single_match (comp, FALSE);
202 gtk_entry_completion_set_match_func (comp,
203 completion_match_func,
207 cell = gtk_cell_renderer_text_new ();
208 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
210 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
214 g_signal_connect (comp, "match_selected",
215 G_CALLBACK (match_selected_callback), chooser_entry);
217 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
218 g_object_unref (comp);
221 g_signal_connect (chooser_entry, "insert_text",
222 G_CALLBACK (insert_text_callback), NULL);
223 g_signal_connect (chooser_entry, "delete_text",
224 G_CALLBACK (delete_text_callback), NULL);
229 gtk_file_chooser_entry_finalize (GObject *object)
231 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
233 if (chooser_entry->base_folder)
234 g_object_unref (chooser_entry->base_folder);
236 if (chooser_entry->current_folder)
237 g_object_unref (chooser_entry->current_folder);
239 if (chooser_entry->current_folder_file)
240 g_object_unref (chooser_entry->current_folder_file);
242 g_free (chooser_entry->file_part);
244 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
248 gtk_file_chooser_entry_dispose (GObject *object)
250 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
252 remove_completion_feedback (chooser_entry);
254 if (chooser_entry->start_autocompletion_idle_id != 0)
256 g_source_remove (chooser_entry->start_autocompletion_idle_id);
257 chooser_entry->start_autocompletion_idle_id = 0;
260 if (chooser_entry->completion_store)
262 g_object_unref (chooser_entry->completion_store);
263 chooser_entry->completion_store = NULL;
266 if (chooser_entry->load_folder_cancellable)
268 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
269 chooser_entry->load_folder_cancellable = NULL;
272 if (chooser_entry->current_folder)
274 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
275 G_CALLBACK (finished_loading_cb), chooser_entry);
276 g_object_unref (chooser_entry->current_folder);
277 chooser_entry->current_folder = NULL;
280 if (chooser_entry->file_system)
282 g_object_unref (chooser_entry->file_system);
283 chooser_entry->file_system = NULL;
286 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
289 /* Match functions for the GtkEntryCompletion */
291 match_selected_callback (GtkEntryCompletion *completion,
294 GtkFileChooserEntry *chooser_entry)
300 gtk_tree_model_get (model, iter,
301 DISPLAY_NAME_COLUMN, &display_name,
305 if (!display_name || !file)
307 /* these shouldn't complain if passed NULL */
308 g_object_unref (file);
309 g_free (display_name);
313 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
315 pos = chooser_entry->file_part_pos;
317 /* We don't set in_change here as we want to update the current_folder
319 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
321 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
324 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
326 g_object_unref (file);
327 g_free (display_name);
332 /* Match function for the GtkEntryCompletion */
334 completion_match_func (GtkEntryCompletion *comp,
335 const char *key_unused,
339 GtkFileChooserEntry *chooser_entry;
342 char *norm_file_part;
345 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
347 /* We ignore the key because it is the contents of the entry. Instead, we
348 * just use our precomputed file_part.
350 if (!chooser_entry->file_part)
355 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
358 return FALSE; /* Uninitialized row, ugh */
361 /* If we have an empty file_part, then we're at the root of a directory. In
362 * that case, we want to match all non-dot files. We might want to match
363 * dot_files too if show_hidden is TRUE on the fileselector in the future.
365 /* Additionally, support for gnome .hidden files would be sweet, too */
366 if (chooser_entry->file_part[0] == '\000')
378 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
379 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
381 #ifdef G_PLATFORM_WIN32
385 temp = norm_file_part;
386 norm_file_part = g_utf8_casefold (norm_file_part, -1);
390 norm_name = g_utf8_casefold (norm_name, -1);
395 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
397 g_free (norm_file_part);
405 clear_completions (GtkFileChooserEntry *chooser_entry)
407 chooser_entry->has_completion = FALSE;
408 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
410 remove_completion_feedback (chooser_entry);
414 beep (GtkFileChooserEntry *chooser_entry)
416 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
419 /* This function will append a directory separator to paths to
420 * display_name iff the path associated with it is a directory.
421 * maybe_append_separator_to_file will g_free the display_name and
422 * return a new one if needed. Otherwise, it will return the old one.
423 * You should be safe calling
425 * display_name = maybe_append_separator_to_file (entry, file, display_name);
427 * g_free (display_name);
430 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
434 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
438 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
442 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
444 gchar *tmp = display_name;
445 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
449 g_object_unref (info);
457 trim_dir_separator_suffix (const char *str)
462 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
463 return g_strndup (str, len - 1);
465 return g_strdup (str);
468 /* Determines if the completion model has entries with a common prefix relative
469 * to the current contents of the entry. Also, if there's one and only one such
470 * path, stores it in unique_path_ret.
473 find_common_prefix (GtkFileChooserEntry *chooser_entry,
474 gchar **common_prefix_ret,
475 GFile **unique_file_ret,
476 gboolean *is_complete_not_unique_ret,
477 gboolean *prefix_expands_the_file_part_ret,
480 GtkEditable *editable;
484 char *text_up_to_cursor;
485 GFile *parsed_folder_file;
486 char *parsed_file_part;
488 *common_prefix_ret = NULL;
489 *unique_file_ret = NULL;
490 *is_complete_not_unique_ret = FALSE;
491 *prefix_expands_the_file_part_ret = FALSE;
493 editable = GTK_EDITABLE (chooser_entry);
495 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
497 parsed = _gtk_file_system_parse (chooser_entry->file_system,
498 chooser_entry->base_folder,
504 g_free (text_up_to_cursor);
509 g_assert (parsed_folder_file != NULL
510 && chooser_entry->current_folder != NULL
511 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
513 g_object_unref (parsed_folder_file);
515 /* First pass: find the common prefix */
517 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
524 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
526 DISPLAY_NAME_COLUMN, &display_name,
530 if (g_str_has_prefix (display_name, parsed_file_part))
532 if (!*common_prefix_ret)
534 *common_prefix_ret = trim_dir_separator_suffix (display_name);
535 *unique_file_ret = g_object_ref (file);
539 gchar *p = *common_prefix_ret;
540 const gchar *q = display_name;
542 while (*p && *p == *q)
550 if (*unique_file_ret)
552 g_object_unref (*unique_file_ret);
553 *unique_file_ret = NULL;
558 g_free (display_name);
559 g_object_unref (file);
560 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
563 /* Second pass: see if the prefix we found is a complete match */
565 if (*common_prefix_ret != NULL)
567 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
574 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
576 DISPLAY_NAME_COLUMN, &display_name,
578 len = strlen (display_name);
581 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
584 if (strncmp (*common_prefix_ret, display_name, len) == 0)
585 *is_complete_not_unique_ret = TRUE;
587 g_free (display_name);
588 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
591 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
593 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
596 g_free (parsed_file_part);
602 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
609 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
610 if (cursor_pos < GTK_ENTRY (chooser_entry)->text_length)
614 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
615 if (G_IS_DIR_SEPARATOR (*next_char_str))
618 g_free (next_char_str);
625 INVALID_INPUT, /* what the user typed is bogus */
626 NO_MATCH, /* no matches based on what the user typed */
627 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
628 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
629 COMPLETED, /* completion inserted (ambiguous suffix) */
630 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
631 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
632 } CommonPrefixResult;
634 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
635 static CommonPrefixResult
636 append_common_prefix (GtkFileChooserEntry *chooser_entry,
638 gboolean show_errors)
640 gchar *common_prefix;
642 gboolean is_complete_not_unique;
643 gboolean prefix_expands_the_file_part;
645 CommonPrefixResult result;
646 gboolean have_result;
648 clear_completions (chooser_entry);
650 if (chooser_entry->completion_store == NULL)
654 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
658 beep (chooser_entry);
659 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
662 g_error_free (error);
664 return INVALID_INPUT;
671 if (!char_after_cursor_is_directory_separator (chooser_entry))
672 common_prefix = maybe_append_separator_to_file (chooser_entry,
676 g_object_unref (unique_file);
680 if (prefix_expands_the_file_part)
681 result = COMPLETED_UNIQUE;
683 result = NOTHING_INSERTED_UNIQUE;
686 result = INVALID_INPUT;
692 if (is_complete_not_unique)
694 result = COMPLETE_BUT_NOT_UNIQUE;
702 gint common_prefix_len;
705 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
706 common_prefix_len = g_utf8_strlen (common_prefix, -1);
708 pos = chooser_entry->file_part_pos;
710 if (prefix_expands_the_file_part)
712 chooser_entry->in_change = TRUE;
713 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
715 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
718 chooser_entry->in_change = FALSE;
722 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
724 pos); /* equivalent to cursor_pos + common_prefix_len); */
725 chooser_entry->has_completion = TRUE;
728 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
730 else if (!have_result)
732 result = NOTHING_INSERTED_COMPLETE;
736 g_free (common_prefix);
753 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
754 const gchar *new_text,
755 gint new_text_length,
758 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
762 old_text_len = GTK_ENTRY (chooser_entry)->text_length;
763 insert_pos = *position;
765 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
767 if (chooser_entry->in_change)
770 remove_completion_feedback (chooser_entry);
772 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
773 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
774 && insert_pos == old_text_len)
775 install_start_autocompletion_idle (chooser_entry);
779 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
781 if (chooser_entry->in_change)
784 clear_completions (chooser_entry);
788 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
792 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
794 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
796 clear_completions_if_not_in_change (chooser_entry);
800 gtk_file_chooser_entry_set_position (GtkEditable *editable,
803 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
805 parent_editable_iface->set_position (editable, position);
807 clear_completions_if_not_in_change (chooser_entry);
811 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
815 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
817 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
819 clear_completions_if_not_in_change (chooser_entry);
823 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
825 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
826 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
830 gtk_file_chooser_entry_unmap (GtkWidget *widget)
832 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
834 remove_completion_feedback (chooser_entry);
836 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
840 completion_feedback_window_expose_event_cb (GtkWidget *widget,
841 GdkEventExpose *event,
844 /* Stolen from gtk_tooltip_paint_window() */
846 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
848 gtk_paint_flat_box (chooser_entry->completion_feedback_window->style,
849 chooser_entry->completion_feedback_window->window,
853 chooser_entry->completion_feedback_window,
856 chooser_entry->completion_feedback_window->allocation.width,
857 chooser_entry->completion_feedback_window->allocation.height);
863 set_invisible_mouse_cursor (GdkWindow *window)
865 /* Stolen from gtkentry.c:set_invisible_cursor() */
866 /* FIXME: implement a stupid public gdk_window_set_invisible_mouse_cursor() */
868 GdkBitmap *empty_bitmap;
871 char invisible_cursor_bits[] = { 0x0 };
873 useless.red = useless.green = useless.blue = 0;
876 empty_bitmap = gdk_bitmap_create_from_data (window,
877 invisible_cursor_bits,
880 cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
885 gdk_window_set_cursor (window, cursor);
887 gdk_cursor_unref (cursor);
889 g_object_unref (empty_bitmap);
893 completion_feedback_window_realize_cb (GtkWidget *widget,
896 /* We hide the mouse cursor inside the completion feedback window, since
897 * GtkEntry hides the cursor when the user types. We don't want the cursor to
898 * come back if the completion feedback ends up where the mouse is.
900 set_invisible_mouse_cursor (widget->window);
904 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
906 /* Stolen from gtk_tooltip_init() */
908 GtkWidget *alignment;
910 chooser_entry->completion_feedback_window = gtk_window_new (GTK_WINDOW_POPUP);
911 gtk_window_set_type_hint (GTK_WINDOW (chooser_entry->completion_feedback_window),
912 GDK_WINDOW_TYPE_HINT_TOOLTIP);
913 gtk_widget_set_app_paintable (chooser_entry->completion_feedback_window, TRUE);
914 gtk_window_set_resizable (GTK_WINDOW (chooser_entry->completion_feedback_window), FALSE);
915 gtk_widget_set_name (chooser_entry->completion_feedback_window, "gtk-tooltip");
917 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
918 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
919 chooser_entry->completion_feedback_window->style->ythickness,
920 chooser_entry->completion_feedback_window->style->ythickness,
921 chooser_entry->completion_feedback_window->style->xthickness,
922 chooser_entry->completion_feedback_window->style->xthickness);
923 gtk_container_add (GTK_CONTAINER (chooser_entry->completion_feedback_window), alignment);
924 gtk_widget_show (alignment);
926 g_signal_connect (chooser_entry->completion_feedback_window, "expose_event",
927 G_CALLBACK (completion_feedback_window_expose_event_cb), chooser_entry);
928 g_signal_connect (chooser_entry->completion_feedback_window, "realize",
929 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
930 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
932 chooser_entry->completion_feedback_label = gtk_label_new (NULL);
933 gtk_container_add (GTK_CONTAINER (alignment), chooser_entry->completion_feedback_label);
934 gtk_widget_show (chooser_entry->completion_feedback_label);
938 completion_feedback_timeout_cb (gpointer data)
940 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
942 chooser_entry->completion_feedback_timeout_id = 0;
944 remove_completion_feedback (chooser_entry);
949 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
951 if (chooser_entry->completion_feedback_timeout_id != 0)
952 g_source_remove (chooser_entry->completion_feedback_timeout_id);
954 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
955 completion_feedback_timeout_cb,
959 /* Gets the x position of the text cursor in the entry, in widget coordinates */
961 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
964 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
965 * the reader, you have to implement support for the entry's scroll offset and
966 * RTL layouts and all the fancy Pango stuff.
970 gint layout_x, layout_y;
972 PangoRectangle strong_pos;
974 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
976 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
978 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
979 GTK_ENTRY (chooser_entry)->current_pos);
981 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
983 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
987 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
989 /* More or less stolen from gtk_tooltip_position() */
991 GtkRequisition feedback_req;
992 gint entry_x, entry_y;
994 GtkAllocation *entry_allocation;
995 int feedback_x, feedback_y;
997 gtk_widget_size_request (chooser_entry->completion_feedback_window, &feedback_req);
999 gdk_window_get_origin (GTK_WIDGET (chooser_entry)->window, &entry_x, &entry_y);
1000 entry_allocation = &(GTK_WIDGET (chooser_entry)->allocation);
1002 get_entry_cursor_x (chooser_entry, &cursor_x);
1004 /* FIXME: fit to the screen if we bump on the screen's edge */
1005 feedback_x = entry_x + cursor_x + entry_allocation->height / 2; /* cheap "half M-width" */
1006 feedback_y = entry_y + (entry_allocation->height - feedback_req.height) / 2;
1008 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1009 gtk_widget_show (chooser_entry->completion_feedback_window);
1011 install_completion_feedback_timer (chooser_entry);
1015 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1016 const gchar *feedback)
1018 if (chooser_entry->completion_feedback_window == NULL)
1019 create_completion_feedback_window (chooser_entry);
1021 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1023 show_completion_feedback_window (chooser_entry);
1027 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1029 if (chooser_entry->completion_feedback_window)
1030 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1032 chooser_entry->completion_feedback_window = NULL;
1033 chooser_entry->completion_feedback_label = NULL;
1035 if (chooser_entry->completion_feedback_timeout_id != 0)
1037 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1038 chooser_entry->completion_feedback_timeout_id = 0;
1043 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1045 CommonPrefixResult result;
1047 g_assert (chooser_entry->current_folder != NULL);
1048 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1050 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1052 * - If there is a common prefix, insert it (done)
1053 * - If there is no common prefix, pop up the suggestion window
1054 * - If there are no matches at all, beep and bring up a tooltip (done)
1055 * - If the suggestion window is already up, scroll it
1057 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1062 /* We already beeped in append_common_prefix(); do nothing here */
1066 beep (chooser_entry);
1067 /* translators: this text is shown when there are no completions
1068 * for something the user typed in a file chooser entry
1070 pop_up_completion_feedback (chooser_entry, _("No match"));
1073 case NOTHING_INSERTED_COMPLETE:
1074 /* FIXME: pop up the suggestion window or scroll it */
1077 case NOTHING_INSERTED_UNIQUE:
1078 /* translators: this text is shown when there is exactly one completion
1079 * for something the user typed in a file chooser entry
1081 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1088 case COMPLETED_UNIQUE:
1092 case COMPLETE_BUT_NOT_UNIQUE:
1093 /* translators: this text is shown when the text in a file chooser
1094 * entry is a complete filename, but could be continued to find
1097 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1101 g_assert_not_reached ();
1106 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1108 refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1110 if (!chooser_entry->current_folder_file)
1112 /* Here, no folder path means we couldn't parse what the user typed. */
1114 beep (chooser_entry);
1115 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
1117 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1121 if (chooser_entry->current_folder
1122 && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1124 explicitly_complete (chooser_entry);
1128 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1130 /* translators: this text is shown while the system is searching
1131 * for possible completions for text in a file chooser entry
1133 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1138 gtk_file_chooser_entry_focus (GtkWidget *widget,
1139 GtkDirectionType direction)
1141 GtkFileChooserEntry *chooser_entry;
1142 GtkEditable *editable;
1144 GdkModifierType state;
1145 gboolean control_pressed;
1147 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1148 editable = GTK_EDITABLE (widget);
1149 entry = GTK_ENTRY (widget);
1151 if (!chooser_entry->eat_tabs)
1152 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1154 control_pressed = FALSE;
1156 if (gtk_get_current_event_state (&state))
1158 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1159 control_pressed = TRUE;
1162 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1163 * makes it 'safe' for people to hit. */
1164 if ((direction == GTK_DIR_TAB_FORWARD) &&
1165 (GTK_WIDGET_HAS_FOCUS (widget)) &&
1166 (! control_pressed))
1168 if (chooser_entry->has_completion)
1169 gtk_editable_set_position (editable, entry->text_length);
1171 start_explicit_completion (chooser_entry);
1176 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
1180 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1181 GdkEventFocus *event)
1183 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1185 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1187 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1191 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1193 if (chooser_entry->has_completion)
1195 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1196 GTK_ENTRY (chooser_entry)->text_length);
1199 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1203 gtk_file_chooser_entry_activate (GtkEntry *entry)
1205 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1207 commit_completion_and_refresh (chooser_entry);
1208 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1212 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1214 if (!chooser_entry->completion_store)
1217 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1218 g_object_unref (chooser_entry->completion_store);
1219 chooser_entry->completion_store = NULL;
1222 /* Fills the completion store from the contents of the current folder */
1224 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1229 discard_completion_store (chooser_entry);
1231 files = _gtk_folder_list_children (chooser_entry->current_folder);
1233 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1237 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1242 file = tmp_list->data;
1244 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1248 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1251 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name);
1253 gtk_list_store_append (chooser_entry->completion_store, &iter);
1254 gtk_list_store_set (chooser_entry->completion_store, &iter,
1255 DISPLAY_NAME_COLUMN, display_name,
1259 g_object_unref (info);
1260 g_free (display_name);
1264 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1265 g_slist_free (files);
1267 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1268 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1270 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1271 GTK_TREE_MODEL (chooser_entry->completion_store));
1274 /* When we finish loading the current folder, this function should get called to
1275 * perform the deferred autocompletion or explicit completion.
1278 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1280 switch (chooser_entry->load_complete_action)
1282 case LOAD_COMPLETE_NOTHING:
1285 case LOAD_COMPLETE_AUTOCOMPLETE:
1286 autocomplete (chooser_entry);
1289 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1290 explicitly_complete (chooser_entry);
1294 g_assert_not_reached ();
1297 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1301 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1303 populate_completion_store (chooser_entry);
1304 perform_load_complete_action (chooser_entry);
1306 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1309 /* Callback when the current folder finishes loading */
1311 finished_loading_cb (GFile *file,
1314 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1316 finish_folder_load (chooser_entry);
1319 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1321 load_directory_get_folder_callback (GCancellable *cancellable,
1323 const GError *error,
1326 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1327 GtkFileChooserEntry *chooser_entry = data;
1329 if (cancellable != chooser_entry->load_folder_cancellable)
1332 chooser_entry->load_folder_cancellable = NULL;
1336 LoadCompleteAction old_load_complete_action;
1338 old_load_complete_action = chooser_entry->load_complete_action;
1340 clear_completions (chooser_entry);
1342 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1344 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1346 beep (chooser_entry);
1347 pop_up_completion_feedback (chooser_entry, error->message);
1350 if (chooser_entry->current_folder)
1352 g_object_unref (chooser_entry->current_folder);
1353 chooser_entry->current_folder = NULL;
1357 if (cancelled || error)
1360 g_assert (folder != NULL);
1361 chooser_entry->current_folder = g_object_ref (folder);
1363 discard_completion_store (chooser_entry);
1365 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1366 finish_folder_load (chooser_entry);
1368 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1369 G_CALLBACK (finished_loading_cb), chooser_entry);
1372 g_object_unref (chooser_entry);
1373 g_object_unref (cancellable);
1377 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1379 if (chooser_entry->current_folder_file == NULL ||
1380 chooser_entry->file_system == NULL)
1383 g_assert (chooser_entry->current_folder == NULL);
1384 g_assert (chooser_entry->load_folder_cancellable == NULL);
1386 chooser_entry->load_folder_cancellable =
1387 _gtk_file_system_get_folder (chooser_entry->file_system,
1388 chooser_entry->current_folder_file,
1389 "standard::name,standard::display-name,standard::type",
1390 load_directory_get_folder_callback,
1391 g_object_ref (chooser_entry));
1395 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1397 gboolean force_reload)
1399 gboolean reload = FALSE;
1401 if (chooser_entry->current_folder_file)
1403 if ((folder_file && !g_file_equal (folder_file, chooser_entry->current_folder_file))
1408 /* We changed our current directory. We need to clear out the old
1409 * directory information.
1411 if (chooser_entry->current_folder)
1413 if (chooser_entry->load_folder_cancellable)
1415 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
1416 chooser_entry->load_folder_cancellable = NULL;
1419 g_object_unref (chooser_entry->current_folder);
1420 chooser_entry->current_folder = NULL;
1423 g_object_unref (chooser_entry->current_folder_file);
1424 chooser_entry->current_folder_file = g_object_ref (folder_file);
1429 chooser_entry->current_folder_file = (folder_file) ? g_object_ref (folder_file) : NULL;
1434 start_loading_current_folder (chooser_entry);
1438 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1439 RefreshMode refresh_mode)
1441 GtkEditable *editable;
1446 gsize total_len, file_part_len;
1449 editable = GTK_EDITABLE (chooser_entry);
1451 switch (refresh_mode)
1453 case REFRESH_UP_TO_CURSOR_POSITION:
1454 end_pos = gtk_editable_get_position (editable);
1457 case REFRESH_WHOLE_TEXT:
1458 end_pos = GTK_ENTRY (chooser_entry)->text_length;
1462 g_assert_not_reached ();
1466 text = gtk_editable_get_chars (editable, 0, end_pos);
1468 if (!chooser_entry->file_system ||
1469 !chooser_entry->base_folder ||
1470 !_gtk_file_system_parse (chooser_entry->file_system,
1471 chooser_entry->base_folder, text,
1472 &folder_file, &file_part, NULL)) /* NULL-GError */
1474 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1475 file_part = g_strdup ("");
1480 file_part_len = strlen (file_part);
1481 total_len = strlen (text);
1482 if (total_len > file_part_len)
1483 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1490 g_free (chooser_entry->file_part);
1492 chooser_entry->file_part = file_part;
1493 chooser_entry->file_part_pos = file_part_pos;
1495 reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1498 g_object_unref (folder_file);
1502 autocomplete (GtkFileChooserEntry *chooser_entry)
1504 g_assert (chooser_entry->current_folder != NULL);
1505 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1506 g_assert (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length);
1508 append_common_prefix (chooser_entry, TRUE, FALSE);
1512 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1514 refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1516 if (!chooser_entry->current_folder)
1518 /* We don't beep or anything, since this is autocompletion - the user
1519 * didn't request any action explicitly.
1524 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1525 autocomplete (chooser_entry);
1527 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1531 start_autocompletion_idle_handler (gpointer data)
1533 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1535 start_autocompletion (chooser_entry);
1537 chooser_entry->start_autocompletion_idle_id = 0;
1543 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1545 if (chooser_entry->start_autocompletion_idle_id != 0)
1548 chooser_entry->start_autocompletion_idle_id = g_idle_add (start_autocompletion_idle_handler, chooser_entry);
1553 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1554 const gchar *new_text,
1555 gint new_text_length,
1559 const gchar *colon = memchr (new_text, ':', new_text_length);
1562 /* Disallow these characters altogether */
1563 for (i = 0; i < new_text_length; i++)
1565 if (new_text[i] == '<' ||
1566 new_text[i] == '>' ||
1567 new_text[i] == '"' ||
1568 new_text[i] == '|' ||
1569 new_text[i] == '*' ||
1574 if (i < new_text_length ||
1575 /* Disallow entering text that would cause a colon to be anywhere except
1576 * after a drive letter.
1579 *position + (colon - new_text) != 1) ||
1580 (new_text_length > 0 &&
1582 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1583 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1585 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1586 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1594 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1599 /* If deleting a drive letter, delete the colon, too */
1600 if (start_pos == 0 && end_pos == 1 &&
1601 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1602 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1604 g_signal_handlers_block_by_func (chooser_entry,
1605 G_CALLBACK (delete_text_callback),
1607 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1608 g_signal_handlers_unblock_by_func (chooser_entry,
1609 G_CALLBACK (delete_text_callback),
1616 * _gtk_file_chooser_entry_new:
1617 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1619 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1620 * is an internal implementation widget for the GTK+ file chooser
1621 * which is an entry with completion with respect to a
1622 * #GtkFileSystem object.
1624 * Return value: the newly created #GtkFileChooserEntry
1627 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1629 GtkFileChooserEntry *chooser_entry;
1631 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1632 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1634 return GTK_WIDGET (chooser_entry);
1638 * _gtk_file_chooser_entry_set_file_system:
1639 * @chooser_entry: a #GtkFileChooser
1640 * @file_system: an object implementing #GtkFileSystem
1642 * Sets the file system for @chooser_entry.
1645 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1646 GtkFileSystem *file_system)
1648 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1649 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1651 if (file_system != chooser_entry->file_system)
1653 if (chooser_entry->file_system)
1654 g_object_unref (chooser_entry->file_system);
1656 chooser_entry->file_system = g_object_ref (file_system);
1661 * _gtk_file_chooser_entry_set_base_folder:
1662 * @chooser_entry: a #GtkFileChooserEntry
1663 * @file: file for a folder in the chooser entries current file system.
1665 * Sets the folder with respect to which completions occur.
1668 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1671 if (chooser_entry->base_folder)
1672 g_object_unref (chooser_entry->base_folder);
1674 chooser_entry->base_folder = file;
1676 if (chooser_entry->base_folder)
1677 g_object_ref (chooser_entry->base_folder);
1679 clear_completions (chooser_entry);
1680 _gtk_file_chooser_entry_select_filename (chooser_entry);
1684 * _gtk_file_chooser_entry_get_current_folder:
1685 * @chooser_entry: a #GtkFileChooserEntry
1687 * Gets the current folder for the #GtkFileChooserEntry. If the
1688 * user has only entered a filename, this will be the base folder
1689 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1690 * user has entered a relative or absolute path, then it will
1691 * be different. If the user has entered a relative or absolute
1692 * path that doesn't point to a folder in the file system, it will
1695 * Return value: the file for the current folder - this value is owned by the
1696 * chooser entry and must not be modified or freed.
1699 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1701 commit_completion_and_refresh (chooser_entry);
1702 return chooser_entry->current_folder_file;
1706 * _gtk_file_chooser_entry_get_file_part:
1707 * @chooser_entry: a #GtkFileChooserEntry
1709 * Gets the non-folder portion of whatever the user has entered
1710 * into the file selector. What is returned is a UTF-8 string,
1711 * and if a filename path is needed, g_file_get_child_for_display_name()
1714 * Return value: the entered filename - this value is owned by the
1715 * chooser entry and must not be modified or freed.
1718 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1720 commit_completion_and_refresh (chooser_entry);
1721 return chooser_entry->file_part;
1725 * _gtk_file_chooser_entry_set_file_part:
1726 * @chooser_entry: a #GtkFileChooserEntry
1727 * @file_part: text to display in the entry, in UTF-8
1729 * Sets the current text shown in the file chooser entry.
1732 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1733 const gchar *file_part)
1735 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1737 chooser_entry->in_change = TRUE;
1738 clear_completions (chooser_entry);
1739 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1740 chooser_entry->in_change = FALSE;
1745 * _gtk_file_chooser_entry_set_action:
1746 * @chooser_entry: a #GtkFileChooserEntry
1747 * @action: the action which is performed by the file selector using this entry
1749 * Sets action which is performed by the file selector using this entry.
1750 * The #GtkFileChooserEntry will use different completion strategies for
1751 * different actions.
1754 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1755 GtkFileChooserAction action)
1757 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1759 if (chooser_entry->action != action)
1761 GtkEntryCompletion *comp;
1763 chooser_entry->action = action;
1765 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1767 /* FIXME: do we need to actually set the following? */
1771 case GTK_FILE_CHOOSER_ACTION_OPEN:
1772 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1773 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1775 case GTK_FILE_CHOOSER_ACTION_SAVE:
1776 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1777 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1785 * _gtk_file_chooser_entry_get_action:
1786 * @chooser_entry: a #GtkFileChooserEntry
1788 * Gets the action for this entry.
1790 * Returns: the action
1792 GtkFileChooserAction
1793 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1795 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1796 GTK_FILE_CHOOSER_ACTION_OPEN);
1798 return chooser_entry->action;
1802 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1805 gboolean retval = FALSE;
1807 if (chooser_entry->current_folder)
1809 GFileInfo *file_info;
1811 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1815 retval = (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY);
1816 g_object_unref (file_info);
1825 * _gtk_file_chooser_entry_select_filename:
1826 * @chooser_entry: a #GtkFileChooserEntry
1828 * Selects the filename (without the extension) for user edition.
1831 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1833 const gchar *str, *ext;
1836 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1838 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1839 ext = g_strrstr (str, ".");
1842 len = g_utf8_pointer_to_offset (str, ext);
1845 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);