RadioButton.cs 7.2 KB

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