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 UIA FrameWork Events
48 static object UIAIsLinkChangedEvent = new object ();
50 internal event EventHandler UIAIsLinkChanged {
51 add { Events.AddHandler (UIAIsLinkChangedEvent, value); }
52 remove { Events.RemoveHandler (UIAIsLinkChangedEvent, value); }
55 internal void OnUIAIsLinkChanged (EventArgs e)
57 EventHandler eh = (EventHandler) Events [UIAIsLinkChangedEvent];
64 #region Public Constructors
65 public ToolStripLabel ()
66 : this (null, null, false, null, String.Empty)
70 public ToolStripLabel (Image image)
71 : this (null, image, false, null, String.Empty)
75 public ToolStripLabel (string text)
76 : this (text, null, false, null, String.Empty)
80 public ToolStripLabel (string text, Image image)
81 : this (text, image, false, null, String.Empty)
85 public ToolStripLabel (string text, Image image, bool isLink)
86 : this (text, image, isLink, null, String.Empty)
90 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
91 : this (text, image, isLink, onClick, String.Empty)
95 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
96 : base (text, image, onClick, name)
98 this.active_link_color = Color.Red;
99 this.is_link = isLink;
100 this.link_behavior = LinkBehavior.SystemDefault;
101 this.link_color = Color.FromArgb (0, 0, 255);
102 this.link_visited = false;
103 this.visited_link_color = Color.FromArgb (128, 0, 128);
107 #region Public Properties
108 public Color ActiveLinkColor {
109 get { return this.active_link_color; }
111 this.active_link_color = value;
116 public override bool CanSelect { get { return false; } }
118 [DefaultValue (false)]
120 get { return this.is_link; }
122 this.is_link = value;
125 // UIA Framework Event: IsLink Changed
126 OnUIAIsLinkChanged (EventArgs.Empty);
131 [DefaultValue (LinkBehavior.SystemDefault)]
132 public LinkBehavior LinkBehavior {
133 get { return this.link_behavior; }
135 this.link_behavior = value;
140 public Color LinkColor {
141 get { return this.link_color; }
143 this.link_color = value;
148 [DefaultValue (false)]
149 public bool LinkVisited {
150 get { return this.link_visited; }
152 this.link_visited = value;
157 public Color VisitedLinkColor
159 get { return this.visited_link_color; }
161 this.visited_link_color = value;
167 #region Protected Methods
168 [EditorBrowsable (EditorBrowsableState.Advanced)]
169 protected override AccessibleObject CreateAccessibilityInstance ()
171 ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
173 ao.role = AccessibleRole.StaticText;
174 ao.state = AccessibleStates.ReadOnly;
179 protected override void OnFontChanged (EventArgs e)
181 base.OnFontChanged (e);
184 protected override void OnMouseEnter (EventArgs e)
186 base.OnMouseEnter (e);
189 protected override void OnMouseLeave (EventArgs e)
191 base.OnMouseLeave (e);
194 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
196 if (this.Owner != null) {
197 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
198 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
200 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
202 Rectangle text_layout_rect;
203 Rectangle image_layout_rect;
205 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
207 if (this.IsOnDropDown) {
209 text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
211 text_layout_rect = new Rectangle (7, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
212 if (image_layout_rect != Rectangle.Empty)
213 image_layout_rect = new Rectangle (new Point (4, 3), base.GetImageSize ());
216 if (image_layout_rect != Rectangle.Empty)
217 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
218 if (text_layout_rect != Rectangle.Empty)
220 if (this.Pressed) // Mouse Down
222 switch (this.link_behavior) {
223 case LinkBehavior.SystemDefault:
224 case LinkBehavior.AlwaysUnderline:
225 case LinkBehavior.HoverUnderline:
226 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));
228 case LinkBehavior.NeverUnderline:
229 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));
233 else if (this.Selected) // Hover
235 switch (this.link_behavior) {
236 case LinkBehavior.SystemDefault:
237 case LinkBehavior.AlwaysUnderline:
238 case LinkBehavior.HoverUnderline:
239 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));
241 case LinkBehavior.NeverUnderline:
242 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
248 if (this.link_visited) // Normal, Visited
250 switch (this.link_behavior) {
251 case LinkBehavior.SystemDefault:
252 case LinkBehavior.AlwaysUnderline:
253 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));
255 case LinkBehavior.NeverUnderline:
256 case LinkBehavior.HoverUnderline:
257 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));
263 switch (this.link_behavior) {
264 case LinkBehavior.SystemDefault:
265 case LinkBehavior.AlwaysUnderline:
266 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));
268 case LinkBehavior.NeverUnderline:
269 case LinkBehavior.HoverUnderline:
270 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
277 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
280 // call Paint handlers last.
284 protected internal override bool ProcessMnemonic (char charCode)
286 this.Parent.SelectNextToolStripItem (this, true);