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