ButtonBase.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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.3 2004/08/23 23:27:44 pbartok
  27. // - Finishing touches. Works now, just needs some optimizations.
  28. //
  29. // Revision 1.2 2004/08/21 21:57:41 pbartok
  30. // - Added loads of debug output for development
  31. // - Fixed typo in method name
  32. //
  33. // Revision 1.1 2004/08/15 21:31:10 pbartok
  34. // - First (mostly) working version
  35. //
  36. //
  37. //
  38. // NOT COMPLETE
  39. using System.ComponentModel;
  40. using System.Drawing;
  41. namespace System.Windows.Forms {
  42. [MonoTODO("Need to register for SizeChanged and force regen of button, need to add algorithm to determen when to redraw")]
  43. public abstract class ButtonBase : Control {
  44. #region Local Variables
  45. private FlatStyle flat_style;
  46. private int image_index;
  47. private Image image;
  48. private ImageList image_list;
  49. private ContentAlignment image_alignment;
  50. private ContentAlignment text_alignment;
  51. private ImeMode ime_mode;
  52. private bool is_default;
  53. private bool has_focus;
  54. private bool is_pressed;
  55. private bool is_entered;
  56. StringFormat text_format;
  57. #endregion // Local Variables
  58. #region Private Properties and Methods
  59. internal ButtonState ButtonState {
  60. get {
  61. ButtonState ret = ButtonState.Normal;
  62. if (Enabled) {
  63. // Popup style is only followed as long as the mouse isn't "in" the control
  64. if (is_entered) {
  65. if (flat_style == FlatStyle.Flat) {
  66. ret |= ButtonState.Flat;
  67. }
  68. } else {
  69. if (flat_style == FlatStyle.Flat || flat_style == FlatStyle.Popup) {
  70. ret |= ButtonState.Flat;
  71. }
  72. }
  73. if (is_entered && is_pressed) {
  74. ret |= ButtonState.Pushed;
  75. }
  76. } else {
  77. ret |= ButtonState.Inactive;
  78. if ((flat_style == FlatStyle.Flat) || (flat_style == FlatStyle.Popup)) {
  79. ret |= ButtonState.Flat;
  80. }
  81. }
  82. return ret;
  83. }
  84. }
  85. internal bool CheckRedraw() {
  86. // FIXME - check if something has actually changed
  87. Redraw();
  88. return true;
  89. }
  90. [MonoTODO("Make the FillRectangle use a global brush instead of creating one every time")]
  91. internal void Redraw() {
  92. ButtonState state;
  93. int width;
  94. int height;
  95. width = this.ClientSize.Width;
  96. height = this.ClientSize.Height;
  97. SolidBrush sb = new SolidBrush(ThemeEngine.Current.ColorButtonFace);
  98. this.DeviceContext.FillRectangle(sb, this.ClientRectangle);
  99. sb.Dispose();
  100. ThemeEngine.Current.DrawButton(this.DeviceContext, this.ClientRectangle, this.ButtonState);
  101. if (has_focus) {
  102. ThemeEngine.Current.DrawFocusRectangle(this.DeviceContext, this.ClientRectangle, ThemeEngine.Current.ColorButtonText, ThemeEngine.Current.ColorButtonFace);
  103. }
  104. // First, draw the image
  105. if ((image != null) || (image_list != null)) {
  106. // Need to draw a picture
  107. Image i;
  108. int image_x;
  109. int image_y;
  110. int image_width;
  111. int image_height;
  112. if (ImageIndex!=-1) { // We use ImageIndex instead of image_index since it will return -1 if image_list is null
  113. i = this.image_list.Images[image_index];
  114. } else {
  115. i = this.image;
  116. }
  117. image_width = image.Width;
  118. image_height = image.Height;
  119. switch(image_alignment) {
  120. case ContentAlignment.TopLeft: {
  121. image_x=0;
  122. image_y=0;
  123. break;
  124. }
  125. case ContentAlignment.TopCenter: {
  126. image_x=(width-image_width)/2;
  127. image_y=0;
  128. break;
  129. }
  130. case ContentAlignment.TopRight: {
  131. image_x=width-image_width;
  132. image_y=0;
  133. break;
  134. }
  135. case ContentAlignment.MiddleLeft: {
  136. image_x=0;
  137. image_y=(height-image_height)/2;
  138. break;
  139. }
  140. case ContentAlignment.MiddleCenter: {
  141. image_x=(width-image_width)/2;
  142. image_y=(height-image_height)/2;
  143. break;
  144. }
  145. case ContentAlignment.MiddleRight: {
  146. image_x=width-image_width;
  147. image_y=(height-image_height)/2;
  148. break;
  149. }
  150. case ContentAlignment.BottomLeft: {
  151. image_x=0;
  152. image_y=height-image_height;
  153. break;
  154. }
  155. case ContentAlignment.BottomCenter: {
  156. image_x=(width-image_width)/2;
  157. image_y=height-image_height;
  158. break;
  159. }
  160. case ContentAlignment.BottomRight: {
  161. image_x=width-image_width;
  162. image_y=height-image_height;
  163. break;
  164. }
  165. default: {
  166. image_x=0;
  167. image_y=0;
  168. break;
  169. }
  170. }
  171. if (is_pressed) {
  172. image_x+=2;
  173. image_y+=2;
  174. }
  175. if (is_enabled) {
  176. this.DeviceContext.DrawImage(i, image_x, image_y);
  177. } else {
  178. ThemeEngine.Current.DrawImageDisabled(this.DeviceContext, i, image_x, image_y, ThemeEngine.Current.ColorButtonFace);
  179. }
  180. }
  181. // Now the text
  182. if (text != null && text != String.Empty) {
  183. Rectangle text_rect = new Rectangle(3, 3, ClientSize.Width-6, ClientSize.Height-6); // FIXME; calculate rect properly
  184. if (is_pressed) {
  185. text_rect.X++;
  186. text_rect.Y++;
  187. }
  188. if (is_enabled) {
  189. SolidBrush b = new SolidBrush(ThemeEngine.Current.ColorButtonText);
  190. this.DeviceContext.DrawString(text, this.Font, b, text_rect, text_format);
  191. } else {
  192. ThemeEngine.Current.DrawStringDisabled(this.DeviceContext, text, this.Font, ThemeEngine.Current.ColorButtonText, text_rect, text_format);
  193. }
  194. }
  195. Refresh();
  196. }
  197. #endregion // Private Properties and Methods
  198. #region Public Constructors
  199. protected ButtonBase() : base() {
  200. flat_style = FlatStyle.Standard;
  201. image_index = -1;
  202. image = null;
  203. image_list = null;
  204. image_alignment = ContentAlignment.MiddleCenter;
  205. text_alignment = ContentAlignment.MiddleCenter;
  206. ime_mode = ImeMode.Inherit;
  207. is_default = false;
  208. is_entered = false;
  209. is_pressed = false;
  210. has_focus = false;
  211. text_format = new StringFormat();
  212. }
  213. #endregion // Public Constructors
  214. #region Public Instance Properties
  215. public FlatStyle FlatStyle {
  216. get {
  217. return flat_style;
  218. }
  219. set {
  220. flat_style = value;
  221. Redraw();
  222. }
  223. }
  224. public Image Image {
  225. get {
  226. return image;
  227. }
  228. set {
  229. image = value;
  230. Redraw();
  231. }
  232. }
  233. public ContentAlignment ImageAlign {
  234. get {
  235. return image_alignment;
  236. }
  237. set {
  238. image_alignment=value;
  239. Redraw();
  240. }
  241. }
  242. public int ImageIndex {
  243. get {
  244. if (image_list==null) {
  245. return -1;
  246. }
  247. return image_index;
  248. }
  249. set {
  250. image_index=value;
  251. Redraw();
  252. }
  253. }
  254. public ImageList ImageList {
  255. get {
  256. return image_list;
  257. }
  258. set {
  259. if (image_list != null) {
  260. image_list.Dispose();
  261. }
  262. image_list = value;
  263. if (value != null) {
  264. if (image != null) {
  265. image=null;
  266. }
  267. if (image_list.Images.Count >= image_index) {
  268. image_index=image_list.Images.Count-1;
  269. }
  270. }
  271. Redraw();
  272. }
  273. }
  274. public ImeMode ImeMode {
  275. get {
  276. return ime_mode;
  277. }
  278. set {
  279. ime_mode = value;
  280. }
  281. }
  282. public virtual ContentAlignment TextAlign {
  283. get {
  284. return text_alignment;
  285. }
  286. set {
  287. if (text_alignment != value) {
  288. switch(text_alignment) {
  289. case ContentAlignment.TopLeft: {
  290. text_format.Alignment=StringAlignment.Near;
  291. text_format.LineAlignment=StringAlignment.Near;
  292. break;
  293. }
  294. case ContentAlignment.TopCenter: {
  295. text_format.Alignment=StringAlignment.Center;
  296. text_format.LineAlignment=StringAlignment.Near;
  297. break;
  298. }
  299. case ContentAlignment.TopRight: {
  300. text_format.Alignment=StringAlignment.Far;
  301. text_format.LineAlignment=StringAlignment.Near;
  302. break;
  303. }
  304. case ContentAlignment.MiddleLeft: {
  305. text_format.Alignment=StringAlignment.Near;
  306. text_format.LineAlignment=StringAlignment.Center;
  307. break;
  308. }
  309. case ContentAlignment.MiddleCenter: {
  310. text_format.Alignment=StringAlignment.Center;
  311. text_format.LineAlignment=StringAlignment.Center;
  312. break;
  313. }
  314. case ContentAlignment.MiddleRight: {
  315. text_format.Alignment=StringAlignment.Far;
  316. text_format.LineAlignment=StringAlignment.Center;
  317. break;
  318. }
  319. case ContentAlignment.BottomLeft: {
  320. text_format.Alignment=StringAlignment.Near;
  321. text_format.LineAlignment=StringAlignment.Far;
  322. break;
  323. }
  324. case ContentAlignment.BottomCenter: {
  325. text_format.Alignment=StringAlignment.Center;
  326. text_format.LineAlignment=StringAlignment.Far;
  327. break;
  328. }
  329. case ContentAlignment.BottomRight: {
  330. text_format.Alignment=StringAlignment.Far;
  331. text_format.LineAlignment=StringAlignment.Far;
  332. break;
  333. }
  334. }
  335. Redraw();
  336. }
  337. }
  338. }
  339. #endregion // Public Instance Properties
  340. #region Protected Instance Properties
  341. protected override CreateParams CreateParams {
  342. get {
  343. CreateParams cp;
  344. cp=base.CreateParams;
  345. cp.Style=(int)WindowStyles.WS_VISIBLE | (int)WindowStyles.WS_CHILD;
  346. return cp;
  347. }
  348. }
  349. protected override ImeMode DefaultImeMode {
  350. get {
  351. return ImeMode.Inherit;
  352. }
  353. }
  354. protected override Size DefaultSize {
  355. get {
  356. return new Size(75, 23);
  357. }
  358. }
  359. protected bool IsDefault {
  360. get {
  361. return is_default;
  362. }
  363. set {
  364. if (is_default != value) {
  365. is_default = true;
  366. Redraw();
  367. }
  368. }
  369. }
  370. #endregion // Public Instance Properties
  371. #region Protected Instance Methods
  372. [MonoTODO("Finish setting properties of the AccessibleObject")]
  373. protected override AccessibleObject CreateAccessibilityInstance() {
  374. AccessibleObject ao;
  375. ao=base.CreateAccessibilityInstance();
  376. ao.description="Button";
  377. return ao;
  378. }
  379. protected override void Dispose(bool Disposing) {
  380. base.Dispose(Disposing);
  381. }
  382. protected override void OnEnabledChanged(EventArgs e) {
  383. CheckRedraw();
  384. base.OnEnabledChanged(e);
  385. }
  386. protected override void OnGotFocus(EventArgs e) {
  387. has_focus=true;
  388. CheckRedraw();
  389. base.OnGotFocus(e);
  390. }
  391. protected override void OnKeyDown(KeyEventArgs kevent) {
  392. if (is_enabled && (kevent.KeyData == Keys.Enter || kevent.KeyData == Keys.Space)) {
  393. OnClick(EventArgs.Empty);
  394. kevent.Handled=true;
  395. }
  396. base.OnKeyDown(kevent);
  397. }
  398. protected override void OnKeyUp(KeyEventArgs kevent) {
  399. base.OnKeyUp(kevent);
  400. }
  401. protected override void OnLostFocus(EventArgs e) {
  402. has_focus=false;
  403. CheckRedraw();
  404. base.OnLostFocus(e);
  405. }
  406. protected override void OnMouseDown(MouseEventArgs mevent) {
  407. if (is_enabled && (mevent.Button == MouseButtons.Left)) {
  408. is_pressed = true;
  409. CheckRedraw();
  410. }
  411. base.OnMouseDown(mevent);
  412. }
  413. protected override void OnMouseEnter(EventArgs e) {
  414. is_entered=true;
  415. CheckRedraw();
  416. base.OnMouseEnter(e);
  417. }
  418. protected override void OnMouseLeave(EventArgs e) {
  419. is_entered=false;
  420. CheckRedraw();
  421. base.OnMouseLeave(e);
  422. }
  423. protected override void OnMouseMove(MouseEventArgs mevent) {
  424. base.OnMouseMove(mevent);
  425. }
  426. protected override void OnMouseUp(MouseEventArgs mevent) {
  427. if (is_pressed && mevent.Button == MouseButtons.Left) {
  428. is_pressed = false;
  429. CheckRedraw();
  430. OnClick(EventArgs.Empty);
  431. }
  432. base.OnMouseUp(mevent);
  433. }
  434. protected override void OnPaint(PaintEventArgs pevent) {
  435. pevent.Graphics.DrawImage(this.ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  436. base.OnPaint(pevent);
  437. }
  438. protected override void OnParentChanged(EventArgs e) {
  439. base.OnParentChanged(e);
  440. }
  441. protected override void OnTextChanged(EventArgs e) {
  442. Redraw();
  443. base.OnTextChanged(e);
  444. }
  445. protected override void OnVisibleChanged(EventArgs e) {
  446. if (!Visible) {
  447. is_pressed = false;
  448. has_focus = false;
  449. is_entered = false;
  450. }
  451. base.OnVisibleChanged(e);
  452. }
  453. protected void ResetFlagsandPaint() {
  454. // Nothing to do; MS internal
  455. }
  456. protected override void WndProc(ref Message m) {
  457. base.WndProc(ref m);
  458. }
  459. #endregion // Public Instance Properties
  460. }
  461. }