ButtonBase.cs 13 KB

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