6e8b222b47812273fdf77c6e5734777ec1680ebc
[pspp] / src / ui / gui / psppire-dialog-action-recode.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2007, 2009, 2010, 2011, 2012, 2014, 2016  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17
18 #include <config.h>
19
20 #include "psppire-var-view.h"
21
22 #include "psppire-dialog-action-recode.h"
23 #include "builder-wrapper.h"
24 #include <ui/gui/dialog-common.h>
25
26 #include "psppire-acr.h"
27
28 #include "psppire-selector.h"
29 #include "psppire-val-chooser.h"
30
31 #include "helper.h"
32 #include <ui/syntax-gen.h>
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36 #define N_(msgid) msgid
37
38
39 /* This might need to be changed to something less naive.
40    In particular, what happends with dates, etc?
41  */
42 static gchar *
43 num_to_string (gdouble x)
44 {
45   return g_strdup_printf ("%.*g", DBL_DIG + 1, x);
46 }
47
48 /* Define a boxed type to represent a value which is a candidate
49    to replace an existing value */
50
51 enum new_value_type
52  {
53    NV_NUMERIC,
54    NV_STRING,
55    NV_SYSMIS,
56    NV_COPY
57  };
58
59
60 struct new_value
61 {
62   enum new_value_type type;
63   union {
64     double v;
65     gchar *s;
66   } v;
67 };
68
69
70 static struct new_value *
71 new_value_copy (struct new_value *nv)
72 {
73   struct new_value *copy = g_memdup (nv, sizeof (*copy));
74
75   if ( nv->type == NV_STRING )
76     copy->v.s = xstrdup (nv->v.s);
77
78   return copy;
79 }
80
81
82 static void
83 new_value_free (struct new_value *nv)
84 {
85   if ( nv->type == NV_STRING )
86     g_free (nv->v.s);
87
88   g_free (nv);
89 }
90
91
92 static void
93 new_value_to_string (const GValue *src, GValue *dest)
94 {
95   const struct new_value *nv = g_value_get_boxed (src);
96
97   g_assert (nv);
98
99   switch (nv->type)
100     {
101     case NV_NUMERIC:
102       {
103         gchar *text = g_strdup_printf ("%.*g", DBL_DIG + 1, nv->v.v);
104         g_value_set_string (dest, text);
105         g_free (text);
106       }
107       break;
108     case NV_STRING:
109       g_value_set_string (dest, nv->v.s);
110       break;
111     case NV_COPY:
112       g_value_set_string (dest, "COPY");
113       break;
114     case NV_SYSMIS:
115       g_value_set_string (dest, "SYSMIS");
116       break;
117     default:
118       /* Shouldn't ever happen */
119       g_warning ("Invalid type in new recode value");
120       g_value_set_string (dest, "???");
121       break;
122     }
123 }
124
125 GType
126 new_value_get_type (void)
127 {
128   static GType t = 0;
129
130   if (t == 0 )
131     {
132       t = g_boxed_type_register_static  ("psppire-recode-new-values",
133                                          (GBoxedCopyFunc) new_value_copy,
134                                          (GBoxedFreeFunc) new_value_free);
135
136       g_value_register_transform_func (t, G_TYPE_STRING,
137                                        new_value_to_string);
138     }
139
140   return t;
141 }
142
143 \f
144
145 static void
146 on_string_toggled (GtkToggleButton *b, PsppireDialogActionRecode *rd)
147 {
148   gboolean active;
149   if (! rd->input_var_is_string )
150     return ;
151
152   active = gtk_toggle_button_get_active (b);
153   gtk_widget_set_sensitive (rd->convert_button, !active);
154 }
155
156
157 static void
158 on_convert_toggled (GtkToggleButton *b, PsppireDialogActionRecode *rd)
159 {
160   gboolean active;
161
162   g_return_if_fail (rd->input_var_is_string);
163
164   active = gtk_toggle_button_get_active (b);
165   gtk_widget_set_sensitive (rd->string_button, !active);
166 }
167
168 static void
169 focus_value_entry (GtkWidget *w, PsppireDialogActionRecode *rd)
170 {
171   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)))
172     gtk_widget_grab_focus (rd->new_value_entry);
173 }
174
175
176 /* Callback for the new_value_entry and new_value_togglebutton widgets.
177    It's used to enable/disable the acr. */
178 static void
179 set_acr (PsppireDialogActionRecode *rd)
180 {
181   const gchar *text;
182
183   if ( !gtk_toggle_button_get_active
184        (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_NEW_VALUE])))
185     {
186       psppire_acr_set_enabled (PSPPIRE_ACR (rd->acr), TRUE);
187       return;
188     }
189
190   text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
191
192   psppire_acr_set_enabled (PSPPIRE_ACR (rd->acr), !g_str_equal (text, ""));
193 }
194
195 enum
196   {
197     COL_VALUE_OLD,
198     COL_VALUE_NEW,
199     n_COL_VALUES
200   };
201
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
205    selected row.
206 */
207 static void
208 on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
209 {
210   PsppireDialogActionRecode *rd = data;
211   GtkTreeModel *model = NULL;
212   GtkTreeIter iter;
213
214   GValue ov_value = {0};
215   GValue nv_value = {0};
216   struct old_value *ov = NULL;
217   struct new_value *nv = NULL;
218
219   if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
220     return;
221
222
223   gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
224                             COL_VALUE_OLD, &ov_value);
225
226   gtk_tree_model_get_value (GTK_TREE_MODEL (model), &iter,
227                             COL_VALUE_NEW, &nv_value);
228
229   ov = g_value_get_boxed (&ov_value);
230   nv = g_value_get_boxed (&nv_value);
231
232   if (nv)
233     {
234       switch (nv->type)
235         {
236         case NV_NUMERIC:
237           {
238             gchar *str = num_to_string (nv->v.v);
239
240             gtk_toggle_button_set_active
241               (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE]), TRUE);
242
243             gtk_entry_set_text (GTK_ENTRY (rd->new_value_entry), str);
244             g_free (str);
245           }
246           break;
247         case NV_STRING:
248           gtk_toggle_button_set_active
249             (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE]), TRUE);
250
251           gtk_entry_set_text (GTK_ENTRY (rd->new_value_entry), nv->v.s);
252           break;
253         case NV_SYSMIS:
254           gtk_toggle_button_set_active
255             (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS]), TRUE);
256
257           break;
258         case NV_COPY:
259           gtk_toggle_button_set_active
260             (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY]), TRUE);
261
262           break;
263         default:
264           g_warning ("Invalid new value type");
265           break;
266         }
267
268       g_value_unset (&nv_value);
269     }
270
271   psppire_val_chooser_set_status (PSPPIRE_VAL_CHOOSER (rd->old_value_chooser), ov);
272 }
273
274 /* Initialise VAL to reflect the current status of RD */
275 static gboolean
276 set_old_value (GValue *val, const PsppireDialogActionRecode *rd)
277 {
278   PsppireValChooser *vc = PSPPIRE_VAL_CHOOSER (rd->old_value_chooser);
279
280   struct old_value ov;
281
282   psppire_val_chooser_get_status (vc, &ov);
283
284   g_value_init (val, old_value_get_type ());
285   g_value_set_boxed (val, &ov);
286
287   return TRUE;
288 }
289
290
291 /* Initialse VAL to reflect the current status of RD */
292 static gboolean
293 set_new_value (GValue *val, const PsppireDialogActionRecode *rd)
294 {
295   const gchar *text = NULL;
296   struct new_value nv;
297
298   if (gtk_toggle_button_get_active
299       (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_VALUE])))
300     {
301       text = gtk_entry_get_text (GTK_ENTRY (rd->new_value_entry));
302       nv.type = NV_NUMERIC;
303
304       if (PSPPIRE_DIALOG_ACTION_RECODE_CLASS (G_OBJECT_GET_CLASS (rd))->target_is_string (rd))
305         nv.type = NV_STRING;
306
307       if ( nv.type == NV_STRING )
308         nv.v.s = g_strdup (text);
309       else
310         nv.v.v = g_strtod (text, 0);
311     }
312   else if (gtk_toggle_button_get_active
313             (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_COPY])))
314     {
315       nv.type = NV_COPY;
316     }
317   else if (gtk_toggle_button_get_active
318             (GTK_TOGGLE_BUTTON (rd->toggle [BUTTON_NEW_SYSMIS])))
319     {
320       nv.type = NV_SYSMIS;
321     }
322   else
323     return FALSE;
324
325   g_value_init (val, new_value_get_type ());
326   g_value_set_boxed (val, &nv);
327
328   return TRUE;
329 }
330
331 /* A function to set a value in a column in the ACR */
332 static gboolean
333 set_value (gint col, GValue  *val, gpointer data)
334 {
335   PsppireDialogActionRecode *rd = data;
336
337   switch ( col )
338     {
339     case COL_VALUE_OLD:
340       set_old_value (val, rd);
341       break;
342     case COL_VALUE_NEW:
343       set_new_value (val, rd);
344       break;
345     default:
346       return FALSE;
347     }
348
349   return TRUE;
350 }
351
352 static void
353 run_old_and_new_dialog (PsppireDialogActionRecode *rd)
354 {
355   gint response;
356   GtkListStore *local_store = clone_list_store (rd->value_map);
357   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (rd);
358
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);
361
362   {
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;
366     GtkTreeIter iter;
367     GtkTreeModel *model =
368       gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
369
370     gboolean not_empty = gtk_tree_model_get_iter_first (model, &iter);
371
372     g_return_if_fail (not_empty);
373
374     gtk_tree_model_get (model, &iter, 0, &v, -1);
375
376     rd->input_var_is_string = var_is_alpha (v);
377
378     g_object_set (rd->old_value_chooser, "is-string", rd->input_var_is_string, NULL);
379
380     gtk_widget_set_sensitive (rd->toggle [BUTTON_NEW_SYSMIS],
381                               var_is_numeric (v));
382
383     gtk_widget_set_sensitive (rd->convert_button, var_is_alpha (v));
384   }
385
386
387   response = psppire_dialog_run (rd->old_and_new_dialog);
388   psppire_acr_set_model (PSPPIRE_ACR (rd->acr), NULL);
389
390
391   if ( response == PSPPIRE_RESPONSE_CONTINUE )
392     {
393       g_object_unref (rd->value_map);
394       rd->value_map = clone_list_store (local_store);
395     }
396   else
397     g_object_unref (local_store);
398
399
400   psppire_dialog_notify_change (PSPPIRE_DIALOG (pda->dialog));
401 }
402
403
404 /* Sets the sensitivity of TARGET dependent upon the active status
405    of BUTTON */
406 static void
407 toggle_sensitivity (GtkToggleButton *button, GtkWidget *target)
408 {
409   gboolean state = gtk_toggle_button_get_active (button);
410
411   /*  g_print ("%s Setting %p (%s) to %d because of %p\n",
412       __FUNCTION__, target, gtk_widget_get_name (target), state, button); */
413
414   gtk_widget_set_sensitive (target, state);
415 }
416
417
418 \f
419
420 static void
421 psppire_dialog_action_recode_class_init (PsppireDialogActionRecodeClass *class);
422
423 G_DEFINE_TYPE (PsppireDialogActionRecode, psppire_dialog_action_recode, PSPPIRE_TYPE_DIALOG_ACTION);
424
425 void
426 psppire_dialog_action_recode_refresh (PsppireDialogAction *rd_)
427 {
428   PsppireDialogActionRecode *rd = PSPPIRE_DIALOG_ACTION_RECODE (rd_);
429
430   GtkTreeModel *vars =
431     gtk_tree_view_get_model (GTK_TREE_VIEW (rd->variable_treeview));
432
433   gtk_list_store_clear (GTK_LIST_STORE (vars));
434
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);
438
439   gtk_list_store_clear (GTK_LIST_STORE (rd->value_map));
440 }
441
442
443 static void
444 psppire_dialog_action_recode_activate (PsppireDialogAction *act)
445 {
446   if (PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_recode_parent_class)->activate)
447     PSPPIRE_DIALOG_ACTION_CLASS (psppire_dialog_action_recode_parent_class)->activate (act);
448 }
449
450
451 void
452 psppire_dialog_action_recode_pre_activate (PsppireDialogActionRecode *act, void (*populate_treeview) (PsppireDialogActionRecode *))
453 {
454   PsppireDialogAction *pda = PSPPIRE_DIALOG_ACTION (act);
455
456   GHashTable *thing = psppire_dialog_action_get_hash_table (pda);
457   GtkBuilder *xml = g_hash_table_lookup (thing, act);
458   if (!xml)
459     {
460       xml = builder_new ("recode.ui");
461       g_hash_table_insert (thing, act, xml);
462
463       pda->dialog = get_widget_assert   (xml, "recode-dialog");
464       pda->source = get_widget_assert   (xml, "treeview1");
465
466   
467       GtkWidget *selector = get_widget_assert (xml, "psppire-selector1");
468       GtkWidget *oldandnew = get_widget_assert (xml, "button1");
469
470
471       act->output_variable_box = get_widget_assert (xml,"frame4");
472
473       act->change_button = get_widget_assert (xml, "change-button");
474       act->variable_treeview =   get_widget_assert (xml, "treeview2");
475       act->new_name_entry = get_widget_assert (xml, "dest-name-entry");
476       act->new_label_entry = get_widget_assert (xml, "dest-label-entry");
477
478       act->value_map = gtk_list_store_new (2,
479                                            old_value_get_type (),
480                                            new_value_get_type ());
481
482       if (populate_treeview)
483         populate_treeview (act);
484       
485       psppire_selector_set_allow (PSPPIRE_SELECTOR (selector), homogeneous_types);
486
487       /* Set up the Old & New Values subdialog */
488       {
489         act->string_button = get_widget_assert (xml, "checkbutton1");
490         act->width_entry   = get_widget_assert (xml, "spinbutton1");
491
492         act->convert_button = get_widget_assert (xml, "checkbutton2");
493
494         act->old_value_chooser = get_widget_assert (xml, "val-chooser");
495
496         act->new_value_entry = get_widget_assert (xml, "entry1");
497
498
499         act->toggle[BUTTON_NEW_VALUE]  = get_widget_assert (xml, "radiobutton1");
500         act->toggle[BUTTON_NEW_SYSMIS] = get_widget_assert (xml, "radiobutton2");
501         act->toggle[BUTTON_NEW_COPY]   = get_widget_assert (xml, "radiobutton3");
502
503         act->new_copy_label = get_widget_assert (xml, "label3");
504         act->strings_box    = get_widget_assert (xml, "table3");
505
506         act->old_and_new_dialog =
507           PSPPIRE_DIALOG (get_widget_assert (xml, "old-new-values-dialog"));
508
509         act->acr = get_widget_assert (xml, "psppire-acr1");
510
511         g_signal_connect_swapped (act->toggle[BUTTON_NEW_VALUE], "toggled",
512                                   G_CALLBACK (set_acr), act);
513
514         g_signal_connect_after (act->toggle[BUTTON_NEW_VALUE], "toggled",
515                                 G_CALLBACK (focus_value_entry), act);
516
517         g_signal_connect_swapped (act->new_value_entry, "changed",
518                                   G_CALLBACK (set_acr), act);
519
520         {
521           GtkTreeSelection *sel;
522           /* Remove the ACR's default column.  We don't like it */
523           GtkTreeViewColumn *column = gtk_tree_view_get_column (PSPPIRE_ACR(act->acr)->tv, 0);
524           gtk_tree_view_remove_column (PSPPIRE_ACR (act->acr)->tv, column);
525
526
527           column =
528             gtk_tree_view_column_new_with_attributes (_("Old"),
529                                                       gtk_cell_renderer_text_new (),
530                                                       "text", 0,
531                                                       NULL);
532
533           gtk_tree_view_append_column (PSPPIRE_ACR (act->acr)->tv, column);
534
535           column =
536             gtk_tree_view_column_new_with_attributes (_("New"),
537                                                       gtk_cell_renderer_text_new (),
538                                                       "text", 1,
539                                                       NULL);
540
541           gtk_tree_view_append_column (PSPPIRE_ACR(act->acr)->tv, column);
542           g_object_set (PSPPIRE_ACR (act->acr)->tv, "headers-visible", TRUE, NULL);
543
544
545           sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (PSPPIRE_ACR(act->acr)->tv));
546           g_signal_connect (sel, "changed",
547                             G_CALLBACK (on_acr_selection_change), act);
548         }
549
550
551         g_signal_connect_swapped (oldandnew, "clicked",
552                                   G_CALLBACK (run_old_and_new_dialog), act);
553
554
555         g_signal_connect (act->toggle[BUTTON_NEW_VALUE], "toggled",
556                           G_CALLBACK (toggle_sensitivity), act->new_value_entry);
557
558         g_signal_connect (act->string_button, "toggled",
559                           G_CALLBACK (toggle_sensitivity), act->width_entry);
560
561         g_signal_connect (act->string_button, "toggled",
562                           G_CALLBACK (on_string_toggled), act);
563
564         g_signal_connect (act->convert_button, "toggled",
565                           G_CALLBACK (on_convert_toggled), act);
566       }
567     }
568 }
569
570 /* Generate a syntax fragment for NV and append it to STR */
571 static void
572 new_value_append_syntax (struct string *dds, const struct new_value *nv)
573 {
574   switch (nv->type)
575     {
576     case NV_NUMERIC:
577       ds_put_c_format (dds, "%.*g", DBL_DIG + 1, nv->v.v);
578       break;
579     case NV_STRING:
580       syntax_gen_string (dds, ss_cstr (nv->v.s));
581       break;
582     case NV_COPY:
583       ds_put_cstr (dds, "COPY");
584       break;
585     case NV_SYSMIS:
586       ds_put_cstr (dds, "SYSMIS");
587       break;
588     default:
589       /* Shouldn't ever happen */
590       g_warning ("Invalid type in new recode value");
591       ds_put_cstr (dds, "???");
592       break;
593     }
594 }
595
596
597 char *
598 psppire_dialog_action_recode_generate_syntax (const PsppireDialogAction *act,
599                                               void (*append_string_decls) (const PsppireDialogActionRecode *, struct string *),
600                                               void (*append_into_clause) (const PsppireDialogActionRecode *, struct string *),
601                                               void (*append_new_value_labels) (const PsppireDialogActionRecode *, struct string *))
602 {
603   PsppireDialogActionRecode *rd = PSPPIRE_DIALOG_ACTION_RECODE (act);
604   gboolean ok;
605   GtkTreeIter iter;
606   gchar *text;
607   struct string dds;
608
609   ds_init_empty (&dds);
610
611   append_string_decls (rd, &dds);
612   
613   ds_put_cstr (&dds, "\nRECODE ");
614
615   psppire_var_view_append_names_str (PSPPIRE_VAR_VIEW (rd->variable_treeview), 0, &dds);
616
617   ds_put_cstr (&dds, "\n\t");
618
619   if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->convert_button)))
620     {
621       ds_put_cstr (&dds, "(CONVERT) ");
622     }
623
624   for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (rd->value_map),
625                                            &iter);
626        ok;
627        ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (rd->value_map), &iter))
628     {
629       GValue ov_value = {0};
630       GValue nv_value = {0};
631       struct old_value *ov;
632       struct new_value *nv;
633       gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
634                                 COL_VALUE_OLD, &ov_value);
635
636       gtk_tree_model_get_value (GTK_TREE_MODEL (rd->value_map), &iter,
637                                 COL_VALUE_NEW, &nv_value);
638
639       ov = g_value_get_boxed (&ov_value);
640       nv = g_value_get_boxed (&nv_value);
641
642       ds_put_cstr (&dds, "(");
643
644       old_value_append_syntax (&dds, ov);
645       ds_put_cstr (&dds, " = ");
646       new_value_append_syntax (&dds, nv);
647
648       ds_put_cstr (&dds, ") ");
649       g_value_unset (&ov_value);
650       g_value_unset (&nv_value);
651     }
652
653   append_into_clause (rd, &dds);
654
655   ds_put_cstr (&dds, ".");
656
657   append_new_value_labels (rd, &dds);
658   
659   ds_put_cstr (&dds, "\nEXECUTE.\n");
660
661
662   text = ds_steal_cstr (&dds);
663
664   ds_destroy (&dds);
665
666   return text;
667 }
668
669
670 static void
671 psppire_dialog_action_recode_class_init (PsppireDialogActionRecodeClass *class)
672 {
673   psppire_dialog_action_set_activation (class, psppire_dialog_action_recode_activate);
674 }
675
676
677 static void
678 psppire_dialog_action_recode_init (PsppireDialogActionRecode *act)
679 {
680 }
681