diff --git a/ChangeLog b/ChangeLog index 3d083fccc..0e37e5c9d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2005-03-04 Mike Kestner + + * generator/ImportSignature.cs : out param handling fix. + * generator/Makefile.am : kill StringGen.cs. + * generator/MethodBody.cs : simplify out param handling. + * generator/StringGen.cs : kill it, now using MarshalGen. + * generator/SymbolTable.cs : make non-const strings use an appropriate + MarshalGen. Non-const strings are now correctly marshaled as Utf8. + * glib/Marshaller.cs : add some utf8-fu for strdup/free marshaling. + Add a method to alloc/copy structs to native memory, unused yet. + * gtk/Gtk.metadata : partially fix a broken delegate. + 2005-03-04 Mike Kestner * generator/Method.cs : refactor out some retval logic. diff --git a/generator/ImportSignature.cs b/generator/ImportSignature.cs index 3d324b54c..b20da5ff7 100644 --- a/generator/ImportSignature.cs +++ b/generator/ImportSignature.cs @@ -56,7 +56,7 @@ namespace GtkSharp.Generation { parms [i] = ""; if (p.CType == "GError**") parms [i] += "out "; - else if (p.PassAs != "" && (!m_type.EndsWith ("IntPtr") || p.Generatable is LPGen || p.Generatable is LPUGen || UsesHandle (p.Generatable))) + else if (p.PassAs != "") parms [i] += p.PassAs + " "; parms [i] += m_type + " " + p.Name; } diff --git a/generator/Makefile.am b/generator/Makefile.am index bdb752948..e426c30f5 100644 --- a/generator/Makefile.am +++ b/generator/Makefile.am @@ -43,7 +43,6 @@ sources = \ SimpleBase.cs \ SimpleGen.cs \ Statistics.cs \ - StringGen.cs \ StructBase.cs \ StructGen.cs \ SymbolTable.cs \ diff --git a/generator/MethodBody.cs b/generator/MethodBody.cs index f7c9dbe5a..48c855ea3 100644 --- a/generator/MethodBody.cs +++ b/generator/MethodBody.cs @@ -86,16 +86,10 @@ namespace GtkSharp.Generation { if (p.CType == "GError**") { result [i] += "out "; } else if (p.PassAs != "") { - if (p.Generatable is LPGen || p.Generatable is LPUGen || !p.MarshalType.EndsWith ("IntPtr")) - result [i] += p.PassAs + " "; - - if (igen is EnumGen) - call_parm = p.Name + "_as_int"; - else if (igen is LPUGen || igen is LPGen) - call_parm = p.Name + "_as_ptr"; - else if (UsesHandle (igen)) { - call_parm = p.PassAs + " " + p.Name + "_handle"; - } + result [i] += p.PassAs + " "; + + if (p.CSType != p.MarshalType && !(igen is StructBase || igen is ByRefGen)) + call_parm = p.Name + "_as_native"; } if (p.CType == "GError**") { @@ -128,14 +122,8 @@ namespace GtkSharp.Generation { if (is_set) name = "value"; - if ((is_get || p.PassAs == "out") && UsesHandle (gen)) - sw.WriteLine(indent + "\t\t\tIntPtr " + name + "_handle;"); - - if (p.PassAs == "out" && gen is EnumGen) - sw.WriteLine(indent + "\t\t\tint " + name + "_as_int;"); - - if (p.PassAs == "out" && (gen is LPGen || gen is LPUGen)) - sw.WriteLine(indent + "\t\t\t" + gen.MarshalType + " " + name + "_as_ptr;"); + if ((is_get || p.PassAs == "out") && p.CSType != p.MarshalType && !(gen is StructBase || gen is ByRefGen)) + sw.WriteLine(indent + "\t\t\t" + gen.MarshalType + " " + name + "_as_native;"); if (p.IsArray && p.MarshalType != p.CSType) { sw.WriteLine(indent + "\t\t\t{0}[] native_{1} = new {0} [{1}.Length];", p.MarshalType.TrimEnd('[', ']'), name); @@ -170,14 +158,9 @@ namespace GtkSharp.Generation { IGeneratable gen = p.Generatable; - if (p.PassAs == "out" && gen is EnumGen) - sw.WriteLine(indent + "\t\t\t" + p.Name + " = (" + p.CSType + ") " + p.Name + "_as_int;"); - - if (p.PassAs == "out" && (gen is LPGen || gen is LPUGen)) - sw.WriteLine(indent + "\t\t\t" + p.Name + " = (" + p.CSType + ") " + p.Name + "_as_ptr;"); - - if (p.PassAs == "out" && UsesHandle (gen)) - sw.WriteLine(indent + "\t\t\t" + p.Name + " = " + gen.FromNativeReturn (p.Name + "_handle") + ";"); + string name = p.Name; + if (p.PassAs == "out" && p.CSType != p.MarshalType && !(gen is StructBase || gen is ByRefGen)) + sw.WriteLine(indent + "\t\t\t" + p.Name + " = " + gen.FromNative (p.Name + "_as_native") + ";"); } } diff --git a/generator/StringGen.cs b/generator/StringGen.cs deleted file mode 100644 index 3354c4713..000000000 --- a/generator/StringGen.cs +++ /dev/null @@ -1,51 +0,0 @@ -// GtkSharp.Generation.StringGen.cs - The String type Generatable. -// -// Author: Rachel Hestilow -// Mike Kestner -// -// Copyright (c) 2003 Rachel Hestilow -// Copyright (c) 2004 Novell, Inc. -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of version 2 of the GNU General Public -// License as published by the Free Software Foundation. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// General Public License for more details. -// -// You should have received a copy of the GNU General Public -// License along with this program; if not, write to the -// Free Software Foundation, Inc., 59 Temple Place - Suite 330, -// Boston, MA 02111-1307, USA. - - -namespace GtkSharp.Generation { - - using System; - - public class StringGen : ConstStringGen { - - public StringGen (string ctype) : base (ctype) - { - } - - public override string ToNativeReturnType { - get { - return "IntPtr"; - } - } - - public override string FromNativeReturn(String var) - { - return "GLib.Marshaller.PtrToStringGFree(" + var + ")"; - } - - public override string ToNativeReturn(String var) - { - return "GLib.Marshaller.StringToPtrGStrdup(" + var + ")"; - } - } -} - diff --git a/generator/SymbolTable.cs b/generator/SymbolTable.cs index 4c51713ab..ba3e5b7ed 100644 --- a/generator/SymbolTable.cs +++ b/generator/SymbolTable.cs @@ -1,9 +1,9 @@ // GtkSharp.Generation.SymbolTable.cs - The Symbol Table Class. // -// Author: Mike Kestner +// Author: Mike Kestner // // Copyright (c) 2001-2003 Mike Kestner -// Copyright (c) 2004 Novell, Inc. +// Copyright (c) 2004-2005 Novell, Inc. // // This program is free software; you can redistribute it and/or // modify it under the terms of version 2 of the GNU General Public @@ -90,8 +90,8 @@ namespace GtkSharp.Generation { AddType (new ConstStringGen ("const-gchar")); AddType (new ConstStringGen ("const-xmlChar")); AddType (new ConstStringGen ("const-char")); - AddType (new StringGen ("gchar")); - AddType (new StringGen ("char")); + AddType (new MarshalGen ("gchar", "string", "IntPtr", "GLib.Marshaller.StringToPtrGStrdup({0})", "GLib.Marshaller.PtrToStringGFree({0})")); + AddType (new MarshalGen ("char", "string", "IntPtr", "GLib.Marshaller.StringToPtrGStrdup({0})", "GLib.Marshaller.PtrToStringGFree({0})")); // manually wrapped types requiring more complex marshaling AddType (new ManualGen ("GObject", "GLib.Object", "GLib.Object.GetObject ({0})")); diff --git a/glib/Marshaller.cs b/glib/Marshaller.cs index 49d201536..7ffc76c47 100644 --- a/glib/Marshaller.cs +++ b/glib/Marshaller.cs @@ -32,8 +32,20 @@ namespace GLib { [DllImport("libglib-2.0-0.dll")] static extern void g_free (IntPtr mem); - public static string PtrToStringGFree (IntPtr ptr) { - string ret = Marshal.PtrToStringAnsi (ptr); + [DllImport("libglib-2.0-0.dll")] + static extern IntPtr g_utf8_strlen (IntPtr mem, int size); + + public static string Utf8PtrToString (IntPtr ptr) + { + int len = (int) g_utf8_strlen (ptr, -1); + byte[] bytes = new byte [len]; + Marshal.Copy (ptr, bytes, 0, len); + return System.Text.Encoding.UTF8.GetString (bytes); + } + + public static string PtrToStringGFree (IntPtr ptr) + { + string ret = Utf8PtrToString (ptr); g_free (ptr); return ret; } @@ -45,17 +57,18 @@ namespace GLib { // The last pointer is a null terminator. string[] ret = new string[ptrs.Length - 1]; for (int i = 0; i < ret.Length; i++) { - ret[i] = Marshal.PtrToStringAnsi (ptrs[i]); + ret[i] = Utf8PtrToString (ptrs[i]); g_free (ptrs[i]); } return ret; } [DllImport("libglib-2.0-0.dll")] - static extern IntPtr g_strdup (string str); + static extern IntPtr g_strdup (byte[] bytes); public static IntPtr StringToPtrGStrdup (string str) { - return g_strdup (str); + byte[] bytes = System.Text.Encoding.UTF8.GetBytes (str); + return g_strdup (bytes); } public static string StringFormat (string format, params object[] args) { @@ -204,6 +217,12 @@ namespace GLib { return glibsharp_utf16_to_unichar ((ushort) c); } + public static IntPtr PtrToStructureAlloc (object o) + { + IntPtr result = Marshal.AllocHGlobal (Marshal.SizeOf (o)); + Marshal.StructureToPtr (o, result, false); + return result; + } } } diff --git a/gtk/Gtk.metadata b/gtk/Gtk.metadata index 38e6d12b5..e1e2f2ac0 100644 --- a/gtk/Gtk.metadata +++ b/gtk/Gtk.metadata @@ -24,6 +24,7 @@ 1 1 1 + 1 1 GdkModifierType BindingsActivate