1 /* GTK - The GIMP Toolkit
2 * gtkfilechooserentry.c: Entry with filename completion
3 * Copyright (C) 2003, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
27 #include "gtkfilechooserentry.h"
32 typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass;
34 #define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
35 #define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY))
36 #define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass))
38 struct _GtkFileChooserEntryClass
40 GtkEntryClass parent_class;
43 /* Action to take when the current folder finishes loading (for explicit or automatic completion) */
45 LOAD_COMPLETE_NOTHING,
46 LOAD_COMPLETE_AUTOCOMPLETE,
47 LOAD_COMPLETE_EXPLICIT_COMPLETION
50 struct _GtkFileChooserEntry
52 GtkEntry parent_instance;
54 GtkFileChooserAction action;
56 GtkFileSystem *file_system;
57 GtkFilePath *base_folder;
61 /* Folder being loaded or already loaded */
62 GtkFilePath *current_folder_path;
63 GtkFileFolder *current_folder;
64 GtkFileSystemHandle *load_folder_handle;
66 LoadCompleteAction load_complete_action;
68 GtkListStore *completion_store;
70 guint start_autocompletion_idle_id;
72 guint has_completion : 1;
84 static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface);
86 static void gtk_file_chooser_entry_finalize (GObject *object);
87 static void gtk_file_chooser_entry_dispose (GObject *object);
88 static void gtk_file_chooser_entry_grab_focus (GtkWidget *widget);
89 static gboolean gtk_file_chooser_entry_focus (GtkWidget *widget,
90 GtkDirectionType direction);
91 static gboolean gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
92 GdkEventFocus *event);
93 static void gtk_file_chooser_entry_activate (GtkEntry *entry);
94 static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
95 const gchar *new_text,
98 static void gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
101 static void gtk_file_chooser_entry_set_position (GtkEditable *editable,
103 static void gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
108 static gint insert_text_callback (GtkFileChooserEntry *widget,
109 const gchar *new_text,
110 gint new_text_length,
113 static void delete_text_callback (GtkFileChooserEntry *widget,
119 static gboolean match_selected_callback (GtkEntryCompletion *completion,
122 GtkFileChooserEntry *chooser_entry);
123 static gboolean completion_match_func (GtkEntryCompletion *comp,
127 static char *maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
129 gchar *display_name);
132 REFRESH_UP_TO_CURSOR_POSITION,
136 static void refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
137 RefreshMode refresh_mode);
138 static void finished_loading_cb (GtkFileFolder *folder,
140 static void autocomplete (GtkFileChooserEntry *chooser_entry);
141 static void install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry);
143 static GtkEditableClass *parent_editable_iface;
145 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserEntry, _gtk_file_chooser_entry, GTK_TYPE_ENTRY,
146 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
147 gtk_file_chooser_entry_iface_init))
150 _gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class)
152 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
153 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
154 GtkEntryClass *entry_class = GTK_ENTRY_CLASS (class);
156 gobject_class->finalize = gtk_file_chooser_entry_finalize;
157 gobject_class->dispose = gtk_file_chooser_entry_dispose;
159 widget_class->grab_focus = gtk_file_chooser_entry_grab_focus;
160 widget_class->focus = gtk_file_chooser_entry_focus;
161 widget_class->focus_out_event = gtk_file_chooser_entry_focus_out_event;
163 entry_class->activate = gtk_file_chooser_entry_activate;
167 gtk_file_chooser_entry_iface_init (GtkEditableClass *iface)
169 parent_editable_iface = g_type_interface_peek_parent (iface);
171 iface->do_insert_text = gtk_file_chooser_entry_do_insert_text;
172 iface->do_delete_text = gtk_file_chooser_entry_do_delete_text;
173 iface->set_position = gtk_file_chooser_entry_set_position;
174 iface->set_selection_bounds = gtk_file_chooser_entry_set_selection_bounds;
178 _gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry)
180 GtkEntryCompletion *comp;
181 GtkCellRenderer *cell;
183 g_object_set (chooser_entry, "truncate-multiline", TRUE, NULL);
185 comp = gtk_entry_completion_new ();
186 gtk_entry_completion_set_popup_single_match (comp, FALSE);
188 gtk_entry_completion_set_match_func (comp,
189 completion_match_func,
193 cell = gtk_cell_renderer_text_new ();
194 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comp),
196 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (comp),
200 g_signal_connect (comp, "match_selected",
201 G_CALLBACK (match_selected_callback), chooser_entry);
203 gtk_entry_set_completion (GTK_ENTRY (chooser_entry), comp);
204 g_object_unref (comp);
207 g_signal_connect (chooser_entry, "insert_text",
208 G_CALLBACK (insert_text_callback), NULL);
209 g_signal_connect (chooser_entry, "delete_text",
210 G_CALLBACK (delete_text_callback), NULL);
215 gtk_file_chooser_entry_finalize (GObject *object)
217 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
219 gtk_file_path_free (chooser_entry->base_folder);
220 gtk_file_path_free (chooser_entry->current_folder_path);
221 g_free (chooser_entry->file_part);
223 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->finalize (object);
227 gtk_file_chooser_entry_dispose (GObject *object)
229 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object);
231 if (chooser_entry->start_autocompletion_idle_id != 0)
233 g_source_remove (chooser_entry->start_autocompletion_idle_id);
234 chooser_entry->start_autocompletion_idle_id = 0;
237 if (chooser_entry->completion_store)
239 g_object_unref (chooser_entry->completion_store);
240 chooser_entry->completion_store = NULL;
243 if (chooser_entry->load_folder_handle)
245 gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
246 chooser_entry->load_folder_handle = NULL;
249 if (chooser_entry->current_folder)
251 g_signal_handlers_disconnect_by_func (chooser_entry->current_folder,
252 G_CALLBACK (finished_loading_cb), chooser_entry);
253 g_object_unref (chooser_entry->current_folder);
254 chooser_entry->current_folder = NULL;
257 if (chooser_entry->file_system)
259 g_object_unref (chooser_entry->file_system);
260 chooser_entry->file_system = NULL;
263 G_OBJECT_CLASS (_gtk_file_chooser_entry_parent_class)->dispose (object);
266 /* Match functions for the GtkEntryCompletion */
268 match_selected_callback (GtkEntryCompletion *completion,
271 GtkFileChooserEntry *chooser_entry)
277 gtk_tree_model_get (model, iter,
278 DISPLAY_NAME_COLUMN, &display_name,
282 if (!display_name || !path)
284 /* these shouldn't complain if passed NULL */
285 gtk_file_path_free (path);
286 g_free (display_name);
290 display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
292 pos = chooser_entry->file_part_pos;
294 /* We don't set in_change here as we want to update the current_folder
296 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
298 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
301 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), -1);
303 gtk_file_path_free (path);
304 g_free (display_name);
309 /* Match function for the GtkEntryCompletion */
311 completion_match_func (GtkEntryCompletion *comp,
312 const char *key_unused,
316 GtkFileChooserEntry *chooser_entry;
319 char *norm_file_part;
322 chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
324 /* We ignore the key because it is the contents of the entry. Instead, we
325 * just use our precomputed file_part.
327 if (!chooser_entry->file_part)
332 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store), iter, DISPLAY_NAME_COLUMN, &name, -1);
335 return FALSE; /* Uninitialized row, ugh */
338 /* If we have an empty file_part, then we're at the root of a directory. In
339 * that case, we want to match all non-dot files. We might want to match
340 * dot_files too if show_hidden is TRUE on the fileselector in the future.
342 /* Additionally, support for gnome .hidden files would be sweet, too */
343 if (chooser_entry->file_part[0] == '\000')
355 norm_file_part = g_utf8_normalize (chooser_entry->file_part, -1, G_NORMALIZE_ALL);
356 norm_name = g_utf8_normalize (name, -1, G_NORMALIZE_ALL);
358 #ifdef G_PLATFORM_WIN32
362 temp = norm_file_part;
363 norm_file_part = g_utf8_casefold (norm_file_part, -1);
367 norm_name = g_utf8_casefold (norm_name, -1);
372 result = (strncmp (norm_file_part, norm_name, strlen (norm_file_part)) == 0);
374 g_free (norm_file_part);
382 clear_completions (GtkFileChooserEntry *chooser_entry)
384 chooser_entry->has_completion = FALSE;
385 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
389 beep (GtkFileChooserEntry *chooser_entry)
391 gdk_display_beep (gtk_widget_get_display (GTK_WIDGET (chooser_entry)));
394 /* This function will append a directory separator to paths to
395 * display_name iff the path associated with it is a directory.
396 * maybe_append_separator_to_path will g_free the display_name and
397 * return a new one if needed. Otherwise, it will return the old one.
398 * You should be safe calling
400 * display_name = maybe_append_separator_to_path (entry, path, display_name);
402 * g_free (display_name);
405 maybe_append_separator_to_path (GtkFileChooserEntry *chooser_entry,
409 if (!g_str_has_suffix (display_name, G_DIR_SEPARATOR_S) && path)
413 info = gtk_file_folder_get_info (chooser_entry->current_folder,
414 path, NULL); /* NULL-GError */
418 if (gtk_file_info_get_is_folder (info))
420 gchar *tmp = display_name;
421 display_name = g_strconcat (tmp, G_DIR_SEPARATOR_S, NULL);
425 gtk_file_info_free (info);
432 /* Determines if the completion model has entries with a common prefix relative
433 * to the current contents of the entry. Also, if there's one and only one such
434 * path, stores it in unique_path_ret.
437 find_common_prefix (GtkFileChooserEntry *chooser_entry,
438 gchar **common_prefix_ret,
439 GtkFilePath **unique_path_ret,
442 GtkEditable *editable;
446 char *text_up_to_cursor;
447 GtkFilePath *parsed_folder_path;
448 char *parsed_file_part;
450 *common_prefix_ret = NULL;
451 *unique_path_ret = NULL;
453 if (chooser_entry->completion_store == NULL)
456 editable = GTK_EDITABLE (chooser_entry);
458 text_up_to_cursor = gtk_editable_get_chars (editable, 0, gtk_editable_get_position (editable));
460 parsed = gtk_file_system_parse (chooser_entry->file_system,
461 chooser_entry->base_folder,
467 printf ("Text up to cursor: \"%s\"\n", text_up_to_cursor);
468 printf ("parsed_folder_path: \"%s\"\nparsed_file_part: \"%s\"\n",
469 parsed_folder_path ? (char *) parsed_folder_path : "<NONE>",
470 parsed_file_part ? parsed_file_part : "<NONE>");
472 g_free (text_up_to_cursor);
477 g_assert (parsed_folder_path != NULL
478 && chooser_entry->current_folder_path != NULL
479 && gtk_file_path_compare (parsed_folder_path, chooser_entry->current_folder_path) == 0);
481 valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
488 gtk_tree_model_get (GTK_TREE_MODEL (chooser_entry->completion_store),
490 DISPLAY_NAME_COLUMN, &display_name,
494 if (g_str_has_prefix (display_name, parsed_file_part))
496 if (!*common_prefix_ret)
498 *common_prefix_ret = g_strdup (display_name);
499 *unique_path_ret = gtk_file_path_copy (path);
503 gchar *p = *common_prefix_ret;
504 const gchar *q = display_name;
506 while (*p && *p == *q)
514 gtk_file_path_free (*unique_path_ret);
515 *unique_path_ret = NULL;
519 g_free (display_name);
520 gtk_file_path_free (path);
521 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (chooser_entry->completion_store), &iter);
524 gtk_file_path_free (parsed_folder_path);
525 g_free (parsed_file_part);
535 COMPLETE_BUT_NOT_UNIQUE
536 } CommonPrefixResult;
538 /* Finds a common prefix based on the contents of the entry and mandatorily appends it */
539 static CommonPrefixResult
540 append_common_prefix (GtkFileChooserEntry *chooser_entry,
542 gboolean show_errors)
544 gchar *common_prefix;
545 GtkFilePath *unique_path;
547 CommonPrefixResult result;
548 gboolean have_result;
550 clear_completions (chooser_entry);
553 if (!find_common_prefix (chooser_entry, &common_prefix, &unique_path, &error))
557 beep (chooser_entry);
558 /* FIXME: display the error somehow */
561 g_error_free (error);
563 return INVALID_INPUT;
570 common_prefix = maybe_append_separator_to_path (chooser_entry,
573 gtk_file_path_free (unique_path);
576 result = UNIQUE_PREFIX_APPENDED;
578 result = INVALID_INPUT;
584 /* FIXME: if there was no unique_path, but there was a common_prefix,
585 * then we *may* have "complete but not unique". find_common_prefix()
586 * needs to say if the match was a complete entry, or a partial one.
590 printf ("common prefix: \"%s\"\n",
591 common_prefix ? common_prefix : "<NONE>");
596 gint common_prefix_len;
599 cursor_pos = gtk_editable_get_position (GTK_EDITABLE (chooser_entry));
600 common_prefix_len = g_utf8_strlen (common_prefix, -1);
602 pos = chooser_entry->file_part_pos;
604 chooser_entry->in_change = TRUE;
605 printf ("Deleting range (%d, %d)\n", pos, cursor_pos);
606 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry),
608 printf ("Inserting common prefix at %d\n", pos);
609 gtk_editable_insert_text (GTK_EDITABLE (chooser_entry),
612 chooser_entry->in_change = FALSE;
616 printf ("Selecting range (%d, %d)\n", cursor_pos, pos);
617 gtk_editable_select_region (GTK_EDITABLE (chooser_entry),
619 pos); /* cursor_pos + common_prefix_len); */
620 chooser_entry->has_completion = TRUE;
623 gtk_editable_set_position (GTK_EDITABLE (chooser_entry), pos);
625 g_free (common_prefix);
630 return PREFIX_APPENDED;
637 return NO_COMMON_PREFIX;
642 gtk_file_chooser_entry_do_insert_text (GtkEditable *editable,
643 const gchar *new_text,
644 gint new_text_length,
647 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
651 old_text_len = GTK_ENTRY (chooser_entry)->text_length;
652 insert_pos = *position;
654 parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position);
656 if (chooser_entry->in_change)
659 if ((chooser_entry->action == GTK_FILE_CHOOSER_ACTION_OPEN
660 || chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER)
661 && insert_pos == old_text_len)
662 install_start_autocompletion_idle (chooser_entry);
666 clear_completions_if_not_in_change (GtkFileChooserEntry *chooser_entry)
668 if (chooser_entry->in_change)
671 clear_completions (chooser_entry);
675 gtk_file_chooser_entry_do_delete_text (GtkEditable *editable,
679 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
681 parent_editable_iface->do_delete_text (editable, start_pos, end_pos);
683 clear_completions_if_not_in_change (chooser_entry);
687 gtk_file_chooser_entry_set_position (GtkEditable *editable,
690 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
692 parent_editable_iface->set_position (editable, position);
694 clear_completions_if_not_in_change (chooser_entry);
698 gtk_file_chooser_entry_set_selection_bounds (GtkEditable *editable,
702 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable);
704 parent_editable_iface->set_selection_bounds (editable, start_pos, end_pos);
706 clear_completions_if_not_in_change (chooser_entry);
710 gtk_file_chooser_entry_grab_focus (GtkWidget *widget)
712 GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->grab_focus (widget);
713 _gtk_file_chooser_entry_select_filename (GTK_FILE_CHOOSER_ENTRY (widget));
717 explicitly_complete (GtkFileChooserEntry *chooser_entry)
719 CommonPrefixResult result;
721 g_assert (chooser_entry->current_folder != NULL);
722 g_assert (gtk_file_folder_is_finished_loading (chooser_entry->current_folder));
724 /* FIXME: see what Emacs does in case there is no common prefix, or there is more than one match:
726 * - If there is a common prefix, insert it
727 * - If there is no common prefix, pop up the suggestion window
728 * - If there are no matches at all, beep and bring up a tooltip
729 * - If the suggestion window is already up, scroll it
731 result = append_common_prefix (chooser_entry, FALSE, TRUE);
735 case PREFIX_APPENDED:
738 case UNIQUE_PREFIX_APPENDED:
744 case NO_COMMON_PREFIX:
745 /* FIXME: pop up the suggestion window */
746 /* FIXME: we need to distinguish between "no match" and "many matches" */
750 g_assert_not_reached ();
753 /* FIXME: this bit of code is commented out for reference; this is how we used to force the suggestion window to pop up
755 * Trigger the completion window to pop up again by a
756 * zero-length insertion, a bit of a hack.
758 * gtk_editable_insert_text (editable, "", -1, &pos);
763 start_explicit_completion (GtkFileChooserEntry *chooser_entry)
765 printf ("Starting explicit completion - refreshing current folder\n");
767 refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
769 if (!chooser_entry->current_folder_path)
771 /* Here, no folder path means we couldn't parse what the user typed. */
773 printf ("We don't have a current_folder_path - means the user typed something bogus\n");
775 beep (chooser_entry);
776 /* FIXME: present a tooltip to tell the user that his folder is invalid */
778 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
782 if (chooser_entry->current_folder
783 && gtk_file_folder_is_finished_loading (chooser_entry->current_folder))
785 printf ("File folder is finished loading, doing explicit completion immediately\n");
786 explicitly_complete (chooser_entry);
790 printf ("File folder is not yet loaded; will do explicit completion later\n");
791 chooser_entry->load_complete_action = LOAD_COMPLETE_EXPLICIT_COMPLETION;
793 /* FIXME: here, Emacs would say, "Making completion list..." */
798 gtk_file_chooser_entry_focus (GtkWidget *widget,
799 GtkDirectionType direction)
801 GtkFileChooserEntry *chooser_entry;
802 GtkEditable *editable;
804 GdkModifierType state;
805 gboolean control_pressed;
807 chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
808 editable = GTK_EDITABLE (widget);
809 entry = GTK_ENTRY (widget);
811 if (!chooser_entry->eat_tabs)
812 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
814 control_pressed = FALSE;
816 if (gtk_get_current_event_state (&state))
818 if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
819 control_pressed = TRUE;
822 /* This is a bit evil -- it makes Tab never leave the entry. It basically
823 * makes it 'safe' for people to hit. */
824 if ((direction == GTK_DIR_TAB_FORWARD) &&
825 (GTK_WIDGET_HAS_FOCUS (widget)) &&
828 if (chooser_entry->has_completion)
830 gboolean has_selection;
833 printf ("Hit Tab, and we have a completion! Will unselect it\n");
835 has_selection = gtk_editable_get_selection_bounds (editable, NULL, &sel_end);
836 g_assert (has_selection && sel_end == GTK_ENTRY (entry)->text_length);
838 gtk_editable_set_position (editable, sel_end);
842 printf ("Hit Tab, we don't have a selected completion. Will start explicit completion\n");
844 start_explicit_completion (chooser_entry);
850 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus (widget, direction);
854 gtk_file_chooser_entry_focus_out_event (GtkWidget *widget,
855 GdkEventFocus *event)
857 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget);
859 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
861 return GTK_WIDGET_CLASS (_gtk_file_chooser_entry_parent_class)->focus_out_event (widget, event);
865 gtk_file_chooser_entry_activate (GtkEntry *entry)
867 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry);
869 if (chooser_entry->has_completion)
871 gtk_editable_set_position (GTK_EDITABLE (entry),
875 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
877 GTK_ENTRY_CLASS (_gtk_file_chooser_entry_parent_class)->activate (entry);
881 discard_completion_store (GtkFileChooserEntry *chooser_entry)
883 if (!chooser_entry->completion_store)
886 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)), NULL);
887 g_object_unref (chooser_entry->completion_store);
888 chooser_entry->completion_store = NULL;
891 /* Fills the completion store from the contents of the current folder */
893 populate_completion_store (GtkFileChooserEntry *chooser_entry)
898 printf ("Populating completion store\n");
900 if (!gtk_file_folder_list_children (chooser_entry->current_folder, &paths, NULL)) /* NULL-GError */
903 discard_completion_store (chooser_entry);
905 chooser_entry->completion_store = gtk_list_store_new (N_COLUMNS,
909 for (tmp_list = paths; tmp_list; tmp_list = tmp_list->next)
914 path = tmp_list->data;
916 info = gtk_file_folder_get_info (chooser_entry->current_folder,
918 NULL); /* NULL-GError */
921 gchar *display_name = g_strdup (gtk_file_info_get_display_name (info));
924 display_name = maybe_append_separator_to_path (chooser_entry, path, display_name);
926 gtk_list_store_append (chooser_entry->completion_store, &iter);
927 gtk_list_store_set (chooser_entry->completion_store, &iter,
928 DISPLAY_NAME_COLUMN, display_name,
932 gtk_file_info_free (info);
933 g_free (display_name);
937 gtk_file_paths_free (paths);
939 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (chooser_entry->completion_store),
940 DISPLAY_NAME_COLUMN, GTK_SORT_ASCENDING);
942 gtk_entry_completion_set_model (gtk_entry_get_completion (GTK_ENTRY (chooser_entry)),
943 GTK_TREE_MODEL (chooser_entry->completion_store));
946 /* When we finish loading the current folder, this function should get called to
947 * perform the deferred autocompletion or explicit completion.
950 perform_load_complete_action (GtkFileChooserEntry *chooser_entry)
952 switch (chooser_entry->load_complete_action)
954 case LOAD_COMPLETE_NOTHING:
957 case LOAD_COMPLETE_AUTOCOMPLETE:
958 printf ("Load is complete; will autocomplete immediately\n");
959 autocomplete (chooser_entry);
962 case LOAD_COMPLETE_EXPLICIT_COMPLETION:
963 printf ("Load is complete; will do explicit completion\n");
964 explicitly_complete (chooser_entry);
968 g_assert_not_reached ();
971 chooser_entry->load_complete_action = LOAD_COMPLETE_NOTHING;
975 finish_folder_load (GtkFileChooserEntry *chooser_entry)
977 populate_completion_store (chooser_entry);
978 perform_load_complete_action (chooser_entry);
980 gtk_widget_set_tooltip_text (GTK_WIDGET (chooser_entry), NULL);
983 /* Callback when the current folder finishes loading */
985 finished_loading_cb (GtkFileFolder *folder,
988 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
990 printf ("Folder finished loading asynchronously! Will populate the completion store\n");
992 finish_folder_load (chooser_entry);
995 /* Callback when the current folder's handle gets obtained (not necessarily loaded completely) */
997 load_directory_get_folder_callback (GtkFileSystemHandle *handle,
998 GtkFileFolder *folder,
1002 gboolean cancelled = handle->cancelled;
1003 GtkFileChooserEntry *chooser_entry = data;
1005 if (handle != chooser_entry->load_folder_handle)
1008 chooser_entry->load_folder_handle = NULL;
1010 /* FIXME: if there was an error *AND* we had a pending explicit completion, beep and pop up a
1011 * tooltip to say that the folder could not be loaded.
1014 /* FIXME: if error, remove the current tooltip ("making completion list") */
1016 if (cancelled || error)
1019 g_assert (folder != NULL);
1020 chooser_entry->current_folder = folder;
1022 printf ("Got folder asynchronously!\n");
1024 discard_completion_store (chooser_entry);
1026 if (gtk_file_folder_is_finished_loading (chooser_entry->current_folder))
1028 printf ("And the folder is already finished loading. Will populate the completion store.\n");
1029 finish_folder_load (chooser_entry);
1033 printf ("Folder is not yet completely loaded. Will load it asynchronously...\n");
1034 g_signal_connect (chooser_entry->current_folder, "finished-loading",
1035 G_CALLBACK (finished_loading_cb), chooser_entry);
1039 g_object_unref (chooser_entry);
1040 g_object_unref (handle);
1044 start_loading_current_folder (GtkFileChooserEntry *chooser_entry)
1046 if (chooser_entry->current_folder_path == NULL ||
1047 chooser_entry->file_system == NULL)
1050 g_assert (chooser_entry->current_folder == NULL);
1051 g_assert (chooser_entry->load_folder_handle == NULL);
1053 printf ("Starting async load of folder %s\n", (char *) chooser_entry->current_folder_path);
1055 chooser_entry->load_folder_handle =
1056 gtk_file_system_get_folder (chooser_entry->file_system,
1057 chooser_entry->current_folder_path,
1058 GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER,
1059 load_directory_get_folder_callback,
1060 g_object_ref (chooser_entry));
1064 reload_current_folder (GtkFileChooserEntry *chooser_entry,
1065 GtkFilePath *folder_path,
1066 gboolean force_reload)
1068 gboolean reload = FALSE;
1070 if (chooser_entry->current_folder_path)
1072 if ((folder_path && gtk_file_path_compare (folder_path, chooser_entry->current_folder_path) != 0)
1077 /* We changed our current directory. We need to clear out the old
1078 * directory information.
1080 if (chooser_entry->current_folder)
1082 if (chooser_entry->load_folder_handle)
1084 printf ("Cancelling folder load\n");
1085 gtk_file_system_cancel_operation (chooser_entry->load_folder_handle);
1086 chooser_entry->load_folder_handle = NULL;
1089 g_object_unref (chooser_entry->current_folder);
1090 chooser_entry->current_folder = NULL;
1093 gtk_file_path_free (chooser_entry->current_folder_path);
1094 chooser_entry->current_folder_path = gtk_file_path_copy (folder_path);
1099 chooser_entry->current_folder_path = gtk_file_path_copy (folder_path);
1104 start_loading_current_folder (chooser_entry);
1108 refresh_current_folder_and_file_part (GtkFileChooserEntry *chooser_entry,
1109 RefreshMode refresh_mode)
1111 GtkEditable *editable;
1114 GtkFilePath *folder_path;
1116 gsize total_len, file_part_len;
1119 editable = GTK_EDITABLE (chooser_entry);
1121 switch (refresh_mode)
1123 case REFRESH_UP_TO_CURSOR_POSITION:
1124 end_pos = gtk_editable_get_position (editable);
1127 case REFRESH_WHOLE_TEXT:
1128 end_pos = GTK_ENTRY (chooser_entry)->text_length;
1132 g_assert_not_reached ();
1136 text = gtk_editable_get_chars (editable, 0, end_pos);
1138 if (!chooser_entry->file_system ||
1139 !chooser_entry->base_folder ||
1140 !gtk_file_system_parse (chooser_entry->file_system,
1141 chooser_entry->base_folder, text,
1142 &folder_path, &file_part, NULL)) /* NULL-GError */
1144 folder_path = gtk_file_path_copy (chooser_entry->base_folder);
1145 file_part = g_strdup ("");
1150 file_part_len = strlen (file_part);
1151 total_len = strlen (text);
1152 if (total_len > file_part_len)
1153 file_part_pos = g_utf8_strlen (text, total_len - file_part_len);
1158 printf ("Parsed text \"%s\", file_part=\"%s\", file_part_pos=%d, folder_path=\"%s\"\n",
1162 folder_path ? (char *) folder_path : "(NULL)");
1166 g_free (chooser_entry->file_part);
1168 chooser_entry->file_part = file_part;
1169 chooser_entry->file_part_pos = file_part_pos;
1171 reload_current_folder (chooser_entry, folder_path, file_part_pos == -1);
1172 gtk_file_path_free (folder_path);
1176 autocomplete (GtkFileChooserEntry *chooser_entry)
1178 g_assert (chooser_entry->current_folder != NULL);
1179 g_assert (gtk_file_folder_is_finished_loading (chooser_entry->current_folder));
1180 g_assert (gtk_editable_get_position (GTK_EDITABLE (chooser_entry)) == GTK_ENTRY (chooser_entry)->text_length);
1182 printf ("Doing autocompletion since our folder is finished loading\n");
1184 append_common_prefix (chooser_entry, TRUE, FALSE);
1188 start_autocompletion (GtkFileChooserEntry *chooser_entry)
1190 printf ("Starting autocompletion\n");
1192 refresh_current_folder_and_file_part (chooser_entry, REFRESH_UP_TO_CURSOR_POSITION);
1194 if (!chooser_entry->current_folder)
1196 /* We don't beep or anything, since this is autocompletion - the user
1197 * didn't request any action explicitly.
1199 printf ("No current_folder; not doing autocompletion after all\n");
1203 if (gtk_file_folder_is_finished_loading (chooser_entry->current_folder))
1205 printf ("File folder is finished loading; doing autocompletion immediately\n");
1206 autocomplete (chooser_entry);
1210 printf ("File folder is not yet loaded; will do autocompletion later\n");
1211 chooser_entry->load_complete_action = LOAD_COMPLETE_AUTOCOMPLETE;
1216 start_autocompletion_idle_handler (gpointer data)
1218 GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (data);
1220 start_autocompletion (chooser_entry);
1222 chooser_entry->start_autocompletion_idle_id = 0;
1228 install_start_autocompletion_idle (GtkFileChooserEntry *chooser_entry)
1230 if (chooser_entry->start_autocompletion_idle_id != 0)
1233 chooser_entry->start_autocompletion_idle_id = g_idle_add (start_autocompletion_idle_handler, chooser_entry);
1238 insert_text_callback (GtkFileChooserEntry *chooser_entry,
1239 const gchar *new_text,
1240 gint new_text_length,
1244 const gchar *colon = memchr (new_text, ':', new_text_length);
1247 /* Disallow these characters altogether */
1248 for (i = 0; i < new_text_length; i++)
1250 if (new_text[i] == '<' ||
1251 new_text[i] == '>' ||
1252 new_text[i] == '"' ||
1253 new_text[i] == '|' ||
1254 new_text[i] == '*' ||
1259 if (i < new_text_length ||
1260 /* Disallow entering text that would cause a colon to be anywhere except
1261 * after a drive letter.
1264 *position + (colon - new_text) != 1) ||
1265 (new_text_length > 0 &&
1267 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1268 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':'))
1270 gtk_widget_error_bell (GTK_WIDGET (chooser_entry));
1271 g_signal_stop_emission_by_name (chooser_entry, "insert_text");
1279 delete_text_callback (GtkFileChooserEntry *chooser_entry,
1284 /* If deleting a drive letter, delete the colon, too */
1285 if (start_pos == 0 && end_pos == 1 &&
1286 GTK_ENTRY (chooser_entry)->text_length >= 2 &&
1287 gtk_entry_get_text (GTK_ENTRY (chooser_entry))[1] == ':')
1289 g_signal_handlers_block_by_func (chooser_entry,
1290 G_CALLBACK (delete_text_callback),
1292 gtk_editable_delete_text (GTK_EDITABLE (chooser_entry), 0, 1);
1293 g_signal_handlers_unblock_by_func (chooser_entry,
1294 G_CALLBACK (delete_text_callback),
1301 * _gtk_file_chooser_entry_new:
1302 * @eat_tabs: If %FALSE, allow focus navigation with the tab key.
1304 * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry
1305 * is an internal implementation widget for the GTK+ file chooser
1306 * which is an entry with completion with respect to a
1307 * #GtkFileSystem object.
1309 * Return value: the newly created #GtkFileChooserEntry
1312 _gtk_file_chooser_entry_new (gboolean eat_tabs)
1314 GtkFileChooserEntry *chooser_entry;
1316 chooser_entry = g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL);
1317 chooser_entry->eat_tabs = (eat_tabs != FALSE);
1319 return GTK_WIDGET (chooser_entry);
1323 * _gtk_file_chooser_entry_set_file_system:
1324 * @chooser_entry: a #GtkFileChooser
1325 * @file_system: an object implementing #GtkFileSystem
1327 * Sets the file system for @chooser_entry.
1330 _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry,
1331 GtkFileSystem *file_system)
1333 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1334 g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
1336 if (file_system != chooser_entry->file_system)
1338 if (chooser_entry->file_system)
1339 g_object_unref (chooser_entry->file_system);
1341 chooser_entry->file_system = g_object_ref (file_system);
1346 * _gtk_file_chooser_entry_set_base_folder:
1347 * @chooser_entry: a #GtkFileChooserEntry
1348 * @path: path of a folder in the chooser entries current file system.
1350 * Sets the folder with respect to which completions occur.
1353 _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry,
1354 const GtkFilePath *path)
1356 if (chooser_entry->base_folder)
1357 gtk_file_path_free (chooser_entry->base_folder);
1359 chooser_entry->base_folder = gtk_file_path_copy (path);
1361 clear_completions (chooser_entry);
1362 _gtk_file_chooser_entry_select_filename (chooser_entry);
1366 * _gtk_file_chooser_entry_get_current_folder:
1367 * @chooser_entry: a #GtkFileChooserEntry
1369 * Gets the current folder for the #GtkFileChooserEntry. If the
1370 * user has only entered a filename, this will be the base folder
1371 * (see _gtk_file_chooser_entry_set_base_folder()), but if the
1372 * user has entered a relative or absolute path, then it will
1373 * be different. If the user has entered a relative or absolute
1374 * path that doesn't point to a folder in the file system, it will
1377 * Return value: the path of current folder - this value is owned by the
1378 * chooser entry and must not be modified or freed.
1381 _gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry)
1383 if (chooser_entry->has_completion)
1385 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1386 GTK_ENTRY (chooser_entry)->text_length);
1388 return chooser_entry->current_folder_path;
1392 * _gtk_file_chooser_entry_get_file_part:
1393 * @chooser_entry: a #GtkFileChooserEntry
1395 * Gets the non-folder portion of whatever the user has entered
1396 * into the file selector. What is returned is a UTF-8 string,
1397 * and if a filename path is needed, gtk_file_system_make_path()
1400 * Return value: the entered filename - this value is owned by the
1401 * chooser entry and must not be modified or freed.
1404 _gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry)
1406 if (chooser_entry->has_completion)
1408 gtk_editable_set_position (GTK_EDITABLE (chooser_entry),
1409 GTK_ENTRY (chooser_entry)->text_length);
1412 refresh_current_folder_and_file_part (chooser_entry, REFRESH_WHOLE_TEXT);
1414 return chooser_entry->file_part;
1418 * _gtk_file_chooser_entry_set_file_part:
1419 * @chooser_entry: a #GtkFileChooserEntry
1420 * @file_part: text to display in the entry, in UTF-8
1422 * Sets the current text shown in the file chooser entry.
1425 _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry,
1426 const gchar *file_part)
1428 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1430 chooser_entry->in_change = TRUE;
1431 clear_completions (chooser_entry);
1432 gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part);
1433 chooser_entry->in_change = FALSE;
1438 * _gtk_file_chooser_entry_set_action:
1439 * @chooser_entry: a #GtkFileChooserEntry
1440 * @action: the action which is performed by the file selector using this entry
1442 * Sets action which is performed by the file selector using this entry.
1443 * The #GtkFileChooserEntry will use different completion strategies for
1444 * different actions.
1447 _gtk_file_chooser_entry_set_action (GtkFileChooserEntry *chooser_entry,
1448 GtkFileChooserAction action)
1450 g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry));
1452 if (chooser_entry->action != action)
1454 GtkEntryCompletion *comp;
1456 chooser_entry->action = action;
1458 comp = gtk_entry_get_completion (GTK_ENTRY (chooser_entry));
1462 case GTK_FILE_CHOOSER_ACTION_OPEN:
1463 case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
1464 gtk_entry_completion_set_popup_single_match (comp, FALSE);
1466 case GTK_FILE_CHOOSER_ACTION_SAVE:
1467 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
1468 gtk_entry_completion_set_popup_single_match (comp, TRUE);
1476 * _gtk_file_chooser_entry_get_action:
1477 * @chooser_entry: a #GtkFileChooserEntry
1479 * Gets the action for this entry.
1481 * Returns: the action
1483 GtkFileChooserAction
1484 _gtk_file_chooser_entry_get_action (GtkFileChooserEntry *chooser_entry)
1486 g_return_val_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry),
1487 GTK_FILE_CHOOSER_ACTION_OPEN);
1489 return chooser_entry->action;
1493 _gtk_file_chooser_entry_get_is_folder (GtkFileChooserEntry *chooser_entry,
1494 const GtkFilePath *path)
1496 gboolean retval = FALSE;
1498 if (chooser_entry->current_folder)
1500 GtkFileInfo *file_info;
1502 file_info = gtk_file_folder_get_info (chooser_entry->current_folder,
1506 retval = gtk_file_info_get_is_folder (file_info);
1507 gtk_file_info_free (file_info);
1516 * _gtk_file_chooser_entry_select_filename:
1517 * @chooser_entry: a #GtkFileChooserEntry
1519 * Selects the filename (without the extension) for user edition.
1522 _gtk_file_chooser_entry_select_filename (GtkFileChooserEntry *chooser_entry)
1524 const gchar *str, *ext;
1527 if (chooser_entry->action == GTK_FILE_CHOOSER_ACTION_SAVE)
1529 str = gtk_entry_get_text (GTK_ENTRY (chooser_entry));
1530 ext = g_strrstr (str, ".");
1533 len = g_utf8_pointer_to_offset (str, ext);
1536 gtk_editable_select_region (GTK_EDITABLE (chooser_entry), 0, (gint) len);