diff --git a/ChangeLog b/ChangeLog index af43097bc..a52641eab 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2003-12-30 Mike Kestner + + * glib/Object.cs (ConnectDefaultHandlers): reflection code to + hook up overridden default signal handlers. + * glue/type.c (gtksharp_override_virtual_method): peek the gtype + and ref the class if it isn't created yet. + * sample/Subclass.cs : update to override Button.OnClicked. + 2003-12-26 Mike Kestner * glue/selectiondata.c : new glue to make SelectionData opaque diff --git a/glib/Object.cs b/glib/Object.cs index 1ad0d236b..2d13446f9 100644 --- a/glib/Object.cs +++ b/glib/Object.cs @@ -141,6 +141,28 @@ namespace GLib { return GetObject (o, false); } + private static void ConnectDefaultHandlers (GType gtype, System.Type t) + { + foreach (MethodInfo minfo in t.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) { + MethodInfo baseinfo = minfo.GetBaseDefinition (); + if (baseinfo == minfo) + continue; + + foreach (object attr in baseinfo.GetCustomAttributes (true)) { + if (attr.ToString () != "GLib.DefaultSignalHandlerAttribute") + continue; + + DefaultSignalHandlerAttribute sigattr = attr as DefaultSignalHandlerAttribute; + MethodInfo connector = sigattr.Type.GetMethod (sigattr.ConnectionMethod, BindingFlags.Static | BindingFlags.NonPublic); + object[] parms = new object [1]; + parms [0] = gtype; + connector.Invoke (null, parms); + break; + } + } + + } + [DllImport("gtksharpglue")] static extern IntPtr gtksharp_register_type (string name, IntPtr parent_type); @@ -165,7 +187,9 @@ namespace GLib { GType parent_gtype = (GType) pi.GetValue (null, null); string name = t.Namespace.Replace(".", "_") + t.Name; GtkSharp.ObjectManager.RegisterType (name, t.Namespace + t.Name, t.Assembly.GetName().Name); - return new GLib.GType (gtksharp_register_type (name, parent_gtype.Val)); + GType gtype = new GType (gtksharp_register_type (name, parent_gtype.Val)); + ConnectDefaultHandlers (gtype, t); + return gtype; } /// diff --git a/glue/type.c b/glue/type.c index 7e5cce543..c607d942a 100644 --- a/glue/type.c +++ b/glue/type.c @@ -71,6 +71,8 @@ gtksharp_register_type (gchar *name, GType parent) void gtksharp_override_virtual_method (GType g_type, const gchar *name, GCallback callback) { + if (g_type_class_peek (g_type) == NULL) + g_type_class_ref (g_type); guint id = g_signal_lookup (name, g_type); GClosure *closure = g_cclosure_new (callback, NULL, NULL); g_signal_override_class_closure (id, g_type, closure); diff --git a/sample/Subclass.cs b/sample/Subclass.cs index 330a7af4b..6e1a2c899 100755 --- a/sample/Subclass.cs +++ b/sample/Subclass.cs @@ -1,8 +1,8 @@ -// Subclass.cs - Widget subclass Test implementation +// Subclass.cs - Widget subclass Test // -// Author: Mike Kestner +// Author: Mike Kestner // -// (c) 2001-2002 Mike Kestner +// (c) 2001-2003 Mike Kestner, Novell, Inc. namespace GtkSamples { @@ -17,35 +17,22 @@ namespace GtkSamples { { Application.Init (); Window win = new Window ("Button Tester"); - win.DefaultSize = new Size (200, 150); - win.DeleteEvent += new DeleteEventHandler (Window_Delete); Button btn = new MyButton (); - btn.Label = "I'm a subclassed button"; - btn.Clicked += new EventHandler (btn_click); win.Add (btn); win.ShowAll (); Application.Run (); return 0; } - - static void btn_click (object obj, EventArgs args) - { - Console.WriteLine ("Button Clicked"); - } - - static void Window_Delete (object obj, DeleteEventArgs args) - { - Application.Quit (); - args.RetVal = true; - } - } public class MyButton : Gtk.Button { static GLib.GType gtype = GLib.GType.Invalid; - public MyButton () : base (GType) {} + public MyButton () : base (GType) + { + Label = "I'm a subclassed button"; + } public static new GLib.GType GType { get { @@ -54,5 +41,10 @@ namespace GtkSamples { return gtype; } } + + protected override void OnClicked () + { + Console.WriteLine ("Button::Clicked default handler fired."); + } } }