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