Use GTK+ single includes
[modest] / src / widgets / modest-wizard-dialog.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 /**
31  * SECTION:modest-wizard-dialog
32  * @short_description: A widget to create a guided installation
33  * process wizard
34  *
35  * #ModestWizardDialog is a widget to create a guided installation
36  * process. The dialog has four standard buttons, previous, next,
37  * finish, cancel, and contains several pages with optional icons.
38  * Response buttons are dimmed/undimmed automatically and the standard
39  * icon is shown/hidden in response to page navigation. The notebook
40  * widget provided by users contains the actual wizard pages.
41  */
42
43 #include <config.h>
44 #include <gtk/gtk.h>
45
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49
50 #ifndef MODEST_TOOLKIT_GTK
51 #if MODEST_HILDON_API == 0
52 #include <hildon-widgets/hildon-defines.h>
53 #else
54 #include <hildon/hildon-defines.h>
55 #endif /*MODEST_HILDON_API == 0*/
56 #endif /*!MODEST_TOOLKIT_GTK*/
57
58 #include "modest-wizard-dialog.h"
59 #include "modest-debug.h"
60 #include "modest-text-utils.h"
61
62 static GtkDialogClass *parent_class;
63
64 static void class_init              (ModestWizardDialogClass   *wizard_dialog_class);
65
66 static void init                    (ModestWizardDialog        *wizard_dialog);
67
68 static void create_title            (ModestWizardDialog        *wizard_dialog);
69
70 static void set_property            (GObject                   *object,
71                                      guint                     property_id,
72                                      const GValue              *value,
73                                      GParamSpec                *pspec);
74
75 static void get_property            (GObject                   *object,
76                                      guint                     property_id,
77                                      GValue                    *value,
78                                      GParamSpec                *pspec);
79
80 static void finalize                (GObject                   *object);
81
82 static void response                (ModestWizardDialog        *wizard, 
83                                      gint                      response_id,
84                                      gpointer                  unused);
85
86 static void make_buttons_sensitive  (ModestWizardDialog *wizard_dialog,
87                                      gboolean           previous,
88                                      gboolean           finish,
89                                      gboolean next);
90
91 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
92 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
93 static void invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog);
94 static gboolean invoke_save_vfunc (ModestWizardDialog *wizard_dialog);
95
96 enum {
97     PROP_ZERO,
98     PROP_WIZARD_NAME,
99     PROP_WIZARD_NOTEBOOK,
100     PROP_WIZARD_AUTOTITLE
101 };
102
103 struct _ModestWizardDialogPrivate {
104     gchar       *wizard_name;
105     GtkNotebook *notebook;
106     GtkBox      *box;
107     GtkWidget   *image;
108     gboolean    autotitle;
109
110     ModestWizardDialogResponseOverrideFunc override_func;
111 };
112
113
114 GType
115 modest_wizard_dialog_get_type (void)
116 {
117     static GType wizard_dialog_type = 0;
118
119     if (!wizard_dialog_type) {
120
121         static const GTypeInfo wizard_dialog_info = {
122             sizeof (ModestWizardDialogClass),
123             NULL,       /* base_init      */
124             NULL,       /* base_finalize  */
125             (GClassInitFunc) class_init,
126             NULL,       /* class_finalize */
127             NULL,       /* class_data     */
128             sizeof (ModestWizardDialog),
129             0,          /* n_preallocs    */
130             (GInstanceInitFunc) init,
131         };
132
133         wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
134                                                      "ModestWizardDialog",
135                                                      &wizard_dialog_info,
136                                                      0);
137     }
138
139     return wizard_dialog_type;
140 }
141
142 static void
143 class_init (ModestWizardDialogClass *wizard_dialog_class)
144 {
145     GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
146
147     parent_class = g_type_class_peek_parent (wizard_dialog_class);
148
149     g_type_class_add_private (wizard_dialog_class,
150                               sizeof(ModestWizardDialogPrivate));
151
152     /* Override virtual methods */
153     object_class->set_property = set_property;
154     object_class->get_property = get_property;
155     object_class->finalize     = finalize;
156
157     wizard_dialog_class->before_next = NULL;
158     wizard_dialog_class->update_model = NULL;
159     wizard_dialog_class->save = NULL;
160     wizard_dialog_class->enable_buttons = NULL;
161
162     /**
163      * ModestWizardDialog:wizard-name:
164      *
165      * The name of the wizard.
166      */
167     g_object_class_install_property (object_class, PROP_WIZARD_NAME,
168             g_param_spec_string 
169             ("wizard-name",
170              "Wizard Name",
171              "The name of the ModestWizardDialog",
172              NULL,
173              G_PARAM_READWRITE));
174
175     /**
176      * ModestWizardDialog:wizard-notebook:
177      *
178      * The notebook object, which is used by the ModestWizardDialog.
179      */
180     g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
181             g_param_spec_object 
182             ("wizard-notebook",
183              "Wizard Notebook",
184              "GtkNotebook object to be used in the "
185              "ModestWizardDialog",
186              GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
187
188     /**
189      * ModestWizardDialog:autotitle
190      *
191      * If the wizard should automatically try to change the window title when changing steps. 
192      * Set to FALSE if you'd like to override the default behaviour. 
193      *
194      * Since: 0.14.5 
195      */
196     g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
197             g_param_spec_boolean 
198             ("autotitle",
199              "AutoTitle",
200              "If the wizard should autotitle itself",
201              TRUE, 
202              G_PARAM_READWRITE));
203 }
204
205 static void 
206 finalize (GObject *object)
207 {
208     ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
209     g_return_if_fail (dialog != NULL);
210
211     if (dialog->priv->wizard_name != NULL)
212         g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
213     
214     if (G_OBJECT_CLASS (parent_class)->finalize)
215         G_OBJECT_CLASS (parent_class)->finalize(object);
216 }
217
218 /* Disable or enable the Previous, Next and Finish buttons */
219 static void
220 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
221                         gboolean previous,
222                         gboolean finish,
223                         gboolean next)
224 {
225     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
226                                        MODEST_WIZARD_DIALOG_PREVIOUS,
227                                        previous);
228
229     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
230                                        MODEST_WIZARD_DIALOG_FINISH,
231                                        finish);
232
233     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
234                                        MODEST_WIZARD_DIALOG_NEXT,
235                                        next);
236 }
237
238 static void 
239 init (ModestWizardDialog *wizard_dialog)
240 {
241     /* Initialize private structure for faster member access */
242     ModestWizardDialogPrivate *priv =
243         G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
244                 MODEST_TYPE_WIZARD_DIALOG,
245                 ModestWizardDialogPrivate);
246
247     GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
248
249     /* Init internal widgets */
250     GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
251     gtk_dialog_set_has_separator (dialog, FALSE);
252     wizard_dialog->priv = priv;
253     priv->override_func = NULL;
254     priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
255 #ifdef MODEST_TOOLKIT_HILDON2
256     priv->image = NULL;
257 #else
258 #ifdef MODEST_TOOLKIT_GTK
259     priv->image = gtk_image_new_from_stock (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG);
260 #else /*MODEST_TOOLKIT_GTK*/
261 #if MODEST_HILDON_API == 0
262     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
263                                                 HILDON_ICON_SIZE_WIDG_WIZARD);
264 #else       
265     static int icon_size = 0;
266     if (!icon_size)
267             icon_size = gtk_icon_size_register("modest_wizard", 50, 50);
268     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
269                                                 icon_size);
270 #endif /*MODEST_HILDON_API == 0*/
271 #endif /*!MODEST_TOOLKIT_GTK*/
272 #endif /*MODEST_TOOLKIT_HILDON2 */
273     /* Default values for user provided properties */
274     priv->notebook = NULL;
275     priv->wizard_name = NULL;
276     priv->autotitle = TRUE;
277
278     /* Build wizard layout */
279     gtk_box_pack_start (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box), TRUE, TRUE, 0);
280     gtk_box_pack_start (GTK_BOX (priv->box), GTK_WIDGET (vbox), FALSE, FALSE, 0);
281     gtk_widget_show (vbox);
282     gtk_widget_show (GTK_WIDGET (priv->box));
283     if (priv->image) {
284             gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), TRUE, TRUE, 0);
285             gtk_widget_show (priv->image);
286     }
287
288     /* Add response buttons: finish, previous, next, cancel */
289 #ifdef MODEST_TOOLKIT_HILDON1
290     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
291     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
292     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
293     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
294 #endif
295 #ifdef MODEST_TOOLKIT_HILDON2
296     gtk_dialog_add_button (dialog, _HL("wdgt_bd_finish"), MODEST_WIZARD_DIALOG_FINISH);
297     gtk_dialog_add_button (dialog, _HL("wdgt_bd_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
298     gtk_dialog_add_button (dialog, _HL("wdgt_bd_next"), MODEST_WIZARD_DIALOG_NEXT);
299 #endif
300 #ifdef MODEST_TOOLKIT_GTK
301     gtk_dialog_add_button (dialog, GTK_STOCK_SAVE, MODEST_WIZARD_DIALOG_FINISH);
302     gtk_dialog_add_button (dialog, GTK_STOCK_GO_BACK, MODEST_WIZARD_DIALOG_PREVIOUS);
303     gtk_dialog_add_button (dialog, GTK_STOCK_GO_FORWARD, MODEST_WIZARD_DIALOG_NEXT);
304     gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, MODEST_WIZARD_DIALOG_CANCEL);
305 #endif
306
307     /* Set initial button states: previous and finish buttons are disabled */
308     make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
309
310     gtk_widget_show (GTK_WIDGET (dialog->vbox));
311
312     /* connect to dialog's response signal */
313     g_signal_connect (G_OBJECT (dialog), "response",
314             G_CALLBACK (response), NULL);
315
316 }
317
318 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
319 static void on_notebook_page_added(GtkNotebook *notebook, 
320                                    GtkWidget   *child,
321                                    guint        page_num,
322                                    gpointer     user_data)
323 {
324         ModestWizardDialog* dialog = NULL;
325
326         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
327         dialog = MODEST_WIZARD_DIALOG(user_data);
328
329         /* The title should show the total number of pages: */
330         create_title (dialog);
331 }
332
333 static void on_notebook_page_removed(GtkNotebook *notebook, 
334                                      GtkWidget   *child,
335                                      guint        page_num,
336                                      gpointer     user_data)
337 {
338         ModestWizardDialog* dialog = NULL;
339
340         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
341         dialog = MODEST_WIZARD_DIALOG(user_data);
342
343         /* The title should show the total number of pages: */
344         create_title (dialog);
345 }
346 #endif /* GTK_CHECK_VERSION */
347
348 static void
349 on_notebook_switch_page (GtkNotebook *notebook,
350                          GtkNotebookPage *page,
351                          guint page_num,
352                          ModestWizardDialog *self)
353 {
354         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(self));
355
356         create_title (self);
357 }
358
359 static void
360 connect_to_notebook_signals(ModestWizardDialog* dialog)
361 {
362 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
363         ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
364         g_return_if_fail (priv->notebook);
365         
366         /* Connect to the notebook signals,
367          * so we can update the title when necessary: */
368         g_signal_connect (G_OBJECT (priv->notebook), "page-added",
369                       G_CALLBACK (on_notebook_page_added), dialog);
370         g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
371                       G_CALLBACK (on_notebook_page_removed), dialog);
372 #endif /* GTK_CHECK_VERSION */
373         g_signal_connect_after (G_OBJECT (priv->notebook), "switch-page",
374                                 G_CALLBACK (on_notebook_switch_page), dialog);
375 }
376
377
378 static void
379 set_property (GObject      *object, 
380               guint        property_id,
381               const GValue *value, 
382               GParamSpec   *pspec)
383 {
384     ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
385
386     switch (property_id) {
387
388         case PROP_WIZARD_AUTOTITLE:
389
390             priv->autotitle = g_value_get_boolean (value);
391
392             if (priv->autotitle && 
393                 priv->wizard_name && 
394                 priv->notebook)
395                 create_title (MODEST_WIZARD_DIALOG (object));
396             else if (priv->wizard_name)
397                 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
398             
399             break;
400
401         case PROP_WIZARD_NAME: 
402
403             /* Set new wizard name. This name will appear in titlebar */
404             if (priv->wizard_name)
405                 g_free (priv->wizard_name);
406
407             gchar *str = (gchar *) g_value_get_string (value);
408             g_return_if_fail (str != NULL);
409
410             priv->wizard_name = g_strdup (str);
411
412             /* We need notebook in order to create title, since page information
413                is used in title generation */
414             
415             if (priv->notebook && priv->autotitle)
416                 create_title (MODEST_WIZARD_DIALOG (object));
417     
418             break;
419
420         case PROP_WIZARD_NOTEBOOK: {
421
422             GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
423             g_return_if_fail (book != NULL);
424
425             priv->notebook = book;
426
427             /* Set the default properties for the notebook (disable tabs,
428              * and remove borders) to make it look like a nice wizard widget */
429             gtk_notebook_set_show_tabs (priv->notebook, FALSE);
430             gtk_notebook_set_show_border (priv->notebook, FALSE);
431             gtk_box_pack_start (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook), TRUE, TRUE, 0);
432
433             /* Show the notebook so that a gtk_widget_show on the dialog is
434              * all that is required to display the dialog correctly */
435             gtk_widget_show ( GTK_WIDGET (priv->notebook));
436
437             /* Update dialog title to reflect current page stats etc */ 
438             ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);      
439             if (priv->wizard_name && priv->autotitle)
440                 create_title (wizard_dialog);
441                 
442             connect_to_notebook_signals (wizard_dialog);
443             
444             }break;
445
446         default:
447             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
448             break;
449     }
450 }
451
452 static void
453 get_property (GObject      *object,
454               guint        property_id,
455               GValue       *value,
456               GParamSpec   *pspec)
457 {
458     ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
459
460     switch (property_id) {
461
462         case PROP_WIZARD_NAME:
463             g_value_set_string (value, priv->wizard_name);
464             break;
465
466         case PROP_WIZARD_NOTEBOOK:
467             g_value_set_object (value, priv->notebook);
468             break;
469
470         default:
471             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
472             break;
473     }
474 }
475
476 /*
477  * Creates the title of the dialog taking into account the current 
478  * page of the notebook.
479  */
480 static void
481 create_title (ModestWizardDialog *wizard_dialog)
482 {
483     gchar *str = NULL;
484     ModestWizardDialogPrivate *priv = NULL;
485     GtkNotebook *notebook = NULL;
486     gint pages, current;
487     const gchar *steps;
488
489     g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
490     g_return_if_fail (wizard_dialog->priv != NULL);
491
492     priv = wizard_dialog->priv;
493     notebook = priv->notebook;
494
495     if (!notebook)
496         return;
497
498     /* Get page information, we'll need that when creating title */
499     pages = gtk_notebook_get_n_pages (notebook);
500     if (pages == 0)
501             return;
502
503     current = gtk_notebook_get_current_page (priv->notebook);
504     if (current < 0)
505             current = 0;
506
507     steps = gtk_notebook_get_tab_label_text (notebook,
508                                              gtk_notebook_get_nth_page (notebook, current));
509
510     str = g_strdup_printf ((steps&&*steps)?_HL("%s%s %s"):_HL("%s"),
511                            priv->wizard_name, _HL("ecdg_ti_caption_separator"),
512                            steps);
513
514     /* Update the dialog to display the generated title */
515     gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
516     g_free (str);
517 }
518
519 /*
520  * Response signal handler. This function is needed because GtkDialog's 
521  * handler for this signal closes the dialog and we don't want that, we 
522  * want to change pages and, dim certain response buttons. Overriding the 
523  * virtual function would not work because that would be called after the 
524  * signal handler implemented by GtkDialog.
525  * FIXME: There is a much saner way to do that [MDK]
526  */
527 static void 
528 response (ModestWizardDialog   *wizard_dialog,
529           gint                 response_id,
530           gpointer             unused)
531 {
532     ModestWizardDialogPrivate *priv = wizard_dialog->priv;
533     GtkNotebook *notebook = priv->notebook;
534     gint current = 0;
535     gboolean is_first, is_last;
536
537     if (priv->override_func) {
538             if (priv->override_func (wizard_dialog, response_id, gtk_notebook_get_current_page (notebook))) {
539                     /* Don't let the dialog close */
540                     g_signal_stop_emission_by_name (wizard_dialog, "response");
541                     
542                     /* Force refresh of title */
543                     if (priv->autotitle) 
544                             create_title (wizard_dialog);
545                     return;
546             }
547     }
548     
549     switch (response_id) {
550         
551         case MODEST_WIZARD_DIALOG_PREVIOUS:
552             gtk_notebook_prev_page (notebook); /* go to previous page */
553             break;
554
555         case MODEST_WIZARD_DIALOG_NEXT:
556                 if (invoke_before_next_vfunc (wizard_dialog))
557                         gtk_notebook_next_page (notebook); /* go to next page */
558                 
559             break;
560
561         case MODEST_WIZARD_DIALOG_CANCEL:
562                 return;
563                 break;      
564         case MODEST_WIZARD_DIALOG_FINISH:
565                 if (invoke_before_next_vfunc (wizard_dialog))
566                 return;
567             
568             break;
569
570     }
571
572     current = gtk_notebook_get_current_page (notebook);
573     gint last = gtk_notebook_get_n_pages (notebook) - 1;
574     is_last = current == last;
575     is_first = current == 0;
576
577     /* If first page, previous and finish are disabled, 
578        if last page, next is disabled */
579     make_buttons_sensitive (wizard_dialog,
580                             (is_first) ? FALSE : TRUE,
581                             TRUE,
582                             (is_last) ? FALSE : TRUE);
583
584     /* Allow derived classes to disable buttons to prevent navigation,
585      * according to their own validation logic: */
586     invoke_enable_buttons_vfunc (wizard_dialog);
587
588     /* Don't let the dialog close */
589     g_signal_stop_emission_by_name (wizard_dialog, "response");
590
591     /* We show the default image on first and last pages */
592     last = gtk_notebook_get_n_pages (notebook) - 1;
593     if (priv->image) {
594             if (current == last || current == 0)
595                     gtk_widget_show (GTK_WIDGET(priv->image));
596             else
597                     gtk_widget_hide (GTK_WIDGET(priv->image));
598     }
599
600     /* New page number may appear in the title, update it */
601     if (priv->autotitle) 
602         create_title (wizard_dialog);
603 }
604
605 /**
606  * modest_wizard_dialog_new:
607  * @parent: a #GtkWindow
608  * @wizard_name: the name of dialog
609  * @notebook: the notebook to be shown on the dialog
610  *
611  * Creates a new #ModestWizardDialog.
612  *
613  * Returns: a new #ModestWizardDialog
614  */
615 GtkWidget*
616 modest_wizard_dialog_new (GtkWindow   *parent,
617                           const char  *wizard_name,
618                           GtkNotebook *notebook)
619 {
620     GtkWidget *widget;
621
622     g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
623
624     widget = GTK_WIDGET (g_object_new
625             (MODEST_TYPE_WIZARD_DIALOG,
626              "wizard-name", wizard_name,
627              "wizard-notebook", notebook, NULL));
628
629     if (parent)
630         gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
631
632     return widget;
633 }
634
635 /**
636  * modest_wizard_dialog_force_title_update:
637  * @wizard_dialog: The wizard dialog
638  *
639  * Force the title to be rebuilt, for instance when you have added or 
640  * removed notebook pages. This function is not necessary when using GTK+ 2.10, 
641  * because that has GtkNotebook signals that will be used to update the title 
642  * automatically.
643  */
644 void
645 modest_wizard_dialog_force_title_update (ModestWizardDialog   *wizard_dialog)
646 {
647         create_title (wizard_dialog);
648 }
649
650 static gboolean
651 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
652 {
653         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
654         
655         /* Call the vfunc, which may be overridden by derived classes: */
656         if (klass->before_next) {
657                 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
658         
659                 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
660                 
661                 /* Get widgets for the two pages: */
662                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
663                 
664                 GtkWidget* next_page_widget = NULL;
665                 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
666                         next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
667
668                 MODEST_DEBUG_BLOCK (
669                 g_debug ("Switching to page %d (%s)",
670                          gtk_notebook_page_num (priv->notebook, next_page_widget),
671                          gtk_notebook_get_tab_label_text (priv->notebook, next_page_widget));
672
673                 {
674                         GtkWidget *p;
675                         gint i;
676                         g_debug ("\t***************");
677                         for (i=0; i<gtk_notebook_get_n_pages(priv->notebook);i++) {
678                                 p = gtk_notebook_get_nth_page (priv->notebook, i);
679                                 g_debug ("\t%d - %s", i, gtk_notebook_get_tab_label_text (priv->notebook, p));
680                         }
681                         g_debug ("\t***************");
682                 }
683                                     );
684                 
685                 /* Ask the vfunc implementation whether navigation should be allowed: */
686                 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
687         }
688         
689         /* Allow navigation by default if there is no vfunc implementation: */
690         return TRUE;
691 }
692
693 static void
694 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
695 {
696         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
697         
698         /* Call the vfunc, which may be overridden by derived classes: */
699         if (klass->enable_buttons) {
700                 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
701         
702                 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
703                 
704                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
705                         
706                 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);
707         }
708 }
709
710 static void
711 invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog)
712 {
713         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
714         
715         /* Call the vfunc, which may be overridden by derived classes: */
716         if (klass->update_model) {
717                 (*(klass->update_model)) (wizard_dialog);
718         }
719 }
720
721 static gboolean
722 invoke_save_vfunc (ModestWizardDialog *wizard_dialog)
723 {
724         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
725         
726         /* Call the vfunc, which may be overridden by derived classes: */
727         if (klass->save) {
728                 return (*(klass->save)) (wizard_dialog);
729         } else {
730                 return TRUE;
731         }
732 }
733
734 void 
735 modest_wizard_dialog_set_response_override_handler (ModestWizardDialog *wizard_dialog,
736                                                     ModestWizardDialogResponseOverrideFunc callback)
737 {
738     ModestWizardDialogPrivate *priv = wizard_dialog->priv;
739
740     priv->override_func = callback;
741 }
742
743 void
744 modest_wizard_dialog_update_model (ModestWizardDialog *wizard_dialog)
745 {
746         g_return_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog));
747
748         invoke_update_model_vfunc (wizard_dialog);
749 }
750
751 gboolean
752 modest_wizard_dialog_save (ModestWizardDialog *wizard_dialog)
753 {
754         g_return_val_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog), FALSE);
755
756         return invoke_save_vfunc (wizard_dialog);
757 }