ButtonBase.cs 13 KB

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