Modified webpage: now tinymail repository is in gitorious.
[modest] / src / widgets / modest-wizard-dialog.c
index 975cf7e..c2792d1 100644 (file)
  */
 
 #include <config.h>
-#include <gtk/gtkdialog.h>
-#include <gtk/gtknotebook.h>
-#include <gtk/gtkimage.h>
-#include <gtk/gtkbox.h>
-#include <gtk/gtkhbox.h>
-#include <gtk/gtkvbox.h>
-#include <gtk/gtkbutton.h>
 #include <gtk/gtk.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 
-#ifndef MODEST_TOOLKIT_GTK
-#if MODEST_HILDON_API == 0
-#include <hildon-widgets/hildon-defines.h>
-#else
+#ifdef MODEST_TOOLKIT_HILDON2
 #include <hildon/hildon-defines.h>
-#endif /*MODEST_HILDON_API == 0*/
-#endif /*!MODEST_TOOLKIT_GTK*/
+#endif
 
 #include "modest-wizard-dialog.h"
 #include "modest-debug.h"
@@ -94,9 +83,11 @@ static void make_buttons_sensitive  (ModestWizardDialog *wizard_dialog,
                                      gboolean           previous,
                                      gboolean           finish,
                                      gboolean next);
-                                     
+
 static gboolean invoke_before_next_vfunc (ModestWizardDialog *wizard_dialog);
 static void invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog);
+static void invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog);
+static gboolean invoke_save_vfunc (ModestWizardDialog *wizard_dialog);
 
 enum {
     PROP_ZERO,
@@ -111,6 +102,8 @@ struct _ModestWizardDialogPrivate {
     GtkBox      *box;
     GtkWidget   *image;
     gboolean    autotitle;
+
+    ModestWizardDialogResponseOverrideFunc override_func;
 };
 
 
@@ -157,6 +150,11 @@ class_init (ModestWizardDialogClass *wizard_dialog_class)
     object_class->get_property = get_property;
     object_class->finalize     = finalize;
 
+    wizard_dialog_class->before_next = NULL;
+    wizard_dialog_class->update_model = NULL;
+    wizard_dialog_class->save = NULL;
+    wizard_dialog_class->enable_buttons = NULL;
+
     /**
      * ModestWizardDialog:wizard-name:
      *
@@ -248,6 +246,7 @@ init (ModestWizardDialog *wizard_dialog)
     GtkWidget *vbox = gtk_vbox_new (FALSE, 0);
     gtk_dialog_set_has_separator (dialog, FALSE);
     wizard_dialog->priv = priv;
+    priv->override_func = NULL;
     priv->box = GTK_BOX (gtk_hbox_new (FALSE, 0));
 #ifdef MODEST_TOOLKIT_HILDON2
     priv->image = NULL;
@@ -255,16 +254,11 @@ init (ModestWizardDialog *wizard_dialog)
 #ifdef MODEST_TOOLKIT_GTK
     priv->image = gtk_image_new_from_stock (GTK_STOCK_PREFERENCES, GTK_ICON_SIZE_DIALOG);
 #else /*MODEST_TOOLKIT_GTK*/
-#if MODEST_HILDON_API == 0
-    priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
-                                               HILDON_ICON_SIZE_WIDG_WIZARD);
-#else      
     static int icon_size = 0;
     if (!icon_size)
            icon_size = gtk_icon_size_register("modest_wizard", 50, 50);
     priv->image = gtk_image_new_from_icon_name ("qgn_widg_wizard",
                                                icon_size);
-#endif /*MODEST_HILDON_API == 0*/
 #endif /*!MODEST_TOOLKIT_GTK*/
 #endif /*MODEST_TOOLKIT_HILDON2 */
     /* Default values for user provided properties */
@@ -273,10 +267,14 @@ init (ModestWizardDialog *wizard_dialog)
     priv->autotitle = TRUE;
 
     /* Build wizard layout */
-    gtk_box_pack_start_defaults (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box));
-    gtk_box_pack_start_defaults (GTK_BOX (priv->box), GTK_WIDGET (vbox));
-    if (priv->image)
-      gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), FALSE, FALSE, 0);
+    gtk_box_pack_start (GTK_BOX (dialog->vbox), GTK_WIDGET (priv->box), TRUE, TRUE, 0);
+    gtk_box_pack_start (GTK_BOX (priv->box), GTK_WIDGET (vbox), FALSE, FALSE, 0);
+    gtk_widget_show (vbox);
+    gtk_widget_show (GTK_WIDGET (priv->box));
+    if (priv->image) {
+           gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (priv->image), TRUE, TRUE, 0);
+           gtk_widget_show (priv->image);
+    }
 
     /* Add response buttons: finish, previous, next, cancel */
 #ifdef MODEST_TOOLKIT_HILDON1
