CheckBox.cs 7.2 KB

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