]> O.S.I.I.S - jp/crow.git/commitdiff
instantiators dictionary no longer static but local to Interface
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Fri, 23 Feb 2018 11:51:16 +0000 (12:51 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Fri, 23 Feb 2018 11:51:16 +0000 (12:51 +0100)
CrowIDE/src/CrowIDE.cs
CrowIDE/src/ImlVisualEditor.cs
src/CompilerServices/CompilerServices.cs
src/GraphicObjects/Docker.cs
src/GraphicObjects/TemplatedControl.cs
src/GraphicObjects/TemplatedGroup.cs
src/Instantiator.cs
src/Interface.cs
src/ItemTemplate.cs

index 905468a9d3972d2265f53770ddd373df394fedd0..2e0f29012679ba2b84da489b217d11d1d126c8a0 100644 (file)
@@ -65,7 +65,7 @@ namespace CrowIDE
                }
 
                void openFileDialog () {                        
-                       AddWidget (instFileDlg.CreateInstance(CurrentInterface)).DataSource = this;
+                       AddWidget (instFileDlg.CreateInstance()).DataSource = this;
                }
                void openOptionsDialog(){}
                void newFile() {}
@@ -96,7 +96,7 @@ namespace CrowIDE
                        base.OnLoad (e);
 
                        instFileDlg = Instantiator.CreateFromImlFragment
-                               ("<FileDialog Caption='Open File' CurrentDirectory='{²CurrentDirectory}' SearchPattern='*.sln' OkClicked='onFileOpen'/>");
+                               (CurrentInterface, "<FileDialog Caption='Open File' CurrentDirectory='{²CurrentDirectory}' SearchPattern='*.sln' OkClicked='onFileOpen'/>");
 
                        initCommands ();
 
