2 * This is a copy of modest-wizard-dialog.h with a rename and some API additions,
3 * for osso-modest-easysetup.
5 * This file was part of modest-libs
7 * Copyright (C) 2005, 2006, 2007 Nokia Corporation, all rights reserved.
12 * SECTION:modest-wizard-dialog
13 * @short_description: A widget to create a guided installation
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.
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 #include <hildon-widgets/hildon-defines.h>
33 #include "modest-wizard-dialog.h"
41 /* Specify the hildon-libs translation domain,
42 * so we can reuse its translations
43 * instead of repeating them in our own translations.
45 /* #define _(String) dgettext(PACKAGE, String) */
47 #define _(String) dgettext("hildon-libs", String)
49 static GtkDialogClass *parent_class;
51 static void class_init (ModestWizardDialogClass *wizard_dialog_class);
53 static void init (ModestWizardDialog *wizard_dialog);
55 static void create_title (ModestWizardDialog *wizard_dialog);
57 static void set_property (GObject *object,
62 static void get_property (GObject *object,
67 static void finalize (GObject *object);
69 static void response (ModestWizardDialog *wizard,
73 static void make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
78 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
79 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
88 struct _ModestWizardDialogPrivate {
90 GtkNotebook *notebook;
98 modest_wizard_dialog_get_type (void)
100 static GType wizard_dialog_type = 0;
102 if (!wizard_dialog_type) {
104 static const GTypeInfo wizard_dialog_info = {
105 sizeof (ModestWizardDialogClass),
106 NULL, /* base_init */
107 NULL, /* base_finalize */
108 (GClassInitFunc) class_init,
109 NULL, /* class_finalize */
110 NULL, /* class_data */
111 sizeof (ModestWizardDialog),
113 (GInstanceInitFunc) init,
116 wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
117 "ModestWizardDialog",
122 return wizard_dialog_type;
126 class_init (ModestWizardDialogClass *wizard_dialog_class)
128 GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
130 parent_class = g_type_class_peek_parent (wizard_dialog_class);
132 g_type_class_add_private (wizard_dialog_class,
133 sizeof(ModestWizardDialogPrivate));
135 /* Override virtual methods */
136 object_class->set_property = set_property;
137 object_class->get_property = get_property;
138 object_class->finalize = finalize;
141 * ModestWizardDialog:wizard-name:
143 * The name of the wizard.
145 g_object_class_install_property (object_class, PROP_WIZARD_NAME,
149 "The name of the ModestWizardDialog",
154 * ModestWizardDialog:wizard-notebook:
156 * The notebook object, which is used by the ModestWizardDialog.
158 g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
162 "GtkNotebook object to be used in the "
163 "ModestWizardDialog",
164 GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
167 * ModestWizardDialog:autotitle
169 * If the wizard should automatically try to change the window title when changing steps.
170 * Set to FALSE if you'd like to override the default behaviour.
174 g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
178 "If the wizard should autotitle itself",
184 finalize (GObject *object)
186 ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
187 g_return_if_fail (dialog != NULL);
189 if (dialog->priv->wizard_name != NULL)
190 g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
192 if (G_OBJECT_CLASS (parent_class)->finalize)
193 G_OBJECT_CLASS (parent_class)->finalize(object);
196 /* Disable or enable the Previous, Next and Finish buttons */
198 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
203 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
204 MODEST_WIZARD_DIALOG_PREVIOUS,
207 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
208 MODEST_WIZARD_DIALOG_FINISH,
211 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
212 MODEST_WIZARD_DIALOG_NEXT,
217 init (ModestWizardDialog *wizard_dialog)
219 /* Initialize private structure for faster member access */
220 ModestWizardDialogPrivate *priv =
221 G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
222 MODEST_TYPE_WIZARD_DIALOG,
223 ModestWizardDialogPrivate);
225 GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
227 /* Init internal widgets */
228 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
229 gtk_dialog_set_has_separator (dialog, FALSE);
230 wizard_dialog->priv = priv;
231 priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
232 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
233 HILDON_ICON_SIZE_WIDG_WIZARD);
235 /* Default values for user provided properties */
236 priv->notebook = NULL;
237 priv->wizard_name = NULL;
238 priv->autotitle = TRUE;
240 /* Build wizard layout */
241 gtk_box_pack_start_defaults (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box));
242 gtk_box_pack_start_defaults (GTK_BOX (priv->box), GTK_WIDGET (vbox));
243 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), FALSE, FALSE, 0);
245 /* Add response buttons: finish, previous, next, cancel */
246 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
247 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
248 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
249 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
251 /* Set initial button states: previous and finish buttons are disabled */
252 make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
254 /* Show all the internal widgets */
255 gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
257 /* connect to dialog's response signal */
258 g_signal_connect (G_OBJECT (dialog), "response",
259 G_CALLBACK (response), NULL);
263 set_property (GObject *object,
268 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
270 switch (property_id) {
272 case PROP_WIZARD_AUTOTITLE:
274 priv->autotitle = g_value_get_boolean (value);
276 if (priv->autotitle &&
279 create_title (MODEST_WIZARD_DIALOG (object));
280 else if (priv->wizard_name)
281 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
285 case PROP_WIZARD_NAME:
287 /* Set new wizard name. This name will appear in titlebar */
288 if (priv->wizard_name)
289 g_free (priv->wizard_name);
291 gchar *str = (gchar *) g_value_get_string (value);
292 g_return_if_fail (str != NULL);
294 priv->wizard_name = g_strdup (str);
296 /* We need notebook in order to create title, since page information
297 is used in title generation */
299 if (priv->notebook && priv->autotitle)
300 create_title (MODEST_WIZARD_DIALOG (object));
304 case PROP_WIZARD_NOTEBOOK: {
306 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
307 g_return_if_fail (book != NULL);
309 priv->notebook = book;
311 /* Set the default properties for the notebook (disable tabs,
312 * and remove borders) to make it look like a nice wizard widget */
313 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
314 gtk_notebook_set_show_border (priv->notebook, FALSE);
315 gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
317 /* Show the notebook so that a gtk_widget_show on the dialog is
318 * all that is required to display the dialog correctly */
319 gtk_widget_show ( GTK_WIDGET (priv->notebook));
321 /* Update dialog title to reflect current page stats etc */
322 if (priv->wizard_name && priv->autotitle)
323 create_title (MODEST_WIZARD_DIALOG (object));
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
334 get_property (GObject *object,
339 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
341 switch (property_id) {
343 case PROP_WIZARD_NAME:
344 g_value_set_string (value, priv->wizard_name);
347 case PROP_WIZARD_NOTEBOOK:
348 g_value_set_object (value, priv->notebook);
352 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
358 * Creates the title of the dialog taking into account the current
359 * page of the notebook.
362 create_title (ModestWizardDialog *wizard_dialog)
366 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
367 GtkNotebook *notebook = priv->notebook;
372 /* Get page information, we'll need that when creating title */
373 pages = gtk_notebook_get_n_pages (notebook);
374 current = gtk_notebook_get_current_page (priv->notebook);
378 /* the welcome title on the initial page */
380 str = g_strdup_printf (_("ecdg_ti_wizard_welcome"),
381 priv->wizard_name, pages);
383 const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
384 gtk_notebook_get_nth_page (notebook, current));
386 str = g_strdup_printf (_("ecdg_ti_wizard_step"),
387 priv->wizard_name, current + 1, pages, steps);
390 /* Update the dialog to display the generated title */
391 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
396 * Response signal handler. This function is needed because GtkDialog's
397 * handler for this signal closes the dialog and we don't want that, we
398 * want to change pages and, dimm certain response buttons. Overriding the
399 * virtual function would not work because that would be called after the
400 * signal handler implemented by GtkDialog.
401 * FIXME: There is a much saner way to do that [MDK]
404 response (ModestWizardDialog *wizard_dialog,
408 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
409 GtkNotebook *notebook = priv->notebook;
411 gboolean is_first, is_last;
413 switch (response_id) {
415 case MODEST_WIZARD_DIALOG_PREVIOUS:
416 gtk_notebook_prev_page (notebook); /* go to previous page */
419 case MODEST_WIZARD_DIALOG_NEXT:
420 if (invoke_before_next_vfunc (wizard_dialog))
421 gtk_notebook_next_page (notebook); /* go to next page */
425 case MODEST_WIZARD_DIALOG_CANCEL:
428 case MODEST_WIZARD_DIALOG_FINISH:
429 if (invoke_before_next_vfunc (wizard_dialog))
436 current = gtk_notebook_get_current_page (notebook);
437 gint last = gtk_notebook_get_n_pages (notebook) - 1;
438 is_last = current == last;
439 is_first = current == 0;
441 /* If first page, previous and finish are disabled,
442 if last page, next is disabled */
443 make_buttons_sensitive (wizard_dialog,
444 !is_first /* previous */, !is_first /* finish */, !is_last /* next*/);
446 /* Allow derived classes to disable buttons to prevent navigation,
447 * according to their own validation logic: */
448 invoke_enable_buttons_vfunc (wizard_dialog);
450 /* Don't let the dialog close */
451 g_signal_stop_emission_by_name (wizard_dialog, "response");
453 /* We show the default image on first and last pages */
454 last = gtk_notebook_get_n_pages (notebook) - 1;
455 if (current == last || current == 0)
456 gtk_widget_show (GTK_WIDGET(priv->image));
458 gtk_widget_hide (GTK_WIDGET(priv->image));
460 /* New page number may appear in the title, update it */
462 create_title (wizard_dialog);
466 * modest_wizard_dialog_new:
467 * @parent: a #GtkWindow
468 * @wizard_name: the name of dialog
469 * @notebook: the notebook to be shown on the dialog
471 * Creates a new #ModestWizardDialog.
473 * Returns: a new #ModestWizardDialog
476 modest_wizard_dialog_new (GtkWindow *parent,
477 const char *wizard_name,
478 GtkNotebook *notebook)
482 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
484 widget = GTK_WIDGET (g_object_new
485 (MODEST_TYPE_WIZARD_DIALOG,
486 "wizard-name", wizard_name,
487 "wizard-notebook", notebook, NULL));
490 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
496 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
498 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
500 /* Call the vfunc, which may be overridden by derived classes: */
501 if (klass->before_next) {
502 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
504 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
506 /* Get widgets for the two pages: */
507 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
509 GtkWidget* next_page_widget = NULL;
510 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
511 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
513 /* Ask the vfunc implementation whether navigation should be allowed: */
514 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
517 /* Allow navigation by default if there is no vfunc implementation: */
522 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
524 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
526 /* Call the vfunc, which may be overridden by derived classes: */
527 if (klass->enable_buttons) {
528 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
530 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
532 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
534 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);