2002-11-10 10:03:51 +00:00
|
|
|
// GtkSharp.Generation.SignalHandler.cs - The SignalHandler marshaling Class.
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
//
|
2003-05-19 02:45:17 +00:00
|
|
|
// (c) 2002-2003 Mike Kestner
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
namespace GtkSharp.Generation {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
public class SignalHandler {
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
XmlElement sig;
|
|
|
|
string ns;
|
|
|
|
string retval = "";
|
|
|
|
string s_ret = "";
|
|
|
|
string p_ret = "";
|
|
|
|
Parameters parms = null;
|
|
|
|
|
|
|
|
public SignalHandler (XmlElement sig, string ns)
|
|
|
|
{
|
|
|
|
this.sig = sig;
|
|
|
|
this.ns = ns;
|
|
|
|
XmlElement params_elem = sig["parameters"] as XmlElement;
|
|
|
|
if (params_elem != null)
|
|
|
|
parms = new Parameters (params_elem, ns);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Validate ()
|
2002-11-10 10:03:51 +00:00
|
|
|
{
|
|
|
|
XmlElement ret_elem = sig["return-type"];
|
|
|
|
if (ret_elem == null) {
|
|
|
|
Console.Write("Missing return-type ");
|
2003-09-15 04:20:33 +00:00
|
|
|
return false;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
retval = ret_elem.GetAttribute("type");
|
2002-11-10 10:03:51 +00:00
|
|
|
if (retval == "") {
|
|
|
|
Console.Write("Invalid return-type ");
|
2003-09-15 04:20:33 +00:00
|
|
|
return false;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
s_ret = SymbolTable.Table.GetCSType(retval);
|
|
|
|
p_ret = SymbolTable.Table.GetMarshalReturnType(retval);
|
2002-11-10 10:03:51 +00:00
|
|
|
if ((s_ret == "") || (p_ret == "")) {
|
|
|
|
Console.Write("Funky type: " + retval);
|
2003-09-15 04:20:33 +00:00
|
|
|
return false;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
if (parms == null || !parms.Validate ()) {
|
2002-11-10 10:03:51 +00:00
|
|
|
Console.Write("Missing parameters ");
|
2003-09-15 04:20:33 +00:00
|
|
|
return false;
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string ISig {
|
|
|
|
get {
|
|
|
|
string result = "";
|
|
|
|
for (int i = 0; i < parms.Count; i++) {
|
|
|
|
if (i > 0)
|
|
|
|
result += ", ";
|
|
|
|
|
|
|
|
result += (parms[i].MarshalType + " arg" + i);
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
2003-09-15 04:20:33 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string BaseName {
|
|
|
|
get {
|
|
|
|
string result = SymbolTable.Table.GetName (retval);
|
|
|
|
for (int i = 0; i < parms.Count; i++) {
|
|
|
|
if (parms[i].Generatable is ObjectGen || parms[i].Generatable is InterfaceGen) {
|
|
|
|
result += "Object";
|
|
|
|
} else {
|
|
|
|
result += SymbolTable.Table.GetName(parms[i].CType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
public string Name {
|
|
|
|
get {
|
|
|
|
return BaseName + "Signal";
|
|
|
|
}
|
|
|
|
}
|
2002-11-10 10:03:51 +00:00
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
public void Generate (string implementor_ns)
|
|
|
|
{
|
|
|
|
SymbolTable table = SymbolTable.Table;
|
|
|
|
|
|
|
|
string sname = Name;
|
|
|
|
string dname = BaseName + "Delegate";
|
|
|
|
string cbname = BaseName + "Callback";
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
char sep = Path.DirectorySeparatorChar;
|
2003-09-15 04:20:33 +00:00
|
|
|
String dir = ".." + sep + implementor_ns.ToLower() + sep + "generated";
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
if (!Directory.Exists(dir)) {
|
|
|
|
Directory.CreateDirectory(dir);
|
|
|
|
}
|
|
|
|
|
2003-09-15 04:20:33 +00:00
|
|
|
String filename = dir + sep + implementor_ns + "Sharp." + sname + ".cs";
|
2002-11-10 10:03:51 +00:00
|
|
|
|
|
|
|
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 ();
|
2003-09-15 04:20:33 +00:00
|
|
|
sw.WriteLine("namespace " + implementor_ns + "Sharp {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine();
|
|
|
|
sw.WriteLine("\tusing System;");
|
|
|
|
sw.WriteLine("\tusing System.Runtime.InteropServices;");
|
|
|
|
sw.WriteLine("\tusing GtkSharp;");
|
|
|
|
sw.WriteLine();
|
2003-05-08 04:44:13 +00:00
|
|
|
sw.Write("\tinternal delegate " + p_ret + " ");
|
2003-09-15 04:20:33 +00:00
|
|
|
sw.WriteLine(dname + "(" + ISig + ", int key);");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine();
|
2003-05-08 04:44:13 +00:00
|
|
|
sw.WriteLine("\tinternal class " + sname + " : SignalCallback {");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine();
|
|
|
|
sw.WriteLine("\t\tprivate static " + dname + " _Delegate;");
|
|
|
|
sw.WriteLine();
|
2003-02-26 02:16:38 +00:00
|
|
|
sw.WriteLine("\t\tprivate IntPtr _raw;");
|
|
|
|
sw.WriteLine("\t\tprivate uint _HandlerID;");
|
|
|
|
sw.WriteLine();
|
2003-02-19 06:23:36 +00:00
|
|
|
sw.Write("\t\tprivate static " + p_ret + " ");
|
2003-09-15 04:20:33 +00:00
|
|
|
sw.WriteLine(cbname + "(" + ISig + ", int key)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t{");
|
|
|
|
sw.WriteLine("\t\t\tif (!_Instances.Contains(key))");
|
|
|
|
sw.WriteLine("\t\t\t\tthrow new Exception(\"Unexpected signal key \" + key);");
|
|
|
|
sw.WriteLine();
|
|
|
|
sw.WriteLine("\t\t\t" + sname + " inst = (" + sname + ") _Instances[key];");
|
|
|
|
if ((s_ret == "void") && (parms.Count == 1)) {
|
|
|
|
sw.WriteLine("\t\t\tEventHandler h = (EventHandler) inst._handler;");
|
|
|
|
sw.WriteLine("\t\t\th (inst._obj, new EventArgs ());");
|
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine();
|
|
|
|
} else {
|
|
|
|
sw.WriteLine("\t\t\tSignalArgs args = (SignalArgs) Activator.CreateInstance (inst._argstype);");
|
|
|
|
if (parms.Count > 1) {
|
|
|
|
sw.WriteLine("\t\t\targs.Args = new object[" + (parms.Count-1) + "];");
|
|
|
|
}
|
|
|
|
for (int idx=1; idx < parms.Count; idx++) {
|
2003-07-15 05:52:09 +00:00
|
|
|
// sw.WriteLine("\t\t\tConsole.WriteLine (\"" + sname + " arg{0}: \" + arg{0});", idx);
|
2003-09-15 04:20:33 +00:00
|
|
|
string ctype = parms[idx].CType;
|
2003-05-19 02:45:17 +00:00
|
|
|
ClassBase wrapper = table.GetClassGen (ctype);
|
|
|
|
if ((wrapper != null && !(wrapper is StructBase)) || table.IsManuallyWrapped (ctype)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\tif (arg{0} == IntPtr.Zero)", idx);
|
|
|
|
sw.WriteLine("\t\t\t\targs.Args[{0}] = null;", idx - 1);
|
|
|
|
sw.WriteLine("\t\t\telse {");
|
|
|
|
if (wrapper != null && wrapper is ObjectGen)
|
2003-08-19 04:52:25 +00:00
|
|
|
sw.WriteLine("\t\t\t\targs.Args[" + (idx-1) + "] = GLib.Object.GetObject(arg" + idx + ", false);");
|
2002-11-10 10:03:51 +00:00
|
|
|
else
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine("\t\t\t\targs.Args[" + (idx-1) + "] = " + table.FromNative (ctype, "arg" + idx) + ";");
|
|
|
|
if ((wrapper != null && (wrapper is OpaqueGen)) || table.IsManuallyWrapped (ctype)) {
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\t\tif (args.Args[" + (idx-1) + "] == null)");
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine("\t\t\t\t\targs.Args[{0}] = new {1}(arg{2});", idx-1, table.GetCSType (ctype), idx);
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
sw.WriteLine("\t\t\t}");
|
|
|
|
} else {
|
2003-05-19 02:45:17 +00:00
|
|
|
sw.WriteLine("\t\t\targs.Args[" + (idx-1) + "] = " + table.FromNative (ctype, "arg" + idx) + ";");
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sw.WriteLine("\t\t\tobject[] argv = new object[2];");
|
|
|
|
sw.WriteLine("\t\t\targv[0] = inst._obj;");
|
|
|
|
sw.WriteLine("\t\t\targv[1] = args;");
|
|
|
|
sw.WriteLine("\t\t\tinst._handler.DynamicInvoke(argv);");
|
|
|
|
if (retval != "void") {
|
|
|
|
sw.WriteLine ("\t\t\tif (args.RetVal == null)");
|
|
|
|
if (s_ret == "bool")
|
|
|
|
sw.WriteLine ("\t\t\t\treturn false;");
|
|
|
|
else
|
|
|
|
sw.WriteLine ("\t\t\t\tthrow new Exception(\"args.RetVal unset in callback\");");
|
|
|
|
|
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\t\treturn (" + p_ret + ") " + table.ToNativeReturn (retval, "((" + s_ret + ")args.RetVal)") + ";");
|
2002-11-10 10:03:51 +00:00
|
|
|
}
|
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine();
|
|
|
|
}
|
2003-02-26 02:16:38 +00:00
|
|
|
sw.WriteLine("\t\t[DllImport(\"libgobject-2.0-0.dll\")]");
|
|
|
|
sw.Write("\t\tstatic extern uint g_signal_connect_data(");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.Write("IntPtr obj, String name, " + dname + " cb, int key, IntPtr p,");
|
|
|
|
sw.WriteLine(" int flags);");
|
|
|
|
sw.WriteLine();
|
|
|
|
sw.Write("\t\tpublic " + sname + "(GLib.Object obj, IntPtr raw, ");
|
2003-02-06 00:58:02 +00:00
|
|
|
sw.WriteLine("String name, Delegate eh, Type argstype) : base(obj, eh, argstype)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t{");
|
|
|
|
sw.WriteLine("\t\t\tif (_Delegate == null) {");
|
|
|
|
sw.WriteLine("\t\t\t\t_Delegate = new " + dname + "(" + cbname + ");");
|
|
|
|
sw.WriteLine("\t\t\t}");
|
2003-02-26 02:16:38 +00:00
|
|
|
sw.WriteLine("\t\t\t_raw = raw;");
|
|
|
|
sw.Write("\t\t\t_HandlerID = g_signal_connect_data(raw, name, ");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("_Delegate, _key, new IntPtr(0), 0);");
|
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine();
|
2003-02-26 02:16:38 +00:00
|
|
|
sw.WriteLine("\t\t[DllImport(\"libgobject-2.0-0.dll\")]");
|
|
|
|
sw.WriteLine("\t\tstatic extern void g_signal_handler_disconnect (IntPtr instance, uint handler);");
|
|
|
|
sw.WriteLine();
|
|
|
|
sw.WriteLine("\t\tprotected override void Dispose (bool disposing)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t{");
|
|
|
|
sw.WriteLine("\t\t\t_Instances.Remove(_key);");
|
2003-02-28 10:55:59 +00:00
|
|
|
sw.WriteLine("\t\t\tif(_Instances.Count == 0)");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t\t\t_Delegate = null;");
|
2003-02-28 10:55:59 +00:00
|
|
|
sw.WriteLine();
|
|
|
|
sw.WriteLine("\t\t\tg_signal_handler_disconnect (_raw, _HandlerID);");
|
|
|
|
sw.WriteLine("\t\t\tbase.Dispose (disposing);");
|
2002-11-10 10:03:51 +00:00
|
|
|
sw.WriteLine("\t\t}");
|
|
|
|
sw.WriteLine("\t}");
|
|
|
|
sw.WriteLine("}");
|
|
|
|
sw.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|