Label.cs 15 KB

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