ControlPaint.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. //
  26. // $Log: ControlPaint.cs,v $
  27. // Revision 1.5 2004/10/13 03:15:16 pbartok
  28. // - Fixed Dark(), DarkDark(), Light() and LightLight() methods to match MS
  29. // documentation. They need to return defined colors if the passed
  30. // color matches the configured control color. Thanks to John BouAntoun for
  31. // pointing this out.
  32. //
  33. // Revision 1.4 2004/09/28 18:44:25 pbartok
  34. // - Streamlined Theme interfaces:
  35. // * Each DrawXXX method for a control now is passed the object for the
  36. // control to be drawn in order to allow accessing any state the theme
  37. // might require
  38. //
  39. // * ControlPaint methods for the theme now have a CP prefix to avoid
  40. // name clashes with the Draw methods for controls
  41. //
  42. // * Every control now retrieves it's DefaultSize from the current theme
  43. //
  44. // Revision 1.3 2004/08/11 22:20:59 pbartok
  45. // - Signature fixes
  46. //
  47. // Revision 1.2 2004/07/26 17:42:03 jordi
  48. // Theme support
  49. //
  50. // Revision 1.1 2004/07/09 05:21:25 pbartok
  51. // - Initial check-in
  52. //
  53. //
  54. // NOT COMPLETE
  55. using System.Drawing;
  56. using System.Drawing.Drawing2D;
  57. using System.Drawing.Imaging;
  58. namespace System.Windows.Forms {
  59. public sealed class ControlPaint {
  60. #region Local Variables
  61. static int RGBMax=255;
  62. static int HLSMax=255;
  63. #endregion // Local Variables
  64. #region Private Enumerations
  65. #region Constructor
  66. // Prevent a public constructor from being created
  67. private ControlPaint() {
  68. }
  69. #endregion // Constructor
  70. #endregion // Private Enumerations
  71. #region Helpers
  72. private static Color Win32ToColor(int Win32Color) {
  73. return(Color.FromArgb(
  74. (int)(Win32Color) & 0xff0000 >> 16, // blue
  75. (int)(Win32Color) & 0xff00 >> 8, // green
  76. (int)(Win32Color) & 0xff // red
  77. ));
  78. }
  79. internal static void Color2HBS(Color color, out int h, out int l, out int s) {
  80. int r;
  81. int g;
  82. int b;
  83. int cMax;
  84. int cMin;
  85. int rDelta;
  86. int gDelta;
  87. int bDelta;
  88. r=color.R;
  89. g=color.G;
  90. b=color.B;
  91. cMax = Math.Max(Math.Max(r, g), b);
  92. cMin = Math.Min(Math.Min(r, g), b);
  93. l = (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax);
  94. if (cMax==cMin) { // Achromatic
  95. h=0; // h undefined
  96. s=0;
  97. l=r;
  98. return;
  99. }
  100. /* saturation */
  101. if (l<=(HLSMax/2)) {
  102. s=(((cMax-cMin)*HLSMax)+((cMax+cMin)/2))/(cMax+cMin);
  103. } else {
  104. s=(((cMax-cMin)*HLSMax)+((2*RGBMax-cMax-cMin)/2))/(2*RGBMax-cMax-cMin);
  105. }
  106. /* hue */
  107. rDelta=(((cMax-r)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
  108. gDelta=(((cMax-g)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
  109. bDelta=(((cMax-b)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
  110. if (r == cMax) {
  111. h=bDelta - gDelta;
  112. } else if (g == cMax) {
  113. h=(HLSMax/3) + rDelta - bDelta;
  114. } else { /* B == cMax */
  115. h=((2*HLSMax)/3) + gDelta - rDelta;
  116. }
  117. if (h<0) {
  118. h+=HLSMax;
  119. }
  120. if (h>HLSMax) {
  121. h-=HLSMax;
  122. }
  123. }
  124. private static int HueToRGB(int n1, int n2, int hue) {
  125. if (hue<0) {
  126. hue+=HLSMax;
  127. }
  128. if (hue>HLSMax) {
  129. hue -= HLSMax;
  130. }
  131. /* return r,g, or b value from this tridrant */
  132. if (hue<(HLSMax/6)) {
  133. return(n1+(((n2-n1)*hue+(HLSMax/12))/(HLSMax/6)));
  134. }
  135. if (hue<(HLSMax/2)) {
  136. return(n2);
  137. }
  138. if (hue<((HLSMax*2)/3)) {
  139. return(n1+(((n2-n1)*(((HLSMax*2)/3)-hue)+(HLSMax/12))/(HLSMax/6)));
  140. } else {
  141. return(n1);
  142. }
  143. }
  144. internal static Color HBS2Color(int hue, int lum, int sat) {
  145. int R;
  146. int G;
  147. int B;
  148. int Magic1;
  149. int Magic2;
  150. if (sat == 0) { /* Achromatic */
  151. R=G=B=(lum*RGBMax)/HLSMax;
  152. // FIXME : Should throw exception if hue!=0
  153. } else {
  154. if (lum<=(HLSMax/2)) {
  155. Magic2=(lum*(HLSMax+sat)+(HLSMax/2))/HLSMax;
  156. } else {
  157. Magic2=sat+lum-((sat*lum)+(HLSMax/2))/HLSMax;
  158. }
  159. Magic1=2*lum-Magic2;
  160. R = Math.Min(255, (HueToRGB(Magic1,Magic2,hue+(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
  161. G = Math.Min(255, (HueToRGB(Magic1,Magic2,hue)*RGBMax+(HLSMax/2))/HLSMax);
  162. B = Math.Min(255, (HueToRGB(Magic1,Magic2,hue-(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
  163. }
  164. return(Color.FromArgb(R, G, B));
  165. }
  166. #endregion // Helpers
  167. #region Public Static Properties
  168. public static Color ContrastControlDark {
  169. get { return(SystemColors.ControlDark); }
  170. }
  171. #endregion // Public Static Properties
  172. #region Public Static Methods
  173. public static IntPtr CreateHBitmap16Bit(Bitmap bitmap, Color background){
  174. throw new NotImplementedException ();
  175. }
  176. public static IntPtr CreateHBitmapColorMask(Bitmap bitmap, IntPtr monochromeMask){
  177. throw new NotImplementedException ();
  178. }
  179. public static IntPtr CreateHBitmapTransparencyMask(Bitmap bitmap){
  180. throw new NotImplementedException ();
  181. }
  182. public static Color Light(Color baseColor) {
  183. if (baseColor == ThemeEngine.Current.ColorButtonFace) {
  184. return ThemeEngine.Current.ColorButtonLight;
  185. }
  186. return Light( baseColor, 10.0f);
  187. }
  188. public static Color Light(Color baseColor,float percOfLightLight) {
  189. int H, I, S;
  190. ControlPaint.Color2HBS(baseColor, out H, out I, out S);
  191. int NewIntensity = Math.Min( 255, I + ((255*(int)percOfLightLight)/100));
  192. return ControlPaint.HBS2Color(H, NewIntensity, S);
  193. }
  194. public static Color LightLight(Color baseColor) {
  195. if (baseColor == ThemeEngine.Current.ColorButtonFace) {
  196. return ThemeEngine.Current.ColorButtonHilight;
  197. }
  198. return Light( baseColor, 20.0f);
  199. }
  200. public static Color Dark(Color baseColor) {
  201. if (baseColor == ThemeEngine.Current.ColorButtonFace) {
  202. return ThemeEngine.Current.ColorButtonShadow;
  203. }
  204. return Dark(baseColor, 10.0f);
  205. }
  206. public static Color Dark(Color baseColor,float percOfDarkDark) {
  207. int H, I, S;
  208. ControlPaint.Color2HBS(baseColor, out H, out I, out S);
  209. int NewIntensity = Math.Max(0, I - ((255*(int)percOfDarkDark) / 100));
  210. return ControlPaint.HBS2Color(H, NewIntensity, S);
  211. }
  212. public static Color DarkDark(Color baseColor) {
  213. if (baseColor == ThemeEngine.Current.ColorButtonFace) {
  214. return ThemeEngine.Current.ColorButtonDkShadow;
  215. }
  216. return Dark(baseColor, 20.0f);
  217. }
  218. public static void DrawBorder(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style) {
  219. DrawBorder(graphics, bounds, color, 1, style, color, 1, style, color, 1, style, color, 1, style);
  220. }
  221. public static void DrawBorder( Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
  222. ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
  223. Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor, int bottomWidth,
  224. ButtonBorderStyle bottomStyle) {
  225. ThemeEngine.Current.CPDrawBorder (graphics, bounds, leftColor, leftWidth,
  226. leftStyle, topColor, topWidth, topStyle, rightColor, rightWidth, rightStyle,
  227. bottomColor, bottomWidth, bottomStyle);
  228. }
  229. public static void DrawBorder3D(Graphics graphics, Rectangle rectangle) {
  230. DrawBorder3D(graphics, rectangle, Border3DStyle.Etched, Border3DSide.All);
  231. }
  232. public static void DrawBorder3D(Graphics graphics, Rectangle rectangle, Border3DStyle style) {
  233. DrawBorder3D(graphics, rectangle, style, Border3DSide.All);
  234. }
  235. public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height) {
  236. DrawBorder3D(graphics, new Rectangle(x, y, width, height), Border3DStyle.Etched, Border3DSide.All);
  237. }
  238. public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height, Border3DStyle style) {
  239. DrawBorder3D(graphics, new Rectangle(x, y, width, height), style, Border3DSide.All);
  240. }
  241. public static void DrawBorder3D( Graphics graphics, int x, int y, int width, int height, Border3DStyle style,Border3DSide sides) {
  242. DrawBorder3D( graphics, new Rectangle(x, y, width, height), style, sides);
  243. }
  244. public static void DrawBorder3D( Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
  245. ThemeEngine.Current.CPDrawBorder3D (graphics, rectangle, style, sides);
  246. }
  247. public static void DrawButton( Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  248. DrawButton(graphics, new Rectangle(x, y, width, height), state);
  249. }
  250. public static void DrawButton( Graphics graphics, Rectangle rectangle, ButtonState state) {
  251. ThemeEngine.Current.CPDrawButton (graphics, rectangle, state);
  252. }
  253. public static void DrawCaptionButton(Graphics graphics, int x, int y, int width, int height, CaptionButton button, ButtonState state) {
  254. DrawCaptionButton(graphics, new Rectangle(x, y, width, height), button, state);
  255. }
  256. public static void DrawCaptionButton(Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
  257. ThemeEngine.Current.CPDrawCaptionButton (graphics, rectangle, button, state);
  258. }
  259. public static void DrawCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  260. DrawCheckBox(graphics, new Rectangle(x, y, width, height), state);
  261. }
  262. public static void DrawCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
  263. ThemeEngine.Current.CPDrawCheckBox (graphics, rectangle, state);
  264. }
  265. public static void DrawComboButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
  266. ThemeEngine.Current.CPDrawComboButton (graphics, rectangle, state);
  267. }
  268. public static void DrawComboButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  269. DrawComboButton(graphics, new Rectangle(x, y, width, height), state);
  270. }
  271. public static void DrawContainerGrabHandle(Graphics graphics, Rectangle bounds) {
  272. ThemeEngine.Current.CPDrawContainerGrabHandle (graphics, bounds);
  273. }
  274. public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle) {
  275. DrawFocusRectangle(graphics, rectangle, Color.White, Color.Black);
  276. }
  277. public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) {
  278. ThemeEngine.Current.CPDrawFocusRectangle (graphics, rectangle, foreColor, backColor);
  279. }
  280. public static void DrawGrabHandle(Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
  281. ThemeEngine.Current.CPDrawGrabHandle (graphics, rectangle, primary, enabled);
  282. }
  283. public static void DrawGrid(Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
  284. ThemeEngine.Current.CPDrawGrid (graphics, area, pixelsBetweenDots, backColor);
  285. }
  286. public static void DrawImageDisabled(Graphics graphics, Image image, int x, int y, Color background) {
  287. ThemeEngine.Current.CPDrawImageDisabled (graphics, image, x, y, background);
  288. }
  289. public static void DrawLockedFrame(Graphics graphics, Rectangle rectangle, bool primary) {
  290. ThemeEngine.Current.CPDrawLockedFrame (graphics, rectangle, primary);
  291. }
  292. public static void DrawMenuGlyph(Graphics graphics, Rectangle rectangle, MenuGlyph glyph) {
  293. ThemeEngine.Current.CPDrawMenuGlyph (graphics, rectangle, glyph);
  294. }
  295. public static void DrawMenuGlyph(Graphics graphics, int x, int y, int width, int height, MenuGlyph glyph) {
  296. DrawMenuGlyph(graphics, new Rectangle(x, y, width, height), glyph);
  297. }
  298. public static void DrawMixedCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
  299. DrawCheckBox(graphics, rectangle, state);
  300. }
  301. public static void DrawMixedCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  302. DrawMixedCheckBox(graphics, new Rectangle(x, y, width, height), state);
  303. }
  304. public static void DrawRadioButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
  305. DrawRadioButton(graphics, new Rectangle(x, y, width, height), state);
  306. }
  307. public static void DrawRadioButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
  308. ThemeEngine.Current.CPDrawRadioButton (graphics, rectangle, state);
  309. }
  310. [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
  311. public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style) {
  312. throw new NotImplementedException();
  313. }
  314. [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
  315. public static void DrawReversibleLine(Point start, Point end, Color backColor) {
  316. throw new NotImplementedException();
  317. }
  318. [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
  319. public static void FillReversibleRectangle(Rectangle rectangle, Color backColor) {
  320. throw new NotImplementedException();
  321. }
  322. public static void DrawScrollButton (Graphics graphics, int x, int y, int width, int height, ScrollButton button, ButtonState state) {
  323. ThemeEngine.Current.CPDrawScrollButton (graphics, new Rectangle(x, y, width, height), button, state);
  324. }
  325. public static void DrawScrollButton (Graphics graphics, Rectangle rectangle, ScrollButton button, ButtonState state) {
  326. ThemeEngine.Current.CPDrawScrollButton (graphics, rectangle, button, state);
  327. }
  328. [MonoTODO]
  329. public static void DrawSelectionFrame(Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect, Color backColor) {
  330. throw new NotImplementedException();
  331. }
  332. public static void DrawSizeGrip (Graphics graphics, Color backColor, Rectangle bounds)
  333. {
  334. ThemeEngine.Current.CPDrawSizeGrip (graphics, backColor, bounds);
  335. }
  336. public static void DrawSizeGrip(Graphics graphics, Color backColor, int x, int y, int width, int height) {
  337. DrawSizeGrip(graphics, backColor, new Rectangle(x, y, width, height));
  338. }
  339. public static void DrawStringDisabled(Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format) {
  340. ThemeEngine.Current.CPDrawStringDisabled (graphics, s, font, color, layoutRectangle, format);
  341. }
  342. #endregion // Public Static Methods
  343. }
  344. }