Label.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  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.
  19. //
  20. // Copyright (c) 2004-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. // Peter Bartok, [email protected]
  25. //
  26. //
  27. // COMPLETE
  28. using System.ComponentModel;
  29. using System.ComponentModel.Design;
  30. using System.Drawing;
  31. using System.Drawing.Text;
  32. using System.Drawing.Imaging;
  33. using System.Runtime.InteropServices;
  34. namespace System.Windows.Forms
  35. {
  36. [DefaultProperty("Text")]
  37. [Designer ("System.Windows.Forms.Design.LabelDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  38. public class Label : Control
  39. {
  40. private bool autosize;
  41. private Image image;
  42. private bool render_transparent;
  43. private FlatStyle flat_style;
  44. private int preferred_height;
  45. private int preferred_width;
  46. private bool use_mnemonic;
  47. private int image_index = -1;
  48. private ImageList image_list;
  49. internal ContentAlignment image_align;
  50. internal StringFormat string_format;
  51. internal ContentAlignment text_align;
  52. static SizeF req_witdthsize = new SizeF (0,0);
  53. #region Events
  54. public event EventHandler AutoSizeChanged;
  55. [Browsable(false)]
  56. [EditorBrowsable(EditorBrowsableState.Never)]
  57. public new event EventHandler BackgroundImageChanged;
  58. [Browsable(false)]
  59. [EditorBrowsable(EditorBrowsableState.Never)]
  60. public new event EventHandler ImeModeChanged;
  61. [Browsable(false)]
  62. [EditorBrowsable(EditorBrowsableState.Never)]
  63. public new event KeyEventHandler KeyDown;
  64. [Browsable(false)]
  65. [EditorBrowsable(EditorBrowsableState.Never)]
  66. public new event KeyPressEventHandler KeyPress;
  67. [Browsable(false)]
  68. [EditorBrowsable(EditorBrowsableState.Never)]
  69. public new event KeyEventHandler KeyUp;
  70. [Browsable(false)]
  71. [EditorBrowsable(EditorBrowsableState.Never)]
  72. public new event EventHandler TabStopChanged;
  73. public event EventHandler TextAlignChanged;
  74. #endregion
  75. public Label ()
  76. {
  77. // Defaults in the Spec
  78. autosize = false;
  79. tab_stop = false;
  80. string_format = new StringFormat();
  81. TextAlign = ContentAlignment.TopLeft;
  82. image = null;
  83. UseMnemonic = true;
  84. image_list = null;
  85. image_align = ContentAlignment.MiddleCenter;
  86. SetUseMnemonic (UseMnemonic);
  87. flat_style = FlatStyle.Standard;
  88. CalcPreferredHeight ();
  89. CalcPreferredWidth ();
  90. AutoSizeChanged = null;
  91. TextAlignChanged = null;
  92. SetStyle (ControlStyles.Selectable, false);
  93. SetStyle (ControlStyles.ResizeRedraw |
  94. ControlStyles.UserPaint |
  95. ControlStyles.AllPaintingInWmPaint |
  96. ControlStyles.SupportsTransparentBackColor |
  97. ControlStyles.DoubleBuffer, true);
  98. HandleCreated += new EventHandler (OnHandleCreatedLB);
  99. }
  100. #region Public Properties
  101. [DefaultValue(false)]
  102. [Localizable(true)]
  103. [RefreshProperties(RefreshProperties.All)]
  104. public virtual bool AutoSize {
  105. get { return autosize; }
  106. set {
  107. if (autosize == value)
  108. return;
  109. autosize = value;
  110. CalcAutoSize ();
  111. Refresh ();
  112. OnAutoSizeChanged (new EventArgs ());
  113. }
  114. }
  115. [Browsable(false)]
  116. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  117. [EditorBrowsable(EditorBrowsableState.Never)]
  118. public override Image BackgroundImage {
  119. get {
  120. return base.BackgroundImage;
  121. }
  122. set {
  123. if (base.BackgroundImage == value)
  124. return;
  125. if (BackgroundImageChanged != null)
  126. BackgroundImageChanged (this, EventArgs.Empty);
  127. base.BackgroundImage = value;
  128. Refresh ();
  129. }
  130. }
  131. [DefaultValue(BorderStyle.None)]
  132. [DispId(-504)]
  133. public virtual BorderStyle BorderStyle {
  134. get { return InternalBorderStyle; }
  135. set { InternalBorderStyle = value; }
  136. }
  137. protected override CreateParams CreateParams {
  138. get { return base.CreateParams;}
  139. }
  140. protected override ImeMode DefaultImeMode {
  141. get { return ImeMode.Disable;}
  142. }
  143. protected override Size DefaultSize {
  144. get {return ThemeEngine.Current.LabelDefaultSize;}
  145. }
  146. [DefaultValue(FlatStyle.Standard)]
  147. public FlatStyle FlatStyle {
  148. get {
  149. return flat_style;
  150. }
  151. set {
  152. if (!Enum.IsDefined (typeof (FlatStyle), value))
  153. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
  154. if (flat_style == value)
  155. return;
  156. flat_style = value;
  157. Refresh ();
  158. }
  159. }
  160. [Localizable(true)]
  161. public Image Image {
  162. get {
  163. if (image != null) {
  164. return image;
  165. }
  166. if (image_list != null && ImageIndex >= 0) {
  167. return image_list.Images[ImageIndex];
  168. }
  169. return null;
  170. }
  171. set {
  172. if (image == value)
  173. return;
  174. image = value;
  175. Refresh ();
  176. }
  177. }
  178. [DefaultValue(ContentAlignment.MiddleCenter)]
  179. [Localizable(true)]
  180. public ContentAlignment ImageAlign {
  181. get {
  182. return image_align;
  183. }
  184. set {
  185. if (!Enum.IsDefined (typeof (ContentAlignment), value))
  186. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
  187. if (image_align == value)
  188. return;
  189. image_align = value;
  190. Refresh ();
  191. }
  192. }
  193. [DefaultValue (-1)]
  194. [Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  195. [Localizable (true)]
  196. [TypeConverter (typeof (ImageIndexConverter))]
  197. public int ImageIndex {
  198. get {
  199. if (ImageList == null) {
  200. return -1;
  201. }
  202. if (image_index >= image_list.Images.Count) {
  203. return image_list.Images.Count - 1;
  204. }
  205. return image_index;
  206. }
  207. set {
  208. if (value < -1)
  209. throw new ArgumentException ();
  210. if (image_index == value)
  211. return;
  212. image_index = value;
  213. if (ImageList != null && image_index !=-1)
  214. Image = null;
  215. Refresh ();
  216. }
  217. }
  218. [DefaultValue(null)]
  219. public ImageList ImageList {
  220. get { return image_list;}
  221. set {
  222. if (image_list == value)
  223. return;
  224. image_list = value;
  225. if (image_list != null && image_index !=-1)
  226. Image = null;
  227. Refresh ();
  228. }
  229. }
  230. [Browsable(false)]
  231. [EditorBrowsable(EditorBrowsableState.Never)]
  232. public new ImeMode ImeMode {
  233. get { return base.ImeMode; }
  234. set {
  235. if (value == ImeMode)
  236. return;
  237. base.ImeMode = value;
  238. if (ImeModeChanged != null)
  239. ImeModeChanged (this, EventArgs.Empty);
  240. }
  241. }
  242. [Browsable(false)]
  243. [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
  244. [EditorBrowsable(EditorBrowsableState.Advanced)]
  245. public virtual int PreferredHeight {
  246. get { return preferred_height; }
  247. }
  248. [Browsable(false)]
  249. [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Hidden)]
  250. [EditorBrowsable(EditorBrowsableState.Advanced)]
  251. public virtual int PreferredWidth {
  252. get {return preferred_width; }
  253. }
  254. protected virtual bool RenderTransparent {
  255. get { return render_transparent; }
  256. set { render_transparent = value;}
  257. }
  258. [Browsable(false)]
  259. [DefaultValue(false)]
  260. [EditorBrowsable(EditorBrowsableState.Never)]
  261. public new bool TabStop {
  262. get { return base.TabStop; }
  263. set {
  264. if (value == base.TabStop)
  265. return;
  266. base.TabStop = value;
  267. if (TabStopChanged != null)
  268. TabStopChanged (this, EventArgs.Empty);
  269. }
  270. }
  271. [DefaultValue(ContentAlignment.TopLeft)]
  272. [Localizable(true)]
  273. public virtual ContentAlignment TextAlign {
  274. get { return text_align; }
  275. set {
  276. if (!Enum.IsDefined (typeof (ContentAlignment), value))
  277. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
  278. if (text_align != value) {
  279. text_align = value;
  280. switch (value) {
  281. case ContentAlignment.BottomLeft:
  282. string_format.LineAlignment = StringAlignment.Far;
  283. string_format.Alignment = StringAlignment.Near;
  284. break;
  285. case ContentAlignment.BottomCenter:
  286. string_format.LineAlignment = StringAlignment.Far;
  287. string_format.Alignment = StringAlignment.Center;
  288. break;
  289. case ContentAlignment.BottomRight:
  290. string_format.LineAlignment = StringAlignment.Far;
  291. string_format.Alignment = StringAlignment.Far;
  292. break;
  293. case ContentAlignment.TopLeft:
  294. string_format.LineAlignment = StringAlignment.Near;
  295. string_format.Alignment = StringAlignment.Near;
  296. break;
  297. case ContentAlignment.TopCenter:
  298. string_format.LineAlignment = StringAlignment.Near;
  299. string_format.Alignment = StringAlignment.Center;
  300. break;
  301. case ContentAlignment.TopRight:
  302. string_format.LineAlignment = StringAlignment.Near;
  303. string_format.Alignment = StringAlignment.Far;
  304. break;
  305. case ContentAlignment.MiddleLeft:
  306. string_format.LineAlignment = StringAlignment.Center;
  307. string_format.Alignment = StringAlignment.Near;
  308. break;
  309. case ContentAlignment.MiddleRight:
  310. string_format.LineAlignment = StringAlignment.Center;
  311. string_format.Alignment = StringAlignment.Far;
  312. break;
  313. case ContentAlignment.MiddleCenter:
  314. string_format.LineAlignment = StringAlignment.Center;
  315. string_format.Alignment = StringAlignment.Center;
  316. break;
  317. default:
  318. break;
  319. }
  320. OnTextAlignChanged (new EventArgs ());
  321. Refresh();
  322. }
  323. }
  324. }
  325. [DefaultValue(true)]
  326. public bool UseMnemonic {
  327. get { return use_mnemonic; }
  328. set {
  329. if (use_mnemonic != value) {
  330. use_mnemonic = value;
  331. SetUseMnemonic (use_mnemonic);
  332. Refresh ();
  333. }
  334. }
  335. }
  336. #endregion
  337. #region Public Methods
  338. protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
  339. {
  340. Rectangle rcImageClip = area;
  341. rcImageClip.Inflate (-2,-2);
  342. int X = area.X;
  343. int Y = area.Y;
  344. if (img_align == ContentAlignment.TopCenter ||
  345. img_align == ContentAlignment.MiddleCenter ||
  346. img_align == ContentAlignment.BottomCenter) {
  347. X += (area.Width - image.Width) / 2;
  348. }
  349. else if (img_align == ContentAlignment.TopRight ||
  350. img_align == ContentAlignment.MiddleRight||
  351. img_align == ContentAlignment.BottomRight) {
  352. X += (area.Width - image.Width);
  353. }
  354. if( img_align == ContentAlignment.BottomCenter ||
  355. img_align == ContentAlignment.BottomLeft ||
  356. img_align == ContentAlignment.BottomRight) {
  357. Y += area.Height - image.Height;
  358. }
  359. else if(img_align == ContentAlignment.MiddleCenter ||
  360. img_align == ContentAlignment.MiddleLeft ||
  361. img_align == ContentAlignment.MiddleRight) {
  362. Y += (area.Height - image.Height) / 2;
  363. }
  364. rcImageClip.X = X;
  365. rcImageClip.Y = Y;
  366. rcImageClip.Width = image.Width;
  367. rcImageClip.Height = image.Height;
  368. return rcImageClip;
  369. }
  370. protected override AccessibleObject CreateAccessibilityInstance ()
  371. {
  372. return base.CreateAccessibilityInstance ();
  373. }
  374. protected override void Dispose(bool disposing)
  375. {
  376. base.Dispose (disposing);
  377. if (disposing)
  378. string_format.Dispose ();
  379. }
  380. protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
  381. {
  382. if (image == null || g == null)
  383. return;
  384. Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
  385. if (Enabled)
  386. g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
  387. else
  388. ControlPaint.DrawImageDisabled (g, image, rcImageClip.X, rcImageClip.Y, BackColor);
  389. }
  390. protected virtual void OnAutoSizeChanged (EventArgs e)
  391. {
  392. if (AutoSizeChanged != null)
  393. AutoSizeChanged (this, e);
  394. }
  395. protected override void OnEnabledChanged (EventArgs e)
  396. {
  397. base.OnEnabledChanged (e);
  398. }
  399. protected override void OnFontChanged (EventArgs e)
  400. {
  401. base.OnFontChanged (e);
  402. if (autosize) {
  403. CalcAutoSize();
  404. } else {
  405. CalcPreferredHeight ();
  406. }
  407. Refresh ();
  408. }
  409. protected override void OnPaint (PaintEventArgs pevent)
  410. {
  411. ThemeEngine.Current.DrawLabel (pevent.Graphics, ClientRectangle, this);
  412. DrawImage (pevent.Graphics, Image, ClientRectangle, image_align);
  413. base.OnPaint(pevent);
  414. }
  415. protected override void OnParentChanged (EventArgs e)
  416. {
  417. base.OnParentChanged (e);
  418. }
  419. protected virtual void OnTextAlignChanged (EventArgs e)
  420. {
  421. if (TextAlignChanged != null)
  422. TextAlignChanged (this, e);
  423. }
  424. protected override void OnTextChanged (EventArgs e)
  425. {
  426. base.OnTextChanged (e);
  427. if (autosize) {
  428. CalcAutoSize ();
  429. } else {
  430. CalcPreferredWidth ();
  431. }
  432. Refresh ();
  433. }
  434. protected override void OnVisibleChanged (EventArgs e)
  435. {
  436. base.OnVisibleChanged (e);
  437. }
  438. protected override bool ProcessMnemonic (char charCode)
  439. {
  440. if (IsMnemonic(charCode, Text) == true) {
  441. // Select item next in line in tab order
  442. if (this.parent != null) {
  443. parent.SelectNextControl(this, true, false, false, false);
  444. }
  445. return true;
  446. }
  447. return base.ProcessMnemonic (charCode);
  448. }
  449. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  450. {
  451. base.SetBoundsCore (x, y, width, height, specified);
  452. }
  453. public override string ToString()
  454. {
  455. return base.ToString () + ", Text: " + Text;
  456. }
  457. protected override void WndProc (ref Message m)
  458. {
  459. switch ((Msg) m.Msg) {
  460. case Msg.WM_DRAWITEM: {
  461. m.Result = (IntPtr)1;
  462. }
  463. break;
  464. default:
  465. base.WndProc (ref m);
  466. break;
  467. }
  468. }
  469. #endregion Public Methods
  470. #region Private Methods
  471. private void CalcAutoSize ()
  472. {
  473. if (IsHandleCreated == false || AutoSize == false)
  474. return;
  475. CalcPreferredWidth ();
  476. CalcPreferredHeight ();
  477. Width = PreferredWidth;
  478. Height = PreferredHeight;
  479. }
  480. private void CalcPreferredHeight ()
  481. {
  482. preferred_height = Font.Height;
  483. switch (border_style) {
  484. case BorderStyle.None:
  485. preferred_height += 3;
  486. break;
  487. case BorderStyle.FixedSingle:
  488. case BorderStyle.Fixed3D:
  489. preferred_height += 6;
  490. break;
  491. default:
  492. break;
  493. }
  494. }
  495. private void CalcPreferredWidth ()
  496. {
  497. SizeF size;
  498. size = DeviceContext.MeasureString (Text, Font, req_witdthsize, string_format);
  499. preferred_width = (int) size.Width + 3;
  500. }
  501. private void OnHandleCreatedLB (Object o, EventArgs e)
  502. {
  503. if (autosize)
  504. CalcAutoSize ();
  505. }
  506. private void SetUseMnemonic (bool use)
  507. {
  508. if (use)
  509. string_format.HotkeyPrefix = HotkeyPrefix.Show;
  510. else
  511. string_format.HotkeyPrefix = HotkeyPrefix.None;
  512. }
  513. #endregion Private Methods
  514. #if NET_2_0
  515. public bool UseCompatibleTextRendering {
  516. get {
  517. return use_compatible_text_rendering;
  518. }
  519. set {
  520. use_compatible_text_rendering = value;
  521. }
  522. }
  523. #endif
  524. }
  525. }