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; }
99 [EditorBrowsable(EditorBrowsableState.Never)]
100 public new event KeyPressEventHandler KeyPress {
101 add { base.KeyPress += value; }
102 remove { base.KeyPress -= value; }
106 [EditorBrowsable(EditorBrowsableState.Never)]
107 public new event KeyEventHandler KeyUp {
108 add { base.KeyUp += value; }
109 remove { base.KeyUp -= value; }
113 [EditorBrowsable(EditorBrowsableState.Never)]
114 public new event EventHandler TabStopChanged {
115 add { base.TabStopChanged += value; }
116 remove { base.TabStopChanged -= value; }
119 public event EventHandler TextAlignChanged {
120 add { Events.AddHandler (TextAlignChangedEvent, value); }
121 remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
127 // Defaults in the Spec
130 string_format = new StringFormat();
131 TextAlign = ContentAlignment.TopLeft;
135 image_align = ContentAlignment.MiddleCenter;
136 SetUseMnemonic (UseMnemonic);
137 flat_style = FlatStyle.Standard;
139 CalcPreferredHeight ();
140 CalcPreferredWidth ();
142 SetStyle (ControlStyles.Selectable, false);
143 SetStyle (ControlStyles.ResizeRedraw |
144 ControlStyles.UserPaint |
145 ControlStyles.AllPaintingInWmPaint |
146 ControlStyles.SupportsTransparentBackColor
148 | ControlStyles.OptimizedDoubleBuffer
150 | ControlStyles.DoubleBuffer
154 HandleCreated += new EventHandler (OnHandleCreatedLB);
157 #region Public Properties
158 [DefaultValue(false)]
160 [RefreshProperties(RefreshProperties.All)]
163 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
164 [EditorBrowsable (EditorBrowsableState.Always)]
166 public new virtual bool AutoSize {
167 get { return autosize; }
169 if (autosize == value)
176 OnAutoSizeChanged (EventArgs.Empty);
181 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
182 [EditorBrowsable(EditorBrowsableState.Never)]
183 public override Image BackgroundImage {
184 get { return base.BackgroundImage; }
186 base.BackgroundImage = value;
191 [DefaultValue(BorderStyle.None)]
193 public virtual BorderStyle BorderStyle {
194 get { return InternalBorderStyle; }
195 set { InternalBorderStyle = value; }
198 protected override CreateParams CreateParams {
200 CreateParams create_params = base.CreateParams;
202 if (BorderStyle != BorderStyle.Fixed3D)
203 return create_params;
205 create_params.ExStyle &= ~(int) WindowExStyles.WS_EX_CLIENTEDGE;
206 create_params.ExStyle |= (int)WindowExStyles.WS_EX_STATICEDGE;
208 return create_params;
212 protected override ImeMode DefaultImeMode {
213 get { return ImeMode.Disable;}
216 protected override Size DefaultSize {
217 get {return ThemeEngine.Current.LabelDefaultSize;}
220 [DefaultValue(FlatStyle.Standard)]
221 public FlatStyle FlatStyle {
222 get { return flat_style; }
224 if (!Enum.IsDefined (typeof (FlatStyle), value))
225 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
227 if (flat_style == value)
242 if (image_list != null && ImageIndex >= 0) {
243 return image_list.Images[ImageIndex];
257 [DefaultValue(ContentAlignment.MiddleCenter)]
259 public ContentAlignment ImageAlign {
260 get { return image_align; }
262 if (!Enum.IsDefined (typeof (ContentAlignment), value))
263 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
265 if (image_align == value)
274 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
276 [TypeConverter (typeof (ImageIndexConverter))]
278 [RefreshProperties (RefreshProperties.Repaint)]
280 public int ImageIndex {
282 if (ImageList == null) {
286 if (image_index >= image_list.Images.Count) {
287 return image_list.Images.Count - 1;
295 throw new ArgumentException ();
297 if (image_index == value)
302 if (ImageList != null && image_index !=-1)
311 [RefreshProperties (RefreshProperties.Repaint)]
313 public ImageList ImageList {
314 get { return image_list;}
316 if (image_list == value)
321 if (image_list != null && image_index !=-1)
329 [EditorBrowsable(EditorBrowsableState.Never)]
330 public new ImeMode ImeMode {
331 get { return base.ImeMode; }
332 set { base.ImeMode = value; }
336 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
337 [EditorBrowsable(EditorBrowsableState.Advanced)]
338 public virtual int PreferredHeight {
339 get { return preferred_height; }
343 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
344 [EditorBrowsable(EditorBrowsableState.Advanced)]
345 public virtual int PreferredWidth {
346 get {return preferred_width; }
350 [Obsolete ("This property has been deprecated. Use BackColor instead.")]
352 protected virtual bool RenderTransparent {
353 get { return render_transparent; }
354 set { render_transparent = value;}
358 [DefaultValue(false)]
359 [EditorBrowsable(EditorBrowsableState.Never)]
360 public new bool TabStop {
361 get { return base.TabStop; }
362 set { base.TabStop = value; }
365 [DefaultValue(ContentAlignment.TopLeft)]
367 public virtual ContentAlignment TextAlign {
368 get { return text_align; }
371 if (!Enum.IsDefined (typeof (ContentAlignment), value))
372 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
374 if (text_align != value) {
380 case ContentAlignment.BottomLeft:
381 string_format.LineAlignment = StringAlignment.Far;
382 string_format.Alignment = StringAlignment.Near;
384 case ContentAlignment.BottomCenter:
385 string_format.LineAlignment = StringAlignment.Far;
386 string_format.Alignment = StringAlignment.Center;
388 case ContentAlignment.BottomRight:
389 string_format.LineAlignment = StringAlignment.Far;
390 string_format.Alignment = StringAlignment.Far;
392 case ContentAlignment.TopLeft:
393 string_format.LineAlignment = StringAlignment.Near;
394 string_format.Alignment = StringAlignment.Near;
396 case ContentAlignment.TopCenter:
397 string_format.LineAlignment = StringAlignment.Near;
398 string_format.Alignment = StringAlignment.Center;
400 case ContentAlignment.TopRight:
401 string_format.LineAlignment = StringAlignment.Near;
402 string_format.Alignment = StringAlignment.Far;
404 case ContentAlignment.MiddleLeft:
405 string_format.LineAlignment = StringAlignment.Center;
406 string_format.Alignment = StringAlignment.Near;
408 case ContentAlignment.MiddleRight:
409 string_format.LineAlignment = StringAlignment.Center;
410 string_format.Alignment = StringAlignment.Far;
412 case ContentAlignment.MiddleCenter:
413 string_format.LineAlignment = StringAlignment.Center;
414 string_format.Alignment = StringAlignment.Center;
420 OnTextAlignChanged (EventArgs.Empty);
427 public bool UseMnemonic {
428 get { return use_mnemonic; }
430 if (use_mnemonic != value) {
431 use_mnemonic = value;
432 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;
480 protected override AccessibleObject CreateAccessibilityInstance ()
482 return base.CreateAccessibilityInstance ();
485 protected override void Dispose(bool disposing)
487 base.Dispose (disposing);
490 string_format.Dispose ();
493 protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
495 if (image == null || g == null)
498 Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
501 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
503 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
507 protected virtual void OnAutoSizeChanged (EventArgs e)
509 EventHandler eh = (EventHandler)(Events [AutoSizeChangedEvent]);
515 protected override void OnEnabledChanged (EventArgs e)
517 base.OnEnabledChanged (e);
520 protected override void OnFontChanged (EventArgs e)
522 base.OnFontChanged (e);
526 CalcPreferredHeight ();
531 protected override void OnPaint (PaintEventArgs pevent)
533 ThemeEngine.Current.DrawLabel (pevent.Graphics, ClientRectangle, this);
534 DrawImage (pevent.Graphics, Image, ClientRectangle, image_align);
535 base.OnPaint(pevent);
538 protected override void OnParentChanged (EventArgs e)
540 base.OnParentChanged (e);
543 protected virtual void OnTextAlignChanged (EventArgs e)
545 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
550 protected override void OnTextChanged (EventArgs e)
552 base.OnTextChanged (e);
556 CalcPreferredWidth ();
561 protected override void OnVisibleChanged (EventArgs e)
563 base.OnVisibleChanged (e);
566 protected override bool ProcessMnemonic (char charCode)
568 if (IsMnemonic(charCode, Text) == true) {
569 // Select item next in line in tab order
570 if (this.Parent != null) {
571 Parent.SelectNextControl(this, true, false, false, false);
576 return base.ProcessMnemonic (charCode);
579 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
581 base.SetBoundsCore (x, y, width, height, specified);
584 public override string ToString()
586 return base.ToString () + ", Text: " + Text;
589 protected override void WndProc (ref Message m)
591 switch ((Msg) m.Msg) {
592 case Msg.WM_DRAWITEM: {
593 m.Result = (IntPtr)1;
597 base.WndProc (ref m);
602 #endregion Public Methods
604 #region Private Methods
606 private void CalcAutoSize ()
608 if (IsHandleCreated == false || AutoSize == false)
611 CalcPreferredWidth ();
612 CalcPreferredHeight ();
614 Width = PreferredWidth;
615 Height = PreferredHeight;
618 private void CalcPreferredHeight ()
620 preferred_height = Font.Height;
623 if (!use_compatible_text_rendering)
627 switch (border_style) {
628 case BorderStyle.None:
629 preferred_height += 3;
631 case BorderStyle.FixedSingle:
632 case BorderStyle.Fixed3D:
633 preferred_height += 6;
640 private void CalcPreferredWidth ()
642 if (Text == string.Empty) {
647 size = DeviceContext.MeasureString (Text, Font, req_witdthsize, string_format);
648 preferred_width = (int) size.Width + 3;
652 private void OnHandleCreatedLB (Object o, EventArgs e)
658 private void SetUseMnemonic (bool use)
661 string_format.HotkeyPrefix = HotkeyPrefix.Show;
663 string_format.HotkeyPrefix = HotkeyPrefix.None;
666 #endregion Private Methods
668 [DefaultValue (false)]
669 public bool UseCompatibleTextRendering {
670 get { return use_compatible_text_rendering; }
671 set { 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 {
678 get { return base.Text; }
679 set { base.Text = value; }
682 protected override void OnMouseEnter (EventArgs e)
684 base.OnMouseEnter (e);
687 protected override void OnMouseLeave (EventArgs e)
689 base.OnMouseLeave (e);
692 protected override void OnHandleDestroyed (EventArgs e)
694 base.OnHandleDestroyed (e);