RadioButton.cs 7.1 KB

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