CheckBox.cs 6.8 KB

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