ButtonPainter.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // Andreia Gaita ([email protected])
  24. using System;
  25. using System.Drawing;
  26. namespace System.Windows.Forms.Theming.Default
  27. {
  28. /// <summary>
  29. /// Summary description for Button.
  30. /// </summary>
  31. internal class ButtonPainter
  32. {
  33. public ButtonPainter ()
  34. {
  35. }
  36. protected SystemResPool ResPool { get { return ThemeEngine.Current.ResPool; } }
  37. #region Buttons
  38. #region Standard Button
  39. public virtual void Draw (Graphics g, Rectangle bounds, ButtonThemeState state, Color backColor, Color foreColor) {
  40. bool is_themecolor = backColor.ToArgb () == ThemeEngine.Current.ColorControl.ToArgb () || backColor == Color.Empty ? true : false;
  41. CPColor cpcolor = is_themecolor ? CPColor.Empty : ResPool.GetCPColor (backColor);
  42. Pen pen;
  43. switch (state) {
  44. case ButtonThemeState.Normal:
  45. case ButtonThemeState.Entered:
  46. case ButtonThemeState.Disabled:
  47. pen = is_themecolor ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
  48. g.DrawLine (pen, bounds.X, bounds.Y, bounds.X, bounds.Bottom - 2);
  49. g.DrawLine (pen, bounds.X + 1, bounds.Y, bounds.Right - 2, bounds.Y);
  50. pen = is_themecolor ? SystemPens.Control : ResPool.GetPen (backColor);
  51. g.DrawLine (pen, bounds.X + 1, bounds.Y + 1, bounds.X + 1, bounds.Bottom - 3);
  52. g.DrawLine (pen, bounds.X + 2, bounds.Y + 1, bounds.Right - 3, bounds.Y + 1);
  53. pen = is_themecolor ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
  54. g.DrawLine (pen, bounds.X + 1, bounds.Bottom - 2, bounds.Right - 2, bounds.Bottom - 2);
  55. g.DrawLine (pen, bounds.Right - 2, bounds.Y + 1, bounds.Right - 2, bounds.Bottom - 3);
  56. pen = is_themecolor ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
  57. g.DrawLine (pen, bounds.X, bounds.Bottom - 1, bounds.Right - 1, bounds.Bottom - 1);
  58. g.DrawLine (pen, bounds.Right - 1, bounds.Y, bounds.Right - 1, bounds.Bottom - 2);
  59. break;
  60. case ButtonThemeState.Pressed:
  61. g.DrawRectangle (ResPool.GetPen (foreColor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
  62. bounds.Inflate (-1, -1);
  63. pen = is_themecolor ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
  64. g.DrawRectangle (pen, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
  65. break;
  66. case ButtonThemeState.Default:
  67. g.DrawRectangle (ResPool.GetPen (foreColor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
  68. bounds.Inflate (-1, -1);
  69. pen = is_themecolor ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
  70. g.DrawLine (pen, bounds.X, bounds.Y, bounds.X, bounds.Bottom - 2);
  71. g.DrawLine (pen, bounds.X + 1, bounds.Y, bounds.Right - 2, bounds.Y);
  72. pen = is_themecolor ? SystemPens.Control : ResPool.GetPen (backColor);
  73. g.DrawLine (pen, bounds.X + 1, bounds.Y + 1, bounds.X + 1, bounds.Bottom - 3);
  74. g.DrawLine (pen, bounds.X + 2, bounds.Y + 1, bounds.Right - 3, bounds.Y + 1);
  75. pen = is_themecolor ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
  76. g.DrawLine (pen, bounds.X + 1, bounds.Bottom - 2, bounds.Right - 2, bounds.Bottom - 2);
  77. g.DrawLine (pen, bounds.Right - 2, bounds.Y + 1, bounds.Right - 2, bounds.Bottom - 3);
  78. pen = is_themecolor ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
  79. g.DrawLine (pen, bounds.X, bounds.Bottom - 1, bounds.Right - 1, bounds.Bottom - 1);
  80. g.DrawLine (pen, bounds.Right - 1, bounds.Y, bounds.Right - 1, bounds.Bottom - 2);
  81. break;
  82. }
  83. }
  84. #endregion
  85. #region FlatStyle Button
  86. public virtual void DrawFlat (Graphics g, Rectangle bounds, ButtonThemeState state, Color backColor, Color foreColor, FlatButtonAppearance appearance) {
  87. bool is_themecolor = backColor.ToArgb () == ThemeEngine.Current.ColorControl.ToArgb () || backColor == Color.Empty ? true : false;
  88. CPColor cpcolor = is_themecolor ? CPColor.Empty : ResPool.GetCPColor (backColor);
  89. Pen pen;
  90. switch (state) {
  91. case ButtonThemeState.Normal:
  92. case ButtonThemeState.Disabled:
  93. // This will just use the BackColor
  94. break;
  95. case ButtonThemeState.Entered:
  96. case ButtonThemeState.Default | ButtonThemeState.Entered:
  97. if (appearance.MouseOverBackColor != Color.Empty)
  98. g.FillRectangle (ResPool.GetSolidBrush (appearance.MouseOverBackColor), bounds);
  99. else
  100. g.FillRectangle (ResPool.GetSolidBrush (ChangeIntensity (backColor, .9F)), bounds);
  101. break;
  102. case ButtonThemeState.Pressed:
  103. if (appearance.MouseDownBackColor != Color.Empty)
  104. g.FillRectangle (ResPool.GetSolidBrush (appearance.MouseDownBackColor), bounds);
  105. else
  106. g.FillRectangle (ResPool.GetSolidBrush (ChangeIntensity (backColor, .95F)), bounds);
  107. break;
  108. case ButtonThemeState.Default:
  109. if (appearance.CheckedBackColor != Color.Empty)
  110. g.FillRectangle (ResPool.GetSolidBrush (appearance.CheckedBackColor), bounds);
  111. break;
  112. }
  113. if (appearance.BorderColor == Color.Empty)
  114. pen = is_themecolor ? SystemPens.ControlDarkDark : ResPool.GetSizedPen (cpcolor.DarkDark, appearance.BorderSize);
  115. else
  116. pen = ResPool.GetSizedPen (appearance.BorderColor, appearance.BorderSize);
  117. bounds.Width -= 1;
  118. bounds.Height -= 1;
  119. if (appearance.BorderSize > 0)
  120. g.DrawRectangle (pen, bounds);
  121. }
  122. #endregion
  123. #region Popup Button
  124. public virtual void DrawPopup (Graphics g, Rectangle bounds, ButtonThemeState state, Color backColor, Color foreColor) {
  125. bool is_themecolor = backColor.ToArgb () == ThemeEngine.Current.ColorControl.ToArgb () || backColor == Color.Empty ? true : false;
  126. CPColor cpcolor = is_themecolor ? CPColor.Empty : ResPool.GetCPColor (backColor);
  127. Pen pen;
  128. switch (state) {
  129. case ButtonThemeState.Normal:
  130. case ButtonThemeState.Disabled:
  131. case ButtonThemeState.Pressed:
  132. case ButtonThemeState.Default:
  133. pen = is_themecolor ? SystemPens.ControlDarkDark : ResPool.GetPen (cpcolor.DarkDark);
  134. bounds.Width -= 1;
  135. bounds.Height -= 1;
  136. g.DrawRectangle (pen, bounds);
  137. if (state == ButtonThemeState.Default || state == ButtonThemeState.Pressed) {
  138. bounds.Inflate (-1, -1);
  139. g.DrawRectangle (pen, bounds);
  140. }
  141. break;
  142. case ButtonThemeState.Entered:
  143. pen = is_themecolor ? SystemPens.ControlLightLight : ResPool.GetPen (cpcolor.LightLight);
  144. g.DrawLine (pen, bounds.X, bounds.Y, bounds.X, bounds.Bottom - 2);
  145. g.DrawLine (pen, bounds.X + 1, bounds.Y, bounds.Right - 2, bounds.Y);
  146. pen = is_themecolor ? SystemPens.ControlDark : ResPool.GetPen (cpcolor.Dark);
  147. g.DrawLine (pen, bounds.X, bounds.Bottom - 1, bounds.Right - 1, bounds.Bottom - 1);
  148. g.DrawLine (pen, bounds.Right - 1, bounds.Y, bounds.Right - 1, bounds.Bottom - 2);
  149. break;
  150. }
  151. }
  152. #endregion
  153. #endregion
  154. private static Color ChangeIntensity (Color baseColor, float percent)
  155. {
  156. int H, I, S;
  157. ControlPaint.Color2HBS (baseColor, out H, out I, out S);
  158. int NewIntensity = Math.Min (255, (int)(I * percent));
  159. return ControlPaint.HBS2Color (H, NewIntensity, S);
  160. }
  161. }
  162. }