1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2010, 2011, 2012, 2013 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 "dialog-common.h"
21 #include <gl/c-xvasprintf.h>
22 #include <language/stats/aggregate.h>
24 #include <ui/syntax-gen.h>
25 #include <libpspp/str.h>
27 #include "aggregate-dialog.h"
28 #include "psppire-selector.h"
29 #include "psppire-dictview.h"
30 #include "psppire-dialog.h"
32 #include "psppire-data-window.h"
33 #include "psppire-var-view.h"
34 #include "psppire-acr.h"
36 #include "dict-display.h"
39 #include "builder-wrapper.h"
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
51 COMBO_MODEL_COL_DESC = 0,
52 COMBO_MODEL_COL_SYNTAX,
53 COMBO_MODEL_COL_SRC_VARS,
61 PsppireDataWindow *de ;
63 GtkWidget *break_variables;
65 GtkWidget *replace_radiobutton;
66 GtkWidget *add_radiobutton;
67 GtkWidget *filename_radiobutton;
68 GtkWidget *filename_button;
69 GtkWidget *filename_box;
70 GtkWidget *filename_label;
72 GtkWidget *function_combo;
74 GtkWidget *summary_acr;
75 GtkWidget *summary_var_name_entry;
76 GtkWidget *summary_var_label_entry;
78 GtkWidget *summary_sv;
79 GtkWidget *summary_sv_entry;
81 GtkWidget *summary_arg1;
82 GtkWidget *summary_arg2;
84 GtkWidget *summary_arg1_entry;
85 GtkWidget *summary_arg2_entry;
87 GtkWidget *sorted_button;
88 GtkWidget *needs_sort_button;
94 static char * generate_syntax (const struct aggregate *rd);
97 static void update_arguments (struct aggregate *agg);
101 refresh (struct aggregate *agg)
103 GtkTreeModel *liststore =
104 gtk_tree_view_get_model (GTK_TREE_VIEW (agg->break_variables));
105 gtk_list_store_clear (GTK_LIST_STORE (liststore));
107 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (agg->add_radiobutton), TRUE);
108 gtk_label_set_text (GTK_LABEL (agg->filename_label), "");
111 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (agg->needs_sort_button), TRUE);
113 gtk_entry_set_text (GTK_ENTRY (agg->summary_sv_entry), "");
114 gtk_entry_set_text (GTK_ENTRY (agg->summary_arg1_entry), "");
115 gtk_entry_set_text (GTK_ENTRY (agg->summary_arg2_entry), "");
116 gtk_entry_set_text (GTK_ENTRY (agg->summary_var_label_entry), "");
117 gtk_entry_set_text (GTK_ENTRY (agg->summary_var_name_entry), "N_BREAK");
118 gtk_editable_select_region (GTK_EDITABLE (agg->summary_var_name_entry), 0, -1);
120 gtk_combo_box_set_active (GTK_COMBO_BOX (agg->function_combo), N);
122 gtk_list_store_clear (PSPPIRE_ACR (agg->summary_acr)->list_store);
124 update_arguments (agg);
129 dialog_state_valid (gpointer data)
132 const struct aggregate *agg = data;
134 GtkTreeModel *liststore =
135 gtk_tree_view_get_model (GTK_TREE_VIEW (agg->break_variables));
137 if ( ! gtk_tree_model_get_iter_first (liststore, &iter))
140 liststore = GTK_TREE_MODEL (PSPPIRE_ACR (agg->summary_acr)->list_store);
142 if ( ! gtk_tree_model_get_iter_first (liststore, &iter))
150 choose_filename (struct aggregate *fd)
152 GtkFileFilter *filter;
154 GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Aggregate destination file"),
156 GTK_FILE_CHOOSER_ACTION_SAVE,
157 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
158 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
160 g_object_set (dialog, "local-only", FALSE, NULL);
162 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
165 filter = gtk_file_filter_new ();
166 gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
167 gtk_file_filter_add_mime_type (filter, "application/x-spss-sav");
168 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
170 filter = gtk_file_filter_new ();
171 gtk_file_filter_set_name (filter, _("Compressed System Files (*.zsav)"));
172 gtk_file_filter_add_pattern (filter, "*.zsav");
173 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
175 filter = gtk_file_filter_new ();
176 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
177 gtk_file_filter_add_mime_type (filter, "application/x-spss-por");
178 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
181 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
185 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
187 gtk_label_set_text (GTK_LABEL (fd->filename_label), filename);
193 gtk_widget_destroy (dialog);
198 populate_combo_model (GtkComboBox *cb)
200 GtkListStore *list = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
202 const struct agr_func *af = agr_func_tab;
203 GtkCellRenderer *renderer ;
205 for (af = agr_func_tab; af->name; ++af)
207 const gchar *s = af->description;
211 gtk_list_store_append (list, &iter);
212 gtk_list_store_set (list, &iter,
213 COMBO_MODEL_COL_DESC, gettext (s),
214 COMBO_MODEL_COL_SYNTAX, af->name,
215 COMBO_MODEL_COL_SRC_VARS, af->src_vars,
216 COMBO_MODEL_COL_ARITY, af->n_args,
220 renderer = gtk_cell_renderer_text_new ();
221 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cb), renderer, FALSE);
223 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cb), renderer, "text", 0);
225 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
226 g_object_unref (list);
231 /* Returns TRUE iff all the necessary controls have been set to
232 completely specify a summary function */
234 summary_complete (const struct aggregate *agg)
238 enum agr_src_vars src_vars;
240 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (agg->function_combo));
242 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (agg->summary_var_name_entry))))
245 ok = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (agg->function_combo), &iter);
252 gtk_tree_model_get (model,
254 COMBO_MODEL_COL_ARITY, &n_args,
255 COMBO_MODEL_COL_SRC_VARS, &src_vars,
258 if ( src_vars == AGR_SV_YES )
260 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (agg->summary_sv_entry))))
266 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (agg->summary_arg2_entry))))
272 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (agg->summary_arg1_entry))))
282 append_summary_spec (const struct aggregate *agg, GtkTreeIter *iter, GString *string);
286 render_summary (GtkTreeViewColumn *tree_column,
287 GtkCellRenderer *cell,
288 GtkTreeModel *tree_model,
292 struct aggregate *agg = data;
294 GString *string = g_string_new ("");
296 append_summary_spec (agg, iter, string);
299 g_object_set (cell, "text", string->str, NULL);
301 g_string_free (string, TRUE);
305 /* Enable/Disable the summary variable ACR */
307 update_acr (struct aggregate *agg)
309 gboolean ready = summary_complete (agg);
311 psppire_acr_set_enabled (PSPPIRE_ACR (agg->summary_acr), ready);
315 /* Update the sensitivity of the summary variable argument fields */
317 update_arguments (struct aggregate *agg)
321 gboolean ok = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (agg->function_combo), &iter);
325 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (agg->function_combo));
327 enum agr_src_vars src_vars;
328 gtk_tree_model_get (model,
330 COMBO_MODEL_COL_ARITY, &n_args,
331 COMBO_MODEL_COL_SRC_VARS, &src_vars,
334 gtk_widget_set_sensitive (agg->summary_sv, src_vars != AGR_SV_NO);
335 gtk_widget_set_sensitive (agg->summary_arg2, n_args >= 2);
336 gtk_widget_set_sensitive (agg->summary_arg1, n_args >= 1);
340 gtk_widget_set_sensitive (agg->summary_sv, FALSE);
341 gtk_widget_set_sensitive (agg->summary_arg2, FALSE);
342 gtk_widget_set_sensitive (agg->summary_arg1, FALSE);
348 SUMMARY_COL_VARNAME = 0,
349 SUMMARY_COL_VARLABEL,
356 /* Set VAL to the value appropriate for COL according to the
357 current state of the dialog */
359 get_summary_spec (gint col, GValue *val, gpointer data)
361 const struct aggregate *agg = data;
364 case SUMMARY_COL_VARNAME:
365 g_value_init (val, G_TYPE_STRING);
366 g_value_set_string (val, gtk_entry_get_text (GTK_ENTRY (agg->summary_var_name_entry)));
368 case SUMMARY_COL_VARLABEL:
369 g_value_init (val, G_TYPE_STRING);
370 g_value_set_string (val, gtk_entry_get_text (GTK_ENTRY (agg->summary_var_label_entry)));
372 case SUMMARY_COL_SRCVAR:
373 g_value_init (val, G_TYPE_STRING);
374 g_value_set_string (val, gtk_entry_get_text (GTK_ENTRY (agg->summary_sv_entry)));
376 case SUMMARY_COL_FUNCIDX:
377 g_value_init (val, G_TYPE_INT);
378 g_value_set_int (val, gtk_combo_box_get_active (GTK_COMBO_BOX (agg->function_combo)));
380 case SUMMARY_COL_ARG1:
382 const gchar *text = gtk_entry_get_text (GTK_ENTRY (agg->summary_arg1_entry));
383 g_value_init (val, G_TYPE_DOUBLE);
384 g_value_set_double (val, g_strtod (text, 0));
387 case SUMMARY_COL_ARG2:
389 const gchar *text = gtk_entry_get_text (GTK_ENTRY (agg->summary_arg2_entry));
390 g_value_init (val, G_TYPE_DOUBLE);
391 g_value_set_double (val, g_strtod (text, 0));
395 g_assert_not_reached ();
403 /* Update the status of the dialog box according to what row of the ACR's treeview
406 on_acr_change (const struct aggregate *agg, GtkTreeView *tv)
408 const gchar *varname;
416 GtkTreePath *path = NULL;
417 GtkTreeModel *model = gtk_tree_view_get_model (tv);
418 gtk_tree_view_get_cursor (tv, &path, NULL);
420 gtk_tree_model_get_iter (model, &iter, path);
422 gtk_tree_model_get (model, &iter,
423 SUMMARY_COL_VARNAME, &varname,
424 SUMMARY_COL_VARLABEL, &label,
425 SUMMARY_COL_FUNCIDX, &f_idx,
426 SUMMARY_COL_SRCVAR, &srcvar,
427 SUMMARY_COL_ARG1, &arg1,
428 SUMMARY_COL_ARG2, &arg2,
431 gtk_entry_set_text (GTK_ENTRY (agg->summary_var_name_entry), varname);
432 gtk_entry_set_text (GTK_ENTRY (agg->summary_var_label_entry), label);
433 gtk_entry_set_text (GTK_ENTRY (agg->summary_sv_entry), srcvar);
435 text = c_xasprintf ("%g", arg1);
436 gtk_entry_set_text (GTK_ENTRY (agg->summary_arg1_entry), text);
439 text = c_xasprintf ("%g", arg2);
440 gtk_entry_set_text (GTK_ENTRY (agg->summary_arg2_entry), text);
443 gtk_combo_box_set_active (GTK_COMBO_BOX (agg->function_combo), f_idx);
447 /* Set the pane to 50% of its maximum size */
449 set_initial_pos (GtkPaned *pane)
453 "max-position", &max_pos,
456 gtk_paned_set_position (pane, max_pos);
461 /* Pops up the Aggregate dialog box */
463 aggregate_dialog (PsppireDataWindow *dw)
471 GtkWidget *break_selector ;
473 fd.xml = builder_new ("aggregate.ui");
477 dialog = get_widget_assert (fd.xml, "psppire-dialog1");
478 source = get_widget_assert (fd.xml, "dict-view");
479 break_selector = get_widget_assert (fd.xml, "break-selector");
481 fd.pane = get_widget_assert (fd.xml, "hbox1");
483 fd.break_variables = get_widget_assert (fd.xml, "psppire-var-view1");
484 fd.filename_radiobutton = get_widget_assert (fd.xml, "filename-radiobutton");
485 fd.filename_button = get_widget_assert (fd.xml, "filename-button");
486 fd.filename_box = get_widget_assert (fd.xml, "filename-box");
487 fd.filename_label = get_widget_assert (fd.xml, "filename-label");
488 fd.replace_radiobutton = get_widget_assert (fd.xml, "replace-radiobutton");
489 fd.add_radiobutton = get_widget_assert (fd.xml, "add-radiobutton");
490 fd.function_combo = get_widget_assert (fd.xml, "function-combo");
492 fd.summary_acr = get_widget_assert (fd.xml, "psppire-acr1");
493 fd.summary_var_name_entry = get_widget_assert (fd.xml, "summary-var-name-entry");
495 fd.summary_arg1 = get_widget_assert (fd.xml, "summary-arg1");
496 fd.summary_arg2 = get_widget_assert (fd.xml, "summary-arg2");
498 fd.summary_arg1_entry = get_widget_assert (fd.xml, "summary-arg-entry1");
499 fd.summary_arg2_entry = get_widget_assert (fd.xml, "summary-arg-entry2");
501 fd.summary_var_label_entry = get_widget_assert (fd.xml, "summary-var-label-entry");
503 fd.summary_sv = get_widget_assert (fd.xml, "source-var");
504 fd.summary_sv_entry = get_widget_assert (fd.xml, "source-var-entry");
506 fd.sorted_button = get_widget_assert (fd.xml, "sorted-radiobutton");
507 fd.needs_sort_button = get_widget_assert (fd.xml, "needs-sort-radiobutton");
510 GtkTreeViewColumn *column ;
514 GtkCellRenderer *cell_renderer ;
516 GtkListStore *list = gtk_list_store_new (6,
524 psppire_acr_set_model (PSPPIRE_ACR (fd.summary_acr), list);
525 g_object_unref (list);
527 psppire_acr_set_get_value_func (PSPPIRE_ACR (fd.summary_acr),
528 get_summary_spec, &fd);
530 column = gtk_tree_view_get_column (PSPPIRE_ACR (fd.summary_acr)->tv, 0);
532 l = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
534 cell_renderer = l->data;
536 gtk_tree_view_column_set_cell_data_func (column,
542 g_signal_connect_swapped (PSPPIRE_ACR (fd.summary_acr)->tv,
543 "cursor-changed", G_CALLBACK (on_acr_change), &fd);
546 g_signal_connect_swapped (fd.summary_var_name_entry, "changed", G_CALLBACK (update_acr), &fd);
547 g_signal_connect_swapped (fd.function_combo, "changed", G_CALLBACK (update_acr), &fd);
548 g_signal_connect_swapped (fd.summary_sv_entry, "changed", G_CALLBACK (update_acr), &fd);
549 g_signal_connect_swapped (fd.summary_arg1_entry, "changed", G_CALLBACK (update_acr), &fd);
550 g_signal_connect_swapped (fd.summary_arg2_entry, "changed", G_CALLBACK (update_acr), &fd);
553 g_signal_connect (fd.pane, "realize", G_CALLBACK (set_initial_pos), &fd);
556 g_signal_connect_swapped (fd.function_combo, "changed",
557 G_CALLBACK (update_arguments), &fd);
559 populate_combo_model (GTK_COMBO_BOX (fd.function_combo));
561 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &fd);
563 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
565 g_object_get (fd.de->data_editor, "dictionary", &fd.dict, NULL);
566 g_object_set (source, "model", fd.dict, NULL);
569 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (break_selector), NULL);
572 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
573 dialog_state_valid, &fd);
575 g_signal_connect (fd.filename_radiobutton, "toggled",
576 G_CALLBACK (set_sensitivity_from_toggle), fd.filename_box );
578 g_signal_connect_swapped (fd.filename_button, "clicked",
579 G_CALLBACK (choose_filename), &fd);
582 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
586 case GTK_RESPONSE_OK:
587 g_free (execute_syntax_string (dw, generate_syntax (&fd)));
589 case PSPPIRE_RESPONSE_PASTE:
590 g_free (paste_syntax_to_window (generate_syntax (&fd)));
596 g_object_unref (fd.xml);
603 append_destination_filename (const struct aggregate *agg, GString *gs)
605 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (agg->filename_radiobutton)))
608 const gchar *s = gtk_label_get_text (GTK_LABEL (agg->filename_label));
610 syntax_gen_string (&ss, ss_cstr (s));
611 g_string_append (gs, ds_cstr (&ss));
616 g_string_append (gs, "* ");
618 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (agg->replace_radiobutton)))
619 g_string_append (gs, "MODE=REPLACE");
621 g_string_append (gs, "MODE=ADDVARIABLES");
625 /* Append the syntax of the summary function pointed to by ITER to STRING */
627 append_summary_spec (const struct aggregate *agg, GtkTreeIter *iter, GString *string)
629 GtkTreeIter combo_iter;
630 char *varname = NULL;
631 char *funcname = NULL;
633 GtkTreeModel *acr_model = GTK_TREE_MODEL (PSPPIRE_ACR (agg->summary_acr)->list_store);
634 GtkTreeModel *combo_model = gtk_combo_box_get_model (GTK_COMBO_BOX (agg->function_combo));
637 /* This is an index into the combo_model. Its used to get the function name */
641 enum agr_src_vars has_src_vars;
643 gchar *srcvar = NULL;
645 gtk_tree_model_get (acr_model, iter,
646 SUMMARY_COL_VARNAME, &varname,
647 SUMMARY_COL_VARLABEL, &label,
648 SUMMARY_COL_FUNCIDX, &f_idx,
649 SUMMARY_COL_SRCVAR, &srcvar,
650 SUMMARY_COL_ARG1, &arg1,
651 SUMMARY_COL_ARG2, &arg2,
654 gtk_tree_model_iter_nth_child (combo_model, &combo_iter, NULL, f_idx);
656 gtk_tree_model_get (combo_model, &combo_iter,
657 COMBO_MODEL_COL_SYNTAX, &funcname,
658 COMBO_MODEL_COL_ARITY, &arity,
659 COMBO_MODEL_COL_SRC_VARS, &has_src_vars,
662 g_string_append (string, varname);
664 if (0 != strcmp ("", label))
668 syntax_gen_string (&ss, ss_cstr (label));
669 g_string_append (string, " ");
670 g_string_append (string, ds_cstr (&ss));
674 g_string_append_printf (string, " = %s", funcname);
676 if ( has_src_vars != AGR_SV_NO)
679 ds_init_cstr (&dss, " (");
681 ds_put_cstr (&dss, srcvar);
684 ds_put_c_format (&dss, ", %g", arg1);
687 ds_put_c_format (&dss, ", %g", arg2);
689 ds_put_cstr (&dss, ")");
691 g_string_append (string, ds_cstr (&dss));
705 append_summary_variable_syntax (const struct aggregate *agg, GString *string)
708 GtkTreeModel *acr_model = GTK_TREE_MODEL (PSPPIRE_ACR (agg->summary_acr)->list_store);
713 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (acr_model), &iter);
715 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (acr_model), &iter)
718 g_string_append (string, "\n\t/");
720 append_summary_spec (agg, &iter, string);
726 generate_syntax (const struct aggregate *agg)
730 GString *string = g_string_new ("AGGREGATE OUTFILE=");
732 append_destination_filename (agg, string);
734 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (agg->sorted_button)))
735 g_string_append (string, "\n\t/PRESORTED");
737 g_string_append (string, "\n\t/BREAK=");
739 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (agg->break_variables), 0, string);
741 append_summary_variable_syntax (agg, string);
743 g_string_append (string, ".\n");
747 g_string_free (string, FALSE);