1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2008 Free Software Foundation, Inc.
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 "select-cases-dialog.h"
21 #include <glade/glade.h>
23 #include "psppire-dialog.h"
24 #include "data-editor.h"
25 #include "dialog-common.h"
26 #include "dict-display.h"
27 #include "widget-io.h"
28 #include <language/syntax-string-source.h>
29 #include "syntax-editor.h"
33 #define _(msgid) gettext (msgid)
34 #define N_(msgid) msgid
38 /* FIXME: These shouldn't be here */
39 #include "psppire-data-store.h"
42 struct select_cases_dialog
44 /* The XML that created the dialog */
47 GtkWidget *spinbutton ;
48 GtkWidget *spinbutton1 ;
49 GtkWidget *spinbutton2 ;
54 PsppireDataStore *data_store;
57 static gchar * generate_syntax (const struct select_cases_dialog *scd);
61 set_sensitivity_from_toggle (GtkToggleButton *togglebutton, gpointer data)
64 gboolean active = gtk_toggle_button_get_active (togglebutton);
66 gtk_widget_set_sensitive (w, active);
70 set_sensitivity_from_toggle_invert (GtkToggleButton *togglebutton,
74 gboolean active = gtk_toggle_button_get_active (togglebutton);
76 gtk_widget_set_sensitive (w, !active);
81 static const gchar label1[]=N_("Approximately %3d%% of all cases.");
82 static const gchar label2[]=N_("Exactly %3d cases from the first %3d cases.");
86 sample_subdialog (GtkButton *b, gpointer data)
89 struct select_cases_dialog *scd = data;
91 gint case_count = psppire_data_store_get_case_count (scd->data_store);
93 GtkWidget *parent_dialog = get_widget_assert (scd->xml,
94 "select-cases-dialog");
95 GtkWidget *dialog = get_widget_assert (scd->xml,
96 "select-cases-random-sample-dialog");
97 GtkWidget *percent = get_widget_assert (scd->xml,
98 "radiobutton-sample-percent");
99 GtkWidget *sample_n_cases = get_widget_assert (scd->xml,
100 "radiobutton-sample-n-cases");
101 GtkWidget *table = get_widget_assert (scd->xml,
102 "select-cases-random-sample-table");
106 scd->hbox1 = widget_scanf (gettext (label1), &scd->spinbutton);
108 gtk_widget_show (scd->hbox1);
110 gtk_table_attach_defaults (GTK_TABLE (table),
111 scd->hbox1, 1, 2, 0, 1);
113 g_signal_connect (G_OBJECT (percent), "toggled",
114 G_CALLBACK (set_sensitivity_from_toggle), scd->hbox1);
116 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (percent), TRUE);
123 widget_scanf (gettext (label2), &scd->spinbutton1, &scd->spinbutton2);
125 gtk_spin_button_set_range (GTK_SPIN_BUTTON (scd->spinbutton1),
128 gtk_spin_button_set_range (GTK_SPIN_BUTTON (scd->spinbutton2),
131 gtk_widget_show (scd->hbox2);
132 gtk_widget_set_sensitive (scd->hbox2, FALSE);
134 gtk_table_attach_defaults (GTK_TABLE (table),
135 scd->hbox2, 1, 2, 1, 2);
137 g_signal_connect (G_OBJECT (sample_n_cases), "toggled",
138 G_CALLBACK (set_sensitivity_from_toggle), scd->hbox2);
140 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (sample_n_cases), FALSE);
144 gtk_window_set_transient_for (GTK_WINDOW (dialog),
145 GTK_WINDOW (parent_dialog));
147 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
149 if ( response != PSPPIRE_RESPONSE_CONTINUE)
151 g_signal_handlers_disconnect_by_func
153 G_CALLBACK (set_sensitivity_from_toggle),
156 g_signal_handlers_disconnect_by_func
157 (G_OBJECT (sample_n_cases),
158 G_CALLBACK (set_sensitivity_from_toggle),
161 gtk_widget_destroy(scd->hbox1);
162 gtk_widget_destroy(scd->hbox2);
163 scd->hbox1 = scd->hbox2 = NULL;
168 GtkWidget *l0 = get_widget_assert (scd->xml, "random-sample-label");
170 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (percent)))
172 text = widget_printf (gettext(label1), scd->spinbutton);
173 gtk_label_set_text (GTK_LABEL (l0), text);
178 widget_printf (gettext(label2), scd->spinbutton1, scd->spinbutton2);
179 gtk_label_set_text (GTK_LABEL (l0), text);
188 range_subdialog (GtkButton *b, gpointer data)
191 struct select_cases_dialog *scd = data;
193 gint n_cases = psppire_data_store_get_case_count (scd->data_store);
195 GtkWidget *parent_dialog = get_widget_assert (scd->xml,
196 "select-cases-dialog");
198 GtkWidget *dialog = get_widget_assert (scd->xml,
199 "select-cases-range-dialog");
201 GtkWidget *first = get_widget_assert (scd->xml,
202 "range-dialog-first");
204 GtkWidget *last = get_widget_assert (scd->xml,
205 "range-dialog-last");
208 gtk_spin_button_set_range (GTK_SPIN_BUTTON (last), 1, n_cases);
210 gtk_spin_button_set_range (GTK_SPIN_BUTTON (first), 1, n_cases);
212 gtk_window_set_transient_for (GTK_WINDOW (dialog),
213 GTK_WINDOW (parent_dialog));
216 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
217 if ( response == PSPPIRE_RESPONSE_CONTINUE)
219 GtkWidget *first = get_widget_assert (scd->xml, "range-dialog-first");
220 GtkWidget *last = get_widget_assert (scd->xml, "range-dialog-last");
221 GtkWidget *l1 = get_widget_assert (scd->xml, "range-sample-label");
222 gchar *text = widget_printf (_("%d thru %d"), first, last);
224 gtk_label_set_text (GTK_LABEL (l1), text);
231 set_radiobutton (GtkWidget *button, gpointer data)
233 GtkToggleButton *toggle = data;
234 gtk_toggle_button_set_active (toggle, TRUE);
237 /* Pops up the Select Cases dialog box */
239 select_cases_dialog (GObject *o, gpointer data)
242 struct select_cases_dialog scd = {0,0,0,0,0,0};
244 struct data_editor *de = data;
245 GtkWidget *entry = NULL;
246 GtkWidget *selector ;
247 GtkWidget *button_range;
248 GtkWidget *button_sample;
250 scd.xml = XML_NEW ("psppire.glade");
252 g_object_get (de->data_editor, "data-store", &scd.data_store, NULL);
254 button_range = get_widget_assert (scd.xml, "button-range");
255 button_sample = get_widget_assert (scd.xml, "button-sample");
256 entry = get_widget_assert (scd.xml, "filter-variable-entry");
257 selector = get_widget_assert (scd.xml, "psppire-selector-filter");
260 GtkWidget *button_if =
261 get_widget_assert (scd.xml, "button-if");
263 GtkWidget *radiobutton_if =
264 get_widget_assert (scd.xml, "radiobutton-if");
266 GtkWidget *radiobutton_all =
267 get_widget_assert (scd.xml, "radiobutton-all");
269 GtkWidget *radiobutton_sample =
270 get_widget_assert (scd.xml, "radiobutton-sample");
272 GtkWidget *radiobutton_range =
273 get_widget_assert (scd.xml, "radiobutton-range");
275 GtkWidget *radiobutton_filter =
276 get_widget_assert (scd.xml, "radiobutton-filter-variable");
278 GtkWidget *range_label =
279 get_widget_assert (scd.xml, "range-sample-label");
281 GtkWidget *sample_label =
282 get_widget_assert (scd.xml, "random-sample-label");
284 g_signal_connect (radiobutton_all, "toggled",
285 G_CALLBACK (set_sensitivity_from_toggle_invert),
286 get_widget_assert (scd.xml, "filter-delete-button-box")
289 g_signal_connect (button_if, "clicked",
290 G_CALLBACK (set_radiobutton), radiobutton_if);
292 g_signal_connect (button_sample, "clicked",
293 G_CALLBACK (set_radiobutton), radiobutton_sample);
295 g_signal_connect (button_range, "clicked",
296 G_CALLBACK (set_radiobutton), radiobutton_range);
298 g_signal_connect (selector, "clicked",
299 G_CALLBACK (set_radiobutton), radiobutton_filter);
301 g_signal_connect (selector, "selected",
302 G_CALLBACK (set_radiobutton), radiobutton_filter);
304 g_signal_connect (radiobutton_range, "toggled",
305 G_CALLBACK (set_sensitivity_from_toggle),
309 g_signal_connect (radiobutton_sample, "toggled",
310 G_CALLBACK (set_sensitivity_from_toggle),
314 g_signal_connect (radiobutton_filter, "toggled",
315 G_CALLBACK (set_sensitivity_from_toggle),
322 dialog = get_widget_assert (scd.xml, "select-cases-dialog");
323 gtk_window_set_transient_for (GTK_WINDOW (dialog), de->parent.window);
326 GtkWidget *source = get_widget_assert (scd.xml, "select-cases-treeview");
328 attach_dictionary_to_treeview (GTK_TREE_VIEW (source),
329 scd.data_store->dict,
330 GTK_SELECTION_SINGLE, NULL);
332 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
335 insert_source_row_into_entry,
336 is_currently_in_entry,
342 g_signal_connect (button_range,
343 "clicked", G_CALLBACK (range_subdialog), &scd);
346 g_signal_connect (button_sample,
347 "clicked", G_CALLBACK (sample_subdialog), &scd);
350 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
354 case GTK_RESPONSE_OK:
356 gchar *syntax = generate_syntax (&scd);
357 struct getl_interface *sss = create_syntax_string_source (syntax);
358 execute_syntax (sss);
363 case PSPPIRE_RESPONSE_PASTE:
365 gchar *syntax = generate_syntax (&scd);
367 struct syntax_editor *se =
368 (struct syntax_editor *) window_create (WINDOW_SYNTAX, NULL);
370 gtk_text_buffer_insert_at_cursor (se->buffer, syntax, -1);
379 g_object_unref (scd.xml);
384 generate_syntax (const struct select_cases_dialog *scd)
387 GString *string = NULL;
389 if ( gtk_toggle_button_get_active
390 (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
391 "radiobutton-all"))))
393 return strdup ("\n");
396 string = g_string_new ("");
398 if ( gtk_toggle_button_get_active
399 (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
400 "radiobutton-sample"))))
402 GtkWidget *random_sample =
403 get_widget_assert (scd->xml,
404 "radiobutton-sample-percent");
406 g_string_append (string, "SAMPLE ");
408 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (random_sample)))
410 const double percentage =
411 gtk_spin_button_get_value (GTK_SPIN_BUTTON (scd->spinbutton));
412 g_string_append_printf (string, "%g.", percentage / 100.0);
417 gtk_spin_button_get_value (GTK_SPIN_BUTTON (scd->spinbutton1));
418 const gint from_n_cases =
419 gtk_spin_button_get_value (GTK_SPIN_BUTTON (scd->spinbutton2));
421 g_string_append_printf (string, "%d FROM %d .", n_cases, from_n_cases);
425 else if ( gtk_toggle_button_get_active
426 (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
427 "radiobutton-range"))))
429 GtkSpinButton *first =
430 GTK_SPIN_BUTTON (get_widget_assert (scd->xml,
431 "range-dialog-first"));
433 GtkSpinButton *last =
434 GTK_SPIN_BUTTON (get_widget_assert (scd->xml,
435 "range-dialog-last"));
437 g_string_append_printf (string,
438 "COMPUTE filter_$ = ($CASENUM >= %ld "
439 "AND $CASENUM <= %ld).\n",
440 (long) gtk_spin_button_get_value (first),
441 (long) gtk_spin_button_get_value (last)
443 g_string_append (string, "EXECUTE.\n");
444 g_string_append_printf (string, "SELECT IF filter_$.\n");
447 else if ( gtk_toggle_button_get_active
449 (get_widget_assert (scd->xml,
450 "radiobutton-filter-variable"))))
453 GTK_ENTRY (get_widget_assert (scd->xml,
454 "filter-variable-entry"));
456 g_string_append_printf (string, "SELECT IF (%s <> 0).",
457 gtk_entry_get_text (entry));
461 g_string_append (string, "\n");
464 /* Are we filtering or deleting ? */
465 if ( gtk_toggle_button_get_active
466 (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
467 "radiobutton-delete"))))
469 g_string_append (string, "EXECUTE.\n");
471 if ( gtk_toggle_button_get_active
472 (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
473 "radiobutton-range"))))
475 g_string_append (string, "DELETE VARIABLES filter_$.\n");
481 g_string_free (string, FALSE);