Replaced the glade definition of about dialog with a C one.
[pspp] / src / ui / gui / select-cases-dialog.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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 #include <config.h>
18
19 #include "select-cases-dialog.h"
20 #include <gtk/gtk.h>
21 #include "executor.h"
22 #include "psppire-dialog.h"
23 #include "psppire-data-window.h"
24 #include "psppire-selector.h"
25 #include "dict-display.h"
26 #include "dialog-common.h"
27 #include "widget-io.h"
28 #include <language/syntax-string-source.h>
29 #include "helper.h"
30 #include <xalloc.h>
31
32
33 #include <gettext.h>
34 #define _(msgid) gettext (msgid)
35 #define N_(msgid) msgid
36
37
38
39 /* FIXME: These shouldn't be here */
40 #include "psppire-data-store.h"
41
42
43 struct select_cases_dialog
44 {
45   /* The XML that created the dialog */
46   GtkBuilder *xml;
47
48   GtkWidget *spinbutton ;
49   GtkWidget *spinbutton1 ;
50   GtkWidget *spinbutton2 ;
51
52   GtkWidget *hbox1;
53   GtkWidget *hbox2;
54
55   PsppireDataStore *data_store;
56 };
57
58 static gchar * generate_syntax (const struct select_cases_dialog *scd);
59
60
61 static void
62 set_sensitivity_from_toggle (GtkToggleButton *togglebutton,  gpointer data)
63 {
64   GtkWidget *w = data;
65   gboolean active = gtk_toggle_button_get_active (togglebutton);
66
67   gtk_widget_set_sensitive (w, active);
68 }
69
70 static void
71 set_sensitivity_from_toggle_invert (GtkToggleButton *togglebutton,
72                                     gpointer data)
73 {
74   GtkWidget *w = data;
75   gboolean active = gtk_toggle_button_get_active (togglebutton);
76
77   gtk_widget_set_sensitive (w, !active);
78 }
79
80
81
82 static const gchar label1[]=N_("Approximately %3d%% of all cases.");
83 static const gchar label2[]=N_("Exactly %3d cases from the first %3d cases.");
84
85
86 static void
87 sample_subdialog (GtkButton *b, gpointer data)
88 {
89   gint response;
90   struct select_cases_dialog *scd = data;
91
92   gint case_count = psppire_data_store_get_case_count (scd->data_store);
93
94   GtkWidget *parent_dialog = get_widget_assert (scd->xml,
95                                                 "select-cases-dialog");
96   GtkWidget *dialog = get_widget_assert (scd->xml,
97                                          "select-cases-random-sample-dialog");
98   GtkWidget *percent = get_widget_assert (scd->xml,
99                                           "radiobutton-sample-percent");
100   GtkWidget *sample_n_cases = get_widget_assert (scd->xml,
101                                                  "radiobutton-sample-n-cases");
102   GtkWidget *table = get_widget_assert (scd->xml,
103                                         "select-cases-random-sample-table");
104
105   if ( ! scd->hbox1 )
106     {
107       scd->hbox1 = widget_scanf (gettext (label1), &scd->spinbutton);
108
109       gtk_widget_show (scd->hbox1);
110
111       gtk_table_attach_defaults (GTK_TABLE (table),
112                                  scd->hbox1, 1, 2, 0, 1);
113
114       g_signal_connect (percent, "toggled",
115                         G_CALLBACK (set_sensitivity_from_toggle), scd->hbox1);
116
117       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (percent), TRUE);
118     }
119
120
121   if ( ! scd->hbox2 )
122     {
123       scd->hbox2 =
124         widget_scanf (gettext (label2), &scd->spinbutton1, &scd->spinbutton2);
125
126       gtk_spin_button_set_range (GTK_SPIN_BUTTON (scd->spinbutton1),
127                                  1, case_count);
128
129       gtk_spin_button_set_range (GTK_SPIN_BUTTON (scd->spinbutton2),
130                                  1, case_count);
131
132       gtk_widget_show (scd->hbox2);
133       gtk_widget_set_sensitive (scd->hbox2, FALSE);
134
135       gtk_table_attach_defaults (GTK_TABLE (table),
136                                  scd->hbox2, 1, 2, 1, 2);
137
138       g_signal_connect (sample_n_cases, "toggled",
139                         G_CALLBACK (set_sensitivity_from_toggle), scd->hbox2);
140
141       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (sample_n_cases), FALSE);
142     }
143
144
145   gtk_window_set_transient_for (GTK_WINDOW (dialog),
146                                 GTK_WINDOW (parent_dialog));
147
148   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
149
150   if ( response != PSPPIRE_RESPONSE_CONTINUE)
151     {
152       g_signal_handlers_disconnect_by_func
153         (G_OBJECT (percent),
154          G_CALLBACK (set_sensitivity_from_toggle),
155          scd->hbox1);
156
157       g_signal_handlers_disconnect_by_func
158         (G_OBJECT (sample_n_cases),
159          G_CALLBACK (set_sensitivity_from_toggle),
160          scd->hbox2);
161
162       gtk_widget_destroy(scd->hbox1);
163       gtk_widget_destroy(scd->hbox2);
164       scd->hbox1 = scd->hbox2 = NULL;
165     }
166   else
167     {
168       gchar *text;
169       GtkWidget *l0 = get_widget_assert (scd->xml, "random-sample-label");
170
171       if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (percent)))
172         {
173           text = widget_printf (gettext(label1), scd->spinbutton);
174           gtk_label_set_text (GTK_LABEL (l0), text);
175         }
176       else
177         {
178           text =
179             widget_printf (gettext(label2), scd->spinbutton1, scd->spinbutton2);
180           gtk_label_set_text (GTK_LABEL (l0), text);
181
182         }
183       g_free (text);
184
185     }
186 }
187
188 static void
189 range_subdialog (GtkButton *b, gpointer data)
190 {
191   gint response;
192   struct select_cases_dialog *scd = data;
193
194   gint n_cases = psppire_data_store_get_case_count (scd->data_store);
195
196   GtkWidget *parent_dialog = get_widget_assert (scd->xml,
197                                                 "select-cases-dialog");
198
199   GtkWidget *dialog = get_widget_assert (scd->xml,
200                                          "select-cases-range-dialog");
201
202   GtkWidget *first = get_widget_assert (scd->xml,
203                                         "range-dialog-first");
204
205   GtkWidget *last = get_widget_assert (scd->xml,
206                                         "range-dialog-last");
207
208
209   gtk_spin_button_set_range (GTK_SPIN_BUTTON (last), 1, n_cases);
210
211   gtk_spin_button_set_range (GTK_SPIN_BUTTON (first), 1, n_cases);
212
213   gtk_window_set_transient_for (GTK_WINDOW (dialog),
214                                 GTK_WINDOW (parent_dialog));
215
216
217   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
218   if ( response == PSPPIRE_RESPONSE_CONTINUE)
219     {
220       GtkWidget *first = get_widget_assert (scd->xml, "range-dialog-first");
221       GtkWidget *last = get_widget_assert (scd->xml, "range-dialog-last");
222       GtkWidget *l1 = get_widget_assert (scd->xml, "range-sample-label");
223       gchar *text = widget_printf (_("%d thru %d"), first, last);
224
225       gtk_label_set_text (GTK_LABEL (l1), text);
226
227       g_free (text);
228     }
229 }
230
231 static void
232 set_radiobutton (GtkWidget *button, gpointer data)
233 {
234   GtkToggleButton *toggle = data;
235   gtk_toggle_button_set_active (toggle, TRUE);
236 }
237
238 /* Pops up the Select Cases dialog box */
239 void
240 select_cases_dialog (GObject *o, gpointer data)
241 {
242   gint response;
243   struct select_cases_dialog scd = {0,0,0,0,0,0};
244   GtkWidget *dialog   ;
245   PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
246   GtkWidget *entry = NULL;
247   GtkWidget *selector ;
248   GtkWidget *button_range;
249   GtkWidget *button_sample;
250
251   scd.xml = builder_new ("psppire.ui");
252
253   g_object_get (de->data_editor, "data-store", &scd.data_store, NULL);
254
255   button_range = get_widget_assert (scd.xml, "button-range");
256   button_sample = get_widget_assert (scd.xml, "button-sample");
257   entry = get_widget_assert (scd.xml, "filter-variable-entry");
258   selector = get_widget_assert (scd.xml, "psppire-selector-filter");
259
260   {
261     GtkWidget *button_if =
262       get_widget_assert (scd.xml, "button-if");
263
264     GtkWidget *radiobutton_if =
265       get_widget_assert (scd.xml, "radiobutton-if");
266
267     GtkWidget *radiobutton_all =
268       get_widget_assert (scd.xml, "radiobutton-all");
269
270     GtkWidget *radiobutton_sample =
271       get_widget_assert (scd.xml, "radiobutton-sample");
272
273     GtkWidget *radiobutton_range =
274       get_widget_assert (scd.xml, "radiobutton-range");
275
276     GtkWidget *radiobutton_filter =
277       get_widget_assert (scd.xml, "radiobutton-filter-variable");
278
279     GtkWidget *range_label =
280       get_widget_assert (scd.xml, "range-sample-label");
281
282     GtkWidget *sample_label =
283       get_widget_assert (scd.xml, "random-sample-label");
284
285     g_signal_connect (radiobutton_all, "toggled",
286                       G_CALLBACK (set_sensitivity_from_toggle_invert),
287                       get_widget_assert (scd.xml, "filter-delete-button-box")
288                       );
289
290     g_signal_connect (button_if, "clicked",
291                       G_CALLBACK (set_radiobutton), radiobutton_if);
292
293     g_signal_connect (button_sample, "clicked",
294                       G_CALLBACK (set_radiobutton), radiobutton_sample);
295
296     g_signal_connect (button_range,  "clicked",
297                       G_CALLBACK (set_radiobutton), radiobutton_range);
298
299     g_signal_connect (selector, "clicked",
300                       G_CALLBACK (set_radiobutton), radiobutton_filter);
301
302     g_signal_connect (selector, "selected",
303                       G_CALLBACK (set_radiobutton), radiobutton_filter);
304
305     g_signal_connect (radiobutton_range, "toggled",
306                       G_CALLBACK (set_sensitivity_from_toggle),
307                       range_label
308                       );
309
310     g_signal_connect (radiobutton_sample, "toggled",
311                       G_CALLBACK (set_sensitivity_from_toggle),
312                       sample_label
313                       );
314
315     g_signal_connect (radiobutton_filter, "toggled",
316                       G_CALLBACK (set_sensitivity_from_toggle),
317                       entry
318                       );
319   }
320
321
322
323   dialog = get_widget_assert (scd.xml, "select-cases-dialog");
324   gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (de));
325
326   {
327     GtkWidget *source = get_widget_assert   (scd.xml, "select-cases-treeview");
328
329     g_object_set (source, "dictionary",
330                   scd.data_store->dict,
331                   "selection-mode",
332                   GTK_SELECTION_SINGLE, NULL);
333
334     psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
335                                    source,
336                                    entry,
337                                    insert_source_row_into_entry,
338                                    is_currently_in_entry,
339                                    NULL);
340   }
341
342
343
344   g_signal_connect (button_range,
345                     "clicked", G_CALLBACK (range_subdialog), &scd);
346
347
348   g_signal_connect (button_sample,
349                     "clicked", G_CALLBACK (sample_subdialog), &scd);
350
351
352   response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
353
354   switch (response)
355     {
356     case GTK_RESPONSE_OK:
357       {
358         gchar *syntax = generate_syntax (&scd);
359
360         struct getl_interface *sss = create_syntax_string_source (syntax);
361         execute_syntax (sss);
362
363         g_free (syntax);
364       }
365       break;
366     case PSPPIRE_RESPONSE_PASTE:
367       {
368         gchar *syntax = generate_syntax (&scd);
369         paste_syntax_in_new_window (syntax);
370
371         g_free (syntax);
372       }
373       break;
374     default:
375       break;
376     }
377
378   g_object_unref (scd.xml);
379 }
380
381
382 static gchar *
383 generate_syntax (const struct select_cases_dialog *scd)
384 {
385   gchar *text = NULL;
386   GString *string = NULL;
387
388   if ( gtk_toggle_button_get_active
389        (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
390                                               "radiobutton-all"))))
391     {
392       return xstrdup ("\n");
393     }
394
395   string = g_string_new ("");
396
397   if ( gtk_toggle_button_get_active
398        (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
399                                               "radiobutton-sample"))))
400   {
401     GtkWidget *random_sample =
402       get_widget_assert (scd->xml,
403                          "radiobutton-sample-percent");
404
405     g_string_append (string, "SAMPLE ");
406
407     if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (random_sample)))
408       {
409         const double percentage =
410           gtk_spin_button_get_value (GTK_SPIN_BUTTON (scd->spinbutton));
411         g_string_append_printf (string, "%g.", percentage / 100.0);
412       }
413     else
414       {
415         const gint n_cases =
416           gtk_spin_button_get_value (GTK_SPIN_BUTTON (scd->spinbutton1));
417         const gint from_n_cases =
418           gtk_spin_button_get_value (GTK_SPIN_BUTTON (scd->spinbutton2));
419
420         g_string_append_printf (string, "%d FROM %d .", n_cases, from_n_cases);
421       }
422
423   }
424   else if ( gtk_toggle_button_get_active
425             (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
426                                                    "radiobutton-range"))))
427     {
428       GtkSpinButton *first =
429         GTK_SPIN_BUTTON (get_widget_assert (scd->xml,
430                                            "range-dialog-first"));
431
432       GtkSpinButton *last =
433         GTK_SPIN_BUTTON (get_widget_assert (scd->xml,
434                                            "range-dialog-last"));
435
436       g_string_append_printf (string,
437                               "COMPUTE filter_$ = ($CASENUM >= %ld "
438                                "AND $CASENUM <= %ld).\n",
439                               (long) gtk_spin_button_get_value (first),
440                               (long) gtk_spin_button_get_value (last)
441                               );
442       g_string_append (string, "EXECUTE.\n");
443       g_string_append_printf (string, "SELECT IF filter_$.\n");
444
445     }
446   else if ( gtk_toggle_button_get_active
447             (GTK_TOGGLE_BUTTON
448              (get_widget_assert (scd->xml,
449                                  "radiobutton-filter-variable"))))
450     {
451       GtkEntry *entry =
452         GTK_ENTRY (get_widget_assert (scd->xml,
453                                       "filter-variable-entry"));
454
455       g_string_append_printf (string, "SELECT IF (%s <> 0).",
456                               gtk_entry_get_text (entry));
457     }
458
459
460   g_string_append (string, "\n");
461
462
463   /* Are we filtering or deleting ? */
464   if ( gtk_toggle_button_get_active
465        (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
466                                               "radiobutton-delete"))))
467     {
468       g_string_append (string, "EXECUTE.\n");
469
470       if ( gtk_toggle_button_get_active
471            (GTK_TOGGLE_BUTTON (get_widget_assert (scd->xml,
472                                                   "radiobutton-range"))))
473         {
474           g_string_append (string, "DELETE VARIABLES filter_$.\n");
475         }
476     }
477
478
479   text  = string->str;
480   g_string_free (string, FALSE);
481   return text;
482 }