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 /* Callback for the new_value_entry and new_value_togglebutton widgets.
805 It's used to enable/disable the acr. */
807 set_acr (struct recode_dialog *rd)
811 if ( !gtk_toggle_button_get_active
812 (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_NEW_VALUE])))
814 psppire_acr_set_enabled (rd->acr, TRUE);
818 text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
820 psppire_acr_set_enabled (rd->acr, !g_str_equal (text, ""));
824 render_new_var_name (GtkTreeViewColumn *tree_column,
825 GtkCellRenderer *cell,
826 GtkTreeModel *tree_model,
830 struct nlp *nlp = NULL;
831 struct recode_dialog *rd = data;
833 struct variable *var = NULL;
835 gtk_tree_model_get (tree_model, iter,
839 nlp = g_hash_table_lookup (rd->varmap, var);
842 g_object_set (cell, "text", nlp->name, NULL);
844 g_object_set (cell, "text", "", NULL);
850 recode_dialog (PsppireDataWindow *de, gboolean diff)
854 struct recode_dialog rd;
856 GtkBuilder *builder = builder_new ("recode.ui");
858 GtkWidget *selector = get_widget_assert (builder, "psppire-selector1");
860 GtkWidget *oldandnew = get_widget_assert (builder, "button1");
863 GtkWidget *output_variable_box = get_widget_assert (builder,"frame4");
865 PsppireVarStore *vs = NULL;
866 g_object_get (de->data_editor, "var-store", &vs, NULL);
868 rd.change_button = get_widget_assert (builder, "change-button");
870 rd.dialog = get_widget_assert (builder, "recode-dialog");
871 rd.dict_treeview = get_widget_assert (builder, "treeview1");
872 rd.variable_treeview = get_widget_assert (builder, "treeview2");
873 rd.new_name_entry = get_widget_assert (builder, "dest-name-entry");
874 rd.new_label_entry = get_widget_assert (builder, "dest-label-entry");
876 g_object_get (vs, "dictionary", &rd.dict, NULL);
878 rd.value_map = gtk_list_store_new (2,
879 old_value_get_type (),
880 new_value_get_type ()
883 g_object_set (output_variable_box, "visible", diff, NULL);
886 gtk_window_set_title (GTK_WINDOW (rd.dialog),
887 _("Recode into Different Variables"));
889 gtk_window_set_title (GTK_WINDOW (rd.dialog),
890 _("Recode into Same Variables"));
894 gtk_window_set_transient_for (GTK_WINDOW (rd.dialog), GTK_WINDOW (de));
896 g_object_set (rd.dict_treeview, "model", rd.dict, NULL);
900 GtkTreeSelection *sel;
902 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
904 GtkTreeViewColumn *col = gtk_tree_view_column_new_with_attributes (_("New"),
909 gtk_tree_view_column_set_cell_data_func (col, renderer,
914 gtk_tree_view_append_column (GTK_TREE_VIEW (rd.variable_treeview), col);
917 col = gtk_tree_view_get_column (GTK_TREE_VIEW (rd.variable_treeview), 0);
919 g_object_set (col, "title", _("Old"), NULL);
921 g_object_set (rd.variable_treeview, "headers-visible", TRUE, NULL);
923 rd.varmap = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, nlp_destroy);
925 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd.variable_treeview));
927 g_signal_connect (sel, "changed",
928 G_CALLBACK (on_selection_change), &rd);
930 g_signal_connect (rd.change_button, "clicked",
931 G_CALLBACK (on_change_clicked), &rd);
934 g_signal_connect (model, "row-inserted",
935 G_CALLBACK (select_something), &rd);
939 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), homogeneous_types);
941 /* Set up the Old & New Values subdialog */
943 rd.string_button = get_widget_assert (builder, "checkbutton1");
944 rd.width_entry = get_widget_assert (builder, "spinbutton1");
946 rd.convert_button = get_widget_assert (builder, "checkbutton2");
948 rd.ov_range_lower_entry = get_widget_assert (builder, "entry5");
949 rd.ov_range_upper_entry = get_widget_assert (builder, "entry3");
950 rd.ov_low_up_entry = get_widget_assert (builder, "entry6");
951 rd.ov_high_down_entry = get_widget_assert (builder, "entry7");
953 rd.new_value_entry = get_widget_assert (builder, "entry1");
954 rd.ov_value_entry = get_widget_assert (builder, "entry2");
956 rd.toggle[BUTTON_NEW_VALUE] = get_widget_assert (builder, "radiobutton1");
957 rd.toggle[BUTTON_NEW_SYSMIS] = get_widget_assert (builder, "radiobutton2");
958 rd.toggle[BUTTON_NEW_COPY] = get_widget_assert (builder, "radiobutton3");
959 rd.toggle[BUTTON_OLD_VALUE] = get_widget_assert (builder, "radiobutton4");
960 rd.toggle[BUTTON_OLD_SYSMIS] = get_widget_assert (builder, "radiobutton6");
961 rd.toggle[BUTTON_OLD_MISSING]= get_widget_assert (builder, "radiobutton7");
962 rd.toggle[BUTTON_OLD_RANGE] = get_widget_assert (builder, "radiobutton8");
963 rd.toggle[BUTTON_OLD_LOW_UP] = get_widget_assert (builder, "radiobutton10");
964 rd.toggle[BUTTON_OLD_HIGH_DOWN] = get_widget_assert (builder, "radiobutton5");
965 rd.toggle[BUTTON_OLD_ELSE] = get_widget_assert (builder, "radiobutton11");
967 rd.new_copy_label = get_widget_assert (builder, "label3");
968 rd.strings_box = get_widget_assert (builder, "table3");
970 rd.old_and_new_dialog =
971 PSPPIRE_DIALOG (get_widget_assert (builder, "old-new-values-dialog"));
973 gtk_window_set_transient_for (GTK_WINDOW (rd.old_and_new_dialog),
976 rd.acr = PSPPIRE_ACR (get_widget_assert (builder, "psppire-acr1"));
978 g_signal_connect_swapped (rd.toggle[BUTTON_NEW_VALUE], "toggled",
979 G_CALLBACK (set_acr), &rd);
981 g_signal_connect_swapped (rd.new_value_entry, "changed",
982 G_CALLBACK (set_acr), &rd);
985 GtkTreeSelection *sel;
986 /* Remove the ACR's default column. We don't like it */
987 GtkTreeViewColumn *column = gtk_tree_view_get_column (rd.acr->tv, 0);
988 gtk_tree_view_remove_column (rd.acr->tv, column);
992 gtk_tree_view_column_new_with_attributes (_("Old"),
993 gtk_cell_renderer_text_new (),
997 gtk_tree_view_append_column (rd.acr->tv, column);
1000 gtk_tree_view_column_new_with_attributes (_("New"),
1001 gtk_cell_renderer_text_new (),
1005 gtk_tree_view_append_column (rd.acr->tv, column);
1006 g_object_set (rd.acr->tv, "headers-visible", TRUE, NULL);
1009 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd.acr->tv));
1010 g_signal_connect (sel, "changed",
1011 G_CALLBACK (on_acr_selection_change), &rd);
1015 g_signal_connect_swapped (oldandnew, "clicked",
1016 G_CALLBACK (run_old_and_new_dialog), &rd);
1019 g_signal_connect (rd.toggle[BUTTON_NEW_VALUE], "toggled",
1020 G_CALLBACK (toggle_sensitivity), rd.new_value_entry);
1022 g_signal_connect (rd.toggle[BUTTON_OLD_VALUE], "toggled",
1023 G_CALLBACK (toggle_sensitivity), rd.ov_value_entry);
1025 g_signal_connect (rd.toggle[BUTTON_OLD_RANGE], "toggled",
1026 G_CALLBACK (toggle_sensitivity),
1027 get_widget_assert (builder, "entry3"));
1029 g_signal_connect (rd.toggle[BUTTON_OLD_RANGE], "toggled",
1030 G_CALLBACK (toggle_sensitivity),
1031 get_widget_assert (builder, "entry5"));
1033 g_signal_connect (rd.toggle[BUTTON_OLD_LOW_UP], "toggled",
1034 G_CALLBACK (toggle_sensitivity), rd.ov_low_up_entry);
1036 g_signal_connect (rd.toggle[BUTTON_OLD_HIGH_DOWN], "toggled",
1037 G_CALLBACK (toggle_sensitivity), rd.ov_high_down_entry);
1039 g_signal_connect (rd.string_button, "toggled",
1040 G_CALLBACK (toggle_sensitivity), rd.width_entry);
1042 g_signal_connect (rd.string_button, "toggled",
1043 G_CALLBACK (on_string_toggled), &rd);
1045 g_signal_connect (rd.convert_button, "toggled",
1046 G_CALLBACK (on_convert_toggled), &rd);
1048 g_signal_connect_swapped (rd.old_and_new_dialog, "show",
1049 G_CALLBACK (on_old_new_show), &rd);
1052 g_signal_connect (rd.dialog, "refresh", G_CALLBACK (refresh), &rd);
1055 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (rd.dialog),
1056 dialog_state_valid, &rd);
1058 response = psppire_dialog_run (PSPPIRE_DIALOG (rd.dialog));
1062 case GTK_RESPONSE_OK:
1064 gchar *syntax = generate_syntax (&rd);
1066 struct getl_interface *sss = create_syntax_string_source (syntax);
1067 execute_syntax (sss);
1072 case PSPPIRE_RESPONSE_PASTE:
1074 gchar *syntax = generate_syntax (&rd);
1075 paste_syntax_to_window (syntax);
1084 g_hash_table_destroy (rd.varmap);
1086 gtk_list_store_clear (GTK_LIST_STORE (rd.value_map));
1087 g_object_unref (rd.value_map);
1089 g_object_unref (builder);
1092 /* Initialise VAL to reflect the current status of RD */
1094 set_old_value (GValue *val, const struct recode_dialog *rd)
1096 const gchar *text = NULL;
1097 struct old_value ov;
1098 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1099 (rd->toggle [BUTTON_OLD_VALUE])))
1101 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_value_entry));
1102 if ( rd->input_var_is_string )
1104 ov.type = OV_STRING;
1105 ov.v.s = g_strdup (text);
1109 ov.type = OV_NUMERIC;
1110 ov.v.v = g_strtod (text, 0);
1113 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1114 (rd->toggle [BUTTON_OLD_MISSING])))
1116 ov.type = OV_MISSING;
1118 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1119 (rd->toggle [BUTTON_OLD_SYSMIS])))
1121 ov.type = OV_SYSMIS;
1123 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1124 (rd->toggle [BUTTON_OLD_ELSE])))
1128 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1129 (rd->toggle [BUTTON_OLD_RANGE])))
1132 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_range_lower_entry));
1135 ov.v.range[0] = g_strtod (text, 0);
1137 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_range_upper_entry));
1138 ov.v.range[1] = g_strtod (text, 0);
1140 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1141 (rd->toggle [BUTTON_OLD_LOW_UP])))
1144 gtk_entry_get_text (GTK_ENTRY (rd->ov_low_up_entry));
1146 ov.type = OV_LOW_UP;
1147 ov.v.range[1] = g_strtod (text, 0);
1149 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1150 (rd->toggle [BUTTON_OLD_HIGH_DOWN])))
1153 gtk_entry_get_text (GTK_ENTRY (rd->ov_high_down_entry));
1155 ov.type = OV_HIGH_DOWN;
1156 ov.v.range[0] = g_strtod (text, 0);
1161 g_value_init (val, old_value_get_type ());
1162 g_value_set_boxed (val, &ov);
1168 /* Initialse VAL to reflect the current status of RD */
1170 set_new_value (GValue *val, const struct recode_dialog *rd)
1172 const gchar *text = NULL;
1173 struct new_value nv;
1175 if ( gtk_toggle_button_get_active
1176 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE])))
1178 text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
1180 nv.type = NV_NUMERIC;
1182 (! rd->different && rd->input_var_is_string) ||
1184 gtk_toggle_button_get_active
1185 (GTK_TOGGLE_BUTTON (rd->string_button)))
1188 nv.type = NV_STRING;
1191 if ( nv.type == NV_STRING )
1192 nv.v.s = g_strdup (text);
1194 nv.v.v = g_strtod (text, 0);
1196 else if ( gtk_toggle_button_get_active
1197 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY])))
1202 else if ( gtk_toggle_button_get_active
1203 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS])))
1205 nv.type = NV_SYSMIS;
1210 g_value_init (val, new_value_get_type ());
1211 g_value_set_boxed (val, &nv);
1217 /* A function to set a value in a column in the ACR */
1219 set_value (gint col, GValue *val, gpointer data)
1221 struct recode_dialog *rd = data;
1226 set_old_value (val, rd);
1229 set_new_value (val, rd);
1239 run_old_and_new_dialog (struct recode_dialog *rd)
1242 GtkListStore *local_store = clone_list_store (rd->value_map);
1244 psppire_acr_set_model (rd->acr, local_store);
1245 psppire_acr_set_get_value_func (rd->acr, set_value, rd);
1247 gtk_window_set_title (GTK_WINDOW (rd->old_and_new_dialog),
1249 ? _("Recode into Different Variables: Old and New Values ")
1250 : _("Recode into Same Variables: Old and New Values")
1255 /* Find the type of the first variable (it's invariant that
1256 all variables are of the same type) */
1257 const struct variable *v;
1259 GtkTreeModel *model =
1260 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
1262 gboolean not_empty = gtk_tree_model_get_iter_first (model, &iter);
1264 g_return_if_fail (not_empty);
1266 gtk_tree_model_get (model, &iter, 0, &v, -1);
1268 rd->input_var_is_string = var_is_alpha (v);
1270 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_SYSMIS],
1271 var_is_numeric (v));
1272 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_RANGE],
1273 var_is_numeric (v));
1274 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_LOW_UP],
1275 var_is_numeric (v));
1276 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_HIGH_DOWN],
1277 var_is_numeric (v));
1278 gtk_widget_set_sensitive (rd->toggle [BUTTON_NEW_SYSMIS],
1279 var_is_numeric (v));
1281 gtk_widget_set_sensitive (rd->convert_button, var_is_alpha (v));
1285 response = psppire_dialog_run (rd->old_and_new_dialog);
1286 psppire_acr_set_model (rd->acr, NULL);
1289 if ( response == PSPPIRE_RESPONSE_CONTINUE )
1291 g_object_unref (rd->value_map);
1292 rd->value_map = clone_list_store (local_store);
1295 g_object_unref (local_store);
1298 psppire_dialog_notify_change (PSPPIRE_DIALOG (rd->dialog));
1302 /* Generate a syntax fragment for NV and append it to STR */
1304 new_value_append_syntax (GString *str, const struct new_value *nv)
1309 g_string_append_printf (str, "%g", nv->v.v);
1313 struct string ds = DS_EMPTY_INITIALIZER;
1314 syntax_gen_string (&ds, ss_cstr (nv->v.s));
1315 g_string_append (str, ds_cstr (&ds));
1320 g_string_append (str, "COPY");
1323 g_string_append (str, "SYSMIS");
1326 /* Shouldn't ever happen */
1327 g_warning ("Invalid type in new recode value");
1328 g_string_append (str, "???");
1334 /* Generate a syntax fragment for NV and append it to STR */
1336 old_value_append_syntax (GString *str, const struct old_value *ov)
1341 g_string_append_printf (str, "%g", ov->v.v);
1345 struct string ds = DS_EMPTY_INITIALIZER;
1346 syntax_gen_string (&ds, ss_cstr (ov->v.s));
1347 g_string_append (str, ds_cstr (&ds));
1352 g_string_append (str, "MISSING");
1355 g_string_append (str, "SYSMIS");
1358 g_string_append (str, "ELSE");
1361 g_string_append_printf (str, "%g THRU %g",
1366 g_string_append_printf (str, "LOWEST THRU %g",
1370 g_string_append_printf (str, "%g THRU HIGHEST",
1374 g_warning ("Invalid type in old recode value");
1375 g_string_append (str, "???");
1383 generate_syntax (const struct recode_dialog *rd)
1389 GString *str = g_string_sized_new (100);
1391 /* Declare new string variables if applicable */
1392 if ( rd->different &&
1393 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->string_button)))
1395 GHashTableIter iter;
1397 struct variable *var = NULL;
1398 struct nlp *nlp = NULL;
1400 g_hash_table_iter_init (&iter, rd->varmap);
1401 while (g_hash_table_iter_next (&iter, (void**) &var, (void**) &nlp))
1403 g_string_append (str, "\nSTRING ");
1404 g_string_append (str, nlp->name);
1405 g_string_append_printf (str, " (A%d).",
1407 gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->width_entry) )
1412 g_string_append (str, "\nRECODE ");
1414 psppire_var_view_append_names (PSPPIRE_VAR_VIEW (rd->variable_treeview), 0, str);
1416 g_string_append (str, "\n\t");
1418 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->convert_button)))
1420 g_string_append (str, "(CONVERT) ");
1423 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->value_map),
1426 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->value_map), &iter))
1428 GValue ov_value = {0};
1429 GValue nv_value = {0};
1430 struct old_value *ov;
1431 struct new_value *nv;
1432 gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
1433 COL_VALUE_OLD, &ov_value);
1435 gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
1436 COL_VALUE_NEW, &nv_value);
1438 ov = g_value_get_boxed (&ov_value);
1439 nv = g_value_get_boxed (&nv_value);
1441 g_string_append (str, "(");
1443 old_value_append_syntax (str, ov);
1444 g_string_append (str, " = ");
1445 new_value_append_syntax (str, nv);
1447 g_string_append (str, ") ");
1448 g_value_unset (&ov_value);
1449 g_value_unset (&nv_value);
1453 if ( rd->different )
1457 g_string_append (str, "\n\tINTO ");
1459 for (ok = psppire_var_view_get_iter_first (PSPPIRE_VAR_VIEW (rd->variable_treeview), &iter);
1461 ok = psppire_var_view_get_iter_next (PSPPIRE_VAR_VIEW (rd->variable_treeview), &iter))
1463 struct nlp *nlp = NULL;
1464 const struct variable *var = psppire_var_view_get_variable (PSPPIRE_VAR_VIEW (rd->variable_treeview), 0, &iter);
1466 nlp = g_hash_table_lookup (rd->varmap, var);
1468 g_string_append (str, nlp->name);
1469 g_string_append (str, " ");
1473 g_string_append (str, ".");
1475 /* If applicable, set labels for the new variables. */
1476 if ( rd->different )
1478 GHashTableIter iter;
1480 struct variable *var = NULL;
1481 struct nlp *nlp = NULL;
1483 g_hash_table_iter_init (&iter, rd->varmap);
1484 while (g_hash_table_iter_next (&iter, (void**) &var, (void**) &nlp))
1487 g_string_append_printf (str, "\nVARIABLE LABELS %s %s.",
1488 nlp->name, nlp->label);
1492 g_string_append (str, "\nEXECUTE.\n");
1497 g_string_free (str, FALSE);