CheckBox.cs 5.8 KB

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