ButtonBase.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. // COMPLETE
  26. using System.ComponentModel;
  27. using System.ComponentModel.Design;
  28. using System.Drawing;
  29. using System.Drawing.Text;
  30. using System.Runtime.InteropServices;
  31. namespace System.Windows.Forms {
  32. public abstract class ButtonBase : Control {
  33. #region Local Variables
  34. internal FlatStyle flat_style;
  35. internal int image_index;
  36. internal Image image;
  37. internal ImageList image_list;
  38. internal ContentAlignment image_alignment;
  39. internal ContentAlignment text_alignment;
  40. private bool is_default;
  41. internal bool is_pressed;
  42. internal bool enter_state;
  43. internal StringFormat text_format;
  44. #endregion // Local Variables
  45. #region ButtonBaseAccessibleObject sub-class
  46. [ComVisible(true)]
  47. public class ButtonBaseAccessibleObject : ControlAccessibleObject {
  48. #region ButtonBaseAccessibleObject Local Variables
  49. private Control owner;
  50. #endregion // ButtonBaseAccessibleObject Local Variables
  51. #region ButtonBaseAccessibleObject Constructors
  52. public ButtonBaseAccessibleObject(Control owner) : base(owner) {
  53. this.owner = owner;
  54. }
  55. #endregion // ButtonBaseAccessibleObject Constructors
  56. #region ButtonBaseAccessibleObject Methods
  57. public override void DoDefaultAction() {
  58. ((ButtonBase)owner).PerformClick();
  59. }
  60. #endregion // ButtonBaseAccessibleObject Methods
  61. }
  62. #endregion // ButtonBaseAccessibleObject sub-class
  63. #region Private Properties and Methods
  64. internal ButtonState ButtonState {
  65. get {
  66. ButtonState ret = ButtonState.Normal;
  67. if (Enabled) {
  68. // Popup style is only followed as long as the mouse isn't "in" the control
  69. if (is_entered) {
  70. if (flat_style == FlatStyle.Flat) {
  71. ret |= ButtonState.Flat;
  72. }
  73. } else {
  74. if (flat_style == FlatStyle.Flat || flat_style == FlatStyle.Popup) {
  75. ret |= ButtonState.Flat;
  76. }
  77. }
  78. if (is_entered && is_pressed) {
  79. ret |= ButtonState.Pushed;
  80. }
  81. } else {
  82. ret |= ButtonState.Inactive;
  83. if ((flat_style == FlatStyle.Flat) || (flat_style == FlatStyle.Popup)) {
  84. ret |= ButtonState.Flat;
  85. }
  86. }
  87. return ret;
  88. }
  89. }
  90. [MonoTODO("Make the FillRectangle use a global brush instead of creating one every time")]
  91. internal void Redraw() {
  92. Refresh ();
  93. }
  94. // Derived classes should override Draw method and we dont want
  95. // to break the control signature, hence this approach.
  96. internal virtual void Draw (PaintEventArgs pevent) {
  97. ThemeEngine.Current.DrawButtonBase (pevent.Graphics, pevent.ClipRectangle, this);
  98. }
  99. internal virtual void HaveDoubleClick() {
  100. // override me
  101. }
  102. private void RedrawEvent(object sender, System.EventArgs e) {
  103. Redraw();
  104. }
  105. #endregion // Private Properties and Methods
  106. #region Public Constructors
  107. protected ButtonBase() : base() {
  108. flat_style = FlatStyle.Standard;
  109. image_index = -1;
  110. image = null;
  111. image_list = null;
  112. image_alignment = ContentAlignment.MiddleCenter;
  113. text_alignment = ContentAlignment.MiddleCenter;
  114. ime_mode = ImeMode.Inherit;
  115. is_default = false;
  116. is_entered = false;
  117. is_pressed = false;
  118. has_focus = false;
  119. text_format = new StringFormat();
  120. text_format.Alignment = StringAlignment.Center;
  121. text_format.LineAlignment = StringAlignment.Center;
  122. text_format.HotkeyPrefix = HotkeyPrefix.Show;
  123. TextChanged+=new System.EventHandler(RedrawEvent);
  124. ForeColorChanged+=new EventHandler(RedrawEvent);
  125. BackColorChanged+=new System.EventHandler(RedrawEvent);
  126. FontChanged+=new EventHandler(RedrawEvent);
  127. SizeChanged+=new EventHandler(RedrawEvent);
  128. SetStyle(ControlStyles.ResizeRedraw |
  129. ControlStyles.Opaque |
  130. ControlStyles.UserMouse |
  131. ControlStyles.SupportsTransparentBackColor |
  132. ControlStyles.CacheText |
  133. ControlStyles.DoubleBuffer, true);
  134. SetStyle(ControlStyles.StandardClick, false);
  135. }
  136. #endregion // Public Constructors
  137. #region Public Instance Properties
  138. [Localizable(true)]
  139. [DefaultValue(FlatStyle.Standard)]
  140. [MWFDescription("Determines look of button"), MWFCategory("Appearance")]
  141. public FlatStyle FlatStyle {
  142. get {
  143. return flat_style;
  144. }
  145. set {
  146. flat_style = value;
  147. Redraw();
  148. }
  149. }
  150. [Localizable(true)]
  151. [MWFDescription("Sets image to be displayed on button face"), MWFCategory("Appearance")]
  152. public Image Image {
  153. get {
  154. return image;
  155. }
  156. set {
  157. image = value;
  158. Redraw();
  159. }
  160. }
  161. [Localizable(true)]
  162. [DefaultValue(ContentAlignment.MiddleCenter)]
  163. [MWFDescription("Sets the alignment of the image to be displayed on button face"), MWFCategory("Appearance")]
  164. public ContentAlignment ImageAlign {
  165. get {
  166. return image_alignment;
  167. }
  168. set {
  169. image_alignment=value;
  170. Redraw();
  171. }
  172. }
  173. [Localizable(true)]
  174. [DefaultValue(-1)]
  175. [Editor("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design, typeof(System.Drawing.Design.UITypeEditor))]
  176. [TypeConverter(typeof(ImageIndexConverter))]
  177. [MWFDescription("Index of image to display, if ImageList is used for button face images"), MWFCategory("Appearance")]
  178. public int ImageIndex {
  179. get {
  180. if (image_list==null) {
  181. return -1;
  182. }
  183. return image_index;
  184. }
  185. set {
  186. image_index=value;
  187. Redraw();
  188. }
  189. }
  190. [DefaultValue(null)]
  191. [MWFDescription("ImageList used for ImageIndex"), MWFCategory("Appearance")]
  192. public ImageList ImageList {
  193. get {
  194. return image_list;
  195. }
  196. set {
  197. if (image_list != null) {
  198. image_list.Dispose();
  199. }
  200. image_list = value;
  201. if (value != null) {
  202. if (image != null) {
  203. image=null;
  204. }
  205. if (image_list.Images.Count >= image_index) {
  206. image_index=image_list.Images.Count-1;
  207. }
  208. }
  209. Redraw();
  210. }
  211. }
  212. [Browsable(false)]
  213. [EditorBrowsable (EditorBrowsableState.Never)]
  214. public new ImeMode ImeMode {
  215. get {
  216. return ime_mode;
  217. }
  218. set {
  219. if (ime_mode != value) {
  220. ime_mode = value;
  221. if (ImeModeChanged != null) {
  222. ImeModeChanged(this, EventArgs.Empty);
  223. }
  224. }
  225. }
  226. }
  227. [Localizable(true)]
  228. [DefaultValue(ContentAlignment.MiddleCenter)]
  229. [MWFDescription("Alignment for button text"), MWFCategory("Appearance")]
  230. public virtual ContentAlignment TextAlign {
  231. get {
  232. return text_alignment;
  233. }
  234. set {
  235. if (text_alignment != value) {
  236. text_alignment = value;
  237. switch(text_alignment) {
  238. case ContentAlignment.TopLeft: {
  239. text_format.Alignment=StringAlignment.Near;
  240. text_format.LineAlignment=StringAlignment.Near;
  241. break;
  242. }
  243. case ContentAlignment.TopCenter: {
  244. text_format.Alignment=StringAlignment.Center;
  245. text_format.LineAlignment=StringAlignment.Near;
  246. break;
  247. }
  248. case ContentAlignment.TopRight: {
  249. text_format.Alignment=StringAlignment.Far;
  250. text_format.LineAlignment=StringAlignment.Near;
  251. break;
  252. }
  253. case ContentAlignment.MiddleLeft: {
  254. text_format.Alignment=StringAlignment.Near;
  255. text_format.LineAlignment=StringAlignment.Center;
  256. break;
  257. }
  258. case ContentAlignment.MiddleCenter: {
  259. text_format.Alignment=StringAlignment.Center;
  260. text_format.LineAlignment=StringAlignment.Center;
  261. break;
  262. }
  263. case ContentAlignment.MiddleRight: {
  264. text_format.Alignment=StringAlignment.Far;
  265. text_format.LineAlignment=StringAlignment.Center;
  266. break;
  267. }
  268. case ContentAlignment.BottomLeft: {
  269. text_format.Alignment=StringAlignment.Near;
  270. text_format.LineAlignment=StringAlignment.Far;
  271. break;
  272. }
  273. case ContentAlignment.BottomCenter: {
  274. text_format.Alignment=StringAlignment.Center;
  275. text_format.LineAlignment=StringAlignment.Far;
  276. break;
  277. }
  278. case ContentAlignment.BottomRight: {
  279. text_format.Alignment=StringAlignment.Far;
  280. text_format.LineAlignment=StringAlignment.Far;
  281. break;
  282. }
  283. }
  284. Redraw();
  285. }
  286. }
  287. }
  288. #endregion // Public Instance Properties
  289. #region Protected Instance Properties
  290. protected override CreateParams CreateParams {
  291. get {
  292. CreateParams cp;
  293. cp=base.CreateParams;
  294. cp.Style=(int)(WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN);
  295. return cp;
  296. }
  297. }
  298. protected override ImeMode DefaultImeMode {
  299. get {
  300. return ImeMode.Inherit;
  301. }
  302. }
  303. protected override Size DefaultSize {
  304. get {
  305. return ThemeEngine.Current.ButtonBaseDefaultSize;
  306. }
  307. }
  308. protected bool IsDefault {
  309. get {
  310. return is_default;
  311. }
  312. set {
  313. if (is_default != value) {
  314. is_default = true;
  315. Redraw();
  316. }
  317. }
  318. }
  319. #endregion // Public Instance Properties
  320. #region Protected Instance Methods
  321. protected override AccessibleObject CreateAccessibilityInstance() {
  322. AccessibleObject ao;
  323. ao = base.CreateAccessibilityInstance();
  324. ao.description = "Button";
  325. ao.role = AccessibleRole.PushButton;
  326. return ao;
  327. }
  328. protected override void Dispose(bool Disposing) {
  329. base.Dispose(Disposing);
  330. }
  331. protected override void OnEnabledChanged(EventArgs e) {
  332. Redraw();
  333. base.OnEnabledChanged(e);
  334. }
  335. protected override void OnGotFocus(EventArgs e) {
  336. has_focus=true;
  337. Redraw();
  338. base.OnGotFocus(e);
  339. }
  340. protected override void OnKeyDown(KeyEventArgs kevent) {
  341. if (is_enabled && (kevent.KeyData == Keys.Space)) {
  342. enter_state = is_entered;
  343. is_entered = true;
  344. OnMouseDown(new MouseEventArgs(MouseButtons.Left, 1, 2, 2, 0));
  345. kevent.Handled=true;
  346. }
  347. base.OnKeyDown(kevent);
  348. }
  349. protected override void OnKeyUp(KeyEventArgs kevent) {
  350. if (is_enabled && (kevent.KeyData == Keys.Space)) {
  351. OnMouseUp(new MouseEventArgs(MouseButtons.Left, 1, 2, 2, 0));
  352. is_entered = enter_state;
  353. kevent.Handled=true;
  354. }
  355. base.OnKeyUp(kevent);
  356. }
  357. protected override void OnLostFocus(EventArgs e) {
  358. has_focus=false;
  359. Redraw();
  360. base.OnLostFocus(e);
  361. }
  362. protected override void OnMouseDown(MouseEventArgs mevent) {
  363. if (is_enabled && (mevent.Button == MouseButtons.Left)) {
  364. is_pressed = true;
  365. this.Capture = true;
  366. Redraw();
  367. }
  368. base.OnMouseDown(mevent);
  369. }
  370. protected override void OnMouseEnter(EventArgs e) {
  371. is_entered=true;
  372. if ( is_enabled )
  373. Redraw();
  374. base.OnMouseEnter(e);
  375. }
  376. protected override void OnMouseLeave(EventArgs e) {
  377. is_entered=false;
  378. if ( is_enabled )
  379. Redraw();
  380. base.OnMouseLeave(e);
  381. }
  382. protected override void OnMouseMove(MouseEventArgs mevent) {
  383. bool inside = false;
  384. bool redraw = false;
  385. if (mevent.X>=0 && mevent.Y>=0 && mevent.X<this.client_size.Width && mevent.Y<=this.client_size.Height) {
  386. inside = true;
  387. }
  388. // If the button was pressed and we leave, release the button press and vice versa
  389. if (this.Capture && (inside != is_pressed)) {
  390. is_pressed = inside;
  391. redraw = true;
  392. }
  393. if (is_entered != inside) {
  394. is_entered = inside;
  395. redraw = true;
  396. }
  397. if (redraw) {
  398. Redraw();
  399. }
  400. base.OnMouseMove(mevent);
  401. }
  402. protected override void OnMouseUp(MouseEventArgs mevent) {
  403. if (this.Capture && mevent.Button == MouseButtons.Left) {
  404. this.Capture = false;
  405. if (is_pressed) {
  406. is_pressed = false;
  407. Redraw();
  408. } else if ((this.flat_style == FlatStyle.Flat) || (this.flat_style == FlatStyle.Popup)) {
  409. Redraw();
  410. }
  411. if (mevent.X>=0 && mevent.Y>=0 && mevent.X<this.client_size.Width && mevent.Y<=this.client_size.Height) {
  412. OnClick(EventArgs.Empty);
  413. }
  414. }
  415. base.OnMouseUp(mevent);
  416. }
  417. protected override void OnPaint(PaintEventArgs pevent) {
  418. Draw (pevent);
  419. base.OnPaint (pevent);
  420. }
  421. protected override void OnParentChanged(EventArgs e) {
  422. base.OnParentChanged(e);
  423. }
  424. protected override void OnTextChanged(EventArgs e) {
  425. Redraw();
  426. base.OnTextChanged(e);
  427. }
  428. protected override void OnVisibleChanged(EventArgs e) {
  429. if (!Visible) {
  430. is_pressed = false;
  431. has_focus = false;
  432. is_entered = false;
  433. }
  434. base.OnVisibleChanged(e);
  435. }
  436. protected void ResetFlagsandPaint() {
  437. // Nothing to do; MS internal
  438. // Should we do Redraw (); ?
  439. }
  440. protected override void WndProc(ref Message m) {
  441. switch((Msg)m.Msg) {
  442. case Msg.WM_LBUTTONDBLCLK: {
  443. HaveDoubleClick();
  444. break;
  445. }
  446. case Msg.WM_MBUTTONDBLCLK: {
  447. HaveDoubleClick();
  448. break;
  449. }
  450. case Msg.WM_RBUTTONDBLCLK: {
  451. HaveDoubleClick();
  452. break;
  453. }
  454. }
  455. base.WndProc (ref m);
  456. }
  457. #endregion // Public Instance Properties
  458. #region Internal Methods
  459. private void PerformClick() {
  460. OnClick(EventArgs.Empty);
  461. }
  462. #endregion // Internal Methods
  463. #region Events
  464. [Browsable(false)]
  465. [EditorBrowsable (EditorBrowsableState.Never)]
  466. public new event EventHandler ImeModeChanged;
  467. #endregion // Events
  468. }
  469. }