* ListView.cs: When doing layout calculations don't use a ref
[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 (this.IsOnDropDown) {
188                                         if (this.ShowMargin)
189                                                 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
190                                         else
191                                                 text_layout_rect = new Rectangle (7, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
192                                         if (image_layout_rect != Rectangle.Empty)
193                                                 image_layout_rect = new Rectangle (new Point (4, 3), base.GetImageSize ());
194                                 }
195
196                                 if (image_layout_rect != Rectangle.Empty)
197                                         this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
198                                 if (text_layout_rect != Rectangle.Empty)
199                                         if (this.is_link) {
200                                                 if (this.Pressed)               // Mouse Down
201                                                 {
202                                                         switch (this.link_behavior) {
203                                                                 case LinkBehavior.SystemDefault:
204                                                                 case LinkBehavior.AlwaysUnderline:
205                                                                 case LinkBehavior.HoverUnderline:
206                                                                         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));
207                                                                         break;
208                                                                 case LinkBehavior.NeverUnderline:
209                                                                         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));
210                                                                         break;
211                                                         }
212                                                 }
213                                                 else if (this.Selected)         // Hover
214                                                 {
215                                                         switch (this.link_behavior) {
216                                                                 case LinkBehavior.SystemDefault:
217                                                                 case LinkBehavior.AlwaysUnderline:
218                                                                 case LinkBehavior.HoverUnderline:
219                                                                         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));
220                                                                         break;
221                                                                 case LinkBehavior.NeverUnderline:
222                                                                         this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
223                                                                         break;
224                                                         }
225                                                 }
226                                                 else {
227
228                                                         if (this.link_visited)          // Normal, Visited
229                                                         {
230                                                                 switch (this.link_behavior) {
231                                                                         case LinkBehavior.SystemDefault:
232                                                                         case LinkBehavior.AlwaysUnderline:
233                                                                                 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));
234                                                                                 break;
235                                                                         case LinkBehavior.NeverUnderline:
236                                                                         case LinkBehavior.HoverUnderline:
237                                                                                 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));
238                                                                                 break;
239                                                                 }
240                                                         }
241                                                         else                            // Normal
242                                                         {
243                                                                 switch (this.link_behavior) {
244                                                                         case LinkBehavior.SystemDefault:
245                                                                         case LinkBehavior.AlwaysUnderline:
246                                                                                 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));
247                                                                                 break;
248                                                                         case LinkBehavior.NeverUnderline:
249                                                                         case LinkBehavior.HoverUnderline:
250                                                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
251                                                                                 break;
252                                                                 }
253
254                                                         }
255                                                 }
256                                         } else
257                                                 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
258                         }
259                 }
260
261                 protected internal override bool ProcessMnemonic (char charCode)
262                 {
263                         this.Parent.SelectNextToolStripItem (this, true);
264                         return true;
265                 }
266                 #endregion
267         }
268 }
269 #endif