Scatter plot dialog: Correct order of variables in syntax generator
[pspp] / src / ui / gui / psppire-buttonbox.c
index 184ebd16df8d8deac3115172710dd451847eb7d4..567a13cc0d7ee013919e07e6a856e82f56406f24 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2007  Free Software Foundation
+   Copyright (C) 2007, 2010, 2011, 2012  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <glib.h>
 #include <gtk/gtk.h>
-#include <gtk/gtksignal.h>
 #include "psppire-buttonbox.h"
 #include "psppire-dialog.h"
 
+#include "helper.h"
+
 #include <gettext.h>
 
 #define _(msgid) gettext (msgid)
@@ -63,57 +64,85 @@ psppire_button_box_get_type (void)
 }
 
 enum {
-  PROP_BUTTONS = 1
+  PROP_BUTTONS = 1,
+  PROP_DEFAULT = 2
 };
 
+static void
+set_default (PsppireButtonBox *bb)
+{
+  int i;
+
+  for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
+    if (bb->def == (1 << i))
+      {
+        gtk_widget_set_can_default (bb->button[i], TRUE);
+        gtk_widget_grab_default (bb->button[i]);
+      }
+}
+
 static void
 psppire_buttonbox_set_property (GObject         *object,
-                              guint            prop_id,
-                              const GValue    *value,
-                              GParamSpec      *pspec)
+                                guint            prop_id,
+                                const GValue    *value,
+                                GParamSpec      *pspec)
 {
   gint i;
   guint flags;
   PsppireButtonBox *bb = PSPPIRE_BUTTONBOX (object);
-  if ( prop_id != PROP_BUTTONS)
+
+  switch (prop_id)
     {
+    case PROP_BUTTONS:
+      flags = g_value_get_flags (value);
+      for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
+        g_object_set (bb->button[i], "visible", 0x01 & (flags >> i)  , NULL);
+      break;
+
+    case PROP_DEFAULT:
+      bb->def = g_value_get_flags (value);
+      if (gtk_widget_get_realized (GTK_WIDGET (bb)))
+        set_default (bb);
+      break;
+
+    default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      return ;
     }
-
-  flags = g_value_get_flags (value);
-
-  for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
-    g_object_set (bb->button[i], "visible", 0x01 & (flags >> i)  , NULL);
 }
 
 static void
 psppire_buttonbox_get_property (GObject         *object,
-                              guint            prop_id,
-                              GValue          *value,
-                              GParamSpec      *pspec)
+                                guint            prop_id,
+                                GValue          *value,
+                                GParamSpec      *pspec)
 {
   guint flags = 0;
   gint i;
 
   PsppireButtonBox *bb = PSPPIRE_BUTTONBOX (object);
 
-  if  (PROP_BUTTONS != prop_id)
+  switch (prop_id)
     {
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-      return;
-    }
+    case PROP_BUTTONS:
+      for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
+        {
+          gboolean visibility;
+          g_object_get (bb->button[i], "visible", &visibility, NULL);
 
-  for (i = 0 ; i < n_PsppireButtonBoxButtons ; ++i )
-    {
-      gboolean visibility;
-      g_object_get (bb->button[i], "visible", &visibility, NULL);
+          if ( visibility )
+            flags |= (0x01 << i);
+        }
 
-      if ( visibility )
-       flags |= (0x01 << i);
-    }
+      g_value_set_flags (value, flags);
+      break;
+
+    case PROP_DEFAULT:
+      g_value_set_flags (value, bb->def);
 
-  g_value_set_flags (value, flags);
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
 }
 
 
@@ -123,12 +152,14 @@ typedef enum
     PSPPIRE_BUTTON_GOTO_MASK   = (1 << PSPPIRE_BUTTON_GOTO),
     PSPPIRE_BUTTON_CONTINUE_MASK = (1 << PSPPIRE_BUTTON_CONTINUE),
     PSPPIRE_BUTTON_CANCEL_MASK = (1 << PSPPIRE_BUTTON_CANCEL),
+    PSPPIRE_BUTTON_CLOSE_MASK  = (1 << PSPPIRE_BUTTON_CLOSE),
     PSPPIRE_BUTTON_HELP_MASK   = (1 << PSPPIRE_BUTTON_HELP),
     PSPPIRE_BUTTON_RESET_MASK  = (1 << PSPPIRE_BUTTON_RESET),
     PSPPIRE_BUTTON_PASTE_MASK  = (1 << PSPPIRE_BUTTON_PASTE)
   } PsppireButtonMask;
 
 static GParamSpec *button_flags;
