2008-09-15 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-wizard-dialog.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
7  *   Fixes: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  *
24  */
25
26 /**
27  * SECTION:hildon-wizard-dialog
28  * @short_description: A widget to create a guided installation
29  * process wizard.
30  *
31  * #HildonWizardDialog is a widget to create a guided installation
32  * process. The dialog has four standard buttons, previous, next,
33  * finish, cancel, and contains several pages with optional icons.
34  * Response buttons are dimmed/undimmed automatically and the standard
35  * icon is shown/hidden in response to page navigation. The notebook
36  * widget provided by users contains the actual wizard pages.
37  * 
38  * Using of the API is very simple, it has only one function to create it
39  * and the rest of it is handled by developers notebook.
40  * Also the response is returned, either cancel or finnish.
41  * Next and previous buttons are handled by the wizard dialog it self, by
42  * switching the page either forward or backward in the notebook.
43  */
44
45 #ifdef                                          HAVE_CONFIG_H
46 #include                                        <config.h>
47 #endif
48
49 #include                                        <libintl.h>
50
51 #include                                        "hildon-wizard-dialog.h"
52 #include                                        "hildon-defines.h"
53 #include                                        "hildon-wizard-dialog-private.h"
54
55 #define                                         _(String) dgettext("hildon-libs", String)
56
57 static GtkDialogClass*                          parent_class;
58
59 static void 
60 hildon_wizard_dialog_class_init                 (HildonWizardDialogClass *wizard_dialog_class);
61
62 static void 
63 hildon_wizard_dialog_init                       (HildonWizardDialog *wizard_dialog);
64
65 static void 
66 create_title                                    (HildonWizardDialog *wizard_dialog);
67
68 static void 
69 hildon_wizard_dialog_set_property               (GObject *object,
70                                                  guint property_id,
71                                                  const GValue *value,
72                                                  GParamSpec *pspec);
73
74 static void 
75 hildon_wizard_dialog_get_property               (GObject *object,
76                                                  guint property_id,
77                                                  GValue *value,
78                                                  GParamSpec *pspec);
79
80 static void 
81 finalize                                        (GObject *object);
82
83 static void 
84 response                                        (HildonWizardDialog *wizard, 
85                                                  gint response_id,
86                                                  gpointer unused);
87
88 static void 
89 make_buttons_sensitive                          (HildonWizardDialog *wizard_dialog,
90                                                  gboolean previous,
91                                                  gboolean finish,
92                                                  gboolean next);
93
94 enum 
95 {
96     PROP_0,
97     PROP_NAME,
98     PROP_NOTEBOOK,
99     PROP_AUTOTITLE
100 };
101
102 /**
103  * hildon_wizard_dialog_get_type:
104  *
105  * Initializes and returns the type of a hildon wizard dialog.
106  *
107  * @Returns: GType of #HildonWzardDialog
108  */
109 GType G_GNUC_CONST
110 hildon_wizard_dialog_get_type                   (void)
111 {
112     static GType wizard_dialog_type = 0;
113
114     if (! wizard_dialog_type) {
115
116         static const GTypeInfo wizard_dialog_info = {
117             sizeof (HildonWizardDialogClass),
118             NULL,       /* base_init      */
119             NULL,       /* base_finalize  */
120             (GClassInitFunc) hildon_wizard_dialog_class_init,
121             NULL,       /* class_finalize */
122             NULL,       /* class_data     */
123             sizeof (HildonWizardDialog),
124             0,          /* n_preallocs    */
125             (GInstanceInitFunc) hildon_wizard_dialog_init,
126         };
127
128         wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
129                 "HildonWizardDialog",
130                 &wizard_dialog_info,
131                 0);
132     }
133
134     return wizard_dialog_type;
135 }
136
137 static void
138 hildon_wizard_dialog_class_init                 (HildonWizardDialogClass *wizard_dialog_class)
139 {
140     GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
141
142     parent_class = g_type_class_peek_parent (wizard_dialog_class);
143
144     g_type_class_add_private (wizard_dialog_class, sizeof (HildonWizardDialogPrivate));
145
146     /* Override virtual methods */
147     object_class->set_property = hildon_wizard_dialog_set_property;
148     object_class->get_property = hildon_wizard_dialog_get_property;
149     object_class->finalize     = finalize;
150
151     /**
152      * HildonWizardDialog:wizard-name:
153      *
154      * The name of the wizard.
155      */
156     g_object_class_install_property (object_class, PROP_NAME,
157             g_param_spec_string 
158             ("wizard-name",
159              "Wizard Name",
160              "The name of the HildonWizardDialog",
161              NULL,
162              G_PARAM_READWRITE));
163
164     /**
165      * HildonWizardDialog:wizard-notebook:
166      *
167      * The notebook object, which is used by the HildonWizardDialog.
168      */
169     g_object_class_install_property (object_class, PROP_NOTEBOOK,
170             g_param_spec_object 
171             ("wizard-notebook",
172              "Wizard Notebook",
173              "GtkNotebook object to be used in the "
174              "HildonWizardDialog",
175              GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
176
177     /**
178      * HildonWizardDialog:autotitle
179      *
180      * If the wizard should automatically try to change the window title when changing steps. 
181      * Set to FALSE if you'd like to override the default behaviour. 
182      *
183      * Since: 0.14.5 
184      */
185     g_object_class_install_property (object_class, PROP_AUTOTITLE,
186             g_param_spec_boolean 
187             ("autotitle",
188              "AutoTitle",
189              "If the wizard should autotitle itself",
190              TRUE, 
191              G_PARAM_READWRITE));
192 }
193
194 static void 
195 finalize                                        (GObject *object)
196 {
197     HildonWizardDialogPrivate *priv = HILDON_WIZARD_DIALOG_GET_PRIVATE (object);
198
199     g_assert (priv);
200
201     if (priv->wizard_name != NULL)
202         g_free (priv->wizard_name);
203
204     if (G_OBJECT_CLASS (parent_class)->finalize)
205         G_OBJECT_CLASS (parent_class)->finalize (object);
206 }
207
208 /* Disable or enable the Previous, Next and Finish buttons */
209 static void
210 make_buttons_sensitive                          (HildonWizardDialog *wizard_dialog,
211                                                  gboolean previous,
212                                                  gboolean finish,
213                                                  gboolean next)
214 {
215     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
216             HILDON_WIZARD_DIALOG_PREVIOUS,
217             previous);
218
219     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
220             HILDON_WIZARD_DIALOG_FINISH,
221             finish);
222
223     gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
224             HILDON_WIZARD_DIALOG_NEXT,
225             next);
226 }
227
228 static void 
229 hildon_wizard_dialog_init                       (HildonWizardDialog *wizard_dialog)
230 {
231     /* Initialize private structure for faster member access */
232     HildonWizardDialogPrivate *priv = HILDON_WIZARD_DIALOG_GET_PRIVATE (wizard_dialog);
233     g_assert (priv);
234
235     GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
236
237     /* Init internal widgets */
238     GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
239     gtk_dialog_set_has_separator (dialog, FALSE);
240
241     priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
242     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard", HILDON_ICON_SIZE_WIZARD);
243
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"), HILDON_WIZARD_DIALOG_FINISH);
256     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_previous"), HILDON_WIZARD_DIALOG_PREVIOUS);
257     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_next"), HILDON_WIZARD_DIALOG_NEXT);
258     gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_cancel"), HILDON_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 hildon_wizard_dialog_set_property               (GObject *object, 
273                                                  guint property_id,
274                                                  const GValue *value, 
275                                                  GParamSpec *pspec)
276 {
277     HildonWizardDialogPrivate *priv = HILDON_WIZARD_DIALOG_GET_PRIVATE (object);
278     g_assert (priv);
279
280     switch (property_id) {
281
282         case PROP_AUTOTITLE:
283
284             priv->autotitle = g_value_get_boolean (value);
285
286             if (priv->autotitle && 
287                     priv->wizard_name && 
288                     priv->notebook) 
289                 create_title (HILDON_WIZARD_DIALOG (object));
290             else if (priv->wizard_name)
291                 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
292             break;
293
294         case PROP_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 (HILDON_WIZARD_DIALOG (object));
310             break;
311
312         case PROP_NOTEBOOK: {
313
314             GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
315             g_return_if_fail (book != NULL);
316
317             priv->notebook = book;
318
319             /* Set the default properties for the notebook (disable tabs,
320              * and remove borders) to make it look like a nice wizard widget */
321             gtk_notebook_set_show_tabs (priv->notebook, FALSE);
322             gtk_notebook_set_show_border (priv->notebook, FALSE);
323             gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
324
325             /* Show the notebook so that a gtk_widget_show on the dialog is
326              * all that is required to display the dialog correctly */
327             gtk_widget_show (GTK_WIDGET (priv->notebook));
328
329             /* Update dialog title to reflect current page stats etc */        
330             if (priv->wizard_name && priv->autotitle)
331                 create_title (HILDON_WIZARD_DIALOG (object));
332
333         } break;
334
335         default:
336             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
337             break;
338     }
339 }
340
341 static void
342 hildon_wizard_dialog_get_property               (GObject *object,
343                                                  guint property_id,
344                                                  GValue *value,
345                                                  GParamSpec *pspec)
346 {
347     HildonWizardDialogPrivate *priv = HILDON_WIZARD_DIALOG_GET_PRIVATE (object);
348     g_assert (priv);
349
350     switch (property_id) {
351
352         case PROP_NAME:
353             g_value_set_string (value, priv->wizard_name);
354             break;
355
356         case PROP_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 /* Creates the title of the dialog taking into account the current 
367  * page of the notebook. */
368 static void
369 create_title                                    (HildonWizardDialog *wizard_dialog)
370 {
371     gint pages, current;
372     gchar *str = NULL;
373     HildonWizardDialogPrivate *priv = HILDON_WIZARD_DIALOG_GET_PRIVATE (wizard_dialog);
374     GtkNotebook *notebook;
375
376     g_assert (priv);
377     notebook = priv->notebook;
378
379     if (! notebook)
380         return;
381
382     /* Get page information, we'll need that when creating title */
383     pages = gtk_notebook_get_n_pages (notebook);
384     current = gtk_notebook_get_current_page (priv->notebook);
385     if (current < 0)
386         current = 0;
387
388     /* the welcome title on the initial page */
389     if (current == 0) {
390         str = g_strdup_printf (_("ecdg_ti_wizard_welcome"), 
391                 priv->wizard_name, pages);
392     } else {
393         const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
394                 gtk_notebook_get_nth_page (notebook, current));
395
396         str = g_strdup_printf (_("ecdg_ti_wizard_step"), 
397                 priv->wizard_name, current + 1, pages, steps);
398     }
399
400     /* Update the dialog to display the generated title */
401     gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
402     g_free (str);
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 static void 
412 response                                        (HildonWizardDialog *wizard_dialog,
413                                                  gint response_id,
414                                                  gpointer unused)
415 {
416     HildonWizardDialogPrivate *priv = HILDON_WIZARD_DIALOG_GET_PRIVATE (wizard_dialog);
417     GtkNotebook *notebook = priv->notebook;
418     gint current = 0;
419     gint last = gtk_notebook_get_n_pages (notebook) - 1;
420     gboolean is_first, is_last;
421
422     g_assert (priv);
423
424     current = gtk_notebook_current_page (notebook);
425
426     switch (response_id) {
427
428         case HILDON_WIZARD_DIALOG_PREVIOUS:
429             --current;
430             is_last = (current == last);
431             is_first = (current == 0);
432             make_buttons_sensitive (wizard_dialog,
433                                     !is_first, !is_first, !is_last); 
434             gtk_notebook_prev_page (notebook); /* go to previous page */
435             break;
436
437         case HILDON_WIZARD_DIALOG_NEXT:
438             ++current;
439             is_last = (current == last);
440             is_first = (current == 0);
441             make_buttons_sensitive (wizard_dialog,
442                                     !is_first, !is_first, !is_last);
443             gtk_notebook_next_page (notebook); /* go to next page */
444             break;
445
446         case HILDON_WIZARD_DIALOG_CANCEL:      
447         case HILDON_WIZARD_DIALOG_FINISH:      
448             return;
449
450     }
451
452     current = gtk_notebook_get_current_page (notebook);
453     is_last = current == last;
454     is_first = current == 0;
455
456     /* Don't let the dialog close */
457     g_signal_stop_emission_by_name (wizard_dialog, "response");
458
459     /* We show the default image on first and last pages */
460     if (current == last || current == 0)
461         gtk_widget_show (GTK_WIDGET(priv->image));
462     else
463         gtk_widget_hide (GTK_WIDGET(priv->image));
464
465     /* New page number may appear in the title, update it */
466     if (priv->autotitle) 
467         create_title (wizard_dialog);
468 }
469
470 /**
471  * hildon_wizard_dialog_new:
472  * @parent: a #GtkWindow
473  * @wizard_name: the name of dialog
474  * @notebook: the notebook to be shown on the dialog
475  *
476  * Creates a new #HildonWizardDialog.
477  *
478  * Returns: a new #HildonWizardDialog
479  */
480 GtkWidget*
481 hildon_wizard_dialog_new                        (GtkWindow *parent,
482                                                  const char *wizard_name,
483                                                  GtkNotebook *notebook)
484 {
485     GtkWidget *widget;
486
487     g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
488
489     widget = GTK_WIDGET (g_object_new
490             (HILDON_TYPE_WIZARD_DIALOG,
491              "wizard-name", wizard_name,
492              "wizard-notebook", notebook, NULL));
493
494     if (parent)
495         gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
496
497     return widget;
498 }
499