1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2007, 2009, 2010, 2011, 2012, 2014, 2016 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/>. */
20 #include "psppire-var-view.h"
22 #include "psppire-dialog-action-recode.h"
23 #include "builder-wrapper.h"
24 #include <ui/gui/dialog-common.h>
26 #include "psppire-acr.h"
28 #include "psppire-selector.h"
29 #include "psppire-val-chooser.h"
32 #include <ui/syntax-gen.h>
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
39 /* This might need to be changed to something less naive.
40 In particular, what happends with dates, etc?
43 num_to_string (gdouble x)
45 return g_strdup_printf ("%.*g", DBL_DIG + 1, x);
48 /* Define a boxed type to represent a value which is a candidate
49 to replace an existing value */
62 enum new_value_type type;
70 static struct new_value *
71 new_value_copy (struct new_value *nv)
73 struct new_value *copy = g_memdup (nv, sizeof (*copy));
75 if ( nv->type == NV_STRING )
76 copy->v.s = xstrdup (nv->v.s);
83 new_value_free (struct new_value *nv)
85 if ( nv->type == NV_STRING )
93 new_value_to_string (const GValue *src, GValue *dest)
95 const struct new_value *nv = g_value_get_boxed (src);
103 gchar *text = g_strdup_printf ("%.*g", DBL_DIG + 1, nv->v.v);
104 g_value_set_string (dest, text);
109 g_value_set_string (dest, nv->v.s);
112 g_value_set_string (dest, "COPY");
115 g_value_set_string (dest, "SYSMIS");
118 /* Shouldn't ever happen */
119 g_warning ("Invalid type in new recode value");
120 g_value_set_string (dest, "???");
126 new_value_get_type (void)
132 t = g_boxed_type_register_static ("psppire-recode-new-values",
133 (GBoxedCopyFunc) new_value_copy,
134 (GBoxedFreeFunc) new_value_free);
136 g_value_register_transform_func (t, G_TYPE_STRING,
137 new_value_to_string);
146 on_string_toggled (GtkToggleButton *b, PsppireDialogActionRecode *rd)
149 if (! rd->input_var_is_string )
152 active = gtk_toggle_button_get_active (b);
153 gtk_widget_set_sensitive (rd->convert_button, !active);
158 on_convert_toggled (GtkToggleButton *b, PsppireDialogActionRecode *rd)
162 g_return_if_fail (rd->input_var_is_string);
164 active = gtk_toggle_button_get_active (b);
165 gtk_widget_set_sensitive (rd->string_button, !active);
169 focus_value_entry (GtkWidget *w, PsppireDialogActionRecode *rd)
171 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)))
172 gtk_widget_grab_focus (rd->new_value_entry);
176 /* Callback for the new_value_entry and new_value_togglebutton widgets.
177 It's used to enable/disable the acr. */
179 set_acr (PsppireDialogActionRecode *rd)
183 if ( !gtk_toggle_button_get_active
184 (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_NEW_VALUE])))
186 psppire_acr_set_enabled (PSPPIRE_ACR (rd->acr), TRUE);
190 text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
192 psppire_acr_set_enabled (PSPPIRE_ACR (rd->acr), !g_str_equal (text, ""));
202 /* Callback which gets called when a new row is selected
203 in the acr's variable treeview.
204 We use if to set the togglebuttons and entries to correspond to the
208 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
210 PsppireDialogActionRecode *rd = data;
211 GtkTreeModel *model = NULL;
214 GValue ov_value = {0};
215 GValue nv_value = {0};
216 struct old_value *ov = NULL;
217 struct new_value *nv = NULL;
219 if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
223 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
224 COL_VALUE_OLD, &ov_value);
226 gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
227 COL_VALUE_NEW, &nv_value);
229 ov = g_value_get_boxed (&ov_value);
230 nv = g_value_get_boxed (&nv_value);
238 gchar *str = num_to_string (nv->v.v);
240 gtk_toggle_button_set_active
241 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE]), TRUE);
243 gtk_entry_set_text (GTK_ENTRY (rd->new_value_entry), str);
248 gtk_toggle_button_set_active
249 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE]), TRUE);
251 gtk_entry_set_text (GTK_ENTRY (rd->new_value_entry), nv->v.s);
254 gtk_toggle_button_set_active
255 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS]), TRUE);
259 gtk_toggle_button_set_active
260 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY]), TRUE);
264 g_warning ("Invalid new value type");
268 g_value_unset (&nv_value);
271 psppire_val_chooser_set_status (PSPPIRE_VAL_CHOOSER (rd->old_value_chooser), ov);
274 /* Initialise VAL to reflect the current status of RD */
276 set_old_value (GValue *val, const PsppireDialogActionRecode *rd)
278 PsppireValChooser *vc = PSPPIRE_VAL_CHOOSER (rd->old_value_chooser);
282 psppire_val_chooser_get_status (vc, &ov);
284 g_value_init (val, old_value_get_type ());
285 g_value_set_boxed (val, &ov);
291 /* Initialse VAL to reflect the current status of RD */
293 set_new_value (GValue *val, const PsppireDialogActionRecode *rd)
295 const gchar *text = NULL;
298 if (gtk_toggle_button_get_active
299 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE])))
301 text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
302 nv.type = NV_NUMERIC;
304 if (PSPPIRE_DIALOG_ACTION_RECODE_CLASS (G_OBJECT_GET_CLASS (rd))->target_is_string (rd))
307 if ( nv.type == NV_STRING )
308 nv.v.s = g_strdup (text);
310 nv.v.v = g_strtod (text, 0);
312 else if (gtk_toggle_button_get_active
313 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY])))
317 else if (gtk_toggle_button_get_active
318 (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS])))
325 g_value_init (val, new_value_get_type ());
326 g_value_set_boxed (val, &nv);
331 /* A function to set a value in a column in the ACR */
333 set_value (gint col, GValue *val, gpointer data)
335 PsppireDialogActionRecode *rd = data;
340 set_old_value (val, rd);
343 set_new_value (val, rd);
353 run_old_and_new_dialog (PsppireDialogActionRecode *rd)
356 GtkListStore *local_store = clone_list_store (rd->value_map);
357 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (rd);
359 psppire_acr_set_model (PSPPIRE_ACR (rd->acr), local_store);
360 psppire_acr_set_get_value_func (PSPPIRE_ACR (rd->acr), set_value, rd);
363 /* Find the type of the first variable (it's invariant that
364 all variables are of the same type) */
365 const struct variable *v;
367 GtkTreeModel *model =
368 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
370 gboolean not_empty = gtk_tree_model_get_iter_first (model, &iter);
372 g_return_if_fail (not_empty);
374 gtk_tree_model_get (model, &iter, 0, &v, -1);
376 rd->input_var_is_string = var_is_alpha (v);
378 g_object_set (rd->old_value_chooser, "is-string", rd->input_var_is_string, NULL);
380 gtk_widget_set_sensitive (rd->toggle [BUTTON_NEW_SYSMIS],
383 gtk_widget_set_sensitive (rd->convert_button, var_is_alpha (v));
387 response = psppire_dialog_run (rd->old_and_new_dialog);
388 psppire_acr_set_model (PSPPIRE_ACR (rd->acr), NULL);
391 if ( response == PSPPIRE_RESPONSE_CONTINUE )
393 g_object_unref (rd->value_map);
394 rd->value_map = clone_list_store (local_store);
397 g_object_unref (local_store);
400 psppire_dialog_notify_change (PSPPIRE_DIALOG (pda->dialog));
404 /* Sets the sensitivity of TARGET dependent upon the active status
407 toggle_sensitivity (GtkToggleButton *button, GtkWidget *target)
409 gboolean state = gtk_toggle_button_get_active (button);
411 /* g_print ("%s Setting %p (%s) to %d because of %p\n",
412 __FUNCTION__, target, gtk_widget_get_name (target), state, button); */
414 gtk_widget_set_sensitive (target, state);
421 psppire_dialog_action_recode_class_init (PsppireDialogActionRecodeClass *class);
423 G_DEFINE_TYPE (PsppireDialogActionRecode, psppire_dialog_action_recode, PSPPIRE_TYPE_DIALOG_ACTION);
426 psppire_dialog_action_recode_refresh (PsppireDialogAction *rd_)
428 PsppireDialogActionRecode *rd = PSPPIRE_DIALOG_ACTION_RECODE (rd_);
431 gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
433 gtk_list_store_clear (GTK_LIST_STORE (vars));
435 gtk_widget_set_sensitive (rd->change_button, FALSE);
436 gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
437 gtk_widget_set_sensitive (rd->new_label_entry, FALSE);
439 gtk_list_store_clear (GTK_LIST_STORE (rd->value_map));
444 psppire_dialog_action_recode_pre_activate (PsppireDialogActionRecode *act, void (*populate_treeview) (PsppireDialogActionRecode *))
446 PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (act);
448 GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
449 GtkBuilder *xml = g_hash_table_lookup (thing, act);
452 xml = builder_new ("recode.ui");
453 g_hash_table_insert (thing, act, xml);
455 pda->dialog = get_widget_assert (xml, "recode-dialog");
456 pda->source = get_widget_assert (xml, "treeview1");
459 GtkWidget *selector = get_widget_assert (xml, "psppire-selector1");
460 GtkWidget *oldandnew = get_widget_assert (xml, "button1");
463 act->output_variable_box = get_widget_assert (xml,"frame4");
465 act->change_button = get_widget_assert (xml, "change-button");
466 act->variable_treeview = get_widget_assert (xml, "treeview2");
467 act->new_name_entry = get_widget_assert (xml, "dest-name-entry");
468 act->new_label_entry = get_widget_assert (xml, "dest-label-entry");
470 act->value_map = gtk_list_store_new (2,
471 old_value_get_type (),
472 new_value_get_type ());
474 if (populate_treeview)
475 populate_treeview (act);
477 psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), homogeneous_types);
479 /* Set up the Old & New Values subdialog */
481 act->string_button = get_widget_assert (xml, "checkbutton1");
482 act->width_entry = get_widget_assert (xml, "spinbutton1");
484 act->convert_button = get_widget_assert (xml, "checkbutton2");
486 act->old_value_chooser = get_widget_assert (xml, "val-chooser");
488 act->new_value_entry = get_widget_assert (xml, "entry1");
491 act->toggle[BUTTON_NEW_VALUE] = get_widget_assert (xml, "radiobutton1");
492 act->toggle[BUTTON_NEW_SYSMIS] = get_widget_assert (xml, "radiobutton2");
493 act->toggle[BUTTON_NEW_COPY] = get_widget_assert (xml, "radiobutton3");
495 act->new_copy_label = get_widget_assert (xml, "label3");
496 act->strings_box = get_widget_assert (xml, "table3");
498 act->old_and_new_dialog =
499 PSPPIRE_DIALOG (get_widget_assert (xml, "old-new-values-dialog"));
501 act->acr = get_widget_assert (xml, "psppire-acr1");
503 g_signal_connect_swapped (act->toggle[BUTTON_NEW_VALUE], "toggled",
504 G_CALLBACK (set_acr), act);
506 g_signal_connect_after (act->toggle[BUTTON_NEW_VALUE], "toggled",
507 G_CALLBACK (focus_value_entry), act);
509 g_signal_connect_swapped (act->new_value_entry, "changed",
510 G_CALLBACK (set_acr), act);
513 GtkTreeSelection *sel;
514 /* Remove the ACR's default column. We don't like it */
515 GtkTreeViewColumn *column = gtk_tree_view_get_column (PSPPIRE_ACR(act->acr)->tv, 0);
516 gtk_tree_view_remove_column (PSPPIRE_ACR (act->acr)->tv, column);
520 gtk_tree_view_column_new_with_attributes (_("Old"),
521 gtk_cell_renderer_text_new (),
525 gtk_tree_view_append_column (PSPPIRE_ACR (act->acr)->tv, column);
528 gtk_tree_view_column_new_with_attributes (_("New"),
529 gtk_cell_renderer_text_new (),
533 gtk_tree_view_append_column (PSPPIRE_ACR(act->acr)->tv, column);
534 g_object_set (PSPPIRE_ACR (act->acr)->tv, "headers-visible", TRUE, NULL);
537 sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (PSPPIRE_ACR(act->acr)->tv));
538 g_signal_connect (sel, "changed",
539 G_CALLBACK (on_acr_selection_change), act);
543 g_signal_connect_swapped (oldandnew, "clicked",
544 G_CALLBACK (run_old_and_new_dialog), act);
547 g_signal_connect (act->toggle[BUTTON_NEW_VALUE], "toggled",
548 G_CALLBACK (toggle_sensitivity), act->new_value_entry);
550 g_signal_connect (act->string_button, "toggled",
551 G_CALLBACK (toggle_sensitivity), act->width_entry);
553 g_signal_connect (act->string_button, "toggled",
554 G_CALLBACK (on_string_toggled), act);
556 g_signal_connect (act->convert_button, "toggled",
557 G_CALLBACK (on_convert_toggled), act);
562 /* Generate a syntax fragment for NV and append it to STR */
564 new_value_append_syntax (struct string *dds, const struct new_value *nv)
569 ds_put_c_format (dds, "%.*g", DBL_DIG + 1, nv->v.v);
572 syntax_gen_string (dds, ss_cstr (nv->v.s));
575 ds_put_cstr (dds, "COPY");
578 ds_put_cstr (dds, "SYSMIS");
581 /* Shouldn't ever happen */
582 g_warning ("Invalid type in new recode value");
583 ds_put_cstr (dds, "???");
590 psppire_dialog_action_recode_generate_syntax (const PsppireDialogAction *act,
591 void (*append_string_decls) (const PsppireDialogActionRecode *, struct string *),
592 void (*append_into_clause) (const PsppireDialogActionRecode *, struct string *),
593 void (*append_new_value_labels) (const PsppireDialogActionRecode *, struct string *))
595 PsppireDialogActionRecode *rd = PSPPIRE_DIALOG_ACTION_RECODE (act);
601 ds_init_empty (&dds);
603 append_string_decls (rd, &dds);
605 ds_put_cstr (&dds, "\nRECODE ");
607 psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (rd->variable_treeview), 0, &dds);
609 ds_put_cstr (&dds, "\n\t");
611 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->convert_button)))
613 ds_put_cstr (&dds, "(CONVERT) ");
616 for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->value_map),
619 ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->value_map), &iter))
621 GValue ov_value = {0};
622 GValue nv_value = {0};
623 struct old_value *ov;
624 struct new_value *nv;
625 gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
626 COL_VALUE_OLD, &ov_value);
628 gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
629 COL_VALUE_NEW, &nv_value);
631 ov = g_value_get_boxed (&ov_value);
632 nv = g_value_get_boxed (&nv_value);
634 ds_put_cstr (&dds, "(");
636 old_value_append_syntax (&dds, ov);
637 ds_put_cstr (&dds, " = ");
638 new_value_append_syntax (&dds, nv);
640 ds_put_cstr (&dds, ") ");
641 g_value_unset (&ov_value);
642 g_value_unset (&nv_value);
645 append_into_clause (rd, &dds);
647 ds_put_cstr (&dds, ".");
649 append_new_value_labels (rd, &dds);
651 ds_put_cstr (&dds, "\nEXECUTE.\n");
654 text = ds_steal_cstr (&dds);
663 psppire_dialog_action_recode_class_init (PsppireDialogActionRecodeClass *class)
669 psppire_dialog_action_recode_init (PsppireDialogActionRecode *act)