Label.cs 15 KB

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