]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdialog.c
bgo505857 - GtkFileChooserDialog ensures its default response
[~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 "gtkfilesystem.h"
29 #include "gtktypebuiltins.h"
30 #include "gtkintl.h"
31 #include "gtkalias.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   GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
84                                                                    GTK_TYPE_FILE_CHOOSER_DIALOG,
85                                                                    GtkFileChooserDialogPrivate);
86   GtkDialog *fc_dialog = GTK_DIALOG (dialog);
87
88   dialog->priv = priv;
89   dialog->priv->response_requested = FALSE;
90
91   gtk_dialog_set_has_separator (fc_dialog, FALSE);
92   gtk_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
93   gtk_box_set_spacing (GTK_BOX (fc_dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
94   gtk_container_set_border_width (GTK_CONTAINER (fc_dialog->action_area), 5);
95
96   /* We do a signal connection here rather than overriding the method in
97    * class_init because GtkDialog::response is a RUN_LAST signal.  We want *our*
98    * handler to be run *first*, regardless of whether the user installs response
99    * handlers of his own.
100    */
101   g_signal_connect (dialog, "response",
102                     G_CALLBACK (response_cb), NULL);
103 }
104
105 static void
106 gtk_file_chooser_dialog_finalize (GObject *object)
107 {
108   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
109
110   g_free (dialog->priv->file_system);
111
112   G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);  
113 }
114
115 static gboolean
116 is_stock_accept_response_id (int response_id)
117 {
118   return (response_id == GTK_RESPONSE_ACCEPT
119           || response_id == GTK_RESPONSE_OK
120           || response_id == GTK_RESPONSE_YES
121           || response_id == GTK_RESPONSE_APPLY);
122 }
123
124 /* Callback used when the user activates a file in the file chooser widget */
125 static void
126 file_chooser_widget_file_activated (GtkFileChooser       *chooser,
127                                     GtkFileChooserDialog *dialog)
128 {
129   GList *children, *l;
130
131   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
132     return;
133
134   /* There probably isn't a default widget, so make things easier for the
135    * programmer by looking for a reasonable button on our own.
136    */
137
138   children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
139
140   for (l = children; l; l = l->next)
141     {
142       GtkWidget *widget;
143       int response_id;
144
145       widget = GTK_WIDGET (l->data);
146       response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
147       if (is_stock_accept_response_id (response_id))
148         {
149           gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
150           break;
151         }
152     }
153
154   g_list_free (children);
155 }
156
157 static void
158 clamp_to_screen (GtkWidget *widget,
159                  gint      *width,
160                  gint      *height)
161 {
162   GdkScreen *screen;
163   int monitor_num;
164   GdkRectangle monitor;
165
166   g_return_if_fail (GTK_WIDGET_REALIZED (widget));
167   
168   screen = gtk_widget_get_screen (widget);
169   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
170
171   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
172
173   if (width)
174     *width = MIN (*width, (monitor.width * 3) / 4);
175
176   if (height)
177     *height = MIN (*height, (monitor.height * 3) / 4);
178 }
179
180 static void
181 file_chooser_widget_default_size_changed (GtkWidget            *widget,
182                                           GtkFileChooserDialog *dialog)
183 {
184   GtkFileChooserDialogPrivate *priv;
185   gint width, height;
186   gint default_width, default_height;
187   GtkRequisition req, widget_req;
188   gboolean resizable;
189
190   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
191
192   /* Unset any previously set size */
193   gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
194
195   if (GTK_WIDGET_DRAWABLE (widget))
196     {
197       /* Force a size request of everything before we start.  This will make sure
198        * that widget->requisition is meaningful. */
199       gtk_widget_size_request (GTK_WIDGET (dialog), &req);
200       gtk_widget_size_request (widget, &widget_req);
201
202       width = req.width - widget_req.width;
203       height = req.height - widget_req.height;
204     }
205   else
206     {
207       width = GTK_WIDGET (dialog)->allocation.width - widget->allocation.width;
208       height = GTK_WIDGET (dialog)->allocation.height - widget->allocation.height;
209     }
210
211   resizable = _gtk_file_chooser_embed_get_resizable (GTK_FILE_CHOOSER_EMBED (priv->widget));
212   _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
213                                             &default_width, &default_height);
214
215   /* Ideal target size plus any extra size */
216   width = default_width + width + (2 * GTK_CONTAINER (dialog)->border_width);
217   height = default_height + height + (2 * GTK_CONTAINER (dialog)->border_width);
218
219   if (GTK_WIDGET_REALIZED (dialog))
220     clamp_to_screen (GTK_WIDGET (dialog), &width, &height);
221
222   if (resizable)
223     {
224       gtk_window_set_resizable (GTK_WINDOW (dialog), resizable);
225       gtk_window_resize (GTK_WINDOW (dialog), width, height);
226     }
227   else
228     {
229       gtk_widget_set_size_request (GTK_WIDGET (dialog), width, -1);
230       gtk_window_set_resizable (GTK_WINDOW (dialog), resizable);
231     }
232 }
233
234 static void
235 file_chooser_widget_response_requested (GtkWidget            *widget,
236                                         GtkFileChooserDialog *dialog)
237 {
238   GList *children, *l;
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
244   children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->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 (GTK_DIALOG (dialog), widget);
253       if (is_stock_accept_response_id (response_id))
254         {
255           dialog->priv->response_requested = TRUE;
256           gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
257           break;
258         }
259     }
260
261   g_list_free (children);
262 }
263   
264 static GObject*
265 gtk_file_chooser_dialog_constructor (GType                  type,
266                                      guint                  n_construct_properties,
267                                      GObjectConstructParam *construct_params)
268 {
269   GtkFileChooserDialogPrivate *priv;
270   GObject *object;
271
272   object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
273                                                                                n_construct_properties,
274                                                                                construct_params);
275   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
276
277   gtk_widget_push_composite_child ();
278
279   if (priv->file_system)
280     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
281                                  "file-system-backend", priv->file_system,
282                                  NULL);
283   else
284     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
285
286   g_signal_connect (priv->widget, "file-activated",
287                     G_CALLBACK (file_chooser_widget_file_activated), object);
288   g_signal_connect (priv->widget, "default-size-changed",
289                     G_CALLBACK (file_chooser_widget_default_size_changed), object);
290   g_signal_connect (priv->widget, "response-requested",
291                     G_CALLBACK (file_chooser_widget_response_requested), object);
292
293   gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
294   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
295
296   gtk_widget_show (priv->widget);
297
298   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
299                                   GTK_FILE_CHOOSER (priv->widget));
300
301   gtk_widget_pop_composite_child ();
302
303   return object;
304 }
305
306 static void
307 gtk_file_chooser_dialog_set_property (GObject         *object,
308                                       guint            prop_id,
309                                       const GValue    *value,
310                                       GParamSpec      *pspec)
311
312 {
313   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
314
315   switch (prop_id)
316     {
317     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
318       g_free (priv->file_system);
319       priv->file_system = g_value_dup_string (value);
320       break;
321     default:
322       g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
323       break;
324     }
325 }
326
327 static void
328 gtk_file_chooser_dialog_get_property (GObject         *object,
329                                       guint            prop_id,
330                                       GValue          *value,
331                                       GParamSpec      *pspec)
332 {
333   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
334
335   g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
336 }
337
338 #if 0
339 static void
340 set_default_size (GtkFileChooserDialog *dialog)
341 {
342   GtkWidget *widget;
343   GtkWindow *window;
344   int default_width, default_height;
345   int width, height;
346   int font_size;
347   GdkScreen *screen;
348   int monitor_num;
349   GtkRequisition req;
350   GdkRectangle monitor;
351
352   widget = GTK_WIDGET (dialog);
353   window = GTK_WINDOW (dialog);
354
355   /* Size based on characters */
356
357   font_size = pango_font_description_get_size (widget->style->font_desc);
358   font_size = PANGO_PIXELS (font_size);
359
360   width = font_size * NUM_CHARS;
361   height = font_size * NUM_LINES;
362
363   /* Use at least the requisition size... */
364
365   gtk_widget_size_request (widget, &req);
366   width = MAX (width, req.width);
367   height = MAX (height, req.height);
368
369   /* ... but no larger than the monitor */
370
371   screen = gtk_widget_get_screen (widget);
372   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
373
374   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
375
376   width = MIN (width, monitor.width * 3 / 4);
377   height = MIN (height, monitor.height * 3 / 4);
378
379   /* Set size */
380
381   gtk_window_get_default_size (window, &default_width, &default_height);
382
383   gtk_window_set_default_size (window,
384                                (default_width == -1) ? width : default_width,
385                                (default_height == -1) ? height : default_height);
386 }
387 #endif
388
389 static void
390 foreach_ensure_default_response_cb (GtkWidget *widget,
391                                     gpointer   data)
392 {
393   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
394   int response_id;
395
396   response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
397   if (is_stock_accept_response_id (response_id))
398     gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
399 }
400
401 static void
402 ensure_default_response (GtkFileChooserDialog *dialog)
403 {
404   gtk_container_foreach (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area),
405                          foreach_ensure_default_response_cb,
406                          dialog);
407 }
408
409 /* GtkWidget::map handler */
410 static void
411 gtk_file_chooser_dialog_map (GtkWidget *widget)
412 {
413   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
414   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
415
416   ensure_default_response (dialog);
417
418   if (!GTK_WIDGET_MAPPED (priv->widget))
419     gtk_widget_map (priv->widget);
420
421   file_chooser_widget_default_size_changed (priv->widget, dialog);
422   _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
423
424   GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
425 }
426
427 /* GtkWidget::unmap handler */
428 static void
429 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
430 {
431   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
432   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
433
434   GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
435
436   /* See bug #145470.  We unmap the GtkFileChooserWidget so that if the dialog
437    * is remapped, the widget will be remapped as well.  Implementations should
438    * refresh their contents when this happens, as some applications keep a
439    * single file chooser alive and map/unmap it as needed, rather than creating
440    * a new file chooser every time they need one.
441    */
442   gtk_widget_unmap (priv->widget);
443 }
444
445 /* GtkDialog::response handler */
446 static void
447 response_cb (GtkDialog *dialog,
448              gint       response_id)
449 {
450   GtkFileChooserDialogPrivate *priv;
451
452   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
453
454   /* Act only on response IDs we recognize */
455   if (is_stock_accept_response_id (response_id)
456       && !priv->response_requested
457       && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
458     {
459       g_signal_stop_emission_by_name (dialog, "response");
460     }
461
462   priv->response_requested = FALSE;
463 }
464
465 static GtkWidget *
466 gtk_file_chooser_dialog_new_valist (const gchar          *title,
467                                     GtkWindow            *parent,
468                                     GtkFileChooserAction  action,
469                                     const gchar          *backend,
470                                     const gchar          *first_button_text,
471                                     va_list               varargs)
472 {
473   GtkWidget *result;
474   const char *button_text = first_button_text;
475   gint response_id;
476
477   result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
478                          "title", title,
479                          "action", action,
480                          "file-system-backend", backend,
481                          NULL);
482
483   if (parent)
484     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
485
486   while (button_text)
487     {
488       response_id = va_arg (varargs, gint);
489       gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
490       button_text = va_arg (varargs, const gchar *);
491     }
492
493   return result;
494 }
495
496 /**
497  * gtk_file_chooser_dialog_new:
498  * @title: Title of the dialog, or %NULL
499  * @parent: Transient parent of the dialog, or %NULL
500  * @action: Open or save mode for the dialog
501  * @first_button_text: stock ID or text to go in the first button, or %NULL
502  * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
503  *
504  * Creates a new #GtkFileChooserDialog.  This function is analogous to
505  * gtk_dialog_new_with_buttons().
506  *
507  * Return value: a new #GtkFileChooserDialog
508  *
509  * Since: 2.4
510  **/
511 GtkWidget *
512 gtk_file_chooser_dialog_new (const gchar         *title,
513                              GtkWindow           *parent,
514                              GtkFileChooserAction action,
515                              const gchar         *first_button_text,
516                              ...)
517 {
518   GtkWidget *result;
519   va_list varargs;
520   
521   va_start (varargs, first_button_text);
522   result = gtk_file_chooser_dialog_new_valist (title, parent, action,
523                                                NULL, first_button_text,
524                                                varargs);
525   va_end (varargs);
526
527   return result;
528 }
529
530 /**
531  * gtk_file_chooser_dialog_new_with_backend:
532  * @title: Title of the dialog, or %NULL
533  * @parent: Transient parent of the dialog, or %NULL
534  * @action: Open or save mode for the dialog
535  * @backend: The name of the specific filesystem backend to use.
536  * @first_button_text: stock ID or text to go in the first button, or %NULL
537  * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
538  *
539  * Creates a new #GtkFileChooserDialog with a specified backend. This is
540  * especially useful if you use gtk_file_chooser_set_local_only() to allow
541  * non-local files and you use a more expressive vfs, such as gnome-vfs,
542  * to load files.
543  *
544  * Return value: a new #GtkFileChooserDialog
545  *
546  * Since: 2.4
547  **/
548 GtkWidget *
549 gtk_file_chooser_dialog_new_with_backend (const gchar          *title,
550                                           GtkWindow            *parent,
551                                           GtkFileChooserAction  action,
552                                           const gchar          *backend,
553                                           const gchar          *first_button_text,
554                                           ...)
555 {
556   GtkWidget *result;
557   va_list varargs;
558   
559   va_start (varargs, first_button_text);
560   result = gtk_file_chooser_dialog_new_valist (title, parent, action,
561                                                backend, first_button_text,
562                                                varargs);
563   va_end (varargs);
564
565   return result;
566 }
567
568 #define __GTK_FILE_CHOOSER_DIALOG_C__
569 #include "gtkaliasdef.c"