diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/GraphicObjects/Expandable.cs | 1 | ||||
| -rw-r--r-- | src/GraphicObjects/Group.cs | 2 | ||||
| -rw-r--r-- | src/GraphicObjects/TemplatedGroup.cs | 144 | ||||
| -rw-r--r-- | src/IMLReader.cs | 24 |
4 files changed, 162 insertions, 9 deletions
diff --git a/src/GraphicObjects/Expandable.cs b/src/GraphicObjects/Expandable.cs index 059cab8b..70451f41 100644 --- a/src/GraphicObjects/Expandable.cs +++ b/src/GraphicObjects/Expandable.cs @@ -63,6 +63,7 @@ namespace Crow NotifyValueChanged ("HasContent", HasContent); } } + //TODO: move loadTemplate and ResolveBinding in TemplatedContainer protected override void loadTemplate(GraphicObject template = null) { base.loadTemplate (template); diff --git a/src/GraphicObjects/Group.cs b/src/GraphicObjects/Group.cs index 950992e2..402128b0 100644 --- a/src/GraphicObjects/Group.cs +++ b/src/GraphicObjects/Group.cs @@ -27,8 +27,6 @@ namespace Crow bool _multiSelect = false; List<GraphicObject> children = new List<GraphicObject>(); - public GraphicObject activeWidget; - public virtual List<GraphicObject> Children { get { return children; } } diff --git a/src/GraphicObjects/TemplatedGroup.cs b/src/GraphicObjects/TemplatedGroup.cs new file mode 100644 index 00000000..12448b82 --- /dev/null +++ b/src/GraphicObjects/TemplatedGroup.cs @@ -0,0 +1,144 @@ +// +// TemplatedContainer.cs +// +// Author: +// Jean-Philippe Bruyère <jp.bruyere@hotmail.com> +// +// Copyright (c) 2015 jp +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +using System; +using System.Xml.Serialization; +using System.Xml; +using System.Reflection; +using System.Collections.Generic; + +namespace Crow +{ + public abstract class TemplatedGroup : TemplatedControl + { + #region CTOR + public TemplatedGroup () : base(){} + #endregion + + protected Group items; + + public virtual List<GraphicObject> Items{ get { return items.Children; }} + + public virtual void AddItem(GraphicObject g){ + items.AddChild (g); + g.LogicalParent = this; + } + public virtual void RemoveItem(GraphicObject g) + { + items.RemoveChild (g); + } + + public virtual void ClearItems() + { + items.ClearChildren (); + } + + protected override void loadTemplate(GraphicObject template = null) + { + base.loadTemplate (template); + + items = this.child.FindByName ("ItemsContainer") as Group; + if (items == null) + throw new Exception ("TemplatedGroup template Must contain a Group named 'ItemsContainer'"); + } + + #region GraphicObject overrides + public override GraphicObject FindByName (string nameToFind) + { + if (Name == nameToFind) + return this; + + foreach (GraphicObject w in Items) { + GraphicObject r = w.FindByName (nameToFind); + if (r != null) + return r; + } + return null; + } + public override bool Contains (GraphicObject goToFind) + { + foreach (GraphicObject w in Items) { + if (w == goToFind) + return true; + if (w.Contains (goToFind)) + return true; + } + return false; + } + #endregion + + #region IXmlSerialisation Overrides + public override void ReadXml(System.Xml.XmlReader reader) + { + using (System.Xml.XmlReader subTree = reader.ReadSubtree ()) { + subTree.Read (); + string tmp = subTree.ReadOuterXml (); + + //seek for template tag + using (XmlReader xr = new XmlTextReader (tmp, XmlNodeType.Element, null)) { + xr.Read (); + base.ReadXml (xr); + } + //process content + using (XmlReader xr = new XmlTextReader (tmp, XmlNodeType.Element, null)) { + xr.Read (); //skip current node + + while (!xr.EOF) { + xr.Read (); //read first child + + if (!xr.IsStartElement ()) + continue; + + if (xr.Name == "Template" || Name == "ItemTemplate"){ + xr.Skip (); + if (!xr.IsStartElement ()) + continue; + } + + Type t = Type.GetType ("Crow." + xr.Name); + if (t == null) { + Assembly a = Assembly.GetEntryAssembly (); + foreach (Type expT in a.GetExportedTypes ()) { + if (expT.Name == xr.Name) + t = expT; + } + } + if (t == null) + throw new Exception (xr.Name + " type not found"); + + GraphicObject go = (GraphicObject)Activator.CreateInstance (t); + + (go as IXmlSerializable).ReadXml (xr); + + AddItem (go); + + xr.Read (); //closing tag + } + + } + } + } + public override void WriteXml(System.Xml.XmlWriter writer) + { + throw new NotImplementedException (); + } + #endregion + } +} diff --git a/src/IMLReader.cs b/src/IMLReader.cs index e4f98ecb..922a48d7 100644 --- a/src/IMLReader.cs +++ b/src/IMLReader.cs @@ -107,6 +107,7 @@ namespace Crow il.Emit (OpCodes.Ldloc_0);//save current go onto the stack if child has to be added + #region Template and TemplateItems loading if (typeof(TemplatedControl).IsAssignableFrom (crowType)) { //if its a template, first read template elements using (IMLReader reader = new IMLReader (il, tmpXml)) { @@ -188,10 +189,12 @@ namespace Crow } } } + #endregion using (IMLReader reader = new IMLReader(il,tmpXml)){ reader.Read (); + #region Styling and default values loading if (reader.HasAttributes) { string style = reader.GetAttribute ("Style"); if (!string.IsNullOrEmpty (style)) { @@ -201,7 +204,9 @@ namespace Crow } reader.il.Emit (OpCodes.Ldloc_0); reader.il.Emit (OpCodes.Callvirt, typeof(GraphicObject).GetMethod ("loadDefaultValues")); + #endregion + #region Attributes reading if (reader.HasAttributes) { MethodInfo miAddBinding = typeof(GraphicObject).GetMethod ("BindMember"); @@ -229,6 +234,7 @@ namespace Crow } reader.MoveToElement (); } + #endregion if (reader.IsEmptyElement) { reader.il.Emit (OpCodes.Pop);//pop saved ref to current object @@ -253,7 +259,7 @@ namespace Crow case XmlNodeType.Element: //Templates if (reader.Name == "Template" || - reader.Name == "ItemTemplate") { + reader.Name == "ItemTemplate") { reader.Skip (); continue; } @@ -263,16 +269,18 @@ namespace Crow miAddChild = typeof(Group).GetMethod ("AddChild"); else if (typeof(Container).IsAssignableFrom (crowType)) miAddChild = typeof(Container).GetMethod ("SetChild"); - else if (typeof(TemplatedContainer).IsAssignableFrom (crowType)&&!templateLoading) - miAddChild = typeof(TemplatedContainer).GetProperty("Content").GetSetMethod(); + else if (typeof(TemplatedContainer).IsAssignableFrom (crowType) && !templateLoading) + miAddChild = typeof(TemplatedContainer).GetProperty ("Content").GetSetMethod (); + else if (typeof(TemplatedGroup).IsAssignableFrom (crowType) && !templateLoading) + miAddChild = typeof(TemplatedGroup).GetMethod ("AddItem", + BindingFlags.Instance | BindingFlags.Public); else if (typeof(TemplatedControl).IsAssignableFrom (crowType)) miAddChild = typeof(TemplatedControl).GetMethod ("loadTemplate", - BindingFlags.Instance | BindingFlags.NonPublic); + BindingFlags.Instance | BindingFlags.NonPublic); else if (typeof(PrivateContainer).IsAssignableFrom (crowType)) miAddChild = typeof(PrivateContainer).GetMethod ("SetChild", BindingFlags.Instance | BindingFlags.NonPublic); } - //push current instance on stack for parenting //loc_0 will be used for child reader.il.Emit (OpCodes.Ldloc_0); @@ -288,13 +296,15 @@ namespace Crow if (t == null) throw new Exception (reader.Name + " type not found"); - reader.il.Emit(OpCodes.Newobj, t.GetConstructors () [0]);//TODO:search parameterless ctor + reader.il.Emit (OpCodes.Newobj, t.GetConstructors () [0]);//TODO:search parameterless ctor reader.il.Emit (OpCodes.Stloc_0);//child is now loc_0 CompilerServices.emitSetCurInterface (il); - reader.emitLoader(t); + reader.emitLoader (t); reader.il.Emit (OpCodes.Ldloc_0);//load child on stack for parenting + if (miAddChild == null) + System.Diagnostics.Debugger.Break (); reader.il.Emit (OpCodes.Callvirt, miAddChild); reader.il.Emit (OpCodes.Stloc_0); //reset local to current go reader.il.Emit (OpCodes.Ldloc_0);//save current go onto the stack if child has to be added |