2
0

Label.cs 16 KB

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