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