ButtonBase.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. using System.Drawing.Text;
  98. namespace System.Windows.Forms {
  99. public abstract class ButtonBase : Control {
  100. #region Local Variables
  101. internal FlatStyle flat_style;
  102. internal int image_index;
  103. internal Image image;
  104. internal ImageList image_list;
  105. internal ContentAlignment image_alignment;
  106. internal ContentAlignment text_alignment;
  107. private ImeMode ime_mode;
  108. private bool is_default;
  109. internal bool has_focus;
  110. internal bool is_pressed;
  111. internal bool is_entered;
  112. internal bool redraw;
  113. internal StringFormat text_format;
  114. #endregion // Local Variables
  115. #region Private Properties and Methods
  116. internal ButtonState ButtonState {
  117. get {
  118. ButtonState ret = ButtonState.Normal;
  119. if (Enabled) {
  120. // Popup style is only followed as long as the mouse isn't "in" the control
  121. if (is_entered) {
  122. if (flat_style == FlatStyle.Flat) {
  123. ret |= ButtonState.Flat;
  124. }
  125. } else {
  126. if (flat_style == FlatStyle.Flat || flat_style == FlatStyle.Popup) {
  127. ret |= ButtonState.Flat;
  128. }
  129. }
  130. if (is_entered && is_pressed) {
  131. ret |= ButtonState.Pushed;
  132. }
  133. } else {
  134. ret |= ButtonState.Inactive;
  135. if ((flat_style == FlatStyle.Flat) || (flat_style == FlatStyle.Popup)) {
  136. ret |= ButtonState.Flat;
  137. }
  138. }
  139. return ret;
  140. }
  141. }
  142. [MonoTODO("Make the FillRectangle use a global brush instead of creating one every time")]
  143. internal void Redraw() {
  144. redraw = true;
  145. Refresh ();
  146. }
  147. // Derived classes should override Draw method and we dont want
  148. // to break the control signature, hence this approach.
  149. internal virtual void Draw (PaintEventArgs pevent) {
  150. if (redraw) {
  151. ThemeEngine.Current.DrawButtonBase(this.DeviceContext, pevent.ClipRectangle, this);
  152. redraw = false;
  153. }
  154. }
  155. private void RedrawEvent(object sender, System.EventArgs e) {
  156. Redraw();
  157. }
  158. #endregion // Private Properties and Methods
  159. #region Public Constructors
  160. protected ButtonBase() : base() {
  161. flat_style = FlatStyle.Standard;
  162. image_index = -1;
  163. image = null;
  164. image_list = null;
  165. image_alignment = ContentAlignment.MiddleCenter;
  166. text_alignment = ContentAlignment.MiddleCenter;
  167. ime_mode = ImeMode.Inherit;
  168. is_default = false;
  169. is_entered = false;
  170. is_pressed = false;
  171. has_focus = false;
  172. redraw = true;
  173. text_format = new StringFormat();
  174. text_format.Alignment = StringAlignment.Center;
  175. text_format.LineAlignment = StringAlignment.Center;
  176. text_format.HotkeyPrefix = HotkeyPrefix.Show;
  177. TextChanged+=new System.EventHandler(RedrawEvent);
  178. ForeColorChanged+=new EventHandler(RedrawEvent);
  179. BackColorChanged+=new System.EventHandler(RedrawEvent);
  180. FontChanged+=new EventHandler(RedrawEvent);
  181. SizeChanged+=new EventHandler(RedrawEvent);
  182. SetStyle (ControlStyles.ResizeRedraw, true);
  183. }
  184. #endregion // Public Constructors
  185. #region Public Instance Properties
  186. public FlatStyle FlatStyle {
  187. get {
  188. return flat_style;
  189. }
  190. set {
  191. flat_style = value;
  192. Redraw();
  193. }
  194. }
  195. public Image Image {
  196. get {
  197. return image;
  198. }
  199. set {
  200. image = value;
  201. Redraw();
  202. }
  203. }
  204. public ContentAlignment ImageAlign {
  205. get {
  206. return image_alignment;
  207. }
  208. set {
  209. image_alignment=value;
  210. Redraw();
  211. }
  212. }
  213. public int ImageIndex {
  214. get {
  215. if (image_list==null) {
  216. return -1;
  217. }
  218. return image_index;
  219. }
  220. set {
  221. image_index=value;
  222. Redraw();
  223. }
  224. }
  225. public ImageList ImageList {
  226. get {
  227. return image_list;
  228. }
  229. set {
  230. if (image_list != null) {
  231. image_list.Dispose();
  232. }
  233. image_list = value;
  234. if (value != null) {
  235. if (image != null) {
  236. image=null;
  237. }
  238. if (image_list.Images.Count >= image_index) {
  239. image_index=image_list.Images.Count-1;
  240. }
  241. }
  242. Redraw();
  243. }
  244. }
  245. public ImeMode ImeMode {
  246. get {
  247. return ime_mode;
  248. }
  249. set {
  250. ime_mode = value;
  251. }
  252. }
  253. public virtual ContentAlignment TextAlign {
  254. get {
  255. return text_alignment;
  256. }
  257. set {
  258. if (text_alignment != value) {
  259. text_alignment = value;
  260. switch(text_alignment) {
  261. case ContentAlignment.TopLeft: {
  262. text_format.Alignment=StringAlignment.Near;
  263. text_format.LineAlignment=StringAlignment.Near;
  264. break;
  265. }
  266. case ContentAlignment.TopCenter: {
  267. text_format.Alignment=StringAlignment.Center;
  268. text_format.LineAlignment=StringAlignment.Near;
  269. break;
  270. }
  271. case ContentAlignment.TopRight: {
  272. text_format.Alignment=StringAlignment.Far;
  273. text_format.LineAlignment=StringAlignment.Near;
  274. break;
  275. }
  276. case ContentAlignment.MiddleLeft: {
  277. text_format.Alignment=StringAlignment.Near;
  278. text_format.LineAlignment=StringAlignment.Center;
  279. break;
  280. }
  281. case ContentAlignment.MiddleCenter: {
  282. text_format.Alignment=StringAlignment.Center;
  283. text_format.LineAlignment=StringAlignment.Center;
  284. break;
  285. }
  286. case ContentAlignment.MiddleRight: {
  287. text_format.Alignment=StringAlignment.Far;
  288. text_format.LineAlignment=StringAlignment.Center;
  289. break;
  290. }
  291. case ContentAlignment.BottomLeft: {
  292. text_format.Alignment=StringAlignment.Near;
  293. text_format.LineAlignment=StringAlignment.Far;
  294. break;
  295. }
  296. case ContentAlignment.BottomCenter: {
  297. text_format.Alignment=StringAlignment.Center;
  298. text_format.LineAlignment=StringAlignment.Far;
  299. break;
  300. }
  301. case ContentAlignment.BottomRight: {
  302. text_format.Alignment=StringAlignment.Far;
  303. text_format.LineAlignment=StringAlignment.Far;
  304. break;
  305. }
  306. }
  307. Redraw();
  308. }
  309. }
  310. }
  311. #endregion // Public Instance Properties
  312. #region Protected Instance Properties
  313. protected override CreateParams CreateParams {
  314. get {
  315. CreateParams cp;
  316. cp=base.CreateParams;
  317. cp.Style=(int)WindowStyles.WS_VISIBLE | (int)WindowStyles.WS_CHILD;
  318. SetStyle(ControlStyles.UserPaint, true);
  319. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  320. return cp;
  321. }
  322. }
  323. protected override ImeMode DefaultImeMode {
  324. get {
  325. return ImeMode.Inherit;
  326. }
  327. }
  328. protected override Size DefaultSize {
  329. get {
  330. return ThemeEngine.Current.ButtonBaseDefaultSize;
  331. }
  332. }
  333. protected bool IsDefault {
  334. get {
  335. return is_default;
  336. }
  337. set {
  338. if (is_default != value) {
  339. is_default = true;
  340. Redraw();
  341. }
  342. }
  343. }
  344. #endregion // Public Instance Properties
  345. #region Protected Instance Methods
  346. [MonoTODO("Finish setting properties of the AccessibleObject")]
  347. protected override AccessibleObject CreateAccessibilityInstance() {
  348. AccessibleObject ao;
  349. ao=base.CreateAccessibilityInstance();
  350. ao.description="Button";
  351. return ao;
  352. }
  353. protected override void Dispose(bool Disposing) {
  354. base.Dispose(Disposing);
  355. }
  356. protected override void OnEnabledChanged(EventArgs e) {
  357. Redraw();
  358. base.OnEnabledChanged(e);
  359. }
  360. protected override void OnGotFocus(EventArgs e) {
  361. has_focus=true;
  362. Redraw();
  363. base.OnGotFocus(e);
  364. }
  365. protected override void OnKeyDown(KeyEventArgs kevent) {
  366. if (is_enabled && (kevent.KeyData == Keys.Enter || kevent.KeyData == Keys.Space)) {
  367. OnClick(EventArgs.Empty);
  368. kevent.Handled=true;
  369. }
  370. base.OnKeyDown(kevent);
  371. }
  372. protected override void OnKeyUp(KeyEventArgs kevent) {
  373. base.OnKeyUp(kevent);
  374. }
  375. protected override void OnLostFocus(EventArgs e) {
  376. has_focus=false;
  377. Redraw();
  378. base.OnLostFocus(e);
  379. }
  380. protected override void OnMouseDown(MouseEventArgs mevent) {
  381. if (is_enabled && (mevent.Button == MouseButtons.Left)) {
  382. is_pressed = true;
  383. this.Capture = true;
  384. Redraw();
  385. }
  386. base.OnMouseDown(mevent);
  387. }
  388. protected override void OnMouseEnter(EventArgs e) {
  389. is_entered=true;
  390. if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
  391. Redraw();
  392. }
  393. base.OnMouseEnter(e);
  394. }
  395. protected override void OnMouseLeave(EventArgs e) {
  396. is_entered=false;
  397. if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
  398. Redraw();
  399. }
  400. base.OnMouseLeave(e);
  401. }
  402. protected override void OnMouseMove(MouseEventArgs mevent) {
  403. bool inside = false;
  404. bool redraw = false;
  405. if (mevent.X>=0 && mevent.Y>=0 && mevent.X<this.client_size.Width && mevent.Y<=this.client_size.Height) {
  406. inside = true;
  407. }
  408. // If the button was pressed and we leave, release the button press and vice versa
  409. if (this.Capture && (inside != is_pressed)) {
  410. is_pressed = inside;
  411. redraw = true;
  412. }
  413. if (is_entered != inside) {
  414. is_entered = inside;
  415. redraw = true;
  416. }
  417. if (redraw) {
  418. Redraw();
  419. }
  420. base.OnMouseMove(mevent);
  421. }
  422. protected override void OnMouseUp(MouseEventArgs mevent) {
  423. if (this.Capture && mevent.Button == MouseButtons.Left) {
  424. this.Capture = false;
  425. if (is_pressed) {
  426. is_pressed = false;
  427. Redraw();
  428. } else if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
  429. Redraw();
  430. }
  431. if (mevent.X>=0 && mevent.Y>=0 && mevent.X<this.client_size.Width && mevent.Y<=this.client_size.Height) {
  432. OnClick(EventArgs.Empty);
  433. }
  434. }
  435. base.OnMouseUp(mevent);
  436. }
  437. protected override void OnPaint(PaintEventArgs pevent) {
  438. Draw (pevent);
  439. pevent.Graphics.DrawImage(this.ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  440. base.OnPaint (pevent);
  441. }
  442. protected override void OnParentChanged(EventArgs e) {
  443. base.OnParentChanged(e);
  444. }
  445. protected override void OnTextChanged(EventArgs e) {
  446. Redraw();
  447. base.OnTextChanged(e);
  448. }
  449. protected override void OnVisibleChanged(EventArgs e) {
  450. if (!Visible) {
  451. is_pressed = false;
  452. has_focus = false;
  453. is_entered = false;
  454. }
  455. base.OnVisibleChanged(e);
  456. }
  457. protected void ResetFlagsandPaint() {
  458. // Nothing to do; MS internal
  459. // Should we do Redraw (); ?
  460. }
  461. protected override void WndProc(ref Message m) {
  462. base.WndProc(ref m);
  463. }
  464. #endregion // Public Instance Properties
  465. }
  466. }