1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007 Free Software Foundation
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.
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.
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/>. */
19 #include "rank-dialog.h"
24 #include <language/syntax-string-source.h>
25 #include <ui/gui/psppire-data-window.h>
26 #include <ui/gui/dialog-common.h>
27 #include <ui/gui/dict-display.h>
28 #include <ui/gui/helper.h>
29 #include <ui/gui/psppire-dialog.h>
30 #include <ui/gui/psppire-var-store.h>
31 #include <ui/gui/psppire-var-view.h>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
59 GtkWidget *group_vars;
62 GtkToggleButton *ascending_togglebutton;
63 GtkToggleButton *summary_togglebutton;
66 /* Types subdialog widgets */
68 GtkWidget *types_dialog;
69 GtkWidget *ntiles_entry;
71 GtkToggleButton *func_button[n_RANK_FUNCS];
72 GtkWidget *formula_box;
74 GtkToggleButton *blom;
75 GtkToggleButton *tukey;
76 GtkToggleButton *rankit;
79 /* Ties subdialog widgets */
81 PsppireDialog *ties_dialog;
82 GtkToggleButton *mean;
84 GtkToggleButton *high;
85 GtkToggleButton *condense;
89 refresh (PsppireDialog *dialog, struct rank_dialog *rd)
91 GtkTreeModel *liststore;
93 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->rank_vars));
94 gtk_list_store_clear (GTK_LIST_STORE (liststore));
96 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
97 gtk_list_store_clear (GTK_LIST_STORE (liststore));
99 gtk_toggle_button_set_active (rd->ascending_togglebutton, TRUE);
100 gtk_toggle_button_set_active (rd->summary_togglebutton, FALSE);
104 generate_syntax (const struct rank_dialog *rd)
108 GtkTreeModel *gs = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
112 GString *str = g_string_new ("RANK VARIABLES=");
114 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->rank_vars), 0, str);
116 g_string_append_printf (str, " (%c)",
117 gtk_toggle_button_get_active (rd->ascending_togglebutton)
120 if ( gtk_tree_model_get_iter_first (gs, ¬used) )
122 g_string_append (str, "\n\tBY ");
124 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->group_vars), 0, str);
127 g_string_append (str, "\n\t/PRINT = ");
128 if (gtk_toggle_button_get_active (rd->summary_togglebutton))
129 g_string_append (str, "YES");
131 g_string_append (str, "NO");
134 if (gtk_toggle_button_get_active (rd->func_button [RANK]))
135 g_string_append (str, "\n\t/RANK");
136 if (gtk_toggle_button_get_active (rd->func_button [NORMAL]))
137 g_string_append (str, "\n\t/NORMAL");
138 if (gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
139 g_string_append (str, "\n\t/PROPORTION");
140 if (gtk_toggle_button_get_active (rd->func_button [PERCENT]))
141 g_string_append (str, "\n\t/PERCENT");
142 if (gtk_toggle_button_get_active (rd->func_button [RFRACTION]))
143 g_string_append (str, "\n\t/RFRACTION");
144 if (gtk_toggle_button_get_active (rd->func_button [N]))
145 g_string_append (str, "\n\t/N");
146 if (gtk_toggle_button_get_active (rd->func_button [SAVAGE]))
147 g_string_append (str, "\n\t/SAVAGE");
148 if (gtk_toggle_button_get_active (rd->func_button [NTILES]))
150 gint n = gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->ntiles_entry));
151 g_string_append_printf (str, "\n\t/NTILES(%d)", n);
155 if (gtk_toggle_button_get_active (rd->func_button [NORMAL])
157 gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
159 g_string_append (str, "\n\t/FRACTION=");
161 if ( gtk_toggle_button_get_active (rd->blom))
162 g_string_append (str, "BLOM");
163 else if ( gtk_toggle_button_get_active (rd->tukey))
164 g_string_append (str, "TUKEY");
165 else if ( gtk_toggle_button_get_active (rd->rankit))
166 g_string_append (str, "RANKIT");
167 else if ( gtk_toggle_button_get_active (rd->vw))
168 g_string_append (str, "VW");
171 g_string_append (str, "\n\t/TIES=");
172 if ( gtk_toggle_button_get_active (rd->mean))
173 g_string_append (str, "MEAN");
174 else if ( gtk_toggle_button_get_active (rd->low))
175 g_string_append (str, "LOW");
176 else if ( gtk_toggle_button_get_active (rd->high))
177 g_string_append (str, "HIGH");
178 else if ( gtk_toggle_button_get_active (rd->condense))
179 g_string_append (str, "CONDENSE");
182 g_string_append (str, ".");
186 g_string_free (str, FALSE);
191 /* Dialog is valid iff at least one variable has been selected */
193 dialog_state_valid (gpointer data)
195 struct rank_dialog *rd = data;
197 GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->rank_vars));
201 return gtk_tree_model_get_iter_first (vars, ¬used);
204 static void on_ntiles_toggle (GtkToggleButton *, gpointer);
205 static void run_types_dialog (GtkButton *, gpointer);
206 static void run_ties_dialog (GtkButton *, gpointer );
209 set_sensitivity (struct rank_dialog *rd)
211 gboolean sens = gtk_toggle_button_get_active
212 (GTK_TOGGLE_BUTTON (rd->func_button[PROPORTION]))
214 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->func_button[NORMAL]));
216 gtk_widget_set_sensitive (rd->formula_box, sens);
219 /* Pops up the Rank dialog box */
221 rank_dialog (PsppireDataWindow *de)
225 struct rank_dialog rd;
227 GtkBuilder * builder = builder_new ("rank.ui");
229 GtkWidget *vars = get_widget_assert (builder, "dict-treeview");
231 GtkWidget *types_button = get_widget_assert (builder, "button1");
232 GtkWidget *ties_button = get_widget_assert (builder, "button2");
234 PsppireVarStore *vs = NULL;
236 g_object_get (de->data_editor, "var-store", &vs, NULL);
238 g_object_get (vs, "dictionary", &rd.dict, NULL);
239 rd.rank_vars = get_widget_assert (builder, "variables-treeview");
240 rd.group_vars = get_widget_assert (builder, "group-vars-treeview");
241 rd.dialog = get_widget_assert (builder, "rank-dialog");
242 rd.ascending_togglebutton =
243 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "radiobutton1"));
245 rd.summary_togglebutton =
246 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "summary-checkbutton"));
248 rd.types_dialog = get_widget_assert (builder, "rank-types-dialog");
251 rd.ntiles_entry = get_widget_assert (builder, "ntiles-entry");
253 rd.func_button[RANK] =
254 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rank-checkbutton"));
256 rd.func_button[SAVAGE] =
257 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "savage-checkbutton"));
259 rd.func_button[RFRACTION] =
260 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rfrac-checkbutton"));
262 rd.func_button[PERCENT] =
263 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "percent-checkbutton"));
266 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "sum-checkbutton"));
268 rd.func_button[NTILES] =
269 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "ntiles-checkbutton"));
271 rd.func_button[PROPORTION] =
272 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "prop-checkbutton"));
274 rd.func_button[NORMAL] =
275 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "normal-checkbutton"));
277 rd.formula_box = get_widget_assert (builder, "formula-frame");
279 rd.blom = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "blom-button"));
280 rd.tukey = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "tukey-button"));
281 rd.rankit = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rankit-button"));
282 rd.vw = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "vw-button"));
285 rd.ties_dialog = PSPPIRE_DIALOG (get_widget_assert (builder, "ties-dialog"));
287 rd.mean = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "mean-button"));
288 rd.low = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "low-button"));
289 rd.high = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "high-button"));
290 rd.condense = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "condense-button"));
292 g_signal_connect_swapped (rd.func_button[PROPORTION], "toggled",
293 G_CALLBACK (set_sensitivity),
296 g_signal_connect_swapped (rd.func_button[NORMAL], "toggled",
297 G_CALLBACK (set_sensitivity),
300 g_signal_connect (rd.func_button[NTILES], "toggled",
301 G_CALLBACK (on_ntiles_toggle),
304 gtk_window_set_transient_for (GTK_WINDOW (rd.dialog), GTK_WINDOW (de));
306 g_object_set (vars, "model", rd.dict, NULL);
308 g_signal_connect (types_button, "clicked",
309 G_CALLBACK (run_types_dialog), &rd);
311 g_signal_connect (ties_button, "clicked",
312 G_CALLBACK (run_ties_dialog), &rd);
314 g_signal_connect (rd.dialog, "refresh", G_CALLBACK (refresh), &rd);
316 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (rd.dialog),
317 dialog_state_valid, &rd);
319 response = psppire_dialog_run (PSPPIRE_DIALOG (rd.dialog));
324 case GTK_RESPONSE_OK:
326 gchar *syntax = generate_syntax (&rd);
328 struct getl_interface *sss = create_syntax_string_source (syntax);
329 execute_syntax (sss);
334 case PSPPIRE_RESPONSE_PASTE:
336 gchar *syntax = generate_syntax (&rd);
337 paste_syntax_to_window (syntax);
345 g_object_unref (builder);
350 types_dialog_reset (struct rank_dialog *rd)
354 for (i = 0 ; i < n_RANK_FUNCS ; ++i )
355 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->func_button [i]),
358 gtk_widget_set_sensitive (rd->ntiles_entry, FALSE);
360 gtk_widget_set_sensitive (rd->formula_box, FALSE);
366 run_types_dialog (GtkButton *b, gpointer data)
368 struct rank_dialog *rd = data;
371 gtk_window_set_transient_for (GTK_WINDOW (rd->types_dialog),
372 GTK_WINDOW (rd->dialog));
374 types_dialog_reset (rd);
376 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->types_dialog));
380 run_ties_dialog (GtkButton *b, gpointer data)
382 struct rank_dialog *rd = data;
385 gtk_window_set_transient_for (GTK_WINDOW (rd->ties_dialog),
386 GTK_WINDOW (rd->dialog));
389 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->ties_dialog));
394 on_ntiles_toggle (GtkToggleButton *toggle_button, gpointer data)
397 gboolean active = gtk_toggle_button_get_active (toggle_button);
398 gtk_widget_set_sensitive (w, active);