From 4ca12da2740078a292600736c60c09897263ef95 Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Fri, 5 Aug 2016 03:59:33 +0200 Subject: [PATCH] =?utf8?q?ItemTemplate=20class=20derived=20from=20IMLStrea?= =?utf8?q?m.=20replace=20xmlLoadingCount=20by=20xmlLoading.=20=09modifi?= =?utf8?q?=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20src/GraphicObjects/ListB?= =?utf8?q?ox.cs=20=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20src/Gra?= =?utf8?q?phicObjects/TemplatedControl.cs=20=09modifi=C3=A9=C2=A0:=20=20?= =?utf8?q?=20=20=20=20=20=20=20src/IMLStream.cs=20=09modifi=C3=A9=C2=A0:?= =?utf8?q?=20=20=20=20=20=20=20=20=20src/Interface.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/GraphicObjects/ListBox.cs | 10 +++---- src/GraphicObjects/TemplatedControl.cs | 10 +++---- src/IMLStream.cs | 40 ++++++++++++++++++++++++-- src/Interface.cs | 10 +------ 4 files changed, 49 insertions(+), 21 deletions(-) diff --git a/src/GraphicObjects/ListBox.cs b/src/GraphicObjects/ListBox.cs index fdb3960f..c4f9e0e1 100644 --- a/src/GraphicObjects/ListBox.cs +++ b/src/GraphicObjects/ListBox.cs @@ -113,9 +113,9 @@ namespace Crow #endregion void loading(){ if (itemTemplates == null) - itemTemplates = new Dictionary (); + itemTemplates = new Dictionary (); if (!itemTemplates.ContainsKey ("default")) - itemTemplates["default"] = new IMLStream (ItemTemplate); + itemTemplates["default"] = new ItemTemplate (ItemTemplate); for (int i = 1; i <= (data.Count / itemPerPage) + 1; i++) { if (cancelLoading) @@ -160,7 +160,7 @@ namespace Crow return; GraphicObject g = null; - IMLStream itemStream = null; + ItemTemplate itemStream = null; Type dataType = data [i].GetType (); if (itemTemplates.ContainsKey (dataType.FullName)) @@ -174,8 +174,8 @@ namespace Crow g.DataSource = data [i]; } g.MouseClick += itemClick; - if (!string.IsNullOrEmpty (itemStream.Datas)) { - + if (itemStream.Expand != null && g is Expandable) { + (g as Expandable).Expand += itemStream.Expand; } //g.LogicalParent = this; } diff --git a/src/GraphicObjects/TemplatedControl.cs b/src/GraphicObjects/TemplatedControl.cs index 590ca84b..d3feb1d2 100644 --- a/src/GraphicObjects/TemplatedControl.cs +++ b/src/GraphicObjects/TemplatedControl.cs @@ -56,7 +56,7 @@ namespace Crow #region CTOR public TemplatedControl () : base() { - if (Interface.XmlLoaderCount > 0) + if (Interface.CurrentInterface.XmlLoading) return; loadTemplate (); } @@ -64,7 +64,7 @@ namespace Crow string _template; string _itemTemplate; - protected Dictionary itemTemplates; + protected Dictionary itemTemplates; [XmlAttributeAttribute][DefaultValue(null)] public string Template { @@ -151,10 +151,10 @@ namespace Crow itemTmp = xr.ReadInnerXml (); if (itemTemplates == null) - itemTemplates = new Dictionary (); + itemTemplates = new Dictionary (); //TODO:check encoding - itemTemplates[dataType] = new IMLStream (Encoding.UTF8.GetBytes(itemTmp)); - itemTemplates [dataType].Datas = datas; + itemTemplates[dataType] = new ItemTemplate (Encoding.UTF8.GetBytes(itemTmp)); + itemTemplates [dataType].CreateExpandDelegate(dataType, datas); continue; } diff --git a/src/IMLStream.cs b/src/IMLStream.cs index 7207327d..14262595 100644 --- a/src/IMLStream.cs +++ b/src/IMLStream.cs @@ -20,14 +20,16 @@ // along with this program. If not, see . using System; using System.IO; +using System.Reflection; +using System.Reflection.Emit; namespace Crow { public class IMLStream : MemoryStream { + public string Path; public Type RootType; - //TODO:this has nothing to do in there, I should derive IMLStream - public string Datas; public IMLStream(string path) : base (){ + Path = path; using (Stream stream = Interface.GetStreamFromPath (path)) stream.CopyTo (this); RootType = Interface.GetTopContainerOfXMLStream (this); @@ -36,5 +38,39 @@ namespace Crow RootType = Interface.GetTopContainerOfXMLStream (this); } } + public class ItemTemplate : IMLStream { + public EventHandler Expand; + + public ItemTemplate(string path) + : base(path){} + public ItemTemplate(Byte[] b) + : base(b){} + + public void CreateExpandDelegate (string strDataType, string method){ + Type dataType = Type.GetType(strDataType); + Type host_type = typeof(ItemTemplate);//not sure is the best place to put the dyn method + Type evt_type = typeof(EventHandler); + + MethodInfo evtInvoke = evt_type.GetMethod ("Invoke"); + ParameterInfo [] evtParams = evtInvoke.GetParameters (); + Type handlerArgsType = evtParams [1].ParameterType; + + Type [] args = { typeof (object), typeof (object), handlerArgsType }; + DynamicMethod dm = new DynamicMethod ("dyn_expand_" + method, + typeof (void), + args, + host_type); + + + #region IL generation + ILGenerator il = dm.GetILGenerator (256); + + il.Emit (OpCodes.Ret); + + #endregion + + Expand = (EventHandler)dm.CreateDelegate (evt_type, this); + } + } } diff --git a/src/Interface.cs b/src/Interface.cs index ca5cb28f..3233cdfe 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -59,11 +59,6 @@ namespace Crow #endregion #region Static and constants - /// keep ressource path for debug msg - internal static string CurrentGOMLPath = ""; - //used in templatedControl - internal static int XmlLoaderCount = 0; - public static int TabSize = 4; public static string LineBreak = "\r\n"; //TODO: shold be declared in graphicObject @@ -227,17 +222,14 @@ namespace Crow System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; - Interface.XmlLoaderCount ++; - CurrentGOMLPath = path; GraphicObject tmp = null; try { using (Stream stream = GetStreamFromPath (path)) { tmp = Load(stream, GetTopContainerOfXMLStream(stream), hostClass); } } catch (Exception ex) { - throw new Exception ("Error loading <" + CurrentGOMLPath + ">:", ex); + throw new Exception ("Error loading <" + path + ">:", ex); } - Interface.XmlLoaderCount --; Thread.CurrentThread.CurrentCulture = savedCulture; -- 2.47.3