CheckBox.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. //
  27. // $Log: CheckBox.cs,v $
  28. // Revision 1.8 2004/10/13 02:46:22 pbartok
  29. // - Fix from John BouAntoun: Now properly sets the Appearance property
  30. //
  31. // Revision 1.7 2004/09/28 18:44:25 pbartok
  32. // - Streamlined Theme interfaces:
  33. // * Each DrawXXX method for a control now is passed the object for the
  34. // control to be drawn in order to allow accessing any state the theme
  35. // might require
  36. //
  37. // * ControlPaint methods for the theme now have a CP prefix to avoid
  38. // name clashes with the Draw methods for controls
  39. //
  40. // * Every control now retrieves it's DefaultSize from the current theme
  41. //
  42. // Revision 1.6 2004/09/01 20:01:24 pbartok
  43. // - Added missing default
  44. // - Added missing region mark
  45. //
  46. // Revision 1.5 2004/09/01 01:55:58 pbartok
  47. // - Fixed to match the removal of the needs_redraw concept
  48. //
  49. // Revision 1.4 2004/08/31 18:48:31 pbartok
  50. // - Finished (famous last words)
  51. //
  52. // Revision 1.3 2004/08/30 20:42:26 pbartok
  53. // - Implemented CheckBox drawing code
  54. //
  55. // Revision 1.2 2004/08/30 15:44:20 pbartok
  56. // - Updated to fix broken build. Not complete yet.
  57. //
  58. // Revision 1.1 2004/07/09 05:21:25 pbartok
  59. // - Initial check-in
  60. //
  61. //
  62. // COMPLETE
  63. using System;
  64. using System.Drawing;
  65. namespace System.Windows.Forms {
  66. public class CheckBox : ButtonBase {
  67. #region Local Variables
  68. internal Appearance appearance;
  69. internal bool auto_check;
  70. internal ContentAlignment check_alignment;
  71. internal ContentAlignment text_alignment;
  72. internal CheckState check_state;
  73. internal bool three_state;
  74. #endregion // Local Variables
  75. #region Public Constructors
  76. public CheckBox() {
  77. appearance = Appearance.Normal;
  78. auto_check = true;
  79. check_alignment = ContentAlignment.MiddleLeft;
  80. text_alignment = ContentAlignment.MiddleLeft;
  81. }
  82. #endregion // Public Constructors
  83. #region Public Instance Properties
  84. public Appearance Appearance {
  85. get {
  86. return appearance;
  87. }
  88. set {
  89. if (value != appearance) {
  90. appearance = value;
  91. if (AppearanceChanged != null) {
  92. AppearanceChanged(this, EventArgs.Empty);
  93. }
  94. Redraw();
  95. }
  96. }
  97. }
  98. public bool AutoCheck {
  99. get {
  100. return auto_check;
  101. }
  102. set {
  103. auto_check = value;
  104. }
  105. }
  106. public ContentAlignment CheckAlign {
  107. get {
  108. return check_alignment;
  109. }
  110. set {
  111. if (value != check_alignment) {
  112. check_alignment = value;
  113. Redraw();
  114. }
  115. }
  116. }
  117. public bool Checked {
  118. get {
  119. if (check_state != CheckState.Unchecked) {
  120. return true;
  121. }
  122. return false;
  123. }
  124. set {
  125. if (value && (check_state != CheckState.Checked)) {
  126. check_state = CheckState.Checked;
  127. Redraw();
  128. OnCheckedChanged(EventArgs.Empty);
  129. } else if (!value && (check_state != CheckState.Unchecked)) {
  130. check_state = CheckState.Unchecked;
  131. Redraw();
  132. OnCheckedChanged(EventArgs.Empty);
  133. }
  134. }
  135. }
  136. public CheckState CheckState {
  137. get {
  138. return check_state;
  139. }
  140. set {
  141. if (value != check_state) {
  142. bool was_checked = (check_state != CheckState.Unchecked);
  143. check_state = value;
  144. if (was_checked != (check_state != CheckState.Unchecked)) {
  145. OnCheckedChanged(EventArgs.Empty);
  146. }
  147. OnCheckStateChanged(EventArgs.Empty);
  148. Redraw();
  149. }
  150. }
  151. }
  152. public override ContentAlignment TextAlign {
  153. get {
  154. return text_alignment;
  155. }
  156. set {
  157. if (value != text_alignment) {
  158. text_alignment = value;
  159. Redraw();
  160. }
  161. }
  162. }
  163. public bool ThreeState {
  164. get {
  165. return three_state;
  166. }
  167. set {
  168. three_state = value;
  169. }
  170. }
  171. #endregion // Public Instance Properties
  172. #region Protected Instance Properties
  173. protected override CreateParams CreateParams {
  174. get {
  175. return base.CreateParams;
  176. }
  177. }
  178. protected override Size DefaultSize {
  179. get {
  180. return new Size(104, 24);
  181. }
  182. }
  183. #endregion // Protected Instance Properties
  184. #region Public Instance Methods
  185. public override string ToString() {
  186. return base.ToString() + ", CheckState: " + (int)check_state;
  187. }
  188. #endregion // Public Instance Methods
  189. #region Protected Instance Methods
  190. protected override AccessibleObject CreateAccessibilityInstance() {
  191. return base.CreateAccessibilityInstance ();
  192. }
  193. protected virtual void OnAppearanceChanged(EventArgs e) {
  194. if (AppearanceChanged != null) {
  195. AppearanceChanged(this, e);
  196. }
  197. }
  198. protected virtual void OnCheckedChanged(EventArgs e) {
  199. if (CheckedChanged != null) {
  200. CheckedChanged(this, e);
  201. }
  202. }
  203. protected virtual void OnCheckStateChanged(EventArgs e) {
  204. if (CheckStateChanged != null) {
  205. CheckStateChanged(this, e);
  206. }
  207. }
  208. protected override void OnClick(EventArgs e) {
  209. if (auto_check) {
  210. switch(check_state) {
  211. case CheckState.Unchecked: {
  212. if (three_state) {
  213. CheckState = CheckState.Indeterminate;
  214. } else {
  215. CheckState = CheckState.Checked;
  216. }
  217. break;
  218. }
  219. case CheckState.Indeterminate: {
  220. CheckState = CheckState.Checked;
  221. break;
  222. }
  223. case CheckState.Checked: {
  224. CheckState = CheckState.Unchecked;
  225. break;
  226. }
  227. }
  228. }
  229. }
  230. protected override void OnHandleCreated(EventArgs e) {
  231. base.OnHandleCreated (e);
  232. }
  233. protected override void OnMouseUp(MouseEventArgs e) {
  234. base.OnMouseUp (e);
  235. }
  236. protected override bool ProcessMnemonic(char charCode) {
  237. return base.ProcessMnemonic (charCode);
  238. }
  239. #endregion // Protected Instance Methods
  240. #region Events
  241. public event EventHandler AppearanceChanged;
  242. public event EventHandler CheckedChanged;
  243. public event EventHandler CheckStateChanged;
  244. #endregion // Events
  245. #region Internal drawing code
  246. internal override void Redraw() {
  247. ThemeEngine.Current.DrawCheckBox(this.DeviceContext, this.ClientRectangle, this);
  248. Refresh();
  249. }
  250. #endregion // Internal drawing code
  251. }
  252. }