From 69c470239bfbab11192f3699f29b5352d74d3630 Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Wed, 10 Aug 2016 03:07:00 +0200 Subject: ItemTemplate test, major changes in Instantiator implementation bugs modifié : Crow.csproj modifié : Default.style modifié : Tests/BasicTests.cs modifié : Tests/Interfaces/Divers/welcome.crow modifié : Tests/Interfaces/basicTests/0.crow modifié : Tests/Interfaces/basicTests/4.crow modifié : src/GraphicObjects/ListBox.cs modifié : src/GraphicObjects/ProgressBar.cs modifié : src/GraphicObjects/TemplatedControl.cs modifié : src/IMLReader.cs modifié : src/Instantiator.cs modifié : src/Interface.cs renommé : src/IMLStream.cs -> src/ItemTemplate.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/GraphicObjects/TemplatedControl.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/GraphicObjects/TemplatedControl.cs') diff --git a/src/GraphicObjects/TemplatedControl.cs b/src/GraphicObjects/TemplatedControl.cs index b325a0d2..f78173d2 100644 --- a/src/GraphicObjects/TemplatedControl.cs +++ b/src/GraphicObjects/TemplatedControl.cs @@ -65,7 +65,8 @@ namespace Crow string _template; string _itemTemplate; - public Dictionary ItemTemplates; + public Dictionary ItemTemplates = new Dictionary();//TODO: dont instantiate if not used + //but then i should test if null in msil gen [XmlAttributeAttribute][DefaultValue(null)] public string Template { @@ -84,7 +85,7 @@ namespace Crow [XmlAttributeAttribute][DefaultValue("#Crow.Templates.ItemTemplate.goml")] public string ItemTemplate { get { return _itemTemplate; } - set { + set { if (value == _itemTemplate) return; @@ -146,7 +147,7 @@ namespace Crow xr.Read (); //read first child xr.Read (); //skip root node - while (!xr.EOF) { + while (!xr.EOF) { if (!xr.IsStartElement ()) { xr.Read (); continue; @@ -164,8 +165,11 @@ namespace Crow if (ItemTemplates == null) ItemTemplates = new Dictionary (); - //TODO:check encoding - ItemTemplates[dataType] = new ItemTemplate (Encoding.UTF8.GetBytes(itemTmp)); + + using (IMLReader iTmp = new IMLReader (null, itemTmp)) { + ItemTemplates [dataType] = + new ItemTemplate (iTmp.RootType, iTmp.GetLoader ()); + } if (!string.IsNullOrEmpty (datas)) ItemTemplates [dataType].CreateExpandDelegate(this, dataType, datas); @@ -197,7 +201,7 @@ namespace Crow //if no template found, load default one if (this.child == null) loadTemplate (); - + //normal xml read using (XmlReader xr = new XmlTextReader (tmp, XmlNodeType.Element, null)) { xr.Read (); -- cgit v1.3.1-4-g156e From 13b1a9755a9e036122dd2f3e29714bbb5141118c Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Wed, 10 Aug 2016 14:24:30 +0200 Subject: TreeView expand delegate creation in IMLReader --- src/GraphicObjects/TemplatedControl.cs | 4 +-- src/IMLReader.cs | 49 +++++++++++++++++++++++----------- src/ItemTemplate.cs | 12 ++++++--- 3 files changed, 43 insertions(+), 22 deletions(-) (limited to 'src/GraphicObjects/TemplatedControl.cs') diff --git a/src/GraphicObjects/TemplatedControl.cs b/src/GraphicObjects/TemplatedControl.cs index 63bdbb21..422a45ec 100644 --- a/src/GraphicObjects/TemplatedControl.cs +++ b/src/GraphicObjects/TemplatedControl.cs @@ -167,10 +167,10 @@ namespace Crow using (IMLReader iTmp = new IMLReader (null, itemTmp)) { ItemTemplates [dataType] = - new ItemTemplate (iTmp.RootType, iTmp.GetLoader ()); + new ItemTemplate (iTmp.RootType, iTmp.GetLoader (), dataType, datas); } if (!string.IsNullOrEmpty (datas)) - ItemTemplates [dataType].CreateExpandDelegate(this, dataType, datas); + ItemTemplates [dataType].CreateExpandDelegate(this); continue; } diff --git a/src/IMLReader.cs b/src/IMLReader.cs index 1b2a5efb..87426726 100644 --- a/src/IMLReader.cs +++ b/src/IMLReader.cs @@ -111,6 +111,7 @@ namespace Crow string templatePath = reader.GetAttribute ("Template"); //string itemTemplatePath = reader.GetAttribute ("ItemTemplate"); + List itemTemplateIds = new List (); bool inlineTemplate = false; reader.Read (); @@ -121,7 +122,7 @@ namespace Crow inlineTemplate = true; reader.Read (); - readChildren (reader, crowType); + readChildren (reader, crowType,true); continue; } else if (reader.Name == "ItemTemplate") { string dataType = "default", datas = "", path = ""; @@ -137,20 +138,10 @@ namespace Crow using (IMLReader iTmp = new IMLReader (null, reader.ReadInnerXml ())) { string uid = Guid.NewGuid ().ToString (); Interface.Instantiators [uid] = - new ItemTemplate (iTmp.RootType, iTmp.GetLoader ()); - reader.il.Emit (OpCodes.Ldloc_0);//load TempControl ref - reader.il.Emit (OpCodes.Ldfld,//load ItemTemplates dic field - typeof(TemplatedControl).GetField("ItemTemplates")); - reader.il.Emit (OpCodes.Ldstr, dataType);//load key - reader.il.Emit (OpCodes.Ldstr, uid);//load value - reader.il.Emit (OpCodes.Callvirt, - typeof(Interface).GetMethod ("GetItemTemplate")); - reader.il.Emit (OpCodes.Callvirt, - typeof(Dictionary).GetMethod ("set_Item", - new Type[] { typeof(string), typeof(ItemTemplate) })); + new ItemTemplate (iTmp.RootType, iTmp.GetLoader (), dataType, datas); + + itemTemplateIds.Add (new string[] { dataType, uid, datas }); } -// if (!string.IsNullOrEmpty (datas)) -// ItemTemplates [dataType].CreateExpandDelegate(this, dataType, datas); continue; } @@ -173,6 +164,32 @@ namespace Crow crowType.GetMethod ("loadTemplate", BindingFlags.Instance | BindingFlags.NonPublic)); } } + foreach (string[] iTempId in itemTemplateIds) { + reader.il.Emit (OpCodes.Ldloc_0);//load TempControl ref + reader.il.Emit (OpCodes.Ldfld,//load ItemTemplates dic field + typeof(TemplatedControl).GetField("ItemTemplates")); + reader.il.Emit (OpCodes.Ldstr, iTempId[0]);//load key + reader.il.Emit (OpCodes.Ldstr, iTempId[1]);//load value + reader.il.Emit (OpCodes.Callvirt, + typeof(Interface).GetMethod ("GetItemTemplate")); + reader.il.Emit (OpCodes.Callvirt, + typeof(Dictionary).GetMethod ("set_Item", + new Type[] { typeof(string), typeof(ItemTemplate) })); + + if (!string.IsNullOrEmpty (iTempId [2])) { + //expand delegate creation + reader.il.Emit (OpCodes.Ldloc_0);//load TempControl ref + reader.il.Emit (OpCodes.Ldfld,//load ItemTemplates dic field + typeof(TemplatedControl).GetField ("ItemTemplates")); + reader.il.Emit (OpCodes.Ldstr, iTempId [0]);//load key + reader.il.Emit (OpCodes.Callvirt, + typeof(Dictionary).GetMethod ("get_Item", + new Type[] { typeof(string) })); + reader.il.Emit (OpCodes.Ldloc_0);//load root of treeView + reader.il.Emit (OpCodes.Callvirt, + typeof(ItemTemplate).GetMethod ("CreateExpandDelegate")); + } + } } } @@ -229,7 +246,7 @@ namespace Crow /// /// Parse child node an generate corresponding msil /// - void readChildren(IMLReader reader, Type crowType){ + void readChildren(IMLReader reader, Type crowType, bool templateLoading = false){ MethodInfo miAddChild = null; bool endTagReached = false; while (reader.Read()){ @@ -250,7 +267,7 @@ namespace Crow miAddChild = typeof(Group).GetMethod ("AddChild"); else if (typeof(Container).IsAssignableFrom (crowType)) miAddChild = typeof(Container).GetMethod ("SetChild"); - else if (typeof(TemplatedContainer).IsAssignableFrom (crowType)) + else if (typeof(TemplatedContainer).IsAssignableFrom (crowType)&&!templateLoading) miAddChild = typeof(TemplatedContainer).GetProperty("Content").GetSetMethod(); else if (typeof(TemplatedControl).IsAssignableFrom (crowType)) miAddChild = typeof(TemplatedControl).GetMethod ("loadTemplate", diff --git a/src/ItemTemplate.cs b/src/ItemTemplate.cs index ebc7b282..1afefe2b 100644 --- a/src/ItemTemplate.cs +++ b/src/ItemTemplate.cs @@ -32,18 +32,22 @@ namespace Crow { public class ItemTemplate : Instantiator { public EventHandler Expand; + string strDataType; + string fetchMethodName; #region CTOR public ItemTemplate(string path) : base(path) { } - public ItemTemplate (Type _root, Interface.LoaderInvoker _loader) + public ItemTemplate (Type _root, Interface.LoaderInvoker _loader,string _dataType, string _fetchDataMethod) :base(_root, _loader) { + strDataType = _dataType; + fetchMethodName = _fetchDataMethod; } #endregion - public void CreateExpandDelegate (TemplatedControl host, string strDataType, string method){ + public void CreateExpandDelegate (TemplatedControl host){ Type dataType = Type.GetType(strDataType); Type hostType = typeof(TemplatedControl);//not sure is the best place to put the dyn method Type evtType = typeof(EventHandler); @@ -54,7 +58,7 @@ namespace Crow Type handlerArgsType = evtParams [1].ParameterType; Type [] args = { typeof (object), typeof (object), handlerArgsType }; - DynamicMethod dm = new DynamicMethod ("dyn_expand_" + method, + DynamicMethod dm = new DynamicMethod ("dyn_expand_" + fetchMethodName, typeof (void), args, hostType); @@ -81,7 +85,7 @@ namespace Crow il.Emit (OpCodes.Ldarg_1); il.Emit (OpCodes.Callvirt, typeof(GraphicObject).GetProperty("DataSource").GetGetMethod ()); - MethodInfo miGetDatas = dataType.GetMethod (method, new Type[] {}); + MethodInfo miGetDatas = dataType.GetMethod (fetchMethodName, new Type[] {}); il.Emit (OpCodes.Callvirt, miGetDatas); il.Emit (OpCodes.Callvirt, listBoxType.GetProperty("Data").GetSetMethod ()); -- cgit v1.3.1-4-g156e