From 33533985d0364c246e83296328071653349d3cdc Mon Sep 17 00:00:00 2001 From: Bob Smith Date: Fri, 21 Sep 2001 14:03:17 +0000 Subject: [PATCH] Totally reworked the signal system. Should be much more flexable. svn path=/trunk/gtk-sharp/; revision=919 --- ChangeLog | 4 ++ gdk/Event.cs | 64 +++++++++++++++++++++ gdk/Signals/SimpleEvent.cs | 61 ++++++++++++++++++++ gdk/makefile | 11 ++++ glib/Signals/Simple.cs | 39 +++++++++++++ gtk/Button.cs | 20 +------ gtk/Object.cs | 55 ------------------ gtk/SimpleSignal.cs | 11 ---- gtk/Widget.cs | 115 +++++++++++++++++++++++++------------ sample/HelloWorld.cs | 4 +- 10 files changed, 261 insertions(+), 123 deletions(-) create mode 100644 gdk/Event.cs create mode 100644 gdk/Signals/SimpleEvent.cs create mode 100755 gdk/makefile create mode 100644 glib/Signals/Simple.cs delete mode 100644 gtk/SimpleSignal.cs diff --git a/ChangeLog b/ChangeLog index e198051be..cd107a4b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2001-09-21 + + * Signal system totally reworked. It should be stable now. + 2001-09-20 * glib/ObjectManager.cs: Nuked. diff --git a/gdk/Event.cs b/gdk/Event.cs new file mode 100644 index 000000000..98efb443e --- /dev/null +++ b/gdk/Event.cs @@ -0,0 +1,64 @@ +namespace Gdk { + using System; + using System.Runtime.InteropServices; + + public enum EventType + { + Nothing = -1, + Delete = 0, + Destroy = 1, + Expose = 2, + MotionNotify = 3, + ButtonPress = 4, + 2ButtonPress = 5, + 3ButtonPress = 6, + ButtonRelease = 7, + KeyPress = 8, + KeyRelease = 9, + EnterNotify = 10, + LeaveNotify = 11, + FocusChange = 12, + Configure = 13, + Map = 14, + Unmap = 15, + PropertyNotify = 16, + SelectionClear = 17, + SelectionRequest = 18, + SelectionNotify = 19, + ProximityIn = 20, + ProximityOut = 21, + DragEnter = 22, + DragLeave = 23, + DragMotion = 24, + DragStatus = 25, + DropStart = 26, + DropFinished = 27, + ClientEvent = 28, + VisibilityNotify = 29, + NoExpose = 30, + Scroll = 31, + WindowState = 32, + Setting = 33 + } + + 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)); + } + } + } +} diff --git a/gdk/Signals/SimpleEvent.cs b/gdk/Signals/SimpleEvent.cs new file mode 100644 index 000000000..a500b8792 --- /dev/null +++ b/gdk/Signals/SimpleEvent.cs @@ -0,0 +1,61 @@ +// Gdk.Signals.SimpleEvent.cs - Gdk Simple Event Signal implementation +// +// Author: Bob Smith +// +// (c) 2001 Bob Smith + +namespace Gdk.Signals { + using System; + using System.Runtime.InteropServices; + using Glib; + using Gdk; + + public class SimpleEventArgs : EventArgs { + public SimpleEventArgs(Gdk.Event event) + { + _event = event; + } + private Gdk.Event _event; + public Gdk.Event Event + { + get + { + return _event; + } + } + public static explicit operator Gdk.Event(SimpleEventArgs value) + { + return value.Event; + } + } + + public delegate bool SimpleEventDelegate(IntPtr obj, IntPtr data); + public class SimpleEvent { + public SimpleEvent(){} + private static bool SimpleEventCallback(IntPtr obj, IntPtr e, IntPtr data) + { + Glib.Object o = Glib.Object.GetObject(obj); + EventHandler eh = o.Events[(int)data]; + if (eh != null) + { + EventArgs args = new SimpleEventArgs (new Gdk.Event(e)); + eh(o, args); + } + return true; //FIXME: How do we manage the return value? + } + private static SimpleEventDelegate _simpleDelegate; + private static GCHandle _simpleEventGCHandle; + public static SimpleEventDelegate Delegate + { + get + { + if (SimpleEvent._simpleEventDelegate == null) + { + SimpleEvent._simpleDelegate = new SimpleEventDelegate(SimpleCallback); + SimpleEvent._simpleGCHandle = GCHandle.Alloc (SimpleEvent._simpleEventDelegate, GCHandleType.Pinned); + } + return SimpleEvent._simpleEventDelegate; + } + } + } +} diff --git a/gdk/makefile b/gdk/makefile new file mode 100755 index 000000000..e2eb4ebb7 --- /dev/null +++ b/gdk/makefile @@ -0,0 +1,11 @@ +CSC=/cygdrive/c/windows/microsoft.net/framework/v1.0.2914/csc.exe + +all: + @echo "You must use 'make windows' or 'make unix'." + @echo "'make unix' is broken for now." + +windows: + $(CSC) /unsafe /target:library /out:gdk-sharp.dll /recurse:*.cs + +unix: + @echo "'make unix' is broken for now." diff --git a/glib/Signals/Simple.cs b/glib/Signals/Simple.cs new file mode 100644 index 000000000..b3f9cc0bb --- /dev/null +++ b/glib/Signals/Simple.cs @@ -0,0 +1,39 @@ +// Glib.Signals.Simple.cs - Glib Simple Signal implementation +// +// Author: Bob Smith +// +// (c) 2001 Bob Smith + +namespace Glib.Signals { + using System; + using System.Runtime.InteropServices; + using Glib; + + public delegate void SimpleDelegate(IntPtr obj, IntPtr data); + public class Simple { + public Simple(){} + private static void SimpleCallback(IntPtr obj, IntPtr data) + { + Glib.Object o = Glib.Object.GetObject(obj); + EventHandler eh = o.Events[(int)data]; + if (eh != null) + { + eh(o, EventArgs.Empty); + } + } + private static SimpleDelegate _simpleDelegate; + private static GCHandle _simpleGCHandle; + public static SimpleDelegate Delegate + { + get + { + if (Simple._simpleDelegate == null) + { + Simple._simpleDelegate = new SimpleDelegate(SimpleCallback); + Simple._simpleGCHandle = GCHandle.Alloc (Simple._simpleDelegate, GCHandleType.Pinned); + } + return Simple._simpleDelegate; + } + } + } +} diff --git a/gtk/Button.cs b/gtk/Button.cs index 096acfb9e..fae68e659 100644 --- a/gtk/Button.cs +++ b/gtk/Button.cs @@ -11,30 +11,16 @@ namespace Gtk { public class Button : Widget { - private static readonly object ClickedEvent = new object (); + private static readonly string ClickedEvent = "clicked"; public event EventHandler Clicked { add { - if (Events[ClickedEvent] == null) - { - ConnectSignal ("clicked", new SimpleCallback (EmitClickedEvent)); - } - Events.AddHandler (ClickedEvent, value); + AddSimpleEvent(ClickedEvent, value); } remove { - Events.RemoveHandler (ClickedEvent, value); - } - } - - private void EmitClickedEvent (IntPtr obj) - { - EventHandler eh = (EventHandler)(Events[ClickedEvent]); - if (eh != null) - { - EventArgs args = new EventArgs (); - eh(this, args); + RemoveSimpleEvent (ClickedEvent, value); } } diff --git a/gtk/Object.cs b/gtk/Object.cs index cd39b9001..83fb6bb63 100755 --- a/gtk/Object.cs +++ b/gtk/Object.cs @@ -12,60 +12,5 @@ namespace Gtk { public abstract class Object : GLib.Object { - /// - /// Destroy Event - /// - /// - /// - /// Occurs when the Object is destroyed. - /// - - private static readonly object DestroyEvent = new object (); - - public event EventHandler Destroy - { - add - { - if (Events[DestroyEvent] == null) - { - ConnectSignal ("destroy", new SimpleSignal (EmitDestroyEvent)); - } - Events.AddHandler (DeleteEvent, value); - } - remove - { - Events.RemoveHandler (DeleteEvent, value); - } - } - - private static void EmitDestroyEvent (IntPtr obj, IntPtr data) - { - Glib.Object o = Glib.Object.GetObject(obj); - EventHandler eh = (EventHandler)(o.Events[DeleteEvent]); - if (eh != null) - { - EventArgs args = new EventArgs (); - eh(this, args); - } - } - - protected delegate void SimpleCallback (IntPtr obj); - - [DllImport("gtk-1.3")] - static extern void gtk_signal_connect_full ( - IntPtr obj, string evname, - SimpleCallback cb, IntPtr unsupported, - IntPtr data, IntPtr destroycb, - int objsig, int after ); - - - protected void ConnectSignal (string name, SimpleCallback cb) - { - gtk_signal_connect_full (RawObject, name, cb, - new IntPtr (0), new IntPtr (0), - new IntPtr (0), 0, 0); - } - - } } diff --git a/gtk/SimpleSignal.cs b/gtk/SimpleSignal.cs deleted file mode 100644 index 3725d0074..000000000 --- a/gtk/SimpleSignal.cs +++ /dev/null @@ -1,11 +0,0 @@ -// SimpleSignal.cs - Simple callback delegate. -// -// Author: Bob Smith -// -// (c) 2001 Bob Smith - -namespace Gtk { - using System; - - public delegate void SimpleSignal (IntPtr obj, IntPtr data); -} diff --git a/gtk/Widget.cs b/gtk/Widget.cs index b357dddd5..42fdc85a4 100755 --- a/gtk/Widget.cs +++ b/gtk/Widget.cs @@ -6,63 +6,102 @@ namespace Gtk { - using System; - using System.Runtime.InteropServices; + using System; + using System.Runtime.InteropServices; + using Glib; + using Gdk; - public abstract class Widget : Object { + public abstract class Widget : Object { - /// - /// Delete Event - /// - /// - /// - /// Occurs when the Widget is deleted by the window - /// manager. - /// - - private static readonly object DeleteEvent = new object (); - - public event EventHandler Delete + private static readonly string DeleteEvent = "delete-event"; + public event EventHandler DeleteEvent { add { - if (Events[DeleteEvent] == null) - { - ConnectSignal ("delete-event", new SimpleCallback (EmitDeleteEvent)); - } - Events.AddHandler (DeleteEvent, value); + AddGdkSimpleEvent(DeleteEvent, value); } remove { - Events.RemoveHandler (DeleteEvent, value); + RemoveGdkSimpleEvent (DeleteEvent, value); } } - private void EmitDeleteEvent (IntPtr obj) + public void AddSimpleEvent(Object type, string name, EventHandler value) { - EventHandler eh = (EventHandler)(Events[DeleteEvent]); - if (eh != null) + if (Events[type] == null) { - EventArgs args = new EventArgs (); - eh(this, args); + ConnectSimpleSignal(name, type); } + Events.AddHandler(type, value); } - /// - /// Show Method - /// - /// - /// - /// Makes the Widget visible on the display. - /// + public void AddSimpleEvent(String type, EventHandle value) + : this (type, type, value) {} + + public void RemoveSimpleEvent(Object type, string name, EventHander value) + { + Events.RemoveHandler(type, value); + } + + public void RemoveSimpleEvent(String type, EventHandle value) + : this (type, type, value) {} + + public void AddGdkSimpleEvent(Object type, string name, EventHandler value) + { + if (Events[type] == null) + { + ConnectGdkSimpleEventSignal(name, type); + } + Events.AddHandler(type, value); + } + + public void AddGdkSimpleEvent(String type, EventHandle value) + : this (type, type, value) {} + + public void RemoveGdkSimpleEvent(Object type, string name, EventHander value) + { + Events.RemoveHandler(type, value); + } + + public void RemoveGdkSimpleEvent(String type, EventHandle value) + : this (type, type, value) {} + [DllImport("gtk-1.3")] - static extern void gtk_widget_show (IntPtr obj); + static extern void gtk_signal_connect_full ( + IntPtr obj, string evname, + SimpleDelegate cb, IntPtr unsupported, + IntPtr data, IntPtr destroycb, + int objsig, int after ); - public void Show () + public void ConnectSimpleSignal(string name, Object signal) { - gtk_widget_show (RawObject); - + gtk_signal_connect_full(RawObject, name, Glib.Signals.Simple.Delegate, + new IntPtr (0), new IntPtr (signal.GetHashCode()), + new IntPtr (0), 0, 0); } - } + + public void ConnectGdkSimpleSignal(string name, Object signal) + { + gtk_signal_connect_full(RawObject, name, Gdk.Signals.SimpleEvent.Delegate, + new IntPtr (0), new IntPtr (signal.GetHashCode()), + new IntPtr (0), 0, 0); + } + + /// + /// Show Method + /// + /// + /// + /// Makes the Widget visible on the display. + /// + + [DllImport("gtk-1.3")] + static extern void gtk_widget_show (IntPtr obj); + + public void Show () + { + gtk_widget_show (RawObject); + } + } } diff --git a/sample/HelloWorld.cs b/sample/HelloWorld.cs index 56d585f48..47890c8a1 100755 --- a/sample/HelloWorld.cs +++ b/sample/HelloWorld.cs @@ -15,13 +15,13 @@ namespace GtkSamples { { Application.Init (ref args); Window win = new Window ("Gtk# Hello World"); - win.Delete += new EventHandler (delete_cb); + win.DeleteEvent += new EventHandler (Window_Delete); win.Show (); Application.Run (); return 0; } - static void delete_cb (object obj, EventArgs args) + static void Window_Delete (object obj, EventArgs args) { Application.Quit (); }