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.
44 #include <gtk/gtkdialog.h>
45 #include <gtk/gtknotebook.h>
46 #include <gtk/gtkimage.h>
47 #include <gtk/gtkbox.h>
48 #include <gtk/gtkhbox.h>
49 #include <gtk/gtkvbox.h>
50 #include <gtk/gtkbutton.h>
57 #ifndef MODEST_TOOLKIT_GTK
58 #include <hildon/hildon-defines.h>
59 #endif /*!MODEST_TOOLKIT_GTK*/
61 #include "modest-wizard-dialog.h"
62 #include "modest-debug.h"
63 #include "modest-text-utils.h"
65 static GtkDialogClass *parent_class;
67 static void class_init (ModestWizardDialogClass *wizard_dialog_class);
69 static void init (ModestWizardDialog *wizard_dialog);
71 static void create_title (ModestWizardDialog *wizard_dialog);
73 static void set_property (GObject *object,
78 static void get_property (GObject *object,
83 static void finalize (GObject *object);
85 static void response (ModestWizardDialog *wizard,
89 static void make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
94 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
95 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
96 static void invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog);
97 static gboolean invoke_save_vfunc (ModestWizardDialog *wizard_dialog);
102 PROP_WIZARD_NOTEBOOK,
103 PROP_WIZARD_AUTOTITLE
106 struct _ModestWizardDialogPrivate {
108 GtkNotebook *notebook;
113 ModestWizardDialogResponseOverrideFunc override_func;
118 modest_wizard_dialog_get_type (void)
120 static GType wizard_dialog_type = 0;
122 if (!wizard_dialog_type) {
124 static const GTypeInfo wizard_dialog_info = {
125 sizeof (ModestWizardDialogClass),
126 NULL, /* base_init */
127 NULL, /* base_finalize */
128 (GClassInitFunc) class_init,
129 NULL, /* class_finalize */
130 NULL, /* class_data */
131 sizeof (ModestWizardDialog),
133 (GInstanceInitFunc) init,
136 wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
137 "ModestWizardDialog",
142 return wizard_dialog_type;
146 class_init (ModestWizardDialogClass *wizard_dialog_class)
148 GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
150 parent_class = g_type_class_peek_parent (wizard_dialog_class);
152 g_type_class_add_private (wizard_dialog_class,
153 sizeof(ModestWizardDialogPrivate));
155 /* Override virtual methods */
156 object_class->set_property = set_property;
157 object_class->get_property = get_property;
158 object_class->finalize = finalize;
160 wizard_dialog_class->before_next = NULL;
161 wizard_dialog_class->update_model = NULL;
162 wizard_dialog_class->save = NULL;
163 wizard_dialog_class->enable_buttons = NULL;
166 * ModestWizardDialog:wizard-name:
168 * The name of the wizard.
170 g_object_class_install_property (object_class, PROP_WIZARD_NAME,
174 "The name of the ModestWizardDialog",
179 * ModestWizardDialog:wizard-notebook:
181 * The notebook object, which is used by the ModestWizardDialog.
183 g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
187 "GtkNotebook object to be used in the "
188 "ModestWizardDialog",
189 GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
192 * ModestWizardDialog:autotitle
194 * If the wizard should automatically try to change the window title when changing steps.
195 * Set to FALSE if you'd like to override the default behaviour.
199 g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
203 "If the wizard should autotitle itself",
209 finalize (GObject *object)
211 ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
212 g_return_if_fail (dialog != NULL);
214 if (dialog->priv->wizard_name != NULL)
215 g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
217 if (G_OBJECT_CLASS (parent_class)->finalize)
218 G_OBJECT_CLASS (parent_class)->finalize(object);
221 /* Disable or enable the Previous, Next and Finish buttons */
223 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
228 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
229 MODEST_WIZARD_DIALOG_PREVIOUS,
232 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
233 MODEST_WIZARD_DIALOG_FINISH,
236 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
237 MODEST_WIZARD_DIALOG_NEXT,
242 init (ModestWizardDialog *wizard_dialog)
244 /* Initialize private structure for faster member access */
245 ModestWizardDialogPrivate *priv =
246 G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
247 MODEST_TYPE_WIZARD_DIALOG,
248 ModestWizardDialogPrivate);
250 GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
252 /* Init internal widgets */
253 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
254 gtk_dialog_set_has_separator (dialog, FALSE);
255 wizard_dialog->priv = priv;
256 priv->override_func = NULL;
257 priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
258 #ifdef MODEST_TOOLKIT_HILDON2
261 #ifdef MODEST_TOOLKIT_GTK
262 priv->image = gtk_image_new_from_stock (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG);
263 #else /*MODEST_TOOLKIT_GTK*/
264 static int icon_size = 0;
266 icon_size = gtk_icon_size_register("modest_wizard", 50, 50);
267 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
269 #endif /*!MODEST_TOOLKIT_GTK*/
270 #endif /*MODEST_TOOLKIT_HILDON2 */
271 /* Default values for user provided properties */
272 priv->notebook = NULL;
273 priv->wizard_name = NULL;
274 priv->autotitle = TRUE;
276 /* Build wizard layout */
277 gtk_box_pack_start (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box), TRUE, TRUE, 0);
278 gtk_box_pack_start (GTK_BOX (priv->box), GTK_WIDGET (vbox), FALSE, FALSE, 0);
279 gtk_widget_show (vbox);
280 gtk_widget_show (GTK_WIDGET (priv->box));
282 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), TRUE, TRUE, 0);
283 gtk_widget_show (priv->image);
286 /* Add response buttons: finish, previous, next, cancel */
287 #ifdef MODEST_TOOLKIT_HILDON1
288 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
289 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
290 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
291 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
293 #ifdef MODEST_TOOLKIT_HILDON2
294 gtk_dialog_add_button (dialog, _HL("wdgt_bd_finish"), MODEST_WIZARD_DIALOG_FINISH);
295 gtk_dialog_add_button (dialog, _HL("wdgt_bd_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
296 gtk_dialog_add_button (dialog, _HL("wdgt_bd_next"), MODEST_WIZARD_DIALOG_NEXT);
298 #ifdef MODEST_TOOLKIT_GTK
299 gtk_dialog_add_button (dialog, GTK_STOCK_SAVE, MODEST_WIZARD_DIALOG_FINISH);
300 gtk_dialog_add_button (dialog, GTK_STOCK_GO_BACK, MODEST_WIZARD_DIALOG_PREVIOUS);
301 gtk_dialog_add_button (dialog, GTK_STOCK_GO_FORWARD, MODEST_WIZARD_DIALOG_NEXT);
302 gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, MODEST_WIZARD_DIALOG_CANCEL);
305 /* Set initial button states: previous and finish buttons are disabled */
306 make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
308 gtk_widget_show (GTK_WIDGET (dialog->vbox));
310 /* connect to dialog's response signal */
311 g_signal_connect (G_OBJECT (dialog), "response",
312 G_CALLBACK (response), NULL);
316 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
317 static void on_notebook_page_added(GtkNotebook *notebook,
322 ModestWizardDialog* dialog = NULL;
324 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
325 dialog = MODEST_WIZARD_DIALOG(user_data);
327 /* The title should show the total number of pages: */
328 create_title (dialog);
331 static void on_notebook_page_removed(GtkNotebook *notebook,
336 ModestWizardDialog* dialog = NULL;
338 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
339 dialog = MODEST_WIZARD_DIALOG(user_data);
341 /* The title should show the total number of pages: */
342 create_title (dialog);
344 #endif /* GTK_CHECK_VERSION */
347 on_notebook_switch_page (GtkNotebook *notebook,
348 GtkNotebookPage *page,
350 ModestWizardDialog *self)
352 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(self));
358 connect_to_notebook_signals(ModestWizardDialog* dialog)
360 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
361 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
362 g_return_if_fail (priv->notebook);
364 /* Connect to the notebook signals,
365 * so we can update the title when necessary: */
366 g_signal_connect (G_OBJECT (priv->notebook), "page-added",
367 G_CALLBACK (on_notebook_page_added), dialog);
368 g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
369 G_CALLBACK (on_notebook_page_removed), dialog);
370 #endif /* GTK_CHECK_VERSION */
371 g_signal_connect_after (G_OBJECT (priv->notebook), "switch-page",
372 G_CALLBACK (on_notebook_switch_page), dialog);
377 set_property (GObject *object,
382 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
384 switch (property_id) {
386 case PROP_WIZARD_AUTOTITLE:
388 priv->autotitle = g_value_get_boolean (value);
390 if (priv->autotitle &&
393 create_title (MODEST_WIZARD_DIALOG (object));
394 else if (priv->wizard_name)
395 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
399 case PROP_WIZARD_NAME:
401 /* Set new wizard name. This name will appear in titlebar */
402 if (priv->wizard_name)
403 g_free (priv->wizard_name);
405 gchar *str = (gchar *) g_value_get_string (value);
406 g_return_if_fail (str != NULL);
408 priv->wizard_name = g_strdup (str);
410 /* We need notebook in order to create title, since page information
411 is used in title generation */
413 if (priv->notebook && priv->autotitle)
414 create_title (MODEST_WIZARD_DIALOG (object));
418 case PROP_WIZARD_NOTEBOOK: {
420 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
421 g_return_if_fail (book != NULL);
423 priv->notebook = book;
425 /* Set the default properties for the notebook (disable tabs,
426 * and remove borders) to make it look like a nice wizard widget */
427 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
428 gtk_notebook_set_show_border (priv->notebook, FALSE);
429 gtk_box_pack_start (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook), TRUE, TRUE, 0);
431 /* Show the notebook so that a gtk_widget_show on the dialog is
432 * all that is required to display the dialog correctly */
433 gtk_widget_show ( GTK_WIDGET (priv->notebook));
435 /* Update dialog title to reflect current page stats etc */
436 ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);
437 if (priv->wizard_name && priv->autotitle)
438 create_title (wizard_dialog);
440 connect_to_notebook_signals (wizard_dialog);
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
451 get_property (GObject *object,
456 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
458 switch (property_id) {
460 case PROP_WIZARD_NAME:
461 g_value_set_string (value, priv->wizard_name);
464 case PROP_WIZARD_NOTEBOOK:
465 g_value_set_object (value, priv->notebook);
469 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
475 * Creates the title of the dialog taking into account the current
476 * page of the notebook.
479 create_title (ModestWizardDialog *wizard_dialog)
482 ModestWizardDialogPrivate *priv = NULL;
483 GtkNotebook *notebook = NULL;
487 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
488 g_return_if_fail (wizard_dialog->priv != NULL);
490 priv = wizard_dialog->priv;
491 notebook = priv->notebook;
496 /* Get page information, we'll need that when creating title */
497 pages = gtk_notebook_get_n_pages (notebook);
501 current = gtk_notebook_get_current_page (priv->notebook);
505 steps = gtk_notebook_get_tab_label_text (notebook,
506 gtk_notebook_get_nth_page (notebook, current));
508 str = g_strdup_printf ((steps&&*steps)?_HL("%s%s %s"):_HL("%s"),
509 priv->wizard_name, _HL("ecdg_ti_caption_separator"),
512 /* Update the dialog to display the generated title */
513 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
518 * Response signal handler. This function is needed because GtkDialog's
519 * handler for this signal closes the dialog and we don't want that, we
520 * want to change pages and, dim certain response buttons. Overriding the
521 * virtual function would not work because that would be called after the
522 * signal handler implemented by GtkDialog.
523 * FIXME: There is a much saner way to do that [MDK]
526 response (ModestWizardDialog *wizard_dialog,
530 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
531 GtkNotebook *notebook = priv->notebook;
533 gboolean is_first, is_last;
535 if (priv->override_func) {
536 if (priv->override_func (wizard_dialog, response_id, gtk_notebook_get_current_page (notebook))) {
537 /* Don't let the dialog close */
538 g_signal_stop_emission_by_name (wizard_dialog, "response");
540 /* Force refresh of title */
542 create_title (wizard_dialog);
547 switch (response_id) {
549 case MODEST_WIZARD_DIALOG_PREVIOUS:
550 gtk_notebook_prev_page (notebook); /* go to previous page */
553 case MODEST_WIZARD_DIALOG_NEXT:
554 if (invoke_before_next_vfunc (wizard_dialog))
555 gtk_notebook_next_page (notebook); /* go to next page */
559 case MODEST_WIZARD_DIALOG_CANCEL:
562 case MODEST_WIZARD_DIALOG_FINISH:
563 if (invoke_before_next_vfunc (wizard_dialog))
570 current = gtk_notebook_get_current_page (notebook);
571 gint last = gtk_notebook_get_n_pages (notebook) - 1;
572 is_last = current == last;
573 is_first = current == 0;
575 /* If first page, previous and finish are disabled,
576 if last page, next is disabled */
577 make_buttons_sensitive (wizard_dialog,
578 (is_first) ? FALSE : TRUE,
580 (is_last) ? FALSE : TRUE);
582 /* Allow derived classes to disable buttons to prevent navigation,
583 * according to their own validation logic: */
584 invoke_enable_buttons_vfunc (wizard_dialog);
586 /* Don't let the dialog close */
587 g_signal_stop_emission_by_name (wizard_dialog, "response");
589 /* We show the default image on first and last pages */
590 last = gtk_notebook_get_n_pages (notebook) - 1;
592 if (current == last || current == 0)
593 gtk_widget_show (GTK_WIDGET(priv->image));
595 gtk_widget_hide (GTK_WIDGET(priv->image));
598 /* New page number may appear in the title, update it */
600 create_title (wizard_dialog);
604 * modest_wizard_dialog_new:
605 * @parent: a #GtkWindow
606 * @wizard_name: the name of dialog
607 * @notebook: the notebook to be shown on the dialog
609 * Creates a new #ModestWizardDialog.
611 * Returns: a new #ModestWizardDialog
614 modest_wizard_dialog_new (GtkWindow *parent,
615 const char *wizard_name,
616 GtkNotebook *notebook)
620 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
622 widget = GTK_WIDGET (g_object_new
623 (MODEST_TYPE_WIZARD_DIALOG,
624 "wizard-name", wizard_name,
625 "wizard-notebook", notebook, NULL));
628 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
634 * modest_wizard_dialog_force_title_update:
635 * @wizard_dialog: The wizard dialog
637 * Force the title to be rebuilt, for instance when you have added or
638 * removed notebook pages. This function is not necessary when using GTK+ 2.10,
639 * because that has GtkNotebook signals that will be used to update the title
643 modest_wizard_dialog_force_title_update (ModestWizardDialog *wizard_dialog)
645 create_title (wizard_dialog);
649 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
651 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
653 /* Call the vfunc, which may be overridden by derived classes: */
654 if (klass->before_next) {
655 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
657 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
659 /* Get widgets for the two pages: */
660 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
662 GtkWidget* next_page_widget = NULL;
663 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
664 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
667 g_debug ("Switching to page %d (%s)",
668 gtk_notebook_page_num (priv->notebook, next_page_widget),
669 gtk_notebook_get_tab_label_text (priv->notebook, next_page_widget));
674 g_debug ("\t***************");
675 for (i=0; i<gtk_notebook_get_n_pages(priv->notebook);i++) {
676 p = gtk_notebook_get_nth_page (priv->notebook, i);
677 g_debug ("\t%d - %s", i, gtk_notebook_get_tab_label_text (priv->notebook, p));
679 g_debug ("\t***************");
683 /* Ask the vfunc implementation whether navigation should be allowed: */
684 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
687 /* Allow navigation by default if there is no vfunc implementation: */
692 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
694 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
696 /* Call the vfunc, which may be overridden by derived classes: */
697 if (klass->enable_buttons) {
698 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
700 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
702 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
704 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);
709 invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog)
711 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
713 /* Call the vfunc, which may be overridden by derived classes: */
714 if (klass->update_model) {
715 (*(klass->update_model)) (wizard_dialog);
720 invoke_save_vfunc (ModestWizardDialog *wizard_dialog)
722 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
724 /* Call the vfunc, which may be overridden by derived classes: */
726 return (*(klass->save)) (wizard_dialog);
733 modest_wizard_dialog_set_response_override_handler (ModestWizardDialog *wizard_dialog,
734 ModestWizardDialogResponseOverrideFunc callback)
736 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
738 priv->override_func = callback;
742 modest_wizard_dialog_update_model (ModestWizardDialog *wizard_dialog)
744 g_return_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog));
746 invoke_update_model_vfunc (wizard_dialog);
750 modest_wizard_dialog_save (ModestWizardDialog *wizard_dialog)
752 g_return_val_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog), FALSE);
754 return invoke_save_vfunc (wizard_dialog);