RadioButton.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. // COMPLETE
  26. using System.ComponentModel;
  27. using System.Drawing;
  28. using System.Drawing.Text;
  29. using System.Runtime.InteropServices;
  30. namespace System.Windows.Forms {
  31. [DefaultProperty("Checked")]
  32. [DefaultEvent("CheckedChanged")]
  33. public class RadioButton : ButtonBase {
  34. #region Local Variables
  35. internal Appearance appearance;
  36. internal bool auto_check;
  37. internal ContentAlignment radiobutton_alignment;
  38. internal CheckState check_state;
  39. #endregion // Local Variables
  40. #region RadioButtonAccessibleObject Subclass
  41. [ComVisible(true)]
  42. public class RadioButtonAccessibleObject : ControlAccessibleObject {
  43. #region RadioButtonAccessibleObject Local Variables
  44. private RadioButton owner;
  45. #endregion // RadioButtonAccessibleObject Local Variables
  46. #region RadioButtonAccessibleObject Constructors
  47. public RadioButtonAccessibleObject(RadioButton owner) : base(owner) {
  48. this.owner = owner;
  49. }
  50. #endregion // RadioButtonAccessibleObject Constructors
  51. #region RadioButtonAccessibleObject Properties
  52. public override string DefaultAction {
  53. get {
  54. return "Select";
  55. }
  56. }
  57. public override AccessibleRole Role {
  58. get {
  59. return AccessibleRole.RadioButton;
  60. }
  61. }
  62. public override AccessibleStates State {
  63. get {
  64. AccessibleStates retval;
  65. retval = AccessibleStates.Default;
  66. if (owner.check_state == CheckState.Checked) {
  67. retval |= AccessibleStates.Checked;
  68. }
  69. if (owner.Focused) {
  70. retval |= AccessibleStates.Focused;
  71. }
  72. if (owner.CanFocus) {
  73. retval |= AccessibleStates.Focusable;
  74. }
  75. return retval;
  76. }
  77. }
  78. #endregion // RadioButtonAccessibleObject Properties
  79. #region RadioButtonAccessibleObject Methods
  80. public override void DoDefaultAction() {
  81. owner.PerformClick();
  82. }
  83. #endregion // RadioButtonAccessibleObject Methods
  84. }
  85. #endregion // RadioButtonAccessibleObject Sub-class
  86. #region Public Constructors
  87. public RadioButton() {
  88. appearance = Appearance.Normal;
  89. auto_check = true;
  90. radiobutton_alignment = ContentAlignment.MiddleLeft;
  91. text_alignment = ContentAlignment.MiddleLeft;
  92. TabStop = false;
  93. }
  94. #endregion // Public Constructors
  95. #region Private Methods
  96. private void UpdateSiblings() {
  97. Control c;
  98. if (auto_check == false) {
  99. return;
  100. }
  101. // Remove tabstop property from and uncheck our radio-button siblings
  102. c = this.Parent;
  103. if (c != null) {
  104. for (int i = 0; i < c.Controls.Count; i++) {
  105. if ((this != c.Controls[i]) && (c.Controls[i] is RadioButton)) {
  106. if (((RadioButton)(c.Controls[i])).auto_check) {
  107. c.Controls[i].TabStop = false;
  108. ((RadioButton)(c.Controls[i])).Checked = false;
  109. }
  110. }
  111. }
  112. }
  113. this.TabStop = true;
  114. }
  115. internal override void Draw (PaintEventArgs pe) {
  116. ThemeEngine.Current.DrawRadioButton (pe.Graphics, this.ClientRectangle, this);
  117. }
  118. #endregion // Private Methods
  119. #region Public Instance Properties
  120. [DefaultValue(Appearance.Normal)]
  121. [Localizable(true)]
  122. public Appearance Appearance {
  123. get {
  124. return appearance;
  125. }
  126. set {
  127. if (value != appearance) {
  128. appearance = value;
  129. EventHandler eh = (EventHandler)(Events [AppearanceChangedEvent]);
  130. if (eh != null)
  131. eh (this, EventArgs.Empty);
  132. Redraw();
  133. }
  134. }
  135. }
  136. [DefaultValue(true)]
  137. public bool AutoCheck {
  138. get {
  139. return auto_check;
  140. }
  141. set {
  142. auto_check = value;
  143. }
  144. }
  145. [Bindable(true)]
  146. [Localizable(true)]
  147. [DefaultValue(ContentAlignment.MiddleLeft)]
  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. [DefaultValue(false)]
  160. public bool Checked {
  161. get {
  162. if (check_state != CheckState.Unchecked) {
  163. return true;
  164. }
  165. return false;
  166. }
  167. set {
  168. if (value && (check_state != CheckState.Checked)) {
  169. UpdateSiblings();
  170. check_state = CheckState.Checked;
  171. Redraw();
  172. OnCheckedChanged(EventArgs.Empty);
  173. } else if (!value && (check_state != CheckState.Unchecked)) {
  174. check_state = CheckState.Unchecked;
  175. Redraw();
  176. OnCheckedChanged(EventArgs.Empty);
  177. }
  178. }
  179. }
  180. [DefaultValue(false)]
  181. public new bool TabStop {
  182. get { return base.TabStop; }
  183. set { base.TabStop = value; }
  184. }
  185. [DefaultValue(ContentAlignment.MiddleLeft)]
  186. [Localizable(true)]
  187. public override ContentAlignment TextAlign {
  188. get {
  189. return text_alignment;
  190. }
  191. set {
  192. if (value != text_alignment) {
  193. text_alignment = value;
  194. Redraw();
  195. }
  196. }
  197. }
  198. #endregion // Public Instance Properties
  199. #region Protected Instance Properties
  200. protected override CreateParams CreateParams {
  201. get {
  202. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  203. SetStyle(ControlStyles.UserPaint, true);
  204. return base.CreateParams;
  205. }
  206. }
  207. protected override Size DefaultSize {
  208. get {
  209. return ThemeEngine.Current.RadioButtonDefaultSize;
  210. }
  211. }
  212. #endregion // Protected Instance Properties
  213. #region Public Instance Methods
  214. public void PerformClick() {
  215. OnClick(EventArgs.Empty);
  216. }
  217. public override string ToString() {
  218. return base.ToString() + ", Checked: " + this.Checked;
  219. }
  220. #endregion // Public Instance Methods
  221. #region Protected Instance Methods
  222. protected override AccessibleObject CreateAccessibilityInstance() {
  223. AccessibleObject ao;
  224. ao = base.CreateAccessibilityInstance ();
  225. ao.role = AccessibleRole.RadioButton;
  226. return ao;
  227. }
  228. protected virtual void OnCheckedChanged(EventArgs e) {
  229. EventHandler eh = (EventHandler)(Events [CheckedChangedEvent]);
  230. if (eh != null)
  231. eh (this, e);
  232. }
  233. protected override void OnClick(EventArgs e) {
  234. if (auto_check) {
  235. if (!Checked) {
  236. Checked = true;
  237. }
  238. } else {
  239. Checked = !Checked;
  240. }
  241. base.OnClick (e);
  242. }
  243. protected override void OnEnter(EventArgs e) {
  244. base.OnEnter(e);
  245. }
  246. protected override void OnHandleCreated(EventArgs e) {
  247. base.OnHandleCreated(e);
  248. }
  249. protected override void OnMouseUp(MouseEventArgs mevent) {
  250. base.OnMouseUp(mevent);
  251. }
  252. protected override bool ProcessMnemonic(char charCode) {
  253. if (IsMnemonic(charCode, Text) == true) {
  254. Select();
  255. PerformClick();
  256. return true;
  257. }
  258. return base.ProcessMnemonic(charCode);
  259. }
  260. #endregion // Protected Instance Methods
  261. #region Events
  262. static object AppearanceChangedEvent = new object ();
  263. static object CheckedChangedEvent = new object ();
  264. public event EventHandler AppearanceChanged {
  265. add { Events.AddHandler (AppearanceChangedEvent, value); }
  266. remove { Events.RemoveHandler (AppearanceChangedEvent, value); }
  267. }
  268. public event EventHandler CheckedChanged {
  269. add { Events.AddHandler (CheckedChangedEvent, value); }
  270. remove { Events.RemoveHandler (CheckedChangedEvent, value); }
  271. }
  272. [Browsable(false)]
  273. [EditorBrowsable (EditorBrowsableState.Never)]
  274. public new event EventHandler DoubleClick {
  275. add { base.DoubleClick += value; }
  276. remove { base.DoubleClick -= value; }
  277. }
  278. #endregion // Events
  279. }
  280. }