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>
36 #ifdef MODEST_HILDON_VERSION_0
37 #include <hildon-widgets/hildon-defines.h>
39 #include <hildon/hildon-defines.h>
40 #endif /*MODEST_HILDON_VERSION_0*/
42 #include "modest-wizard-dialog.h"
46 /* Specify the hildon-libs translation domain,
47 * so we can reuse its translations
48 * instead of repeating them in our own translations.
50 /* #define _(String) dgettext(PACKAGE, String) */
52 #define _(String) dgettext("hildon-libs", String)
54 static GtkDialogClass *parent_class;
56 static void class_init (ModestWizardDialogClass *wizard_dialog_class);
58 static void init (ModestWizardDialog *wizard_dialog);
60 static void create_title (ModestWizardDialog *wizard_dialog);
62 static void set_property (GObject *object,
67 static void get_property (GObject *object,
72 static void finalize (GObject *object);
74 static void response (ModestWizardDialog *wizard,
78 static void make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
83 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
84 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
93 struct _ModestWizardDialogPrivate {
95 GtkNotebook *notebook;
103 modest_wizard_dialog_get_type (void)
105 static GType wizard_dialog_type = 0;
107 if (!wizard_dialog_type) {
109 static const GTypeInfo wizard_dialog_info = {
110 sizeof (ModestWizardDialogClass),
111 NULL, /* base_init */
112 NULL, /* base_finalize */
113 (GClassInitFunc) class_init,
114 NULL, /* class_finalize */
115 NULL, /* class_data */
116 sizeof (ModestWizardDialog),
118 (GInstanceInitFunc) init,
121 wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
122 "ModestWizardDialog",
127 return wizard_dialog_type;
131 class_init (ModestWizardDialogClass *wizard_dialog_class)
133 GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
135 parent_class = g_type_class_peek_parent (wizard_dialog_class);
137 g_type_class_add_private (wizard_dialog_class,
138 sizeof(ModestWizardDialogPrivate));
140 /* Override virtual methods */
141 object_class->set_property = set_property;
142 object_class->get_property = get_property;
143 object_class->finalize = finalize;
146 * ModestWizardDialog:wizard-name:
148 * The name of the wizard.
150 g_object_class_install_property (object_class, PROP_WIZARD_NAME,
154 "The name of the ModestWizardDialog",
159 * ModestWizardDialog:wizard-notebook:
161 * The notebook object, which is used by the ModestWizardDialog.
163 g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
167 "GtkNotebook object to be used in the "
168 "ModestWizardDialog",
169 GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
172 * ModestWizardDialog:autotitle
174 * If the wizard should automatically try to change the window title when changing steps.
175 * Set to FALSE if you'd like to override the default behaviour.
179 g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
183 "If the wizard should autotitle itself",
189 finalize (GObject *object)
191 ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
192 g_return_if_fail (dialog != NULL);
194 if (dialog->priv->wizard_name != NULL)
195 g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
197 if (G_OBJECT_CLASS (parent_class)->finalize)
198 G_OBJECT_CLASS (parent_class)->finalize(object);
201 /* Disable or enable the Previous, Next and Finish buttons */
203 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
208 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
209 MODEST_WIZARD_DIALOG_PREVIOUS,
212 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
213 MODEST_WIZARD_DIALOG_FINISH,
216 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
217 MODEST_WIZARD_DIALOG_NEXT,
222 init (ModestWizardDialog *wizard_dialog)
224 /* Initialize private structure for faster member access */
225 ModestWizardDialogPrivate *priv =
226 G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
227 MODEST_TYPE_WIZARD_DIALOG,
228 ModestWizardDialogPrivate);
230 GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
232 /* Init internal widgets */
233 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
234 gtk_dialog_set_has_separator (dialog, FALSE);
235 wizard_dialog->priv = priv;
236 priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
237 #ifdef MODEST_HILDON_VERSION_0
238 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
239 HILDON_ICON_SIZE_WIDG_WIZARD);
241 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
242 HILDON_ICON_SIZE_WIZARD);
243 #endif /*MODEST_HILDON_VERSION_0*/
244 /* Default values for user provided properties */
245 priv->notebook = NULL;
246 priv->wizard_name = NULL;
247 priv->autotitle = TRUE;
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);
254 /* Add response buttons: finish, previous, next, cancel */
255 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
256 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
257 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
258 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
260 /* Set initial button states: previous and finish buttons are disabled */
261 make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
263 /* Show all the internal widgets */
264 gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
266 /* connect to dialog's response signal */
267 g_signal_connect (G_OBJECT (dialog), "response",
268 G_CALLBACK (response), NULL);
272 set_property (GObject *object,
277 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
279 switch (property_id) {
281 case PROP_WIZARD_AUTOTITLE:
283 priv->autotitle = g_value_get_boolean (value);
285 if (priv->autotitle &&
288 create_title (MODEST_WIZARD_DIALOG (object));
289 else if (priv->wizard_name)
290 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
294 case PROP_WIZARD_NAME:
296 /* Set new wizard name. This name will appear in titlebar */
297 if (priv->wizard_name)
298 g_free (priv->wizard_name);
300 gchar *str = (gchar *) g_value_get_string (value);
301 g_return_if_fail (str != NULL);
303 priv->wizard_name = g_strdup (str);
305 /* We need notebook in order to create title, since page information
306 is used in title generation */
308 if (priv->notebook && priv->autotitle)
309 create_title (MODEST_WIZARD_DIALOG (object));
313 case PROP_WIZARD_NOTEBOOK: {
315 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
316 g_return_if_fail (book != NULL);
318 priv->notebook = book;
320 /* Set the default properties for the notebook (disable tabs,
321 * and remove borders) to make it look like a nice wizard widget */
322 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
323 gtk_notebook_set_show_border (priv->notebook, FALSE);
324 gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
326 /* Show the notebook so that a gtk_widget_show on the dialog is
327 * all that is required to display the dialog correctly */
328 gtk_widget_show ( GTK_WIDGET (priv->notebook));
330 /* Update dialog title to reflect current page stats etc */
331 if (priv->wizard_name && priv->autotitle)
332 create_title (MODEST_WIZARD_DIALOG (object));
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
343 get_property (GObject *object,
348 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
350 switch (property_id) {
352 case PROP_WIZARD_NAME:
353 g_value_set_string (value, priv->wizard_name);
356 case PROP_WIZARD_NOTEBOOK:
357 g_value_set_object (value, priv->notebook);
361 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
367 * Creates the title of the dialog taking into account the current
368 * page of the notebook.
371 create_title (ModestWizardDialog *wizard_dialog)
375 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
376 GtkNotebook *notebook = priv->notebook;
381 /* Get page information, we'll need that when creating title */
382 pages = gtk_notebook_get_n_pages (notebook);
383 current = gtk_notebook_get_current_page (priv->notebook);
387 /* the welcome title on the initial page */
389 str = g_strdup_printf (_("ecdg_ti_wizard_welcome"),
390 priv->wizard_name, pages);
392 const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
393 gtk_notebook_get_nth_page (notebook, current));
395 str = g_strdup_printf (_("ecdg_ti_wizard_step"),
396 priv->wizard_name, current + 1, pages, steps);
399 /* Update the dialog to display the generated title */
400 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
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]
413 response (ModestWizardDialog *wizard_dialog,
417 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
418 GtkNotebook *notebook = priv->notebook;
420 gboolean is_first, is_last;
422 switch (response_id) {
424 case MODEST_WIZARD_DIALOG_PREVIOUS:
425 gtk_notebook_prev_page (notebook); /* go to previous page */
428 case MODEST_WIZARD_DIALOG_NEXT:
429 if (invoke_before_next_vfunc (wizard_dialog))
430 gtk_notebook_next_page (notebook); /* go to next page */
434 case MODEST_WIZARD_DIALOG_CANCEL:
437 case MODEST_WIZARD_DIALOG_FINISH:
438 if (invoke_before_next_vfunc (wizard_dialog))
445 current = gtk_notebook_get_current_page (notebook);
446 gint last = gtk_notebook_get_n_pages (notebook) - 1;
447 is_last = current == last;
448 is_first = current == 0;
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 /* previous */, !is_first /* finish */, !is_last /* next*/);
455 /* Allow derived classes to disable buttons to prevent navigation,
456 * according to their own validation logic: */
457 invoke_enable_buttons_vfunc (wizard_dialog);
459 /* Don't let the dialog close */
460 g_signal_stop_emission_by_name (wizard_dialog, "response");
462 /* We show the default image on first and last pages */
463 last = gtk_notebook_get_n_pages (notebook) - 1;
464 if (current == last || current == 0)
465 gtk_widget_show (GTK_WIDGET(priv->image));
467 gtk_widget_hide (GTK_WIDGET(priv->image));
469 /* New page number may appear in the title, update it */
471 create_title (wizard_dialog);
475 * modest_wizard_dialog_new:
476 * @parent: a #GtkWindow
477 * @wizard_name: the name of dialog
478 * @notebook: the notebook to be shown on the dialog
480 * Creates a new #ModestWizardDialog.
482 * Returns: a new #ModestWizardDialog
485 modest_wizard_dialog_new (GtkWindow *parent,
486 const char *wizard_name,
487 GtkNotebook *notebook)
491 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
493 widget = GTK_WIDGET (g_object_new
494 (MODEST_TYPE_WIZARD_DIALOG,
495 "wizard-name", wizard_name,
496 "wizard-notebook", notebook, NULL));
499 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
505 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
507 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
509 /* Call the vfunc, which may be overridden by derived classes: */
510 if (klass->before_next) {
511 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
513 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
515 /* Get widgets for the two pages: */
516 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
518 GtkWidget* next_page_widget = NULL;
519 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
520 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
522 /* Ask the vfunc implementation whether navigation should be allowed: */
523 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
526 /* Allow navigation by default if there is no vfunc implementation: */
531 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
533 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
535 /* Call the vfunc, which may be overridden by derived classes: */
536 if (klass->enable_buttons) {
537 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
539 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
541 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
543 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);