+static GParamSpec *default_flags;
 
 static void
 psppire_button_box_class_init (PsppireButtonBoxClass *class)
@@ -140,8 +171,8 @@ psppire_button_box_class_init (PsppireButtonBoxClass *class)
 
   button_flags =
     g_param_spec_flags ("buttons",
-                       _("Buttons"),
-                       _("The mask that decides what buttons appear in the button box"),
+                       "Buttons",
+                       "The mask that decides what buttons appear in the button box",
                        PSPPIRE_TYPE_BUTTON_MASK,
                        PSPPIRE_BUTTON_OK_MASK |
                        PSPPIRE_BUTTON_CANCEL_MASK |
@@ -149,12 +180,20 @@ psppire_button_box_class_init (PsppireButtonBoxClass *class)
                        PSPPIRE_BUTTON_HELP_MASK |
                        PSPPIRE_BUTTON_PASTE_MASK,
                        G_PARAM_READWRITE);
-
-
   g_object_class_install_property (object_class,
                                   PROP_BUTTONS,
                                   button_flags);
 
+  default_flags =
+    g_param_spec_flags ("default",
+                       "Default",
+                       "The mask that decides what what button grabs the default",
+                       PSPPIRE_TYPE_BUTTON_MASK,
+                       0,
+                       G_PARAM_READWRITE);
+  g_object_class_install_property (object_class,
+                                  PROP_DEFAULT,
+                                  default_flags);
 }
 
 static void
@@ -176,6 +215,14 @@ close_and_respond (GtkWidget *w, gint response)
   psppire_dialog_close (dialog);
 }
 
+static gboolean
+is_acceptable (GtkWidget *w)
+{
+  GtkWidget *toplevel = gtk_widget_get_toplevel (w);
+
+  return (PSPPIRE_IS_DIALOG (toplevel)
+          && psppire_dialog_is_acceptable (PSPPIRE_DIALOG (toplevel)));
+}
 
 static void
 close_dialog (GtkWidget *w, gpointer data)
@@ -186,27 +233,31 @@ close_dialog (GtkWidget *w, gpointer data)
 static void
 continue_button_clicked (GtkWidget *w, gpointer data)
 {
-  close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
+  if (is_acceptable (w))
+    close_and_respond (w, PSPPIRE_RESPONSE_CONTINUE);
 }
 
 
 static void
 ok_button_clicked (GtkWidget *w, gpointer data)
 {
-  close_and_respond (w, GTK_RESPONSE_OK);
+  if (is_acceptable (w))
+    close_and_respond (w, GTK_RESPONSE_OK);
 }
 
 
 static void
 paste_button_clicked (GtkWidget *w, gpointer data)
 {
-  close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
+  if (is_acceptable (w))
+    close_and_respond (w, PSPPIRE_RESPONSE_PASTE);
 }
 
 static void
 goto_button_clicked (GtkWidget *w, gpointer data)
 {
-  close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
+  if (is_acceptable (w))
+    close_and_respond (w, PSPPIRE_RESPONSE_GOTO);
 }
 
 
@@ -226,6 +277,22 @@ refresh_clicked (GtkWidget *w, gpointer data)
 
 
 
+static void
+help_clicked (GtkWidget *w, gpointer data)
+{
+  GtkWidget *toplevel = gtk_widget_get_toplevel (w);
+  PsppireDialog *dialog;
+
+  if ( ! PSPPIRE_IS_DIALOG (toplevel))
+    return;
+
+  dialog = PSPPIRE_DIALOG (toplevel);
+
+  psppire_dialog_help (dialog);
+}
+
+
+
 static void
 on_validity_change (GtkWidget *toplevel, gboolean valid, gpointer data)
 {
@@ -248,22 +315,24 @@ on_realize (GtkWidget *buttonbox, gpointer data)
       g_signal_connect (toplevel, "validity-changed",
                        G_CALLBACK (on_validity_change), buttonbox);
     }
+  set_default (PSPPIRE_BUTTONBOX (buttonbox));
 }
 
 static void
 psppire_button_box_init (PsppireButtonBox *bb)
 {
+  bb->def = PSPPIRE_BUTTON_CONTINUE;
 
-  bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_from_stock (GTK_STOCK_OK);
-  gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
+  bb->button[PSPPIRE_BUTTON_OK] = gtk_button_new_with_label (_("OK"));
+  psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_OK]);
   g_signal_connect (bb->button[PSPPIRE_BUTTON_OK], "clicked",
                    G_CALLBACK (ok_button_clicked), NULL);
   g_object_set (bb->button[PSPPIRE_BUTTON_OK], "no-show-all", TRUE, NULL);
 
 
   bb->button[PSPPIRE_BUTTON_GOTO] =
