]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserwidget.c
handle GTK_DATADIR similar as the other filesystem placement 'constants'
[~andy/gtk] / gtk / gtkfilechooserwidget.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilechooserwidget.c: Embeddable file selector widget
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "gtkfilechooserwidget.h"
22 #include "gtkfilechooserdefault.h"
23 #include "gtkfilechooserutils.h"
24 #ifdef G_OS_UNIX
25 #include "gtkfilesystemunix.h"
26 #else if defined G_OS_WIN32
27 #include "gtkfilesystemwin32.h"
28 #endif
29 #include "gtktypebuiltins.h"
30
31 struct _GtkFileChooserWidgetPrivate
32 {
33   GtkWidget *impl;
34
35   GtkFileSystem *file_system;
36 };
37
38 #define GTK_FILE_CHOOSER_WIDGET_GET_PRIVATE(o)  (GTK_FILE_CHOOSER_WIDGET (o)->priv)
39
40 static void gtk_file_chooser_widget_class_init   (GtkFileChooserWidgetClass *class);
41 static void gtk_file_chooser_widget_init         (GtkFileChooserWidget      *chooser_widget);
42
43 static GObject* gtk_file_chooser_widget_constructor  (GType                  type,
44                                                       guint                  n_construct_properties,
45                                                       GObjectConstructParam *construct_params);
46 static void     gtk_file_chooser_widget_set_property (GObject               *object,
47                                                       guint                  prop_id,
48                                                       const GValue          *value,
49                                                       GParamSpec            *pspec);
50 static void     gtk_file_chooser_widget_get_property (GObject               *object,
51                                                       guint                  prop_id,
52                                                       GValue                *value,
53                                                       GParamSpec            *pspec);
54
55 static GObjectClass *parent_class;
56
57 GType
58 gtk_file_chooser_widget_get_type (void)
59 {
60   static GType file_chooser_widget_type = 0;
61
62   if (!file_chooser_widget_type)
63     {
64       static const GTypeInfo file_chooser_widget_info =
65       {
66         sizeof (GtkFileChooserWidgetClass),
67         NULL,           /* base_init */
68         NULL,           /* base_finalize */
69         (GClassInitFunc) gtk_file_chooser_widget_class_init,
70         NULL,           /* class_finalize */
71         NULL,           /* class_data */
72         sizeof (GtkFileChooserWidget),
73         0,              /* n_preallocs */
74         (GInstanceInitFunc) gtk_file_chooser_widget_init,
75       };
76       
77       static const GInterfaceInfo file_chooser_info =
78       {
79         (GInterfaceInitFunc) _gtk_file_chooser_delegate_iface_init, /* interface_init */
80         NULL,                                                       /* interface_finalize */
81         NULL                                                        /* interface_data */
82       };
83
84       file_chooser_widget_type = g_type_register_static (GTK_TYPE_VBOX, "GtkFileChooserWidget",
85                                                          &file_chooser_widget_info, 0);
86       g_type_add_interface_static (file_chooser_widget_type,
87                                    GTK_TYPE_FILE_CHOOSER,
88                                    &file_chooser_info);
89     }
90
91   return file_chooser_widget_type;
92 }
93
94 static void
95 gtk_file_chooser_widget_class_init (GtkFileChooserWidgetClass *class)
96 {
97   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
98
99   parent_class = g_type_class_peek_parent (class);
100
101   gobject_class->constructor = gtk_file_chooser_widget_constructor;
102   gobject_class->set_property = gtk_file_chooser_widget_set_property;
103   gobject_class->get_property = gtk_file_chooser_widget_get_property;
104
105   _gtk_file_chooser_install_properties (gobject_class);
106
107   g_type_class_add_private (class, sizeof (GtkFileChooserWidgetPrivate));
108 }
109
110 static void
111 gtk_file_chooser_widget_init (GtkFileChooserWidget *chooser_widget)
112 {
113   GtkFileChooserWidgetPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser_widget,
114                                                                    GTK_TYPE_FILE_CHOOSER_WIDGET,
115                                                                    GtkFileChooserWidgetPrivate);
116   chooser_widget->priv = priv;
117 }
118
119 static GObject*
120 gtk_file_chooser_widget_constructor (GType                  type,
121                                      guint                  n_construct_properties,
122                                      GObjectConstructParam *construct_params)
123 {
124   GtkFileChooserWidgetPrivate *priv;
125   GObject *object;
126   gchar *current_folder;
127   gchar *current_folder_uri;
128   
129   object = parent_class->constructor (type,
130                                       n_construct_properties,
131                                       construct_params);
132   priv = GTK_FILE_CHOOSER_WIDGET_GET_PRIVATE (object);
133
134   gtk_widget_push_composite_child ();
135
136   if (!priv->file_system)
137     {
138 #if defined G_OS_UNIX
139       priv->file_system = gtk_file_system_unix_new ();
140 #else if defined G_OS_WIN32
141       priv->file_system = gtk_file_system_win32_new ();
142 #endif
143     }
144       
145   priv->impl = _gtk_file_chooser_default_new (priv->file_system);
146   gtk_box_pack_start (GTK_BOX (object), priv->impl, TRUE, TRUE, 0);
147   gtk_widget_show (priv->impl);
148
149   current_folder = g_get_current_dir ();
150   current_folder_uri = g_filename_to_uri (current_folder, NULL, NULL);
151   if (current_folder_uri)
152     {
153       gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (priv->impl), current_folder_uri);
154       g_free (current_folder_uri);
155     }
156   g_free (current_folder);
157   
158   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
159                                   GTK_FILE_CHOOSER (priv->impl));
160   
161   gtk_widget_pop_composite_child ();
162
163   return object;
164 }
165
166 static void
167 gtk_file_chooser_widget_set_property (GObject         *object,
168                                       guint            prop_id,
169                                       const GValue    *value,
170                                       GParamSpec      *pspec)
171 {
172   GtkFileChooserWidgetPrivate *priv = GTK_FILE_CHOOSER_WIDGET_GET_PRIVATE (object);
173
174   switch (prop_id)
175     {
176     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM:
177       {
178         GtkFileSystem *file_system = g_value_get_object (value);
179         if (priv->file_system != file_system)
180           {
181             if (priv->file_system)
182               g_object_unref (priv->file_system);
183             priv->file_system = file_system;
184             if (priv->file_system)
185               g_object_ref (priv->file_system);
186           }
187       }
188       break;
189     default:
190       g_object_set_property (G_OBJECT (priv->impl), pspec->name, value);
191       break;
192     }
193 }
194
195 static void
196 gtk_file_chooser_widget_get_property (GObject         *object,
197                                       guint            prop_id,
198                                       GValue          *value,
199                                       GParamSpec      *pspec)
200 {
201   GtkFileChooserWidgetPrivate *priv = GTK_FILE_CHOOSER_WIDGET_GET_PRIVATE (object);
202   
203   g_object_get_property (G_OBJECT (priv->impl), pspec->name, value);
204 }
205
206 /**
207  * gtk_file_chooser_widget_new:
208  * @action: Open or save mode for the widget
209  * 
210  * Creates a new #GtkFileChooserWidget.  This is a file chooser widget that can
211  * be embedded in custom windows, and it is the same widget that is used by
212  * #GtkFileChooserDialog.
213  * 
214  * Return value: a new #GtkFileChooserWidget
215  *
216  * Since: 2.4
217  **/
218 GtkWidget *
219 gtk_file_chooser_widget_new (GtkFileChooserAction action)
220 {
221   return g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
222                        "action", action,
223                        NULL);
224 }