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, (string)null)]
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 if (AutoSizeChanged != null)
131 AutoSizeChanged (this, new EventArgs ());
136 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
137 [EditorBrowsable(EditorBrowsableState.Never)]
138 public override Image BackgroundImage {
140 return base.BackgroundImage;
143 if (base.BackgroundImage == value)
146 if (BackgroundImageChanged != null)
147 BackgroundImageChanged (this, EventArgs.Empty);
149 base.BackgroundImage = value;
154 [DefaultValue(BorderStyle.None)]
156 public virtual BorderStyle BorderStyle {
161 if (!Enum.IsDefined (typeof (BorderStyle), value))
162 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for BorderStyle", value));
164 if (border_style == value)
167 border_style = value;
172 protected override CreateParams CreateParams {
173 get { return base.CreateParams;}
176 protected override ImeMode DefaultImeMode {
177 get { return ImeMode.Disable;}
180 protected override Size DefaultSize {
181 get {return ThemeEngine.Current.LabelDefaultSize;}
184 [DefaultValue(FlatStyle.Standard)]
185 public FlatStyle FlatStyle {
190 if (!Enum.IsDefined (typeof (FlatStyle), value))
191 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
193 if (flat_style == value)
208 if (image_list != null && ImageIndex >= 0) {
209 return image_list.Images[ImageIndex];
223 [DefaultValue(ContentAlignment.MiddleCenter)]
225 public ContentAlignment ImageAlign {
230 if (!Enum.IsDefined (typeof (ContentAlignment), value))
231 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
233 if (image_align == value)
242 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
244 [TypeConverter (typeof (ImageIndexConverter))]
245 public int ImageIndex {
247 if (ImageList == null) {
251 if (image_index >= image_list.Images.Count) {
252 return image_list.Images.Count - 1;
260 throw new ArgumentException ();
262 if (image_index == value)
267 if (ImageList != null && image_index !=-1)
275 public ImageList ImageList {
276 get { return image_list;}
278 if (image_list == value)
283 if (image_list != null && image_index !=-1)
291 [EditorBrowsable(EditorBrowsableState.Never)]
292 public new ImeMode ImeMode {
293 get { return base.ImeMode; }
295 if (value == ImeMode)
297 base.ImeMode = value;
298 if (ImeModeChanged != null)
299 ImeModeChanged (this, EventArgs.Empty);
304 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
305 [EditorBrowsable(EditorBrowsableState.Advanced)]
306 public virtual int PreferredHeight {
307 get { return preferred_height; }
311 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
312 [EditorBrowsable(EditorBrowsableState.Advanced)]
313 public virtual int PreferredWidth {
314 get {return preferred_width; }
317 protected virtual bool RenderTransparent {
318 get { return render_transparent; }
319 set { render_transparent = value;}
323 [DefaultValue(false)]
324 [EditorBrowsable(EditorBrowsableState.Never)]
325 public new bool TabStop {
326 get { return base.TabStop; }
328 if (value == base.TabStop)
331 base.TabStop = value;
332 if (TabStopChanged != null)
333 TabStopChanged (this, EventArgs.Empty);
337 [DefaultValue(ContentAlignment.TopLeft)]
339 public virtual ContentAlignment TextAlign {
340 get { return text_align; }
343 if (!Enum.IsDefined (typeof (ContentAlignment), value))
344 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
346 if (text_align != value) {
352 case ContentAlignment.BottomLeft:
353 string_format.LineAlignment = StringAlignment.Far;
354 string_format.Alignment = StringAlignment.Near;
356 case ContentAlignment.BottomCenter:
357 string_format.LineAlignment = StringAlignment.Far;
358 string_format.Alignment = StringAlignment.Center;
360 case ContentAlignment.BottomRight:
361 string_format.LineAlignment = StringAlignment.Far;
362 string_format.Alignment = StringAlignment.Far;
364 case ContentAlignment.TopLeft:
365 string_format.LineAlignment = StringAlignment.Near;
366 string_format.Alignment = StringAlignment.Near;
368 case ContentAlignment.TopCenter:
369 string_format.LineAlignment = StringAlignment.Near;
370 string_format.Alignment = StringAlignment.Center;
372 case ContentAlignment.TopRight:
373 string_format.LineAlignment = StringAlignment.Near;
374 string_format.Alignment = StringAlignment.Far;
376 case ContentAlignment.MiddleLeft:
377 string_format.LineAlignment = StringAlignment.Center;
378 string_format.Alignment = StringAlignment.Near;
380 case ContentAlignment.MiddleRight:
381 string_format.LineAlignment = StringAlignment.Center;
382 string_format.Alignment = StringAlignment.Far;
384 case ContentAlignment.MiddleCenter:
385 string_format.LineAlignment = StringAlignment.Center;
386 string_format.Alignment = StringAlignment.Center;
392 if (TextAlignChanged != null)
393 TextAlignChanged (this, new EventArgs ());
401 public bool UseMnemonic {
402 get { return use_mnemonic; }
404 if (use_mnemonic != value) {
405 use_mnemonic = value;
406 SetUseMnemonic (use_mnemonic);
415 #region Public Methods
417 protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
419 Rectangle rcImageClip = area;
420 rcImageClip.Inflate (-2,-2);
425 if (img_align == ContentAlignment.TopCenter ||
426 img_align == ContentAlignment.MiddleCenter ||
427 img_align == ContentAlignment.BottomCenter) {
428 X += (area.Width - image.Width) / 2;
430 else if (img_align == ContentAlignment.TopRight ||
431 img_align == ContentAlignment.MiddleRight||
432 img_align == ContentAlignment.BottomRight) {
433 X += (area.Width - image.Width);
436 if( img_align == ContentAlignment.BottomCenter ||
437 img_align == ContentAlignment.BottomLeft ||
438 img_align == ContentAlignment.BottomRight) {
439 Y += area.Height - image.Height;
441 else if(img_align == ContentAlignment.MiddleCenter ||
442 img_align == ContentAlignment.MiddleLeft ||
443 img_align == ContentAlignment.MiddleRight) {
444 Y += (area.Height - image.Height) / 2;
449 rcImageClip.Width = image.Width;
450 rcImageClip.Height = image.Height;
456 protected override AccessibleObject CreateAccessibilityInstance ()
458 return base.CreateAccessibilityInstance ();
461 protected override void Dispose(bool disposing)
463 base.Dispose (disposing);
466 protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
468 if (image == null || g == null)
471 Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
474 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
476 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
479 protected virtual void OnAutoSizeChanged (EventArgs e)
481 if (AutoSizeChanged != null)
482 AutoSizeChanged (this, e);
485 protected override void OnEnabledChanged (EventArgs e)
487 base.OnEnabledChanged (e);
490 protected override void OnFontChanged (EventArgs e)
492 base.OnFontChanged (e);
493 CalcPreferredHeight ();
497 protected override void OnPaint (PaintEventArgs pevent)
499 if (Width <= 0 || Height <= 0 || Visible == false)
503 pevent.Graphics.DrawImage (ImageBuffer, 0, 0);
506 protected override void OnParentChanged (EventArgs e)
508 base.OnParentChanged (e);
511 protected virtual void OnTextAlignChanged (EventArgs e)
513 if (TextAlignChanged != null)
514 TextAlignChanged (this, e);
517 protected override void OnTextChanged (EventArgs e)
519 base.OnTextChanged (e);
520 CalcPreferredWidth ();
525 protected override void OnVisibleChanged (EventArgs e)
527 base.OnVisibleChanged (e);
530 protected override bool ProcessMnemonic (char charCode)
532 if (IsMnemonic(charCode, Text) == true) {
533 // Select item next in line in tab order
534 if (this.parent != null) {
535 parent.SelectNextControl(this, true, false, false, false);
540 return base.ProcessMnemonic (charCode);
543 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
545 base.SetBoundsCore (x, y, width, height, specified);
548 public override string ToString()
550 return base.ToString () + "Text: " + Text;
553 protected override void WndProc(ref Message m)
555 switch ((Msg) m.Msg) {
556 case Msg.WM_DRAWITEM: {
557 m.Result = (IntPtr)1;
561 base.WndProc (ref m);
566 #endregion Public Methods
568 #region Private Methods
570 private void CalcAutoSize ()
572 if (IsHandleCreated == false || AutoSize == false)
575 CalcPreferredWidth ();
576 CalcPreferredHeight ();
578 Width = PreferredWidth;
579 Height = PreferredHeight;
582 private void CalcPreferredHeight ()
584 preferred_height = Font.Height;
586 switch (border_style) {
587 case BorderStyle.None:
588 preferred_height += 3;
590 case BorderStyle.FixedSingle:
591 case BorderStyle.Fixed3D:
592 preferred_height += 6;
600 private void CalcPreferredWidth ()
603 size = DeviceContext.MeasureString (Text, Font, req_witdthsize, string_format);
604 preferred_width = (int) size.Width + 3;
607 internal void Draw ()
609 ThemeEngine.Current.DrawLabel(DeviceContext, ClientRectangle, this);
610 DrawImage (DeviceContext, Image, ClientRectangle, image_align);
613 private void OnHandleCreatedLB (Object o, EventArgs e)
619 private void SetUseMnemonic (bool use)
622 string_format.HotkeyPrefix = HotkeyPrefix.Show;
624 string_format.HotkeyPrefix = HotkeyPrefix.None;
627 #endregion Private Methods