diff options
| author | jpbruyere <jp.bruyere@hotmail.com> | 2016-04-14 09:36:03 +0200 |
|---|---|---|
| committer | jpbruyere <jp.bruyere@hotmail.com> | 2016-04-14 12:47:15 +0200 |
| commit | 7ff5f14fe6b875dff57b335c4eeeb42397a07c91 (patch) | |
| tree | 23f80be40c98b4d142894a607c8dd2f43112b523 /src | |
| parent | 0b005fefe69b67bf335c3c00944bbac73e8b4831 (diff) | |
proportional ui
Diffstat (limited to 'src')
| -rw-r--r-- | src/CompilerServices/CompilerServices.cs | 20 | ||||
| -rw-r--r-- | src/GraphicObjects/GenericStack.cs | 20 | ||||
| -rw-r--r-- | src/GraphicObjects/GraphicObject.cs | 47 | ||||
| -rw-r--r-- | src/GraphicObjects/Group.cs | 12 | ||||
| -rw-r--r-- | src/GraphicObjects/Label.cs | 7 | ||||
| -rw-r--r-- | src/GraphicObjects/PrivateContainer.cs | 8 | ||||
| -rw-r--r-- | src/GraphicObjects/TextRun.cs | 17 | ||||
| -rw-r--r-- | src/Measure.cs | 101 |
8 files changed, 165 insertions, 67 deletions
diff --git a/src/CompilerServices/CompilerServices.cs b/src/CompilerServices/CompilerServices.cs index c1d3db6d..ea46d439 100644 --- a/src/CompilerServices/CompilerServices.cs +++ b/src/CompilerServices/CompilerServices.cs @@ -391,15 +391,15 @@ namespace Crow il.MarkLabel (jumpTable [i]); il.Emit(OpCodes.Ldloc_1); - //by default, target value type is deducted from source member type to allow + //by default, source value type is deducted from target member type to allow //memberless binding, if targetMember exists, it will be used to determine target //value type for conversion - Type targetValueType = b.Target.Property.PropertyType; + Type sourceValueType = b.Target.Property.PropertyType; if (b.Source.Member != null) { if (b.Source.Member.MemberType == MemberTypes.Property) - targetValueType = b.Source.Property.PropertyType; + sourceValueType = b.Source.Property.PropertyType; else if (b.Source.Member.MemberType == MemberTypes.Field) - targetValueType = b.Source.Field.FieldType; + sourceValueType = b.Source.Field.FieldType; else throw new Exception ("unhandle target member type in binding"); } @@ -409,11 +409,11 @@ namespace Crow if (!tostring.FindMember ("ToString")) throw new Exception ("ToString method not found"); il.Emit (OpCodes.Callvirt, tostring.Method); - }else if (!targetValueType.IsValueType) - il.Emit(OpCodes.Castclass, targetValueType); - else if (b.Target.Property.PropertyType != targetValueType) - il.Emit(OpCodes.Callvirt, CompilerServices.GetConvertMethod( b.Target.Property.PropertyType )); - else + } else if (!sourceValueType.IsValueType) + il.Emit (OpCodes.Castclass, sourceValueType); + else if (b.Target.Property.PropertyType != sourceValueType) { + il.Emit (OpCodes.Callvirt, CompilerServices.GetConvertMethod (b.Target.Property.PropertyType)); + }else il.Emit(OpCodes.Unbox_Any, b.Target.Property.PropertyType); il.Emit(OpCodes.Callvirt, b.Target.Property.GetSetMethod()); @@ -583,7 +583,7 @@ namespace Crow name = "ToSingle"; else if (targetType == typeof (string ) ) return typeof(object).GetMethod("ToString", Type.EmptyTypes); - else + else //try to find implicit convertion throw new NotImplementedException( string.Format( "Conversion to {0} is not implemented.", targetType.Name ) ); return typeof( Convert ).GetMethod( name, BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof( object ) }, null ); diff --git a/src/GraphicObjects/GenericStack.cs b/src/GraphicObjects/GenericStack.cs index 925f908a..455ed928 100644 --- a/src/GraphicObjects/GenericStack.cs +++ b/src/GraphicObjects/GenericStack.cs @@ -133,10 +133,10 @@ namespace Crow if (Children [i].RegisteredLayoutings.HasFlag (LayoutingType.Width)) return false; cptChildren++; - if (Children [i].Width == 0) { - if (!(stretchedGO == null && Width >= 0)) { + if (Children [i].Width == Measure.Stretched) { + if (!(stretchedGO == null && Width != Measure.Fit)) { //change size policy of other stretched children - Children [i].Width = -1; + Children [i].Width = Measure.Fit; return false; } stretchedGO = Children [i]; @@ -146,7 +146,7 @@ namespace Crow } tmpWidth -= Children [i].Slot.Width + Spacing; } - if (stretchedGO != null && Width >= 0) { + if (stretchedGO != null && Width != Measure.Fit) { tmpWidth += (Spacing - 2 * Margin); if (tmpWidth < MinimumSize.Width) tmpWidth = MinimumSize.Width; @@ -172,9 +172,9 @@ namespace Crow if (Children [i].RegisteredLayoutings.HasFlag (LayoutingType.Height)) return false; cptChildren++; - if (Children [i].Height == 0) { - if (!(stretchedGO == null && Height >= 0)){ - Children [i].Height = -1; + if (Children [i].Height == Measure.Stretched) { + if (!(stretchedGO == null && Height != Measure.Fit)){ + Children [i].Height = Measure.Fit; return false; } stretchedGO = Children [i]; @@ -184,7 +184,7 @@ namespace Crow } tmpHeight -= Children[i].Slot.Height + Spacing; } - if (stretchedGO != null && Height >= 0) { + if (stretchedGO != null && Height != Measure.Fit) { tmpHeight += (Spacing - 2 * Margin); if (tmpHeight < MinimumSize.Height) tmpHeight = MinimumSize.Height; @@ -221,14 +221,14 @@ namespace Crow switch (arg.LayoutType) { case LayoutingType.Width: if (Orientation == Orientation.Horizontal) { - if (Width < 0) + if (Width == Measure.Fit) this.RegisterForLayouting (LayoutingType.Width); this.RegisterForLayouting (LayoutingType.ArrangeChildren); } break; case LayoutingType.Height: if (Orientation == Orientation.Vertical) { - if (Height < 0) + if (Height == Measure.Fit) this.RegisterForLayouting (LayoutingType.Height); this.RegisterForLayouting (LayoutingType.ArrangeChildren); } diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index 7b58f652..8877c6b2 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -95,7 +95,8 @@ namespace Crow /// <summary> /// Original size and position 0=Stretched; -1=Fit /// </summary> - int _width, _height, _left, _top; + Measure _width, _height; + int _left, _top; /// <summary> /// Current size and position computed during layouting pass /// </summary> @@ -239,8 +240,8 @@ namespace Crow this.RegisterForLayouting (LayoutingType.Y); } } - [XmlAttributeAttribute()][DefaultValue(0)] - public virtual int Width { + [XmlAttributeAttribute()][DefaultValue("Stretched")] + public virtual Measure Width { get { return _width; } set { if (_width == value) @@ -253,8 +254,8 @@ namespace Crow this.RegisterForLayouting (LayoutingType.Width); } } - [XmlAttributeAttribute()][DefaultValue(0)] - public virtual int Height { + [XmlAttributeAttribute()][DefaultValue("Stretched")] + public virtual Measure Height { get { return _height; } set { if (_height == value) @@ -267,17 +268,21 @@ namespace Crow this.RegisterForLayouting (LayoutingType.Height); } } - [XmlIgnore]public virtual int WidthPolicy { get { return Width < 1 ? Width : 0; } } - [XmlIgnore]public virtual int HeightPolicy { get { return Height < 1 ? Height : 0; } } + [XmlIgnore]public virtual Measure WidthPolicy { get { + return Width.Units == Unit.Percent || Width.IsFixed ? + Measure.Stretched : Measure.Fit; } } + [XmlIgnore]public virtual Measure HeightPolicy { get { + return Height.Units == Unit.Percent || Height.IsFixed ? + Measure.Stretched : Measure.Fit; } } [XmlAttributeAttribute()][DefaultValue(false)] public virtual bool Fit { - get { return Width < 0 && Height < 0 ? true : false; } + get { return Width == Measure.Fit && Height == Measure.Fit ? true : false; } set { if (value == Fit) return; - Width = Height = -1; + Width = Height = Measure.Fit; } } [XmlAttributeAttribute()][DefaultValue(false)] @@ -800,22 +805,24 @@ namespace Crow LastSlots.Y = Slot.Y; break; case LayoutingType.Width: - if (Width > 0) + if (Width.IsFixed) Slot.Width = Width; - else if (Width < 0) { + else if (Width == Measure.Fit) { Slot.Width = measureRawSize (LayoutingType.Width); } else if (Parent.RegisteredLayoutings.HasFlag (LayoutingType.Width)) return false; - else + else if (Width == Measure.Stretched) Slot.Width = Parent.ClientRectangle.Width; + else + Slot.Width = Parent.ClientRectangle.Width * Width / 100; //size constrain if (Slot.Width < MinimumSize.Width) { Slot.Width = MinimumSize.Width; - NotifyValueChanged ("WidthPolicy", 0); + NotifyValueChanged ("WidthPolicy", Measure.Stretched); } else if (Slot.Width > MaximumSize.Width && MaximumSize.Width > 0) { Slot.Width = MaximumSize.Width; - NotifyValueChanged ("WidthPolicy", 0); + NotifyValueChanged ("WidthPolicy", Measure.Stretched); } if (LastSlots.Width == Slot.Width) @@ -828,22 +835,24 @@ namespace Crow LastSlots.Width = Slot.Width; break; case LayoutingType.Height: - if (Height > 0) + if (Height.IsFixed) Slot.Height = Height; - else if (Height < 0) { + else if (Height == Measure.Fit) { Slot.Height = measureRawSize (LayoutingType.Height); } else if (Parent.RegisteredLayoutings.HasFlag (LayoutingType.Height)) return false; - else + else if (Height == Measure.Stretched) Slot.Height = Parent.ClientRectangle.Height; + else + Slot.Height = Parent.ClientRectangle.Height * Height.Value / 100; //size constrain if (Slot.Height < MinimumSize.Height) { Slot.Height = MinimumSize.Height; - NotifyValueChanged ("HeightPolicy", 0); + NotifyValueChanged ("HeightPolicy", Measure.Stretched); } else if (Slot.Height > MaximumSize.Height && MaximumSize.Height > 0) { Slot.Height = MaximumSize.Height; - NotifyValueChanged ("HeightPolicy", 0); + NotifyValueChanged ("HeightPolicy", Measure.Stretched); } if (LastSlots.Height == Slot.Height) diff --git a/src/GraphicObjects/Group.cs b/src/GraphicObjects/Group.cs index 47795b06..78c5bca3 100644 --- a/src/GraphicObjects/Group.cs +++ b/src/GraphicObjects/Group.cs @@ -139,7 +139,7 @@ namespace Crow if (largestChild == null){ //if still null, not possible to determine a width //because all children are stretched, force first one to fit - Children[0].Width = -1; + Children[0].Width = Measure.Fit; return -1;//cancel actual sizing to let child computation take place } return maxChildrenWidth + 2 * Margin; @@ -147,7 +147,7 @@ namespace Crow if (tallestChild == null) searchTallestChild (); if (tallestChild == null) { - Children[0].Height = -1; + Children[0].Height = Measure.Fit; return -1; } return maxChildrenHeight + 2 * Margin; @@ -178,14 +178,14 @@ namespace Crow if (g.Slot.Width > maxChildrenWidth) { maxChildrenWidth = g.Slot.Width; largestChild = g; - if (Width < 0) + if (Width == Measure.Fit) this.RegisterForLayouting (LayoutingType.Width); } else if (g == largestChild) { largestChild = null; maxChildrenWidth = 0; - if (Width < 0) + if (Width == Measure.Fit) this.RegisterForLayouting (LayoutingType.Width); } break; @@ -193,14 +193,14 @@ namespace Crow if (g.Slot.Height > maxChildrenHeight) { maxChildrenHeight = g.Slot.Height; tallestChild = g; - if (Height < 0) + if (Height == Measure.Fit) this.RegisterForLayouting (LayoutingType.Height); } else if (g == tallestChild) { tallestChild = null; maxChildrenHeight = 0; - if (Height < 0) + if (Height == Measure.Fit) this.RegisterForLayouting (LayoutingType.Height); } break; diff --git a/src/GraphicObjects/Label.cs b/src/GraphicObjects/Label.cs index c28f090c..0a909b73 100644 --- a/src/GraphicObjects/Label.cs +++ b/src/GraphicObjects/Label.cs @@ -397,7 +397,8 @@ namespace Crow Rectangle cb = ClientRectangle; //ignore text alignment if size to content = true - if (Width < 0 || Height < 0) + //TODO: split horizontal and vertical logic + if (Width == Measure.Fit || Height == Measure.Fit) { rText.X = cb.X; rText.Y = cb.Y; @@ -406,7 +407,9 @@ namespace Crow widthRatio = (float)cb.Width / rText.Width; if (!verticalStretch) heightRatio = widthRatio; - }if (verticalStretch) { + } + + if (verticalStretch) { heightRatio = (float)cb.Height / rText.Height; if (!horizontalStretch) widthRatio = heightRatio; diff --git a/src/GraphicObjects/PrivateContainer.cs b/src/GraphicObjects/PrivateContainer.cs index f3385a88..98e8d4b1 100644 --- a/src/GraphicObjects/PrivateContainer.cs +++ b/src/GraphicObjects/PrivateContainer.cs @@ -108,12 +108,12 @@ namespace Crow //child has stretched size switch (layoutType) { case LayoutingType.Width: - if (Width < 0 && child.Width == 0) - child.Width = -1; + if (Width == Measure.Fit && child.Width.Units == Unit.Percent) + child.Width = Measure.Fit; break; case LayoutingType.Height: - if (Height < 0 && child.Height == 0) - child.Height = -1; + if (Height == Measure.Fit && child.Height.Units == Unit.Percent) + child.Height = Measure.Fit; break; } } diff --git a/src/GraphicObjects/TextRun.cs b/src/GraphicObjects/TextRun.cs index 021d5654..7f50a34e 100644 --- a/src/GraphicObjects/TextRun.cs +++ b/src/GraphicObjects/TextRun.cs @@ -11,6 +11,7 @@ using System.ComponentModel; namespace Crow { [Serializable] + [DefaultStyle("#Crow.Styles.TextRun.style")] public class TextRun : GraphicObject { #region CTOR @@ -127,22 +128,6 @@ namespace Crow } #region GraphicObject overrides - [XmlAttributeAttribute()][DefaultValue(-1)] - public override int Width { - get { return base.Width; } - set { base.Width = value; } - } - [XmlAttributeAttribute()][DefaultValue(-1)] - public override int Height { - get { return base.Height; } - set { base.Height = value; } - } - [XmlAttributeAttribute()][DefaultValue(2)] - public override int Margin { - get { return base.Margin; } - set { base.Margin = value; } - } - protected override int measureRawSize(LayoutingType lt) { if (lines == null) diff --git a/src/Measure.cs b/src/Measure.cs new file mode 100644 index 00000000..4fbac421 --- /dev/null +++ b/src/Measure.cs @@ -0,0 +1,101 @@ +// +// Measure.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; + +namespace Crow +{ + public enum Unit { Pixel, Percent } + public struct Measure + { + public int Value; + public Unit Units; + + public static Measure Fit = new Measure(-1); + //TODO:implement stretching as 100%, not 0% + public static Measure Stretched = new Measure(0, Unit.Percent); + + public Measure (int _value, Unit _units = Unit.Pixel) + { + Value = _value; + Units = _units; + } + public bool IsFixed { get { return Value > 0 && Units == Unit.Pixel; }} + #region Operators + public static implicit operator int(Measure m){ + return m.Value; + } + public static implicit operator Measure(int i){ + return new Measure(i); + } + public static implicit operator string(Measure m){ + return m.ToString(); + } + public static implicit operator Measure(string s){ + return Measure.Parse(s); + } + + public static bool operator ==(Measure m1, Measure m2){ + return m1.Value == m2.Value && m1.Units == m2.Units; + } + public static bool operator !=(Measure m1, Measure m2){ + return !(m1.Value == m2.Value && m1.Units == m2.Units); + } + #endregion + public override int GetHashCode () + { + return Value.GetHashCode (); + } + public override bool Equals (object obj) + { + return (obj == null || obj.GetType() != typeof(Measure)) ? + false : + this == (Measure)obj; + } + public override string ToString () + { + return Value == -1 ? "Fit" : + Value == 0 ? "Stretched" : + Units == Unit.Percent ? Value.ToString () + "%" : Value.ToString (); + } + public static Measure Parse(string s){ + if (string.IsNullOrEmpty (s)) + return Measure.Stretched; + + string st = s.Trim (); + int tmp = 0; + + if (string.Equals ("Fit", st, StringComparison.Ordinal)) + return Measure.Fit; + else if (string.Equals ("Stretched", s, StringComparison.Ordinal)) + return Measure.Stretched; + else { + if (st.EndsWith ("%", StringComparison.Ordinal)) { + if (int.TryParse (s.Substring(0, st.Length - 1), out tmp)) + return new Measure (tmp, Unit.Percent); + }else if (int.TryParse (s, out tmp)) + return new Measure (tmp); + } + + throw new Exception ("Error parsing Measure."); + } + + } +} |