ButtonBase.cs 12 KB

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