Implemented "type & label" subdialog on compute.
[pspp-builds.git] / src / ui / gui / compute-dialog.c
index 7e14f40607448cfa6ef45de859a4f91dac357acd..843a25244b6bb238ce6e6f095c3320ec0f7c363a 100644 (file)
@@ -1,21 +1,18 @@
-/*
-    PSPPIRE --- A Graphical User Interface for PSPP
-    Copyright (C) 2007  Free Software Foundation
+/* PSPPIRE - a graphical user interface for PSPP.
+   Copyright (C) 2007  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
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
+   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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301, USA. */
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 
 #include <gtk/gtk.h>
 #include "dialog-common.h"
 #include "dict-display.h"
 
+#include <language/expressions/public.h>
 #include <language/syntax-string-source.h>
 #include "syntax-editor.h"
 
-
-#include <language/expressions/private.h>
-#include "c-ctype.h"
-
 static void function_list_populate (GtkTreeView *tv);
 
 static void insert_function_into_syntax_area (GtkTreeIter iter,
@@ -57,7 +51,7 @@ struct compute_dialog
 
 
 static void
-on_target_change (GObject *obj, const struct compute_dialog *cd)
+on_target_change (GObject *obj, struct compute_dialog *cd)
 {
   GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
   GtkWidget *type_and_label = get_widget_assert (cd->xml, "compute-button1");
@@ -157,9 +151,19 @@ generate_syntax (const struct compute_dialog *cd)
 {
   gchar *text;
   GString *string ;
+  const gchar *target_name ;
+  const gchar *expression;
+  const gchar *label;
   GtkTextIter start, end;
-  GtkWidget *target =    get_widget_assert   (cd->xml, "compute-entry1");
+  GtkWidget *target = get_widget_assert   (cd->xml, "compute-entry1");
   GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
+  GtkWidget *string_toggle = get_widget_assert (cd->xml, "radio-button-string");
+  GtkWidget *user_label_toggle =
+    get_widget_assert (cd->xml, "radio-button-user-label");
+  GtkWidget *width_entry = get_widget_assert (cd->xml, "type-and-label-width");
+  GtkWidget *label_entry = get_widget_assert (cd->xml,
+                                             "type-and-label-label-entry");
+
 
   GtkTextBuffer *buffer =
     gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
@@ -167,17 +171,34 @@ generate_syntax (const struct compute_dialog *cd)
   gtk_text_buffer_get_start_iter (buffer, &start);
   gtk_text_buffer_get_end_iter (buffer, &end);
 
+  target_name = gtk_entry_get_text (GTK_ENTRY (target));
 
-  string = g_string_new ("COMPUTE ");
+  expression = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
 
-  g_string_append (string, gtk_entry_get_text (GTK_ENTRY (target)));
+  string = g_string_sized_new (64);
+
+  if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (string_toggle)))
+    {
+      const char *w = gtk_entry_get_text (GTK_ENTRY(width_entry));
+      g_string_append_printf (string, "STRING %s (a%s).\n", target_name, w);
+    }
+  else
+    g_string_append_printf (string, "NUMERIC %s.\n", target_name);
 
-  g_string_append (string, " = ");
+  if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (user_label_toggle)))
+    label = gtk_entry_get_text (GTK_ENTRY (label_entry));
+  else
+    label = expression;
 
-  g_string_append (string,
-                  gtk_text_buffer_get_text (buffer, &start, &end, FALSE));
+  if ( strlen (label) > 0 )
+    g_string_append_printf (string, "VARIABLE LABEL %s '%s'.\n",
+                           target_name,
+                           label);
 
-  g_string_append (string, ".");
+  g_string_append_printf (string, "COMPUTE %s = %s.",
+                         target_name,
+                         expression
+                         );
 
   text = string->str;
 
@@ -186,6 +207,20 @@ generate_syntax (const struct compute_dialog *cd)
   return text;
 }
 
+static void
+run_type_label_dialog (GtkButton *b, gpointer data)
+{
+  struct compute_dialog *cd = data;
+  gint response;
+
+  GtkWidget *subdialog = get_widget_assert (cd->xml, "type-and-label-dialog");
+  GtkWidget *dialog = get_widget_assert (cd->xml, "compute-variable-dialog");
+
+  gtk_window_set_transient_for (GTK_WINDOW (subdialog), GTK_WINDOW (dialog));
+
+  response = psppire_dialog_run (PSPPIRE_DIALOG (subdialog));
+}
+
 
 /* Pops up the Compute dialog box */
 void
@@ -208,6 +243,7 @@ compute_dialog (GObject *o, gpointer data)
   GtkWidget *syntax_area = get_widget_assert (xml, "compute-textview1");
   GtkWidget *var_selector = get_widget_assert (xml, "compute-selector1");
   GtkWidget *func_selector = get_widget_assert (xml, "compute-selector2");
+  GtkWidget *type_and_label = get_widget_assert (xml, "compute-button1");
 
   GtkSheet *var_sheet =
     GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
@@ -250,6 +286,11 @@ compute_dialog (GObject *o, gpointer data)
                    G_CALLBACK (erase),  xml);
 
 
+  g_signal_connect (type_and_label, "clicked",
+                   G_CALLBACK (run_type_label_dialog),  &scd);
+
+
+
   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
 
 
@@ -291,12 +332,6 @@ enum {
 };
 
 
-static const struct operation fs[] =
-  {
-#include "parse.inc"
-  };
-
-
 static void
 function_list_populate (GtkTreeView *tv)
 {
@@ -304,25 +339,20 @@ function_list_populate (GtkTreeView *tv)
   GtkTreeIter iter;
   gint i;
 
-  const gint n_funcs = sizeof (fs) / sizeof fs[0] ;
+  const gint n_funcs = expr_get_function_cnt ();
 
   liststore = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
 
   for (i = 0 ; i < n_funcs ; ++i)
     {
-      if ( fs[i].prototype == NULL)
-       continue;
-
-      /* All the real ones seem to begin with an upper case letter */
-      if ( !c_isupper(*fs[i].prototype))
-       continue;
+      const struct operation *op = expr_get_function (i);
 
       gtk_list_store_append (liststore, &iter);
 
       gtk_list_store_set (liststore, &iter,
-                         COL_NAME, fs[i].name,
-                         COL_USAGE,fs[i].prototype,
-                         COL_ARITY, fs[i].arg_cnt,
+                         COL_NAME, expr_operation_get_name (op),
+                         COL_USAGE, expr_operation_get_prototype (op),
+                         COL_ARITY, expr_operation_get_arg_cnt (op),
                          -1);
     }