Label.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. //
  2. // System.Windows.Forms.Label.cs
  3. //
  4. // Author:
  5. // stubbed out by Daniel Carrera ([email protected])
  6. // implemented for Gtk+ by Rachel Hestilow ([email protected])
  7. // Dennis Hayes ([email protected])
  8. // WineLib implementation started by John Sohn ([email protected])
  9. //
  10. // (C) 2002/3 Ximian, Inc
  11. //
  12. namespace System.Windows.Forms {
  13. using System.ComponentModel;
  14. using System.Drawing;
  15. using System.Drawing.Text;
  16. public class Label : Control {
  17. Image background_image;
  18. BorderStyle border_style;
  19. bool autoSize;
  20. Image image;
  21. ContentAlignment image_align;
  22. StringFormat string_format;
  23. // ImeMode default_ime_mode;
  24. bool render_transparent;
  25. FlatStyle flat_style;
  26. int preferred_height;
  27. int preferred_width;
  28. bool tab_stop;
  29. ContentAlignment text_align;
  30. bool use_mnemonic;
  31. public Label () : base ()
  32. {
  33. // Defaults in the Spec
  34. autoSize = false;
  35. border_style = BorderStyle.None;
  36. // base.TabStop = false;
  37. text_align = ContentAlignment.TopLeft;
  38. // SetStyle (ControlStyles.Selectable, false);
  39. // SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  40. string_format = new StringFormat();
  41. string_format.HotkeyPrefix=HotkeyPrefix.Show;
  42. }
  43. #region Properties
  44. public virtual bool AutoSize {
  45. get {
  46. return autoSize;
  47. }
  48. set {
  49. autoSize = value;
  50. }
  51. }
  52. public override Image BackgroundImage {
  53. get {
  54. return background_image;
  55. }
  56. set {
  57. background_image = value;
  58. Refresh ();
  59. }
  60. }
  61. public virtual BorderStyle BorderStyle {
  62. get {
  63. return border_style;
  64. }
  65. set {
  66. if (border_style == value)
  67. return;
  68. border_style = value;
  69. RecreateHandle ();
  70. }
  71. }
  72. public FlatStyle FlatStyle {
  73. get {
  74. return flat_style;
  75. }
  76. set {
  77. flat_style = value;
  78. Refresh ();
  79. }
  80. }
  81. public Image Image {
  82. get {
  83. return image;
  84. }
  85. set {
  86. image = value;
  87. Refresh ();
  88. }
  89. }
  90. public ContentAlignment ImageAlign {
  91. get {
  92. return image_align;
  93. }
  94. set {
  95. image_align = value;
  96. }
  97. }
  98. [MonoTODO]
  99. public int ImageIndex {
  100. get {
  101. throw new NotImplementedException ();
  102. }
  103. set {
  104. //FIXME:
  105. }
  106. }
  107. #if nodef
  108. [MonoTODO]
  109. public ImageList ImageList {
  110. get {
  111. throw new NotImplementedException ();
  112. }
  113. set {
  114. //FIXME:
  115. }
  116. }
  117. #endif
  118. protected override ImeMode DefaultImeMode {
  119. get {
  120. return ImeMode.Disable;
  121. }
  122. }
  123. public virtual int PreferredHeight {
  124. get {
  125. return preferred_height;
  126. }
  127. }
  128. public virtual int PreferredWidth {
  129. get {
  130. return preferred_width;
  131. }
  132. }
  133. public new bool TabStop {
  134. get {
  135. return tab_stop;
  136. }
  137. set {
  138. tab_stop = value;
  139. }
  140. }
  141. //Compact Framework
  142. public virtual ContentAlignment TextAlign {
  143. get {
  144. return text_align;
  145. }
  146. set {
  147. text_align = value;
  148. // Calculate vertical alignment
  149. if ((value == ContentAlignment.BottomLeft) || (value == ContentAlignment.BottomCenter) || (value == ContentAlignment.BottomRight)) {
  150. string_format.LineAlignment=StringAlignment.Far;
  151. } else if ((value == ContentAlignment.TopLeft) || (value == ContentAlignment.TopCenter) || (value == ContentAlignment.TopRight)) {
  152. string_format.LineAlignment=StringAlignment.Near;
  153. } else {
  154. string_format.LineAlignment=StringAlignment.Center;
  155. }
  156. // Calculate horizontal alignment
  157. if ((value == ContentAlignment.TopLeft) || (value == ContentAlignment.MiddleLeft) || (value == ContentAlignment.BottomLeft)) {
  158. string_format.Alignment=StringAlignment.Near;
  159. } else if ((value == ContentAlignment.TopRight) || (value == ContentAlignment.MiddleRight) || (value == ContentAlignment.BottomRight)) {
  160. string_format.LineAlignment=StringAlignment.Far;
  161. } else {
  162. string_format.LineAlignment=StringAlignment.Center;
  163. }
  164. }
  165. }
  166. public bool UseMnemonic {
  167. get {
  168. return use_mnemonic;
  169. }
  170. set {
  171. use_mnemonic = value;
  172. }
  173. }
  174. protected override CreateParams CreateParams {
  175. get {
  176. CreateParams createParams = base.CreateParams;
  177. createParams.ClassName = XplatUI.DefaultClassName;
  178. int bs = 0;
  179. #if notyet
  180. if (border_style == BorderStyle.FixedSingle)
  181. bs |= (int) WindowStyles.WS_BORDER;
  182. else if (border_style == BorderStyle.Fixed3D)
  183. bs |= (int) WindowStyles.WS_BORDER | (int) SS_Static_Control_Types.SS_SUNKEN;
  184. createParams.Style = (int) (
  185. (int)WindowStyles.WS_CHILD |
  186. (int)WindowStyles.WS_VISIBLE |
  187. (int)SS_Static_Control_Types.SS_LEFT |
  188. (int)WindowStyles.WS_CLIPCHILDREN |
  189. (int)WindowStyles.WS_CLIPSIBLINGS |
  190. (int)SS_Static_Control_Types.SS_OWNERDRAW |
  191. bs);
  192. #else
  193. createParams.Style = (int)WindowStyles.WS_CHILD | (int)WindowStyles.WS_VISIBLE |(int)WindowStyles.WS_CLIPCHILDREN | (int)WindowStyles.WS_CLIPSIBLINGS;
  194. #endif
  195. return createParams;
  196. }
  197. }
  198. protected override Size DefaultSize {
  199. get {
  200. return new Size(100,23);//Correct value
  201. }
  202. }
  203. protected virtual bool RenderTransparent {
  204. get {
  205. return render_transparent;
  206. }
  207. set {
  208. //FIXME:
  209. }
  210. }
  211. #if nodef
  212. protected override ImeMode DefaultImeMode {
  213. get {
  214. //FIXME:
  215. return default_ime_mode;
  216. }
  217. }
  218. #endif
  219. #endregion
  220. #region Methods
  221. public override string ToString()
  222. {
  223. //FIXME: add name of lable, as well as text. would adding base.ToString work?
  224. return "Label: " + base.Text;
  225. }
  226. [MonoTODO]
  227. protected override void Dispose(bool disposing){
  228. base.Dispose(disposing);
  229. }
  230. [MonoTODO]
  231. protected Rectangle CalcImageRenderBounds (
  232. Image image, Rectangle r, ContentAlignment align)
  233. {
  234. throw new NotImplementedException ();
  235. }
  236. [MonoTODO]
  237. protected override AccessibleObject CreateAccessibilityInstance()
  238. {
  239. //FIXME:
  240. return base.CreateAccessibilityInstance();
  241. }
  242. [MonoTODO]
  243. protected void DrawImage (Graphics g, Image image,
  244. Rectangle r, ContentAlignment align)
  245. {
  246. //FIXME:
  247. }
  248. protected virtual void OnAutoSizeChanged (EventArgs e)
  249. {
  250. if (AutoSizeChanged != null) AutoSizeChanged (this, e);
  251. }
  252. protected override void OnEnabledChanged (EventArgs e)
  253. {
  254. //FIXME:
  255. base.OnEnabledChanged (e);
  256. }
  257. protected override void OnFontChanged (EventArgs e)
  258. {
  259. //FIXME:
  260. base.OnFontChanged (e);
  261. }
  262. protected override void OnPaint (PaintEventArgs e)
  263. {
  264. Rectangle paintBounds = ClientRectangle;
  265. Bitmap bmp = new Bitmap( paintBounds.Width, paintBounds.Height,e.Graphics);
  266. Graphics paintOn = Graphics.FromImage(bmp);
  267. Color controlColor = BackColor; //SystemColors.Control;
  268. Color textColor = ForeColor; // SystemColors.ControlText;
  269. if (BackColor == System.Drawing.Color.Red) {
  270. Color t = System.Drawing.Color.Red;
  271. }
  272. Rectangle rc = paintBounds;
  273. Rectangle rcImageClip = paintBounds;
  274. rcImageClip.Inflate(-2,-2);
  275. SolidBrush sb = new SolidBrush( controlColor);
  276. paintOn.FillRectangle(sb, rc);
  277. sb.Dispose();
  278. // Do not place Text and Images on the borders
  279. paintOn.Clip = new Region(rcImageClip);
  280. if(Image != null) {
  281. int X = rc.X;
  282. int Y = rc.Y;
  283. if( ImageAlign == ContentAlignment.TopCenter ||
  284. ImageAlign == ContentAlignment.MiddleCenter ||
  285. ImageAlign == ContentAlignment.BottomCenter) {
  286. X += (rc.Width - Image.Width) / 2;
  287. }
  288. else if(ImageAlign == ContentAlignment.TopRight ||
  289. ImageAlign == ContentAlignment.MiddleRight||
  290. ImageAlign == ContentAlignment.BottomRight) {
  291. X += (rc.Width - Image.Width);
  292. }
  293. if( ImageAlign == ContentAlignment.BottomCenter ||
  294. ImageAlign == ContentAlignment.BottomLeft ||
  295. ImageAlign == ContentAlignment.BottomRight) {
  296. Y += rc.Height - Image.Height;
  297. }
  298. else if(ImageAlign == ContentAlignment.MiddleCenter ||
  299. ImageAlign == ContentAlignment.MiddleLeft ||
  300. ImageAlign == ContentAlignment.MiddleRight) {
  301. Y += (rc.Height - Image.Height) / 2;
  302. }
  303. paintOn.DrawImage(Image, X, Y, Image.Width, Image.Height);
  304. }
  305. if (Enabled) {
  306. SolidBrush brush;
  307. brush=new SolidBrush(textColor);
  308. paintOn.DrawString(Text, Font, brush, rc, string_format);
  309. brush.Dispose();
  310. }
  311. else {
  312. ControlPaint.DrawStringDisabled(paintOn, Text, Font, textColor, rc, string_format);
  313. }
  314. e.Graphics.DrawImage(bmp, 0, 0, paintBounds.Width, paintBounds.Height);
  315. paintOn.Dispose ();
  316. bmp.Dispose();
  317. }
  318. //Compact Framework
  319. protected override void OnParentChanged (EventArgs e)
  320. {
  321. base.OnParentChanged (e);
  322. }
  323. protected virtual void OnTextAlignChanged (EventArgs e) {
  324. if (TextAlignChanged != null) TextAlignChanged (this, e);
  325. }
  326. //Compact Framework
  327. protected override void OnTextChanged (EventArgs e) {
  328. base.OnTextChanged (e);
  329. Invalidate ();
  330. Refresh ();
  331. }
  332. protected override void OnVisibleChanged (EventArgs e)
  333. {
  334. base.OnVisibleChanged (e);
  335. }
  336. protected override bool ProcessMnemonic(char charCode)
  337. {
  338. return base.ProcessMnemonic (charCode);
  339. }
  340. // [MonoTODO]
  341. // protected new ContentAlignment RtlTranslateAlignment (
  342. // ContentAlignment alignment)
  343. // {
  344. // throw new NotImplementedException ();
  345. // }
  346. //
  347. // [MonoTODO]
  348. // protected new HorizontalAlignment RtlTranslateAlignment (
  349. // HorizontalAlignment alignment)
  350. // {
  351. // throw new NotImplementedException ();
  352. // }
  353. //
  354. // [MonoTODO]
  355. // protected new LeftRightAlignment RtlTranslateAlignment (
  356. // LeftRightAlignment align)
  357. // {
  358. // throw new NotImplementedException ();
  359. // }
  360. //
  361. // [MonoTODO]
  362. // protected new virtual void Select (bool directed, bool forward)
  363. // {
  364. // //FIXME:
  365. // }
  366. #if nodef
  367. protected override void SetBoundsCore (
  368. int x, int y, int width, int height,
  369. BoundsSpecified specified)
  370. {
  371. base.SetBoundsCore (x, y, width, height, specified);
  372. }
  373. #endif
  374. // protected new void UpdateBounds()
  375. // {
  376. // base.UpdateBounds ();
  377. // }
  378. //
  379. // protected new void UpdateBounds (int x, int y,
  380. // int width, int height)
  381. // {
  382. // base.UpdateBounds (x, y, width, height);
  383. // }
  384. //
  385. //
  386. // protected new void UpdateBounds (int x, int y, int width,
  387. // int height, int clientWidth,
  388. // int clientHeight)
  389. // {
  390. // base.UpdateBounds (x, y, width, height, clientWidth, clientHeight);
  391. // }
  392. protected override void WndProc(ref Message m)
  393. {
  394. switch ((Msg) m.Msg) {
  395. case Msg.WM_DRAWITEM: {
  396. m.Result = (IntPtr)1;
  397. }
  398. break;
  399. default:
  400. base.WndProc (ref m);
  401. break;
  402. }
  403. }
  404. #endregion
  405. #region Events
  406. public event EventHandler AutoSizeChanged;
  407. public event EventHandler TextAlignChanged;
  408. #endregion
  409. }
  410. }