diff options
| author | jpbruyere <jp.bruyere@hotmail.com> | 2016-07-30 15:32:52 +0200 |
|---|---|---|
| committer | jpbruyere <jp.bruyere@hotmail.com> | 2016-07-30 15:32:52 +0200 |
| commit | be8a4c2923ee9a62646590576cdf2fcdbf0d39a3 (patch) | |
| tree | 502c57dbb061065e252701ada79f06ef183cb841 /src/GraphicObjects | |
| parent | 6c6fb760bfb6a956a3413abfb56571a11113acd3 (diff) | |
basic styling ok, need handler and expression now.
Diffstat (limited to 'src/GraphicObjects')
| -rw-r--r-- | src/GraphicObjects/GraphicObject.cs | 36 | ||||
| -rw-r--r-- | src/GraphicObjects/Label.cs | 16 | ||||
| -rw-r--r-- | src/GraphicObjects/TextRun.cs | 149 |
3 files changed, 107 insertions, 94 deletions
diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index 3eaa91f1..26a7a59d 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -485,6 +485,17 @@ namespace Crow } #endregion + + /// <summary> + /// Add 'Styles' to full class name between assembly name and the rest of the full name space + /// </summary> + 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; + } /// <summary> Loads the default values from XML attributes default </summary> protected virtual void loadDefaultValues() { @@ -500,8 +511,21 @@ namespace Crow Dictionary<string, string> styling = null; - if (Interface.CurrentInterface.Styling.ContainsKey (thisType.Name)) - styling = Interface.CurrentInterface.Styling[thisType.Name]; + //Search for a style mathing : + //1: Full class name, with full namespace + //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)) + styling = Interface.CurrentInterface.Styling [thisType.FullName]; + else if (Interface.CurrentInterface.Styling.ContainsKey (thisType.Name)) + styling = Interface.CurrentInterface.Styling [thisType.Name]; + else { + string styleFullName = getDefaultStyleResId (thisType); + if (Interface.CurrentInterface.Styling.ContainsKey (styleFullName)) + styling = Interface.CurrentInterface.Styling [styleFullName]; + } //Reflexion being very slow compared to dyn method or delegates, //I compile the initial values coded in the CustomAttribs of the class, @@ -546,7 +570,7 @@ namespace Crow memberHasStyle = true; } if (memberHasStyle){ - + defaultValue = styling [name]; }else { if (name == "Style") { //retrieve default value from class attribute @@ -635,10 +659,12 @@ namespace Crow //surely a class or struct if (dvType != typeof(string)) throw new Exception ("Expecting String in default values for: " + pi.Name); - if (pi.PropertyType == typeof(string)) + if (pi.PropertyType == typeof (string)) il.Emit (OpCodes.Ldstr, Convert.ToString (defaultValue)); else { - MethodInfo miParse = pi.PropertyType.GetMethod ("Parse", BindingFlags.Static | BindingFlags.Public); + MethodInfo miParse = pi.PropertyType.GetMethod + ("Parse", BindingFlags.Static | BindingFlags.Public, + Type.DefaultBinder, new Type [] {typeof (string)},null); if (miParse == null) throw new Exception ("no Parse method found for: " + pi.PropertyType.FullName); diff --git a/src/GraphicObjects/Label.cs b/src/GraphicObjects/Label.cs index e1bf02a7..9af86266 100644 --- a/src/GraphicObjects/Label.cs +++ b/src/GraphicObjects/Label.cs @@ -383,11 +383,8 @@ namespace Crow foreach (string s in lines) { string l = s.Replace("\t", new String (' ', Interface.TabSize)); -#if _WIN32 || _WIN64 - TextExtents tmp = gr.TextExtents(str.ToUtf8()); -#elif __linux__ TextExtents tmp = gr.TextExtents (l); -#endif + if (tmp.XAdvance > te.XAdvance) te = tmp; } @@ -582,11 +579,8 @@ namespace Crow Foreground.SetAsSource (gr); gr.MoveTo (lineRect.X, rText.Y + fe.Ascent + fe.Height * i); - #if _WIN32 || _WIN64 - gr.ShowText(l.ToUtf8()); - #elif __linux__ gr.ShowText (l); - #endif + gr.Fill (); } } @@ -675,12 +669,8 @@ namespace Crow if (c == "\t") c = new string (' ', Interface.TabSize); - #if _WIN32 || _WIN64 - byte[] c = System.Text.UTF8Encoding.UTF8.GetBytes(Text.Substring(i, 1)); te = gr.TextExtents(c); - #elif __linux__ - te = gr.TextExtents(c); - #endif + double halfWidth = te.XAdvance / 2; if (mouseLocalPos.X <= cPos + halfWidth) diff --git a/src/GraphicObjects/TextRun.cs b/src/GraphicObjects/TextRun.cs index c644767c..cc882f40 100644 --- a/src/GraphicObjects/TextRun.cs +++ b/src/GraphicObjects/TextRun.cs @@ -10,30 +10,30 @@ using System.ComponentModel; namespace Crow { - public class TextRun : GraphicObject - { + public class TextRun : GraphicObject + { #region CTOR - public TextRun() - { + public TextRun () + { } - public TextRun(string _text) - : base() + public TextRun (string _text) + : base () { Text = _text; } #endregion - //TODO:change protected to private - + //TODO:change protected to private + #region private and protected fields protected string _text = "label"; - Alignment _textAlignment = Alignment.Left; + Alignment _textAlignment = Alignment.Left; bool horizontalStretch = false; bool verticalStretch = false; bool _multiline; bool wordWrap; - protected Rectangle rText; + protected Rectangle rText; protected float widthRatio = 1f; protected float heightRatio = 1f; protected FontExtents fe; @@ -41,69 +41,70 @@ namespace Crow #endregion - [XmlAttributeAttribute()][DefaultValue(Alignment.Left)] - public Alignment TextAlignment - { - get { return _textAlignment; } - set { _textAlignment = value; } - } - [XmlAttributeAttribute()][DefaultValue(false)] + [XmlAttributeAttribute ()] + [DefaultValue (Alignment.Left)] + public Alignment TextAlignment { + get { return _textAlignment; } + set { _textAlignment = value; } + } + [XmlAttributeAttribute ()] + [DefaultValue (false)] public virtual bool HorizontalStretch { get { return horizontalStretch; } set { if (horizontalStretch == value) return; - horizontalStretch = value; + horizontalStretch = value; RegisterForGraphicUpdate (); NotifyValueChanged ("HorizontalStretch", horizontalStretch); } } - [XmlAttributeAttribute()][DefaultValue(false)] + [XmlAttributeAttribute ()] + [DefaultValue (false)] public virtual bool VerticalStretch { get { return verticalStretch; } set { if (verticalStretch == value) return; - verticalStretch = value; + verticalStretch = value; NotifyValueChanged ("VerticalStretch", verticalStretch); } - } - [XmlAttributeAttribute()][DefaultValue("label")] - public string Text - { - get { - return lines == null ? - _text : lines.Aggregate((i, j) => i + Interface.LineBreak + j); + } + [XmlAttributeAttribute ()] + [DefaultValue ("label")] + public string Text { + get { + return lines == null ? + _text : lines.Aggregate ((i, j) => i + Interface.LineBreak + j); } - set - { - if (_text == value) - return; - - RegisterForGraphicUpdate(); + set { + if (_text == value) + return; + + RegisterForGraphicUpdate (); this.RegisterForLayouting (LayoutingType.Sizing); - _text = value; + _text = value; - if (string.IsNullOrEmpty(_text)) + if (string.IsNullOrEmpty (_text)) _text = ""; lines = getLines; - } - } - [XmlAttributeAttribute()][DefaultValue(false)] - public bool Multiline - { + } + } + [XmlAttributeAttribute ()] + [DefaultValue (false)] + public bool Multiline { get { return _multiline; } - set - { + set { _multiline = value; - RegisterForGraphicUpdate(); + RegisterForGraphicUpdate (); } } - [XmlAttributeAttribute()][DefaultValue(false)] + [XmlAttributeAttribute ()] + [DefaultValue (false)] public bool WordWrap { get { return wordWrap; @@ -112,22 +113,22 @@ namespace Crow if (wordWrap == value) return; wordWrap = value; - RegisterForGraphicUpdate(); + RegisterForGraphicUpdate (); } } List<string> lines; List<string> getLines { - get { + get { return _multiline ? - Regex.Split (_text, "\r\n|\r|\n").ToList() : - new List<string>(new string[] { _text }); + Regex.Split (_text, "\r\n|\r|\n").ToList () : + new List<string> (new string [] { _text }); } } #region GraphicObject overrides - protected override int measureRawSize(LayoutingType lt) - { + protected override int measureRawSize (LayoutingType lt) + { if (lines == null) lines = getLines; @@ -140,25 +141,22 @@ namespace Crow fe = gr.FontExtents; - te = new TextExtents(); + te = new TextExtents (); - if (lt == LayoutingType.Height){ + if (lt == LayoutingType.Height) { int lc = lines.Count; //ensure minimal height = text line height if (lc == 0) - lc = 1; + lc = 1; return (int)(fe.Height * lc) + Margin * 2; } foreach (string s in lines) { - string l = s.Replace("\t", new String (' ', Interface.TabSize)); + string l = s.Replace ("\t", new String (' ', Interface.TabSize)); - #if _WIN32 || _WIN64 - TextExtents tmp = gr.TextExtents(str.ToUtf8()); - #elif __linux__ TextExtents tmp = gr.TextExtents (l); - #endif + if (tmp.XAdvance > te.XAdvance) te = tmp; } @@ -176,8 +174,8 @@ namespace Crow gr.Antialias = Antialias.Subpixel; - rText = new Rectangle(new Size( - measureRawSize(LayoutingType.Width), measureRawSize(LayoutingType.Height))); + rText = new Rectangle (new Size ( + measureRawSize (LayoutingType.Width), measureRawSize (LayoutingType.Height))); rText.Width -= 2 * Margin; rText.Height -= 2 * Margin; @@ -188,16 +186,16 @@ namespace Crow //ignore text alignment if size to content = true //or if text size is larger than client bounds - if (Width < 0 || Height < 0 || rText.Width > cb.Width) - { + if (Width < 0 || Height < 0 || rText.Width > cb.Width) { rText.X = cb.X; rText.Y = cb.Y; - }else { + } else { if (horizontalStretch) { widthRatio = (float)cb.Width / rText.Width; if (!verticalStretch) heightRatio = widthRatio; - }if (verticalStretch) { + } + if (verticalStretch) { heightRatio = (float)cb.Height / rText.Height; if (!horizontalStretch) widthRatio = heightRatio; @@ -206,8 +204,7 @@ namespace Crow rText.Width = (int)(widthRatio * cb.Width); rText.Height = (int)(heightRatio * cb.Height); - switch (TextAlignment) - { + switch (TextAlignment) { case Alignment.TopLeft: //ok rText.X = cb.X; rText.Y = cb.Y; @@ -247,11 +244,11 @@ namespace Crow } } - gr.FontMatrix = new Matrix(widthRatio * Font.Size, 0, 0, heightRatio * Font.Size, 0, 0); + gr.FontMatrix = new Matrix (widthRatio * Font.Size, 0, 0, heightRatio * Font.Size, 0, 0); int curLineCount = 0; - for (int i = 0;i < lines.Count;i++) { + for (int i = 0; i < lines.Count; i++) { string l = lines [i].Replace ("\t", new String (' ', Interface.TabSize)); List<string> wl = new List<string> (); int lineLength = (int)gr.TextExtents (l).XAdvance; @@ -275,28 +272,28 @@ namespace Crow foreach (string ll in wl) { lineLength = (int)gr.TextExtents (ll).XAdvance; - + if (string.IsNullOrWhiteSpace (ll)) { curLineCount++; continue; } - Foreground.SetAsSource (gr); + Foreground.SetAsSource (gr); gr.MoveTo (rText.X, rText.Y + fe.Ascent + fe.Height * curLineCount); - #if _WIN32 || _WIN64 +#if _WIN32 || _WIN64 gr.ShowText(ll.ToUtf8()); - #elif __linux__ +#elif __linux__ gr.ShowText (ll); - #endif +#endif gr.Fill (); curLineCount++; - + } - } + } } #endregion - } + } } |