@@ -286,9 +284,9 @@ init (ModestWizardDialog *wizard_dialog)
     gtk_dialog_add_button (dialog, _HL("ecdg_bd_wizard_cancel"), MODEST_WIZARD_DIALOG_CANCEL);
 #endif
 #ifdef MODEST_TOOLKIT_HILDON2
-    gtk_dialog_add_button (dialog, _HL("wdgt_bd_finish "), MODEST_WIZARD_DIALOG_FINISH);
-    gtk_dialog_add_button (dialog, _HL("wdgt_bd_previous "), MODEST_WIZARD_DIALOG_PREVIOUS);
-    gtk_dialog_add_button (dialog, _HL("wdgt_bd_next "), MODEST_WIZARD_DIALOG_NEXT);
+    gtk_dialog_add_button (dialog, _HL("wdgt_bd_finish"), MODEST_WIZARD_DIALOG_FINISH);
+    gtk_dialog_add_button (dialog, _HL("wdgt_bd_previous"), MODEST_WIZARD_DIALOG_PREVIOUS);
+    gtk_dialog_add_button (dialog, _HL("wdgt_bd_next"), MODEST_WIZARD_DIALOG_NEXT);
 #endif
 #ifdef MODEST_TOOLKIT_GTK
     gtk_dialog_add_button (dialog, GTK_STOCK_SAVE, MODEST_WIZARD_DIALOG_FINISH);
@@ -300,12 +298,12 @@ init (ModestWizardDialog *wizard_dialog)
     /* Set initial button states: previous and finish buttons are disabled */
     make_buttons_sensitive (wizard_dialog, FALSE, FALSE, TRUE);
 
-    /* Show all the internal widgets */
-    gtk_widget_show_all (GTK_WIDGET (dialog->vbox));
+    gtk_widget_show (GTK_WIDGET (dialog->vbox));
 
     /* connect to dialog's response signal */
     g_signal_connect (G_OBJECT (dialog), "response",
             G_CALLBACK (response), NULL);
+
 }
 
 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
@@ -339,6 +337,17 @@ static void on_notebook_page_removed(GtkNotebook *notebook,
 #endif /* GTK_CHECK_VERSION */
 
 static void
+on_notebook_switch_page (GtkNotebook *notebook,
+                        GtkNotebookPage *page,
+                        guint page_num,
+                        ModestWizardDialog *self)
+{
+       g_return_if_fail (MODEST_IS_WIZARD_DIALOG(self));
+
+       create_title (self);
+}
+
+static void
 connect_to_notebook_signals(ModestWizardDialog* dialog)
 {
 #if GTK_CHECK_VERSION(2, 10, 0) /* These signals were added in GTK+ 2.10: */
@@ -352,6 +361,8 @@ connect_to_notebook_signals(ModestWizardDialog* dialog)
        g_signal_connect (G_OBJECT (priv->notebook), "page-removed",
                      G_CALLBACK (on_notebook_page_removed), dialog);
 #endif /* GTK_CHECK_VERSION */
+       g_signal_connect_after (G_OBJECT (priv->notebook), "switch-page",
+                               G_CALLBACK (on_notebook_switch_page), dialog);
 }
 
 
@@ -408,7 +419,7 @@ set_property (GObject      *object,
              * and remove borders) to make it look like a nice wizard widget */
             gtk_notebook_set_show_tabs (priv->notebook, FALSE);
             gtk_notebook_set_show_border (priv->notebook, FALSE);
-            gtk_box_pack_start_defaults (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook));
+            gtk_box_pack_start (GTK_BOX( priv->box), GTK_WIDGET (priv->notebook), TRUE, TRUE, 0);
 
             /* Show the notebook so that a gtk_widget_show on the dialog is
              * all that is required to display the dialog correctly */
@@ -463,48 +474,33 @@ create_title (ModestWizardDialog *wizard_dialog)
     gchar *str = NULL;
     ModestWizardDialogPrivate *priv = NULL;
     GtkNotebook *notebook = NULL;
+    gint pages, current;
+    const gchar *steps;
 
     g_return_if_fail (MODEST_IS_WIZARD_DIALOG(wizard_dialog));
     g_return_if_fail (wizard_dialog->priv != NULL);
 
-    priv = wizard_dialog->priv;    
+    priv = wizard_dialog->priv;
     notebook = priv->notebook;
 
     if (!notebook)
         return;
 
     /* Get page information, we'll need that when creating title */
-    gint pages = gtk_notebook_get_n_pages (notebook);
+    pages = gtk_notebook_get_n_pages (notebook);
     if (pages == 0)
-      return;
-       
-    gint current = gtk_notebook_get_current_page (priv->notebook);
+           return;
+
+    current = gtk_notebook_get_current_page (priv->notebook);
     if (current < 0)
