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)]
45 [DefaultBindingProperty ("Text")]
47 public class Label : Control
49 private bool autosize;
51 private bool render_transparent;
52 private FlatStyle flat_style;
53 private int preferred_height;
54 private int preferred_width;
55 private bool use_mnemonic;
56 private int image_index = -1;
57 private ImageList image_list;
58 internal ContentAlignment image_align;
59 internal StringFormat string_format;
60 internal ContentAlignment text_align;
61 static SizeF req_witdthsize = new SizeF (0,0);
64 static object AutoSizeChangedEvent = new object ();
65 static object TextAlignChangedEvent = new object ();
70 [EditorBrowsable (EditorBrowsableState.Always)]
72 public new event EventHandler AutoSizeChanged {
73 add { Events.AddHandler (AutoSizeChangedEvent, value); }
74 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
78 [EditorBrowsable(EditorBrowsableState.Never)]
79 public new event EventHandler BackgroundImageChanged {
80 add { base.BackgroundImageChanged += value; }
81 remove { base.BackgroundImageChanged -= value; }
85 [EditorBrowsable(EditorBrowsableState.Never)]
86 public new event EventHandler ImeModeChanged {
87 add { base.ImeModeChanged += value; }
88 remove { base.ImeModeChanged -= value; }
92 [EditorBrowsable(EditorBrowsableState.Never)]
93 public new event KeyEventHandler KeyDown {
94 add { base.KeyDown += value; }
95 remove { base.KeyDown -= value; }
100 [EditorBrowsable(EditorBrowsableState.Never)]
101 public new event KeyPressEventHandler KeyPress {
102 add { base.KeyPress += value; }
103 remove { base.KeyPress -= value; }
108 [EditorBrowsable(EditorBrowsableState.Never)]
109 public new event KeyEventHandler KeyUp {
110 add { base.KeyUp += value; }
111 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 TextAlign = ContentAlignment.TopLeft;
138 image_align = ContentAlignment.MiddleCenter;
139 SetUseMnemonic (UseMnemonic);
140 flat_style = FlatStyle.Standard;
142 CalcPreferredHeight ();
143 CalcPreferredWidth ();
145 SetStyle (ControlStyles.Selectable, false);
146 SetStyle (ControlStyles.ResizeRedraw |
147 ControlStyles.UserPaint |
148 ControlStyles.AllPaintingInWmPaint |
149 ControlStyles.SupportsTransparentBackColor
151 | ControlStyles.OptimizedDoubleBuffer
153 | ControlStyles.DoubleBuffer
157 HandleCreated += new EventHandler (OnHandleCreatedLB);
160 #region Public Properties
161 [DefaultValue(false)]
163 [RefreshProperties(RefreshProperties.All)]
166 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
167 [EditorBrowsable (EditorBrowsableState.Always)]
169 public new virtual bool AutoSize {
170 get { return autosize; }
172 if (autosize == value)
179 OnAutoSizeChanged (EventArgs.Empty);
184 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
185 [EditorBrowsable(EditorBrowsableState.Never)]
186 public override Image BackgroundImage {
188 return base.BackgroundImage;
191 base.BackgroundImage = value;
196 [DefaultValue(BorderStyle.None)]
198 public virtual BorderStyle BorderStyle {
199 get { return InternalBorderStyle; }
200 set { InternalBorderStyle = value; }
203 protected override CreateParams CreateParams {
204 get { return base.CreateParams;}
207 protected override ImeMode DefaultImeMode {
208 get { return ImeMode.Disable;}
211 protected override Size DefaultSize {
212 get {return ThemeEngine.Current.LabelDefaultSize;}
215 [DefaultValue(FlatStyle.Standard)]
216 public FlatStyle FlatStyle {
221 if (!Enum.IsDefined (typeof (FlatStyle), value))
222 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
224 if (flat_style == value)
239 if (image_list != null && ImageIndex >= 0) {
240 return image_list.Images[ImageIndex];
254 [DefaultValue(ContentAlignment.MiddleCenter)]
256 public ContentAlignment ImageAlign {
261 if (!Enum.IsDefined (typeof (ContentAlignment), value))
262 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
264 if (image_align == value)
273 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
275 [TypeConverter (typeof (ImageIndexConverter))]
277 [RefreshProperties (RefreshProperties.Repaint)]
279 public int ImageIndex {
281 if (ImageList == null) {
285 if (image_index >= image_list.Images.Count) {
286 return image_list.Images.Count - 1;
294 throw new ArgumentException ();
296 if (image_index == value)
301 if (ImageList != null && image_index !=-1)
310 [RefreshProperties (RefreshProperties.Repaint)]
312 public ImageList ImageList {
313 get { return image_list;}
315 if (image_list == value)
320 if (image_list != null && image_index !=-1)
328 [EditorBrowsable(EditorBrowsableState.Never)]
329 public new ImeMode ImeMode {
330 get { return base.ImeMode; }
331 set { base.ImeMode = value; }
335 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
336 [EditorBrowsable(EditorBrowsableState.Advanced)]
337 public virtual int PreferredHeight {
338 get { return preferred_height; }
342 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
343 [EditorBrowsable(EditorBrowsableState.Advanced)]
344 public virtual int PreferredWidth {
345 get {return preferred_width; }
349 [Obsolete ("This property has been deprecated. Use BackColor instead.")]
351 protected virtual bool RenderTransparent {
352 get { return render_transparent; }
353 set { render_transparent = value;}
357 [DefaultValue(false)]
358 [EditorBrowsable(EditorBrowsableState.Never)]
359 public new bool TabStop {
360 get { return base.TabStop; }
361 set { base.TabStop = value; }
364 [DefaultValue(ContentAlignment.TopLeft)]
366 public virtual ContentAlignment TextAlign {
367 get { return text_align; }
370 if (!Enum.IsDefined (typeof (ContentAlignment), value))
371 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
373 if (text_align != value) {
379 case ContentAlignment.BottomLeft:
380 string_format.LineAlignment = StringAlignment.Far;
381 string_format.Alignment = StringAlignment.Near;
383 case ContentAlignment.BottomCenter:
384 string_format.LineAlignment = StringAlignment.Far;
385 string_format.Alignment = StringAlignment.Center;
387 case ContentAlignment.BottomRight:
388 string_format.LineAlignment = StringAlignment.Far;
389 string_format.Alignment = StringAlignment.Far;
391 case ContentAlignment.TopLeft:
392 string_format.LineAlignment = StringAlignment.Near;
393 string_format.Alignment = StringAlignment.Near;
395 case ContentAlignment.TopCenter:
396 string_format.LineAlignment = StringAlignment.Near;
397 string_format.Alignment = StringAlignment.Center;
399 case ContentAlignment.TopRight:
400 string_format.LineAlignment = StringAlignment.Near;
401 string_format.Alignment = StringAlignment.Far;
403 case ContentAlignment.MiddleLeft:
404 string_format.LineAlignment = StringAlignment.Center;
405 string_format.Alignment = StringAlignment.Near;
407 case ContentAlignment.MiddleRight:
408 string_format.LineAlignment = StringAlignment.Center;
409 string_format.Alignment = StringAlignment.Far;
411 case ContentAlignment.MiddleCenter:
412 string_format.LineAlignment = StringAlignment.Center;
413 string_format.Alignment = StringAlignment.Center;
419 OnTextAlignChanged (EventArgs.Empty);
426 public bool UseMnemonic {
427 get { return use_mnemonic; }
429 if (use_mnemonic != value) {
430 use_mnemonic = value;
431 SetUseMnemonic (use_mnemonic);
440 #region Public Methods
442 protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
444 Rectangle rcImageClip = area;
445 rcImageClip.Inflate (-2,-2);
450 if (img_align == ContentAlignment.TopCenter ||
451 img_align == ContentAlignment.MiddleCenter ||
452 img_align == ContentAlignment.BottomCenter) {
453 X += (area.Width - image.Width) / 2;
455 else if (img_align == ContentAlignment.TopRight ||
456 img_align == ContentAlignment.MiddleRight||
457 img_align == ContentAlignment.BottomRight) {
458 X += (area.Width - image.Width);
461 if( img_align == ContentAlignment.BottomCenter ||
462 img_align == ContentAlignment.BottomLeft ||
463 img_align == ContentAlignment.BottomRight) {
464 Y += area.Height - image.Height;
466 else if(img_align == ContentAlignment.MiddleCenter ||
467 img_align == ContentAlignment.MiddleLeft ||
468 img_align == ContentAlignment.MiddleRight) {
469 Y += (area.Height - image.Height) / 2;
474 rcImageClip.Width = image.Width;
475 rcImageClip.Height = image.Height;
481 protected override AccessibleObject CreateAccessibilityInstance ()
483 return base.CreateAccessibilityInstance ();
486 protected override void Dispose(bool disposing)
488 base.Dispose (disposing);
491 string_format.Dispose ();
494 protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
496 if (image == null || g == null)
499 Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
502 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
504 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
507 protected virtual void OnAutoSizeChanged (EventArgs e)
509 EventHandler eh = (EventHandler)(Events [AutoSizeChangedEvent]);
514 protected override void OnEnabledChanged (EventArgs e)
516 base.OnEnabledChanged (e);
519 protected override void OnFontChanged (EventArgs e)
521 base.OnFontChanged (e);
525 CalcPreferredHeight ();
530 protected override void OnPaint (PaintEventArgs pevent)
532 ThemeEngine.Current.DrawLabel (pevent.Graphics, ClientRectangle, this);
533 DrawImage (pevent.Graphics, Image, ClientRectangle, image_align);
534 base.OnPaint(pevent);
537 protected override void OnParentChanged (EventArgs e)
539 base.OnParentChanged (e);
542 protected virtual void OnTextAlignChanged (EventArgs e)
544 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
549 protected override void OnTextChanged (EventArgs e)
551 base.OnTextChanged (e);
555 CalcPreferredWidth ();
560 protected override void OnVisibleChanged (EventArgs e)
562 base.OnVisibleChanged (e);
565 protected override bool ProcessMnemonic (char charCode)
567 if (IsMnemonic(charCode, Text) == true) {
568 // Select item next in line in tab order
569 if (this.Parent != null) {
570 Parent.SelectNextControl(this, true, false, false, false);
575 return base.ProcessMnemonic (charCode);
578 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
580 base.SetBoundsCore (x, y, width, height, specified);
583 public override string ToString()
585 return base.ToString () + ", Text: " + Text;
588 protected override void WndProc (ref Message m)
590 switch ((Msg) m.Msg) {
591 case Msg.WM_DRAWITEM: {
592 m.Result = (IntPtr)1;
596 base.WndProc (ref m);
601 #endregion Public Methods
603 #region Private Methods
605 private void CalcAutoSize ()
607 if (IsHandleCreated == false || AutoSize == false)
610 CalcPreferredWidth ();
611 CalcPreferredHeight ();
613 Width = PreferredWidth;
614 Height = PreferredHeight;
617 private void CalcPreferredHeight ()
619 preferred_height = Font.Height;
621 switch (border_style) {
622 case BorderStyle.None:
623 preferred_height += 3;
625 case BorderStyle.FixedSingle:
626 case BorderStyle.Fixed3D:
627 preferred_height += 6;
635 private void CalcPreferredWidth ()
637 if (Text == string.Empty) {
642 size = DeviceContext.MeasureString (Text, Font, req_witdthsize, string_format);
643 preferred_width = (int) size.Width + 3;
647 private void OnHandleCreatedLB (Object o, EventArgs e)
653 private void SetUseMnemonic (bool use)
656 string_format.HotkeyPrefix = HotkeyPrefix.Show;
658 string_format.HotkeyPrefix = HotkeyPrefix.None;
661 #endregion Private Methods
663 [DefaultValue (false)]
664 public bool UseCompatibleTextRendering {
666 return use_compatible_text_rendering;
670 use_compatible_text_rendering = value;
674 [SettingsBindable (true)]
675 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
676 typeof (System.Drawing.Design.UITypeEditor))]
677 public override string Text {
687 protected override void OnMouseEnter (EventArgs e)
689 base.OnMouseEnter (e);
692 protected override void OnMouseLeave (EventArgs e)
694 base.OnMouseLeave (e);
697 protected override void OnHandleDestroyed (EventArgs e)
699 base.OnHandleDestroyed (e);