Label.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. // TODO:
  27. // - The AutoSize functionality needs missing control functions to be implemented
  28. // - Draw BorderStyle and FlatStyle
  29. //
  30. // Based on work by:
  31. // Daniel Carrera, [email protected] (stubbed out)
  32. //
  33. // INCOMPLETE
  34. using System.Drawing;
  35. using System.Drawing.Text;
  36. using System.Drawing.Imaging;
  37. using System.ComponentModel;
  38. namespace System.Windows.Forms
  39. {
  40. public class Label : Control
  41. {
  42. private Image background_image;
  43. private BorderStyle border_style;
  44. private bool autoSize;
  45. private Image image;
  46. private bool render_transparent;
  47. private FlatStyle flat_style;
  48. private int preferred_height;
  49. private int preferred_width;
  50. private bool tab_stop;
  51. private bool use_mnemonic;
  52. private int image_index = -1;
  53. private ImageList image_list;
  54. protected Bitmap bmp_mem;
  55. protected Graphics dc_mem;
  56. protected Rectangle paint_area;
  57. protected ContentAlignment image_align;
  58. protected StringFormat string_format;
  59. protected ContentAlignment text_align;
  60. #region Events
  61. public event EventHandler AutoSizeChanged;
  62. public event EventHandler TextAlignChanged;
  63. #endregion
  64. public Label () : base ()
  65. {
  66. // Defaults in the Spec
  67. autoSize = false;
  68. border_style = BorderStyle.None;
  69. string_format = new StringFormat();
  70. TextAlign = ContentAlignment.TopLeft;
  71. image = null;
  72. bmp_mem = null;
  73. dc_mem = null;
  74. UseMnemonic = true;
  75. image_list = null;
  76. paint_area = new Rectangle ();
  77. image_align = ContentAlignment.MiddleCenter;
  78. set_usemnemonic (UseMnemonic);
  79. BackColor = Color.FromArgb (255, 236, 233, 216);
  80. ForeColor = Color.Black;
  81. CalcPreferredHeight ();
  82. CalcPreferredWidth ();
  83. AutoSizeChanged = null;
  84. TextAlignChanged = null;
  85. }
  86. #region Public Properties
  87. public virtual bool AutoSize {
  88. get { return autoSize; }
  89. set {
  90. if (autoSize == value)
  91. return;
  92. autoSize = value;
  93. CalcAutoSize ();
  94. Refresh ();
  95. if (AutoSizeChanged != null)
  96. AutoSizeChanged (this, new EventArgs ());
  97. }
  98. }
  99. public override Image BackgroundImage {
  100. get {
  101. return background_image;
  102. }
  103. set {
  104. background_image = value;
  105. Refresh ();
  106. }
  107. }
  108. public virtual BorderStyle BorderStyle {
  109. get {
  110. return border_style;
  111. }
  112. set {
  113. if (!Enum.IsDefined (typeof (BorderStyle), value))
  114. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for BorderStyle", value));
  115. if (border_style == value)
  116. return;
  117. border_style = value;
  118. Refresh ();
  119. }
  120. }
  121. public FlatStyle FlatStyle {
  122. get {
  123. return flat_style;
  124. }
  125. set {
  126. if (!Enum.IsDefined (typeof (FlatStyle), value))
  127. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for FlatStyle", value));
  128. if (flat_style == value)
  129. return;
  130. flat_style = value;
  131. Refresh ();
  132. }
  133. }
  134. public Image Image {
  135. get {
  136. return image;
  137. }
  138. set {
  139. if (image == value)
  140. return;
  141. image = value;
  142. Refresh ();
  143. }
  144. }
  145. public ContentAlignment ImageAlign {
  146. get {
  147. return image_align;
  148. }
  149. set {
  150. if (!Enum.IsDefined (typeof (ContentAlignment), value))
  151. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
  152. if (image_align == value)
  153. return;
  154. image_align = value;
  155. Refresh ();
  156. }
  157. }
  158. public int ImageIndex {
  159. get { return image_index;}
  160. set {
  161. if (value < 0 || value>= image_list.Images.Count)
  162. throw new ArgumentException();
  163. if (image_index == value)
  164. return;
  165. image_index = value;
  166. if (ImageList != null && image_index !=-1)
  167. Image = null;
  168. Refresh ();
  169. }
  170. }
  171. public ImageList ImageList {
  172. get { return image_list;}
  173. set {
  174. if (image_list == value)
  175. return;
  176. if (ImageList != null && image_index !=-1)
  177. Image = null;
  178. Refresh ();
  179. }
  180. }
  181. private void CalcPreferredHeight ()
  182. {
  183. if (font == null) {
  184. preferred_height = 0;
  185. return;
  186. }
  187. preferred_height = Font.Height;
  188. if (border_style == BorderStyle.None)
  189. preferred_height += 3;
  190. }
  191. public virtual int PreferredHeight {
  192. get { return preferred_height; }
  193. }
  194. private void CalcPreferredWidth ()
  195. {
  196. if (font == null) {
  197. preferred_width = 0;
  198. return;
  199. }
  200. SizeF size;
  201. size = dc_mem.MeasureString (Text, Font, new SizeF (paint_area.Width,
  202. paint_area.Height), string_format);
  203. preferred_width = Size.Width;
  204. }
  205. public virtual int PreferredWidth {
  206. get {return preferred_width; }
  207. }
  208. public bool TabStop {
  209. get { return tab_stop; }
  210. set { tab_stop = value; }
  211. }
  212. public virtual ContentAlignment TextAlign {
  213. get { return text_align; }
  214. set {
  215. if (!Enum.IsDefined (typeof (ContentAlignment), value))
  216. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ContentAlignment", value));
  217. if (text_align != value) {
  218. text_align = value;
  219. switch (value) {
  220. case ContentAlignment.BottomLeft:
  221. string_format.LineAlignment = StringAlignment.Far;
  222. string_format.Alignment = StringAlignment.Near;
  223. break;
  224. case ContentAlignment.BottomCenter:
  225. string_format.LineAlignment = StringAlignment.Far;
  226. string_format.Alignment = StringAlignment.Center;
  227. break;
  228. case ContentAlignment.BottomRight:
  229. string_format.LineAlignment = StringAlignment.Far;
  230. string_format.Alignment = StringAlignment.Far;
  231. break;
  232. case ContentAlignment.TopLeft:
  233. string_format.LineAlignment = StringAlignment.Near;
  234. string_format.Alignment = StringAlignment.Near;
  235. break;
  236. case ContentAlignment.TopCenter:
  237. string_format.LineAlignment = StringAlignment.Near;
  238. string_format.Alignment = StringAlignment.Center;
  239. break;
  240. case ContentAlignment.TopRight:
  241. string_format.LineAlignment = StringAlignment.Near;
  242. string_format.Alignment = StringAlignment.Far;
  243. break;
  244. case ContentAlignment.MiddleLeft:
  245. string_format.LineAlignment = StringAlignment.Center;
  246. string_format.Alignment = StringAlignment.Near;
  247. break;
  248. case ContentAlignment.MiddleRight:
  249. string_format.LineAlignment = StringAlignment.Center;
  250. string_format.Alignment = StringAlignment.Far;
  251. break;
  252. case ContentAlignment.MiddleCenter:
  253. string_format.LineAlignment = StringAlignment.Center;
  254. string_format.Alignment = StringAlignment.Center;
  255. break;
  256. default:
  257. break;
  258. }
  259. if (TextAlignChanged != null)
  260. TextAlignChanged (this, new EventArgs ());
  261. Refresh();
  262. }
  263. }
  264. }
  265. public bool UseMnemonic {
  266. get { return use_mnemonic; }
  267. set {
  268. if (use_mnemonic != value) {
  269. use_mnemonic = value;
  270. set_usemnemonic (use_mnemonic);
  271. Refresh ();
  272. }
  273. }
  274. }
  275. #endregion
  276. protected override CreateParams CreateParams {
  277. get {
  278. CreateParams createParams = base.CreateParams;
  279. createParams.ClassName = XplatUI.DefaultClassName;
  280. createParams.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_VISIBLE |(int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
  281. return createParams;
  282. }
  283. }
  284. protected override Size DefaultSize {
  285. get {return new Size (100,23);}
  286. }
  287. protected virtual bool RenderTransparent {
  288. get { return render_transparent; }
  289. set { render_transparent = value;}
  290. }
  291. protected override ImeMode DefaultImeMode {
  292. get { return ImeMode.Disable;}
  293. }
  294. #region Methods
  295. public override string ToString()
  296. {
  297. //FIXME: add name of lable, as well as text. would adding base.ToString work?
  298. return "Label: " + base.Text;
  299. }
  300. protected override void Dispose(bool disposing)
  301. {
  302. base.Dispose (disposing);
  303. }
  304. protected Rectangle CalcImageRenderBounds (Image image, Rectangle area, ContentAlignment img_align)
  305. {
  306. Rectangle rcImageClip = area;
  307. rcImageClip.Inflate (-2,-2);
  308. int X = area.X;
  309. int Y = area.Y;
  310. if (img_align == ContentAlignment.TopCenter ||
  311. img_align == ContentAlignment.MiddleCenter ||
  312. img_align == ContentAlignment.BottomCenter) {
  313. X += (area.Width - image.Width) / 2;
  314. }
  315. else if (img_align == ContentAlignment.TopRight ||
  316. img_align == ContentAlignment.MiddleRight||
  317. img_align == ContentAlignment.BottomRight) {
  318. X += (area.Width - image.Width);
  319. }
  320. if( img_align == ContentAlignment.BottomCenter ||
  321. img_align == ContentAlignment.BottomLeft ||
  322. img_align == ContentAlignment.BottomRight) {
  323. Y += area.Height - image.Height;
  324. }
  325. else if(img_align == ContentAlignment.MiddleCenter ||
  326. img_align == ContentAlignment.MiddleLeft ||
  327. img_align == ContentAlignment.MiddleRight) {
  328. Y += (area.Height - image.Height) / 2;
  329. }
  330. rcImageClip.X = X;
  331. rcImageClip.Y = Y;
  332. rcImageClip.Width = image.Width;
  333. rcImageClip.Height = image.Height;
  334. return rcImageClip;
  335. }
  336. protected override AccessibleObject CreateAccessibilityInstance()
  337. {
  338. return base.CreateAccessibilityInstance();
  339. }
  340. protected void DrawImage (Graphics g, Image image, Rectangle area, ContentAlignment img_align)
  341. {
  342. if (image == null || g == null)
  343. return;
  344. Rectangle rcImageClip = CalcImageRenderBounds (image, area, img_align);
  345. g.DrawImage (image, rcImageClip.X, rcImageClip.Y, rcImageClip.Width, rcImageClip.Height);
  346. }
  347. protected virtual void OnAutoSizeChanged (EventArgs e)
  348. {
  349. if (AutoSizeChanged != null)
  350. AutoSizeChanged (this, e);
  351. }
  352. protected override void OnEnabledChanged (EventArgs e)
  353. {
  354. base.OnEnabledChanged (e);
  355. }
  356. protected override void OnFontChanged (EventArgs e)
  357. {
  358. Console.WriteLine ("OnFontChanged");
  359. base.OnFontChanged (e);
  360. CalcPreferredHeight ();
  361. }
  362. private void CalcAutoSize ()
  363. {
  364. if (IsHandleCreated == false)
  365. return;
  366. SizeF size;
  367. size = dc_mem.MeasureString (Text, Font, new SizeF (paint_area.Width,
  368. paint_area.Height), string_format);
  369. Width = Size.Width;
  370. Height = Size.Height;
  371. Invalidate ();
  372. Console.WriteLine ("CalcAutoSize () after " + Size);
  373. }
  374. protected virtual void Draw ()
  375. {
  376. ThemeEngine.Current.DrawLabel (dc_mem, paint_area, BorderStyle, Text,
  377. ForeColor, BackColor, Font, string_format, Enabled);
  378. DrawImage (dc_mem, Image, paint_area, image_align);
  379. }
  380. private void set_usemnemonic (bool use)
  381. {
  382. if (use)
  383. string_format.HotkeyPrefix = HotkeyPrefix.Show;
  384. else
  385. string_format.HotkeyPrefix = HotkeyPrefix.None;
  386. }
  387. protected override void OnPaint (PaintEventArgs pevent)
  388. {
  389. if (Width <= 0 || Height <= 0 || Visible == false)
  390. return;
  391. /* Copies memory drawing buffer to screen*/
  392. UpdateArea ();
  393. Draw ();
  394. pevent.Graphics.DrawImage (bmp_mem, 0, 0);
  395. }
  396. protected override void OnParentChanged (EventArgs e)
  397. {
  398. base.OnParentChanged (e);
  399. }
  400. protected virtual void OnTextAlignChanged (EventArgs e)
  401. {
  402. if (TextAlignChanged != null)
  403. TextAlignChanged (this, e);
  404. }
  405. protected override void OnTextChanged (EventArgs e)
  406. {
  407. base.OnTextChanged (e);
  408. CalcPreferredWidth ();
  409. Refresh ();
  410. }
  411. protected override void OnVisibleChanged (EventArgs e)
  412. {
  413. base.OnVisibleChanged (e);
  414. }
  415. protected override bool ProcessMnemonic(char charCode)
  416. {
  417. return base.ProcessMnemonic (charCode);
  418. }
  419. protected override void OnHandleCreated (EventArgs e)
  420. {
  421. base.OnHandleCreated(e);
  422. //Console.WriteLine ("OnHandleCreated");
  423. UpdateArea ();
  424. bmp_mem = new Bitmap (Width, Height, PixelFormat.Format32bppArgb);
  425. dc_mem = Graphics.FromImage (bmp_mem);
  426. if (AutoSize)
  427. CalcAutoSize ();
  428. }
  429. private void UpdateArea ()
  430. {
  431. paint_area.X = paint_area.Y = 0;
  432. paint_area.Width = Width;
  433. paint_area.Height = Height;
  434. }
  435. protected override void OnResize (EventArgs e)
  436. {
  437. //Console.WriteLine ("OnResize");
  438. base.OnResize (e);
  439. if (Width <= 0 || Height <= 0)
  440. return;
  441. UpdateArea ();
  442. /* Area for double buffering */
  443. bmp_mem = new Bitmap (Width, Height, PixelFormat.Format32bppArgb);
  444. dc_mem = Graphics.FromImage (bmp_mem);
  445. }
  446. #if nodef
  447. protected override void SetBoundsCore (
  448. int x, int y, int width, int height,
  449. BoundsSpecified specified)
  450. {
  451. base.SetBoundsCore (x, y, width, height, specified);
  452. }
  453. #endif
  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
  467. }
  468. }