psppire-dict: Make PsppireDict not own its "struct dictionary".
[pspp] / src / ui / gui / npar-two-sample-related.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
18 #include <config.h>
19 #include <gtk/gtk.h>
20
21 #include "psppire-data-window.h"
22 #include "psppire-selector.h"
23 #include "psppire-var-view.h"
24
25 #include "psppire-dict.h"
26 #include "psppire-var-store.h"
27
28 #include "dialog-common.h"
29 #include "psppire-dialog.h"
30
31 #include "executor.h"
32
33 #include "helper.h"
34
35 #include "psppire-var-ptr.h"
36
37 #include "paired-dialog.h"
38 #include "npar-two-sample-related.h"
39
40 #include <gettext.h>
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
43
44 enum test
45   {
46     NT_WILCOXON,
47     NT_SIGN,
48     NT_MCNEMAR,
49     n_Tests
50   };
51
52 struct ts_test
53 {
54   GtkWidget *button;
55   char syntax[16];
56 };
57
58
59 static void
60 refresh (void *aux)
61 {
62   int i;
63   struct ts_test *tst = aux;
64
65   for (i = 0 ; i < n_Tests; ++i)
66     {
67       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tst[i].button), FALSE);
68     }
69 }
70
71
72 static gboolean
73 valid (void *aux)
74 {
75   int i;
76   struct ts_test *tst = aux;
77
78   for (i = 0 ; i < n_Tests; ++i)
79     {
80       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (tst[i].button)))
81         return TRUE;
82     }
83
84   return FALSE;
85 }
86
87
88
89 static gchar *
90 generate_syntax (struct paired_samples_dialog *psd, const struct ts_test *test)
91 {
92   int i;
93   gchar *text = NULL;
94   GString *str = g_string_new ("NPAR TEST");
95
96   for (i = 0 ; i < n_Tests; ++i)
97   {
98     if (! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (test[i].button)))
99       continue;
100
101     g_string_append (str, "\n\t");
102     g_string_append (str, test[i].syntax);
103
104     psppire_var_view_append_names (PSPPIRE_VAR_VIEW (psd->pairs_treeview), 0, str);
105
106     g_string_append (str, " WITH ");
107
108     psppire_var_view_append_names (PSPPIRE_VAR_VIEW (psd->pairs_treeview), 1, str);
109
110     g_string_append (str, " (PAIRED)");
111   }
112
113   g_string_append (str, ".\n");
114
115   text = str->str;
116   g_string_free (str, FALSE);
117
118   return text;
119 }
120
121 /* Pops up the dialog box */
122 void
123 two_related_dialog (PsppireDataWindow *de)
124 {
125   gint response;
126   struct ts_test nts[n_Tests];
127   struct paired_samples_dialog *tt_d = two_sample_dialog_create (de);
128
129   GtkWidget *frame = gtk_frame_new (_("Test Type"));
130   GtkWidget *bb = gtk_vbutton_box_new ();
131
132   strcpy (nts[NT_WILCOXON].syntax, "/WILCOXON");
133   strcpy (nts[NT_SIGN].syntax, "/SIGN");
134   strcpy (nts[NT_MCNEMAR].syntax, "/MCNEMAR");
135
136   nts[NT_WILCOXON].button = gtk_check_button_new_with_mnemonic (_("_Wilcoxon"));
137   nts[NT_SIGN].button = gtk_check_button_new_with_mnemonic (_("_Sign"));
138   nts[NT_MCNEMAR].button = gtk_check_button_new_with_mnemonic (_("_McNemar"));
139
140   gtk_box_pack_start (GTK_BOX (bb), nts[NT_WILCOXON].button, FALSE, FALSE, 5);
141   gtk_box_pack_start (GTK_BOX (bb), nts[NT_SIGN].button,     FALSE, FALSE, 5);
142   gtk_box_pack_start (GTK_BOX (bb), nts[NT_MCNEMAR].button,  FALSE, FALSE, 5);
143
144   gtk_container_add (GTK_CONTAINER (frame), bb);
145
146   gtk_widget_show_all (frame);
147   two_sample_dialog_add_widget (tt_d, frame);
148
149   tt_d->refresh = refresh;
150   tt_d->valid = valid;
151   tt_d->aux = nts;
152
153   gtk_window_set_title (GTK_WINDOW (tt_d->dialog), _("Two-Related-Samples Tests"));
154
155   response = psppire_dialog_run (PSPPIRE_DIALOG (tt_d->dialog));
156
157   switch (response)
158     {
159     case GTK_RESPONSE_OK:
160       g_free (execute_syntax_string (de, generate_syntax (tt_d, nts)));
161       break;
162     case PSPPIRE_RESPONSE_PASTE:
163       g_free (paste_syntax_to_window (generate_syntax (tt_d, nts)));
164       break;
165     default:
166       break;
167     }
168
169   two_sample_dialog_destroy (tt_d);
170 }