RadioButton.cs 7.5 KB

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