]> O.S.I.I.S - jp/crow.git/commitdiff
ItemTemplate test, major changes in Instantiator implementation
authorjpbruyere <jp.bruyere@hotmail.com>
Wed, 10 Aug 2016 01:07:00 +0000 (03:07 +0200)
committerjpbruyere <jp.bruyere@hotmail.com>
Wed, 10 Aug 2016 01:07:00 +0000 (03:07 +0200)
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

14 files changed:
Crow.csproj
Default.style
Tests/BasicTests.cs
Tests/Interfaces/Divers/welcome.crow
Tests/Interfaces/basicTests/0.crow
Tests/Interfaces/basicTests/4.crow
src/GraphicObjects/ListBox.cs
src/GraphicObjects/ProgressBar.cs
src/GraphicObjects/TemplatedControl.cs
src/IMLReader.cs
src/IMLStream.cs [deleted file]
src/Instantiator.cs
src/Interface.cs
src/ItemTemplate.cs [new file with mode: 0644]

index 0d75d56c101606545bdd7e357378c397b64b6e52..9ca7cb987858d79380ba65ece26c251982116e74 100644 (file)
     <Compile Include="src\CompilerServices\MemberReference.cs" />
     <Compile Include="src\CompilerServices\Bindings.cs" />
     <Compile Include="src\StyleReader.cs" />
-    <Compile Include="src\IMLStream.cs" />
     <Compile Include="src\GraphicObjects\DirectoryView.cs" />
-    <Compile Include="src\IMLInstantiatorBuilder.cs" />
     <Compile Include="src\Instantiator.cs" />
+    <Compile Include="src\IMLReader.cs" />
+    <Compile Include="src\ItemTemplate.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System" />
     <Reference Include="System.Xml" />
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="System.Drawing" />
-       <Reference Include="cairo-sharp"/>
-       <Reference Include="gdk-sharp"/>
-       <Reference Include="gio-sharp"/>
-       <Reference Include="glib-sharp"/>
+    <Reference Include="cairo-sharp">
+      <Package>gtk-sharp-3.0</Package>
+    </Reference>
+    <Reference Include="gdk-sharp">
+      <Package>gtk-sharp-2.0</Package>
+    </Reference>
+    <Reference Include="gio-sharp">
+      <Package>gio-sharp-3.0</Package>
+    </Reference>
+    <Reference Include="glib-sharp">
+      <Package>glib-sharp-3.0</Package>
+    </Reference>
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
index f3ec0113123038b3bc49e2d8be9d5053aba17b59..b21bd5777c0dd31a7e6313613a2ee53d8b69b099 100644 (file)
@@ -42,4 +42,7 @@ Window {
 }
 Border {
        Foreground = White;
+}
+ProgressBar {
+       Foreground = vgradient|0:BlueCrayola|0.5:SkyBlue|1:BlueCrayola;
 }
\ No newline at end of file
index 249c9abb31f2d72783b2f3db7ff6a5207edb0752..c862ccac462b846c87cc489365ca80cd8b9165c2 100644 (file)
@@ -92,8 +92,8 @@ namespace Tests
 
                        this.KeyDown += KeyboardKeyDown1;
 
-                       testFiles = new string [] { @"Interfaces/Divers/welcome.crow" };
-                       testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/basicTests", "*.crow")).ToArray ();
+                       //testFiles = new string [] { @"Interfaces/Divers/welcome.crow" };
+                       testFiles = Directory.GetFiles (@"Interfaces/basicTests", "*.crow");
                        testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Container", "*.crow")).ToArray ();
                        testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/GraphicObject", "*.crow")).ToArray ();
                        testFiles = testFiles.Concat (Directory.GetFiles (@"Interfaces/Group", "*.crow")).ToArray ();
