From: jpbruyere Date: Sun, 31 Jul 2016 10:02:14 +0000 (+0200) Subject: Style attribute in xml, add default value loader style key. X-Git-Tag: v0.4~24^2~1 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=a508dabb5edb41304143bc135ad769d63164d026;p=jp%2Fcrow.git Style attribute in xml, add default value loader style key. modifié : src/GraphicObjects/GraphicObject.cs modifié : src/Interface.cs --- diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index a24b5763..a053e700 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -45,17 +45,14 @@ namespace Crow uid = currentUid; currentUid++; - if (Interface.XmlSerializerInit) + if (Interface.CurrentInterface.XmlLoading) return; - - loadDefaultValues (); } public GraphicObject (Rectangle _bounds) { - if (Interface.XmlSerializerInit) - return; - - loadDefaultValues (); + if (!Interface.CurrentInterface.XmlLoading) + loadDefaultValues (); + Left = _bounds.Left; Top = _bounds.Top; Width = _bounds.Width; @@ -485,17 +482,6 @@ namespace Crow } #endregion - - /// - /// Add 'Styles' to full class name between assembly name and the rest of the full name space - /// - string getDefaultStyleResId (Type t) { - string [] tmp = t.FullName.Split ('.'); - string res = tmp [0] + ".Styles"; - for (int i = 1; i < tmp.Length; i++) - res += "." + tmp [i]; - return res; - } /// Loads the default values from XML attributes default protected virtual void loadDefaultValues() { @@ -504,10 +490,21 @@ namespace Crow #endif Type thisType = this.GetType (); + + if (!string.IsNullOrEmpty (Style)) { + if (Interface.DefaultValuesLoader.ContainsKey (Style)) { + Interface.DefaultValuesLoader [Style] (this); + return; + } + } if (Interface.DefaultValuesLoader.ContainsKey (thisType.FullName)) { Interface.DefaultValuesLoader [thisType.FullName] (this); return; } + if (Interface.DefaultValuesLoader.ContainsKey (thisType.Name)) { + Interface.DefaultValuesLoader [thisType.Name] (this); + return; + } List> styling = new List>(); @@ -516,15 +513,26 @@ namespace Crow //2: class name //3: style may have been registered with their ressource ID minus .style extention // those files being placed in a Styles folder - - if (Interface.CurrentInterface.Styling.ContainsKey (thisType.FullName)) + string styleKey = Style; + if (!string.IsNullOrEmpty (Style)) { + if (Interface.CurrentInterface.Styling.ContainsKey (Style)) { + styling.Add (Interface.CurrentInterface.Styling [Style]); + } + } + if (Interface.CurrentInterface.Styling.ContainsKey (thisType.FullName)) { styling.Add (Interface.CurrentInterface.Styling [thisType.FullName]); - if (Interface.CurrentInterface.Styling.ContainsKey (thisType.Name)) + if (string.IsNullOrEmpty (styleKey)) + styleKey = thisType.FullName; + } + if (Interface.CurrentInterface.Styling.ContainsKey (thisType.Name)) { styling.Add (Interface.CurrentInterface.Styling [thisType.Name]); - - string styleFullName = getDefaultStyleResId (thisType); - if (Interface.CurrentInterface.Styling.ContainsKey (styleFullName)) - styling.Add (Interface.CurrentInterface.Styling [styleFullName]); + if (string.IsNullOrEmpty (styleKey)) + styleKey = thisType.Name; + } + + if (string.IsNullOrEmpty (styleKey)) + styleKey = thisType.FullName; + //Reflexion being very slow compared to dyn method or delegates, //I compile the initial values coded in the CustomAttribs of the class, @@ -676,8 +684,8 @@ namespace Crow il.Emit(OpCodes.Ret); #endregion - Interface.DefaultValuesLoader[thisType.FullName] = (Interface.loadDefaultInvoker)dm.CreateDelegate(typeof(Interface.loadDefaultInvoker)); - Interface.DefaultValuesLoader[thisType.FullName] (this); + Interface.DefaultValuesLoader[styleKey] = (Interface.loadDefaultInvoker)dm.CreateDelegate(typeof(Interface.loadDefaultInvoker)); + Interface.DefaultValuesLoader[styleKey] (this); } public virtual GraphicObject FindByName(string nameToFind){ @@ -1283,13 +1291,21 @@ namespace Crow } public virtual void ReadXml (System.Xml.XmlReader reader) { - if (!reader.HasAttributes) - return; + if (reader.HasAttributes) { + + style = reader.GetAttribute ("Style"); - while (reader.MoveToNextAttribute ()) - affectMember (reader.Name, reader.Value); + loadDefaultValues (); - reader.MoveToElement(); + while (reader.MoveToNextAttribute ()) { + if (reader.Name == "Style") + continue; + + affectMember (reader.Name, reader.Value); + } + reader.MoveToElement (); + }else + loadDefaultValues (); } public virtual void WriteXml (System.Xml.XmlWriter writer) { diff --git a/src/Interface.cs b/src/Interface.cs index a83a5305..f67849a7 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -59,8 +59,7 @@ namespace Crow #endregion #region Static and constants - /// Used to prevent spurious loading of templates - internal static bool XmlSerializerInit = false; + internal bool XmlLoading = false; /// keep ressource path for debug msg internal static string CurrentGOMLPath = ""; //used in templatedControl @@ -216,7 +215,7 @@ namespace Crow return tmp; } - public static GraphicObject Load (Stream stream, Type type, object hostClass = null) + internal static GraphicObject Load (Stream stream, Type type, object hostClass = null) { #if DEBUG_LOAD Stopwatch loadingTime = new Stopwatch (); @@ -225,16 +224,16 @@ namespace Crow GraphicObject result; + CurrentInterface.XmlLoading = true; XmlSerializerNamespaces xn = new XmlSerializerNamespaces (); xn.Add ("", ""); - XmlSerializerInit = true; XmlSerializer xs = new XmlSerializer (type); - XmlSerializerInit = false; result = (GraphicObject)xs.Deserialize (stream); //result.DataSource = hostClass; + CurrentInterface.XmlLoading = false; #if DEBUG_LOAD FileStream fs = stream as FileStream; @@ -250,11 +249,6 @@ namespace Crow return result; } - public void InterfaceLoad(string path){ - GraphicObject tmp = Interface.Load (path, this); - AddWidget (tmp); - } - public GraphicObject LoadInterface (string path) { lock (UpdateMutex) {