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