patch Re: GtkLayout, segfaulting with [hv]adjustment properties



On Thu, 2001-11-15 at 14:45, Owen Taylor wrote:
> 
> jacob berkman <jacob ximian com> writes:
> 
> > On Wed, 2001-11-14 at 16:03, Owen Taylor wrote:
> > > 
> > > jacob berkman <jacob ximian com> writes:
> > > 
> > > > i am getting a segfault in GtkLayout if i set the height/width
> > > > properties before [hv]adjustment properties.
> > > 
> > > One sketch of a solution would be to:
> > > 
> > >  * If NULL adjustments are set during construction, don't create
> > >    adjustments
> > > 
> > >  * Create default adjustments in a constructor() methods if
> > >    hadjustment, vadjustment are NULL.

ok, here's an attempt.

jacob
-- 
"Beat mixing is 10000 times more fun than even video games."
	-- bt
Index: gtklayout.c
===================================================================
RCS file: /cvs/gnome/gtk+/gtk/gtklayout.c,v
retrieving revision 1.44
diff -u -r1.44 gtklayout.c
--- gtklayout.c	2001/09/08 19:33:05	1.44
+++ gtklayout.c	2001/11/15 21:24:34
@@ -66,6 +66,9 @@
                                            guint           prop_id,
                                            const GValue   *value,
                                            GParamSpec     *pspec);
+static GObject *gtk_layout_constructor    (GType                  type,
+					   guint                  n_properties,
+					   GObjectConstructParam *properties);
 static void gtk_layout_init               (GtkLayout      *layout);
 static void gtk_layout_finalize           (GObject        *object);
 static void gtk_layout_realize            (GtkWidget      *widget);
@@ -100,6 +103,8 @@
                                            GtkLayoutChild *child);
 static void gtk_layout_adjustment_changed (GtkAdjustment  *adjustment,
                                            GtkLayout      *layout);
+static void gtk_layout_set_adjustment_upper (GtkAdjustment *adj,
+					     gdouble        upper);
 
 
 static GtkWidgetClass *parent_class = NULL;
@@ -124,10 +129,11 @@
 {
   GtkLayout *layout;
 
-  layout = gtk_type_new (GTK_TYPE_LAYOUT);
+  layout = g_object_new (GTK_TYPE_LAYOUT,
+			 "hadjustment", hadjustment,
+			 "vadjustment", vadjustment,
+			 NULL);
 
-  gtk_layout_set_adjustments (layout, hadjustment, vadjustment);
-
   return GTK_WIDGET (layout);
 }
 
@@ -172,6 +178,12 @@
   return layout->vadjustment;
 }
 
+static GtkAdjustment *
+new_default_adjustment (void)
+{
+  return GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
+}
+
 static void           
 gtk_layout_set_adjustments (GtkLayout     *layout,
 			    GtkAdjustment *hadj,
@@ -183,12 +195,12 @@
 
   if (hadj)
     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
-  else
-    hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
+  else if (layout->hadjustment)
+    hadj = new_default_adjustment ();
   if (vadj)
     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
-  else
-    vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
+  else if (layout->vadjustment)
+    vadj = new_default_adjustment ();
   
   if (layout->hadjustment && (layout->hadjustment != hadj))
     {
@@ -207,6 +219,7 @@
       layout->hadjustment = hadj;
       gtk_object_ref (GTK_OBJECT (layout->hadjustment));
       gtk_object_sink (GTK_OBJECT (layout->hadjustment));
+      gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width);
       
       gtk_signal_connect (GTK_OBJECT (layout->hadjustment), "value_changed",
 			  (GtkSignalFunc) gtk_layout_adjustment_changed,
@@ -219,6 +232,7 @@
       layout->vadjustment = vadj;
       gtk_object_ref (GTK_OBJECT (layout->vadjustment));
       gtk_object_sink (GTK_OBJECT (layout->vadjustment));
+      gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height);
       
       gtk_signal_connect (GTK_OBJECT (layout->vadjustment), "value_changed",
 			  (GtkSignalFunc) gtk_layout_adjustment_changed,
@@ -226,7 +240,9 @@
       need_adjust = TRUE;
     }
 
-  if (need_adjust)
+  /* vadj or hadj can be NULL while constructing; don't emit a signal
+     then */
+  if (need_adjust && vadj && hadj)
     gtk_layout_adjustment_changed (NULL, layout);
 }
 
@@ -455,8 +471,10 @@
      }
   g_object_thaw_notify (G_OBJECT (layout));
 
-  gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width);
-  gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height);
+  if (layout->hadjustment)
+    gtk_layout_set_adjustment_upper (layout->hadjustment, layout->width);
+  if (layout->vadjustment)
+    gtk_layout_set_adjustment_upper (layout->vadjustment, layout->height);
 
   if (GTK_WIDGET_REALIZED (layout))
     {
@@ -565,6 +583,7 @@
   gobject_class->set_property = gtk_layout_set_property;
   gobject_class->get_property = gtk_layout_get_property;
   gobject_class->finalize = gtk_layout_finalize;
+  gobject_class->constructor = gtk_layout_constructor;
 
   container_class->set_child_property = gtk_layout_set_child_property;
   container_class->get_child_property = gtk_layout_get_child_property;
@@ -774,6 +793,30 @@
   layout->visibility = GDK_VISIBILITY_PARTIAL;
 
   layout->freeze_count = 0;
+}
+
+static GObject *
+gtk_layout_constructor (GType                  type,
+			guint                  n_properties,
+			GObjectConstructParam *properties)
+{
+  GtkLayout *layout;
+  GObject *object;
+  GtkAdjustment *hadj, *vadj;
+  
+  object = G_OBJECT_CLASS (parent_class)->constructor (type,
+						       n_properties,
+						       properties);
+
+  layout = GTK_LAYOUT (object);
+
+  hadj = layout->hadjustment ? layout->hadjustment : new_default_adjustment ();
+  vadj = layout->vadjustment ? layout->vadjustment : new_default_adjustment ();
+
+  if (!layout->hadjustment || !layout->vadjustment)
+    gtk_layout_set_adjustments (layout, hadj, vadj);
+
+  return object;
 }
 
 /* Widget methods


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]