RadioButton.cs 7.2 KB

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