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