1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2009 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/>. */
17 /* This module implements the RECODE dialog.
19 It has two forms. One for recoding values into the same variable.
20 The second for recoding into different variables.
25 #include "recode-dialog.h"
29 #include "psppire-var-view.h"
34 #include <language/syntax-string-source.h>
35 #include <ui/gui/psppire-data-window.h>
36 #include <ui/gui/dialog-common.h>
37 #include <ui/gui/dict-display.h>
38 #include <ui/gui/helper.h>
39 #include <ui/gui/psppire-dialog.h>
40 #include <ui/gui/psppire-var-store.h>
42 #include <ui/syntax-gen.h>
44 #include "psppire-acr.h"
47 #define _(msgid) gettext (msgid)
48 #define N_(msgid) msgid
51 /* Define a boxed type to represent a value which is a candidate
52 to replace an existing value */
65 enum new_value_type type;
73 static struct new_value *
74 new_value_copy (struct new_value *nv)
76 struct new_value *copy = g_memdup (nv, sizeof (*copy));
78 if ( nv->type == NV_STRING )
79 copy->v.s = xstrdup (nv->v.s);
86 new_value_free (struct new_value *nv)
88 if ( nv->type == NV_STRING )
96 new_value_to_string (const GValue *src, GValue *dest)
98 const struct new_value *nv = g_value_get_boxed (src);
106 gchar *text = g_strdup_printf ("%g", nv->v.v);
107 g_value_set_string (dest, text);
112 g_value_set_string (dest, nv->v.s);
115 g_value_set_string (dest, "COPY");
118 g_value_set_string (dest, "SYSMIS");
121 /* Shouldn't ever happen */
122 g_warning ("Invalid type in new recode value");
123 g_value_set_string (dest, "???");
129 new_value_get_type (void)
135 t = g_boxed_type_register_static ("psppire-recode-new-values",
136 (GBoxedCopyFunc) new_value_copy,
137 (GBoxedFreeFunc) new_value_free);
139 g_value_register_transform_func (t, G_TYPE_STRING,
140 new_value_to_string);
150 /* A boxed type representing a value, or a range of values which may
151 potentially be replaced by something */
167 enum old_value_type type;
176 static struct old_value *
177 old_value_copy (struct old_value *ov)
179 struct old_value *copy = g_memdup (ov, sizeof (*copy));
181 if ( ov->type == OV_STRING )
182 copy->v.s = g_strdup (ov->v.s);
189 old_value_free (struct old_value *ov)
191 if (ov->type == OV_STRING)
197 old_value_to_string (const GValue *src, GValue *dest)
199 const struct old_value *ov = g_value_get_boxed (src);
205 gchar *text = g_strdup_printf ("%g", ov->v.v);
206 g_value_set_string (dest, text);
211 g_value_set_string (dest, ov->v.s);
214 g_value_set_string (dest, "MISSING");
217 g_value_set_string (dest, "SYSMIS");
220 g_value_set_string (dest, "ELSE");
225 char en_dash[6] = {0,0,0,0,0,0};
227 g_unichar_to_utf8 (0x2013, en_dash);
229 text = g_strdup_printf ("%g %s %g",
233 g_value_set_string (dest, text);
240 char en_dash[6] = {0,0,0,0,0,0};
242 g_unichar_to_utf8 (0x2013, en_dash);
244 text = g_strdup_printf ("LOWEST %s %g",
248 g_value_set_string (dest, text);
255 char en_dash[6] = {0,0,0,0,0,0};
257 g_unichar_to_utf8 (0x2013, en_dash);
259 text = g_strdup_printf ("%g %s HIGHEST",
263 g_value_set_string (dest, text);
268 g_warning ("Invalid type in old recode value");
269 g_value_set_string (dest, "???");
275 old_value_get_type (void)
281 t = g_boxed_type_register_static ("psppire-recode-old-values",
282 (GBoxedCopyFunc) old_value_copy,
283 (GBoxedFreeFunc) old_value_free);
285 g_value_register_transform_func (t, G_TYPE_STRING,
286 old_value_to_string);
304 BUTTON_OLD_HIGH_DOWN,
314 PsppireDialog *old_and_new_dialog;
316 GtkWidget *dict_treeview;
317 GtkWidget *variable_treeview;
318 GtkWidget *toggle[n_BUTTONS];
320 GtkWidget *strings_box;
321 GtkWidget *convert_button;
322 GtkWidget *new_copy_label;
324 GtkWidget *ov_value_entry;
325 GtkWidget *new_value_entry;
327 GtkWidget *ov_range_lower_entry;
328 GtkWidget *ov_range_upper_entry;
329 GtkWidget *ov_low_up_entry;
330 GtkWidget *ov_high_down_entry;
332 GtkListStore *value_map;
334 /* Indicates that the INTO {new variables} form of the dialog
340 gboolean input_var_is_string;
342 GtkWidget *new_name_entry;
343 GtkWidget *new_label_entry;
344 GtkWidget *change_button;
346 GtkWidget *string_button;
347 GtkWidget *width_entry;
349 /* A hash table of struct nlp's indexed by variable */
354 static void run_old_and_new_dialog (struct recode_dialog *rd);
357 refresh (PsppireDialog *dialog, struct recode_dialog *rd)
360 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
362 gtk_list_store_clear (GTK_LIST_STORE (vars));
364 gtk_widget_set_sensitive (rd->change_button, FALSE);
365 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
366 gtk_widget_set_sensitive (rd->new_label_entry, FALSE);
369 g_hash_table_remove_all (rd->varmap);
371 gtk_list_store_clear (GTK_LIST_STORE (rd->value_map));
374 static char * generate_syntax (const struct recode_dialog *rd);
382 /* Dialog is valid iff at least one variable has been selected,
383 AND the list of mappings is not empty.
386 dialog_state_valid (gpointer data)
388 GtkTreeIter not_used;
389 struct recode_dialog *rd = data;
391 if ( ! rd->value_map )
394 if ( ! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->value_map),
400 GtkTreeModel *model = GTK_TREE_MODEL (PSPPIRE_VAR_VIEW (rd->variable_treeview)->list);
402 if (g_hash_table_size (rd->varmap) != gtk_tree_model_iter_n_children (model, NULL) )
408 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
410 if ( !gtk_tree_model_get_iter_first (vars, ¬_used))
418 on_old_new_show (struct recode_dialog *rd)
420 gtk_toggle_button_set_active
421 (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_OLD_VALUE]), TRUE);
423 g_signal_emit_by_name (rd->toggle[BUTTON_OLD_VALUE], "toggled");
425 gtk_toggle_button_set_active
426 (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_NEW_VALUE]), TRUE);
428 g_signal_emit_by_name (rd->toggle[BUTTON_NEW_VALUE], "toggled");
430 g_object_set (rd->toggle[BUTTON_NEW_COPY],
431 "visible", rd->different, NULL);
433 g_object_set (rd->new_copy_label,
434 "visible", rd->different, NULL);
436 g_object_set (rd->strings_box,
437 "visible", rd->different, NULL);
440 /* Sets the sensitivity of TARGET dependent upon the active status
443 toggle_sensitivity (GtkToggleButton *button, GtkWidget *target)
445 gboolean state = gtk_toggle_button_get_active (button);
447 /* g_print ("%s Setting %p (%s) to %d because of %p\n",
448 __FUNCTION__, target, gtk_widget_get_name (target), state, button); */
450 gtk_widget_set_sensitive (target, state);
453 static void recode_dialog (PsppireDataWindow *de, gboolean diff);
456 /* Pops up the Recode Same version of the dialog box */
458 recode_same_dialog (PsppireDataWindow *de)
460 recode_dialog (de, FALSE);
463 /* Pops up the Recode Different version of the dialog box */
465 recode_different_dialog (PsppireDataWindow *de)
467 recode_dialog (de, TRUE);
471 /* This might need to be changed to something less naive.
472 In particular, what happends with dates, etc?
475 num_to_string (gdouble x)
477 return g_strdup_printf ("%g", x);
480 /* Callback which gets called when a new row is selected
481 in the acr's variable treeview.
482 We use if to set the togglebuttons and entries to correspond to the
486 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
488 struct recode_dialog *rd = data;
489 GtkTreeModel *model = NULL;
492 GValue ov_value = {0};
493 GValue nv_value = {0};
494 struct old_value *ov = NULL;
495 struct new_value *nv = NULL;
497 if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
501 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
502 COL_VALUE_OLD, &ov_value);
504 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
505 COL_VALUE_NEW, &nv_value);
507 ov = g_value_get_boxed (&ov_value);
508 nv = g_value_get_boxed (&nv_value);
516 gchar *str = num_to_string (nv->v.v);
518 gtk_toggle_button_set_active
519 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE]), TRUE);
521 gtk_entry_set_text (GTK_ENTRY (rd->new_value_entry), str);
526 gtk_toggle_button_set_active
527 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE]), TRUE);
529 gtk_entry_set_text (GTK_ENTRY (rd->new_value_entry), nv->v.s);
532 gtk_toggle_button_set_active
533 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS]), TRUE);
537 gtk_toggle_button_set_active
538 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY]), TRUE);
542 g_warning ("Invalid new value type");
546 g_value_unset (&nv_value);
554 gtk_toggle_button_set_active
555 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_VALUE]), TRUE);
557 gtk_entry_set_text (GTK_ENTRY (rd->ov_value_entry), ov->v.s);
564 gtk_toggle_button_set_active
565 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_VALUE]), TRUE);
567 str = num_to_string (ov->v.v);
569 gtk_entry_set_text (GTK_ENTRY (rd->ov_value_entry), str);
576 gtk_toggle_button_set_active
577 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_SYSMIS]), TRUE);
581 gtk_toggle_button_set_active
582 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_MISSING]), TRUE);
589 gtk_toggle_button_set_active
590 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_RANGE]), TRUE);
592 str = num_to_string (ov->v.range[0]);
594 gtk_entry_set_text (GTK_ENTRY (rd->ov_range_lower_entry), str);
599 str = num_to_string (ov->v.range[1]);
601 gtk_entry_set_text (GTK_ENTRY (rd->ov_range_upper_entry), str);
609 gchar *str = num_to_string (ov->v.range[1]);
611 gtk_toggle_button_set_active
612 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_LOW_UP]), TRUE);
614 gtk_entry_set_text (GTK_ENTRY (rd->ov_low_up_entry), str);
622 gchar *str = num_to_string (ov->v.range[0]);
624 gtk_toggle_button_set_active
625 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_HIGH_DOWN]), TRUE);
627 gtk_entry_set_text (GTK_ENTRY (rd->ov_high_down_entry), str);
634 gtk_toggle_button_set_active
635 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_ELSE]), TRUE);
639 g_warning ("Unknown old value type");
642 g_value_unset (&ov_value);
646 /* Name-Label pair */
654 nlp_create (const char *name, const char *label)
656 struct nlp *nlp = xmalloc (sizeof *nlp);
658 nlp->name = g_strdup (name);
662 if ( 0 != strcmp ("", label))
663 nlp->label = g_strdup (label);
669 nlp_destroy (gpointer data)
671 struct nlp *nlp = data ;
681 /* Callback which gets called when a new row is selected
682 in the variable treeview.
683 It sets the name and label entry widgets to reflect the
684 currently selected row.
687 on_selection_change (GtkTreeSelection *selection, gpointer data)
689 struct recode_dialog *rd = data;
691 GtkTreeModel *model = GTK_TREE_MODEL (PSPPIRE_VAR_VIEW (rd->variable_treeview)->list);
693 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
695 if ( rows && !rows->next)
697 /* Exactly one row is selected */
699 struct variable *var;
703 gtk_widget_set_sensitive (rd->change_button, TRUE);
704 gtk_widget_set_sensitive (rd->new_name_entry, TRUE);
705 gtk_widget_set_sensitive (rd->new_label_entry, TRUE);
707 ok = gtk_tree_model_get_iter (model, &iter, (GtkTreePath*) rows->data);
709 gtk_tree_model_get (model, &iter,
713 nlp = g_hash_table_lookup (rd->varmap, var);
717 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), nlp->name ? nlp->name : "");
718 gtk_entry_set_text (GTK_ENTRY (rd->new_label_entry), nlp->label ? nlp->label : "");
722 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
723 gtk_entry_set_text (GTK_ENTRY (rd->new_label_entry), "");
728 gtk_widget_set_sensitive (rd->change_button, FALSE);
729 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
730 gtk_widget_set_sensitive (rd->new_label_entry, FALSE);
732 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
733 gtk_entry_set_text (GTK_ENTRY (rd->new_label_entry), "");
737 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
742 on_string_toggled (GtkToggleButton *b, struct recode_dialog *rd)
745 if (! rd->input_var_is_string )
748 active = gtk_toggle_button_get_active (b);
749 gtk_widget_set_sensitive (rd->convert_button, !active);
754 on_convert_toggled (GtkToggleButton *b, struct recode_dialog *rd)
758 g_return_if_fail (rd->input_var_is_string);
760 active = gtk_toggle_button_get_active (b);
761 gtk_widget_set_sensitive (rd->string_button, !active);
765 on_change_clicked (GObject *obj, gpointer data)
767 struct recode_dialog *rd = data;
768 struct variable *var = NULL;
770 GtkTreeModel *model = GTK_TREE_MODEL (PSPPIRE_VAR_VIEW (rd->variable_treeview)->list);
772 GtkTreeSelection *selection =
773 gtk_tree_view_get_selection (GTK_TREE_VIEW (rd->variable_treeview));
775 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
777 const gchar *dest_var_name =
778 gtk_entry_get_text (GTK_ENTRY (rd->new_name_entry));
780 const gchar *dest_var_label =
781 gtk_entry_get_text (GTK_ENTRY (rd->new_label_entry));
783 if ( NULL == rows || rows->next != NULL)
786 gtk_tree_model_get_iter (model, &iter, rows->data);
788 gtk_tree_model_get (model, &iter, 0, &var, -1);
790 g_hash_table_remove (rd->varmap, var);
792 nlp = nlp_create (dest_var_name, dest_var_label);
794 g_hash_table_insert (rd->varmap, var, nlp);
796 gtk_tree_model_row_changed (model, rows->data, &iter);
799 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
804 /* If there's nothing selected in the variable treeview,
805 then automatically select the first item */
807 select_something (GtkTreeModel *treemodel,
812 struct recode_dialog *rd = data;
813 GtkTreeSelection *sel;
815 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd->variable_treeview));
817 if ( gtk_tree_selection_count_selected_rows (sel) < 1)
821 gtk_tree_model_get_iter_first (treemodel, &iter);
823 gtk_tree_selection_select_iter (sel, &iter);
828 /* Callback for the new_value_entry and new_value_togglebutton widgets.
829 It's used to enable/disable the acr. */
831 set_acr (struct recode_dialog *rd)
835 if ( !gtk_toggle_button_get_active
836 (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_NEW_VALUE])))
838 psppire_acr_set_enabled (rd->acr, TRUE);
842 text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
844 psppire_acr_set_enabled (rd->acr, !g_str_equal (text, ""));
848 render_new_var_name (GtkTreeViewColumn *tree_column,
849 GtkCellRenderer *cell,
850 GtkTreeModel *tree_model,
854 struct nlp *nlp = NULL;
855 struct recode_dialog *rd = data;
857 struct variable *var = NULL;
859 gtk_tree_model_get (tree_model, iter,
863 nlp = g_hash_table_lookup (rd->varmap, var);
866 g_object_set (cell, "text", nlp->name, NULL);
868 g_object_set (cell, "text", "", NULL);
874 recode_dialog (PsppireDataWindow *de, gboolean diff)
878 struct recode_dialog rd;
880 GtkBuilder *builder = builder_new ("recode.ui");
882 GtkWidget *selector = get_widget_assert (builder, "psppire-selector1");
884 GtkWidget *oldandnew = get_widget_assert (builder, "button1");
887 GtkWidget *output_variable_box = get_widget_assert (builder,"frame4");
889 PsppireVarStore *vs = NULL;
890 g_object_get (de->data_editor, "var-store", &vs, NULL);
892 rd.change_button = get_widget_assert (builder, "change-button");
894 rd.dialog = get_widget_assert (builder, "recode-dialog");
895 rd.dict_treeview = get_widget_assert (builder, "treeview1");
896 rd.variable_treeview = get_widget_assert (builder, "treeview2");
897 rd.new_name_entry = get_widget_assert (builder, "dest-name-entry");
898 rd.new_label_entry = get_widget_assert (builder, "dest-label-entry");
900 g_object_get (vs, "dictionary", &rd.dict, NULL);
902 rd.value_map = gtk_list_store_new (2,
903 old_value_get_type (),
904 new_value_get_type ()
907 g_object_set (output_variable_box, "visible", diff, NULL);
910 gtk_window_set_title (GTK_WINDOW (rd.dialog),
911 _("Recode into Different Variables"));
913 gtk_window_set_title (GTK_WINDOW (rd.dialog),
914 _("Recode into Same Variables"));
918 gtk_window_set_transient_for (GTK_WINDOW (rd.dialog), GTK_WINDOW (de));
920 g_object_set (rd.dict_treeview, "model", rd.dict, NULL);
924 GtkTreeModel *model = GTK_TREE_MODEL (PSPPIRE_VAR_VIEW (rd.variable_treeview)->list);
925 GtkTreeSelection *sel;
927 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
929 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes (_("New"),
934 gtk_tree_view_column_set_cell_data_func (col, renderer,
939 gtk_tree_view_append_column (GTK_TREE_VIEW (rd.variable_treeview), col);
942 col = gtk_tree_view_get_column (GTK_TREE_VIEW (rd.variable_treeview), 0);
944 g_object_set (col, "title", _("Old"), NULL);
946 g_object_set (rd.variable_treeview, "headers-visible", TRUE, NULL);
948 rd.varmap = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, nlp_destroy);
950 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd.variable_treeview));
952 g_signal_connect (sel, "changed",
953 G_CALLBACK (on_selection_change), &rd);
955 g_signal_connect (rd.change_button, "clicked",
956 G_CALLBACK (on_change_clicked), &rd);
959 g_signal_connect (model, "row-inserted",
960 G_CALLBACK (select_something), &rd);
964 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), homogeneous_types);
966 /* Set up the Old & New Values subdialog */
968 rd.string_button = get_widget_assert (builder, "checkbutton1");
969 rd.width_entry = get_widget_assert (builder, "spinbutton1");
971 rd.convert_button = get_widget_assert (builder, "checkbutton2");
973 rd.ov_range_lower_entry = get_widget_assert (builder, "entry5");
974 rd.ov_range_upper_entry = get_widget_assert (builder, "entry3");
975 rd.ov_low_up_entry = get_widget_assert (builder, "entry6");
976 rd.ov_high_down_entry = get_widget_assert (builder, "entry7");
978 rd.new_value_entry = get_widget_assert (builder, "entry1");
979 rd.ov_value_entry = get_widget_assert (builder, "entry2");
981 rd.toggle[BUTTON_NEW_VALUE] = get_widget_assert (builder, "radiobutton1");
982 rd.toggle[BUTTON_NEW_SYSMIS] = get_widget_assert (builder, "radiobutton2");
983 rd.toggle[BUTTON_NEW_COPY] = get_widget_assert (builder, "radiobutton3");
984 rd.toggle[BUTTON_OLD_VALUE] = get_widget_assert (builder, "radiobutton4");
985 rd.toggle[BUTTON_OLD_SYSMIS] = get_widget_assert (builder, "radiobutton6");
986 rd.toggle[BUTTON_OLD_MISSING]= get_widget_assert (builder, "radiobutton7");
987 rd.toggle[BUTTON_OLD_RANGE] = get_widget_assert (builder, "radiobutton8");
988 rd.toggle[BUTTON_OLD_LOW_UP] = get_widget_assert (builder, "radiobutton10");
989 rd.toggle[BUTTON_OLD_HIGH_DOWN] = get_widget_assert (builder, "radiobutton5");
990 rd.toggle[BUTTON_OLD_ELSE] = get_widget_assert (builder, "radiobutton11");
992 rd.new_copy_label = get_widget_assert (builder, "label3");
993 rd.strings_box = get_widget_assert (builder, "table3");
995 rd.old_and_new_dialog =
996 PSPPIRE_DIALOG (get_widget_assert (builder, "old-new-values-dialog"));
998 gtk_window_set_transient_for (GTK_WINDOW (rd.old_and_new_dialog),
1001 rd.acr = PSPPIRE_ACR (get_widget_assert (builder, "psppire-acr1"));
1003 g_signal_connect_swapped (rd.toggle[BUTTON_NEW_VALUE], "toggled",
1004 G_CALLBACK (set_acr), &rd);
1006 g_signal_connect_swapped (rd.new_value_entry, "changed",
1007 G_CALLBACK (set_acr), &rd);
1010 GtkTreeSelection *sel;
1011 /* Remove the ACR's default column. We don't like it */
1012 GtkTreeViewColumn *column = gtk_tree_view_get_column (rd.acr->tv, 0);
1013 gtk_tree_view_remove_column (rd.acr->tv, column);
1017 gtk_tree_view_column_new_with_attributes (_("Old"),
1018 gtk_cell_renderer_text_new (),
1022 gtk_tree_view_append_column (rd.acr->tv, column);
1025 gtk_tree_view_column_new_with_attributes (_("New"),
1026 gtk_cell_renderer_text_new (),
1030 gtk_tree_view_append_column (rd.acr->tv, column);
1031 g_object_set (rd.acr->tv, "headers-visible", TRUE, NULL);
1034 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd.acr->tv));
1035 g_signal_connect (sel, "changed",
1036 G_CALLBACK (on_acr_selection_change), &rd);
1040 g_signal_connect_swapped (oldandnew, "clicked",
1041 G_CALLBACK (run_old_and_new_dialog), &rd);
1044 g_signal_connect (rd.toggle[BUTTON_NEW_VALUE], "toggled",
1045 G_CALLBACK (toggle_sensitivity), rd.new_value_entry);
1047 g_signal_connect (rd.toggle[BUTTON_OLD_VALUE], "toggled",
1048 G_CALLBACK (toggle_sensitivity), rd.ov_value_entry);
1050 g_signal_connect (rd.toggle[BUTTON_OLD_RANGE], "toggled",
1051 G_CALLBACK (toggle_sensitivity),
1052 get_widget_assert (builder, "entry3"));
1054 g_signal_connect (rd.toggle[BUTTON_OLD_RANGE], "toggled",
1055 G_CALLBACK (toggle_sensitivity),
1056 get_widget_assert (builder, "entry5"));
1058 g_signal_connect (rd.toggle[BUTTON_OLD_LOW_UP], "toggled",
1059 G_CALLBACK (toggle_sensitivity), rd.ov_low_up_entry);
1061 g_signal_connect (rd.toggle[BUTTON_OLD_HIGH_DOWN], "toggled",
1062 G_CALLBACK (toggle_sensitivity), rd.ov_high_down_entry);
1064 g_signal_connect (rd.string_button, "toggled",
1065 G_CALLBACK (toggle_sensitivity), rd.width_entry);
1067 g_signal_connect (rd.string_button, "toggled",
1068 G_CALLBACK (on_string_toggled), &rd);
1070 g_signal_connect (rd.convert_button, "toggled",
1071 G_CALLBACK (on_convert_toggled), &rd);
1073 g_signal_connect_swapped (rd.old_and_new_dialog, "show",
1074 G_CALLBACK (on_old_new_show), &rd);
1077 g_signal_connect (rd.dialog, "refresh", G_CALLBACK (refresh), &rd);
1080 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (rd.dialog),
1081 dialog_state_valid, &rd);
1083 response = psppire_dialog_run (PSPPIRE_DIALOG (rd.dialog));
1087 case GTK_RESPONSE_OK:
1089 gchar *syntax = generate_syntax (&rd);
1091 struct getl_interface *sss = create_syntax_string_source (syntax);
1092 execute_syntax (sss);
1097 case PSPPIRE_RESPONSE_PASTE:
1099 gchar *syntax = generate_syntax (&rd);
1100 paste_syntax_to_window (syntax);
1109 g_hash_table_destroy (rd.varmap);
1111 gtk_list_store_clear (GTK_LIST_STORE (rd.value_map));
1112 g_object_unref (rd.value_map);
1114 g_object_unref (builder);
1117 /* Initialise VAL to reflect the current status of RD */
1119 set_old_value (GValue *val, const struct recode_dialog *rd)
1121 const gchar *text = NULL;
1122 struct old_value ov;
1123 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1124 (rd->toggle [BUTTON_OLD_VALUE])))
1126 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_value_entry));
1127 if ( rd->input_var_is_string )
1129 ov.type = OV_STRING;
1130 ov.v.s = g_strdup (text);
1134 ov.type = OV_NUMERIC;
1135 ov.v.v = g_strtod (text, 0);
1138 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1139 (rd->toggle [BUTTON_OLD_MISSING])))
1141 ov.type = OV_MISSING;
1143 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1144 (rd->toggle [BUTTON_OLD_SYSMIS])))
1146 ov.type = OV_SYSMIS;
1148 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1149 (rd->toggle [BUTTON_OLD_ELSE])))
1153 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1154 (rd->toggle [BUTTON_OLD_RANGE])))
1157 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_range_lower_entry));
1160 ov.v.range[0] = g_strtod (text, 0);
1162 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_range_upper_entry));
1163 ov.v.range[1] = g_strtod (text, 0);
1165 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1166 (rd->toggle [BUTTON_OLD_LOW_UP])))
1169 gtk_entry_get_text (GTK_ENTRY (rd->ov_low_up_entry));
1171 ov.type = OV_LOW_UP;
1172 ov.v.range[1] = g_strtod (text, 0);
1174 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1175 (rd->toggle [BUTTON_OLD_HIGH_DOWN])))
1178 gtk_entry_get_text (GTK_ENTRY (rd->ov_high_down_entry));
1180 ov.type = OV_HIGH_DOWN;
1181 ov.v.range[0] = g_strtod (text, 0);
1186 g_value_init (val, old_value_get_type ());
1187 g_value_set_boxed (val, &ov);
1193 /* Initialse VAL to reflect the current status of RD */
1195 set_new_value (GValue *val, const struct recode_dialog *rd)
1197 const gchar *text = NULL;
1198 struct new_value nv;
1200 if ( gtk_toggle_button_get_active
1201 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE])))
1203 text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
1205 nv.type = NV_NUMERIC;
1207 (! rd->different && rd->input_var_is_string) ||
1209 gtk_toggle_button_get_active
1210 (GTK_TOGGLE_BUTTON (rd->string_button)))
1213 nv.type = NV_STRING;
1216 if ( nv.type == NV_STRING )
1217 nv.v.s = g_strdup (text);
1219 nv.v.v = g_strtod (text, 0);
1221 else if ( gtk_toggle_button_get_active
1222 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY])))
1227 else if ( gtk_toggle_button_get_active
1228 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS])))
1230 nv.type = NV_SYSMIS;
1235 g_value_init (val, new_value_get_type ());
1236 g_value_set_boxed (val, &nv);
1242 /* A function to set a value in a column in the ACR */
1244 set_value (gint col, GValue *val, gpointer data)
1246 struct recode_dialog *rd = data;
1251 set_old_value (val, rd);
1254 set_new_value (val, rd);
1264 run_old_and_new_dialog (struct recode_dialog *rd)
1267 GtkListStore *local_store = clone_list_store (rd->value_map);
1269 psppire_acr_set_model (rd->acr, local_store);
1270 psppire_acr_set_get_value_func (rd->acr, set_value, rd);
1272 gtk_window_set_title (GTK_WINDOW (rd->old_and_new_dialog),
1274 ? _("Recode into Different Variables: Old and New Values ")
1275 : _("Recode into Same Variables: Old and New Values")
1280 /* Find the type of the first variable (it's invariant that
1281 all variables are of the same type) */
1282 const struct variable *v;
1284 GtkTreeModel *model =
1285 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
1287 gboolean not_empty = gtk_tree_model_get_iter_first (model, &iter);
1289 g_return_if_fail (not_empty);
1291 gtk_tree_model_get (model, &iter, 0, &v, -1);
1293 rd->input_var_is_string = var_is_alpha (v);
1295 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_SYSMIS],
1296 var_is_numeric (v));
1297 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_RANGE],
1298 var_is_numeric (v));
1299 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_LOW_UP],
1300 var_is_numeric (v));
1301 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_HIGH_DOWN],
1302 var_is_numeric (v));
1303 gtk_widget_set_sensitive (rd->toggle [BUTTON_NEW_SYSMIS],
1304 var_is_numeric (v));
1306 gtk_widget_set_sensitive (rd->convert_button, var_is_alpha (v));
1310 response = psppire_dialog_run (rd->old_and_new_dialog);
1311 psppire_acr_set_model (rd->acr, NULL);
1314 if ( response == PSPPIRE_RESPONSE_CONTINUE )
1316 g_object_unref (rd->value_map);
1317 rd->value_map = clone_list_store (local_store);
1320 g_object_unref (local_store);
1323 psppire_dialog_notify_change (PSPPIRE_DIALOG (rd->dialog));
1327 /* Generate a syntax fragment for NV and append it to STR */
1329 new_value_append_syntax (GString *str, const struct new_value *nv)
1334 g_string_append_printf (str, "%g", nv->v.v);
1338 struct string ds = DS_EMPTY_INITIALIZER;
1339 syntax_gen_string (&ds, ss_cstr (nv->v.s));
1340 g_string_append (str, ds_cstr (&ds));
1345 g_string_append (str, "COPY");
1348 g_string_append (str, "SYSMIS");
1351 /* Shouldn't ever happen */
1352 g_warning ("Invalid type in new recode value");
1353 g_string_append (str, "???");
1359 /* Generate a syntax fragment for NV and append it to STR */
1361 old_value_append_syntax (GString *str, const struct old_value *ov)
1366 g_string_append_printf (str, "%g", ov->v.v);
1370 struct string ds = DS_EMPTY_INITIALIZER;
1371 syntax_gen_string (&ds, ss_cstr (ov->v.s));
1372 g_string_append (str, ds_cstr (&ds));
1377 g_string_append (str, "MISSING");
1380 g_string_append (str, "SYSMIS");
1383 g_string_append (str, "ELSE");
1386 g_string_append_printf (str, "%g THRU %g",
1391 g_string_append_printf (str, "LOWEST THRU %g",
1395 g_string_append_printf (str, "%g THRU HIGHEST",
1399 g_warning ("Invalid type in old recode value");
1400 g_string_append (str, "???");
1408 generate_syntax (const struct recode_dialog *rd)
1414 GString *str = g_string_sized_new (100);
1416 /* Declare new string variables if applicable */
1417 if ( rd->different &&
1418 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->string_button)))
1420 GHashTableIter iter;
1422 struct variable *var = NULL;
1423 struct nlp *nlp = NULL;
1425 g_hash_table_iter_init (&iter, rd->varmap);
1426 while (g_hash_table_iter_next (&iter, (void**) &var, (void**) &nlp))
1428 g_string_append (str, "\nSTRING ");
1429 g_string_append (str, nlp->name);
1430 g_string_append_printf (str, " (A%d).",
1432 gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->width_entry) )
1437 g_string_append (str, "\nRECODE ");
1439 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variable_treeview), 0, str);
1441 g_string_append (str, "\n\t");
1443 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->convert_button)))
1445 g_string_append (str, "(CONVERT) ");
1448 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->value_map),
1451 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->value_map), &iter))
1453 GValue ov_value = {0};
1454 GValue nv_value = {0};
1455 struct old_value *ov;
1456 struct new_value *nv;
1457 gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
1458 COL_VALUE_OLD, &ov_value);
1460 gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
1461 COL_VALUE_NEW, &nv_value);
1463 ov = g_value_get_boxed (&ov_value);
1464 nv = g_value_get_boxed (&nv_value);
1466 g_string_append (str, "(");
1468 old_value_append_syntax (str, ov);
1469 g_string_append (str, " = ");
1470 new_value_append_syntax (str, nv);
1472 g_string_append (str, ") ");
1473 g_value_unset (&ov_value);
1474 g_value_unset (&nv_value);
1478 if ( rd->different )
1482 g_string_append (str, "\n\tINTO ");
1484 for (ok = psppire_var_view_get_iter_first (PSPPIRE_VAR_VIEW (rd->variable_treeview), &iter);
1486 ok = psppire_var_view_get_iter_next (PSPPIRE_VAR_VIEW (rd->variable_treeview), &iter))
1488 struct nlp *nlp = NULL;
1489 const struct variable *var = psppire_var_view_get_variable (PSPPIRE_VAR_VIEW (rd->variable_treeview), 0, &iter);
1491 nlp = g_hash_table_lookup (rd->varmap, var);
1493 g_string_append (str, nlp->name);
1494 g_string_append (str, " ");
1498 g_string_append (str, ".");
1500 /* If applicable, set labels for the new variables. */
1501 if ( rd->different )
1503 GHashTableIter iter;
1505 struct variable *var = NULL;
1506 struct nlp *nlp = NULL;
1508 g_hash_table_iter_init (&iter, rd->varmap);
1509 while (g_hash_table_iter_next (&iter, (void**) &var, (void**) &nlp))
1512 g_string_append_printf (str, "\nVARIABLE LABELS %s %s.",
1513 nlp->name, nlp->label);
1517 g_string_append (str, "\nEXECUTE.\n");
1522 g_string_free (str, FALSE);