39a8ae91f196eb588c580d00c27917d69c9c41ad
[pspp-builds.git] / src / ui / gui / paired-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
18 #include <config.h>
19
20 #include <gtk/gtk.h>
21
22
23 #include "paired-dialog.h"
24
25 #include "psppire-data-window.h"
26 #include "psppire-selector.h"
27 #include "psppire-var-view.h"
28
29 #include "psppire-dict.h"
30 #include "psppire-var-store.h"
31
32 #include "dialog-common.h"
33 #include "psppire-dialog.h"
34
35 #include "psppire-var-ptr.h"
36
37
38 #include "helper.h"
39
40
41
42 static void
43 refresh (struct paired_samples_dialog *tt_d)
44 {
45   gtk_list_store_clear (GTK_LIST_STORE (tt_d->list_store));
46
47   if (tt_d->refresh)
48     tt_d->refresh (tt_d->aux);
49 }
50
51 static gboolean
52 dialog_state_valid (gpointer data)
53 {
54   struct variable *v = NULL;
55   struct paired_samples_dialog *tt_d = data;
56   GtkTreeIter dest_iter;
57
58   gint n_rows = gtk_tree_model_iter_n_children  (tt_d->list_store, NULL);
59
60   if ( n_rows == 0 )
61     return FALSE;
62
63   /* Get the last row */
64   gtk_tree_model_iter_nth_child (tt_d->list_store, &dest_iter,
65                                  NULL, n_rows - 1);
66
67   /* Get the last (2nd) column */
68   gtk_tree_model_get (tt_d->list_store, &dest_iter, 1, &v, -1);
69
70
71   if (v == NULL)
72     return FALSE;
73     
74   if ( NULL == tt_d->valid)
75     return TRUE;
76
77   return tt_d->valid (tt_d->aux);
78 }
79
80
81
82 static void
83 select_as_pair_member (GtkTreeIter source_iter,
84                        GtkWidget *dest,
85                        GtkTreeModel *source_model,
86                        gpointer data)
87 {
88   struct variable *v;
89   struct variable *v1;
90   gint n_rows;
91   GtkTreeIter dest_iter;
92   struct paired_samples_dialog *tt_d = data;
93
94
95   gtk_tree_model_get (source_model, &source_iter,
96                       DICT_TVM_COL_VAR, &v, -1);
97
98   n_rows = gtk_tree_model_iter_n_children  (tt_d->list_store, NULL);
99
100   if ( n_rows > 0 )
101     {
102
103       gtk_tree_model_iter_nth_child (tt_d->list_store,
104                                      &dest_iter, NULL, n_rows - 1);
105
106       gtk_tree_model_get (tt_d->list_store, &dest_iter, 1, &v1, -1);
107     }
108   else
109     v1 = NULL;
110
111   if ( n_rows == 0 || v1 != NULL)
112     {
113       gtk_list_store_append (GTK_LIST_STORE (tt_d->list_store), &dest_iter);
114
115       gtk_list_store_set (GTK_LIST_STORE (tt_d->list_store), &dest_iter,
116                           0, v,
117                           1, NULL,
118                           -1);
119     }
120   else
121     {
122       gtk_list_store_set (GTK_LIST_STORE (tt_d->list_store), &dest_iter,
123                           1, v,
124                           -1);
125
126     }
127 }
128
129 void
130 two_sample_dialog_add_widget (struct paired_samples_dialog *psd, GtkWidget *w)
131 {
132   GtkWidget *box = get_widget_assert (psd->xml, "vbox3");
133   gtk_box_pack_start (GTK_BOX (box), w, FALSE, FALSE,  5);
134 }
135
136 void
137 two_sample_dialog_destroy (struct paired_samples_dialog *psd)
138 {
139   g_object_unref (psd->xml);
140   free (psd);
141 }
142
143 struct paired_samples_dialog *
144 two_sample_dialog_create (PsppireDataWindow *de)
145 {
146   GtkWidget *dict_view ;
147   GtkWidget *selector ;
148   struct paired_samples_dialog *tt_d = g_malloc (sizeof *tt_d);
149
150   PsppireVarStore *vs = NULL;
151
152   tt_d->xml = builder_new ("paired-samples.ui");
153
154   dict_view = get_widget_assert (tt_d->xml, "paired-samples-t-test-treeview1");
155
156   selector = get_widget_assert (tt_d->xml, "psppire-selector3");
157
158   tt_d->dialog = get_widget_assert (tt_d->xml, "t-test-paired-samples-dialog");
159
160   g_object_get (de->data_editor, "var-store", &vs, NULL);
161
162   g_object_get (vs, "dictionary", &tt_d->dict, NULL);
163   tt_d->pairs_treeview =
164    get_widget_assert (tt_d->xml, "paired-samples-t-test-treeview2");
165
166   gtk_window_set_transient_for (GTK_WINDOW (tt_d->dialog), GTK_WINDOW (de));
167
168
169   g_object_set (dict_view, "model", tt_d->dict,
170                 "predicate",
171                 var_is_numeric, NULL);
172
173   
174   tt_d->list_store = gtk_tree_view_get_model (GTK_TREE_VIEW (tt_d->pairs_treeview));
175
176   psppire_selector_set_select_func (PSPPIRE_SELECTOR (selector),
177                                     select_as_pair_member,
178                                     tt_d);
179
180   g_signal_connect_swapped (tt_d->dialog, "refresh",
181                             G_CALLBACK (refresh),  tt_d);
182
183   psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (tt_d->dialog),
184                                       dialog_state_valid, tt_d);
185
186   return tt_d;
187 }