CheckBox.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.Drawing;
  29. namespace System.Windows.Forms {
  30. public class CheckBox : ButtonBase {
  31. #region Local Variables
  32. internal Appearance appearance;
  33. internal bool auto_check;
  34. internal ContentAlignment check_alignment;
  35. internal CheckState check_state;
  36. internal bool three_state;
  37. #endregion // Local Variables
  38. #region Public Constructors
  39. public CheckBox() {
  40. appearance = Appearance.Normal;
  41. auto_check = true;
  42. check_alignment = ContentAlignment.MiddleLeft;
  43. text_alignment = ContentAlignment.MiddleLeft;
  44. }
  45. #endregion // Public Constructors
  46. #region Public Instance Properties
  47. public Appearance Appearance {
  48. get {
  49. return appearance;
  50. }
  51. set {
  52. if (value != appearance) {
  53. appearance = value;
  54. if (AppearanceChanged != null) {
  55. AppearanceChanged(this, EventArgs.Empty);
  56. }
  57. Redraw();
  58. }
  59. }
  60. }
  61. public bool AutoCheck {
  62. get {
  63. return auto_check;
  64. }
  65. set {
  66. auto_check = value;
  67. }
  68. }
  69. public ContentAlignment CheckAlign {
  70. get {
  71. return check_alignment;
  72. }
  73. set {
  74. if (value != check_alignment) {
  75. check_alignment = value;
  76. Redraw();
  77. }
  78. }
  79. }
  80. public bool Checked {
  81. get {
  82. if (check_state != CheckState.Unchecked) {
  83. return true;
  84. }
  85. return false;
  86. }
  87. set {
  88. if (value && (check_state != CheckState.Checked)) {
  89. check_state = CheckState.Checked;
  90. Redraw();
  91. OnCheckedChanged(EventArgs.Empty);
  92. } else if (!value && (check_state != CheckState.Unchecked)) {
  93. check_state = CheckState.Unchecked;
  94. Redraw();
  95. OnCheckedChanged(EventArgs.Empty);
  96. }
  97. }
  98. }
  99. public CheckState CheckState {
  100. get {
  101. return check_state;
  102. }
  103. set {
  104. if (value != check_state) {
  105. bool was_checked = (check_state != CheckState.Unchecked);
  106. check_state = value;
  107. if (was_checked != (check_state != CheckState.Unchecked)) {
  108. OnCheckedChanged(EventArgs.Empty);
  109. }
  110. OnCheckStateChanged(EventArgs.Empty);
  111. Redraw();
  112. }
  113. }
  114. }
  115. public override ContentAlignment TextAlign {
  116. get {
  117. return text_alignment;
  118. }
  119. set {
  120. if (value != text_alignment) {
  121. text_alignment = value;
  122. Redraw();
  123. }
  124. }
  125. }
  126. public bool ThreeState {
  127. get {
  128. return three_state;
  129. }
  130. set {
  131. three_state = value;
  132. }
  133. }
  134. #endregion // Public Instance Properties
  135. #region Protected Instance Properties
  136. protected override CreateParams CreateParams {
  137. get {
  138. return base.CreateParams;
  139. }
  140. }
  141. protected override Size DefaultSize {
  142. get {
  143. return new Size(104, 24);
  144. }
  145. }
  146. #endregion // Protected Instance Properties
  147. #region Public Instance Methods
  148. public override string ToString() {
  149. return base.ToString() + ", CheckState: " + (int)check_state;
  150. }
  151. #endregion // Public Instance Methods
  152. #region Protected Instance Methods
  153. protected override AccessibleObject CreateAccessibilityInstance() {
  154. return base.CreateAccessibilityInstance ();
  155. }
  156. protected virtual void OnAppearanceChanged(EventArgs e) {
  157. if (AppearanceChanged != null) {
  158. AppearanceChanged(this, e);
  159. }
  160. }
  161. protected virtual void OnCheckedChanged(EventArgs e) {
  162. if (CheckedChanged != null) {
  163. CheckedChanged(this, e);
  164. }
  165. }
  166. protected virtual void OnCheckStateChanged(EventArgs e) {
  167. if (CheckStateChanged != null) {
  168. CheckStateChanged(this, e);
  169. }
  170. }
  171. protected override void OnClick(EventArgs e) {
  172. if (auto_check) {
  173. switch(check_state) {
  174. case CheckState.Unchecked: {
  175. if (three_state) {
  176. CheckState = CheckState.Indeterminate;
  177. } else {
  178. CheckState = CheckState.Checked;
  179. }
  180. break;
  181. }
  182. case CheckState.Indeterminate: {
  183. CheckState = CheckState.Checked;
  184. break;
  185. }
  186. case CheckState.Checked: {
  187. CheckState = CheckState.Unchecked;
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. protected override void OnHandleCreated(EventArgs e) {
  194. base.OnHandleCreated (e);
  195. }
  196. protected override void OnMouseUp(MouseEventArgs e) {
  197. base.OnMouseUp (e);
  198. }
  199. protected override bool ProcessMnemonic(char charCode) {
  200. return base.ProcessMnemonic (charCode);
  201. }
  202. #endregion // Protected Instance Methods
  203. #region Events
  204. public event EventHandler AppearanceChanged;
  205. public event EventHandler CheckedChanged;
  206. public event EventHandler CheckStateChanged;
  207. #endregion // Events
  208. #region Internal drawing code
  209. internal override void Draw (PaintEventArgs pe) {
  210. if (redraw) {
  211. ThemeEngine.Current.DrawCheckBox (this.DeviceContext, this.ClientRectangle, this);
  212. redraw = false;
  213. }
  214. }
  215. #endregion // Internal drawing code
  216. }
  217. }