c9a1bcbd473e7a4fb49ea183aeddf6a3ba8b6eaa
[pspp-builds.git] / src / ui / gui / ks-one-sample-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2011  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 <ui/syntax-gen.h>
20 #include <libpspp/str.h>
21
22 #include "ks-one-sample-dialog.h"
23 #include "psppire-selector.h"
24 #include "psppire-dictview.h"
25 #include "psppire-dialog.h"
26
27 #include "psppire-data-window.h"
28 #include "psppire-var-view.h"
29
30 #include "executor.h"
31 #include "helper.h"
32 #include "dialog-common.h"
33
34 #include <gtk/gtk.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
39
40
41 enum
42   {
43     CB_NORMAL,
44     CB_POISSON,
45     CB_UNIFORM,
46     CB_EXPONENTIAL
47   };
48
49 struct ks_one_sample
50 {
51   GtkBuilder *xml;
52   PsppireDict *dict;
53
54   GtkWidget *variables;
55   PsppireDataWindow *de ;
56
57   GtkWidget *cb[4];
58 };
59
60 static char * generate_syntax (const struct ks_one_sample *rd);
61
62
63 static void
64 refresh (struct ks_one_sample *fd)
65 {
66   int i;
67   GtkTreeModel *liststore =
68     gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
69   gtk_list_store_clear (GTK_LIST_STORE (liststore));
70
71   for (i = 0; i < 4; ++i)
72     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fd->cb[i]), FALSE);
73 }
74
75
76 static gboolean
77 dialog_state_valid (gpointer data)
78 {
79   int i;
80   struct ks_one_sample *fd = data;
81
82   GtkTreeModel *liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (fd->variables));
83
84   if  (gtk_tree_model_iter_n_children (liststore, NULL) < 1)
85     return FALSE;
86
87   for (i = 0; i < 4; ++i)
88     {
89       if ( TRUE == gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (fd->cb[i])))
90         break;
91     }
92   if ( i >= 4)
93     return FALSE;
94
95
96   return TRUE;
97 }
98
99
100 /* Pops up the Ks_One_Sample dialog box */
101 void
102 ks_one_sample_dialog (PsppireDataWindow *dw)
103 {
104   struct ks_one_sample fd;
105   gint response;
106
107   PsppireVarStore *vs;
108
109   GtkWidget *dialog ;
110   GtkWidget *source ;
111
112   fd.xml = builder_new ("ks-one-sample.ui");
113
114   dialog = get_widget_assert   (fd.xml, "ks-one-sample-dialog");
115   source = get_widget_assert   (fd.xml, "dict-view");
116
117   fd.cb[CB_NORMAL] = get_widget_assert (fd.xml, "checkbutton-normal");
118   fd.cb[CB_POISSON] = get_widget_assert (fd.xml, "checkbutton-poisson");
119   fd.cb[CB_UNIFORM] = get_widget_assert (fd.xml, "checkbutton-uniform");
120   fd.cb[CB_EXPONENTIAL] = get_widget_assert (fd.xml, "checkbutton-exp");
121
122   fd.de = dw;
123
124   g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh),  &fd);
125
126
127   fd.variables = get_widget_assert   (fd.xml, "psppire-var-view1");
128
129   g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
130
131   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
132
133   g_object_get (vs, "dictionary", &fd.dict, NULL);
134   g_object_set (source, "model", fd.dict,
135                 "predicate", var_is_numeric,
136                 NULL);
137
138   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
139                                       dialog_state_valid, &fd);
140
141   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
142
143   switch (response)
144     {
145     case GTK_RESPONSE_OK:
146       g_free (execute_syntax_string (dw, generate_syntax (&fd)));
147       break;
148     case PSPPIRE_RESPONSE_PASTE:
149       g_free (paste_syntax_to_window (generate_syntax (&fd)));
150       break;
151     default:
152       break;
153     }
154
155   g_object_unref (fd.xml);
156 }
157
158
159 \f
160 static void
161 append_fragment (GString *string, const gchar *dist, PsppireVarView *vv)
162 {
163   g_string_append (string, "\n\t/KOLMOGOROV-SMIRNOV");
164
165   g_string_append (string, " ( ");
166   g_string_append (string, dist);
167   g_string_append (string, " ) = ");
168
169   psppire_var_view_append_names (vv, 0, string);
170 }
171
172
173 char *
174 generate_syntax (const struct ks_one_sample *rd)
175 {
176   gchar *text;
177
178   GString *string = g_string_new ("NPAR TEST");
179
180   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_NORMAL])))
181     append_fragment (string, "NORMAL", PSPPIRE_VAR_VIEW (rd->variables));
182
183   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_UNIFORM])))
184     append_fragment (string, "UNIFORM", PSPPIRE_VAR_VIEW (rd->variables));
185
186   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_POISSON])))
187     append_fragment (string, "POISSON", PSPPIRE_VAR_VIEW (rd->variables));
188
189   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->cb[CB_EXPONENTIAL])))
190     append_fragment (string, "EXPONENTIAL", PSPPIRE_VAR_VIEW (rd->variables));
191
192   g_string_append (string, ".\n");
193
194   text = string->str;
195
196   g_string_free (string, FALSE);
197
198   return text;
199 }