1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserentry.c: Entry with filename completion
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gtkfilechooserentry.h"
27 #include "gtkcelllayout.h"
28 #include "gtkcellrenderertext.h"
32 #include "gtksizerequest.h"
33 #include "gtkwindow.h"
36 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
38 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
39 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
40 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
42 struct _GtkFileChooserEntryClass
44 GtkEntryClass parent_class;
47 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
49 LOAD_COMPLETE_NOTHING,
50 LOAD_COMPLETE_AUTOCOMPLETE,
51 LOAD_COMPLETE_EXPLICIT_COMPLETION
57 REFRESH_INVALID_INPUT,
58 REFRESH_INCOMPLETE_HOSTNAME,
63 struct _GtkFileChooserEntry
65 GtkEntry parent_instance;
67 GtkFileChooserAction action;
69 GtkFileSystem *file_system;
71 GFile *current_folder_file;
75 /* Folder being loaded or already loaded */
76 GtkFolder *current_folder;
77 GCancellable *load_folder_cancellable;
79 LoadCompleteAction load_complete_action;
81 GtkListStore *completion_store;
83 guint start_autocompletion_idle_id;
85 GtkWidget *completion_feedback_window;
86 GtkWidget *completion_feedback_label;
87 guint completion_feedback_timeout_id;
89 guint current_folder_loaded : 1;
90 guint has_completion : 1;
103 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
105 static void gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface);
107 static void gtk_file_chooser_entry_finalize (GObject *object);
108 static void gtk_file_chooser_entry_dispose (GObject *object);
109 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
110 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
111 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
113 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
114 GdkEventFocus *event);
115 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
116 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
117 const gchar *new_text,
118 gint new_text_length,
120 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
123 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
125 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
130 static gint insert_text_callback (GtkFileChooserEntry *widget,
131 const gchar *new_text,
132 gint new_text_length,
135 static void delete_text_callback (GtkFileChooserEntry *widget,
141 static gboolean match_selected_callback (GtkEntryCompletion *completion,
144 GtkFileChooserEntry *chooser_entry);
145 static gboolean completion_match_func (GtkEntryCompletion *comp,
149 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
155 REFRESH_UP_TO_CURSOR_POSITION,
159 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
160 RefreshMode refresh_mode);
161 static void finished_loading_cb (GtkFolder *folder,
163 static void autocomplete (GtkFileChooserEntry *chooser_entry);
164 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
165 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
166 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
167 const gchar *feedback);
169 static GtkEditableInterface *parent_editable_iface;
171 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
172 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
173 gtk_file_chooser_entry_iface_init))
176 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
180 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
182 gobject_class->finalize = gtk_file_chooser_entry_finalize;
183 gobject_class->dispose = gtk_file_chooser_entry_dispose;
185 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
186 widget_class->unmap = gtk_file_chooser_entry_unmap;
187 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
188 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
190 entry_class->activate = gtk_file_chooser_entry_activate;
194 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
196 parent_editable_iface = g_type_interface_peek_parent (iface);
198 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
199 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
200 iface->set_position = gtk_file_chooser_entry_set_position;
201 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
205 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
207 GtkEntryCompletion *comp;
208 GtkCellRenderer *cell;
210 chooser_entry->local_only = TRUE;
212 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
214 comp = gtk_entry_completion_new ();
215 gtk_entry_completion_set_popup_single_match (comp, FALSE);
217 gtk_entry_completion_set_match_func (comp,
218 completion_match_func,
222 cell = gtk_cell_renderer_text_new ();
223 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
225 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
227 "text", DISPLAY_NAME_COLUMN);
229 g_signal_connect (comp, "match-selected",
230 G_CALLBACK (match_selected_callback), chooser_entry);
232 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
233 g_object_unref (comp);
236 g_signal_connect (chooser_entry, "insert-text",
237 G_CALLBACK (insert_text_callback), NULL);
238 g_signal_connect (chooser_entry, "delete-text",
239 G_CALLBACK (delete_text_callback), NULL);
244 gtk_file_chooser_entry_finalize (GObject *object)
246 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
248 if (chooser_entry->base_folder)
249 g_object_unref (chooser_entry->base_folder);
251 if (chooser_entry->current_folder)
252 g_object_unref (chooser_entry->current_folder);
254 if (chooser_entry->current_folder_file)
255 g_object_unref (chooser_entry->current_folder_file);
257 g_free (chooser_entry->file_part);
259 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
263 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
265 if (chooser_entry->current_folder)
267 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
268 G_CALLBACK (finished_loading_cb), chooser_entry);
269 g_object_unref (chooser_entry->current_folder);
270 chooser_entry->current_folder = NULL;
272 if (chooser_entry->load_folder_cancellable)
274 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
275 chooser_entry->load_folder_cancellable = NULL;
278 if (chooser_entry->current_folder_file)
280 g_object_unref (chooser_entry->current_folder_file);
281 chooser_entry->current_folder_file = NULL;
286 gtk_file_chooser_entry_dispose (GObject *object)
288 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
290 remove_completion_feedback (chooser_entry);
291 discard_loading_and_current_folder_file (chooser_entry);
293 if (chooser_entry->start_autocompletion_idle_id != 0)
295 g_source_remove (chooser_entry->start_autocompletion_idle_id);
296 chooser_entry->start_autocompletion_idle_id = 0;
299 if (chooser_entry->completion_store)
301 g_object_unref (chooser_entry->completion_store);
302 chooser_entry->completion_store = NULL;
305 if (chooser_entry->file_system)
307 g_object_unref (chooser_entry->file_system);
308 chooser_entry->file_system = NULL;
311 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
314 /* Match functions for the GtkEntryCompletion */
316 match_selected_callback (GtkEntryCompletion *completion,
319 GtkFileChooserEntry *chooser_entry)
326 gtk_tree_model_get (model, iter,
327 DISPLAY_NAME_COLUMN, &display_name,
331 if (!display_name || !file)
334 g_object_unref (file);
336 g_free (display_name);
340 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
342 pos = chooser_entry->file_part_pos;
344 /* We don't set in_change here as we want to update the current_folder
346 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
348 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
351 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
353 g_object_unref (file);
354 g_free (display_name);
359 /* Match function for the GtkEntryCompletion */
361 completion_match_func (GtkEntryCompletion *comp,
362 const char *key_unused,
366 GtkFileChooserEntry *chooser_entry;
369 char *norm_file_part;
372 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
374 /* We ignore the key because it is the contents of the entry. Instead, we
375 * just use our precomputed file_part.
377 if (!chooser_entry->file_part)
382 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
385 return FALSE; /* Uninitialized row, ugh */
388 /* If we have an empty file_part, then we're at the root of a directory. In
389 * that case, we want to match all non-dot files. We might want to match
390 * dot_files too if show_hidden is TRUE on the fileselector in the future.
392 /* Additionally, support for gnome .hidden files would be sweet, too */
393 if (chooser_entry->file_part[0] == '\000')
405 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
406 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
408 #ifdef G_PLATFORM_WIN32
412 temp = norm_file_part;
413 norm_file_part = g_utf8_casefold (norm_file_part, -1);
417 norm_name = g_utf8_casefold (norm_name, -1);
422 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
424 g_free (norm_file_part);
432 clear_completions (GtkFileChooserEntry *chooser_entry)
434 chooser_entry->has_completion = FALSE;
435 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
437 remove_completion_feedback (chooser_entry);
441 beep (GtkFileChooserEntry *chooser_entry)
443 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
446 /* This function will append a directory separator to paths to
447 * display_name iff the path associated with it is a directory.
448 * maybe_append_separator_to_file will g_free the display_name and
449 * return a new one if needed. Otherwise, it will return the old one.
450 * You should be safe calling
452 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
454 * g_free (display_name);
457 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
464 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
468 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
472 if (_gtk_file_info_consider_as_directory (info))
474 gchar *tmp = display_name;
475 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
480 g_object_unref (info);
487 /* Determines if the completion model has entries with a common prefix relative
488 * to the current contents of the entry. Also, if there's one and only one such
489 * path, stores it in unique_path_ret.
492 find_common_prefix (GtkFileChooserEntry *chooser_entry,
493 gchar **common_prefix_ret,
494 GFile **unique_file_ret,
495 gboolean *is_complete_not_unique_ret,
496 gboolean *prefix_expands_the_file_part_ret,
499 GtkEditable *editable;
503 char *text_up_to_cursor;
504 GFile *parsed_folder_file;
505 char *parsed_file_part;
507 *common_prefix_ret = NULL;
508 *unique_file_ret = NULL;
509 *is_complete_not_unique_ret = FALSE;
510 *prefix_expands_the_file_part_ret = FALSE;
512 editable = GTK_EDITABLE (chooser_entry);
514 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
516 parsed = _gtk_file_system_parse (chooser_entry->file_system,
517 chooser_entry->base_folder,
523 g_free (text_up_to_cursor);
528 g_assert (parsed_folder_file != NULL
529 && chooser_entry->current_folder != NULL
530 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
532 g_object_unref (parsed_folder_file);
534 /* First pass: find the common prefix */
536 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
543 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
545 DISPLAY_NAME_COLUMN, &display_name,
549 if (g_str_has_prefix (display_name, parsed_file_part))
551 if (!*common_prefix_ret)
553 *common_prefix_ret = g_strdup (display_name);
554 *unique_file_ret = g_object_ref (file);
558 gchar *p = *common_prefix_ret;
559 const gchar *q = display_name;
561 while (*p && *p == *q)
569 if (*unique_file_ret)
571 g_object_unref (*unique_file_ret);
572 *unique_file_ret = NULL;
577 g_free (display_name);
578 g_object_unref (file);
579 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
582 /* Second pass: see if the prefix we found is a complete match */
584 if (*common_prefix_ret != NULL)
586 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
593 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
595 DISPLAY_NAME_COLUMN, &display_name,
597 len = strlen (display_name);
600 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
603 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
604 *is_complete_not_unique_ret = TRUE;
606 g_free (display_name);
607 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
610 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
612 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
615 g_free (parsed_file_part);
621 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
628 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
629 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
633 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
634 if (G_IS_DIR_SEPARATOR (*next_char_str))
637 g_free (next_char_str);
644 INVALID_INPUT, /* what the user typed is bogus */
645 NO_MATCH, /* no matches based on what the user typed */
646 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
647 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
648 COMPLETED, /* completion inserted (ambiguous suffix) */
649 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
650 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
651 } CommonPrefixResult;
653 /* Finds a common prefix based on the contents of the entry
654 * and mandatorily appends it
656 static CommonPrefixResult
657 append_common_prefix (GtkFileChooserEntry *chooser_entry,
659 gboolean show_errors)
661 gchar *common_prefix;
663 gboolean is_complete_not_unique;
664 gboolean prefix_expands_the_file_part;
666 CommonPrefixResult result = NO_MATCH;
667 gboolean have_result;
669 clear_completions (chooser_entry);
671 if (chooser_entry->completion_store == NULL)
675 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
677 /* If the user types an incomplete hostname ("http://foo" without
678 * a slash after that), it's not an error. We just don't want to
679 * pop up a meaningless completion window in that state.
681 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
684 beep (chooser_entry);
685 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
688 g_error_free (error);
690 return INVALID_INPUT;
697 if (!char_after_cursor_is_directory_separator (chooser_entry))
701 common_prefix = maybe_append_separator_to_file (chooser_entry,
706 prefix_expands_the_file_part = TRUE;
709 g_object_unref (unique_file);
711 if (prefix_expands_the_file_part)
712 result = COMPLETED_UNIQUE;
714 result = NOTHING_INSERTED_UNIQUE;
720 if (is_complete_not_unique)
722 result = COMPLETE_BUT_NOT_UNIQUE;
732 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
734 pos = chooser_entry->file_part_pos;
736 if (prefix_expands_the_file_part)
738 chooser_entry->in_change = TRUE;
739 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
741 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
744 chooser_entry->in_change = FALSE;
748 /* equivalent to cursor_pos + common_prefix_len); */
749 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
752 chooser_entry->has_completion = TRUE;
755 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
757 else if (!have_result)
759 result = NOTHING_INSERTED_COMPLETE;
763 g_free (common_prefix);
780 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
781 const gchar *new_text,
782 gint new_text_length,
785 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
789 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
790 insert_pos = *position;
792 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
794 if (chooser_entry->in_change)
797 remove_completion_feedback (chooser_entry);
799 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
800 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
801 && insert_pos == old_text_len)
802 install_start_autocompletion_idle (chooser_entry);
806 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
808 if (chooser_entry->in_change)
811 clear_completions (chooser_entry);
815 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
819 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
821 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
823 clear_completions_if_not_in_change (chooser_entry);
827 gtk_file_chooser_entry_set_position (GtkEditable *editable,
830 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
832 parent_editable_iface->set_position (editable, position);
834 clear_completions_if_not_in_change (chooser_entry);
838 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
842 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
844 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
846 clear_completions_if_not_in_change (chooser_entry);
850 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
852 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
853 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
857 gtk_file_chooser_entry_unmap (GtkWidget *widget)
859 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
861 remove_completion_feedback (chooser_entry);
863 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
867 completion_feedback_window_draw_cb (GtkWidget *widget,
871 /* Stolen from gtk_tooltip_paint_window() */
873 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
874 GtkStyleContext *context;
876 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
878 gtk_render_background (context, cr, 0, 0,
879 gtk_widget_get_allocated_width (widget),
880 gtk_widget_get_allocated_height (widget));
881 gtk_render_frame (context, cr, 0, 0,
882 gtk_widget_get_allocated_width (widget),
883 gtk_widget_get_allocated_height (widget));
889 set_invisible_mouse_cursor (GdkWindow *window)
894 display = gdk_window_get_display (window);
895 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
897 gdk_window_set_cursor (window, cursor);
899 g_object_unref (cursor);
903 completion_feedback_window_realize_cb (GtkWidget *widget,
906 /* We hide the mouse cursor inside the completion feedback window, since
907 * GtkEntry hides the cursor when the user types. We don't want the cursor to
908 * come back if the completion feedback ends up where the mouse is.
910 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
914 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
916 /* Stolen from gtk_tooltip_init() */
917 GtkWidget *window, *label;
918 GtkStyleContext *context;
920 window = gtk_window_new (GTK_WINDOW_POPUP);
921 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
922 gtk_widget_set_app_paintable (window, TRUE);
923 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
924 gtk_widget_set_name (window, "gtk-tooltip");
926 context = gtk_widget_get_style_context (window);
927 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
929 g_signal_connect (window, "draw",
930 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
931 g_signal_connect (window, "realize",
932 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
933 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
935 label = gtk_label_new (NULL);
936 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
937 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
938 /* FIXME: don't hardcode this */
939 gtk_widget_set_margin_left (label, 6);
940 gtk_widget_set_margin_right (label, 6);
941 gtk_widget_set_margin_top (label, 6);
942 gtk_widget_set_margin_bottom (label, 6);
943 gtk_container_add (GTK_CONTAINER (window), label);
944 gtk_widget_show (label);
946 chooser_entry->completion_feedback_window = window;
947 chooser_entry->completion_feedback_label = label;
951 completion_feedback_timeout_cb (gpointer data)
953 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
955 chooser_entry->completion_feedback_timeout_id = 0;
957 remove_completion_feedback (chooser_entry);
962 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
964 if (chooser_entry->completion_feedback_timeout_id != 0)
965 g_source_remove (chooser_entry->completion_feedback_timeout_id);
967 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
968 completion_feedback_timeout_cb,
972 /* Gets the x position of the text cursor in the entry, in widget coordinates */
974 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
977 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
978 * the reader, you have to implement support for the entry's scroll offset and
979 * RTL layouts and all the fancy Pango stuff.
983 gint layout_x, layout_y;
985 PangoRectangle strong_pos;
986 gint start_pos, end_pos;
988 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
990 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
992 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
993 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
997 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
999 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1003 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1005 /* More or less stolen from gtk_tooltip_position() */
1007 GtkRequisition feedback_req;
1008 GtkWidget *widget = GTK_WIDGET (chooser_entry);
1009 gint entry_x, entry_y;
1011 GtkAllocation entry_allocation;
1012 int feedback_x, feedback_y;
1014 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
1015 &feedback_req, NULL);
1017 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1018 gtk_widget_get_allocation (widget, &entry_allocation);
1020 get_entry_cursor_x (chooser_entry, &cursor_x);
1022 /* FIXME: fit to the screen if we bump on the screen's edge */
1023 /* cheap "half M-width", use height as approximation of character em-size */
1024 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1025 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1027 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1028 gtk_widget_show (chooser_entry->completion_feedback_window);
1030 install_completion_feedback_timer (chooser_entry);
1034 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1035 const gchar *feedback)
1037 if (chooser_entry->completion_feedback_window == NULL)
1038 create_completion_feedback_window (chooser_entry);
1040 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1042 show_completion_feedback_window (chooser_entry);
1046 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1048 if (chooser_entry->completion_feedback_window)
1049 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1051 chooser_entry->completion_feedback_window = NULL;
1052 chooser_entry->completion_feedback_label = NULL;
1054 if (chooser_entry->completion_feedback_timeout_id != 0)
1056 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1057 chooser_entry->completion_feedback_timeout_id = 0;
1062 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1064 CommonPrefixResult result;
1066 g_assert (chooser_entry->current_folder != NULL);
1067 g_assert (chooser_entry->current_folder_loaded);
1069 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1071 * - If there is a common prefix, insert it (done)
1072 * - If there is no common prefix, pop up the suggestion window
1073 * - If there are no matches at all, beep and bring up a tooltip (done)
1074 * - If the suggestion window is already up, scroll it
1076 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1081 /* We already beeped in append_common_prefix(); do nothing here */
1085 beep (chooser_entry);
1086 /* translators: this text is shown when there are no completions
1087 * for something the user typed in a file chooser entry
1089 pop_up_completion_feedback (chooser_entry, _("No match"));
1092 case NOTHING_INSERTED_COMPLETE:
1093 /* FIXME: pop up the suggestion window or scroll it */
1096 case NOTHING_INSERTED_UNIQUE:
1097 /* translators: this text is shown when there is exactly one completion
1098 * for something the user typed in a file chooser entry
1100 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1107 case COMPLETED_UNIQUE:
1111 case COMPLETE_BUT_NOT_UNIQUE:
1112 /* translators: this text is shown when the text in a file chooser
1113 * entry is a complete filename, but could be continued to find
1116 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1120 g_assert_not_reached ();
1125 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1127 RefreshStatus status;
1131 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1138 g_assert (chooser_entry->current_folder_file != NULL);
1140 if (chooser_entry->current_folder && chooser_entry->current_folder_loaded)
1141 explicitly_complete (chooser_entry);
1144 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1146 /* Translators: this text is shown while the system is searching
1147 * for possible completions for filenames in a file chooser entry. */
1148 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1153 case REFRESH_INVALID_INPUT:
1155 /* Translators: this is shown in the feedback for Tab-completion in a file
1156 * chooser's text entry, when the user enters an invalid path. */
1157 feedback_msg = _("Invalid path");
1160 case REFRESH_INCOMPLETE_HOSTNAME:
1163 if (chooser_entry->local_only)
1165 /* hostnames in a local_only file chooser? user error */
1167 /* Translators: this is shown in the feedback for Tab-completion in a
1168 * file chooser's text entry when the user enters something like
1169 * "sftp://blahblah" in an app that only supports local filenames. */
1170 feedback_msg = _("Only local files may be selected");
1174 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1176 /* Translators: this is shown in the feedback for Tab-completion in a
1177 * file chooser's text entry when the user hasn't entered the first '/'
1178 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1179 feedback_msg = _("Incomplete hostname; end it with '/'");
1184 case REFRESH_NONEXISTENT:
1187 /* Translators: this is shown in the feedback for Tab-completion in a file
1188 * chooser's text entry when the user enters a path that does not exist
1189 * and then hits Tab */
1190 feedback_msg = _("Path does not exist");
1193 case REFRESH_NOT_LOCAL:
1195 feedback_msg = _("Only local files may be selected");
1199 g_assert_not_reached ();
1205 g_assert (chooser_entry->current_folder_file == NULL);
1207 beep (chooser_entry);
1208 pop_up_completion_feedback (chooser_entry, feedback_msg);
1209 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1214 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1217 GtkFileChooserEntry *chooser_entry;
1218 GtkEditable *editable;
1220 GdkModifierType state;
1221 gboolean control_pressed;
1223 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1224 editable = GTK_EDITABLE (widget);
1225 entry = GTK_ENTRY (widget);
1227 if (!chooser_entry->eat_tabs)
1228 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1230 control_pressed = FALSE;
1232 if (gtk_get_current_event_state (&state))
1234 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1235 control_pressed = TRUE;
1238 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1239 * makes it 'safe' for people to hit. */
1240 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1242 if (chooser_entry->has_completion)
1243 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1245 start_explicit_completion (chooser_entry);
1250 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1255 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1256 GdkEventFocus *event)
1258 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1260 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1262 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1266 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1268 if (chooser_entry->has_completion)
1270 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1271 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1274 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1275 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1279 gtk_file_chooser_entry_activate (GtkEntry *entry)
1281 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1283 commit_completion_and_refresh (chooser_entry);
1284 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1288 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1290 if (!chooser_entry->completion_store)
1293 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1294 g_object_unref (chooser_entry->completion_store);
1295 chooser_entry->completion_store = NULL;
1298 /* Fills the completion store from the contents of the current folder */
1300 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1305 discard_completion_store (chooser_entry);
1307 files = _gtk_folder_list_children (chooser_entry->current_folder);
1309 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1313 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1318 file = tmp_list->data;
1320 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1324 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1328 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1330 gtk_list_store_append (chooser_entry->completion_store, &iter);
1331 gtk_list_store_set (chooser_entry->completion_store, &iter,
1332 DISPLAY_NAME_COLUMN, display_name,
1336 g_object_unref (info);
1337 g_free (display_name);
1341 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1342 g_slist_free (files);
1344 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1345 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1347 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1348 GTK_TREE_MODEL (chooser_entry->completion_store));
1351 /* When we finish loading the current folder, this function should get called to
1352 * perform the deferred autocompletion or explicit completion.
1355 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1357 switch (chooser_entry->load_complete_action)
1359 case LOAD_COMPLETE_NOTHING:
1362 case LOAD_COMPLETE_AUTOCOMPLETE:
1363 autocomplete (chooser_entry);
1366 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1367 explicitly_complete (chooser_entry);
1371 g_assert_not_reached ();
1374 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1378 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1380 populate_completion_store (chooser_entry);
1381 perform_load_complete_action (chooser_entry);
1383 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1386 /* Callback when the current folder finishes loading */
1388 finished_loading_cb (GtkFolder *folder,
1391 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1393 chooser_entry->current_folder_loaded = TRUE;
1394 finish_folder_load (chooser_entry);
1397 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1399 load_directory_get_folder_callback (GCancellable *cancellable,
1401 const GError *error,
1404 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1405 GtkFileChooserEntry *chooser_entry = data;
1407 if (cancellable != chooser_entry->load_folder_cancellable)
1410 chooser_entry->load_folder_cancellable = NULL;
1414 LoadCompleteAction old_load_complete_action;
1416 old_load_complete_action = chooser_entry->load_complete_action;
1418 discard_completion_store (chooser_entry);
1419 clear_completions (chooser_entry);
1421 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1423 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1425 beep (chooser_entry);
1426 pop_up_completion_feedback (chooser_entry, error->message);
1430 if (cancelled || error)
1433 g_assert (folder != NULL);
1434 chooser_entry->current_folder = g_object_ref (folder);
1436 discard_completion_store (chooser_entry);
1438 chooser_entry->current_folder_loaded = _gtk_folder_is_finished_loading (chooser_entry->current_folder);
1439 if (chooser_entry->current_folder_loaded)
1440 finish_folder_load (chooser_entry);
1442 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1443 G_CALLBACK (finished_loading_cb), chooser_entry);
1446 g_object_unref (chooser_entry);
1447 g_object_unref (cancellable);
1450 static RefreshStatus
1451 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1454 g_assert (folder_file != NULL);
1456 if (chooser_entry->current_folder_file
1457 && g_file_equal (folder_file, chooser_entry->current_folder_file)
1458 && chooser_entry->load_folder_cancellable)
1461 if (chooser_entry->current_folder_file)
1463 discard_loading_and_current_folder_file (chooser_entry);
1466 if (chooser_entry->local_only
1467 && !g_file_is_native (folder_file))
1468 return REFRESH_NOT_LOCAL;
1470 chooser_entry->current_folder_file = g_object_ref (folder_file);
1472 chooser_entry->load_folder_cancellable =
1473 _gtk_file_system_get_folder (chooser_entry->file_system,
1474 chooser_entry->current_folder_file,
1475 "standard::name,standard::display-name,standard::type",
1476 load_directory_get_folder_callback,
1477 g_object_ref (chooser_entry));
1482 static RefreshStatus
1483 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1484 RefreshMode refresh_mode)
1486 GtkEditable *editable;
1491 gsize total_len, file_part_len;
1494 RefreshStatus result;
1496 editable = GTK_EDITABLE (chooser_entry);
1498 switch (refresh_mode)
1500 case REFRESH_UP_TO_CURSOR_POSITION:
1501 end_pos = gtk_editable_get_position (editable);
1504 case REFRESH_WHOLE_TEXT:
1505 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1509 g_assert_not_reached ();
1510 return REFRESH_INVALID_INPUT;
1513 text = gtk_editable_get_chars (editable, 0, end_pos);
1516 if (!_gtk_file_system_parse (chooser_entry->file_system,
1517 chooser_entry->base_folder, text,
1518 &folder_file, &file_part, &error))
1520 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1523 result = REFRESH_INCOMPLETE_HOSTNAME;
1527 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1529 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1530 result = REFRESH_NONEXISTENT;
1532 result = REFRESH_INVALID_INPUT;
1536 g_error_free (error);
1538 file_part = g_strdup ("");
1543 g_assert (folder_file != NULL);
1545 file_part_len = strlen (file_part);
1546 total_len = strlen (text);
1547 if (total_len > file_part_len)
1548 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1552 result = REFRESH_OK;
1557 g_free (chooser_entry->file_part);
1559 chooser_entry->file_part = file_part;
1560 chooser_entry->file_part_pos = file_part_pos;
1562 if (result == REFRESH_OK)
1564 result = reload_current_folder (chooser_entry, folder_file);
1568 discard_loading_and_current_folder_file (chooser_entry);
1572 g_object_unref (folder_file);
1574 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1575 ((result == REFRESH_OK)
1576 && (chooser_entry->current_folder_file != NULL)
1577 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1578 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1579 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1580 || ((result != REFRESH_OK)
1581 && (chooser_entry->current_folder_file == NULL)
1582 && (chooser_entry->load_folder_cancellable == NULL)
1583 && (chooser_entry->current_folder == NULL)));
1589 autocomplete (GtkFileChooserEntry *chooser_entry)
1591 if (!(chooser_entry->current_folder != NULL
1592 && chooser_entry->current_folder_loaded
1593 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1596 append_common_prefix (chooser_entry, TRUE, FALSE);
1600 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1602 RefreshStatus status;
1604 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1609 g_assert (chooser_entry->current_folder_file != NULL);
1611 if (chooser_entry->current_folder && chooser_entry->current_folder_loaded)
1612 autocomplete (chooser_entry);
1614 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1618 case REFRESH_INVALID_INPUT:
1619 case REFRESH_INCOMPLETE_HOSTNAME:
1620 case REFRESH_NONEXISTENT:
1621 case REFRESH_NOT_LOCAL:
1622 /* We don't beep or anything, since this is autocompletion - the user
1623 * didn't request any action explicitly.
1628 g_assert_not_reached ();
1633 start_autocompletion_idle_handler (gpointer data)
1635 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1637 start_autocompletion (chooser_entry);
1639 chooser_entry->start_autocompletion_idle_id = 0;
1645 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1647 if (chooser_entry->start_autocompletion_idle_id != 0)
1650 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1655 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1656 const gchar *new_text,
1657 gint new_text_length,
1661 const gchar *colon = memchr (new_text, ':', new_text_length);
1664 /* Disallow these characters altogether */
1665 for (i = 0; i < new_text_length; i++)
1667 if (new_text[i] == '<' ||
1668 new_text[i] == '>' ||
1669 new_text[i] == '"' ||
1670 new_text[i] == '|' ||
1671 new_text[i] == '*' ||
1676 if (i < new_text_length ||
1677 /* Disallow entering text that would cause a colon to be anywhere except
1678 * after a drive letter.
1681 *position + (colon - new_text) != 1) ||
1682 (new_text_length > 0 &&
1684 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1685 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1687 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1688 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1696 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1701 /* If deleting a drive letter, delete the colon, too */
1702 if (start_pos == 0 && end_pos == 1 &&
1703 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1704 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1706 g_signal_handlers_block_by_func (chooser_entry,
1707 G_CALLBACK (delete_text_callback),
1709 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1710 g_signal_handlers_unblock_by_func (chooser_entry,
1711 G_CALLBACK (delete_text_callback),
1718 * _gtk_file_chooser_entry_new:
1719 * @filesystem: The #GtkFileSystem to use
1720 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1722 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1723 * is an internal implementation widget for the GTK+ file chooser
1724 * which is an entry with completion with respect to a
1725 * #GtkFileSystem object.
1727 * Return value: the newly created #GtkFileChooserEntry
1730 _gtk_file_chooser_entry_new (GtkFileSystem *file_system,
1733 GtkFileChooserEntry *chooser_entry;
1735 g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
1737 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1738 chooser_entry->file_system = g_object_ref (file_system);
1739 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1741 return GTK_WIDGET (chooser_entry);
1745 * _gtk_file_chooser_entry_set_base_folder:
1746 * @chooser_entry: a #GtkFileChooserEntry
1747 * @file: file for a folder in the chooser entries current file system.
1749 * Sets the folder with respect to which completions occur.
1752 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1755 if (chooser_entry->base_folder)
1756 g_object_unref (chooser_entry->base_folder);
1758 chooser_entry->base_folder = file;
1760 if (chooser_entry->base_folder)
1761 g_object_ref (chooser_entry->base_folder);
1763 clear_completions (chooser_entry);
1767 * _gtk_file_chooser_entry_get_current_folder:
1768 * @chooser_entry: a #GtkFileChooserEntry
1770 * Gets the current folder for the #GtkFileChooserEntry. If the
1771 * user has only entered a filename, this will be in the base folder
1772 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1773 * user has entered a relative or absolute path, then it will
1774 * be different. If the user has entered unparsable text, or text which
1775 * the entry cannot handle, this will return %NULL.
1777 * Return value: the file for the current folder - this value is owned by the
1778 * chooser entry and must not be modified or freed.
1781 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1783 commit_completion_and_refresh (chooser_entry);
1784 return chooser_entry->current_folder_file;
1788 * _gtk_file_chooser_entry_get_file_part:
1789 * @chooser_entry: a #GtkFileChooserEntry
1791 * Gets the non-folder portion of whatever the user has entered
1792 * into the file selector. What is returned is a UTF-8 string,
1793 * and if a filename path is needed, g_file_get_child_for_display_name()
1796 * Return value: the entered filename - this value is owned by the
1797 * chooser entry and must not be modified or freed.
1800 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1802 commit_completion_and_refresh (chooser_entry);
1803 return chooser_entry->file_part;
1807 * _gtk_file_chooser_entry_set_file_part:
1808 * @chooser_entry: a #GtkFileChooserEntry
1809 * @file_part: text to display in the entry, in UTF-8
1811 * Sets the current text shown in the file chooser entry.
1814 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1815 const gchar *file_part)
1817 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1819 chooser_entry->in_change = TRUE;
1820 clear_completions (chooser_entry);
1821 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1822 chooser_entry->in_change = FALSE;
1827 * _gtk_file_chooser_entry_set_action:
1828 * @chooser_entry: a #GtkFileChooserEntry
1829 * @action: the action which is performed by the file selector using this entry
1831 * Sets action which is performed by the file selector using this entry.
1832 * The #GtkFileChooserEntry will use different completion strategies for
1833 * different actions.
1836 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1837 GtkFileChooserAction action)
1839 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1841 if (chooser_entry->action != action)
1843 GtkEntryCompletion *comp;
1845 chooser_entry->action = action;
1847 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1849 /* FIXME: do we need to actually set the following? */
1853 case GTK_FILE_CHOOSER_ACTION_OPEN:
1854 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1855 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1857 case GTK_FILE_CHOOSER_ACTION_SAVE:
1858 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1859 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1867 * _gtk_file_chooser_entry_get_action:
1868 * @chooser_entry: a #GtkFileChooserEntry
1870 * Gets the action for this entry.
1872 * Returns: the action
1874 GtkFileChooserAction
1875 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1877 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1878 GTK_FILE_CHOOSER_ACTION_OPEN);
1880 return chooser_entry->action;
1884 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1887 gboolean retval = FALSE;
1889 if (chooser_entry->current_folder)
1891 GFileInfo *file_info;
1893 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1897 retval = _gtk_file_info_consider_as_directory (file_info);
1898 g_object_unref (file_info);
1907 * _gtk_file_chooser_entry_select_filename:
1908 * @chooser_entry: a #GtkFileChooserEntry
1910 * Selects the filename (without the extension) for user edition.
1913 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1915 const gchar *str, *ext;
1918 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1920 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1921 ext = g_strrstr (str, ".");
1924 len = g_utf8_pointer_to_offset (str, ext);
1927 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1931 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1932 gboolean local_only)
1934 chooser_entry->local_only = local_only;
1935 clear_completions (chooser_entry);
1939 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1941 return chooser_entry->local_only;