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>
34 #define _(msgid) gettext (msgid)
35 #define N_(msgid) msgid
58 GtkWidget *group_vars;
61 GtkToggleButton *ascending_togglebutton;
62 GtkToggleButton *summary_togglebutton;
65 /* Types subdialog widgets */
67 GtkWidget *types_dialog;
68 GtkWidget *ntiles_entry;
70 GtkToggleButton *func_button[n_RANK_FUNCS];
71 GtkWidget *formula_box;
73 GtkToggleButton *blom;
74 GtkToggleButton *tukey;
75 GtkToggleButton *rankit;
78 /* Ties subdialog widgets */
80 PsppireDialog *ties_dialog;
81 GtkToggleButton *mean;
83 GtkToggleButton *high;
84 GtkToggleButton *condense;
88 refresh (PsppireDialog *dialog, struct rank_dialog *rd)
90 GtkTreeModel *liststore;
92 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->rank_vars));
93 gtk_list_store_clear (GTK_LIST_STORE (liststore));
95 liststore = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
96 gtk_list_store_clear (GTK_LIST_STORE (liststore));
98 gtk_toggle_button_set_active (rd->ascending_togglebutton, TRUE);
99 gtk_toggle_button_set_active (rd->summary_togglebutton, FALSE);
103 generate_syntax (const struct rank_dialog *rd)
107 GtkTreeModel *gs = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->group_vars));
111 GString *str = g_string_new ("RANK VARIABLES=");
113 append_variable_names (str, rd->dict, GTK_TREE_VIEW (rd->rank_vars), 0);
115 g_string_append_printf (str, " (%c)",
116 gtk_toggle_button_get_active (rd->ascending_togglebutton)
119 if ( gtk_tree_model_get_iter_first (gs, ¬used) )
121 g_string_append (str, "\n\tBY ");
123 append_variable_names (str, rd->dict, GTK_TREE_VIEW (rd->group_vars), 0);
126 g_string_append (str, "\n\t/PRINT = ");
127 if (gtk_toggle_button_get_active (rd->summary_togglebutton))
128 g_string_append (str, "YES");
130 g_string_append (str, "NO");
133 if (gtk_toggle_button_get_active (rd->func_button [RANK]))
134 g_string_append (str, "\n\t/RANK");
135 if (gtk_toggle_button_get_active (rd->func_button [NORMAL]))
136 g_string_append (str, "\n\t/NORMAL");
137 if (gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
138 g_string_append (str, "\n\t/PROPORTION");
139 if (gtk_toggle_button_get_active (rd->func_button [PERCENT]))
140 g_string_append (str, "\n\t/PERCENT");
141 if (gtk_toggle_button_get_active (rd->func_button [RFRACTION]))
142 g_string_append (str, "\n\t/RFRACTION");
143 if (gtk_toggle_button_get_active (rd->func_button [N]))
144 g_string_append (str, "\n\t/N");
145 if (gtk_toggle_button_get_active (rd->func_button [SAVAGE]))
146 g_string_append (str, "\n\t/SAVAGE");
147 if (gtk_toggle_button_get_active (rd->func_button [NTILES]))
149 gint n = gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->ntiles_entry));
150 g_string_append_printf (str, "\n\t/NTILES(%d)", n);
154 if (gtk_toggle_button_get_active (rd->func_button [NORMAL])
156 gtk_toggle_button_get_active (rd->func_button [PROPORTION]))
158 g_string_append (str, "\n\t/FRACTION=");
160 if ( gtk_toggle_button_get_active (rd->blom))
161 g_string_append (str, "BLOM");
162 else if ( gtk_toggle_button_get_active (rd->tukey))
163 g_string_append (str, "TUKEY");
164 else if ( gtk_toggle_button_get_active (rd->rankit))
165 g_string_append (str, "RANKIT");
166 else if ( gtk_toggle_button_get_active (rd->vw))
167 g_string_append (str, "VW");
170 g_string_append (str, "\n\t/TIES=");
171 if ( gtk_toggle_button_get_active (rd->mean))
172 g_string_append (str, "MEAN");
173 else if ( gtk_toggle_button_get_active (rd->low))
174 g_string_append (str, "LOW");
175 else if ( gtk_toggle_button_get_active (rd->high))
176 g_string_append (str, "HIGH");
177 else if ( gtk_toggle_button_get_active (rd->condense))
178 g_string_append (str, "CONDENSE");
181 g_string_append (str, ".");
185 g_string_free (str, FALSE);
190 /* Dialog is valid iff at least one variable has been selected */
192 dialog_state_valid (gpointer data)
194 struct rank_dialog *rd = data;
196 GtkTreeModel *vars = gtk_tree_view_get_model (GTK_TREE_VIEW (rd->rank_vars));
200 return gtk_tree_model_get_iter_first (vars, ¬used);
203 static void on_ntiles_toggle (GtkToggleButton *, gpointer);
204 static void run_types_dialog (GtkButton *, gpointer);
205 static void run_ties_dialog (GtkButton *, gpointer );
208 set_sensitivity (struct rank_dialog *rd)
210 gboolean sens = gtk_toggle_button_get_active
211 (GTK_TOGGLE_BUTTON (rd->func_button[PROPORTION]))
213 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->func_button[NORMAL]));
215 gtk_widget_set_sensitive (rd->formula_box, sens);
218 /* Pops up the Rank dialog box */
220 rank_dialog (GObject *o, gpointer data)
223 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
225 struct rank_dialog rd;
227 GtkBuilder * builder = builder_new ("rank.ui");
229 GtkWidget *vars = get_widget_assert (builder, "dict-treeview");
230 GtkWidget *selector1 = get_widget_assert (builder, "psppire-selector1");
231 GtkWidget *selector2 = get_widget_assert (builder, "psppire-selector2");
234 GtkWidget *types_button = get_widget_assert (builder, "button1");
235 GtkWidget *ties_button = get_widget_assert (builder, "button2");
237 PsppireVarStore *vs = NULL;
239 g_object_get (de->data_editor, "var-store", &vs, NULL);
242 rd.rank_vars = get_widget_assert (builder, "variables-treeview");
243 rd.group_vars = get_widget_assert (builder, "group-vars-treeview");
244 rd.dialog = get_widget_assert (builder, "rank-dialog");
245 rd.ascending_togglebutton =
246 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "radiobutton1"));
248 rd.summary_togglebutton =
249 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "summary-checkbutton"));
251 rd.types_dialog = get_widget_assert (builder, "rank-types-dialog");
254 rd.ntiles_entry = get_widget_assert (builder, "ntiles-entry");
256 rd.func_button[RANK] =
257 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rank-checkbutton"));
259 rd.func_button[SAVAGE] =
260 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "savage-checkbutton"));
262 rd.func_button[RFRACTION] =
263 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rfrac-checkbutton"));
265 rd.func_button[PERCENT] =
266 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "percent-checkbutton"));
269 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "sum-checkbutton"));
271 rd.func_button[NTILES] =
272 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "ntiles-checkbutton"));
274 rd.func_button[PROPORTION] =
275 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "prop-checkbutton"));
277 rd.func_button[NORMAL] =
278 GTK_TOGGLE_BUTTON (get_widget_assert (builder, "normal-checkbutton"));
280 rd.formula_box = get_widget_assert (builder, "formula-frame");
282 rd.blom = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "blom-button"));
283 rd.tukey = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "tukey-button"));
284 rd.rankit = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "rankit-button"));
285 rd.vw = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "vw-button"));
288 rd.ties_dialog = PSPPIRE_DIALOG (get_widget_assert (builder, "ties-dialog"));
290 rd.mean = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "mean-button"));
291 rd.low = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "low-button"));
292 rd.high = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "high-button"));
293 rd.condense = GTK_TOGGLE_BUTTON (get_widget_assert (builder, "condense-button"));
295 g_signal_connect_swapped (rd.func_button[PROPORTION], "toggled",
296 G_CALLBACK (set_sensitivity),
299 g_signal_connect_swapped (rd.func_button[NORMAL], "toggled",
300 G_CALLBACK (set_sensitivity),
303 g_signal_connect (rd.func_button[NTILES], "toggled",
304 G_CALLBACK (on_ntiles_toggle),
307 gtk_window_set_transient_for (GTK_WINDOW (rd.dialog), GTK_WINDOW (de));
309 g_object_set (vars, "dictionary", vs->dict, NULL);
311 set_dest_model (GTK_TREE_VIEW (rd.rank_vars), vs->dict);
313 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
316 insert_source_row_into_tree_view,
320 set_dest_model (GTK_TREE_VIEW (rd.group_vars), vs->dict);
322 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
325 insert_source_row_into_tree_view,
330 g_signal_connect (types_button, "clicked",
331 G_CALLBACK (run_types_dialog), &rd);
333 g_signal_connect (ties_button, "clicked",
334 G_CALLBACK (run_ties_dialog), &rd);
336 g_signal_connect (rd.dialog, "refresh", G_CALLBACK (refresh), &rd);
338 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (rd.dialog),
339 dialog_state_valid, &rd);
341 response = psppire_dialog_run (PSPPIRE_DIALOG (rd.dialog));
346 case GTK_RESPONSE_OK:
348 gchar *syntax = generate_syntax (&rd);
350 struct getl_interface *sss = create_syntax_string_source (syntax);
351 execute_syntax (sss);
356 case PSPPIRE_RESPONSE_PASTE:
358 gchar *syntax = generate_syntax (&rd);
359 paste_syntax_in_new_window (syntax);
367 g_object_unref (builder);
372 types_dialog_reset (struct rank_dialog *rd)
376 for (i = 0 ; i < n_RANK_FUNCS ; ++i )
377 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->func_button [i]),
380 gtk_widget_set_sensitive (rd->ntiles_entry, FALSE);
382 gtk_widget_set_sensitive (rd->formula_box, FALSE);
388 run_types_dialog (GtkButton *b, gpointer data)
390 struct rank_dialog *rd = data;
393 gtk_window_set_transient_for (GTK_WINDOW (rd->types_dialog),
394 GTK_WINDOW (rd->dialog));
396 types_dialog_reset (rd);
398 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->types_dialog));
402 run_ties_dialog (GtkButton *b, gpointer data)
404 struct rank_dialog *rd = data;
407 gtk_window_set_transient_for (GTK_WINDOW (rd->ties_dialog),
408 GTK_WINDOW (rd->dialog));
411 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->ties_dialog));
416 on_ntiles_toggle (GtkToggleButton *toggle_button, gpointer data)
419 gboolean active = gtk_toggle_button_get_active (toggle_button);
420 gtk_widget_set_sensitive (w, active);