Label.cs 14 KB

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