RadioButton.cs 7.6 KB

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