Theme support
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ControlPaint.cs
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    pbartok@novell.com
24 //
25 //
26 // $Revision: 1.2 $
27 // $Modtime: $
28 // $Log: ControlPaint.cs,v $
29 // Revision 1.2  2004/07/26 17:42:03  jordi
30 // Theme support
31 //
32 // Revision 1.1  2004/07/09 05:21:25  pbartok
33 // - Initial check-in
34 //
35 //
36
37 // NOT COMPLETE
38
39 using System.Drawing;
40 using System.Drawing.Drawing2D;
41 using System.Drawing.Imaging;
42
43 namespace System.Windows.Forms {
44         public sealed class ControlPaint {
45                 #region Local Variables
46                 static int              RGBMax=255;
47                 static int              HLSMax=255;
48                 #endregion      // Local Variables
49
50                 #region Private Enumerations
51
52
53
54                 #endregion      // Private Enumerations
55
56                 #region Helpers
57                 private static Color Win32ToColor(int Win32Color) {
58                         return(Color.FromArgb(
59                                 (int)(Win32Color) & 0xff0000 >> 16,             // blue
60                                 (int)(Win32Color) & 0xff00 >> 8,                // green
61                                 (int)(Win32Color) & 0xff                        // red
62                         ));
63                 }
64
65                 internal static void Color2HBS(Color color, out int h, out int l, out int s) {
66                         int     r;
67                         int     g;
68                         int     b;
69                         int     cMax;
70                         int     cMin;
71                         int     rDelta;
72                         int     gDelta;
73                         int     bDelta;
74
75                         r=color.R;
76                         g=color.G;
77                         b=color.B;
78
79                         cMax = Math.Max(Math.Max(r, g), b);
80                         cMin = Math.Min(Math.Min(r, g), b);
81
82                         l = (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax);
83
84                         if (cMax==cMin) {               // Achromatic
85                                 h=0;                                    // h undefined
86                                 s=0;
87                                 l=r;
88                                 return;
89                         }
90
91                         /* saturation */
92                         if (l<=(HLSMax/2)) {
93                                 s=(((cMax-cMin)*HLSMax)+((cMax+cMin)/2))/(cMax+cMin);
94                         } else {
95                                 s=(((cMax-cMin)*HLSMax)+((2*RGBMax-cMax-cMin)/2))/(2*RGBMax-cMax-cMin);
96                         }
97
98                         /* hue */
99                         rDelta=(((cMax-r)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
100                         gDelta=(((cMax-g)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
101                         bDelta=(((cMax-b)*(HLSMax/6))+((cMax-cMin)/2))/(cMax-cMin);
102
103                         if (r == cMax) {
104                                 h=bDelta - gDelta;
105                         } else if (g == cMax) {
106                                 h=(HLSMax/3) + rDelta - bDelta;
107                         } else { /* B == cMax */
108                                 h=((2*HLSMax)/3) + gDelta - rDelta;
109                         }
110
111                         if (h<0) {
112                                 h+=HLSMax;
113                         }
114
115                         if (h>HLSMax) {
116                                 h-=HLSMax;
117                         }
118                 }
119
120                 private static int HueToRGB(int n1, int n2, int hue) {
121                         if (hue<0) {
122                                 hue+=HLSMax;
123                         }
124
125                         if (hue>HLSMax) {
126                                 hue -= HLSMax;
127                         }
128
129                         /* return r,g, or b value from this tridrant */
130                         if (hue<(HLSMax/6)) {
131                                 return(n1+(((n2-n1)*hue+(HLSMax/12))/(HLSMax/6)));
132                         }
133
134                         if (hue<(HLSMax/2)) {
135                                 return(n2);
136                         }
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
145                 internal static Color HBS2Color(int hue, int lum, int sat) {
146                         int     R;
147                         int     G;
148                         int     B;
149                         int     Magic1;
150                         int     Magic2;
151
152                         if (sat == 0) {            /* Achromatic */
153                                 R=G=B=(lum*RGBMax)/HLSMax;
154                                 // FIXME : Should throw exception if hue!=0
155                         } else {
156                                 if (lum<=(HLSMax/2)) {
157                                         Magic2=(lum*(HLSMax+sat)+(HLSMax/2))/HLSMax;
158                                 } else {
159                                         Magic2=sat+lum-((sat*lum)+(HLSMax/2))/HLSMax;
160                                 }
161                                 Magic1=2*lum-Magic2;
162
163                                 R = Math.Min(255, (HueToRGB(Magic1,Magic2,hue+(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
164                                 G = Math.Min(255, (HueToRGB(Magic1,Magic2,hue)*RGBMax+(HLSMax/2))/HLSMax);
165                                 B = Math.Min(255, (HueToRGB(Magic1,Magic2,hue-(HLSMax/3))*RGBMax+(HLSMax/2))/HLSMax);
166                         }
167                         return(Color.FromArgb(R, G, B));
168                 }
169                 #endregion      // Helpers
170
171                 #region Public Static Properties
172                 public static Color ContrastControlDark {
173                         get { return(SystemColors.ControlDark); }
174                 }
175                 #endregion      // Public Static Properties
176
177                 #region Public Static Methods
178                 public static IntPtr CreateHBitmap16Bit(Bitmap bitmap, Color background){
179                         throw new NotImplementedException ();
180                 }
181
182                 public static IntPtr CreateHBitmapColorMask(Bitmap bitmap, IntPtr monochromeMask){
183                         throw new NotImplementedException ();
184                 }
185
186                 public static IntPtr CreateHBitmapTransparencyMask(Bitmap bitmap){
187                         throw new NotImplementedException ();
188                 }
189
190                 public static Color Light(Color baseColor) {
191                         return Light( baseColor, 10.0f);
192                 }
193
194                 public static Color Light(Color baseColor,float percOfLightLight) {
195                         int H, I, S;
196
197                         ControlPaint.Color2HBS(baseColor, out H, out I, out S);
198                         int NewIntensity = Math.Min( 255, I + ((255*(int)percOfLightLight)/100));
199                         return ControlPaint.HBS2Color(H, NewIntensity, S);
200                 }
201
202                 public static Color LightLight(Color baseColor) {
203                         return Light( baseColor, 20.0f);
204                 }
205
206                 public static Color Dark(Color baseColor) {
207                         return Dark(baseColor, 10.0f);
208                 }
209
210                 public static Color Dark(Color baseColor,float percOfDarkDark) {
211                         int H, I, S;
212                         ControlPaint.Color2HBS(baseColor, out H, out I, out S);
213                         int NewIntensity = Math.Max(0, I - ((255*(int)percOfDarkDark) / 100));
214                         return ControlPaint.HBS2Color(H, NewIntensity, S);
215                 }
216
217                 public static Color DarkDark(Color baseColor) {
218                         return Dark(baseColor, 20.0f);
219                 }
220
221                 public static void DrawBorder(Graphics graphics, Rectangle bounds, Color color, ButtonBorderStyle style) {
222                         DrawBorder(graphics, bounds, color, 1, style, color, 1, style, color, 1, style, color, 1, style);
223                 }
224
225                 public static void DrawBorder( Graphics graphics, Rectangle bounds, Color leftColor, int leftWidth,
226                         ButtonBorderStyle leftStyle, Color topColor, int topWidth, ButtonBorderStyle topStyle,
227                         Color rightColor, int rightWidth, ButtonBorderStyle rightStyle, Color bottomColor, int bottomWidth,
228                         ButtonBorderStyle bottomStyle) {
229
230                         ThemeEngine.Current.DrawBorder (graphics, bounds, leftColor, leftWidth,
231                                 leftStyle, topColor, topWidth, topStyle, rightColor, rightWidth, rightStyle,
232                                 bottomColor, bottomWidth, bottomStyle);
233                 }
234
235
236                 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle) {
237                         DrawBorder3D(graphics, rectangle, Border3DStyle.Etched, Border3DSide.All);
238                 }
239
240                 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle, Border3DStyle style) {
241                         DrawBorder3D(graphics, rectangle, style, Border3DSide.All);
242                 }
243
244                 public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height) {
245                         DrawBorder3D(graphics, new Rectangle(x, y, width, height), Border3DStyle.Etched, Border3DSide.All);
246                 }
247
248                 public static void DrawBorder3D(Graphics graphics, int x, int y, int width, int height, Border3DStyle style) {
249                         DrawBorder3D(graphics, new Rectangle(x, y, width, height), style, Border3DSide.All);
250                 }
251
252                 public static void DrawBorder3D( Graphics graphics, int x, int y, int width, int height, Border3DStyle style,Border3DSide sides) {
253                         DrawBorder3D( graphics, new Rectangle(x, y, width, height), style, sides);
254                 }
255
256                 public static void DrawBorder3D( Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
257
258                         ThemeEngine.Current.DrawBorder3D (graphics, rectangle, style, sides);
259                 }
260
261                 public static void DrawButton( Graphics graphics, int x, int y, int width, int height, ButtonState state) {
262                         DrawButton(graphics, new Rectangle(x, y, width, height), state);
263                 }
264
265                 public static void DrawButton( Graphics graphics, Rectangle rectangle, ButtonState state) {
266
267                         ThemeEngine.Current.DrawButton (graphics, rectangle, state);
268                 }
269
270
271                 public static void DrawCaptionButton(Graphics graphics, int x, int y, int width, int height, CaptionButton button, ButtonState state) {
272                         DrawCaptionButton(graphics, new Rectangle(x, y, width, height), button, state);
273                 }
274
275                 public static void DrawCaptionButton(Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
276
277                         ThemeEngine.Current.DrawCaptionButton (graphics, rectangle, button, state);
278                 }
279
280                 public static void DrawCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
281                         DrawCheckBox(graphics, new Rectangle(x, y, width, height), state);
282                 }
283
284                 public static void DrawCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
285
286                         ThemeEngine.Current.DrawCheckBox (graphics, rectangle, state);
287                 }
288
289                 public static void DrawComboButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
290
291                         ThemeEngine.Current.DrawComboButton (graphics, rectangle,  state);
292                 }
293
294                 public static void DrawComboButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
295                         DrawComboButton(graphics, new Rectangle(x, y, width, height), state);
296                 }
297
298                 public static void DrawContainerGrabHandle(Graphics graphics, Rectangle bounds) {
299
300                         ThemeEngine.Current.DrawContainerGrabHandle (graphics, bounds);
301                 }
302
303                 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle) {
304                         DrawFocusRectangle(graphics, rectangle, Color.White, Color.Black);
305                 }
306
307                 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) {
308
309                         ThemeEngine.Current.DrawFocusRectangle (graphics, rectangle, foreColor, backColor);
310                 }
311
312                 public static void DrawGrabHandle(Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
313
314                         ThemeEngine.Current.DrawGrabHandle (graphics, rectangle, primary, enabled);
315                 }
316
317                 public static void DrawGrid(Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
318
319                         ThemeEngine.Current.DrawGrid (graphics, area, pixelsBetweenDots, backColor);
320                 }
321
322                 public static void DrawImageDisabled(Graphics graphics, Image image, int x, int y, Color background) {
323
324                         ThemeEngine.Current.DrawImageDisabled (graphics, image, x, y, background);
325                 }
326
327                 public static void DrawLockedFrame(Graphics graphics, Rectangle rectangle, bool primary) {
328
329                         ThemeEngine.Current.DrawLockedFrame (graphics, rectangle, primary);
330                 }
331
332                 public static void DrawMenuGlyph(Graphics graphics, Rectangle rectangle, MenuGlyph glyph) {
333
334                         ThemeEngine.Current.DrawMenuGlyph (graphics, rectangle, glyph);
335                 }
336
337                 public static void DrawMenuGlyph(Graphics graphics, int x, int y, int width, int height, MenuGlyph glyph) {
338                         DrawMenuGlyph(graphics, new Rectangle(x, y, width, height), glyph);
339                 }
340
341                 public static void DrawMixedCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
342                         DrawCheckBox(graphics, rectangle, state);
343                 }
344
345                 public static void DrawMixedCheckBox(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
346                         DrawMixedCheckBox(graphics, new Rectangle(x, y, width, height), state);
347                 }
348
349
350                 public static void DrawRadioButton(Graphics graphics, int x, int y, int width, int height, ButtonState state) {
351                         DrawRadioButton(graphics, new Rectangle(x, y, width, height), state);
352                 }
353
354                 public static void DrawRadioButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
355
356                         ThemeEngine.Current.DrawRadioButton (graphics, rectangle, state);
357                 }
358
359                 [MonoTODO]
360                 public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style) {
361                         //FIXME:
362                 }
363
364                 [MonoTODO]
365                 public static void DrawReversibleLine(Point start, Point end, Color backColor) {
366                         //FIXME:
367                 }
368
369                 [MonoTODO]
370                 public static void FillReversibleRectangle(Rectangle rectangle, Color backColor) {
371                         //FIXME:
372                 }
373
374
375                 public static void DrawScrollButton (Graphics graphics, Rectangle rectangle, ScrollButton button, ButtonState state) {
376
377                         ThemeEngine.Current.DrawScrollButton (graphics, rectangle, button, state);
378                 }
379
380                 public static void DrawSizeGrip (Graphics graphics, Color backColor, Rectangle bounds)
381                 {
382                         ThemeEngine.Current.DrawSizeGrip (graphics,  backColor,  bounds);
383                 }
384
385                 public static void DrawSizeGrip(Graphics graphics, Color backColor, int x, int y, int width, int height) {
386                         DrawSizeGrip(graphics, backColor, new Rectangle(x, y, width, height));
387                 }
388
389                 public static void DrawStringDisabled(Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format) {
390
391                         ThemeEngine.Current.DrawStringDisabled (graphics, s, font, color, layoutRectangle, format);
392                 }
393                 #endregion      // Public Static Methods
394         }
395 }