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/data-editor.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/syntax-editor.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 struct data_editor *de = data;
225 struct rank_dialog rd;
227 GladeXML *xml = XML_NEW ("rank.glade");
229 GtkWidget *vars = get_widget_assert (xml, "dict-treeview");
230 GtkWidget *selector1 = get_widget_assert (xml, "psppire-selector1");
231 GtkWidget *selector2 = get_widget_assert (xml, "psppire-selector2");
234 GtkWidget *types_button = get_widget_assert (xml, "button1");
235 GtkWidget *ties_button = get_widget_assert (xml, "button2");
237 PsppireVarStore *vs = NULL;
239 g_object_get (de->data_editor, "var-store", &vs, NULL);
242 rd.rank_vars = get_widget_assert (xml, "variables-treeview");
243 rd.group_vars = get_widget_assert (xml, "group-vars-treeview");
244 rd.dialog = get_widget_assert (xml, "rank-dialog");
245 rd.ascending_togglebutton =
246 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "radiobutton1"));
248 rd.summary_togglebutton =
249 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "summary-checkbutton"));
251 rd.types_dialog = get_widget_assert (xml, "rank-types-dialog");
254 rd.ntiles_entry = get_widget_assert (xml, "ntiles-entry");
256 rd.func_button[RANK] =
257 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rank-checkbutton"));
259 rd.func_button[SAVAGE] =
260 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "savage-checkbutton"));
262 rd.func_button[RFRACTION] =
263 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rfrac-checkbutton"));
265 rd.func_button[PERCENT] =
266 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "percent-checkbutton"));
269 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "sum-checkbutton"));
271 rd.func_button[NTILES] =
272 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "ntiles-checkbutton"));
274 rd.func_button[PROPORTION] =
275 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "prop-checkbutton"));
277 rd.func_button[NORMAL] =
278 GTK_TOGGLE_BUTTON (get_widget_assert (xml, "normal-checkbutton"));
280 rd.formula_box = get_widget_assert (xml, "formula-frame");
282 rd.blom = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "blom-button"));
283 rd.tukey = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "tukey-button"));
284 rd.rankit = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "rankit-button"));
285 rd.vw = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "vw-button"));
288 rd.ties_dialog = PSPPIRE_DIALOG (get_widget_assert (xml, "ties-dialog"));
290 rd.mean = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "mean-button"));
291 rd.low = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "low-button"));
292 rd.high = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "high-button"));
293 rd.condense = GTK_TOGGLE_BUTTON (get_widget_assert (xml, "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), de->parent.window);
309 attach_dictionary_to_treeview (GTK_TREE_VIEW (vars),
311 GTK_SELECTION_MULTIPLE, NULL);
314 set_dest_model (GTK_TREE_VIEW (rd.rank_vars), vs->dict);
316 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector1),
319 insert_source_row_into_tree_view,
323 set_dest_model (GTK_TREE_VIEW (rd.group_vars), vs->dict);
325 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector2),
328 insert_source_row_into_tree_view,
333 g_signal_connect (types_button, "clicked",
334 G_CALLBACK (run_types_dialog), &rd);
336 g_signal_connect (ties_button, "clicked",
337 G_CALLBACK (run_ties_dialog), &rd);
339 g_signal_connect (rd.dialog, "refresh", G_CALLBACK (refresh), &rd);
341 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (rd.dialog),
342 dialog_state_valid, &rd);
344 response = psppire_dialog_run (PSPPIRE_DIALOG (rd.dialog));
349 case GTK_RESPONSE_OK:
351 gchar *syntax = generate_syntax (&rd);
352 struct getl_interface *sss = create_syntax_string_source (syntax);
353 execute_syntax (sss);
358 case PSPPIRE_RESPONSE_PASTE:
360 gchar *syntax = generate_syntax (&rd);
362 struct syntax_editor *se =
363 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
365 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
374 g_object_unref (xml);
379 types_dialog_reset (struct rank_dialog *rd)
383 for (i = 0 ; i < n_RANK_FUNCS ; ++i )
384 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (rd->func_button [i]),
387 gtk_widget_set_sensitive (rd->ntiles_entry, FALSE);
389 gtk_widget_set_sensitive (rd->formula_box, FALSE);
395 run_types_dialog (GtkButton *b, gpointer data)
397 struct rank_dialog *rd = data;
400 gtk_window_set_transient_for (GTK_WINDOW (rd->types_dialog),
401 GTK_WINDOW (rd->dialog));
403 types_dialog_reset (rd);
405 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->types_dialog));
409 run_ties_dialog (GtkButton *b, gpointer data)
411 struct rank_dialog *rd = data;
414 gtk_window_set_transient_for (GTK_WINDOW (rd->ties_dialog),
415 GTK_WINDOW (rd->dialog));
418 response = psppire_dialog_run (PSPPIRE_DIALOG (rd->ties_dialog));
423 on_ntiles_toggle (GtkToggleButton *toggle_button, gpointer data)
426 gboolean active = gtk_toggle_button_get_active (toggle_button);
427 gtk_widget_set_sensitive (w, active);