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.
25 #include <gtk/gtkdialog.h>
26 #include <gtk/gtknotebook.h>
27 #include <gtk/gtkimage.h>
28 #include <gtk/gtkbox.h>
29 #include <gtk/gtkhbox.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtkbutton.h>
38 #ifdef MODEST_HILDON_VERSION_0
39 #include <hildon-widgets/hildon-defines.h>
41 #include <hildon/hildon-defines.h>
42 #endif /*MODEST_HILDON_VERSION_0*/
44 #include "modest-wizard-dialog.h"
48 /* Specify the hildon-libs translation domain,
49 * so we can reuse its translations
50 * instead of repeating them in our own translations.
52 /* #define _(String) dgettext(PACKAGE, String) */
54 #define _(String) dgettext("hildon-libs", String)
56 static GtkDialogClass *parent_class;
58 static void class_init (ModestWizardDialogClass *wizard_dialog_class);
60 static void init (ModestWizardDialog *wizard_dialog);
62 static void create_title (ModestWizardDialog *wizard_dialog);
64 static void set_property (GObject *object,
69 static void get_property (GObject *object,
74 static void finalize (GObject *object);
76 static void response (ModestWizardDialog *wizard,
80 static void make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
85 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
86 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
95 struct _ModestWizardDialogPrivate {
97 GtkNotebook *notebook;
105 modest_wizard_dialog_get_type (void)
107 static GType wizard_dialog_type = 0;
109 if (!wizard_dialog_type) {
111 static const GTypeInfo wizard_dialog_info = {
112 sizeof (ModestWizardDialogClass),
113 NULL, /* base_init */
114 NULL, /* base_finalize */
115 (GClassInitFunc) class_init,
116 NULL, /* class_finalize */
117 NULL, /* class_data */
118 sizeof (ModestWizardDialog),
120 (GInstanceInitFunc) init,
123 wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
124 "ModestWizardDialog",
129 return wizard_dialog_type;
133 class_init (ModestWizardDialogClass *wizard_dialog_class)
135 GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
137 parent_class = g_type_class_peek_parent (wizard_dialog_class);
139 g_type_class_add_private (wizard_dialog_class,
140 sizeof(ModestWizardDialogPrivate));
142 /* Override virtual methods */
143 object_class->set_property = set_property;
144 object_class->get_property = get_property;
145 object_class->finalize = finalize;
148 * ModestWizardDialog:wizard-name:
150 * The name of the wizard.
152 g_object_class_install_property (object_class, PROP_WIZARD_NAME,
156 "The name of the ModestWizardDialog",
161 * ModestWizardDialog:wizard-notebook:
163 * The notebook object, which is used by the ModestWizardDialog.
165 g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
169 "GtkNotebook object to be used in the "
170 "ModestWizardDialog",
171 GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
174 * ModestWizardDialog:autotitle
176 * If the wizard should automatically try to change the window title when changing steps.
177 * Set to FALSE if you'd like to override the default behaviour.
181 g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
185 "If the wizard should autotitle itself",
191 finalize (GObject *object)
193 ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
194 g_return_if_fail (dialog != NULL);
196 if (dialog->priv->wizard_name != NULL)
197 g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
199 if (G_OBJECT_CLASS (parent_class)->finalize)
200 G_OBJECT_CLASS (parent_class)->finalize(object);
203 /* Disable or enable the Previous, Next and Finish buttons */
205 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
210 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
211 MODEST_WIZARD_DIALOG_PREVIOUS,
214 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
215 MODEST_WIZARD_DIALOG_FINISH,
218 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
219 MODEST_WIZARD_DIALOG_NEXT,
224 init (ModestWizardDialog *wizard_dialog)
226 /* Initialize private structure for faster member access */
227 ModestWizardDialogPrivate *priv =
228 G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
229 MODEST_TYPE_WIZARD_DIALOG,
230 ModestWizardDialogPrivate);
232 GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
234 /* Init internal widgets */
235 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
236 gtk_dialog_set_has_separator (dialog, FALSE);
237 wizard_dialog->priv = priv;
238 priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
239 #ifdef MODEST_HILDON_VERSION_0
240 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
241 HILDON_ICON_SIZE_WIDG_WIZARD);
243 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
244 HILDON_ICON_SIZE_WIZARD);
245 #endif /*MODEST_HILDON_VERSION_0*/
246 /* Default values for user provided properties */
247 priv->notebook = NULL;
248 priv->wizard_name = NULL;
249 priv->autotitle = TRUE;
251 /* Build wizard layout */
252 gtk_box_pack_start_defaults (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box));
253 gtk_box_pack_start_defaults (GTK_BOX (priv->box), GTK_WIDGET (vbox));
254 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), FALSE, FALSE, 0);
256 /* Add response buttons: finish, previous, next, cancel */
257 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
258 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
259 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
260 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
262 /* Set initial button states: previous and finish buttons are disabled */
263 make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
265 /* Show all the internal widgets */
266 gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
268 /* connect to dialog's response signal */
269 g_signal_connect (G_OBJECT (dialog), "response",
270 G_CALLBACK (response), NULL);
273 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
274 static void on_notebook_page_added(GtkNotebook *notebook,
279 ModestWizardDialog* dialog = NULL;
281 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
282 dialog = MODEST_WIZARD_DIALOG(user_data);
284 /* The title should show the total number of pages: */
285 create_title (dialog);
288 static void on_notebook_page_removed(GtkNotebook *notebook,
293 ModestWizardDialog* dialog = NULL;
295 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
296 dialog = MODEST_WIZARD_DIALOG(user_data);
298 /* The title should show the total number of pages: */
299 create_title (dialog);
301 #endif /* GTK_CHECK_VERSION */
304 connect_to_notebook_signals(ModestWizardDialog* dialog)
306 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
307 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
308 g_return_if_fail (priv->notebook);
310 /* Connect to the notebook signals,
311 * so we can update the title when necessary: */
312 g_signal_connect (G_OBJECT (priv->notebook), "page-added",
313 G_CALLBACK (on_notebook_page_added), dialog);
314 g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
315 G_CALLBACK (on_notebook_page_removed), dialog);
316 #endif /* GTK_CHECK_VERSION */
321 set_property (GObject *object,
326 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
328 switch (property_id) {
330 case PROP_WIZARD_AUTOTITLE:
332 priv->autotitle = g_value_get_boolean (value);
334 if (priv->autotitle &&
337 create_title (MODEST_WIZARD_DIALOG (object));
338 else if (priv->wizard_name)
339 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
343 case PROP_WIZARD_NAME:
345 /* Set new wizard name. This name will appear in titlebar */
346 if (priv->wizard_name)
347 g_free (priv->wizard_name);
349 gchar *str = (gchar *) g_value_get_string (value);
350 g_return_if_fail (str != NULL);
352 priv->wizard_name = g_strdup (str);
354 /* We need notebook in order to create title, since page information
355 is used in title generation */
357 if (priv->notebook && priv->autotitle)
358 create_title (MODEST_WIZARD_DIALOG (object));
362 case PROP_WIZARD_NOTEBOOK: {
364 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
365 g_return_if_fail (book != NULL);
367 priv->notebook = book;
369 /* Set the default properties for the notebook (disable tabs,
370 * and remove borders) to make it look like a nice wizard widget */
371 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
372 gtk_notebook_set_show_border (priv->notebook, FALSE);
373 gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
375 /* Show the notebook so that a gtk_widget_show on the dialog is
376 * all that is required to display the dialog correctly */
377 gtk_widget_show ( GTK_WIDGET (priv->notebook));
379 /* Update dialog title to reflect current page stats etc */
380 ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);
381 if (priv->wizard_name && priv->autotitle)
382 create_title (wizard_dialog);
384 connect_to_notebook_signals (wizard_dialog);
389 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
395 get_property (GObject *object,
400 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
402 switch (property_id) {
404 case PROP_WIZARD_NAME:
405 g_value_set_string (value, priv->wizard_name);
408 case PROP_WIZARD_NOTEBOOK:
409 g_value_set_object (value, priv->notebook);
413 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
419 * Creates the title of the dialog taking into account the current
420 * page of the notebook.
423 create_title (ModestWizardDialog *wizard_dialog)
427 ModestWizardDialogPrivate *priv = NULL;
428 GtkNotebook *notebook = NULL;
430 /* FIXME (jfernandez): Sometines, priv is NULL */
431 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
432 g_return_if_fail (wizard_dialog->priv != NULL);
434 priv = wizard_dialog->priv;
435 notebook = priv->notebook;
440 /* Get page information, we'll need that when creating title */
441 pages = gtk_notebook_get_n_pages (notebook);
442 current = gtk_notebook_get_current_page (priv->notebook);
446 /* the welcome title on the initial page */
448 str = g_strdup_printf (_("ecdg_ti_wizard_welcome"),
449 priv->wizard_name, pages);
451 const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
452 gtk_notebook_get_nth_page (notebook, current));
454 str = g_strdup_printf (_("ecdg_ti_wizard_step"),
455 priv->wizard_name, current + 1, pages, steps);
458 /* Update the dialog to display the generated title */
459 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
464 * Response signal handler. This function is needed because GtkDialog's
465 * handler for this signal closes the dialog and we don't want that, we
466 * want to change pages and, dimm certain response buttons. Overriding the
467 * virtual function would not work because that would be called after the
468 * signal handler implemented by GtkDialog.
469 * FIXME: There is a much saner way to do that [MDK]
472 response (ModestWizardDialog *wizard_dialog,
476 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
477 GtkNotebook *notebook = priv->notebook;
479 gboolean is_first, is_last;
481 switch (response_id) {
483 case MODEST_WIZARD_DIALOG_PREVIOUS:
484 gtk_notebook_prev_page (notebook); /* go to previous page */
487 case MODEST_WIZARD_DIALOG_NEXT:
488 if (invoke_before_next_vfunc (wizard_dialog))
489 gtk_notebook_next_page (notebook); /* go to next page */
493 case MODEST_WIZARD_DIALOG_CANCEL:
496 case MODEST_WIZARD_DIALOG_FINISH:
497 if (invoke_before_next_vfunc (wizard_dialog))
504 current = gtk_notebook_get_current_page (notebook);
505 gint last = gtk_notebook_get_n_pages (notebook) - 1;
506 is_last = current == last;
507 is_first = current == 0;
509 /* If first page, previous and finish are disabled,
510 if last page, next is disabled */
511 make_buttons_sensitive (wizard_dialog,
512 !is_first /* previous */, !is_first /* finish */, !is_last /* next*/);
514 /* Allow derived classes to disable buttons to prevent navigation,
515 * according to their own validation logic: */
516 invoke_enable_buttons_vfunc (wizard_dialog);
518 /* Don't let the dialog close */
519 g_signal_stop_emission_by_name (wizard_dialog, "response");
521 /* We show the default image on first and last pages */
522 last = gtk_notebook_get_n_pages (notebook) - 1;
523 if (current == last || current == 0)
524 gtk_widget_show (GTK_WIDGET(priv->image));
526 gtk_widget_hide (GTK_WIDGET(priv->image));
528 /* New page number may appear in the title, update it */
530 create_title (wizard_dialog);
534 * modest_wizard_dialog_new:
535 * @parent: a #GtkWindow
536 * @wizard_name: the name of dialog
537 * @notebook: the notebook to be shown on the dialog
539 * Creates a new #ModestWizardDialog.
541 * Returns: a new #ModestWizardDialog
544 modest_wizard_dialog_new (GtkWindow *parent,
545 const char *wizard_name,
546 GtkNotebook *notebook)
550 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
552 widget = GTK_WIDGET (g_object_new
553 (MODEST_TYPE_WIZARD_DIALOG,
554 "wizard-name", wizard_name,
555 "wizard-notebook", notebook, NULL));
558 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
564 * modest_wizard_dialog_force_title_update:
565 * @wizard_dialog: The wizard dialog
567 * Force the title to be rebuilt, for instance when you have added or
568 * removed notebook pages. This function is not necessary when using GTK+ 2.10,
569 * because that has GtkNotebook signals that will be used to update the title
573 modest_wizard_dialog_force_title_update (ModestWizardDialog *wizard_dialog)
575 create_title (wizard_dialog);
579 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
581 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
583 /* Call the vfunc, which may be overridden by derived classes: */
584 if (klass->before_next) {
585 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
587 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
589 /* Get widgets for the two pages: */
590 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
592 GtkWidget* next_page_widget = NULL;
593 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
594 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
596 /* Ask the vfunc implementation whether navigation should be allowed: */
597 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
600 /* Allow navigation by default if there is no vfunc implementation: */
605 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
607 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
609 /* Call the vfunc, which may be overridden by derived classes: */
610 if (klass->enable_buttons) {
611 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
613 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
615 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
617 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);