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