486da9198d17d532c49a352297f7de2827ef525a
[modest] / src / maemo / easysetup / modest-wizard-dialog.c
1 /*
2  * This is a copy of modest-wizard-dialog.h with a rename and some API additions,
3  * for osso-modest-easysetup.
4  * 
5  * This file was part of modest-libs
6  *
7  * Copyright (C) 2005, 2006, 2007 Nokia Corporation, all rights reserved.
8  *
9  */
10  
11 /**
12  * SECTION:modest-wizard-dialog
13  * @short_description: A widget to create a guided installation
14  * process wizard
15  *
16  * #ModestWizardDialog is a widget to create a guided installation
17  * process. The dialog has four standard buttons, previous, next,
18  * finish, cancel, and contains several pages with optional icons.
19  * Response buttons are dimmed/undimmed automatically and the standard
20  * icon is shown/hidden in response to page navigation. The notebook
21  * widget provided by users contains the actual wizard pages.
22  */
23
24 #include <config.h>
25 #include <gtk/gtkdialog.h>
26 #include <gtk/gtknotebook.h>
27 #include <gtk/gtkimage.h>
28 #include <gtk/gtkbox.h>
29 #include <gtk/gtkhbox.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtkbutton.h>
32 #include <gtk/gtk.h>
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef MODEST_HILDON_VERSION_0
39 #include <hildon-widgets/hildon-defines.h>
40 #else
41 #include <hildon/hildon-defines.h>
42 #endif /*MODEST_HILDON_VERSION_0*/
43
44 #include "modest-wizard-dialog.h"
45
46 #include <libintl.h>
47
48 /* Specify the hildon-libs translation domain,
49  * so we can reuse its translations 
50  * instead of repeating them in our own translations.
51  */
52 /* #define _(String) dgettext(PACKAGE, String) */
53
54 #define _(String) dgettext("hildon-libs", String)
55
56 static GtkDialogClass *parent_class;
57
58 static void class_init              (ModestWizardDialogClass   *wizard_dialog_class);
59
60 static void init                    (ModestWizardDialog        *wizard_dialog);
61
62 static void create_title            (ModestWizardDialog        *wizard_dialog);
63
64 static void set_property            (GObject                   *object,
65                                      guint                     property_id,
66                                      const GValue              *value,
67                                      GParamSpec                *pspec);
68
69 static void get_property            (GObject                   *object,
70                                      guint                     property_id,
71                                      GValue                    *value,
72                                      GParamSpec                *pspec);
73
74 static void finalize                (GObject                   *object);
75
76 static void response                (ModestWizardDialog        *wizard, 
77                                      gint                      response_id,
78                                      gpointer                  unused);
79
80 static void make_buttons_sensitive  (ModestWizardDialog *wizard_dialog,
81                                      gboolean           previous,
82                                      gboolean           finish,
83                                      gboolean next);
84                                      
85 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
86 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
87
88 enum {
89     PROP_ZERO,
90     PROP_WIZARD_NAME,
91     PROP_WIZARD_NOTEBOOK,
92     PROP_WIZARD_AUTOTITLE
93 };
94
95 struct _ModestWizardDialogPrivate {
96     gchar       *wizard_name;
97     GtkNotebook *notebook;
98     GtkBox      *box;
99     GtkWidget   *image;
100     gboolean    autotitle;
101 };
102
103
104 GType
105 modest_wizard_dialog_get_type (void)
106 {
107     static GType wizard_dialog_type = 0;
108
109     if (!wizard_dialog_type) {
110
111         static const GTypeInfo wizard_dialog_info = {
112             sizeof (ModestWizardDialogClass),
113             NULL,       /* base_init      */
114             NULL,       /* base_finalize  */
115             (GClassInitFunc) class_init,
116             NULL,       /* class_finalize */
117             NULL,       /* class_data     */
118             sizeof (ModestWizardDialog),
119             0,          /* n_preallocs    */
120             (GInstanceInitFunc) init,
121         };
122
123         wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
124                                                      "ModestWizardDialog",
125                                                      &wizard_dialog_info,
126                                                      0);
127     }
128
129     return wizard_dialog_type;
130 }
131
132 static void
133 class_init (ModestWizardDialogClass *wizard_dialog_class)
134 {
135     GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
136
137     parent_class = g_type_class_peek_parent (wizard_dialog_class);
138
139     g_type_class_add_private (wizard_dialog_class,
140                               sizeof(ModestWizardDialogPrivate));
141
142     /* Override virtual methods */
143     object_class->set_property = set_property;
144     object_class->get_property = get_property;
145     object_class->finalize     = finalize;
146
147     /**
148      * ModestWizardDialog:wizard-name:
149      *
150      * The name of the wizard.
151      */
152     g_object_class_install_property (object_class, PROP_WIZARD_NAME,
153             g_param_spec_string 
154             ("wizard-name",
155              "Wizard Name",
156              "The name of the ModestWizardDialog",
157              NULL,
158              G_PARAM_READWRITE));
159
160     /**
161      * ModestWizardDialog:wizard-notebook:
162      *
163      * The notebook object, which is used by the ModestWizardDialog.
164      */
165     g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
166             g_param_spec_object 
167             ("wizard-notebook",
168              "Wizard Notebook",
169              "GtkNotebook object to be used in the "
170              "ModestWizardDialog",
171              GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
172
173     /**
174      * ModestWizardDialog:autotitle
175      *
176      * If the wizard should automatically try to change the window title when changing steps. 
177      * Set to FALSE if you'd like to override the default behaviour. 
178      *
179      * Since: 0.14.5 
180      */
181     g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
182             g_param_spec_boolean 
183             ("autotitle",
184              "AutoTitle",
185              "If the wizard should autotitle itself",
186              TRUE, 
187              G_PARAM_READWRITE));
188 }
189
190 static void 
191 finalize (GObject *object)
192 {
193     ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
194     g_return_if_fail (dialog != NULL);
195
196     if (dialog->priv->wizard_name != NULL)
197         g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
198     
199     if (G_OBJECT_CLASS (parent_class)->finalize)
200         G_OBJECT_CLASS (parent_class)->finalize(object);
201 }
202
203 /* Disable or enable the Previous, Next and Finish buttons */
204 static void
205 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
206                         gboolean previous,
207                         gboolean finish,
208                         gboolean next)
209 {
210     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
211                                        MODEST_WIZARD_DIALOG_PREVIOUS,
212                                        previous);
213
214     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
215                                        MODEST_WIZARD_DIALOG_FINISH,
216                                        finish);
217
218     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
219                                        MODEST_WIZARD_DIALOG_NEXT,
220                                        next);
221 }
222
223 static void 
224 init (ModestWizardDialog *wizard_dialog)
225 {
226     /* Initialize private structure for faster member access */
227     ModestWizardDialogPrivate *priv =
228         G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
229                 MODEST_TYPE_WIZARD_DIALOG,
230                 ModestWizardDialogPrivate);
231
232     GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
233
234     /* Init internal widgets */
235     GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
236     gtk_dialog_set_has_separator (dialog, FALSE);
237     wizard_dialog->priv = priv;
238     priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
239 #ifdef MODEST_HILDON_VERSION_0
240     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
241                                                 HILDON_ICON_SIZE_WIDG_WIZARD);
242 #else
243     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
244                                                 HILDON_ICON_SIZE_WIZARD);
245 #endif /*MODEST_HILDON_VERSION_0*/
246     /* Default values for user provided properties */
247     priv->notebook = NULL;
248     priv->wizard_name = NULL;
249     priv->autotitle = TRUE;
250
251     /* Build wizard layout */
252     gtk_box_pack_start_defaults (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box));
253     gtk_box_pack_start_defaults (GTK_BOX (priv->box), GTK_WIDGET (vbox));
254     gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), FALSE, FALSE, 0);
255
256     /* Add response buttons: finish, previous, next, cancel */
257     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
258     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
259     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
260     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
261
262     /* Set initial button states: previous and finish buttons are disabled */
263     make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
264
265     /* Show all the internal widgets */
266     gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
267
268     /* connect to dialog's response signal */
269     g_signal_connect (G_OBJECT (dialog), "response",
270             G_CALLBACK (response), NULL);
271 }
272
273 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
274 static void on_notebook_page_added(GtkNotebook *notebook, 
275                                    GtkWidget   *child,
276                                    guint        page_num,
277                                    gpointer     user_data)
278 {
279         ModestWizardDialog* dialog = NULL;
280
281         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
282         dialog = MODEST_WIZARD_DIALOG(user_data);
283
284         /* The title should show the total number of pages: */
285         create_title (dialog);
286 }
287
288 static void on_notebook_page_removed(GtkNotebook *notebook, 
289                                      GtkWidget   *child,
290                                      guint        page_num,
291                                      gpointer     user_data)
292 {
293         ModestWizardDialog* dialog = NULL;
294
295         g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
296         dialog = MODEST_WIZARD_DIALOG(user_data);
297
298         /* The title should show the total number of pages: */
299         create_title (dialog);
300 }
301 #endif /* GTK_CHECK_VERSION */
302
303 static void
304 connect_to_notebook_signals(ModestWizardDialog* dialog)
305 {
306 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
307         ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
308         g_return_if_fail (priv->notebook);
309         
310         /* Connect to the notebook signals,
311          * so we can update the title when necessary: */
312         g_signal_connect (G_OBJECT (priv->notebook), "page-added",
313                       G_CALLBACK (on_notebook_page_added), dialog);
314         g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
315                       G_CALLBACK (on_notebook_page_removed), dialog);
316 #endif /* GTK_CHECK_VERSION */
317 }
318
319
320 static void
321 set_property (GObject      *object, 
322               guint        property_id,
323               const GValue *value, 
324               GParamSpec   *pspec)
325 {
326     ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
327
328     switch (property_id) {
329
330         case PROP_WIZARD_AUTOTITLE:
331
332             priv->autotitle = g_value_get_boolean (value);
333
334             if (priv->autotitle && 
335                 priv->wizard_name && 
336                 priv->notebook)
337                 create_title (MODEST_WIZARD_DIALOG (object));
338             else if (priv->wizard_name)
339                 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
340             
341             break;
342
343         case PROP_WIZARD_NAME: 
344
345             /* Set new wizard name. This name will appear in titlebar */
346             if (priv->wizard_name)
347                 g_free (priv->wizard_name);
348
349             gchar *str = (gchar *) g_value_get_string (value);
350             g_return_if_fail (str != NULL);
351
352             priv->wizard_name = g_strdup (str);
353
354             /* We need notebook in order to create title, since page information
355                is used in title generation */
356             
357             if (priv->notebook && priv->autotitle)
358                 create_title (MODEST_WIZARD_DIALOG (object));
359     
360             break;
361
362         case PROP_WIZARD_NOTEBOOK: {
363
364             GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
365             g_return_if_fail (book != NULL);
366
367             priv->notebook = book;
368
369             /* Set the default properties for the notebook (disable tabs,
370              * and remove borders) to make it look like a nice wizard widget */
371             gtk_notebook_set_show_tabs (priv->notebook, FALSE);
372             gtk_notebook_set_show_border (priv->notebook, FALSE);
373             gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
374
375             /* Show the notebook so that a gtk_widget_show on the dialog is
376              * all that is required to display the dialog correctly */
377             gtk_widget_show ( GTK_WIDGET (priv->notebook));
378
379             /* Update dialog title to reflect current page stats etc */ 
380             ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);      
381             if (priv->wizard_name && priv->autotitle)
382                 create_title (wizard_dialog);
383                 
384             connect_to_notebook_signals (wizard_dialog);
385             
386             }break;
387
388         default:
389             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
390             break;
391     }
392 }
393
394 static void
395 get_property (GObject      *object,
396               guint        property_id,
397               GValue       *value,
398               GParamSpec   *pspec)
399 {
400     ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
401
402     switch (property_id) {
403
404         case PROP_WIZARD_NAME:
405             g_value_set_string (value, priv->wizard_name);
406             break;
407
408         case PROP_WIZARD_NOTEBOOK:
409             g_value_set_object (value, priv->notebook);
410             break;
411
412         default:
413             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
414             break;
415     }
416 }
417
418 /*
419  * Creates the title of the dialog taking into account the current 
420  * page of the notebook.
421  */
422 static void
423 create_title (ModestWizardDialog *wizard_dialog)
424 {
425     gint pages, current;
426     gchar *str = NULL;
427     ModestWizardDialogPrivate *priv = NULL;
428     GtkNotebook *notebook = NULL;
429
430     /* FIXME (jfernandez): Sometines, priv is NULL */
431     g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
432     g_return_if_fail (wizard_dialog->priv != NULL);
433
434     priv = wizard_dialog->priv;    
435     notebook = priv->notebook;
436
437     if (!notebook)
438         return;
439
440     /* Get page information, we'll need that when creating title */
441     pages = gtk_notebook_get_n_pages (notebook);
442     current = gtk_notebook_get_current_page (priv->notebook);
443     if (current < 0)
444         current = 0;
445
446     /* the welcome title on the initial page */
447     if (current == 0) {
448         str = g_strdup_printf (_("ecdg_ti_wizard_welcome"), 
449                 priv->wizard_name, pages);
450     } else {
451         const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
452                 gtk_notebook_get_nth_page (notebook, current));
453
454         str = g_strdup_printf (_("ecdg_ti_wizard_step"), 
455                 priv->wizard_name, current + 1, pages, steps);
456     }
457
458     /* Update the dialog to display the generated title */
459     gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
460     g_free (str);
461 }
462
463 /*
464  * Response signal handler. This function is needed because GtkDialog's 
465  * handler for this signal closes the dialog and we don't want that, we 
466  * want to change pages and, dimm certain response buttons. Overriding the 
467  * virtual function would not work because that would be called after the 
468  * signal handler implemented by GtkDialog.
469  * FIXME: There is a much saner way to do that [MDK]
470  */
471 static void 
472 response (ModestWizardDialog   *wizard_dialog,
473           gint                 response_id,
474           gpointer             unused)
475 {
476     ModestWizardDialogPrivate *priv = wizard_dialog->priv;
477     GtkNotebook *notebook = priv->notebook;
478     gint current = 0;
479     gboolean is_first, is_last;
480     
481     switch (response_id) {
482         
483         case MODEST_WIZARD_DIALOG_PREVIOUS:
484             gtk_notebook_prev_page (notebook); /* go to previous page */
485             break;
486
487         case MODEST_WIZARD_DIALOG_NEXT:
488                 if (invoke_before_next_vfunc (wizard_dialog))
489                 gtk_notebook_next_page (notebook); /* go to next page */
490                 
491             break;
492
493         case MODEST_WIZARD_DIALOG_CANCEL:
494                 return;
495                 break;      
496         case MODEST_WIZARD_DIALOG_FINISH:
497                 if (invoke_before_next_vfunc (wizard_dialog))
498                 return;
499             
500             break;
501
502     }
503
504     current = gtk_notebook_get_current_page (notebook);
505     gint last = gtk_notebook_get_n_pages (notebook) - 1;
506     is_last = current == last;
507     is_first = current == 0;
508     
509     /* If first page, previous and finish are disabled, 
510        if last page, next is disabled */
511     make_buttons_sensitive (wizard_dialog,
512             !is_first /* previous */, !is_first /* finish */, !is_last /* next*/);
513             
514     /* Allow derived classes to disable buttons to prevent navigation,
515      * according to their own validation logic: */
516     invoke_enable_buttons_vfunc (wizard_dialog);
517     
518     /* Don't let the dialog close */
519     g_signal_stop_emission_by_name (wizard_dialog, "response");
520
521     /* We show the default image on first and last pages */
522     last = gtk_notebook_get_n_pages (notebook) - 1;
523     if (current == last || current == 0)
524         gtk_widget_show (GTK_WIDGET(priv->image));
525     else
526         gtk_widget_hide (GTK_WIDGET(priv->image));
527
528     /* New page number may appear in the title, update it */
529     if (priv->autotitle) 
530         create_title (wizard_dialog);
531 }
532
533 /**
534  * modest_wizard_dialog_new:
535  * @parent: a #GtkWindow
536  * @wizard_name: the name of dialog
537  * @notebook: the notebook to be shown on the dialog
538  *
539  * Creates a new #ModestWizardDialog.
540  *
541  * Returns: a new #ModestWizardDialog
542  */
543 GtkWidget*
544 modest_wizard_dialog_new (GtkWindow   *parent,
545                           const char  *wizard_name,
546                           GtkNotebook *notebook)
547 {
548     GtkWidget *widget;
549
550     g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
551
552     widget = GTK_WIDGET (g_object_new
553             (MODEST_TYPE_WIZARD_DIALOG,
554              "wizard-name", wizard_name,
555              "wizard-notebook", notebook, NULL));
556
557     if (parent)
558         gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
559
560     return widget;
561 }
562
563 /**
564  * modest_wizard_dialog_force_title_update:
565  * @wizard_dialog: The wizard dialog
566  *
567  * Force the title to be rebuilt, for instance when you have added or 
568  * removed notebook pages. This function is not necessary when using GTK+ 2.10, 
569  * because that has GtkNotebook signals that will be used to update the title 
570  * automatically.
571  */
572 void
573 modest_wizard_dialog_force_title_update (ModestWizardDialog   *wizard_dialog)
574 {
575         create_title (wizard_dialog);
576 }
577
578 static gboolean
579 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
580 {
581         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
582         
583         /* Call the vfunc, which may be overridden by derived classes: */
584         if (klass->before_next) {
585                 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
586         
587                 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
588                 
589                 /* Get widgets for the two pages: */
590                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
591                 
592                 GtkWidget* next_page_widget = NULL;
593                 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
594                         next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
595                 
596                 /* Ask the vfunc implementation whether navigation should be allowed: */
597                 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
598         }
599         
600         /* Allow navigation by default if there is no vfunc implementation: */
601         return TRUE;
602 }
603
604 static void
605 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
606 {
607         ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
608         
609         /* Call the vfunc, which may be overridden by derived classes: */
610         if (klass->enable_buttons) {
611                 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
612         
613                 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
614                 
615                 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
616                         
617                 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);
618         }
619 }