Label.cs 15 KB

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