]> Pileus Git - ~andy/gtk/blob - gtk/gtkassistant.c
Improve GtkAssistant docs
[~andy/gtk] / gtk / gtkassistant.c
1 /* 
2  * GTK - The GIMP Toolkit
3  * Copyright (C) 1999  Red Hat, Inc.
4  * Copyright (C) 2002  Anders Carlsson <andersca@gnu.org>
5  * Copyright (C) 2003  Matthias Clasen <mclasen@redhat.com>
6  * Copyright (C) 2005  Carlos Garnacho Parro <carlosg@gnome.org>
7  *
8  * All rights reserved.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 /**
27  * SECTION:gtkassistant
28  * @Short_description: A widget used to guide users through multi-step operations
29  * @Title: GtkAssistant
30  *
31  * A #GtkAssistant is a widget used to represent a generally complex
32  * operation splitted in several steps, guiding the user through its pages
33  * and controlling the page flow to collect the necessary data.
34  *
35  * The design of GtkAssistant is that it controls what buttons to show and
36  * to make sensitive, based on what it knows about the page sequence and
37  * the <link linkend="GtkAssistantPageType">type</link> of each page, in
38  * addition to state information like the page
39  * <link linkend="gtk-assistant-set-page-complete">completion</link> and
40  * <link linkend="gtk-assistant-commit">committed</link> status.
41  *
42  * <refsect2 id="GtkAssistant-BUILDER-UI">
43  * <title>GtkAssistant as GtkBuildable</title>
44  * <para>
45  * The GtkAssistant implementation of the GtkBuildable interface exposes the
46  * @action_area as internal children with the name "action_area".
47  *
48  * To add pages to an assistant in GtkBuilder, simply add it as a
49  * &lt;child&gt; to the GtkAssistant object, and set its child properties
50  * as necessary.
51  * </para>
52  * </refsect2>
53  */
54
55 #include "config.h"
56
57 #include <atk/atk.h>
58
59 #include "gtkassistant.h"
60
61 #include "gtkaccessible.h"
62 #include "gtkbutton.h"
63 #include "gtkhbox.h"
64 #include "gtkhbbox.h"
65 #include "gtkimage.h"
66 #include "gtklabel.h"
67 #include "gtksizegroup.h"
68 #include "gtksizerequest.h"
69 #include "gtkstock.h"
70
71 #include "gtkintl.h"
72 #include "gtkprivate.h"
73 #include "gtkbuildable.h"
74
75
76 #define HEADER_SPACING 12
77 #define ACTION_AREA_SPACING 12
78
79 typedef struct _GtkAssistantPage GtkAssistantPage;
80
81 struct _GtkAssistantPage
82 {
83   GtkWidget *page;
84   GtkAssistantPageType type;
85   guint      complete : 1;
86   guint      complete_set : 1;
87
88   GtkWidget *title;
89   GdkPixbuf *header_image;
90   GdkPixbuf *sidebar_image;
91 };
92
93 struct _GtkAssistantPrivate
94 {
95   GtkWidget *cancel;
96   GtkWidget *forward;
97   GtkWidget *back;
98   GtkWidget *apply;
99   GtkWidget *close;
100   GtkWidget *last;
101
102   GtkWidget *header_image;
103   GtkWidget *sidebar_image;
104
105   GtkWidget *action_area;
106
107   GList     *pages;
108
109   GtkAssistantPage *current_page;
110
111   GSList    *visited_pages;
112
113   GtkSizeGroup *size_group;
114
115   GtkAssistantPageFunc forward_function;
116   gpointer forward_function_data;
117   GDestroyNotify forward_data_destroy;
118
119   guint committed : 1;
120 };
121
122 static void     gtk_assistant_class_init         (GtkAssistantClass *class);
123 static void     gtk_assistant_init               (GtkAssistant      *assistant);
124 static void     gtk_assistant_destroy            (GtkWidget         *widget);
125 static void     gtk_assistant_style_set          (GtkWidget         *widget,
126                                                   GtkStyle          *old_style);
127 static void     gtk_assistant_get_preferred_width  (GtkWidget        *widget,
128                                                     gint             *minimum,
129                                                     gint             *natural);
130 static void     gtk_assistant_get_preferred_height (GtkWidget        *widget,
131                                                     gint             *minimum,
132                                                     gint             *natural);
133 static void     gtk_assistant_size_allocate      (GtkWidget         *widget,
134                                                   GtkAllocation     *allocation);
135 static void     gtk_assistant_map                (GtkWidget         *widget);
136 static void     gtk_assistant_unmap              (GtkWidget         *widget);
137 static gboolean gtk_assistant_delete_event       (GtkWidget         *widget,
138                                                   GdkEventAny       *event);
139 static gboolean gtk_assistant_draw               (GtkWidget         *widget,
140                                                   cairo_t           *cr);
141 static gboolean gtk_assistant_focus              (GtkWidget         *widget,
142                                                   GtkDirectionType   direction);
143 static void     gtk_assistant_add                (GtkContainer      *container,
144                                                   GtkWidget         *page);
145 static void     gtk_assistant_remove             (GtkContainer      *container,
146                                                   GtkWidget         *page);
147 static void     gtk_assistant_forall             (GtkContainer      *container,
148                                                   gboolean           include_internals,
149                                                   GtkCallback        callback,
150                                                   gpointer           callback_data);
151 static void     gtk_assistant_set_child_property (GtkContainer      *container,
152                                                   GtkWidget         *child,
153                                                   guint              property_id,
154                                                   const GValue      *value,
155                                                   GParamSpec        *pspec);
156 static void     gtk_assistant_get_child_property (GtkContainer      *container,
157                                                   GtkWidget         *child,
158                                                   guint              property_id,
159                                                   GValue            *value,
160                                                   GParamSpec        *pspec);
161
162 static AtkObject *gtk_assistant_get_accessible   (GtkWidget         *widget);
163
164 static void       gtk_assistant_buildable_interface_init     (GtkBuildableIface *iface);
165 static GObject   *gtk_assistant_buildable_get_internal_child (GtkBuildable  *buildable,
166                                                               GtkBuilder    *builder,
167                                                               const gchar   *childname);
168 static gboolean   gtk_assistant_buildable_custom_tag_start   (GtkBuildable  *buildable,
169                                                               GtkBuilder    *builder,
170                                                               GObject       *child,
171                                                               const gchar   *tagname,
172                                                               GMarkupParser *parser,
173                                                               gpointer      *data);
174 static void       gtk_assistant_buildable_custom_finished    (GtkBuildable  *buildable,
175                                                               GtkBuilder    *builder,
176                                                               GObject       *child,
177                                                               const gchar   *tagname,
178                                                               gpointer       user_data);
179
180 static GList*     find_page                                  (GtkAssistant  *assistant,
181                                                               GtkWidget     *page);
182
183 enum
184 {
185   CHILD_PROP_0,
186   CHILD_PROP_PAGE_TYPE,
187   CHILD_PROP_PAGE_TITLE,
188   CHILD_PROP_PAGE_HEADER_IMAGE,
189   CHILD_PROP_PAGE_SIDEBAR_IMAGE,
190   CHILD_PROP_PAGE_COMPLETE
191 };
192
193 enum
194 {
195   CANCEL,
196   PREPARE,
197   APPLY,
198   CLOSE,
199   LAST_SIGNAL
200 };
201
202 static guint signals [LAST_SIGNAL] = { 0 };
203
204
205 G_DEFINE_TYPE_WITH_CODE (GtkAssistant, gtk_assistant, GTK_TYPE_WINDOW,
206                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
207                                                 gtk_assistant_buildable_interface_init))
208
209
210 static void
211 gtk_assistant_class_init (GtkAssistantClass *class)
212 {
213   GObjectClass *gobject_class;
214   GtkWidgetClass *widget_class;
215   GtkContainerClass *container_class;
216
217   gobject_class   = (GObjectClass *) class;
218   widget_class    = (GtkWidgetClass *) class;
219   container_class = (GtkContainerClass *) class;
220
221   widget_class->destroy = gtk_assistant_destroy;
222   widget_class->style_set = gtk_assistant_style_set;
223   widget_class->get_preferred_width = gtk_assistant_get_preferred_width;
224   widget_class->get_preferred_height = gtk_assistant_get_preferred_height;
225   widget_class->size_allocate = gtk_assistant_size_allocate;
226   widget_class->map = gtk_assistant_map;
227   widget_class->unmap = gtk_assistant_unmap;
228   widget_class->delete_event = gtk_assistant_delete_event;
229   widget_class->draw = gtk_assistant_draw;
230   widget_class->focus = gtk_assistant_focus;
231   widget_class->get_accessible = gtk_assistant_get_accessible;
232
233   container_class->add = gtk_assistant_add;
234   container_class->remove = gtk_assistant_remove;
235   container_class->forall = gtk_assistant_forall;
236   container_class->set_child_property = gtk_assistant_set_child_property;
237   container_class->get_child_property = gtk_assistant_get_child_property;
238
239   /**
240    * GtkAssistant::cancel:
241    * @assistant: the #GtkAssistant
242    *
243    * The ::cancel signal is emitted when then the cancel button is clicked.
244    *
245    * Since: 2.10
246    */
247   signals[CANCEL] =
248     g_signal_new (I_("cancel"),
249                   G_TYPE_FROM_CLASS (gobject_class),
250                   G_SIGNAL_RUN_LAST,
251                   G_STRUCT_OFFSET (GtkAssistantClass, cancel),
252                   NULL, NULL,
253                   g_cclosure_marshal_VOID__VOID,
254                   G_TYPE_NONE, 0);
255     
256   /**
257    * GtkAssistant::prepare:
258    * @assistant: the #GtkAssistant
259    * @page: the current page
260    *
261    * The ::prepare signal is emitted when a new page is set as the assistant's
262    * current page, before making the new page visible. A handler for this signal
263    * can do any preparation which are necessary before showing @page.
264    *
265    * Since: 2.10
266    */
267   signals[PREPARE] =
268     g_signal_new (I_("prepare"),
269                   G_TYPE_FROM_CLASS (gobject_class),
270                   G_SIGNAL_RUN_LAST,
271                   G_STRUCT_OFFSET (GtkAssistantClass, prepare),
272                   NULL, NULL,
273                   g_cclosure_marshal_VOID__OBJECT,
274                   G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
275
276   /**
277    * GtkAssistant::apply:
278    * @assistant: the #GtkAssistant
279    *
280    * The ::apply signal is emitted when the apply button is clicked. The default
281    * behavior of the #GtkAssistant is to switch to the page after the current
282    * page, unless the current page is the last one.
283    *
284    * A handler for the ::apply signal should carry out the actions for which
285    * the wizard has collected data. If the action takes a long time to complete,
286    * you might consider putting a page of type %GTK_ASSISTANT_PAGE_PROGRESS
287    * after the confirmation page and handle this operation within the
288    * #GtkAssistant::prepare signal of the progress page.
289    *
290    * Since: 2.10
291    */
292   signals[APPLY] =
293     g_signal_new (I_("apply"),
294                   G_TYPE_FROM_CLASS (gobject_class),
295                   G_SIGNAL_RUN_LAST,
296                   G_STRUCT_OFFSET (GtkAssistantClass, apply),
297                   NULL, NULL,
298                   g_cclosure_marshal_VOID__VOID,
299                   G_TYPE_NONE, 0);
300
301   /**
302    * GtkAssistant::close:
303    * @assistant: the #GtkAssistant
304    *
305    * The ::close signal is emitted either when the close button of
306    * a summary page is clicked, or when the apply button in the last
307    * page in the flow (of type %GTK_ASSISTANT_PAGE_CONFIRM) is clicked.
308    *
309    * Since: 2.10
310    */
311   signals[CLOSE] =
312     g_signal_new (I_("close"),
313                   G_TYPE_FROM_CLASS (gobject_class),
314                   G_SIGNAL_RUN_LAST,
315                   G_STRUCT_OFFSET (GtkAssistantClass, close),
316                   NULL, NULL,
317                   g_cclosure_marshal_VOID__VOID,
318                   G_TYPE_NONE, 0);
319
320   gtk_widget_class_install_style_property (widget_class,
321                                            g_param_spec_int ("header-padding",
322                                                              P_("Header Padding"),
323                                                              P_("Number of pixels around the header."),
324                                                              0,
325                                                              G_MAXINT,
326                                                              6,
327                                                              GTK_PARAM_READABLE));
328   gtk_widget_class_install_style_property (widget_class,
329                                            g_param_spec_int ("content-padding",
330                                                              P_("Content Padding"),
331                                                              P_("Number of pixels around the content pages."),
332                                                              0,
333                                                              G_MAXINT,
334                                                              1,
335                                                              GTK_PARAM_READABLE));
336
337   /**
338    * GtkAssistant:page-type:
339    *
340    * The type of the assistant page.
341    *
342    * Since: 2.10
343    */
344   gtk_container_class_install_child_property (container_class,
345                                               CHILD_PROP_PAGE_TYPE,
346                                               g_param_spec_enum ("page-type", 
347                                                                  P_("Page type"),
348                                                                  P_("The type of the assistant page"),
349                                                                  GTK_TYPE_ASSISTANT_PAGE_TYPE,
350                                                                  GTK_ASSISTANT_PAGE_CONTENT,
351                                                                  GTK_PARAM_READWRITE));
352
353   /**
354    * GtkAssistant:title:
355    *
356    * The title that is displayed in the page header.
357    *
358    * If title and header-image are both %NULL, no header is displayed.
359    *
360    * Since: 2.10
361    */
362   gtk_container_class_install_child_property (container_class,
363                                               CHILD_PROP_PAGE_TITLE,
364                                               g_param_spec_string ("title", 
365                                                                    P_("Page title"),
366                                                                    P_("The title of the assistant page"),
367                                                                    NULL,
368                                                                    GTK_PARAM_READWRITE));
369
370   /**
371    * GtkAssistant:header-image:
372    *
373    * The image that is displayed next to the title in the page header.
374    *
375    * If title and header-image are both %NULL, no header is displayed.
376    *
377    * Since: 2.10
378    */
379   gtk_container_class_install_child_property (container_class,
380                                               CHILD_PROP_PAGE_HEADER_IMAGE,
381                                               g_param_spec_object ("header-image", 
382                                                                    P_("Header image"),
383                                                                    P_("Header image for the assistant page"),
384                                                                    GDK_TYPE_PIXBUF,
385                                                                    GTK_PARAM_READWRITE));
386
387   /**
388    * GtkAssistant:header-image:
389    *
390    * The image that is displayed next to the page.
391    *
392    * Set this to %NULL to make the sidebar disappear.
393    *
394    * Since: 2.10
395    */
396   gtk_container_class_install_child_property (container_class,
397                                               CHILD_PROP_PAGE_SIDEBAR_IMAGE,
398                                               g_param_spec_object ("sidebar-image", 
399                                                                    P_("Sidebar image"),
400                                                                    P_("Sidebar image for the assistant page"),
401                                                                    GDK_TYPE_PIXBUF,
402                                                                    GTK_PARAM_READWRITE));
403   /**
404    * GtkAssistant:complete:
405    *
406    * Setting the "complete" child property to %TRUE marks a page as complete
407    * (i.e.: all the required fields are filled out). GTK+ uses this information
408    * to control the sensitivity of the navigation buttons.
409    *
410    * Since: 2.10
411    **/
412   gtk_container_class_install_child_property (container_class,
413                                               CHILD_PROP_PAGE_COMPLETE,
414                                               g_param_spec_boolean ("complete", 
415                                                                     P_("Page complete"),
416                                                                     P_("Whether all required fields on the page have been filled out"),
417                                                                     FALSE,
418                                                                     G_PARAM_READWRITE));
419
420   g_type_class_add_private (gobject_class, sizeof (GtkAssistantPrivate));
421 }
422
423 static gint
424 default_forward_function (gint current_page, gpointer data)
425 {
426   GtkAssistant *assistant;
427   GtkAssistantPrivate *priv;
428   GtkAssistantPage *page_info;
429   GList *page_node;
430
431   assistant = GTK_ASSISTANT (data);
432   priv = assistant->priv;
433
434   page_node = g_list_nth (priv->pages, ++current_page);
435
436   if (!page_node)
437     return -1;
438
439   page_info = (GtkAssistantPage *) page_node->data;
440
441   while (page_node && !gtk_widget_get_visible (page_info->page))
442     {
443       page_node = page_node->next;
444       current_page++;
445
446       if (page_node)
447         page_info = (GtkAssistantPage *) page_node->data;
448     }
449
450   return current_page;
451 }
452
453 static void
454 compute_last_button_state (GtkAssistant *assistant)
455 {
456   GtkAssistantPrivate *priv = assistant->priv;
457   GtkAssistantPage *page_info, *current_page_info;
458   gint count, page_num, n_pages;
459
460   count = 0;
461   page_num = gtk_assistant_get_current_page (assistant);
462   n_pages  = gtk_assistant_get_n_pages (assistant);
463   current_page_info = page_info = g_list_nth_data (priv->pages, page_num);
464
465   while (page_num >= 0 && page_num < n_pages &&
466          page_info->type == GTK_ASSISTANT_PAGE_CONTENT &&
467          (count == 0 || page_info->complete) &&
468          count < n_pages)
469     {
470       page_num = (priv->forward_function) (page_num, priv->forward_function_data);
471       page_info = g_list_nth_data (priv->pages, page_num);
472
473       count++;
474     }
475
476   /* make the last button visible if we can skip multiple
477    * pages and end on a confirmation or summary page
478    */
479   if (count > 1 && page_info &&
480       (page_info->type == GTK_ASSISTANT_PAGE_CONFIRM ||
481        page_info->type == GTK_ASSISTANT_PAGE_SUMMARY))
482     {
483       gtk_widget_show (priv->last);
484       gtk_widget_set_sensitive (priv->last,
485                                 current_page_info->complete);
486     }
487   else
488     gtk_widget_hide (priv->last);
489 }
490
491 static void
492 compute_progress_state (GtkAssistant *assistant)
493 {
494   GtkAssistantPrivate *priv = assistant->priv;
495   gint page_num, n_pages;
496
497   n_pages = gtk_assistant_get_n_pages (assistant);
498   page_num = gtk_assistant_get_current_page (assistant);
499
500   page_num = (priv->forward_function) (page_num, priv->forward_function_data);
501
502   if (page_num >= 0 && page_num < n_pages)
503     gtk_widget_show (priv->forward);
504   else
505     gtk_widget_hide (priv->forward);
506 }
507
508 static void
509 set_assistant_header_image (GtkAssistant *assistant)
510 {
511   GtkAssistantPrivate *priv = assistant->priv;
512
513   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->header_image),
514                              priv->current_page->header_image);
515 }
516
517 static void
518 set_assistant_sidebar_image (GtkAssistant *assistant)
519 {
520   GtkAssistantPrivate *priv = assistant->priv;
521
522   gtk_image_set_from_pixbuf (GTK_IMAGE (priv->sidebar_image),
523                              priv->current_page->sidebar_image);
524
525   if (priv->current_page->sidebar_image)
526     gtk_widget_show (priv->sidebar_image);
527   else
528     gtk_widget_hide (priv->sidebar_image);
529 }
530
531 static void
532 set_assistant_buttons_state (GtkAssistant *assistant)
533 {
534   GtkAssistantPrivate *priv = assistant->priv;
535
536   if (!priv->current_page)
537     return;
538   
539   switch (priv->current_page->type)
540     {
541     case GTK_ASSISTANT_PAGE_INTRO:
542       gtk_widget_set_sensitive (priv->cancel, TRUE);
543       gtk_widget_set_sensitive (priv->forward, priv->current_page->complete);
544       gtk_widget_grab_default (priv->forward);
545       gtk_widget_show (priv->forward);
546       gtk_widget_hide (priv->back);
547       gtk_widget_hide (priv->apply);
548       gtk_widget_hide (priv->close);
549       compute_last_button_state (assistant);
550       break;
551     case GTK_ASSISTANT_PAGE_CONFIRM:
552       gtk_widget_set_sensitive (priv->cancel, TRUE);
553       gtk_widget_set_sensitive (priv->back, TRUE);
554       gtk_widget_set_sensitive (priv->apply, priv->current_page->complete);
555       gtk_widget_grab_default (priv->apply);
556       gtk_widget_show (priv->back);
557       gtk_widget_show (priv->apply);
558       gtk_widget_hide (priv->forward);
559       gtk_widget_hide (priv->close);
560       gtk_widget_hide (priv->last);
561       break;
562     case GTK_ASSISTANT_PAGE_CONTENT:
563       gtk_widget_set_sensitive (priv->cancel, TRUE);
564       gtk_widget_set_sensitive (priv->back, TRUE);
565       gtk_widget_set_sensitive (priv->forward, priv->current_page->complete);
566       gtk_widget_grab_default (priv->forward);
567       gtk_widget_show (priv->back);
568       gtk_widget_show (priv->forward);
569       gtk_widget_hide (priv->apply);
570       gtk_widget_hide (priv->close);
571       compute_last_button_state (assistant);
572       break;
573     case GTK_ASSISTANT_PAGE_SUMMARY:
574       gtk_widget_set_sensitive (priv->close, priv->current_page->complete);
575       gtk_widget_grab_default (priv->close);
576       gtk_widget_show (priv->close);
577       gtk_widget_hide (priv->back);
578       gtk_widget_hide (priv->forward);
579       gtk_widget_hide (priv->apply);
580       gtk_widget_hide (priv->last);
581       break;
582     case GTK_ASSISTANT_PAGE_PROGRESS:
583       gtk_widget_set_sensitive (priv->cancel, priv->current_page->complete);
584       gtk_widget_set_sensitive (priv->back, priv->current_page->complete);
585       gtk_widget_set_sensitive (priv->forward, priv->current_page->complete);
586       gtk_widget_grab_default (priv->forward);
587       gtk_widget_show (priv->back);
588       gtk_widget_hide (priv->apply);
589       gtk_widget_hide (priv->close);
590       gtk_widget_hide (priv->last);
591       compute_progress_state (assistant);
592       break;
593     default:
594       g_assert_not_reached ();
595     }
596
597   if (priv->committed)
598     gtk_widget_hide (priv->cancel);
599   else if (priv->current_page->type == GTK_ASSISTANT_PAGE_SUMMARY)
600     gtk_widget_hide (priv->cancel);
601   else
602     gtk_widget_show (priv->cancel);
603
604   /* this is quite general, we don't want to
605    * go back if it's the first page */
606   if (!priv->visited_pages)
607     gtk_widget_hide (priv->back);
608 }
609
610 static void
611 set_current_page (GtkAssistant     *assistant,
612                   GtkAssistantPage *page)
613 {
614   GtkAssistantPrivate *priv = assistant->priv;
615   GtkAssistantPage *old_page;
616
617   if (priv->current_page &&
618       gtk_widget_is_drawable (priv->current_page->page))
619     old_page = priv->current_page;
620   else
621     old_page = NULL;
622
623   priv->current_page = page;
624
625   set_assistant_buttons_state (assistant);
626   set_assistant_header_image (assistant);
627   set_assistant_sidebar_image (assistant);
628
629   g_signal_emit (assistant, signals [PREPARE], 0, priv->current_page->page);
630
631   if (gtk_widget_get_visible (priv->current_page->page) && gtk_widget_get_mapped (GTK_WIDGET (assistant)))
632     {
633       gtk_widget_set_child_visible (priv->current_page->page, TRUE);
634       gtk_widget_map (priv->current_page->page);
635       gtk_widget_map (priv->current_page->title);
636     }
637   
638   if (old_page && gtk_widget_get_mapped (old_page->page))
639     {
640       gtk_widget_set_child_visible (old_page->page, FALSE);
641       gtk_widget_unmap (old_page->page);
642       gtk_widget_unmap (old_page->title);
643     }
644
645   if (!gtk_widget_child_focus (priv->current_page->page, GTK_DIR_TAB_FORWARD))
646     {
647       GtkWidget *button[6];
648       gint i;
649
650       /* find the best button to focus */
651       button[0] = priv->apply;
652       button[1] = priv->close;
653       button[2] = priv->forward;
654       button[3] = priv->back;
655       button[4] = priv->cancel;
656       button[5] = priv->last;
657       for (i = 0; i < 6; i++)
658         {
659           if (gtk_widget_get_visible (button[i]) && gtk_widget_get_sensitive (button[i]))
660             {
661               gtk_widget_grab_focus (button[i]);
662               break;
663             }
664         }
665     }
666
667   gtk_widget_queue_resize (GTK_WIDGET (assistant));
668 }
669
670 static gint
671 compute_next_step (GtkAssistant *assistant)
672 {
673   GtkAssistantPrivate *priv = assistant->priv;
674   GtkAssistantPage *page_info;
675   gint current_page, n_pages, next_page;
676
677   current_page = gtk_assistant_get_current_page (assistant);
678   page_info = priv->current_page;
679   n_pages = gtk_assistant_get_n_pages (assistant);
680
681   next_page = (priv->forward_function) (current_page,
682                                         priv->forward_function_data);
683
684   if (next_page >= 0 && next_page < n_pages)
685     {
686       priv->visited_pages = g_slist_prepend (priv->visited_pages, page_info);
687       set_current_page (assistant, g_list_nth_data (priv->pages, next_page));
688
689       return TRUE;
690     }
691
692   return FALSE;
693 }
694
695 static void
696 on_assistant_close (GtkWidget    *widget,
697                     GtkAssistant *assistant)
698 {
699   g_signal_emit (assistant, signals [CLOSE], 0, NULL);
700 }
701
702 static void
703 on_assistant_apply (GtkWidget    *widget,
704                     GtkAssistant *assistant)
705 {
706   gboolean success;
707
708   g_signal_emit (assistant, signals [APPLY], 0);
709
710   success = compute_next_step (assistant);
711
712   /* if the assistant hasn't switched to another page, just emit
713    * the CLOSE signal, it't the last page in the assistant flow
714    */
715   if (!success)
716     g_signal_emit (assistant, signals [CLOSE], 0);
717 }
718
719 static void
720 on_assistant_forward (GtkWidget    *widget,
721                       GtkAssistant *assistant)
722 {
723   if (!compute_next_step (assistant))
724     g_critical ("Page flow is broken, you may want to end it with a page of "
725                 "type GTK_ASSISTANT_PAGE_CONFIRM or GTK_ASSISTANT_PAGE_SUMMARY");
726 }
727
728 static void
729 on_assistant_back (GtkWidget    *widget,
730                    GtkAssistant *assistant)
731 {
732   GtkAssistantPrivate *priv = assistant->priv;
733   GtkAssistantPage *page_info;
734   GSList *page_node;
735
736   /* skip the progress pages when going back */
737   do
738     {
739       page_node = priv->visited_pages;
740
741       g_return_if_fail (page_node != NULL);
742
743       priv->visited_pages = priv->visited_pages->next;
744       page_info = (GtkAssistantPage *) page_node->data;
745       g_slist_free_1 (page_node);
746     }
747   while (page_info->type == GTK_ASSISTANT_PAGE_PROGRESS ||
748          !gtk_widget_get_visible (page_info->page));
749
750   set_current_page (assistant, page_info);
751 }
752
753 static void
754 on_assistant_cancel (GtkWidget    *widget,
755                      GtkAssistant *assistant)
756 {
757   g_signal_emit (assistant, signals [CANCEL], 0, NULL);
758 }
759
760 static void
761 on_assistant_last (GtkWidget    *widget,
762                    GtkAssistant *assistant)
763 {
764   GtkAssistantPrivate *priv = assistant->priv;
765
766   while (priv->current_page->type == GTK_ASSISTANT_PAGE_CONTENT &&
767          priv->current_page->complete)
768     compute_next_step (assistant);
769 }
770
771 static gboolean
772 alternative_button_order (GtkAssistant *assistant)
773 {
774   GtkSettings *settings;
775   GdkScreen *screen;
776   gboolean result;
777
778   screen   = gtk_widget_get_screen (GTK_WIDGET (assistant));
779   settings = gtk_settings_get_for_screen (screen);
780
781   g_object_get (settings,
782                 "gtk-alternative-button-order", &result,
783                 NULL);
784   return result;
785 }
786
787 static void
788 gtk_assistant_init (GtkAssistant *assistant)
789 {
790   GtkAssistantPrivate *priv;
791
792   assistant->priv = G_TYPE_INSTANCE_GET_PRIVATE (assistant,
793                                                  GTK_TYPE_ASSISTANT,
794                                                  GtkAssistantPrivate);
795   priv = assistant->priv;
796
797   gtk_container_set_reallocate_redraws (GTK_CONTAINER (assistant), TRUE);
798   gtk_container_set_border_width (GTK_CONTAINER (assistant), 12);
799
800   gtk_widget_push_composite_child ();
801
802   /* Header */
803   priv->header_image = gtk_image_new ();
804   gtk_misc_set_alignment (GTK_MISC (priv->header_image), 1., 0.5);
805   gtk_widget_set_parent (priv->header_image, GTK_WIDGET (assistant));
806   gtk_widget_show (priv->header_image);
807
808   /* Sidebar */
809   priv->sidebar_image = gtk_image_new ();
810   gtk_misc_set_alignment (GTK_MISC (priv->sidebar_image), 0., 0.);
811   gtk_widget_set_parent (priv->sidebar_image, GTK_WIDGET (assistant));
812   gtk_widget_show (priv->sidebar_image);
813
814   /* Action area  */
815   priv->action_area  = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 6);
816   
817   priv->close   = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
818   priv->apply   = gtk_button_new_from_stock (GTK_STOCK_APPLY);
819   priv->forward = gtk_button_new_from_stock (GTK_STOCK_GO_FORWARD);
820   priv->back    = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
821   priv->cancel  = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
822   priv->last    = gtk_button_new_from_stock (GTK_STOCK_GOTO_LAST);
823   gtk_widget_set_can_default (priv->close, TRUE);
824   gtk_widget_set_can_default (priv->apply, TRUE);
825   gtk_widget_set_can_default (priv->forward, TRUE);
826
827   priv->size_group   = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
828   gtk_size_group_add_widget (priv->size_group, priv->close);
829   gtk_size_group_add_widget (priv->size_group, priv->apply);
830   gtk_size_group_add_widget (priv->size_group, priv->forward);
831   gtk_size_group_add_widget (priv->size_group, priv->back);
832   gtk_size_group_add_widget (priv->size_group, priv->cancel);
833   gtk_size_group_add_widget (priv->size_group, priv->last);
834
835   if (!alternative_button_order (assistant))
836     {
837       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->apply, FALSE, FALSE, 0);
838       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->forward, FALSE, FALSE, 0);
839       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->back, FALSE, FALSE, 0);
840       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->last, FALSE, FALSE, 0);
841       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->cancel, FALSE, FALSE, 0);
842       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->close, FALSE, FALSE, 0);
843     }
844   else
845     {
846       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->close, FALSE, FALSE, 0);
847       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->cancel, FALSE, FALSE, 0);
848       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->apply, FALSE, FALSE, 0);
849       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->forward, FALSE, FALSE, 0);
850       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->back, FALSE, FALSE, 0);
851       gtk_box_pack_end (GTK_BOX (priv->action_area), priv->last, FALSE, FALSE, 0);
852     }
853
854   gtk_widget_set_parent (priv->action_area, GTK_WIDGET (assistant));
855   gtk_widget_show (priv->forward);
856   gtk_widget_show (priv->back);
857   gtk_widget_show (priv->cancel);
858   gtk_widget_show (priv->action_area);
859
860   gtk_widget_pop_composite_child ();
861
862   priv->pages = NULL;
863   priv->current_page = NULL;
864   priv->visited_pages = NULL;
865
866   priv->forward_function = default_forward_function;
867   priv->forward_function_data = assistant;
868   priv->forward_data_destroy = NULL;
869
870   g_signal_connect (G_OBJECT (priv->close), "clicked",
871                     G_CALLBACK (on_assistant_close), assistant);
872   g_signal_connect (G_OBJECT (priv->apply), "clicked",
873                     G_CALLBACK (on_assistant_apply), assistant);
874   g_signal_connect (G_OBJECT (priv->forward), "clicked",
875                     G_CALLBACK (on_assistant_forward), assistant);
876   g_signal_connect (G_OBJECT (priv->back), "clicked",
877                     G_CALLBACK (on_assistant_back), assistant);
878   g_signal_connect (G_OBJECT (priv->cancel), "clicked",
879                     G_CALLBACK (on_assistant_cancel), assistant);
880   g_signal_connect (G_OBJECT (priv->last), "clicked",
881                     G_CALLBACK (on_assistant_last), assistant);
882 }
883
884 static void
885 gtk_assistant_set_child_property (GtkContainer    *container,
886                                   GtkWidget       *child,
887                                   guint            property_id,
888                                   const GValue    *value,
889                                   GParamSpec      *pspec)
890 {
891   switch (property_id)
892     {
893     case CHILD_PROP_PAGE_TYPE:
894       gtk_assistant_set_page_type (GTK_ASSISTANT (container), child,
895                                    g_value_get_enum (value));
896       break;
897     case CHILD_PROP_PAGE_TITLE:
898       gtk_assistant_set_page_title (GTK_ASSISTANT (container), child,
899                                     g_value_get_string (value));
900       break;
901     case CHILD_PROP_PAGE_HEADER_IMAGE:
902       gtk_assistant_set_page_header_image (GTK_ASSISTANT (container), child,
903                                            g_value_get_object (value));
904       break;
905     case CHILD_PROP_PAGE_SIDEBAR_IMAGE:
906       gtk_assistant_set_page_side_image (GTK_ASSISTANT (container), child,
907                                          g_value_get_object (value));
908       break;
909     case CHILD_PROP_PAGE_COMPLETE:
910       gtk_assistant_set_page_complete (GTK_ASSISTANT (container), child,
911                                        g_value_get_boolean (value));
912       break;
913     default:
914       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
915       break;
916     }
917 }
918
919 static void
920 gtk_assistant_get_child_property (GtkContainer *container,
921                                   GtkWidget    *child,
922                                   guint         property_id,
923                                   GValue       *value,
924                                   GParamSpec   *pspec)
925 {
926   switch (property_id)
927     {
928     case CHILD_PROP_PAGE_TYPE:
929       g_value_set_enum (value,
930                         gtk_assistant_get_page_type (GTK_ASSISTANT (container), child));
931       break;
932     case CHILD_PROP_PAGE_TITLE:
933       g_value_set_string (value,
934                           gtk_assistant_get_page_title (GTK_ASSISTANT (container), child));
935       break;
936     case CHILD_PROP_PAGE_HEADER_IMAGE:
937       g_value_set_object (value,
938                           gtk_assistant_get_page_header_image (GTK_ASSISTANT (container), child));
939       break;
940     case CHILD_PROP_PAGE_SIDEBAR_IMAGE:
941       g_value_set_object (value,
942                           gtk_assistant_get_page_side_image (GTK_ASSISTANT (container), child));
943       break;
944     case CHILD_PROP_PAGE_COMPLETE:
945       g_value_set_boolean (value,
946                            gtk_assistant_get_page_complete (GTK_ASSISTANT (container), child));
947       break;
948     default:
949       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
950       break;
951     }
952 }
953
954 static void
955 on_page_notify_visibility (GtkWidget  *widget,
956                            GParamSpec *arg,
957                            gpointer    data)
958 {
959   GtkAssistant *assistant = GTK_ASSISTANT (data);
960
961   /* update buttons state, flow may have changed */
962   if (gtk_widget_get_mapped (GTK_WIDGET (assistant)))
963     set_assistant_buttons_state (assistant);
964 }
965
966 static void
967 remove_page (GtkAssistant *assistant, 
968              GList        *element)
969 {
970   GtkAssistantPrivate *priv = assistant->priv;
971   GtkAssistantPage *page_info;
972   GList *page_node;
973
974   page_info = element->data;
975
976   /* If this is the current page, we need to switch away. */
977   if (page_info == priv->current_page)
978     {
979       if (!compute_next_step (assistant))
980         {
981           /* The best we can do at this point is probably to pick the first
982            * visible page.
983            */
984           page_node = priv->pages;
985
986           while (page_node && !gtk_widget_get_visible (((GtkAssistantPage *) page_node->data)->page))
987             page_node = page_node->next;
988
989           if (page_node == element)
990             page_node = page_node->next;
991
992           if (page_node)
993             priv->current_page = page_node->data;
994           else
995             priv->current_page = NULL;
996         }
997     }
998
999   priv->pages = g_list_remove_link (priv->pages, element);
1000   priv->visited_pages = g_slist_remove_all (priv->visited_pages, page_info);
1001
1002   g_signal_handlers_disconnect_by_func (page_info->page, on_page_notify_visibility, assistant);
1003   gtk_widget_unparent (page_info->page);
1004
1005   if (page_info->header_image)
1006     g_object_unref (page_info->header_image);
1007
1008   if (page_info->sidebar_image)
1009     g_object_unref (page_info->sidebar_image);
1010
1011   gtk_widget_destroy (page_info->title);
1012   g_slice_free (GtkAssistantPage, page_info);
1013   g_list_free_1 (element);
1014 }
1015
1016 static void
1017 gtk_assistant_destroy (GtkWidget *widget)
1018 {
1019   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1020   GtkAssistantPrivate *priv = assistant->priv;
1021
1022   if (priv->header_image)
1023     {
1024       gtk_widget_destroy (priv->header_image);
1025       priv->header_image = NULL;
1026     }
1027
1028   if (priv->sidebar_image)
1029     {
1030       gtk_widget_destroy (priv->sidebar_image);
1031       priv->sidebar_image = NULL;
1032     }
1033
1034   if (priv->action_area)
1035     {
1036       gtk_widget_destroy (priv->action_area);
1037       priv->action_area = NULL;
1038     }
1039
1040   if (priv->size_group)
1041     {
1042       g_object_unref (priv->size_group);
1043       priv->size_group = NULL;
1044     }
1045
1046   if (priv->forward_function)
1047     {
1048       if (priv->forward_function_data &&
1049           priv->forward_data_destroy)
1050         priv->forward_data_destroy (priv->forward_function_data);
1051
1052       priv->forward_function = NULL;
1053       priv->forward_function_data = NULL;
1054       priv->forward_data_destroy = NULL;
1055     }
1056
1057   if (priv->visited_pages)
1058     {
1059       g_slist_free (priv->visited_pages);
1060       priv->visited_pages = NULL;
1061     }
1062
1063   /* We set current to NULL so that the remove code doesn't try
1064    * to do anything funny */
1065   priv->current_page = NULL;
1066
1067   while (priv->pages)
1068     remove_page (assistant, priv->pages);
1069
1070   GTK_WIDGET_CLASS (gtk_assistant_parent_class)->destroy (widget);
1071 }
1072
1073 static GList*
1074 find_page (GtkAssistant  *assistant,
1075            GtkWidget     *page)
1076 {
1077   GtkAssistantPrivate *priv = assistant->priv;
1078   GList *child = priv->pages;
1079   
1080   while (child)
1081     {
1082       GtkAssistantPage *page_info = child->data;
1083       if (page_info->page == page)
1084         return child;
1085
1086       child = child->next;
1087     }
1088   
1089   return NULL;
1090 }
1091
1092 static void
1093 set_title_colors (GtkWidget *assistant,
1094                   GtkWidget *title_label)
1095 {
1096   GtkStyle *style;
1097
1098   gtk_widget_ensure_style (assistant);
1099   style = gtk_widget_get_style (assistant);
1100
1101   /* change colors schema, for making the header text visible */
1102   gtk_widget_modify_bg (title_label, GTK_STATE_NORMAL, &style->bg[GTK_STATE_SELECTED]);
1103   gtk_widget_modify_fg (title_label, GTK_STATE_NORMAL, &style->fg[GTK_STATE_SELECTED]);
1104 }
1105
1106 static void
1107 set_title_font (GtkWidget *assistant,
1108                 GtkWidget *title_label)
1109 {
1110   PangoFontDescription *desc;
1111   gint size;
1112
1113   desc = pango_font_description_new ();
1114   size = pango_font_description_get_size (gtk_widget_get_style (assistant)->font_desc);
1115
1116   pango_font_description_set_weight (desc, PANGO_WEIGHT_ULTRABOLD);
1117   pango_font_description_set_size   (desc, size * PANGO_SCALE_XX_LARGE);
1118
1119   gtk_widget_modify_font (title_label, desc);
1120   pango_font_description_free (desc);
1121 }
1122
1123 static void
1124 gtk_assistant_style_set (GtkWidget *widget,
1125                          GtkStyle  *old_style)
1126 {
1127   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1128   GtkAssistantPrivate *priv = assistant->priv;
1129   GList *list;
1130
1131   list = priv->pages;
1132
1133   while (list)
1134     {
1135       GtkAssistantPage *page = list->data;
1136
1137       set_title_colors (widget, page->title);
1138       set_title_font (widget, page->title);
1139
1140       list = list->next;
1141     }
1142 }
1143
1144 static void
1145 gtk_assistant_size_request (GtkWidget      *widget,
1146                             GtkRequisition *requisition)
1147 {
1148   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1149   GtkAssistantPrivate *priv = assistant->priv;
1150   GtkRequisition child_requisition;
1151   gint header_padding, content_padding;
1152   gint width, height, header_width, header_height;
1153   guint border_width;
1154   GList *list;
1155
1156   gtk_widget_style_get (widget,
1157                         "header-padding", &header_padding,
1158                         "content-padding", &content_padding,
1159                         NULL);
1160   width = height = 0;
1161   header_width = header_height = 0;
1162   list  = priv->pages;
1163
1164   while (list)
1165     {
1166       GtkAssistantPage *page = list->data;
1167       gint w, h;
1168
1169       gtk_widget_get_preferred_size (page->page,
1170                                      &child_requisition, NULL);
1171       width  = MAX (width,  child_requisition.width);
1172       height = MAX (height, child_requisition.height);
1173
1174       gtk_widget_get_preferred_size (page->title,
1175                                      &child_requisition, NULL);
1176       w = child_requisition.width;
1177       h = child_requisition.height;
1178
1179       if (page->header_image)
1180         {
1181           w += gdk_pixbuf_get_width (page->header_image) + HEADER_SPACING;
1182           h  = MAX (h, gdk_pixbuf_get_height (page->header_image));
1183         }
1184
1185       header_width  = MAX (header_width, w);
1186       header_height = MAX (header_height, h);
1187
1188       list = list->next;
1189     }
1190
1191   gtk_widget_get_preferred_size (priv->sidebar_image,
1192                                  &child_requisition, NULL);
1193   width  += child_requisition.width;
1194   height  = MAX (height, child_requisition.height);
1195
1196   gtk_widget_set_size_request (priv->header_image, header_width, header_height);
1197   gtk_widget_get_preferred_size (priv->header_image,
1198                                  &child_requisition, NULL);
1199   width   = MAX (width, header_width) + 2 * header_padding;
1200   height += header_height + 2 * header_padding;
1201
1202   gtk_widget_get_preferred_size (priv->action_area,
1203                                  &child_requisition, NULL);
1204   width   = MAX (width, child_requisition.width);
1205   height += child_requisition.height + ACTION_AREA_SPACING;
1206
1207   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1208   width += border_width * 2 + content_padding * 2;
1209   height += border_width * 2 + content_padding * 2;
1210
1211   requisition->width = width;
1212   requisition->height = height;
1213 }
1214
1215 static void
1216 gtk_assistant_get_preferred_width (GtkWidget *widget,
1217                                    gint      *minimum,
1218                                    gint      *natural)
1219 {
1220   GtkRequisition requisition;
1221
1222   gtk_assistant_size_request (widget, &requisition);
1223
1224   *minimum = *natural = requisition.width;
1225 }
1226
1227 static void
1228 gtk_assistant_get_preferred_height (GtkWidget *widget,
1229                                     gint      *minimum,
1230                                     gint      *natural)
1231 {
1232   GtkRequisition requisition;
1233
1234   gtk_assistant_size_request (widget, &requisition);
1235
1236   *minimum = *natural = requisition.height;
1237 }
1238
1239 static void
1240 gtk_assistant_size_allocate (GtkWidget      *widget,
1241                              GtkAllocation  *allocation)
1242 {
1243   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1244   GtkAssistantPrivate *priv = assistant->priv;
1245   GtkRequisition header_requisition, action_requisition, sidebar_requisition;
1246   GtkAllocation child_allocation, header_allocation;
1247   GtkAllocation action_area_allocation, header_image_allocation;
1248   gint header_padding, content_padding;
1249   guint border_width;
1250   gboolean rtl;
1251   GList *pages;
1252
1253   rtl   = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1254   pages = priv->pages;
1255
1256   gtk_widget_style_get (widget,
1257                         "header-padding", &header_padding,
1258                         "content-padding", &content_padding,
1259                         NULL);
1260
1261   gtk_widget_set_allocation (widget, allocation);
1262   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1263
1264   /* Header */
1265   gtk_widget_get_preferred_size (priv->header_image,
1266                                  &header_requisition, NULL);
1267
1268   header_allocation.x = border_width + header_padding;
1269   header_allocation.y = border_width + header_padding;
1270   header_allocation.width  = allocation->width - 2 * border_width - 2 * header_padding;
1271   header_allocation.height = header_requisition.height;
1272
1273   gtk_widget_size_allocate (priv->header_image, &header_allocation);
1274
1275   /* Action area */
1276   gtk_widget_get_preferred_size (priv->action_area,
1277                                  &action_requisition, NULL);
1278
1279   child_allocation.x = border_width;
1280   child_allocation.y = allocation->height - border_width - action_requisition.height;
1281   child_allocation.width  = allocation->width - 2 * border_width;
1282   child_allocation.height = action_requisition.height;
1283
1284   gtk_widget_size_allocate (priv->action_area, &child_allocation);
1285
1286   gtk_widget_get_allocation (priv->header_image, &header_image_allocation);
1287   gtk_widget_get_allocation (priv->action_area, &action_area_allocation);
1288
1289   /* Sidebar */
1290   gtk_widget_get_preferred_size (priv->sidebar_image,
1291                                  &sidebar_requisition, NULL);
1292
1293   if (rtl)
1294     child_allocation.x = allocation->width - border_width - sidebar_requisition.width;
1295   else
1296     child_allocation.x = border_width;
1297
1298   child_allocation.y = border_width + header_image_allocation.height + 2 * header_padding;
1299   child_allocation.width = sidebar_requisition.width;
1300   child_allocation.height = allocation->height - 2 * border_width -
1301     header_image_allocation.height - 2 * header_padding - action_area_allocation.height;
1302
1303   gtk_widget_size_allocate (priv->sidebar_image, &child_allocation);
1304
1305   /* Pages */
1306   child_allocation.x = border_width + content_padding;
1307   child_allocation.y = border_width +
1308     header_image_allocation.height + 2 * header_padding + content_padding;
1309   child_allocation.width  = allocation->width - 2 * border_width - 2 * content_padding;
1310   child_allocation.height = allocation->height - 2 * border_width -
1311     header_image_allocation.height - 2 * header_padding - ACTION_AREA_SPACING - action_area_allocation.height - 2 * content_padding;
1312
1313   if (gtk_widget_get_visible (priv->sidebar_image))
1314     {
1315       GtkAllocation sidebar_image_allocation;
1316
1317       gtk_widget_get_allocation (priv->sidebar_image, &sidebar_image_allocation);
1318
1319       if (!rtl)
1320         child_allocation.x += sidebar_image_allocation.width;
1321
1322       child_allocation.width -= sidebar_image_allocation.width;
1323     }
1324
1325   while (pages)
1326     {
1327       GtkAssistantPage *page = pages->data;
1328
1329       gtk_widget_size_allocate (page->page, &child_allocation);
1330       gtk_widget_size_allocate (page->title, &header_allocation);
1331       pages = pages->next;
1332     }
1333 }
1334
1335 static void
1336 gtk_assistant_map (GtkWidget *widget)
1337 {
1338   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1339   GtkAssistantPrivate *priv = assistant->priv;
1340   GList *page_node;
1341   GtkAssistantPage *page;
1342
1343   gtk_widget_set_mapped (widget, TRUE);
1344
1345   gtk_widget_map (priv->header_image);
1346   gtk_widget_map (priv->action_area);
1347
1348   if (gtk_widget_get_visible (priv->sidebar_image) &&
1349       !gtk_widget_get_mapped (priv->sidebar_image))
1350     gtk_widget_map (priv->sidebar_image);
1351
1352   /* if there's no default page, pick the first one */
1353   page = NULL;
1354   if (!priv->current_page)
1355     {
1356       page_node = priv->pages;
1357
1358       while (page_node && !gtk_widget_get_visible (((GtkAssistantPage *) page_node->data)->page))
1359         page_node = page_node->next;
1360
1361       if (page_node)
1362         page = page_node->data;
1363     }
1364
1365   if (page &&
1366       gtk_widget_get_visible (page->page) &&
1367       !gtk_widget_get_mapped (page->page))
1368     set_current_page (assistant, page);
1369
1370   GTK_WIDGET_CLASS (gtk_assistant_parent_class)->map (widget);
1371 }
1372
1373 static void
1374 gtk_assistant_unmap (GtkWidget *widget)
1375 {
1376   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1377   GtkAssistantPrivate *priv = assistant->priv;
1378
1379   gtk_widget_set_mapped (widget, FALSE);
1380
1381   gtk_widget_unmap (priv->header_image);
1382   gtk_widget_unmap (priv->action_area);
1383
1384   if (gtk_widget_is_drawable (priv->sidebar_image))
1385     gtk_widget_unmap (priv->sidebar_image);
1386
1387   if (priv->current_page &&
1388       gtk_widget_is_drawable (priv->current_page->page))
1389     gtk_widget_unmap (priv->current_page->page);
1390
1391   g_slist_free (priv->visited_pages);
1392   priv->visited_pages = NULL;
1393   priv->current_page  = NULL;
1394
1395   GTK_WIDGET_CLASS (gtk_assistant_parent_class)->unmap (widget);
1396 }
1397
1398 static gboolean
1399 gtk_assistant_delete_event (GtkWidget   *widget,
1400                             GdkEventAny *event)
1401 {
1402   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1403   GtkAssistantPrivate *priv = assistant->priv;
1404
1405   /* Do not allow cancelling in the middle of a progress page */
1406   if (priv->current_page &&
1407       (priv->current_page->type != GTK_ASSISTANT_PAGE_PROGRESS ||
1408        priv->current_page->complete))
1409     g_signal_emit (widget, signals [CANCEL], 0, NULL);
1410
1411   return TRUE;
1412 }
1413
1414 static void
1415 assistant_paint_colored_box (GtkWidget *widget,
1416                              cairo_t   *cr)
1417 {
1418   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1419   GtkAssistantPrivate *priv = assistant->priv;
1420   GtkAllocation allocation, action_area_allocation, header_image_allocation;
1421   GtkStyle *style;
1422   gint border_width, header_padding, content_padding;
1423   gint content_x, content_width;
1424   gboolean rtl;
1425
1426   rtl  = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1427   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1428
1429   gtk_widget_style_get (widget,
1430                         "header-padding",  &header_padding,
1431                         "content-padding", &content_padding,
1432                         NULL);
1433
1434   style = gtk_widget_get_style (widget);
1435   gtk_widget_get_allocation (widget, &allocation);
1436   gtk_widget_get_allocation (priv->action_area, &action_area_allocation);
1437   gtk_widget_get_allocation (priv->header_image, &header_image_allocation);
1438
1439   /* colored box */
1440   gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_SELECTED]);
1441   cairo_rectangle (cr,
1442                    border_width,
1443                    border_width,
1444                    allocation.width - 2 * border_width,
1445                    allocation.height - action_area_allocation.height - 2 * border_width - ACTION_AREA_SPACING);
1446   cairo_fill (cr);
1447
1448   /* content box */
1449   content_x = content_padding + border_width;
1450   content_width = allocation.width - 2 * content_padding - 2 * border_width;
1451
1452   if (gtk_widget_get_visible (priv->sidebar_image))
1453     {
1454       GtkAllocation sidebar_image_allocation;
1455
1456       gtk_widget_get_allocation (priv->sidebar_image, &sidebar_image_allocation);
1457
1458       if (!rtl)
1459         content_x += sidebar_image_allocation.width;
1460       content_width -= sidebar_image_allocation.width;
1461     }
1462
1463   gdk_cairo_set_source_color (cr, &style->bg[GTK_STATE_NORMAL]);
1464
1465   cairo_rectangle (cr,
1466                    content_x,
1467                    header_image_allocation.height + content_padding + 2 * header_padding + border_width,
1468                    content_width,
1469                    allocation.height - 2 * border_width - action_area_allocation.height -
1470                    header_image_allocation.height - 2 * content_padding - 2 * header_padding - ACTION_AREA_SPACING);
1471   cairo_fill (cr);
1472 }
1473
1474 static gboolean
1475 gtk_assistant_draw (GtkWidget *widget,
1476                     cairo_t   *cr)
1477 {
1478   GtkAssistant *assistant = GTK_ASSISTANT (widget);
1479   GtkAssistantPrivate *priv = assistant->priv;
1480   GtkContainer *container = GTK_CONTAINER (widget);
1481
1482   if (GTK_WIDGET_CLASS (gtk_assistant_parent_class)->draw)
1483     GTK_WIDGET_CLASS (gtk_assistant_parent_class)->draw (widget, cr);
1484
1485   assistant_paint_colored_box (widget, cr);
1486
1487   gtk_container_propagate_draw (container, priv->header_image, cr);
1488   gtk_container_propagate_draw (container, priv->sidebar_image, cr);
1489   gtk_container_propagate_draw (container, priv->action_area, cr);
1490
1491   if (priv->current_page)
1492     {
1493       gtk_container_propagate_draw (container, priv->current_page->page, cr);
1494       gtk_container_propagate_draw (container, priv->current_page->title, cr);
1495     }
1496
1497   return FALSE;
1498 }
1499
1500 static gboolean
1501 gtk_assistant_focus (GtkWidget        *widget,
1502                      GtkDirectionType  direction)
1503 {
1504   GtkAssistantPrivate *priv;
1505   GtkContainer *container;
1506
1507   container = GTK_CONTAINER (widget);
1508   priv = GTK_ASSISTANT (widget)->priv;
1509
1510   /* we only have to care about 2 widgets, action area and the current page */
1511   if (gtk_container_get_focus_child (container) == priv->action_area)
1512     {
1513       if (!gtk_widget_child_focus (priv->action_area, direction) &&
1514           (priv->current_page == NULL ||
1515            !gtk_widget_child_focus (priv->current_page->page, direction)))
1516         {
1517           /* if we're leaving the action area and the current page hasn't
1518              any focusable widget, clear focus and go back to the action area */
1519           gtk_container_set_focus_child (GTK_CONTAINER (priv->action_area), NULL);
1520           gtk_widget_child_focus (priv->action_area, direction);
1521         }
1522     }
1523   else
1524     {
1525       if ((priv->current_page ==  NULL ||
1526            !gtk_widget_child_focus (priv->current_page->page, direction)) &&
1527           !gtk_widget_child_focus (priv->action_area, direction))
1528         {
1529           /* if we're leaving the current page and there isn't nothing focusable
1530              in the action area, try to clear focus and go back to the page */
1531           gtk_window_set_focus (GTK_WINDOW (widget), NULL);
1532           if (priv->current_page != NULL)
1533             gtk_widget_child_focus (priv->current_page->page, direction);
1534         }
1535     }
1536
1537   return TRUE;
1538 }
1539
1540 static void
1541 gtk_assistant_add (GtkContainer *container,
1542                    GtkWidget    *page)
1543 {
1544   gtk_assistant_append_page (GTK_ASSISTANT (container), page);
1545 }
1546
1547 static void
1548 gtk_assistant_remove (GtkContainer *container,
1549                       GtkWidget    *page)
1550 {
1551   GtkAssistant *assistant = (GtkAssistant*) container;
1552   GList *element;
1553
1554   element = find_page (assistant, page);
1555
1556   if (element)
1557     {
1558       remove_page (assistant, element);
1559       gtk_widget_queue_resize ((GtkWidget *) container);
1560     }
1561 }
1562
1563 static void
1564 gtk_assistant_forall (GtkContainer *container,
1565                       gboolean      include_internals,
1566                       GtkCallback   callback,
1567                       gpointer      callback_data)
1568 {
1569   GtkAssistant *assistant = (GtkAssistant*) container;
1570   GtkAssistantPrivate *priv = assistant->priv;
1571   GList *pages;
1572
1573   if (include_internals)
1574     {
1575       (*callback) (priv->header_image, callback_data);
1576       (*callback) (priv->sidebar_image, callback_data);
1577       (*callback) (priv->action_area, callback_data);
1578     }
1579
1580   pages = priv->pages;
1581
1582   while (pages)
1583     {
1584       GtkAssistantPage *page = (GtkAssistantPage *) pages->data;
1585
1586       (*callback) (page->page, callback_data);
1587
1588       if (include_internals)
1589         (*callback) (page->title, callback_data);
1590
1591       pages = pages->next;
1592     }
1593 }
1594
1595 /**
1596  * gtk_assistant_new:
1597  * 
1598  * Creates a new #GtkAssistant.
1599  *
1600  * Return value: a newly created #GtkAssistant
1601  *
1602  * Since: 2.10
1603  **/
1604 GtkWidget*
1605 gtk_assistant_new (void)
1606 {
1607   GtkWidget *assistant;
1608
1609   assistant = g_object_new (GTK_TYPE_ASSISTANT, NULL);
1610
1611   return assistant;
1612 }
1613
1614 /**
1615  * gtk_assistant_get_current_page:
1616  * @assistant: a #GtkAssistant
1617  *
1618  * Returns the page number of the current page
1619  *
1620  * Return value: The index (starting from 0) of the current page in
1621  * the @assistant, if the @assistant has no pages, -1 will be returned
1622  *
1623  * Since: 2.10
1624  **/
1625 gint
1626 gtk_assistant_get_current_page (GtkAssistant *assistant)
1627 {
1628   GtkAssistantPrivate *priv;
1629
1630   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), -1);
1631
1632   priv = assistant->priv;
1633
1634   if (!priv->pages || !priv->current_page)
1635     return -1;
1636
1637   return g_list_index (priv->pages, priv->current_page);
1638 }
1639
1640 /**
1641  * gtk_assistant_set_current_page:
1642  * @assistant: a #GtkAssistant
1643  * @page_num: index of the page to switch to, starting from 0.
1644  *            If negative, the last page will be used. If greater
1645  *            than the number of pages in the @assistant, nothing
1646  *            will be done.
1647  *
1648  * Switches the page to @page_num. Note that this will only be necessary
1649  * in custom buttons, as the @assistant flow can be set with
1650  * gtk_assistant_set_forward_page_func().
1651  *
1652  * Since: 2.10
1653  **/
1654 void
1655 gtk_assistant_set_current_page (GtkAssistant *assistant,
1656                                 gint          page_num)
1657 {
1658   GtkAssistantPrivate *priv;
1659   GtkAssistantPage *page;
1660
1661   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
1662
1663   priv = assistant->priv;
1664
1665   if (page_num >= 0)
1666     page = (GtkAssistantPage *) g_list_nth_data (priv->pages, page_num);
1667   else
1668     page = (GtkAssistantPage *) g_list_last (priv->pages)->data;
1669
1670   g_return_if_fail (page != NULL);
1671
1672   if (priv->current_page == page)
1673     return;
1674
1675   /* only add the page to the visited list if the
1676    * assistant is mapped, if not, just use it as an
1677    * initial page setting, for the cases where the
1678    * initial page is != to 0
1679    */
1680   if (gtk_widget_get_mapped (GTK_WIDGET (assistant)))
1681     priv->visited_pages = g_slist_prepend (priv->visited_pages,
1682                                            priv->current_page);
1683
1684   set_current_page (assistant, page);
1685 }
1686
1687 /**
1688  * gtk_assistant_get_n_pages:
1689  * @assistant: a #GtkAssistant
1690  *
1691  * Returns the number of pages in the @assistant
1692  *
1693  * Return value: The number of pages in the @assistant.
1694  *
1695  * Since: 2.10
1696  **/
1697 gint
1698 gtk_assistant_get_n_pages (GtkAssistant *assistant)
1699 {
1700   GtkAssistantPrivate *priv;
1701
1702   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), 0);
1703
1704   priv = assistant->priv;
1705
1706   return g_list_length (priv->pages);
1707 }
1708
1709 /**
1710  * gtk_assistant_get_nth_page:
1711  * @assistant: a #GtkAssistant
1712  * @page_num: The index of a page in the @assistant, or -1 to get the last page;
1713  *
1714  * Returns the child widget contained in page number @page_num.
1715  *
1716  * Return value: (transfer none): The child widget, or %NULL
1717  *     if @page_num is out of bounds.
1718  *
1719  * Since: 2.10
1720  **/
1721 GtkWidget*
1722 gtk_assistant_get_nth_page (GtkAssistant *assistant,
1723                             gint          page_num)
1724 {
1725   GtkAssistantPrivate *priv;
1726   GtkAssistantPage *page;
1727   GList *elem;
1728
1729   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), NULL);
1730   g_return_val_if_fail (page_num >= -1, NULL);
1731
1732   priv = assistant->priv;
1733
1734   if (page_num == -1)
1735     elem = g_list_last (priv->pages);
1736   else
1737     elem = g_list_nth (priv->pages, page_num);
1738
1739   if (!elem)
1740     return NULL;
1741
1742   page = (GtkAssistantPage *) elem->data;
1743
1744   return page->page;
1745 }
1746
1747 /**
1748  * gtk_assistant_prepend_page:
1749  * @assistant: a #GtkAssistant
1750  * @page: a #GtkWidget
1751  *
1752  * Prepends a page to the @assistant.
1753  *
1754  * Return value: the index (starting at 0) of the inserted page
1755  *
1756  * Since: 2.10
1757  **/
1758 gint
1759 gtk_assistant_prepend_page (GtkAssistant *assistant,
1760                             GtkWidget    *page)
1761 {
1762   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), 0);
1763   g_return_val_if_fail (GTK_IS_WIDGET (page), 0);
1764
1765   return gtk_assistant_insert_page (assistant, page, 0);
1766 }
1767
1768 /**
1769  * gtk_assistant_append_page:
1770  * @assistant: a #GtkAssistant
1771  * @page: a #GtkWidget
1772  *
1773  * Appends a page to the @assistant.
1774  *
1775  * Return value: the index (starting at 0) of the inserted page
1776  *
1777  * Since: 2.10
1778  **/
1779 gint
1780 gtk_assistant_append_page (GtkAssistant *assistant,
1781                            GtkWidget    *page)
1782 {
1783   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), 0);
1784   g_return_val_if_fail (GTK_IS_WIDGET (page), 0);
1785
1786   return gtk_assistant_insert_page (assistant, page, -1);
1787 }
1788
1789 /**
1790  * gtk_assistant_insert_page:
1791  * @assistant: a #GtkAssistant
1792  * @page: a #GtkWidget
1793  * @position: the index (starting at 0) at which to insert the page,
1794  *            or -1 to append the page to the @assistant
1795  *
1796  * Inserts a page in the @assistant at a given position.
1797  *
1798  * Return value: the index (starting from 0) of the inserted page
1799  *
1800  * Since: 2.10
1801  **/
1802 gint
1803 gtk_assistant_insert_page (GtkAssistant *assistant,
1804                            GtkWidget    *page,
1805                            gint          position)
1806 {
1807   GtkAssistantPrivate *priv;
1808   GtkAssistantPage *page_info;
1809   gint n_pages;
1810
1811   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), 0);
1812   g_return_val_if_fail (GTK_IS_WIDGET (page), 0);
1813   g_return_val_if_fail (gtk_widget_get_parent (page) == NULL, 0);
1814   g_return_val_if_fail (!gtk_widget_is_toplevel (page), 0);
1815
1816   priv = assistant->priv;
1817
1818   page_info = g_slice_new0 (GtkAssistantPage);
1819   page_info->page  = page;
1820   page_info->title = gtk_label_new (NULL);
1821
1822   g_signal_connect (G_OBJECT (page), "notify::visible",
1823                     G_CALLBACK (on_page_notify_visibility), assistant);
1824
1825   gtk_misc_set_alignment (GTK_MISC (page_info->title), 0.,0.5);
1826   set_title_colors (GTK_WIDGET (assistant), page_info->title);
1827   set_title_font   (GTK_WIDGET (assistant), page_info->title);
1828   gtk_widget_show  (page_info->title);
1829
1830   n_pages = g_list_length (priv->pages);
1831
1832   if (position < 0 || position > n_pages)
1833     position = n_pages;
1834
1835   priv->pages = g_list_insert (priv->pages, page_info, position);
1836
1837   gtk_widget_set_child_visible (page_info->page, FALSE);
1838   gtk_widget_set_parent (page_info->page,  GTK_WIDGET (assistant));
1839   gtk_widget_set_parent (page_info->title, GTK_WIDGET (assistant));
1840
1841   if (gtk_widget_get_realized (GTK_WIDGET (assistant)))
1842     {
1843       gtk_widget_realize (page_info->page);
1844       gtk_widget_realize (page_info->title);
1845     }
1846
1847   gtk_widget_queue_resize (GTK_WIDGET (assistant));
1848
1849   return position;
1850 }
1851
1852 /**
1853  * gtk_assistant_set_forward_page_func:
1854  * @assistant: a #GtkAssistant
1855  * @page_func: (allow-none): the #GtkAssistantPageFunc, or %NULL to use the default one
1856  * @data: user data for @page_func
1857  * @destroy: destroy notifier for @data
1858  *
1859  * Sets the page forwarding function to be @page_func, this function will
1860  * be used to determine what will be the next page when the user presses
1861  * the forward button. Setting @page_func to %NULL will make the assistant
1862  * to use the default forward function, which just goes to the next visible 
1863  * page.
1864  *
1865  * Since: 2.10
1866  **/
1867 void
1868 gtk_assistant_set_forward_page_func (GtkAssistant         *assistant,
1869                                      GtkAssistantPageFunc  page_func,
1870                                      gpointer              data,
1871                                      GDestroyNotify        destroy)
1872 {
1873   GtkAssistantPrivate *priv;
1874
1875   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
1876
1877   priv = assistant->priv;
1878
1879   if (priv->forward_data_destroy &&
1880       priv->forward_function_data)
1881     (*priv->forward_data_destroy) (priv->forward_function_data);
1882
1883   if (page_func)
1884     {
1885       priv->forward_function = page_func;
1886       priv->forward_function_data = data;
1887       priv->forward_data_destroy = destroy;
1888     }
1889   else
1890     {
1891       priv->forward_function = default_forward_function;
1892       priv->forward_function_data = assistant;
1893       priv->forward_data_destroy = NULL;
1894     }
1895
1896   /* Page flow has possibly changed, so the
1897      buttons state might need to change too */
1898   set_assistant_buttons_state (assistant);
1899 }
1900
1901 /**
1902  * gtk_assistant_add_action_widget:
1903  * @assistant: a #GtkAssistant
1904  * @child: a #GtkWidget
1905  * 
1906  * Adds a widget to the action area of a #GtkAssistant.
1907  *
1908  * Since: 2.10
1909  **/
1910 void
1911 gtk_assistant_add_action_widget (GtkAssistant *assistant,
1912                                  GtkWidget    *child)
1913 {
1914   GtkAssistantPrivate *priv;
1915
1916   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
1917   g_return_if_fail (GTK_IS_WIDGET (child));
1918
1919   priv = assistant->priv;
1920
1921   if (GTK_IS_BUTTON (child))
1922     gtk_size_group_add_widget (priv->size_group, child);
1923
1924   gtk_box_pack_end (GTK_BOX (priv->action_area), child, FALSE, FALSE, 0);
1925 }
1926
1927 /**
1928  * gtk_assistant_remove_action_widget:
1929  * @assistant: a #GtkAssistant
1930  * @child: a #GtkWidget
1931  *
1932  * Removes a widget from the action area of a #GtkAssistant.
1933  *
1934  * Since: 2.10
1935  **/
1936 void
1937 gtk_assistant_remove_action_widget (GtkAssistant *assistant,
1938                                     GtkWidget    *child)
1939 {
1940   GtkAssistantPrivate *priv;
1941
1942   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
1943   g_return_if_fail (GTK_IS_WIDGET (child));
1944
1945   priv = assistant->priv;
1946
1947   if (GTK_IS_BUTTON (child))
1948     gtk_size_group_remove_widget (priv->size_group, child);
1949
1950   gtk_container_remove (GTK_CONTAINER (priv->action_area), child);
1951 }
1952
1953 /**
1954  * gtk_assistant_set_page_title:
1955  * @assistant: a #GtkAssistant
1956  * @page: a page of @assistant
1957  * @title: the new title for @page
1958  * 
1959  * Sets a title for @page. The title is displayed in the header
1960  * area of the assistant when @page is the current page.
1961  *
1962  * Since: 2.10
1963  **/
1964 void
1965 gtk_assistant_set_page_title (GtkAssistant *assistant,
1966                               GtkWidget    *page,
1967                               const gchar  *title)
1968 {
1969   GtkAssistantPage *page_info;
1970   GList *child;
1971
1972   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
1973   g_return_if_fail (GTK_IS_WIDGET (page));
1974
1975   child = find_page (assistant, page);
1976
1977   g_return_if_fail (child != NULL);
1978
1979   page_info = (GtkAssistantPage*) child->data;
1980
1981   gtk_label_set_text ((GtkLabel*) page_info->title, title);
1982   gtk_widget_queue_resize (GTK_WIDGET (assistant));
1983   gtk_widget_child_notify (page, "title");
1984 }
1985
1986 /**
1987  * gtk_assistant_get_page_title:
1988  * @assistant: a #GtkAssistant
1989  * @page: a page of @assistant
1990  * 
1991  * Gets the title for @page. 
1992  * 
1993  * Return value: the title for @page.
1994  *
1995  * Since: 2.10
1996  **/
1997 G_CONST_RETURN gchar*
1998 gtk_assistant_get_page_title (GtkAssistant *assistant,
1999                               GtkWidget    *page)
2000 {
2001   GtkAssistantPage *page_info;
2002   GList *child;
2003
2004   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), NULL);
2005   g_return_val_if_fail (GTK_IS_WIDGET (page), NULL);
2006
2007   child = find_page (assistant, page);
2008
2009   g_return_val_if_fail (child != NULL, NULL);
2010
2011   page_info = (GtkAssistantPage*) child->data;
2012
2013   return gtk_label_get_text ((GtkLabel*) page_info->title);
2014 }
2015
2016 /**
2017  * gtk_assistant_set_page_type:
2018  * @assistant: a #GtkAssistant
2019  * @page: a page of @assistant
2020  * @type: the new type for @page
2021  * 
2022  * Sets the page type for @page. The page type determines the page
2023  * behavior in the @assistant.
2024  *
2025  * Since: 2.10
2026  **/
2027 void
2028 gtk_assistant_set_page_type (GtkAssistant         *assistant,
2029                              GtkWidget            *page,
2030                              GtkAssistantPageType  type)
2031 {
2032   GtkAssistantPrivate *priv;
2033   GtkAssistantPage *page_info;
2034   GList *child;
2035
2036   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
2037   g_return_if_fail (GTK_IS_WIDGET (page));
2038
2039   priv = assistant->priv;
2040   child = find_page (assistant, page);
2041
2042   g_return_if_fail (child != NULL);
2043
2044   page_info = (GtkAssistantPage*) child->data;
2045
2046   if (type != page_info->type)
2047     {
2048       page_info->type = type;
2049
2050       /* backwards compatibility to the era before fixing bug 604289 */
2051       if (type == GTK_ASSISTANT_PAGE_SUMMARY && !page_info->complete_set)
2052         {
2053           gtk_assistant_set_page_complete (assistant, page, TRUE);
2054           page_info->complete_set = FALSE;
2055         }
2056
2057       /* Always set buttons state, a change in a future page
2058          might change current page buttons */
2059       set_assistant_buttons_state (assistant);
2060
2061       gtk_widget_child_notify (page, "page-type");
2062     }
2063 }
2064
2065 /**
2066  * gtk_assistant_get_page_type:
2067  * @assistant: a #GtkAssistant
2068  * @page: a page of @assistant
2069  *
2070  * Gets the page type of @page.
2071  *
2072  * Return value: the page type of @page.
2073  *
2074  * Since: 2.10
2075  **/
2076 GtkAssistantPageType
2077 gtk_assistant_get_page_type (GtkAssistant *assistant,
2078                              GtkWidget    *page)
2079 {
2080   GtkAssistantPage *page_info;
2081   GList *child;
2082
2083   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), GTK_ASSISTANT_PAGE_CONTENT);
2084   g_return_val_if_fail (GTK_IS_WIDGET (page), GTK_ASSISTANT_PAGE_CONTENT);
2085
2086   child = find_page (assistant, page);
2087
2088   g_return_val_if_fail (child != NULL, GTK_ASSISTANT_PAGE_CONTENT);
2089
2090   page_info = (GtkAssistantPage*) child->data;
2091
2092   return page_info->type;
2093 }
2094
2095 /**
2096  * gtk_assistant_set_page_header_image:
2097  * @assistant: a #GtkAssistant
2098  * @page: a page of @assistant
2099  * @pixbuf: (allow-none): the new header image @page
2100  *
2101  * Sets a header image for @page. This image is displayed in the header
2102  * area of the assistant when @page is the current page.
2103  *
2104  * Since: 2.10
2105  **/
2106 void
2107 gtk_assistant_set_page_header_image (GtkAssistant *assistant,
2108                                      GtkWidget    *page,
2109                                      GdkPixbuf    *pixbuf)
2110 {
2111   GtkAssistantPrivate *priv;
2112   GtkAssistantPage *page_info;
2113   GList *child;
2114
2115   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
2116   g_return_if_fail (GTK_IS_WIDGET (page));
2117   g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2118
2119   priv = assistant->priv;
2120   child = find_page (assistant, page);
2121
2122   g_return_if_fail (child != NULL);
2123
2124   page_info = (GtkAssistantPage*) child->data;
2125
2126   if (pixbuf != page_info->header_image)
2127     {
2128       if (page_info->header_image)
2129         {
2130           g_object_unref (page_info->header_image);
2131           page_info->header_image = NULL;
2132         }
2133
2134       if (pixbuf)
2135         page_info->header_image = g_object_ref (pixbuf);
2136
2137       if (page_info == priv->current_page)
2138         set_assistant_header_image (assistant);
2139
2140       gtk_widget_child_notify (page, "header-image");
2141     }
2142 }
2143
2144 /**
2145  * gtk_assistant_get_page_header_image:
2146  * @assistant: a #GtkAssistant
2147  * @page: a page of @assistant
2148  *
2149  * Gets the header image for @page.
2150  *
2151  * Return value: (transfer none): the header image for @page, or %NULL
2152  *     if there's no header image for the page.
2153  *
2154  * Since: 2.10
2155  **/
2156 GdkPixbuf*
2157 gtk_assistant_get_page_header_image (GtkAssistant *assistant,
2158                                      GtkWidget    *page)
2159 {
2160   GtkAssistantPage *page_info;
2161   GList *child;
2162
2163   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), NULL);
2164   g_return_val_if_fail (GTK_IS_WIDGET (page), NULL);
2165
2166   child = find_page (assistant, page);
2167
2168   g_return_val_if_fail (child != NULL, NULL);
2169
2170   page_info = (GtkAssistantPage*) child->data;
2171
2172   return page_info->header_image;
2173 }
2174
2175 /**
2176  * gtk_assistant_set_page_side_image:
2177  * @assistant: a #GtkAssistant
2178  * @page: a page of @assistant
2179  * @pixbuf: (allow-none): the new header image @page
2180  *
2181  * Sets a header image for @page. This image is displayed in the side
2182  * area of the assistant when @page is the current page.
2183  *
2184  * Since: 2.10
2185  **/
2186 void
2187 gtk_assistant_set_page_side_image (GtkAssistant *assistant,
2188                                    GtkWidget    *page,
2189                                    GdkPixbuf    *pixbuf)
2190 {
2191   GtkAssistantPrivate *priv;
2192   GtkAssistantPage *page_info;
2193   GList *child;
2194
2195   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
2196   g_return_if_fail (GTK_IS_WIDGET (page));
2197   g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf));
2198
2199   priv = assistant->priv;
2200   child = find_page (assistant, page);
2201
2202   g_return_if_fail (child != NULL);
2203
2204   page_info = (GtkAssistantPage*) child->data;
2205
2206   if (pixbuf != page_info->sidebar_image)
2207     {
2208       if (page_info->sidebar_image)
2209         {
2210           g_object_unref (page_info->sidebar_image);
2211           page_info->sidebar_image = NULL;
2212         }
2213
2214       if (pixbuf)
2215         page_info->sidebar_image = g_object_ref (pixbuf);
2216
2217       if (page_info == priv->current_page)
2218         set_assistant_sidebar_image (assistant);
2219
2220       gtk_widget_child_notify (page, "sidebar-image");
2221     }
2222 }
2223
2224 /**
2225  * gtk_assistant_get_page_side_image:
2226  * @assistant: a #GtkAssistant
2227  * @page: a page of @assistant
2228  *
2229  * Gets the header image for @page.
2230  *
2231  * Return value: (transfer none): the side image for @page, or %NULL
2232  *     if there's no side image for the page.
2233  *
2234  * Since: 2.10
2235  **/
2236 GdkPixbuf*
2237 gtk_assistant_get_page_side_image (GtkAssistant *assistant,
2238                                    GtkWidget    *page)
2239 {
2240   GtkAssistantPage *page_info;
2241   GList *child;
2242
2243   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), NULL);
2244   g_return_val_if_fail (GTK_IS_WIDGET (page), NULL);
2245
2246   child = find_page (assistant, page);
2247
2248   g_return_val_if_fail (child != NULL, NULL);
2249
2250   page_info = (GtkAssistantPage*) child->data;
2251
2252   return page_info->sidebar_image;
2253 }
2254
2255 /**
2256  * gtk_assistant_set_page_complete:
2257  * @assistant: a #GtkAssistant
2258  * @page: a page of @assistant
2259  * @complete: the completeness status of the page
2260  * 
2261  * Sets whether @page contents are complete. This will make
2262  * @assistant update the buttons state to be able to continue the task.
2263  *
2264  * Since: 2.10
2265  **/
2266 void
2267 gtk_assistant_set_page_complete (GtkAssistant *assistant,
2268                                  GtkWidget    *page,
2269                                  gboolean      complete)
2270 {
2271   GtkAssistantPrivate *priv;
2272   GtkAssistantPage *page_info;
2273   GList *child;
2274
2275   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
2276   g_return_if_fail (GTK_IS_WIDGET (page));
2277
2278   priv = assistant->priv;
2279   child = find_page (assistant, page);
2280
2281   g_return_if_fail (child != NULL);
2282
2283   page_info = (GtkAssistantPage*) child->data;
2284
2285   if (complete != page_info->complete)
2286     {
2287       page_info->complete = complete;
2288       page_info->complete_set = TRUE;
2289
2290       /* Always set buttons state, a change in a future page
2291          might change current page buttons */
2292       set_assistant_buttons_state (assistant);
2293
2294       gtk_widget_child_notify (page, "complete");
2295     }
2296 }
2297
2298 /**
2299  * gtk_assistant_get_page_complete:
2300  * @assistant: a #GtkAssistant
2301  * @page: a page of @assistant
2302  * 
2303  * Gets whether @page is complete.
2304  * 
2305  * Return value: %TRUE if @page is complete.
2306  *
2307  * Since: 2.10
2308  **/
2309 gboolean
2310 gtk_assistant_get_page_complete (GtkAssistant *assistant,
2311                                  GtkWidget    *page)
2312 {
2313   GtkAssistantPage *page_info;
2314   GList *child;
2315
2316   g_return_val_if_fail (GTK_IS_ASSISTANT (assistant), FALSE);
2317   g_return_val_if_fail (GTK_IS_WIDGET (page), FALSE);
2318
2319   child = find_page (assistant, page);
2320
2321   g_return_val_if_fail (child != NULL, FALSE);
2322
2323   page_info = (GtkAssistantPage*) child->data;
2324
2325   return page_info->complete;
2326 }
2327
2328 /**
2329  * gtk_assistant_update_buttons_state:
2330  * @assistant: a #GtkAssistant
2331  * 
2332  * Forces @assistant to recompute the buttons state.
2333  * 
2334  * GTK+ automatically takes care of this in most situations, 
2335  * e.g. when the user goes to a different page, or when the
2336  * visibility or completeness of a page changes.
2337  *
2338  * One situation where it can be necessary to call this
2339  * function is when changing a value on the current page
2340  * affects the future page flow of the assistant.
2341  *
2342  * Since: 2.10
2343  **/
2344 void
2345 gtk_assistant_update_buttons_state (GtkAssistant *assistant)
2346 {
2347   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
2348
2349   set_assistant_buttons_state (assistant);
2350 }
2351
2352 /**
2353  * gtk_assistant_commit:
2354  * @assistant: a #GtkAssistant
2355  *
2356  * Erases the visited page history so the back button is not
2357  * shown on the current page, and removes the cancel button
2358  * from subsequent pages.
2359  *
2360  * Use this when the information provided up to the current
2361  * page is hereafter deemed permanent and cannot be modified
2362  * or undone.  For example, showing a progress page to track
2363  * a long-running, unreversible operation after the user has
2364  * clicked apply on a confirmation page.
2365  *
2366  * Since: 2.22
2367  **/
2368 void
2369 gtk_assistant_commit (GtkAssistant *assistant)
2370 {
2371   g_return_if_fail (GTK_IS_ASSISTANT (assistant));
2372
2373   g_slist_free (assistant->priv->visited_pages);
2374   assistant->priv->visited_pages = NULL;
2375
2376   assistant->priv->committed = TRUE;
2377
2378   set_assistant_buttons_state (assistant);
2379 }
2380
2381
2382
2383 /* accessible implementation */
2384
2385 static gint
2386 gtk_assistant_accessible_get_n_children (AtkObject *accessible)
2387 {
2388   GtkAssistant *assistant;
2389   GtkWidget *widget;
2390
2391   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
2392
2393   if (!widget)
2394     return 0;
2395
2396   assistant = GTK_ASSISTANT (widget);
2397    
2398   return g_list_length (assistant->priv->pages) + 1;
2399 }
2400
2401
2402 static AtkObject *
2403 gtk_assistant_accessible_ref_child (AtkObject *accessible,
2404                                     gint       index)
2405 {
2406   GtkAssistant *assistant;
2407   GtkAssistantPrivate *priv;
2408   GtkWidget *widget, *child;
2409   gint n_pages;
2410   AtkObject *obj;
2411   const gchar *title;
2412
2413   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
2414   if (!widget)
2415     return NULL;
2416
2417   assistant = GTK_ASSISTANT (widget);
2418   priv = assistant->priv;
2419   n_pages = g_list_length (priv->pages);
2420
2421   if (index < 0)
2422     return NULL;
2423   else if (index < n_pages)
2424     {
2425       GtkAssistantPage *page = g_list_nth_data (priv->pages, index);
2426
2427       child = page->page;
2428       title = gtk_assistant_get_page_title (assistant, child);
2429     }
2430   else if (index == n_pages)
2431     {
2432       child = priv->action_area;
2433       title = NULL;
2434     }
2435   else
2436     return NULL;
2437   
2438   obj = gtk_widget_get_accessible (child);
2439
2440   if (title)
2441     atk_object_set_name (obj, title);
2442
2443   return g_object_ref (obj);
2444 }
2445
2446 static void
2447 gtk_assistant_accessible_class_init (AtkObjectClass *class)
2448 {
2449   class->get_n_children = gtk_assistant_accessible_get_n_children;
2450   class->ref_child = gtk_assistant_accessible_ref_child;
2451 }
2452
2453 static GType
2454 gtk_assistant_accessible_get_type (void)
2455 {
2456   static GType type = 0;
2457   
2458   if (!type)
2459     {
2460       /*
2461        * Figure out the size of the class and instance
2462        * we are deriving from
2463        */
2464       AtkObjectFactory *factory;
2465       GType derived_type;
2466       GTypeQuery query;
2467       GType derived_atk_type;
2468
2469       derived_type = g_type_parent (GTK_TYPE_ASSISTANT);
2470       factory = atk_registry_get_factory (atk_get_default_registry (),
2471                                           derived_type);
2472       derived_atk_type = atk_object_factory_get_accessible_type (factory);
2473       g_type_query (derived_atk_type, &query);
2474
2475       type = g_type_register_static_simple (derived_atk_type,
2476                                             I_("GtkAssistantAccessible"),
2477                                             query.class_size,
2478                                             (GClassInitFunc) gtk_assistant_accessible_class_init,
2479                                             query.instance_size,
2480                                             NULL, 0);
2481     }
2482
2483   return type;
2484 }
2485
2486 static AtkObject *
2487 gtk_assistant_accessible_new (GObject *obj)
2488 {
2489   AtkObject *accessible;
2490
2491   g_return_val_if_fail (GTK_IS_ASSISTANT (obj), NULL);
2492
2493   accessible = g_object_new (gtk_assistant_accessible_get_type (), NULL);
2494   atk_object_initialize (accessible, obj);
2495
2496   return accessible;
2497 }
2498
2499 static GType
2500 gtk_assistant_accessible_factory_get_accessible_type (void)
2501 {
2502   return gtk_assistant_accessible_get_type ();
2503 }
2504
2505 static AtkObject*
2506 gtk_assistant_accessible_factory_create_accessible (GObject *obj)
2507 {
2508   return gtk_assistant_accessible_new (obj);
2509 }
2510
2511 static void
2512 gtk_assistant_accessible_factory_class_init (AtkObjectFactoryClass *class)
2513 {
2514   class->create_accessible = gtk_assistant_accessible_factory_create_accessible;
2515   class->get_accessible_type = gtk_assistant_accessible_factory_get_accessible_type;
2516 }
2517
2518 static GType
2519 gtk_assistant_accessible_factory_get_type (void)
2520 {
2521   static GType type = 0;
2522
2523   if (!type)
2524     {
2525       type = g_type_register_static_simple (ATK_TYPE_OBJECT_FACTORY,
2526                                             I_("GtkAssistantAccessibleFactory"),
2527                                             sizeof (AtkObjectFactoryClass),
2528                                             (GClassInitFunc) gtk_assistant_accessible_factory_class_init,
2529                                             sizeof (AtkObjectFactory),
2530                                             NULL, 0);
2531     }
2532
2533   return type;
2534 }
2535
2536 static AtkObject *
2537 gtk_assistant_get_accessible (GtkWidget *widget)
2538 {
2539   static gboolean first_time = TRUE;
2540
2541   if (first_time)
2542     {
2543       AtkObjectFactory *factory;
2544       AtkRegistry *registry;
2545       GType derived_type;
2546       GType derived_atk_type;
2547
2548       /*
2549        * Figure out whether accessibility is enabled by looking at the
2550        * type of the accessible object which would be created for
2551        * the parent type of GtkAssistant.
2552        */
2553       derived_type = g_type_parent (GTK_TYPE_ASSISTANT);
2554
2555       registry = atk_get_default_registry ();
2556       factory = atk_registry_get_factory (registry,
2557                                           derived_type);
2558       derived_atk_type = atk_object_factory_get_accessible_type (factory);
2559       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE))
2560         {
2561           atk_registry_set_factory_type (registry,
2562                                          GTK_TYPE_ASSISTANT,
2563                                          gtk_assistant_accessible_factory_get_type ());
2564         }
2565       first_time = FALSE;
2566     }
2567
2568   return GTK_WIDGET_CLASS (gtk_assistant_parent_class)->get_accessible (widget);
2569 }
2570
2571
2572 static GtkBuildableIface *parent_buildable_iface;
2573
2574 static void
2575 gtk_assistant_buildable_interface_init (GtkBuildableIface *iface)
2576 {
2577   parent_buildable_iface = g_type_interface_peek_parent (iface);
2578   iface->get_internal_child = gtk_assistant_buildable_get_internal_child;
2579   iface->custom_tag_start = gtk_assistant_buildable_custom_tag_start;
2580   iface->custom_finished = gtk_assistant_buildable_custom_finished;
2581 }
2582
2583 static GObject *
2584 gtk_assistant_buildable_get_internal_child (GtkBuildable *buildable,
2585                                             GtkBuilder   *builder,
2586                                             const gchar  *childname)
2587 {
2588     if (strcmp (childname, "action_area") == 0)
2589       return G_OBJECT (GTK_ASSISTANT (buildable)->priv->action_area);
2590
2591     return parent_buildable_iface->get_internal_child (buildable,
2592                                                        builder,
2593                                                        childname);
2594 }
2595
2596 gboolean
2597 gtk_assistant_buildable_custom_tag_start (GtkBuildable  *buildable,
2598                                           GtkBuilder    *builder,
2599                                           GObject       *child,
2600                                           const gchar   *tagname,
2601                                           GMarkupParser *parser,
2602                                           gpointer      *data)
2603 {
2604   return parent_buildable_iface->custom_tag_start (buildable, builder, child,
2605                                                    tagname, parser, data);
2606 }
2607
2608 static void
2609 gtk_assistant_buildable_custom_finished (GtkBuildable *buildable,
2610                                          GtkBuilder   *builder,
2611                                          GObject      *child,
2612                                          const gchar  *tagname,
2613                                          gpointer      user_data)
2614 {
2615   parent_buildable_iface->custom_finished (buildable, builder, child,
2616                                            tagname, user_data);
2617 }