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 #ifdef MODEST_HAVE_HILDON0_WIDGETS
58 #include <hildon-widgets/hildon-defines.h>
60 #include <hildon/hildon-defines.h>
61 #endif /*MODEST_HAVE_HILDON0_WIDGETS*/
63 #include "modest-wizard-dialog.h"
67 /* Specify the hildon-libs translation domain,
68 * so we can reuse its translations
69 * instead of repeating them in our own translations.
71 /* #define _(String) dgettext(PACKAGE, String) */
73 #define _(String) dgettext("hildon-libs", String)
75 static GtkDialogClass *parent_class;
77 static void class_init (ModestWizardDialogClass *wizard_dialog_class);
79 static void init (ModestWizardDialog *wizard_dialog);
81 static void create_title (ModestWizardDialog *wizard_dialog);
83 static void set_property (GObject *object,
88 static void get_property (GObject *object,
93 static void finalize (GObject *object);
95 static void response (ModestWizardDialog *wizard,
99 static void make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
104 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
105 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
110 PROP_WIZARD_NOTEBOOK,
111 PROP_WIZARD_AUTOTITLE
114 struct _ModestWizardDialogPrivate {
116 GtkNotebook *notebook;
124 modest_wizard_dialog_get_type (void)
126 static GType wizard_dialog_type = 0;
128 if (!wizard_dialog_type) {
130 static const GTypeInfo wizard_dialog_info = {
131 sizeof (ModestWizardDialogClass),
132 NULL, /* base_init */
133 NULL, /* base_finalize */
134 (GClassInitFunc) class_init,
135 NULL, /* class_finalize */
136 NULL, /* class_data */
137 sizeof (ModestWizardDialog),
139 (GInstanceInitFunc) init,
142 wizard_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
143 "ModestWizardDialog",
148 return wizard_dialog_type;
152 class_init (ModestWizardDialogClass *wizard_dialog_class)
154 GObjectClass *object_class = G_OBJECT_CLASS (wizard_dialog_class);
156 parent_class = g_type_class_peek_parent (wizard_dialog_class);
158 g_type_class_add_private (wizard_dialog_class,
159 sizeof(ModestWizardDialogPrivate));
161 /* Override virtual methods */
162 object_class->set_property = set_property;
163 object_class->get_property = get_property;
164 object_class->finalize = finalize;
167 * ModestWizardDialog:wizard-name:
169 * The name of the wizard.
171 g_object_class_install_property (object_class, PROP_WIZARD_NAME,
175 "The name of the ModestWizardDialog",
180 * ModestWizardDialog:wizard-notebook:
182 * The notebook object, which is used by the ModestWizardDialog.
184 g_object_class_install_property(object_class, PROP_WIZARD_NOTEBOOK,
188 "GtkNotebook object to be used in the "
189 "ModestWizardDialog",
190 GTK_TYPE_NOTEBOOK, G_PARAM_READWRITE));
193 * ModestWizardDialog:autotitle
195 * If the wizard should automatically try to change the window title when changing steps.
196 * Set to FALSE if you'd like to override the default behaviour.
200 g_object_class_install_property(object_class, PROP_WIZARD_AUTOTITLE,
204 "If the wizard should autotitle itself",
210 finalize (GObject *object)
212 ModestWizardDialog *dialog = MODEST_WIZARD_DIALOG (object);
213 g_return_if_fail (dialog != NULL);
215 if (dialog->priv->wizard_name != NULL)
216 g_free (MODEST_WIZARD_DIALOG (object)->priv->wizard_name);
218 if (G_OBJECT_CLASS (parent_class)->finalize)
219 G_OBJECT_CLASS (parent_class)->finalize(object);
222 /* Disable or enable the Previous, Next and Finish buttons */
224 make_buttons_sensitive (ModestWizardDialog *wizard_dialog,
229 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
230 MODEST_WIZARD_DIALOG_PREVIOUS,
233 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
234 MODEST_WIZARD_DIALOG_FINISH,
237 gtk_dialog_set_response_sensitive (GTK_DIALOG (wizard_dialog),
238 MODEST_WIZARD_DIALOG_NEXT,
243 init (ModestWizardDialog *wizard_dialog)
245 /* Initialize private structure for faster member access */
246 ModestWizardDialogPrivate *priv =
247 G_TYPE_INSTANCE_GET_PRIVATE (wizard_dialog,
248 MODEST_TYPE_WIZARD_DIALOG,
249 ModestWizardDialogPrivate);
251 GtkDialog *dialog = GTK_DIALOG (wizard_dialog);
253 /* Init internal widgets */
254 GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
255 gtk_dialog_set_has_separator (dialog, FALSE);
256 wizard_dialog->priv = priv;
257 priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
258 #ifdef MODEST_HAVE_HILDON0_WIDGETS
259 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
260 HILDON_ICON_SIZE_WIDG_WIZARD);
262 static int icon_size = 0;
264 icon_size = gtk_icon_size_register("modest_wizard", 50, 50);
265 priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
267 #endif /*MODEST_HILDON_VERSION_0*/
268 /* Default values for user provided properties */
269 priv->notebook = NULL;
270 priv->wizard_name = NULL;
271 priv->autotitle = TRUE;
273 /* Build wizard layout */
274 gtk_box_pack_start_defaults (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box));
275 gtk_box_pack_start_defaults (GTK_BOX (priv->box), GTK_WIDGET (vbox));
276 gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), FALSE, FALSE, 0);
278 /* Add response buttons: finish, previous, next, cancel */
279 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_finish"), MODEST_WIZARD_DIALOG_FINISH);
280 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
281 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_next"), MODEST_WIZARD_DIALOG_NEXT);
282 gtk_dialog_add_button (dialog, _("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
284 /* Set initial button states: previous and finish buttons are disabled */
285 make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
287 /* Show all the internal widgets */
288 gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
290 /* connect to dialog's response signal */
291 g_signal_connect (G_OBJECT (dialog), "response",
292 G_CALLBACK (response), NULL);
295 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
296 static void on_notebook_page_added(GtkNotebook *notebook,
301 ModestWizardDialog* dialog = NULL;
303 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(user_data));
304 dialog = MODEST_WIZARD_DIALOG(user_data);
306 /* The title should show the total number of pages: */
307 create_title (dialog);
310 static void on_notebook_page_removed(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);
323 #endif /* GTK_CHECK_VERSION */
326 connect_to_notebook_signals(ModestWizardDialog* dialog)
328 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
329 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(dialog)->priv;
330 g_return_if_fail (priv->notebook);
332 /* Connect to the notebook signals,
333 * so we can update the title when necessary: */
334 g_signal_connect (G_OBJECT (priv->notebook), "page-added",
335 G_CALLBACK (on_notebook_page_added), dialog);
336 g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
337 G_CALLBACK (on_notebook_page_removed), dialog);
338 #endif /* GTK_CHECK_VERSION */
343 set_property (GObject *object,
348 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(object)->priv;
350 switch (property_id) {
352 case PROP_WIZARD_AUTOTITLE:
354 priv->autotitle = g_value_get_boolean (value);
356 if (priv->autotitle &&
359 create_title (MODEST_WIZARD_DIALOG (object));
360 else if (priv->wizard_name)
361 gtk_window_set_title (GTK_WINDOW (object), priv->wizard_name);
365 case PROP_WIZARD_NAME:
367 /* Set new wizard name. This name will appear in titlebar */
368 if (priv->wizard_name)
369 g_free (priv->wizard_name);
371 gchar *str = (gchar *) g_value_get_string (value);
372 g_return_if_fail (str != NULL);
374 priv->wizard_name = g_strdup (str);
376 /* We need notebook in order to create title, since page information
377 is used in title generation */
379 if (priv->notebook && priv->autotitle)
380 create_title (MODEST_WIZARD_DIALOG (object));
384 case PROP_WIZARD_NOTEBOOK: {
386 GtkNotebook *book = GTK_NOTEBOOK (g_value_get_object (value));
387 g_return_if_fail (book != NULL);
389 priv->notebook = book;
391 /* Set the default properties for the notebook (disable tabs,
392 * and remove borders) to make it look like a nice wizard widget */
393 gtk_notebook_set_show_tabs (priv->notebook, FALSE);
394 gtk_notebook_set_show_border (priv->notebook, FALSE);
395 gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
397 /* Show the notebook so that a gtk_widget_show on the dialog is
398 * all that is required to display the dialog correctly */
399 gtk_widget_show ( GTK_WIDGET (priv->notebook));
401 /* Update dialog title to reflect current page stats etc */
402 ModestWizardDialog *wizard_dialog = MODEST_WIZARD_DIALOG (object);
403 if (priv->wizard_name && priv->autotitle)
404 create_title (wizard_dialog);
406 connect_to_notebook_signals (wizard_dialog);
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
417 get_property (GObject *object,
422 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG (object)->priv;
424 switch (property_id) {
426 case PROP_WIZARD_NAME:
427 g_value_set_string (value, priv->wizard_name);
430 case PROP_WIZARD_NOTEBOOK:
431 g_value_set_object (value, priv->notebook);
435 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
441 * Creates the title of the dialog taking into account the current
442 * page of the notebook.
445 create_title (ModestWizardDialog *wizard_dialog)
448 ModestWizardDialogPrivate *priv = NULL;
449 GtkNotebook *notebook = NULL;
451 g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
452 g_return_if_fail (wizard_dialog->priv != NULL);
454 priv = wizard_dialog->priv;
455 notebook = priv->notebook;
460 /* Get page information, we'll need that when creating title */
461 gint pages = gtk_notebook_get_n_pages (notebook);
465 gint current = gtk_notebook_get_current_page (priv->notebook);
469 /* the welcome title on the initial page */
470 /* This is the standard wizard title, with, e.g., 1/4 at the end,
471 * but the Modest UI spec does not want this. */
474 str = g_strdup_printf (_("ecdg_ti_wizard_welcome"),
475 priv->wizard_name, pages);
478 const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
479 gtk_notebook_get_nth_page (notebook, current));
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 (_("ecdg_ti_wizard_step"),
486 priv->wizard_name, current + 1, pages, steps);
489 str = g_strdup_printf (_("%s: %s"),
490 priv->wizard_name, steps);
493 /* Update the dialog to display the generated title */
494 gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
499 * Response signal handler. This function is needed because GtkDialog's
500 * handler for this signal closes the dialog and we don't want that, we
501 * want to change pages and, dim certain response buttons. Overriding the
502 * virtual function would not work because that would be called after the
503 * signal handler implemented by GtkDialog.
504 * FIXME: There is a much saner way to do that [MDK]
507 response (ModestWizardDialog *wizard_dialog,
511 ModestWizardDialogPrivate *priv = wizard_dialog->priv;
512 GtkNotebook *notebook = priv->notebook;
514 gboolean is_first, is_last;
516 switch (response_id) {
518 case MODEST_WIZARD_DIALOG_PREVIOUS:
519 gtk_notebook_prev_page (notebook); /* go to previous page */
522 case MODEST_WIZARD_DIALOG_NEXT:
523 if (invoke_before_next_vfunc (wizard_dialog))
524 gtk_notebook_next_page (notebook); /* go to next page */
528 case MODEST_WIZARD_DIALOG_CANCEL:
531 case MODEST_WIZARD_DIALOG_FINISH:
532 if (invoke_before_next_vfunc (wizard_dialog))
539 current = gtk_notebook_get_current_page (notebook);
540 gint last = gtk_notebook_get_n_pages (notebook) - 1;
541 is_last = current == last;
542 is_first = current == 0;
544 /* If first page, previous and finish are disabled,
545 if last page, next is disabled */
546 make_buttons_sensitive (wizard_dialog,
547 !is_first /* previous */, !is_first /* finish */, !is_last /* next*/);
549 /* Allow derived classes to disable buttons to prevent navigation,
550 * according to their own validation logic: */
551 invoke_enable_buttons_vfunc (wizard_dialog);
553 /* Don't let the dialog close */
554 g_signal_stop_emission_by_name (wizard_dialog, "response");
556 /* We show the default image on first and last pages */
557 last = gtk_notebook_get_n_pages (notebook) - 1;
558 if (current == last || current == 0)
559 gtk_widget_show (GTK_WIDGET(priv->image));
561 gtk_widget_hide (GTK_WIDGET(priv->image));
563 /* New page number may appear in the title, update it */
565 create_title (wizard_dialog);
569 * modest_wizard_dialog_new:
570 * @parent: a #GtkWindow
571 * @wizard_name: the name of dialog
572 * @notebook: the notebook to be shown on the dialog
574 * Creates a new #ModestWizardDialog.
576 * Returns: a new #ModestWizardDialog
579 modest_wizard_dialog_new (GtkWindow *parent,
580 const char *wizard_name,
581 GtkNotebook *notebook)
585 g_return_val_if_fail (GTK_IS_NOTEBOOK (notebook), NULL);
587 widget = GTK_WIDGET (g_object_new
588 (MODEST_TYPE_WIZARD_DIALOG,
589 "wizard-name", wizard_name,
590 "wizard-notebook", notebook, NULL));
593 gtk_window_set_transient_for (GTK_WINDOW (widget), parent);
599 * modest_wizard_dialog_force_title_update:
600 * @wizard_dialog: The wizard dialog
602 * Force the title to be rebuilt, for instance when you have added or
603 * removed notebook pages. This function is not necessary when using GTK+ 2.10,
604 * because that has GtkNotebook signals that will be used to update the title
608 modest_wizard_dialog_force_title_update (ModestWizardDialog *wizard_dialog)
610 create_title (wizard_dialog);
614 invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog)
616 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
618 /* Call the vfunc, which may be overridden by derived classes: */
619 if (klass->before_next) {
620 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
622 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
624 /* Get widgets for the two pages: */
625 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
627 GtkWidget* next_page_widget = NULL;
628 if ((current_page_num + 1) < gtk_notebook_get_n_pages (priv->notebook))
629 next_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num + 1);
631 /* Ask the vfunc implementation whether navigation should be allowed: */
632 return (*(klass->before_next))(wizard_dialog, current_page_widget, next_page_widget);
635 /* Allow navigation by default if there is no vfunc implementation: */
640 invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
642 ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
644 /* Call the vfunc, which may be overridden by derived classes: */
645 if (klass->enable_buttons) {
646 ModestWizardDialogPrivate *priv = MODEST_WIZARD_DIALOG(wizard_dialog)->priv;
648 gint current_page_num = gtk_notebook_get_current_page (priv->notebook);
650 GtkWidget* current_page_widget = gtk_notebook_get_nth_page (priv->notebook, current_page_num);
652 (*(klass->enable_buttons))(wizard_dialog, current_page_widget);