ButtonBase.cs 13 KB

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