CheckBoxPainter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 CheckBoxPainter
  33. {
  34. public CheckBoxPainter ()
  35. {
  36. }
  37. protected SystemResPool ResPool { get { return ThemeEngine.Current.ResPool; } }
  38. public void PaintCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, ElementState state, FlatStyle style, CheckState checkState)
  39. {
  40. switch (style) {
  41. case FlatStyle.Standard:
  42. case FlatStyle.System:
  43. switch (state) {
  44. case ElementState.Normal:
  45. DrawNormalCheckBox (g, bounds, backColor, foreColor, checkState);
  46. break;
  47. case ElementState.Hot:
  48. DrawHotCheckBox (g, bounds, backColor, foreColor, checkState);
  49. break;
  50. case ElementState.Pressed:
  51. DrawPressedCheckBox (g, bounds, backColor, foreColor, checkState);
  52. break;
  53. case ElementState.Disabled:
  54. DrawDisabledCheckBox (g, bounds, backColor, foreColor, checkState);
  55. break;
  56. }
  57. break;
  58. case FlatStyle.Flat:
  59. switch (state) {
  60. case ElementState.Normal:
  61. DrawFlatNormalCheckBox (g, bounds, backColor, foreColor, checkState);
  62. break;
  63. case ElementState.Hot:
  64. DrawFlatHotCheckBox (g, bounds, backColor, foreColor, checkState);
  65. break;
  66. case ElementState.Pressed:
  67. DrawFlatPressedCheckBox (g, bounds, backColor, foreColor, checkState);
  68. break;
  69. case ElementState.Disabled:
  70. DrawFlatDisabledCheckBox (g, bounds, backColor, foreColor, checkState);
  71. break;
  72. }
  73. break;
  74. case FlatStyle.Popup:
  75. switch (state) {
  76. case ElementState.Normal:
  77. DrawPopupNormalCheckBox (g, bounds, backColor, foreColor, checkState);
  78. break;
  79. case ElementState.Hot:
  80. DrawPopupHotCheckBox (g, bounds, backColor, foreColor, checkState);
  81. break;
  82. case ElementState.Pressed:
  83. DrawPopupPressedCheckBox (g, bounds, backColor, foreColor, checkState);
  84. break;
  85. case ElementState.Disabled:
  86. DrawPopupDisabledCheckBox (g, bounds, backColor, foreColor, checkState);
  87. break;
  88. }
  89. break;
  90. }
  91. }
  92. #region Standard
  93. public virtual void DrawNormalCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  94. {
  95. int check_box_visible_size = (bounds.Height > bounds.Width) ? bounds.Width : bounds.Height;
  96. int x_pos = Math.Max (0, bounds.X + (bounds.Width / 2) - check_box_visible_size / 2);
  97. int y_pos = Math.Max (0, bounds.Y + (bounds.Height / 2) - check_box_visible_size / 2);
  98. Rectangle rect = new Rectangle (x_pos, y_pos, check_box_visible_size, check_box_visible_size);
  99. g.FillRectangle (SystemBrushes.ControlLightLight, rect.X + 2, rect.Y + 2, rect.Width - 3, rect.Height - 3);
  100. Pen pen = SystemPens.ControlDark;
  101. g.DrawLine (pen, rect.X, rect.Y, rect.X, rect.Bottom - 2);
  102. g.DrawLine (pen, rect.X + 1, rect.Y, rect.Right - 2, rect.Y);
  103. pen = SystemPens.ControlDarkDark;
  104. g.DrawLine (pen, rect.X + 1, rect.Y + 1, rect.X + 1, rect.Bottom - 3);
  105. g.DrawLine (pen, rect.X + 2, rect.Y + 1, rect.Right - 3, rect.Y + 1);
  106. pen = SystemPens.ControlLightLight;
  107. g.DrawLine (pen, rect.Right - 1, rect.Y, rect.Right - 1, rect.Bottom - 1);
  108. g.DrawLine (pen, rect.X, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
  109. // oh boy, matching ms is like fighting against windmills
  110. using (Pen h_pen = new Pen (ResPool.GetHatchBrush (HatchStyle.Percent50,
  111. Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255),
  112. ColorControl.G, ColorControl.B), ColorControl))) {
  113. g.DrawLine (h_pen, rect.X + 1, rect.Bottom - 2, rect.Right - 2, rect.Bottom - 2);
  114. g.DrawLine (h_pen, rect.Right - 2, rect.Y + 1, rect.Right - 2, rect.Bottom - 2);
  115. }
  116. if (state == CheckState.Checked)
  117. DrawCheck (g, bounds, Color.Black);
  118. else if (state == CheckState.Indeterminate)
  119. DrawCheck (g, bounds, SystemColors.ControlDark);
  120. }
  121. public virtual void DrawHotCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  122. {
  123. DrawNormalCheckBox (g, bounds, backColor, foreColor, state);
  124. }
  125. public virtual void DrawPressedCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  126. {
  127. int check_box_visible_size = (bounds.Height > bounds.Width) ? bounds.Width : bounds.Height;
  128. int x_pos = Math.Max (0, bounds.X + (bounds.Width / 2) - check_box_visible_size / 2);
  129. int y_pos = Math.Max (0, bounds.Y + (bounds.Height / 2) - check_box_visible_size / 2);
  130. Rectangle rect = new Rectangle (x_pos, y_pos, check_box_visible_size, check_box_visible_size);
  131. g.FillRectangle (ResPool.GetHatchBrush (HatchStyle.Percent50,
  132. Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255),
  133. ColorControl.G, ColorControl.B),
  134. ColorControl), rect.X + 2, rect.Y + 2, rect.Width - 3, rect.Height - 3);
  135. Pen pen = SystemPens.ControlDark;
  136. g.DrawLine (pen, rect.X, rect.Y, rect.X, rect.Bottom - 2);
  137. g.DrawLine (pen, rect.X + 1, rect.Y, rect.Right - 2, rect.Y);
  138. pen = SystemPens.ControlDarkDark;
  139. g.DrawLine (pen, rect.X + 1, rect.Y + 1, rect.X + 1, rect.Bottom - 3);
  140. g.DrawLine (pen, rect.X + 2, rect.Y + 1, rect.Right - 3, rect.Y + 1);
  141. pen = SystemPens.ControlLightLight;
  142. g.DrawLine (pen, rect.Right - 1, rect.Y, rect.Right - 1, rect.Bottom - 1);
  143. g.DrawLine (pen, rect.X, rect.Bottom - 1, rect.Right - 1, rect.Bottom - 1);
  144. // oh boy, matching ms is like fighting against windmills
  145. using (Pen h_pen = new Pen (ResPool.GetHatchBrush (HatchStyle.Percent50,
  146. Color.FromArgb (Clamp (ColorControl.R + 3, 0, 255),
  147. ColorControl.G, ColorControl.B), ColorControl))) {
  148. g.DrawLine (h_pen, rect.X + 1, rect.Bottom - 2, rect.Right - 2, rect.Bottom - 2);
  149. g.DrawLine (h_pen, rect.Right - 2, rect.Y + 1, rect.Right - 2, rect.Bottom - 2);
  150. }
  151. if (state == CheckState.Checked)
  152. DrawCheck (g, bounds, Color.Black);
  153. else if (state == CheckState.Indeterminate)
  154. DrawCheck (g, bounds, SystemColors.ControlDarkDark);
  155. }
  156. public virtual void DrawDisabledCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  157. {
  158. DrawPressedCheckBox (g, bounds, backColor, foreColor, CheckState.Unchecked);
  159. if (state == CheckState.Checked || state == CheckState.Indeterminate)
  160. DrawCheck (g, bounds, SystemColors.ControlDark);
  161. }
  162. #endregion
  163. #region FlatStyle
  164. public virtual void DrawFlatNormalCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  165. {
  166. Rectangle checkbox_rectangle;
  167. Rectangle fill_rectangle;
  168. // set up our rectangles first
  169. // clip two pixels from bottom right for non popup rendered checkboxes
  170. checkbox_rectangle = new Rectangle (bounds.X, bounds.Y, Math.Max (bounds.Width - 2, 0), Math.Max (bounds.Height - 2, 0));
  171. fill_rectangle = new Rectangle (checkbox_rectangle.X + 1, checkbox_rectangle.Y + 1, Math.Max (checkbox_rectangle.Width - 2, 0), Math.Max (checkbox_rectangle.Height - 2, 0));
  172. g.FillRectangle (ResPool.GetSolidBrush (ControlPaint.LightLight (backColor)), fill_rectangle);
  173. ControlPaint.DrawBorder (g, checkbox_rectangle, foreColor, ButtonBorderStyle.Solid);
  174. bounds.Offset (-1, 0);
  175. if (state == CheckState.Checked)
  176. DrawCheck (g, bounds, Color.Black);
  177. else if (state == CheckState.Indeterminate)
  178. DrawCheck (g, bounds, SystemColors.ControlDarkDark);
  179. }
  180. public virtual void DrawFlatHotCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  181. {
  182. Rectangle checkbox_rectangle;
  183. Rectangle fill_rectangle;
  184. // set up our rectangles first
  185. // clip two pixels from bottom right for non popup rendered checkboxes
  186. checkbox_rectangle = new Rectangle (bounds.X, bounds.Y, Math.Max (bounds.Width - 2, 0), Math.Max (bounds.Height - 2, 0));
  187. fill_rectangle = new Rectangle (checkbox_rectangle.X + 1, checkbox_rectangle.Y + 1, Math.Max (checkbox_rectangle.Width - 2, 0), Math.Max (checkbox_rectangle.Height - 2, 0));
  188. g.FillRectangle (ResPool.GetSolidBrush (backColor), fill_rectangle);
  189. ControlPaint.DrawBorder (g, checkbox_rectangle, foreColor, ButtonBorderStyle.Solid);
  190. bounds.Offset (-1, 0);
  191. if (state == CheckState.Checked)
  192. DrawCheck (g, bounds, Color.Black);
  193. else if (state == CheckState.Indeterminate)
  194. DrawCheck (g, bounds, SystemColors.ControlDarkDark);
  195. }
  196. public virtual void DrawFlatPressedCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  197. {
  198. DrawFlatNormalCheckBox (g, bounds, backColor, foreColor, state);
  199. }
  200. public virtual void DrawFlatDisabledCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  201. {
  202. Rectangle checkbox_rectangle;
  203. checkbox_rectangle = new Rectangle (bounds.X, bounds.Y, Math.Max (bounds.Width - 2, 0), Math.Max (bounds.Height - 2, 0));
  204. ControlPaint.DrawBorder (g, checkbox_rectangle, foreColor, ButtonBorderStyle.Solid);
  205. bounds.Offset (-1, 0);
  206. if (state == CheckState.Checked || state == CheckState.Indeterminate)
  207. DrawCheck (g, bounds, SystemColors.ControlDarkDark);
  208. }
  209. #endregion
  210. #region Popup
  211. public virtual void DrawPopupNormalCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  212. {
  213. DrawFlatNormalCheckBox (g, bounds, backColor, foreColor, state);
  214. }
  215. public virtual void DrawPopupHotCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  216. {
  217. Rectangle checkbox_rectangle;
  218. Rectangle fill_rectangle;
  219. // clip one pixel from bottom right for non popup rendered checkboxes
  220. checkbox_rectangle = new Rectangle (bounds.X, bounds.Y, Math.Max (bounds.Width - 1, 0), Math.Max (bounds.Height - 1, 0));
  221. fill_rectangle = new Rectangle (checkbox_rectangle.X + 1, checkbox_rectangle.Y + 1, Math.Max (checkbox_rectangle.Width - 3, 0), Math.Max (checkbox_rectangle.Height - 3, 0));
  222. g.FillRectangle (ResPool.GetSolidBrush (ControlPaint.LightLight (backColor)), fill_rectangle);
  223. // draw sunken effect
  224. ThemeEngine.Current.CPDrawBorder3D (g, checkbox_rectangle, Border3DStyle.SunkenInner, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, backColor);
  225. bounds.Offset (-1, 0);
  226. if (state == CheckState.Checked)
  227. DrawCheck (g, bounds, Color.Black);
  228. else if (state == CheckState.Indeterminate)
  229. DrawCheck (g, bounds, SystemColors.ControlDarkDark);
  230. }
  231. public virtual void DrawPopupPressedCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  232. {
  233. Rectangle checkbox_rectangle;
  234. Rectangle fill_rectangle;
  235. // clip one pixel from bottom right for non popup rendered checkboxes
  236. checkbox_rectangle = new Rectangle (bounds.X, bounds.Y, Math.Max (bounds.Width - 1, 0), Math.Max (bounds.Height - 1, 0));
  237. fill_rectangle = new Rectangle (checkbox_rectangle.X + 1, checkbox_rectangle.Y + 1, Math.Max (checkbox_rectangle.Width - 3, 0), Math.Max (checkbox_rectangle.Height - 3, 0));
  238. g.FillRectangle (ResPool.GetSolidBrush (backColor), fill_rectangle);
  239. // draw sunken effect
  240. ThemeEngine.Current.CPDrawBorder3D (g, checkbox_rectangle, Border3DStyle.SunkenInner, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom, backColor);
  241. bounds.Offset (-1, 0);
  242. if (state == CheckState.Checked)
  243. DrawCheck (g, bounds, Color.Black);
  244. else if (state == CheckState.Indeterminate)
  245. DrawCheck (g, bounds, SystemColors.ControlDarkDark);
  246. }
  247. public virtual void DrawPopupDisabledCheckBox (Graphics g, Rectangle bounds, Color backColor, Color foreColor, CheckState state)
  248. {
  249. DrawFlatDisabledCheckBox (g, bounds, backColor, foreColor, state);
  250. }
  251. #endregion
  252. #region Check
  253. public virtual void DrawCheck (Graphics g, Rectangle bounds, Color checkColor)
  254. {
  255. int check_size = (bounds.Height > bounds.Width) ? bounds.Width / 2 : bounds.Height / 2;
  256. Pen check_pen = ResPool.GetPen (checkColor);
  257. if (check_size < 7) {
  258. int lineWidth = Math.Max (3, check_size / 3);
  259. int Scale = Math.Max (1, check_size / 9);
  260. Rectangle rect = new Rectangle (bounds.X + (bounds.Width / 2) - (check_size / 2) - 1, bounds.Y + (bounds.Height / 2) - (check_size / 2) - 1,
  261. check_size, check_size);
  262. for (int i = 0; i < lineWidth; i++) {
  263. g.DrawLine (check_pen, rect.Left + lineWidth / 2, rect.Top + lineWidth + i, rect.Left + lineWidth / 2 + 2 * Scale, rect.Top + lineWidth + 2 * Scale + i);
  264. g.DrawLine (check_pen, rect.Left + lineWidth / 2 + 2 * Scale, rect.Top + lineWidth + 2 * Scale + i, rect.Left + lineWidth / 2 + 6 * Scale, rect.Top + lineWidth - 2 * Scale + i);
  265. }
  266. } else {
  267. int lineWidth = Math.Max (3, check_size / 3) + 1;
  268. int x_half = bounds.Width / 2;
  269. int y_half = bounds.Height / 2;
  270. Rectangle rect = new Rectangle (bounds.X + x_half - (check_size / 2) - 1, bounds.Y + y_half - (check_size / 2),
  271. check_size, check_size);
  272. int gradient_left = check_size / 3;
  273. int gradient_right = check_size - gradient_left - 1;
  274. for (int i = 0; i < lineWidth; i++) {
  275. g.DrawLine (check_pen, rect.X, rect.Bottom - 1 - gradient_left - i, rect.X + gradient_left, rect.Bottom - 1 - i);
  276. g.DrawLine (check_pen, rect.X + gradient_left, rect.Bottom - 1 - i, rect.Right - 1, rect.Bottom - i - 1 - gradient_right);
  277. }
  278. }
  279. }
  280. #endregion
  281. #region Private Methods
  282. private int Clamp (int value, int lower, int upper)
  283. {
  284. if (value < lower) return lower;
  285. else if (value > upper) return upper;
  286. else return value;
  287. }
  288. private Color ColorControl {
  289. get { return SystemColors.Control; }
  290. }
  291. #endregion
  292. }
  293. }