CheckBox.cs 7.8 KB

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