ButtonBase.cs 12 KB

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