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 bool use_mnemonic;
54 private int image_index = -1;
55 private ImageList image_list;
56 internal ContentAlignment image_align;
57 internal StringFormat string_format;
58 internal ContentAlignment text_align;
59 static SizeF req_witdthsize = new SizeF (0,0);
62 static object AutoSizeChangedEvent = new object ();
63 static object TextAlignChangedEvent = new object ();
68 [EditorBrowsable (EditorBrowsableState.Always)]
70 public new event EventHandler AutoSizeChanged {
71 add { Events.AddHandler (AutoSizeChangedEvent, value); }
72 remove { Events.RemoveHandler (AutoSizeChangedEvent, value); }
76 [EditorBrowsable(EditorBrowsableState.Never)]
77 public new event EventHandler BackgroundImageChanged {
78 add { base.BackgroundImageChanged += value; }
79 remove { base.BackgroundImageChanged -= value; }
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 public new event EventHandler ImeModeChanged {
85 add { base.ImeModeChanged += value; }
86 remove { base.ImeModeChanged -= value; }
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 public new event KeyEventHandler KeyDown {
92 add { base.KeyDown += value; }
93 remove { base.KeyDown -= value; }
97 [EditorBrowsable(EditorBrowsableState.Never)]
98 public new event KeyPressEventHandler KeyPress {
99 add { base.KeyPress += value; }
100 remove { base.KeyPress -= value; }
104 [EditorBrowsable(EditorBrowsableState.Never)]
105 public new event KeyEventHandler KeyUp {
106 add { base.KeyUp += value; }
107 remove { base.KeyUp -= value; }
111 [EditorBrowsable(EditorBrowsableState.Never)]
112 public new event EventHandler TabStopChanged {
113 add { base.TabStopChanged += value; }
114 remove { base.TabStopChanged -= value; }
117 public event EventHandler TextAlignChanged {
118 add { Events.AddHandler (TextAlignChangedEvent, value); }
119 remove { Events.RemoveHandler (TextAlignChangedEvent, value); }
125 // Defaults in the Spec
128 string_format = new StringFormat();
129 TextAlign = ContentAlignment.TopLeft;
133 image_align = ContentAlignment.MiddleCenter;
134 SetUseMnemonic (UseMnemonic);
135 flat_style = FlatStyle.Standard;
137 SetStyle (ControlStyles.Selectable, false);
138 SetStyle (ControlStyles.ResizeRedraw |
139 ControlStyles.UserPaint |
140 ControlStyles.AllPaintingInWmPaint |
141 ControlStyles.SupportsTransparentBackColor
143 | ControlStyles.OptimizedDoubleBuffer
145 | ControlStyles.DoubleBuffer
149 HandleCreated += new EventHandler (OnHandleCreatedLB);
152 #region Public Properties
153 [DefaultValue(false)]
155 [RefreshProperties(RefreshProperties.All)]
158 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
159 [EditorBrowsable (EditorBrowsableState.Always)]
161 public new virtual bool AutoSize {
162 get { return autosize; }
164 if (autosize == value)
171 OnAutoSizeChanged (EventArgs.Empty);
176 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
177 [EditorBrowsable(EditorBrowsableState.Never)]
178 public override Image BackgroundImage {
179 get { return base.BackgroundImage; }
181 base.BackgroundImage = value;
186 [DefaultValue(BorderStyle.None)]
188 public virtual BorderStyle BorderStyle {
189 get { return InternalBorderStyle; }
190 set { InternalBorderStyle = value; }
193 protected override CreateParams CreateParams {
195 CreateParams create_params = base.CreateParams;
197 if (BorderStyle != BorderStyle.Fixed3D)
198 return create_params;
200 create_params.ExStyle &= ~(int) WindowExStyles.WS_EX_CLIENTEDGE;
201 create_params.ExStyle |= (int)WindowExStyles.WS_EX_STATICEDGE;
203 return create_params;
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 {
217 get { return flat_style; }
219 if (!Enum.IsDefined (typeof (FlatStyle), value))
220 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
222 if (flat_style == value)
237 if (image_list != null && ImageIndex >= 0) {
238 return image_list.Images[ImageIndex];
252 [DefaultValue(ContentAlignment.MiddleCenter)]
254 public ContentAlignment ImageAlign {
255 get { return image_align; }
257 if (!Enum.IsDefined (typeof (ContentAlignment), value))
258 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
260 if (image_align == value)
269 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
271 [TypeConverter (typeof (ImageIndexConverter))]
273 [RefreshProperties (RefreshProperties.Repaint)]
275 public int ImageIndex {
277 if (ImageList == null) {
281 if (image_index >= image_list.Images.Count) {
282 return image_list.Images.Count - 1;
290 throw new ArgumentException ();
292 if (image_index == value)
297 if (ImageList != null && image_index !=-1)
306 [RefreshProperties (RefreshProperties.Repaint)]
308 public ImageList ImageList {
309 get { return image_list;}
311 if (image_list == value)
316 if (image_list != null && image_index !=-1)
324 [EditorBrowsable(EditorBrowsableState.Never)]
325 public new ImeMode ImeMode {
326 get { return base.ImeMode; }
327 set { base.ImeMode = value; }
331 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
332 [EditorBrowsable(EditorBrowsableState.Advanced)]
333 public virtual int PreferredHeight {
335 int preferred_height = Font.Height;
338 if (!use_compatible_text_rendering)
339 return preferred_height;
342 if (border_style == BorderStyle.None)
343 return preferred_height + 3;
345 return preferred_height + 6;
350 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
351 [EditorBrowsable(EditorBrowsableState.Advanced)]
352 public virtual int PreferredWidth {
354 if (Text == string.Empty)
358 size = DeviceContext.MeasureString (Text, Font, req_witdthsize, string_format);
359 return (int) size.Width + 3;
364 [Obsolete ("This property has been deprecated. Use BackColor instead.")]
366 protected virtual bool RenderTransparent {
367 get { return render_transparent; }
368 set { render_transparent = value;}
372 [DefaultValue(false)]
373 [EditorBrowsable(EditorBrowsableState.Never)]
374 public new bool TabStop {
375 get { return base.TabStop; }
376 set { base.TabStop = value; }
379 [DefaultValue(ContentAlignment.TopLeft)]
381 public virtual ContentAlignment TextAlign {
382 get { return text_align; }
385 if (!Enum.IsDefined (typeof (ContentAlignment), value))
386 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
388 if (text_align != value) {
394 case ContentAlignment.BottomLeft:
395 string_format.LineAlignment = StringAlignment.Far;
396 string_format.Alignment = StringAlignment.Near;
398 case ContentAlignment.BottomCenter:
399 string_format.LineAlignment = StringAlignment.Far;
400 string_format.Alignment = StringAlignment.Center;
402 case ContentAlignment.BottomRight:
403 string_format.LineAlignment = StringAlignment.Far;
404 string_format.Alignment = StringAlignment.Far;
406 case ContentAlignment.TopLeft:
407 string_format.LineAlignment = StringAlignment.Near;
408 string_format.Alignment = StringAlignment.Near;
410 case ContentAlignment.TopCenter:
411 string_format.LineAlignment = StringAlignment.Near;
412 string_format.Alignment = StringAlignment.Center;
414 case ContentAlignment.TopRight:
415 string_format.LineAlignment = StringAlignment.Near;
416 string_format.Alignment = StringAlignment.Far;
418 case ContentAlignment.MiddleLeft:
419 string_format.LineAlignment = StringAlignment.Center;
420 string_format.Alignment = StringAlignment.Near;
422 case ContentAlignment.MiddleRight:
423 string_format.LineAlignment = StringAlignment.Center;
424 string_format.Alignment = StringAlignment.Far;
426 case ContentAlignment.MiddleCenter:
427 string_format.LineAlignment = StringAlignment.Center;
428 string_format.Alignment = StringAlignment.Center;
434 OnTextAlignChanged (EventArgs.Empty);
441 public bool UseMnemonic {
442 get { return use_mnemonic; }
444 if (use_mnemonic != value) {
445 use_mnemonic = value;
446 SetUseMnemonic (use_mnemonic);
454 #region Public Methods
456 protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
458 Rectangle rcImageClip = area;
459 rcImageClip.Inflate (-2,-2);
464 if (img_align == ContentAlignment.TopCenter ||
465 img_align == ContentAlignment.MiddleCenter ||
466 img_align == ContentAlignment.BottomCenter) {
467 X += (area.Width - image.Width) / 2;
469 else if (img_align == ContentAlignment.TopRight ||
470 img_align == ContentAlignment.MiddleRight||
471 img_align == ContentAlignment.BottomRight) {
472 X += (area.Width - image.Width);
475 if( img_align == ContentAlignment.BottomCenter ||
476 img_align == ContentAlignment.BottomLeft ||
477 img_align == ContentAlignment.BottomRight) {
478 Y += area.Height - image.Height;
480 else if(img_align == ContentAlignment.MiddleCenter ||
481 img_align == ContentAlignment.MiddleLeft ||
482 img_align == ContentAlignment.MiddleRight) {
483 Y += (area.Height - image.Height) / 2;
488 rcImageClip.Width = image.Width;
489 rcImageClip.Height = image.Height;
494 protected override AccessibleObject CreateAccessibilityInstance ()
496 return base.CreateAccessibilityInstance ();
499 protected override void Dispose(bool disposing)
501 base.Dispose (disposing);
504 string_format.Dispose ();
507 protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
509 if (image == null || g == null)
512 Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
515 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
517 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
521 protected virtual void OnAutoSizeChanged (EventArgs e)
523 EventHandler eh = (EventHandler)(Events [AutoSizeChangedEvent]);
529 protected override void OnEnabledChanged (EventArgs e)
531 base.OnEnabledChanged (e);
534 protected override void OnFontChanged (EventArgs e)
536 base.OnFontChanged (e);
542 protected override void OnPaint (PaintEventArgs pevent)
544 ThemeEngine.Current.DrawLabel (pevent.Graphics, ClientRectangle, this);
545 DrawImage (pevent.Graphics, Image, ClientRectangle, image_align);
546 base.OnPaint(pevent);
549 protected override void OnParentChanged (EventArgs e)
551 base.OnParentChanged (e);
554 protected virtual void OnTextAlignChanged (EventArgs e)
556 EventHandler eh = (EventHandler)(Events [TextAlignChangedEvent]);
561 protected override void OnTextChanged (EventArgs e)
563 base.OnTextChanged (e);
569 protected override void OnVisibleChanged (EventArgs e)
571 base.OnVisibleChanged (e);
574 protected override bool ProcessMnemonic (char charCode)
576 if (IsMnemonic(charCode, Text) == true) {
577 // Select item next in line in tab order
578 if (this.Parent != null) {
579 Parent.SelectNextControl(this, true, false, false, false);
584 return base.ProcessMnemonic (charCode);
587 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
589 base.SetBoundsCore (x, y, width, height, specified);
592 public override string ToString()
594 return base.ToString () + ", Text: " + Text;
597 protected override void WndProc (ref Message m)
599 switch ((Msg) m.Msg) {
600 case Msg.WM_DRAWITEM: {
601 m.Result = (IntPtr)1;
605 base.WndProc (ref m);
610 #endregion Public Methods
612 #region Private Methods
614 private void CalcAutoSize ()
616 if (IsHandleCreated == false || AutoSize == false)
619 Width = PreferredWidth;
620 Height = PreferredHeight;
623 private void OnHandleCreatedLB (Object o, EventArgs e)
629 private void SetUseMnemonic (bool use)
632 string_format.HotkeyPrefix = HotkeyPrefix.Show;
634 string_format.HotkeyPrefix = HotkeyPrefix.None;
637 #endregion Private Methods
639 [DefaultValue (false)]
640 public bool UseCompatibleTextRendering {
641 get { return use_compatible_text_rendering; }
642 set { use_compatible_text_rendering = value; }
645 [SettingsBindable (true)]
646 [Editor ("System.ComponentModel.Design.MultilineStringEditor, " + Consts.AssemblySystem_Design,
647 typeof (System.Drawing.Design.UITypeEditor))]
648 public override string Text {
649 get { return base.Text; }
650 set { base.Text = value; }
653 protected override void OnMouseEnter (EventArgs e)
655 base.OnMouseEnter (e);
658 protected override void OnMouseLeave (EventArgs e)
660 base.OnMouseLeave (e);
663 protected override void OnHandleDestroyed (EventArgs e)
665 base.OnHandleDestroyed (e);