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