namespace IndexFormula.Finance { using IndexFormula.Finance.DataProvider; using System; using System.ComponentModel; using System.Drawing; using System.Globalization; using System.IO; using System.Runtime.InteropServices; public class Layout { private int Bar = 0; public Rectangle ChartRect; private double[] CLOSE = null; private Color[] ColorTextColor = new Color[] { Color.DarkGreen, Color.Black, Color.Maroon }; public string CompanyName; private double[] DATE = null; public LayoutLabelCollection Labels = new LayoutLabelCollection(); private TextAlign NowAlign; private Rectangle NowBack = Rectangle.Empty; private Color NowBackColor = Color.Empty; private Color NowColor = Color.Black; private Font NowColorFont = new Font("Verdana", 8f); private Font NowFont = new Font("Verdana", 8f); private Rectangle NowFrame = Rectangle.Empty; private Color NowFrameColor = Color.Empty; private string NowIcon = ""; private Point NowPos = Point.Empty; private string NowText = ""; private bool NowUseColor = false; public long StartTick; private TypeConverter tcColor = null; private TypeConverter tcFont = null; public string URL; private void AddLabel() { LayoutLabel ll = new LayoutLabel(); ll.Back = this.NowBack; ll.BackColor = this.NowBackColor; ll.Frame = this.NowFrame; ll.FrameColor = this.NowFrameColor; ll.Pos = this.NowPos; ll.Text = this.NowText; ll.TextColor = this.NowColor; ll.UseColor = this.NowUseColor; ll.Icon = this.NowIcon; ll.Align = this.NowAlign; if (this.NowUseColor) { ll.TextFont = this.NowColorFont; } else { ll.TextFont = this.NowFont; } this.Labels.Add(ll); } private void AdjustNowPos(LayoutLabel ll, SizeF sf, ref Rectangle R, ref Point NowPos) { if (ll.Pos != Point.Empty) { if (ll.Pos.X < 0) { if (ll.Align == TextAlign.Right) { NowPos.X = R.Right + ll.Pos.X; } else { NowPos.X = (R.Right + ll.Pos.X) - ((int) sf.Width); } } else { NowPos.X = R.X + ll.Pos.X; } if (ll.Pos.Y < 0) { NowPos.Y = (R.Bottom + ll.Pos.Y) - ((int) sf.Height); } else { NowPos.Y = R.Y + ll.Pos.Y; } } } public int GetColorIndex(double d1, double d2) { if (d1 < d2) { return 0; } if (d1 == d2) { return 1; } return 2; } public double GetLast() { if ((this.Bar > 0) && (this.Bar < (this.CLOSE.Length + 1))) { return this.CLOSE[this.Bar - 1]; } return 0.0; } private void GetNameValue(string s, out string Name, out string Value) { int index = s.IndexOf('='); if (index > 0) { Name = s.Substring(0, index); Value = s.Substring(index + 1); if (Value.StartsWith("(")) { Value = Value.Substring(1); } if (Value.EndsWith(")")) { Value = Value.Substring(0, Value.Length - 1); } } else { Name = s; Value = ""; } } private string GetOneTag(string s, out string Name, out string Value) { int num = 0; for (int i = 0; i < s.Length; i++) { if (s[i] == '(') { num++; } if (s[i] == ')') { num--; } if ((s[i] == ';') && (num == 0)) { this.GetNameValue(s.Substring(0, i), out Name, out Value); return s.Substring(i + 1); } } this.GetNameValue(s, out Name, out Value); return ""; } private void InternalParse(string s, Rectangle Rect) { while (s != "") { string str; string str2; int num; int num2; s = this.GetOneTag(s, out str, out str2); switch (str) { case "Font": case "ColorFont": { if (this.tcFont == null) { this.tcFont = TypeDescriptor.GetConverter(typeof(Font)); } Font font = (Font) this.tcFont.ConvertFromString(null, CultureInfo.InvariantCulture, str2); if (str == "Font") { this.NowFont = font; } else { this.NowColorFont = font; } continue; } default: { string str3 = str; if (str3 == null) { continue; } str3 = string.IsInterned(str3); if (str3 == "ChartRect") { this.ChartRect = this.ParseRect(Rect, str2); continue; } if (str3 == "Align") { if (str2 == "Right") { this.NowAlign = TextAlign.Right; } else { this.NowAlign = TextAlign.Left; } continue; } if (str3 == "Color") { if (this.tcColor == null) { this.tcColor = TypeDescriptor.GetConverter(typeof(Color)); } this.NowColor = (Color) this.tcColor.ConvertFromString(null, CultureInfo.InvariantCulture, str2); continue; } if (str3 == "Back") { this.NowBack = this.ParseRect(Rect, str2); this.NowText = ""; this.NowBackColor = this.NowColor; this.AddLabel(); this.NowBack = Rectangle.Empty; continue; } if (str3 == "Frame") { this.NowFrame = this.ParseRect(Rect, str2); this.NowText = ""; this.NowFrameColor = this.NowColor; this.AddLabel(); this.NowFrame = Rectangle.Empty; continue; } if (str3 == "Pos") { this.NowPos = this.ParsePoint(Rect, str2); continue; } if (str3 == "Text") { this.NowText = str2; this.NowUseColor = false; this.AddLabel(); this.NowPos = Point.Empty; this.NowText = ""; continue; } if (str3 == "Icon") { this.NowIcon = str2; this.AddLabel(); this.NowPos = Point.Empty; this.NowIcon = ""; continue; } if (str3 != "ColorText") { continue; } num = 0; break; } } Label_028C: num2 = str2.IndexOf("{", num); if (num2 >= 0) { int index = str2.IndexOf("}", num2); if (index > 0) { this.NowUseColor = false; this.NowText = str2.Substring(num, num2 - num); this.AddLabel(); this.NowPos = Point.Empty; this.NowUseColor = true; this.NowText = str2.Substring(num2, (index - num2) + 1); this.AddLabel(); num = index + 1; goto Label_028C; } } } } public void Merge(Layout L) { foreach (LayoutLabel label in L.Labels) { this.Labels.Add(label); } } private Point ParsePoint(Rectangle R, string s) { string[] strArray = s.Split(new char[] { ',' }); try { return new Point(R.X + int.Parse(strArray[0]), R.Y + int.Parse(strArray[1])); } catch { return Point.Empty; } } private Rectangle ParseRect(Rectangle R, string s) { string[] strArray = s.Split(new char[] { ',' }); try { if (strArray.Length == 4) { int num = int.Parse(strArray[0]); if (num >= 0) { R.X += num; } else { R.X = (R.Right + num) + 1; } int num2 = int.Parse(strArray[1]); if (num2 >= 0) { R.Y += num2; } else { R.Y = (R.Bottom + num2) + 1; } int num3 = int.Parse(strArray[2]); if (num3 > 0) { R.Width = num3 - R.Left; } else { R.Width = ((R.Width + num3) + 1) - R.Left; } int num4 = int.Parse(strArray[3]); if (num4 > 0) { R.Height = num4 - R.Top; } else { R.Height = ((R.Height + num4) + 1) - R.Top; } R.Width = Math.Abs(R.Width); R.Height = Math.Abs(R.Height); return R; } return R; } catch { return R; } } public static Layout ParseString(string s, Rectangle Rect) { Layout layout = new Layout(); layout.InternalParse(s, Rect); return layout; } public void Render(Graphics g, Rectangle R, FormulaChart fc) { this.Render(g, R, fc, Point.Empty); } public Point Render(Graphics g, Rectangle R, FormulaChart fc, Point NowPos) { return this.Render(g, R, fc, NowPos, -1); } public Point Render(Graphics g, Rectangle R, FormulaChart fc, Point NowPos, int Bar) { this.Bar = Bar; foreach (LayoutLabel label in this.Labels) { if (label.Frame != Rectangle.Empty) { g.DrawRectangle(new Pen(label.FrameColor), label.Frame); } if (label.Back != Rectangle.Empty) { g.FillRectangle(new SolidBrush(label.BackColor), label.Back); } int colorIndex = 0; string text = this.ReplaceText(fc, label.Text, out colorIndex); SizeF empty = SizeF.Empty; if ((label.Text != null) && (label.Text != "")) { Color textColor; empty = g.MeasureString(text, label.TextFont); this.AdjustNowPos(label, empty, ref R, ref NowPos); if (label.UseColor) { textColor = this.ColorTextColor[colorIndex]; } else { textColor = label.TextColor; } g.DrawString(text, label.TextFont, new SolidBrush(textColor), (PointF) NowPos); } if ((label.Icon != null) && (label.Icon != "")) { string imageFile = FormulaHelper.GetImageFile(label.Icon); if (File.Exists(imageFile)) { Image image = Image.FromFile(imageFile); empty = new SizeF((float) image.Width, (float) image.Height); this.AdjustNowPos(label, empty, ref R, ref NowPos); g.DrawImage(image, NowPos); } } if (label.Pos.X >= 0) { NowPos.X += (int) empty.Width; } } return NowPos; } private string ReplaceText(FormulaChart fc, string s, out int ColorIndex) { ColorIndex = 0; while (true) { int index = s.IndexOf('{'); int num2 = s.IndexOf('}'); if (num2 <= index) { return s; } string str = s.Substring(index + 1, (num2 - index) - 1); int length = str.IndexOf(':'); string format = ""; string strA = str; if (length > 0) { strA = str.Substring(0, length); format = str.Substring(length + 1); } if (strA == "Company") { strA = this.CompanyName; } else if (strA == "URL") { strA = this.URL; } else if (strA == "Time") { strA = (((DateTime.Now.Ticks - this.StartTick) / 0x2710L)).ToString() + "ms"; } else { IDataProvider dataProvider = fc.DataProvider; FormulaArea mainArea = fc.MainArea; this.DATE = dataProvider["DATE"]; this.CLOSE = dataProvider["CLOSE"]; if ((this.CLOSE == null) || (this.CLOSE.Length == 0)) { return s; } if (this.Bar < 0) { for (int i = this.CLOSE.Length - 1; i >= 0; i--) { if (!double.IsNaN(this.CLOSE[i])) { this.Bar = i; break; } } } if (this.Bar < 0) { this.Bar = 0; } if (string.Compare(strA, "D") == 0) { if (format == "") { format = "dd-MMM-yyyy dddd"; } string stringData = dataProvider.GetStringData("LastTradeTime"); DateTime def = fc.IndexToDate(this.Bar); if ((stringData != null) && (stringData != "")) { def = FormulaHelper.ToDateDef(stringData, def); } strA = def.ToString(format); } else { string str5 = dataProvider.GetStringData(strA); if (str5 != null) { strA = str5; } else { try { double last = 0.0; format = mainArea.AxisY.Format; if (string.Compare(strA, "Change", true) == 0) { last = this.GetLast(); if (this.Bar < this.CLOSE.Length) { double num6 = this.CLOSE[this.Bar]; ColorIndex = this.GetColorIndex(last, num6); string str6 = ((num6 - last) / last).ToString("p2"); if (!str6.StartsWith("-")) { str6 = "+" + str6; } if (format == "") { format = "f" + FormulaHelper.TestBestFormat(num6, 2); } strA = ((num6 - last)).ToString(format) + "(" + str6 + ")"; } } else { double naN = 0.0; if (strA == "LC") { naN = this.GetLast(); } else { FormulaData objA = dataProvider[strA]; double[] data = null; if (!object.Equals(objA, null)) { data = objA.Data; } if ((data != null) && (this.Bar < data.Length)) { last = this.GetLast(); naN = data[this.Bar]; if (strA == "VOLUME") { format = "f0"; ColorIndex = this.GetColorIndex(last, this.CLOSE[this.Bar]); } else { ColorIndex = this.GetColorIndex(last, naN); } } else { naN = double.NaN; } } if (format == "") { format = "f" + FormulaHelper.TestBestFormat(naN, 2); } strA = FormulaHelper.FormatDouble(naN, format); } } catch { strA = ""; } } } } s = s.Substring(0, index) + strA + s.Substring(num2 + 1); } } public void SetFont(Font F) { foreach (LayoutLabel label in this.Labels) { label.TextFont = F; } } } }