CheckBox.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // Dennis Hayes [email protected]
  24. // Peter Bartok [email protected]
  25. //
  26. // COMPLETE
  27. using System;
  28. using System.ComponentModel;
  29. using System.Drawing;
  30. using System.Runtime.InteropServices;
  31. namespace System.Windows.Forms {
  32. [DefaultProperty("Checked")]
  33. [DefaultEvent("CheckedChanged")]
  34. public class CheckBox : ButtonBase {
  35. #region Local Variables
  36. internal Appearance appearance;
  37. internal bool auto_check;
  38. internal ContentAlignment check_alignment;
  39. internal CheckState check_state;
  40. internal bool three_state;
  41. #endregion // Local Variables
  42. #region CheckBoxAccessibleObject Subclass
  43. [ComVisible(true)]
  44. public class CheckBoxAccessibleObject : ButtonBaseAccessibleObject {
  45. #region CheckBoxAccessibleObject Local Variables
  46. private CheckBox owner;
  47. #endregion // CheckBoxAccessibleObject Local Variables
  48. #region CheckBoxAccessibleObject Constructors
  49. public CheckBoxAccessibleObject(Control owner) : base(owner) {
  50. this.owner = (CheckBox)owner;
  51. }
  52. #endregion // CheckBoxAccessibleObject Constructors
  53. #region CheckBoxAccessibleObject Properties
  54. public override string DefaultAction {
  55. get {
  56. return "Select";
  57. }
  58. }
  59. public override AccessibleRole Role {
  60. get {
  61. return AccessibleRole.CheckButton;
  62. }
  63. }
  64. public override AccessibleStates State {
  65. get {
  66. AccessibleStates retval;
  67. retval = AccessibleStates.Default;
  68. if (owner.check_state == CheckState.Checked) {
  69. retval |= AccessibleStates.Checked;
  70. }
  71. if (owner.Focused) {
  72. retval |= AccessibleStates.Focused;
  73. }
  74. if (owner.CanFocus) {
  75. retval |= AccessibleStates.Focusable;
  76. }
  77. return retval;
  78. }
  79. }
  80. #endregion // CheckBoxAccessibleObject Properties
  81. }
  82. #endregion // CheckBoxAccessibleObject Sub-class
  83. #region Public Constructors
  84. public CheckBox() {
  85. appearance = Appearance.Normal;
  86. auto_check = true;
  87. check_alignment = ContentAlignment.MiddleLeft;
  88. text_alignment = ContentAlignment.MiddleLeft;
  89. SetStyle(ControlStyles.StandardDoubleClick, false);
  90. }
  91. #endregion // Public Constructors
  92. #region Internal Methods
  93. internal override void Draw (PaintEventArgs pe) {
  94. ThemeEngine.Current.DrawCheckBox (pe.Graphics, this.ClientRectangle, this);
  95. }
  96. internal override void HaveDoubleClick() {
  97. if (DoubleClick != null) DoubleClick(this, EventArgs.Empty);
  98. }
  99. #endregion // Internal Methods
  100. #region Public Instance Properties
  101. [DefaultValue(Appearance.Normal)]
  102. [Localizable(true)]
  103. public Appearance Appearance {
  104. get {
  105. return appearance;
  106. }
  107. set {
  108. if (value != appearance) {
  109. appearance = value;
  110. OnAppearanceChanged (EventArgs.Empty);
  111. Redraw();
  112. }
  113. }
  114. }
  115. [DefaultValue(true)]
  116. public bool AutoCheck {
  117. get {
  118. return auto_check;
  119. }
  120. set {
  121. auto_check = value;
  122. }
  123. }
  124. [Bindable(true)]
  125. [Localizable(true)]
  126. [DefaultValue(ContentAlignment.MiddleLeft)]
  127. public ContentAlignment CheckAlign {
  128. get {
  129. return check_alignment;
  130. }
  131. set {
  132. if (value != check_alignment) {
  133. check_alignment = value;
  134. Redraw();
  135. }
  136. }
  137. }
  138. [Bindable(true)]
  139. [RefreshProperties(RefreshProperties.All)]
  140. [DefaultValue(false)]
  141. public bool Checked {
  142. get {
  143. if (check_state != CheckState.Unchecked) {
  144. return true;
  145. }
  146. return false;
  147. }
  148. set {
  149. if (value && (check_state != CheckState.Checked)) {
  150. check_state = CheckState.Checked;
  151. Redraw();
  152. OnCheckedChanged(EventArgs.Empty);
  153. } else if (!value && (check_state != CheckState.Unchecked)) {
  154. check_state = CheckState.Unchecked;
  155. Redraw();
  156. OnCheckedChanged(EventArgs.Empty);
  157. }
  158. }
  159. }
  160. [DefaultValue(CheckState.Unchecked)]
  161. [RefreshProperties(RefreshProperties.All)]
  162. [Bindable(true)]
  163. public CheckState CheckState {
  164. get {
  165. return check_state;
  166. }
  167. set {
  168. if (value != check_state) {
  169. bool was_checked = (check_state != CheckState.Unchecked);
  170. check_state = value;
  171. if (was_checked != (check_state != CheckState.Unchecked)) {
  172. OnCheckedChanged(EventArgs.Empty);
  173. }
  174. OnCheckStateChanged(EventArgs.Empty);
  175. Redraw();
  176. }
  177. }
  178. }
  179. [DefaultValue(ContentAlignment.MiddleLeft)]
  180. [Localizable(true)]
  181. public override ContentAlignment TextAlign {
  182. get {
  183. return text_alignment;
  184. }
  185. set {
  186. if (value != text_alignment) {
  187. text_alignment = value;
  188. Redraw();
  189. }
  190. }
  191. }
  192. [DefaultValue(false)]
  193. public bool ThreeState {
  194. get {
  195. return three_state;
  196. }
  197. set {
  198. three_state = value;
  199. }
  200. }
  201. #endregion // Public Instance Properties
  202. #region Protected Instance Properties
  203. protected override CreateParams CreateParams {
  204. get {
  205. return base.CreateParams;
  206. }
  207. }
  208. protected override Size DefaultSize {
  209. get {
  210. return new Size(104, 24);
  211. }
  212. }
  213. #endregion // Protected Instance Properties
  214. #region Public Instance Methods
  215. public override string ToString() {
  216. return base.ToString() + ", CheckState: " + (int)check_state;
  217. }
  218. #endregion // Public Instance Methods
  219. #region Protected Instance Methods
  220. protected override AccessibleObject CreateAccessibilityInstance() {
  221. AccessibleObject ao;
  222. ao = base.CreateAccessibilityInstance ();
  223. ao.role = AccessibleRole.CheckButton;
  224. return ao;
  225. }
  226. protected virtual void OnAppearanceChanged(EventArgs e) {
  227. EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
  228. if (eh != null)
  229. eh (this, e);
  230. }
  231. protected virtual void OnCheckedChanged(EventArgs e) {
  232. EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
  233. if (eh != null)
  234. eh (this, e);
  235. }
  236. protected virtual void OnCheckStateChanged(EventArgs e) {
  237. EventHandler eh = (EventHandler)(Events [CheckStateChangedEvent]);
  238. if (eh != null)
  239. eh (this, e);
  240. }
  241. protected override void OnClick(EventArgs e) {
  242. if (auto_check) {
  243. switch(check_state) {
  244. case CheckState.Unchecked: {
  245. if (three_state) {
  246. CheckState = CheckState.Indeterminate;
  247. } else {
  248. CheckState = CheckState.Checked;
  249. }
  250. break;
  251. }
  252. case CheckState.Indeterminate: {
  253. CheckState = CheckState.Checked;
  254. break;
  255. }
  256. case CheckState.Checked: {
  257. CheckState = CheckState.Unchecked;
  258. break;
  259. }
  260. }
  261. }
  262. base.OnClick (e);
  263. }
  264. protected override void OnHandleCreated(EventArgs e) {
  265. base.OnHandleCreated (e);
  266. }
  267. protected override void OnMouseUp(MouseEventArgs mevent) {
  268. base.OnMouseUp (mevent);
  269. }
  270. protected override bool ProcessMnemonic(char charCode) {
  271. if (IsMnemonic(charCode, Text) == true) {
  272. Select();
  273. OnClick(EventArgs.Empty);
  274. return true;
  275. }
  276. return base.ProcessMnemonic(charCode);
  277. }
  278. #endregion // Protected Instance Methods
  279. #region Events
  280. static object AppearanceChangedEvent = new object ();
  281. static object CheckedChangedEvent = new object ();
  282. static object CheckStateChangedEvent = new object ();
  283. public event EventHandler AppearanceChanged {
  284. add { Events.AddHandler (AppearanceChangedEvent, value); }
  285. remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
  286. }
  287. public event EventHandler CheckedChanged {
  288. add { Events.AddHandler (CheckedChangedEvent, value); }
  289. remove { Events.RemoveHandler (CheckedChangedEvent, value); }
  290. }
  291. public event EventHandler CheckStateChanged {
  292. add { Events.AddHandler (CheckStateChangedEvent, value); }
  293. remove { Events.RemoveHandler (CheckStateChangedEvent, value); }
  294. }
  295. #endregion // Events
  296. #region Events
  297. // XXX have a look at this and determine if it
  298. // manipulates base.DoubleClick, and see if
  299. // HaveDoubleClick can just call OnDoubleClick.
  300. [Browsable(false)]
  301. [EditorBrowsable (EditorBrowsableState.Never)]
  302. public new event EventHandler DoubleClick;
  303. #endregion // Events
  304. }
  305. }