]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdialog.c
c180a05c2a6d88864094fd15b6d5856afa946b8f
[~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
189   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
190
191   /* Unset any previously set size */
192   gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
193
194   if (GTK_WIDGET_DRAWABLE (widget))
195     {
196       /* Force a size request of everything before we start.  This will make sure
197        * that widget->requisition is meaningful. */
198       gtk_widget_size_request (GTK_WIDGET (dialog), &req);
199       gtk_widget_size_request (widget, &widget_req);
200
201       width = req.width - widget_req.width;
202       height = req.height - widget_req.height;
203     }
204   else
205     {
206       width = GTK_WIDGET (dialog)->allocation.width - widget->allocation.width;
207       height = GTK_WIDGET (dialog)->allocation.height - widget->allocation.height;
208     }
209
210   _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
211                                             &default_width, &default_height);
212
213   /* Ideal target size plus any extra size */
214   width = default_width + width + (2 * GTK_CONTAINER (dialog)->border_width);
215   height = default_height + height + (2 * GTK_CONTAINER (dialog)->border_width);
216
217   if (GTK_WIDGET_REALIZED (dialog))
218     clamp_to_screen (GTK_WIDGET (dialog), &width, &height);
219
220   gtk_window_resize (GTK_WINDOW (dialog), width, height);
221 }
222
223 static void
224 file_chooser_widget_response_requested (GtkWidget            *widget,
225                                         GtkFileChooserDialog *dialog)
226 {
227   GList *children, *l;
228
229   /* There probably isn't a default widget, so make things easier for the
230    * programmer by looking for a reasonable button on our own.
231    */
232
233   children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
234
235   for (l = children; l; l = l->next)
236     {
237       GtkWidget *widget;
238       int response_id;
239
240       widget = GTK_WIDGET (l->data);
241       response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
242       if (is_stock_accept_response_id (response_id))
243         {
244           dialog->priv->response_requested = TRUE;
245           gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
246           break;
247         }
248     }
249
250   g_list_free (children);
251 }
252   
253 static GObject*
254 gtk_file_chooser_dialog_constructor (GType                  type,
255                                      guint                  n_construct_properties,
256                                      GObjectConstructParam *construct_params)
257 {
258   GtkFileChooserDialogPrivate *priv;
259   GObject *object;
260
261   object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
262                                                                                n_construct_properties,
263                                                                                construct_params);
264   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
265
266   gtk_widget_push_composite_child ();
267
268   if (priv->file_system)
269     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
270                                  "file-system-backend", priv->file_system,
271                                  NULL);
272   else
273     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
274
275   g_signal_connect (priv->widget, "file-activated",
276                     G_CALLBACK (file_chooser_widget_file_activated), object);
277   g_signal_connect (priv->widget, "default-size-changed",
278                     G_CALLBACK (file_chooser_widget_default_size_changed), object);
279   g_signal_connect (priv->widget, "response-requested",
280                     G_CALLBACK (file_chooser_widget_response_requested), object);
281
282   gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
283   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
284
285   gtk_widget_show (priv->widget);
286
287   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
288                                   GTK_FILE_CHOOSER (priv->widget));
289
290   gtk_widget_pop_composite_child ();
291
292   return object;
293 }
294
295 static void
296 gtk_file_chooser_dialog_set_property (GObject         *object,
297                                       guint            prop_id,
298                                       const GValue    *value,
299                                       GParamSpec      *pspec)
300
301 {
302   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
303
304   switch (prop_id)
305     {
306     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
307       g_free (priv->file_system);
308       priv->file_system = g_value_dup_string (value);
309       break;
310     default:
311       g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
312       break;
313     }
314 }
315
316 static void
317 gtk_file_chooser_dialog_get_property (GObject         *object,
318                                       guint            prop_id,
319                                       GValue          *value,
320                                       GParamSpec      *pspec)
321 {
322   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
323
324   g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
325 }
326
327 #if 0
328 static void
329 set_default_size (GtkFileChooserDialog *dialog)
330 {
331   GtkWidget *widget;
332   GtkWindow *window;
333   int default_width, default_height;
334   int width, height;
335   int font_size;
336   GdkScreen *screen;
337   int monitor_num;
338   GtkRequisition req;
339   GdkRectangle monitor;
340
341   widget = GTK_WIDGET (dialog);
342   window = GTK_WINDOW (dialog);
343
344   /* Size based on characters */
345
346   font_size = pango_font_description_get_size (widget->style->font_desc);
347   font_size = PANGO_PIXELS (font_size);
348
349   width = font_size * NUM_CHARS;
350   height = font_size * NUM_LINES;
351
352   /* Use at least the requisition size... */
353
354   gtk_widget_size_request (widget, &req);
355   width = MAX (width, req.width);
356   height = MAX (height, req.height);
357
358   /* ... but no larger than the monitor */
359
360   screen = gtk_widget_get_screen (widget);
361   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
362
363   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
364
365   width = MIN (width, monitor.width * 3 / 4);
366   height = MIN (height, monitor.height * 3 / 4);
367
368   /* Set size */
369
370   gtk_window_get_default_size (window, &default_width, &default_height);
371
372   gtk_window_set_default_size (window,
373                                (default_width == -1) ? width : default_width,
374                                (default_height == -1) ? height : default_height);
375 }
376 #endif
377
378 static void
379 foreach_ensure_default_response_cb (GtkWidget *widget,
380                                     gpointer   data)
381 {
382   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
383   int response_id;
384
385   response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
386   if (is_stock_accept_response_id (response_id))
387     gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
388 }
389
390 static void
391 ensure_default_response (GtkFileChooserDialog *dialog)
392 {
393   gtk_container_foreach (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area),
394                          foreach_ensure_default_response_cb,
395                          dialog);
396 }
397
398 /* GtkWidget::map handler */
399 static void
400 gtk_file_chooser_dialog_map (GtkWidget *widget)
401 {
402   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
403   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
404
405   ensure_default_response (dialog);
406
407   if (!GTK_WIDGET_MAPPED (priv->widget))
408     gtk_widget_map (priv->widget);
409
410   file_chooser_widget_default_size_changed (priv->widget, dialog);
411   _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
412
413   GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
414 }
415
416 /* GtkWidget::unmap handler */
417 static void
418 gtk_file_chooser_dialog_unmap (GtkWidget *widget)
419 {
420   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
421   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
422
423   GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->unmap (widget);
424
425   /* See bug #145470.  We unmap the GtkFileChooserWidget so that if the dialog
426    * is remapped, the widget will be remapped as well.  Implementations should
427    * refresh their contents when this happens, as some applications keep a
428    * single file chooser alive and map/unmap it as needed, rather than creating
429    * a new file chooser every time they need one.
430    */
431   gtk_widget_unmap (priv->widget);
432 }
433
434 /* GtkDialog::response handler */
435 static void
436 response_cb (GtkDialog *dialog,
437              gint       response_id)
438 {
439   GtkFileChooserDialogPrivate *priv;
440
441   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
442
443   /* Act only on response IDs we recognize */
444   if (is_stock_accept_response_id (response_id)
445       && !priv->response_requested
446       && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
447     {
448       g_signal_stop_emission_by_name (dialog, "response");
449     }
450
451   priv->response_requested = FALSE;
452 }
453
454 static GtkWidget *
455 gtk_file_chooser_dialog_new_valist (const gchar          *title,
456                                     GtkWindow            *parent,
457                                     GtkFileChooserAction  action,
458                                     const gchar          *backend,
459                                     const gchar          *first_button_text,
460                                     va_list               varargs)
461 {
462   GtkWidget *result;
463   const char *button_text = first_button_text;
464   gint response_id;
465
466   result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
467                          "title", title,
468                          "action", action,
469                          "file-system-backend", backend,
470                          NULL);
471
472   if (parent)
473     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
474
475   while (button_text)
476     {
477       response_id = va_arg (varargs, gint);
478       gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
479       button_text = va_arg (varargs, const gchar *);
480     }
481
482   return result;
483 }
484
485 /**
486  * gtk_file_chooser_dialog_new:
487  * @title: Title of the dialog, or %NULL
488  * @parent: Transient parent of the dialog, or %NULL
489  * @action: Open or save mode for the dialog
490  * @first_button_text: stock ID or text to go in the first button, or %NULL
491  * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
492  *
493  * Creates a new #GtkFileChooserDialog.  This function is analogous to
494  * gtk_dialog_new_with_buttons().
495  *
496  * Return value: a new #GtkFileChooserDialog
497  *
498  * Since: 2.4
499  **/
500 GtkWidget *
501 gtk_file_chooser_dialog_new (const gchar         *title,
502                              GtkWindow           *parent,
503                              GtkFileChooserAction action,
504                              const gchar         *first_button_text,
505                              ...)
506 {
507   GtkWidget *result;
508   va_list varargs;
509   
510   va_start (varargs, first_button_text);
511   result = gtk_file_chooser_dialog_new_valist (title, parent, action,
512                                                NULL, first_button_text,
513                                                varargs);
514   va_end (varargs);
515
516   return result;
517 }
518
519 /**
520  * gtk_file_chooser_dialog_new_with_backend:
521  * @title: Title of the dialog, or %NULL
522  * @parent: Transient parent of the dialog, or %NULL
523  * @action: Open or save mode for the dialog
524  * @backend: The name of the specific filesystem backend to use.
525  * @first_button_text: stock ID or text to go in the first button, or %NULL
526  * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
527  *
528  * Creates a new #GtkFileChooserDialog with a specified backend. This is
529  * especially useful if you use gtk_file_chooser_set_local_only() to allow
530  * non-local files and you use a more expressive vfs, such as gnome-vfs,
531  * to load files.
532  *
533  * Return value: a new #GtkFileChooserDialog
534  *
535  * Since: 2.4
536  **/
537 GtkWidget *
538 gtk_file_chooser_dialog_new_with_backend (const gchar          *title,
539                                           GtkWindow            *parent,
540                                           GtkFileChooserAction  action,
541                                           const gchar          *backend,
542                                           const gchar          *first_button_text,
543                                           ...)
544 {
545   GtkWidget *result;
546   va_list varargs;
547   
548   va_start (varargs, first_button_text);
549   result = gtk_file_chooser_dialog_new_valist (title, parent, action,
550                                                backend, first_button_text,
551                                                varargs);
552   va_end (varargs);
553
554   return result;
555 }
556
557 #define __GTK_FILE_CHOOSER_DIALOG_C__
558 #include "gtkaliasdef.c"