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