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