2007-06-04 Jonathan Pobst <monkey@jpobst.com>
[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 Public Constructors
47                 public ToolStripLabel ()
48                         : this (null, null, false, null, String.Empty)
49                 {
50                 }
51
52                 public ToolStripLabel (Image image)
53                         : this (null, image, false, null, String.Empty)
54                 {
55                 }
56
57                 public ToolStripLabel (string text)
58                         : this (text, null, false, null, String.Empty)
59                 {
60                 }
61
62                 public ToolStripLabel (string text, Image image)
63                         : this (text, image, false, null, String.Empty)
64                 {
65                 }
66
67                 public ToolStripLabel (string text, Image image, bool isLink)
68                         : this (text, image, isLink, null, String.Empty)
69                 {
70                 }
71
72                 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
73                         : this (text, image, isLink, onClick, String.Empty)
74                 {
75                 }
76
77                 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
78                         : base (text, image, onClick, name)
79                 {
80                         this.active_link_color = Color.Red;
81                         this.is_link = isLink;
82                         this.link_behavior = LinkBehavior.SystemDefault;
83                         this.link_color = Color.FromArgb (0, 0, 255);
84                         this.link_visited = false;
85                         this.visited_link_color = Color.FromArgb (128, 0, 128);
86                 }
87                 #endregion
88
89                 #region Public Properties
90                 public Color ActiveLinkColor {
91                         get { return this.active_link_color; }
92                         set {
93                                 this.active_link_color = value; 
94                                 this.Invalidate ();
95                         }
96                 }
97
98                 public override bool CanSelect { get { return false; } }
99                 
100                 [DefaultValue (false)]
101                 public bool IsLink {
102                         get { return this.is_link; }
103                         set {
104                                 this.is_link = value; 
105                                 this.Invalidate ();
106                         }
107                 }
108
109                 [DefaultValue (LinkBehavior.SystemDefault)]
110                 public LinkBehavior LinkBehavior {
111                         get { return this.link_behavior; }
112                         set {
113                                 this.link_behavior = value; 
114                                 this.Invalidate ();
115                         }
116                 }
117
118                 public Color LinkColor {
119                         get { return this.link_color; }
120                         set {
121                                 this.link_color = value; 
122                                 this.Invalidate ();
123                         }
124                 }
125
126                 [DefaultValue (false)]
127                 public bool LinkVisited {
128                         get { return this.link_visited; }
129                         set {
130                                 this.link_visited = value; 
131                                 this.Invalidate ();
132                         }
133                 }
134
135                 public Color VisitedLinkColor
136                 {
137                         get { return this.visited_link_color; }
138                         set {
139                                 this.visited_link_color = value; 
140                                 this.Invalidate ();
141                         }
142                 }
143                 #endregion
144
145                 #region Protected Methods
146                 [EditorBrowsable (EditorBrowsableState.Advanced)]
147                 protected override AccessibleObject CreateAccessibilityInstance ()
148                 {
149                         ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
150
151                         ao.role = AccessibleRole.StaticText;
152                         ao.state = AccessibleStates.ReadOnly;   
153
154                         return ao;
155                 }
156                 
157                 protected override void OnFontChanged (EventArgs e)
158                 {
159                         base.OnFontChanged (e);
160                 }
161
162                 protected override void OnMouseEnter (EventArgs e)
163                 {
164                         base.OnMouseEnter (e);
165                 }
166
167                 protected override void OnMouseLeave (EventArgs e)
168                 {
169                         base.OnMouseLeave (e);
170                 }
171                 
172                 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
173                 {
174                         base.OnPaint (e);
175
176                         if (this.Owner != null) {
177                                 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
178                                 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
179
180                                 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
181
182                                 Rectangle text_layout_rect;
183                                 Rectangle image_layout_rect;
184
185                                 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
186
187                                 if (image_layout_rect != Rectangle.Empty)
188                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
189                                 if (text_layout_rect != Rectangle.Empty)
190                                         if (this.is_link) {
191                                                 if (this.Pressed)               // Mouse Down
192                                                 {
193                                                         switch (this.link_behavior) {
194                                                                 case LinkBehavior.SystemDefault:
195                                                                 case LinkBehavior.AlwaysUnderline:
196                                                                 case LinkBehavior.HoverUnderline:
197                                                                         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));
198                                                                         break;
199                                                                 case LinkBehavior.NeverUnderline:
200                                                                         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));
201                                                                         break;
202                                                         }
203                                                 }
204                                                 else if (this.Selected)         // Hover
205                                                 {
206                                                         switch (this.link_behavior) {
207                                                                 case LinkBehavior.SystemDefault:
208                                                                 case LinkBehavior.AlwaysUnderline:
209                                                                 case LinkBehavior.HoverUnderline:
210                                                                         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));
211                                                                         break;
212                                                                 case LinkBehavior.NeverUnderline:
213                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
214                                                                         break;
215                                                         }
216                                                 }
217                                                 else {
218
219                                                         if (this.link_visited)          // Normal, Visited
220                                                         {
221                                                                 switch (this.link_behavior) {
222                                                                         case LinkBehavior.SystemDefault:
223                                                                         case LinkBehavior.AlwaysUnderline:
224                                                                                 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));
225                                                                                 break;
226                                                                         case LinkBehavior.NeverUnderline:
227                                                                         case LinkBehavior.HoverUnderline:
228                                                                                 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));
229                                                                                 break;
230                                                                 }
231                                                         }
232                                                         else                            // Normal
233                                                         {
234                                                                 switch (this.link_behavior) {
235                                                                         case LinkBehavior.SystemDefault:
236                                                                         case LinkBehavior.AlwaysUnderline:
237                                                                                 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));
238                                                                                 break;
239                                                                         case LinkBehavior.NeverUnderline:
240                                                                         case LinkBehavior.HoverUnderline:
241                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
242                                                                                 break;
243                                                                 }
244
245                                                         }
246                                                 }
247                                         } else
248                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
249                         }
250                 }
251
252                 protected internal override bool ProcessMnemonic (char charCode)
253                 {
254                         this.Parent.SelectNextToolStripItem (this, true);
255                         return true;
256                 }
257                 #endregion
258         }
259 }
260 #endif