diff --git a/ChangeLog b/ChangeLog index 7a90b6902..4c218ee7b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2005-09-02 Ben Maurer + + * sample/NodeViewDemo.cs: take advantage of the stuff below + + * gtk/TreeNodeValueAttribute.cs: Allow on props + + * gtk/TreeNodeAttribute.cs: Obsolete column count + + * gtk/NodeStore.cs: Change this to not need the TreeNodeAttribute + column count. Handle fields as well as properties. + 2005-08-30 Mike Kestner * gdk/Event.cs : add some null guarding to GetEvent. diff --git a/gtk/NodeStore.cs b/gtk/NodeStore.cs index de2143cf7..d95f73044 100644 --- a/gtk/NodeStore.cs +++ b/gtk/NodeStore.cs @@ -92,8 +92,8 @@ namespace Gtk { Hashtable node_hash = new IDHashtable (); GLib.GType[] ctypes; - PropertyInfo[] getters; - int n_cols = 1; + MemberInfo [] getters; + int n_cols; bool list_only = false; ArrayList nodes = new ArrayList (); TreeModelIfaceDelegates tree_model_iface; @@ -150,7 +150,11 @@ namespace Gtk { if (node == null) return; g_value_init (ref val, ctypes [col].Val); - object col_val = getters[col].GetValue (node, null); + object col_val; + if (getters [col] is PropertyInfo) + col_val = ((PropertyInfo) getters [col]).GetValue (node, null); + else + col_val = ((FieldInfo) getters [col]).GetValue (node); val.Val = col_val; } @@ -293,19 +297,34 @@ namespace Gtk { void ScanType (Type type) { - foreach (TreeNodeAttribute attr in type.GetCustomAttributes (typeof (TreeNodeAttribute), false)) { - n_cols = attr.ColumnCount; - list_only = attr.ListOnly; - } + TreeNodeAttribute tna = (TreeNodeAttribute) Attribute.GetCustomAttribute (type, typeof (TreeNodeAttribute), false); + if (tna != null) + list_only = tna.ListOnly; + + ArrayList minfos = new ArrayList (); + + foreach (PropertyInfo pi in type.GetProperties ()) + foreach (TreeNodeValueAttribute attr in pi.GetCustomAttributes (typeof (TreeNodeValueAttribute), false)) + minfos.Add (pi); + + foreach (FieldInfo fi in type.GetFields ()) + foreach (TreeNodeValueAttribute attr in fi.GetCustomAttributes (typeof (TreeNodeValueAttribute), false)) + minfos.Add (fi); + + ctypes = new GLib.GType [minfos.Count]; + getters = new MemberInfo [minfos.Count]; - ctypes = new GLib.GType [n_cols]; - getters = new PropertyInfo [n_cols]; - - foreach (PropertyInfo pi in type.GetProperties ()) { - foreach (TreeNodeValueAttribute attr in pi.GetCustomAttributes (typeof (TreeNodeValueAttribute), false)) { + foreach (MemberInfo mi in minfos) { + foreach (TreeNodeValueAttribute attr in mi.GetCustomAttributes (typeof (TreeNodeValueAttribute), false)) { int col = attr.Column; - getters [col] = pi; - ctypes[col] = (GLib.GType) pi.PropertyType; + + if (getters [col] != null) + throw new Exception ("You have two TreeNodeValueAttributes with the same column"); + + getters [col] = mi; + Type t = mi is PropertyInfo ? ((PropertyInfo) mi).PropertyType + : ((FieldInfo) mi).FieldType; + ctypes [col] = (GLib.GType) t; } } } diff --git a/gtk/TreeNodeAttribute.cs b/gtk/TreeNodeAttribute.cs index 656ac51cc..c0643aaa7 100644 --- a/gtk/TreeNodeAttribute.cs +++ b/gtk/TreeNodeAttribute.cs @@ -25,16 +25,12 @@ namespace Gtk { [AttributeUsage(AttributeTargets.Class)] public sealed class TreeNodeAttribute : Attribute { - int col_count; bool list_only; - + + [Obsolete ("This is no longer needed; it gets detected by Gtk#")] public int ColumnCount { - get { - return col_count; - } - set { - col_count = value; - } + get { return 0; } + set { } } public bool ListOnly { diff --git a/gtk/TreeNodeValueAttribute.cs b/gtk/TreeNodeValueAttribute.cs index 464b921df..d3bbfc483 100644 --- a/gtk/TreeNodeValueAttribute.cs +++ b/gtk/TreeNodeValueAttribute.cs @@ -23,7 +23,7 @@ namespace Gtk { using System; - [AttributeUsage (AttributeTargets.Property, AllowMultiple = true)] + [AttributeUsage (AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)] public sealed class TreeNodeValueAttribute : Attribute { int col; diff --git a/sample/NodeViewDemo.cs b/sample/NodeViewDemo.cs index c3aa8c40f..56e3a1932 100644 --- a/sample/NodeViewDemo.cs +++ b/sample/NodeViewDemo.cs @@ -10,25 +10,21 @@ namespace GtkSamples { using System.Reflection; using Gtk; - [TreeNode (ColumnCount=2)] public class DemoTreeNode : TreeNode { - - string name; string desc; static int count = 0; public DemoTreeNode (string name, string desc) { - this.name = name; + this.Name = name; this.desc = desc; count++; } - - [TreeNodeValue (Column=0)] - public string Name { - get { return name; } - } - + + // TreeNodeValues can come from both properties and fields + [TreeNodeValue (Column=0)] + public string Name; + [TreeNodeValue (Column=1)] public string Description { get { return desc; }