diff --git a/gtk/gtkfilechooserdefault.c b/gtk/gtkfilechooserdefault.c index 30d2d46a2..e1930ca51 100644 --- a/gtk/gtkfilechooserdefault.c +++ b/gtk/gtkfilechooserdefault.c @@ -1,5 +1,5 @@ /* GTK - The GIMP Toolkit - * gtkfilechooserwidget.c: Embeddable file selector widget + * gtkfilechooserimpldefault.c: Default implementation of GtkFileChooser * Copyright (C) 2003, Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -19,12 +19,15 @@ */ #include "gtkfilechooserimpldefault.h" +#include "gtkfilechooserentry.h" #include "gtkfilechooserenums.h" #include "gtkfilechooserutils.h" #include "gtkfilechooser.h" #include "gtkfilesystemmodel.h" #include +#include +#include #include #include #include @@ -69,12 +72,14 @@ struct _GtkFileChooserImplDefault GtkWidget *tree; GtkWidget *list_scrollwin; GtkWidget *list; + GtkWidget *entry; GtkWidget *preview_widget; }; static void gtk_file_chooser_impl_default_class_init (GtkFileChooserImplDefaultClass *class); static void gtk_file_chooser_impl_default_iface_init (GtkFileChooserIface *iface); static void gtk_file_chooser_impl_default_init (GtkFileChooserImplDefault *impl); +static void gtk_file_chooser_impl_default_finalize (GObject *object); static void gtk_file_chooser_impl_default_set_property (GObject *object, guint prop_id, const GValue *value, @@ -99,6 +104,8 @@ static void tree_selection_changed (GtkTreeSelection *tree_selection, GtkFileChooserImplDefault *impl); static void list_selection_changed (GtkTreeSelection *tree_selection, GtkFileChooserImplDefault *impl); +static void entry_activate (GtkEntry *entry, + GtkFileChooserImplDefault *impl); GType _gtk_file_chooser_impl_default_get_type (void) @@ -142,6 +149,7 @@ gtk_file_chooser_impl_default_class_init (GtkFileChooserImplDefaultClass *class) { GObjectClass *gobject_class = G_OBJECT_CLASS (class); + gobject_class->finalize = gtk_file_chooser_impl_default_finalize; gobject_class->set_property = gtk_file_chooser_impl_default_set_property; gobject_class->get_property = gtk_file_chooser_impl_default_get_property; @@ -164,13 +172,20 @@ static void gtk_file_chooser_impl_default_init (GtkFileChooserImplDefault *impl) { GtkWidget *hpaned; + GtkWidget *hbox; + GtkWidget *label; GtkTreeSelection *selection; +#if 0 + GList *focus_chain; +#endif impl->folder_mode = FALSE; impl->local_only = TRUE; impl->preview_widget_active = TRUE; impl->select_multiple = FALSE; impl->show_hidden = FALSE; + + gtk_container_set_border_width (GTK_CONTAINER (impl), 5); gtk_widget_push_composite_child (); @@ -181,6 +196,8 @@ gtk_file_chooser_impl_default_init (GtkFileChooserImplDefault *impl) impl->tree_scrollwin = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->tree_scrollwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->tree_scrollwin), + GTK_SHADOW_IN); gtk_paned_add1 (GTK_PANED (hpaned), impl->tree_scrollwin); gtk_widget_show (impl->tree_scrollwin); @@ -199,6 +216,8 @@ gtk_file_chooser_impl_default_init (GtkFileChooserImplDefault *impl) impl->list_scrollwin = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (impl->list_scrollwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (impl->list_scrollwin), + GTK_SHADOW_IN); gtk_paned_add2 (GTK_PANED (hpaned), impl->list_scrollwin); gtk_widget_show (impl->list_scrollwin); @@ -210,10 +229,44 @@ gtk_file_chooser_impl_default_init (GtkFileChooserImplDefault *impl) selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list)); g_signal_connect (selection, "changed", G_CALLBACK (list_selection_changed), impl); + + hbox = gtk_hbox_new (FALSE, 6); + gtk_box_pack_start (GTK_BOX (impl), hbox, FALSE, FALSE, 6); + gtk_widget_show (hbox); + + label = gtk_label_new_with_mnemonic ("_Location:"); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); + gtk_widget_show (label); + + impl->entry = _gtk_file_chooser_entry_new (); + gtk_entry_set_activates_default (GTK_ENTRY (impl->entry), TRUE); + g_signal_connect (impl->entry, "activate", + G_CALLBACK (entry_activate), impl); + + gtk_box_pack_start (GTK_BOX (hbox), impl->entry, TRUE, TRUE, 0); + gtk_widget_show (impl->entry); + + gtk_label_set_mnemonic_widget (GTK_LABEL (label), impl->entry); + +#if 0 + focus_chain = g_list_append (NULL, impl->entry); + focus_chain = g_list_append (focus_chain, impl->tree); + focus_chain = g_list_append (focus_chain, impl->list); + gtk_container_set_focus_chain (GTK_CONTAINER (impl), focus_chain); + g_list_free (focus_chain); +#endif gtk_widget_pop_composite_child (); } +static void +gtk_file_chooser_impl_default_finalize (GObject *object) +{ + GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (object); + + g_object_unref (impl->file_system); +} + static void set_preview_widget (GtkFileChooserImplDefault *impl, GtkWidget *preview_widget) @@ -356,7 +409,7 @@ expand_and_select_func (GtkFileSystemModel *model, gtk_tree_view_expand_to_path (tree_view, path); gtk_tree_view_expand_row (tree_view, path, FALSE); gtk_tree_view_set_cursor (tree_view, path, NULL, FALSE); - gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->tree), path, NULL, TRUE, 0.3, 0.0); + gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (impl->tree), path, NULL, TRUE, 0.3, 0.5); } static void @@ -364,6 +417,7 @@ gtk_file_chooser_impl_default_set_current_folder (GtkFileChooser *chooser, const char *uri) { GtkFileChooserImplDefault *impl = GTK_FILE_CHOOSER_IMPL_DEFAULT (chooser); + _gtk_file_system_model_uri_do (impl->tree_model, uri, expand_and_select_func, impl); } @@ -592,6 +646,27 @@ open_and_close (GtkTreeView *tree_view, gtk_tree_path_free (path); } +static void +update_chooser_entry (GtkFileChooserImplDefault *impl) +{ + GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (impl->list)); + const GtkFileInfo *info; + GtkTreeIter iter; + GtkTreeIter child_iter; + + if (!gtk_tree_selection_get_selected (selection, NULL, &iter)) + return; + + gtk_tree_model_sort_convert_iter_to_child_iter (impl->sort_model, + &child_iter, + &iter); + + info = _gtk_file_system_model_get_info (impl->list_model, &child_iter); + + _gtk_file_chooser_entry_set_file_part (GTK_FILE_CHOOSER_ENTRY (impl->entry), + gtk_file_info_get_display_name (info)); +} + static void tree_selection_changed (GtkTreeSelection *selection, GtkFileChooserImplDefault *impl) @@ -610,6 +685,7 @@ tree_selection_changed (GtkTreeSelection *selection, if (impl->current_folder) g_free (impl->current_folder); impl->current_folder = g_strdup (uri); + _gtk_file_chooser_entry_set_base_folder (GTK_FILE_CHOOSER_ENTRY (impl->entry), uri); if (impl->list_model) { @@ -647,18 +723,85 @@ tree_selection_changed (GtkTreeSelection *selection, GTK_TREE_MODEL (impl->sort_model)); gtk_tree_view_set_search_column (GTK_TREE_VIEW (impl->list), GTK_FILE_SYSTEM_MODEL_DISPLAY_NAME); - + g_signal_emit_by_name (impl, "current_folder_changed", 0); + + update_chooser_entry (impl); + g_signal_emit_by_name (impl, "selection_changed", 0); + } static void list_selection_changed (GtkTreeSelection *selection, GtkFileChooserImplDefault *impl) { + update_chooser_entry (impl); + g_signal_emit_by_name (impl, "selection_changed", 0); } +static void +entry_activate (GtkEntry *entry, + GtkFileChooserImplDefault *impl) +{ + GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (entry); + const gchar *folder_uri = _gtk_file_chooser_entry_get_current_folder (chooser_entry); + const gchar *file_part = _gtk_file_chooser_entry_get_file_part (chooser_entry); + gchar *new_folder = NULL; + + /* If the file part is non-empty, we need to figure out if it + * refers to a folder within folder. We could optimize the case + * here where the folder is already loaded for one of our tree models. + */ + if (file_part[0] == '\0' && strcmp (impl->current_folder, folder_uri) != 0) + new_folder = g_strdup (folder_uri); + else + { + GtkFileFolder *folder = NULL; + gchar *subfolder_uri = NULL; + GtkFileInfo *info = NULL; + + folder = gtk_file_system_get_folder (impl->file_system, + folder_uri, + GTK_FILE_INFO_IS_FOLDER, + NULL); /* NULL-GError */ + + if (folder) + subfolder_uri = gtk_file_system_make_uri (impl->file_system, + folder_uri, + file_part, + NULL); /* NULL-GError */ + + if (subfolder_uri) + info = gtk_file_folder_get_info (folder, + subfolder_uri, + NULL); /* NULL-GError */ + + if (info && gtk_file_info_get_is_folder (info)) + new_folder = g_strdup (subfolder_uri); + + if (folder) + g_object_unref (folder); + + if (subfolder_uri) + g_free (subfolder_uri); + + if (info) + gtk_file_info_free (info); + } + + if (new_folder) + { + g_signal_stop_emission_by_name (entry, "activate"); + + gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (impl), new_folder); + _gtk_file_chooser_entry_set_file_part (chooser_entry, ""); + + g_free (new_folder); + } +} + const GtkFileInfo * get_list_file_info (GtkFileChooserImplDefault *impl, GtkTreeIter *iter) @@ -749,7 +892,7 @@ _gtk_file_chooser_impl_default_new (GtkFileSystem *file_system) GtkTreeViewColumn *column; GtkCellRenderer *renderer; - impl->file_system = file_system; + impl->file_system = g_object_ref(file_system); impl->tree_model = _gtk_file_system_model_new (file_system, NULL, -1, GTK_FILE_INFO_DISPLAY_NAME); _gtk_file_system_model_set_show_files (impl->tree_model, FALSE); @@ -782,5 +925,8 @@ _gtk_file_chooser_impl_default_new (GtkFileSystem *file_system) gtk_tree_view_column_set_sort_column_id (column, 1); gtk_tree_view_append_column (GTK_TREE_VIEW (impl->list), column); + _gtk_file_chooser_entry_set_file_system (GTK_FILE_CHOOSER_ENTRY (impl->entry), + file_system); + return result; } diff --git a/gtk/gtkfilechooserdefault.h b/gtk/gtkfilechooserdefault.h index a5416dd1a..cd30f890c 100644 --- a/gtk/gtkfilechooserdefault.h +++ b/gtk/gtkfilechooserdefault.h @@ -1,5 +1,5 @@ /* GTK - The GIMP Toolkit - * gtkfilechooserwidget.h: Embeddable file selector widget + * gtkfilechooserimpldefault.h: Default implementation of GtkFileChooser * Copyright (C) 2003, Red Hat, Inc. * * This library is free software; you can redistribute it and/or diff --git a/gtk/gtkfilechooserentry.c b/gtk/gtkfilechooserentry.c new file mode 100644 index 000000000..c1af383ee --- /dev/null +++ b/gtk/gtkfilechooserentry.c @@ -0,0 +1,484 @@ +/* GTK - The GIMP Toolkit + * gtkfilechooserentry.c: Entry with filename completion + * Copyright (C) 2003, Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "gtkfilechooserentry.h" + +#include + +#include + +typedef struct _GtkFileChooserEntryClass GtkFileChooserEntryClass; + +#define GTK_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass)) +#define GTK_IS_FILE_CHOOSER_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_ENTRY)) +#define GTK_FILE_CHOOSER_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntryClass)) + +struct _GtkFileChooserEntryClass +{ + GtkEntryClass parent_class; +}; + +struct _GtkFileChooserEntry +{ + GtkEntry parent_instance; + + GtkFileSystem *file_system; + gchar *base_folder; + gchar *current_folder_uri; + gchar *file_part; + GSource *completion_idle; + + GtkFileFolder *current_folder; + + guint in_change : 1; + guint has_completion : 1; +}; + +static void gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class); +static void gtk_file_chooser_entry_iface_init (GtkEditableClass *iface); +static void gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry); + +static void gtk_file_chooser_entry_finalize (GObject *object); +static gboolean gtk_file_chooser_entry_focus (GtkWidget *widget, + GtkDirectionType direction); +static void gtk_file_chooser_entry_changed (GtkEditable *editable); +static void gtk_file_chooser_entry_do_insert_text (GtkEditable *editable, + const gchar *new_text, + gint new_text_length, + gint *position); + +static void clear_completion_callback (GtkFileChooserEntry *chooser_entry, + GParamSpec *pspec); + +GObjectClass *parent_class; +GtkEditableClass *parent_editable_iface; + +GType +_gtk_file_chooser_entry_get_type (void) +{ + static GType file_chooser_entry_type = 0; + + if (!file_chooser_entry_type) + { + static const GTypeInfo file_chooser_entry_info = + { + sizeof (GtkFileChooserEntryClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) gtk_file_chooser_entry_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (GtkFileChooserEntry), + 0, /* n_preallocs */ + (GInstanceInitFunc) gtk_file_chooser_entry_init, + }; + + static const GInterfaceInfo editable_info = + { + (GInterfaceInitFunc) gtk_file_chooser_entry_iface_init, /* interface_init */ + NULL, /* interface_finalize */ + NULL /* interface_data */ + }; + + + file_chooser_entry_type = g_type_register_static (GTK_TYPE_ENTRY, "GtkFileChooserEntry", + &file_chooser_entry_info, 0); + g_type_add_interface_static (file_chooser_entry_type, + GTK_TYPE_EDITABLE, + &editable_info); + } + + + return file_chooser_entry_type; +} + +static void +gtk_file_chooser_entry_class_init (GtkFileChooserEntryClass *class) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (class); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class); + + parent_class = g_type_class_peek_parent (class); + + gobject_class->finalize = gtk_file_chooser_entry_finalize; + + widget_class->focus = gtk_file_chooser_entry_focus; +} + +static void +gtk_file_chooser_entry_iface_init (GtkEditableClass *iface) +{ + parent_editable_iface = g_type_interface_peek_parent (iface); + + iface->do_insert_text = gtk_file_chooser_entry_do_insert_text; + iface->changed = gtk_file_chooser_entry_changed; +} + +static void +gtk_file_chooser_entry_init (GtkFileChooserEntry *chooser_entry) +{ + g_signal_connect (chooser_entry, "notify::cursor-position", + G_CALLBACK (clear_completion_callback), NULL); + g_signal_connect (chooser_entry, "notify::selection-bound", + G_CALLBACK (clear_completion_callback), NULL); +} + +static void +gtk_file_chooser_entry_finalize (GObject *object) +{ + GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (object); + + if (chooser_entry->current_folder) + g_object_unref (chooser_entry->current_folder); + + if (chooser_entry->file_system) + g_object_unref (chooser_entry->file_system); + + g_free (chooser_entry->base_folder); + g_free (chooser_entry->current_folder_uri); + g_free (chooser_entry->file_part); + + parent_class->finalize (object); +} + +static gboolean +completion_idle_callback (GtkFileChooserEntry *chooser_entry) +{ + GtkEditable *editable = GTK_EDITABLE (chooser_entry); + GSList *child_uris = NULL; + GSList *tmp_list; + gchar *common_prefix = NULL; + gchar *unique_uri = NULL; + + chooser_entry->completion_idle = NULL; + + if (!chooser_entry->current_folder && + chooser_entry->file_system && + chooser_entry->current_folder_uri) + chooser_entry->current_folder = gtk_file_system_get_folder (chooser_entry->file_system, + chooser_entry->current_folder_uri, + GTK_FILE_INFO_DISPLAY_NAME | GTK_FILE_INFO_IS_FOLDER, + NULL); /* NULL-GError */ + if (chooser_entry->current_folder) + gtk_file_folder_list_children (chooser_entry->current_folder, + &child_uris, + NULL); /* NULL-GError */ + + for (tmp_list = child_uris; tmp_list; tmp_list = tmp_list->next) + { + GtkFileInfo *info; + + info = gtk_file_folder_get_info (chooser_entry->current_folder, + tmp_list->data, + NULL); /* NULL-GError */ + if (info) + { + const gchar *display_name = gtk_file_info_get_display_name (info); + + if (g_str_has_prefix (display_name, chooser_entry->file_part)) + { + if (!common_prefix) + { + common_prefix = g_strdup (display_name); + unique_uri = g_strdup (tmp_list->data); + } + else + { + gchar *p = common_prefix; + const gchar *q = display_name; + + while (*p && *p == *q) + { + p++; + q++; + } + + *p = '\0'; + + g_free (unique_uri); + unique_uri = NULL; + } + } + + gtk_file_info_free (info); + } + } + + if (unique_uri) + { + GtkFileInfo *info; + + info = gtk_file_folder_get_info (chooser_entry->current_folder, + unique_uri, + NULL); /* NULL-GError */ + + if (info) + { + if (gtk_file_info_get_is_folder (info)) + { + gchar *tmp = common_prefix; + common_prefix = g_strconcat (tmp, "/", NULL); + g_free (tmp); + } + + gtk_file_info_free (info); + } + + g_free (unique_uri); + } + + g_slist_foreach (child_uris, (GFunc)g_free, NULL); + g_slist_free (child_uris); + + if (common_prefix) + { + gint total_len; + gint file_part_len; + gint common_prefix_len; + gint pos; + + total_len = g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (chooser_entry)), -1); + file_part_len = g_utf8_strlen (chooser_entry->file_part, -1); + common_prefix_len = g_utf8_strlen (common_prefix, -1); + + if (common_prefix_len > file_part_len) + { + chooser_entry->in_change = TRUE; + + pos = total_len - file_part_len; + gtk_editable_delete_text (editable, + pos, -1); + gtk_editable_insert_text (editable, + common_prefix, -1, + &pos); + gtk_editable_select_region (editable, + total_len, + total_len - file_part_len + common_prefix_len); + + chooser_entry->in_change = FALSE; + chooser_entry->has_completion = TRUE; + } + + g_free (common_prefix); + } + + return FALSE; +} + +static void +gtk_file_chooser_entry_do_insert_text (GtkEditable *editable, + const gchar *new_text, + gint new_text_length, + gint *position) +{ + GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable); + + parent_editable_iface->do_insert_text (editable, new_text, new_text_length, position); + + if (!chooser_entry->in_change && + *position == GTK_ENTRY (editable)->text_length && + !chooser_entry->completion_idle) + { + chooser_entry->completion_idle = g_idle_source_new (); + g_source_set_priority (chooser_entry->completion_idle, G_PRIORITY_HIGH); + g_source_set_closure (chooser_entry->completion_idle, + g_cclosure_new_object (G_CALLBACK (completion_idle_callback), + G_OBJECT (chooser_entry))); + g_source_attach (chooser_entry->completion_idle, NULL); + } +} + +static gboolean +gtk_file_chooser_entry_focus (GtkWidget *widget, + GtkDirectionType direction) +{ + GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (widget); + + if (direction == GTK_DIR_TAB_FORWARD && + GTK_WIDGET_HAS_FOCUS (widget) && + chooser_entry->has_completion) + { + gtk_editable_set_position (GTK_EDITABLE (widget), + GTK_ENTRY (widget)->text_length); + return TRUE; + } + else + return GTK_WIDGET_CLASS (parent_class)->focus (widget, direction); +} + +static void +gtk_file_chooser_entry_changed (GtkEditable *editable) +{ + GtkFileChooserEntry *chooser_entry = GTK_FILE_CHOOSER_ENTRY (editable); + const gchar *text; + gchar *folder_uri; + gchar *file_part; + + text = gtk_entry_get_text (GTK_ENTRY (editable)); + + if (!chooser_entry->file_system || + !chooser_entry->base_folder || + !gtk_file_system_parse (chooser_entry->file_system, + chooser_entry->base_folder, text, + &folder_uri, &file_part, NULL)) /* NULL-GError */ + { + folder_uri = g_strdup (chooser_entry->base_folder); + file_part = g_strdup (""); + } + + if (chooser_entry->current_folder_uri && + strcmp (folder_uri, chooser_entry->current_folder_uri) != 0) + { + if (chooser_entry->current_folder_uri) + g_free (chooser_entry->current_folder_uri); + if (chooser_entry->current_folder) + { + g_object_unref (chooser_entry->current_folder); + chooser_entry->current_folder = NULL; + } + } + + chooser_entry->current_folder_uri = folder_uri; + + if (chooser_entry->file_part) + g_free (chooser_entry->file_part); + + chooser_entry->file_part = file_part; +} + +static void +clear_completion_callback (GtkFileChooserEntry *chooser_entry, + GParamSpec *pspec) +{ + chooser_entry->has_completion = FALSE; +} + +/** + * _gtk_file_chooser_entry_new: + * + * Creates a new #GtkFileChooserEntry object. #GtkFileChooserEntry + * is an internal implementation widget for the GTK+ file chooser + * which is an entry with completion with respect to a + * #GtkFileSystem object. + * + * Return value: the newly created #GtkFileChooserEntry + **/ +GtkWidget * +_gtk_file_chooser_entry_new (void) +{ + return g_object_new (GTK_TYPE_FILE_CHOOSER_ENTRY, NULL); +} + +/** + * _gtk_file_chooser_entry_set_file_system: + * @chooser_entry: a #GtkFileChooser + * @file_system: an object implementing #GtkFileSystem + * + * Sets the file system for @chooser_entry. + **/ +void +_gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry, + GtkFileSystem *file_system) +{ + g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry)); + g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system)); + + if (file_system != chooser_entry->file_system) + { + if (chooser_entry->file_system) + g_object_unref (chooser_entry->file_system); + + chooser_entry->file_system = g_object_ref (file_system); + } +} + +/** + * _gtk_file_chooser_entry_set_base_folder: + * @chooser_entry: a #GtkFileChooserEntry + * @uri: URI of a folder in the chooser entries current file system. + * + * Sets the folder with respect to which completions occur. + **/ +void +_gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry, + const gchar *uri) +{ + if (chooser_entry->base_folder) + g_free (chooser_entry->base_folder); + + chooser_entry->base_folder = g_strdup (uri); +} + +/** + * _gtk_file_chooser_entry_get_current_folder: + * @chooser_entry: a #GtkFileChooserEntry + * + * Gets the current folder for the #GtkFileChooserEntry. If the + * user has only entered a filename, this will be the base folder + * (see _gtk_file_chooser_entry_set_base_folder()), but if the + * user has entered a relative or absolute path, then it will + * be different. If the user has entered a relative or absolute + * path that doesn't point to a folder in the file system, it will + * be %NULL. + * + * Return value: the URI of current folder - this value is owned by the + * chooser entry and must not be modified or freed. + **/ +const gchar * +_gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry) +{ + return chooser_entry->current_folder_uri; +} + +/** + * _gtk_file_chooser_entry_get_file_part: + * @chooser_entry: a #GtkFileChooserEntry + * + * Gets the non-folder portion of whatever the user has entered + * into the file selector. What is returned is a UTF-8 string, + * and if a filename URI is needed, gtk_file_system_make_uri() + * must be used + * + * Return value: the entered filename - this value is owned by the + * chooser entry and must not be modified or freed. + **/ +const gchar * +_gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry) +{ + return chooser_entry->file_part; +} + +/** + * _gtk_file_chooser_entry_set_file_part: + * @chooser_entry: a #GtkFileChooserEntry + * @file_part: text to display in the entry, in UTF-8 + * + * Sets the current text shown in the file chooser entry. + **/ +void +_gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry, + const gchar *file_part) +{ + g_return_if_fail (GTK_IS_FILE_CHOOSER_ENTRY (chooser_entry)); + + chooser_entry->in_change = TRUE; + gtk_entry_set_text (GTK_ENTRY (chooser_entry), file_part); + chooser_entry->in_change = FALSE; +} diff --git a/gtk/gtkfilechooserentry.h b/gtk/gtkfilechooserentry.h new file mode 100644 index 000000000..866f72500 --- /dev/null +++ b/gtk/gtkfilechooserentry.h @@ -0,0 +1,48 @@ +/* GTK - The GIMP Toolkit + * gtkfilechooserentry.h: Entry with filename completion + * Copyright (C) 2003, Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GTK_FILE_CHOOSER_ENTRY_H__ +#define __GTK_FILE_CHOOSER_ENTRY_H__ + +#include "gtkfilesystem.h" +#include + +G_BEGIN_DECLS + +#define GTK_TYPE_FILE_CHOOSER_ENTRY (_gtk_file_chooser_entry_get_type ()) +#define GTK_FILE_CHOOSER_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY, GtkFileChooserEntry)) +#define GTK_IS_FILE_CHOOSER_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_CHOOSER_ENTRY)) + +typedef struct _GtkFileChooserEntry GtkFileChooserEntry; + +GType _gtk_file_chooser_entry_get_type (void); +GtkWidget * _gtk_file_chooser_entry_new (void); +void _gtk_file_chooser_entry_set_file_system (GtkFileChooserEntry *chooser_entry, + GtkFileSystem *file_system); +void _gtk_file_chooser_entry_set_base_folder (GtkFileChooserEntry *chooser_entry, + const gchar *uri); +void _gtk_file_chooser_entry_set_file_part (GtkFileChooserEntry *chooser_entry, + const gchar *file_part); +const gchar *_gtk_file_chooser_entry_get_current_folder (GtkFileChooserEntry *chooser_entry); +const gchar *_gtk_file_chooser_entry_get_file_part (GtkFileChooserEntry *chooser_entry); + +G_END_DECLS + +#endif /* __GTK_FILE_CHOOSER_ENTRY_H__ */ diff --git a/gtk/gtkfilesystem.c b/gtk/gtkfilesystem.c index f2faa570f..492895d00 100644 --- a/gtk/gtkfilesystem.c +++ b/gtk/gtkfilesystem.c @@ -346,7 +346,9 @@ gtk_file_system_get_parent (GtkFileSystem *file_system, if (parent) *parent = tmp_parent; - + else + g_free (tmp_parent); + return result; } @@ -364,6 +366,71 @@ gtk_file_system_make_uri (GtkFileSystem *file_system, return GTK_FILE_SYSTEM_GET_IFACE (file_system)->make_uri (file_system, base_uri, display_name, error); } +/** + * gtk_file_system_parse: + * @file_system: a #GtkFileSystem + * @base_uri: reference folder with respect to which relative + * paths should be interpreted. + * @str: the string to parse + * @folder: location to store folder portion of result, or %NULL + * @file_part: location to store file portion of result, or %NULL + * @error: location to store error, or %NULL + * + * Given a string entered by a user, parse it (possibly using + * heuristics) into a folder URI and a UTF-8 encoded + * filename part. (Suitable for passing to gtk_file_system_make_uri()) + * + * Note that the returned filename point may point to a subfolder + * of the returned folder. Adding a trailing path separator is needed + * to enforce the interpretation as a folder name. + * + * If parsing fails because the syntax of @str is not understood, + * and error of type GTK_FILE_SYSTEM_ERROR_BAD_FILENAME will + * be set in @error and %FALSE returned. + * + * If parsing fails because a path was encountered that doesn't + * exist on the filesystem, then an error of type + * %GTK_FILE_SYSTEM_ERROR_NONEXISTANT will be set in @error + * and %FALSE returned. (This only applies to parsing relative paths, + * not to interpretation of @file_part. No check is made as + * to whether @file_part exists.) + * + * Return value: %TRUE if the parsing succeeds, otherwise, %FALSE. + **/ +gboolean +gtk_file_system_parse (GtkFileSystem *file_system, + const gchar *base_uri, + const gchar *str, + gchar **folder, + gchar **file_part, + GError **error) +{ + gchar *tmp_folder = NULL; + gchar *tmp_file_part = NULL; + gboolean result; + + g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE); + g_return_val_if_fail (base_uri != NULL, FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + + result = GTK_FILE_SYSTEM_GET_IFACE (file_system)->parse (file_system, base_uri, str, + &tmp_folder, &tmp_file_part, + error); + g_assert (result || (tmp_folder == NULL && tmp_file_part == NULL)); + + if (folder) + *folder = tmp_folder; + else + g_free (tmp_folder); + + if (file_part) + *file_part = tmp_file_part; + else + g_free (tmp_file_part); + + return result; +} /***************************************** * GtkFileFolder * @@ -448,6 +515,11 @@ gtk_file_folder_list_children (GtkFileFolder *folder, if (children) *children = tmp_children; + else + { + g_slist_foreach (tmp_children, (GFunc)g_free, NULL); + g_slist_free (tmp_children); + } return result; } diff --git a/gtk/gtkfilesystem.h b/gtk/gtkfilesystem.h index bd349a26c..913d5149d 100644 --- a/gtk/gtkfilesystem.h +++ b/gtk/gtkfilesystem.h @@ -133,6 +133,12 @@ struct _GtkFileSystemIface gchar * (*make_uri) (GtkFileSystem *file_system, const gchar *base_uri, const gchar *display_name, + GError **error); + gboolean (*parse) (GtkFileSystem *file_system, + const gchar *base_uri, + const gchar *str, + gchar **folder, + gchar **file_part, GError **error); /* Signals */ @@ -162,6 +168,12 @@ gchar * gtk_file_system_make_uri (GtkFileSystem *file_system, const gchar *base_uri, const gchar *display_name, GError **error); +gboolean gtk_file_system_parse (GtkFileSystem *file_system, + const gchar *base_uri, + const gchar *str, + gchar **folder, + gchar **file_part, + GError **error); /* * Detailed information about a particular folder */ diff --git a/gtk/gtkfilesystemunix.c b/gtk/gtkfilesystemunix.c index 69f2d9e6c..34e173dc3 100644 --- a/gtk/gtkfilesystemunix.c +++ b/gtk/gtkfilesystemunix.c @@ -91,6 +91,12 @@ static gchar * gtk_file_system_unix_make_uri (GtkFileSystem *file const gchar *base_uri, const gchar *display_name, GError **error); +static gboolean gtk_file_system_unix_parse (GtkFileSystem *file_system, + const gchar *base_uri, + const gchar *str, + gchar **folder, + gchar **file_part, + GError **error); static GType gtk_file_folder_unix_get_type (void); static void gtk_file_folder_unix_class_init (GtkFileFolderUnixClass *class); @@ -186,6 +192,7 @@ gtk_file_system_unix_iface_init (GtkFileSystemIface *iface) iface->create_folder = gtk_file_system_unix_create_folder; iface->get_parent = gtk_file_system_unix_get_parent; iface->make_uri = gtk_file_system_unix_make_uri; + iface->parse = gtk_file_system_unix_parse; } static void @@ -333,6 +340,149 @@ gtk_file_system_unix_make_uri (GtkFileSystem *file_system, return result; } +/* If this was a publically exported function, it should return + * a dup'ed result, but we make it modify-in-place for efficiency + * here, and because it works for us. + */ +static void +canonicalize_filename (gchar *filename) +{ + gchar *p, *q; + gboolean last_was_slash = FALSE; + + p = filename; + q = filename; + + while (*p) + { + if (*p == G_DIR_SEPARATOR) + { + if (!last_was_slash) + *q++ = G_DIR_SEPARATOR; + + last_was_slash = TRUE; + } + else + { + if (last_was_slash && *p == '.') + { + if (*(p + 1) == G_DIR_SEPARATOR || + *(p + 1) == '\0') + { + if (*(p + 1) == '\0') + break; + + p += 1; + } + else if (*(p + 1) == '.' && + (*(p + 2) == G_DIR_SEPARATOR || + *(p + 2) == '\0')) + { + if (q > filename + 1) + { + q--; + while (q > filename + 1 && + *(q - 1) != G_DIR_SEPARATOR) + q--; + } + + if (*(p + 2) == '\0') + break; + + p += 2; + } + else + { + *q++ = *p; + last_was_slash = FALSE; + } + } + else + { + *q++ = *p; + last_was_slash = FALSE; + } + } + + p++; + } + + if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR) + q--; + + *q = '\0'; +} + +static gboolean +gtk_file_system_unix_parse (GtkFileSystem *file_system, + const gchar *base_uri, + const gchar *str, + gchar **folder, + gchar **file_part, + GError **error) +{ + char *base_filename; + gchar *last_slash; + gboolean result = FALSE; + + base_filename = filename_from_uri (base_uri, error); + if (!base_filename) + return FALSE; + + last_slash = strrchr (str, G_DIR_SEPARATOR); + if (!last_slash) + { + *folder = g_strdup (base_uri); + *file_part = g_strdup (str); + result = TRUE; + } + else + { + gchar *folder_part; + gchar *folder_path; + GError *tmp_error = NULL; + + if (last_slash == str) + folder_part = g_strdup ("/"); + else + folder_part = g_filename_from_utf8 (str, last_slash - str, + NULL, NULL, &tmp_error); + + if (!folder_part) + { + g_set_error (error, + GTK_FILE_SYSTEM_ERROR, + GTK_FILE_SYSTEM_ERROR_BAD_FILENAME, + "%s", + tmp_error->message); + g_error_free (tmp_error); + } + else + { + if (folder_part[0] == G_DIR_SEPARATOR) + folder_path = folder_part; + else + { + folder_path = g_build_filename (base_filename, folder_part, NULL); + g_free (folder_part); + } + + canonicalize_filename (folder_path); + + *folder = filename_to_uri (folder_path); + *file_part = g_strdup (last_slash + 1); + + g_free (folder_path); + + result = TRUE; + } + } + + g_free (base_filename); + + return result; +} + /* * GtkFileFolderUnix */ diff --git a/tests/testfilechooser.c b/tests/testfilechooser.c index b44f3efe4..bfdaa7233 100644 --- a/tests/testfilechooser.c +++ b/tests/testfilechooser.c @@ -30,6 +30,13 @@ print_selected (GtkFileChooser *chooser) g_slist_free (uris); } +static void +response_cb (GtkDialog *dialog, + gint response_id) +{ + gtk_main_quit (); +} + int main (int argc, char **argv) { @@ -46,10 +53,14 @@ main (int argc, char **argv) GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); + gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK); + g_signal_connect (dialog, "selection_changed", G_CALLBACK (print_selected), NULL); g_signal_connect (dialog, "current_folder_changed", G_CALLBACK (print_current_folder), NULL); + g_signal_connect (dialog, "response", + G_CALLBACK (response_cb), NULL); gtk_window_set_default_size (GTK_WINDOW (dialog), 600, 400); gtk_widget_show (dialog);