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;
37 namespace System.Windows.Forms
39 [DefaultProperty ("Text")]
40 [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")]
47 public class Label : Control
49 private bool autosize;
51 private bool auto_ellipsis;
54 private bool render_transparent;
55 private FlatStyle flat_style;
56 private bool use_mnemonic;
57 private int image_index = -1;
59 private string image_key = string.Empty;
61 private ImageList image_list;
62 internal ContentAlignment image_align;
63 internal StringFormat string_format;
64 internal ContentAlignment text_align;
65 static SizeF req_witdthsize = new SizeF (0,0);
68 static object AutoSizeChangedEvent = new object ();
69 static object TextAlignChangedEvent = new object ();
74 [EditorBrowsable (EditorBrowsableState.Always)]
76 public new event EventHandler AutoSizeChanged {
77 add { Events.AddHandler (AutoSizeChangedEvent, value); }
78 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
82 [EditorBrowsable(EditorBrowsableState.Never)]
83 public new event EventHandler BackgroundImageChanged {
84 add { base.BackgroundImageChanged += value; }
85 remove { base.BackgroundImageChanged -= value; }
90 [EditorBrowsable (EditorBrowsableState.Never)]
91 public new event EventHandler BackgroundImageLayoutChanged {
92 add { base.BackgroundImageLayoutChanged += value; }
93 remove { base.BackgroundImageLayoutChanged -= value; }
98 [EditorBrowsable(EditorBrowsableState.Never)]
99 public new event EventHandler ImeModeChanged {
100 add { base.ImeModeChanged += value; }
101 remove { base.ImeModeChanged -= value; }
105 [EditorBrowsable(EditorBrowsableState.Never)]
106 public new event KeyEventHandler KeyDown {
107 add { base.KeyDown += value; }
108 remove { base.KeyDown -= value; }
112 [EditorBrowsable(EditorBrowsableState.Never)]
113 public new event KeyPressEventHandler KeyPress {
114 add { base.KeyPress += value; }
115 remove { base.KeyPress -= value; }
119 [EditorBrowsable(EditorBrowsableState.Never)]
120 public new event KeyEventHandler KeyUp {
121 add { base.KeyUp += value; }
122 remove { base.KeyUp -= value; }
126 [EditorBrowsable(EditorBrowsableState.Never)]
127 public new event EventHandler TabStopChanged {
128 add { base.TabStopChanged += value; }
129 remove { base.TabStopChanged -= value; }
132 public event EventHandler TextAlignChanged {
133 add { Events.AddHandler (TextAlignChangedEvent, value); }
134 remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
140 // Defaults in the Spec
143 string_format = new StringFormat();
144 TextAlign = ContentAlignment.TopLeft;
148 image_align = ContentAlignment.MiddleCenter;
149 SetUseMnemonic (UseMnemonic);
150 flat_style = FlatStyle.Standard;
152 SetStyle (ControlStyles.Selectable, false);
153 SetStyle (ControlStyles.ResizeRedraw |
154 ControlStyles.UserPaint |
155 ControlStyles.AllPaintingInWmPaint |
156 ControlStyles.SupportsTransparentBackColor
158 | ControlStyles.OptimizedDoubleBuffer
160 | ControlStyles.DoubleBuffer
164 HandleCreated += new EventHandler (OnHandleCreatedLB);
167 #region Public Properties
170 [DefaultValue (false)]
172 [EditorBrowsable (EditorBrowsableState.Always)]
173 public bool AutoEllipsis
175 get { return this.auto_ellipsis; }
178 if (this.auto_ellipsis != value) {
179 this.auto_ellipsis = value;
181 if (this.auto_ellipsis)
182 string_format.Trimming = StringTrimming.EllipsisCharacter;
184 string_format.Trimming = StringTrimming.Character;
192 [DefaultValue(false)]
194 [RefreshProperties(RefreshProperties.All)]
197 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
198 [EditorBrowsable (EditorBrowsableState.Always)]
200 public new virtual bool AutoSize {
201 get { return autosize; }
203 if (autosize == value)
207 base.SetAutoSizeMode (AutoSizeMode.GrowAndShrink);
208 base.AutoSize = value;
214 OnAutoSizeChanged (EventArgs.Empty);
219 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 public override Image BackgroundImage {
222 get { return base.BackgroundImage; }
224 base.BackgroundImage = value;
231 [EditorBrowsable (EditorBrowsableState.Never)]
232 public override ImageLayout BackgroundImageLayout {
233 get { return base.BackgroundImageLayout; }
234 set { base.BackgroundImageLayout = value; }
238 [DefaultValue(BorderStyle.None)]
240 public virtual BorderStyle BorderStyle {
241 get { return InternalBorderStyle; }
242 set { InternalBorderStyle = value; }
245 protected override CreateParams CreateParams {
247 CreateParams create_params = base.CreateParams;
249 if (BorderStyle != BorderStyle.Fixed3D)
250 return create_params;
252 create_params.ExStyle &= ~(int) WindowExStyles.WS_EX_CLIENTEDGE;
253 create_params.ExStyle |= (int)WindowExStyles.WS_EX_STATICEDGE;
255 return create_params;
259 protected override ImeMode DefaultImeMode {
260 get { return ImeMode.Disable;}
264 protected override Padding DefaultMargin {
265 get { return new Padding (3, 0, 3, 0); }
269 protected override Size DefaultSize {
270 get {return ThemeEngine.Current.LabelDefaultSize;}
273 [DefaultValue(FlatStyle.Standard)]
274 public FlatStyle FlatStyle {
275 get { return flat_style; }
277 if (!Enum.IsDefined (typeof (FlatStyle), value))
278 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
280 if (flat_style == value)
291 if (this.image != null)
294 if (this.image_index >= 0)
295 if (this.image_list != null)
296 return this.image_list.Images[this.image_index];
299 if (!string.IsNullOrEmpty (this.image_key))
300 if (this.image_list != null)
301 return this.image_list.Images[this.image_key];
306 if (this.image != value) {
308 this.image_index = -1;
310 this.image_key = string.Empty;
312 this.image_list = null;
315 if (this.AutoSize && this.Parent != null)
316 this.Parent.PerformLayout (this, "Image");
324 [DefaultValue(ContentAlignment.MiddleCenter)]
326 public ContentAlignment ImageAlign {
327 get { return image_align; }
329 if (!Enum.IsDefined (typeof (ContentAlignment), value))
330 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
332 if (image_align == value)
341 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
343 [TypeConverter (typeof (ImageIndexConverter))]
345 [RefreshProperties (RefreshProperties.Repaint)]
347 public int ImageIndex {
349 if (ImageList == null) {
353 if (image_index >= image_list.Images.Count) {
354 return image_list.Images.Count - 1;
362 throw new ArgumentException ();
364 if (this.image_index != value) {
365 this.image_index = value;
368 this.image_key = string.Empty;
378 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
379 [RefreshProperties (RefreshProperties.Repaint)]
380 [TypeConverter (typeof (ImageKeyConverter))]
381 public string ImageKey {
382 get { return this.image_key; }
384 if (this.image_key != value) {
386 this.image_index = -1;
387 this.image_key = value;
396 [RefreshProperties (RefreshProperties.Repaint)]
398 public ImageList ImageList {
399 get { return image_list;}
401 if (image_list == value)
406 if (image_list != null && image_index !=-1)
414 [EditorBrowsable(EditorBrowsableState.Never)]
415 public new ImeMode ImeMode {
416 get { return base.ImeMode; }
417 set { base.ImeMode = value; }
420 internal virtual Size InternalGetPreferredSize (Size proposed)
424 if (Text == string.Empty) {
425 size = new Size (0, Font.Height);
427 size = Size.Ceiling (TextRenderer.MeasureString (Text, Font, req_witdthsize, string_format));
432 if (!use_compatible_text_rendering)
435 size.Height = Font.Height;
438 if (border_style == BorderStyle.None)
447 public override Size GetPreferredSize (Size proposed)
449 return InternalGetPreferredSize (proposed);
454 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
455 [EditorBrowsable(EditorBrowsableState.Advanced)]
456 public virtual int PreferredHeight {
457 get { return InternalGetPreferredSize (Size.Empty).Height; }
461 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
462 [EditorBrowsable(EditorBrowsableState.Advanced)]
463 public virtual int PreferredWidth {
464 get { return InternalGetPreferredSize (Size.Empty).Width; }
468 [Obsolete ("This property has been deprecated. Use BackColor instead.")]
470 protected virtual bool RenderTransparent {
471 get { return render_transparent; }
472 set { render_transparent = value;}
476 [DefaultValue(false)]
477 [EditorBrowsable(EditorBrowsableState.Never)]
478 public new bool TabStop {
479 get { return base.TabStop; }
480 set { base.TabStop = value; }
483 [DefaultValue(ContentAlignment.TopLeft)]
485 public virtual ContentAlignment TextAlign {
486 get { return text_align; }
489 if (!Enum.IsDefined (typeof (ContentAlignment), value))
490 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
492 if (text_align != value) {
498 case ContentAlignment.BottomLeft:
499 string_format.LineAlignment = StringAlignment.Far;
500 string_format.Alignment = StringAlignment.Near;
502 case ContentAlignment.BottomCenter:
503 string_format.LineAlignment = StringAlignment.Far;
504 string_format.Alignment = StringAlignment.Center;
506 case ContentAlignment.BottomRight:
507 string_format.LineAlignment = StringAlignment.Far;
508 string_format.Alignment = StringAlignment.Far;
510 case ContentAlignment.TopLeft:
511 string_format.LineAlignment = StringAlignment.Near;
512 string_format.Alignment = StringAlignment.Near;
514 case ContentAlignment.TopCenter:
515 string_format.LineAlignment = StringAlignment.Near;
516 string_format.Alignment = StringAlignment.Center;
518 case ContentAlignment.TopRight:
519 string_format.LineAlignment = StringAlignment.Near;
520 string_format.Alignment = StringAlignment.Far;
522 case ContentAlignment.MiddleLeft:
523 string_format.LineAlignment = StringAlignment.Center;
524 string_format.Alignment = StringAlignment.Near;
526 case ContentAlignment.MiddleRight:
527 string_format.LineAlignment = StringAlignment.Center;
528 string_format.Alignment = StringAlignment.Far;
530 case ContentAlignment.MiddleCenter:
531 string_format.LineAlignment = StringAlignment.Center;
532 string_format.Alignment = StringAlignment.Center;
538 OnTextAlignChanged (EventArgs.Empty);
545 public bool UseMnemonic {
546 get { return use_mnemonic; }
548 if (use_mnemonic != value) {
549 use_mnemonic = value;
550 SetUseMnemonic (use_mnemonic);
558 #region Public Methods
560 protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
562 Rectangle rcImageClip = area;
563 rcImageClip.Inflate (-2,-2);
568 if (img_align == ContentAlignment.TopCenter ||
569 img_align == ContentAlignment.MiddleCenter ||
570 img_align == ContentAlignment.BottomCenter) {
571 X += (area.Width - image.Width) / 2;
573 else if (img_align == ContentAlignment.TopRight ||
574 img_align == ContentAlignment.MiddleRight||
575 img_align == ContentAlignment.BottomRight) {
576 X += (area.Width - image.Width);
579 if( img_align == ContentAlignment.BottomCenter ||
580 img_align == ContentAlignment.BottomLeft ||
581 img_align == ContentAlignment.BottomRight) {
582 Y += area.Height - image.Height;
584 else if(img_align == ContentAlignment.MiddleCenter ||
585 img_align == ContentAlignment.MiddleLeft ||
586 img_align == ContentAlignment.MiddleRight) {
587 Y += (area.Height - image.Height) / 2;
592 rcImageClip.Width = image.Width;
593 rcImageClip.Height = image.Height;
598 protected override AccessibleObject CreateAccessibilityInstance ()
600 return base.CreateAccessibilityInstance ();
603 protected override void Dispose(bool disposing)
605 base.Dispose (disposing);
608 string_format.Dispose ();
611 protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
613 if (image == null || g == null)
616 Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
619 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
621 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
625 protected virtual void OnAutoSizeChanged (EventArgs e)
627 EventHandler eh = (EventHandler)(Events [AutoSizeChangedEvent]);
633 protected override void OnEnabledChanged (EventArgs e)
635 base.OnEnabledChanged (e);
638 protected override void OnFontChanged (EventArgs e)
640 base.OnFontChanged (e);
647 protected override void OnPaddingChanged (EventArgs e)
649 base.OnPaddingChanged (e);
653 protected override void OnPaint (PaintEventArgs pevent)
655 ThemeEngine.Current.DrawLabel (pevent.Graphics, ClientRectangle, this);
656 DrawImage (pevent.Graphics, Image, ClientRectangle, image_align);
657 base.OnPaint(pevent);
660 protected override void OnParentChanged (EventArgs e)
662 base.OnParentChanged (e);
666 protected override void OnRightToLeftChanged (EventArgs e)
668 base.OnRightToLeftChanged (e);
672 protected virtual void OnTextAlignChanged (EventArgs e)
674 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
679 protected override void OnTextChanged (EventArgs e)
681 base.OnTextChanged (e);
687 protected override void OnVisibleChanged (EventArgs e)
689 base.OnVisibleChanged (e);
692 protected override bool ProcessMnemonic (char charCode)
694 if (IsMnemonic(charCode, Text) == true) {
695 // Select item next in line in tab order
696 if (this.Parent != null) {
697 Parent.SelectNextControl(this, true, false, false, false);
702 return base.ProcessMnemonic (charCode);
705 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
707 base.SetBoundsCore (x, y, width, height, specified);
710 public override string ToString()
712 return base.ToString () + ", Text: " + Text;
715 protected override void WndProc (ref Message m)
717 switch ((Msg) m.Msg) {
718 case Msg.WM_DRAWITEM: {
719 m.Result = (IntPtr)1;
723 base.WndProc (ref m);
728 #endregion Public Methods
730 #region Private Methods
732 private void CalcAutoSize ()
737 Size s = InternalGetPreferredSize (Size.Empty);
739 SetBounds (Left, Top, s.Width, s.Height, BoundsSpecified.None);
742 private void OnHandleCreatedLB (Object o, EventArgs e)
748 private void SetUseMnemonic (bool use)
751 string_format.HotkeyPrefix = HotkeyPrefix.Show;
753 string_format.HotkeyPrefix = HotkeyPrefix.None;
756 #endregion Private Methods
758 [DefaultValue (false)]
759 public bool UseCompatibleTextRendering {
760 get { return use_compatible_text_rendering; }
761 set { use_compatible_text_rendering = value; }
764 [SettingsBindable (true)]
765 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
766 typeof (System.Drawing.Design.UITypeEditor))]
767 public override string Text {
768 get { return base.Text; }
769 set { base.Text = value; }
772 protected override void OnMouseEnter (EventArgs e)
774 base.OnMouseEnter (e);
777 protected override void OnMouseLeave (EventArgs e)
779 base.OnMouseLeave (e);
782 protected override void OnHandleDestroyed (EventArgs e)
784 base.OnHandleDestroyed (e);