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")]
43 [ClassInterface (ClassInterfaceType.AutoDispatch)]
45 [ToolboxItem ("System.Windows.Forms.Design.AutoSizeToolboxItem," + Consts.AssemblySystem_Design)]
46 [DefaultBindingProperty ("Text")]
48 public class Label : Control
50 private bool autosize;
51 private bool auto_ellipsis;
53 private bool render_transparent;
54 private FlatStyle flat_style;
55 private bool use_mnemonic;
56 private int image_index = -1;
58 private string image_key = string.Empty;
60 private ImageList image_list;
61 internal ContentAlignment image_align;
62 internal StringFormat string_format;
63 internal ContentAlignment text_align;
64 static SizeF req_witdthsize = new SizeF (0,0);
67 static object AutoSizeChangedEvent = new object ();
68 static object TextAlignChangedEvent = new object ();
73 [EditorBrowsable (EditorBrowsableState.Always)]
79 event EventHandler AutoSizeChanged {
80 add { Events.AddHandler (AutoSizeChangedEvent, value); }
81 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
85 [EditorBrowsable(EditorBrowsableState.Never)]
86 public new event EventHandler BackgroundImageChanged {
87 add { base.BackgroundImageChanged += value; }
88 remove { base.BackgroundImageChanged -= value; }
93 [EditorBrowsable (EditorBrowsableState.Never)]
94 public new event EventHandler BackgroundImageLayoutChanged {
95 add { base.BackgroundImageLayoutChanged += value; }
96 remove { base.BackgroundImageLayoutChanged -= value; }
101 [EditorBrowsable(EditorBrowsableState.Never)]
102 public new event EventHandler ImeModeChanged {
103 add { base.ImeModeChanged += value; }
104 remove { base.ImeModeChanged -= value; }
108 [EditorBrowsable(EditorBrowsableState.Never)]
109 public new event KeyEventHandler KeyDown {
110 add { base.KeyDown += value; }
111 remove { base.KeyDown -= value; }
115 [EditorBrowsable(EditorBrowsableState.Never)]
116 public new event KeyPressEventHandler KeyPress {
117 add { base.KeyPress += value; }
118 remove { base.KeyPress -= value; }
122 [EditorBrowsable(EditorBrowsableState.Never)]
123 public new event KeyEventHandler KeyUp {
124 add { base.KeyUp += value; }
125 remove { base.KeyUp -= value; }
129 [EditorBrowsable(EditorBrowsableState.Never)]
130 public new event EventHandler TabStopChanged {
131 add { base.TabStopChanged += value; }
132 remove { base.TabStopChanged -= value; }
135 public event EventHandler TextAlignChanged {
136 add { Events.AddHandler (TextAlignChangedEvent, value); }
137 remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
143 // Defaults in the Spec
146 string_format = new StringFormat();
147 string_format.FormatFlags = StringFormatFlags.LineLimit;
148 TextAlign = ContentAlignment.TopLeft;
152 image_align = ContentAlignment.MiddleCenter;
153 SetUseMnemonic (UseMnemonic);
154 flat_style = FlatStyle.Standard;
156 SetStyle (ControlStyles.Selectable, false);
157 SetStyle (ControlStyles.ResizeRedraw |
158 ControlStyles.UserPaint |
159 ControlStyles.AllPaintingInWmPaint |
160 ControlStyles.SupportsTransparentBackColor
162 | ControlStyles.OptimizedDoubleBuffer
164 | ControlStyles.DoubleBuffer
168 HandleCreated += new EventHandler (OnHandleCreatedLB);
171 #region Public Properties
173 [DefaultValue (false)]
175 [EditorBrowsable (EditorBrowsableState.Always)]
182 get { return this.auto_ellipsis; }
185 if (this.auto_ellipsis != value) {
186 this.auto_ellipsis = value;
188 if (this.auto_ellipsis)
189 string_format.Trimming = StringTrimming.EllipsisCharacter;
191 string_format.Trimming = StringTrimming.Character;
194 Parent.PerformLayout (this, "AutoEllipsis");
202 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
203 [EditorBrowsable (EditorBrowsableState.Always)]
205 [DefaultValue(false)]
207 [RefreshProperties(RefreshProperties.All)]
215 get { return autosize; }
217 if (autosize == value)
221 base.SetAutoSizeMode (AutoSizeMode.GrowAndShrink);
222 base.AutoSize = value;
228 OnAutoSizeChanged (EventArgs.Empty);
233 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
234 [EditorBrowsable(EditorBrowsableState.Never)]
235 public override Image BackgroundImage {
236 get { return base.BackgroundImage; }
238 base.BackgroundImage = value;
245 [EditorBrowsable (EditorBrowsableState.Never)]
246 public override ImageLayout BackgroundImageLayout {
247 get { return base.BackgroundImageLayout; }
248 set { base.BackgroundImageLayout = value; }
252 [DefaultValue(BorderStyle.None)]
254 public virtual BorderStyle BorderStyle {
255 get { return InternalBorderStyle; }
256 set { InternalBorderStyle = value; }
259 protected override CreateParams CreateParams {
261 CreateParams create_params = base.CreateParams;
263 if (BorderStyle != BorderStyle.Fixed3D)
264 return create_params;
266 create_params.ExStyle &= ~(int) WindowExStyles.WS_EX_CLIENTEDGE;
267 create_params.ExStyle |= (int)WindowExStyles.WS_EX_STATICEDGE;
269 return create_params;
273 protected override ImeMode DefaultImeMode {
274 get { return ImeMode.Disable;}
278 protected override Padding DefaultMargin {
279 get { return new Padding (3, 0, 3, 0); }
283 protected override Size DefaultSize {
284 get { return ThemeElements.LabelPainter.DefaultSize; }
287 [DefaultValue(FlatStyle.Standard)]
288 public FlatStyle FlatStyle {
289 get { return flat_style; }
291 if (!Enum.IsDefined (typeof (FlatStyle), value))
292 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
294 if (flat_style == value)
299 Parent.PerformLayout (this, "FlatStyle");
307 if (this.image != null)
310 if (this.image_index >= 0)
311 if (this.image_list != null)
312 return this.image_list.Images[this.image_index];
315 if (!string.IsNullOrEmpty (this.image_key))
316 if (this.image_list != null)
317 return this.image_list.Images[this.image_key];
322 if (this.image != value) {
324 this.image_index = -1;
326 this.image_key = string.Empty;
328 this.image_list = null;
331 if (this.AutoSize && this.Parent != null)
332 this.Parent.PerformLayout (this, "Image");
340 [DefaultValue(ContentAlignment.MiddleCenter)]
342 public ContentAlignment ImageAlign {
343 get { return image_align; }
345 if (!Enum.IsDefined (typeof (ContentAlignment), value))
346 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
348 if (image_align == value)
357 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
359 [TypeConverter (typeof (ImageIndexConverter))]
361 [RefreshProperties (RefreshProperties.Repaint)]
363 public int ImageIndex {
365 if (ImageList == null) {
369 if (image_index >= image_list.Images.Count) {
370 return image_list.Images.Count - 1;
378 throw new ArgumentException ();
380 if (this.image_index != value) {
381 this.image_index = value;
384 this.image_key = string.Empty;
394 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
395 [RefreshProperties (RefreshProperties.Repaint)]
396 [TypeConverter (typeof (ImageKeyConverter))]
397 public string ImageKey {
398 get { return this.image_key; }
400 if (this.image_key != value) {
402 this.image_index = -1;
403 this.image_key = value;
412 [RefreshProperties (RefreshProperties.Repaint)]
414 public ImageList ImageList {
415 get { return image_list;}
417 if (image_list == value)
422 if (image_list != null && image_index !=-1)
430 [EditorBrowsable(EditorBrowsableState.Never)]
431 public new ImeMode ImeMode {
432 get { return base.ImeMode; }
433 set { base.ImeMode = value; }
436 internal virtual Size InternalGetPreferredSize (Size proposed)
440 if (Text == string.Empty) {
441 size = new Size (0, Font.Height);
443 size = Size.Ceiling (TextRenderer.MeasureString (Text, Font, req_witdthsize, string_format));
448 size.Width += Padding.Horizontal;
449 size.Height += Padding.Vertical;
451 if (!use_compatible_text_rendering)
454 size.Height = Font.Height;
457 if (border_style == BorderStyle.None)
466 public override Size GetPreferredSize (Size proposedSize)
468 return InternalGetPreferredSize (proposedSize);
473 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
474 [EditorBrowsable(EditorBrowsableState.Advanced)]
475 public virtual int PreferredHeight {
476 get { return InternalGetPreferredSize (Size.Empty).Height; }
480 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
481 [EditorBrowsable(EditorBrowsableState.Advanced)]
482 public virtual int PreferredWidth {
483 get { return InternalGetPreferredSize (Size.Empty).Width; }
487 [Obsolete ("This property has been deprecated. Use BackColor instead.")]
489 protected virtual bool RenderTransparent {
490 get { return render_transparent; }
491 set { render_transparent = value;}
495 [DefaultValue(false)]
496 [EditorBrowsable(EditorBrowsableState.Never)]
497 public new bool TabStop {
498 get { return base.TabStop; }
499 set { base.TabStop = value; }
502 [DefaultValue(ContentAlignment.TopLeft)]
504 public virtual ContentAlignment TextAlign {
505 get { return text_align; }
508 if (!Enum.IsDefined (typeof (ContentAlignment), value))
509 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
511 if (text_align != value) {
514 case ContentAlignment.BottomLeft:
515 string_format.LineAlignment = StringAlignment.Far;
516 string_format.Alignment = StringAlignment.Near;
518 case ContentAlignment.BottomCenter:
519 string_format.LineAlignment = StringAlignment.Far;
520 string_format.Alignment = StringAlignment.Center;
522 case ContentAlignment.BottomRight:
523 string_format.LineAlignment = StringAlignment.Far;
524 string_format.Alignment = StringAlignment.Far;
526 case ContentAlignment.TopLeft:
527 string_format.LineAlignment = StringAlignment.Near;
528 string_format.Alignment = StringAlignment.Near;
530 case ContentAlignment.TopCenter:
531 string_format.LineAlignment = StringAlignment.Near;
532 string_format.Alignment = StringAlignment.Center;
534 case ContentAlignment.TopRight:
535 string_format.LineAlignment = StringAlignment.Near;
536 string_format.Alignment = StringAlignment.Far;
538 case ContentAlignment.MiddleLeft:
539 string_format.LineAlignment = StringAlignment.Center;
540 string_format.Alignment = StringAlignment.Near;
542 case ContentAlignment.MiddleRight:
543 string_format.LineAlignment = StringAlignment.Center;
544 string_format.Alignment = StringAlignment.Far;
546 case ContentAlignment.MiddleCenter:
547 string_format.LineAlignment = StringAlignment.Center;
548 string_format.Alignment = StringAlignment.Center;
554 OnTextAlignChanged (EventArgs.Empty);
561 public bool UseMnemonic {
562 get { return use_mnemonic; }
564 if (use_mnemonic != value) {
565 use_mnemonic = value;
566 SetUseMnemonic (use_mnemonic);
574 #region Public Methods
576 protected Rectangle CalcImageRenderBounds (Image image, Rectangle r, ContentAlignment align)
578 Rectangle rcImageClip = r;
579 rcImageClip.Inflate (-2,-2);
584 if (align == ContentAlignment.TopCenter ||
585 align == ContentAlignment.MiddleCenter ||
586 align == ContentAlignment.BottomCenter) {
587 X += (r.Width - image.Width) / 2;
588 } else if (align == ContentAlignment.TopRight ||
589 align == ContentAlignment.MiddleRight||
590 align == ContentAlignment.BottomRight) {
591 X += (r.Width - image.Width);
594 if( align == ContentAlignment.BottomCenter ||
595 align == ContentAlignment.BottomLeft ||
596 align == ContentAlignment.BottomRight) {
597 Y += r.Height - image.Height;
598 } else if(align == ContentAlignment.MiddleCenter ||
599 align == ContentAlignment.MiddleLeft ||
600 align == ContentAlignment.MiddleRight) {
601 Y += (r.Height - image.Height) / 2;
606 rcImageClip.Width = image.Width;
607 rcImageClip.Height = image.Height;
612 protected override AccessibleObject CreateAccessibilityInstance ()
614 return base.CreateAccessibilityInstance ();
617 protected override void Dispose(bool disposing)
619 base.Dispose (disposing);
622 string_format.Dispose ();
625 protected internal void DrawImage (Graphics g, Image image, Rectangle r, ContentAlignment align)
627 if (image == null || g == null)
630 Rectangle rcImageClip = CalcImageRenderBounds (image, r, align);
633 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
635 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
639 protected virtual void OnAutoSizeChanged (EventArgs e)
641 EventHandler eh = (EventHandler)(Events [AutoSizeChangedEvent]);
647 protected override void OnEnabledChanged (EventArgs e)
649 base.OnEnabledChanged (e);
652 protected override void OnFontChanged (EventArgs e)
654 base.OnFontChanged (e);
661 protected override void OnPaddingChanged (EventArgs e)
663 base.OnPaddingChanged (e);
667 protected override void OnPaint (PaintEventArgs e)
669 ThemeElements.LabelPainter.Draw (e.Graphics, ClientRectangle, this);
673 protected override void OnParentChanged (EventArgs e)
675 base.OnParentChanged (e);
679 protected override void OnRightToLeftChanged (EventArgs e)
681 base.OnRightToLeftChanged (e);
685 protected virtual void OnTextAlignChanged (EventArgs e)
687 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
692 protected override void OnTextChanged (EventArgs e)
694 base.OnTextChanged (e);
700 protected override void OnVisibleChanged (EventArgs e)
702 base.OnVisibleChanged (e);
705 protected override bool ProcessMnemonic (char charCode)
707 if (IsMnemonic (charCode, Text)) {
708 // Select item next in line in tab order
709 if (this.Parent != null)
710 Parent.SelectNextControl(this, true, false, false, false);
714 return base.ProcessMnemonic (charCode);
717 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
719 base.SetBoundsCore (x, y, width, height, specified);
722 public override string ToString()
724 return base.ToString () + ", Text: " + Text;
727 protected override void WndProc (ref Message m)
729 switch ((Msg) m.Msg) {
730 case Msg.WM_DRAWITEM:
731 m.Result = (IntPtr)1;
734 base.WndProc (ref m);
739 #endregion Public Methods
741 #region Private Methods
743 private void CalcAutoSize ()
748 Size s = InternalGetPreferredSize (Size.Empty);
750 SetBounds (Left, Top, s.Width, s.Height, BoundsSpecified.Size);
753 private void OnHandleCreatedLB (Object o, EventArgs e)
759 private void SetUseMnemonic (bool use)
762 string_format.HotkeyPrefix = HotkeyPrefix.Show;
764 string_format.HotkeyPrefix = HotkeyPrefix.None;
767 #endregion Private Methods
769 [DefaultValue (false)]
770 public bool UseCompatibleTextRendering {
771 get { return use_compatible_text_rendering; }
772 set { use_compatible_text_rendering = value; }
775 [SettingsBindable (true)]
776 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
777 typeof (System.Drawing.Design.UITypeEditor))]
778 public override string Text {
779 get { return base.Text; }
780 set { base.Text = value; }
783 protected override void OnMouseEnter (EventArgs e)
785 base.OnMouseEnter (e);
788 protected override void OnMouseLeave (EventArgs e)
790 base.OnMouseLeave (e);
793 protected override void OnHandleDestroyed (EventArgs e)
795 base.OnHandleDestroyed (e);