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(ModestWizardDialog* dialog,
279 /* The title should show the total number of pages: */
280 create_title (dialog);
283 static void on_notebook_page_removed(ModestWizardDialog* dialog,
288 /* The title should show the total number of pages: */
289 create_title (dialog);
291 #endif /* GTK_CHECK_VERSION */
294 connect_to_notebook_signals(ModestWizardDialog* dialog)
296 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
297 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
298 g_return_if_fail (priv->notebook);
300 /* Connect to the notebook signals,
301 * so we can update the title when necessary: */
302 g_signal_connect (G_OBJECT (priv->notebook), "page-added",
303 G_CALLBACK (on_notebook_page_added), NULL);
304 g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
305 G_CALLBACK (on_notebook_page_removed), NULL);
306 #endif /* GTK_CHECK_VERSION */
311 set_property (GObject *object,
316 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
318 switch (property_id) {
320 case PROP_WIZARD_AUTOTITLE:
322 priv->autotitle = g_value_get_boolean (value);
324 if (priv->autotitle &&
327 create_title (MODEST_WIZARD_DIALOG (object));
328 else if (priv->wizard_name)
329 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
333 case PROP_WIZARD_NAME:
335 /* Set new wizard name. This name will appear in titlebar */
336 if (priv->wizard_name)
337 g_free (priv->wizard_name);
339 gchar *str = (gchar *) g_value_get_string (value);
340 g_return_if_fail (str != NULL);
342 priv->wizard_name = g_strdup (str);
344 /* We need notebook in order to create title, since page information
345 is used in title generation */
347 if (priv->notebook && priv->autotitle)
348 create_title (MODEST_WIZARD_DIALOG (object));
352 case PROP_WIZARD_NOTEBOOK: {
354 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
355 g_return_if_fail (book != NULL);
357 priv->notebook = book;
359 /* Set the default properties for the notebook (disable tabs,
360 * and remove borders) to make it look like a nice wizard widget */
361 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
362 gtk_notebook_set_show_border (priv->notebook, FALSE);
363 gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
365 /* Show the notebook so that a gtk_widget_show on the dialog is
366 * all that is required to display the dialog correctly */
367 gtk_widget_show ( GTK_WIDGET (priv->notebook));
369 /* Update dialog title to reflect current page stats etc */
370 ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);
371 if (priv->wizard_name && priv->autotitle)
372 create_title (wizard_dialog);
374 connect_to_notebook_signals (wizard_dialog);
379 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
385 get_property (GObject *object,
390 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
392 switch (property_id) {
394 case PROP_WIZARD_NAME:
395 g_value_set_string (value, priv->wizard_name);
398 case PROP_WIZARD_NOTEBOOK:
399 g_value_set_object (value, priv->notebook);
403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
409 * Creates the title of the dialog taking into account the current
410 * page of the notebook.
413 create_title (ModestWizardDialog *wizard_dialog)
417 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
418 GtkNotebook *notebook = priv->notebook;
423 /* Get page information, we'll need that when creating title */
424 pages = gtk_notebook_get_n_pages (notebook);
425 current = gtk_notebook_get_current_page (priv->notebook);
429 /* the welcome title on the initial page */
431 str = g_strdup_printf (_("ecdg_ti_wizard_welcome"),
432 priv->wizard_name, pages);
434 const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
435 gtk_notebook_get_nth_page (notebook, current));
437 str = g_strdup_printf (_("ecdg_ti_wizard_step"),
438 priv->wizard_name, current + 1, pages, steps);
441 /* Update the dialog to display the generated title */
442 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
447 * Response signal handler. This function is needed because GtkDialog's
448 * handler for this signal closes the dialog and we don't want that, we
449 * want to change pages and, dimm certain response buttons. Overriding the
450 * virtual function would not work because that would be called after the
451 * signal handler implemented by GtkDialog.
452 * FIXME: There is a much saner way to do that [MDK]
455 response (ModestWizardDialog *wizard_dialog,
459 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
460 GtkNotebook *notebook = priv->notebook;
462 gboolean is_first, is_last;
464 switch (response_id) {
466 case MODEST_WIZARD_DIALOG_PREVIOUS:
467 gtk_notebook_prev_page (notebook); /* go to previous page */
470 case MODEST_WIZARD_DIALOG_NEXT:
471 if (invoke_before_next_vfunc (wizard_dialog))
472 gtk_notebook_next_page (notebook); /* go to next page */
476 case MODEST_WIZARD_DIALOG_CANCEL:
479 case MODEST_WIZARD_DIALOG_FINISH:
480 if (invoke_before_next_vfunc (wizard_dialog))
487 current = gtk_notebook_get_current_page (notebook);
488 gint last = gtk_notebook_get_n_pages (notebook) - 1;
489 is_last = current == last;
490 is_first = current == 0;
492 /* If first page, previous and finish are disabled,
493 if last page, next is disabled */
494 make_buttons_sensitive (wizard_dialog,
495 !is_first /* previous */, !is_first /* finish */, !is_last /* next*/);
497 /* Allow derived classes to disable buttons to prevent navigation,
498 * according to their own validation logic: */
499 invoke_enable_buttons_vfunc (wizard_dialog);
501 /* Don't let the dialog close */
502 g_signal_stop_emission_by_name (wizard_dialog, "response");
504 /* We show the default image on first and last pages */
505 last = gtk_notebook_get_n_pages (notebook) - 1;
506 if (current == last || current == 0)
507 gtk_widget_show (GTK_WIDGET(priv->image));
509 gtk_widget_hide (GTK_WIDGET(priv->image));
511 /* New page number may appear in the title, update it */
513 create_title (wizard_dialog);
517 * modest_wizard_dialog_new:
518 * @parent: a #GtkWindow
519 * @wizard_name: the name of dialog
520 * @notebook: the notebook to be shown on the dialog
522 * Creates a new #ModestWizardDialog.
524 * Returns: a new #ModestWizardDialog
527 modest_wizard_dialog_new (GtkWindow *parent,
528 const char *wizard_name,
529 GtkNotebook *notebook)
533 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
535 widget = GTK_WIDGET (g_object_new
536 (MODEST_TYPE_WIZARD_DIALOG,
537 "wizard-name", wizard_name,
538 "wizard-notebook", notebook, NULL));
541 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
547 * modest_wizard_dialog_force_title_update:
548 * @wizard_dialog: The wizard dialog
550 * Force the title to be rebuilt, for instance when you have added or
551 * removed notebook pages. This function is not necessary when using GTK+ 2.10,
552 * because that has GtkNotebook signals that will be used to update the title
556 modest_wizard_dialog_force_title_update (ModestWizardDialog *wizard_dialog)
558 create_title (wizard_dialog);
562 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
564 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
566 /* Call the vfunc, which may be overridden by derived classes: */
567 if (klass->before_next) {
568 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
570 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
572 /* Get widgets for the two pages: */
573 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
575 GtkWidget* next_page_widget = NULL;
576 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
577 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
579 /* Ask the vfunc implementation whether navigation should be allowed: */
580 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
583 /* Allow navigation by default if there is no vfunc implementation: */
588 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
590 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
592 /* Call the vfunc, which may be overridden by derived classes: */
593 if (klass->enable_buttons) {
594 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
596 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
598 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
600 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);