X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=inline;f=src%2Fui%2Fgui%2Fcompute-dialog.c;h=5d40d21639bbe68fd9d06d6ea35dd9f50fba4a40;hb=5e2c79af8a242eb36e62291277805933d5ad5903;hp=7c6a78480a2c012e02c375f13d46adc4b672ded7;hpb=f5c108becd49d78f4898cab11352291f5689d24e;p=pspp-builds.git
diff --git a/src/ui/gui/compute-dialog.c b/src/ui/gui/compute-dialog.c
index 7c6a7848..5d40d216 100644
--- a/src/ui/gui/compute-dialog.c
+++ b/src/ui/gui/compute-dialog.c
@@ -1,30 +1,26 @@
-/*
- 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.
-
- 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. */
+ 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, see . */
+#include
#include
#include "compute-dialog.h"
#include "helper.h"
#include "psppire-dialog.h"
#include "psppire-keypad.h"
#include "data-editor.h"
-#include
#include "psppire-var-store.h"
#include "dialog-common.h"
#include "dict-display.h"
@@ -37,12 +33,14 @@ static void function_list_populate (GtkTreeView *tv);
static void insert_function_into_syntax_area (GtkTreeIter iter,
GtkWidget *text_view,
- GtkTreeModel *model
+ GtkTreeModel *model,
+ gpointer data
);
static void insert_source_row_into_text_view (GtkTreeIter iter,
GtkWidget *dest,
- GtkTreeModel *model
+ GtkTreeModel *model,
+ gpointer data
);
@@ -50,11 +48,13 @@ static void insert_source_row_into_text_view (GtkTreeIter iter,
struct compute_dialog
{
GladeXML *xml; /* The xml that generated the widgets */
+ PsppireDict *dict;
+ gboolean use_type;
};
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");
@@ -79,18 +79,15 @@ refresh (GObject *obj, const struct compute_dialog *cd)
GtkTreeSelection *selection;
-
/* Clear the target variable entry box */
gtk_entry_set_text (GTK_ENTRY (target), "");
g_signal_emit_by_name (target, "changed");
-
/* Clear the syntax area textbuffer */
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
gtk_text_buffer_delete (buffer, &start, &end);
-
/* Unselect all items in the treeview */
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (varlist));
gtk_tree_selection_unselect_all (selection);
@@ -154,9 +151,19 @@ generate_syntax (const struct compute_dialog *cd)
{
gchar *text;
GString *string ;
+ const gchar *target_name ;
+ 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));
@@ -164,17 +171,44 @@ 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);
- g_string_append (string, " = ");
+ if ( cd-> use_type &&
+ NULL == psppire_dict_lookup_var (cd->dict, target_name ))
+ {
+ 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);
+ }
+
+ 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.\n",
+ target_name,
+ expression
+ );
+
+ g_string_append (string, "EXECUTE.\n");
+
+
+ g_free (expression);
text = string->str;
@@ -183,6 +217,148 @@ generate_syntax (const struct compute_dialog *cd)
return text;
}
+static void
+reset_type_label_dialog (struct compute_dialog *cd)
+{
+ const gchar *target_name;
+ struct variable *target_var;
+
+ 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");
+
+ GtkWidget *numeric_target =
+ get_widget_assert (cd->xml, "radio-button-numeric");
+
+ GtkWidget *string_target =
+ get_widget_assert (cd->xml, "radio-button-string");
+
+
+ target_name = gtk_entry_get_text
+ (GTK_ENTRY (get_widget_assert (cd->xml, "compute-entry1")));
+
+
+ if ( (target_var = psppire_dict_lookup_var (cd->dict, target_name)) )
+ {
+ /* Existing Variable */
+ const gchar *label ;
+ GtkWidget *user_label =
+ get_widget_assert (cd->xml, "radio-button-user-label");
+
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (user_label), TRUE);
+
+ label = var_get_label (target_var);
+
+ if ( label )
+ {
+ gtk_entry_set_text (GTK_ENTRY (label_entry), label);
+ }
+
+ gtk_widget_set_sensitive (width_entry, FALSE);
+
+ if ( var_is_numeric (target_var))
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numeric_target),
+ TRUE);
+ else
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (string_target),
+ TRUE);
+
+ gtk_widget_set_sensitive (numeric_target, FALSE);
+ gtk_widget_set_sensitive (string_target, FALSE);
+ }
+ else
+ {
+ GtkWidget *expression =
+ get_widget_assert (cd->xml, "radio-button-expression-label");
+
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (expression), TRUE);
+
+ gtk_widget_set_sensitive (width_entry, TRUE);
+ gtk_widget_set_sensitive (numeric_target, TRUE);
+ gtk_widget_set_sensitive (string_target, TRUE);
+
+ gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (numeric_target),
+ TRUE);
+ }
+
+}
+
+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));
+
+ reset_type_label_dialog (cd);
+ response = psppire_dialog_run (PSPPIRE_DIALOG (subdialog));
+ if ( response == PSPPIRE_RESPONSE_CONTINUE)
+ cd->use_type = TRUE;
+}
+
+
+static void
+on_expression_toggle (GtkToggleButton *button, gpointer data)
+{
+ struct compute_dialog *cd = data;
+
+ GtkWidget *entry =
+ get_widget_assert (cd->xml, "type-and-label-label-entry");
+
+ if ( gtk_toggle_button_get_active (button))
+ {
+ gtk_entry_set_text (GTK_ENTRY (entry), "");
+ gtk_widget_set_sensitive (entry, FALSE);
+ }
+ else
+ {
+ const char *label;
+ struct variable *target_var;
+ const gchar *target_name = gtk_entry_get_text
+ (GTK_ENTRY (get_widget_assert (cd->xml, "compute-entry1")));
+
+ target_var = psppire_dict_lookup_var (cd->dict, target_name);
+ if ( target_var )
+ {
+ label = var_get_label (target_var);
+
+ if ( label )
+ gtk_entry_set_text (GTK_ENTRY (entry), label);
+ }
+ else
+ gtk_entry_set_text (GTK_ENTRY (entry), "");
+
+ gtk_widget_set_sensitive (entry, TRUE);
+ }
+}
+
+
+/* Return TRUE if the dialog box's widgets' state are such that clicking OK
+ might not result in erroneous syntax being generated */
+static gboolean
+contents_plausible (gpointer data)
+{
+ struct compute_dialog *cd = data;
+
+ GtkWidget *target = get_widget_assert (cd->xml, "compute-entry1");
+ GtkWidget *syntax_area = get_widget_assert (cd->xml, "compute-textview1");
+ GtkTextBuffer *buffer =
+ gtk_text_view_get_buffer (GTK_TEXT_VIEW (syntax_area));
+
+ if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (target))))
+ return FALSE;
+
+ if ( gtk_text_buffer_get_char_count (buffer) == 0 )
+ return FALSE;
+
+ return TRUE;
+}
/* Pops up the Compute dialog box */
void
@@ -191,26 +367,32 @@ compute_dialog (GObject *o, gpointer data)
gint response;
struct data_editor *de = data;
- PsppireVarStore *vs;
+ PsppireVarStore *vs = NULL;
struct compute_dialog scd;
GladeXML *xml = XML_NEW ("psppire.glade");
GtkWidget *dialog = get_widget_assert (xml, "compute-variable-dialog");
- GtkWidget *dict_view = get_widget_assert (xml, "compute-treeview1");
- GtkWidget *functions = get_widget_assert (xml, "compute-treeview2");
- GtkWidget *keypad = get_widget_assert (xml, "psppire-keypad1");
- GtkWidget *target = get_widget_assert (xml, "compute-entry1");
+ GtkWidget *dict_view = get_widget_assert (xml, "compute-treeview1");
+ GtkWidget *functions = get_widget_assert (xml, "compute-treeview2");
+ GtkWidget *keypad = get_widget_assert (xml, "psppire-keypad1");
+ GtkWidget *target = get_widget_assert (xml, "compute-entry1");
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");
+
+ GtkWidget *expression =
+ get_widget_assert (xml, "radio-button-expression-label");
- GtkSheet *var_sheet =
- GTK_SHEET (get_widget_assert (de->xml, "variable_sheet"));
- vs = PSPPIRE_VAR_STORE (gtk_sheet_get_model (var_sheet));
+ g_object_get (de->data_editor, "var-store", &vs, NULL);
+ scd.dict = vs->dict;
+ scd.use_type = FALSE;
+ g_signal_connect (expression, "toggled",
+ G_CALLBACK(on_expression_toggle), &scd);
gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
@@ -223,6 +405,7 @@ compute_dialog (GObject *o, gpointer data)
psppire_selector_set_subjects (PSPPIRE_SELECTOR (var_selector),
dict_view, syntax_area,
insert_source_row_into_text_view,
+ NULL,
NULL);
@@ -231,11 +414,15 @@ compute_dialog (GObject *o, gpointer data)
psppire_selector_set_subjects (PSPPIRE_SELECTOR (func_selector),
functions, syntax_area,
insert_function_into_syntax_area,
+ NULL,
NULL);
scd.xml = xml;
+ psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
+ contents_plausible, &scd);
+
g_signal_connect (target, "changed", G_CALLBACK (on_target_change), &scd);
g_signal_connect (dialog, "refresh", G_CALLBACK (refresh), &scd);
@@ -247,6 +434,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));
@@ -282,9 +474,9 @@ compute_dialog (GObject *o, gpointer data)
enum {
- COL_NAME,
- COL_USAGE,
- COL_ARITY
+ COMPUTE_COL_NAME,
+ COMPUTE_COL_USAGE,
+ COMPUTE_COL_ARITY
};
@@ -306,9 +498,9 @@ function_list_populate (GtkTreeView *tv)
gtk_list_store_append (liststore, &iter);
gtk_list_store_set (liststore, &iter,
- COL_NAME, expr_operation_get_name (op),
- COL_USAGE, expr_operation_get_prototype (op),
- COL_ARITY, expr_operation_get_arg_cnt (op),
+ COMPUTE_COL_NAME, expr_operation_get_name (op),
+ COMPUTE_COL_USAGE, expr_operation_get_prototype (op),
+ COMPUTE_COL_ARITY, expr_operation_get_arg_cnt (op),
-1);
}
@@ -329,7 +521,7 @@ function_list_populate (GtkTreeView *tv)
gtk_tree_view_column_pack_start (col, renderer, TRUE);
- gtk_tree_view_column_add_attribute (col, renderer, "text", COL_USAGE);
+ gtk_tree_view_column_add_attribute (col, renderer, "text", COMPUTE_COL_USAGE);
}
gtk_tree_view_set_model (tv, GTK_TREE_MODEL (liststore));
@@ -341,7 +533,8 @@ function_list_populate (GtkTreeView *tv)
static void
insert_function_into_syntax_area (GtkTreeIter iter,
GtkWidget *text_view,
- GtkTreeModel *model
+ GtkTreeModel *model,
+ gpointer data
)
{
GString *string;
@@ -356,8 +549,8 @@ insert_function_into_syntax_area (GtkTreeIter iter,
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
- gtk_tree_model_get_value (model, &iter, COL_NAME, &name_value);
- gtk_tree_model_get_value (model, &iter, COL_ARITY, &arity_value);
+ gtk_tree_model_get_value (model, &iter, COMPUTE_COL_NAME, &name_value);
+ gtk_tree_model_get_value (model, &iter, COMPUTE_COL_ARITY, &arity_value);
arity = g_value_get_int (&arity_value);
@@ -404,7 +597,8 @@ insert_function_into_syntax_area (GtkTreeIter iter,
static void
insert_source_row_into_text_view (GtkTreeIter iter,
GtkWidget *dest,
- GtkTreeModel *model
+ GtkTreeModel *model,
+ gpointer data
)
{
GtkTreePath *path;