LinkLabelPainter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2007 Novell, Inc.
  21. //
  22. // Authors:
  23. // Everaldo Canuto ([email protected])
  24. using System;
  25. using System.Drawing;
  26. using System.Drawing.Drawing2D;
  27. namespace System.Windows.Forms.Theming.Default
  28. {
  29. internal class LinkLabelPainter
  30. {
  31. public LinkLabelPainter ()
  32. {
  33. }
  34. private Color GetPieceColor (LinkLabel label, LinkLabel.Piece piece, int i)
  35. {
  36. if (!label.Enabled)
  37. return label.DisabledLinkColor;
  38. if (piece.link == null)
  39. return label.ForeColor;
  40. if (!piece.link.Enabled)
  41. return label.DisabledLinkColor;
  42. if (piece.link.Active)
  43. return label.ActiveLinkColor;
  44. if ((label.LinkVisited && i == 0) || piece.link.Visited)
  45. return label.VisitedLinkColor;
  46. return label.LinkColor;
  47. }
  48. public virtual void Draw (Graphics dc, Rectangle clip_rectangle, LinkLabel label)
  49. {
  50. Rectangle client_rect = label.PaddingClientRectangle;
  51. label.DrawImage (dc, label.Image, client_rect, label.ImageAlign);
  52. if (label.pieces == null)
  53. return;
  54. // Paint all text as disabled.
  55. if (!label.Enabled) {
  56. dc.SetClip (clip_rectangle);
  57. ThemeEngine.Current.CPDrawStringDisabled (
  58. dc, label.Text, label.Font, label.BackColor, client_rect, label.string_format);
  59. return;
  60. }
  61. Font font, link_font = ThemeEngine.Current.GetLinkFont (label);
  62. Region text_region = new Region (new Rectangle());
  63. // Draw links.
  64. for (int i = 0; i < label.pieces.Length; i ++) {
  65. LinkLabel.Piece piece = label.pieces[i];
  66. if (piece.link == null) {
  67. text_region.Union (piece.region);
  68. continue;
  69. }
  70. Color color = GetPieceColor (label, piece, i);
  71. if ( (label.LinkBehavior == LinkBehavior.AlwaysUnderline) ||
  72. (label.LinkBehavior == LinkBehavior.SystemDefault) ||
  73. ((label.LinkBehavior == LinkBehavior.HoverUnderline) && piece.link.Hovered) )
  74. font = link_font;
  75. else
  76. font = label.Font;
  77. dc.Clip = piece.region;
  78. dc.Clip.Intersect (clip_rectangle);
  79. dc.DrawString (label.Text, font,
  80. ThemeEngine.Current.ResPool.GetSolidBrush (color),
  81. client_rect, label.string_format);
  82. // Draw focus rectangle
  83. if ((piece.link != null) && piece.link.Focused) {
  84. foreach (RectangleF rect in piece.region.GetRegionScans (dc.Transform))
  85. ControlPaint.DrawFocusRectangle (dc, Rectangle.Round (rect), label.ForeColor, label.BackColor);
  86. }
  87. }
  88. // Draw normal text (without links).
  89. if (!text_region.IsEmpty (dc)) {
  90. dc.Clip = text_region;
  91. dc.Clip.Intersect (clip_rectangle);
  92. if (!dc.Clip.IsEmpty (dc))
  93. dc.DrawString(label.Text, label.Font,
  94. ThemeEngine.Current.ResPool.GetSolidBrush(label.ForeColor),
  95. client_rect, label.string_format);
  96. }
  97. }
  98. }
  99. }