Set eol style
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ToolStripLabel.cs
1 //
2 // ToolStripLabel.cs
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 //
23 // Copyright (c) 2006 Jonathan Pobst
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28 #if NET_2_0
29
30 using System.Drawing;
31
32 namespace System.Windows.Forms
33 {
34         public class ToolStripLabel : ToolStripItem
35         {
36                 private Color active_link_color;
37                 private bool is_link;
38                 private LinkBehavior link_behavior;
39                 private Color link_color;
40                 private bool link_visited;
41                 private Color visited_link_color;
42
43                 #region Public Constructors
44                 public ToolStripLabel ()
45                         : this (String.Empty, null, false, null, String.Empty)
46                 {
47                 }
48
49                 public ToolStripLabel (Image image)
50                         : this (String.Empty, image, false, null, String.Empty)
51                 {
52                 }
53
54                 public ToolStripLabel (string text)
55                         : this (text, null, false, null, String.Empty)
56                 {
57                 }
58
59                 public ToolStripLabel (string text, Image image)
60                         : this (text, image, false, null, String.Empty)
61                 {
62                 }
63
64                 public ToolStripLabel (string text, Image image, bool isLink)
65                         : this (text, image, isLink, null, String.Empty)
66                 {
67                 }
68
69                 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
70                         : this (text, image, isLink, onClick, String.Empty)
71                 {
72                 }
73
74                 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
75                         : base (text, image, onClick, name)
76                 {
77                         this.active_link_color = Color.Red;
78                         this.is_link = isLink;
79                         this.link_behavior = LinkBehavior.SystemDefault;
80                         this.link_color = Color.Blue;
81                         this.link_visited = false;
82                         this.visited_link_color = Color.Purple;
83                 }
84                 #endregion
85
86                 #region Public Properties
87                 public Color ActiveLinkColor {
88                         get { return this.active_link_color; }
89                         set {
90                                 this.active_link_color = value; 
91                                 this.Invalidate ();
92                         }
93                 }
94
95                 public override bool CanSelect { get { return false; } }
96                 
97                 public bool IsLink {
98                         get { return this.is_link; }
99                         set {
100                                 this.is_link = value; 
101                                 this.Invalidate ();
102                         }
103                 }
104
105                 public LinkBehavior LinkBehavior {
106                         get { return this.link_behavior; }
107                         set {
108                                 this.link_behavior = value; 
109                                 this.Invalidate ();
110                         }
111                 }
112
113                 public Color LinkColor {
114                         get { return this.link_color; }
115                         set {
116                                 this.link_color = value; 
117                                 this.Invalidate ();
118                         }
119                 }
120
121                 public bool LinkVisited {
122                         get { return this.link_visited; }
123                         set {
124                                 this.link_visited = value; 
125                                 this.Invalidate ();
126                         }
127                 }
128
129                 public Color VisitedLinkColor
130                 {
131                         get { return this.visited_link_color; }
132                         set {
133                                 this.visited_link_color = value; 
134                                 this.Invalidate ();
135                         }
136                 }
137                 #endregion
138
139                 #region Protected Methods
140                 protected override void OnFontChanged (EventArgs e)
141                 {
142                         base.OnFontChanged (e);
143                 }
144
145                 protected override void OnMouseEnter (EventArgs e)
146                 {
147                         base.OnMouseEnter (e);
148                 }
149
150                 protected override void OnMouseLeave (EventArgs e)
151                 {
152                         base.OnMouseLeave (e);
153                 }
154                 
155                 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
156                 {
157                         base.OnPaint (e);
158
159                         if (this.Owner != null) {
160                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
161                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
162
163                                 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
164
165                                 Rectangle text_layout_rect;
166                                 Rectangle image_layout_rect;
167
168                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
169
170                                 if (image_layout_rect != Rectangle.Empty)
171                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
172                                 if (text_layout_rect != Rectangle.Empty)
173                                         if (this.is_link) {
174                                                 if (this.Pressed)               // Mouse Down
175                                                 {
176                                                         switch (this.link_behavior) {
177                                                                 case LinkBehavior.SystemDefault:
178                                                                 case LinkBehavior.AlwaysUnderline:
179                                                                 case LinkBehavior.HoverUnderline:
180                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.active_link_color, new Font (this.Font, FontStyle.Underline), this.TextAlign));
181                                                                         break;
182                                                                 case LinkBehavior.NeverUnderline:
183                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.active_link_color, this.Font, this.TextAlign));
184                                                                         break;
185                                                         }
186                                                 }
187                                                 else if (this.Selected)         // Hover
188                                                 {
189                                                         switch (this.link_behavior) {
190                                                                 case LinkBehavior.SystemDefault:
191                                                                 case LinkBehavior.AlwaysUnderline:
192                                                                 case LinkBehavior.HoverUnderline:
193                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, new Font (this.Font, FontStyle.Underline), this.TextAlign));
194                                                                         break;
195                                                                 case LinkBehavior.NeverUnderline:
196                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
197                                                                         break;
198                                                         }
199                                                 }
200                                                 else {
201
202                                                         if (this.link_visited)          // Normal, Visited
203                                                         {
204                                                                 switch (this.link_behavior) {
205                                                                         case LinkBehavior.SystemDefault:
206                                                                         case LinkBehavior.AlwaysUnderline:
207                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.visited_link_color, new Font (this.Font, FontStyle.Underline), this.TextAlign));
208                                                                                 break;
209                                                                         case LinkBehavior.NeverUnderline:
210                                                                         case LinkBehavior.HoverUnderline:
211                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.visited_link_color, this.Font, this.TextAlign));
212                                                                                 break;
213                                                                 }
214                                                         }
215                                                         else                            // Normal
216                                                         {
217                                                                 switch (this.link_behavior) {
218                                                                         case LinkBehavior.SystemDefault:
219                                                                         case LinkBehavior.AlwaysUnderline:
220                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, new Font (this.Font, FontStyle.Underline), this.TextAlign));
221                                                                                 break;
222                                                                         case LinkBehavior.NeverUnderline:
223                                                                         case LinkBehavior.HoverUnderline:
224                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
225                                                                                 break;
226                                                                 }
227
228                                                         }
229                                                 }
230                                         } else
231                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
232                         }
233                 }
234                 #endregion
235         }
236 }
237 #endif