2002-01-08 20:05:47 +00:00
|
|
|
// GtkSharp.Generation.CallbackGen.cs - The Callback Generatable.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
//
|
2002-04-04 16:20:53 +00:00
|
|
|
// (c) 2002 Mike Kestner
|
2002-01-08 20:05:47 +00:00
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
2002-05-23 23:43:25 +00:00
|
|
|
public class CallbackGen : GenBase, IGeneratable {
|
|
|
|
|
|
|
|
private Parameters parms;
|
|
|
|
|
2002-06-21 20:25:43 +00:00
|
|
|
public CallbackGen (XmlElement ns, XmlElement elem) : base (ns, elem)
|
2002-05-23 23:43:25 +00:00
|
|
|
{
|
|
|
|
if (elem ["parameters"] != null)
|
|
|
|
parms = new Parameters (elem ["parameters"]);
|
2002-01-08 20:05:47 +00:00
|
|
|
}
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2002-01-08 20:05:47 +00:00
|
|
|
public String MarshalType {
|
|
|
|
get
|
|
|
|
{
|
2002-08-28 20:58:01 +00:00
|
|
|
return "GtkSharp." + NS + Name + "Native";
|
2002-01-08 20:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2002-07-26 06:08:52 +00:00
|
|
|
public String MarshalReturnType {
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return MarshalType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-01-08 20:05:47 +00:00
|
|
|
public String CallByName (String var_name)
|
|
|
|
{
|
2002-08-28 20:58:01 +00:00
|
|
|
return var_name + ".NativeDelegate";
|
2002-01-08 20:05:47 +00:00
|
|
|
}
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2002-02-08 23:56:27 +00:00
|
|
|
public String FromNative(String var)
|
|
|
|
{
|
2002-04-04 16:20:53 +00:00
|
|
|
return var;
|
2002-02-08 23:56:27 +00:00
|
|
|
}
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2002-07-26 06:08:52 +00:00
|
|
|
public String FromNativeReturn(String var)
|
|
|
|
{
|
|
|
|
return FromNative (var);
|
|
|
|
}
|
|
|
|
|
2002-08-28 20:58:01 +00:00
|
|
|
private void GenWrapper (string s_ret, string sig)
|
|
|
|
{
|
|
|
|
char sep = Path.DirectorySeparatorChar;
|
|
|
|
string dir = ".." + sep + NS.ToLower() + sep + "generated";
|
|
|
|
|
|
|
|
if (!Directory.Exists (dir))
|
|
|
|
Directory.CreateDirectory (dir);
|
|
|
|
|
|
|
|
string wrapper = NS + Name + "Native";
|
|
|
|
|
|
|
|
string filename = dir + sep + "GtkSharp." + wrapper + ".cs";
|
|
|
|
|
|
|
|
FileStream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
|
|
|
|
StreamWriter sw = new StreamWriter (stream);
|
|
|
|
|
|
|
|
sw.WriteLine ("// Generated File. Do not modify.");
|
|
|
|
sw.WriteLine ("// <c> 2001-2002 Mike Kestner");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("namespace GtkSharp {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
sw.WriteLine ("\tusing System;");
|
|
|
|
sw.WriteLine ("\tusing System.Collections;");
|
|
|
|
|
|
|
|
string import_sig;
|
|
|
|
if (parms != null)
|
|
|
|
{
|
|
|
|
parms.CreateSignature (false);
|
|
|
|
import_sig = parms.ImportSig;
|
|
|
|
} else
|
|
|
|
import_sig = "";
|
|
|
|
|
|
|
|
XmlElement ret_elem = Elem["return-type"];
|
|
|
|
string rettype = ret_elem.GetAttribute("type");
|
|
|
|
string m_ret = SymbolTable.GetMarshalReturnType (rettype);
|
|
|
|
|
|
|
|
sw.WriteLine ("\tpublic delegate " + m_ret + " " + wrapper + "(" + import_sig + ");");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
sw.WriteLine ("\tpublic class " + NS + Name + "Wrapper : GLib.DelegateWrapper {");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\tpublic " + m_ret + " NativeCallback (" + import_sig + ")");
|
|
|
|
sw.WriteLine ("\t\t{");
|
|
|
|
int count = (parms != null) ? parms.Count : 0;
|
|
|
|
if (count > 0)
|
|
|
|
sw.WriteLine ("\t\t\tobject[] _args = new object[{0}];", count - 1);
|
|
|
|
int idx = 0;
|
|
|
|
bool need_sep = false;
|
|
|
|
string call_str = "";
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
string parm_name = parms[i].Name;
|
|
|
|
string ctype = parms[i].CType;
|
|
|
|
if (ctype == "gpointer" && (parm_name.EndsWith ("data") || parm_name.EndsWith ("data_or_owner")))
|
|
|
|
continue;
|
|
|
|
string cstype = parms[i].CSType;
|
|
|
|
// FIXME: Too much code copy/pasted here. Refactor?
|
|
|
|
ClassBase parm_wrapper = SymbolTable.GetClassGen (ctype);
|
|
|
|
if (parm_wrapper != null && (parm_wrapper is StructBase)) {
|
|
|
|
sw.WriteLine("\t\t\t{0}._Initialize ();", parm_name);
|
|
|
|
}
|
|
|
|
sw.WriteLine("\t\t\t_args[" + idx + "] = " + SymbolTable.FromNative (ctype, parm_name) + ";");
|
|
|
|
if ((parm_wrapper != null && ((parm_wrapper is OpaqueGen))) || SymbolTable.IsManuallyWrapped (ctype)) {
|
|
|
|
sw.WriteLine("\t\t\tif (_args[" + idx + "] == null)");
|
|
|
|
sw.WriteLine("\t\t\t\t_args[{0}] = new {1}({2});", idx, cstype, parm_name);
|
|
|
|
}
|
|
|
|
if (need_sep)
|
|
|
|
call_str += ", ";
|
|
|
|
else
|
|
|
|
need_sep = true;
|
|
|
|
call_str += String.Format ("({0}) _args[{1}]", cstype, idx);
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.Write ("\t\t\t");
|
|
|
|
string invoke = "_managed (" + call_str + ")";
|
|
|
|
if (m_ret != "void") {
|
|
|
|
ClassBase parm_wrapper = SymbolTable.GetClassGen (rettype);
|
|
|
|
if (parm_wrapper != null && (parm_wrapper is ObjectGen || parm_wrapper is OpaqueGen))
|
|
|
|
sw.WriteLine ("return (({0}) {1}).Handle;", s_ret, invoke);
|
|
|
|
else if (SymbolTable.IsStruct (rettype) || SymbolTable.IsBoxed (rettype)) {
|
|
|
|
// Shoot. I have no idea what to do here.
|
|
|
|
sw.WriteLine ("return IntPtr.Zero;");
|
|
|
|
}
|
|
|
|
else if (SymbolTable.IsEnum (rettype))
|
|
|
|
sw.WriteLine ("return (int) {0};", invoke);
|
|
|
|
else
|
|
|
|
sw.WriteLine ("return ({0}) {1};", s_ret, invoke);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sw.WriteLine (invoke + ";");
|
|
|
|
sw.WriteLine ("\t\t}");
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\tpublic {0} NativeDelegate;", wrapper);
|
|
|
|
sw.WriteLine ("\t\tprotected {0} _managed;", NS + "." + Name);
|
|
|
|
sw.WriteLine ();
|
|
|
|
|
|
|
|
sw.WriteLine ("\t\tpublic {0} ({1} managed) : base ()", NS + Name + "Wrapper", NS + "." + Name);
|
|
|
|
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}");
|
|
|
|
|
|
|
|
CloseWriter (sw);
|
|
|
|
}
|
|
|
|
|
2002-05-23 23:43:25 +00:00
|
|
|
public void Generate ()
|
2002-01-08 20:05:47 +00:00
|
|
|
{
|
2002-08-19 Rachel Hestilow <hestilow@ximian.com>
* art/Makefile.in (clean): Change to avoid bugging out on generated/CVS.
* glib/ObjectManager.cs: Added. Used to be auto-generated, but
now it can infer names, and relies on per-namespace ObjectManager
classes to inform it of oddly-named classes.
* generator/IGeneratable.cs, GenBase.cs: New "DoGenerate" property.
* generator/*Gen.cs: Honor DoGenerate.
* generator/CodeGenerator.cs: Support including dependency files
which will not be generated.
* generator/ObjectGen.cs: Generate mapping file per-namespace, as one
that calls back to the one in glib. Only generate if the name does
not follow the normal conventions, otherwise, GtkSharp.ObjectManager
can infer the name.
* generator/Parser.cs: Accept 'generate' flag to pass on to the
IGeneratables. Parse a new toplevel element, "symbol", which adds
a type to the SymbolTable (instead of hard-coding it).
* generator/SignalHandler.cs: Do not optimize signal handler creation,
instead creating them in their own namespaces. Do not generate
if the calling Signal told us not to.
* generator/Signal.cs: Do not generate handlers if container's DoGenerate
is false. Adjust to the marshaller name being in a sub-namespace.
* generator/SymbolTable.cs (AddSimpleType, AddManualType): Used
to add simple and manually wrapped types at runtime instead of
compile-time.
(FromNative): Remove hard-coded cases for manually wrapped types, use
a generic case instead.
* api: Added. Move api files and generation targets here.
* source: Added. Move source parsing here.
* generator/makefile: Move actual generation to api/.
* glib/Makefile.in: Remove generated/* target.
* glue/Makefile.am: Fix to include canvas-marshal. Move canvas stuff
to GNOME target.
* gnome/CanvasProxy.cs: Update to work with SignalHandlers being
namespace-specific.
* parser/Metadata.pm: Moved to GAPI/Metadata.pm, renamed, etc.
* parser/gapi2xml.pl: Use GAPI::Metadata.
* parser/makefile: Install scripts, remove source parse build target.
Rename formatXML to gapi_format_xml.
svn path=/trunk/gtk-sharp/; revision=6818
2002-08-20 19:56:18 +00:00
|
|
|
if (!DoGenerate)
|
|
|
|
return;
|
|
|
|
|
2002-05-23 23:43:25 +00:00
|
|
|
XmlElement ret_elem = Elem["return-type"];
|
2002-04-04 16:20:53 +00:00
|
|
|
if (ret_elem == null) {
|
2002-05-23 23:43:25 +00:00
|
|
|
Console.WriteLine("No return type in callback " + CName);
|
2002-04-04 16:20:53 +00:00
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return;
|
|
|
|
}
|
2002-05-23 23:43:25 +00:00
|
|
|
|
2002-04-04 16:20:53 +00:00
|
|
|
string rettype = ret_elem.GetAttribute("type");
|
2002-08-28 20:58:01 +00:00
|
|
|
string s_ret = SymbolTable.GetCSType (rettype);
|
2002-04-04 16:20:53 +00:00
|
|
|
if (s_ret == "") {
|
|
|
|
Console.WriteLine("rettype: " + rettype + " in callback " + CName);
|
|
|
|
Statistics.ThrottledCount++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-05-23 23:43:25 +00:00
|
|
|
if ((parms != null) && !parms.Validate ()) {
|
2002-05-29 08:13:46 +00:00
|
|
|
Console.WriteLine(" in callback " + CName + " **** Stubbing it out ****");
|
2002-05-23 23:43:25 +00:00
|
|
|
Statistics.ThrottledCount++;
|
2002-05-29 08:13:46 +00:00
|
|
|
parms = null;
|
2002-04-04 16:20:53 +00:00
|
|
|
}
|
2002-05-23 23:43:25 +00:00
|
|
|
|
|
|
|
StreamWriter sw = CreateWriter ();
|
|
|
|
|
|
|
|
string sig = "";
|
2002-08-28 20:58:01 +00:00
|
|
|
if (parms != null) {
|
|
|
|
parms.HideData = true;
|
|
|
|
parms.CreateSignature (false);
|
2002-05-23 23:43:25 +00:00
|
|
|
sig = parms.Signature;
|
2002-08-28 20:58:01 +00:00
|
|
|
}
|
2002-05-23 23:43:25 +00:00
|
|
|
|
|
|
|
sw.WriteLine ("\tpublic delegate " + s_ret + " " + Name + "(" + sig + ");");
|
|
|
|
|
|
|
|
CloseWriter (sw);
|
2002-08-28 20:58:01 +00:00
|
|
|
|
|
|
|
GenWrapper (s_ret, sig);
|
|
|
|
|
2002-05-23 23:43:25 +00:00
|
|
|
Statistics.CBCount++;
|
2002-04-04 16:20:53 +00:00
|
|
|
}
|
2002-01-08 20:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|