1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
//
// ToolStripLabel.cs
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2006 Jonathan Pobst
//
// Authors:
// Jonathan Pobst (monkey@jpobst.com)
//
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design;
namespace System.Windows.Forms
{
[ToolStripItemDesignerAvailability (ToolStripItemDesignerAvailability.ToolStrip)]
public class ToolStripLabel : ToolStripItem
{
private Color active_link_color;
private bool is_link;
private LinkBehavior link_behavior;
private Color link_color;
private bool link_visited;
private Color visited_link_color;
#region UIA FrameWork Events
static object UIAIsLinkChangedEvent = new object ();
internal event EventHandler UIAIsLinkChanged {
add { Events.AddHandler (UIAIsLinkChangedEvent, value); }
remove { Events.RemoveHandler (UIAIsLinkChangedEvent, value); }
}
internal void OnUIAIsLinkChanged (EventArgs e)
{
EventHandler eh = (EventHandler) Events [UIAIsLinkChangedEvent];
if (eh != null)
eh (this, e);
}
#endregion
#region Public Constructors
public ToolStripLabel ()
: this (null, null, false, null, String.Empty)
{
}
public ToolStripLabel (Image image)
: this (null, image, false, null, String.Empty)
{
}
public ToolStripLabel (string text)
: this (text, null, false, null, String.Empty)
{
}
public ToolStripLabel (string text, Image image)
: this (text, image, false, null, String.Empty)
{
}
public ToolStripLabel (string text, Image image, bool isLink)
: this (text, image, isLink, null, String.Empty)
{
}
public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick)
: this (text, image, isLink, onClick, String.Empty)
{
}
public ToolStripLabel (string text, Image image, bool isLink, EventHandler onClick, string name)
: base (text, image, onClick, name)
{
this.active_link_color = Color.Red;
this.is_link = isLink;
this.link_behavior = LinkBehavior.SystemDefault;
this.link_color = Color.FromArgb (0, 0, 255);
this.link_visited = false;
this.visited_link_color = Color.FromArgb (128, 0, 128);
}
#endregion
#region Public Properties
public Color ActiveLinkColor {
get { return this.active_link_color; }
set {
this.active_link_color = value;
this.Invalidate ();
}
}
public override bool CanSelect { get { return false; } }
[DefaultValue (false)]
public bool IsLink {
get { return this.is_link; }
set {
this.is_link = value;
this.Invalidate ();
// UIA Framework Event: IsLink Changed
OnUIAIsLinkChanged (EventArgs.Empty);
}
}
[DefaultValue (LinkBehavior.SystemDefault)]
public LinkBehavior LinkBehavior {
get { return this.link_behavior; }
set {
this.link_behavior = value;
this.Invalidate ();
}
}
public Color LinkColor {
get { return this.link_color; }
set {
this.link_color = value;
this.Invalidate ();
}
}
[DefaultValue (false)]
public bool LinkVisited {
get { return this.link_visited; }
set {
this.link_visited = value;
this.Invalidate ();
}
}
public Color VisitedLinkColor
{
get { return this.visited_link_color; }
set {
this.visited_link_color = value;
this.Invalidate ();
}
}
#endregion
#region Protected Methods
[EditorBrowsable (EditorBrowsableState.Advanced)]
protected override AccessibleObject CreateAccessibilityInstance ()
{
ToolStripItemAccessibleObject ao = new ToolStripItemAccessibleObject (this);
ao.role = AccessibleRole.StaticText;
ao.state = AccessibleStates.ReadOnly;
return ao;
}
protected override void OnFontChanged (EventArgs e)
{
base.OnFontChanged (e);
}
protected override void OnMouseEnter (EventArgs e)
{
base.OnMouseEnter (e);
}
protected override void OnMouseLeave (EventArgs e)
{
base.OnMouseLeave (e);
}
protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
{
if (this.Owner != null) {
Color font_color = this.Enabled ? this.ForeColor : SystemColors.GrayText;
Image draw_image = this.Enabled ? this.Image : ToolStripRenderer.CreateDisabledImage (this.Image);
this.Owner.Renderer.DrawLabelBackground (new System.Windows.Forms.ToolStripItemRenderEventArgs (e.Graphics, this));
Rectangle text_layout_rect;
Rectangle image_layout_rect;
this.CalculateTextAndImageRectangles (out text_layout_rect, out image_layout_rect);
if (this.IsOnDropDown) {
if (this.ShowMargin)
text_layout_rect = new Rectangle (35, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
else
text_layout_rect = new Rectangle (7, text_layout_rect.Top, text_layout_rect.Width, text_layout_rect.Height);
if (image_layout_rect != Rectangle.Empty)
image_layout_rect = new Rectangle (new Point (4, 3), base.GetImageSize ());
}
if (image_layout_rect != Rectangle.Empty)
this.Owner.Renderer.DrawItemImage (new System.Windows.Forms.ToolStripItemImageRenderEventArgs (e.Graphics, this, draw_image, image_layout_rect));
if (text_layout_rect != Rectangle.Empty)
if (this.is_link) {
if (this.Pressed) // Mouse Down
{
switch (this.link_behavior) {
case LinkBehavior.SystemDefault:
case LinkBehavior.AlwaysUnderline:
case LinkBehavior.HoverUnderline:
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));
break;
case LinkBehavior.NeverUnderline:
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));
break;
}
}
else if (this.Selected) // Hover
{
switch (this.link_behavior) {
case LinkBehavior.SystemDefault:
case LinkBehavior.AlwaysUnderline:
case LinkBehavior.HoverUnderline:
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));
break;
case LinkBehavior.NeverUnderline:
this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
break;
}
}
else {
if (this.link_visited) // Normal, Visited
{
switch (this.link_behavior) {
case LinkBehavior.SystemDefault:
case LinkBehavior.AlwaysUnderline:
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));
break;
case LinkBehavior.NeverUnderline:
case LinkBehavior.HoverUnderline:
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));
break;
}
}
else // Normal
{
switch (this.link_behavior) {
case LinkBehavior.SystemDefault:
case LinkBehavior.AlwaysUnderline:
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));
break;
case LinkBehavior.NeverUnderline:
case LinkBehavior.HoverUnderline:
this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, this.link_color, this.Font, this.TextAlign));
break;
}
}
}
} else
this.Owner.Renderer.DrawItemText (new System.Windows.Forms.ToolStripItemTextRenderEventArgs (e.Graphics, this, this.Text, text_layout_rect, font_color, this.Font, this.TextAlign));
}
// call Paint handlers last.
base.OnPaint (e);
}
protected internal override bool ProcessMnemonic (char charCode)
{
this.Parent.SelectNextToolStripItem (this, true);
return true;
}
#endregion
}
}
|