]> Pileus Git - ~andy/gtk/blob - gtk/gtkfilechooserdialog.c
Move documentation to inline comments: GtkFileChooserDialog
[~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
24 #include "gtkfilechooserdialog.h"
25
26 #include "gtkfilechooserprivate.h"
27 #include "gtkfilechooserwidget.h"
28 #include "gtkfilechooserutils.h"
29 #include "gtkfilechooserembed.h"
30 #include "gtkfilesystem.h"
31 #include "gtksizerequest.h"
32 #include "gtktypebuiltins.h"
33 #include "gtkintl.h"
34
35 #include <stdarg.h>
36
37
38 /**
39  * SECTION:gtkfilechooserdialog
40  * @Short_description: A file chooser dialog, suitable for "File/Open" or "File/Save" commands
41  * @Title: GtkFileChooserDialog
42  * @See_also: #GtkFileChooser, #GtkDialog
43  *
44  * #GtkFileChooserDialog is a dialog box suitable for use with
45  * "File/Open" or "File/Save as" commands.  This widget works by
46  * putting a #GtkFileChooserWidget inside a #GtkDialog.  It exposes
47  * the #GtkFileChooserIface interface, so you can use all of the
48  * #GtkFileChooser functions on the file chooser dialog as well as
49  * those for #GtkDialog.
50  *
51  * Note that #GtkFileChooserDialog does not have any methods of its
52  * own.  Instead, you should use the functions that work on a
53  * #GtkFileChooser.
54  *
55  * <example id="gtkfilechooser-typical-usage">
56  * <title>Typical usage</title>
57  * In the simplest of cases, you can the following code to use
58  * #GtkFileChooserDialog to select a file for opening:
59  * <para>
60  * <informalexample><programlisting>
61  * GtkWidget *dialog;
62  *
63  * dialog = gtk_file_chooser_dialog_new ("Open File",
64  *                                       parent_window,
65  *                                       GTK_FILE_CHOOSER_ACTION_OPEN,
66  *                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
67  *                                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
68  *                                       NULL);
69  *
70  * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
71  *   {
72  *     char *filename;
73  *
74  *     filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
75  *     open_file (filename);
76  *     g_free (filename);
77  *   }
78  *
79  * gtk_widget_destroy (dialog);
80  * </programlisting></informalexample>
81  * </para>
82  * To use a dialog for saving, you can use this:
83  * <para>
84  * <informalexample><programlisting>
85  * GtkWidget *dialog;
86  *
87  * dialog = gtk_file_chooser_dialog_new ("Save File",
88  *                                       parent_window,
89  *                                       GTK_FILE_CHOOSER_ACTION_SAVE,
90  *                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
91  *                                       GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
92  *                                       NULL);
93  * gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
94  *
95  * if (user_edited_a_new_document)
96  *   {
97  *     gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), default_folder_for_saving);
98  *     gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), "Untitled document");
99  *   }
100  * else
101  *   gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), filename_for_existing_document);
102  *
103  * if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
104  *   {
105  *     char *filename;
106  *
107  *     filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
108  *     save_to_file (filename);
109  *     g_free (filename);
110  *   }
111  *
112  * gtk_widget_destroy (dialog);
113  * </programlisting></informalexample>
114  * </para>
115  * </example>
116  * <section id="gtkfilechooserdialog-response-codes">
117  * <title>Response Codes</title>
118  * #GtkFileChooserDialog inherits from #GtkDialog, so buttons that
119  * go in its action area have response codes such as
120  * #GTK_RESPONSE_ACCEPT and #GTK_RESPONSE_CANCEL.  For example, you
121  * could call gtk_file_chooser_dialog_new() as follows:
122  * <para>
123  * <informalexample><programlisting>
124  * GtkWidget *dialog;
125  *
126  * dialog = gtk_file_chooser_dialog_new ("Open File",
127  *                                       parent_window,
128  *                                       GTK_FILE_CHOOSER_ACTION_OPEN,
129  *                                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
130  *                                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
131  *                                       NULL);
132  * </programlisting></informalexample>
133  * </para>
134  * This will create buttons for "Cancel" and "Open" that use stock
135  * response identifiers from #GtkResponseType.  For most dialog
136  * boxes you can use your own custom response codes rather than the
137  * ones in #GtkResponseType, but #GtkFileChooserDialog assumes that
138  * its "accept"-type action, e.g. an "Open" or "Save" button,
139  * <emphasis>will</emphasis> have one of the following response
140  * codes:
141  * <para>
142  * <simplelist id="gtkfilechooserdialog-responses">
143  * <member>#GTK_RESPONSE_ACCEPT</member>
144  * <member>#GTK_RESPONSE_OK</member>
145  * <member>#GTK_RESPONSE_YES</member>
146  * <member>#GTK_RESPONSE_APPLY</member>
147  * </simplelist>
148  * </para>
149  * This is because #GtkFileChooserDialog must intercept responses
150  * and switch to folders if appropriate, rather than letting the
151  * dialog terminate &mdash; the implementation uses these known
152  * response codes to know which responses can be blocked if
153  * appropriate.
154  * <para>
155  * <note>
156  * To summarize, make sure you use a
157  * <link linkend="gtkfilechooserdialog-responses">stock response code</link>
158  * when you use #GtkFileChooserDialog to ensure proper operation.
159  * </note>
160  * </para>
161  * </section>
162  */
163
164
165 #define GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE(o)  (GTK_FILE_CHOOSER_DIALOG (o)->priv)
166
167 static void gtk_file_chooser_dialog_finalize   (GObject                   *object);
168
169 static GObject* gtk_file_chooser_dialog_constructor  (GType                  type,
170                                                       guint                  n_construct_properties,
171                                                       GObjectConstructParam *construct_params);
172 static void     gtk_file_chooser_dialog_set_property (GObject               *object,
173                                                       guint                  prop_id,
174                                                       const GValue          *value,
175                                                       GParamSpec            *pspec);
176 static void     gtk_file_chooser_dialog_get_property (GObject               *object,
177                                                       guint                  prop_id,
178                                                       GValue                *value,
179                                                       GParamSpec            *pspec);
180
181 static void     gtk_file_chooser_dialog_map          (GtkWidget             *widget);
182
183 static void response_cb (GtkDialog *dialog,
184                          gint       response_id);
185
186 G_DEFINE_TYPE_WITH_CODE (GtkFileChooserDialog, gtk_file_chooser_dialog, GTK_TYPE_DIALOG,
187                          G_IMPLEMENT_INTERFACE (GTK_TYPE_FILE_CHOOSER,
188                                                 _gtk_file_chooser_delegate_iface_init))
189
190 static void
191 gtk_file_chooser_dialog_class_init (GtkFileChooserDialogClass *class)
192 {
193   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
194   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
195
196   gobject_class->constructor = gtk_file_chooser_dialog_constructor;
197   gobject_class->set_property = gtk_file_chooser_dialog_set_property;
198   gobject_class->get_property = gtk_file_chooser_dialog_get_property;
199   gobject_class->finalize = gtk_file_chooser_dialog_finalize;
200
201   widget_class->map       = gtk_file_chooser_dialog_map;
202
203   _gtk_file_chooser_install_properties (gobject_class);
204
205   g_type_class_add_private (class, sizeof (GtkFileChooserDialogPrivate));
206 }
207
208 static void
209 gtk_file_chooser_dialog_init (GtkFileChooserDialog *dialog)
210 {
211   GtkWidget *action_area, *content_area;
212   GtkFileChooserDialogPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
213                                                                    GTK_TYPE_FILE_CHOOSER_DIALOG,
214                                                                    GtkFileChooserDialogPrivate);
215   GtkDialog *fc_dialog = GTK_DIALOG (dialog);
216
217   dialog->priv = priv;
218   dialog->priv->response_requested = FALSE;
219
220   content_area = gtk_dialog_get_content_area (fc_dialog);
221   action_area = gtk_dialog_get_action_area (fc_dialog);
222
223   gtk_container_set_border_width (GTK_CONTAINER (fc_dialog), 5);
224   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
225   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
226
227   /* We do a signal connection here rather than overriding the method in
228    * class_init because GtkDialog::response is a RUN_LAST signal.  We want *our*
229    * handler to be run *first*, regardless of whether the user installs response
230    * handlers of his own.
231    */
232   g_signal_connect (dialog, "response",
233                     G_CALLBACK (response_cb), NULL);
234 }
235
236 static void
237 gtk_file_chooser_dialog_finalize (GObject *object)
238 {
239   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (object);
240
241   g_free (dialog->priv->file_system);
242
243   G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->finalize (object);  
244 }
245
246 static gboolean
247 is_stock_accept_response_id (int response_id)
248 {
249   return (response_id == GTK_RESPONSE_ACCEPT
250           || response_id == GTK_RESPONSE_OK
251           || response_id == GTK_RESPONSE_YES
252           || response_id == GTK_RESPONSE_APPLY);
253 }
254
255 /* Callback used when the user activates a file in the file chooser widget */
256 static void
257 file_chooser_widget_file_activated (GtkFileChooser       *chooser,
258                                     GtkFileChooserDialog *dialog)
259 {
260   GtkDialog *fc_dialog = GTK_DIALOG (dialog);
261   GtkWidget *action_area;
262   GList *children, *l;
263
264   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
265     return;
266
267   /* There probably isn't a default widget, so make things easier for the
268    * programmer by looking for a reasonable button on our own.
269    */
270   action_area = gtk_dialog_get_action_area (fc_dialog);
271   children = gtk_container_get_children (GTK_CONTAINER (action_area));
272
273   for (l = children; l; l = l->next)
274     {
275       GtkWidget *widget;
276       int response_id;
277
278       widget = GTK_WIDGET (l->data);
279       response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
280       if (gtk_widget_is_sensitive (widget) &&
281           is_stock_accept_response_id (response_id))
282         {
283           gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
284           break;
285         }
286     }
287
288   g_list_free (children);
289 }
290
291 #if 0
292 /* FIXME: to see why this function is ifdef-ed out, see the comment below in
293  * file_chooser_widget_default_size_changed().
294  */
295 static void
296 load_position (int *out_xpos, int *out_ypos)
297 {
298   GtkFileChooserSettings *settings;
299   int x, y, width, height;
300
301   settings = _gtk_file_chooser_settings_new ();
302   _gtk_file_chooser_settings_get_geometry (settings, &x, &y, &width, &height);
303   g_object_unref (settings);
304
305   *out_xpos = x;
306   *out_ypos = y;
307 }
308 #endif
309
310 static void
311 file_chooser_widget_default_size_changed (GtkWidget            *widget,
312                                           GtkFileChooserDialog *dialog)
313 {
314   GtkFileChooserDialogPrivate *priv;
315   gint default_width, default_height;
316   GtkRequisition req, widget_req;
317
318   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
319
320   /* Unset any previously set size */
321   gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, -1);
322
323   if (gtk_widget_is_drawable (widget))
324     {
325       /* Force a size request of everything before we start.  This will make sure
326        * that widget->requisition is meaningful. */
327       gtk_widget_get_preferred_size (GTK_WIDGET (dialog),
328                                      &req, NULL);
329       gtk_widget_get_preferred_size (widget,
330                                      &widget_req, NULL);
331     }
332
333   _gtk_file_chooser_embed_get_default_size (GTK_FILE_CHOOSER_EMBED (priv->widget),
334                                             &default_width, &default_height);
335
336   gtk_window_resize (GTK_WINDOW (dialog), default_width, default_height);
337
338   if (!gtk_widget_get_mapped (GTK_WIDGET (dialog)))
339     {
340 #if 0
341       /* FIXME: the code to restore the position does not work yet.  It is not
342        * clear whether it is actually desirable --- if enabled, applications
343        * would not be able to say "center the file chooser on top of my toplevel
344        * window".  So, we don't use this code at all.
345        */
346       load_position (&xpos, &ypos);
347       if (xpos >= 0 && ypos >= 0)
348         {
349           gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_NONE);
350           gtk_window_move (GTK_WINDOW (dialog), xpos, ypos);
351         }
352 #endif
353     }
354 }
355
356 static void
357 file_chooser_widget_response_requested (GtkWidget            *widget,
358                                         GtkFileChooserDialog *dialog)
359 {
360   GtkDialog *fc_dialog = GTK_DIALOG (dialog);
361   GtkWidget *action_area;
362   GList *children, *l;
363
364   dialog->priv->response_requested = TRUE;
365
366   if (gtk_window_activate_default (GTK_WINDOW (dialog)))
367     return;
368
369   /* There probably isn't a default widget, so make things easier for the
370    * programmer by looking for a reasonable button on our own.
371    */
372   action_area = gtk_dialog_get_action_area (fc_dialog);
373   children = gtk_container_get_children (GTK_CONTAINER (action_area));
374
375   for (l = children; l; l = l->next)
376     {
377       GtkWidget *widget;
378       int response_id;
379
380       widget = GTK_WIDGET (l->data);
381       response_id = gtk_dialog_get_response_for_widget (fc_dialog, widget);
382       if (gtk_widget_is_sensitive (widget) &&
383           is_stock_accept_response_id (response_id))
384         {
385           gtk_widget_activate (widget); /* Should we gtk_dialog_response (dialog, response_id) instead? */
386           break;
387         }
388     }
389
390   if (l == NULL)
391     dialog->priv->response_requested = FALSE;
392
393   g_list_free (children);
394 }
395   
396 static GObject*
397 gtk_file_chooser_dialog_constructor (GType                  type,
398                                      guint                  n_construct_properties,
399                                      GObjectConstructParam *construct_params)
400 {
401   GtkFileChooserDialogPrivate *priv;
402   GtkWidget *content_area;
403   GObject *object;
404
405   object = G_OBJECT_CLASS (gtk_file_chooser_dialog_parent_class)->constructor (type,
406                                                                                n_construct_properties,
407                                                                                construct_params);
408   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
409
410   gtk_widget_push_composite_child ();
411
412   if (priv->file_system)
413     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET,
414                                  NULL);
415   else
416     priv->widget = g_object_new (GTK_TYPE_FILE_CHOOSER_WIDGET, NULL);
417
418   g_signal_connect (priv->widget, "file-activated",
419                     G_CALLBACK (file_chooser_widget_file_activated), object);
420   g_signal_connect (priv->widget, "default-size-changed",
421                     G_CALLBACK (file_chooser_widget_default_size_changed), object);
422   g_signal_connect (priv->widget, "response-requested",
423                     G_CALLBACK (file_chooser_widget_response_requested), object);
424
425   content_area = gtk_dialog_get_content_area (GTK_DIALOG (object));
426
427   gtk_container_set_border_width (GTK_CONTAINER (priv->widget), 5);
428   gtk_box_pack_start (GTK_BOX (content_area), priv->widget, TRUE, TRUE, 0);
429
430   gtk_widget_show (priv->widget);
431
432   _gtk_file_chooser_set_delegate (GTK_FILE_CHOOSER (object),
433                                   GTK_FILE_CHOOSER (priv->widget));
434
435   gtk_widget_pop_composite_child ();
436
437   return object;
438 }
439
440 static void
441 gtk_file_chooser_dialog_set_property (GObject         *object,
442                                       guint            prop_id,
443                                       const GValue    *value,
444                                       GParamSpec      *pspec)
445
446 {
447   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
448
449   switch (prop_id)
450     {
451     default:
452       g_object_set_property (G_OBJECT (priv->widget), pspec->name, value);
453       break;
454     }
455 }
456
457 static void
458 gtk_file_chooser_dialog_get_property (GObject         *object,
459                                       guint            prop_id,
460                                       GValue          *value,
461                                       GParamSpec      *pspec)
462 {
463   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (object);
464
465   g_object_get_property (G_OBJECT (priv->widget), pspec->name, value);
466 }
467
468 static void
469 foreach_ensure_default_response_cb (GtkWidget *widget,
470                                     gpointer   data)
471 {
472   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (data);
473   int response_id;
474
475   response_id = gtk_dialog_get_response_for_widget (GTK_DIALOG (dialog), widget);
476   if (is_stock_accept_response_id (response_id))
477     gtk_dialog_set_default_response (GTK_DIALOG (dialog), response_id);
478 }
479
480 static void
481 ensure_default_response (GtkFileChooserDialog *dialog)
482 {
483   GtkWidget *action_area;
484
485   action_area = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
486   gtk_container_foreach (GTK_CONTAINER (action_area),
487                          foreach_ensure_default_response_cb,
488                          dialog);
489 }
490
491 /* GtkWidget::map handler */
492 static void
493 gtk_file_chooser_dialog_map (GtkWidget *widget)
494 {
495   GtkFileChooserDialog *dialog = GTK_FILE_CHOOSER_DIALOG (widget);
496   GtkFileChooserDialogPrivate *priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
497
498   ensure_default_response (dialog);
499
500   _gtk_file_chooser_embed_initial_focus (GTK_FILE_CHOOSER_EMBED (priv->widget));
501
502   GTK_WIDGET_CLASS (gtk_file_chooser_dialog_parent_class)->map (widget);
503 }
504
505 /* GtkDialog::response handler */
506 static void
507 response_cb (GtkDialog *dialog,
508              gint       response_id)
509 {
510   GtkFileChooserDialogPrivate *priv;
511
512   priv = GTK_FILE_CHOOSER_DIALOG_GET_PRIVATE (dialog);
513
514   /* Act only on response IDs we recognize */
515   if (is_stock_accept_response_id (response_id)
516       && !priv->response_requested
517       && !_gtk_file_chooser_embed_should_respond (GTK_FILE_CHOOSER_EMBED (priv->widget)))
518     {
519       g_signal_stop_emission_by_name (dialog, "response");
520     }
521
522   priv->response_requested = FALSE;
523 }
524
525 static GtkWidget *
526 gtk_file_chooser_dialog_new_valist (const gchar          *title,
527                                     GtkWindow            *parent,
528                                     GtkFileChooserAction  action,
529                                     const gchar          *first_button_text,
530                                     va_list               varargs)
531 {
532   GtkWidget *result;
533   const char *button_text = first_button_text;
534   gint response_id;
535
536   result = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
537                          "title", title,
538                          "action", action,
539                          NULL);
540
541   if (parent)
542     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
543
544   while (button_text)
545     {
546       response_id = va_arg (varargs, gint);
547       gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
548       button_text = va_arg (varargs, const gchar *);
549     }
550
551   return result;
552 }
553
554 /**
555  * gtk_file_chooser_dialog_new:
556  * @title: (allow-none): Title of the dialog, or %NULL
557  * @parent: (allow-none): Transient parent of the dialog, or %NULL
558  * @action: Open or save mode for the dialog
559  * @first_button_text: (allow-none): stock ID or text to go in the first button, or %NULL
560  * @Varargs: response ID for the first button, then additional (button, id) pairs, ending with %NULL
561  *
562  * Creates a new #GtkFileChooserDialog.  This function is analogous to
563  * gtk_dialog_new_with_buttons().
564  *
565  * Return value: a new #GtkFileChooserDialog
566  *
567  * Since: 2.4
568  **/
569 GtkWidget *
570 gtk_file_chooser_dialog_new (const gchar         *title,
571                              GtkWindow           *parent,
572                              GtkFileChooserAction action,
573                              const gchar         *first_button_text,
574                              ...)
575 {
576   GtkWidget *result;
577   va_list varargs;
578   
579   va_start (varargs, first_button_text);
580   result = gtk_file_chooser_dialog_new_valist (title, parent, action,
581                                                first_button_text,
582                                                varargs);
583   va_end (varargs);
584
585   return result;
586 }