[Gtk-sharp-list] patch for crash with non-default themes

Vladimir Vukicevic vladimir@pobox.com
04 Oct 2002 21:26:35 -0700


--=-rOOTyj6ab401j5s4P9C3
Content-Type: text/plain
Content-Transfer-Encoding: 7bit


This was an interesting one :)  Themes include a derivative object of
GtkStyle in the .so with a different name -- i.e. BluecurveStyle.  When
this name makes it to the ObjectManager.cs class in gtk-sharp, it was
trying to instantiate a "Bluecurve.Style,bluecurve-sharp" object, which
obviously dosen't exist.  The patch catches objects that don't have a
gtk# wrapping and walks up the gobject type hierarchy until it finds an
object that a wrapper does exist for (even if that's GObject).  It then
uses that type.  So, in the Style case, it will correctly return a
Gtk.Style C# type.

Note that this will very rarely happen in practice, except for themes. 
However, this has the nice side-effect that any function that returns
GObject-derived goop in an IntPtr can get wrapped in a GLib.Object, and
can be passed to other similarily-wrapped objects, etc, without an
explicit wrapper existing for that particular type.

Comments, or let me know if it's ok to commit.

Thanks!
	- Vlad

-- 
Vladimir Vukicevic <vladimir@pobox.com>

--=-rOOTyj6ab401j5s4P9C3
Content-Disposition: attachment; filename=unknown-object.patch
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-patch; name=unknown-object.patch; charset=ANSI_X3.4-1968

Index: ChangeLog
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/gtk-sharp/ChangeLog,v
retrieving revision 1.227
diff -u -u -r1.227 ChangeLog
--- ChangeLog	4 Oct 2002 16:38:47 -0000	1.227
+++ ChangeLog	5 Oct 2002 04:32:06 -0000
@@ -1,3 +1,11 @@
+2002-10-04  Vladimir Vukicevic  <vladimir@pobox.com>
+
+	* glib/ObjectManager.cs, glue/type.c: If there isn't
+	an exact match for a C GObject class (i.e. BluecurveStyle),
+	walk up the gobject type hierarchy until we find a type
+	that we do have a wrapper for, and return that.  This means
+	that you'll always, worst-case, end up with a GObject.
+
 2002-10-02  Vladimir Vukicevic  <vladimir@pobox.com>
=20
 	* gtk/TreeView.custom: added TreeView Handle as argument
Index: glib/ObjectManager.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/gtk-sharp/glib/ObjectManager.cs,v
retrieving revision 1.5
diff -u -u -r1.5 ObjectManager.cs
--- glib/ObjectManager.cs	20 Aug 2002 19:56:17 -0000	1.5
+++ glib/ObjectManager.cs	5 Oct 2002 04:32:06 -0000
@@ -26,10 +26,20 @@
 				mangled =3D (string)types[typename];
 			else
 				mangled =3D GetExpected (typename);
-
 			Type t =3D Type.GetType (mangled);
+
+			// if null, try to get a parent type
 			if (t =3D=3D null)
+				t =3D GetValidParentType (raw);
+
+			if (t =3D=3D null) {
+				// should never get reached since everything should end up at
+				// GObject. Perhaps throw an exception here instead?
+				Console.WriteLine ("*** Warning: No C# equivalent for class '" + typen=
ame +
+						   "' found, returning null");
 				return null;
+			}
+
 			return (GLib.Object) Activator.CreateInstance (t, new object[] {raw});
 		}
=20
@@ -51,14 +61,54 @@
 			for (int i =3D 0; i < cname.Length; i++)
 			{
 				if (needs_dot && i > 0 && Char.IsUpper (cname[i])) {
-					ns =3D expected.ToString ().ToLower ();=20
-					expected.Append ('.');
+					// check for initial "G" and mangle to "GLib" if so
+					// really only necessary for GObject
+					if (expected.Length =3D=3D 1 && expected[0] =3D=3D 'G') {
+						ns =3D "glib";
+						expected =3D new StringBuilder ("GLib.");
+					} else {
+						ns =3D expected.ToString ().ToLower ();=20
+						expected.Append ('.');
+					}
 					needs_dot =3D false;
 				}
 				expected.Append (cname[i]);
 			}
 			expected.AppendFormat (",{0}-sharp", ns);
-			return expected.ToString ();
+
+			string expected_string =3D expected.ToString ();
+			RegisterType (cname, expected_string);
+			return expected_string;
+		}
+
+		[DllImport("gtksharpglue")]
+		static extern int gtksharp_get_type_id (IntPtr raw);
+
+		[DllImport("gtksharpglue")]
+		static extern int gtksharp_get_parent_type (int typ);
+
+		[DllImport("gtksharpglue")]
+		static extern string gtksharp_get_type_name_for_id (int typ);
+
+		static Type GetValidParentType (IntPtr raw)
+		{
+			int type_id =3D gtksharp_get_type_id (raw);
+			string typename;
+			string mangled;
+			Type t;
+			// We will always end up at GObject and will break this loop
+			while (true) {
+				type_id =3D gtksharp_get_parent_type (type_id);
+				typename =3D gtksharp_get_type_name_for_id (type_id);
+				if (types.ContainsKey (typename))
+					mangled =3D (string)types[typename];
+				else
+					mangled =3D GetExpected (typename);
+				t =3D Type.GetType (mangled);
+				if (t !=3D null) {
+					return t;
+				}
+			}
 		}
 	}
 }
Index: glue/type.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/gtk-sharp/glue/type.c,v
retrieving revision 1.2
diff -u -u -r1.2 type.c
--- glue/type.c	1 Sep 2002 04:46:38 -0000	1.2
+++ glue/type.c	5 Oct 2002 04:32:06 -0000
@@ -19,3 +19,20 @@
 	return G_IS_OBJECT (obj);
 }
=20
+GType
+gtksharp_get_type_id (GObject *obj)
+{
+	return G_TYPE_FROM_INSTANCE (obj);
+}
+
+GType
+gtksharp_get_parent_type (GType typ)
+{
+	return g_type_parent (typ);
+}
+
+gchar *
+gtksharp_get_type_name_for_id (GType typ)
+{
+	return g_type_name (typ);
+}

--=-rOOTyj6ab401j5s4P9C3--