CheckBox.cs 7.2 KB

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