RadioButtonPainter.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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) 2007 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jonathan Pobst ([email protected])
  24. using System;
  25. using System.Drawing;
  26. using System.Drawing.Drawing2D;
  27. namespace System.Windows.Forms.Theming.Default
  28. {
  29. /// <summary>
  30. /// Summary description for Button.
  31. /// </summary>
  32. internal class RadioButtonPainter
  33. {
  34. public RadioButtonPainter ()
  35. {
  36. }
  37. protected SystemResPool ResPool { get { return ThemeEngine.Current.ResPool; } }
  38. public void PaintRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, ElementState state, FlatStyle style, bool isChecked)
  39. {
  40. switch (style) {
  41. case FlatStyle.Standard:
  42. case FlatStyle.System:
  43. switch (state) {
  44. case ElementState.Normal:
  45. DrawNormalRadioButton (g, bounds, backColor, foreColor, isChecked);
  46. break;
  47. case ElementState.Hot:
  48. DrawHotRadioButton (g, bounds, backColor, foreColor, isChecked);
  49. break;
  50. case ElementState.Pressed:
  51. DrawPressedRadioButton (g, bounds, backColor, foreColor, isChecked);
  52. break;
  53. case ElementState.Disabled:
  54. DrawDisabledRadioButton (g, bounds, backColor, foreColor, isChecked);
  55. break;
  56. }
  57. break;
  58. case FlatStyle.Flat:
  59. switch (state) {
  60. case ElementState.Normal:
  61. DrawFlatNormalRadioButton (g, bounds, backColor, foreColor, isChecked);
  62. break;
  63. case ElementState.Hot:
  64. DrawFlatHotRadioButton (g, bounds, backColor, foreColor, isChecked);
  65. break;
  66. case ElementState.Pressed:
  67. DrawFlatPressedRadioButton (g, bounds, backColor, foreColor, isChecked);
  68. break;
  69. case ElementState.Disabled:
  70. DrawFlatDisabledRadioButton (g, bounds, backColor, foreColor, isChecked);
  71. break;
  72. }
  73. break;
  74. case FlatStyle.Popup:
  75. switch (state) {
  76. case ElementState.Normal:
  77. DrawPopupNormalRadioButton (g, bounds, backColor, foreColor, isChecked);
  78. break;
  79. case ElementState.Hot:
  80. DrawPopupHotRadioButton (g, bounds, backColor, foreColor, isChecked);
  81. break;
  82. case ElementState.Pressed:
  83. DrawPopupPressedRadioButton (g, bounds, backColor, foreColor, isChecked);
  84. break;
  85. case ElementState.Disabled:
  86. DrawPopupDisabledRadioButton (g, bounds, backColor, foreColor, isChecked);
  87. break;
  88. }
  89. break;
  90. }
  91. }
  92. #region Standard
  93. public virtual void DrawNormalRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  94. {
  95. ButtonState bs = ButtonState.Normal;
  96. if (isChecked)
  97. bs |= ButtonState.Checked;
  98. ControlPaint.DrawRadioButton (g, bounds, bs);
  99. }
  100. public virtual void DrawHotRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  101. {
  102. DrawNormalRadioButton (g, bounds, backColor, foreColor, isChecked);
  103. }
  104. public virtual void DrawPressedRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  105. {
  106. ButtonState bs = ButtonState.Pushed;
  107. if (isChecked)
  108. bs |= ButtonState.Checked;
  109. ControlPaint.DrawRadioButton (g, bounds, bs);
  110. }
  111. public virtual void DrawDisabledRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  112. {
  113. ButtonState bs = ButtonState.Inactive;
  114. if (isChecked)
  115. bs |= ButtonState.Checked;
  116. ControlPaint.DrawRadioButton (g, bounds, bs);
  117. }
  118. #endregion
  119. #region FlatStyle
  120. public virtual void DrawFlatNormalRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  121. {
  122. g.DrawArc (SystemPens.ControlDarkDark, bounds, 0, 359);
  123. g.FillPie (SystemBrushes.ControlLightLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
  124. if (isChecked)
  125. DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
  126. }
  127. public virtual void DrawFlatHotRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  128. {
  129. g.DrawArc (SystemPens.ControlDarkDark, bounds, 0, 359);
  130. g.FillPie (SystemBrushes.ControlLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
  131. if (isChecked)
  132. DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
  133. }
  134. public virtual void DrawFlatPressedRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  135. {
  136. g.DrawArc (SystemPens.ControlDarkDark, bounds, 0, 359);
  137. g.FillPie (SystemBrushes.ControlLightLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
  138. if (isChecked)
  139. DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
  140. }
  141. public virtual void DrawFlatDisabledRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  142. {
  143. g.FillPie (SystemBrushes.Control, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
  144. g.DrawArc (SystemPens.ControlDark, bounds, 0, 359);
  145. if (isChecked)
  146. DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDark);
  147. }
  148. #endregion
  149. #region Popup
  150. public virtual void DrawPopupNormalRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  151. {
  152. g.FillPie (SystemBrushes.ControlLightLight, bounds, 0, 359);
  153. g.DrawArc (SystemPens.ControlDark, bounds, 0, 359);
  154. if (isChecked)
  155. DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
  156. }
  157. public virtual void DrawPopupHotRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  158. {
  159. g.FillPie (SystemBrushes.ControlLightLight, bounds, 0, 359);
  160. g.DrawArc (SystemPens.ControlLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
  161. g.DrawArc (SystemPens.ControlDark, bounds, 135, 180);
  162. g.DrawArc (SystemPens.ControlLightLight, bounds, 315, 180);
  163. if (isChecked)
  164. DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
  165. }
  166. public virtual void DrawPopupPressedRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  167. {
  168. g.FillPie (SystemBrushes.ControlLightLight, bounds, 0, 359);
  169. g.DrawArc (SystemPens.ControlLight, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
  170. g.DrawArc (SystemPens.ControlDark, bounds, 135, 180);
  171. g.DrawArc (SystemPens.ControlLightLight, bounds, 315, 180);
  172. if (isChecked)
  173. DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
  174. }
  175. public virtual void DrawPopupDisabledRadioButton (Graphics g, Rectangle bounds, Color backColor, Color foreColor, bool isChecked)
  176. {
  177. g.FillPie (SystemBrushes.Control, bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2, 0, 359);
  178. g.DrawArc (SystemPens.ControlDark, bounds, 0, 359);
  179. if (isChecked)
  180. DrawFlatRadioGlyphDot (g, bounds, SystemColors.ControlDarkDark);
  181. }
  182. #endregion
  183. #region Glyph
  184. protected void DrawFlatRadioGlyphDot (Graphics g, Rectangle bounds, Color dotColor)
  185. {
  186. int lineWidth = Math.Max (1, Math.Min (bounds.Width, bounds.Height) / 3);
  187. Pen dot_pen = ResPool.GetPen (dotColor);
  188. Brush dot_brush = ResPool.GetSolidBrush (dotColor);
  189. if (bounds.Height > 13) {
  190. g.FillPie (dot_brush, bounds.X + lineWidth, bounds.Y + lineWidth, bounds.Width - lineWidth * 2, bounds.Height - lineWidth * 2, 0, 359);
  191. } else {
  192. int x_half_pos = (bounds.Width / 2) + bounds.X;
  193. int y_half_pos = (bounds.Height / 2) + bounds.Y;
  194. g.DrawLine (dot_pen, x_half_pos - 1, y_half_pos, x_half_pos + 2, y_half_pos);
  195. g.DrawLine (dot_pen, x_half_pos - 1, y_half_pos + 1, x_half_pos + 2, y_half_pos + 1);
  196. g.DrawLine (dot_pen, x_half_pos, y_half_pos - 1, x_half_pos, y_half_pos + 2);
  197. g.DrawLine (dot_pen, x_half_pos + 1, y_half_pos - 1, x_half_pos + 1, y_half_pos + 2);
  198. }
  199. }
  200. #endregion
  201. }
  202. }