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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2007 Novell, Inc.
23 // Everaldo Canuto (ecanuto@novell.com)
27 using System.Drawing.Drawing2D;
29 namespace System.Windows.Forms.Theming.Default
31 internal class LinkLabelPainter
33 public LinkLabelPainter ()
37 private Color GetPieceColor (LinkLabel label, LinkLabel.Piece piece, int i)
40 return label.DisabledLinkColor;
42 if (piece.link == null)
43 return label.ForeColor;
45 if (!piece.link.Enabled)
46 return label.DisabledLinkColor;
48 if (piece.link.Active)
49 return label.ActiveLinkColor;
51 if ((label.LinkVisited && i == 0) || piece.link.Visited)
52 return label.VisitedLinkColor;
54 return label.LinkColor;
57 public virtual void Draw (Graphics dc, Rectangle clip_rectangle, LinkLabel label)
59 Rectangle client_rect = label.PaddingClientRectangle;
61 label.DrawImage (dc, label.Image, client_rect, label.ImageAlign);
63 if (label.pieces == null)
66 // Paint all text as disabled.
68 dc.SetClip (clip_rectangle);
69 ThemeEngine.Current.CPDrawStringDisabled (
70 dc, label.Text, label.Font, label.BackColor, client_rect, label.string_format);
74 Font font, link_font = ThemeEngine.Current.GetLinkFont (label);
76 Region text_region = new Region (new Rectangle());
79 for (int i = 0; i < label.pieces.Length; i ++) {
80 LinkLabel.Piece piece = label.pieces[i];
82 if (piece.link == null) {
83 text_region.Union (piece.region);
87 Color color = GetPieceColor (label, piece, i);
89 if ( (label.LinkBehavior == LinkBehavior.AlwaysUnderline) ||
90 (label.LinkBehavior == LinkBehavior.SystemDefault) ||
91 ((label.LinkBehavior == LinkBehavior.HoverUnderline) && piece.link.Hovered) )
96 dc.Clip = piece.region;
97 dc.Clip.Intersect (clip_rectangle);
98 dc.DrawString (label.Text, font,
99 ThemeEngine.Current.ResPool.GetSolidBrush (color),
100 client_rect, label.string_format);
102 // Draw focus rectangle
103 if ((piece.link != null) && piece.link.Focused) {
104 foreach (RectangleF rect in piece.region.GetRegionScans (dc.Transform))
105 ControlPaint.DrawFocusRectangle (dc, Rectangle.Round (rect), label.ForeColor, label.BackColor);
109 // Draw normal text (without links).
110 if (!text_region.IsEmpty (dc)) {
111 dc.Clip = text_region;
112 dc.Clip.Intersect (clip_rectangle);
113 if (!dc.Clip.IsEmpty (dc))
114 dc.DrawString(label.Text, label.Font,
115 ThemeEngine.Current.ResPool.GetSolidBrush(label.ForeColor),
116 client_rect, label.string_format);