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")]
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 {
64 base.BackgroundImageChanged += value;
67 base.BackgroundImageChanged -= value;
72 [EditorBrowsable(EditorBrowsableState.Never)]
73 public new event EventHandler ImeModeChanged {
75 base.ImeModeChanged += value;
78 base.ImeModeChanged -= value;
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 public new event KeyEventHandler KeyDown {
86 base.KeyDown += value;
89 base.KeyDown -= value;
95 [EditorBrowsable(EditorBrowsableState.Never)]
96 public new event KeyPressEventHandler KeyPress {
98 base.KeyPress += value;
101 base.KeyPress -= value;
107 [EditorBrowsable(EditorBrowsableState.Never)]
108 public new event KeyEventHandler KeyUp {
119 [EditorBrowsable(EditorBrowsableState.Never)]
120 public new event EventHandler TabStopChanged {
122 base.TabStopChanged += value;
125 base.TabStopChanged -= value;
129 public event EventHandler TextAlignChanged;
134 // Defaults in the Spec
137 string_format = new StringFormat();
138 TextAlign = ContentAlignment.TopLeft;
142 image_align = ContentAlignment.MiddleCenter;
143 SetUseMnemonic (UseMnemonic);
144 flat_style = FlatStyle.Standard;
146 CalcPreferredHeight ();
147 CalcPreferredWidth ();
149 AutoSizeChanged = null;
150 TextAlignChanged = null;
152 SetStyle (ControlStyles.Selectable, false);
153 SetStyle (ControlStyles.ResizeRedraw |
154 ControlStyles.UserPaint |
155 ControlStyles.AllPaintingInWmPaint |
156 ControlStyles.SupportsTransparentBackColor |
157 ControlStyles.DoubleBuffer, true);
159 HandleCreated += new EventHandler (OnHandleCreatedLB);
162 #region Public Properties
163 [DefaultValue(false)]
165 [RefreshProperties(RefreshProperties.All)]
166 public 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 {
185 return base.BackgroundImage;
188 base.BackgroundImage = value;
193 [DefaultValue(BorderStyle.None)]
195 public virtual BorderStyle BorderStyle {
196 get { return InternalBorderStyle; }
197 set { InternalBorderStyle = value; }
200 protected override CreateParams CreateParams {
201 get { return base.CreateParams;}
204 protected override ImeMode DefaultImeMode {
205 get { return ImeMode.Disable;}
208 protected override Size DefaultSize {
209 get {return ThemeEngine.Current.LabelDefaultSize;}
212 [DefaultValue(FlatStyle.Standard)]
213 public FlatStyle FlatStyle {
218 if (!Enum.IsDefined (typeof (FlatStyle), value))
219 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
221 if (flat_style == value)
236 if (image_list != null && ImageIndex >= 0) {
237 return image_list.Images[ImageIndex];
251 [DefaultValue(ContentAlignment.MiddleCenter)]
253 public ContentAlignment ImageAlign {
258 if (!Enum.IsDefined (typeof (ContentAlignment), value))
259 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
261 if (image_align == value)
270 [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
272 [TypeConverter (typeof (ImageIndexConverter))]
273 public int ImageIndex {
275 if (ImageList == null) {
279 if (image_index >= image_list.Images.Count) {
280 return image_list.Images.Count - 1;
288 throw new ArgumentException ();
290 if (image_index == value)
295 if (ImageList != null && image_index !=-1)
303 public ImageList ImageList {
304 get { return image_list;}
306 if (image_list == value)
311 if (image_list != null && image_index !=-1)
319 [EditorBrowsable(EditorBrowsableState.Never)]
320 public new ImeMode ImeMode {
321 get { return base.ImeMode; }
322 set { base.ImeMode = value; }
326 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
327 [EditorBrowsable(EditorBrowsableState.Advanced)]
328 public virtual int PreferredHeight {
329 get { return preferred_height; }
333 [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
334 [EditorBrowsable(EditorBrowsableState.Advanced)]
335 public virtual int PreferredWidth {
336 get {return preferred_width; }
339 protected virtual bool RenderTransparent {
340 get { return render_transparent; }
341 set { render_transparent = value;}
345 [DefaultValue(false)]
346 [EditorBrowsable(EditorBrowsableState.Never)]
347 public new bool TabStop {
348 get { return base.TabStop; }
349 set { base.TabStop = value; }
352 [DefaultValue(ContentAlignment.TopLeft)]
354 public virtual ContentAlignment TextAlign {
355 get { return text_align; }
358 if (!Enum.IsDefined (typeof (ContentAlignment), value))
359 throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
361 if (text_align != value) {
367 case ContentAlignment.BottomLeft:
368 string_format.LineAlignment = StringAlignment.Far;
369 string_format.Alignment = StringAlignment.Near;
371 case ContentAlignment.BottomCenter:
372 string_format.LineAlignment = StringAlignment.Far;
373 string_format.Alignment = StringAlignment.Center;
375 case ContentAlignment.BottomRight:
376 string_format.LineAlignment = StringAlignment.Far;
377 string_format.Alignment = StringAlignment.Far;
379 case ContentAlignment.TopLeft:
380 string_format.LineAlignment = StringAlignment.Near;
381 string_format.Alignment = StringAlignment.Near;
383 case ContentAlignment.TopCenter:
384 string_format.LineAlignment = StringAlignment.Near;
385 string_format.Alignment = StringAlignment.Center;
387 case ContentAlignment.TopRight:
388 string_format.LineAlignment = StringAlignment.Near;
389 string_format.Alignment = StringAlignment.Far;
391 case ContentAlignment.MiddleLeft:
392 string_format.LineAlignment = StringAlignment.Center;
393 string_format.Alignment = StringAlignment.Near;
395 case ContentAlignment.MiddleRight:
396 string_format.LineAlignment = StringAlignment.Center;
397 string_format.Alignment = StringAlignment.Far;
399 case ContentAlignment.MiddleCenter:
400 string_format.LineAlignment = StringAlignment.Center;
401 string_format.Alignment = StringAlignment.Center;
407 OnTextAlignChanged (EventArgs.Empty);
414 public bool UseMnemonic {
415 get { return use_mnemonic; }
417 if (use_mnemonic != value) {
418 use_mnemonic = value;
419 SetUseMnemonic (use_mnemonic);
428 #region Public Methods
430 protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
432 Rectangle rcImageClip = area;
433 rcImageClip.Inflate (-2,-2);
438 if (img_align == ContentAlignment.TopCenter ||
439 img_align == ContentAlignment.MiddleCenter ||
440 img_align == ContentAlignment.BottomCenter) {
441 X += (area.Width - image.Width) / 2;
443 else if (img_align == ContentAlignment.TopRight ||
444 img_align == ContentAlignment.MiddleRight||
445 img_align == ContentAlignment.BottomRight) {
446 X += (area.Width - image.Width);
449 if( img_align == ContentAlignment.BottomCenter ||
450 img_align == ContentAlignment.BottomLeft ||
451 img_align == ContentAlignment.BottomRight) {
452 Y += area.Height - image.Height;
454 else if(img_align == ContentAlignment.MiddleCenter ||
455 img_align == ContentAlignment.MiddleLeft ||
456 img_align == ContentAlignment.MiddleRight) {
457 Y += (area.Height - image.Height) / 2;
462 rcImageClip.Width = image.Width;
463 rcImageClip.Height = image.Height;
469 protected override AccessibleObject CreateAccessibilityInstance ()
471 return base.CreateAccessibilityInstance ();
474 protected override void Dispose(bool disposing)
476 base.Dispose (disposing);
479 string_format.Dispose ();
482 protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
484 if (image == null || g == null)
487 Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
490 g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
492 ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
495 protected virtual void OnAutoSizeChanged (EventArgs e)
497 if (AutoSizeChanged != null)
498 AutoSizeChanged (this, e);
501 protected override void OnEnabledChanged (EventArgs e)
503 base.OnEnabledChanged (e);
506 protected override void OnFontChanged (EventArgs e)
508 base.OnFontChanged (e);
512 CalcPreferredHeight ();
517 protected override void OnPaint (PaintEventArgs pevent)
519 ThemeEngine.Current.DrawLabel (pevent.Graphics, ClientRectangle, this);
520 DrawImage (pevent.Graphics, Image, ClientRectangle, image_align);
521 base.OnPaint(pevent);
524 protected override void OnParentChanged (EventArgs e)
526 base.OnParentChanged (e);
529 protected virtual void OnTextAlignChanged (EventArgs e)
531 if (TextAlignChanged != null)
532 TextAlignChanged (this, e);
535 protected override void OnTextChanged (EventArgs e)
537 base.OnTextChanged (e);
541 CalcPreferredWidth ();
546 protected override void OnVisibleChanged (EventArgs e)
548 base.OnVisibleChanged (e);
551 protected override bool ProcessMnemonic (char charCode)
553 if (IsMnemonic(charCode, Text) == true) {
554 // Select item next in line in tab order
555 if (this.parent != null) {
556 parent.SelectNextControl(this, true, false, false, false);
561 return base.ProcessMnemonic (charCode);
564 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
566 base.SetBoundsCore (x, y, width, height, specified);
569 public override string ToString()
571 return base.ToString () + ", Text: " + Text;
574 protected override void WndProc (ref Message m)
576 switch ((Msg) m.Msg) {
577 case Msg.WM_DRAWITEM: {
578 m.Result = (IntPtr)1;
582 base.WndProc (ref m);
587 #endregion Public Methods
589 #region Private Methods
591 private void CalcAutoSize ()
593 if (IsHandleCreated == false || AutoSize == false)
596 CalcPreferredWidth ();
597 CalcPreferredHeight ();
599 Width = PreferredWidth;
600 Height = PreferredHeight;
603 private void CalcPreferredHeight ()
605 preferred_height = Font.Height;
607 switch (border_style) {
608 case BorderStyle.None:
609 preferred_height += 3;
611 case BorderStyle.FixedSingle:
612 case BorderStyle.Fixed3D:
613 preferred_height += 6;
621 private void CalcPreferredWidth ()
624 size = DeviceContext.MeasureString (Text, Font, req_witdthsize, string_format);
625 preferred_width = (int) size.Width + 3;
628 private void OnHandleCreatedLB (Object o, EventArgs e)
634 private void SetUseMnemonic (bool use)
637 string_format.HotkeyPrefix = HotkeyPrefix.Show;
639 string_format.HotkeyPrefix = HotkeyPrefix.None;
642 #endregion Private Methods
645 public bool UseCompatibleTextRendering {
647 return use_compatible_text_rendering;
651 use_compatible_text_rendering = value;