1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * SECTION:modest-wizard-dialog
32 * @short_description: A widget to create a guided installation
35 * #ModestWizardDialog is a widget to create a guided installation
36 * process. The dialog has four standard buttons, previous, next,
37 * finish, cancel, and contains several pages with optional icons.
38 * Response buttons are dimmed/undimmed automatically and the standard
39 * icon is shown/hidden in response to page navigation. The notebook
40 * widget provided by users contains the actual wizard pages.
50 #ifdef MODEST_TOOLKIT_HILDON2
51 #include <hildon/hildon-defines.h>
54 #include "modest-wizard-dialog.h"
55 #include "modest-debug.h"
56 #include "modest-text-utils.h"
58 static GtkDialogClass *parent_class;
60 static void class_init (ModestWizardDialogClass *wizard_dialog_class);
62 static void init (ModestWizardDialog *wizard_dialog);
64 static void create_title (ModestWizardDialog *wizard_dialog);
66 static void set_property (GObject *object,
71 static void get_property (GObject *object,
76 static void finalize (GObject *object);
78 static void response (ModestWizardDialog *wizard,
82 static void make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
87 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
88 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
89 static void invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog);
90 static gboolean invoke_save_vfunc (ModestWizardDialog *wizard_dialog);
99 struct _ModestWizardDialogPrivate {
101 GtkNotebook *notebook;
106 ModestWizardDialogResponseOverrideFunc override_func;
111 modest_wizard_dialog_get_type (void)
113 static GType wizard_dialog_type = 0;
115 if (!wizard_dialog_type) {
117 static const GTypeInfo wizard_dialog_info = {
118 sizeof (ModestWizardDialogClass),
119 NULL, /* base_init */
120 NULL, /* base_finalize */
121 (GClassInitFunc) class_init,
122 NULL, /* class_finalize */
123 NULL, /* class_data */
124 sizeof (ModestWizardDialog),
126 (GInstanceInitFunc) init,
129 wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
130 "ModestWizardDialog",
135 return wizard_dialog_type;
139 class_init (ModestWizardDialogClass *wizard_dialog_class)
141 GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
143 parent_class = g_type_class_peek_parent (wizard_dialog_class);
145 g_type_class_add_private (wizard_dialog_class,
146 sizeof(ModestWizardDialogPrivate));
148 /* Override virtual methods */
149 object_class->set_property = set_property;
150 object_class->get_property = get_property;
151 object_class->finalize = finalize;
153 wizard_dialog_class->before_next = NULL;
154 wizard_dialog_class->update_model = NULL;
155 wizard_dialog_class->save = NULL;
156 wizard_dialog_class->enable_buttons = NULL;
159 * ModestWizardDialog:wizard-name:
161 * The name of the wizard.
163 g_object_class_install_property (object_class, PROP_WIZARD_NAME,
167 "The name of the ModestWizardDialog",
172 * ModestWizardDialog:wizard-notebook:
174 * The notebook object, which is used by the ModestWizardDialog.
176 g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
180 "GtkNotebook object to be used in the "
181 "ModestWizardDialog",
182 GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
185 * ModestWizardDialog:autotitle
187 * If the wizard should automatically try to change the window title when changing steps.
188 * Set to FALSE if you'd like to override the default behaviour.
192 g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
196 "If the wizard should autotitle itself",
202 finalize (GObject *object)
204 ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
205 g_return_if_fail (dialog != NULL);
207 if (dialog->priv->wizard_name != NULL)
208 g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
210 if (G_OBJECT_CLASS (parent_class)->finalize)
211 G_OBJECT_CLASS (parent_class)->finalize(object);
214 /* Disable or enable the Previous, Next and Finish buttons */
216 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
221 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
222 MODEST_WIZARD_DIALOG_PREVIOUS,
225 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
226 MODEST_WIZARD_DIALOG_FINISH,
229 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
230 MODEST_WIZARD_DIALOG_NEXT,
235 init (ModestWizardDialog *wizard_dialog)
237 /* Initialize private structure for faster member access */
238 ModestWizardDialogPrivate *priv =
239 G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
240 MODEST_TYPE_WIZARD_DIALOG,
241 ModestWizardDialogPrivate);
243 GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
245 /* Init internal widgets */
246 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
247 gtk_dialog_set_has_separator (dialog, FALSE);
248 wizard_dialog->priv = priv;
249 priv->override_func = NULL;
250 priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
251 #ifdef MODEST_TOOLKIT_HILDON2
254 #ifdef MODEST_TOOLKIT_GTK
255 priv->image = gtk_image_new_from_stock (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG);
256 #else /*MODEST_TOOLKIT_GTK*/
257 static int icon_size = 0;
259 icon_size = gtk_icon_size_register("modest_wizard", 50, 50);
260 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
262 #endif /*!MODEST_TOOLKIT_GTK*/
263 #endif /*MODEST_TOOLKIT_HILDON2 */
264 /* Default values for user provided properties */
265 priv->notebook = NULL;
266 priv->wizard_name = NULL;
267 priv->autotitle = TRUE;
269 /* Build wizard layout */
270 gtk_box_pack_start (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box), TRUE, TRUE, 0);
271 gtk_box_pack_start (GTK_BOX (priv->box), GTK_WIDGET (vbox), FALSE, FALSE, 0);
272 gtk_widget_show (vbox);
273 gtk_widget_show (GTK_WIDGET (priv->box));
275 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), TRUE, TRUE, 0);
276 gtk_widget_show (priv->image);
279 /* Add response buttons: finish, previous, next, cancel */
280 #ifdef MODEST_TOOLKIT_HILDON1
281 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
282 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
283 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
284 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
286 #ifdef MODEST_TOOLKIT_HILDON2
287 gtk_dialog_add_button (dialog, _HL("wdgt_bd_finish"), MODEST_WIZARD_DIALOG_FINISH);
288 gtk_dialog_add_button (dialog, _HL("wdgt_bd_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
289 gtk_dialog_add_button (dialog, _HL("wdgt_bd_next"), MODEST_WIZARD_DIALOG_NEXT);
291 #ifdef MODEST_TOOLKIT_GTK
292 gtk_dialog_add_button (dialog, GTK_STOCK_SAVE, MODEST_WIZARD_DIALOG_FINISH);
293 gtk_dialog_add_button (dialog, GTK_STOCK_GO_BACK, MODEST_WIZARD_DIALOG_PREVIOUS);
294 gtk_dialog_add_button (dialog, GTK_STOCK_GO_FORWARD, MODEST_WIZARD_DIALOG_NEXT);
295 gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, MODEST_WIZARD_DIALOG_CANCEL);
298 /* Set initial button states: previous and finish buttons are disabled */
299 make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
301 gtk_widget_show (GTK_WIDGET (dialog->vbox));
303 /* connect to dialog's response signal */
304 g_signal_connect (G_OBJECT (dialog), "response",
305 G_CALLBACK (response), NULL);
309 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
310 static void on_notebook_page_added(GtkNotebook *notebook,
315 ModestWizardDialog* dialog = NULL;
317 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
318 dialog = MODEST_WIZARD_DIALOG(user_data);
320 /* The title should show the total number of pages: */
321 create_title (dialog);
324 static void on_notebook_page_removed(GtkNotebook *notebook,
329 ModestWizardDialog* dialog = NULL;
331 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
332 dialog = MODEST_WIZARD_DIALOG(user_data);
334 /* The title should show the total number of pages: */
335 create_title (dialog);
337 #endif /* GTK_CHECK_VERSION */
340 on_notebook_switch_page (GtkNotebook *notebook,
341 GtkNotebookPage *page,
343 ModestWizardDialog *self)
345 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(self));
351 connect_to_notebook_signals(ModestWizardDialog* dialog)
353 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
354 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
355 g_return_if_fail (priv->notebook);
357 /* Connect to the notebook signals,
358 * so we can update the title when necessary: */
359 g_signal_connect (G_OBJECT (priv->notebook), "page-added",
360 G_CALLBACK (on_notebook_page_added), dialog);
361 g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
362 G_CALLBACK (on_notebook_page_removed), dialog);
363 #endif /* GTK_CHECK_VERSION */
364 g_signal_connect_after (G_OBJECT (priv->notebook), "switch-page",
365 G_CALLBACK (on_notebook_switch_page), dialog);
370 set_property (GObject *object,
375 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
377 switch (property_id) {
379 case PROP_WIZARD_AUTOTITLE:
381 priv->autotitle = g_value_get_boolean (value);
383 if (priv->autotitle &&
386 create_title (MODEST_WIZARD_DIALOG (object));
387 else if (priv->wizard_name)
388 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
392 case PROP_WIZARD_NAME:
394 /* Set new wizard name. This name will appear in titlebar */
395 if (priv->wizard_name)
396 g_free (priv->wizard_name);
398 gchar *str = (gchar *) g_value_get_string (value);
399 g_return_if_fail (str != NULL);
401 priv->wizard_name = g_strdup (str);
403 /* We need notebook in order to create title, since page information
404 is used in title generation */
406 if (priv->notebook && priv->autotitle)
407 create_title (MODEST_WIZARD_DIALOG (object));
411 case PROP_WIZARD_NOTEBOOK: {
413 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
414 g_return_if_fail (book != NULL);
416 priv->notebook = book;
418 /* Set the default properties for the notebook (disable tabs,
419 * and remove borders) to make it look like a nice wizard widget */
420 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
421 gtk_notebook_set_show_border (priv->notebook, FALSE);
422 gtk_box_pack_start (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook), TRUE, TRUE, 0);
424 /* Show the notebook so that a gtk_widget_show on the dialog is
425 * all that is required to display the dialog correctly */
426 gtk_widget_show ( GTK_WIDGET (priv->notebook));
428 /* Update dialog title to reflect current page stats etc */
429 ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);
430 if (priv->wizard_name && priv->autotitle)
431 create_title (wizard_dialog);
433 connect_to_notebook_signals (wizard_dialog);
438 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
444 get_property (GObject *object,
449 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
451 switch (property_id) {
453 case PROP_WIZARD_NAME:
454 g_value_set_string (value, priv->wizard_name);
457 case PROP_WIZARD_NOTEBOOK:
458 g_value_set_object (value, priv->notebook);
462 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
468 * Creates the title of the dialog taking into account the current
469 * page of the notebook.
472 create_title (ModestWizardDialog *wizard_dialog)
475 ModestWizardDialogPrivate *priv = NULL;
476 GtkNotebook *notebook = NULL;
480 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
481 g_return_if_fail (wizard_dialog->priv != NULL);
483 priv = wizard_dialog->priv;
484 notebook = priv->notebook;
489 /* Get page information, we'll need that when creating title */
490 pages = gtk_notebook_get_n_pages (notebook);
494 current = gtk_notebook_get_current_page (priv->notebook);
498 steps = gtk_notebook_get_tab_label_text (notebook,
499 gtk_notebook_get_nth_page (notebook, current));
501 str = g_strdup_printf ((steps&&*steps)?_HL("%s%s %s"):_HL("%s"),
502 priv->wizard_name, _HL("ecdg_ti_caption_separator"),
505 /* Update the dialog to display the generated title */
506 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
511 * Response signal handler. This function is needed because GtkDialog's
512 * handler for this signal closes the dialog and we don't want that, we
513 * want to change pages and, dim certain response buttons. Overriding the
514 * virtual function would not work because that would be called after the
515 * signal handler implemented by GtkDialog.
516 * FIXME: There is a much saner way to do that [MDK]
519 response (ModestWizardDialog *wizard_dialog,
523 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
524 GtkNotebook *notebook = priv->notebook;
526 gboolean is_first, is_last;
528 if (priv->override_func) {
529 if (priv->override_func (wizard_dialog, response_id, gtk_notebook_get_current_page (notebook))) {
530 /* Don't let the dialog close */
531 g_signal_stop_emission_by_name (wizard_dialog, "response");
533 /* Force refresh of title */
535 create_title (wizard_dialog);
540 switch (response_id) {
542 case MODEST_WIZARD_DIALOG_PREVIOUS:
543 gtk_notebook_prev_page (notebook); /* go to previous page */
546 case MODEST_WIZARD_DIALOG_NEXT:
547 if (invoke_before_next_vfunc (wizard_dialog))
548 gtk_notebook_next_page (notebook); /* go to next page */
552 case MODEST_WIZARD_DIALOG_CANCEL:
555 case MODEST_WIZARD_DIALOG_FINISH:
556 if (invoke_before_next_vfunc (wizard_dialog))
563 current = gtk_notebook_get_current_page (notebook);
564 gint last = gtk_notebook_get_n_pages (notebook) - 1;
565 is_last = current == last;
566 is_first = current == 0;
568 /* If first page, previous and finish are disabled,
569 if last page, next is disabled */
570 make_buttons_sensitive (wizard_dialog,
571 (is_first) ? FALSE : TRUE,
573 (is_last) ? FALSE : TRUE);
575 /* Allow derived classes to disable buttons to prevent navigation,
576 * according to their own validation logic: */
577 invoke_enable_buttons_vfunc (wizard_dialog);
579 /* Don't let the dialog close */
580 g_signal_stop_emission_by_name (wizard_dialog, "response");
582 /* We show the default image on first and last pages */
583 last = gtk_notebook_get_n_pages (notebook) - 1;
585 if (current == last || current == 0)
586 gtk_widget_show (GTK_WIDGET(priv->image));
588 gtk_widget_hide (GTK_WIDGET(priv->image));
591 /* New page number may appear in the title, update it */
593 create_title (wizard_dialog);
597 * modest_wizard_dialog_new:
598 * @parent: a #GtkWindow
599 * @wizard_name: the name of dialog
600 * @notebook: the notebook to be shown on the dialog
602 * Creates a new #ModestWizardDialog.
604 * Returns: a new #ModestWizardDialog
607 modest_wizard_dialog_new (GtkWindow *parent,
608 const char *wizard_name,
609 GtkNotebook *notebook)
613 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
615 widget = GTK_WIDGET (g_object_new
616 (MODEST_TYPE_WIZARD_DIALOG,
617 "wizard-name", wizard_name,
618 "wizard-notebook", notebook, NULL));
621 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
627 * modest_wizard_dialog_force_title_update:
628 * @wizard_dialog: The wizard dialog
630 * Force the title to be rebuilt, for instance when you have added or
631 * removed notebook pages. This function is not necessary when using GTK+ 2.10,
632 * because that has GtkNotebook signals that will be used to update the title
636 modest_wizard_dialog_force_title_update (ModestWizardDialog *wizard_dialog)
638 create_title (wizard_dialog);
642 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
644 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
646 /* Call the vfunc, which may be overridden by derived classes: */
647 if (klass->before_next) {
648 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
650 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
652 /* Get widgets for the two pages: */
653 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
655 GtkWidget* next_page_widget = NULL;
656 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
657 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
660 g_debug ("Switching to page %d (%s)",
661 gtk_notebook_page_num (priv->notebook, next_page_widget),
662 gtk_notebook_get_tab_label_text (priv->notebook, next_page_widget));
667 g_debug ("\t***************");
668 for (i=0; i<gtk_notebook_get_n_pages(priv->notebook);i++) {
669 p = gtk_notebook_get_nth_page (priv->notebook, i);
670 g_debug ("\t%d - %s", i, gtk_notebook_get_tab_label_text (priv->notebook, p));
672 g_debug ("\t***************");
676 /* Ask the vfunc implementation whether navigation should be allowed: */
677 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
680 /* Allow navigation by default if there is no vfunc implementation: */
685 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
687 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
689 /* Call the vfunc, which may be overridden by derived classes: */
690 if (klass->enable_buttons) {
691 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
693 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
695 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
697 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);
702 invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog)
704 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
706 /* Call the vfunc, which may be overridden by derived classes: */
707 if (klass->update_model) {
708 (*(klass->update_model)) (wizard_dialog);
713 invoke_save_vfunc (ModestWizardDialog *wizard_dialog)
715 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
717 /* Call the vfunc, which may be overridden by derived classes: */
719 return (*(klass->save)) (wizard_dialog);
726 modest_wizard_dialog_set_response_override_handler (ModestWizardDialog *wizard_dialog,
727 ModestWizardDialogResponseOverrideFunc callback)
729 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
731 priv->override_func = callback;
735 modest_wizard_dialog_update_model (ModestWizardDialog *wizard_dialog)
737 g_return_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog));
739 invoke_update_model_vfunc (wizard_dialog);
743 modest_wizard_dialog_save (ModestWizardDialog *wizard_dialog)
745 g_return_val_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog), FALSE);
747 return invoke_save_vfunc (wizard_dialog);