RadioButton.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. // COMPLETE
  26. using System.Drawing;
  27. using System.Drawing.Text;
  28. namespace System.Windows.Forms {
  29. public class RadioButton : ButtonBase {
  30. #region Local Variables
  31. internal Appearance appearance;
  32. internal bool auto_check;
  33. internal ContentAlignment radiobutton_alignment;
  34. internal CheckState check_state;
  35. private bool is_tabstop;
  36. #endregion // Local Variables
  37. #region RadioButtonAccessibleObject Subclass
  38. public class RadioButtonAccessibleObject : ControlAccessibleObject {
  39. #region RadioButtonAccessibleObject Local Variables
  40. private RadioButton owner;
  41. #endregion // RadioButtonAccessibleObject Local Variables
  42. #region RadioButtonAccessibleObject Constructors
  43. public RadioButtonAccessibleObject(RadioButton owner) : base(owner) {
  44. this.owner = owner;
  45. }
  46. #endregion // RadioButtonAccessibleObject Constructors
  47. #region RadioButtonAccessibleObject Properties
  48. public override string DefaultAction {
  49. get {
  50. return "Select";
  51. }
  52. }
  53. public override AccessibleRole Role {
  54. get {
  55. return AccessibleRole.RadioButton;
  56. }
  57. }
  58. public override AccessibleStates State {
  59. get {
  60. AccessibleStates retval;
  61. retval = AccessibleStates.Default;
  62. if (owner.check_state == CheckState.Checked) {
  63. retval |= AccessibleStates.Checked;
  64. }
  65. if (owner.Focused) {
  66. retval |= AccessibleStates.Focused;
  67. }
  68. if (owner.CanFocus) {
  69. retval |= AccessibleStates.Focusable;
  70. }
  71. return retval;
  72. }
  73. }
  74. #endregion // RadioButtonAccessibleObject Properties
  75. #region RadioButtonAccessibleObject Methods
  76. public override void DoDefaultAction() {
  77. owner.PerformClick();
  78. }
  79. #endregion // RadioButtonAccessibleObject Methods
  80. }
  81. #endregion // RadioButtonAccessibleObject Sub-class
  82. #region Public Constructors
  83. public RadioButton() {
  84. appearance = Appearance.Normal;
  85. auto_check = true;
  86. radiobutton_alignment = ContentAlignment.MiddleLeft;
  87. text_alignment = ContentAlignment.MiddleLeft;
  88. is_tabstop = false;
  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 radiobutton_alignment;
  117. }
  118. set {
  119. if (value != radiobutton_alignment) {
  120. radiobutton_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 new bool TabStop {
  145. get {
  146. return is_tabstop;
  147. }
  148. set {
  149. is_tabstop = value;
  150. }
  151. }
  152. public override ContentAlignment TextAlign {
  153. get {
  154. return text_alignment;
  155. }
  156. set {
  157. if (value != text_alignment) {
  158. text_alignment = value;
  159. Redraw();
  160. }
  161. }
  162. }
  163. #endregion // Public Instance Properties
  164. #region Protected Instance Properties
  165. protected override CreateParams CreateParams {
  166. get {
  167. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  168. SetStyle(ControlStyles.UserPaint, true);
  169. return base.CreateParams;
  170. }
  171. }
  172. protected override Size DefaultSize {
  173. get {
  174. return ThemeEngine.Current.RadioButtonDefaultSize;
  175. }
  176. }
  177. #endregion // Protected Instance Properties
  178. #region Public Instance Methods
  179. public void PerformClick() {
  180. OnClick(EventArgs.Empty);
  181. }
  182. public override string ToString() {
  183. return base.ToString() + ", Checked: " + this.Checked;
  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 OnCheckedChanged(EventArgs e) {
  191. if (CheckedChanged != null) {
  192. CheckedChanged(this, e);
  193. }
  194. }
  195. protected override void OnClick(EventArgs e) {
  196. if (auto_check) {
  197. Checked = !Checked;
  198. }
  199. }
  200. protected override void OnEnter(EventArgs e) {
  201. base.OnEnter(e);
  202. }
  203. protected override void OnHandleCreated(EventArgs e) {
  204. base.OnHandleCreated(e);
  205. }
  206. protected override void OnMouseUp(MouseEventArgs mevent) {
  207. base.OnMouseUp(mevent);
  208. }
  209. protected override bool ProcessMnemonic(char charCode) {
  210. return base.ProcessMnemonic(charCode);
  211. }
  212. #endregion // Protected Instance Methods
  213. #region Events
  214. public event EventHandler AppearanceChanged;
  215. public event EventHandler CheckedChanged;
  216. #endregion // Events
  217. #region Internal Drawing Code
  218. internal override void Draw (PaintEventArgs pe) {
  219. if (redraw) {
  220. ThemeEngine.Current.DrawRadioButton(this.DeviceContext, this.ClientRectangle, this);
  221. redraw = false;
  222. }
  223. }
  224. #endregion // Internal Drawing Code
  225. }
  226. }