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;
69 [EditorBrowsable(EditorBrowsableState.Never)]
70 public new event KeyEventHandler KeyDown;
73 [EditorBrowsable(EditorBrowsableState.Never)]
74 public new event KeyPressEventHandler KeyPress;
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 string_format = new StringFormat();
92 TextAlign = ContentAlignment.TopLeft;
96 image_align = ContentAlignment.MiddleCenter;
97 SetUseMnemonic (UseMnemonic);
98 flat_style = FlatStyle.Standard;
100 CalcPreferredHeight ();
101 CalcPreferredWidth ();
103 AutoSizeChanged = null;
104 TextAlignChanged = null;
106 SetStyle (ControlStyles.Selectable, false);
107 SetStyle (ControlStyles.ResizeRedraw |
108 ControlStyles.UserPaint |
109 ControlStyles.AllPaintingInWmPaint |
110 ControlStyles.SupportsTransparentBackColor |
111 ControlStyles.DoubleBuffer, 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 {
156 get { return InternalBorderStyle; }
157 set { InternalBorderStyle = value; }
160 protected override CreateParams CreateParams {
161 get { return base.CreateParams;}
164 protected override ImeMode DefaultImeMode {
165 get { return ImeMode.Disable;}
168 protected override Size DefaultSize {
169 get {return ThemeEngine.Current.LabelDefaultSize;}
172 [DefaultValue(FlatStyle.Standard)]
173 public FlatStyle FlatStyle {
178 if (!Enum.IsDefined (typeof (FlatStyle), value))
179 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
181 if (flat_style == value)
196 if (image_list != null && ImageIndex >= 0) {
197 return image_list.Images[ImageIndex];
211 [DefaultValue(ContentAlignment.MiddleCenter)]
213 public ContentAlignment ImageAlign {
218 if (!Enum.IsDefined (typeof (ContentAlignment), value))
219 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
221 if (image_align == value)
230 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
232 [TypeConverter (typeof (ImageIndexConverter))]
233 public int ImageIndex {
235 if (ImageList == null) {
239 if (image_index >= image_list.Images.Count) {
240 return image_list.Images.Count - 1;
248 throw new ArgumentException ();
250 if (image_index == value)
255 if (ImageList != null && image_index !=-1)
263 public ImageList ImageList {
264 get { return image_list;}
266 if (image_list == value)
271 if (image_list != null && image_index !=-1)
279 [EditorBrowsable(EditorBrowsableState.Never)]
280 public new ImeMode ImeMode {
281 get { return base.ImeMode; }
283 if (value == ImeMode)
285 base.ImeMode = value;
286 if (ImeModeChanged != null)
287 ImeModeChanged (this, EventArgs.Empty);
292 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
293 [EditorBrowsable(EditorBrowsableState.Advanced)]
294 public virtual int PreferredHeight {
295 get { return preferred_height; }
299 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
300 [EditorBrowsable(EditorBrowsableState.Advanced)]
301 public virtual int PreferredWidth {
302 get {return preferred_width; }
305 protected virtual bool RenderTransparent {
306 get { return render_transparent; }
307 set { render_transparent = value;}
311 [DefaultValue(false)]
312 [EditorBrowsable(EditorBrowsableState.Never)]
313 public new bool TabStop {
314 get { return base.TabStop; }
316 if (value == base.TabStop)
319 base.TabStop = value;
320 if (TabStopChanged != null)
321 TabStopChanged (this, EventArgs.Empty);
325 [DefaultValue(ContentAlignment.TopLeft)]
327 public virtual ContentAlignment TextAlign {
328 get { return text_align; }
331 if (!Enum.IsDefined (typeof (ContentAlignment), value))
332 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
334 if (text_align != value) {
340 case ContentAlignment.BottomLeft:
341 string_format.LineAlignment = StringAlignment.Far;
342 string_format.Alignment = StringAlignment.Near;
344 case ContentAlignment.BottomCenter:
345 string_format.LineAlignment = StringAlignment.Far;
346 string_format.Alignment = StringAlignment.Center;
348 case ContentAlignment.BottomRight:
349 string_format.LineAlignment = StringAlignment.Far;
350 string_format.Alignment = StringAlignment.Far;
352 case ContentAlignment.TopLeft:
353 string_format.LineAlignment = StringAlignment.Near;
354 string_format.Alignment = StringAlignment.Near;
356 case ContentAlignment.TopCenter:
357 string_format.LineAlignment = StringAlignment.Near;
358 string_format.Alignment = StringAlignment.Center;
360 case ContentAlignment.TopRight:
361 string_format.LineAlignment = StringAlignment.Near;
362 string_format.Alignment = StringAlignment.Far;
364 case ContentAlignment.MiddleLeft:
365 string_format.LineAlignment = StringAlignment.Center;
366 string_format.Alignment = StringAlignment.Near;
368 case ContentAlignment.MiddleRight:
369 string_format.LineAlignment = StringAlignment.Center;
370 string_format.Alignment = StringAlignment.Far;
372 case ContentAlignment.MiddleCenter:
373 string_format.LineAlignment = StringAlignment.Center;
374 string_format.Alignment = StringAlignment.Center;
380 OnTextAlignChanged (new EventArgs ());
387 public bool UseMnemonic {
388 get { return use_mnemonic; }
390 if (use_mnemonic != value) {
391 use_mnemonic = value;
392 SetUseMnemonic (use_mnemonic);
401 #region Public Methods
403 protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
405 Rectangle rcImageClip = area;
406 rcImageClip.Inflate (-2,-2);
411 if (img_align == ContentAlignment.TopCenter ||
412 img_align == ContentAlignment.MiddleCenter ||
413 img_align == ContentAlignment.BottomCenter) {
414 X += (area.Width - image.Width) / 2;
416 else if (img_align == ContentAlignment.TopRight ||
417 img_align == ContentAlignment.MiddleRight||
418 img_align == ContentAlignment.BottomRight) {
419 X += (area.Width - image.Width);
422 if( img_align == ContentAlignment.BottomCenter ||
423 img_align == ContentAlignment.BottomLeft ||
424 img_align == ContentAlignment.BottomRight) {
425 Y += area.Height - image.Height;
427 else if(img_align == ContentAlignment.MiddleCenter ||
428 img_align == ContentAlignment.MiddleLeft ||
429 img_align == ContentAlignment.MiddleRight) {
430 Y += (area.Height - image.Height) / 2;
435 rcImageClip.Width = image.Width;
436 rcImageClip.Height = image.Height;
442 protected override AccessibleObject CreateAccessibilityInstance ()
444 return base.CreateAccessibilityInstance ();
447 protected override void Dispose(bool disposing)
449 base.Dispose (disposing);
452 string_format.Dispose ();
455 protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
457 if (image == null || g == null)
460 Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
463 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
465 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
468 protected virtual void OnAutoSizeChanged (EventArgs e)
470 if (AutoSizeChanged != null)
471 AutoSizeChanged (this, e);
474 protected override void OnEnabledChanged (EventArgs e)
476 base.OnEnabledChanged (e);
479 protected override void OnFontChanged (EventArgs e)
481 base.OnFontChanged (e);
485 CalcPreferredHeight ();
490 protected override void OnPaint (PaintEventArgs pevent)
492 ThemeEngine.Current.DrawLabel (pevent.Graphics, ClientRectangle, this);
493 DrawImage (pevent.Graphics, Image, ClientRectangle, image_align);
494 base.OnPaint(pevent);
497 protected override void OnParentChanged (EventArgs e)
499 base.OnParentChanged (e);
502 protected virtual void OnTextAlignChanged (EventArgs e)
504 if (TextAlignChanged != null)
505 TextAlignChanged (this, e);
508 protected override void OnTextChanged (EventArgs e)
510 base.OnTextChanged (e);
514 CalcPreferredWidth ();
519 protected override void OnVisibleChanged (EventArgs e)
521 base.OnVisibleChanged (e);
524 protected override bool ProcessMnemonic (char charCode)
526 if (IsMnemonic(charCode, Text) == true) {
527 // Select item next in line in tab order
528 if (this.parent != null) {
529 parent.SelectNextControl(this, true, false, false, false);
534 return base.ProcessMnemonic (charCode);
537 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
539 base.SetBoundsCore (x, y, width, height, specified);
542 public override string ToString()
544 return base.ToString () + ", Text: " + Text;
547 protected override void WndProc (ref Message m)
549 switch ((Msg) m.Msg) {
550 case Msg.WM_DRAWITEM: {
551 m.Result = (IntPtr)1;
555 base.WndProc (ref m);
560 #endregion Public Methods
562 #region Private Methods
564 private void CalcAutoSize ()
566 if (IsHandleCreated == false || AutoSize == false)
569 CalcPreferredWidth ();
570 CalcPreferredHeight ();
572 Width = PreferredWidth;
573 Height = PreferredHeight;
576 private void CalcPreferredHeight ()
578 preferred_height = Font.Height;
580 switch (border_style) {
581 case BorderStyle.None:
582 preferred_height += 3;
584 case BorderStyle.FixedSingle:
585 case BorderStyle.Fixed3D:
586 preferred_height += 6;
594 private void CalcPreferredWidth ()
597 size = DeviceContext.MeasureString (Text, Font, req_witdthsize, string_format);
598 preferred_width = (int) size.Width + 3;
601 private void OnHandleCreatedLB (Object o, EventArgs e)
607 private void SetUseMnemonic (bool use)
610 string_format.HotkeyPrefix = HotkeyPrefix.Show;
612 string_format.HotkeyPrefix = HotkeyPrefix.None;
615 #endregion Private Methods