PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/Backup/FireballFX/Fireball.Windows.Forms/Windows/Forms/FormatLabelControl.cs

http://gluar.googlecode.com/
C# | 1329 lines | 1059 code | 200 blank | 70 comment | 153 complexity | ad3e7114b609c7e011a205f9318d7078 MD5 | raw file
  1. //ORIGINAL LGPL SOURCE CODE FINDED ON COMPONA LGPL SOURCE CODE
  2. using System;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Windows.Forms;
  8. using Fireball.Drawing.GDI;
  9. namespace Fireball.Windows.Forms
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public class FormatLabelControl : Widget
  15. {
  16. private string _Text = "format <b>label</b>";
  17. private FormatLabelElement[] _Elements = null;
  18. private ArrayList _Rows = null;
  19. private Hashtable _Fonts = new Hashtable();
  20. private Hashtable _Images = new Hashtable();
  21. private bool _WordWrap = true;
  22. private PictureBox Filler;
  23. private VScrollBar vScroll;
  24. private HScrollBar hScroll;
  25. private bool _AutoSizeHorizontal = false;
  26. private bool _AutoSizeVertical = false;
  27. private ScrollBars _ScrollBars = 0;
  28. private FormatLabelElement _ActiveElement = null;
  29. private Color _Link_Color = Color.Blue;
  30. private Color _Link_Color_Hover = Color.Blue;
  31. private bool _Link_UnderLine = false;
  32. private bool _Link_UnderLine_Hover = true;
  33. private bool _HasImageError = false;
  34. private ImageList _ImageList = null;
  35. public event ClickLinkEventHandler ClickLink = null;
  36. protected void OnClickLink(string Link)
  37. {
  38. if (ClickLink != null)
  39. ClickLink(this, new ClickLinkEventArgs(Link));
  40. }
  41. #region PUBLIC PROPERTY MARGIN
  42. private int _Margin = 0;
  43. public int LabelMargin
  44. {
  45. get { return _Margin; }
  46. set
  47. {
  48. _Margin = value;
  49. CreateRows();
  50. this.Invalidate();
  51. }
  52. }
  53. #endregion
  54. public ImageList ImageList
  55. {
  56. get { return _ImageList; }
  57. set
  58. {
  59. _ImageList = value;
  60. this.Invalidate();
  61. //this.Text = this.Text;
  62. }
  63. }
  64. public Color Link_Color
  65. {
  66. get { return _Link_Color; }
  67. set
  68. {
  69. _Link_Color = value;
  70. this.Invalidate();
  71. }
  72. }
  73. protected override void OnInvalidated(InvalidateEventArgs e)
  74. {
  75. if (this.IsHandleCreated)
  76. RedrawBuffer();
  77. base.OnInvalidated(e);
  78. }
  79. public Color Link_Color_Hover
  80. {
  81. get { return _Link_Color_Hover; }
  82. set
  83. {
  84. _Link_Color_Hover = value;
  85. this.Invalidate();
  86. }
  87. }
  88. public bool Link_UnderLine
  89. {
  90. get { return _Link_UnderLine; }
  91. set
  92. {
  93. _Link_UnderLine = value;
  94. this.Invalidate();
  95. }
  96. }
  97. public bool Link_UnderLine_Hover
  98. {
  99. get { return _Link_UnderLine_Hover; }
  100. set
  101. {
  102. _Link_UnderLine_Hover = value;
  103. this.Invalidate();
  104. }
  105. }
  106. public bool AutoSizeHorizontal
  107. {
  108. get { return _AutoSizeHorizontal; }
  109. set { _AutoSizeHorizontal = value; }
  110. }
  111. public bool AutoSizeVertical
  112. {
  113. get { return _AutoSizeVertical; }
  114. set { _AutoSizeVertical = value; }
  115. }
  116. #region Defaults
  117. private Container components = null;
  118. public FormatLabelControl()
  119. {
  120. _Rows = new ArrayList();
  121. InitializeComponent();
  122. SetStyle(ControlStyles.ResizeRedraw, true);
  123. SetStyle(ControlStyles.Opaque, true);
  124. this.Text = this.Text;
  125. InitScrollbars();
  126. }
  127. protected override void Dispose(bool disposing)
  128. {
  129. if (disposing)
  130. {
  131. foreach (GDIObject o in this._Fonts.Values)
  132. o.Dispose();
  133. if (components != null)
  134. components.Dispose();
  135. if (_bufferSurface != null)
  136. _bufferSurface.Dispose();
  137. }
  138. base.Dispose(disposing);
  139. }
  140. #region Component Designer generated code
  141. /// <summary>
  142. /// Required method for Designer support - do not modify
  143. /// the contents of this method with the code editor.
  144. /// </summary>
  145. private void InitializeComponent()
  146. {
  147. this.Filler = new System.Windows.Forms.PictureBox();
  148. this.vScroll = new System.Windows.Forms.VScrollBar();
  149. this.hScroll = new System.Windows.Forms.HScrollBar();
  150. this.SuspendLayout();
  151. //
  152. // Filler
  153. //
  154. this.Filler.BackColor = System.Drawing.SystemColors.Control;
  155. this.Filler.Cursor = System.Windows.Forms.Cursors.Default;
  156. this.Filler.Location = new System.Drawing.Point(136, 112);
  157. this.Filler.Name = "Filler";
  158. this.Filler.Size = new System.Drawing.Size(16, 16);
  159. this.Filler.TabIndex = 5;
  160. this.Filler.TabStop = false;
  161. //
  162. // vScroll
  163. //
  164. this.vScroll.Cursor = System.Windows.Forms.Cursors.Default;
  165. this.vScroll.LargeChange = 2;
  166. this.vScroll.Location = new System.Drawing.Point(136, -8);
  167. this.vScroll.Name = "vScroll";
  168. this.vScroll.Size = new System.Drawing.Size(16, 112);
  169. this.vScroll.TabIndex = 4;
  170. this.vScroll.Scroll += new System.Windows.Forms.ScrollEventHandler(this.vScroll_Scroll);
  171. //
  172. // hScroll
  173. //
  174. this.hScroll.Cursor = System.Windows.Forms.Cursors.Default;
  175. this.hScroll.LargeChange = 1;
  176. this.hScroll.Location = new System.Drawing.Point(0, 112);
  177. this.hScroll.Maximum = 600;
  178. this.hScroll.Name = "hScroll";
  179. this.hScroll.Size = new System.Drawing.Size(128, 16);
  180. this.hScroll.TabIndex = 3;
  181. //
  182. // FormatLabelControl
  183. //
  184. this.BackColor = System.Drawing.SystemColors.Window;
  185. this.Controls.AddRange(new System.Windows.Forms.Control[]
  186. {
  187. this.Filler,
  188. this.vScroll,
  189. this.hScroll
  190. });
  191. this.Name = "FormatLabelControl";
  192. this.Size = new System.Drawing.Size(160, 136);
  193. this.ResumeLayout(false);
  194. }
  195. [Browsable(true)]
  196. [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  197. public override string Text
  198. {
  199. get { return _Text; }
  200. set
  201. {
  202. try
  203. {
  204. //Text=value;
  205. _Text = value;
  206. CreateAll();
  207. this.Invalidate();
  208. }
  209. catch (Exception x)
  210. {
  211. Console.WriteLine(x.Message);
  212. System.Diagnostics.Debugger.Break();
  213. }
  214. RedrawBuffer();
  215. }
  216. }
  217. private void CreateAll()
  218. {
  219. _Elements = CreateElements();
  220. ClearFonts();
  221. ApplyFormat(_Elements);
  222. CreateWords(_Elements);
  223. CreateRows();
  224. SetAutoSize();
  225. }
  226. private void ClearFonts()
  227. {
  228. foreach (GDIFont gf in _Fonts.Values)
  229. {
  230. gf.Dispose();
  231. }
  232. _Fonts.Clear();
  233. }
  234. #endregion
  235. #endregion
  236. private void SetAutoSize()
  237. {
  238. if (this.AutoSizeHorizontal)
  239. this.Width = this.GetWidth();
  240. if (this.AutoSizeVertical)
  241. this.Height = this.GetHeight();
  242. }
  243. public bool WordWrap
  244. {
  245. get { return _WordWrap; }
  246. set
  247. {
  248. _WordWrap = value;
  249. CreateRows();
  250. this.Invalidate();
  251. }
  252. }
  253. [Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Obsolete("", false)]
  254. public override Image BackgroundImage
  255. {
  256. get { return base.BackgroundImage; }
  257. set { base.BackgroundImage = value; }
  258. }
  259. GDISurface _bufferSurface = null;
  260. private void RedrawBuffer()
  261. {
  262. if (_bufferSurface != null)
  263. _bufferSurface.Dispose();
  264. _bufferSurface = new GDISurface(this.Width, this.Height, this, true);
  265. using (Graphics gfx = Graphics.FromHdc(_bufferSurface.hDC))
  266. {
  267. //try
  268. //{
  269. _bufferSurface.FontTransparent = true;
  270. if (this.BackgroundImage != null)
  271. {
  272. gfx.DrawImage(this.BackgroundImage, 0, 0, this.Width, this.Height);
  273. }
  274. else
  275. {
  276. _bufferSurface.Clear(this.BackColor);
  277. }
  278. int x = LabelMargin;
  279. int y = LabelMargin;
  280. for (int i = vScroll.Value; i < _Rows.Count; i++)
  281. {
  282. FormatLabelRow r = (FormatLabelRow)_Rows[i];
  283. x = LabelMargin;
  284. r.Visible = true;
  285. r.Top = y;
  286. if (r.RenderSeparator)
  287. {
  288. Color c1 = Color.FromArgb(120, 0, 0, 0);
  289. Brush b1 = new SolidBrush(c1);
  290. gfx.FillRectangle(b1, 0, y, this.Width, 1);
  291. Color c2 = Color.FromArgb(120, 255, 255, 255);
  292. Brush b2 = new SolidBrush(c2);
  293. gfx.FillRectangle(b2, 0, y + 1, this.Width, 1);
  294. b1.Dispose();
  295. b2.Dispose();
  296. //bbuff.DrawLine (this.ForeColor,new Point (0,y),new Point (this.Width,y));
  297. }
  298. foreach (FormatLabelWord w in r.Words)
  299. {
  300. int ypos = r.Height - w.Height + y;
  301. if (w.Image != null)
  302. {
  303. gfx.DrawImage(w.Image, x, y);
  304. //bbuff.FillRect (Color.Red ,x,ypos,w.Width ,w.Height);
  305. }
  306. else
  307. {
  308. GDIFont gf = null;
  309. if (w.Element.Link != null)
  310. {
  311. Font f = null;
  312. FontStyle fs = w.Element.Font.Style;
  313. if (w.Element.Link == _ActiveElement)
  314. {
  315. if (_Link_UnderLine_Hover)
  316. fs |= FontStyle.Underline;
  317. f = new Font(w.Element.Font, fs);
  318. }
  319. else
  320. {
  321. if (_Link_UnderLine)
  322. fs |= FontStyle.Underline;
  323. f = new Font(w.Element.Font, fs);
  324. }
  325. gf = GetFont(f);
  326. }
  327. else
  328. {
  329. gf = GetFont(w.Element.Font);
  330. }
  331. _bufferSurface.Font = gf;
  332. if (w.Element.Effect != TextEffect.None)
  333. {
  334. _bufferSurface.TextForeColor = w.Element.EffectColor;
  335. if (w.Element.Effect == TextEffect.Outline)
  336. {
  337. for (int xx = -1; xx <= 1; xx++)
  338. for (int yy = -1; yy <= 1; yy++)
  339. _bufferSurface.DrawTabbedString(w.Text, x + xx, ypos + yy, 0, 0);
  340. }
  341. else if (w.Element.Effect != TextEffect.None)
  342. {
  343. _bufferSurface.DrawTabbedString(w.Text, x + 1, ypos + 1, 0, 0);
  344. }
  345. }
  346. if (w.Element.Link != null)
  347. {
  348. if (w.Element.Link == _ActiveElement)
  349. {
  350. _bufferSurface.TextForeColor = Link_Color_Hover;
  351. }
  352. else
  353. {
  354. _bufferSurface.TextForeColor = Link_Color;
  355. }
  356. }
  357. else
  358. _bufferSurface.TextForeColor = w.Element.ForeColor;
  359. _bufferSurface.TextBackColor = w.Element.BackColor;
  360. _bufferSurface.DrawTabbedString(w.Text, x, ypos, 0, 0);
  361. }
  362. w.ScreenArea =new Rectangle(new Point(x, ypos), w.ScreenArea.Size);
  363. //w.ScreenArea.Y = ypos;
  364. x += w.Width;
  365. }
  366. y += r.Height + r.BottomPadd;
  367. if (y > this.Height)
  368. break;
  369. }
  370. //}
  371. //catch (Exception x)
  372. //{
  373. // Console.WriteLine(x.Message);
  374. //}
  375. }
  376. }
  377. protected override void OnPaint(PaintEventArgs e)
  378. {
  379. this.SetAutoSize();
  380. //base.OnPaint (e);
  381. if (_HasImageError)
  382. CreateAll();
  383. if (_bufferSurface != null)
  384. _bufferSurface.RenderToControl(0, 0);
  385. }
  386. private FormatLabelElement[] CreateElements()
  387. {
  388. string text = this.Text.Replace("\n", "");
  389. text = text.Replace("\r", "");
  390. string[] parts = text.Split('<');
  391. ArrayList Elements = new ArrayList();
  392. int i = 0;
  393. foreach (string part in parts)
  394. {
  395. FormatLabelElement cmd = new FormatLabelElement();
  396. if (i == 0)
  397. {
  398. cmd.Text = part;
  399. }
  400. else
  401. {
  402. string[] TagTextPair = part.Split('>');
  403. cmd.Tag = TagTextPair[0].ToLower();
  404. if (cmd.Tag.IndexOfAny(" \t".ToCharArray()) >= 0)
  405. {
  406. int ws = cmd.Tag.IndexOfAny(" \t".ToCharArray());
  407. string s1 = TagTextPair[0].Substring(0, ws).ToLower();
  408. string s2 = TagTextPair[0].Substring(ws + 1);
  409. cmd.Tag = s1 + " " + s2;
  410. }
  411. cmd.Text = TagTextPair[1];
  412. if (cmd.TagName == "img")
  413. {
  414. FormatLabelElement img = new FormatLabelElement();
  415. img.Tag = cmd.Tag;
  416. Elements.Add(img);
  417. cmd.Tag = "";
  418. // Elements.Add (cmd);
  419. }
  420. //
  421. // if (cmd.TagName == "hr")
  422. // {
  423. // Element hr=new Element();
  424. // hr.Tag = cmd.Tag;
  425. // Elements.Add (hr);
  426. // cmd.Tag ="";
  427. // cmd.Text ="a";
  428. // // Elements.Add (cmd);
  429. // }
  430. cmd.Text = cmd.Text.Replace("\t", " ");
  431. cmd.Text = cmd.Text.Replace("&#145;", "'");
  432. cmd.Text = cmd.Text.Replace("&#146;", "'");
  433. cmd.Text = cmd.Text.Replace(" ", ((char) 1).ToString());
  434. cmd.Text = HttpUtility.HtmlDecode(cmd.Text);
  435. // cmd.Text =cmd.Text.Replace (" ","*");
  436. cmd.Text = cmd.Text.Replace(((char) 1).ToString(), " ");
  437. }
  438. Elements.Add(cmd);
  439. i++;
  440. }
  441. FormatLabelElement[] res = new FormatLabelElement[Elements.Count];
  442. Elements.CopyTo(res);
  443. return res;
  444. }
  445. private string GetAttrib(string attrib, string tag)
  446. {
  447. try
  448. {
  449. if (tag.IndexOf(attrib) < 0)
  450. return "";
  451. //tag=tag.Replace("\"","");
  452. tag = tag.Replace("\t", " ");
  453. int start = tag.IndexOf(attrib);
  454. int end = start + attrib.Length;
  455. int valuestart = tag.IndexOf("=", end);
  456. if (valuestart < 0)
  457. return "";
  458. valuestart++;
  459. string value = tag.Substring(valuestart);
  460. while (value.StartsWith(" "))
  461. value = value.Substring(1);
  462. //int pos=0;
  463. if (value.StartsWith("\""))
  464. {
  465. // = "value"
  466. value = value.Substring(1);
  467. int valueend = value.IndexOf("\"");
  468. value = value.Substring(0, valueend);
  469. return value;
  470. }
  471. else
  472. {
  473. // = value
  474. int valueend = value.IndexOf(" ");
  475. if (valueend < 0)
  476. valueend = value.Length;
  477. value = value.Substring(0, valueend);
  478. return value;
  479. }
  480. //return "";
  481. }
  482. catch
  483. {
  484. return "";
  485. }
  486. }
  487. private void ApplyFormat(FormatLabelElement[] Elements)
  488. {
  489. Stack bold = new Stack();
  490. Stack italic = new Stack();
  491. Stack underline = new Stack();
  492. Stack forecolor = new Stack();
  493. Stack backcolor = new Stack();
  494. Stack fontsize = new Stack();
  495. Stack fontname = new Stack();
  496. Stack link = new Stack();
  497. Stack effectcolor = new Stack();
  498. Stack effect = new Stack();
  499. bold.Push(this.Font.Bold);
  500. italic.Push(this.Font.Italic);
  501. underline.Push(this.Font.Underline);
  502. forecolor.Push(this.ForeColor);
  503. backcolor.Push(Color.Transparent);
  504. fontsize.Push((int) (this.Font.Size*1.3));
  505. fontname.Push(this.Font.Name);
  506. effect.Push(TextEffect.None);
  507. effectcolor.Push(Color.Black);
  508. link.Push(null);
  509. foreach (FormatLabelElement Element in Elements)
  510. {
  511. switch (Element.TagName)
  512. {
  513. case "b":
  514. {
  515. bold.Push(true);
  516. break;
  517. }
  518. case "a":
  519. {
  520. //underline.Push (true);
  521. //forecolor.Push (_l);
  522. link.Push(Element);
  523. break;
  524. }
  525. case "i":
  526. case "em":
  527. {
  528. italic.Push(true);
  529. break;
  530. }
  531. case "u":
  532. {
  533. underline.Push(true);
  534. break;
  535. }
  536. case "font":
  537. {
  538. string _fontname = GetAttrib("face", Element.Tag);
  539. string _size = GetAttrib("size", Element.Tag);
  540. string _color = GetAttrib("color", Element.Tag);
  541. string _effectcolor = GetAttrib("effectcolor", Element.Tag);
  542. string _effect = GetAttrib("effect", Element.Tag);
  543. if (_size == "")
  544. fontsize.Push(fontsize.Peek());
  545. else
  546. fontsize.Push(int.Parse(_size));
  547. if (_fontname == "")
  548. fontname.Push(fontname.Peek());
  549. else
  550. fontname.Push(_fontname);
  551. if (_color == "")
  552. forecolor.Push(forecolor.Peek());
  553. else
  554. forecolor.Push(Color.FromName(_color));
  555. if (_effectcolor == "")
  556. effectcolor.Push(effectcolor.Peek());
  557. else
  558. effectcolor.Push(Color.FromName(_effectcolor));
  559. if (_effect == "")
  560. effect.Push(effect.Peek());
  561. else
  562. effect.Push(Enum.Parse(typeof (TextEffect), _effect, true));
  563. break;
  564. }
  565. case "br":
  566. {
  567. Element.NewLine = true;
  568. break;
  569. }
  570. case "hr":
  571. {
  572. Element.NewLine = true;
  573. break;
  574. }
  575. case "h3":
  576. {
  577. fontsize.Push((int) (this.Font.Size*1.4));
  578. bold.Push(true);
  579. Element.NewLine = true;
  580. break;
  581. }
  582. case "h4":
  583. {
  584. fontsize.Push((int) (this.Font.Size*1.2));
  585. bold.Push(true);
  586. Element.NewLine = true;
  587. break;
  588. }
  589. case "/b":
  590. {
  591. bold.Pop();
  592. break;
  593. }
  594. case "/a":
  595. {
  596. //underline.Pop ();
  597. //forecolor.Pop ();
  598. link.Pop();
  599. break;
  600. }
  601. case "/i":
  602. case "/em":
  603. {
  604. italic.Pop();
  605. break;
  606. }
  607. case "/u":
  608. {
  609. underline.Pop();
  610. break;
  611. }
  612. case "/font":
  613. {
  614. fontname.Pop();
  615. fontsize.Pop();
  616. forecolor.Pop();
  617. effect.Pop();
  618. effectcolor.Pop();
  619. break;
  620. }
  621. case "/h3":
  622. {
  623. fontsize.Pop();
  624. bold.Pop();
  625. Element.NewLine = true;
  626. break;
  627. }
  628. case "/h4":
  629. {
  630. fontsize.Pop();
  631. bold.Pop();
  632. Element.NewLine = true;
  633. break;
  634. }
  635. default:
  636. {
  637. break;
  638. }
  639. }
  640. //---------------------------------------------------------------------
  641. bool Bold = (bool) bold.Peek();
  642. bool Italic = (bool) italic.Peek();
  643. bool Underline = (bool) underline.Peek();
  644. FormatLabelElement Link = (FormatLabelElement) link.Peek();
  645. string FontName = (string) fontname.Peek();
  646. int FontSize = (int) fontsize.Peek();
  647. Color BackColor = (Color) backcolor.Peek();
  648. Color ForeColor = (Color) forecolor.Peek();
  649. TextEffect Effect = (TextEffect) effect.Peek();
  650. Color EffectColor = (Color) effectcolor.Peek();
  651. FontStyle fs = 0;
  652. if (Bold) fs |= FontStyle.Bold;
  653. if (Italic) fs |= FontStyle.Italic;
  654. if (Underline) fs |= FontStyle.Underline;
  655. Font font = new Font(FontName, FontSize, fs);
  656. Element.Font = font;
  657. Element.BackColor = BackColor;
  658. Element.ForeColor = ForeColor;
  659. Element.Link = Link;
  660. Element.Effect = Effect;
  661. Element.EffectColor = EffectColor;
  662. }
  663. }
  664. private bool IsIndex(string SRC)
  665. {
  666. try
  667. {
  668. int i = int.Parse(SRC);
  669. return true;
  670. }
  671. catch
  672. {
  673. return false;
  674. }
  675. }
  676. private void CreateWords(FormatLabelElement[] Elements)
  677. {
  678. GDISurface bbuff = new GDISurface(1, 1, this, false);
  679. _HasImageError = false;
  680. foreach (FormatLabelElement Element in Elements)
  681. {
  682. if (Element.TagName == "img")
  683. {
  684. Element.words = new FormatLabelWord[1];
  685. Element.words[0] = new FormatLabelWord();
  686. Image img = null;
  687. try
  688. {
  689. string SRC = GetAttrib("img", Element.Tag).ToLower();
  690. if (IsIndex(SRC))
  691. {
  692. int index = int.Parse(SRC);
  693. img = this.ImageList.Images[index];
  694. }
  695. else if (SRC.StartsWith("http://")) //from url
  696. {
  697. }
  698. else if (SRC.StartsWith("file://")) // from file
  699. {
  700. img = Image.FromFile(SRC.Substring(7));
  701. }
  702. else //from file
  703. {
  704. img = Image.FromFile(SRC);
  705. }
  706. }
  707. catch
  708. {
  709. img = new Bitmap(20, 20);
  710. _HasImageError = true;
  711. }
  712. Element.words[0].Image = img;
  713. Element.words[0].Element = Element;
  714. if (img != null)
  715. {
  716. Element.words[0].Height = img.Height;
  717. Element.words[0].Width = img.Width;
  718. Element.words[0].ScreenArea = new Rectangle(Element.words[0].ScreenArea.Location,
  719. new Size(img.Width, img.Height));
  720. }
  721. }
  722. else
  723. {
  724. string[] words = Element.Text.Split(' ');
  725. Element.words = new FormatLabelWord[words.Length];
  726. int i = 0;
  727. foreach (string word in words)
  728. {
  729. Element.words[i] = new FormatLabelWord();
  730. string tmp = "";
  731. Element.words[i].Element = Element;
  732. if (i == words.Length - 1)
  733. {
  734. Element.words[i].Text = word;
  735. tmp = word;
  736. }
  737. else
  738. {
  739. Element.words[i].Text = word + " ";
  740. tmp = word + " "; //last space cant be measured , lets measure an "," instead
  741. }
  742. //SizeF size=g.MeasureString (tmp,Element.Font);
  743. bbuff.Font = GetFont(Element.Font);
  744. Size s = bbuff.MeasureTabbedString(tmp, 0);
  745. Element.words[i].Height = s.Height;
  746. Element.words[i].Width = s.Width - 0;
  747. Element.words[i].ScreenArea = new Rectangle(Element.words[i].ScreenArea.Location,
  748. new Size(Element.words[i].Width, Element.words[i].Height));
  749. // Element.words[i].Link =Element.Link ;
  750. i++;
  751. }
  752. }
  753. }
  754. bbuff.Dispose();
  755. }
  756. private GDIFont GetFont(Font font)
  757. {
  758. GDIFont gf = (GDIFont) _Fonts[GetFontKey(font)];
  759. if (gf == null)
  760. {
  761. gf = new GDIFont(font.Name, font.Size, font.Bold, font.Italic, font.Underline, false);
  762. _Fonts[GetFontKey(font)] = gf;
  763. }
  764. return gf;
  765. }
  766. private string GetFontKey(Font font)
  767. {
  768. return font.Name + font.Bold.ToString() + font.Italic.ToString() + font.Underline.ToString() + font.Size.ToString();
  769. }
  770. private void CreateRows()
  771. {
  772. if (_Elements != null)
  773. {
  774. int x = 0;
  775. _Rows = new ArrayList();
  776. //build rows---------------------------------------------
  777. FormatLabelRow row = new FormatLabelRow();
  778. _Rows.Add(row);
  779. bool WhiteSpace = false;
  780. foreach (FormatLabelElement Element in _Elements)
  781. {
  782. if (Element.words == null)
  783. return;
  784. if (Element.NewLine)
  785. {
  786. //tag forces a new line
  787. x = 0;
  788. row = new FormatLabelRow();
  789. _Rows.Add(row);
  790. WhiteSpace = true;
  791. }
  792. if (Element.TagName == "hr")
  793. {
  794. row.RenderSeparator = true;
  795. }
  796. //else
  797. //{
  798. foreach (FormatLabelWord word in Element.words)
  799. {
  800. if (WordWrap)
  801. {
  802. int scrollwdh = 0;
  803. if (ScrollBars == ScrollBars.Both || ScrollBars == ScrollBars.Vertical)
  804. scrollwdh = vScroll.Width;
  805. if ((word.Width + x) > this.ClientWidth - LabelMargin - scrollwdh)
  806. {
  807. //new line due to wordwrap
  808. x = 0;
  809. row = new FormatLabelRow();
  810. _Rows.Add(row);
  811. WhiteSpace = true;
  812. }
  813. }
  814. if (word.Text.Replace(" ", "") != "" || word.Image != null)
  815. WhiteSpace = false;
  816. if (!WhiteSpace)
  817. {
  818. row.Words.Add(word);
  819. x += word.Width;
  820. }
  821. }
  822. //}
  823. }
  824. //apply width and height to all rows
  825. int index = 0;
  826. foreach (FormatLabelRow r in this._Rows)
  827. {
  828. int width = 0;
  829. int height = 0;
  830. int padd = 0;
  831. if (index > 0)
  832. {
  833. int previndex = index - 1;
  834. FormatLabelRow prev = (FormatLabelRow) _Rows[previndex];
  835. while (previndex >= 0 && prev.Words.Count == 0)
  836. {
  837. prev = (FormatLabelRow) _Rows[previndex];
  838. previndex--;
  839. }
  840. if (previndex >= 0)
  841. {
  842. prev = (FormatLabelRow) _Rows[previndex];
  843. if (prev.Words.Count > 0)
  844. {
  845. FormatLabelWord w = (FormatLabelWord) prev.Words[prev.Words.Count - 1];
  846. height = w.Height;
  847. }
  848. }
  849. }
  850. foreach (FormatLabelWord w in r.Words)
  851. {
  852. if (w.Height > height && (w.Text != ""))
  853. height = w.Height;
  854. width += w.Width;
  855. }
  856. r.Height = height;
  857. int MaxImageH = 0;
  858. foreach (FormatLabelWord w in r.Words)
  859. {
  860. if (w.Image != null)
  861. {
  862. if (w.Height > height)
  863. MaxImageH = w.Height;
  864. }
  865. }
  866. foreach (FormatLabelWord w in r.Words)
  867. {
  868. int imgH = 0;
  869. int imgPadd = 0;
  870. if (w.Image != null)
  871. {
  872. string valign = GetAttrib("valign", w.Element.Tag);
  873. switch (valign)
  874. {
  875. case "top":
  876. {
  877. imgH = r.Height;
  878. imgPadd = w.Height - imgH;
  879. break;
  880. }
  881. case "middle":
  882. case "center":
  883. {
  884. int tmp = 0;
  885. imgH = r.Height;
  886. tmp = (w.Height - imgH)/2;
  887. imgH += tmp;
  888. imgPadd = tmp;
  889. break;
  890. }
  891. case "bottom":
  892. {
  893. imgH = w.Height;
  894. imgPadd = 0;
  895. break;
  896. }
  897. default:
  898. {
  899. imgH = w.Height;
  900. imgPadd = 0;
  901. break;
  902. }
  903. }
  904. if (imgH > height)
  905. height = imgH;
  906. if (imgPadd > padd)
  907. padd = imgPadd;
  908. width += w.Width;
  909. }
  910. }
  911. r.Width = width;
  912. r.Height = height;
  913. r.BottomPadd = padd;
  914. index++;
  915. }
  916. this.vScroll.Maximum = this._Rows.Count;
  917. }
  918. }
  919. private void InitScrollbars()
  920. {
  921. if (vScroll == null || hScroll == null)
  922. return;
  923. if (this.ScrollBars == ScrollBars.Both)
  924. {
  925. vScroll.Left = this.ClientWidth - vScroll.Width;
  926. vScroll.Top = 0;
  927. vScroll.Height = this.ClientHeight - hScroll.Height;
  928. hScroll.Left = 0;
  929. hScroll.Top = this.ClientHeight - hScroll.Height;
  930. hScroll.Width = this.ClientWidth - vScroll.Width;
  931. Filler.Left = vScroll.Left;
  932. Filler.Top = hScroll.Top;
  933. Filler.Visible = true;
  934. vScroll.Visible = true;
  935. hScroll.Visible = true;
  936. }
  937. else if (this.ScrollBars == ScrollBars.Vertical)
  938. {
  939. vScroll.Left = this.ClientWidth - vScroll.Width;
  940. vScroll.Top = 0;
  941. vScroll.Height = this.ClientHeight;
  942. hScroll.Left = 0;
  943. hScroll.Top = this.ClientHeight - hScroll.Height;
  944. hScroll.Width = this.ClientWidth - vScroll.Width;
  945. Filler.Left = vScroll.Left;
  946. Filler.Top = hScroll.Top;
  947. Filler.Visible = false;
  948. vScroll.Visible = true;
  949. hScroll.Visible = false;
  950. }
  951. else if (this.ScrollBars == ScrollBars.Horizontal)
  952. {
  953. vScroll.Left = this.ClientWidth - vScroll.Width;
  954. vScroll.Top = 0;
  955. vScroll.Height = this.ClientHeight;
  956. hScroll.Left = 0;
  957. hScroll.Top = this.ClientHeight - hScroll.Height;
  958. hScroll.Width = this.ClientWidth;
  959. Filler.Left = vScroll.Left;
  960. Filler.Top = hScroll.Top;
  961. Filler.Visible = false;
  962. vScroll.Visible = false;
  963. hScroll.Visible = true;
  964. }
  965. else if (this.ScrollBars == ScrollBars.None)
  966. {
  967. vScroll.Left = this.ClientWidth - vScroll.Width;
  968. vScroll.Top = 0;
  969. vScroll.Height = this.ClientHeight;
  970. hScroll.Left = 0;
  971. hScroll.Top = this.ClientHeight - hScroll.Height;
  972. hScroll.Width = this.ClientWidth;
  973. Filler.Left = vScroll.Left;
  974. Filler.Top = hScroll.Top;
  975. Filler.Visible = false;
  976. vScroll.Visible = false;
  977. hScroll.Visible = false;
  978. }
  979. }
  980. protected override void OnResize(EventArgs e)
  981. {
  982. try
  983. {
  984. InitScrollbars();
  985. SetAutoSize();
  986. }
  987. catch
  988. {
  989. }
  990. CreateRows();
  991. base.OnResize(e);
  992. }
  993. protected override void OnSizeChanged(EventArgs e)
  994. {
  995. base.OnSizeChanged(e);
  996. if (this.IsHandleCreated)
  997. RedrawBuffer();
  998. }
  999. protected override void OnMouseUp(MouseEventArgs e)
  1000. {
  1001. int y = e.Y;
  1002. int x = e.X;
  1003. int index = 0;
  1004. bool Link = false;
  1005. //this.Cursor =Cursors.Arrow;
  1006. _ActiveElement = null;
  1007. if (this._Rows != null)
  1008. {
  1009. foreach (FormatLabelRow r in this._Rows)
  1010. {
  1011. if (y >= r.Top && y <= r.Top + r.Height)
  1012. {
  1013. foreach (FormatLabelWord w in r.Words)
  1014. {
  1015. if (y >= w.ScreenArea.Top && y <= w.ScreenArea.Bottom)
  1016. {
  1017. if (x >= w.ScreenArea.Left && x <= w.ScreenArea.Right)
  1018. {
  1019. //MessageBox.Show (w.Text);
  1020. if (w.Element.Link != null)
  1021. {
  1022. Link = true;
  1023. _ActiveElement = w.Element.Link;
  1024. break;
  1025. }
  1026. //this.Cursor =Cursors.Hand;
  1027. }
  1028. }
  1029. }
  1030. break;
  1031. }
  1032. index++;
  1033. }
  1034. }
  1035. if (Link)
  1036. {
  1037. this.Cursor = Cursors.Hand;
  1038. this.Invalidate();
  1039. OnClickLink(GetAttrib("href", _ActiveElement.Tag));
  1040. }
  1041. else
  1042. {
  1043. this.Cursor = Cursors.Arrow;
  1044. this.Invalidate();
  1045. }
  1046. base.OnMouseUp(e);
  1047. }
  1048. protected override void OnMouseMove(MouseEventArgs e)
  1049. {
  1050. int y = e.Y;
  1051. int x = e.X;
  1052. int index = 0;
  1053. bool Link = false;
  1054. //this.Cursor =Cursors.Arrow;
  1055. _ActiveElement = null;
  1056. if (this._Rows != null)
  1057. {
  1058. foreach (FormatLabelRow r in this._Rows)
  1059. {
  1060. if (y >= r.Top && y <= r.Top + r.Height)
  1061. {
  1062. foreach (FormatLabelWord w in r.Words)
  1063. {
  1064. if (y >= w.ScreenArea.Top && y <= w.ScreenArea.Bottom)
  1065. {
  1066. if (x >= w.ScreenArea.Left && x <= w.ScreenArea.Right)
  1067. {
  1068. //MessageBox.Show (w.Text);
  1069. if (w.Element.Link != null)
  1070. {
  1071. Link = true;
  1072. _ActiveElement = w.Element.Link;
  1073. break;
  1074. }
  1075. //this.Cursor =Cursors.Hand;
  1076. }
  1077. }
  1078. }
  1079. break;
  1080. }
  1081. index++;
  1082. }
  1083. }
  1084. if (Link)
  1085. {
  1086. this.Cursor = Cursors.Hand;
  1087. this.Invalidate();
  1088. }
  1089. else
  1090. {
  1091. this.Cursor = Cursors.Arrow;
  1092. this.Invalidate();
  1093. }
  1094. base.OnMouseMove(e);
  1095. }
  1096. private void vScroll_Scroll(object sender, ScrollEventArgs e)
  1097. {
  1098. this.Invalidate();
  1099. }
  1100. public ScrollBars ScrollBars
  1101. {
  1102. get { return _ScrollBars; }
  1103. set
  1104. {
  1105. _ScrollBars = value;
  1106. InitScrollbars();
  1107. }
  1108. }
  1109. public int GetWidth()
  1110. {
  1111. int max = 0;
  1112. foreach (FormatLabelRow r in this._Rows)
  1113. {
  1114. if (r.Width > max)
  1115. max = r.Width;
  1116. }
  1117. return max + LabelMargin*2 + this.BorderWidth*2;
  1118. }
  1119. public int GetHeight()
  1120. {
  1121. int max = 0;
  1122. foreach (FormatLabelRow r in this._Rows)
  1123. {
  1124. max += r.Height;
  1125. }
  1126. return max + LabelMargin*2 + this.BorderWidth*2;
  1127. }
  1128. }
  1129. }