RadioButton.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 ContentAlignment text_alignment;
  35. internal CheckState check_state;
  36. private bool is_tabstop;
  37. #endregion // Local Variables
  38. #region RadioButtonAccessibleObject Subclass
  39. public class RadioButtonAccessibleObject : ControlAccessibleObject {
  40. #region RadioButtonAccessibleObject Local Variables
  41. private RadioButton owner;
  42. #endregion // RadioButtonAccessibleObject Local Variables
  43. #region RadioButtonAccessibleObject Constructors
  44. public RadioButtonAccessibleObject(RadioButton owner) : base(owner) {
  45. this.owner = owner;
  46. }
  47. #endregion // RadioButtonAccessibleObject Constructors
  48. #region RadioButtonAccessibleObject Properties
  49. public override string DefaultAction {
  50. get {
  51. return "Select";
  52. }
  53. }
  54. public override AccessibleRole Role {
  55. get {
  56. return AccessibleRole.RadioButton;
  57. }
  58. }
  59. public override AccessibleStates State {
  60. get {
  61. AccessibleStates retval;
  62. retval = AccessibleStates.Default;
  63. if (owner.check_state == CheckState.Checked) {
  64. retval |= AccessibleStates.Checked;
  65. }
  66. if (owner.Focused) {
  67. retval |= AccessibleStates.Focused;
  68. }
  69. if (owner.CanFocus) {
  70. retval |= AccessibleStates.Focusable;
  71. }
  72. return retval;
  73. }
  74. }
  75. #endregion // RadioButtonAccessibleObject Properties
  76. #region RadioButtonAccessibleObject Methods
  77. public override void DoDefaultAction() {
  78. owner.PerformClick();
  79. }
  80. #endregion // RadioButtonAccessibleObject Methods
  81. }
  82. #endregion // RadioButtonAccessibleObject Sub-class
  83. #region Public Constructors
  84. public RadioButton() {
  85. appearance = Appearance.Normal;
  86. auto_check = true;
  87. radiobutton_alignment = ContentAlignment.MiddleLeft;
  88. text_alignment = ContentAlignment.MiddleLeft;
  89. is_tabstop = false;
  90. }
  91. #endregion // Public Constructors
  92. #region Public Instance Properties
  93. public Appearance Appearance {
  94. get {
  95. return appearance;
  96. }
  97. set {
  98. if (value != appearance) {
  99. appearance = value;
  100. if (AppearanceChanged != null) {
  101. AppearanceChanged(this, EventArgs.Empty);
  102. }
  103. Redraw();
  104. }
  105. }
  106. }
  107. public bool AutoCheck {
  108. get {
  109. return auto_check;
  110. }
  111. set {
  112. auto_check = value;
  113. }
  114. }
  115. public ContentAlignment CheckAlign {
  116. get {
  117. return radiobutton_alignment;
  118. }
  119. set {
  120. if (value != radiobutton_alignment) {
  121. radiobutton_alignment = value;
  122. Redraw();
  123. }
  124. }
  125. }
  126. public bool Checked {
  127. get {
  128. if (check_state != CheckState.Unchecked) {
  129. return true;
  130. }
  131. return false;
  132. }
  133. set {
  134. if (value && (check_state != CheckState.Checked)) {
  135. check_state = CheckState.Checked;
  136. Redraw();
  137. OnCheckedChanged(EventArgs.Empty);
  138. } else if (!value && (check_state != CheckState.Unchecked)) {
  139. check_state = CheckState.Unchecked;
  140. Redraw();
  141. OnCheckedChanged(EventArgs.Empty);
  142. }
  143. }
  144. }
  145. public bool TabStop {
  146. get {
  147. return is_tabstop;
  148. }
  149. set {
  150. is_tabstop = value;
  151. }
  152. }
  153. public override ContentAlignment TextAlign {
  154. get {
  155. return text_alignment;
  156. }
  157. set {
  158. if (value != text_alignment) {
  159. text_alignment = value;
  160. Redraw();
  161. }
  162. }
  163. }
  164. #endregion // Public Instance Properties
  165. #region Protected Instance Properties
  166. protected override CreateParams CreateParams {
  167. get {
  168. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  169. SetStyle(ControlStyles.UserPaint, true);
  170. return base.CreateParams;
  171. }
  172. }
  173. protected override Size DefaultSize {
  174. get {
  175. return ThemeEngine.Current.RadioButtonDefaultSize;
  176. }
  177. }
  178. #endregion // Protected Instance Properties
  179. #region Public Instance Methods
  180. public void PerformClick() {
  181. OnClick(EventArgs.Empty);
  182. }
  183. public override string ToString() {
  184. return base.ToString() + ", Checked: " + this.Checked;
  185. }
  186. #endregion // Public Instance Methods
  187. #region Protected Instance Methods
  188. protected override AccessibleObject CreateAccessibilityInstance() {
  189. return base.CreateAccessibilityInstance ();
  190. }
  191. protected virtual void OnCheckedChanged(EventArgs e) {
  192. if (CheckedChanged != null) {
  193. CheckedChanged(this, e);
  194. }
  195. }
  196. protected override void OnClick(EventArgs e) {
  197. if (auto_check) {
  198. Checked = !Checked;
  199. }
  200. }
  201. protected override void OnEnter(EventArgs e) {
  202. base.OnEnter(e);
  203. }
  204. protected override void OnHandleCreated(EventArgs e) {
  205. base.OnHandleCreated(e);
  206. }
  207. protected override void OnMouseUp(MouseEventArgs mevent) {
  208. base.OnMouseUp(mevent);
  209. }
  210. protected override bool ProcessMnemonic(char charCode) {
  211. return base.ProcessMnemonic(charCode);
  212. }
  213. #endregion // Protected Instance Methods
  214. #region Events
  215. public event EventHandler AppearanceChanged;
  216. public event EventHandler CheckedChanged;
  217. #endregion // Events
  218. #region Internal Drawing Code
  219. internal override void Draw (PaintEventArgs pe) {
  220. if (redraw) {
  221. ThemeEngine.Current.DrawRadioButton(this.DeviceContext, this.ClientRectangle, this);
  222. redraw = false;
  223. }
  224. }
  225. #endregion // Internal Drawing Code
  226. }
  227. }