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"
30 #include "gtkfilesystemmodel.h"
33 #include "gtksizerequest.h"
34 #include "gtkwindow.h"
37 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
39 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
40 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
41 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
43 struct _GtkFileChooserEntryClass
45 GtkEntryClass parent_class;
48 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
50 LOAD_COMPLETE_NOTHING,
51 LOAD_COMPLETE_EXPLICIT_COMPLETION
57 REFRESH_INVALID_INPUT,
58 REFRESH_INCOMPLETE_HOSTNAME,
63 struct _GtkFileChooserEntry
65 GtkEntry parent_instance;
67 GtkFileChooserAction action;
70 GFile *current_folder_file;
75 LoadCompleteAction load_complete_action;
77 GtkTreeModel *completion_store;
79 GtkWidget *completion_feedback_window;
80 GtkWidget *completion_feedback_label;
81 guint completion_feedback_timeout_id;
83 guint current_folder_loaded : 1;
97 #define COMPLETION_FEEDBACK_TIMEOUT_MS 2000
99 static void gtk_file_chooser_entry_finalize (GObject *object);
100 static void gtk_file_chooser_entry_dispose (GObject *object);
101 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
102 static void gtk_file_chooser_entry_unmap (GtkWidget *widget);
103 static gboolean gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
105 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
106 GdkEventFocus *event);
107 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
110 static gint insert_text_callback (GtkFileChooserEntry *widget,
111 const gchar *new_text,
112 gint new_text_length,
115 static void delete_text_callback (GtkFileChooserEntry *widget,
121 static gboolean match_selected_callback (GtkEntryCompletion *completion,
124 GtkFileChooserEntry *chooser_entry);
125 static gboolean completion_match_func (GtkEntryCompletion *comp,
130 static RefreshStatus refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
132 static void finished_loading_cb (GtkFileSystemModel *model,
134 GtkFileChooserEntry *chooser_entry);
135 static void remove_completion_feedback (GtkFileChooserEntry *chooser_entry);
136 static void pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
137 const gchar *feedback);
139 G_DEFINE_TYPE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY)
142 gtk_file_chooser_entry_dispatch_properties_changed (GObject *object,
146 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
147 GtkEditable *editable = GTK_EDITABLE (object);
150 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
152 /* What we are after: The text in front of the cursor was modified.
153 * Unfortunately, there's no other way to catch this. */
155 if (chooser_entry->in_change)
158 for (i = 0; i < n_pspecs; i++)
160 if (pspecs[i]->name == I_("cursor-position") ||
161 pspecs[i]->name == I_("selection-bound") ||
162 pspecs[i]->name == I_("text"))
167 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
169 remove_completion_feedback (chooser_entry);
170 gtk_editable_get_selection_bounds (editable, &start, &end);
171 text = gtk_editable_get_chars (editable, 0, MIN (start, end));
172 refresh_current_folder_and_file_part (chooser_entry, text);
181 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
183 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
184 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
185 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
187 gobject_class->finalize = gtk_file_chooser_entry_finalize;
188 gobject_class->dispose = gtk_file_chooser_entry_dispose;
189 gobject_class->dispatch_properties_changed = gtk_file_chooser_entry_dispatch_properties_changed;
191 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
192 widget_class->unmap = gtk_file_chooser_entry_unmap;
193 widget_class->key_press_event = gtk_file_chooser_entry_key_press_event;
194 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
196 entry_class->activate = gtk_file_chooser_entry_activate;
200 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
202 GtkEntryCompletion *comp;
203 GtkCellRenderer *cell;
205 chooser_entry->local_only = TRUE;
206 chooser_entry->base_folder = g_file_new_for_path (g_get_home_dir ());
208 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
210 comp = gtk_entry_completion_new ();
211 gtk_entry_completion_set_popup_single_match (comp, FALSE);
212 /* see docs for gtk_entry_completion_set_text_column() */
213 g_object_set (comp, "text-column", FULL_PATH_COLUMN, NULL);
215 gtk_entry_completion_set_match_func (comp,
216 completion_match_func,
220 cell = gtk_cell_renderer_text_new ();
221 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
223 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
225 "text", DISPLAY_NAME_COLUMN);
227 g_signal_connect (comp, "match-selected",
228 G_CALLBACK (match_selected_callback), chooser_entry);
230 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
231 g_object_unref (comp);
234 g_signal_connect (chooser_entry, "insert-text",
235 G_CALLBACK (insert_text_callback), NULL);
236 g_signal_connect (chooser_entry, "delete-text",
237 G_CALLBACK (delete_text_callback), NULL);
242 gtk_file_chooser_entry_finalize (GObject *object)
244 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
246 g_object_unref (chooser_entry->base_folder);
248 if (chooser_entry->current_folder_file)
249 g_object_unref (chooser_entry->current_folder_file);
251 g_free (chooser_entry->dir_part);
252 g_free (chooser_entry->file_part);
254 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
258 discard_loading_and_current_folder_file (GtkFileChooserEntry *chooser_entry)
260 if (chooser_entry->current_folder_file)
262 g_object_unref (chooser_entry->current_folder_file);
263 chooser_entry->current_folder_file = NULL;
268 gtk_file_chooser_entry_dispose (GObject *object)
270 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
272 remove_completion_feedback (chooser_entry);
273 discard_loading_and_current_folder_file (chooser_entry);
275 if (chooser_entry->completion_store)
277 g_object_unref (chooser_entry->completion_store);
278 chooser_entry->completion_store = NULL;
281 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
284 /* Match functions for the GtkEntryCompletion */
286 match_selected_callback (GtkEntryCompletion *completion,
289 GtkFileChooserEntry *chooser_entry)
293 gtk_tree_model_get (model, iter,
294 FULL_PATH_COLUMN, &path,
297 /* We don't set in_change here as we want to update the current_folder
299 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
301 gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
302 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
312 /* Match function for the GtkEntryCompletion */
314 completion_match_func (GtkEntryCompletion *comp,
315 const char *key_unused,
319 GtkFileChooserEntry *chooser_entry;
322 char *norm_file_part;
325 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
327 /* We ignore the key because it is the contents of the entry. Instead, we
328 * just use our precomputed file_part.
330 if (!chooser_entry->file_part)
335 gtk_tree_model_get (chooser_entry->completion_store, iter, DISPLAY_NAME_COLUMN, &name, -1);
338 return FALSE; /* Uninitialized row, ugh */
341 /* If we have an empty file_part, then we're at the root of a directory. In
342 * that case, we want to match all non-dot files. We might want to match
343 * dot_files too if show_hidden is TRUE on the fileselector in the future.
345 /* Additionally, support for gnome .hidden files would be sweet, too */
346 if (chooser_entry->file_part[0] == '\000')
358 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
359 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
361 #ifdef G_PLATFORM_WIN32
365 temp = norm_file_part;
366 norm_file_part = g_utf8_casefold (norm_file_part, -1);
370 norm_name = g_utf8_casefold (norm_name, -1);
375 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
377 g_free (norm_file_part);
385 clear_completions (GtkFileChooserEntry *chooser_entry)
387 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
389 remove_completion_feedback (chooser_entry);
393 beep (GtkFileChooserEntry *chooser_entry)
395 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
399 is_valid_scheme_character (char c)
401 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
405 has_uri_scheme (const char *str)
411 if (!is_valid_scheme_character (*p))
416 while (is_valid_scheme_character (*p));
418 return (strncmp (p, "://", 3) == 0);
422 gtk_file_chooser_get_file_for_text (GtkFileChooserEntry *chooser_entry,
427 if (str[0] == '~' || g_path_is_absolute (str) || has_uri_scheme (str))
428 file = g_file_parse_name (str);
430 file = g_file_resolve_relative_path (chooser_entry->base_folder, str);
436 gtk_file_chooser_get_directory_for_text (GtkFileChooserEntry *chooser_entry,
439 GFile *file, *parent;
441 file = gtk_file_chooser_get_file_for_text (chooser_entry, text);
443 if (text[0] == 0 || text[strlen (text) - 1] == G_DIR_SEPARATOR)
446 parent = g_file_get_parent (file);
447 g_object_unref (file);
453 gtk_file_chooser_entry_parse (GtkFileChooserEntry *chooser_entry,
460 gboolean result = FALSE;
461 gboolean is_dir = FALSE;
462 gchar *last_slash = NULL;
465 is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);
467 last_slash = strrchr (str, G_DIR_SEPARATOR);
469 file = gtk_file_chooser_get_file_for_text (chooser_entry, str);
471 if (g_file_equal (chooser_entry->base_folder, file))
473 /* this is when user types '.', could be the
474 * beginning of a hidden file, ./ or ../
476 *folder = g_object_ref (file);
477 *file_part = g_strdup (str);
482 /* it's a dir, or at least it ends with the dir separator */
483 *folder = g_object_ref (file);
484 *file_part = g_strdup ("");
491 parent_file = g_file_get_parent (file);
496 GTK_FILE_CHOOSER_ERROR,
497 GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
498 "Could not get parent file");
504 *folder = parent_file;
508 *file_part = g_strdup (last_slash + 1);
510 *file_part = g_strdup (str);
514 g_object_unref (file);
519 /* Determines if the completion model has entries with a common prefix relative
520 * to the current contents of the entry. Also, if there's one and only one such
521 * path, stores it in unique_path_ret.
524 find_common_prefix (GtkFileChooserEntry *chooser_entry,
525 gchar **common_prefix_ret,
526 GFile **unique_file_ret,
527 gboolean *is_complete_not_unique_ret,
528 gboolean *prefix_expands_the_file_part_ret,
531 GtkEditable *editable;
535 char *text_up_to_cursor;
536 GFile *parsed_folder_file;
537 char *parsed_file_part;
539 *common_prefix_ret = NULL;
540 *unique_file_ret = NULL;
541 *is_complete_not_unique_ret = FALSE;
542 *prefix_expands_the_file_part_ret = FALSE;
544 editable = GTK_EDITABLE (chooser_entry);
546 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
548 parsed = gtk_file_chooser_entry_parse (chooser_entry,
554 g_free (text_up_to_cursor);
559 g_assert (parsed_folder_file != NULL
560 && g_file_equal (parsed_folder_file, chooser_entry->current_folder_file));
562 g_object_unref (parsed_folder_file);
564 /* First pass: find the common prefix */
566 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
573 gtk_tree_model_get (chooser_entry->completion_store,
575 DISPLAY_NAME_COLUMN, &display_name,
579 if (g_str_has_prefix (display_name, parsed_file_part))
581 if (!*common_prefix_ret)
583 *common_prefix_ret = g_strdup (display_name);
584 *unique_file_ret = g_object_ref (file);
588 gchar *p = *common_prefix_ret;
589 const gchar *q = display_name;
591 while (*p && *p == *q)
599 if (*unique_file_ret)
601 g_object_unref (*unique_file_ret);
602 *unique_file_ret = NULL;
607 g_free (display_name);
608 g_object_unref (file);
609 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
612 /* Second pass: see if the prefix we found is a complete match */
614 if (*common_prefix_ret != NULL)
616 valid = gtk_tree_model_get_iter_first (chooser_entry->completion_store, &iter);
623 gtk_tree_model_get (chooser_entry->completion_store,
625 DISPLAY_NAME_COLUMN, &display_name,
627 len = strlen (display_name);
630 if (G_IS_DIR_SEPARATOR (display_name[len - 1]))
633 if (*unique_file_ret == NULL && strncmp (*common_prefix_ret, display_name, len) == 0)
634 *is_complete_not_unique_ret = TRUE;
636 g_free (display_name);
637 valid = gtk_tree_model_iter_next (chooser_entry->completion_store, &iter);
640 /* Finally: Did we generate a new completion, or was the user's input already completed as far as it could go? */
642 *prefix_expands_the_file_part_ret = g_utf8_strlen (*common_prefix_ret, -1) > g_utf8_strlen (parsed_file_part, -1);
645 g_free (parsed_file_part);
651 INVALID_INPUT, /* what the user typed is bogus */
652 NO_MATCH, /* no matches based on what the user typed */
653 NOTHING_INSERTED_COMPLETE, /* what the user typed is already completed as far as it will go */
654 NOTHING_INSERTED_UNIQUE, /* what the user typed is already completed, and is a unique match */
655 COMPLETED, /* completion inserted (ambiguous suffix) */
656 COMPLETED_UNIQUE, /* completion inserted, and it is a complete name and a unique match */
657 COMPLETE_BUT_NOT_UNIQUE /* completion inserted, it is a complete name but not unique */
658 } CommonPrefixResult;
660 /* Finds a common prefix based on the contents of the entry
661 * and mandatorily appends it
663 static CommonPrefixResult
664 append_common_prefix (GtkFileChooserEntry *chooser_entry)
666 gchar *common_prefix;
668 gboolean is_complete_not_unique;
669 gboolean prefix_expands_the_file_part;
671 CommonPrefixResult result = NO_MATCH;
672 gboolean have_result;
674 clear_completions (chooser_entry);
676 if (chooser_entry->completion_store == NULL)
680 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_file, &is_complete_not_unique, &prefix_expands_the_file_part, &error))
682 g_error_free (error);
684 return INVALID_INPUT;
691 g_object_unref (unique_file);
693 if (prefix_expands_the_file_part)
694 result = COMPLETED_UNIQUE;
696 result = NOTHING_INSERTED_UNIQUE;
702 if (is_complete_not_unique)
704 result = COMPLETE_BUT_NOT_UNIQUE;
714 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
716 pos = chooser_entry->file_part_pos;
718 if (prefix_expands_the_file_part)
720 chooser_entry->in_change = TRUE;
721 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
723 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
726 chooser_entry->in_change = FALSE;
728 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
730 else if (!have_result)
732 result = NOTHING_INSERTED_COMPLETE;
736 g_free (common_prefix);
753 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
755 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
756 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
760 gtk_file_chooser_entry_unmap (GtkWidget *widget)
762 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
764 remove_completion_feedback (chooser_entry);
766 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->unmap (widget);
770 completion_feedback_window_draw_cb (GtkWidget *widget,
774 /* Stolen from gtk_tooltip_paint_window() */
776 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
777 GtkStyleContext *context;
779 context = gtk_widget_get_style_context (chooser_entry->completion_feedback_window);
781 gtk_render_background (context, cr, 0, 0,
782 gtk_widget_get_allocated_width (widget),
783 gtk_widget_get_allocated_height (widget));
784 gtk_render_frame (context, cr, 0, 0,
785 gtk_widget_get_allocated_width (widget),
786 gtk_widget_get_allocated_height (widget));
792 set_invisible_mouse_cursor (GdkWindow *window)
797 display = gdk_window_get_display (window);
798 cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR);
800 gdk_window_set_cursor (window, cursor);
802 g_object_unref (cursor);
806 completion_feedback_window_realize_cb (GtkWidget *widget,
809 /* We hide the mouse cursor inside the completion feedback window, since
810 * GtkEntry hides the cursor when the user types. We don't want the cursor to
811 * come back if the completion feedback ends up where the mouse is.
813 set_invisible_mouse_cursor (gtk_widget_get_window (widget));
817 create_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
819 /* Stolen from gtk_tooltip_init() */
820 GtkWidget *window, *label;
821 GtkStyleContext *context;
823 window = gtk_window_new (GTK_WINDOW_POPUP);
824 gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_TOOLTIP);
825 gtk_widget_set_app_paintable (window, TRUE);
826 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
827 gtk_widget_set_name (window, "gtk-tooltip");
829 context = gtk_widget_get_style_context (window);
830 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLTIP);
832 g_signal_connect (window, "draw",
833 G_CALLBACK (completion_feedback_window_draw_cb), chooser_entry);
834 g_signal_connect (window, "realize",
835 G_CALLBACK (completion_feedback_window_realize_cb), chooser_entry);
836 /* FIXME: connect to motion-notify-event, and *show* the cursor when the mouse moves */
838 label = gtk_label_new (NULL);
839 gtk_widget_set_halign (label, GTK_ALIGN_CENTER);
840 gtk_widget_set_valign (label, GTK_ALIGN_CENTER);
841 /* FIXME: don't hardcode this */
842 gtk_widget_set_margin_left (label, 6);
843 gtk_widget_set_margin_right (label, 6);
844 gtk_widget_set_margin_top (label, 6);
845 gtk_widget_set_margin_bottom (label, 6);
846 gtk_container_add (GTK_CONTAINER (window), label);
847 gtk_widget_show (label);
849 chooser_entry->completion_feedback_window = window;
850 chooser_entry->completion_feedback_label = label;
854 completion_feedback_timeout_cb (gpointer data)
856 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
858 chooser_entry->completion_feedback_timeout_id = 0;
860 remove_completion_feedback (chooser_entry);
865 install_completion_feedback_timer (GtkFileChooserEntry *chooser_entry)
867 if (chooser_entry->completion_feedback_timeout_id != 0)
868 g_source_remove (chooser_entry->completion_feedback_timeout_id);
870 chooser_entry->completion_feedback_timeout_id = gdk_threads_add_timeout (COMPLETION_FEEDBACK_TIMEOUT_MS,
871 completion_feedback_timeout_cb,
875 /* Gets the x position of the text cursor in the entry, in widget coordinates */
877 get_entry_cursor_x (GtkFileChooserEntry *chooser_entry,
880 /* FIXME: see the docs for gtk_entry_get_layout_offsets(). As an exercise for
881 * the reader, you have to implement support for the entry's scroll offset and
882 * RTL layouts and all the fancy Pango stuff.
886 gint layout_x, layout_y;
888 PangoRectangle strong_pos;
889 gint start_pos, end_pos;
891 layout = gtk_entry_get_layout (GTK_ENTRY (chooser_entry));
893 gtk_entry_get_layout_offsets (GTK_ENTRY (chooser_entry), &layout_x, &layout_y);
895 gtk_editable_get_selection_bounds (GTK_EDITABLE (chooser_entry), &start_pos, &end_pos);
896 layout_index = gtk_entry_text_index_to_layout_index (GTK_ENTRY (chooser_entry),
900 pango_layout_get_cursor_pos (layout, layout_index, &strong_pos, NULL);
902 *x_ret = layout_x + strong_pos.x / PANGO_SCALE;
906 show_completion_feedback_window (GtkFileChooserEntry *chooser_entry)
908 /* More or less stolen from gtk_tooltip_position() */
910 GtkRequisition feedback_req;
911 GtkWidget *widget = GTK_WIDGET (chooser_entry);
912 gint entry_x, entry_y;
914 GtkAllocation entry_allocation;
915 int feedback_x, feedback_y;
917 gtk_widget_get_preferred_size (chooser_entry->completion_feedback_window,
918 &feedback_req, NULL);
920 gdk_window_get_origin (gtk_widget_get_window (widget), &entry_x, &entry_y);
921 gtk_widget_get_allocation (widget, &entry_allocation);
923 get_entry_cursor_x (chooser_entry, &cursor_x);
925 /* FIXME: fit to the screen if we bump on the screen's edge */
926 /* cheap "half M-width", use height as approximation of character em-size */
927 feedback_x = entry_x + cursor_x + entry_allocation.height / 2;
928 feedback_y = entry_y + (entry_allocation.height - feedback_req.height) / 2;
930 gtk_window_move (GTK_WINDOW (chooser_entry->completion_feedback_window), feedback_x, feedback_y);
931 gtk_widget_show (chooser_entry->completion_feedback_window);
933 install_completion_feedback_timer (chooser_entry);
937 pop_up_completion_feedback (GtkFileChooserEntry *chooser_entry,
938 const gchar *feedback)
940 if (chooser_entry->completion_feedback_window == NULL)
941 create_completion_feedback_window (chooser_entry);
943 gtk_label_set_text (GTK_LABEL (chooser_entry->completion_feedback_label), feedback);
945 show_completion_feedback_window (chooser_entry);
949 remove_completion_feedback (GtkFileChooserEntry *chooser_entry)
951 if (chooser_entry->completion_feedback_window)
952 gtk_widget_destroy (chooser_entry->completion_feedback_window);
954 chooser_entry->completion_feedback_window = NULL;
955 chooser_entry->completion_feedback_label = NULL;
957 if (chooser_entry->completion_feedback_timeout_id != 0)
959 g_source_remove (chooser_entry->completion_feedback_timeout_id);
960 chooser_entry->completion_feedback_timeout_id = 0;
965 explicitly_complete (GtkFileChooserEntry *chooser_entry)
967 CommonPrefixResult result;
969 g_assert (chooser_entry->current_folder_loaded);
971 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
973 * - If there is a common prefix, insert it (done)
974 * - If there is no common prefix, pop up the suggestion window
975 * - If there are no matches at all, beep and bring up a tooltip (done)
976 * - If the suggestion window is already up, scroll it
978 result = append_common_prefix (chooser_entry);
983 /* We already beeped in append_common_prefix(); do nothing here */
987 beep (chooser_entry);
988 /* translators: this text is shown when there are no completions
989 * for something the user typed in a file chooser entry
991 pop_up_completion_feedback (chooser_entry, _("No match"));
994 case NOTHING_INSERTED_COMPLETE:
995 /* FIXME: pop up the suggestion window or scroll it */
998 case NOTHING_INSERTED_UNIQUE:
999 /* translators: this text is shown when there is exactly one completion
1000 * for something the user typed in a file chooser entry
1002 pop_up_completion_feedback (chooser_entry, _("Sole completion"));
1009 case COMPLETED_UNIQUE:
1013 case COMPLETE_BUT_NOT_UNIQUE:
1014 /* translators: this text is shown when the text in a file chooser
1015 * entry is a complete filename, but could be continued to find
1018 pop_up_completion_feedback (chooser_entry, _("Complete, but not unique"));
1022 g_assert_not_reached ();
1027 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
1029 RefreshStatus status;
1031 char *feedback_msg, *text;
1033 text = gtk_editable_get_chars (GTK_EDITABLE (chooser_entry),
1034 0, gtk_editable_get_position (GTK_EDITABLE (chooser_entry)));
1035 status = refresh_current_folder_and_file_part (chooser_entry, text);
1043 g_assert (chooser_entry->current_folder_file != NULL);
1045 if (chooser_entry->current_folder_loaded)
1046 explicitly_complete (chooser_entry);
1049 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
1051 /* Translators: this text is shown while the system is searching
1052 * for possible completions for filenames in a file chooser entry. */
1053 pop_up_completion_feedback (chooser_entry, _("Completing..."));
1058 case REFRESH_INVALID_INPUT:
1060 /* Translators: this is shown in the feedback for Tab-completion in a file
1061 * chooser's text entry, when the user enters an invalid path. */
1062 feedback_msg = _("Invalid path");
1065 case REFRESH_INCOMPLETE_HOSTNAME:
1068 if (chooser_entry->local_only)
1070 /* hostnames in a local_only file chooser? user error */
1072 /* Translators: this is shown in the feedback for Tab-completion in a
1073 * file chooser's text entry when the user enters something like
1074 * "sftp://blahblah" in an app that only supports local filenames. */
1075 feedback_msg = _("Only local files may be selected");
1079 /* Another option is to complete the hostname based on the remote volumes that are mounted */
1081 /* Translators: this is shown in the feedback for Tab-completion in a
1082 * file chooser's text entry when the user hasn't entered the first '/'
1083 * after a hostname and yet hits Tab (such as "sftp://blahblah[Tab]") */
1084 feedback_msg = _("Incomplete hostname; end it with '/'");
1089 case REFRESH_NONEXISTENT:
1092 /* Translators: this is shown in the feedback for Tab-completion in a file
1093 * chooser's text entry when the user enters a path that does not exist
1094 * and then hits Tab */
1095 feedback_msg = _("Path does not exist");
1098 case REFRESH_NOT_LOCAL:
1100 feedback_msg = _("Only local files may be selected");
1104 g_assert_not_reached ();
1110 g_assert (chooser_entry->current_folder_file == NULL);
1112 beep (chooser_entry);
1113 pop_up_completion_feedback (chooser_entry, feedback_msg);
1114 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1119 gtk_file_chooser_entry_key_press_event (GtkWidget *widget,
1122 GtkFileChooserEntry *chooser_entry;
1123 GtkEditable *editable;
1124 GdkModifierType state;
1125 gboolean control_pressed;
1127 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1128 editable = GTK_EDITABLE (widget);
1130 if (!chooser_entry->eat_tabs)
1131 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1133 control_pressed = FALSE;
1135 if (gtk_get_current_event_state (&state))
1137 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
1138 control_pressed = TRUE;
1141 /* This is a bit evil -- it makes Tab never leave the entry. It basically
1142 * makes it 'safe' for people to hit. */
1143 if (event->keyval == GDK_KEY_Tab && !control_pressed)
1147 gtk_editable_get_selection_bounds (editable, &start, &end);
1150 gtk_editable_set_position (editable, MAX (start, end));
1152 start_explicit_completion (chooser_entry);
1157 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->key_press_event (widget, event);
1162 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
1163 GdkEventFocus *event)
1165 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
1167 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
1169 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
1173 commit_completion_and_refresh (GtkFileChooserEntry *chooser_entry)
1175 /* Here we ignore the result of refresh_current_folder_and_file_part(); there is nothing we can do with it */
1176 refresh_current_folder_and_file_part (chooser_entry, gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
1180 gtk_file_chooser_entry_activate (GtkEntry *entry)
1182 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
1184 commit_completion_and_refresh (chooser_entry);
1185 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
1189 discard_completion_store (GtkFileChooserEntry *chooser_entry)
1191 if (!chooser_entry->completion_store)
1194 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
1195 gtk_entry_completion_set_inline_completion (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), FALSE);
1196 g_object_unref (chooser_entry->completion_store);
1197 chooser_entry->completion_store = NULL;
1201 completion_store_set (GtkFileSystemModel *model,
1208 GtkFileChooserEntry *chooser_entry = data;
1210 const char *prefix = "";
1211 const char *suffix = "";
1216 g_value_set_object (value, file);
1218 case FULL_PATH_COLUMN:
1219 prefix = chooser_entry->dir_part;
1221 case DISPLAY_NAME_COLUMN:
1222 if (_gtk_file_info_consider_as_directory (info))
1223 suffix = G_DIR_SEPARATOR_S;
1225 g_value_take_string (value, g_strconcat (
1227 g_file_info_get_display_name (info),
1232 g_assert_not_reached ();
1239 /* Fills the completion store from the contents of the current folder */
1241 populate_completion_store (GtkFileChooserEntry *chooser_entry)
1243 discard_completion_store (chooser_entry);
1245 chooser_entry->completion_store = GTK_TREE_MODEL (
1246 _gtk_file_system_model_new_for_directory (chooser_entry->current_folder_file,
1247 "standard::name,standard::display-name,standard::type",
1248 completion_store_set,
1254 g_signal_connect (chooser_entry->completion_store, "finished-loading",
1255 G_CALLBACK (finished_loading_cb), chooser_entry);
1257 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
1258 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
1260 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
1261 chooser_entry->completion_store);
1264 /* When we finish loading the current folder, this function should get called to
1265 * perform the deferred explicit completion.
1268 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
1270 switch (chooser_entry->load_complete_action)
1272 case LOAD_COMPLETE_NOTHING:
1275 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
1276 explicitly_complete (chooser_entry);
1280 g_assert_not_reached ();
1285 /* Callback when the current folder finishes loading */
1287 finished_loading_cb (GtkFileSystemModel *model,
1289 GtkFileChooserEntry *chooser_entry)
1291 GtkEntryCompletion *completion;
1293 chooser_entry->current_folder_loaded = TRUE;
1297 LoadCompleteAction old_load_complete_action;
1299 old_load_complete_action = chooser_entry->load_complete_action;
1301 discard_completion_store (chooser_entry);
1302 clear_completions (chooser_entry);
1304 if (old_load_complete_action == LOAD_COMPLETE_EXPLICIT_COMPLETION)
1306 /* Since this came from explicit user action (Tab completion), we'll present errors visually */
1308 beep (chooser_entry);
1309 pop_up_completion_feedback (chooser_entry, error->message);
1315 perform_load_complete_action (chooser_entry);
1317 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
1319 completion = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1320 gtk_entry_completion_set_inline_completion (completion, TRUE);
1321 gtk_entry_completion_complete (completion);
1322 gtk_entry_completion_insert_prefix (completion);
1325 static RefreshStatus
1326 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1329 g_assert (folder_file != NULL);
1331 if (chooser_entry->current_folder_file
1332 && g_file_equal (folder_file, chooser_entry->current_folder_file))
1335 if (chooser_entry->current_folder_file)
1337 discard_loading_and_current_folder_file (chooser_entry);
1340 if (chooser_entry->local_only
1341 && !g_file_is_native (folder_file))
1342 return REFRESH_NOT_LOCAL;
1344 chooser_entry->current_folder_file = g_object_ref (folder_file);
1345 chooser_entry->current_folder_loaded = FALSE;
1347 populate_completion_store (chooser_entry);
1352 static RefreshStatus
1353 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1358 gsize total_len, file_part_len;
1361 RefreshStatus result;
1364 if (!gtk_file_chooser_entry_parse (chooser_entry,
1365 text, &folder_file, &file_part, &error))
1367 folder_file = g_object_ref (chooser_entry->base_folder);
1369 if (g_error_matches (error, GTK_FILE_CHOOSER_ERROR, GTK_FILE_CHOOSER_ERROR_NONEXISTENT))
1370 result = REFRESH_NONEXISTENT;
1372 result = REFRESH_INVALID_INPUT;
1375 g_error_free (error);
1377 file_part = g_strdup ("");
1382 g_assert (folder_file != NULL);
1384 file_part_len = strlen (file_part);
1385 total_len = strlen (text);
1386 if (total_len > file_part_len)
1387 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1391 result = REFRESH_OK;
1394 g_free (chooser_entry->file_part);
1395 g_free (chooser_entry->dir_part);
1397 chooser_entry->dir_part = file_part_pos > 0 ? g_strndup (text, file_part_pos) : g_strdup ("");
1398 chooser_entry->file_part = file_part;
1399 chooser_entry->file_part_pos = file_part_pos;
1401 if (result == REFRESH_OK)
1403 result = reload_current_folder (chooser_entry, folder_file);
1407 discard_loading_and_current_folder_file (chooser_entry);
1411 g_object_unref (folder_file);
1413 g_assert (/* we are OK and we have a current folder file and (loading process or folder handle)... */
1414 ((result == REFRESH_OK)
1415 && (chooser_entry->current_folder_file != NULL))
1416 /* ... OR we have an error, and we don't have a current folder file nor a loading process nor a folder handle */
1417 || ((result != REFRESH_OK)
1418 && (chooser_entry->current_folder_file == NULL)));
1425 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1426 const gchar *new_text,
1427 gint new_text_length,
1431 const gchar *colon = memchr (new_text, ':', new_text_length);
1434 /* Disallow these characters altogether */
1435 for (i = 0; i < new_text_length; i++)
1437 if (new_text[i] == '<' ||
1438 new_text[i] == '>' ||
1439 new_text[i] == '"' ||
1440 new_text[i] == '|' ||
1441 new_text[i] == '*' ||
1446 if (i < new_text_length ||
1447 /* Disallow entering text that would cause a colon to be anywhere except
1448 * after a drive letter.
1451 *position + (colon - new_text) != 1) ||
1452 (new_text_length > 0 &&
1454 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1455 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1457 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1458 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1466 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1471 /* If deleting a drive letter, delete the colon, too */
1472 if (start_pos == 0 && end_pos == 1 &&
1473 gtk_entry_get_text_length (GTK_ENTRY (chooser_entry)) >= 2 &&
1474 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1476 g_signal_handlers_block_by_func (chooser_entry,
1477 G_CALLBACK (delete_text_callback),
1479 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1480 g_signal_handlers_unblock_by_func (chooser_entry,
1481 G_CALLBACK (delete_text_callback),
1488 * _gtk_file_chooser_entry_new:
1489 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1491 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1492 * is an internal implementation widget for the GTK+ file chooser
1493 * which is an entry with completion with respect to a
1494 * #GtkFileSystem object.
1496 * Return value: the newly created #GtkFileChooserEntry
1499 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1501 GtkFileChooserEntry *chooser_entry;
1503 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1504 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1506 return GTK_WIDGET (chooser_entry);
1510 * _gtk_file_chooser_entry_set_base_folder:
1511 * @chooser_entry: a #GtkFileChooserEntry
1512 * @file: file for a folder in the chooser entries current file system.
1514 * Sets the folder with respect to which completions occur.
1517 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1521 g_object_ref (file);
1523 file = g_file_new_for_path (g_get_home_dir ());
1525 if (g_file_equal (chooser_entry->base_folder, file))
1527 g_object_unref (file);
1531 if (chooser_entry->base_folder)
1532 g_object_unref (chooser_entry->base_folder);
1534 chooser_entry->base_folder = file;
1536 clear_completions (chooser_entry);
1540 * _gtk_file_chooser_entry_get_current_folder:
1541 * @chooser_entry: a #GtkFileChooserEntry
1543 * Gets the current folder for the #GtkFileChooserEntry. If the
1544 * user has only entered a filename, this will be in the base folder
1545 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1546 * user has entered a relative or absolute path, then it will
1547 * be different. If the user has entered unparsable text, or text which
1548 * the entry cannot handle, this will return %NULL.
1550 * Return value: the file for the current folder - you must g_object_unref()
1551 * the value after use.
1554 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1556 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1558 return gtk_file_chooser_get_directory_for_text (chooser_entry,
1559 gtk_entry_get_text (GTK_ENTRY (chooser_entry)));
1563 * _gtk_file_chooser_entry_get_file_part:
1564 * @chooser_entry: a #GtkFileChooserEntry
1566 * Gets the non-folder portion of whatever the user has entered
1567 * into the file selector. What is returned is a UTF-8 string,
1568 * and if a filename path is needed, g_file_get_child_for_display_name()
1571 * Return value: the entered filename - this value is owned by the
1572 * chooser entry and must not be modified or freed.
1575 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1577 const char *last_slash, *text;
1579 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry), NULL);
1581 text = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1582 last_slash = strrchr (text, G_DIR_SEPARATOR);
1584 return last_slash + 1;
1590 * _gtk_file_chooser_entry_set_file_part:
1591 * @chooser_entry: a #GtkFileChooserEntry
1592 * @file_part: text to display in the entry, in UTF-8
1594 * Sets the current text shown in the file chooser entry.
1597 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1598 const gchar *file_part)
1600 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1602 chooser_entry->in_change = TRUE;
1603 clear_completions (chooser_entry);
1604 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1605 chooser_entry->in_change = FALSE;
1610 * _gtk_file_chooser_entry_set_action:
1611 * @chooser_entry: a #GtkFileChooserEntry
1612 * @action: the action which is performed by the file selector using this entry
1614 * Sets action which is performed by the file selector using this entry.
1615 * The #GtkFileChooserEntry will use different completion strategies for
1616 * different actions.
1619 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1620 GtkFileChooserAction action)
1622 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1624 if (chooser_entry->action != action)
1626 GtkEntryCompletion *comp;
1628 chooser_entry->action = action;
1630 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1632 /* FIXME: do we need to actually set the following? */
1636 case GTK_FILE_CHOOSER_ACTION_OPEN:
1637 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1638 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1640 case GTK_FILE_CHOOSER_ACTION_SAVE:
1641 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1642 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1650 * _gtk_file_chooser_entry_get_action:
1651 * @chooser_entry: a #GtkFileChooserEntry
1653 * Gets the action for this entry.
1655 * Returns: the action
1657 GtkFileChooserAction
1658 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1660 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1661 GTK_FILE_CHOOSER_ACTION_OPEN);
1663 return chooser_entry->action;
1667 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1673 if (chooser_entry->completion_store == NULL ||
1674 !_gtk_file_system_model_get_iter_for_file (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1679 info = _gtk_file_system_model_get_info (GTK_FILE_SYSTEM_MODEL (chooser_entry->completion_store),
1682 return _gtk_file_info_consider_as_directory (info);
1687 * _gtk_file_chooser_entry_select_filename:
1688 * @chooser_entry: a #GtkFileChooserEntry
1690 * Selects the filename (without the extension) for user edition.
1693 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1695 const gchar *str, *ext;
1698 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1700 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1701 ext = g_strrstr (str, ".");
1704 len = g_utf8_pointer_to_offset (str, ext);
1707 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);
1711 _gtk_file_chooser_entry_set_local_only (GtkFileChooserEntry *chooser_entry,
1712 gboolean local_only)
1714 chooser_entry->local_only = local_only;
1715 clear_completions (chooser_entry);
1719 _gtk_file_chooser_entry_get_local_only (GtkFileChooserEntry *chooser_entry)
1721 return chooser_entry->local_only;