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