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