]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdialog.c
Add a checkbox to queue an merge/unmerge. Patch from Matthias Clasen.
[~andy/gtk] / gtk / gtkfilechooserdialog.c
1 /* GTK - The GIMP Toolkit
2  * gtkfilechooserdialog.c: File selector dialog
3  * Copyright (C) 2003, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22 #include "gtkfilechooserdialog.h"
23 #include "gtkfilechooserwidget.h"
24 #include "gtkfilechooserutils.h"
25 #include "gtkfilechooserembed.h"
26 #include "gtkfilesystem.h"
27 #include "gtktypebuiltins.h"
28
29 #include <stdarg.h>
30
31 struct _GtkFileChooserDialogPrivate
32 {
33   GtkWidget *widget;
34   
35   char *file_system;
36
37   /* for use with GtkFileChooserEmbed */
38   gint default_width;
39   gint default_height;
40   gboolean resize_horizontally;
41   gboolean resize_vertically;
42 };
43
44 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o)  (GTK_FILE_CHOOSER_DIALOG (o)->priv)
45
46 static void gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class);
47 static void gtk_file_chooser_dialog_init       (GtkFileChooserDialog      *dialog);
48 static void gtk_file_chooser_dialog_finalize   (GObject                   *object);
49
50 static GObject* gtk_file_chooser_dialog_constructor  (GType                  type,
51                                                       guint                  n_construct_properties,
52                                                       GObjectConstructParam *construct_params);
53 static void     gtk_file_chooser_dialog_set_property (GObject               *object,
54                                                       guint                  prop_id,
55                                                       const GValue          *value,
56                                                       GParamSpec            *pspec);
57 static void     gtk_file_chooser_dialog_get_property (GObject               *object,
58                                                       guint                  prop_id,
59                                                       GValue                *value,
60                                                       GParamSpec            *pspec);
61
62 static void     gtk_file_chooser_dialog_style_set    (GtkWidget             *widget,
63                                                       GtkStyle              *previous_style);
64
65 static void response_cb (GtkDialog *dialog,
66                          gint       response_id);
67
68 static GObjectClass *parent_class;
69
70 GType
71 gtk_file_chooser_dialog_get_type (void)
72 {
73   static GType file_chooser_dialog_type = 0;
74
75   if (!file_chooser_dialog_type)
76     {
77       static const GTypeInfo file_chooser_dialog_info =
78       {
79         sizeof (GtkFileChooserDialogClass),
80         NULL,           /* base_init */
81         NULL,           /* base_finalize */
82         (GClassInitFunc) gtk_file_chooser_dialog_class_init,
83         NULL,           /* class_finalize */
84         NULL,           /* class_data */
85         sizeof (GtkFileChooserDialog),
86         0,              /* n_preallocs */
87         (GInstanceInitFunc) gtk_file_chooser_dialog_init,
88       };
89
90       static const GInterfaceInfo file_chooser_info =
91       {
92         (GInterfaceInitFunc) _gtk_file_chooser_delegate_iface_init, /* interface_init */
93         NULL,                                                       /* interface_finalize */
94         NULL                                                        /* interface_data */
95       };
96
97       file_chooser_dialog_type = g_type_register_static (GTK_TYPE_DIALOG, "GtkFileChooserDialog",
98                                                          &file_chooser_dialog_info, 0);
99       g_type_add_interface_static (file_chooser_dialog_type,
100                                    GTK_TYPE_FILE_CHOOSER,
101                                    &file_chooser_info);
102     }
103
104   return file_chooser_dialog_type;
105 }
106
107 static void
108 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
109 {
110   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
111   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
112
113   parent_class = g_type_class_peek_parent (class);
114
115   gobject_class->constructor = gtk_file_chooser_dialog_constructor;
116   gobject_class->set_property = gtk_file_chooser_dialog_set_property;
117   gobject_class->get_property = gtk_file_chooser_dialog_get_property;
118   gobject_class->finalize = gtk_file_chooser_dialog_finalize;
119
120   widget_class->style_set = gtk_file_chooser_dialog_style_set;
121
122   _gtk_file_chooser_install_properties (gobject_class);
123
124   g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
125 }
126
127 static void
128 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
129 {
130   GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
131                                                                    GTK_TYPE_FILE_CHOOSER_DIALOG,
132                                                                    GtkFileChooserDialogPrivate);
133   dialog->priv = priv;
134   dialog->priv->default_width = -1;
135   dialog->priv->default_height = -1;
136   dialog->priv->resize_horizontally = TRUE;
137   dialog->priv->resize_vertically = TRUE;
138
139   gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
140
141   /* We do a signal connection here rather than overriding the method in
142    * class_init because GtkDialog::response is a RUN_LAST signal.  We want *our*
143    * handler to be run *first*, regardless of whether the user installs response
144    * handlers of his own.
145    */
146   g_signal_connect (dialog, "response",
147                     G_CALLBACK (response_cb), NULL);
148 }
149
150 static void
151 gtk_file_chooser_dialog_finalize (GObject *object)
152 {
153   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
154
155   g_free (dialog->priv->file_system);
156
157   G_OBJECT_CLASS (parent_class)->finalize (object);  
158 }
159
160 /* Callback used when the user activates a file in the file chooser widget */
161 static void
162 file_chooser_widget_file_activated (GtkFileChooser       *chooser,
163                                     GtkFileChooserDialog *dialog)
164 {
165   GList *children, *l;
166
167   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
168     return;
169
170   /* There probably isn't a default widget, so make things easier for the
171    * programmer by looking for a reasonable button on our own.
172    */
173
174   children = gtk_container_get_children (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area));
175
176   for (l = children; l; l = l->next)
177     {
178       GtkWidget *widget;
179       int response_id;
180
181       widget = GTK_WIDGET (l->data);
182       response_id = _gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
183       if (response_id == GTK_RESPONSE_ACCEPT
184           || response_id == GTK_RESPONSE_OK
185           || response_id == GTK_RESPONSE_YES
186           || response_id == GTK_RESPONSE_APPLY)
187         {
188           gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
189           break;
190         }
191     }
192
193   g_list_free (children);
194 }
195
196 static void
197 file_chooser_widget_update_hints (GtkFileChooserDialog *dialog)
198 {
199   GtkFileChooserDialogPrivate *priv;
200   GdkGeometry geometry;
201
202   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
203
204   geometry.min_width = -1;
205   geometry.min_height = -1;
206   geometry.max_width = (priv->resize_horizontally?G_MAXSHORT:-1);
207   geometry.max_height = (priv->resize_vertically?G_MAXSHORT:-1);
208
209   gtk_window_set_geometry_hints (GTK_WINDOW (dialog), NULL,
210                                  &geometry,
211                                  GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
212 }
213
214 static void
215 clamp_to_screen (GtkWidget *widget,
216                  gint      *width,
217                  gint      *height)
218 {
219   GdkScreen *screen;
220   int monitor_num;
221   GdkRectangle monitor;
222
223   g_return_if_fail (GTK_WIDGET_REALIZED (widget));
224   
225   screen = gtk_widget_get_screen (widget);
226   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
227
228   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
229
230   if (width)
231     *width = MIN (*width, (monitor.width * 3) / 4);
232
233   if (height)
234     *height = MIN (*height, (monitor.height * 3) / 4);
235 }
236
237 static void
238 file_chooser_widget_default_realized_size_changed (GtkWidget            *widget,
239                                                    GtkFileChooserDialog *dialog)
240 {
241   GtkFileChooserDialogPrivate *priv;
242   gint width;
243   gint height;
244   gint default_width, default_height;
245   GtkRequisition req;
246   gboolean resize_horizontally;
247   gboolean resize_vertically;
248   gboolean update_hints;
249   gint dx = 0, dy = 0;
250   gint cur_width, cur_height;
251
252   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
253
254   /* Force a size request of everything before we start.  This will make sure
255    * that widget->requisition is meaningful. */
256   gtk_widget_size_request (GTK_WIDGET (dialog), &req);
257   gtk_window_get_size (GTK_WINDOW (dialog), &cur_width, &cur_height);
258   width = GTK_WIDGET (dialog)->requisition.width - priv->widget->requisition.width;
259   height = GTK_WIDGET (dialog)->requisition.height - priv->widget->requisition.height;
260   _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
261                                             &default_width, &default_height);
262
263   /* Ideal target size modulo any resizing */
264   width = default_width + width;
265   height = default_height + height;
266
267   /* Now, we test for resizability */
268   update_hints = FALSE;
269   _gtk_file_chooser_embed_get_resizable_hints (GTK_FILE_CHOOSER_EMBED (priv->widget),
270                                                &resize_horizontally,
271                                                &resize_vertically);
272   resize_vertically = (!! resize_vertically);     /* normalize */
273   resize_horizontally = (!! resize_horizontally);
274
275   if (resize_horizontally && priv->resize_horizontally)
276     {
277       dx = default_width - priv->default_width;
278       priv->default_width = default_width;
279     }
280   else if (resize_horizontally && ! priv->resize_horizontally)
281     {
282       /* We restore to the ideal size + any change in default_size (which is not
283        * expected).  It would be nicer to store the older size to restore to in
284        * the future. */
285       dx = default_width - priv->default_width;
286       dx += width - cur_width;
287       priv->default_width = default_width;
288       update_hints = TRUE;
289     }
290   else
291     {
292       update_hints = TRUE;
293     }
294   
295   if (resize_vertically && priv->resize_vertically)
296     {
297       dy = default_height - priv->default_height;
298       priv->default_height = default_height;
299     }
300   else if (resize_vertically && ! priv->resize_vertically)
301     {
302       dy = default_height - priv->default_height;
303       dy += height - cur_height;
304       priv->default_height = default_height;
305       update_hints = TRUE;
306     }
307   else
308     {
309       update_hints = TRUE;
310     }
311
312   priv->resize_horizontally = resize_horizontally;
313   priv->resize_vertically = resize_vertically;
314
315   if (dx != 0 || dy != 0)
316     {
317       gint new_width = cur_width + dx;
318       gint new_height = cur_height + dy;
319
320       clamp_to_screen (GTK_WIDGET (dialog), &new_width, &new_height);
321       
322       gtk_window_resize (GTK_WINDOW (dialog), new_width, new_height);
323     }
324
325   /* Only store the size if we can resize in that direction. */
326   if (update_hints)
327     file_chooser_widget_update_hints (dialog);
328 }
329
330 static void
331 file_chooser_widget_default_unrealized_size_changed (GtkWidget            *widget,
332                                                      GtkFileChooserDialog *dialog)
333 {
334   GtkFileChooserDialogPrivate *priv;
335   GtkRequisition req;
336   gint width, height;
337
338   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
339   gtk_widget_size_request (GTK_WIDGET (dialog), &req);
340
341   _gtk_file_chooser_embed_get_resizable_hints (GTK_FILE_CHOOSER_EMBED (priv->widget),
342                                                &(priv->resize_horizontally),
343                                                &(priv->resize_vertically));
344   _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
345                                             &(priv->default_width), &(priv->default_height));
346   
347   /* Determine how much space the rest of the dialog uses compared to priv->widget */
348   width = priv->default_width + GTK_WIDGET (dialog)->requisition.width - priv->widget->requisition.width;
349   height = priv->default_height + GTK_WIDGET (dialog)->requisition.height - priv->widget->requisition.height;
350
351   gtk_window_set_default_size (GTK_WINDOW (dialog), width, height);
352   file_chooser_widget_update_hints (dialog);
353 }
354
355 static void
356 file_chooser_widget_default_size_changed (GtkWidget            *widget,
357                                           GtkFileChooserDialog *dialog)
358 {
359   if (GTK_WIDGET_REALIZED (dialog))
360     file_chooser_widget_default_realized_size_changed (widget, dialog);
361   else
362     file_chooser_widget_default_unrealized_size_changed (widget, dialog);
363 }
364   
365 static GObject*
366 gtk_file_chooser_dialog_constructor (GType                  type,
367                                      guint                  n_construct_properties,
368                                      GObjectConstructParam *construct_params)
369 {
370   GtkFileChooserDialogPrivate *priv;
371   GObject *object;
372
373   object = parent_class->constructor (type,
374                                       n_construct_properties,
375                                       construct_params);
376   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
377
378   gtk_widget_push_composite_child ();
379
380   if (priv->file_system)
381     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
382                                  "file-system-backend", priv->file_system,
383                                  NULL);
384   else
385     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
386
387   g_signal_connect (priv->widget, "file-activated",
388                     G_CALLBACK (file_chooser_widget_file_activated), object);
389   g_signal_connect (priv->widget, "default-size-changed",
390                     G_CALLBACK (file_chooser_widget_default_size_changed), object);
391
392   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (object)->vbox), priv->widget, TRUE, TRUE, 0);
393
394   gtk_widget_show (priv->widget);
395
396   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
397                                   GTK_FILE_CHOOSER (priv->widget));
398
399   _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
400
401   gtk_widget_pop_composite_child ();
402
403   return object;
404 }
405
406 static void
407 gtk_file_chooser_dialog_set_property (GObject         *object,
408                                       guint            prop_id,
409                                       const GValue    *value,
410                                       GParamSpec      *pspec)
411
412 {
413   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
414
415   switch (prop_id)
416     {
417     case GTK_FILE_CHOOSER_PROP_FILE_SYSTEM_BACKEND:
418       g_free (priv->file_system);
419       priv->file_system = g_value_dup_string (value);
420       break;
421     default:
422       g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
423       break;
424     }
425 }
426
427 static void
428 gtk_file_chooser_dialog_get_property (GObject         *object,
429                                       guint            prop_id,
430                                       GValue          *value,
431                                       GParamSpec      *pspec)
432 {
433   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
434
435   g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
436 }
437
438 #if 0
439 static void
440 set_default_size (GtkFileChooserDialog *dialog)
441 {
442   GtkWidget *widget;
443   GtkWindow *window;
444   int default_width, default_height;
445   int width, height;
446   int font_size;
447   GdkScreen *screen;
448   int monitor_num;
449   GtkRequisition req;
450   GdkRectangle monitor;
451
452   widget = GTK_WIDGET (dialog);
453   window = GTK_WINDOW (dialog);
454
455   /* Size based on characters */
456
457   font_size = pango_font_description_get_size (widget->style->font_desc);
458   font_size = PANGO_PIXELS (font_size);
459
460   width = font_size * NUM_CHARS;
461   height = font_size * NUM_LINES;
462
463   /* Use at least the requisition size... */
464
465   gtk_widget_size_request (widget, &req);
466   width = MAX (width, req.width);
467   height = MAX (height, req.height);
468
469   /* ... but no larger than the monitor */
470
471   screen = gtk_widget_get_screen (widget);
472   monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window);
473
474   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
475
476   width = MIN (width, monitor.width * 3 / 4);
477   height = MIN (height, monitor.height * 3 / 4);
478
479   /* Set size */
480
481   gtk_window_get_default_size (window, &default_width, &default_height);
482
483   gtk_window_set_default_size (window,
484                                (default_width == -1) ? width : default_width,
485                                (default_height == -1) ? height : default_height);
486 }
487 #endif
488
489 static void
490 gtk_file_chooser_dialog_style_set (GtkWidget *widget,
491                                    GtkStyle  *previous_style)
492 {
493   GtkDialog *dialog;
494
495   if (GTK_WIDGET_CLASS (parent_class)->style_set)
496     GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style);
497
498   dialog = GTK_DIALOG (widget);
499
500   /* Override the style properties with HIG-compliant spacings.  Ugh.
501    * http://developer.gnome.org/projects/gup/hig/1.0/layout.html#layout-dialogs
502    * http://developer.gnome.org/projects/gup/hig/1.0/windows.html#alert-spacing
503    */
504
505   gtk_container_set_border_width (GTK_CONTAINER (dialog->vbox), 12);
506   gtk_box_set_spacing (GTK_BOX (dialog->vbox), 24);
507
508   gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 0);
509   gtk_box_set_spacing (GTK_BOX (dialog->action_area), 6);
510 }
511
512 /* GtkDialog::response handler */
513 static void
514 response_cb (GtkDialog *dialog,
515              gint       response_id)
516 {
517   GtkFileChooserDialogPrivate *priv;
518
519   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
520
521   /* Act only on response IDs we recognize */
522   if (!(response_id == GTK_RESPONSE_ACCEPT
523         || response_id == GTK_RESPONSE_OK
524         || response_id == GTK_RESPONSE_YES
525         || response_id == GTK_RESPONSE_APPLY))
526     return;
527
528   if (!_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
529     g_signal_stop_emission_by_name (dialog, "response");
530 }
531
532 static GtkWidget *
533 gtk_file_chooser_dialog_new_valist (const gchar          *title,
534                                     GtkWindow            *parent,
535                                     GtkFileChooserAction  action,
536                                     const gchar          *backend,
537                                     const gchar          *first_button_text,
538                                     va_list               varargs)
539 {
540   GtkWidget *result;
541   const char *button_text = first_button_text;
542   gint response_id;
543
544   result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
545                          "title", title,
546                          "action", action,
547                          "file-system-backend", backend,
548                          NULL);
549
550   if (parent)
551     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
552
553   while (button_text)
554     {
555       response_id = va_arg (varargs, gint);
556       gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
557       button_text = va_arg (varargs, const gchar *);
558     }
559
560   return result;
561 }
562
563 /**
564  * gtk_file_chooser_dialog_new:
565  * @title: Title of the dialog, or %NULL
566  * @parent: Transient parent of the dialog, or %NULL
567  * @action: Open or save mode for the dialog
568  * @first_button_text: stock ID or text to go in the first button, or %NULL
569  * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
570  *
571  * Creates a new #GtkFileChooserDialog.  This function is analogous to
572  * gtk_dialog_new_with_buttons().
573  *
574  * Return value: a new #GtkFileChooserDialog
575  *
576  * Since: 2.4
577  **/
578 GtkWidget *
579 gtk_file_chooser_dialog_new (const gchar         *title,
580                              GtkWindow           *parent,
581                              GtkFileChooserAction action,
582                              const gchar         *first_button_text,
583                              ...)
584 {
585   GtkWidget *result;
586   va_list varargs;
587   
588   va_start (varargs, first_button_text);
589   result = gtk_file_chooser_dialog_new_valist (title, parent, action,
590                                                NULL, first_button_text,
591                                                varargs);
592   va_end (varargs);
593
594   return result;
595 }
596
597 /**
598  * gtk_file_chooser_dialog_new_with_backend:
599  * @title: Title of the dialog, or %NULL
600  * @parent: Transient parent of the dialog, or %NULL
601  * @backend: The name of the specific filesystem backend to use.
602  * @action: Open or save mode for the dialog
603  * @first_button_text: stock ID or text to go in the first button, or %NULL
604  * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
605  *
606  * Creates a new #GtkFileChooserDialog with a specified backend. This is
607  * especially useful if you use gtk_file_chooser_set_local_only() to allow
608  * non-local files and you use a more expressive vfs, such as gnome-vfs,
609  * to load files.
610  *
611  * Return value: a new #GtkFileChooserDialog
612  *
613  * Since: 2.4
614  **/
615 GtkWidget *
616 gtk_file_chooser_dialog_new_with_backend (const gchar          *title,
617                                           GtkWindow            *parent,
618                                           GtkFileChooserAction  action,
619                                           const gchar          *backend,
620                                           const gchar          *first_button_text,
621                                           ...)
622 {
623   GtkWidget *result;
624   va_list varargs;
625   
626   va_start (varargs, first_button_text);
627   result = gtk_file_chooser_dialog_new_valist (title, parent, action,
628                                                backend, first_button_text,
629                                                varargs);
630   va_end (varargs);
631
632   return result;
633 }