Label.cs 15 KB

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