]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserwidget.c
Start a set of automated tests for the file chooser. The only test in
[~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 <config.h>
22 #include "gtkfilechooserprivate.h"
23 #include "gtkfilechooserwidget.h"
24 #include "gtkfilechooserdefault.h"
25 #include "gtkfilechooserutils.h"
26 #include "gtktypebuiltins.h"
27 #include "gtkfilechooserembed.h"
28 #include "gtkintl.h"
29 #include "gtkalias.h"
30
31 #define GTK_FILE_CHOOSER_WIDGET_GET_PRIVATE(o)  (GTK_FILE_CHOOSER_WIDGET (o)->priv)
32
33 static void gtk_file_chooser_widget_class_init   (GtkFileChooserWidgetClass *class);
34 static void gtk_file_chooser_widget_init         (GtkFileChooserWidget      *chooser_widget);
35 static void gtk_file_chooser_widget_finalize     (GObject                   *object);
36
37 static GObject* gtk_file_chooser_widget_constructor  (GType                  type,
38                                                       guint                  n_construct_properties,
39                                                       GObjectConstructParam *construct_params);
40 static void     gtk_file_chooser_widget_set_property (GObject               *object,
41                                                       guint                  prop_id,
42                                                       const GValue          *value,
43                                                       GParamSpec            *pspec);
44 static void     gtk_file_chooser_widget_get_property (GObject               *object,
45                                                       guint                  prop_id,
46                                                       GValue                *value,
47                                                       GParamSpec            *pspec);
48
49 static GObjectClass *parent_class;
50
51 GType
52 gtk_file_chooser_widget_get_type (void)
53 {
54   static GType file_chooser_widget_type = 0;
55
56   if (!file_chooser_widget_type)
57     {
58       static const GTypeInfo file_chooser_widget_info =
59       {
60         sizeof (GtkFileChooserWidgetClass),
61         NULL,           /* base_init */
62         NULL,           /* base_finalize */
63         (GClassInitFunc) gtk_file_chooser_widget_class_init,
64         NULL,           /* class_finalize */
65         NULL,           /* class_data */
66         sizeof (GtkFileChooserWidget),
67         0,              /* n_preallocs */
68         (GInstanceInitFunc) gtk_file_chooser_widget_init,
69       };
70       
71       static const GInterfaceInfo file_chooser_info =
72       {
73         (GInterfaceInitFunc) _gtk_file_chooser_delegate_iface_init, /* interface_init */
74         NULL,                                                       /* interface_finalize */
75         NULL                                                        /* interface_data */
76       };
77
78       static const GInterfaceInfo file_chooser_embed_info =
79       {
80         (GInterfaceInitFunc) _gtk_file_chooser_embed_delegate_iface_init, /* interface_init */
81         NULL,                                                             /* interface_finalize */
82         NULL                                                              /* interface_data */
83       };
84
85       file_chooser_widget_type = g_type_register_static (GTK_TYPE_VBOX, I_("GtkFileChooserWidget"),
86                                                          &file_chooser_widget_info, 0);
87
88       g_type_add_interface_static (file_chooser_widget_type,
89                                    GTK_TYPE_FILE_CHOOSER,
90                                    &file_chooser_info);
91       g_type_add_interface_static (file_chooser_widget_type,
92                                    GTK_TYPE_FILE_CHOOSER_EMBED,
93                                    &file_chooser_embed_info);
94     }
95
96   return file_chooser_widget_type;
97 }
98
99 static void
100 gtk_file_chooser_widget_class_init (GtkFileChooserWidgetClass *class)
101 {
102   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
103
104   parent_class = g_type_class_peek_parent (class);
105
106   gobject_class->constructor = gtk_file_chooser_widget_constructor;
107   gobject_class->set_property = gtk_file_chooser_widget_set_property;
108   gobject_class->get_property = gtk_file_chooser_widget_get_property;
109   gobject_class->finalize = gtk_file_chooser_widget_finalize;
110
111   _gtk_file_chooser_install_properties (gobject_class);
112
113   g_type_class_add_private (class, sizeof (GtkFileChooserWidgetPrivate));
114 }
115
116 static void
117 gtk_file_chooser_widget_init (GtkFileChooserWidget *chooser_widget)
118 {
119   GtkFileChooserWidgetPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (chooser_widget,
120                                                                    GTK_TYPE_FILE_CHOOSER_WIDGET,
121                                                                    GtkFileChooserWidgetPrivate);
122   chooser_widget->priv = priv;
123 }
124
125 static void
126 gtk_file_chooser_widget_finalize (GObject *object)
127 {
128   GtkFileChooserWidget *chooser = GTK_FILE_CHOOSER_WIDGET (object);
129
130   g_free (chooser->priv->file_system);
131
132   G_OBJECT_CLASS (parent_class)->finalize (object);
133 }
134
135 static GObject*
136 gtk_file_chooser_widget_constructor (GType                  type,
137                                      guint                  n_construct_properties,
138                                      GObjectConstructParam *construct_params)
139 {
140   GtkFileChooserWidgetPrivate *priv;
141   GObject *object;
142   gchar *current_folder;
143   
144   object = parent_class->constructor (type,
145                                       n_construct_properties,
146                                       construct_params);
147   priv = GTK_FILE_CHOOSER_WIDGET_GET_PRIVATE (object);
148
149   gtk_widget_push_composite_child ();
150
151   priv->impl = _gtk_file_chooser_default_new (priv->file_system);
152   
153   gtk_box_pack_start (GTK_BOX (object), priv->impl, TRUE, TRUE, 0);
154   gtk_widget_show (priv->impl);
155
156   current_folder = g_get_current_dir ();
157   gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (priv->impl), current_folder);
158   g_free (current_folder);
159
160   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
161                                   GTK_FILE_CHOOSER (priv->impl));
162
163   _gtk_file_chooser_embed_set_delegate (GTK_FILE_CHOOSER_EMBED (object),
164                                         GTK_FILE_CHOOSER_EMBED (priv->impl));
165   
166   gtk_widget_pop_composite_child ();
167
168   return object;
169 }
170
171 static void
172 gtk_file_chooser_widget_set_property (GObject         *object,
173                                       guint            prop_id,
174                                       const GValue    *value,
175                                       GParamSpec      *pspec)
176 {
177   GtkFileChooserWidgetPrivate *priv = GTK_FILE_CHOOSER_WIDGET_GET_PRIVATE (object);
178
179   switch (prop_id)
180     {
181     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
182       g_free (priv->file_system);
183       priv->file_system = g_value_dup_string (value);
184       break;
185     default:
186       g_object_set_property (G_OBJECT (priv->impl), pspec->name, value);
187       break;
188     }
189 }
190
191 static void
192 gtk_file_chooser_widget_get_property (GObject         *object,
193                                       guint            prop_id,
194                                       GValue          *value,
195                                       GParamSpec      *pspec)
196 {
197   GtkFileChooserWidgetPrivate *priv = GTK_FILE_CHOOSER_WIDGET_GET_PRIVATE (object);
198   
199   g_object_get_property (G_OBJECT (priv->impl), pspec->name, value);
200 }
201
202 /**
203  * gtk_file_chooser_widget_new:
204  * @action: Open or save mode for the widget
205  * 
206  * Creates a new #GtkFileChooserWidget.  This is a file chooser widget that can
207  * be embedded in custom windows, and it is the same widget that is used by
208  * #GtkFileChooserDialog.
209  * 
210  * Return value: a new #GtkFileChooserWidget
211  *
212  * Since: 2.4
213  **/
214 GtkWidget *
215 gtk_file_chooser_widget_new (GtkFileChooserAction action)
216 {
217   return g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
218                        "action", action,
219                        NULL);
220 }
221
222 /**
223  * gtk_file_chooser_widget_new_with_backend:
224  * @action: Open or save mode for the widget
225  * @backend: The name of the specific filesystem backend to use.
226  * 
227  * Creates a new #GtkFileChooserWidget with a specified backend.  This is
228  * especially useful if you use gtk_file_chooser_set_local_only() to allow
229  * non-local files.  This is a file chooser widget that can be embedded in
230  * custom windows and it is the same widget that is used by
231  * #GtkFileChooserDialog.
232  * 
233  * Return value: a new #GtkFileChooserWidget
234  *
235  * Since: 2.4
236  **/
237 GtkWidget *
238 gtk_file_chooser_widget_new_with_backend (GtkFileChooserAction  action,
239                                           const gchar          *backend)
240 {
241   return g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
242                        "action", action,
243                        "file-system-backend", backend,
244                        NULL);
245 }
246
247 #define __GTK_FILE_CHOOSER_WIDGET_C__
248 #include "gtkaliasdef.c"