From 384192443233d2f55672efe0b4276f243c66b001 Mon Sep 17 00:00:00 2001 From: Rachel Hestilow Date: Wed, 1 Jan 2003 21:37:45 +0000 Subject: [PATCH] 2003-01-01 Rachel Hestilow * glade/XML.custom: New method BindFields. (Autoconnect): Call BindFields. * glade/GladeWidgetAttribute.cs: Added. * sample/GladeTest.cs: Test GladeWidgetAttribute. svn path=/trunk/gtk-sharp/; revision=10048 --- ChangeLog | 8 +++++++ glade/GladeWidgetAttribute.cs | 26 +++++++++++++++++++++ glade/XML.custom | 44 +++++++++++++++++++++++++++++++++-- sample/GladeTest.cs | 9 ++++++- 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 glade/GladeWidgetAttribute.cs diff --git a/ChangeLog b/ChangeLog index ff59c4fb9..30c61e912 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2003-01-01 Rachel Hestilow + + * glade/XML.custom: New method BindFields. + (Autoconnect): Call BindFields. + * glade/GladeWidgetAttribute.cs: Added. + + * sample/GladeTest.cs: Test GladeWidgetAttribute. + 2002-12-25 Rodrigo Moya * sources/gtk-sharp.sources: added libgphoto2. diff --git a/glade/GladeWidgetAttribute.cs b/glade/GladeWidgetAttribute.cs new file mode 100644 index 000000000..c637fec22 --- /dev/null +++ b/glade/GladeWidgetAttribute.cs @@ -0,0 +1,26 @@ +// GladeWidgetAttribute.cs +// +// Author: Rachel Hestilow +// +// (c) 2003 Rachel Hestilow + +namespace Glade { + using System; + + [AttributeUsage (AttributeTargets.Field)] + public class GladeWidgetAttribute : Attribute + { + private string name; + + public GladeWidgetAttribute (string name) + { + this.name = name; + } + + public string Name + { + get { return name; } + } + } +} + diff --git a/glade/XML.custom b/glade/XML.custom index ac908fd10..205bff8d8 100644 --- a/glade/XML.custom +++ b/glade/XML.custom @@ -5,6 +5,9 @@ // // (c) 2002 Ricardo Fernández Pascual // +// Field binding code by Rachel Hestilow +// (c) 2003 Rachel Hestilow +// // This code is inserted after the automatically generated code. // keep this around so it doesn't get GC'd @@ -62,7 +65,7 @@ [DllImport("glade-2.0")] static extern IntPtr glade_xml_new_from_buffer(byte[] buffer, int size, string root, string domain); - /// Creates a Glade.XML object from a Stream + /// Creates a Glade.XML object from a Stream /// Reads the contents of the stream and parses it. It must be in /// correct Glade format public XML (System.IO.Stream s, string root, string domain) @@ -73,7 +76,7 @@ Raw = glade_xml_new_from_buffer(buffer, size, root, domain); } - /// Creates a Glade.XML object from a resource + /// Creates a Glade.XML object from a resource /// Reads the contents of the resource in the /// given assembly and parses it. If the assembly is null, /// the current assembly will be used. It must be in @@ -99,6 +102,7 @@ /// provided by the given object. public void Autoconnect (object handler) { + BindFields (handler); SignalConnector sc = new SignalConnector (this, handler); sc.Autoconnect (); } @@ -108,6 +112,7 @@ /// methods provided by the given type. public void Autoconnect (Type handler_class) { + BindFields (handler_class); SignalConnector sc = new SignalConnector (this, handler_class); sc.Autoconnect (); } @@ -242,3 +247,38 @@ } } } + + private void BindFields (object target, Type type) + { + System.Reflection.BindingFlags flags = System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic; + if (target != null) + flags |= System.Reflection.BindingFlags.Instance; + else + flags |= System.Reflection.BindingFlags.Static; + + System.Reflection.FieldInfo[] fields = type.GetFields (flags); + if (fields == null) + return; + + foreach (System.Reflection.FieldInfo field in fields) + { + object[] attrs = field.GetCustomAttributes (typeof (GladeWidgetAttribute), true); + if (attrs == null || attrs.Length == 0) + continue; + // The widget to field binding must be 1:1, so only check + // the first attribute. + GladeWidgetAttribute widget = (GladeWidgetAttribute) attrs[0]; + field.SetValue (target, GetWidget (widget.Name), flags, null, null); + } + } + + public void BindFields (object target) + { + BindFields (target, target.GetType ()); + } + + public void BindFields (Type type) + { + BindFields (null, type); + } + diff --git a/sample/GladeTest.cs b/sample/GladeTest.cs index 2c83a1b8a..5aaf5f94e 100644 --- a/sample/GladeTest.cs +++ b/sample/GladeTest.cs @@ -16,6 +16,9 @@ namespace GladeSamples { public class GladeTest : Program { + [GladeWidget("main_window")] + Gtk.Window main_window; + public static void Main (string[] args) { new GladeTest (args).Run (); @@ -28,7 +31,11 @@ namespace GladeSamples { you don't want */ Glade.XML gxml = new Glade.XML (null, "test.glade", "main_window", null); gxml.Autoconnect (this); - } + if (main_window != null) + Console.WriteLine ("Main Window Title: \"{0}\"", main_window.Title); + else + Console.WriteLine ("GladeWidgetAttribute is broken."); + } public void OnWindowDeleteEvent (object o, DeleteEventArgs args) {