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;
33 namespace System.Windows.Forms
35 public class ToolStripLabel : ToolStripItem
37 private Color active_link_color;
39 private LinkBehavior link_behavior;
40 private Color link_color;
41 private bool link_visited;
42 private Color visited_link_color;
44 #region Public Constructors
45 public ToolStripLabel ()
46 : this (String.Empty, null, false, null, String.Empty)
50 public ToolStripLabel (Image image)
51 : this (String.Empty, image, false, null, String.Empty)
55 public ToolStripLabel (string text)
56 : this (text, null, false, null, String.Empty)
60 public ToolStripLabel (string text, Image image)
61 : this (text, image, false, null, String.Empty)
65 public ToolStripLabel (string text, Image image, bool isLink)
66 : this (text, image, isLink, null, String.Empty)
70 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
71 : this (text, image, isLink, onClick, String.Empty)
75 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
76 : base (text, image, onClick, name)
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;
87 #region Public Properties
88 public Color ActiveLinkColor {
89 get { return this.active_link_color; }
91 this.active_link_color = value;
96 public override bool CanSelect { get { return false; } }
98 [DefaultValue (false)]
100 get { return this.is_link; }
102 this.is_link = value;
107 [DefaultValue (LinkBehavior.SystemDefault)]
108 public LinkBehavior LinkBehavior {
109 get { return this.link_behavior; }
111 this.link_behavior = value;
116 public Color LinkColor {
117 get { return this.link_color; }
119 this.link_color = value;
124 [DefaultValue (false)]
125 public bool LinkVisited {
126 get { return this.link_visited; }
128 this.link_visited = value;
133 public Color VisitedLinkColor
135 get { return this.visited_link_color; }
137 this.visited_link_color = value;
143 #region Protected Methods
144 protected override AccessibleObject CreateAccessibilityInstance ()
146 ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
148 ao.role = AccessibleRole.StaticText;
149 ao.state = AccessibleStates.ReadOnly;
154 protected override void OnFontChanged (EventArgs e)
156 base.OnFontChanged (e);
159 protected override void OnMouseEnter (EventArgs e)
161 base.OnMouseEnter (e);
164 protected override void OnMouseLeave (EventArgs e)
166 base.OnMouseLeave (e);
169 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
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);
177 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
179 Rectangle text_layout_rect;
180 Rectangle image_layout_rect;
182 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
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)
188 if (this.Pressed) // Mouse Down
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));
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));
201 else if (this.Selected) // Hover
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));
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));
216 if (this.link_visited) // Normal, Visited
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));
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));
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));
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));
245 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));