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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004 Novell, Inc.
23 // Peter Bartok pbartok@novell.com
26 // $Log: ControlPaint.cs,v $
27 // Revision 1.4 2004/09/28 18:44:25 pbartok
28 // - Streamlined Theme interfaces:
29 // * Each DrawXXX method for a control now is passed the object for the
30 // control to be drawn in order to allow accessing any state the theme
33 // * ControlPaint methods for the theme now have a CP prefix to avoid
34 // name clashes with the Draw methods for controls
36 // * Every control now retrieves it's DefaultSize from the current theme
38 // Revision 1.3 2004/08/11 22:20:59 pbartok
41 // Revision 1.2 2004/07/26 17:42:03 jordi
44 // Revision 1.1 2004/07/09 05:21:25 pbartok
52 using System.Drawing.Drawing2D;
53 using System.Drawing.Imaging;
55 namespace System.Windows.Forms {
56 public sealed class ControlPaint {
57 #region Local Variables
58 static int RGBMax=255;
59 static int HLSMax=255;
60 #endregion // Local Variables
62 #region Private Enumerations
66 // Prevent a public constructor from being created
67 private ControlPaint() {
69 #endregion // Constructor
72 #endregion // Private Enumerations
75 private static Color Win32ToColor(int Win32Color) {
76 return(Color.FromArgb(
77 (int)(Win32Color) & 0xff0000 >> 16, // blue
78 (int)(Win32Color) & 0xff00 >> 8, // green
79 (int)(Win32Color) & 0xff // red
83 internal static void Color2HBS(Color color, out int h, out int l, out int s) {
97 cMax = Math.Max(Math.Max(r, g), b);
98 cMin = Math.Min(Math.Min(r, g), b);
100 l = (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax);
102 if (cMax==cMin) { // Achromatic
111 s=(((cMax-cMin)*HLSMax)+((cMax+cMin)/2))/(cMax+cMin);
113 s=(((cMax-cMin)*HLSMax)+((2*RGBMax-cMax-cMin)/2))/(2*RGBMax-cMax-cMin);
117 rDelta=(((cMax-r)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
118 gDelta=(((cMax-g)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
119 bDelta=(((cMax-b)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
123 } else if (g == cMax) {
124 h=(HLSMax/3) + rDelta - bDelta;
125 } else { /* B == cMax */
126 h=((2*HLSMax)/3) + gDelta - rDelta;
138 private static int HueToRGB(int n1, int n2, int hue) {
147 /* return r,g, or b value from this tridrant */
148 if (hue<(HLSMax/6)) {
149 return(n1+(((n2-n1)*hue+(HLSMax/12))/(HLSMax/6)));
152 if (hue<(HLSMax/2)) {
156 if (hue<((HLSMax*2)/3)) {
157 return(n1+(((n2-n1)*(((HLSMax*2)/3)-hue)+(HLSMax/12))/(HLSMax/6)));
163 internal static Color HBS2Color(int hue, int lum, int sat) {
170 if (sat == 0) { /* Achromatic */
171 R=G=B=(lum*RGBMax)/HLSMax;
172 // FIXME : Should throw exception if hue!=0
174 if (lum<=(HLSMax/2)) {
175 Magic2=(lum*(HLSMax+sat)+(HLSMax/2))/HLSMax;
177 Magic2=sat+lum-((sat*lum)+(HLSMax/2))/HLSMax;
181 R = Math.Min(255, (HueToRGB(Magic1,Magic2,hue+(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
182 G = Math.Min(255, (HueToRGB(Magic1,Magic2,hue)*RGBMax+(HLSMax/2))/HLSMax);
183 B = Math.Min(255, (HueToRGB(Magic1,Magic2,hue-(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
185 return(Color.FromArgb(R, G, B));
187 #endregion // Helpers
189 #region Public Static Properties
190 public static Color ContrastControlDark {
191 get { return(SystemColors.ControlDark); }
193 #endregion // Public Static Properties
195 #region Public Static Methods
196 public static IntPtr CreateHBitmap16Bit(Bitmap bitmap, Color background){
197 throw new NotImplementedException ();
200 public static IntPtr CreateHBitmapColorMask(Bitmap bitmap, IntPtr monochromeMask){
201 throw new NotImplementedException ();
204 public static IntPtr CreateHBitmapTransparencyMask(Bitmap bitmap){
205 throw new NotImplementedException ();
208 public static Color Light(Color baseColor) {
209 return Light( baseColor, 10.0f);
212 public static Color Light(Color baseColor,float percOfLightLight) {
215 ControlPaint.Color2HBS(baseColor, out H, out I, out S);
216 int NewIntensity = Math.Min( 255, I + ((255*(int)percOfLightLight)/100));
217 return ControlPaint.HBS2Color(H, NewIntensity, S);
220 public static Color LightLight(Color baseColor) {
221 return Light( baseColor, 20.0f);
224 public static Color Dark(Color baseColor) {
225 return Dark(baseColor, 10.0f);
228 public static Color Dark(Color baseColor,float percOfDarkDark) {
230 ControlPaint.Color2HBS(baseColor, out H, out I, out S);
231 int NewIntensity = Math.Max(0, I - ((255*(int)percOfDarkDark) / 100));
232 return ControlPaint.HBS2Color(H, NewIntensity, S);
235 public static Color DarkDark(Color baseColor) {
236 return Dark(baseColor, 20.0f);
239 public static void DrawBorder(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style) {
240 DrawBorder(graphics, bounds, color, 1, style, color, 1, style, color, 1, style, color, 1, style);
243 public static void DrawBorder( Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
244 ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
245 Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor, int bottomWidth,
246 ButtonBorderStyle bottomStyle) {
248 ThemeEngine.Current.CPDrawBorder (graphics, bounds, leftColor, leftWidth,
249 leftStyle, topColor, topWidth, topStyle, rightColor, rightWidth, rightStyle,
250 bottomColor, bottomWidth, bottomStyle);
254 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle) {
255 DrawBorder3D(graphics, rectangle, Border3DStyle.Etched, Border3DSide.All);
258 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle, Border3DStyle style) {
259 DrawBorder3D(graphics, rectangle, style, Border3DSide.All);
262 public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height) {
263 DrawBorder3D(graphics, new Rectangle(x, y, width, height), Border3DStyle.Etched, Border3DSide.All);
266 public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height, Border3DStyle style) {
267 DrawBorder3D(graphics, new Rectangle(x, y, width, height), style, Border3DSide.All);
270 public static void DrawBorder3D( Graphics graphics, int x, int y, int width, int height, Border3DStyle style,Border3DSide sides) {
271 DrawBorder3D( graphics, new Rectangle(x, y, width, height), style, sides);
274 public static void DrawBorder3D( Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
276 ThemeEngine.Current.CPDrawBorder3D (graphics, rectangle, style, sides);
279 public static void DrawButton( Graphics graphics, int x, int y, int width, int height, ButtonState state) {
280 DrawButton(graphics, new Rectangle(x, y, width, height), state);
283 public static void DrawButton( Graphics graphics, Rectangle rectangle, ButtonState state) {
285 ThemeEngine.Current.CPDrawButton (graphics, rectangle, state);
289 public static void DrawCaptionButton(Graphics graphics, int x, int y, int width, int height, CaptionButton button, ButtonState state) {
290 DrawCaptionButton(graphics, new Rectangle(x, y, width, height), button, state);
293 public static void DrawCaptionButton(Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
295 ThemeEngine.Current.CPDrawCaptionButton (graphics, rectangle, button, state);
298 public static void DrawCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
299 DrawCheckBox(graphics, new Rectangle(x, y, width, height), state);
302 public static void DrawCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
304 ThemeEngine.Current.CPDrawCheckBox (graphics, rectangle, state);
307 public static void DrawComboButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
309 ThemeEngine.Current.CPDrawComboButton (graphics, rectangle, state);
312 public static void DrawComboButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
313 DrawComboButton(graphics, new Rectangle(x, y, width, height), state);
316 public static void DrawContainerGrabHandle(Graphics graphics, Rectangle bounds) {
318 ThemeEngine.Current.CPDrawContainerGrabHandle (graphics, bounds);
321 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle) {
322 DrawFocusRectangle(graphics, rectangle, Color.White, Color.Black);
325 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) {
327 ThemeEngine.Current.CPDrawFocusRectangle (graphics, rectangle, foreColor, backColor);
330 public static void DrawGrabHandle(Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
332 ThemeEngine.Current.CPDrawGrabHandle (graphics, rectangle, primary, enabled);
335 public static void DrawGrid(Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
337 ThemeEngine.Current.CPDrawGrid (graphics, area, pixelsBetweenDots, backColor);
340 public static void DrawImageDisabled(Graphics graphics, Image image, int x, int y, Color background) {
342 ThemeEngine.Current.CPDrawImageDisabled (graphics, image, x, y, background);
345 public static void DrawLockedFrame(Graphics graphics, Rectangle rectangle, bool primary) {
347 ThemeEngine.Current.CPDrawLockedFrame (graphics, rectangle, primary);
350 public static void DrawMenuGlyph(Graphics graphics, Rectangle rectangle, MenuGlyph glyph) {
352 ThemeEngine.Current.CPDrawMenuGlyph (graphics, rectangle, glyph);
355 public static void DrawMenuGlyph(Graphics graphics, int x, int y, int width, int height, MenuGlyph glyph) {
356 DrawMenuGlyph(graphics, new Rectangle(x, y, width, height), glyph);
359 public static void DrawMixedCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
360 DrawCheckBox(graphics, rectangle, state);
363 public static void DrawMixedCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
364 DrawMixedCheckBox(graphics, new Rectangle(x, y, width, height), state);
368 public static void DrawRadioButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
369 DrawRadioButton(graphics, new Rectangle(x, y, width, height), state);
372 public static void DrawRadioButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
374 ThemeEngine.Current.CPDrawRadioButton (graphics, rectangle, state);
377 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
378 public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style) {
379 throw new NotImplementedException();
382 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
383 public static void DrawReversibleLine(Point start, Point end, Color backColor) {
384 throw new NotImplementedException();
387 [MonoTODO("Figure out a good System.Drawing way for XOR drawing")]
388 public static void FillReversibleRectangle(Rectangle rectangle, Color backColor) {
389 throw new NotImplementedException();
393 public static void DrawScrollButton (Graphics graphics, int x, int y, int width, int height, ScrollButton button, ButtonState state) {
394 ThemeEngine.Current.CPDrawScrollButton (graphics, new Rectangle(x, y, width, height), button, state);
397 public static void DrawScrollButton (Graphics graphics, Rectangle rectangle, ScrollButton button, ButtonState state) {
398 ThemeEngine.Current.CPDrawScrollButton (graphics, rectangle, button, state);
402 public static void DrawSelectionFrame(Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect, Color backColor) {
403 throw new NotImplementedException();
406 public static void DrawSizeGrip (Graphics graphics, Color backColor, Rectangle bounds)
408 ThemeEngine.Current.CPDrawSizeGrip (graphics, backColor, bounds);
411 public static void DrawSizeGrip(Graphics graphics, Color backColor, int x, int y, int width, int height) {
412 DrawSizeGrip(graphics, backColor, new Rectangle(x, y, width, height));
415 public static void DrawStringDisabled(Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format) {
417 ThemeEngine.Current.CPDrawStringDisabled (graphics, s, font, color, layoutRectangle, format);
419 #endregion // Public Static Methods