index fe116f7888143401dcaddf764ed65f200da4b888..cf799cc714690896ab0642929dfab84a78817beb 100644 (file)
@@ -144,7 +144,7 @@ namespace CrowIDE
 
                        Instantiator iTmp;
                        try {
-                               iTmp = Instantiator.CreateFromImlFragment (imlSource);
+                               iTmp = Instantiator.CreateFromImlFragment (imlVE, imlSource);
                        } catch (Exception ex) {
                                System.Diagnostics.Debug.WriteLine (ex.ToString());
                                return;
@@ -172,7 +172,7 @@ namespace CrowIDE
                                try {
                                        imlVE.ClearInterface ();
                                        if (itor != null)
-                                               imlVE.AddWidget(itor.CreateInstance(imlVE));
+                                               imlVE.AddWidget(itor.CreateInstance());
 
                                } catch (Exception ex) {
                                        System.Diagnostics.Debug.WriteLine (ex.ToString());
index 1e7fab8e4628bb05b65688fbe369e7ee206ea9a9..eb8f2ecfcfd1694e212f3574075b51ca83100247 100644 (file)
@@ -87,7 +87,7 @@ namespace Crow.IML
                internal static EventInfo eiLogicalParentChanged = typeof(GraphicObject).GetEvent("LogicalParentChanged");
 
                internal static MethodInfo miIFaceLoad = typeof(Interface).GetMethod ("Load", BindingFlags.Instance | BindingFlags.Public);
-               internal static MethodInfo miGetITemp = typeof(Interface).GetMethod ("GetItemTemplate");
+               internal static MethodInfo miGetITemp = typeof(Interface).GetMethod ("GetItemTemplate", BindingFlags.Instance | BindingFlags.Public);
 
                internal static MethodInfo miAddITemp = typeof(Dictionary<string, ItemTemplate>).GetMethod ("set_Item", new Type[] { typeof(string), typeof(ItemTemplate) });
                internal static MethodInfo miGetITempFromDic = typeof(Dictionary<string, ItemTemplate>).GetMethod ("get_Item", new Type[] { typeof(string) });
index c52c094f22603ed6362f090cd7f9e3b2cdc5cd6f..d34e5ba7c247214655a22de03f73325c0f2d43c4 100644 (file)
@@ -35,12 +35,12 @@ namespace Crow
                static Instantiator instStack, instSplit, instSpacer;
                #region CTOR
                static Docker () {
-                       instStack = Instantiator.CreateFromImlFragment(@"<GenericStack Background='Blue' AllowDrop='true' DragEnter='onStackDragEnter'/>");
-                       instSplit = Instantiator.CreateFromImlFragment(@"<Splitter/>");
-                       instSpacer = Instantiator.CreateFromImlFragment(@"<GraphicObject Background='Red' IsEnabled='false'/>");
                }
                public Docker () : base ()
                {
+                       instStack = Instantiator.CreateFromImlFragment(CurrentInterface, @"<GenericStack Background='Blue' AllowDrop='true' DragEnter='onStackDragEnter'/>");
+                       instSplit = Instantiator.CreateFromImlFragment(CurrentInterface, @"<Splitter/>");
+                       instSpacer = Instantiator.CreateFromImlFragment(CurrentInterface, @"<GraphicObject Background='Red' IsEnabled='false'/>");
                }
                #endregion
 
@@ -196,7 +196,7 @@ namespace Crow
                                return;
                        lock (CurrentInterface.UpdateMutex) {
 
-                               Splitter splitter = instSplit.CreateInstance<Splitter> (CurrentInterface);
+                               Splitter splitter = instSplit.CreateInstance<Splitter> ();
 
                                dw.Resizable = false;
                                dw.Left = dw.Top = 0;
@@ -204,7 +204,7 @@ namespace Crow
 
                                Rectangle r;
                                if (mainStack == null) {
-                                       mainStack = instStack.CreateInstance<GenericStack> (CurrentInterface);
+                                       mainStack = instStack.CreateInstance<GenericStack> ();
                                        this.AddChild (mainStack);
                                        this.putWidgetOnBottom (mainStack);
                                        r = ClientRectangle;
index 34e4ce07d3cd40b7a60e4a84c0951808e05c00ee..e0e77fc0d7c0217f6b4c428b561be564e048b4ad 100644 (file)
@@ -133,9 +133,9 @@ namespace Crow
                                this.ClearTemplateBinding();
                        
                        if (template == null) {
-                               if (!Interface.DefaultTemplates.ContainsKey (this.GetType ().FullName))
+                               if (!CurrentInterface.DefaultTemplates.ContainsKey (this.GetType ().FullName))
                                        throw new Exception (string.Format ("No default template found for '{0}'", this.GetType ().FullName));
-                               this.SetChild (CurrentInterface.Load (Interface.DefaultTemplates[this.GetType ().FullName]));
+                               this.SetChild (CurrentInterface.Load (CurrentInterface.DefaultTemplates[this.GetType ().FullName]));
                        }else
                                this.SetChild (template);
                }
index a6f1b1514269f009b1d2fa130602eed8f7a9ad4c..cf378dbd8fc6cdd91aae6314f6b8721255622beb 100644 (file)
@@ -428,7 +428,7 @@ namespace Crow
                        }
 
                        lock (CurrentInterface.LayoutMutex) {
-                               g = iTemp.CreateInstance(CurrentInterface);
+                               g = iTemp.CreateInstance();
                                page.AddChild (g);
 //                             if (isPaged)
                                g.LogicalParent = this;
index 14e33bf093451a68f7779ff67825e2135a65b340..2481206abdf5bcd22759daee60d1c4594d688170 100644 (file)
@@ -70,6 +70,7 @@ namespace Crow.IML
 
                public Type RootType;
                InstanciatorInvoker loader;
+               protected Interface iface;
 
                internal string sourcePath;
 
@@ -77,14 +78,15 @@ namespace Crow.IML
                /// <summary>
                /// Initializes a new instance of the Instantiator class.
                /// </summary>
-               public Instantiator (string path) : this (Interface.GetStreamFromPath(path), path) {
+               public Instantiator (Interface _iface, string path) : this (_iface, Interface.GetStreamFromPath(path), path) {
                        
                }
                /// <summary>
                /// Initializes a new instance of the Instantiator class.
                /// </summary>
-               public Instantiator (Stream stream, string srcPath = null)
+               public Instantiator (Interface _iface, Stream stream, string srcPath = null)
                {
+                       iface = _iface;
                        sourcePath = srcPath;
                        #if DEBUG_LOAD
                        Stopwatch loadingTime = new Stopwatch ();
@@ -109,12 +111,14 @@ namespace Crow.IML
                /// Initializes a new instance of the Instantiator class with an already openned xml reader
                /// positionned on the start tag inside the itemTemplate
                /// </summary>
-               public Instantiator (XmlReader itr){
+               public Instantiator (Interface _iface, XmlReader itr){
+                       iface = _iface;
                        parseIML (itr);
                }
                //TODO:check if still used
-               public Instantiator (Type _root, InstanciatorInvoker _loader)
+               public Instantiator (Interface _iface, Type _root, InstanciatorInvoker _loader)
                {
+                       iface = _iface;
                        RootType = _root;
                        loader = _loader;
                }
@@ -123,11 +127,11 @@ namespace Crow.IML
                /// </summary>
                /// <returns>A new instantiator</returns>
                /// <param name="fragment">IML string</param>
-               public static Instantiator CreateFromImlFragment (string fragment)
+               public static Instantiator CreateFromImlFragment (Interface _iface, string fragment)
                {
                        try {
                                using (Stream s = new MemoryStream (Encoding.UTF8.GetBytes (fragment))) {
-                                       return new Instantiator (s);
+                                       return new Instantiator (_iface, s);
                                }
                        } catch (Exception ex) {
                                throw new Exception ("Error loading IML fragment:\n" + fragment + "\n", ex);
@@ -141,7 +145,7 @@ namespace Crow.IML
                /// </summary>
                /// <returns>The new graphic object instance</returns>
                /// <param name="iface">The interface to bind to</param>
-               public GraphicObject CreateInstance(Interface iface){
+               public GraphicObject CreateInstance(){
                        return loader (iface) as GraphicObject;
                }
                /// <summary>
@@ -150,7 +154,7 @@ namespace Crow.IML
                /// </summary>
                /// <returns>The new T instance</returns>
                /// <param name="iface">The interface to bind to</param>
-               public T CreateInstance<T>(Interface iface){
+               public T CreateInstance<T>(){
                        return (T)loader (iface);
                }
                List<DynamicMethod> dsValueChangedDynMeths = new List<DynamicMethod>();
@@ -242,16 +246,16 @@ namespace Crow.IML
 
                        if (string.IsNullOrEmpty (path)) {
                                itemTmpID += Guid.NewGuid ().ToString ();
-                               Interface.Instantiators [itemTmpID] =
-                                       new ItemTemplate (new MemoryStream (Encoding.UTF8.GetBytes (reader.ReadInnerXml ())), dataTest, dataType, datas);
+                               iface.Instantiators [itemTmpID] =
+                                       new ItemTemplate (iface, new MemoryStream (Encoding.UTF8.GetBytes (reader.ReadInnerXml ())), dataTest, dataType, datas);
 
                        } else {
                                if (!reader.IsEmptyElement)
                                        throw new Exception ("ItemTemplate with Path attribute set may not include sub nodes");
                                itemTmpID += path+dataType+datas;
-                               if (!Interface.Instantiators.ContainsKey (itemTmpID))
-                                       Interface.Instantiators [itemTmpID] =
-                                               new ItemTemplate (Interface.GetStreamFromPath (path), dataTest, dataType, datas);
+                               if (!iface.Instantiators.ContainsKey (itemTmpID))
+                                       iface.Instantiators [itemTmpID] =
+                                               new ItemTemplate (iface, Interface.GetStreamFromPath (path), dataTest, dataType, datas);
                        }
                        return new string [] { dataType, itemTmpID, datas, dataTest };
                }
@@ -299,7 +303,7 @@ namespace Crow.IML
                                        //try to load ItemTemplate(s) from ItemTemplate attribute of TemplatedGroup
                                        if (!string.IsNullOrEmpty (itemTemplatePath)) {
                                                //check if it is already loaded in cache as a single itemTemplate instantiator
-                                               if (Interface.Instantiators.ContainsKey (itemTemplatePath)) {
+                                               if (iface.Instantiators.ContainsKey (itemTemplatePath)) {
                                                        itemTemplateIds.Add (new string [] { "default", itemTemplatePath, "" });
                                                } else {
                                                        using (Stream stream = Interface.GetStreamFromPath (itemTemplatePath)) {
@@ -312,8 +316,8 @@ namespace Crow.IML
                                                                                if (itr.NodeType == XmlNodeType.Element) {
                                                                                        if (itr.Name != "ItemTemplate") {
                                                                                                //the file contains a single template to use as default
-                                                                                               Interface.Instantiators [itemTemplatePath] =
-                                                                                                       new ItemTemplate (itr);
+                                                                                               iface.Instantiators [itemTemplatePath] =
+                                                                                                       new ItemTemplate (iface, itr);
                                                                                                itemTemplateIds.Add (new string [] { "default", itemTemplatePath, "", "TypeOf" });
                                                                                                break;//we should be at the end of the file
                                                                                        }
@@ -334,8 +338,12 @@ namespace Crow.IML
                                foreach (string [] iTempId in itemTemplateIds) {
                                        ctx.il.Emit (OpCodes.Ldloc_0);//load TempControl ref
                                        ctx.il.Emit (OpCodes.Ldfld, CompilerServices.fldItemTemplates);//load ItemTemplates dic field
+
+                                       //prepare argument to add itemTemplate to templated group dic of ItemTemplates
                                        ctx.il.Emit (OpCodes.Ldstr, iTempId [0]);//load key
-                                       ctx.il.Emit (OpCodes.Ldstr, iTempId [1]);//load value
+                                       //load itemTemplate
+                                       ctx.il.Emit (OpCodes.Ldarg_1);//load currentInterface
+                                       ctx.il.Emit (OpCodes.Ldstr, iTempId [1]);//load path
                                        ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miGetITemp);
                                        ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miAddITemp);
 
index 3d2389b2db00604755baff0761c6b1e01be3ddd7..ee95eeb54047e65073bf8dce95f78f95f7f17846 100644 (file)
@@ -89,7 +89,6 @@ namespace Crow
                        }
 
                        loadCursors ();
-                       findAvailableTemplates ();
 
                        FontRenderingOptions = new FontOptions ();
                        FontRenderingOptions.Antialias = Antialias.Subpixel;
@@ -101,6 +100,7 @@ namespace Crow
                        CurrentInterface = this;
                        CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
                        loadStyling ();
+                       findAvailableTemplates ();
                        initTooltip ();
                        initContextMenus ();
                }
@@ -210,7 +210,7 @@ namespace Crow
                /// <summary>each IML and fragments (such as inline Templates) are compiled as a Dynamic Method stored here
                /// on the first instance creation of a IML item.
                /// </summary>
-               public static Dictionary<String, Instantiator> Instantiators = new Dictionary<string, Instantiator>();
+               public Dictionary<String, Instantiator> Instantiators = new Dictionary<string, Instantiator>();
                public List<CrowThread> CrowThreads = new List<CrowThread>();//used to monitor thread finished
 
                public DragDropEventArgs DragAndDropOperation = null;
@@ -278,16 +278,16 @@ namespace Crow
                /// Resource ID must be 'fullClassName.template' (not case sensitive)
                /// Those found in application assembly have priority to the default Crow's one
                /// </summary>
-               public static Dictionary<string, string> DefaultTemplates = new Dictionary<string, string>();
+               public Dictionary<string, string> DefaultTemplates = new Dictionary<string, string>();
                /// <summary>Finds available default templates at startup</summary>
-               static void findAvailableTemplates(){
+               void findAvailableTemplates(){
                        searchTemplatesOnDisk ("./");
                        string defTemplatePath = System.IO.Path.Combine (CrowConfigRoot, "defaultTemplates");
                        searchTemplatesOnDisk (defTemplatePath);
                        searchTemplatesIn (Assembly.GetEntryAssembly ());
                        searchTemplatesIn (Assembly.GetExecutingAssembly ());
                }
-               static void searchTemplatesOnDisk (string templatePath){
+               void searchTemplatesOnDisk (string templatePath){
                        if (!Directory.Exists (templatePath))
                                return;
                        foreach (string f in Directory.GetFiles(templatePath, "*.template",SearchOption.AllDirectories)) {
@@ -297,7 +297,7 @@ namespace Crow
                                DefaultTemplates [clsName] = f;
                        }
                }
-               static void searchTemplatesIn(Assembly assembly){
+               void searchTemplatesIn(Assembly assembly){
                        if (assembly == null)
                                return;
                        foreach (string resId in assembly
@@ -344,7 +344,7 @@ namespace Crow
                /// <param name="imlFragment">a valid IML string</param>
                public GraphicObject LoadIMLFragment (string imlFragment) {
                        lock (UpdateMutex) {
-                               GraphicObject tmp = Instantiator.CreateFromImlFragment (imlFragment).CreateInstance(this);
+                               GraphicObject tmp = Instantiator.CreateFromImlFragment (this, imlFragment).CreateInstance();
                                AddWidget (tmp);
                                return tmp;
                        }
@@ -370,7 +370,7 @@ namespace Crow
                public GraphicObject Load (string path)
                {
                        //try {
-                               return GetInstantiator (path).CreateInstance (this);
+                               return GetInstantiator (path).CreateInstance ();
                        //} catch (Exception ex) {
                        //      throw new Exception ("Error loading <" + path + ">:", ex);
                        //}
@@ -380,22 +380,22 @@ namespace Crow
                /// </summary>
                /// <returns>new Instantiator</returns>
                /// <param name="path">path of the iml file to load</param>
-               public static Instantiator GetInstantiator(string path){
+               public Instantiator GetInstantiator(string path){
                        if (!Instantiators.ContainsKey(path))
-                               Instantiators [path] = new Instantiator(path);
+                               Instantiators [path] = new Instantiator(this, path);
                        return Instantiators [path];
                }
                /// <summary>Item templates are derived from instantiator, this function
                /// try to fetch the requested one in the cache or create it.
                /// They have additional properties for recursivity and
                /// custom display per item type</summary>
-               public static ItemTemplate GetItemTemplate(string path){
+               public ItemTemplate GetItemTemplate(string path){
                        if (!Instantiators.ContainsKey(path))
-                               Instantiators [path] = new ItemTemplate(path);
+                               Instantiators [path] = new ItemTemplate(this, path);
                        return Instantiators [path] as ItemTemplate;
                }
                //TODO: .Net xml serialisation is no longer used, it has been replaced with instantiators
-               public static void Save<T> (string file, T graphicObject)
+               public void Save<T> (string file, T graphicObject)
                {
                        XmlSerializerNamespaces xn = new XmlSerializerNamespaces ();
                        xn.Add ("", "");
index 5ede1d9455b6cfd58104522bd8dc45e6cc43a93b..7eb9d752b2a9e6798cd2481910922baa3d95a698 100644 (file)
@@ -61,8 +61,8 @@ namespace Crow
                /// <param name="path">IML file to parse</param>
                /// <param name="_dataType">type this item will be choosen for, or member of the data item</param>
                /// <param name="_fetchDataMethod">for hierarchical data, method to call for children fetching</param>
-               public ItemTemplate(string path, string _dataTest = "TypeOf", string _dataType = null, string _fetchDataMethod = null)
-                       : base(path) {
+               public ItemTemplate(Interface _iface, string path, string _dataTest = "TypeOf", string _dataType = null, string _fetchDataMethod = null)
+                       : base(_iface, path) {
                        strDataType = _dataType;
                        fetchMethodName = _fetchDataMethod;
                        dataTest = _dataTest;
@@ -74,8 +74,8 @@ namespace Crow
                /// <param name="path">IML fragment to parse</param>
                /// <param name="_dataType">type this item will be choosen for, or member of the data item</param>
                /// <param name="_fetchDataMethod">for hierarchical data, method to call for children fetching</param>
-               public ItemTemplate (Stream ImlFragment, string _dataTest, string _dataType, string _fetchDataMethod)
-                       :base(ImlFragment)
+               public ItemTemplate (Interface _iface, Stream ImlFragment, string _dataTest, string _dataType, string _fetchDataMethod)
+                       :base(_iface, ImlFragment)
                {
                        strDataType = _dataType;
                        fetchMethodName = _fetchDataMethod;
@@ -87,8 +87,8 @@ namespace Crow
                /// <param name="path">XML reader positionned before or at the root node</param>
                /// <param name="_dataType">type this item will be choosen for, or member of the data item</param>
                /// <param name="_fetchDataMethod">for hierarchical data, method to call for children fetching</param>
-               public ItemTemplate (XmlReader reader, string _dataTest = "TypeOf" , string _dataType = null, string _fetchDataMethod = null)
-                       :base(reader)
+               public ItemTemplate (Interface _iface, XmlReader reader, string _dataTest = "TypeOf" , string _dataType = null, string _fetchDataMethod = null)
+                       :base(_iface, reader)
                {
                        strDataType = _dataType;
                        fetchMethodName = _fetchDataMethod;