diff --git a/generator/InterfaceGen.cs b/generator/InterfaceGen.cs index c29fa16514..c3f80f34a5 100644 --- a/generator/InterfaceGen.cs +++ b/generator/InterfaceGen.cs @@ -58,6 +58,11 @@ namespace GtkSharp.Generation { } } + public override string CallByName (string var, bool owned) + { + return String.Format ("{0} == null ? IntPtr.Zero : (({0} is GLib.Object) ? ({0} as GLib.Object).{1} : ({0} as {2}Adapter).{1})", var, owned ? "OwnedHandle" : "Handle", QualifiedName); + } + public override string FromNative (string var, bool owned) { return QualifiedName + "Adapter.GetObject (" + var + ", " + (owned ? "true" : "false") + ")"; @@ -121,19 +126,23 @@ namespace GtkSharp.Generation { void GenerateCtors (StreamWriter sw) { + // Native GObjects do not implement the *Implementor interfaces + sw.WriteLine ("\t\tGLib.Object implementor;", Name); + sw.WriteLine (); + if (!IsConsumeOnly) { sw.WriteLine ("\t\tpublic " + Name + "Adapter ()"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tInitHandler = new GLib.GInterfaceInitHandler (Initialize);"); sw.WriteLine ("\t\t}"); sw.WriteLine (); - sw.WriteLine ("\t\t{0}Implementor implementor;", Name); - sw.WriteLine (); sw.WriteLine ("\t\tpublic {0}Adapter ({0}Implementor implementor)", Name); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tif (implementor == null)"); sw.WriteLine ("\t\t\t\tthrow new ArgumentNullException (\"implementor\");"); - sw.WriteLine ("\t\t\tthis.implementor = implementor;"); + sw.WriteLine ("\t\t\telse if (!(implementor is GLib.Object))"); + sw.WriteLine ("\t\t\t\tthrow new ArgumentException (\"implementor must be a subclass of GLib.Object\");"); + sw.WriteLine ("\t\t\tthis.implementor = implementor as GLib.Object;"); sw.WriteLine ("\t\t}"); sw.WriteLine (); } @@ -142,7 +151,7 @@ namespace GtkSharp.Generation { sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tif (!_gtype.IsInstance (handle))"); sw.WriteLine ("\t\t\t\tthrow new ArgumentException (\"The gobject doesn't implement the GInterface of this adapter\", \"handle\");"); - sw.WriteLine ("\t\t\tthis.handle = handle;"); + sw.WriteLine ("\t\t\timplementor = GLib.Object.GetObject (handle);"); sw.WriteLine ("\t\t}"); sw.WriteLine (); } @@ -163,16 +172,15 @@ namespace GtkSharp.Generation { void GenerateHandleProp (StreamWriter sw) { - sw.WriteLine ("\t\tIntPtr handle;"); sw.WriteLine ("\t\tpublic override IntPtr Handle {"); sw.WriteLine ("\t\t\tget {"); - if (IsConsumeOnly) { - sw.WriteLine ("\t\t\t\treturn handle;"); - } else { - sw.WriteLine ("\t\t\t\tif (handle != IntPtr.Zero)"); - sw.WriteLine ("\t\t\t\t\treturn handle;"); - sw.WriteLine ("\t\t\t\treturn implementor == null ? IntPtr.Zero : implementor.Handle;"); - } + sw.WriteLine ("\t\t\t\treturn implementor.Handle;"); + sw.WriteLine ("\t\t\t}"); + sw.WriteLine ("\t\t}"); + sw.WriteLine (); + sw.WriteLine ("\t\tpublic IntPtr OwnedHandle {"); + sw.WriteLine ("\t\t\tget {"); + sw.WriteLine ("\t\t\t\treturn implementor.OwnedHandle;"); sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t}"); sw.WriteLine (); @@ -206,7 +214,7 @@ namespace GtkSharp.Generation { { sw.WriteLine ("\t\tpublic " + Name + "Implementor Implementor {"); sw.WriteLine ("\t\t\tget {"); - sw.WriteLine ("\t\t\t\treturn implementor;"); + sw.WriteLine ("\t\t\t\treturn implementor as {0}Implementor;", Name); sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t}"); sw.WriteLine (); diff --git a/generator/MiniObjectGen.cs b/generator/MiniObjectGen.cs index c44f59c06c..48a1faf75a 100644 --- a/generator/MiniObjectGen.cs +++ b/generator/MiniObjectGen.cs @@ -59,6 +59,11 @@ namespace GtkSharp.Generation { } } + public override string CallByName (string var, bool owned) + { + return String.Format ("{0} == null ? IntPtr.Zero : {0}.{1}", var, owned ? "OwnedHandle" : "Handle"); + } + public override bool Validate () { ArrayList invalids = new ArrayList (); @@ -312,11 +317,6 @@ namespace GtkSharp.Generation { sw.Close (); } - public string CallByName (string name, bool owned) - { - return name + " == null ? IntPtr.Zero : " + name + (owned ? ".OwnedHandle" : ".Handle"); - } - public override string FromNative (string var, bool owned) { return "Gst.MiniObject.GetObject(" + var + (owned ? ", true" : "") + ") as " + QualifiedName; diff --git a/generator/ObjectBase.cs b/generator/ObjectBase.cs index 809f0e8948..30d1e94c9b 100644 --- a/generator/ObjectBase.cs +++ b/generator/ObjectBase.cs @@ -155,11 +155,13 @@ namespace GtkSharp.Generation { } } - public virtual string CallByName (string var, bool owned) + public override string CallByName (string var) { - return String.Format ("{0} == null ? IntPtr.Zero : ({0} as GLib.Object).{1}", var, owned ? "OwnedHandle" : "Handle"); + return CallByName (var, false); } + public abstract string CallByName (string var, bool owned); + public override string FromNative (string var, bool owned) { return "GLib.Object.GetObject(" + var + (owned ? ", true" : "") + ") as " + QualifiedName; diff --git a/generator/ObjectGen.cs b/generator/ObjectGen.cs index 7a7d561da0..3dfc700690 100644 --- a/generator/ObjectGen.cs +++ b/generator/ObjectGen.cs @@ -70,6 +70,11 @@ namespace GtkSharp.Generation { } } + public override string CallByName (string var, bool owned) + { + return String.Format ("{0} == null ? IntPtr.Zero : {0}.{1}", var, owned ? "OwnedHandle" : "Handle"); + } + public override bool Validate () { ArrayList invalids = new ArrayList (); diff --git a/generator/Parameters.cs b/generator/Parameters.cs index ec131b5e02..9f2e19dd43 100644 --- a/generator/Parameters.cs +++ b/generator/Parameters.cs @@ -262,10 +262,8 @@ namespace GtkSharp.Generation { call_parm += CallName; } else if (gen is IManualMarshaler) call_parm = "native_" + CallName; - else if (gen is MiniObjectGen) - call_parm = ((MiniObjectGen) gen).CallByName(CallName, Owned); - else if (gen is ObjectGen) - call_parm = ((ObjectGen) gen).CallByName(CallName, Owned); + else if (gen is ObjectBase) + call_parm = (gen as ObjectBase).CallByName(CallName, Owned); else call_parm = gen.CallByName(CallName); @@ -285,7 +283,7 @@ namespace GtkSharp.Generation { result [i] = (gen as IManualMarshaler).ReleaseNative ("native_" + CallName) + ";"; return result; } else if (PassAs != String.Empty && MarshalType != CSType) - if (gen is HandleBase) + if (gen is HandleBase) return new string [] { CallName + " = " + (gen as HandleBase).FromNative ("native_" + CallName, Owned) + ";" }; else return new string [] { CallName + " = " + gen.FromNative ("native_" + CallName) + ";" };