supply g_memdup2() for glib before 2.68.
[pspp] / src / ui / gui / psppire-dialog-action-recode.c
index 5b052844897d0bb9cbf2a195dacc7ed8a40753f1..3b7165f5cb2c2f9256f34f254596a76bdf864a74 100644 (file)
@@ -1,5 +1,6 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2007, 2009, 2010, 2011, 2012, 2014, 2016  Free Software Foundation
+   Copyright (C) 2007, 2009, 2010, 2011, 2012, 2014, 2016,
+   2020, 2022  Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include "helper.h"
 #include <ui/syntax-gen.h>
+#include "ui/gui/glibfix.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
 #define N_(msgid) msgid
 
-
 /* This might need to be changed to something less naive.
    In particular, what happends with dates, etc?
  */
@@ -70,9 +71,9 @@ struct new_value
 static struct new_value *
 new_value_copy (struct new_value *nv)
 {
-  struct new_value *copy = g_memdup (nv, sizeof (*copy));
+  struct new_value *copy = g_memdup2 (nv, sizeof (*copy));
 
-  if ( nv->type == NV_STRING )
+  if (nv->type == NV_STRING)
     copy->v.s = xstrdup (nv->v.s);
 
   return copy;
@@ -82,7 +83,7 @@ new_value_copy (struct new_value *nv)
 static void
 new_value_free (struct new_value *nv)
 {
-  if ( nv->type == NV_STRING )
+  if (nv->type == NV_STRING)
     g_free (nv->v.s);
 
   g_free (nv);
@@ -127,7 +128,7 @@ new_value_get_type (void)
 {
   static GType t = 0;
 
-  if (t == 0 )
+  if (t == 0)
     {
       t = g_boxed_type_register_static  ("psppire-recode-new-values",
                                         (GBoxedCopyFunc) new_value_copy,
@@ -146,7 +147,7 @@ static void
 on_string_toggled (GtkToggleButton *b, PsppireDialogActionRecode *rd)
 {
   gboolean active;
-  if (! rd->input_var_is_string )
+  if (! rd->input_var_is_string)
     return ;
 
   active = gtk_toggle_button_get_active (b);
@@ -168,7 +169,7 @@ on_convert_toggled (GtkToggleButton *b, PsppireDialogActionRecode *rd)
 static void
 focus_value_entry (GtkWidget *w, PsppireDialogActionRecode *rd)
 {
-  if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)))
+  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w)))
     gtk_widget_grab_focus (rd->new_value_entry);
 }
 
@@ -180,7 +181,7 @@ set_acr (PsppireDialogActionRecode *rd)
 {
   const gchar *text;
 
-  if ( !gtk_toggle_button_get_active
+  if (!gtk_toggle_button_get_active
        (GTK_TOGGLE_BUTTON (rd->toggle[BUTTON_NEW_VALUE])))
     {
       psppire_acr_set_enabled (PSPPIRE_ACR (rd->acr), TRUE);
@@ -216,7 +217,7 @@ on_acr_selection_change (GtkTreeSelection *selection, gpointer data)
   struct old_value *ov = NULL;
   struct new_value *nv = NULL;
 
-  if ( ! gtk_tree_selection_get_selected (selection, &model, &iter) )
+  if (! gtk_tree_selection_get_selected (selection, &model, &iter))
     return;
 
 
@@ -304,7 +305,7 @@ set_new_value (GValue *val, const PsppireDialogActionRecode *rd)
       if (PSPPIRE_DIALOG_ACTION_RECODE_CLASS (G_OBJECT_GET_CLASS (rd))->target_is_string (rd))
        nv.type = NV_STRING;
 
-      if ( nv.type == NV_STRING )
+      if (nv.type == NV_STRING)
        nv.v.s = g_strdup (text);
       else
        nv.v.v = g_strtod (text, 0);
@@ -334,7 +335,7 @@ set_value (gint col, GValue  *val, gpointer data)
 {
   PsppireDialogActionRecode *rd = data;
 
-  switch ( col )
+  switch (col)
     {
     case COL_VALUE_OLD:
       set_old_value (val, rd);
@@ -349,6 +350,16 @@ set_value (gint col, GValue  *val, gpointer data)
   return TRUE;
 }
 
+static void
+set_old_and_new_button_sensitivity (GtkTreeSelection *sel, PsppireDialogActionRecode *rd)
+{
+  GtkTreeModel *model = NULL;
+
+  GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
+
+  gtk_widget_set_sensitive (rd->old_and_new, rows != NULL);
+}
+
 static void
 run_old_and_new_dialog (PsppireDialogActionRecode *rd)
 {
@@ -388,7 +399,7 @@ run_old_and_new_dialog (PsppireDialogActionRecode *rd)
   psppire_acr_set_model (PSPPIRE_ACR (rd->acr), NULL);
 
 
-  if ( response == PSPPIRE_RESPONSE_CONTINUE )
+  if (response == PSPPIRE_RESPONSE_CONTINUE)
     {
       g_object_unref (rd->value_map);
       rd->value_map = clone_list_store (local_store);
@@ -435,6 +446,7 @@ psppire_dialog_action_recode_refresh (PsppireDialogAction *rd_)
   gtk_widget_set_sensitive (rd->change_button, FALSE);
   gtk_widget_set_sensitive (rd->new_name_entry, FALSE);
   gtk_widget_set_sensitive (rd->new_label_entry, FALSE);
+  gtk_widget_set_sensitive (rd->old_and_new, FALSE);
 
   gtk_list_store_clear (GTK_LIST_STORE (rd->value_map));
 }
@@ -453,8 +465,7 @@ psppire_dialog_action_recode_pre_activate (PsppireDialogActionRecode *act,
 
 
   GtkWidget *selector = get_widget_assert (xml, "psppire-selector1");
-  GtkWidget *oldandnew = get_widget_assert (xml, "button1");
-
+  act->old_and_new  = get_widget_assert (xml, "button1");
 
   act->output_variable_box = get_widget_assert (xml,"frame4");
 
@@ -536,10 +547,15 @@ psppire_dialog_action_recode_pre_activate (PsppireDialogActionRecode *act,
     }
 
 
-    g_signal_connect_swapped (oldandnew, "clicked",
+    g_signal_connect_swapped (act->old_and_new, "clicked",
                              G_CALLBACK (run_old_and_new_dialog), act);
 
 
+    GtkTreeSelection *sel =
+      gtk_tree_view_get_selection (GTK_TREE_VIEW (act->variable_treeview));
+    g_signal_connect (sel, "changed",
+                      G_CALLBACK (set_old_and_new_button_sensitivity), act);
+
     g_signal_connect (act->toggle[BUTTON_NEW_VALUE], "toggled",
                      G_CALLBACK (toggle_sensitivity), act->new_value_entry);
 
@@ -604,7 +620,7 @@ psppire_dialog_action_recode_generate_syntax (const PsppireDialogAction *act,
 
   ds_put_cstr (&dds, "\n\t");
 
-  if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->convert_button)))
+  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (rd->convert_button)))
     {
       ds_put_cstr (&dds, "(CONVERT) ");
     }