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