1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2010 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"
20 #include <language/syntax-string-source.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"
44 #define _(msgid) gettext (msgid)
45 #define N_(msgid) msgid
50 COMBO_MODEL_COL_DESC = 0,
51 COMBO_MODEL_COL_SYNTAX,
52 COMBO_MODEL_COL_SRC_VARS,
60 PsppireDataWindow *de ;
62 GtkWidget *break_variables;
64 GtkWidget *replace_radiobutton;
65 GtkWidget *add_radiobutton;
66 GtkWidget *filename_radiobutton;
67 GtkWidget *filename_button;
68 GtkWidget *filename_box;
69 GtkWidget *filename_label;
71 GtkWidget *function_combo;
73 GtkWidget *summary_acr;
74 GtkWidget *summary_var_name_entry;
75 GtkWidget *summary_var_label_entry;
77 GtkWidget *summary_sv;
78 GtkWidget *summary_sv_entry;
80 GtkWidget *summary_arg1;
81 GtkWidget *summary_arg2;
83 GtkWidget *summary_arg1_entry;
84 GtkWidget *summary_arg2_entry;
86 GtkWidget *sorted_button;
87 GtkWidget *needs_sort_button;
93 static char * generate_syntax (const struct aggregate *rd);
96 /* Makes widget W's sensitivity follow the active state of TOGGLE */
98 sensitive_if_active (GtkToggleButton *toggle, GtkWidget *w)
100 gboolean active = gtk_toggle_button_get_active (toggle);
102 gtk_widget_set_sensitive (w, active);
105 static void update_arguments (struct aggregate *agg);
109 refresh (struct aggregate *agg)
111 GtkTreeModel *liststore =
112 gtk_tree_view_get_model (GTK_TREE_VIEW (agg->break_variables));
113 gtk_list_store_clear (GTK_LIST_STORE (liststore));
115 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (agg->add_radiobutton), TRUE);
116 gtk_label_set_text (GTK_LABEL (agg->filename_label), "");
119 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (agg->needs_sort_button), TRUE);
121 gtk_entry_set_text (GTK_ENTRY (agg->summary_sv_entry), "");
122 gtk_entry_set_text (GTK_ENTRY (agg->summary_arg1_entry), "");
123 gtk_entry_set_text (GTK_ENTRY (agg->summary_arg2_entry), "");
124 gtk_entry_set_text (GTK_ENTRY (agg->summary_var_label_entry), "");
125 gtk_entry_set_text (GTK_ENTRY (agg->summary_var_name_entry), "N_BREAK");
126 gtk_editable_select_region (GTK_EDITABLE (agg->summary_var_name_entry), 0, -1);
128 gtk_combo_box_set_active (GTK_COMBO_BOX (agg->function_combo), N);
130 gtk_list_store_clear (PSPPIRE_ACR (agg->summary_acr)->list_store);
132 update_arguments (agg);
137 dialog_state_valid (gpointer data)
140 const struct aggregate *agg = data;
142 GtkTreeModel *liststore =
143 gtk_tree_view_get_model (GTK_TREE_VIEW (agg->break_variables));
145 if ( ! gtk_tree_model_get_iter_first (liststore, &iter))
148 liststore = GTK_TREE_MODEL (PSPPIRE_ACR (agg->summary_acr)->list_store);
150 if ( ! gtk_tree_model_get_iter_first (liststore, &iter))
158 choose_filename (struct aggregate *fd)
160 GtkFileFilter *filter;
162 GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Aggregate destination file"),
164 GTK_FILE_CHOOSER_ACTION_SAVE,
165 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
166 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
169 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
172 filter = gtk_file_filter_new ();
173 gtk_file_filter_set_name (filter, _("System Files (*.sav)"));
174 gtk_file_filter_add_pattern (filter, "*.sav");
175 gtk_file_filter_add_pattern (filter, "*.SAV");
176 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
178 filter = gtk_file_filter_new ();
179 gtk_file_filter_set_name (filter, _("Portable Files (*.por) "));
180 gtk_file_filter_add_pattern (filter, "*.por");
181 gtk_file_filter_add_pattern (filter, "*.POR");
182 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
185 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
189 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
191 gtk_label_set_text (GTK_LABEL (fd->filename_label), filename);
197 gtk_widget_destroy (dialog);
202 populate_combo_model (GtkComboBox *cb)
204 GtkListStore *list = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
206 const struct agr_func *af = agr_func_tab;
207 GtkCellRenderer *renderer ;
209 for (af = agr_func_tab; af->name; ++af)
211 const gchar *s = af->description;
215 gtk_list_store_append (list, &iter);
216 gtk_list_store_set (list, &iter,
217 COMBO_MODEL_COL_DESC, s,
218 COMBO_MODEL_COL_SYNTAX, af->name,
219 COMBO_MODEL_COL_SRC_VARS, af->src_vars,
220 COMBO_MODEL_COL_ARITY, af->n_args,
224 renderer = gtk_cell_renderer_text_new ();
225 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cb), renderer, FALSE);
227 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (cb), renderer, "text", 0);
229 gtk_combo_box_set_model (GTK_COMBO_BOX (cb), GTK_TREE_MODEL (list));
234 /* Returns TRUE iff all the necessary controls have been set to
235 completely specify a summary function */
237 summary_complete (const struct aggregate *agg)
241 enum agr_src_vars src_vars;
243 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (agg->function_combo));
245 if ( 0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (agg->summary_var_name_entry))))
248 ok = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (agg->function_combo), &iter);
255 gtk_tree_model_get (model,
257 COMBO_MODEL_COL_ARITY, &n_args,
258 COMBO_MODEL_COL_SRC_VARS, &src_vars,
261 if ( src_vars == AGR_SV_YES )
263 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (agg->summary_sv_entry))))
269 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (agg->summary_arg2_entry))))
275 if (0 == strcmp ("", gtk_entry_get_text (GTK_ENTRY (agg->summary_arg1_entry))))
285 append_summary_spec (const struct aggregate *agg, GtkTreeIter *iter, GString *string);
289 render_summary (GtkTreeViewColumn *tree_column,
290 GtkCellRenderer *cell,
291 GtkTreeModel *tree_model,
295 struct aggregate *agg = data;
297 GString *string = g_string_new ("");
299 append_summary_spec (agg, iter, string);
302 g_object_set (cell, "text", string->str, NULL);
304 g_string_free (string, TRUE);
308 /* Enable/Disable the summary variable ACR */
310 update_acr (struct aggregate *agg)
312 gboolean ready = summary_complete (agg);
314 psppire_acr_set_enabled (PSPPIRE_ACR (agg->summary_acr), ready);
318 /* Update the sensitivity of the summary variable argument fields */
320 update_arguments (struct aggregate *agg)
324 gboolean ok = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (agg->function_combo), &iter);
328 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (agg->function_combo));
330 enum agr_src_vars src_vars;
331 gtk_tree_model_get (model,
333 COMBO_MODEL_COL_ARITY, &n_args,
334 COMBO_MODEL_COL_SRC_VARS, &src_vars,
337 gtk_widget_set_sensitive (agg->summary_sv, src_vars != AGR_SV_NO);
338 gtk_widget_set_sensitive (agg->summary_arg2, n_args >= 2);
339 gtk_widget_set_sensitive (agg->summary_arg1, n_args >= 1);
343 gtk_widget_set_sensitive (agg->summary_sv, FALSE);
344 gtk_widget_set_sensitive (agg->summary_arg2, FALSE);
345 gtk_widget_set_sensitive (agg->summary_arg1, FALSE);
351 SUMMARY_COL_VARNAME = 0,
352 SUMMARY_COL_VARLABEL,
359 /* Set VAL to the value appropriate for COL according to the
360 current state of the dialog */
362 get_summary_spec (gint col, GValue *val, gpointer data)
364 const struct aggregate *agg = data;
367 case SUMMARY_COL_VARNAME:
368 g_value_init (val, G_TYPE_STRING);
369 g_value_set_string (val, gtk_entry_get_text (GTK_ENTRY (agg->summary_var_name_entry)));
371 case SUMMARY_COL_VARLABEL:
372 g_value_init (val, G_TYPE_STRING);
373 g_value_set_string (val, gtk_entry_get_text (GTK_ENTRY (agg->summary_var_label_entry)));
375 case SUMMARY_COL_SRCVAR:
376 g_value_init (val, G_TYPE_STRING);
377 g_value_set_string (val, gtk_entry_get_text (GTK_ENTRY (agg->summary_sv_entry)));
379 case SUMMARY_COL_FUNCIDX:
380 g_value_init (val, G_TYPE_INT);
381 g_value_set_int (val, gtk_combo_box_get_active (GTK_COMBO_BOX (agg->function_combo)));
383 case SUMMARY_COL_ARG1:
385 const gchar *text = gtk_entry_get_text (GTK_ENTRY (agg->summary_arg1_entry));
386 g_value_init (val, G_TYPE_DOUBLE);
387 g_value_set_double (val, g_strtod (text, 0));
390 case SUMMARY_COL_ARG2:
392 const gchar *text = gtk_entry_get_text (GTK_ENTRY (agg->summary_arg2_entry));
393 g_value_init (val, G_TYPE_DOUBLE);
394 g_value_set_double (val, g_strtod (text, 0));
398 g_assert_not_reached ();
406 /* Update the status of the dialog box according to what row of the ACR's treeview
409 on_acr_change (const struct aggregate *agg, GtkTreeView *tv)
411 const gchar *varname;
419 GtkTreePath *path = NULL;
420 GtkTreeModel *model = gtk_tree_view_get_model (tv);
421 gtk_tree_view_get_cursor (tv, &path, NULL);
423 gtk_tree_model_get_iter (model, &iter, path);
425 gtk_tree_model_get (model, &iter,
426 SUMMARY_COL_VARNAME, &varname,
427 SUMMARY_COL_VARLABEL, &label,
428 SUMMARY_COL_FUNCIDX, &f_idx,
429 SUMMARY_COL_SRCVAR, &srcvar,
430 SUMMARY_COL_ARG1, &arg1,
431 SUMMARY_COL_ARG2, &arg2,
434 gtk_entry_set_text (GTK_ENTRY (agg->summary_var_name_entry), varname);
435 gtk_entry_set_text (GTK_ENTRY (agg->summary_var_label_entry), label);
436 gtk_entry_set_text (GTK_ENTRY (agg->summary_sv_entry), srcvar);
438 text = g_strdup_printf ("%g", arg1);
439 gtk_entry_set_text (GTK_ENTRY (agg->summary_arg1_entry), text);
442 text = g_strdup_printf ("%g", arg2);
443 gtk_entry_set_text (GTK_ENTRY (agg->summary_arg2_entry), text);
447 gtk_combo_box_set_active (GTK_COMBO_BOX (agg->function_combo), f_idx);
451 /* Set the pane to 50% of its maximum size */
453 set_initial_pos (GtkPaned *pane)
457 "max-position", &max_pos,
460 gtk_paned_set_position (pane, max_pos / 2);
465 /* Pops up the Aggregate dialog box */
467 aggregate_dialog (PsppireDataWindow *dw)
477 GtkWidget *break_selector ;
479 fd.xml = builder_new ("aggregate.ui");
483 dialog = get_widget_assert (fd.xml, "psppire-dialog1");
484 source = get_widget_assert (fd.xml, "dict-view");
485 break_selector = get_widget_assert (fd.xml, "break-selector");
487 fd.pane = get_widget_assert (fd.xml, "hbox1");
489 fd.break_variables = get_widget_assert (fd.xml, "psppire-var-view1");
490 fd.filename_radiobutton = get_widget_assert (fd.xml, "filename-radiobutton");
491 fd.filename_button = get_widget_assert (fd.xml, "filename-button");
492 fd.filename_box = get_widget_assert (fd.xml, "filename-box");
493 fd.filename_label = get_widget_assert (fd.xml, "filename-label");
494 fd.replace_radiobutton = get_widget_assert (fd.xml, "replace-radiobutton");
495 fd.add_radiobutton = get_widget_assert (fd.xml, "add-radiobutton");
496 fd.function_combo = get_widget_assert (fd.xml, "function-combo");
498 fd.summary_acr = get_widget_assert (fd.xml, "psppire-acr1");
499 fd.summary_var_name_entry = get_widget_assert (fd.xml, "summary-var-name-entry");
501 fd.summary_arg1 = get_widget_assert (fd.xml, "summary-arg1");
502 fd.summary_arg2 = get_widget_assert (fd.xml, "summary-arg2");
504 fd.summary_arg1_entry = get_widget_assert (fd.xml, "summary-arg-entry1");
505 fd.summary_arg2_entry = get_widget_assert (fd.xml, "summary-arg-entry2");
507 fd.summary_var_label_entry = get_widget_assert (fd.xml, "summary-var-label-entry");
509 fd.summary_sv = get_widget_assert (fd.xml, "source-var");
510 fd.summary_sv_entry = get_widget_assert (fd.xml, "source-var-entry");
512 fd.sorted_button = get_widget_assert (fd.xml, "sorted-radiobutton");
513 fd.needs_sort_button = get_widget_assert (fd.xml, "needs-sort-radiobutton");
516 GtkTreeViewColumn *column ;
520 GtkCellRenderer *cell_renderer ;
522 GtkListStore *list = gtk_list_store_new (6,
530 psppire_acr_set_model (PSPPIRE_ACR (fd.summary_acr), list);
532 psppire_acr_set_get_value_func (PSPPIRE_ACR (fd.summary_acr),
533 get_summary_spec, &fd);
535 column = gtk_tree_view_get_column (PSPPIRE_ACR (fd.summary_acr)->tv, 0);
537 l = gtk_tree_view_column_get_cell_renderers (column);
539 cell_renderer = l->data;
541 gtk_tree_view_column_set_cell_data_func (column,
547 g_signal_connect_swapped (PSPPIRE_ACR (fd.summary_acr)->tv,
548 "cursor-changed", G_CALLBACK (on_acr_change), &fd);
551 g_signal_connect_swapped (fd.summary_var_name_entry, "changed", G_CALLBACK (update_acr), &fd);
552 g_signal_connect_swapped (fd.function_combo, "changed", G_CALLBACK (update_acr), &fd);
553 g_signal_connect_swapped (fd.summary_sv_entry, "changed", G_CALLBACK (update_acr), &fd);
554 g_signal_connect_swapped (fd.summary_arg1_entry, "changed", G_CALLBACK (update_acr), &fd);
555 g_signal_connect_swapped (fd.summary_arg2_entry, "changed", G_CALLBACK (update_acr), &fd);
558 g_signal_connect (fd.pane, "realize", G_CALLBACK (set_initial_pos), &fd);
561 g_signal_connect_swapped (fd.function_combo, "changed",
562 G_CALLBACK (update_arguments), &fd);
564 populate_combo_model (GTK_COMBO_BOX (fd.function_combo));
566 g_signal_connect_swapped (dialog, "refresh", G_CALLBACK (refresh), &fd);
568 g_object_get (fd.de->data_editor, "var-store", &vs, NULL);
570 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fd.de));
572 g_object_get (vs, "dictionary", &fd.dict, NULL);
573 g_object_set (source, "model", fd.dict, NULL);
576 psppire_selector_set_filter_func (PSPPIRE_SELECTOR (break_selector), NULL);
579 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (dialog),
580 dialog_state_valid, &fd);
582 g_signal_connect (fd.filename_radiobutton, "toggled",
583 G_CALLBACK (sensitive_if_active), fd.filename_box );
585 g_signal_connect_swapped (fd.filename_button, "clicked",
586 G_CALLBACK (choose_filename), &fd);
589 response = psppire_dialog_run (PSPPIRE_DIALOG (dialog));
593 case GTK_RESPONSE_OK:
595 gchar *syntax = generate_syntax (&fd);
597 struct getl_interface *sss = create_syntax_string_source (syntax);
598 execute_syntax (sss);
603 case PSPPIRE_RESPONSE_PASTE:
605 gchar *syntax = generate_syntax (&fd);
606 paste_syntax_to_window (syntax);
615 g_object_unref (fd.xml);
622 append_destination_filename (const struct aggregate *agg, GString *gs)
624 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (agg->filename_radiobutton)))
627 const gchar *s = gtk_label_get_text (GTK_LABEL (agg->filename_label));
629 syntax_gen_string (&ss, ss_cstr (s));
630 g_string_append (gs, ds_cstr (&ss));
635 g_string_append (gs, "* ");
637 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (agg->replace_radiobutton)))
638 g_string_append (gs, "MODE=REPLACE");
640 g_string_append (gs, "MODE=ADDVARIABLES");
644 /* Append the syntax of the summary function pointed to by ITER to STRING */
646 append_summary_spec (const struct aggregate *agg, GtkTreeIter *iter, GString *string)
648 GtkTreeIter combo_iter;
650 const char *funcname;
652 GtkTreeModel *acr_model = GTK_TREE_MODEL (PSPPIRE_ACR (agg->summary_acr)->list_store);
653 GtkTreeModel *combo_model = gtk_combo_box_get_model (GTK_COMBO_BOX (agg->function_combo));
656 /* This is an index into the combo_model. Its used to get the function name */
660 enum agr_src_vars has_src_vars;
662 const gchar *srcvar ;
664 gtk_tree_model_get (acr_model, iter,
665 SUMMARY_COL_VARNAME, &varname,
666 SUMMARY_COL_VARLABEL, &label,
667 SUMMARY_COL_FUNCIDX, &f_idx,
668 SUMMARY_COL_SRCVAR, &srcvar,
669 SUMMARY_COL_ARG1, &arg1,
670 SUMMARY_COL_ARG2, &arg2,
673 gtk_tree_model_iter_nth_child (combo_model, &combo_iter, NULL, f_idx);
675 gtk_tree_model_get (combo_model, &combo_iter,
676 COMBO_MODEL_COL_SYNTAX, &funcname,
677 COMBO_MODEL_COL_ARITY, &arity,
678 COMBO_MODEL_COL_SRC_VARS, &has_src_vars,
681 g_string_append (string, varname);
683 if (0 != strcmp ("", label))
687 syntax_gen_string (&ss, ss_cstr (label));
688 g_string_append (string, " ");
689 g_string_append (string, ds_cstr (&ss));
693 g_string_append_printf (string, " = %s", funcname);
695 if ( has_src_vars != AGR_SV_NO)
697 g_string_append (string, " (");
699 g_string_append (string, srcvar);
702 g_string_append_printf (string, ", %g", arg1);
705 g_string_append_printf (string, ", %g", arg2);
707 g_string_append (string, ")");
714 append_summary_variable_syntax (const struct aggregate *agg, GString *string)
717 GtkTreeModel *acr_model = GTK_TREE_MODEL (PSPPIRE_ACR (agg->summary_acr)->list_store);
722 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (acr_model), &iter);
724 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (acr_model), &iter)
727 g_string_append (string, "\n\t/");
729 append_summary_spec (agg, &iter, string);
735 generate_syntax (const struct aggregate *agg)
739 GString *string = g_string_new ("AGGREGATE OUTFILE=");
741 append_destination_filename (agg, string);
743 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (agg->sorted_button)))
744 g_string_append (string, "\n\t/PRESORTED");
746 g_string_append (string, "\n\t/BREAK=");
748 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (agg->break_variables), 0, string);
750 append_summary_variable_syntax (agg, string);
752 g_string_append (string, ".\n");
756 g_string_free (string, FALSE);