d08fc109aa69ad021a6b7d909e8dc842d79d2173
[pspp] / src / ui / gui / psppire-dialog-action-paired.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2012, 2013  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
20 #include "psppire-dialog-action-paired.h"
21
22 #include "psppire-var-view.h"
23
24 #include "psppire-selector.h"
25 #include "psppire-dialog.h"
26 #include "builder-wrapper.h"
27
28 #include "t-test-options.h"
29
30 #include <gettext.h>
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
33
34
35 static void psppire_dialog_action_paired_init            (PsppireDialogActionPaired      *act);
36 static void psppire_dialog_action_paired_class_init      (PsppireDialogActionPairedClass *class);
37
38 G_DEFINE_TYPE (PsppireDialogActionPaired, psppire_dialog_action_paired, PSPPIRE_TYPE_DIALOG_ACTION);
39
40
41 static gboolean
42 dialog_state_valid (gpointer data)
43 {
44   PsppireDialogActionPaired *pd = PSPPIRE_DIALOG_ACTION_PAIRED (data);
45   gint n_rows = gtk_tree_model_iter_n_children  (GTK_TREE_MODEL (pd->list_store), NULL);
46   struct variable *v = NULL;
47   GtkTreeIter dest_iter;
48
49   if ( n_rows == 0 )
50     return FALSE;
51
52   /* Get the last row */
53   gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (pd->list_store), &dest_iter,
54                                  NULL, n_rows - 1);
55
56   /* Get the last (2nd) column */
57   gtk_tree_model_get (GTK_TREE_MODEL (pd->list_store), &dest_iter, 1, &v, -1);
58
59   if (v == NULL)
60     return FALSE;
61     
62   /* if ( NULL == pd->valid) */
63   /*   return TRUE; */
64
65   return TRUE;
66   //  return pd->valid (pd->aux);
67 }
68
69 static void
70 refresh (PsppireDialogAction *rd_)
71 {
72   PsppireDialogActionPaired *pd = PSPPIRE_DIALOG_ACTION_PAIRED (rd_);
73
74   gtk_list_store_clear (GTK_LIST_STORE (pd->list_store));
75 }
76
77
78 static void
79 select_as_pair_member (GtkTreeIter source_iter,
80                        GtkWidget *dest,
81                        GtkTreeModel *source_model,
82                        gpointer data)
83 {
84   struct variable *v;
85   struct variable *v1;
86   gint n_rows;
87   GtkTreeIter dest_iter;
88   PsppireDialogActionPaired *tt_d = PSPPIRE_DIALOG_ACTION_PAIRED (data);
89
90
91   gtk_tree_model_get (source_model, &source_iter,
92                       DICT_TVM_COL_VAR, &v, -1);
93
94   n_rows = gtk_tree_model_iter_n_children  (GTK_TREE_MODEL (tt_d->list_store), NULL);
95
96   if ( n_rows > 0 )
97     {
98
99       gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (tt_d->list_store),
100                                      &dest_iter, NULL, n_rows - 1);
101
102       gtk_tree_model_get (GTK_TREE_MODEL (tt_d->list_store), &dest_iter, 1, &v1, -1);
103     }
104   else
105     v1 = NULL;
106
107   if ( n_rows == 0 || v1 != NULL)
108     {
109       gtk_list_store_append (tt_d->list_store, &dest_iter);
110
111       gtk_list_store_set (tt_d->list_store, &dest_iter,
112                           0, v,
113                           1, NULL,
114                           -1);
115     }
116   else
117     {
118       gtk_list_store_set (tt_d->list_store, &dest_iter,
119                           1, v,
120                           -1);
121     }
122 }
123
124
125
126 static gchar *
127 generate_syntax (PsppireDialogAction *pda)
128 {
129   PsppireDialogActionPaired *d = PSPPIRE_DIALOG_ACTION_PAIRED (pda);
130   gchar *text = NULL;
131   GString *str =   g_string_new ("T-TEST \n\tPAIRS = ");
132
133   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->pairs_treeview), 0, str);
134
135   g_string_append (str, " WITH ");
136
137   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (d->pairs_treeview), 1, str);
138
139   g_string_append (str, " (PAIRED)");
140   g_string_append (str, "\n");
141
142   tt_options_dialog_append_syntax (d->opt, str);
143
144   g_string_append (str, ".\n");
145
146   text = str->str;
147   g_string_free (str, FALSE);
148
149   return text;
150 }
151
152 static void
153 psppire_dialog_action_paired_activate (GtkAction *a)
154 {
155   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
156   PsppireDialogActionPaired *act = PSPPIRE_DIALOG_ACTION_PAIRED (a);
157
158   GtkBuilder *xml = builder_new ("paired-samples.ui");
159   GtkWidget *selector = get_widget_assert (xml, "psppire-selector3");
160   GtkWidget *bb = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
161   GtkWidget *button = gtk_button_new_with_mnemonic (_("O_ptions..."));
162   GtkWidget *box = get_widget_assert (xml, "vbox3");
163
164
165   pda->dialog = get_widget_assert   (xml, "t-test-paired-samples-dialog");
166   pda->source = get_widget_assert   (xml, "paired-samples-t-test-treeview1");
167
168   gtk_window_set_title (GTK_WINDOW (pda->dialog), _("Paired Samples T Test"));
169
170   act->pairs_treeview = get_widget_assert (xml, "paired-samples-t-test-treeview2");
171   act->list_store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (act->pairs_treeview)));
172
173   act->opt = tt_options_dialog_create (GTK_WINDOW (pda->toplevel));
174
175
176   g_signal_connect_swapped (button, "clicked", G_CALLBACK (tt_options_dialog_run), act->opt);
177
178
179   gtk_box_pack_start (GTK_BOX (bb), button, TRUE, TRUE, 5);
180   gtk_box_pack_start (GTK_BOX (box), bb, FALSE, FALSE, 5);
181   gtk_widget_show_all (box);
182  
183
184   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
185   psppire_dialog_action_set_refresh (pda, refresh);
186
187   g_object_set (pda->source,
188                 "predicate", var_is_numeric,
189                 NULL);
190
191   psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
192                                     select_as_pair_member,
193                                     act);
194   
195   g_object_unref (xml);
196
197   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_paired_parent_class)->activate)
198     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_paired_parent_class)->activate (pda);
199 }
200
201 static void
202 psppire_dialog_action_paired_finalize (GObject *o)
203 {
204   PsppireDialogActionPaired *act = PSPPIRE_DIALOG_ACTION_PAIRED (o);
205   tt_options_dialog_destroy (act->opt);
206 }
207
208 static void
209 psppire_dialog_action_paired_class_init (PsppireDialogActionPairedClass *class)
210 {
211   GObjectClass *object_class = G_OBJECT_CLASS (class);
212   psppire_dialog_action_set_activation (class, psppire_dialog_action_paired_activate);
213   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
214
215   object_class->finalize = psppire_dialog_action_paired_finalize;
216 }
217
218
219 static void
220 psppire_dialog_action_paired_init (PsppireDialogActionPaired *act)
221 {
222   act->opt = NULL;
223 }
224