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 #if MODEST_HILDON_API == 0
59 #include <hildon-widgets/hildon-defines.h>
61 #include <hildon/hildon-defines.h>
62 #endif /*MODEST_HILDON_API == 0*/
63 #endif /*!MODEST_TOOLKIT_GTK*/
65 #include "modest-wizard-dialog.h"
66 #include "modest-debug.h"
67 #include "modest-text-utils.h"
69 static GtkDialogClass *parent_class;
71 static void class_init (ModestWizardDialogClass *wizard_dialog_class);
73 static void init (ModestWizardDialog *wizard_dialog);
75 static void create_title (ModestWizardDialog *wizard_dialog);
77 static void set_property (GObject *object,
82 static void get_property (GObject *object,
87 static void finalize (GObject *object);
89 static void response (ModestWizardDialog *wizard,
93 static void make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
98 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
99 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
104 PROP_WIZARD_NOTEBOOK,
105 PROP_WIZARD_AUTOTITLE
108 struct _ModestWizardDialogPrivate {
110 GtkNotebook *notebook;
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;
161 * ModestWizardDialog:wizard-name:
163 * The name of the wizard.
165 g_object_class_install_property (object_class, PROP_WIZARD_NAME,
169 "The name of the ModestWizardDialog",
174 * ModestWizardDialog:wizard-notebook:
176 * The notebook object, which is used by the ModestWizardDialog.
178 g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
182 "GtkNotebook object to be used in the "
183 "ModestWizardDialog",
184 GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
187 * ModestWizardDialog:autotitle
189 * If the wizard should automatically try to change the window title when changing steps.
190 * Set to FALSE if you'd like to override the default behaviour.
194 g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
198 "If the wizard should autotitle itself",
204 finalize (GObject *object)
206 ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
207 g_return_if_fail (dialog != NULL);
209 if (dialog->priv->wizard_name != NULL)
210 g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
212 if (G_OBJECT_CLASS (parent_class)->finalize)
213 G_OBJECT_CLASS (parent_class)->finalize(object);
216 /* Disable or enable the Previous, Next and Finish buttons */
218 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
223 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
224 MODEST_WIZARD_DIALOG_PREVIOUS,
227 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
228 MODEST_WIZARD_DIALOG_FINISH,
231 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
232 MODEST_WIZARD_DIALOG_NEXT,
237 init (ModestWizardDialog *wizard_dialog)
239 /* Initialize private structure for faster member access */
240 ModestWizardDialogPrivate *priv =
241 G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
242 MODEST_TYPE_WIZARD_DIALOG,
243 ModestWizardDialogPrivate);
245 GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
247 /* Init internal widgets */
248 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
249 gtk_dialog_set_has_separator (dialog, FALSE);
250 wizard_dialog->priv = priv;
251 priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
252 #ifndef MODEST_TOOLKIT_GTK
253 #if MODEST_HILDON_API == 0
254 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
255 HILDON_ICON_SIZE_WIDG_WIZARD);
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_HILDON_API == 0*/
263 #else /*MODEST_TOOLKIT_GTK*/
264 priv->image = gtk_image_new_from_stock (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG);
265 #endif /*!MODEST_TOOLKIT_GTK*/
266 /* Default values for user provided properties */
267 priv->notebook = NULL;
268 priv->wizard_name = NULL;
269 priv->autotitle = TRUE;
271 /* Build wizard layout */
272 gtk_box_pack_start_defaults (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box));
273 gtk_box_pack_start_defaults (GTK_BOX (priv->box), GTK_WIDGET (vbox));
274 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), FALSE, FALSE, 0);
276 /* Add response buttons: finish, previous, next, cancel */
277 #ifdef MODEST_TOOLKIT_HILDON1
278 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
279 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
280 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
281 gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
283 #ifdef MODEST_TOOLKIT_HILDON2
284 gtk_dialog_add_button (dialog, _HL("wdgt_bd_finish "), MODEST_WIZARD_DIALOG_FINISH);
285 gtk_dialog_add_button (dialog, _HL("wdgt_bd_previous "), MODEST_WIZARD_DIALOG_PREVIOUS);
286 gtk_dialog_add_button (dialog, _HL("wdgt_bd_next "), MODEST_WIZARD_DIALOG_NEXT);
288 #ifdef MODEST_TOOLKIT_GTK
289 gtk_dialog_add_button (dialog, GTK_STOCK_SAVE, MODEST_WIZARD_DIALOG_FINISH);
290 gtk_dialog_add_button (dialog, GTK_STOCK_GO_BACK, MODEST_WIZARD_DIALOG_PREVIOUS);
291 gtk_dialog_add_button (dialog, GTK_STOCK_GO_FORWARD, MODEST_WIZARD_DIALOG_NEXT);
292 gtk_dialog_add_button (dialog, GTK_STOCK_CANCEL, MODEST_WIZARD_DIALOG_CANCEL);
295 /* Set initial button states: previous and finish buttons are disabled */
296 make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
298 /* Show all the internal widgets */
299 gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
301 /* connect to dialog's response signal */
302 g_signal_connect (G_OBJECT (dialog), "response",
303 G_CALLBACK (response), NULL);
306 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
307 static void on_notebook_page_added(GtkNotebook *notebook,
312 ModestWizardDialog* dialog = NULL;
314 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
315 dialog = MODEST_WIZARD_DIALOG(user_data);
317 /* The title should show the total number of pages: */
318 create_title (dialog);
321 static void on_notebook_page_removed(GtkNotebook *notebook,
326 ModestWizardDialog* dialog = NULL;
328 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
329 dialog = MODEST_WIZARD_DIALOG(user_data);
331 /* The title should show the total number of pages: */
332 create_title (dialog);
334 #endif /* GTK_CHECK_VERSION */
337 connect_to_notebook_signals(ModestWizardDialog* dialog)
339 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
340 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
341 g_return_if_fail (priv->notebook);
343 /* Connect to the notebook signals,
344 * so we can update the title when necessary: */
345 g_signal_connect (G_OBJECT (priv->notebook), "page-added",
346 G_CALLBACK (on_notebook_page_added), dialog);
347 g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
348 G_CALLBACK (on_notebook_page_removed), dialog);
349 #endif /* GTK_CHECK_VERSION */
354 set_property (GObject *object,
359 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
361 switch (property_id) {
363 case PROP_WIZARD_AUTOTITLE:
365 priv->autotitle = g_value_get_boolean (value);
367 if (priv->autotitle &&
370 create_title (MODEST_WIZARD_DIALOG (object));
371 else if (priv->wizard_name)
372 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
376 case PROP_WIZARD_NAME:
378 /* Set new wizard name. This name will appear in titlebar */
379 if (priv->wizard_name)
380 g_free (priv->wizard_name);
382 gchar *str = (gchar *) g_value_get_string (value);
383 g_return_if_fail (str != NULL);
385 priv->wizard_name = g_strdup (str);
387 /* We need notebook in order to create title, since page information
388 is used in title generation */
390 if (priv->notebook && priv->autotitle)
391 create_title (MODEST_WIZARD_DIALOG (object));
395 case PROP_WIZARD_NOTEBOOK: {
397 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
398 g_return_if_fail (book != NULL);
400 priv->notebook = book;
402 /* Set the default properties for the notebook (disable tabs,
403 * and remove borders) to make it look like a nice wizard widget */
404 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
405 gtk_notebook_set_show_border (priv->notebook, FALSE);
406 gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
408 /* Show the notebook so that a gtk_widget_show on the dialog is
409 * all that is required to display the dialog correctly */
410 gtk_widget_show ( GTK_WIDGET (priv->notebook));
412 /* Update dialog title to reflect current page stats etc */
413 ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);
414 if (priv->wizard_name && priv->autotitle)
415 create_title (wizard_dialog);
417 connect_to_notebook_signals (wizard_dialog);
422 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
428 get_property (GObject *object,
433 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
435 switch (property_id) {
437 case PROP_WIZARD_NAME:
438 g_value_set_string (value, priv->wizard_name);
441 case PROP_WIZARD_NOTEBOOK:
442 g_value_set_object (value, priv->notebook);
446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
452 * Creates the title of the dialog taking into account the current
453 * page of the notebook.
456 create_title (ModestWizardDialog *wizard_dialog)
459 ModestWizardDialogPrivate *priv = NULL;
460 GtkNotebook *notebook = NULL;
462 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
463 g_return_if_fail (wizard_dialog->priv != NULL);
465 priv = wizard_dialog->priv;
466 notebook = priv->notebook;
471 /* Get page information, we'll need that when creating title */
472 gint pages = gtk_notebook_get_n_pages (notebook);
476 gint current = gtk_notebook_get_current_page (priv->notebook);
480 /* the welcome title on the initial page */
481 /* This is the standard wizard title, with, e.g., 1/4 at the end,
482 * but the Modest UI spec does not want this. */
485 str = g_strdup_printf (_HL("ecdg_ti_wizard_welcome"),
486 priv->wizard_name, pages);
489 const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
490 gtk_notebook_get_nth_page (notebook, current));
492 /* This is the standard wizard title, with, e.g., 1/4 at the end,
493 * but the Modest UI spec does not want this.
496 str = g_strdup_printf (_HL("ecdg_ti_wizard_step"),
497 priv->wizard_name, current + 1, pages, steps);
500 str = g_strdup_printf (_HL("%s: %s"),
501 priv->wizard_name, steps);
504 /* Update the dialog to display the generated title */
505 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
510 * Response signal handler. This function is needed because GtkDialog's
511 * handler for this signal closes the dialog and we don't want that, we
512 * want to change pages and, dim certain response buttons. Overriding the
513 * virtual function would not work because that would be called after the
514 * signal handler implemented by GtkDialog.
515 * FIXME: There is a much saner way to do that [MDK]
518 response (ModestWizardDialog *wizard_dialog,
522 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
523 GtkNotebook *notebook = priv->notebook;
525 gboolean is_first, is_last;
527 switch (response_id) {
529 case MODEST_WIZARD_DIALOG_PREVIOUS:
530 gtk_notebook_prev_page (notebook); /* go to previous page */
533 case MODEST_WIZARD_DIALOG_NEXT:
534 if (invoke_before_next_vfunc (wizard_dialog))
535 gtk_notebook_next_page (notebook); /* go to next page */
539 case MODEST_WIZARD_DIALOG_CANCEL:
542 case MODEST_WIZARD_DIALOG_FINISH:
543 if (invoke_before_next_vfunc (wizard_dialog))
550 current = gtk_notebook_get_current_page (notebook);
551 gint last = gtk_notebook_get_n_pages (notebook) - 1;
552 is_last = current == last;
553 is_first = current == 0;
555 /* If first page, previous and finish are disabled,
556 if last page, next is disabled */
557 make_buttons_sensitive (wizard_dialog,
558 !is_first /* previous */, !is_first /* finish */, !is_last /* next*/);
560 /* Allow derived classes to disable buttons to prevent navigation,
561 * according to their own validation logic: */
562 invoke_enable_buttons_vfunc (wizard_dialog);
564 /* Don't let the dialog close */
565 g_signal_stop_emission_by_name (wizard_dialog, "response");
567 /* We show the default image on first and last pages */
568 last = gtk_notebook_get_n_pages (notebook) - 1;
569 if (current == last || current == 0)
570 gtk_widget_show (GTK_WIDGET(priv->image));
572 gtk_widget_hide (GTK_WIDGET(priv->image));
574 /* New page number may appear in the title, update it */
576 create_title (wizard_dialog);
580 * modest_wizard_dialog_new:
581 * @parent: a #GtkWindow
582 * @wizard_name: the name of dialog
583 * @notebook: the notebook to be shown on the dialog
585 * Creates a new #ModestWizardDialog.
587 * Returns: a new #ModestWizardDialog
590 modest_wizard_dialog_new (GtkWindow *parent,
591 const char *wizard_name,
592 GtkNotebook *notebook)
596 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
598 widget = GTK_WIDGET (g_object_new
599 (MODEST_TYPE_WIZARD_DIALOG,
600 "wizard-name", wizard_name,
601 "wizard-notebook", notebook, NULL));
604 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
610 * modest_wizard_dialog_force_title_update:
611 * @wizard_dialog: The wizard dialog
613 * Force the title to be rebuilt, for instance when you have added or
614 * removed notebook pages. This function is not necessary when using GTK+ 2.10,
615 * because that has GtkNotebook signals that will be used to update the title
619 modest_wizard_dialog_force_title_update (ModestWizardDialog *wizard_dialog)
621 create_title (wizard_dialog);
625 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
627 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
629 /* Call the vfunc, which may be overridden by derived classes: */
630 if (klass->before_next) {
631 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
633 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
635 /* Get widgets for the two pages: */
636 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
638 GtkWidget* next_page_widget = NULL;
639 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
640 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
643 g_debug ("Switching to page %d (%s)",
644 gtk_notebook_page_num (priv->notebook, next_page_widget),
645 gtk_notebook_get_tab_label_text (priv->notebook, next_page_widget));
650 g_debug ("\t***************");
651 for (i=0; i<gtk_notebook_get_n_pages(priv->notebook);i++) {
652 p = gtk_notebook_get_nth_page (priv->notebook, i);
653 g_debug ("\t%d - %s", i, gtk_notebook_get_tab_label_text (priv->notebook, p));
655 g_debug ("\t***************");
659 /* Ask the vfunc implementation whether navigation should be allowed: */
660 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
663 /* Allow navigation by default if there is no vfunc implementation: */
668 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
670 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
672 /* Call the vfunc, which may be overridden by derived classes: */
673 if (klass->enable_buttons) {
674 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
676 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
678 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
680 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);