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 #ifndef MODEST_TOOLKIT_GTK
51 #if MODEST_HILDON_API == 0
52 #include <hildon-widgets/hildon-defines.h>
54 #include <hildon/hildon-defines.h>
55 #endif /*MODEST_HILDON_API == 0*/
56 #endif /*!MODEST_TOOLKIT_GTK*/
58 #include "modest-wizard-dialog.h"
59 #include "modest-debug.h"
60 #include "modest-text-utils.h"
62 static GtkDialogClass *parent_class;
64 static void class_init (ModestWizardDialogClass *wizard_dialog_class);
66 static void init (ModestWizardDialog *wizard_dialog);
68 static void create_title (ModestWizardDialog *wizard_dialog);
70 static void set_property (GObject *object,
75 static void get_property (GObject *object,
80 static void finalize (GObject *object);
82 static void response (ModestWizardDialog *wizard,
86 static void make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
91 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
92 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
93 static void invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog);
94 static gboolean invoke_save_vfunc (ModestWizardDialog *wizard_dialog);
100 PROP_WIZARD_AUTOTITLE
103 struct _ModestWizardDialogPrivate {
105 GtkNotebook *notebook;
110 ModestWizardDialogResponseOverrideFunc override_func;
115 modest_wizard_dialog_get_type (void)
117 static GType wizard_dialog_type = 0;
119 if (!wizard_dialog_type) {
121 static const GTypeInfo wizard_dialog_info = {
122 sizeof (ModestWizardDialogClass),
123 NULL, /* base_init */
124 NULL, /* base_finalize */
125 (GClassInitFunc) class_init,
126 NULL, /* class_finalize */
127 NULL, /* class_data */
128 sizeof (ModestWizardDialog),
130 (GInstanceInitFunc) init,
133 wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
134 "ModestWizardDialog",
139 return wizard_dialog_type;
143 class_init (ModestWizardDialogClass *wizard_dialog_class)
145 GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
147 parent_class = g_type_class_peek_parent (wizard_dialog_class);
149 g_type_class_add_private (wizard_dialog_class,
150 sizeof(ModestWizardDialogPrivate));
152 /* Override virtual methods */
153 object_class->set_property = set_property;
154 object_class->get_property = get_property;
155 object_class->finalize = finalize;
157 wizard_dialog_class->before_next = NULL;
158 wizard_dialog_class->update_model = NULL;
159 wizard_dialog_class->save = NULL;
160 wizard_dialog_class->enable_buttons = NULL;
163 * ModestWizardDialog:wizard-name:
165 * The name of the wizard.
167 g_object_class_install_property (object_class, PROP_WIZARD_NAME,
171 "The name of the ModestWizardDialog",
176 * ModestWizardDialog:wizard-notebook:
178 * The notebook object, which is used by the ModestWizardDialog.
180 g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
184 "GtkNotebook object to be used in the "
185 "ModestWizardDialog",
186 GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
189 * ModestWizardDialog:autotitle
191 * If the wizard should automatically try to change the window title when changing steps.
192 * Set to FALSE if you'd like to override the default behaviour.
196 g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
200 "If the wizard should autotitle itself",
206 finalize (GObject *object)
208 ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
209 g_return_if_fail (dialog != NULL);
211 if (dialog->priv->wizard_name != NULL)
212 g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
214 if (G_OBJECT_CLASS (parent_class)->finalize)
215 G_OBJECT_CLASS (parent_class)->finalize(object);
218 /* Disable or enable the Previous, Next and Finish buttons */
220 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
225 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
226 MODEST_WIZARD_DIALOG_PREVIOUS,
229 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
230 MODEST_WIZARD_DIALOG_FINISH,
233 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
234 MODEST_WIZARD_DIALOG_NEXT,
239 init (ModestWizardDialog *wizard_dialog)
241 /* Initialize private structure for faster member access */
242 ModestWizardDialogPrivate *priv =
243 G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
244 MODEST_TYPE_WIZARD_DIALOG,
245 ModestWizardDialogPrivate);
247 GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
249 /* Init internal widgets */
250 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
251 gtk_dialog_set_has_separator (dialog, FALSE);
252 wizard_dialog->priv = priv;
253 priv->override_func = NULL;
254 priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
255 #ifdef MODEST_TOOLKIT_HILDON2
258 #ifdef MODEST_TOOLKIT_GTK
259 priv->image = gtk_image_new_from_stock (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG);
260 #else /*MODEST_TOOLKIT_GTK*/
261 #if MODEST_HILDON_API == 0
262 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
263 HILDON_ICON_SIZE_WIDG_WIZARD);
265 static int icon_size = 0;
267 icon_size = gtk_icon_size_register("modest_wizard", 50, 50);
268 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
270 #endif /*MODEST_HILDON_API == 0*/
271 #endif /*!MODEST_TOOLKIT_GTK*/
272 #endif /*MODEST_TOOLKIT_HILDON2 */
273 /* Default values for user provided properties */
274 priv->notebook = NULL;
275 priv->wizard_name = NULL;
276 priv->autotitle = TRUE;
278 /* Build wizard layout */
279 gtk_box_pack_start (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box), TRUE, TRUE, 0);
280 gtk_box_pack_start (GTK_BOX (priv->box), GTK_WIDGET (vbox), FALSE, FALSE, 0);
281 gtk_widget_show (vbox);
282 gtk_widget_show (GTK_WIDGET (priv->box));
284 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), TRUE, TRUE, 0);
285 gtk_widget_show (priv->image);
288 /* Add response buttons: finish, previous, next, cancel */
289 #ifdef MODEST_TOOLKIT_HILDON1
290 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
291 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
292 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
293 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
295 #ifdef MODEST_TOOLKIT_HILDON2
296 gtk_dialog_add_button (dialog, _HL("wdgt_bd_finish"), MODEST_WIZARD_DIALOG_FINISH);
297 gtk_dialog_add_button (dialog, _HL("wdgt_bd_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
298 gtk_dialog_add_button (dialog, _HL("wdgt_bd_next"), MODEST_WIZARD_DIALOG_NEXT);
300 #ifdef MODEST_TOOLKIT_GTK
301 gtk_dialog_add_button (dialog, GTK_STOCK_SAVE, MODEST_WIZARD_DIALOG_FINISH);
302 gtk_dialog_add_button (dialog, GTK_STOCK_GO_BACK, MODEST_WIZARD_DIALOG_PREVIOUS);
303 gtk_dialog_add_button (dialog, GTK_STOCK_GO_FORWARD, MODEST_WIZARD_DIALOG_NEXT);
304 gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, MODEST_WIZARD_DIALOG_CANCEL);
307 /* Set initial button states: previous and finish buttons are disabled */
308 make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
310 gtk_widget_show (GTK_WIDGET (dialog->vbox));
312 /* connect to dialog's response signal */
313 g_signal_connect (G_OBJECT (dialog), "response",
314 G_CALLBACK (response), NULL);
318 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
319 static void on_notebook_page_added(GtkNotebook *notebook,
324 ModestWizardDialog* dialog = NULL;
326 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
327 dialog = MODEST_WIZARD_DIALOG(user_data);
329 /* The title should show the total number of pages: */
330 create_title (dialog);
333 static void on_notebook_page_removed(GtkNotebook *notebook,
338 ModestWizardDialog* dialog = NULL;
340 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
341 dialog = MODEST_WIZARD_DIALOG(user_data);
343 /* The title should show the total number of pages: */
344 create_title (dialog);
346 #endif /* GTK_CHECK_VERSION */
349 on_notebook_switch_page (GtkNotebook *notebook,
350 GtkNotebookPage *page,
352 ModestWizardDialog *self)
354 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(self));
360 connect_to_notebook_signals(ModestWizardDialog* dialog)
362 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
363 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
364 g_return_if_fail (priv->notebook);
366 /* Connect to the notebook signals,
367 * so we can update the title when necessary: */
368 g_signal_connect (G_OBJECT (priv->notebook), "page-added",
369 G_CALLBACK (on_notebook_page_added), dialog);
370 g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
371 G_CALLBACK (on_notebook_page_removed), dialog);
372 #endif /* GTK_CHECK_VERSION */
373 g_signal_connect_after (G_OBJECT (priv->notebook), "switch-page",
374 G_CALLBACK (on_notebook_switch_page), dialog);
379 set_property (GObject *object,
384 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
386 switch (property_id) {
388 case PROP_WIZARD_AUTOTITLE:
390 priv->autotitle = g_value_get_boolean (value);
392 if (priv->autotitle &&
395 create_title (MODEST_WIZARD_DIALOG (object));
396 else if (priv->wizard_name)
397 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
401 case PROP_WIZARD_NAME:
403 /* Set new wizard name. This name will appear in titlebar */
404 if (priv->wizard_name)
405 g_free (priv->wizard_name);
407 gchar *str = (gchar *) g_value_get_string (value);
408 g_return_if_fail (str != NULL);
410 priv->wizard_name = g_strdup (str);
412 /* We need notebook in order to create title, since page information
413 is used in title generation */
415 if (priv->notebook && priv->autotitle)
416 create_title (MODEST_WIZARD_DIALOG (object));
420 case PROP_WIZARD_NOTEBOOK: {
422 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
423 g_return_if_fail (book != NULL);
425 priv->notebook = book;
427 /* Set the default properties for the notebook (disable tabs,
428 * and remove borders) to make it look like a nice wizard widget */
429 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
430 gtk_notebook_set_show_border (priv->notebook, FALSE);
431 gtk_box_pack_start (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook), TRUE, TRUE, 0);
433 /* Show the notebook so that a gtk_widget_show on the dialog is
434 * all that is required to display the dialog correctly */
435 gtk_widget_show ( GTK_WIDGET (priv->notebook));
437 /* Update dialog title to reflect current page stats etc */
438 ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);
439 if (priv->wizard_name && priv->autotitle)
440 create_title (wizard_dialog);
442 connect_to_notebook_signals (wizard_dialog);
447 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
453 get_property (GObject *object,
458 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
460 switch (property_id) {
462 case PROP_WIZARD_NAME:
463 g_value_set_string (value, priv->wizard_name);
466 case PROP_WIZARD_NOTEBOOK:
467 g_value_set_object (value, priv->notebook);
471 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
477 * Creates the title of the dialog taking into account the current
478 * page of the notebook.
481 create_title (ModestWizardDialog *wizard_dialog)
484 ModestWizardDialogPrivate *priv = NULL;
485 GtkNotebook *notebook = NULL;
489 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
490 g_return_if_fail (wizard_dialog->priv != NULL);
492 priv = wizard_dialog->priv;
493 notebook = priv->notebook;
498 /* Get page information, we'll need that when creating title */
499 pages = gtk_notebook_get_n_pages (notebook);
503 current = gtk_notebook_get_current_page (priv->notebook);
507 steps = gtk_notebook_get_tab_label_text (notebook,
508 gtk_notebook_get_nth_page (notebook, current));
510 str = g_strdup_printf ((steps&&*steps)?_HL("%s%s %s"):_HL("%s"),
511 priv->wizard_name, _HL("ecdg_ti_caption_separator"),
514 /* Update the dialog to display the generated title */
515 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
520 * Response signal handler. This function is needed because GtkDialog's
521 * handler for this signal closes the dialog and we don't want that, we
522 * want to change pages and, dim certain response buttons. Overriding the
523 * virtual function would not work because that would be called after the
524 * signal handler implemented by GtkDialog.
525 * FIXME: There is a much saner way to do that [MDK]
528 response (ModestWizardDialog *wizard_dialog,
532 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
533 GtkNotebook *notebook = priv->notebook;
535 gboolean is_first, is_last;
537 if (priv->override_func) {
538 if (priv->override_func (wizard_dialog, response_id, gtk_notebook_get_current_page (notebook))) {
539 /* Don't let the dialog close */
540 g_signal_stop_emission_by_name (wizard_dialog, "response");
542 /* Force refresh of title */
544 create_title (wizard_dialog);
549 switch (response_id) {
551 case MODEST_WIZARD_DIALOG_PREVIOUS:
552 gtk_notebook_prev_page (notebook); /* go to previous page */
555 case MODEST_WIZARD_DIALOG_NEXT:
556 if (invoke_before_next_vfunc (wizard_dialog))
557 gtk_notebook_next_page (notebook); /* go to next page */
561 case MODEST_WIZARD_DIALOG_CANCEL:
564 case MODEST_WIZARD_DIALOG_FINISH:
565 if (invoke_before_next_vfunc (wizard_dialog))
572 current = gtk_notebook_get_current_page (notebook);
573 gint last = gtk_notebook_get_n_pages (notebook) - 1;
574 is_last = current == last;
575 is_first = current == 0;
577 /* If first page, previous and finish are disabled,
578 if last page, next is disabled */
579 make_buttons_sensitive (wizard_dialog,
580 (is_first) ? FALSE : TRUE,
582 (is_last) ? FALSE : TRUE);
584 /* Allow derived classes to disable buttons to prevent navigation,
585 * according to their own validation logic: */
586 invoke_enable_buttons_vfunc (wizard_dialog);
588 /* Don't let the dialog close */
589 g_signal_stop_emission_by_name (wizard_dialog, "response");
591 /* We show the default image on first and last pages */
592 last = gtk_notebook_get_n_pages (notebook) - 1;
594 if (current == last || current == 0)
595 gtk_widget_show (GTK_WIDGET(priv->image));
597 gtk_widget_hide (GTK_WIDGET(priv->image));
600 /* New page number may appear in the title, update it */
602 create_title (wizard_dialog);
606 * modest_wizard_dialog_new:
607 * @parent: a #GtkWindow
608 * @wizard_name: the name of dialog
609 * @notebook: the notebook to be shown on the dialog
611 * Creates a new #ModestWizardDialog.
613 * Returns: a new #ModestWizardDialog
616 modest_wizard_dialog_new (GtkWindow *parent,
617 const char *wizard_name,
618 GtkNotebook *notebook)
622 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
624 widget = GTK_WIDGET (g_object_new
625 (MODEST_TYPE_WIZARD_DIALOG,
626 "wizard-name", wizard_name,
627 "wizard-notebook", notebook, NULL));
630 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
636 * modest_wizard_dialog_force_title_update:
637 * @wizard_dialog: The wizard dialog
639 * Force the title to be rebuilt, for instance when you have added or
640 * removed notebook pages. This function is not necessary when using GTK+ 2.10,
641 * because that has GtkNotebook signals that will be used to update the title
645 modest_wizard_dialog_force_title_update (ModestWizardDialog *wizard_dialog)
647 create_title (wizard_dialog);
651 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
653 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
655 /* Call the vfunc, which may be overridden by derived classes: */
656 if (klass->before_next) {
657 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
659 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
661 /* Get widgets for the two pages: */
662 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
664 GtkWidget* next_page_widget = NULL;
665 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
666 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
669 g_debug ("Switching to page %d (%s)",
670 gtk_notebook_page_num (priv->notebook, next_page_widget),
671 gtk_notebook_get_tab_label_text (priv->notebook, next_page_widget));
676 g_debug ("\t***************");
677 for (i=0; i<gtk_notebook_get_n_pages(priv->notebook);i++) {
678 p = gtk_notebook_get_nth_page (priv->notebook, i);
679 g_debug ("\t%d - %s", i, gtk_notebook_get_tab_label_text (priv->notebook, p));
681 g_debug ("\t***************");
685 /* Ask the vfunc implementation whether navigation should be allowed: */
686 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
689 /* Allow navigation by default if there is no vfunc implementation: */
694 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
696 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
698 /* Call the vfunc, which may be overridden by derived classes: */
699 if (klass->enable_buttons) {
700 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
702 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
704 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
706 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);
711 invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog)
713 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
715 /* Call the vfunc, which may be overridden by derived classes: */
716 if (klass->update_model) {
717 (*(klass->update_model)) (wizard_dialog);
722 invoke_save_vfunc (ModestWizardDialog *wizard_dialog)
724 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
726 /* Call the vfunc, which may be overridden by derived classes: */
728 return (*(klass->save)) (wizard_dialog);
735 modest_wizard_dialog_set_response_override_handler (ModestWizardDialog *wizard_dialog,
736 ModestWizardDialogResponseOverrideFunc callback)
738 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
740 priv->override_func = callback;
744 modest_wizard_dialog_update_model (ModestWizardDialog *wizard_dialog)
746 g_return_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog));
748 invoke_update_model_vfunc (wizard_dialog);
752 modest_wizard_dialog_save (ModestWizardDialog *wizard_dialog)
754 g_return_val_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog), FALSE);
756 return invoke_save_vfunc (wizard_dialog);