1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc.
23 // Jordi Mas i Hernandez, jordi@ximian.com
24 // Peter Bartok, pbartok@novell.com
30 using System.ComponentModel;
31 using System.ComponentModel.Design;
33 using System.Drawing.Text;
34 using System.Drawing.Imaging;
35 using System.Runtime.InteropServices;
36 using System.Windows.Forms.Theming;
38 namespace System.Windows.Forms
40 [DefaultProperty ("Text")]
41 [Designer ("System.Windows.Forms.Design.LabelDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
42 [ClassInterface (ClassInterfaceType.AutoDispatch)]
44 [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts.AssemblySystem_Design)]
45 [DefaultBindingProperty ("Text")]
46 public class Label : Control
48 private bool autosize;
49 private bool auto_ellipsis;
51 private bool render_transparent;
52 private FlatStyle flat_style;
53 private bool use_mnemonic;
54 private int image_index = -1;
55 private string image_key = string.Empty;
56 private ImageList image_list;
57 internal ContentAlignment image_align;
58 internal StringFormat string_format;
59 internal ContentAlignment text_align;
60 static SizeF req_witdthsize = new SizeF (0,0);
63 static object AutoSizeChangedEvent = new object ();
64 static object TextAlignChangedEvent = new object ();
67 [EditorBrowsable (EditorBrowsableState.Always)]
68 public new event EventHandler AutoSizeChanged {
69 add { Events.AddHandler (AutoSizeChangedEvent, value); }
70 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
74 [EditorBrowsable(EditorBrowsableState.Never)]
75 public new event EventHandler BackgroundImageChanged {
76 add { base.BackgroundImageChanged += value; }
77 remove { base.BackgroundImageChanged -= value; }
81 [EditorBrowsable (EditorBrowsableState.Never)]
82 public new event EventHandler BackgroundImageLayoutChanged {
83 add { base.BackgroundImageLayoutChanged += value; }
84 remove { base.BackgroundImageLayoutChanged -= value; }
88 [EditorBrowsable(EditorBrowsableState.Never)]
89 public new event EventHandler ImeModeChanged {
90 add { base.ImeModeChanged += value; }
91 remove { base.ImeModeChanged -= value; }
95 [EditorBrowsable(EditorBrowsableState.Never)]
96 public new event KeyEventHandler KeyDown {
97 add { base.KeyDown += value; }
98 remove { base.KeyDown -= value; }
102 [EditorBrowsable(EditorBrowsableState.Never)]
103 public new event KeyPressEventHandler KeyPress {
104 add { base.KeyPress += value; }
105 remove { base.KeyPress -= value; }
109 [EditorBrowsable(EditorBrowsableState.Never)]
110 public new event KeyEventHandler KeyUp {
111 add { base.KeyUp += value; }
112 remove { base.KeyUp -= value; }
116 [EditorBrowsable(EditorBrowsableState.Never)]
117 public new event EventHandler TabStopChanged {
118 add { base.TabStopChanged += value; }
119 remove { base.TabStopChanged -= value; }
122 public event EventHandler TextAlignChanged {
123 add { Events.AddHandler (TextAlignChangedEvent, value); }
124 remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
130 // Defaults in the Spec
133 string_format = new StringFormat();
134 string_format.FormatFlags = StringFormatFlags.LineLimit;
135 TextAlign = ContentAlignment.TopLeft;
139 image_align = ContentAlignment.MiddleCenter;
140 SetUseMnemonic (UseMnemonic);
141 flat_style = FlatStyle.Standard;
143 SetStyle (ControlStyles.Selectable, false);
144 SetStyle (ControlStyles.ResizeRedraw |
145 ControlStyles.UserPaint |
146 ControlStyles.AllPaintingInWmPaint |
147 ControlStyles.SupportsTransparentBackColor |
148 ControlStyles.OptimizedDoubleBuffer
151 HandleCreated += new EventHandler (OnHandleCreatedLB);
154 #region Public Properties
156 [DefaultValue (false)]
158 [EditorBrowsable (EditorBrowsableState.Always)]
159 public bool AutoEllipsis {
160 get { return this.auto_ellipsis; }
163 if (this.auto_ellipsis != value) {
164 this.auto_ellipsis = value;
166 if (this.auto_ellipsis)
167 string_format.Trimming = StringTrimming.EllipsisCharacter;
169 string_format.Trimming = StringTrimming.Character;
172 Parent.PerformLayout (this, "AutoEllipsis");
179 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
180 [EditorBrowsable (EditorBrowsableState.Always)]
181 [DefaultValue(false)]
183 [RefreshProperties(RefreshProperties.All)]
184 public override bool AutoSize {
185 get { return autosize; }
187 if (autosize == value)
190 base.SetAutoSizeMode (AutoSizeMode.GrowAndShrink);
191 base.AutoSize = value;
196 OnAutoSizeChanged (EventArgs.Empty);
201 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
202 [EditorBrowsable(EditorBrowsableState.Never)]
203 public override Image BackgroundImage {
204 get { return base.BackgroundImage; }
206 base.BackgroundImage = value;
212 [EditorBrowsable (EditorBrowsableState.Never)]
213 public override ImageLayout BackgroundImageLayout {
214 get { return base.BackgroundImageLayout; }
215 set { base.BackgroundImageLayout = value; }
218 [DefaultValue(BorderStyle.None)]
220 public virtual BorderStyle BorderStyle {
221 get { return InternalBorderStyle; }
222 set { InternalBorderStyle = value; }
225 protected override CreateParams CreateParams {
227 CreateParams create_params = base.CreateParams;
229 if (BorderStyle != BorderStyle.Fixed3D)
230 return create_params;
232 create_params.ExStyle &= ~(int) WindowExStyles.WS_EX_CLIENTEDGE;
233 create_params.ExStyle |= (int)WindowExStyles.WS_EX_STATICEDGE;
235 return create_params;
239 protected override ImeMode DefaultImeMode {
240 get { return ImeMode.Disable;}
243 protected override Padding DefaultMargin {
244 get { return new Padding (3, 0, 3, 0); }
247 protected override Size DefaultSize {
248 get { return ThemeElements.LabelPainter.DefaultSize; }
251 [DefaultValue(FlatStyle.Standard)]
252 public FlatStyle FlatStyle {
253 get { return flat_style; }
255 if (!Enum.IsDefined (typeof (FlatStyle), value))
256 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
258 if (flat_style == value)
263 Parent.PerformLayout (this, "FlatStyle");
271 if (this.image != null)
274 if (this.image_index >= 0)
275 if (this.image_list != null)
276 return this.image_list.Images[this.image_index];
278 if (!string.IsNullOrEmpty (this.image_key))
279 if (this.image_list != null)
280 return this.image_list.Images[this.image_key];
285 if (this.image != value) {
287 this.image_index = -1;
288 this.image_key = string.Empty;
289 this.image_list = null;
291 if (this.AutoSize && this.Parent != null)
292 this.Parent.PerformLayout (this, "Image");
299 [DefaultValue(ContentAlignment.MiddleCenter)]
301 public ContentAlignment ImageAlign {
302 get { return image_align; }
304 if (!Enum.IsDefined (typeof (ContentAlignment), value))
305 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
307 if (image_align == value)
316 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
318 [TypeConverter (typeof (ImageIndexConverter))]
319 [RefreshProperties (RefreshProperties.Repaint)]
320 public int ImageIndex {
322 if (ImageList == null) {
326 if (image_index >= image_list.Images.Count) {
327 return image_list.Images.Count - 1;
335 throw new ArgumentException ();
337 if (this.image_index != value) {
338 this.image_index = value;
340 this.image_key = string.Empty;
348 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
349 [RefreshProperties (RefreshProperties.Repaint)]
350 [TypeConverter (typeof (ImageKeyConverter))]
351 public string ImageKey {
352 get { return this.image_key; }
354 if (this.image_key != value) {
356 this.image_index = -1;
357 this.image_key = value;
364 [RefreshProperties (RefreshProperties.Repaint)]
365 public ImageList ImageList {
366 get { return image_list;}
368 if (image_list == value)
373 if (image_list != null && image_index !=-1)
381 [EditorBrowsable(EditorBrowsableState.Never)]
382 public new ImeMode ImeMode {
383 get { return base.ImeMode; }
384 set { base.ImeMode = value; }
387 internal virtual Size InternalGetPreferredSize (Size proposed)
391 if (Text == string.Empty) {
392 size = new Size (0, Font.Height);
394 size = Size.Ceiling (TextRenderer.MeasureString (Text, Font, req_witdthsize, string_format));
398 size.Width += Padding.Horizontal;
399 size.Height += Padding.Vertical;
401 if (!use_compatible_text_rendering)
404 if (border_style == BorderStyle.None)
412 public override Size GetPreferredSize (Size proposedSize)
414 return InternalGetPreferredSize (proposedSize);
418 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
419 [EditorBrowsable(EditorBrowsableState.Advanced)]
420 public virtual int PreferredHeight {
421 get { return InternalGetPreferredSize (Size.Empty).Height; }
425 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
426 [EditorBrowsable(EditorBrowsableState.Advanced)]
427 public virtual int PreferredWidth {
428 get { return InternalGetPreferredSize (Size.Empty).Width; }
431 [Obsolete ("This property has been deprecated. Use BackColor instead.")]
432 protected virtual bool RenderTransparent {
433 get { return render_transparent; }
434 set { render_transparent = value;}
438 [DefaultValue(false)]
439 [EditorBrowsable(EditorBrowsableState.Never)]
440 public new bool TabStop {
441 get { return base.TabStop; }
442 set { base.TabStop = value; }
445 [DefaultValue(ContentAlignment.TopLeft)]
447 public virtual ContentAlignment TextAlign {
448 get { return text_align; }
451 if (!Enum.IsDefined (typeof (ContentAlignment), value))
452 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
454 if (text_align != value) {
457 case ContentAlignment.BottomLeft:
458 string_format.LineAlignment = StringAlignment.Far;
459 string_format.Alignment = StringAlignment.Near;
461 case ContentAlignment.BottomCenter:
462 string_format.LineAlignment = StringAlignment.Far;
463 string_format.Alignment = StringAlignment.Center;
465 case ContentAlignment.BottomRight:
466 string_format.LineAlignment = StringAlignment.Far;
467 string_format.Alignment = StringAlignment.Far;
469 case ContentAlignment.TopLeft:
470 string_format.LineAlignment = StringAlignment.Near;
471 string_format.Alignment = StringAlignment.Near;
473 case ContentAlignment.TopCenter:
474 string_format.LineAlignment = StringAlignment.Near;
475 string_format.Alignment = StringAlignment.Center;
477 case ContentAlignment.TopRight:
478 string_format.LineAlignment = StringAlignment.Near;
479 string_format.Alignment = StringAlignment.Far;
481 case ContentAlignment.MiddleLeft:
482 string_format.LineAlignment = StringAlignment.Center;
483 string_format.Alignment = StringAlignment.Near;
485 case ContentAlignment.MiddleRight:
486 string_format.LineAlignment = StringAlignment.Center;
487 string_format.Alignment = StringAlignment.Far;
489 case ContentAlignment.MiddleCenter:
490 string_format.LineAlignment = StringAlignment.Center;
491 string_format.Alignment = StringAlignment.Center;
497 OnTextAlignChanged (EventArgs.Empty);
504 public bool UseMnemonic {
505 get { return use_mnemonic; }
507 if (use_mnemonic != value) {
508 use_mnemonic = value;
509 SetUseMnemonic (use_mnemonic);
517 #region Public Methods
519 protected Rectangle CalcImageRenderBounds (Image image, Rectangle r, ContentAlignment align)
521 Rectangle rcImageClip = r;
522 rcImageClip.Inflate (-2,-2);
527 if (align == ContentAlignment.TopCenter ||
528 align == ContentAlignment.MiddleCenter ||
529 align == ContentAlignment.BottomCenter) {
530 X += (r.Width - image.Width) / 2;
531 } else if (align == ContentAlignment.TopRight ||
532 align == ContentAlignment.MiddleRight||
533 align == ContentAlignment.BottomRight) {
534 X += (r.Width - image.Width);
537 if( align == ContentAlignment.BottomCenter ||
538 align == ContentAlignment.BottomLeft ||
539 align == ContentAlignment.BottomRight) {
540 Y += r.Height - image.Height;
541 } else if(align == ContentAlignment.MiddleCenter ||
542 align == ContentAlignment.MiddleLeft ||
543 align == ContentAlignment.MiddleRight) {
544 Y += (r.Height - image.Height) / 2;
549 rcImageClip.Width = image.Width;
550 rcImageClip.Height = image.Height;
555 protected override AccessibleObject CreateAccessibilityInstance ()
557 return base.CreateAccessibilityInstance ();
560 protected override void Dispose(bool disposing)
562 base.Dispose (disposing);
565 string_format.Dispose ();
568 protected internal void DrawImage (Graphics g, Image image, Rectangle r, ContentAlignment align)
570 if (image == null || g == null)
573 Rectangle rcImageClip = CalcImageRenderBounds (image, r, align);
576 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
578 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
581 protected override void OnEnabledChanged (EventArgs e)
583 base.OnEnabledChanged (e);
586 protected override void OnFontChanged (EventArgs e)
588 base.OnFontChanged (e);
594 protected override void OnPaddingChanged (EventArgs e)
596 base.OnPaddingChanged (e);
599 protected override void OnPaint (PaintEventArgs e)
601 ThemeElements.LabelPainter.Draw (e.Graphics, ClientRectangle, this);
605 protected override void OnParentChanged (EventArgs e)
607 base.OnParentChanged (e);
610 protected override void OnRightToLeftChanged (EventArgs e)
612 base.OnRightToLeftChanged (e);
615 protected virtual void OnTextAlignChanged (EventArgs e)
617 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
622 protected override void OnTextChanged (EventArgs e)
624 base.OnTextChanged (e);
630 protected override void OnVisibleChanged (EventArgs e)
632 base.OnVisibleChanged (e);
635 protected override bool ProcessMnemonic (char charCode)
637 if (IsMnemonic (charCode, Text)) {
638 // Select item next in line in tab order
639 if (this.Parent != null)
640 Parent.SelectNextControl(this, true, false, false, false);
644 return base.ProcessMnemonic (charCode);
647 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
649 base.SetBoundsCore (x, y, width, height, specified);
652 public override string ToString()
654 return base.ToString () + ", Text: " + Text;
657 protected override void WndProc (ref Message m)
659 switch ((Msg) m.Msg) {
660 case Msg.WM_DRAWITEM:
661 m.Result = (IntPtr)1;
664 base.WndProc (ref m);
669 #endregion Public Methods
671 #region Private Methods
673 private void CalcAutoSize ()
678 Size s = InternalGetPreferredSize (Size.Empty);
680 SetBounds (Left, Top, s.Width, s.Height, BoundsSpecified.Size);
683 private void OnHandleCreatedLB (Object o, EventArgs e)
689 private void SetUseMnemonic (bool use)
692 string_format.HotkeyPrefix = HotkeyPrefix.Show;
694 string_format.HotkeyPrefix = HotkeyPrefix.None;
697 #endregion Private Methods
698 [DefaultValue (false)]
699 public bool UseCompatibleTextRendering {
700 get { return use_compatible_text_rendering; }
701 set { use_compatible_text_rendering = value; }
704 [SettingsBindable (true)]
705 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
706 typeof (System.Drawing.Design.UITypeEditor))]
707 public override string Text {
708 get { return base.Text; }
709 set { base.Text = value; }
712 protected override void OnMouseEnter (EventArgs e)
714 base.OnMouseEnter (e);
717 protected override void OnMouseLeave (EventArgs e)
719 base.OnMouseLeave (e);
722 protected override void OnHandleDestroyed (EventArgs e)
724 base.OnHandleDestroyed (e);