Show only numeric variables in correlations dialog
[pspp-builds.git] / src / ui / gui / correlation-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2009  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "dialog-common.h"
20 #include <language/syntax-string-source.h>
21 #include <ui/syntax-gen.h>
22 #include <libpspp/str.h>
23
24 #include "correlation-dialog.h"
25 #include "psppire-selector.h"
26 #include "psppire-dictview.h"
27 #include "psppire-dialog.h"
28
29 #include "psppire-data-window.h"
30 #include "psppire-var-view.h"
31
32 #include "executor.h"
33 #include "helper.h"
34
35 #include <gtk/gtk.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41
42 struct correlation
43 {
44   PsppireDict *dict;
45
46   GtkWidget *variables ;
47
48   GtkWidget *significant;
49   GtkWidget *two_tailed;
50 };
51
52
53 static char * generate_syntax (const struct correlation *rd);
54
55
56 static void
57 refresh (struct correlation *rd)
58 {
59   GtkTreeModel *liststore =
60     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variables));
61   gtk_list_store_clear (GTK_LIST_STORE (liststore));
62
63   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->significant), FALSE);
64
65   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->two_tailed), TRUE);
66 }
67
68
69 static gboolean
70 dialog_state_valid (gpointer data)
71 {
72   struct correlation *corr = data;
73
74   GtkTreeModel *liststore =
75     gtk_tree_view_get_model (GTK_TREE_VIEW (corr->variables));
76
77   if  (gtk_tree_model_iter_n_children (liststore, NULL) >= 1)
78     return TRUE;
79
80   return FALSE;
81 }
82
83
84 /* Pops up the Correlation dialog box */
85 void
86 correlation_dialog (GObject *o, gpointer data)
87 {
88   struct correlation rd;
89   gint response;
90
91   GtkBuilder *xml = builder_new ("correlation.ui");
92   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
93   PsppireVarStore *vs;
94
95   GtkWidget *dialog = get_widget_assert   (xml, "correlation-dialog");
96   GtkWidget *source = get_widget_assert   (xml, "dict-view");
97
98   g_object_get (de->data_editor, "var-store", &vs, NULL);
99
100   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
101
102   g_object_get (vs, "dictionary", &rd.dict, NULL);
103   g_object_set (source,
104                 "model", rd.dict, 
105                 "predicate", var_is_numeric,
106                 NULL);
107
108   rd.variables = get_widget_assert (xml, "psppire-var-view1");
109   rd.significant = get_widget_assert (xml, "button-flag-significants");
110   rd.two_tailed = get_widget_assert (xml, "button-two-tailed");
111
112   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &rd);
113
114   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
115                                       dialog_state_valid, &rd);
116
117   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
118
119   switch (response)
120     {
121     case GTK_RESPONSE_OK:
122       {
123         gchar *syntax = generate_syntax (&rd);
124
125         struct getl_interface *sss = create_syntax_string_source (syntax);
126         execute_syntax (sss);
127
128         g_free (syntax);
129       }
130       break;
131     case PSPPIRE_RESPONSE_PASTE:
132       {
133         gchar *syntax = generate_syntax (&rd);
134         paste_syntax_in_new_window (syntax);
135
136         g_free (syntax);
137       }
138       break;
139     default:
140       break;
141     }
142
143   g_object_unref (xml);
144 }
145
146
147 \f
148
149 static char *
150 generate_syntax (const struct correlation *rd)
151 {
152   gchar *text;
153   GString *string = g_string_new ("CORRELATION");
154   g_string_append (string, "\n\t/VARIABLES = ");
155
156   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variables), 0, string);
157
158
159   g_string_append (string, "\n\t/PRINT =");
160
161   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->two_tailed)))
162     g_string_append (string, " TWOTAIL");
163   else
164     g_string_append (string, " ONETAIL");
165
166
167   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->significant)))
168     g_string_append (string, " NOSIG");
169   else
170     g_string_append (string, " SIG");
171
172
173   g_string_append (string, ".\n");
174
175   text = string->str;
176
177   g_string_free (string, FALSE);
178
179   return text;
180 }