Merge branch 'master' of http://github.com/mono/mono
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.Theming / Default / LinkLabelPainter.cs
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 (ecanuto@novell.com)
24
25 using System;
26 using System.Drawing;
27 using System.Drawing.Drawing2D;
28
29 namespace System.Windows.Forms.Theming.Default
30 {
31         internal class LinkLabelPainter
32         {
33                 public LinkLabelPainter ()
34                 {
35                 }
36
37                 private Color GetPieceColor (LinkLabel label, LinkLabel.Piece piece, int i)
38                 {
39                         if (!label.Enabled)
40                                 return label.DisabledLinkColor;
41
42                         if (piece.link == null)
43                                 return label.ForeColor;
44
45                         if (!piece.link.Enabled)
46                                 return label.DisabledLinkColor;
47                                 
48                         if (piece.link.Active)
49                                 return label.ActiveLinkColor;
50                                 
51                         if ((label.LinkVisited && i == 0) || piece.link.Visited)
52                                 return label.VisitedLinkColor;
53                         
54                         return label.LinkColor;
55                 }
56                 
57                 public virtual void Draw (Graphics dc, Rectangle clip_rectangle, LinkLabel label)
58                 {
59                         Rectangle client_rect = label.PaddingClientRectangle;
60
61                         label.DrawImage (dc, label.Image, client_rect, label.ImageAlign);
62
63                         if (label.pieces == null)
64                                 return;
65
66                         // Paint all text as disabled.
67                         if (!label.Enabled) {
68                                 dc.SetClip (clip_rectangle);
69                                 ThemeEngine.Current.CPDrawStringDisabled (
70                                         dc, label.Text, label.Font, label.BackColor, client_rect, label.string_format);
71                                 return;
72                         }
73
74                         Font font, link_font = ThemeEngine.Current.GetLinkFont (label);
75                         
76                         Region text_region = new Region (new Rectangle());
77
78                         // Draw links.
79                         for (int i = 0; i < label.pieces.Length; i ++) {
80                                 LinkLabel.Piece piece = label.pieces[i];
81                                 
82                                 if (piece.link == null) {
83                                         text_region.Union (piece.region);
84                                         continue;
85                                 }
86
87                                 Color color = GetPieceColor (label, piece, i);
88
89                                 if ( (label.LinkBehavior == LinkBehavior.AlwaysUnderline) || 
90                                          (label.LinkBehavior == LinkBehavior.SystemDefault) ||
91                                          ((label.LinkBehavior == LinkBehavior.HoverUnderline) && piece.link.Hovered) )
92                                         font = link_font;
93                                 else
94                                         font = label.Font;
95                                 
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);
101                         
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);
106                                 }
107                         }
108                         
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);
117                         }
118                 }
119         }
120 }