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-2005 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")]
41 public class Label : Control
43 private bool autosize;
45 private bool render_transparent;
46 private FlatStyle flat_style;
47 private int preferred_height;
48 private int preferred_width;
49 private bool use_mnemonic;
50 private int image_index = -1;
51 private ImageList image_list;
52 internal ContentAlignment image_align;
53 internal StringFormat string_format;
54 internal ContentAlignment text_align;
55 static SizeF req_witdthsize = new SizeF (0,0);
58 public event EventHandler AutoSizeChanged;
61 [EditorBrowsable(EditorBrowsableState.Never)]
62 public new event EventHandler BackgroundImageChanged;
65 [EditorBrowsable(EditorBrowsableState.Never)]
66 public new event EventHandler ImeModeChanged;
\r
69 [EditorBrowsable(EditorBrowsableState.Never)]
70 public new event KeyEventHandler KeyDown;
\r
73 [EditorBrowsable(EditorBrowsableState.Never)]
74 public new event KeyPressEventHandler KeyPress;
\r
77 [EditorBrowsable(EditorBrowsableState.Never)]
78 public new event KeyEventHandler KeyUp;
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 public new event EventHandler TabStopChanged;
84 public event EventHandler TextAlignChanged;
89 // Defaults in the Spec
91 border_style = BorderStyle.None;
92 string_format = new StringFormat();
93 TextAlign = ContentAlignment.TopLeft;
97 image_align = ContentAlignment.MiddleCenter;
98 SetUseMnemonic (UseMnemonic);
100 BackColor = ThemeEngine.Current.ColorButtonFace;
101 ForeColor = ThemeEngine.Current.ColorWindowText;
103 CalcPreferredHeight ();
104 CalcPreferredWidth ();
106 AutoSizeChanged = null;
107 TextAlignChanged = null;
109 SetStyle (ControlStyles.ResizeRedraw, true);
110 SetStyle (ControlStyles.Selectable, false);
111 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
113 HandleCreated += new EventHandler (OnHandleCreatedLB);
116 #region Public Properties
117 [DefaultValue(false)]
119 [RefreshProperties(RefreshProperties.All)]
120 public virtual bool AutoSize {
121 get { return autosize; }
123 if (autosize == value)
130 OnAutoSizeChanged (new EventArgs ());
135 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
136 [EditorBrowsable(EditorBrowsableState.Never)]
137 public override Image BackgroundImage {
139 return base.BackgroundImage;
142 if (base.BackgroundImage == value)
145 if (BackgroundImageChanged != null)
146 BackgroundImageChanged (this, EventArgs.Empty);
148 base.BackgroundImage = value;
153 [DefaultValue(BorderStyle.None)]
155 public virtual BorderStyle BorderStyle {
160 if (!Enum.IsDefined (typeof (BorderStyle), value))
161 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for BorderStyle", value));
163 if (border_style == value)
166 border_style = value;
171 protected override CreateParams CreateParams {
172 get { return base.CreateParams;}
175 protected override ImeMode DefaultImeMode {
176 get { return ImeMode.Disable;}
179 protected override Size DefaultSize {
180 get {return ThemeEngine.Current.LabelDefaultSize;}
183 [DefaultValue(FlatStyle.Standard)]
184 public FlatStyle FlatStyle {
189 if (!Enum.IsDefined (typeof (FlatStyle), value))
190 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
192 if (flat_style == value)
207 if (image_list != null && ImageIndex >= 0) {
208 return image_list.Images[ImageIndex];
222 [DefaultValue(ContentAlignment.MiddleCenter)]
224 public ContentAlignment ImageAlign {
229 if (!Enum.IsDefined (typeof (ContentAlignment), value))
230 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
232 if (image_align == value)
241 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
243 [TypeConverter (typeof (ImageIndexConverter))]
244 public int ImageIndex {
246 if (ImageList == null) {
250 if (image_index >= image_list.Images.Count) {
251 return image_list.Images.Count - 1;
259 throw new ArgumentException ();
261 if (image_index == value)
266 if (ImageList != null && image_index !=-1)
274 public ImageList ImageList {
275 get { return image_list;}
277 if (image_list == value)
282 if (image_list != null && image_index !=-1)
290 [EditorBrowsable(EditorBrowsableState.Never)]
291 public new ImeMode ImeMode {
292 get { return base.ImeMode; }
294 if (value == ImeMode)
296 base.ImeMode = value;
297 if (ImeModeChanged != null)
298 ImeModeChanged (this, EventArgs.Empty);
303 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
304 [EditorBrowsable(EditorBrowsableState.Advanced)]
305 public virtual int PreferredHeight {
306 get { return preferred_height; }
310 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
311 [EditorBrowsable(EditorBrowsableState.Advanced)]
312 public virtual int PreferredWidth {
313 get {return preferred_width; }
316 protected virtual bool RenderTransparent {
317 get { return render_transparent; }
318 set { render_transparent = value;}
322 [DefaultValue(false)]
323 [EditorBrowsable(EditorBrowsableState.Never)]
324 public new bool TabStop {
325 get { return base.TabStop; }
327 if (value == base.TabStop)
330 base.TabStop = value;
331 if (TabStopChanged != null)
332 TabStopChanged (this, EventArgs.Empty);
336 [DefaultValue(ContentAlignment.TopLeft)]
338 public virtual ContentAlignment TextAlign {
339 get { return text_align; }
342 if (!Enum.IsDefined (typeof (ContentAlignment), value))
343 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
345 if (text_align != value) {
351 case ContentAlignment.BottomLeft:
352 string_format.LineAlignment = StringAlignment.Far;
353 string_format.Alignment = StringAlignment.Near;
355 case ContentAlignment.BottomCenter:
356 string_format.LineAlignment = StringAlignment.Far;
357 string_format.Alignment = StringAlignment.Center;
359 case ContentAlignment.BottomRight:
360 string_format.LineAlignment = StringAlignment.Far;
361 string_format.Alignment = StringAlignment.Far;
363 case ContentAlignment.TopLeft:
364 string_format.LineAlignment = StringAlignment.Near;
365 string_format.Alignment = StringAlignment.Near;
367 case ContentAlignment.TopCenter:
368 string_format.LineAlignment = StringAlignment.Near;
369 string_format.Alignment = StringAlignment.Center;
371 case ContentAlignment.TopRight:
372 string_format.LineAlignment = StringAlignment.Near;
373 string_format.Alignment = StringAlignment.Far;
375 case ContentAlignment.MiddleLeft:
376 string_format.LineAlignment = StringAlignment.Center;
377 string_format.Alignment = StringAlignment.Near;
379 case ContentAlignment.MiddleRight:
380 string_format.LineAlignment = StringAlignment.Center;
381 string_format.Alignment = StringAlignment.Far;
383 case ContentAlignment.MiddleCenter:
384 string_format.LineAlignment = StringAlignment.Center;
385 string_format.Alignment = StringAlignment.Center;
391 OnTextAlignChanged (new EventArgs ());
398 public bool UseMnemonic {
399 get { return use_mnemonic; }
401 if (use_mnemonic != value) {
402 use_mnemonic = value;
403 SetUseMnemonic (use_mnemonic);
412 #region Public Methods
414 protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
416 Rectangle rcImageClip = area;
417 rcImageClip.Inflate (-2,-2);
422 if (img_align == ContentAlignment.TopCenter ||
423 img_align == ContentAlignment.MiddleCenter ||
424 img_align == ContentAlignment.BottomCenter) {
425 X += (area.Width - image.Width) / 2;
427 else if (img_align == ContentAlignment.TopRight ||
428 img_align == ContentAlignment.MiddleRight||
429 img_align == ContentAlignment.BottomRight) {
430 X += (area.Width - image.Width);
433 if( img_align == ContentAlignment.BottomCenter ||
434 img_align == ContentAlignment.BottomLeft ||
435 img_align == ContentAlignment.BottomRight) {
436 Y += area.Height - image.Height;
438 else if(img_align == ContentAlignment.MiddleCenter ||
439 img_align == ContentAlignment.MiddleLeft ||
440 img_align == ContentAlignment.MiddleRight) {
441 Y += (area.Height - image.Height) / 2;
446 rcImageClip.Width = image.Width;
447 rcImageClip.Height = image.Height;
453 protected override AccessibleObject CreateAccessibilityInstance ()
455 return base.CreateAccessibilityInstance ();
458 protected override void Dispose(bool disposing)
460 base.Dispose (disposing);
463 protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
465 if (image == null || g == null)
468 Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
471 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
473 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
476 protected virtual void OnAutoSizeChanged (EventArgs e)
478 if (AutoSizeChanged != null)
479 AutoSizeChanged (this, e);
482 protected override void OnEnabledChanged (EventArgs e)
484 base.OnEnabledChanged (e);
487 protected override void OnFontChanged (EventArgs e)
489 base.OnFontChanged (e);
493 CalcPreferredHeight ();
498 protected override void OnPaint (PaintEventArgs pevent)
500 ThemeEngine.Current.DrawLabel (pevent.Graphics, ClientRectangle, this);
501 DrawImage (pevent.Graphics, Image, ClientRectangle, image_align);
502 base.OnPaint(pevent);
505 protected override void OnParentChanged (EventArgs e)
507 base.OnParentChanged (e);
510 protected virtual void OnTextAlignChanged (EventArgs e)
512 if (TextAlignChanged != null)
513 TextAlignChanged (this, e);
516 protected override void OnTextChanged (EventArgs e)
518 base.OnTextChanged (e);
522 CalcPreferredWidth ();
527 protected override void OnVisibleChanged (EventArgs e)
529 base.OnVisibleChanged (e);
532 protected override bool ProcessMnemonic (char charCode)
534 if (IsMnemonic(charCode, Text) == true) {
535 // Select item next in line in tab order
536 if (this.parent != null) {
537 parent.SelectNextControl(this, true, false, false, false);
542 return base.ProcessMnemonic (charCode);
545 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
547 base.SetBoundsCore (x, y, width, height, specified);
550 public override string ToString()
552 return base.ToString () + "Text: " + Text;
555 protected override void WndProc(ref Message m)
557 switch ((Msg) m.Msg) {
558 case Msg.WM_DRAWITEM: {
559 m.Result = (IntPtr)1;
563 base.WndProc (ref m);
568 #endregion Public Methods
570 #region Private Methods
572 private void CalcAutoSize ()
574 if (IsHandleCreated == false || AutoSize == false)
577 CalcPreferredWidth ();
578 CalcPreferredHeight ();
580 Width = PreferredWidth;
581 Height = PreferredHeight;
584 private void CalcPreferredHeight ()
586 preferred_height = Font.Height;
588 switch (border_style) {
589 case BorderStyle.None:
590 preferred_height += 3;
592 case BorderStyle.FixedSingle:
593 case BorderStyle.Fixed3D:
594 preferred_height += 6;
602 private void CalcPreferredWidth ()
605 size = DeviceContext.MeasureString (Text, Font, req_witdthsize, string_format);
606 preferred_width = (int) size.Width + 3;
609 private void OnHandleCreatedLB (Object o, EventArgs e)
615 private void SetUseMnemonic (bool use)
618 string_format.HotkeyPrefix = HotkeyPrefix.Show;
620 string_format.HotkeyPrefix = HotkeyPrefix.None;
623 #endregion Private Methods