-    gtk_button_new_from_stock (GTK_STOCK_JUMP_TO);
-  gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
+    gtk_button_new_with_label (_("Go To"));
+  psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_GOTO]);
   g_signal_connect (bb->button[PSPPIRE_BUTTON_GOTO], "clicked",
                    G_CALLBACK (goto_button_clicked), NULL);
   g_object_set (bb->button[PSPPIRE_BUTTON_GOTO], "no-show-all", TRUE, NULL);
@@ -272,7 +341,7 @@ psppire_button_box_init (PsppireButtonBox *bb)
   bb->button[PSPPIRE_BUTTON_CONTINUE] =
     gtk_button_new_with_mnemonic (_("Continue"));
 
-  gtk_box_pack_start_defaults (GTK_BOX (bb),
+  psppire_box_pack_start_defaults (GTK_BOX (bb),
                               bb->button[PSPPIRE_BUTTON_CONTINUE]);
   g_signal_connect (bb->button[PSPPIRE_BUTTON_CONTINUE], "clicked",
                    G_CALLBACK (continue_button_clicked), NULL);
@@ -282,28 +351,36 @@ psppire_button_box_init (PsppireButtonBox *bb)
 
 
 
-  bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_from_stock (GTK_STOCK_PASTE);
+  bb->button[PSPPIRE_BUTTON_PASTE] = gtk_button_new_with_label (_("Paste"));
   g_signal_connect (bb->button[PSPPIRE_BUTTON_PASTE], "clicked",
                    G_CALLBACK (paste_button_clicked), NULL);
-  gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
+  psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_PASTE]);
   g_object_set (bb->button[PSPPIRE_BUTTON_PASTE], "no-show-all", TRUE, NULL);
 
-  bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
+  bb->button[PSPPIRE_BUTTON_CANCEL] = gtk_button_new_with_label (_("Cancel"));
   g_signal_connect (bb->button[PSPPIRE_BUTTON_CANCEL], "clicked",
                    G_CALLBACK (close_dialog), NULL);
-  gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
+  psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CANCEL]);
   g_object_set (bb->button[PSPPIRE_BUTTON_CANCEL], "no-show-all", TRUE, NULL);
 
+  bb->button[PSPPIRE_BUTTON_CLOSE] = gtk_button_new_with_label (_("Close"));
+  g_signal_connect (bb->button[PSPPIRE_BUTTON_CLOSE], "clicked",
+                   G_CALLBACK (close_dialog), NULL);
+  psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_CLOSE]);
+  g_object_set (bb->button[PSPPIRE_BUTTON_CLOSE], "no-show-all", TRUE, NULL);
+
 
-  bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_from_stock ("pspp-stock-reset");
+  bb->button[PSPPIRE_BUTTON_RESET] = gtk_button_new_with_label (_("Reset"));
   g_signal_connect (bb->button[PSPPIRE_BUTTON_RESET], "clicked",
                    G_CALLBACK (refresh_clicked), NULL);
-  gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
+  psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_RESET]);
   g_object_set (bb->button[PSPPIRE_BUTTON_RESET], "no-show-all", TRUE, NULL);
 
 
-  bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_from_stock (GTK_STOCK_HELP);
-  gtk_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
+  bb->button[PSPPIRE_BUTTON_HELP] = gtk_button_new_with_label (_("Help"));
+  g_signal_connect (bb->button[PSPPIRE_BUTTON_HELP], "clicked",
+                   G_CALLBACK (help_clicked), NULL);
+  psppire_box_pack_start_defaults (GTK_BOX (bb), bb->button[PSPPIRE_BUTTON_HELP]);
   g_object_set (bb->button[PSPPIRE_BUTTON_HELP], "no-show-all", TRUE, NULL);
 
 
@@ -329,8 +406,9 @@ psppire_button_box_init (PsppireButtonBox *bb)
 }
 
 
