ButtonBase.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. // Peter Bartok [email protected]
  24. //
  25. // $Log: ButtonBase.cs,v $
  26. // Revision 1.8 2004/09/02 22:24:35 pbartok
  27. // - Fixed selection of text color
  28. // - Fixed handling of resize event; now properly recreates double buffering
  29. // bitmap
  30. // - Added missing assignment of TextAlignment
  31. // - Added proper default for TextAlignment
  32. //
  33. // Revision 1.7 2004/09/01 02:07:37 pbartok
  34. // - Enabled display of strings
  35. //
  36. // Revision 1.6 2004/09/01 01:55:20 pbartok
  37. // - Removed the rather odd split between 'needs redraw' and redrawing
  38. // - Now handles the events that require regeneration (ambient properties and
  39. // size)
  40. //
  41. // Revision 1.5 2004/08/31 18:49:58 pbartok
  42. // - Removed debug
  43. // - Minor fixes
  44. //
  45. // Revision 1.4 2004/08/30 20:42:10 pbartok
  46. // - Made Redraw() and Redraw() virtual
  47. // - Improved mouse up/down/move logic to properly track buttons
  48. //
  49. // Revision 1.3 2004/08/23 23:27:44 pbartok
  50. // - Finishing touches. Works now, just needs some optimizations.
  51. //
  52. // Revision 1.2 2004/08/21 21:57:41 pbartok
  53. // - Added loads of debug output for development
  54. // - Fixed typo in method name
  55. //
  56. // Revision 1.1 2004/08/15 21:31:10 pbartok
  57. // - First (mostly) working version
  58. //
  59. //
  60. //
  61. // NOT COMPLETE
  62. using System.ComponentModel;
  63. using System.Drawing;
  64. namespace System.Windows.Forms {
  65. public abstract class ButtonBase : Control {
  66. #region Local Variables
  67. private FlatStyle flat_style;
  68. private int image_index;
  69. private Image image;
  70. private ImageList image_list;
  71. private ContentAlignment image_alignment;
  72. private ContentAlignment text_alignment;
  73. private ImeMode ime_mode;
  74. private bool is_default;
  75. private bool has_focus;
  76. private bool is_pressed;
  77. private bool is_entered;
  78. StringFormat text_format;
  79. #endregion // Local Variables
  80. #region Private Properties and Methods
  81. internal ButtonState ButtonState {
  82. get {
  83. ButtonState ret = ButtonState.Normal;
  84. if (Enabled) {
  85. // Popup style is only followed as long as the mouse isn't "in" the control
  86. if (is_entered) {
  87. if (flat_style == FlatStyle.Flat) {
  88. ret |= ButtonState.Flat;
  89. }
  90. } else {
  91. if (flat_style == FlatStyle.Flat || flat_style == FlatStyle.Popup) {
  92. ret |= ButtonState.Flat;
  93. }
  94. }
  95. if (is_entered && is_pressed) {
  96. ret |= ButtonState.Pushed;
  97. }
  98. } else {
  99. ret |= ButtonState.Inactive;
  100. if ((flat_style == FlatStyle.Flat) || (flat_style == FlatStyle.Popup)) {
  101. ret |= ButtonState.Flat;
  102. }
  103. }
  104. return ret;
  105. }
  106. }
  107. [MonoTODO("Make the FillRectangle use a global brush instead of creating one every time")]
  108. internal virtual void Redraw() {
  109. ButtonState state;
  110. int width;
  111. int height;
  112. width = this.ClientSize.Width;
  113. height = this.ClientSize.Height;
  114. SolidBrush sb = new SolidBrush(this.BackColor);
  115. this.DeviceContext.FillRectangle(sb, this.ClientRectangle);
  116. sb.Dispose();
  117. ThemeEngine.Current.DrawButton(this.DeviceContext, this.ClientRectangle, this.ButtonState);
  118. if (has_focus) {
  119. ThemeEngine.Current.DrawFocusRectangle(this.DeviceContext, this.ClientRectangle, ThemeEngine.Current.ColorButtonText, ThemeEngine.Current.ColorButtonFace);
  120. }
  121. // First, draw the image
  122. if ((image != null) || (image_list != null)) {
  123. // Need to draw a picture
  124. Image i;
  125. int image_x;
  126. int image_y;
  127. int image_width;
  128. int image_height;
  129. if (ImageIndex!=-1) { // We use ImageIndex instead of image_index since it will return -1 if image_list is null
  130. i = this.image_list.Images[image_index];
  131. } else {
  132. i = this.image;
  133. }
  134. image_width = image.Width;
  135. image_height = image.Height;
  136. switch(image_alignment) {
  137. case ContentAlignment.TopLeft: {
  138. image_x=0;
  139. image_y=0;
  140. break;
  141. }
  142. case ContentAlignment.TopCenter: {
  143. image_x=(width-image_width)/2;
  144. image_y=0;
  145. break;
  146. }
  147. case ContentAlignment.TopRight: {
  148. image_x=width-image_width;
  149. image_y=0;
  150. break;
  151. }
  152. case ContentAlignment.MiddleLeft: {
  153. image_x=0;
  154. image_y=(height-image_height)/2;
  155. break;
  156. }
  157. case ContentAlignment.MiddleCenter: {
  158. image_x=(width-image_width)/2;
  159. image_y=(height-image_height)/2;
  160. break;
  161. }
  162. case ContentAlignment.MiddleRight: {
  163. image_x=width-image_width;
  164. image_y=(height-image_height)/2;
  165. break;
  166. }
  167. case ContentAlignment.BottomLeft: {
  168. image_x=0;
  169. image_y=height-image_height;
  170. break;
  171. }
  172. case ContentAlignment.BottomCenter: {
  173. image_x=(width-image_width)/2;
  174. image_y=height-image_height;
  175. break;
  176. }
  177. case ContentAlignment.BottomRight: {
  178. image_x=width-image_width;
  179. image_y=height-image_height;
  180. break;
  181. }
  182. default: {
  183. image_x=0;
  184. image_y=0;
  185. break;
  186. }
  187. }
  188. if (is_pressed) {
  189. image_x+=2;
  190. image_y+=2;
  191. }
  192. if (is_enabled) {
  193. this.DeviceContext.DrawImage(i, image_x, image_y);
  194. } else {
  195. ThemeEngine.Current.DrawImageDisabled(this.DeviceContext, i, image_x, image_y, ThemeEngine.Current.ColorButtonFace);
  196. }
  197. }
  198. // Now the text
  199. if (text != null && text != String.Empty) {
  200. Rectangle text_rect = new Rectangle(3, 3, ClientSize.Width-6, ClientSize.Height-6); // FIXME; calculate rect properly
  201. if (is_pressed) {
  202. text_rect.X++;
  203. text_rect.Y++;
  204. }
  205. if (is_enabled) {
  206. SolidBrush b = new SolidBrush(this.ForeColor);
  207. this.DeviceContext.DrawString(text, this.Font, b, text_rect, text_format);
  208. b.Dispose();
  209. } else {
  210. ThemeEngine.Current.DrawStringDisabled(this.DeviceContext, text, this.Font, ThemeEngine.Current.ColorButtonText, text_rect, text_format);
  211. }
  212. }
  213. Refresh();
  214. }
  215. private void RedrawEvent(object sender, System.EventArgs e) {
  216. Redraw();
  217. }
  218. private void SizeEvent(object sender, System.EventArgs e) {
  219. this.CreateBuffers(this.client_size.Width, this.client_size.Height);
  220. Redraw();
  221. }
  222. #endregion // Private Properties and Methods
  223. #region Public Constructors
  224. protected ButtonBase() : base() {
  225. flat_style = FlatStyle.Standard;
  226. image_index = -1;
  227. image = null;
  228. image_list = null;
  229. image_alignment = ContentAlignment.MiddleCenter;
  230. text_alignment = ContentAlignment.MiddleCenter;
  231. ime_mode = ImeMode.Inherit;
  232. is_default = false;
  233. is_entered = false;
  234. is_pressed = false;
  235. has_focus = false;
  236. text_format = new StringFormat();
  237. text_format.Alignment = StringAlignment.Center;
  238. text_format.LineAlignment = StringAlignment.Center;
  239. SizeChanged+=new System.EventHandler(SizeEvent);
  240. TextChanged+=new System.EventHandler(RedrawEvent);
  241. ForeColorChanged+=new EventHandler(RedrawEvent);
  242. BackColorChanged+=new System.EventHandler(RedrawEvent);
  243. FontChanged+=new EventHandler(RedrawEvent);
  244. }
  245. #endregion // Public Constructors
  246. #region Public Instance Properties
  247. public FlatStyle FlatStyle {
  248. get {
  249. return flat_style;
  250. }
  251. set {
  252. flat_style = value;
  253. Redraw();
  254. }
  255. }
  256. public Image Image {
  257. get {
  258. return image;
  259. }
  260. set {
  261. image = value;
  262. Redraw();
  263. }
  264. }
  265. public ContentAlignment ImageAlign {
  266. get {
  267. return image_alignment;
  268. }
  269. set {
  270. image_alignment=value;
  271. Redraw();
  272. }
  273. }
  274. public int ImageIndex {
  275. get {
  276. if (image_list==null) {
  277. return -1;
  278. }
  279. return image_index;
  280. }
  281. set {
  282. image_index=value;
  283. Redraw();
  284. }
  285. }
  286. public ImageList ImageList {
  287. get {
  288. return image_list;
  289. }
  290. set {
  291. if (image_list != null) {
  292. image_list.Dispose();
  293. }
  294. image_list = value;
  295. if (value != null) {
  296. if (image != null) {
  297. image=null;
  298. }
  299. if (image_list.Images.Count >= image_index) {
  300. image_index=image_list.Images.Count-1;
  301. }
  302. }
  303. Redraw();
  304. }
  305. }
  306. public ImeMode ImeMode {
  307. get {
  308. return ime_mode;
  309. }
  310. set {
  311. ime_mode = value;
  312. }
  313. }
  314. public virtual ContentAlignment TextAlign {
  315. get {
  316. return text_alignment;
  317. }
  318. set {
  319. if (text_alignment != value) {
  320. text_alignment = value;
  321. switch(text_alignment) {
  322. case ContentAlignment.TopLeft: {
  323. text_format.Alignment=StringAlignment.Near;
  324. text_format.LineAlignment=StringAlignment.Near;
  325. break;
  326. }
  327. case ContentAlignment.TopCenter: {
  328. text_format.Alignment=StringAlignment.Center;
  329. text_format.LineAlignment=StringAlignment.Near;
  330. break;
  331. }
  332. case ContentAlignment.TopRight: {
  333. text_format.Alignment=StringAlignment.Far;
  334. text_format.LineAlignment=StringAlignment.Near;
  335. break;
  336. }
  337. case ContentAlignment.MiddleLeft: {
  338. text_format.Alignment=StringAlignment.Near;
  339. text_format.LineAlignment=StringAlignment.Center;
  340. break;
  341. }
  342. case ContentAlignment.MiddleCenter: {
  343. text_format.Alignment=StringAlignment.Center;
  344. text_format.LineAlignment=StringAlignment.Center;
  345. break;
  346. }
  347. case ContentAlignment.MiddleRight: {
  348. text_format.Alignment=StringAlignment.Far;
  349. text_format.LineAlignment=StringAlignment.Center;
  350. break;
  351. }
  352. case ContentAlignment.BottomLeft: {
  353. text_format.Alignment=StringAlignment.Near;
  354. text_format.LineAlignment=StringAlignment.Far;
  355. break;
  356. }
  357. case ContentAlignment.BottomCenter: {
  358. text_format.Alignment=StringAlignment.Center;
  359. text_format.LineAlignment=StringAlignment.Far;
  360. break;
  361. }
  362. case ContentAlignment.BottomRight: {
  363. text_format.Alignment=StringAlignment.Far;
  364. text_format.LineAlignment=StringAlignment.Far;
  365. break;
  366. }
  367. }
  368. Redraw();
  369. }
  370. }
  371. }
  372. #endregion // Public Instance Properties
  373. #region Protected Instance Properties
  374. protected override CreateParams CreateParams {
  375. get {
  376. CreateParams cp;
  377. cp=base.CreateParams;
  378. cp.Style=(int)WindowStyles.WS_VISIBLE | (int)WindowStyles.WS_CHILD;
  379. SetStyle(ControlStyles.UserPaint, true);
  380. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  381. return cp;
  382. }
  383. }
  384. protected override ImeMode DefaultImeMode {
  385. get {
  386. return ImeMode.Inherit;
  387. }
  388. }
  389. protected override Size DefaultSize {
  390. get {
  391. return new Size(75, 23);
  392. }
  393. }
  394. protected bool IsDefault {
  395. get {
  396. return is_default;
  397. }
  398. set {
  399. if (is_default != value) {
  400. is_default = true;
  401. Redraw();
  402. }
  403. }
  404. }
  405. #endregion // Public Instance Properties
  406. #region Protected Instance Methods
  407. [MonoTODO("Finish setting properties of the AccessibleObject")]
  408. protected override AccessibleObject CreateAccessibilityInstance() {
  409. AccessibleObject ao;
  410. ao=base.CreateAccessibilityInstance();
  411. ao.description="Button";
  412. return ao;
  413. }
  414. protected override void Dispose(bool Disposing) {
  415. base.Dispose(Disposing);
  416. }
  417. protected override void OnEnabledChanged(EventArgs e) {
  418. Redraw();
  419. base.OnEnabledChanged(e);
  420. }
  421. protected override void OnGotFocus(EventArgs e) {
  422. has_focus=true;
  423. Redraw();
  424. base.OnGotFocus(e);
  425. }
  426. protected override void OnKeyDown(KeyEventArgs kevent) {
  427. if (is_enabled && (kevent.KeyData == Keys.Enter || kevent.KeyData == Keys.Space)) {
  428. OnClick(EventArgs.Empty);
  429. kevent.Handled=true;
  430. }
  431. base.OnKeyDown(kevent);
  432. }
  433. protected override void OnKeyUp(KeyEventArgs kevent) {
  434. base.OnKeyUp(kevent);
  435. }
  436. protected override void OnLostFocus(EventArgs e) {
  437. has_focus=false;
  438. Redraw();
  439. base.OnLostFocus(e);
  440. }
  441. protected override void OnMouseDown(MouseEventArgs mevent) {
  442. if (is_enabled && (mevent.Button == MouseButtons.Left)) {
  443. is_pressed = true;
  444. this.Capture = true;
  445. Redraw();
  446. }
  447. base.OnMouseDown(mevent);
  448. }
  449. protected override void OnMouseEnter(EventArgs e) {
  450. is_entered=true;
  451. if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
  452. Redraw();
  453. }
  454. base.OnMouseEnter(e);
  455. }
  456. protected override void OnMouseLeave(EventArgs e) {
  457. is_entered=false;
  458. if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
  459. Redraw();
  460. }
  461. base.OnMouseLeave(e);
  462. }
  463. protected override void OnMouseMove(MouseEventArgs mevent) {
  464. bool inside = false;
  465. bool redraw = false;
  466. if (mevent.X>=0 && mevent.Y>=0 && mevent.X<this.client_size.Width && mevent.Y<=this.client_size.Height) {
  467. inside = true;
  468. }
  469. // If the button was pressed and we leave, release the button press and vice versa
  470. if (this.Capture && (inside != is_pressed)) {
  471. is_pressed = inside;
  472. redraw = true;
  473. }
  474. if (is_entered != inside) {
  475. is_entered = inside;
  476. redraw = true;
  477. }
  478. if (redraw) {
  479. Redraw();
  480. }
  481. base.OnMouseMove(mevent);
  482. }
  483. protected override void OnMouseUp(MouseEventArgs mevent) {
  484. if (this.Capture && mevent.Button == MouseButtons.Left) {
  485. this.Capture = false;
  486. if (is_pressed) {
  487. is_pressed = false;
  488. Redraw();
  489. }
  490. if (mevent.X>=0 && mevent.Y>=0 && mevent.X<this.client_size.Width && mevent.Y<=this.client_size.Height) {
  491. OnClick(EventArgs.Empty);
  492. }
  493. }
  494. base.OnMouseUp(mevent);
  495. }
  496. protected override void OnPaint(PaintEventArgs pevent) {
  497. pevent.Graphics.DrawImage(this.ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  498. base.OnPaint(pevent);
  499. }
  500. protected override void OnParentChanged(EventArgs e) {
  501. base.OnParentChanged(e);
  502. }
  503. protected override void OnTextChanged(EventArgs e) {
  504. Redraw();
  505. base.OnTextChanged(e);
  506. }
  507. protected override void OnVisibleChanged(EventArgs e) {
  508. if (!Visible) {
  509. is_pressed = false;
  510. has_focus = false;
  511. is_entered = false;
  512. }
  513. base.OnVisibleChanged(e);
  514. }
  515. protected void ResetFlagsandPaint() {
  516. // Nothing to do; MS internal
  517. }
  518. protected override void WndProc(ref Message m) {
  519. base.WndProc(ref m);
  520. }
  521. #endregion // Public Instance Properties
  522. }
  523. }