@@ -147,17 +147,17 @@ namespace Tests
                }
                void Tv_SelectedItemChanged (object sender, SelectionChangeEventArgs e)
                {
-                       FileInfo fi = e.NewValue as FileInfo;
-                       if (fi == null)
-                               return;
-                       if (fi.Extension == ".crow" || fi.Extension == ".goml") {
-                               IMLStream imls = new IMLStream (fi.FullName);
-                               lock (CrowInterface.UpdateMutex) {
-                                       (CrowInterface.FindByName ("crowContainer") as Container).SetChild
-                                       (imls.Instance);
-                                       CurSources = imls.Source;
-                               }
-                       }
+//                     FileInfo fi = e.NewValue as FileInfo;
+//                     if (fi == null)
+//                             return;
+//                     if (fi.Extension == ".crow" || fi.Extension == ".goml") {
+//                             IMLStream imls = new IMLStream (fi.FullName);
+//                             lock (CrowInterface.UpdateMutex) {
+//                                     (CrowInterface.FindByName ("crowContainer") as Container).SetChild
+//                                     (imls.Instance);
+//                                     CurSources = imls.Source;
+//                             }
+//                     }
                }
                void onButClick(object send, MouseButtonEventArgs e)
                {
index 65fc3577b0fa2860374c2165bfd773f6be77f452..b82949c180e55daf2f776e27eb1d8fdbcb2ae244 100644 (file)
@@ -1,6 +1,11 @@
 <?xml version="1.0"?>
 <VerticalStack Fit="true" VerticalAlignment="Top" Background="0.7,0.7,0.7,0.5"
+       MouseEnter="{Background=Blue}"
+       MouseLeave="{Background=Onyx}"
        Margin="10" CornerRadius="10">
-       <Label Font="20" Text="Press &lt;F3&gt; to cycle into the examples"/>
+       <Label Font="20" Text="Press &lt;F3&gt; to cycle into the examples"
+       MouseEnter="{Background=Red}"
+       MouseLeave="{Background=Transparent}"
+       />
        <Label Font="20" Text="Those are basic tests used to validate changes"/>
 </VerticalStack>
\ No newline at end of file
index 9d3948af6a922327f6206f3b392612c2241f584d..0e803a81e11a63ed57778eea23902a0aa1b1c412 100755 (executable)
@@ -1,2 +1,4 @@
 <?xml version="1.0"?>
-<CheckBox/>
\ No newline at end of file
+<Expandable >
+
+</Expandable>
index f696a9ec9f386f57b14733382581fdca8609f86f..854a2e4edea73173b81dd13adb2783c48d2a2276 100755 (executable)
@@ -1,8 +1,8 @@
 <?xml version="1.0"?>
 <HorizontalStack Fit="true">
        <VerticalStack Fit="true" Name="vsFps"  Spacing="10" >
-               <ProgressBar CornerRadius="5" Background="DimGray" Margin="1" Maximum="1000" Value="{fps}" Width="200" Height="15"/>
-               <HorizontalStack Fit="true">
+<!--           <ProgressBar CornerRadius="5" Background="DimGray" Margin="1" Maximum="1000" Value="{fps}" Width="200" Height="15"/>
+               <HorizontalStack Fit="true">-->
                        <Label Text="Memory:" Width="50" TextAlignment="Right"/>
                        <Label Text="{memory}" Font="droid,12" TextAlignment="Center"
                                Background="vgradient|0:BlueCrayola|1:Black"/>
index 58d41e0ba177763b249ea159287a68501b900c96..ab349ed4c95fa6404c9e5b4e0b3ad70b2e859141 100644 (file)
@@ -119,7 +119,7 @@ namespace Crow
                        if (ItemTemplates == null)
                                ItemTemplates = new Dictionary<string, ItemTemplate> ();
                        if (!ItemTemplates.ContainsKey ("default"))
-                               ItemTemplates["default"] = new ItemTemplate (ItemTemplate);
+                               ItemTemplates ["default"] = Interface.GetItemTemplate (ItemTemplate);
 
                        for (int i = 1; i <= (data.Count / itemPerPage) + 1; i++) {
                                if (cancelLoading)
@@ -164,16 +164,16 @@ namespace Crow
                                        return;
                                
                                GraphicObject g = null;
-                               ItemTemplate itemStream = null;
+                               ItemTemplate iTemp = null;
                                Type dataType = data [i].GetType ();
 
                                if (ItemTemplates.ContainsKey (dataType.FullName))
-                                       itemStream = ItemTemplates [dataType.FullName];
+                                       iTemp = ItemTemplates [dataType.FullName];
                                else
-                                       itemStream = ItemTemplates ["default"];
+                                       iTemp = ItemTemplates ["default"];
 
                                lock (Interface.CurrentInterface.LayoutMutex) {
-                                       g = itemStream.Instance;
+                                       g = iTemp.CreateInstance();
                                        page.AddChild (g);
                                        g.DataSource = data [i];
                                }
@@ -190,8 +190,8 @@ namespace Crow
                                }else
                                        g.MouseClick += itemClick;
                                
-                               if (itemStream.Expand != null && g is Expandable) {
-                                       (g as Expandable).Expand += itemStream.Expand;
+                               if (iTemp.Expand != null && g is Expandable) {
+                                       (g as Expandable).Expand += iTemp.Expand;
                                }
                                //g.LogicalParent = this;
                        }
index e3f778bfdbe8da40cc29f3e632b6674a321bfa67..6e1ce404b5761c6eed6553225570be66ff0b635b 100644 (file)
@@ -9,7 +9,7 @@ using System.ComponentModel;
 
 namespace Crow
 {
-       [Serializable]
+       
        public class ProgressBar : NumericControl
     {
                #region CTOR
@@ -22,12 +22,6 @@ namespace Crow
                }
 
                #region GraphicObject overrides
-               [XmlAttributeAttribute()][DefaultValue("vgradient|0:BlueCrayola|0,5:SkyBlue|1:BlueCrayola")]
-               public override Fill Foreground {
-                       get { return base.Foreground; }
-                       set { base.Foreground = value; }
-               }
-
                protected override void onDraw (Context gr)
                {
                        base.onDraw (gr);
index b325a0d28e676ae5a0c0204ad1491dbb2469fbaa..f78173d263ff93ef77d47d20884a6a84a6274ad8 100644 (file)
@@ -65,7 +65,8 @@ namespace Crow
 
                string _template;
                string _itemTemplate;
-               public Dictionary<string, ItemTemplate> ItemTemplates;
+               public Dictionary<string, ItemTemplate> ItemTemplates = new Dictionary<string, Crow.ItemTemplate>();//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<string, ItemTemplate> ();
-                                                               //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 ();
index c1238081792915d1fd4af1864f27cbd9384c337d..0474fdc0581db1cd09b5c8c18cdf707460c86199 100644 (file)
@@ -24,11 +24,14 @@ using System.IO;
 using System.Reflection;
 using System.Reflection.Emit;
 using System.Linq;
+using System.Collections.Generic;
 
 namespace Crow
 {
        public class IMLReader : XmlTextReader
        {
+               Interface.LoaderInvoker loader = null;
+
                public string ImlPath;
                public Stream ImlStream;
                public Type RootType = null;
@@ -45,27 +48,40 @@ namespace Crow
                        : base(stream)
                {
                        ImlStream = stream;
-                       readRootType();
-                       InitEmitter();
-                       BuildInstanciator(RootType);
-                       Read();//close tag
+                       createInstantiator ();
                }
+               /// <summary>
+               /// Used to parse xmlFrament with same code generator linked
+               /// If ilGen=null, a new Code Generator will be created.
+               /// </summary>
                public IMLReader (ILGenerator ilGen, string xmlFragment)
                        : base(xmlFragment, XmlNodeType.Element,null){
                        il = ilGen;
+
+                       if (il != null)
+                               return;
+
+                       createInstantiator();
                }
                #endregion
 
+               void createInstantiator(){
+                       readRootType();
+                       InitEmitter();
+                       emitLoader(RootType);
+                       Read();//close tag
+               }
                /// <summary>
                /// Finalize instatiator MSIL and return LoaderInvoker delegate
                /// </summary>
-               public Instantiator GetInstanciator(){
-                       il.Emit(OpCodes.Ret);
+               public Interface.LoaderInvoker GetLoader(){
+                       if (loader != null)
+                               return loader;
 
-                       return new Instantiator (RootType,
-                               (Interface.LoaderInvoker)dm.CreateDelegate (typeof(Interface.LoaderInvoker)));
+                       il.Emit(OpCodes.Ret);
+                       loader = (Interface.LoaderInvoker)dm.CreateDelegate (typeof(Interface.LoaderInvoker));
+                       return loader;
                }
-
                /// <summary>
                /// Inits il generator, RootType must have been read first
                /// </summary>
@@ -83,7 +99,7 @@ namespace Crow
                        il.Emit (OpCodes.Ldarg_0);
                        il.Emit (OpCodes.Stloc_0);
                }
-               void BuildInstanciator(Type crowType){
+               void emitLoader(Type crowType){
                        string tmpXml = ReadOuterXml ();
 
                        il.Emit (OpCodes.Ldloc_0);//save current go onto the stack if child has to be added
@@ -92,10 +108,11 @@ namespace Crow
                                //if its a template, first read template elements
                                using (IMLReader reader = new IMLReader (il, tmpXml)) {
 
-                                       string template = reader.GetAttribute ("Template");
+                                       string templatePath = reader.GetAttribute ("Template");
+                                       //string itemTemplatePath = reader.GetAttribute ("ItemTemplate");
 
                                        bool inlineTemplate = false;
-                                       if (string.IsNullOrEmpty (template)) {
+                                       if (string.IsNullOrEmpty (templatePath)) {
                                                reader.Read ();
 
                                                while (reader.Read ()) {
@@ -107,38 +124,48 @@ namespace Crow
 
                                                                readChildren (reader, crowType);
                                                                continue;
-                                                       }else if (reader.Name == "ItemTemplate") {
+                                                       } else if (reader.Name == "ItemTemplate") {
                                                                reader.Skip ();
-                                                               //                                                              string dataType = "default", datas = "", itemTmp;
-                                                               //                                                              while (reader.MoveToNextAttribute ()) {
-                                                               //                                                                      if (reader.Name == "DataType")
-                                                               //                                                                              dataType = reader.Value;
-                                                               //                                                                      else if (reader.Name == "Data")
-                                                               //                                                                              datas = reader.Value;
-                                                               //                                                              }
-                                                               //
-                                                               //                                                              reader.Read();
-                                                               //                                                              itemTmp = .ReadInnerXml ();
-                                                               //
-                                                               //                                                              if (ItemTemplates == null)
-                                                               //                                                                      ItemTemplates = new Dictionary<string, ItemTemplate> ();
-                                                               //                                                              //TODO:check encoding
-                                                               //                                                              ItemTemplates[dataType] = new ItemTemplate (Encoding.UTF8.GetBytes(itemTmp));
-                                                               //                                                              if (!string.IsNullOrEmpty (datas))
-                                                               //                                                                      ItemTemplates [dataType].CreateExpandDelegate(this, dataType, datas);
+                                                               continue;
+
+                                                               string dataType = "default", datas = "", path = "";
+                                                               while (reader.MoveToNextAttribute ()) {
+                                                                       if (reader.Name == "DataType")
+                                                                               dataType = reader.Value;
+                                                                       else if (reader.Name == "Data")
+                                                                               datas = reader.Value;
+                                                                       else if (reader.Name == "Path")
+                                                                               path = reader.Value;
+                                                               }
+
+                                                               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.Ldstr, dataType);
+                                                                       reader.il.Emit (OpCodes.Ldstr, uid);
+                                                                       reader.il.Emit (OpCodes.Callvirt,
+                                                                               typeof(Interface).GetMethod ("GetItemTemplate"));
+                                                                       reader.il.Emit (OpCodes.Callvirt,
+                                                                               typeof(Dictionary<string, ItemTemplate>).GetMethod ("Add",
+                                                                                       new Type[] { typeof(string), typeof(ItemTemplate) }));
+                                                               }
+//                                                     if (!string.IsNullOrEmpty (datas))
+//                                                             ItemTemplates [dataType].CreateExpandDelegate(this, dataType, datas);
 
                                                                continue;
                                                        }
-                                               }
-                                               if (!inlineTemplate) {
-                                                       DefaultTemplate dt = (DefaultTemplate)crowType.GetCustomAttributes (typeof(DefaultTemplate), true).FirstOrDefault();
-                                                       template = dt.Path;
+
+                                                       if (!inlineTemplate) {
+                                                               DefaultTemplate dt = (DefaultTemplate)crowType.GetCustomAttributes (typeof(DefaultTemplate), true).FirstOrDefault ();
+                                                               templatePath = dt.Path;
+                                                       }
                                                }
                                        }
                                        if (!inlineTemplate) {
                                                reader.il.Emit (OpCodes.Ldloc_0);//Load  this templateControl ref
 
-                                               reader.il.Emit (OpCodes.Ldstr, template); //Load template path string
+                                               reader.il.Emit (OpCodes.Ldstr, templatePath); //Load template path string
                                                reader.il.Emit (OpCodes.Callvirt,//call Interface.Load(path)
                                                        typeof(Interface).GetMethod ("Load", BindingFlags.Static | BindingFlags.Public));
                                        }
@@ -245,7 +272,7 @@ namespace Crow
                                        reader.il.Emit(OpCodes.Newobj, t.GetConstructors () [0]);
                                        reader.il.Emit (OpCodes.Stloc_0);//child is now loc_0
 
-                                       reader.BuildInstanciator(t);
+                                       reader.emitLoader(t);
 
                                        reader.il.Emit (OpCodes.Ldloc_0);//load child on stack for parenting
                                        reader.il.Emit (OpCodes.Callvirt, miAddChild);
diff --git a/src/IMLStream.cs b/src/IMLStream.cs
deleted file mode 100644 (file)
index 290c535..0000000
+++ /dev/null
@@ -1,130 +0,0 @@
-//
-//  IMLStream.cs
-//
-//  Author:
-//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
-//
-//  Copyright (c) 2016 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.IO;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using System.Xml.Serialization;
-using System.Xml;
-using System.Collections.Generic;
-using System.Diagnostics;
-
-namespace Crow
-{
-       public class IMLStream : MemoryStream {
-               public string Path;
-               public Type RootType;
-               public IMLStream(string path) : base (){
-                       Path = path;
-                       using (Stream stream = Interface.GetStreamFromPath (path))
-                               stream.CopyTo (this);
-                       //RootType = Interface.GetTopContainerOfXMLStream (this);
-               }
-               public IMLStream(Byte[] b) : base (b){                  
-                       //RootType = Interface.GetTopContainerOfXMLStream (this);
-               }
-               /// <summary>
-               /// Create a graphicObject instance from the this XML stream.
-               /// </summary>
-               public GraphicObject Instance {
-                       get {
-                               System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
-                               Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
-
-                               Seek (0, SeekOrigin.Begin);
-                               GraphicObject tmp = null;//Interface.Load (this, this.RootType);
-
-                               Thread.CurrentThread.CurrentCulture = savedCulture;
-                               return tmp;
-                       }
-               }
-               /// <summary>
-               /// Gets the xml source code as a string
-               /// </summary>
-               public string Source {
-                       get {
-                               Seek (0, SeekOrigin.Begin);
-                               using (StreamReader sr = new StreamReader(this))
-                                       return sr.ReadToEnd();
-                       }
-               }
-       }
-       public class ItemTemplate : IMLStream {         
-               public EventHandler Expand;
-
-               public ItemTemplate(string path)
-                       : base(path){}
-               public ItemTemplate(Byte[] b)
-                       : base(b){}
-
-               public void CreateExpandDelegate (TemplatedControl host, string strDataType, string method){
-                       Type dataType = Type.GetType(strDataType);
-                       Type hostType = typeof(TemplatedControl);//not sure is the best place to put the dyn method
-                       Type evtType = typeof(EventHandler);
-                       Type listBoxType = typeof(ListBox);
-
-                       MethodInfo evtInvoke = evtType.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,
-                               hostType);
-
-
-                       #region IL generation
-                       ILGenerator il = dm.GetILGenerator (256);
-                       il.DeclareLocal(typeof(GraphicObject));
-
-                       il.Emit (OpCodes.Ldarg_1);
-
-                       MethodInfo miFindByName = typeof(GraphicObject).GetMethod("FindByName");
-                       il.Emit(OpCodes.Ldstr, "List");
-                       il.Emit (OpCodes.Callvirt, miFindByName);
-                       il.Emit (OpCodes.Stloc_0);
-
-                       FieldInfo fiTemplates = typeof(TemplatedControl).GetField("ItemTemplates");
-                       il.Emit (OpCodes.Ldloc_0);
-                       il.Emit (OpCodes.Ldarg_0);
-                       il.Emit (OpCodes.Ldfld, fiTemplates);
-                       il.Emit (OpCodes.Stfld, fiTemplates);
-
-                       il.Emit (OpCodes.Ldloc_0);
-                       il.Emit (OpCodes.Ldarg_1);
-                       il.Emit (OpCodes.Callvirt, typeof(GraphicObject).GetProperty("DataSource").GetGetMethod ());
-
-                       MethodInfo miGetDatas = dataType.GetMethod (method, new Type[] {});
-                       il.Emit (OpCodes.Callvirt, miGetDatas);
-
-                       il.Emit (OpCodes.Callvirt, listBoxType.GetProperty("Data").GetSetMethod ());
-
-                       il.Emit (OpCodes.Ret);
-
-                       #endregion
-
-                       Expand = (EventHandler)dm.CreateDelegate (evtType, host);
-               }
-       }
-}
-
index e54e744ee8856abd19f03301da2fc3d737270e35..23871ef28923763ffaf27168db425fabd4fde3a1 100644 (file)
 //  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.Threading;
 
 namespace Crow
 {
        public class Instantiator
        {
-               Type RootType;
-               Interface.LoaderInvoker Loader;
-               public Instantiator (Type _root, Interface.LoaderInvoker loader)
+               public Type RootType;
+               Interface.LoaderInvoker loader;
+
+               #region CTOR
+               public Instantiator (string path){
+                       System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
+                       Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
+
+                       #if DEBUG_LOAD
+                       Stopwatch loadingTime = new Stopwatch ();
+                       loadingTime.Start ();
+                       #endif
+                       try {
+                               using (IMLReader itr = new IMLReader (path)){
+                                       loader = itr.GetLoader ();
+                                       RootType = itr.RootType;
+                               }
+                       } catch (Exception ex) {
+                               throw new Exception ("Error loading <" + path + ">:", ex);
+                       }
+
+                       #if DEBUG_LOAD
+                       loadingTime.Stop ();
+                       Debug.WriteLine ("IML Instantiator creation '{2}' : {0} ticks, {1} ms",
+                       loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path);
+                       #endif
+
+                       Thread.CurrentThread.CurrentCulture = savedCulture;                     
+               }
+               public Instantiator (Type _root, Interface.LoaderInvoker _loader)
                {
                        RootType = _root;
-                       Loader = loader;
+                       loader = _loader;
                }
+               #endregion
+
                public GraphicObject CreateInstance(){
                        GraphicObject tmp = (GraphicObject)Activator.CreateInstance(RootType);
-                       Loader (tmp);
+                       loader (tmp);
                        return tmp;
                }
        }
index f3d8c73bc98b98442f5739bb372034b23d021458..517d481afc493a0b5dcb3af7ce63effe0557e924 100644 (file)
@@ -28,6 +28,7 @@ using System.Threading;
 using System.Xml;
 using System.Xml.Serialization;
 using Cairo;
+using System.Globalization;
 
 namespace Crow
 {
@@ -55,6 +56,7 @@ namespace Crow
                }
                public Interface(){
                        Interface.CurrentInterface = this;
+                       CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.InvariantCulture; 
                }
                #endregion
 
@@ -195,47 +197,27 @@ namespace Crow
                        System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
                        Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
 
-                       GraphicObject tmp = null;
                        try {
-                               if (!Instantiators.ContainsKey(path))
-                                       BuildInstaciator(path);
-                               tmp = Instantiators [path].CreateInstance ();
-
+                               return GetInstantiator (path).CreateInstance ();
                        } catch (Exception ex) {
                                throw new Exception ("Error loading <" + path + ">:", ex);
                        }
 
                        Thread.CurrentThread.CurrentCulture = savedCulture;
-
-                       return tmp;
                }
-               public static void BuildInstaciator (string path)
-               {
-                       System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
-                       Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
-
-                       #if DEBUG_LOAD
-                       Stopwatch loadingTime = new Stopwatch ();
-                       loadingTime.Start ();
-                       #endif
-
-                       try {
-                               using (IMLReader itr = new IMLReader (path)){
-                                       Instantiators[path] = itr.GetInstanciator();
-                               }
-                       } catch (Exception ex) {
-                               throw new Exception ("Error loading <" + path + ">:", ex);
-                       }
-
-                       #if DEBUG_LOAD
-                       loadingTime.Stop ();
-                       Debug.WriteLine ("IML Loading '{2}' : {0} ticks, {1} ms",
-                       loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path);
-                       #endif
-
-                       Thread.CurrentThread.CurrentCulture = savedCulture;
+               /// <summary>
+               /// fetch it from cache or create it
+               /// </summary>
+               public static Instantiator GetInstantiator(string path){
+                       if (!Instantiators.ContainsKey(path))
+                               Instantiators [path] = new Instantiator(path);
+                       return Instantiators [path];
+               }
+               public static ItemTemplate GetItemTemplate(string path){
+                       if (!Instantiators.ContainsKey(path))
+                               Instantiators [path] = new ItemTemplate(path);
+                       return Instantiators [path] as ItemTemplate;
                }
-
                public GraphicObject LoadInterface (string path)
                {
                        lock (UpdateMutex) {
diff --git a/src/ItemTemplate.cs b/src/ItemTemplate.cs
new file mode 100644 (file)
index 0000000..ebc7b28
--- /dev/null
@@ -0,0 +1,97 @@
+//
+//  IMLStream.cs
+//
+//  Author:
+//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
+//
+//  Copyright (c) 2016 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.IO;
+using System.Reflection;
+using System.Reflection.Emit;
+using System.Threading;
+using System.Xml.Serialization;
+using System.Xml;
+using System.Collections.Generic;
+using System.Diagnostics;
+
+namespace Crow
+{
+       public class ItemTemplate : Instantiator {              
+               public EventHandler Expand;
+
+               #region CTOR
+               public ItemTemplate(string path) 
+                       : base(path) {
+               }
+               public ItemTemplate (Type _root, Interface.LoaderInvoker _loader)
+                       :base(_root, _loader)
+               {
+               }
+               #endregion
+
+               public void CreateExpandDelegate (TemplatedControl host, string strDataType, string method){
+                       Type dataType = Type.GetType(strDataType);
+                       Type hostType = typeof(TemplatedControl);//not sure is the best place to put the dyn method
+                       Type evtType = typeof(EventHandler);
+                       Type listBoxType = typeof(ListBox);
+
+                       MethodInfo evtInvoke = evtType.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,
+                               hostType);
+
+
+                       #region IL generation
+                       ILGenerator il = dm.GetILGenerator (256);
+                       il.DeclareLocal(typeof(GraphicObject));
+
+                       il.Emit (OpCodes.Ldarg_1);
+
+                       MethodInfo miFindByName = typeof(GraphicObject).GetMethod("FindByName");
+                       il.Emit(OpCodes.Ldstr, "List");
+                       il.Emit (OpCodes.Callvirt, miFindByName);
+                       il.Emit (OpCodes.Stloc_0);
+
+                       FieldInfo fiTemplates = typeof(TemplatedControl).GetField("ItemTemplates");
+                       il.Emit (OpCodes.Ldloc_0);
+                       il.Emit (OpCodes.Ldarg_0);
+                       il.Emit (OpCodes.Ldfld, fiTemplates);
+                       il.Emit (OpCodes.Stfld, fiTemplates);
+
+                       il.Emit (OpCodes.Ldloc_0);
+                       il.Emit (OpCodes.Ldarg_1);
+                       il.Emit (OpCodes.Callvirt, typeof(GraphicObject).GetProperty("DataSource").GetGetMethod ());
+
+                       MethodInfo miGetDatas = dataType.GetMethod (method, new Type[] {});
+                       il.Emit (OpCodes.Callvirt, miGetDatas);
+
+                       il.Emit (OpCodes.Callvirt, listBoxType.GetProperty("Data").GetSetMethod ());
+
+                       il.Emit (OpCodes.Ret);
+
+                       #endregion
+
+                       Expand = (EventHandler)dm.CreateDelegate (evtType, host);
+               }
+       }
+}
+