- Streamlined Theme interfaces:
[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 // $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
31 //     might require
32 //
33 //   * ControlPaint methods for the theme now have a CP prefix to avoid
34 //     name clashes with the Draw methods for controls
35 //
36 //   * Every control now retrieves it's DefaultSize from the current theme
37 //
38 // Revision 1.3  2004/08/11 22:20:59  pbartok
39 // - Signature fixes
40 //
41 // Revision 1.2  2004/07/26 17:42:03  jordi
42 // Theme support
43 //
44 // Revision 1.1  2004/07/09 05:21:25  pbartok
45 // - Initial check-in
46 //
47 //
48
49 // NOT COMPLETE
50
51 using System.Drawing;
52 using System.Drawing.Drawing2D;
53 using System.Drawing.Imaging;
54
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
61
62                 #region Private Enumerations
63
64
65                 #region Constructor
66                 // Prevent a public constructor from being created
67                 private ControlPaint() {
68                 }
69                 #endregion      // Constructor
70
71
72                 #endregion      // Private Enumerations
73
74                 #region Helpers
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
80                         ));
81                 }
82
83                 internal static void Color2HBS(Color color, out int h, out int l, out int s) {
84                         int     r;
85                         int     g;
86                         int     b;
87                         int     cMax;
88                         int     cMin;
89                         int     rDelta;
90                         int     gDelta;
91                         int     bDelta;
92
93                         r=color.R;
94                         g=color.G;
95                         b=color.B;
96
97                         cMax = Math.Max(Math.Max(r, g), b);
98                         cMin = Math.Min(Math.Min(r, g), b);
99
100                         l = (((cMax+cMin)*HLSMax)+RGBMax)/(2*RGBMax);
101
102                         if (cMax==cMin) {               // Achromatic
103                                 h=0;                                    // h undefined
104                                 s=0;
105                                 l=r;
106                                 return;
107                         }
108
109                         /* saturation */
110                         if (l<=(HLSMax/2)) {
111                                 s=(((cMax-cMin)*HLSMax)+((cMax+cMin)/2))/(cMax+cMin);
112                         } else {
113                                 s=(((cMax-cMin)*HLSMax)+((2*RGBMax-cMax-cMin)/2))/(2*RGBMax-cMax-cMin);
114                         }
115
116                         /* hue */
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);
120
121                         if (r == cMax) {
122                                 h=bDelta - gDelta;
123                         } else if (g == cMax) {
124                                 h=(HLSMax/3) + rDelta - bDelta;
125                         } else { /* B == cMax */
126                                 h=((2*HLSMax)/3) + gDelta - rDelta;
127                         }
128
129                         if (h<0) {
130                                 h+=HLSMax;
131                         }
132
133                         if (h>HLSMax) {
134                                 h-=HLSMax;
135                         }
136                 }
137
138                 private static int HueToRGB(int n1, int n2, int hue) {
139                         if (hue<0) {
140                                 hue+=HLSMax;
141                         }
142
143                         if (hue>HLSMax) {
144                                 hue -= HLSMax;
145                         }
146
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)));
150                         }
151
152                         if (hue<(HLSMax/2)) {
153                                 return(n2);
154                         }
155
156                         if (hue<((HLSMax*2)/3)) {
157                                 return(n1+(((n2-n1)*(((HLSMax*2)/3)-hue)+(HLSMax/12))/(HLSMax/6)));
158                         } else {
159                                 return(n1);
160                         }
161                 }
162
163                 internal static Color HBS2Color(int hue, int lum, int sat) {
164                         int     R;
165                         int     G;
166                         int     B;
167                         int     Magic1;
168                         int     Magic2;
169
170                         if (sat == 0) {            /* Achromatic */
171                                 R=G=B=(lum*RGBMax)/HLSMax;
172                                 // FIXME : Should throw exception if hue!=0
173                         } else {
174                                 if (lum<=(HLSMax/2)) {
175                                         Magic2=(lum*(HLSMax+sat)+(HLSMax/2))/HLSMax;
176                                 } else {
177                                         Magic2=sat+lum-((sat*lum)+(HLSMax/2))/HLSMax;
178                                 }
179                                 Magic1=2*lum-Magic2;
180
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);
184                         }
185                         return(Color.FromArgb(R, G, B));
186                 }
187                 #endregion      // Helpers
188
189                 #region Public Static Properties
190                 public static Color ContrastControlDark {
191                         get { return(SystemColors.ControlDark); }
192                 }
193                 #endregion      // Public Static Properties
194
195                 #region Public Static Methods
196                 public static IntPtr CreateHBitmap16Bit(Bitmap bitmap, Color background){
197                         throw new NotImplementedException ();
198                 }
199
200                 public static IntPtr CreateHBitmapColorMask(Bitmap bitmap, IntPtr monochromeMask){
201                         throw new NotImplementedException ();
202                 }
203
204                 public static IntPtr CreateHBitmapTransparencyMask(Bitmap bitmap){
205                         throw new NotImplementedException ();
206                 }
207
208                 public static Color Light(Color baseColor) {
209                         return Light( baseColor, 10.0f);
210                 }
211
212                 public static Color Light(Color baseColor,float percOfLightLight) {
213                         int H, I, S;
214
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);
218                 }
219
220                 public static Color LightLight(Color baseColor) {
221                         return Light( baseColor, 20.0f);
222                 }
223
224                 public static Color Dark(Color baseColor) {
225                         return Dark(baseColor, 10.0f);
226                 }
227
228                 public static Color Dark(Color baseColor,float percOfDarkDark) {
229                         int H, I, S;
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);
233                 }
234
235                 public static Color DarkDark(Color baseColor) {
236                         return Dark(baseColor, 20.0f);
237                 }
238
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);
241                 }
242
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) {
247
248                         ThemeEngine.Current.CPDrawBorder (graphics, bounds, leftColor, leftWidth,
249                                 leftStyle, topColor, topWidth, topStyle, rightColor, rightWidth, rightStyle,
250                                 bottomColor, bottomWidth, bottomStyle);
251                 }
252
253
254                 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle) {
255                         DrawBorder3D(graphics, rectangle, Border3DStyle.Etched, Border3DSide.All);
256                 }
257
258                 public static void DrawBorder3D(Graphics graphics, Rectangle rectangle, Border3DStyle style) {
259                         DrawBorder3D(graphics, rectangle, style, Border3DSide.All);
260                 }
261
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);
264                 }
265
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);
268                 }
269
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);
272                 }
273
274                 public static void DrawBorder3D( Graphics graphics, Rectangle rectangle, Border3DStyle style, Border3DSide sides) {
275
276                         ThemeEngine.Current.CPDrawBorder3D (graphics, rectangle, style, sides);
277                 }
278
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);
281                 }
282
283                 public static void DrawButton( Graphics graphics, Rectangle rectangle, ButtonState state) {
284
285                         ThemeEngine.Current.CPDrawButton (graphics, rectangle, state);
286                 }
287
288
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);
291                 }
292
293                 public static void DrawCaptionButton(Graphics graphics, Rectangle rectangle, CaptionButton button, ButtonState state) {
294
295                         ThemeEngine.Current.CPDrawCaptionButton (graphics, rectangle, button, state);
296                 }
297
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);
300                 }
301
302                 public static void DrawCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
303
304                         ThemeEngine.Current.CPDrawCheckBox (graphics, rectangle, state);
305                 }
306
307                 public static void DrawComboButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
308
309                         ThemeEngine.Current.CPDrawComboButton (graphics, rectangle,  state);
310                 }
311
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);
314                 }
315
316                 public static void DrawContainerGrabHandle(Graphics graphics, Rectangle bounds) {
317
318                         ThemeEngine.Current.CPDrawContainerGrabHandle (graphics, bounds);
319                 }
320
321                 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle) {
322                         DrawFocusRectangle(graphics, rectangle, Color.White, Color.Black);
323                 }
324
325                 public static void DrawFocusRectangle( Graphics graphics, Rectangle rectangle, Color foreColor, Color backColor) {
326
327                         ThemeEngine.Current.CPDrawFocusRectangle (graphics, rectangle, foreColor, backColor);
328                 }
329
330                 public static void DrawGrabHandle(Graphics graphics, Rectangle rectangle, bool primary, bool enabled) {
331
332                         ThemeEngine.Current.CPDrawGrabHandle (graphics, rectangle, primary, enabled);
333                 }
334
335                 public static void DrawGrid(Graphics graphics, Rectangle area, Size pixelsBetweenDots, Color backColor) {
336
337                         ThemeEngine.Current.CPDrawGrid (graphics, area, pixelsBetweenDots, backColor);
338                 }
339
340                 public static void DrawImageDisabled(Graphics graphics, Image image, int x, int y, Color background) {
341
342                         ThemeEngine.Current.CPDrawImageDisabled (graphics, image, x, y, background);
343                 }
344
345                 public static void DrawLockedFrame(Graphics graphics, Rectangle rectangle, bool primary) {
346
347                         ThemeEngine.Current.CPDrawLockedFrame (graphics, rectangle, primary);
348                 }
349
350                 public static void DrawMenuGlyph(Graphics graphics, Rectangle rectangle, MenuGlyph glyph) {
351
352                         ThemeEngine.Current.CPDrawMenuGlyph (graphics, rectangle, glyph);
353                 }
354
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);
357                 }
358
359                 public static void DrawMixedCheckBox(Graphics graphics, Rectangle rectangle, ButtonState state) {
360                         DrawCheckBox(graphics, rectangle, state);
361                 }
362
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);
365                 }
366
367
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);
370                 }
371
372                 public static void DrawRadioButton(Graphics graphics, Rectangle rectangle, ButtonState state) {
373
374                         ThemeEngine.Current.CPDrawRadioButton (graphics, rectangle, state);
375                 }
376
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();
380                 }
381
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();
385                 }
386
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();
390                 }
391
392
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);
395                 }
396
397                 public static void DrawScrollButton (Graphics graphics, Rectangle rectangle, ScrollButton button, ButtonState state) {
398                         ThemeEngine.Current.CPDrawScrollButton (graphics, rectangle, button, state);
399                 }
400
401                 [MonoTODO]
402                 public static void DrawSelectionFrame(Graphics graphics, bool active, Rectangle outsideRect, Rectangle insideRect, Color backColor) {
403                         throw new NotImplementedException();
404                 }
405
406                 public static void DrawSizeGrip (Graphics graphics, Color backColor, Rectangle bounds)
407                 {
408                         ThemeEngine.Current.CPDrawSizeGrip (graphics,  backColor,  bounds);
409                 }
410
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));
413                 }
414
415                 public static void DrawStringDisabled(Graphics graphics, string s, Font font, Color color, RectangleF layoutRectangle, StringFormat format) {
416
417                         ThemeEngine.Current.CPDrawStringDisabled (graphics, s, font, color, layoutRectangle, format);
418                 }
419                 #endregion      // Public Static Methods
420         }
421 }