ButtonBase.cs 12 KB

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