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)
32 namespace System.Windows.Forms
34 public class ToolStripLabel : ToolStripItem
36 private Color active_link_color;
38 private LinkBehavior link_behavior;
39 private Color link_color;
40 private bool link_visited;
41 private Color visited_link_color;
43 #region Public Constructors
44 public ToolStripLabel ()
45 : this (String.Empty, null, false, null, String.Empty)
49 public ToolStripLabel (Image image)
50 : this (String.Empty, image, false, null, String.Empty)
54 public ToolStripLabel (string text)
55 : this (text, null, false, null, String.Empty)
59 public ToolStripLabel (string text, Image image)
60 : this (text, image, false, null, String.Empty)
64 public ToolStripLabel (string text, Image image, bool isLink)
65 : this (text, image, isLink, null, String.Empty)
69 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
70 : this (text, image, isLink, onClick, String.Empty)
74 public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
75 : base (text, image, onClick, name)
77 this.active_link_color = Color.Red;
78 this.is_link = isLink;
79 this.link_behavior = LinkBehavior.SystemDefault;
80 this.link_color = Color.Blue;
81 this.link_visited = false;
82 this.visited_link_color = Color.Purple;
86 #region Public Properties
87 public Color ActiveLinkColor {
88 get { return this.active_link_color; }
90 this.active_link_color = value;
95 public override bool CanSelect { get { return false; } }
98 get { return this.is_link; }
100 this.is_link = value;
105 public LinkBehavior LinkBehavior {
106 get { return this.link_behavior; }
108 this.link_behavior = value;
113 public Color LinkColor {
114 get { return this.link_color; }
116 this.link_color = value;
121 public bool LinkVisited {
122 get { return this.link_visited; }
124 this.link_visited = value;
129 public Color VisitedLinkColor
131 get { return this.visited_link_color; }
133 this.visited_link_color = value;
139 #region Protected Methods
140 protected override void OnFontChanged (EventArgs e)
142 base.OnFontChanged (e);
145 protected override void OnMouseEnter (EventArgs e)
147 base.OnMouseEnter (e);
150 protected override void OnMouseLeave (EventArgs e)
152 base.OnMouseLeave (e);
155 protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
159 if (this.Owner != null) {
160 Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
161 Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
163 this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
165 Rectangle text_layout_rect;
166 Rectangle image_layout_rect;
168 this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
170 if (image_layout_rect != Rectangle.Empty)
171 this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
172 if (text_layout_rect != Rectangle.Empty)
174 if (this.Pressed) // Mouse Down
176 switch (this.link_behavior) {
177 case LinkBehavior.SystemDefault:
178 case LinkBehavior.AlwaysUnderline:
179 case LinkBehavior.HoverUnderline:
180 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));
182 case LinkBehavior.NeverUnderline:
183 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));
187 else if (this.Selected) // Hover
189 switch (this.link_behavior) {
190 case LinkBehavior.SystemDefault:
191 case LinkBehavior.AlwaysUnderline:
192 case LinkBehavior.HoverUnderline:
193 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));
195 case LinkBehavior.NeverUnderline:
196 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
202 if (this.link_visited) // Normal, Visited
204 switch (this.link_behavior) {
205 case LinkBehavior.SystemDefault:
206 case LinkBehavior.AlwaysUnderline:
207 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));
209 case LinkBehavior.NeverUnderline:
210 case LinkBehavior.HoverUnderline:
211 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));
217 switch (this.link_behavior) {
218 case LinkBehavior.SystemDefault:
219 case LinkBehavior.AlwaysUnderline:
220 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));
222 case LinkBehavior.NeverUnderline:
223 case LinkBehavior.HoverUnderline:
224 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
231 this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));