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