-        current = 0;
-
-    /* the welcome title on the initial page */
-    /* This is the standard wizard title, with, e.g., 1/4 at the end,
-        * but the Modest UI spec does not want this. */
-       /*
-    if (current == 0) {
-        str = g_strdup_printf (_HL("ecdg_ti_wizard_welcome"), 
-                priv->wizard_name, pages);
-    } else {
-    */
-       const gchar *steps = gtk_notebook_get_tab_label_text (notebook,
-               gtk_notebook_get_nth_page (notebook, current));
-                
-               /* This is the standard wizard title, with, e.g., 1/4 at the end,
-                * but the Modest UI spec does not want this.
-                */
-               /*
-        str = g_strdup_printf (_HL("ecdg_ti_wizard_step"), 
-                priv->wizard_name, current + 1, pages, steps);
-        */
-
-        str = g_strdup_printf (_HL("%s: %s"), 
-                priv->wizard_name, steps);
-    /* } */
+           current = 0;
+
+    steps = gtk_notebook_get_tab_label_text (notebook,
+                                            gtk_notebook_get_nth_page (notebook, current));
+
+    str = g_strdup_printf ((steps&&*steps)?_HL("%s%s %s"):_HL("%s"),
+                          priv->wizard_name, _HL("ecdg_ti_caption_separator"),
+                          steps);
 
     /* Update the dialog to display the generated title */
     gtk_window_set_title (GTK_WINDOW (wizard_dialog), str);
@@ -528,6 +524,18 @@ response (ModestWizardDialog   *wizard_dialog,
     GtkNotebook *notebook = priv->notebook;
     gint current = 0;
     gboolean is_first, is_last;
+
+    if (priv->override_func) {
+           if (priv->override_func (wizard_dialog, response_id, gtk_notebook_get_current_page (notebook))) {
+                   /* Don't let the dialog close */
+                   g_signal_stop_emission_by_name (wizard_dialog, "response");
+                   
+                   /* Force refresh of title */
+                   if (priv->autotitle) 
+                           create_title (wizard_dialog);
+                   return;
+           }
+    }
     
     switch (response_id) {
         
@@ -556,16 +564,18 @@ response (ModestWizardDialog   *wizard_dialog,
     gint last = gtk_notebook_get_n_pages (notebook) - 1;
     is_last = current == last;
     is_first = current == 0;
-    
+
     /* If first page, previous and finish are disabled, 
        if last page, next is disabled */
     make_buttons_sensitive (wizard_dialog,
-            !is_first /* previous */, !is_first /* finish */, !is_last /* next*/);
-            
+                           (is_first) ? FALSE : TRUE,
+                           TRUE,
+                           (is_last) ? FALSE : TRUE);
+
     /* Allow derived classes to disable buttons to prevent navigation,
      * according to their own validation logic: */
     invoke_enable_buttons_vfunc (wizard_dialog);
-    
+
     /* Don't let the dialog close */
     g_signal_stop_emission_by_name (wizard_dialog, "response");
 
@@ -687,3 +697,52 @@ invoke_enable_buttons_vfunc (ModestWizardDialog *wizard_dialog)
                (*(klass->enable_buttons))(wizard_dialog, current_page_widget);
        }
 }
+
+static void
+invoke_update_model_vfunc (ModestWizardDialog *wizard_dialog)
+{
+       ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
+       
+       /* Call the vfunc, which may be overridden by derived classes: */
+       if (klass->update_model) {
+               (*(klass->update_model)) (wizard_dialog);
+       }
+}
+
+static gboolean
+invoke_save_vfunc (ModestWizardDialog *wizard_dialog)
+{
+       ModestWizardDialogClass *klass = MODEST_WIZARD_DIALOG_GET_CLASS (wizard_dialog);
+       
+       /* Call the vfunc, which may be overridden by derived classes: */
+       if (klass->save) {
+               return (*(klass->save)) (wizard_dialog);
+       } else {
+               return TRUE;
+       }
+}
+
+void 
+modest_wizard_dialog_set_response_override_handler (ModestWizardDialog *wizard_dialog,
+                                                   ModestWizardDialogResponseOverrideFunc callback)
+{
+    ModestWizardDialogPrivate *priv = wizard_dialog->priv;
+
+    priv->override_func = callback;
+}
+
+void
+modest_wizard_dialog_update_model (ModestWizardDialog *wizard_dialog)
+{
+       g_return_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog));
+
+       invoke_update_model_vfunc (wizard_dialog);
+}
+
+gboolean
+modest_wizard_dialog_save (ModestWizardDialog *wizard_dialog)
+{
+       g_return_val_if_fail (MODEST_IS_WIZARD_DIALOG (wizard_dialog), FALSE);
+
+       return invoke_save_vfunc (wizard_dialog);
+}