2003-01-01 Rachel Hestilow <hestilow@ximian.com>

* 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
This commit is contained in:
Rachel Hestilow 2003-01-01 21:37:45 +00:00
parent 2e97cf0091
commit 3841924432
4 changed files with 84 additions and 3 deletions

View file

@ -1,3 +1,11 @@
2003-01-01 Rachel Hestilow <hestilow@ximian.com>
* glade/XML.custom: New method BindFields.
(Autoconnect): Call BindFields.
* glade/GladeWidgetAttribute.cs: Added.
* sample/GladeTest.cs: Test GladeWidgetAttribute.
2002-12-25 Rodrigo Moya <rodrigo@ximian.com>
* sources/gtk-sharp.sources: added libgphoto2.

View file

@ -0,0 +1,26 @@
// GladeWidgetAttribute.cs
//
// Author: Rachel Hestilow <hestilow@ximian.com>
//
// (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; }
}
}
}

View file

@ -5,6 +5,9 @@
//
// (c) 2002 Ricardo Fernández Pascual
//
// Field binding code by Rachel Hestilow <hestilow@ximian.com>
// (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);
/// <summary>Creates a Glade.XML object from a Stream</sumary>
/// <summary>Creates a Glade.XML object from a Stream</summary>
/// <remarks>Reads the contents of the stream and parses it. It must be in
/// correct Glade format</remarks>
public XML (System.IO.Stream s, string root, string domain)
@ -73,7 +76,7 @@
Raw = glade_xml_new_from_buffer(buffer, size, root, domain);
}
/// <summary>Creates a Glade.XML object from a resource</sumary>
/// <summary>Creates a Glade.XML object from a resource</summary>
/// <remarks>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.</remarks>
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.</remarks>
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);
}

View file

@ -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)
{