2002-11-10 10:03:51 +00:00
|
|
|
// GtkSharp.Generation.CallbackGen.cs - The Callback Generatable.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
//
|
2003-10-05 00:20:17 +00:00
|
|
|
// (c) 2002-2003 Mike Kestner
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
public class CallbackGen : GenBase, IGeneratable {
|
|
|
|
|
|
|
|
private Parameters parms;
|
2003-12-03 23:08:14 +00:00
|
|
|
private Signature sig = null;
|
|
|
|
private ImportSignature isig = null;
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
public CallbackGen (XmlElement ns, XmlElement elem) : base (ns, elem)
|
|
|
|
{
|
2003-12-03 23:08:14 +00:00
|
|
|
if (elem ["parameters"] != null) {
|
2003-06-14 17:30:32 +00:00
|
|
|
parms = new Parameters (elem ["parameters"], NS);
|
2003-12-03 23:08:14 +00:00
|
|
|
parms.HideData = true;
|
|
|
|
}
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2003-06-14 17:30:32 +00:00
|
|
|
public string MarshalType {
|
2002-11-10 10:03:51 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return NS + "Sharp." + Name + "Native";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-14 17:30:32 +00:00
|
|
|
public string MarshalReturnType {
|
2002-11-10 10:03:51 +00:00
|
|
|
get
|
|
|
|
{
|
|
|
|
return MarshalType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-14 17:30:32 +00:00
|
|
|
public string CallByName (string var_name)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
return var_name + ".NativeDelegate";
|
|
|
|
}
|
|
|
|
|
2003-06-14 17:30:32 +00:00
|
|
|
public string FromNative(string var)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2003-06-14 17:30:32 +00:00
|
|
|
public string FromNativeReturn(string var)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
return FromNative (var);
|
|
|
|
}
|
|
|
|
|
2003-06-14 17:30:32 +00:00
|
|
|
public virtual string ToNativeReturn(string var)
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
{
|
|
|
|
return CallByName (var);
|
|
|
|
}
|
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
public string GenWrapper (string ns, GenerationInfo gen_info)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
string wrapper = Name + "Native";
|
2003-10-05 00:20:17 +00:00
|
|
|
string qualname = ns + "Sharp." + wrapper;
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
isig = new ImportSignature (parms, NS);
|
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
StreamWriter sw = gen_info.OpenStream (qualname);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-05-14 00:27:00 +00:00
|
|
|
sw.WriteLine ("namespace " + ns + "Sharp {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tusing System;");
|
|
|
|
sw.WriteLine ();
|
2003-01-05 23:51:37 +00:00
|
|
|
|
|
|
|
sw.WriteLine ("#region Autogenerated code");
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-05-19 02:45:17 +00:00
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
XmlElement ret_elem = Elem["return-type"];
|
|
|
|
string rettype = ret_elem.GetAttribute("type");
|
2003-05-19 02:45:17 +00:00
|
|
|
string m_ret = table.GetMarshalReturnType (rettype);
|
|
|
|
string s_ret = table.GetCSType (rettype);
|
|
|
|
ClassBase ret_wrapper = table.GetClassGen (rettype);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
sw.WriteLine ("\tinternal delegate " + m_ret + " " + wrapper + "(" + isig.ToString() + ");");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
|
2003-06-14 17:30:32 +00:00
|
|
|
sw.WriteLine ("\tinternal class " + Name + "Wrapper : GLib.DelegateWrapper {");
|
2003-04-06 09:21:15 +00:00
|
|
|
if (m_ret != "void") {
|
2003-05-19 02:45:17 +00:00
|
|
|
if (table.IsEnum (rettype)) {
|
2003-04-06 09:21:15 +00:00
|
|
|
sw.WriteLine ("\t\tstatic int _dummy;");
|
|
|
|
} else if (ret_wrapper != null && (ret_wrapper is ObjectGen || ret_wrapper is OpaqueGen)) {
|
|
|
|
// Do nothing
|
2003-05-19 02:45:17 +00:00
|
|
|
} else if (!table.IsStruct (rettype) && !table.IsBoxed (rettype)) {
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
sw.WriteLine ("\t\tstatic {0} _dummy;", m_ret);
|
2003-04-06 09:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ();
|
2003-12-03 23:08:14 +00:00
|
|
|
sw.WriteLine ("\t\tpublic " + m_ret + " NativeCallback (" + isig.ToString() + ")");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
2003-04-06 09:21:15 +00:00
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
int count = (parms != null) ? parms.Count : 0;
|
|
|
|
int idx = 0;
|
|
|
|
bool need_sep = false;
|
|
|
|
string call_str = "";
|
2003-10-13 21:53:40 +00:00
|
|
|
string cleanup_str = "";
|
2002-11-10 10:03:51 +00:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
string parm_name = parms[i].Name;
|
|
|
|
string ctype = parms[i].CType;
|
2003-02-24 03:13:08 +00:00
|
|
|
|
|
|
|
if (i > 0 && parms[i].IsLength && parms[i-1].IsString)
|
|
|
|
continue;
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
if ((i == count - 1) && ctype == "gpointer" && (parm_name.EndsWith ("data") || parm_name.EndsWith ("data_or_owner")))
|
|
|
|
continue;
|
2003-02-24 03:13:08 +00:00
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
string cstype = parms[i].CSType;
|
2003-05-19 02:45:17 +00:00
|
|
|
ClassBase parm_wrapper = table.GetClassGen (ctype);
|
2003-10-13 21:53:40 +00:00
|
|
|
sw.Write("\t\t\t" + cstype + " _arg" + idx);
|
|
|
|
if (parms[i].PassAs == "out") {
|
|
|
|
sw.WriteLine(";");
|
|
|
|
cleanup_str += "\t\t\t" + parm_name + " = " + table.CallByName (ctype, "_arg" + idx) + ";\n";
|
|
|
|
} else
|
|
|
|
sw.WriteLine(" = " + table.FromNative (ctype, parm_name) + ";");
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
if (need_sep)
|
|
|
|
call_str += ", ";
|
|
|
|
else
|
|
|
|
need_sep = true;
|
2003-10-13 21:53:40 +00:00
|
|
|
call_str += String.Format ("{0} _arg{1}", parms[i].PassAs, idx);
|
2002-11-10 10:03:51 +00:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.Write ("\t\t\t");
|
|
|
|
string invoke = "_managed (" + call_str + ")";
|
|
|
|
if (m_ret != "void") {
|
2003-11-03 01:36:55 +00:00
|
|
|
if (cleanup_str == "")
|
|
|
|
sw.Write ("return ");
|
|
|
|
else {
|
|
|
|
sw.Write (m_ret + " ret = ");
|
|
|
|
cleanup_str += "\t\t\treturn ret;\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret_wrapper != null && (ret_wrapper is ObjectGen || ret_wrapper is OpaqueGen))
|
|
|
|
sw.WriteLine ("(({0}) {1}).Handle;", s_ret, invoke);
|
|
|
|
else if (table.IsStruct (rettype) || table.IsBoxed (rettype)) {
|
|
|
|
// Shoot. I have no idea what to do here.
|
|
|
|
sw.WriteLine ("IntPtr.Zero;");
|
|
|
|
} else if (table.IsEnum (rettype))
|
|
|
|
sw.WriteLine ("(int) {0};", invoke);
|
|
|
|
else
|
|
|
|
sw.WriteLine ("({0}) {1};", m_ret, table.ToNativeReturn (rettype, invoke));
|
2003-10-13 21:53:40 +00:00
|
|
|
} else
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine (invoke + ";");
|
2003-10-13 21:53:40 +00:00
|
|
|
|
|
|
|
if (cleanup_str != "")
|
|
|
|
sw.Write (cleanup_str);
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
2003-06-14 17:30:32 +00:00
|
|
|
sw.WriteLine ("\t\tinternal {0} NativeDelegate;", wrapper);
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\tprotected {0} _managed;", NS + "." + Name);
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
2003-04-06 09:21:15 +00:00
|
|
|
sw.WriteLine ("\t\tpublic {0} ({1} managed, object o) : base (o)", Name + "Wrapper", NS + "." + Name);
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\t\tNativeDelegate = new {0} (NativeCallback);", wrapper);
|
|
|
|
sw.WriteLine ("\t\t\t_managed = managed;");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ("\t}");
|
2003-01-05 23:51:37 +00:00
|
|
|
sw.WriteLine ("#endregion");
|
2003-10-05 00:37:24 +00:00
|
|
|
sw.WriteLine ("}");
|
|
|
|
sw.Close ();
|
2003-06-14 17:30:32 +00:00
|
|
|
return ns + "Sharp." + Name + "Wrapper";
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Generate ()
|
2003-10-05 00:20:17 +00:00
|
|
|
{
|
|
|
|
GenerationInfo gen_info = new GenerationInfo (NSElem);
|
|
|
|
Generate (gen_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Generate (GenerationInfo gen_info)
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
XmlElement ret_elem = Elem["return-type"];
|
|
|
|
if (ret_elem == null) {
|
|
|
|
Console.WriteLine("No return type in callback " + CName);
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-05-19 02:45:17 +00:00
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
2002-11-10 10:03:51 +00:00
|
|
|
string rettype = ret_elem.GetAttribute("type");
|
2003-05-19 02:45:17 +00:00
|
|
|
string s_ret = table.GetCSType (rettype);
|
2002-11-10 10:03:51 +00:00
|
|
|
if (s_ret == "") {
|
|
|
|
Console.WriteLine("rettype: " + rettype + " in callback " + CName);
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((parms != null) && !parms.Validate ()) {
|
|
|
|
Console.WriteLine(" in callback " + CName + " **** Stubbing it out ****");
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
parms = null;
|
|
|
|
}
|
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
sig = new Signature (parms);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-12-03 23:08:14 +00:00
|
|
|
StreamWriter sw = gen_info.OpenStream (Name);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.WriteLine ("namespace " + NS + " {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tusing System;");
|
|
|
|
sw.WriteLine ();
|
2003-12-03 23:08:14 +00:00
|
|
|
sw.WriteLine ("\tpublic delegate " + s_ret + " " + Name + "(" + sig.ToString() + ");");
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("}");
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
sw.Close ();
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-10-05 00:20:17 +00:00
|
|
|
GenWrapper (NS, gen_info);
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
Statistics.CBCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|