2
0

CheckBox.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. if (AppearanceChanged != null) {
  111. AppearanceChanged(this, EventArgs.Empty);
  112. }
  113. Redraw();
  114. }
  115. }
  116. }
  117. [DefaultValue(true)]
  118. public bool AutoCheck {
  119. get {
  120. return auto_check;
  121. }
  122. set {
  123. auto_check = value;
  124. }
  125. }
  126. [Bindable(true)]
  127. [Localizable(true)]
  128. [DefaultValue(ContentAlignment.MiddleLeft)]
  129. public ContentAlignment CheckAlign {
  130. get {
  131. return check_alignment;
  132. }
  133. set {
  134. if (value != check_alignment) {
  135. check_alignment = value;
  136. Redraw();
  137. }
  138. }
  139. }
  140. [Bindable(true)]
  141. [RefreshProperties(RefreshProperties.All)]
  142. [DefaultValue(false)]
  143. public bool Checked {
  144. get {
  145. if (check_state != CheckState.Unchecked) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. set {
  151. if (value && (check_state != CheckState.Checked)) {
  152. check_state = CheckState.Checked;
  153. Redraw();
  154. OnCheckedChanged(EventArgs.Empty);
  155. } else if (!value && (check_state != CheckState.Unchecked)) {
  156. check_state = CheckState.Unchecked;
  157. Redraw();
  158. OnCheckedChanged(EventArgs.Empty);
  159. }
  160. }
  161. }
  162. [DefaultValue(CheckState.Unchecked)]
  163. [RefreshProperties(RefreshProperties.All)]
  164. [Bindable(true)]
  165. public CheckState CheckState {
  166. get {
  167. return check_state;
  168. }
  169. set {
  170. if (value != check_state) {
  171. bool was_checked = (check_state != CheckState.Unchecked);
  172. check_state = value;
  173. if (was_checked != (check_state != CheckState.Unchecked)) {
  174. OnCheckedChanged(EventArgs.Empty);
  175. }
  176. OnCheckStateChanged(EventArgs.Empty);
  177. Redraw();
  178. }
  179. }
  180. }
  181. [DefaultValue(ContentAlignment.MiddleLeft)]
  182. [Localizable(true)]
  183. public override ContentAlignment TextAlign {
  184. get {
  185. return text_alignment;
  186. }
  187. set {
  188. if (value != text_alignment) {
  189. text_alignment = value;
  190. Redraw();
  191. }
  192. }
  193. }
  194. [DefaultValue(false)]
  195. public bool ThreeState {
  196. get {
  197. return three_state;
  198. }
  199. set {
  200. three_state = value;
  201. }
  202. }
  203. #endregion // Public Instance Properties
  204. #region Protected Instance Properties
  205. protected override CreateParams CreateParams {
  206. get {
  207. return base.CreateParams;
  208. }
  209. }
  210. protected override Size DefaultSize {
  211. get {
  212. return new Size(104, 24);
  213. }
  214. }
  215. #endregion // Protected Instance Properties
  216. #region Public Instance Methods
  217. public override string ToString() {
  218. return base.ToString() + ", CheckState: " + (int)check_state;
  219. }
  220. #endregion // Public Instance Methods
  221. #region Protected Instance Methods
  222. protected override AccessibleObject CreateAccessibilityInstance() {
  223. AccessibleObject ao;
  224. ao = base.CreateAccessibilityInstance ();
  225. ao.role = AccessibleRole.CheckButton;
  226. return ao;
  227. }
  228. protected virtual void OnAppearanceChanged(EventArgs e) {
  229. if (AppearanceChanged != null) {
  230. AppearanceChanged(this, e);
  231. }
  232. }
  233. protected virtual void OnCheckedChanged(EventArgs e) {
  234. if (CheckedChanged != null) {
  235. CheckedChanged(this, e);
  236. }
  237. }
  238. protected virtual void OnCheckStateChanged(EventArgs e) {
  239. if (CheckStateChanged != null) {
  240. CheckStateChanged(this, e);
  241. }
  242. }
  243. protected override void OnClick(EventArgs e) {
  244. if (auto_check) {
  245. switch(check_state) {
  246. case CheckState.Unchecked: {
  247. if (three_state) {
  248. CheckState = CheckState.Indeterminate;
  249. } else {
  250. CheckState = CheckState.Checked;
  251. }
  252. break;
  253. }
  254. case CheckState.Indeterminate: {
  255. CheckState = CheckState.Checked;
  256. break;
  257. }
  258. case CheckState.Checked: {
  259. CheckState = CheckState.Unchecked;
  260. break;
  261. }
  262. }
  263. }
  264. base.OnClick (e);
  265. }
  266. protected override void OnHandleCreated(EventArgs e) {
  267. base.OnHandleCreated (e);
  268. }
  269. protected override void OnMouseUp(MouseEventArgs mevent) {
  270. base.OnMouseUp (mevent);
  271. }
  272. protected override bool ProcessMnemonic(char charCode) {
  273. if (IsMnemonic(charCode, Text) == true) {
  274. Select();
  275. OnClick(EventArgs.Empty);
  276. return true;
  277. }
  278. return base.ProcessMnemonic(charCode);
  279. }
  280. #endregion // Protected Instance Methods
  281. #region Events
  282. public event EventHandler AppearanceChanged;
  283. public event EventHandler CheckedChanged;
  284. public event EventHandler CheckStateChanged;
  285. #endregion // Events
  286. #region Events
  287. [Browsable(false)]
  288. [EditorBrowsable (EditorBrowsableState.Never)]
  289. public event EventHandler DoubleClick;
  290. #endregion // Events
  291. }
  292. }