]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdialog.c
Add gtk_file_chooser_set_current_name() to set the current entry contents.
[~andy/gtk] / gtk / gtkfilechooserdialog.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilechooserdialog.c: File selector dialog
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 "gtkfilechooserdialog.h"
22 #include "gtkfilechooserwidget.h"
23 #include "gtkfilechooserenums.h"
24 #include "gtkfilechooserutils.h"
25 #include "gtkfilesystem.h"
26
27 #include <stdarg.h>
28
29 struct _GtkFileChooserDialogPrivate
30 {
31   GtkWidget *widget;
32   
33   GtkFileSystem *file_system;
34 };
35
36 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o)  (GTK_FILE_CHOOSER_DIALOG (o)->priv)
37
38 static void gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class);
39 static void gtk_file_chooser_dialog_init       (GtkFileChooserDialog      *dialog);
40
41 static GObject* gtk_file_chooser_dialog_constructor  (GType                  type,
42                                                       guint                  n_construct_properties,
43                                                       GObjectConstructParam *construct_params);
44 static void     gtk_file_chooser_dialog_set_property (GObject               *object,
45                                                       guint                  prop_id,
46                                                       const GValue          *value,
47                                                       GParamSpec            *pspec);
48 static void     gtk_file_chooser_dialog_get_property (GObject               *object,
49                                                       guint                  prop_id,
50                                                       GValue                *value,
51                                                       GParamSpec            *pspec);
52
53 GObjectClass *parent_class;
54
55 GType
56 gtk_file_chooser_dialog_get_type (void)
57 {
58   static GType file_chooser_dialog_type = 0;
59
60   if (!file_chooser_dialog_type)
61     {
62       static const GTypeInfo file_chooser_dialog_info =
63       {
64         sizeof (GtkFileChooserDialogClass),
65         NULL,           /* base_init */
66         NULL,           /* base_finalize */
67         (GClassInitFunc) gtk_file_chooser_dialog_class_init,
68         NULL,           /* class_finalize */
69         NULL,           /* class_data */
70         sizeof (GtkFileChooserDialog),
71         0,              /* n_preallocs */
72         (GInstanceInitFunc) gtk_file_chooser_dialog_init,
73       };
74       
75       static const GInterfaceInfo file_chooser_info =
76       {
77         (GInterfaceInitFunc) _gtk_file_chooser_delegate_iface_init, /* interface_init */
78         NULL,                                                       /* interface_finalize */
79         NULL                                                        /* interface_data */
80       };
81
82       file_chooser_dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "GtkFileChooserDialog",
83                                                          &file_chooser_dialog_info, 0);
84       g_type_add_interface_static (file_chooser_dialog_type,
85                                    GTK_TYPE_FILE_CHOOSER,
86                                    &file_chooser_info);
87     }
88
89   return file_chooser_dialog_type;
90 }
91
92 static void
93 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
94 {
95   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
96
97   parent_class = g_type_class_peek_parent (class);
98
99   gobject_class->constructor = gtk_file_chooser_dialog_constructor;
100   gobject_class->set_property = gtk_file_chooser_dialog_set_property;
101   gobject_class->get_property = gtk_file_chooser_dialog_get_property;
102
103   _gtk_file_chooser_install_properties (gobject_class);
104
105   g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
106 }
107
108 static void
109 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
110 {
111   GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
112                                                                    GTK_TYPE_FILE_CHOOSER_DIALOG,
113                                                                    GtkFileChooserDialogPrivate);
114   dialog->priv = priv;
115 }
116
117 static GObject*
118 gtk_file_chooser_dialog_constructor (GType                  type,
119                                      guint                  n_construct_properties,
120                                      GObjectConstructParam *construct_params)
121 {
122   GtkFileChooserDialogPrivate *priv;
123   GObject *object;
124                                                                   
125   object = parent_class->constructor (type,
126                                       n_construct_properties,
127                                       construct_params);
128   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
129
130   gtk_widget_push_composite_child ();
131
132   if (priv->file_system)
133     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
134                                  "file-system", priv->file_system,
135                                  NULL);
136   else
137     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
138   
139   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
140   gtk_widget_show (priv->widget);
141
142   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
143                                   GTK_FILE_CHOOSER (priv->widget));
144
145   gtk_widget_pop_composite_child ();
146
147   return object;
148 }
149
150 static void
151 gtk_file_chooser_dialog_set_property (GObject         *object,
152                                       guint            prop_id,
153                                       const GValue    *value,
154                                       GParamSpec      *pspec)
155      
156 {
157   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
158
159   switch (prop_id)
160     {
161     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM:
162       {
163         GtkFileSystem *file_system = g_value_get_object (value);
164         if (priv->file_system != file_system)
165           {
166             if (priv->file_system)
167               g_object_unref (priv->file_system);
168             priv->file_system = file_system;
169             if (priv->file_system)
170               g_object_ref (priv->file_system);
171           }
172       }
173       break;
174     default:
175       g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
176       break;
177     }
178 }
179
180 static void
181 gtk_file_chooser_dialog_get_property (GObject         *object,
182                                       guint            prop_id,
183                                       GValue          *value,
184                                       GParamSpec      *pspec)
185 {
186   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
187   
188   g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
189 }
190
191 GtkWidget *
192 gtk_file_chooser_dialog_new (const gchar         *title,
193                              GtkWindow           *parent,
194                              GtkFileChooserAction action,
195                              const gchar         *first_button_text,
196                              ...)
197 {
198   GtkWidget *result;
199   va_list varargs;
200   const char *button_text = first_button_text;
201   gint response_id;
202   
203   result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
204                          "title", title,
205                          "action", action,
206                          NULL);
207
208   if (parent)
209     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
210
211   va_start (varargs, first_button_text);
212
213   while (button_text)
214     {
215       response_id = va_arg (varargs, gint);
216       gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
217       button_text = va_arg (varargs, const gchar *);
218     }
219
220   va_end (varargs);
221
222   return result;
223 }