Merge master into gtk3.
[pspp] / src / ui / gui / psppire-dialog-action-chisquare.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2010, 2011, 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-chisquare.h"
21
22 #include "psppire-var-view.h"
23
24 #include "psppire-dialog.h"
25 #include "builder-wrapper.h"
26 #include "psppire-acr.h"
27 #include "dialog-common.h"
28
29 #include <libpspp/str.h>
30
31 static void psppire_dialog_action_chisquare_init            (PsppireDialogActionChisquare      *act);
32 static void psppire_dialog_action_chisquare_class_init      (PsppireDialogActionChisquareClass *class);
33
34 G_DEFINE_TYPE (PsppireDialogActionChisquare, psppire_dialog_action_chisquare, PSPPIRE_TYPE_DIALOG_ACTION);
35
36
37 static char *
38 generate_syntax (PsppireDialogAction *act)
39 {
40   PsppireDialogActionChisquare *scd = PSPPIRE_DIALOG_ACTION_CHISQUARE (act);
41
42   gchar *text;
43   struct string dss;
44
45   ds_init_cstr (&dss, "NPAR TEST\n\t/CHISQUARE=");
46
47   psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (scd->var_view), 0, &dss);
48
49   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->range_button)))
50     {
51       ds_put_cstr (&dss, "(");
52       
53       ds_put_cstr (&dss, 
54                        gtk_entry_get_text (GTK_ENTRY (scd->value_lower)));
55
56       ds_put_cstr (&dss, ", ");
57
58       ds_put_cstr (&dss,
59                        gtk_entry_get_text (GTK_ENTRY (scd->value_upper)));
60
61       ds_put_cstr (&dss, ")");
62     }
63
64   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (scd->values_button)))
65     {
66       GtkListStore *ls = scd->expected_list;
67       GtkTreeIter iter;
68       gboolean ok;
69
70       ds_put_cstr (&dss, "\n\t");
71       ds_put_cstr (&dss, "/EXPECTED = ");
72
73       
74       for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ls),
75                                                &iter);
76            ok;
77            ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (ls), &iter))
78         {
79           gdouble v;
80
81           gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter, 0, &v, -1);
82
83           ds_put_c_format (&dss, " %g", v);
84         }
85     }
86
87   ds_put_cstr (&dss, ".\n");
88
89   text = ds_steal_cstr (&dss);
90
91   ds_destroy (&dss);
92
93   return text;
94 }
95
96
97 static gboolean
98 dialog_state_valid (gpointer a)
99 {
100   GtkTreeIter notused;
101   PsppireDialogActionChisquare *act = PSPPIRE_DIALOG_ACTION_CHISQUARE (a);
102
103   GtkTreeModel *vars =
104     gtk_tree_view_get_model (GTK_TREE_VIEW (act->var_view));
105
106   if ( !gtk_tree_model_get_iter_first (vars, &notused) )
107     return FALSE;
108
109   return TRUE;
110 }
111
112 static void
113 refresh (PsppireDialogAction *rd_)
114 {
115   PsppireDialogActionChisquare *csd = PSPPIRE_DIALOG_ACTION_CHISQUARE (rd_);
116
117   GtkTreeModel *liststore =
118     gtk_tree_view_get_model (GTK_TREE_VIEW (csd->var_view));
119
120   gtk_list_store_clear (GTK_LIST_STORE (liststore));
121
122   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (csd->button1), TRUE);
123
124   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (csd->button2), TRUE);
125   gtk_entry_set_text (GTK_ENTRY (csd->value_lower), "");
126   gtk_entry_set_text (GTK_ENTRY (csd->value_upper), "");
127 }
128
129 static void
130 psppire_dialog_action_chisquare_activate (GtkAction *a)
131 {
132   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
133   PsppireDialogActionChisquare *act = PSPPIRE_DIALOG_ACTION_CHISQUARE (a);
134
135   GtkBuilder *xml = builder_new ("chi-square.ui");
136
137   GtkWidget *range_table = get_widget_assert   (xml, "range-table");
138   GtkWidget *values_acr = get_widget_assert   (xml, "psppire-acr1");
139   GtkWidget *expected_value_entry =
140     get_widget_assert   (xml, "expected-value-entry");
141
142   pda->dialog = get_widget_assert   (xml, "chisquare-dialog");
143   pda->source = get_widget_assert   (xml, "dict-view");
144
145   act->expected_list = gtk_list_store_new (1, G_TYPE_DOUBLE);
146
147   act->button1 = get_widget_assert   (xml, "radiobutton1");
148   act->button2 = get_widget_assert   (xml, "radiobutton3");
149   act->var_view = get_widget_assert   (xml, "variables-treeview");
150
151   act->range_button = get_widget_assert   (xml, "radiobutton4");
152   act->value_lower = get_widget_assert   (xml, "entry1");
153   act->value_upper = get_widget_assert   (xml, "entry2");
154
155   act->values_button = get_widget_assert   (xml, "radiobutton2");
156
157   psppire_dialog_action_set_valid_predicate (pda, dialog_state_valid);
158   psppire_dialog_action_set_refresh (pda, refresh);
159
160   g_object_unref (xml);
161
162   g_signal_connect (act->range_button, "toggled", 
163                     G_CALLBACK (set_sensitivity_from_toggle), 
164                     range_table);
165
166
167   g_signal_connect (act->values_button, "toggled", 
168                     G_CALLBACK (set_sensitivity_from_toggle), 
169                     values_acr);
170
171   g_signal_connect (act->values_button, "toggled", 
172                     G_CALLBACK (set_sensitivity_from_toggle), 
173                     expected_value_entry);
174
175   psppire_acr_set_entry (PSPPIRE_ACR (values_acr),
176                          GTK_ENTRY (expected_value_entry));
177
178   psppire_acr_set_model(PSPPIRE_ACR (values_acr), act->expected_list);
179
180
181   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_chisquare_parent_class)->activate)
182     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_chisquare_parent_class)->activate (pda);
183 }
184
185 static void
186 psppire_dialog_action_chisquare_class_init (PsppireDialogActionChisquareClass *class)
187 {
188   GtkActionClass *action_class = GTK_ACTION_CLASS (class);
189
190   action_class->activate = psppire_dialog_action_chisquare_activate;
191   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
192 }
193
194
195 static void
196 psppire_dialog_action_chisquare_init (PsppireDialogActionChisquare *act)
197 {
198 }
199