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)
30 using System.ComponentModel;
31 using System.Windows.Forms.Design;
33 namespace System.Windows.Forms
35 [ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip)]
36 public class ToolStripLabel : ToolStripItem
38 private Color active_link_color;
40 private LinkBehavior link_behavior;
41 private Color link_color;
42 private bool link_visited;
43 private Color visited_link_color;
45 #region UIA FrameWork Events
46 static object UIAIsLinkChangedEvent = new object ();
48 internal event EventHandler UIAIsLinkChanged {
49 add { Events.AddHandler (UIAIsLinkChangedEvent, value); }
50 remove { Events.RemoveHandler (UIAIsLinkChangedEvent, value); }
53 internal void OnUIAIsLinkChanged (EventArgs e)
55 EventHandler eh = (EventHandler) Events [UIAIsLinkChangedEvent];
61 #region Public Constructors
62 public ToolStripLabel ()
63 : this (null, null, false, null, String.Empty)
67 public ToolStripLabel (Image image)
68 : this (null, image, false, null, String.Empty)
72 public ToolStripLabel (string text)
73 : this (text, null, false, null, String.Empty)
77 public ToolStripLabel (string text, Image image)
78 : this (text, image, false, null, String.Empty)
82 public ToolStripLabel (string text, Image image, bool isLink)
83 : this (text, image, isLink, null, String.Empty)
87 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
88 : this (text, image, isLink, onClick, String.Empty)
92 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
93 : base (text, image, onClick, name)
95 this.active_link_color = Color.Red;
96 this.is_link = isLink;
97 this.link_behavior = LinkBehavior.SystemDefault;
98 this.link_color = Color.FromArgb (0, 0, 255);
99 this.link_visited = false;
100 this.visited_link_color = Color.FromArgb (128, 0, 128);
104 #region Public Properties
105 public Color ActiveLinkColor {
106 get { return this.active_link_color; }
108 this.active_link_color = value;
113 public override bool CanSelect { get { return false; } }
115 [DefaultValue (false)]
117 get { return this.is_link; }
119 this.is_link = value;
122 // UIA Framework Event: IsLink Changed
123 OnUIAIsLinkChanged (EventArgs.Empty);
127 [DefaultValue (LinkBehavior.SystemDefault)]
128 public LinkBehavior LinkBehavior {
129 get { return this.link_behavior; }
131 this.link_behavior = value;
136 public Color LinkColor {
137 get { return this.link_color; }
139 this.link_color = value;
144 [DefaultValue (false)]
145 public bool LinkVisited {
146 get { return this.link_visited; }
148 this.link_visited = value;
153 public Color VisitedLinkColor
155 get { return this.visited_link_color; }
157 this.visited_link_color = value;
163 #region Protected Methods
164 [EditorBrowsable (EditorBrowsableState.Advanced)]
165 protected override AccessibleObject CreateAccessibilityInstance ()
167 ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
169 ao.role = AccessibleRole.StaticText;
170 ao.state = AccessibleStates.ReadOnly;
175 protected override void OnFontChanged (EventArgs e)
177 base.OnFontChanged (e);
180 protected override void OnMouseEnter (EventArgs e)
182 base.OnMouseEnter (e);
185 protected override void OnMouseLeave (EventArgs e)
187 base.OnMouseLeave (e);
190 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
192 if (this.Owner != null) {
193 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
194 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
196 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
198 Rectangle text_layout_rect;
199 Rectangle image_layout_rect;
201 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
203 if (this.IsOnDropDown) {
205 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
207 text_layout_rect = new Rectangle (7, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
208 if (image_layout_rect != Rectangle.Empty)
209 image_layout_rect = new Rectangle (new Point (4, 3), base.GetImageSize ());
212 if (image_layout_rect != Rectangle.Empty)
213 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
214 if (text_layout_rect != Rectangle.Empty)
216 if (this.Pressed) // Mouse Down
218 switch (this.link_behavior) {
219 case LinkBehavior.SystemDefault:
220 case LinkBehavior.AlwaysUnderline:
221 case LinkBehavior.HoverUnderline:
222 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));
224 case LinkBehavior.NeverUnderline:
225 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));
229 else if (this.Selected) // Hover
231 switch (this.link_behavior) {
232 case LinkBehavior.SystemDefault:
233 case LinkBehavior.AlwaysUnderline:
234 case LinkBehavior.HoverUnderline:
235 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));
237 case LinkBehavior.NeverUnderline:
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));
244 if (this.link_visited) // Normal, Visited
246 switch (this.link_behavior) {
247 case LinkBehavior.SystemDefault:
248 case LinkBehavior.AlwaysUnderline:
249 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));
251 case LinkBehavior.NeverUnderline:
252 case LinkBehavior.HoverUnderline:
253 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));
259 switch (this.link_behavior) {
260 case LinkBehavior.SystemDefault:
261 case LinkBehavior.AlwaysUnderline:
262 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));
264 case LinkBehavior.NeverUnderline:
265 case LinkBehavior.HoverUnderline:
266 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
273 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
276 // call Paint handlers last.
280 protected internal override bool ProcessMnemonic (char charCode)
282 this.Parent.SelectNextToolStripItem (this, true);