Label.cs 15 KB

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