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