Fix bug #395
[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
29 using System.Drawing;
30 using System.ComponentModel;
31 using System.Windows.Forms.Design;
32
33 namespace System.Windows.Forms
34 {
35         [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip)]
36         public class ToolStripLabel : ToolStripItem
37         {
38                 private Color active_link_color;
39                 private bool is_link;
40                 private LinkBehavior link_behavior;
41                 private Color link_color;
42                 private bool link_visited;
43                 private Color visited_link_color;
44
45                 #region UIA FrameWork Events
46                 static object UIAIsLinkChangedEvent = new object ();
47
48                 internal event EventHandler UIAIsLinkChanged {
49                         add { Events.AddHandler (UIAIsLinkChangedEvent, value); }
50                         remove { Events.RemoveHandler (UIAIsLinkChangedEvent, value); }
51                 }
52
53                 internal void OnUIAIsLinkChanged (EventArgs e)
54                 {
55                         EventHandler eh = (EventHandler) Events [UIAIsLinkChangedEvent];
56                         if (eh != null)
57                                 eh (this, e);
58                 }
59                 #endregion
60
61                 #region Public Constructors
62                 public ToolStripLabel ()
63                         : this (null, null, false, null, String.Empty)
64                 {
65                 }
66
67                 public ToolStripLabel (Image image)
68                         : this (null, image, false, null, String.Empty)
69                 {
70                 }
71
72                 public ToolStripLabel (string text)
73                         : this (text, null, false, null, String.Empty)
74                 {
75                 }
76
77                 public ToolStripLabel (string text, Image image)
78                         : this (text, image, false, null, String.Empty)
79                 {
80                 }
81
82                 public ToolStripLabel (string text, Image image, bool isLink)
83                         : this (text, image, isLink, null, String.Empty)
84                 {
85                 }
86
87                 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
88                         : this (text, image, isLink, onClick, String.Empty)
89                 {
90                 }
91
92                 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
93                         : base (text, image, onClick, name)
94                 {
95                         this.active_link_color = Color.Red;
96                         this.is_link = isLink;
97                         this.link_behavior = LinkBehavior.SystemDefault;
98                         this.link_color = Color.FromArgb (0, 0, 255);
99                         this.link_visited = false;
100                         this.visited_link_color = Color.FromArgb (128, 0, 128);
101                 }
102                 #endregion
103
104                 #region Public Properties
105                 public Color ActiveLinkColor {
106                         get { return this.active_link_color; }
107                         set {
108                                 this.active_link_color = value; 
109                                 this.Invalidate ();
110                         }
111                 }
112
113                 public override bool CanSelect { get { return false; } }
114                 
115                 [DefaultValue (false)]
116                 public bool IsLink {
117                         get { return this.is_link; }
118                         set {
119                                 this.is_link = value; 
120                                 this.Invalidate ();
121
122                                 // UIA Framework Event: IsLink Changed
123                                 OnUIAIsLinkChanged (EventArgs.Empty);
124                         }
125                 }
126
127                 [DefaultValue (LinkBehavior.SystemDefault)]
128                 public LinkBehavior LinkBehavior {
129                         get { return this.link_behavior; }
130                         set {
131                                 this.link_behavior = value; 
132                                 this.Invalidate ();
133                         }
134                 }
135
136                 public Color LinkColor {
137                         get { return this.link_color; }
138                         set {
139                                 this.link_color = value; 
140                                 this.Invalidate ();
141                         }
142                 }
143
144                 [DefaultValue (false)]
145                 public bool LinkVisited {
146                         get { return this.link_visited; }
147                         set {
148                                 this.link_visited = value; 
149                                 this.Invalidate ();
150                         }
151                 }
152
153                 public Color VisitedLinkColor
154                 {
155                         get { return this.visited_link_color; }
156                         set {
157                                 this.visited_link_color = value; 
158                                 this.Invalidate ();
159                         }
160                 }
161                 #endregion
162
163                 #region Protected Methods
164                 [EditorBrowsable (EditorBrowsableState.Advanced)]
165                 protected override AccessibleObject CreateAccessibilityInstance ()
166                 {
167                         ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
168
169                         ao.role = AccessibleRole.StaticText;
170                         ao.state = AccessibleStates.ReadOnly;   
171
172                         return ao;
173                 }
174                 
175                 protected override void OnFontChanged (EventArgs e)
176                 {
177                         base.OnFontChanged (e);
178                 }
179
180                 protected override void OnMouseEnter (EventArgs e)
181                 {
182                         base.OnMouseEnter (e);
183                 }
184
185                 protected override void OnMouseLeave (EventArgs e)
186                 {
187                         base.OnMouseLeave (e);
188                 }
189                 
190                 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
191                 {
192                         if (this.Owner != null) {
193                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
194                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
195
196                                 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
197
198                                 Rectangle text_layout_rect;
199                                 Rectangle image_layout_rect;
200
201                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
202
203                                 if (this.IsOnDropDown) {
204                                         if (this.ShowMargin)
205                                                 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
206                                         else
207                                                 text_layout_rect = new Rectangle (7, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
208                                         if (image_layout_rect != Rectangle.Empty)
209                                                 image_layout_rect = new Rectangle (new Point (4, 3), base.GetImageSize ());
210                                 }
211
212                                 if (image_layout_rect != Rectangle.Empty)
213                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
214                                 if (text_layout_rect != Rectangle.Empty)
215                                         if (this.is_link) {
216                                                 if (this.Pressed)               // Mouse Down
217                                                 {
218                                                         switch (this.link_behavior) {
219                                                                 case LinkBehavior.SystemDefault:
220                                                                 case LinkBehavior.AlwaysUnderline:
221                                                                 case LinkBehavior.HoverUnderline:
222                                                                         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));
223                                                                         break;
224                                                                 case LinkBehavior.NeverUnderline:
225                                                                         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));
226                                                                         break;
227                                                         }
228                                                 }
229                                                 else if (this.Selected)         // Hover
230                                                 {
231                                                         switch (this.link_behavior) {
232                                                                 case LinkBehavior.SystemDefault:
233                                                                 case LinkBehavior.AlwaysUnderline:
234                                                                 case LinkBehavior.HoverUnderline:
235                                                                         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));
236                                                                         break;
237                                                                 case LinkBehavior.NeverUnderline:
238                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
239                                                                         break;
240                                                         }
241                                                 }
242                                                 else {
243
244                                                         if (this.link_visited)          // Normal, Visited
245                                                         {
246                                                                 switch (this.link_behavior) {
247                                                                         case LinkBehavior.SystemDefault:
248                                                                         case LinkBehavior.AlwaysUnderline:
249                                                                                 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));
250                                                                                 break;
251                                                                         case LinkBehavior.NeverUnderline:
252                                                                         case LinkBehavior.HoverUnderline:
253                                                                                 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));
254                                                                                 break;
255                                                                 }
256                                                         }
257                                                         else                            // Normal
258                                                         {
259                                                                 switch (this.link_behavior) {
260                                                                         case LinkBehavior.SystemDefault:
261                                                                         case LinkBehavior.AlwaysUnderline:
262                                                                                 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));
263                                                                                 break;
264                                                                         case LinkBehavior.NeverUnderline:
265                                                                         case LinkBehavior.HoverUnderline:
266                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
267                                                                                 break;
268                                                                 }
269
270                                                         }
271                                                 }
272                                         } else
273                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
274                         }
275
276                         // call Paint handlers last.
277                         base.OnPaint (e);
278                 }
279
280                 protected internal override bool ProcessMnemonic (char charCode)
281                 {
282                         this.Parent.SelectNextToolStripItem (this, true);
283                         return true;
284                 }
285                 #endregion
286         }
287 }