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