From bb8d2c4ee92055399eb6a7c9ac74b5b37b08574f Mon Sep 17 00:00:00 2001 From: Mike Kestner Date: Sun, 25 Nov 2001 00:25:47 +0000 Subject: [PATCH] 2001-11-24 Mike Kestner * codegen/defs-parse.pl : mkdir the glib/generated dir. (gen_signal): Call new get_sighandler sub. Doesn't use the returned value yet. s/event/ev3nt on arg names. (get_sighandler): new sub to lookup or gen a signal helper/delegate. Only generates the delegate so far. * codegen/hardcoded.defs : Added a stub for Gdk.Event. * gdk/Event.cs : Killed, now a generated struct. * gdk/SimpleEvent.cs (SimpleEventCallback): Use Marshal.PtrToStructure to create the Event, not a ctor(IntPtr). * glib/SignalCallback.cs : New abstract base class for signal helpers. svn path=/trunk/gtk-sharp/; revision=1437 --- ChangeLog | 13 +++++++ codegen/defs-parse.pl | 77 +++++++++++++++++++++++++++++++++++++++--- codegen/hardcoded.defs | 8 +++++ gdk/Event.cs | 47 -------------------------- gdk/SimpleEvent.cs | 5 +-- glib/SignalCallback.cs | 58 +++++++++++++++++++++++++++++++ 6 files changed, 154 insertions(+), 54 deletions(-) delete mode 100644 gdk/Event.cs create mode 100644 glib/SignalCallback.cs diff --git a/ChangeLog b/ChangeLog index 5aee1c12e..a03ed2ac4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2001-11-24 Mike Kestner + + * codegen/defs-parse.pl : mkdir the glib/generated dir. + (gen_signal): Call new get_sighandler sub. Doesn't use the returned + value yet. s/event/ev3nt on arg names. + (get_sighandler): new sub to lookup or gen a signal helper/delegate. + Only generates the delegate so far. + * codegen/hardcoded.defs : Added a stub for Gdk.Event. + * gdk/Event.cs : Killed, now a generated struct. + * gdk/SimpleEvent.cs (SimpleEventCallback): Use Marshal.PtrToStructure + to create the Event, not a ctor(IntPtr). + * glib/SignalCallback.cs : New abstract base class for signal helpers. + 2001-11-14 Mike Kestner * codegen/defs-parse.pl : Add System.Collections to usings. diff --git a/codegen/defs-parse.pl b/codegen/defs-parse.pl index e5d2001f4..3ce4f0f69 100755 --- a/codegen/defs-parse.pl +++ b/codegen/defs-parse.pl @@ -18,9 +18,12 @@ %usings = ( - 'Gdk', "System,System.Collections,System.Runtime.InteropServices,GLib", - 'Gtk', "System,System.Collections,System.Runtime.InteropServices,GLib,Gdk"); + 'GLib', "System,System.Collections,System.Runtime.InteropServices,GLib", + 'Gdk', "System,System.Collections,System.Runtime.InteropServices,GLib,Gdk", + 'Gtk', "System,System.Collections,System.Runtime.InteropServices,GLib,Gdk,Gtk", + 'GtkSharp', "System,System.Collections,System.Runtime.InteropServices,GLib,Gdk,Gtk"); +`mkdir -p ../glib/generated`; `mkdir -p ../gdk/generated`; `mkdir -p ../gtk/generated`; @@ -241,7 +244,6 @@ sub gen_object } } - print "Generating Class $typename in $dir/$typename.cs\n"; open (OUTFILE, ">$dir/$typename.cs") || die "can't open file"; print OUTFILE "// Generated file: Do not modify\n\n"; @@ -299,13 +301,12 @@ sub gen_object print OUTFILE `cat $custom` if -e $custom; print OUTFILE "\t}\n}\n"; close (OUTFILE); - print "done\n"; } sub gen_signal { my ($name, $def, $dll) = @_; - my ($cname, @plist, $ret, $sret, $mret, $code); + my ($marsh, $cname, @plist, $ret, $sret, $mret, $code); $def =~ /define-signal (\w+)/; $cname = "\"$1\""; @@ -316,6 +317,8 @@ sub gen_signal $def =~ /parameters\s*'\((.*)\)\)\)/; @plist = split(/\)'\(/, $1); + $marsh = get_sighandler ($def); + if (($ret eq "none") && (@plist == 1)) { $marsh = "SimpleSignal"; } elsif (($ret eq "gboolean") && (@plist == 2) && @@ -494,6 +497,7 @@ sub gen_param_strings $ptype = $1; $pname = $2; $pname =~ s/object/objekt/; + $pname =~ s/event/ev3nt/; if ($sig) { $sig .= ', '; $call .= ', '; @@ -518,3 +522,66 @@ sub gen_param_strings return ($call, $pinv, $sig); } +sub get_sighandler +{ + my ($def) = @_; + my ($key, $name, $sname, $dname, $dir, $ns, $nspace, $tok); + + $def =~ /return-type \"(\w+)\"/; + my $ret = $1; + + $def =~ /parameters'\((.*)\)\)\)/; + my @parms = split(/\)'\(/, $1); + + for ($i = 1; $i < @parms; $i++) { + $parms[$i] =~ /^\"(\w+)/; + $key .= ":$1"; + } + $key = $ret . $key; + + if (exists($sighandlers{$key})) { + return $sighandlers{$key}; + } + + my ($call, $pinv, $sig) = gen_param_strings($def); + + if ($key =~ /Gtk/) { + $dir = "../gtk/generated"; + $nspace = "Gtk"; + } elsif ($key =~ /Gdk/) { + $dir = "../gdk/generated"; + $nspace = "Gdk"; + } else { + $dir = "../glib/generated"; + $nspace = "GLib"; + } + + $name = ""; + foreach $tok (split(/:/, $key)) { + if (exists($objects{$tok})) { + $name .= "Object"; + } elsif (exists($maptypes{$tok})) { + $name .= "$maptypes{$tok}"; + } else { + die "Whassup with $tok?"; + } + } + $sname = $name . "Signal"; + $dname = $name . "Delegate"; + + $sighandlers{$key} = $name; + + open (SIGFILE, ">$dir/$sname.cs") || die "can't open file"; + + print SIGFILE "// Generated file: Do not modify\n\n"; + print SIGFILE "namespace GtkSharp {\n\n"; + foreach $ns (split (/,/, $usings{$nspace})) { + print SIGFILE "\tusing $ns;\n"; + } + print SIGFILE "\tpublic delegate $marshaltypes{$ret} "; + print SIGFILE "$dname($pinv, int key);\n\n"; + print SIGFILE "}\n"; + close (SIGFILE); + + return $sighandlers{$key}; +} diff --git a/codegen/hardcoded.defs b/codegen/hardcoded.defs index 1d80e7ad2..414a9bfdb 100644 --- a/codegen/hardcoded.defs +++ b/codegen/hardcoded.defs @@ -16,6 +16,14 @@ ) ) +(define-struct Event + (in-module "Gdk") + (c-name "GdkEvent") + (fields + '("GdkEventType" "type") + ) +) + (define-struct AccelEntry (in-module "Gtk") (c-name "GtkAccelEntry") diff --git a/gdk/Event.cs b/gdk/Event.cs deleted file mode 100644 index 25b18b519..000000000 --- a/gdk/Event.cs +++ /dev/null @@ -1,47 +0,0 @@ -namespace Gdk { - using System; - using System.Runtime.InteropServices; - - public class Event - { - public Event(IntPtr e) - { - _event = e; - } - protected IntPtr _event; - public EventType Type - { - get - { - IntPtr ptr = Marshal.ReadIntPtr (_event); - return (EventType)((int)ptr); - } - set - { - Marshal.WriteIntPtr(_event, new IntPtr((int)value)); - } - } - -/* FIXME: Fix or kill later. - public EventAny Any - { - get - { - return (EventAll)this; - } - } - public static explicit operator EventAll (Event e) - { - return Marshal.PtrToStructure(e._event, EventAll); - } -*/ - } - - [StructLayout(LayoutKind.Sequential)] - public struct EventAny - { - public IntPtr type; - public IntPtr window; - public SByte send_event; - } -} diff --git a/gdk/SimpleEvent.cs b/gdk/SimpleEvent.cs index 349a95626..8a413a573 100644 --- a/gdk/SimpleEvent.cs +++ b/gdk/SimpleEvent.cs @@ -71,8 +71,9 @@ namespace Gdk { throw new Exception ("Unexpected event key"); SimpleEvent se = (SimpleEvent) _Instances [inst_key]; - EventArgs args = new SimpleEventArgs ( - new Gdk.Event (e)); + Event evnt = new Event (); + Marshal.PtrToStructure (e, evnt); + EventArgs args = new SimpleEventArgs (evnt); se._handler (se._obj, args); return true; //FIXME: How do we manage the return value? } diff --git a/glib/SignalCallback.cs b/glib/SignalCallback.cs new file mode 100644 index 000000000..26beabbc9 --- /dev/null +++ b/glib/SignalCallback.cs @@ -0,0 +1,58 @@ +// GtkSharp.SignalCallback.cs - Signal callback base class implementation +// +// Authors: Mike Kestner +// +// (c) 2001 Mike Kestner + +namespace GtkSharp { + using System; + using System.Collections; + using System.Runtime.InteropServices; + using GLib; + + /// + /// SignalCallback Class + /// + /// + /// + /// Base Class for GSignal to C# event marshalling. + /// + + public abstract class SignalCallback { + + // A counter used to produce unique keys for instances. + protected static int _NextKey = 0; + + // Hashtable containing refs to all current instances. + protected static Hashtable _Instances = new Hashtable (); + + // protected instance members + protected GLib.Object _obj; + protected EventHandler _handler; + protected int _key; + + /// + /// SignalCallback Constructor + /// + /// + /// + /// Initializes instance data. + /// + + public SignalCallback (GLib.Object obj, IntPtr raw, + String name, EventHandler eh) + { + _key = _NextKey++; + _obj = obj; + _handler = eh; + _Instances [_key] = this; + } + + // Destructor needed to release references from the instance + // table and unpin the delegate if no refs remain. + ~SignalCallback () + { + _Instances.Remove (_key); + } + } +}