Ensure that data sheet and variable sheets can be constructed via GtkBuilder.
authorJohn Darrington <john@darrington.wattle.id.au>
Sat, 3 Oct 2020 05:18:47 +0000 (07:18 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sun, 27 Sep 2020 15:25:40 +0000 (17:25 +0200)
This change removes the reliance of the _new functions on setting
necessary properties.  Otherwise these objects do not get set
when constructed by GtkBuilder.

src/ui/gui/psppire-data-sheet.c
src/ui/gui/psppire-variable-sheet.c

index feb64b1aab51d07dcea862d94f11de89ad2eb508..728604d5138db9afdc2ac14d87a09cb1a515fd47 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2017, 2019  Free Software Foundation
+   Copyright (C) 2017, 2019, 2020  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
@@ -372,10 +372,28 @@ psppire_data_sheet_dispose (GObject *obj)
   G_OBJECT_CLASS (parent_class)->dispose (obj);
 }
 
+
+static void
+psppire_data_sheet_realize (GtkWidget *widget)
+{
+  g_object_set (widget,
+                "forward-conversion", psppire_data_store_value_to_string,
+                "reverse-conversion", psppire_data_store_string_to_value,
+                "editable", TRUE,
+                "horizontal-draggable", TRUE,
+                NULL);
+
+  /* Chain up to the parent class */
+  GTK_WIDGET_CLASS (parent_class)->realize (widget);
+}
+
 static void
 psppire_data_sheet_class_init (PsppireDataSheetClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+
+  widget_class->realize = psppire_data_sheet_realize;
   object_class->dispose = psppire_data_sheet_dispose;
   object_class->finalize = psppire_data_sheet_finalize;
 
@@ -385,15 +403,7 @@ psppire_data_sheet_class_init (PsppireDataSheetClass *class)
 GtkWidget*
 psppire_data_sheet_new (void)
 {
-  GObject *obj =
-    g_object_new (PSPPIRE_TYPE_DATA_SHEET,
-                 "forward-conversion", psppire_data_store_value_to_string,
-                 "reverse-conversion", psppire_data_store_string_to_value,
-                 "editable", TRUE,
-                 "horizontal-draggable", TRUE,
-                 NULL);
-
-  return GTK_WIDGET (obj);
+  return g_object_new (PSPPIRE_TYPE_DATA_SHEET, NULL);
 }
 
 
@@ -457,6 +467,8 @@ set_dictionary (PsppireDataSheet *sheet)
   GtkTreeModel *data_model = NULL;
   g_object_get (sheet, "data-model", &data_model, NULL);
 
+  g_return_if_fail (data_model);
+
   PsppireDataStore *store = PSPPIRE_DATA_STORE (data_model);
   g_object_set (sheet, "hmodel", store->dict, NULL);
 
index d877c80dc969a72390217b7f0ca940d2a72c6cf1..b0fcbfca909b52375f0757711a2154dfba6bdbd3 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2017  Free Software Foundation
+   Copyright (C) 2017, 2020  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
@@ -455,33 +455,42 @@ psppire_variable_sheet_finalize (GObject *object)
     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
 }
 
+static void
+psppire_variable_sheet_realize (GtkWidget *widget)
+{
+  /* This is a kludge.  These are properties from the parent class.
+     They should really be set immediately after initialisation, but there is no
+     simple way to do that.  */
+  g_object_set (widget,
+                "editable", TRUE,
+                "select-renderer-func", select_renderer_func,
+                "vertical-draggable", TRUE,
+                "forward-conversion", var_sheet_data_to_string,
+                NULL);
+
+  if (GTK_WIDGET_CLASS (parent_class)->realize)
+    (*GTK_WIDGET_CLASS (parent_class)->realize) (widget);
+}
+
+
 static void
 psppire_variable_sheet_class_init (PsppireVariableSheetClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
+
   object_class->dispose = psppire_variable_sheet_dispose;
 
   parent_class = g_type_class_peek_parent (class);
 
+  widget_class->realize = psppire_variable_sheet_realize;
   object_class->finalize = psppire_variable_sheet_finalize;
 }
 
 GtkWidget*
 psppire_variable_sheet_new (void)
 {
-  PsppireVarSheetHeader *vsh =
-    g_object_new (PSPPIRE_TYPE_VAR_SHEET_HEADER, NULL);
-
-  GObject *obj =
-    g_object_new (PSPPIRE_TYPE_VARIABLE_SHEET,
-                 "select-renderer-func", select_renderer_func,
-                 "hmodel", vsh,
-                 "forward-conversion", var_sheet_data_to_string,
-                 "editable", TRUE,
-                 "vertical-draggable", TRUE,
-                 NULL);
-
-  return GTK_WIDGET (obj);
+  return g_object_new (PSPPIRE_TYPE_VARIABLE_SHEET, NULL);
 }
 
 static void
@@ -609,4 +618,11 @@ psppire_variable_sheet_init (PsppireVariableSheet *sheet)
 
   g_signal_connect (sheet, "row-moved",
                    G_CALLBACK (move_variable), NULL);
+
+  PsppireVarSheetHeader *vsh =
+    g_object_new (PSPPIRE_TYPE_VAR_SHEET_HEADER, NULL);
+
+  g_object_set (sheet,
+                "hmodel", vsh,
+                NULL);
 }