Label.cs 15 KB

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