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"
32 #include <language/syntax-string-source.h>
33 #include <ui/gui/psppire-data-window.h>
34 #include <ui/gui/dialog-common.h>
35 #include <ui/gui/dict-display.h>
36 #include <ui/gui/helper.h>
37 #include <ui/gui/psppire-dialog.h>
38 #include <ui/gui/psppire-var-store.h>
40 #include <ui/syntax-gen.h>
42 #include "psppire-acr.h"
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
49 /* Define a boxed type to represent a value which is a candidate
50 to replace an existing value */
63 enum new_value_type type;
71 static struct new_value *
72 new_value_copy (struct new_value *nv)
74 struct new_value *copy = g_memdup (nv, sizeof (*copy));
76 if ( nv->type == NV_STRING )
77 copy->v.s = xstrdup (nv->v.s);
84 new_value_free (struct new_value *nv)
86 if ( nv->type == NV_STRING )
94 new_value_to_string (const GValue *src, GValue *dest)
96 const struct new_value *nv = g_value_get_boxed (src);
104 gchar *text = g_strdup_printf ("%g", nv->v.v);
105 g_value_set_string (dest, text);
110 g_value_set_string (dest, nv->v.s);
113 g_value_set_string (dest, "COPY");
116 g_value_set_string (dest, "SYSMIS");
119 /* Shouldn't ever happen */
120 g_warning ("Invalid type in new recode value");
121 g_value_set_string (dest, "???");
127 new_value_get_type (void)
133 t = g_boxed_type_register_static ("psppire-recode-new-values",
134 (GBoxedCopyFunc) new_value_copy,
135 (GBoxedFreeFunc) new_value_free);
137 g_value_register_transform_func (t, G_TYPE_STRING,
138 new_value_to_string);
148 /* A boxed type representing a value, or a range of values which may
149 potentially be replaced by something */
165 enum old_value_type type;
174 static struct old_value *
175 old_value_copy (struct old_value *ov)
177 struct old_value *copy = g_memdup (ov, sizeof (*copy));
179 if ( ov->type == OV_STRING )
180 copy->v.s = g_strdup (ov->v.s);
187 old_value_free (struct old_value *ov)
189 if (ov->type == OV_STRING)
195 old_value_to_string (const GValue *src, GValue *dest)
197 const struct old_value *ov = g_value_get_boxed (src);
203 gchar *text = g_strdup_printf ("%g", ov->v.v);
204 g_value_set_string (dest, text);
209 g_value_set_string (dest, ov->v.s);
212 g_value_set_string (dest, "MISSING");
215 g_value_set_string (dest, "SYSMIS");
218 g_value_set_string (dest, "ELSE");
223 char en_dash[6] = {0,0,0,0,0,0};
225 g_unichar_to_utf8 (0x2013, en_dash);
227 text = g_strdup_printf ("%g %s %g",
231 g_value_set_string (dest, text);
238 char en_dash[6] = {0,0,0,0,0,0};
240 g_unichar_to_utf8 (0x2013, en_dash);
242 text = g_strdup_printf ("LOWEST %s %g",
246 g_value_set_string (dest, text);
253 char en_dash[6] = {0,0,0,0,0,0};
255 g_unichar_to_utf8 (0x2013, en_dash);
257 text = g_strdup_printf ("%g %s HIGHEST",
261 g_value_set_string (dest, text);
266 g_warning ("Invalid type in old recode value");
267 g_value_set_string (dest, "???");
273 old_value_get_type (void)
279 t = g_boxed_type_register_static ("psppire-recode-old-values",
280 (GBoxedCopyFunc) old_value_copy,
281 (GBoxedFreeFunc) old_value_free);
283 g_value_register_transform_func (t, G_TYPE_STRING,
284 old_value_to_string);
302 BUTTON_OLD_HIGH_DOWN,
312 PsppireDialog *old_and_new_dialog;
314 GtkWidget *dict_treeview;
315 GtkWidget *variable_treeview;
316 GtkWidget *toggle[n_BUTTONS];
318 GtkWidget *strings_box;
319 GtkWidget *convert_button;
320 GtkWidget *new_copy_label;
322 GtkWidget *ov_value_entry;
323 GtkWidget *new_value_entry;
325 GtkWidget *ov_range_lower_entry;
326 GtkWidget *ov_range_upper_entry;
327 GtkWidget *ov_low_up_entry;
328 GtkWidget *ov_high_down_entry;
330 GtkListStore *value_map;
332 /* Indicates that the INTO {new variables} form of the dialog
338 gboolean input_var_is_string;
340 GtkListStore *var_map;
341 GtkWidget *new_name_entry;
342 GtkWidget *new_label_entry;
343 GtkWidget *change_button;
345 GtkWidget *string_button;
346 GtkWidget *width_entry;
350 static void run_old_and_new_dialog (struct recode_dialog *rd);
353 refresh (PsppireDialog *dialog, struct recode_dialog *rd)
355 gtk_widget_set_sensitive (rd->change_button, FALSE);
356 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
357 gtk_widget_set_sensitive (rd->new_label_entry, FALSE);
361 gtk_list_store_clear (GTK_LIST_STORE (rd->var_map));
365 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
367 gtk_list_store_clear (GTK_LIST_STORE (vars));
370 gtk_list_store_clear (GTK_LIST_STORE (rd->value_map));
373 static char * generate_syntax (const struct recode_dialog *rd);
388 /* Dialog is valid iff at least one variable has been selected,
389 AND the list of mappings is not empty.
392 dialog_state_valid (gpointer data)
394 GtkTreeIter not_used;
395 struct recode_dialog *rd = data;
397 if ( ! rd->value_map )
400 if ( ! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->value_map),
410 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->var_map),
413 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->var_map),
418 gtk_tree_model_get (GTK_TREE_MODEL (rd->var_map), &iter,
419 COL_NEW_NAME, &name, -1);
430 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
432 if ( !gtk_tree_model_get_iter_first (vars, ¬_used))
441 on_old_new_show (struct recode_dialog *rd)
443 gtk_toggle_button_set_active
444 (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_OLD_VALUE]), TRUE);
446 g_signal_emit_by_name (rd->toggle[BUTTON_OLD_VALUE], "toggled");
448 gtk_toggle_button_set_active
449 (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_NEW_VALUE]), TRUE);
451 g_signal_emit_by_name (rd->toggle[BUTTON_NEW_VALUE], "toggled");
453 g_object_set (rd->toggle[BUTTON_NEW_COPY],
454 "visible", rd->different, NULL);
456 g_object_set (rd->new_copy_label,
457 "visible", rd->different, NULL);
459 g_object_set (rd->strings_box,
460 "visible", rd->different, NULL);
463 /* Sets the sensitivity of TARGET dependent upon the active status
466 toggle_sensitivity (GtkToggleButton *button, GtkWidget *target)
468 gboolean state = gtk_toggle_button_get_active (button);
470 /* g_print ("%s Setting %p (%s) to %d because of %p\n",
471 __FUNCTION__, target, gtk_widget_get_name (target), state, button); */
473 gtk_widget_set_sensitive (target, state);
476 static void recode_dialog (PsppireDataWindow *de, gboolean diff);
479 /* Pops up the Recode Same version of the dialog box */
481 recode_same_dialog (GObject *o, gpointer data)
483 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
485 recode_dialog (de, FALSE);
488 /* Pops up the Recode Different version of the dialog box */
490 recode_different_dialog (GObject *o, gpointer data)
492 PsppireDataWindow *de = PSPPIRE_DATA_WINDOW (data);
494 recode_dialog (de, TRUE);
498 render_new_var_name (GtkTreeViewColumn *tree_column,
499 GtkCellRenderer *cell,
500 GtkTreeModel *tree_model,
504 gchar *new_var_name = NULL;
506 gtk_tree_model_get (tree_model, iter, COL_NEW_NAME, &new_var_name, -1);
508 g_object_set (cell, "text", new_var_name, NULL);
510 g_free (new_var_name);
514 /* This might need to be changed to something less naive.
515 In particular, what happends with dates, etc?
518 num_to_string (gdouble x)
520 return g_strdup_printf ("%g", x);
523 /* Callback which gets called when a new row is selected
524 in the acr's variable treeview.
525 We use if to set the togglebuttons and entries to correspond to the
529 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
531 struct recode_dialog *rd = data;
532 GtkTreeModel *model = NULL;
535 GValue ov_value = {0};
536 GValue nv_value = {0};
537 struct old_value *ov = NULL;
538 struct new_value *nv = NULL;
540 if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
544 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
545 COL_VALUE_OLD, &ov_value);
547 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
548 COL_VALUE_NEW, &nv_value);
550 ov = g_value_get_boxed (&ov_value);
551 nv = g_value_get_boxed (&nv_value);
559 gchar *str = num_to_string (nv->v.v);
561 gtk_toggle_button_set_active
562 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE]), TRUE);
564 gtk_entry_set_text (GTK_ENTRY (rd->new_value_entry), str);
569 gtk_toggle_button_set_active
570 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE]), TRUE);
572 gtk_entry_set_text (GTK_ENTRY (rd->new_value_entry), nv->v.s);
575 gtk_toggle_button_set_active
576 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS]), TRUE);
580 gtk_toggle_button_set_active
581 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY]), TRUE);
585 g_warning ("Invalid new value type");
589 g_value_unset (&nv_value);
597 gtk_toggle_button_set_active
598 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_VALUE]), TRUE);
600 gtk_entry_set_text (GTK_ENTRY (rd->ov_value_entry), ov->v.s);
607 gtk_toggle_button_set_active
608 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_VALUE]), TRUE);
610 str = num_to_string (ov->v.v);
612 gtk_entry_set_text (GTK_ENTRY (rd->ov_value_entry), str);
619 gtk_toggle_button_set_active
620 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_SYSMIS]), TRUE);
624 gtk_toggle_button_set_active
625 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_MISSING]), TRUE);
632 gtk_toggle_button_set_active
633 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_RANGE]), TRUE);
635 str = num_to_string (ov->v.range[0]);
637 gtk_entry_set_text (GTK_ENTRY (rd->ov_range_lower_entry), str);
642 str = num_to_string (ov->v.range[1]);
644 gtk_entry_set_text (GTK_ENTRY (rd->ov_range_upper_entry), str);
652 gchar *str = num_to_string (ov->v.range[1]);
654 gtk_toggle_button_set_active
655 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_LOW_UP]), TRUE);
657 gtk_entry_set_text (GTK_ENTRY (rd->ov_low_up_entry), str);
665 gchar *str = num_to_string (ov->v.range[0]);
667 gtk_toggle_button_set_active
668 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_HIGH_DOWN]), TRUE);
670 gtk_entry_set_text (GTK_ENTRY (rd->ov_high_down_entry), str);
677 gtk_toggle_button_set_active
678 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_OLD_ELSE]), TRUE);
682 g_warning ("Unknown old value type");
685 g_value_unset (&ov_value);
689 /* Callback which gets called when a new row is selected
690 in the variable treeview.
691 It sets the name and label entry widgets to reflect the
692 currently selected row.
695 on_selection_change (GtkTreeSelection *selection, gpointer data)
697 struct recode_dialog *rd = data;
698 GtkTreeModel *model = GTK_TREE_MODEL (rd->var_map);
700 GList *rows = gtk_tree_selection_get_selected_rows (selection, &model);
702 if ( rows && !rows->next)
704 /* Exactly one row is selected */
711 gtk_widget_set_sensitive (rd->change_button, TRUE);
712 gtk_widget_set_sensitive (rd->new_name_entry, TRUE);
713 gtk_widget_set_sensitive (rd->new_label_entry, TRUE);
715 ok = gtk_tree_model_get_iter (model, &iter, (GtkTreePath*) rows->data);
717 gtk_tree_model_get (GTK_TREE_MODEL (rd->var_map), &iter,
719 COL_NEW_LABEL, &label,
722 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), name ? name : "");
723 gtk_entry_set_text (GTK_ENTRY (rd->new_label_entry), label ? label : "");
730 gtk_widget_set_sensitive (rd->change_button, FALSE);
731 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
732 gtk_widget_set_sensitive (rd->new_label_entry, FALSE);
734 gtk_entry_set_text (GTK_ENTRY (rd->new_name_entry), "");
735 gtk_entry_set_text (GTK_ENTRY (rd->new_label_entry), "");
738 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
743 on_string_toggled (GtkToggleButton *b, struct recode_dialog *rd)
746 if (! rd->input_var_is_string )
749 active = gtk_toggle_button_get_active (b);
750 gtk_widget_set_sensitive (rd->convert_button, !active);
755 on_convert_toggled (GtkToggleButton *b, struct recode_dialog *rd)
759 g_return_if_fail (rd->input_var_is_string);
761 active = gtk_toggle_button_get_active (b);
762 gtk_widget_set_sensitive (rd->string_button, !active);
767 on_change_clicked (GObject *obj, gpointer data)
769 struct recode_dialog *rd = data;
770 GtkTreeModel *model = GTK_TREE_MODEL (rd->var_map);
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));
786 gtk_tree_model_get_iter (model, &iter, rows->data);
788 gtk_list_store_set (rd->var_map, &iter,
789 COL_NEW_NAME, dest_var_name,
790 COL_NEW_LABEL, dest_var_label,
793 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
798 /* If there's nothing selected in the variable treeview,
799 then automatically select the first item */
801 select_something (GtkTreeModel *treemodel,
806 struct recode_dialog *rd = data;
807 GtkTreeSelection *sel;
809 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd->variable_treeview));
811 if ( gtk_tree_selection_count_selected_rows (sel) < 1)
815 gtk_tree_model_get_iter_first (treemodel, &iter);
817 gtk_tree_selection_select_iter (sel, &iter);
822 /* Callback for the new_value_entry and new_value_togglebutton widgets.
823 It's used to enable/disable the acr. */
825 set_acr (struct recode_dialog *rd)
829 if ( !gtk_toggle_button_get_active
830 (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_NEW_VALUE])))
832 psppire_acr_set_enabled (rd->acr, TRUE);
836 text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
838 psppire_acr_set_enabled (rd->acr, !g_str_equal (text, ""));
842 recode_dialog (PsppireDataWindow *de, gboolean diff)
846 struct recode_dialog rd;
848 GtkBuilder *builder = builder_new ("recode.ui");
850 GtkWidget *selector = get_widget_assert (builder, "psppire-selector1");
852 GtkWidget *oldandnew = get_widget_assert (builder, "button1");
855 GtkWidget *output_variable_box = get_widget_assert (builder,"frame4");
858 PsppireVarStore *vs = NULL;
860 g_object_get (de->data_editor, "var-store", &vs, NULL);
862 rd.change_button = get_widget_assert (builder, "change-button");
864 rd.dialog = get_widget_assert (builder, "recode-dialog");
865 rd.dict_treeview = get_widget_assert (builder, "treeview1");
866 rd.variable_treeview = get_widget_assert (builder, "treeview2");
867 rd.new_name_entry = get_widget_assert (builder, "dest-name-entry");
868 rd.new_label_entry = get_widget_assert (builder, "dest-label-entry");
872 rd.value_map = gtk_list_store_new (2,
873 old_value_get_type (),
874 new_value_get_type ()
877 g_object_set (output_variable_box, "visible", diff, NULL);
880 gtk_window_set_title (GTK_WINDOW (rd.dialog),
881 _("Recode into Different Variables"));
883 gtk_window_set_title (GTK_WINDOW (rd.dialog),
884 _("Recode into Same Variables"));
888 gtk_window_set_transient_for (GTK_WINDOW (rd.dialog), GTK_WINDOW (de));
891 g_object_set (rd.dict_treeview, "dictionary", vs->dict, NULL);
893 if ( ! rd.different )
895 set_dest_model (GTK_TREE_VIEW (rd.variable_treeview), vs->dict);
899 GtkTreeSelection *sel;
900 GtkTreeViewColumn *col;
901 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
903 rd.var_map = gtk_list_store_new (n_COL_VARS, G_TYPE_INT,
909 gtk_tree_view_set_model (GTK_TREE_VIEW (rd.variable_treeview),
910 GTK_TREE_MODEL (rd.var_map));
912 col = gtk_tree_view_column_new_with_attributes (_("Old"),
917 gtk_tree_view_column_set_cell_data_func (col, renderer,
922 gtk_tree_view_append_column (GTK_TREE_VIEW (rd.variable_treeview), col);
925 renderer = gtk_cell_renderer_text_new ();
927 col = gtk_tree_view_column_new_with_attributes (_("New"),
932 gtk_tree_view_column_set_cell_data_func (col, renderer,
937 gtk_tree_view_append_column (GTK_TREE_VIEW (rd.variable_treeview), col);
939 g_object_set (rd.variable_treeview, "headers-visible", TRUE, NULL);
941 g_signal_connect (rd.change_button, "clicked",
942 G_CALLBACK (on_change_clicked), &rd);
944 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd.variable_treeview));
945 g_signal_connect (sel, "changed",
946 G_CALLBACK (on_selection_change), &rd);
948 g_signal_connect (rd.var_map, "row-inserted",
949 G_CALLBACK (select_something), &rd);
954 psppire_selector_set_subjects (PSPPIRE_SELECTOR (selector),
956 rd.variable_treeview,
957 insert_source_row_into_tree_view,
961 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), homogeneous_types);
963 /* Set up the Old & New Values subdialog */
965 rd.string_button = get_widget_assert (builder, "checkbutton1");
966 rd.width_entry = get_widget_assert (builder, "spinbutton1");
968 rd.convert_button = get_widget_assert (builder, "checkbutton2");
970 rd.ov_range_lower_entry = get_widget_assert (builder, "entry5");
971 rd.ov_range_upper_entry = get_widget_assert (builder, "entry3");
972 rd.ov_low_up_entry = get_widget_assert (builder, "entry6");
973 rd.ov_high_down_entry = get_widget_assert (builder, "entry7");
975 rd.new_value_entry = get_widget_assert (builder, "entry1");
976 rd.ov_value_entry = get_widget_assert (builder, "entry2");
978 rd.toggle[BUTTON_NEW_VALUE] = get_widget_assert (builder, "radiobutton1");
979 rd.toggle[BUTTON_NEW_SYSMIS] = get_widget_assert (builder, "radiobutton2");
980 rd.toggle[BUTTON_NEW_COPY] = get_widget_assert (builder, "radiobutton3");
981 rd.toggle[BUTTON_OLD_VALUE] = get_widget_assert (builder, "radiobutton4");
982 rd.toggle[BUTTON_OLD_SYSMIS] = get_widget_assert (builder, "radiobutton6");
983 rd.toggle[BUTTON_OLD_MISSING]= get_widget_assert (builder, "radiobutton7");
984 rd.toggle[BUTTON_OLD_RANGE] = get_widget_assert (builder, "radiobutton8");
985 rd.toggle[BUTTON_OLD_LOW_UP] = get_widget_assert (builder, "radiobutton10");
986 rd.toggle[BUTTON_OLD_HIGH_DOWN] = get_widget_assert (builder, "radiobutton5");
987 rd.toggle[BUTTON_OLD_ELSE] = get_widget_assert (builder, "radiobutton11");
989 rd.new_copy_label = get_widget_assert (builder, "label3");
990 rd.strings_box = get_widget_assert (builder, "table3");
992 rd.old_and_new_dialog =
993 PSPPIRE_DIALOG (get_widget_assert (builder, "old-new-values-dialog"));
995 gtk_window_set_transient_for (GTK_WINDOW (rd.old_and_new_dialog),
998 rd.acr = PSPPIRE_ACR (get_widget_assert (builder, "psppire-acr1"));
1000 g_signal_connect_swapped (rd.toggle[BUTTON_NEW_VALUE], "toggled",
1001 G_CALLBACK (set_acr), &rd);
1003 g_signal_connect_swapped (rd.new_value_entry, "changed",
1004 G_CALLBACK (set_acr), &rd);
1007 GtkTreeSelection *sel;
1008 /* Remove the ACR's default column. We don't like it */
1009 GtkTreeViewColumn *column = gtk_tree_view_get_column (rd.acr->tv, 0);
1010 gtk_tree_view_remove_column (rd.acr->tv, column);
1014 gtk_tree_view_column_new_with_attributes (_("Old"),
1015 gtk_cell_renderer_text_new (),
1019 gtk_tree_view_append_column (rd.acr->tv, column);
1022 gtk_tree_view_column_new_with_attributes (_("New"),
1023 gtk_cell_renderer_text_new (),
1027 gtk_tree_view_append_column (rd.acr->tv, column);
1028 g_object_set (rd.acr->tv, "headers-visible", TRUE, NULL);
1031 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (rd.acr->tv));
1032 g_signal_connect (sel, "changed",
1033 G_CALLBACK (on_acr_selection_change), &rd);
1037 g_signal_connect_swapped (oldandnew, "clicked",
1038 G_CALLBACK (run_old_and_new_dialog), &rd);
1041 g_signal_connect (rd.toggle[BUTTON_NEW_VALUE], "toggled",
1042 G_CALLBACK (toggle_sensitivity), rd.new_value_entry);
1044 g_signal_connect (rd.toggle[BUTTON_OLD_VALUE], "toggled",
1045 G_CALLBACK (toggle_sensitivity), rd.ov_value_entry);
1047 g_signal_connect (rd.toggle[BUTTON_OLD_RANGE], "toggled",
1048 G_CALLBACK (toggle_sensitivity),
1049 get_widget_assert (builder, "entry3"));
1051 g_signal_connect (rd.toggle[BUTTON_OLD_RANGE], "toggled",
1052 G_CALLBACK (toggle_sensitivity),
1053 get_widget_assert (builder, "entry5"));
1055 g_signal_connect (rd.toggle[BUTTON_OLD_LOW_UP], "toggled",
1056 G_CALLBACK (toggle_sensitivity), rd.ov_low_up_entry);
1058 g_signal_connect (rd.toggle[BUTTON_OLD_HIGH_DOWN], "toggled",
1059 G_CALLBACK (toggle_sensitivity), rd.ov_high_down_entry);
1061 g_signal_connect (rd.string_button, "toggled",
1062 G_CALLBACK (toggle_sensitivity), rd.width_entry);
1064 g_signal_connect (rd.string_button, "toggled",
1065 G_CALLBACK (on_string_toggled), &rd);
1067 g_signal_connect (rd.convert_button, "toggled",
1068 G_CALLBACK (on_convert_toggled), &rd);
1070 g_signal_connect_swapped (rd.old_and_new_dialog, "show",
1071 G_CALLBACK (on_old_new_show), &rd);
1074 g_signal_connect (rd.dialog, "refresh", G_CALLBACK (refresh), &rd);
1077 psppire_dialog_set_valid_predicate (PSPPIRE_DIALOG (rd.dialog),
1078 dialog_state_valid, &rd);
1080 response = psppire_dialog_run (PSPPIRE_DIALOG (rd.dialog));
1084 case GTK_RESPONSE_OK:
1086 gchar *syntax = generate_syntax (&rd);
1088 struct getl_interface *sss = create_syntax_string_source (syntax);
1089 execute_syntax (sss);
1094 case PSPPIRE_RESPONSE_PASTE:
1096 gchar *syntax = generate_syntax (&rd);
1097 paste_syntax_in_new_window (syntax);
1107 gtk_list_store_clear (GTK_LIST_STORE (rd.value_map));
1108 g_object_unref (rd.value_map);
1110 g_object_unref (builder);
1113 /* Initialise VAL to reflect the current status of RD */
1115 set_old_value (GValue *val, const struct recode_dialog *rd)
1117 const gchar *text = NULL;
1118 struct old_value ov;
1119 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1120 (rd->toggle [BUTTON_OLD_VALUE])))
1122 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_value_entry));
1123 if ( rd->input_var_is_string )
1125 ov.type = OV_STRING;
1126 ov.v.s = g_strdup (text);
1130 ov.type = OV_NUMERIC;
1131 ov.v.v = g_strtod (text, 0);
1134 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1135 (rd->toggle [BUTTON_OLD_MISSING])))
1137 ov.type = OV_MISSING;
1139 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1140 (rd->toggle [BUTTON_OLD_SYSMIS])))
1142 ov.type = OV_SYSMIS;
1144 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1145 (rd->toggle [BUTTON_OLD_ELSE])))
1149 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1150 (rd->toggle [BUTTON_OLD_RANGE])))
1153 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_range_lower_entry));
1156 ov.v.range[0] = g_strtod (text, 0);
1158 text = gtk_entry_get_text (GTK_ENTRY (rd->ov_range_upper_entry));
1159 ov.v.range[1] = g_strtod (text, 0);
1161 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1162 (rd->toggle [BUTTON_OLD_LOW_UP])))
1165 gtk_entry_get_text (GTK_ENTRY (rd->ov_low_up_entry));
1167 ov.type = OV_LOW_UP;
1168 ov.v.range[1] = g_strtod (text, 0);
1170 else if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
1171 (rd->toggle [BUTTON_OLD_HIGH_DOWN])))
1174 gtk_entry_get_text (GTK_ENTRY (rd->ov_high_down_entry));
1176 ov.type = OV_HIGH_DOWN;
1177 ov.v.range[0] = g_strtod (text, 0);
1182 g_value_init (val, old_value_get_type ());
1183 g_value_set_boxed (val, &ov);
1189 /* Initialse VAL to reflect the current status of RD */
1191 set_new_value (GValue *val, const struct recode_dialog *rd)
1193 const gchar *text = NULL;
1194 struct new_value nv;
1196 if ( gtk_toggle_button_get_active
1197 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE])))
1199 text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
1201 nv.type = NV_NUMERIC;
1203 (! rd->different && rd->input_var_is_string) ||
1205 gtk_toggle_button_get_active
1206 (GTK_TOGGLE_BUTTON (rd->string_button)))
1209 nv.type = NV_STRING;
1212 if ( nv.type == NV_STRING )
1213 nv.v.s = g_strdup (text);
1215 nv.v.v = g_strtod (text, 0);
1217 else if ( gtk_toggle_button_get_active
1218 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY])))
1223 else if ( gtk_toggle_button_get_active
1224 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS])))
1226 nv.type = NV_SYSMIS;
1231 g_value_init (val, new_value_get_type ());
1232 g_value_set_boxed (val, &nv);
1238 /* A function to set a value in a column in the ACR */
1240 set_value (gint col, GValue *val, gpointer data)
1242 struct recode_dialog *rd = data;
1247 set_old_value (val, rd);
1250 set_new_value (val, rd);
1260 run_old_and_new_dialog (struct recode_dialog *rd)
1263 GtkListStore *local_store = clone_list_store (rd->value_map);
1265 psppire_acr_set_model (rd->acr, local_store);
1266 psppire_acr_set_get_value_func (rd->acr, set_value, rd);
1268 gtk_window_set_title (GTK_WINDOW (rd->old_and_new_dialog),
1270 ? _("Recode into Different Variables: Old and New Values ")
1271 : _("Recode into Same Variables: Old and New Values")
1276 /* Find the type of the first variable (it's invariant that
1277 all variables are of the same type) */
1278 const struct variable *v;
1281 GtkTreeModel *model =
1282 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
1284 gboolean not_empty = gtk_tree_model_get_iter_first (model, &iter);
1286 g_return_if_fail (not_empty);
1288 gtk_tree_model_get (model, &iter, 0, &idx, -1);
1290 v = psppire_dict_get_variable (rd->dict, idx);
1292 rd->input_var_is_string = var_is_alpha (v);
1294 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_SYSMIS],
1295 var_is_numeric (v));
1296 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_RANGE],
1297 var_is_numeric (v));
1298 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_LOW_UP],
1299 var_is_numeric (v));
1300 gtk_widget_set_sensitive (rd->toggle [BUTTON_OLD_HIGH_DOWN],
1301 var_is_numeric (v));
1302 gtk_widget_set_sensitive (rd->toggle [BUTTON_NEW_SYSMIS],
1303 var_is_numeric (v));
1305 gtk_widget_set_sensitive (rd->convert_button, var_is_alpha (v));
1309 response = psppire_dialog_run (rd->old_and_new_dialog);
1310 psppire_acr_set_model (rd->acr, NULL);
1313 if ( response == PSPPIRE_RESPONSE_CONTINUE )
1315 g_object_unref (rd->value_map);
1316 rd->value_map = clone_list_store (local_store);
1319 g_object_unref (local_store);
1322 psppire_dialog_notify_change (PSPPIRE_DIALOG (rd->dialog));
1326 /* Generate a syntax fragment for NV and append it to STR */
1328 new_value_append_syntax (GString *str, const struct new_value *nv)
1333 g_string_append_printf (str, "%g", nv->v.v);
1337 struct string ds = DS_EMPTY_INITIALIZER;
1338 syntax_gen_string (&ds, ss_cstr (nv->v.s));
1339 g_string_append (str, ds_cstr (&ds));
1344 g_string_append (str, "COPY");
1347 g_string_append (str, "SYSMIS");
1350 /* Shouldn't ever happen */
1351 g_warning ("Invalid type in new recode value");
1352 g_string_append (str, "???");
1358 /* Generate a syntax fragment for NV and append it to STR */
1360 old_value_append_syntax (GString *str, const struct old_value *ov)
1365 g_string_append_printf (str, "%g", ov->v.v);
1369 struct string ds = DS_EMPTY_INITIALIZER;
1370 syntax_gen_string (&ds, ss_cstr (ov->v.s));
1371 g_string_append (str, ds_cstr (&ds));
1376 g_string_append (str, "MISSING");
1379 g_string_append (str, "SYSMIS");
1382 g_string_append (str, "ELSE");
1385 g_string_append_printf (str, "%g THRU %g",
1390 g_string_append_printf (str, "LOWEST THRU %g",
1394 g_string_append_printf (str, "%g THRU HIGHEST",
1398 g_warning ("Invalid type in old recode value");
1399 g_string_append (str, "???");
1407 generate_syntax (const struct recode_dialog *rd)
1413 GString *str = g_string_sized_new (100);
1415 /* Declare new string variables if applicable */
1416 if ( rd->different &&
1417 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->string_button)))
1422 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->var_map),
1425 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->var_map), &iter))
1429 gtk_tree_model_get (GTK_TREE_MODEL (rd->var_map), &iter,
1430 COL_NEW_NAME, &name, -1);
1432 g_string_append (str, "\nSTRING ");
1433 g_string_append (str, name);
1434 g_string_append_printf (str, " (A%d).",
1436 gtk_spin_button_get_value (GTK_SPIN_BUTTON (rd->width_entry) )
1443 g_string_append (str, "\nRECODE ");
1445 append_variable_names (str, rd->dict, GTK_TREE_VIEW (rd->variable_treeview), 0);
1447 g_string_append (str, "\n\t");
1449 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->convert_button)))
1451 g_string_append (str, "(CONVERT) ");
1454 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->value_map),
1457 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->value_map), &iter))
1459 GValue ov_value = {0};
1460 GValue nv_value = {0};
1461 struct old_value *ov;
1462 struct new_value *nv;
1463 gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
1464 COL_VALUE_OLD, &ov_value);
1466 gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
1467 COL_VALUE_NEW, &nv_value);
1469 ov = g_value_get_boxed (&ov_value);
1470 nv = g_value_get_boxed (&nv_value);
1472 g_string_append (str, "(");
1474 old_value_append_syntax (str, ov);
1475 g_string_append (str, " = ");
1476 new_value_append_syntax (str, nv);
1478 g_string_append (str, ") ");
1479 g_value_unset (&ov_value);
1480 g_value_unset (&nv_value);
1484 if ( rd->different )
1487 g_string_append (str, "\n\tINTO ");
1489 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->var_map),
1492 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->var_map), &iter))
1496 gtk_tree_model_get (GTK_TREE_MODEL (rd->var_map), &iter,
1497 COL_NEW_NAME, &name, -1);
1499 g_string_append (str, name);
1500 g_string_append (str, " ");
1506 g_string_append (str, ".");
1509 /* If applicable, set labels for the new variables. */
1510 if ( rd->different )
1514 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->var_map),
1517 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->var_map), &iter))
1520 gchar *label = NULL;
1523 gtk_tree_model_get (GTK_TREE_MODEL (rd->var_map), &iter,
1524 COL_NEW_NAME, &name,
1525 COL_NEW_LABEL, &label, -1);
1527 if ( 0 == strcmp (label, "") )
1534 ds_init_empty (&ls);
1535 syntax_gen_string (&ls, ss_cstr (label));
1538 g_string_append_printf (str, "\nVARIABLE LABELS %s %s.",
1539 name, ds_cstr (&ls));
1547 g_string_append (str, "\nEXECUTE.\n");
1552 g_string_free (str, FALSE);