-/* This function is lifted verbatim from the Gtk2.10.6 library */
-
+/* This function was lifted verbatim from the Gtk2.10.6 library.
+   But later modified to fit Gtk2.24
+ */
 void
 _psppire_button_box_child_requisition (GtkWidget *widget,
                                       int       *nvis_children,
@@ -339,7 +417,6 @@ _psppire_button_box_child_requisition (GtkWidget *widget,
                                       int       *height)
 {
   GtkButtonBox *bbox;
-  GtkBoxChild *child;
   GList *children;
   gint nchildren;
   gint nsecondaries;
@@ -369,38 +446,39 @@ _psppire_button_box_child_requisition (GtkWidget *widget,
                         "child-internal-pad-y", &ipad_y_default,
                        NULL);
 
-  child_min_width = bbox->child_min_width   != GTK_BUTTONBOX_DEFAULT
-    ? bbox->child_min_width : width_default;
-  child_min_height = bbox->child_min_height !=GTK_BUTTONBOX_DEFAULT
-    ? bbox->child_min_height : height_default;
-  ipad_x = bbox->child_ipad_x != GTK_BUTTONBOX_DEFAULT
-    ? bbox->child_ipad_x : ipad_x_default;
-  ipad_y = bbox->child_ipad_y != GTK_BUTTONBOX_DEFAULT
-    ? bbox->child_ipad_y : ipad_y_default;
+  child_min_width = width_default;
+  child_min_height = height_default;
+  ipad_x = ipad_x_default;
+  ipad_y = ipad_y_default;
 
   nchildren = 0;
   nsecondaries = 0;
-  children = GTK_BOX(bbox)->children;
+
   needed_width = child_min_width;
   needed_height = child_min_height;
   ipad_w = ipad_x * 2;
   ipad_h = ipad_y * 2;
 
+  children = gtk_container_get_children (GTK_CONTAINER (bbox));
   while (children)
     {
-      child = children->data;
+      GtkWidget *child = children->data;
       children = children->next;
 
-      if (GTK_WIDGET_VISIBLE (child->widget))
+      if (gtk_widget_get_visible (child))
        {
+          gboolean is_secondary = FALSE;
          nchildren += 1;
-         gtk_widget_size_request (child->widget, &child_requisition);
+         gtk_widget_get_preferred_size (child, NULL, &child_requisition);
 
          if (child_requisition.width + ipad_w > needed_width)
            needed_width = child_requisition.width + ipad_w;
          if (child_requisition.height + ipad_h > needed_height)
            needed_height = child_requisition.height + ipad_h;
-         if (child->is_secondary)
+
+          gtk_container_child_get (GTK_CONTAINER (bbox), child, "secondary", &is_secondary, NULL);
+
+          if (is_secondary)
            nsecondaries++;
        }
     }
@@ -424,16 +502,17 @@ psppire_button_flags_get_type (void)
     {
       static const GFlagsValue values[] =
        {
-         { PSPPIRE_BUTTON_OK_MASK,     "PSPPIRE_BUTTON_OK_MASK",     N_("OK") },
-         { PSPPIRE_BUTTON_GOTO_MASK,   "PSPPIRE_BUTTON_GOTO_MASK", N_("Go To") },
-         { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", N_("Continue") },
-         { PSPPIRE_BUTTON_CANCEL_MASK, "PSPPIRE_BUTTON_CANCEL_MASK", N_("Cancel") },
-         { PSPPIRE_BUTTON_HELP_MASK,   "PSPPIRE_BUTTON_HELP_MASK",   N_("Help") },
-         { PSPPIRE_BUTTON_RESET_MASK,  "PSPPIRE_BUTTON_RESET_MASK",  N_("Reset") },
-         { PSPPIRE_BUTTON_PASTE_MASK,  "PSPPIRE_BUTTON_PASTE_MASK",  N_("Paste") },
+         { PSPPIRE_BUTTON_OK_MASK,      "PSPPIRE_BUTTON_OK_MASK",       "Accept dialog and run it" },
+         { PSPPIRE_BUTTON_GOTO_MASK,    "PSPPIRE_BUTTON_GOTO_MASK",     "Goto case/variable" },
+         { PSPPIRE_BUTTON_CONTINUE_MASK,"PSPPIRE_BUTTON_CONTINUE_MASK", "Accept and close the subdialog" },
+         { PSPPIRE_BUTTON_CANCEL_MASK,  "PSPPIRE_BUTTON_CANCEL_MASK",   "Close dialog and discard settings" },
+         { PSPPIRE_BUTTON_CLOSE_MASK,   "PSPPIRE_BUTTON_CLOSE_MASK",    "Close dialog" },
+         { PSPPIRE_BUTTON_HELP_MASK,    "PSPPIRE_BUTTON_HELP_MASK",     "Invoke context sensitive help" },
+         { PSPPIRE_BUTTON_RESET_MASK,   "PSPPIRE_BUTTON_RESET_MASK",    "Restore dialog to its default settings" },
+         { PSPPIRE_BUTTON_PASTE_MASK,   "PSPPIRE_BUTTON_PASTE_MASK",    "Accept dialog and paste syntax" },
          { 0, NULL, NULL }
        };
-
+      
       ftype = g_flags_register_static
        (g_intern_static_string ("PsppireButtonFlags"), values);