]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdialog.c
dd8a9a8aaa41e92313cb9ee0d178568bcf4384fe
[~andy/gtk] / gtk / gtkfilechooserdialog.c
1 /* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
2 /* GTK - The GIMP Toolkit
3  * gtkfilechooserdialog.c: File selector dialog
4  * Copyright (C) 2003, Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include "gtkfilechooserdialog.h"
25
26 #include "gtkfilechooserprivate.h"
27 #include "gtkfilechooserwidget.h"
28 #include "gtkfilechooserutils.h"
29 #include "gtkfilechooserembed.h"
30 #include "gtkfilesystem.h"
31 #include "gtksizerequest.h"
32 #include "gtktypebuiltins.h"
33 #include "gtkintl.h"
34
35 #include <stdarg.h>
36
37 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o)  (GTK_FILE_CHOOSER_DIALOG (o)->priv)
38
39 static void gtk_file_chooser_dialog_finalize   (GObject                   *object);
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 static void     gtk_file_chooser_dialog_map          (GtkWidget             *widget);
54 static void     gtk_file_chooser_dialog_unmap        (GtkWidget             *widget);
55
56 static void response_cb (GtkDialog *dialog,
57                          gint       response_id);
58
59 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDialog, gtk_file_chooser_dialog, GTK_TYPE_DIALOG,
60                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
61                                                 _gtk_file_chooser_delegate_iface_init))
62
63 static void
64 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
65 {
66   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
67   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
68
69   gobject_class->constructor = gtk_file_chooser_dialog_constructor;
70   gobject_class->set_property = gtk_file_chooser_dialog_set_property;
71   gobject_class->get_property = gtk_file_chooser_dialog_get_property;
72   gobject_class->finalize = gtk_file_chooser_dialog_finalize;
73
74   widget_class->map       = gtk_file_chooser_dialog_map;
75   widget_class->unmap     = gtk_file_chooser_dialog_unmap;
76
77   _gtk_file_chooser_install_properties (gobject_class);
78
79   g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
80 }
81
82 static void
83 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
84 {
85   GtkWidget *action_area, *content_area;
86   GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
87                                                                    GTK_TYPE_FILE_CHOOSER_DIALOG,
88                                                                    GtkFileChooserDialogPrivate);
89   GtkDialog *fc_dialog = GTK_DIALOG (dialog);
90
91   dialog->priv = priv;
92   dialog->priv->response_requested = FALSE;
93
94   content_area = gtk_dialog_get_content_area (fc_dialog);
95   action_area = gtk_dialog_get_action_area (fc_dialog);
96
97   gtk_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
98   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
99   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
100
101   /* We do a signal connection here rather than overriding the method in
102    * class_init because GtkDialog::response is a RUN_LAST signal.  We want *our*
103    * handler to be run *first*, regardless of whether the user installs response
104    * handlers of his own.
105    */
106   g_signal_connect (dialog, "response",
107                     G_CALLBACK (response_cb), NULL);
108 }
109
110 static void
111 gtk_file_chooser_dialog_finalize (GObject *object)
112 {
113   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
114
115   g_free (dialog->priv->file_system);
116
117   G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);  
118 }
119
120 static gboolean
121 is_stock_accept_response_id (int response_id)
122 {
123   return (response_id == GTK_RESPONSE_ACCEPT
124           || response_id == GTK_RESPONSE_OK
125           || response_id == GTK_RESPONSE_YES
126           || response_id == GTK_RESPONSE_APPLY);
127 }
128
129 /* Callback used when the user activates a file in the file chooser widget */
130 static void
131 file_chooser_widget_file_activated (GtkFileChooser       *chooser,
132                                     GtkFileChooserDialog *dialog)
133 {
134   GtkDialog *fc_dialog = GTK_DIALOG (dialog);
135   GtkWidget *action_area;
136   GList *children, *l;
137
138   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
139     return;
140
141   /* There probably isn't a default widget, so make things easier for the
142    * programmer by looking for a reasonable button on our own.
143    */
144   action_area = gtk_dialog_get_action_area (fc_dialog);
145   children = gtk_container_get_children (GTK_CONTAINER (action_area));
146
147   for (l = children; l; l = l->next)
148     {
149       GtkWidget *widget;
150       int response_id;
151
152       widget = GTK_WIDGET (l->data);
153       response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
154       if (gtk_widget_is_sensitive (widget) &&
155           is_stock_accept_response_id (response_id))
156         {
157           gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
158           break;
159         }
160     }
161
162   g_list_free (children);
163 }
164
165 #if 0
166 /* FIXME: to see why this function is ifdef-ed out, see the comment below in
167  * file_chooser_widget_default_size_changed().
168  */
169 static void
170 load_position (int *out_xpos, int *out_ypos)
171 {
172   GtkFileChooserSettings *settings;
173   int x, y, width, height;
174
175   settings = _gtk_file_chooser_settings_new ();
176   _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
177   g_object_unref (settings);
178
179   *out_xpos = x;
180   *out_ypos = y;
181 }
182 #endif
183
184 static void
185 file_chooser_widget_default_size_changed (GtkWidget            *widget,
186                                           GtkFileChooserDialog *dialog)
187 {
188   GtkFileChooserDialogPrivate *priv;
189   gint default_width, default_height;
190   GtkRequisition req, widget_req;
191
192   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
193
194   /* Unset any previously set size */
195   gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
196
197   if (gtk_widget_is_drawable (widget))
198     {
199       /* Force a size request of everything before we start.  This will make sure
200        * that widget->requisition is meaningful. */
201       gtk_widget_get_preferred_size (GTK_WIDGET (dialog),
202                                      &req, NULL);
203       gtk_widget_get_preferred_size (widget,
204                                      &widget_req, NULL);
205     }
206
207   _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
208                                             &default_width, &default_height);
209
210   gtk_window_resize (GTK_WINDOW (dialog), default_width, default_height);
211
212   if (!gtk_widget_get_mapped (GTK_WIDGET (dialog)))
213     {
214 #if 0
215       /* FIXME: the code to restore the position does not work yet.  It is not
216        * clear whether it is actually desirable --- if enabled, applications
217        * would not be able to say "center the file chooser on top of my toplevel
218        * window".  So, we don't use this code at all.
219        */
220       load_position (&xpos, &ypos);
221       if (xpos >= 0 && ypos >= 0)
222         {
223           gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE);
224           gtk_window_move (GTK_WINDOW (dialog), xpos, ypos);
225         }
226 #endif
227     }
228 }
229
230 static void
231 file_chooser_widget_response_requested (GtkWidget            *widget,
232                                         GtkFileChooserDialog *dialog)
233 {
234   GtkDialog *fc_dialog = GTK_DIALOG (dialog);
235   GtkWidget *action_area;
236   GList *children, *l;
237
238   dialog->priv->response_requested = TRUE;
239
240   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
241     return;
242
243   /* There probably isn't a default widget, so make things easier for the
244    * programmer by looking for a reasonable button on our own.
245    */
246   action_area = gtk_dialog_get_action_area (fc_dialog);
247   children = gtk_container_get_children (GTK_CONTAINER (action_area));
248
249   for (l = children; l; l = l->next)
250     {
251       GtkWidget *widget;
252       int response_id;
253
254       widget = GTK_WIDGET (l->data);
255       response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
256       if (gtk_widget_is_sensitive (widget) &&
257           is_stock_accept_response_id (response_id))
258         {
259           gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
260           break;
261         }
262     }
263
264   if (l == NULL)
265     dialog->priv->response_requested = FALSE;
266
267   g_list_free (children);
268 }
269   
270 static GObject*
271 gtk_file_chooser_dialog_constructor (GType                  type,
272                                      guint                  n_construct_properties,
273                                      GObjectConstructParam *construct_params)
274 {
275   GtkFileChooserDialogPrivate *priv;
276   GtkWidget *content_area;
277   GObject *object;
278
279   object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
280                                                                                n_construct_properties,
281                                                                                construct_params);
282   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
283
284   gtk_widget_push_composite_child ();
285
286   if (priv->file_system)
287     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
288                                  NULL);
289   else
290     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
291
292   g_signal_connect (priv->widget, "file-activated",
293                     G_CALLBACK (file_chooser_widget_file_activated), object);
294   g_signal_connect (priv->widget, "default-size-changed",
295                     G_CALLBACK (file_chooser_widget_default_size_changed), object);
296   g_signal_connect (priv->widget, "response-requested",
297                     G_CALLBACK (file_chooser_widget_response_requested), object);
298
299   content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
300
301   gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
302   gtk_box_pack_start (GTK_BOX (content_area), priv->widget, TRUE, TRUE, 0);
303
304   gtk_widget_show (priv->widget);
305
306   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
307                                   GTK_FILE_CHOOSER (priv->widget));
308
309   gtk_widget_pop_composite_child ();
310
311   return object;
312 }
313
314 static void
315 gtk_file_chooser_dialog_set_property (GObject         *object,
316                                       guint            prop_id,
317                                       const GValue    *value,
318                                       GParamSpec      *pspec)
319
320 {
321   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
322
323   switch (prop_id)
324     {
325     default:
326       g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
327       break;
328     }
329 }
330
331 static void
332 gtk_file_chooser_dialog_get_property (GObject         *object,
333                                       guint            prop_id,
334                                       GValue          *value,
335                                       GParamSpec      *pspec)
336 {
337   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
338
339   g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
340 }
341
342 static void
343 foreach_ensure_default_response_cb (GtkWidget *widget,
344                                     gpointer   data)
345 {
346   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
347   int response_id;
348
349   response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
350   if (is_stock_accept_response_id (response_id))
351     gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
352 }
353
354 static void
355 ensure_default_response (GtkFileChooserDialog *dialog)
356 {
357   GtkWidget *action_area;
358
359   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
360   gtk_container_foreach (GTK_CONTAINER (action_area),
361                          foreach_ensure_default_response_cb,
362                          dialog);
363 }
364
365 /* GtkWidget::map handler */
366 static void
367 gtk_file_chooser_dialog_map (GtkWidget *widget)
368 {
369   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
370   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
371
372   ensure_default_response (dialog);
373
374   if (!gtk_widget_get_mapped (priv->widget))
375     gtk_widget_map (priv->widget);
376
377   _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
378
379   GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
380 }
381
382 /* GtkWidget::unmap handler */
383 static void
384 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
385 {
386   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
387   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
388
389   GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
390
391   /* See bug #145470.  We unmap the GtkFileChooserWidget so that if the dialog
392    * is remapped, the widget will be remapped as well.  Implementations should
393    * refresh their contents when this happens, as some applications keep a
394    * single file chooser alive and map/unmap it as needed, rather than creating
395    * a new file chooser every time they need one.
396    */
397   gtk_widget_unmap (priv->widget);
398 }
399
400 /* GtkDialog::response handler */
401 static void
402 response_cb (GtkDialog *dialog,
403              gint       response_id)
404 {
405   GtkFileChooserDialogPrivate *priv;
406
407   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
408
409   /* Act only on response IDs we recognize */
410   if (is_stock_accept_response_id (response_id)
411       && !priv->response_requested
412       && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
413     {
414       g_signal_stop_emission_by_name (dialog, "response");
415     }
416
417   priv->response_requested = FALSE;
418 }
419
420 static GtkWidget *
421 gtk_file_chooser_dialog_new_valist (const gchar          *title,
422                                     GtkWindow            *parent,
423                                     GtkFileChooserAction  action,
424                                     const gchar          *first_button_text,
425                                     va_list               varargs)
426 {
427   GtkWidget *result;
428   const char *button_text = first_button_text;
429   gint response_id;
430
431   result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
432                          "title", title,
433                          "action", action,
434                          NULL);
435
436   if (parent)
437     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
438
439   while (button_text)
440     {
441       response_id = va_arg (varargs, gint);
442       gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
443       button_text = va_arg (varargs, const gchar *);
444     }
445
446   return result;
447 }
448
449 /**
450  * gtk_file_chooser_dialog_new:
451  * @title: (allow-none): Title of the dialog, or %NULL
452  * @parent: (allow-none): Transient parent of the dialog, or %NULL
453  * @action: Open or save mode for the dialog
454  * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
455  * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
456  *
457  * Creates a new #GtkFileChooserDialog.  This function is analogous to
458  * gtk_dialog_new_with_buttons().
459  *
460  * Return value: a new #GtkFileChooserDialog
461  *
462  * Since: 2.4
463  **/
464 GtkWidget *
465 gtk_file_chooser_dialog_new (const gchar         *title,
466                              GtkWindow           *parent,
467                              GtkFileChooserAction action,
468                              const gchar         *first_button_text,
469                              ...)
470 {
471   GtkWidget *result;
472   va_list varargs;
473   
474   va_start (varargs, first_button_text);
475   result = gtk_file_chooser_dialog_new_valist (title, parent, action,
476                                                first_button_text,
477                                                varargs);
478   va_end (varargs);
479
480   return result;
481 }