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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Jonathan Pobst
26 // Jonathan Pobst (monkey@jpobst.com)
31 using System.ComponentModel;
32 using System.Windows.Forms.Design;
34 namespace System.Windows.Forms
36 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip)]
37 public class ToolStripLabel : ToolStripItem
39 private Color active_link_color;
41 private LinkBehavior link_behavior;
42 private Color link_color;
43 private bool link_visited;
44 private Color visited_link_color;
46 #region Public Constructors
47 public ToolStripLabel ()
48 : this (null, null, false, null, String.Empty)
52 public ToolStripLabel (Image image)
53 : this (null, image, false, null, String.Empty)
57 public ToolStripLabel (string text)
58 : this (text, null, false, null, String.Empty)
62 public ToolStripLabel (string text, Image image)
63 : this (text, image, false, null, String.Empty)
67 public ToolStripLabel (string text, Image image, bool isLink)
68 : this (text, image, isLink, null, String.Empty)
72 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
73 : this (text, image, isLink, onClick, String.Empty)
77 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
78 : base (text, image, onClick, name)
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);
89 #region Public Properties
90 public Color ActiveLinkColor {
91 get { return this.active_link_color; }
93 this.active_link_color = value;
98 public override bool CanSelect { get { return false; } }
100 [DefaultValue (false)]
102 get { return this.is_link; }
104 this.is_link = value;
109 [DefaultValue (LinkBehavior.SystemDefault)]
110 public LinkBehavior LinkBehavior {
111 get { return this.link_behavior; }
113 this.link_behavior = value;
118 public Color LinkColor {
119 get { return this.link_color; }
121 this.link_color = value;
126 [DefaultValue (false)]
127 public bool LinkVisited {
128 get { return this.link_visited; }
130 this.link_visited = value;
135 public Color VisitedLinkColor
137 get { return this.visited_link_color; }
139 this.visited_link_color = value;
145 #region Protected Methods
146 [EditorBrowsable (EditorBrowsableState.Advanced)]
147 protected override AccessibleObject CreateAccessibilityInstance ()
149 ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
151 ao.role = AccessibleRole.StaticText;
152 ao.state = AccessibleStates.ReadOnly;
157 protected override void OnFontChanged (EventArgs e)
159 base.OnFontChanged (e);
162 protected override void OnMouseEnter (EventArgs e)
164 base.OnMouseEnter (e);
167 protected override void OnMouseLeave (EventArgs e)
169 base.OnMouseLeave (e);
172 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
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);
180 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
182 Rectangle text_layout_rect;
183 Rectangle image_layout_rect;
185 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
187 if (this.IsOnDropDown) {
189 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
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 ());
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)
200 if (this.Pressed) // Mouse Down
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));
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));
213 else if (this.Selected) // Hover
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));
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));
228 if (this.link_visited) // Normal, Visited
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));
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));
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));
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));
257 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
261 protected internal override bool ProcessMnemonic (char charCode)
263 this.Parent.SelectNextToolStripItem (this, true);