From: Jean-Philippe Bruyère Date: Sun, 26 Jan 2020 03:22:20 +0000 (+0100) Subject: improve style and templates loading sync, prevent 2x loading in some case X-Git-Tag: v0.9.5-beta~137^2~2 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=bf352abe61774c03840a65c6050c599c5be41cbe;p=jp%2Fcrow.git improve style and templates loading sync, prevent 2x loading in some case --- diff --git a/Crow/src/CompilerServices/CompilerServices.cs b/Crow/src/CompilerServices/CompilerServices.cs index 6637185c..86e7a7a9 100644 --- a/Crow/src/CompilerServices/CompilerServices.cs +++ b/Crow/src/CompilerServices/CompilerServices.cs @@ -1,28 +1,6 @@ -// -// CompilerServices.cs +// Copyright (c) 2013-2020 Jean-Philippe Bruyère // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.Reflection.Emit; diff --git a/Crow/src/Fill/Fill.cs b/Crow/src/Fill/Fill.cs index 7d521045..719002d2 100644 --- a/Crow/src/Fill/Fill.cs +++ b/Crow/src/Fill/Fill.cs @@ -1,31 +1,8 @@ -// -// Fill.cs +// Copyright (c) 2013-2020 Jean-Philippe Bruyère // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; -using System.Collections.Generic; using Crow.Cairo; namespace Crow @@ -45,16 +22,15 @@ namespace Crow if (string.IsNullOrEmpty (s)) return null; if (s.Substring (1).StartsWith ("gradient", StringComparison.Ordinal)) - return (Gradient)Gradient.Parse (s); - if (s.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) - return Parse (s); - if (s.EndsWith (".png", true, System.Globalization.CultureInfo.InvariantCulture) || + return (Gradient)Gradient.Parse (s); + if (s.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture) || + s.EndsWith (".png", true, System.Globalization.CultureInfo.InvariantCulture) || s.EndsWith (".jpg", true, System.Globalization.CultureInfo.InvariantCulture) || - s.EndsWith (".jpeg", true, System.Globalization.CultureInfo.InvariantCulture) || + s.EndsWith (".jpeg", true, System.Globalization.CultureInfo.InvariantCulture)|| s.EndsWith (".bmp", true, System.Globalization.CultureInfo.InvariantCulture) || s.EndsWith (".gif", true, System.Globalization.CultureInfo.InvariantCulture)) - return Parse (s); - + return Picture.Parse (s); + return (SolidColor)SolidColor.Parse (s); } public static implicit operator Color(Fill c){ diff --git a/Crow/src/IML/IMLContext.cs b/Crow/src/IML/IMLContext.cs index 6773de42..3aab2158 100644 --- a/Crow/src/IML/IMLContext.cs +++ b/Crow/src/IML/IMLContext.cs @@ -1,28 +1,6 @@ -// -// Context.cs +// Copyright (c) 2013-2020 Jean-Philippe Bruyère // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.Collections.Generic; diff --git a/Crow/src/Instantiator.cs b/Crow/src/Instantiator.cs index 1952006c..20ca1ff8 100644 --- a/Crow/src/Instantiator.cs +++ b/Crow/src/Instantiator.cs @@ -306,19 +306,19 @@ namespace Crow.IML { } if (!inlineTemplate) {//load from path or default template - ctx.il.Emit (OpCodes.Ldloc_0);//Load current templatedControl ref - if (string.IsNullOrEmpty (templatePath)) { - ctx.il.Emit (OpCodes.Ldnull);//default template loading - } else { + + if (!string.IsNullOrEmpty (templatePath)) { + ctx.il.Emit (OpCodes.Ldloc_0);//Load current templatedControl ref + // ctx.il.Emit (OpCodes.Ldnull);//default template loading + //} else { ctx.il.Emit (OpCodes.Ldarg_1);//load currentInterface ctx.il.Emit (OpCodes.Ldstr, templatePath); //Load template path string //get declaring type for search fallback assembly ctx.il.Emit (OpCodes.Ldloc_0); ctx.il.Emit (OpCodes.Call, CompilerServices.miGetType); - ctx.il.Emit (OpCodes.Callvirt, - CompilerServices.miIFaceCreateTemplateInst); + ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miIFaceCreateTemplateInst); + ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miLoadTmp);//load template } - ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miLoadTmp);//load template } if (itemTemplateIds.Count == 0) { //try to load ItemTemplate(s) from ItemTemplate attribute of TemplatedGroup @@ -454,7 +454,7 @@ namespace Crow.IML { if (reader.HasAttributes) { while (reader.MoveToNextAttribute ()) { - if (reader.Name == "Style" || reader.Name == "DataSourceType") + if (reader.Name == "Style" || reader.Name == "DataSourceType" || reader.Name == "Template") continue; #if DESIGN_MODE diff --git a/Crow/src/Style.cs b/Crow/src/Style.cs index 5ca434a0..ded06b60 100644 --- a/Crow/src/Style.cs +++ b/Crow/src/Style.cs @@ -1,28 +1,6 @@ -// -// Style.cs +// Copyright (c) 2013-2020 Jean-Philippe Bruyère // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.Collections.Generic; diff --git a/Crow/src/Widgets/EnumSelector.cs b/Crow/src/Widgets/EnumSelector.cs index 70985f2a..fa43e3b3 100644 --- a/Crow/src/Widgets/EnumSelector.cs +++ b/Crow/src/Widgets/EnumSelector.cs @@ -31,10 +31,33 @@ namespace Crow #region private fields Enum enumValue; Type enumType; + string rbStyle; + IML.Instantiator radioButtonITor; #endregion #region public properties /// + /// Enum values are presented with RadioButton controls. Here you may specify a template to use + /// for the radio buttons. + /// + [DefaultValue (null)] + public string RadioButtonStyle { + get { return rbStyle; } + set { + if (rbStyle == value) + return; + rbStyle = value; + radioButtonITor = null; + //force refresh to use new template if values are already displayed + if (enumValue != null) { + Enum tmp = enumValue; + enumValue = null; + EnumValue = tmp; + } + NotifyValueChanged ("RadioButtonStyle", rbStyle); + } + } + /// /// use to define the colors of the 3d border /// [DefaultValue(null)] @@ -45,13 +68,16 @@ namespace Crow return; enumValue = value; + if (radioButtonITor == null) + radioButtonITor = IFace.CreateITorFromIMLFragment ($""); if (enumValue != null) { if (enumType != enumValue.GetType ()) { enumValueContainer.ClearChildren (); enumType = enumValue.GetType (); foreach (string en in enumType.GetEnumNames ()) { - RadioButton rb = new RadioButton (IFace); + + RadioButton rb = radioButtonITor.CreateInstance (); rb.Caption = en; rb.LogicalParent = this; if (enumValue.ToString () == en) diff --git a/Crow/src/Widgets/Group.cs b/Crow/src/Widgets/Group.cs index a992e5af..3e399734 100644 --- a/Crow/src/Widgets/Group.cs +++ b/Crow/src/Widgets/Group.cs @@ -1,36 +1,11 @@ -// -// Group.cs +// Copyright (c) 2013-2020 Jean-Philippe Bruyère // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.Collections.Generic; using System.ComponentModel; -using System.Xml.Serialization; using Crow.Cairo; -using System.Diagnostics; -using System.Reflection; using System.Threading; diff --git a/Crow/src/Widgets/HorizontalStack.cs b/Crow/src/Widgets/HorizontalStack.cs index 64d7cae5..a6b0fd56 100644 --- a/Crow/src/Widgets/HorizontalStack.cs +++ b/Crow/src/Widgets/HorizontalStack.cs @@ -1,41 +1,14 @@ -// -// HorizontalStack.cs +// Copyright (c) 2013-2020 Jean-Philippe Bruyère // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Xml.Serialization; namespace Crow { /// /// group control stacking its children horizontally /// - public class HorizontalStack : GenericStack + public class HorizontalStack : GenericStack { #region CTOR protected HorizontalStack () : base(){} diff --git a/Crow/src/Widgets/RadioButton.cs b/Crow/src/Widgets/RadioButton.cs index d18971a2..ef2c6e93 100644 --- a/Crow/src/Widgets/RadioButton.cs +++ b/Crow/src/Widgets/RadioButton.cs @@ -1,42 +1,23 @@ -// -// RadioButton.cs +// Copyright (c) 2013-2020 Jean-Philippe Bruyère // -// Author: -// Jean-Philippe Bruyère -// -// Copyright (c) 2013-2017 Jean-Philippe Bruyère -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.ComponentModel; -using System.Xml.Serialization; namespace Crow { - public class RadioButton : TemplatedControl - { + /// + /// Bistate templated control. Several occurences in one Group constrol will allow + /// only one checked RadioButton, other are automatically disabled. + /// + public class RadioButton : TemplatedControl + { bool isChecked; #region CTOR - protected RadioButton () : base(){} - public RadioButton(Interface iface) : base(iface){} + protected RadioButton () : base () { } + public RadioButton (Interface iface) : base (iface) { } #endregion public event EventHandler Checked; @@ -44,7 +25,7 @@ namespace Crow #region GraphicObject overrides public override void onMouseDown (object sender, MouseButtonEventArgs e) - { + { Group pg = Parent as Group; if (pg != null) { for (int i = 0; i < pg.Children.Count; i++) { @@ -60,15 +41,13 @@ namespace Crow } #endregion - [DefaultValue(false)] - public bool IsChecked - { + [DefaultValue (false)] + public bool IsChecked { get { return isChecked; } - set - { + set { if (isChecked == value) return; - + isChecked = value; NotifyValueChanged ("IsChecked", value); @@ -77,7 +56,7 @@ namespace Crow Checked.Raise (this, null); else Unchecked.Raise (this, null); - } - } + } + } } } diff --git a/Crow/src/Widgets/TemplatedControl.cs b/Crow/src/Widgets/TemplatedControl.cs index 563fc0a6..962337de 100644 --- a/Crow/src/Widgets/TemplatedControl.cs +++ b/Crow/src/Widgets/TemplatedControl.cs @@ -36,7 +36,7 @@ namespace Crow public TemplatedControl (Interface iface) : base(iface){} #endregion - string _template; + string _template = "NOT_SET"; string caption; /// @@ -72,12 +72,6 @@ namespace Crow } #region GraphicObject overrides - - public override void Initialize () - { - loadTemplate (); - base.Initialize (); - } /// /// override search method from GraphicObject to prevent /// searching inside template diff --git a/Crow/src/Widgets/Widget.cs b/Crow/src/Widgets/Widget.cs index 4fe87abe..fee189b7 100644 --- a/Crow/src/Widgets/Widget.cs +++ b/Crow/src/Widgets/Widget.cs @@ -247,7 +247,7 @@ namespace Crow /// /// Initialize this Graphic object instance by setting style and default values and loading template if required /// - public virtual void Initialize(){ + public void Initialize(){ loadDefaultValues (); } #region private fields @@ -1134,56 +1134,24 @@ namespace Crow } } - foreach (PropertyInfo pi in thisType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { - if (pi.GetSetMethod () == null) - continue; - XmlIgnoreAttribute xia = (XmlIgnoreAttribute)pi.GetCustomAttribute (typeof(XmlIgnoreAttribute)); - if (xia != null) - continue; - - object defaultValue; - - int styleIndex = -1; - if (styling.Count > 0){ - for (int i = 0; i < styling.Count; i++) { - if (styling[i].ContainsKey (pi.Name)){ - styleIndex = i; - break; - } - } - } - if (styleIndex >= 0){ - if (pi.PropertyType.IsEnum)//maybe should be in parser.. - defaultValue = Enum.Parse(pi.PropertyType, (string)styling[styleIndex] [pi.Name], true); - else - defaultValue = styling[styleIndex] [pi.Name]; - - #if DESIGN_MODE - if (defaultValue != null){ - FileLocation fl = styling[styleIndex].Locations[pi.Name]; - il.Emit (OpCodes.Ldloc_0); - il.Emit (OpCodes.Ldstr, pi.Name); - il.Emit (OpCodes.Ldstr, fl.FilePath); - il.Emit (OpCodes.Ldc_I4, fl.Line); - il.Emit (OpCodes.Ldc_I4, fl.Column); - il.Emit (OpCodes.Call, miDesignAddDefLoc); + //first set template if it exists + PropertyInfo piTmp = thisType.GetProperty ("Template"); + if (piTmp != null) { + //if template has been declared in IML, cancel style or default loading + System.Reflection.Emit.Label cancelTemplateLoad = il.DefineLabel (); + il.Emit (OpCodes.Ldloc_0);//load target widget + il.Emit (OpCodes.Ldfld, typeof (PrivateContainer).GetField ("child", BindingFlags.Instance | BindingFlags.NonPublic)); + il.Emit (OpCodes.Brtrue, cancelTemplateLoad); - il.Emit (OpCodes.Ldloc_0); - il.Emit (OpCodes.Ldfld, typeof(Widget).GetField("design_style_values")); - il.Emit (OpCodes.Ldstr, pi.Name); - il.Emit (OpCodes.Ldstr, defaultValue.ToString()); - il.Emit (OpCodes.Call, CompilerServices.miDicStrStrAdd); - } - #endif + setDefaultValue (il, piTmp, ref styling); - }else { - DefaultValueAttribute dv = (DefaultValueAttribute)pi.GetCustomAttribute (typeof (DefaultValueAttribute)); - if (dv == null) - continue; - defaultValue = dv.Value; - } + il.MarkLabel (cancelTemplateLoad); + } - CompilerServices.EmitSetValue (il, pi, defaultValue); + foreach (PropertyInfo pi in thisType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { + if (pi.Name == "Template") + continue; + setDefaultValue (il, pi, ref styling); } il.Emit(OpCodes.Ret); #endregion @@ -1201,6 +1169,59 @@ namespace Crow onInitialized (this, null); } + void setDefaultValue (ILGenerator il, PropertyInfo pi, ref List