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