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 has_completion : 1;
102 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
104 static void gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface);
106 static void gtk_file_chooser_entry_finalize (GObject *object);
107 static void gtk_file_chooser_entry_dispose (GObject *object);
108 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
109 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
110 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
112 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
113 GdkEventFocus *event);
114 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
115 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
116 const gchar *new_text,
117 gint new_text_length,
119 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
122 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
124 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
129 static gint insert_text_callback (GtkFileChooserEntry *widget,
130 const gchar *new_text,
131 gint new_text_length,
134 static void delete_text_callback (GtkFileChooserEntry *widget,
140 static gboolean match_selected_callback (GtkEntryCompletion *completion,
143 GtkFileChooserEntry *chooser_entry);
144 static gboolean completion_match_func (GtkEntryCompletion *comp,
148 static char *maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
154 REFRESH_UP_TO_CURSOR_POSITION,
158 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
159 RefreshMode refresh_mode);
160 static void finished_loading_cb (GtkFolder *folder,
162 static void autocomplete (GtkFileChooserEntry *chooser_entry);
163 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
164 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
165 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
166 const gchar *feedback);
168 static GtkEditableInterface *parent_editable_iface;
170 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
171 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
172 gtk_file_chooser_entry_iface_init))
175 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
177 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
178 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
179 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
181 gobject_class->finalize = gtk_file_chooser_entry_finalize;
182 gobject_class->dispose = gtk_file_chooser_entry_dispose;
184 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
185 widget_class->unmap = gtk_file_chooser_entry_unmap;
186 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
187 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
189 entry_class->activate = gtk_file_chooser_entry_activate;
193 gtk_file_chooser_entry_iface_init (GtkEditableInterface *iface)
195 parent_editable_iface = g_type_interface_peek_parent (iface);
197 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
198 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
199 iface->set_position = gtk_file_chooser_entry_set_position;
200 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
204 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
206 GtkEntryCompletion *comp;
207 GtkCellRenderer *cell;
209 chooser_entry->local_only = TRUE;
211 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
213 comp = gtk_entry_completion_new ();
214 gtk_entry_completion_set_popup_single_match (comp, FALSE);
216 gtk_entry_completion_set_match_func (comp,
217 completion_match_func,
221 cell = gtk_cell_renderer_text_new ();
222 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
224 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
226 "text", DISPLAY_NAME_COLUMN);
228 g_signal_connect (comp, "match-selected",
229 G_CALLBACK (match_selected_callback), chooser_entry);
231 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
232 g_object_unref (comp);
235 g_signal_connect (chooser_entry, "insert-text",
236 G_CALLBACK (insert_text_callback), NULL);
237 g_signal_connect (chooser_entry, "delete-text",
238 G_CALLBACK (delete_text_callback), NULL);
243 gtk_file_chooser_entry_finalize (GObject *object)
245 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
247 if (chooser_entry->base_folder)
248 g_object_unref (chooser_entry->base_folder);
250 if (chooser_entry->current_folder)
251 g_object_unref (chooser_entry->current_folder);
253 if (chooser_entry->current_folder_file)
254 g_object_unref (chooser_entry->current_folder_file);
256 g_free (chooser_entry->file_part);
258 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
262 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
264 if (chooser_entry->current_folder)
266 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
267 G_CALLBACK (finished_loading_cb), chooser_entry);
268 g_object_unref (chooser_entry->current_folder);
269 chooser_entry->current_folder = NULL;
271 if (chooser_entry->load_folder_cancellable)
273 g_cancellable_cancel (chooser_entry->load_folder_cancellable);
274 chooser_entry->load_folder_cancellable = NULL;
277 if (chooser_entry->current_folder_file)
279 g_object_unref (chooser_entry->current_folder_file);
280 chooser_entry->current_folder_file = NULL;
285 gtk_file_chooser_entry_dispose (GObject *object)
287 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
289 remove_completion_feedback (chooser_entry);
290 discard_loading_and_current_folder_file (chooser_entry);
292 if (chooser_entry->start_autocompletion_idle_id != 0)
294 g_source_remove (chooser_entry->start_autocompletion_idle_id);
295 chooser_entry->start_autocompletion_idle_id = 0;
298 if (chooser_entry->completion_store)
300 g_object_unref (chooser_entry->completion_store);
301 chooser_entry->completion_store = NULL;
304 if (chooser_entry->file_system)
306 g_object_unref (chooser_entry->file_system);
307 chooser_entry->file_system = NULL;
310 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
313 /* Match functions for the GtkEntryCompletion */
315 match_selected_callback (GtkEntryCompletion *completion,
318 GtkFileChooserEntry *chooser_entry)
325 gtk_tree_model_get (model, iter,
326 DISPLAY_NAME_COLUMN, &display_name,
330 if (!display_name || !file)
333 g_object_unref (file);
335 g_free (display_name);
339 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
341 pos = chooser_entry->file_part_pos;
343 /* We don't set in_change here as we want to update the current_folder
345 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
347 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
350 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
352 g_object_unref (file);
353 g_free (display_name);
358 /* Match function for the GtkEntryCompletion */
360 completion_match_func (GtkEntryCompletion *comp,
361 const char *key_unused,
365 GtkFileChooserEntry *chooser_entry;
368 char *norm_file_part;
371 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
373 /* We ignore the key because it is the contents of the entry. Instead, we
374 * just use our precomputed file_part.
376 if (!chooser_entry->file_part)
381 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
384 return FALSE; /* Uninitialized row, ugh */
387 /* If we have an empty file_part, then we're at the root of a directory. In
388 * that case, we want to match all non-dot files. We might want to match
389 * dot_files too if show_hidden is TRUE on the fileselector in the future.
391 /* Additionally, support for gnome .hidden files would be sweet, too */
392 if (chooser_entry->file_part[0] == '\000')
404 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
405 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
407 #ifdef G_PLATFORM_WIN32
411 temp = norm_file_part;
412 norm_file_part = g_utf8_casefold (norm_file_part, -1);
416 norm_name = g_utf8_casefold (norm_name, -1);
421 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
423 g_free (norm_file_part);
431 clear_completions (GtkFileChooserEntry *chooser_entry)
433 chooser_entry->has_completion = FALSE;
434 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
436 remove_completion_feedback (chooser_entry);
440 beep (GtkFileChooserEntry *chooser_entry)
442 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
445 /* This function will append a directory separator to paths to
446 * display_name iff the path associated with it is a directory.
447 * maybe_append_separator_to_file will g_free the display_name and
448 * return a new one if needed. Otherwise, it will return the old one.
449 * You should be safe calling
451 * display_name = maybe_append_separator_to_file (entry, file, display_name, &appended);
453 * g_free (display_name);
456 maybe_append_separator_to_file (GtkFileChooserEntry *chooser_entry,
463 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && file)
467 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
471 if (_gtk_file_info_consider_as_directory (info))
473 gchar *tmp = display_name;
474 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
479 g_object_unref (info);
487 trim_dir_separator_suffix (const char *str)
492 if (len > 0 && G_IS_DIR_SEPARATOR (str[len - 1]))
493 return g_strndup (str, len - 1);
495 return g_strdup (str);
498 /* Determines if the completion model has entries with a common prefix relative
499 * to the current contents of the entry. Also, if there's one and only one such
500 * path, stores it in unique_path_ret.
503 find_common_prefix (GtkFileChooserEntry *chooser_entry,
504 gchar **common_prefix_ret,
505 GFile **unique_file_ret,
506 gboolean *is_complete_not_unique_ret,
507 gboolean *prefix_expands_the_file_part_ret,
510 GtkEditable *editable;
514 char *text_up_to_cursor;
515 GFile *parsed_folder_file;
516 char *parsed_file_part;
518 *common_prefix_ret = NULL;
519 *unique_file_ret = NULL;
520 *is_complete_not_unique_ret = FALSE;
521 *prefix_expands_the_file_part_ret = FALSE;
523 editable = GTK_EDITABLE (chooser_entry);
525 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
527 parsed = _gtk_file_system_parse (chooser_entry->file_system,
528 chooser_entry->base_folder,
534 g_free (text_up_to_cursor);
539 g_assert (parsed_folder_file != NULL
540 && chooser_entry->current_folder != NULL
541 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
543 g_object_unref (parsed_folder_file);
545 /* First pass: find the common prefix */
547 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
554 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
556 DISPLAY_NAME_COLUMN, &display_name,
560 if (g_str_has_prefix (display_name, parsed_file_part))
562 if (!*common_prefix_ret)
564 *common_prefix_ret = trim_dir_separator_suffix (display_name);
565 *unique_file_ret = g_object_ref (file);
569 gchar *p = *common_prefix_ret;
570 const gchar *q = display_name;
572 while (*p && *p == *q)
580 if (*unique_file_ret)
582 g_object_unref (*unique_file_ret);
583 *unique_file_ret = NULL;
588 g_free (display_name);
589 g_object_unref (file);
590 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
593 /* Second pass: see if the prefix we found is a complete match */
595 if (*common_prefix_ret != NULL)
597 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
604 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
606 DISPLAY_NAME_COLUMN, &display_name,
608 len = strlen (display_name);
611 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
614 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
615 *is_complete_not_unique_ret = TRUE;
617 g_free (display_name);
618 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
621 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
623 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
626 g_free (parsed_file_part);
632 char_after_cursor_is_directory_separator (GtkFileChooserEntry *chooser_entry)
639 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
640 if (cursor_pos < gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)))
644 next_char_str = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry), cursor_pos, cursor_pos + 1);
645 if (G_IS_DIR_SEPARATOR (*next_char_str))
648 g_free (next_char_str);
655 INVALID_INPUT, /* what the user typed is bogus */
656 NO_MATCH, /* no matches based on what the user typed */
657 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
658 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
659 COMPLETED, /* completion inserted (ambiguous suffix) */
660 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
661 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
662 } CommonPrefixResult;
664 /* Finds a common prefix based on the contents of the entry
665 * and mandatorily appends it
667 static CommonPrefixResult
668 append_common_prefix (GtkFileChooserEntry *chooser_entry,
670 gboolean show_errors)
672 gchar *common_prefix;
674 gboolean is_complete_not_unique;
675 gboolean prefix_expands_the_file_part;
677 CommonPrefixResult result = NO_MATCH;
678 gboolean have_result;
680 clear_completions (chooser_entry);
682 if (chooser_entry->completion_store == NULL)
686 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
688 /* If the user types an incomplete hostname ("http://foo" without
689 * a slash after that), it's not an error. We just don't want to
690 * pop up a meaningless completion window in that state.
692 if (!g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME)
695 beep (chooser_entry);
696 pop_up_completion_feedback (chooser_entry, _("Invalid path"));
699 g_error_free (error);
701 return INVALID_INPUT;
708 if (!char_after_cursor_is_directory_separator (chooser_entry))
712 common_prefix = maybe_append_separator_to_file (chooser_entry,
717 prefix_expands_the_file_part = TRUE;
720 g_object_unref (unique_file);
722 if (prefix_expands_the_file_part)
723 result = COMPLETED_UNIQUE;
725 result = NOTHING_INSERTED_UNIQUE;
731 if (is_complete_not_unique)
733 result = COMPLETE_BUT_NOT_UNIQUE;
743 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
745 pos = chooser_entry->file_part_pos;
747 if (prefix_expands_the_file_part)
749 chooser_entry->in_change = TRUE;
750 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
752 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
755 chooser_entry->in_change = FALSE;
759 /* equivalent to cursor_pos + common_prefix_len); */
760 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
763 chooser_entry->has_completion = TRUE;
766 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
768 else if (!have_result)
770 result = NOTHING_INSERTED_COMPLETE;
774 g_free (common_prefix);
791 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
792 const gchar *new_text,
793 gint new_text_length,
796 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
800 old_text_len = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
801 insert_pos = *position;
803 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
805 if (chooser_entry->in_change)
808 remove_completion_feedback (chooser_entry);
810 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
811 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
812 && insert_pos == old_text_len)
813 install_start_autocompletion_idle (chooser_entry);
817 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
819 if (chooser_entry->in_change)
822 clear_completions (chooser_entry);
826 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
830 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
832 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
834 clear_completions_if_not_in_change (chooser_entry);
838 gtk_file_chooser_entry_set_position (GtkEditable *editable,
841 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
843 parent_editable_iface->set_position (editable, position);
845 clear_completions_if_not_in_change (chooser_entry);
849 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
853 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
855 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
857 clear_completions_if_not_in_change (chooser_entry);
861 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
863 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
864 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
868 gtk_file_chooser_entry_unmap (GtkWidget *widget)
870 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
872 remove_completion_feedback (chooser_entry);
874 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
878 completion_feedback_window_draw_cb (GtkWidget *widget,
882 /* Stolen from gtk_tooltip_paint_window() */
884 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
885 GtkStyleContext *context;
887 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
889 gtk_render_background (context, cr, 0, 0,
890 gtk_widget_get_allocated_width (widget),
891 gtk_widget_get_allocated_height (widget));
892 gtk_render_frame (context, cr, 0, 0,
893 gtk_widget_get_allocated_width (widget),
894 gtk_widget_get_allocated_height (widget));
900 set_invisible_mouse_cursor (GdkWindow *window)
905 display = gdk_window_get_display (window);
906 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
908 gdk_window_set_cursor (window, cursor);
910 g_object_unref (cursor);
914 completion_feedback_window_realize_cb (GtkWidget *widget,
917 /* We hide the mouse cursor inside the completion feedback window, since
918 * GtkEntry hides the cursor when the user types. We don't want the cursor to
919 * come back if the completion feedback ends up where the mouse is.
921 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
925 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
927 /* Stolen from gtk_tooltip_init() */
928 GtkWidget *window, *label;
929 GtkStyleContext *context;
931 window = gtk_window_new (GTK_WINDOW_POPUP);
932 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
933 gtk_widget_set_app_paintable (window, TRUE);
934 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
935 gtk_widget_set_name (window, "gtk-tooltip");
937 context = gtk_widget_get_style_context (window);
938 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
940 g_signal_connect (window, "draw",
941 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
942 g_signal_connect (window, "realize",
943 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
944 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
946 label = gtk_label_new (NULL);
947 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
948 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
949 /* FIXME: don't hardcode this */
950 gtk_widget_set_margin_left (label, 6);
951 gtk_widget_set_margin_right (label, 6);
952 gtk_widget_set_margin_top (label, 6);
953 gtk_widget_set_margin_bottom (label, 6);
954 gtk_container_add (GTK_CONTAINER (window), label);
955 gtk_widget_show (label);
957 chooser_entry->completion_feedback_window = window;
958 chooser_entry->completion_feedback_label = label;
962 completion_feedback_timeout_cb (gpointer data)
964 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
966 chooser_entry->completion_feedback_timeout_id = 0;
968 remove_completion_feedback (chooser_entry);
973 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
975 if (chooser_entry->completion_feedback_timeout_id != 0)
976 g_source_remove (chooser_entry->completion_feedback_timeout_id);
978 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
979 completion_feedback_timeout_cb,
983 /* Gets the x position of the text cursor in the entry, in widget coordinates */
985 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
988 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
989 * the reader, you have to implement support for the entry's scroll offset and
990 * RTL layouts and all the fancy Pango stuff.
994 gint layout_x, layout_y;
996 PangoRectangle strong_pos;
997 gint start_pos, end_pos;
999 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
1001 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
1003 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
1004 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
1008 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
1010 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
1014 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
1016 /* More or less stolen from gtk_tooltip_position() */
1018 GtkRequisition feedback_req;
1019 GtkWidget *widget = GTK_WIDGET (chooser_entry);
1020 gint entry_x, entry_y;
1022 GtkAllocation entry_allocation;
1023 int feedback_x, feedback_y;
1025 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
1026 &feedback_req, NULL);
1028 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
1029 gtk_widget_get_allocation (widget, &entry_allocation);
1031 get_entry_cursor_x (chooser_entry, &cursor_x);
1033 /* FIXME: fit to the screen if we bump on the screen's edge */
1034 /* cheap "half M-width", use height as approximation of character em-size */
1035 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
1036 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
1038 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
1039 gtk_widget_show (chooser_entry->completion_feedback_window);
1041 install_completion_feedback_timer (chooser_entry);
1045 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
1046 const gchar *feedback)
1048 if (chooser_entry->completion_feedback_window == NULL)
1049 create_completion_feedback_window (chooser_entry);
1051 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
1053 show_completion_feedback_window (chooser_entry);
1057 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
1059 if (chooser_entry->completion_feedback_window)
1060 gtk_widget_destroy (chooser_entry->completion_feedback_window);
1062 chooser_entry->completion_feedback_window = NULL;
1063 chooser_entry->completion_feedback_label = NULL;
1065 if (chooser_entry->completion_feedback_timeout_id != 0)
1067 g_source_remove (chooser_entry->completion_feedback_timeout_id);
1068 chooser_entry->completion_feedback_timeout_id = 0;
1073 explicitly_complete (GtkFileChooserEntry *chooser_entry)
1075 CommonPrefixResult result;
1077 g_assert (chooser_entry->current_folder != NULL);
1078 g_assert (_gtk_folder_is_finished_loading (chooser_entry->current_folder));
1080 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
1082 * - If there is a common prefix, insert it (done)
1083 * - If there is no common prefix, pop up the suggestion window
1084 * - If there are no matches at all, beep and bring up a tooltip (done)
1085 * - If the suggestion window is already up, scroll it
1087 result = append_common_prefix (chooser_entry, FALSE, TRUE);
1092 /* We already beeped in append_common_prefix(); do nothing here */
1096 beep (chooser_entry);
1097 /* translators: this text is shown when there are no completions
1098 * for something the user typed in a file chooser entry
1100 pop_up_completion_feedback (chooser_entry, _("No match"));
1103 case NOTHING_INSERTED_COMPLETE:
1104 /* FIXME: pop up the suggestion window or scroll it */
1107 case NOTHING_INSERTED_UNIQUE:
1108 /* translators: this text is shown when there is exactly one completion
1109 * for something the user typed in a file chooser entry
1111 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1118 case COMPLETED_UNIQUE:
1122 case COMPLETE_BUT_NOT_UNIQUE:
1123 /* translators: this text is shown when the text in a file chooser
1124 * entry is a complete filename, but could be continued to find
1127 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1131 g_assert_not_reached ();
1136 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1138 RefreshStatus status;
1142 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1149 g_assert (chooser_entry->current_folder_file != NULL);
1151 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1152 explicitly_complete (chooser_entry);
1155 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1157 /* Translators: this text is shown while the system is searching
1158 * for possible completions for filenames in a file chooser entry. */
1159 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1164 case REFRESH_INVALID_INPUT:
1166 /* Translators: this is shown in the feedback for Tab-completion in a file
1167 * chooser's text entry, when the user enters an invalid path. */
1168 feedback_msg = _("Invalid path");
1171 case REFRESH_INCOMPLETE_HOSTNAME:
1174 if (chooser_entry->local_only)
1176 /* hostnames in a local_only file chooser? user error */
1178 /* Translators: this is shown in the feedback for Tab-completion in a
1179 * file chooser's text entry when the user enters something like
1180 * "sftp://blahblah" in an app that only supports local filenames. */
1181 feedback_msg = _("Only local files may be selected");
1185 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1187 /* Translators: this is shown in the feedback for Tab-completion in a
1188 * file chooser's text entry when the user hasn't entered the first '/'
1189 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1190 feedback_msg = _("Incomplete hostname; end it with '/'");
1195 case REFRESH_NONEXISTENT:
1198 /* Translators: this is shown in the feedback for Tab-completion in a file
1199 * chooser's text entry when the user enters a path that does not exist
1200 * and then hits Tab */
1201 feedback_msg = _("Path does not exist");
1204 case REFRESH_NOT_LOCAL:
1206 feedback_msg = _("Only local files may be selected");
1210 g_assert_not_reached ();
1216 g_assert (chooser_entry->current_folder_file == NULL);
1218 beep (chooser_entry);
1219 pop_up_completion_feedback (chooser_entry, feedback_msg);
1220 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1225 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1228 GtkFileChooserEntry *chooser_entry;
1229 GtkEditable *editable;
1231 GdkModifierType state;
1232 gboolean control_pressed;
1234 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1235 editable = GTK_EDITABLE (widget);
1236 entry = GTK_ENTRY (widget);
1238 if (!chooser_entry->eat_tabs)
1239 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1241 control_pressed = FALSE;
1243 if (gtk_get_current_event_state (&state))
1245 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1246 control_pressed = TRUE;
1249 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1250 * makes it 'safe' for people to hit. */
1251 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1253 if (chooser_entry->has_completion)
1254 gtk_editable_set_position (editable, gtk_entry_get_text_length (entry));
1256 start_explicit_completion (chooser_entry);
1261 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1266 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1267 GdkEventFocus *event)
1269 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1271 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1273 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1277 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1279 if (chooser_entry->has_completion)
1281 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1282 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)));
1285 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1286 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1290 gtk_file_chooser_entry_activate (GtkEntry *entry)
1292 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1294 commit_completion_and_refresh (chooser_entry);
1295 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1299 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1301 if (!chooser_entry->completion_store)
1304 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1305 g_object_unref (chooser_entry->completion_store);
1306 chooser_entry->completion_store = NULL;
1309 /* Fills the completion store from the contents of the current folder */
1311 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1316 discard_completion_store (chooser_entry);
1318 files = _gtk_folder_list_children (chooser_entry->current_folder);
1320 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
1324 for (tmp_list = files; tmp_list; tmp_list = tmp_list->next)
1329 file = tmp_list->data;
1331 info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1335 gchar *display_name = g_strdup (g_file_info_get_display_name (info));
1339 display_name = maybe_append_separator_to_file (chooser_entry, file, display_name, &dummy);
1341 gtk_list_store_append (chooser_entry->completion_store, &iter);
1342 gtk_list_store_set (chooser_entry->completion_store, &iter,
1343 DISPLAY_NAME_COLUMN, display_name,
1347 g_object_unref (info);
1348 g_free (display_name);
1352 g_slist_foreach (files, (GFunc) g_object_unref, NULL);
1353 g_slist_free (files);
1355 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1356 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1358 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1359 GTK_TREE_MODEL (chooser_entry->completion_store));
1362 /* When we finish loading the current folder, this function should get called to
1363 * perform the deferred autocompletion or explicit completion.
1366 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1368 switch (chooser_entry->load_complete_action)
1370 case LOAD_COMPLETE_NOTHING:
1373 case LOAD_COMPLETE_AUTOCOMPLETE:
1374 autocomplete (chooser_entry);
1377 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1378 explicitly_complete (chooser_entry);
1382 g_assert_not_reached ();
1385 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1389 finish_folder_load (GtkFileChooserEntry *chooser_entry)
1391 populate_completion_store (chooser_entry);
1392 perform_load_complete_action (chooser_entry);
1394 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1397 /* Callback when the current folder finishes loading */
1399 finished_loading_cb (GtkFolder *folder,
1402 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1404 finish_folder_load (chooser_entry);
1407 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
1409 load_directory_get_folder_callback (GCancellable *cancellable,
1411 const GError *error,
1414 gboolean cancelled = g_cancellable_is_cancelled (cancellable);
1415 GtkFileChooserEntry *chooser_entry = data;
1417 if (cancellable != chooser_entry->load_folder_cancellable)
1420 chooser_entry->load_folder_cancellable = NULL;
1424 LoadCompleteAction old_load_complete_action;
1426 old_load_complete_action = chooser_entry->load_complete_action;
1428 discard_completion_store (chooser_entry);
1429 clear_completions (chooser_entry);
1431 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1433 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1435 beep (chooser_entry);
1436 pop_up_completion_feedback (chooser_entry, error->message);
1440 if (cancelled || error)
1443 g_assert (folder != NULL);
1444 chooser_entry->current_folder = g_object_ref (folder);
1446 discard_completion_store (chooser_entry);
1448 if (_gtk_folder_is_finished_loading (chooser_entry->current_folder))
1449 finish_folder_load (chooser_entry);
1451 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1452 G_CALLBACK (finished_loading_cb), chooser_entry);
1455 g_object_unref (chooser_entry);
1456 g_object_unref (cancellable);
1459 static RefreshStatus
1460 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1462 if (chooser_entry->file_system == NULL)
1465 g_assert (chooser_entry->current_folder_file != NULL);
1466 g_assert (chooser_entry->current_folder == NULL);
1467 g_assert (chooser_entry->load_folder_cancellable == NULL);
1469 if (chooser_entry->local_only
1470 && !g_file_is_native (chooser_entry->current_folder_file))
1472 discard_loading_and_current_folder_file (chooser_entry);
1474 return REFRESH_NOT_LOCAL;
1477 chooser_entry->load_folder_cancellable =
1478 _gtk_file_system_get_folder (chooser_entry->file_system,
1479 chooser_entry->current_folder_file,
1480 "standard::name,standard::display-name,standard::type",
1481 load_directory_get_folder_callback,
1482 g_object_ref (chooser_entry));
1487 static RefreshStatus
1488 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1490 gboolean force_reload)
1492 g_assert (folder_file != NULL);
1494 if (chooser_entry->current_folder_file
1495 && g_file_equal (folder_file, chooser_entry->current_folder_file)
1496 && chooser_entry->load_folder_cancellable
1500 if (chooser_entry->current_folder_file)
1502 discard_loading_and_current_folder_file (chooser_entry);
1505 chooser_entry->current_folder_file = g_object_ref (folder_file);
1507 return start_loading_current_folder (chooser_entry);
1510 static RefreshStatus
1511 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1512 RefreshMode refresh_mode)
1514 GtkEditable *editable;
1519 gsize total_len, file_part_len;
1522 RefreshStatus result;
1524 editable = GTK_EDITABLE (chooser_entry);
1526 switch (refresh_mode)
1528 case REFRESH_UP_TO_CURSOR_POSITION:
1529 end_pos = gtk_editable_get_position (editable);
1532 case REFRESH_WHOLE_TEXT:
1533 end_pos = gtk_entry_get_text_length (GTK_ENTRY (chooser_entry));
1537 g_assert_not_reached ();
1538 return REFRESH_INVALID_INPUT;
1541 text = gtk_editable_get_chars (editable, 0, end_pos);
1544 if (!chooser_entry->file_system ||
1545 !_gtk_file_system_parse (chooser_entry->file_system,
1546 chooser_entry->base_folder, text,
1547 &folder_file, &file_part, &error))
1549 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME))
1552 result = REFRESH_INCOMPLETE_HOSTNAME;
1556 folder_file = (chooser_entry->base_folder) ? g_object_ref (chooser_entry->base_folder) : NULL;
1558 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1559 result = REFRESH_NONEXISTENT;
1561 result = REFRESH_INVALID_INPUT;
1565 g_error_free (error);
1567 file_part = g_strdup ("");
1572 g_assert (folder_file != NULL);
1574 file_part_len = strlen (file_part);
1575 total_len = strlen (text);
1576 if (total_len > file_part_len)
1577 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1581 result = REFRESH_OK;
1586 g_free (chooser_entry->file_part);
1588 chooser_entry->file_part = file_part;
1589 chooser_entry->file_part_pos = file_part_pos;
1591 if (result == REFRESH_OK)
1593 result = reload_current_folder (chooser_entry, folder_file, file_part_pos == -1);
1597 discard_loading_and_current_folder_file (chooser_entry);
1601 g_object_unref (folder_file);
1603 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1604 ((result == REFRESH_OK)
1605 && (chooser_entry->current_folder_file != NULL)
1606 && (((chooser_entry->load_folder_cancellable != NULL) && (chooser_entry->current_folder == NULL))
1607 || ((chooser_entry->load_folder_cancellable == NULL) && (chooser_entry->current_folder != NULL))))
1608 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1609 || ((result != REFRESH_OK)
1610 && (chooser_entry->current_folder_file == NULL)
1611 && (chooser_entry->load_folder_cancellable == NULL)
1612 && (chooser_entry->current_folder == NULL)));
1618 autocomplete (GtkFileChooserEntry *chooser_entry)
1620 if (!(chooser_entry->current_folder != NULL
1621 && _gtk_folder_is_finished_loading (chooser_entry->current_folder)
1622 && gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == gtk_entry_get_text_length (GTK_ENTRY (chooser_entry))))
1625 append_common_prefix (chooser_entry, TRUE, FALSE);
1629 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1631 RefreshStatus status;
1633 status = refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1638 g_assert (chooser_entry->current_folder_file != NULL);
1640 if (chooser_entry->current_folder && _gtk_folder_is_finished_loading (chooser_entry->current_folder))
1641 autocomplete (chooser_entry);
1643 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1647 case REFRESH_INVALID_INPUT:
1648 case REFRESH_INCOMPLETE_HOSTNAME:
1649 case REFRESH_NONEXISTENT:
1650 case REFRESH_NOT_LOCAL:
1651 /* We don't beep or anything, since this is autocompletion - the user
1652 * didn't request any action explicitly.
1657 g_assert_not_reached ();
1662 start_autocompletion_idle_handler (gpointer data)
1664 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1666 start_autocompletion (chooser_entry);
1668 chooser_entry->start_autocompletion_idle_id = 0;
1674 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1676 if (chooser_entry->start_autocompletion_idle_id != 0)
1679 chooser_entry->start_autocompletion_idle_id = gdk_threads_add_idle (start_autocompletion_idle_handler, chooser_entry);
1684 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1685 const gchar *new_text,
1686 gint new_text_length,
1690 const gchar *colon = memchr (new_text, ':', new_text_length);
1693 /* Disallow these characters altogether */
1694 for (i = 0; i < new_text_length; i++)
1696 if (new_text[i] == '<' ||
1697 new_text[i] == '>' ||
1698 new_text[i] == '"' ||
1699 new_text[i] == '|' ||
1700 new_text[i] == '*' ||
1705 if (i < new_text_length ||
1706 /* Disallow entering text that would cause a colon to be anywhere except
1707 * after a drive letter.
1710 *position + (colon - new_text) != 1) ||
1711 (new_text_length > 0 &&
1713 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1714 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1716 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1717 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1725 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1730 /* If deleting a drive letter, delete the colon, too */
1731 if (start_pos == 0 && end_pos == 1 &&
1732 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1733 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1735 g_signal_handlers_block_by_func (chooser_entry,
1736 G_CALLBACK (delete_text_callback),
1738 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1739 g_signal_handlers_unblock_by_func (chooser_entry,
1740 G_CALLBACK (delete_text_callback),
1747 * _gtk_file_chooser_entry_new:
1748 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1750 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1751 * is an internal implementation widget for the GTK+ file chooser
1752 * which is an entry with completion with respect to a
1753 * #GtkFileSystem object.
1755 * Return value: the newly created #GtkFileChooserEntry
1758 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1760 GtkFileChooserEntry *chooser_entry;
1762 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1763 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1765 return GTK_WIDGET (chooser_entry);
1769 * _gtk_file_chooser_entry_set_file_system:
1770 * @chooser_entry: a #GtkFileChooser
1771 * @file_system: an object implementing #GtkFileSystem
1773 * Sets the file system for @chooser_entry.
1776 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1777 GtkFileSystem *file_system)
1779 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1780 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1782 if (file_system != chooser_entry->file_system)
1784 if (chooser_entry->file_system)
1785 g_object_unref (chooser_entry->file_system);
1787 chooser_entry->file_system = g_object_ref (file_system);
1792 * _gtk_file_chooser_entry_set_base_folder:
1793 * @chooser_entry: a #GtkFileChooserEntry
1794 * @file: file for a folder in the chooser entries current file system.
1796 * Sets the folder with respect to which completions occur.
1799 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1802 if (chooser_entry->base_folder)
1803 g_object_unref (chooser_entry->base_folder);
1805 chooser_entry->base_folder = file;
1807 if (chooser_entry->base_folder)
1808 g_object_ref (chooser_entry->base_folder);
1810 clear_completions (chooser_entry);
1814 * _gtk_file_chooser_entry_get_current_folder:
1815 * @chooser_entry: a #GtkFileChooserEntry
1817 * Gets the current folder for the #GtkFileChooserEntry. If the
1818 * user has only entered a filename, this will be in the base folder
1819 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1820 * user has entered a relative or absolute path, then it will
1821 * be different. If the user has entered unparsable text, or text which
1822 * the entry cannot handle, this will return %NULL.
1824 * Return value: the file for the current folder - this value is owned by the
1825 * chooser entry and must not be modified or freed.
1828 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1830 commit_completion_and_refresh (chooser_entry);
1831 return chooser_entry->current_folder_file;
1835 * _gtk_file_chooser_entry_get_file_part:
1836 * @chooser_entry: a #GtkFileChooserEntry
1838 * Gets the non-folder portion of whatever the user has entered
1839 * into the file selector. What is returned is a UTF-8 string,
1840 * and if a filename path is needed, g_file_get_child_for_display_name()
1843 * Return value: the entered filename - this value is owned by the
1844 * chooser entry and must not be modified or freed.
1847 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1849 commit_completion_and_refresh (chooser_entry);
1850 return chooser_entry->file_part;
1854 * _gtk_file_chooser_entry_set_file_part:
1855 * @chooser_entry: a #GtkFileChooserEntry
1856 * @file_part: text to display in the entry, in UTF-8
1858 * Sets the current text shown in the file chooser entry.
1861 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1862 const gchar *file_part)
1864 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1866 chooser_entry->in_change = TRUE;
1867 clear_completions (chooser_entry);
1868 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1869 chooser_entry->in_change = FALSE;
1874 * _gtk_file_chooser_entry_set_action:
1875 * @chooser_entry: a #GtkFileChooserEntry
1876 * @action: the action which is performed by the file selector using this entry
1878 * Sets action which is performed by the file selector using this entry.
1879 * The #GtkFileChooserEntry will use different completion strategies for
1880 * different actions.
1883 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1884 GtkFileChooserAction action)
1886 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1888 if (chooser_entry->action != action)
1890 GtkEntryCompletion *comp;
1892 chooser_entry->action = action;
1894 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1896 /* FIXME: do we need to actually set the following? */
1900 case GTK_FILE_CHOOSER_ACTION_OPEN:
1901 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1902 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1904 case GTK_FILE_CHOOSER_ACTION_SAVE:
1905 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1906 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1914 * _gtk_file_chooser_entry_get_action:
1915 * @chooser_entry: a #GtkFileChooserEntry
1917 * Gets the action for this entry.
1919 * Returns: the action
1921 GtkFileChooserAction
1922 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1924 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1925 GTK_FILE_CHOOSER_ACTION_OPEN);
1927 return chooser_entry->action;
1931 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1934 gboolean retval = FALSE;
1936 if (chooser_entry->current_folder)
1938 GFileInfo *file_info;
1940 file_info = _gtk_folder_get_info (chooser_entry->current_folder, file);
1944 retval = _gtk_file_info_consider_as_directory (file_info);
1945 g_object_unref (file_info);
1954 * _gtk_file_chooser_entry_select_filename:
1955 * @chooser_entry: a #GtkFileChooserEntry
1957 * Selects the filename (without the extension) for user edition.
1960 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1962 const gchar *str, *ext;
1965 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1967 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1968 ext = g_strrstr (str, ".");
1971 len = g_utf8_pointer_to_offset (str, ext);
1974 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1978 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1979 gboolean local_only)
1981 chooser_entry->local_only = local_only;
1982 clear_completions (chooser_entry);
1986 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1988 return chooser_entry->local_only;