Rename psppire_dialog_action_get_pointer -> psppire_dialog_action_get_hash_table
[pspp] / src / ui / gui / psppire-dialog-action-rank.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2010, 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 "psppire-dialog-action-rank.h"
21
22 #include "psppire-var-view.h"
23 #include "dialog-common.h"
24 #include "psppire-selector.h"
25 #include "psppire-dict.h"
26 #include "psppire-dialog.h"
27 #include "builder-wrapper.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33 static void psppire_dialog_action_rank_class_init      (PsppireDialogActionRankClass *class);
34
35 G_DEFINE_TYPE (PsppireDialogActionRank, psppire_dialog_action_rank, PSPPIRE_TYPE_DIALOG_ACTION);
36
37
38 static char *
39 generate_syntax (PsppireDialogAction *act)
40 {
41   PsppireDialogActionRank *rd  = PSPPIRE_DIALOG_ACTION_RANK (act);
42
43   gchar *text = NULL;
44   GtkTreeModel *gs = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
45
46   GtkTreeIter notused;
47
48   GString *str = g_string_new ("RANK VARIABLES=");
49
50   psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->rank_vars), 0, str);
51
52   g_string_append_printf (str, " (%c)",
53                    gtk_toggle_button_get_active (rd->ascending_togglebutton)
54                    ?'A':'D');
55
56   if (  gtk_tree_model_get_iter_first (gs, &notused) )
57     {
58       g_string_append (str, "\n\tBY ");
59
60       psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->group_vars),  0, str);
61     }
62
63   g_string_append (str, "\n\t/PRINT = ");
64   if (gtk_toggle_button_get_active (rd->summary_togglebutton))
65     g_string_append (str, "YES");
66   else
67     g_string_append (str, "NO");
68
69
70   if (gtk_toggle_button_get_active (rd->func_button [RANK]))
71     g_string_append (str, "\n\t/RANK");
72   if (gtk_toggle_button_get_active (rd->func_button [NORMAL]))
73     g_string_append (str, "\n\t/NORMAL");
74   if (gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
75     g_string_append (str, "\n\t/PROPORTION");
76   if (gtk_toggle_button_get_active (rd->func_button [PERCENT]))
77     g_string_append (str, "\n\t/PERCENT");
78   if (gtk_toggle_button_get_active (rd->func_button [RFRACTION]))
79     g_string_append (str, "\n\t/RFRACTION");
80   if (gtk_toggle_button_get_active (rd->func_button [N]))
81     g_string_append (str, "\n\t/N");
82   if (gtk_toggle_button_get_active (rd->func_button [SAVAGE]))
83     g_string_append (str, "\n\t/SAVAGE");
84   if (gtk_toggle_button_get_active (rd->func_button [NTILES]))
85     {
86       gint n = gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->ntiles_entry));
87       g_string_append_printf (str, "\n\t/NTILES(%d)", n);
88     }
89
90
91   if (gtk_toggle_button_get_active (rd->func_button [NORMAL])
92       ||
93       gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
94     {
95       g_string_append (str, "\n\t/FRACTION=");
96
97       if ( gtk_toggle_button_get_active (rd->blom))
98         g_string_append (str, "BLOM");
99       else if ( gtk_toggle_button_get_active (rd->tukey))
100         g_string_append (str, "TUKEY");
101       else if ( gtk_toggle_button_get_active (rd->rankit))
102         g_string_append (str, "RANKIT");
103       else if ( gtk_toggle_button_get_active (rd->vw))
104         g_string_append (str, "VW");
105     }
106
107   g_string_append (str, "\n\t/TIES=");
108   if ( gtk_toggle_button_get_active (rd->mean))
109     g_string_append (str, "MEAN");
110   else if ( gtk_toggle_button_get_active (rd->low))
111     g_string_append (str, "LOW");
112   else if ( gtk_toggle_button_get_active (rd->high))
113     g_string_append (str, "HIGH");
114   else if ( gtk_toggle_button_get_active (rd->condense))
115     g_string_append (str, "CONDENSE");
116
117
118   g_string_append (str, ".");
119   text = str->str;
120
121   g_string_free (str, FALSE);
122
123   return text;
124 }
125
126 static gboolean
127 dialog_state_valid (PsppireDialogAction *da)
128 {
129   PsppireDialogActionRank *dar  = PSPPIRE_DIALOG_ACTION_RANK (da);
130   GtkTreeIter notused;
131   GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (dar->rank_vars));
132
133   return gtk_tree_model_get_iter_first (vars, &notused);
134 }
135
136 static void
137 dialog_refresh (PsppireDialogAction *act)
138 {
139   PsppireDialogActionRank *dar  = PSPPIRE_DIALOG_ACTION_RANK (act);
140
141   GtkTreeModel *liststore;
142
143   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dar->rank_vars));
144   gtk_list_store_clear (GTK_LIST_STORE (liststore));
145
146   liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (dar->group_vars));
147   gtk_list_store_clear (GTK_LIST_STORE (liststore));
148
149   gtk_toggle_button_set_active (dar->ascending_togglebutton, TRUE);
150   gtk_toggle_button_set_active (dar->summary_togglebutton, FALSE);
151 }
152
153 static void
154 types_dialog_reset (PsppireDialogActionRank *rd)
155 {
156   gint i;
157
158   for (i = 0 ; i < n_RANK_FUNCS ; ++i )
159     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->func_button [i]),
160                                   FALSE);
161
162   gtk_widget_set_sensitive (rd->ntiles_entry, FALSE);
163
164   gtk_widget_set_sensitive (rd->formula_box, FALSE);
165 }
166
167 static void
168 run_types_dialog (GtkButton *b, PsppireDialogActionRank *dar)
169 {
170   PsppireDialogAction *act  = PSPPIRE_DIALOG_ACTION (dar);
171
172   gtk_window_set_transient_for (GTK_WINDOW (dar->types_dialog),
173                                 GTK_WINDOW (act->dialog));
174
175   types_dialog_reset (dar);
176
177   psppire_dialog_run (PSPPIRE_DIALOG (dar->types_dialog));
178 }
179
180 static void
181 run_ties_dialog (GtkButton *b,  PsppireDialogActionRank *dar)
182 {
183   PsppireDialogAction *act  = PSPPIRE_DIALOG_ACTION (dar);
184
185   gtk_window_set_transient_for (GTK_WINDOW (dar->ties_dialog),
186                                 GTK_WINDOW (act->dialog));
187
188   psppire_dialog_run (PSPPIRE_DIALOG (dar->ties_dialog));
189 }
190
191 static void
192 on_ntiles_toggle (GtkToggleButton *toggle_button, PsppireDialogActionRank *dar)
193 {
194   gboolean active = gtk_toggle_button_get_active (toggle_button);
195   gtk_widget_set_sensitive (GTK_WIDGET (dar), active);
196 }
197
198 static void
199 set_sensitivity (PsppireDialogActionRank *dar)
200 {
201   gboolean sens = gtk_toggle_button_get_active
202     (GTK_TOGGLE_BUTTON (dar->func_button[PROPORTION]))
203     ||
204     gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dar->func_button[NORMAL]));
205
206   gtk_widget_set_sensitive (dar->formula_box, sens);
207 }
208
209 static void
210 psppire_dialog_action_rank_activate (GtkAction *a)
211 {
212   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (a);
213   PsppireDialogActionRank *act = PSPPIRE_DIALOG_ACTION_RANK (a);
214
215   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
216   GtkBuilder *xml = g_hash_table_lookup (thing, a);
217   if (!xml)
218     {
219       xml = builder_new ("rank.ui");
220       g_hash_table_insert (thing, a, xml);
221     }
222
223   GtkWidget *types_button = get_widget_assert (xml, "button1");
224   GtkWidget *ties_button = get_widget_assert (xml, "button2");
225
226   pda->dialog    = get_widget_assert   (xml, "rank-dialog");
227   pda->source    = get_widget_assert   (xml, "dict-treeview");
228   act->rank_vars = get_widget_assert   (xml, "variables-treeview");
229   act->group_vars =  get_widget_assert (xml, "group-vars-treeview");
230   act->ascending_togglebutton =
231     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
232
233   act->summary_togglebutton =
234     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "summary-checkbutton"));
235
236   act->types_dialog = get_widget_assert (xml, "rank-types-dialog");
237
238
239   act->ntiles_entry  = get_widget_assert (xml, "ntiles-entry");
240
241   act->func_button[RANK]    =
242     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rank-checkbutton"));
243
244   act->func_button[SAVAGE]  =
245     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "savage-checkbutton"));
246
247   act->func_button[RFRACTION] =
248     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rfrac-checkbutton"));
249
250   act->func_button[PERCENT] =
251     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "percent-checkbutton"));
252
253   act->func_button[N]       =
254     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "sum-checkbutton"));
255
256   act->func_button[NTILES] =
257     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ntiles-checkbutton"));
258
259   act->func_button[PROPORTION] =
260     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "prop-checkbutton"));
261
262   act->func_button[NORMAL] =
263     GTK_TOGGLE_BUTTON (get_widget_assert (xml, "normal-checkbutton"));
264
265   act->formula_box = get_widget_assert (xml, "formula-frame");
266
267   act->blom = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "blom-button"));
268   act->tukey = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "tukey-button"));
269   act->rankit = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rankit-button"));
270   act->vw = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "vw-button"));
271
272   /* Ties dialog */
273   act->ties_dialog = PSPPIRE_DIALOG (get_widget_assert (xml, "ties-dialog"));
274
275   act->mean = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "mean-button"));
276   act->low = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "low-button"));
277   act->high = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "high-button"));
278   act->condense = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "condense-button"));
279
280   g_signal_connect_swapped (act->func_button[PROPORTION], "toggled",
281                             G_CALLBACK (set_sensitivity),
282                             act);
283
284   g_signal_connect_swapped (act->func_button[NORMAL], "toggled",
285                             G_CALLBACK (set_sensitivity),
286                             act);
287
288   g_signal_connect (types_button, "clicked",
289                     G_CALLBACK (run_types_dialog),  act);
290
291   g_signal_connect (ties_button, "clicked",
292                     G_CALLBACK (run_ties_dialog),  act);
293
294   g_signal_connect (act->func_button[NTILES], "toggled",
295                     G_CALLBACK (on_ntiles_toggle),
296                     act->ntiles_entry);
297
298   psppire_dialog_action_set_valid_predicate (pda, (void *) dialog_state_valid);
299   psppire_dialog_action_set_refresh (pda, dialog_refresh);
300
301   PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_rank_parent_class)->activate (pda);
302 }
303
304 static void
305 psppire_dialog_action_rank_class_init (PsppireDialogActionRankClass *class)
306 {
307   psppire_dialog_action_set_activation (class, psppire_dialog_action_rank_activate);
308
309   PSPPIRE_DIALOG_ACTION_CLASS (class)->generate_syntax = generate_syntax;
310 }
311
312 static void
313 psppire_dialog_action_rank_init (PsppireDialogActionRank *act)
314 {
315 }