wintrust: Use path in WIN_TRUST_SUBJECT_FILE structure rather than assuming a path...
[wine] / dlls / comdlg32 / colordlg.c
1 /*
2  * COMMDLG - Color Dialog
3  *
4  * Copyright 1994 Martin Ayotte
5  * Copyright 1996 Albrecht Kleine
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /* BUGS : still seems to not refresh correctly
23    sometimes, especially when 2 instances of the
24    dialog are loaded at the same time */
25
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "commdlg.h"
36 #include "dlgs.h"
37 #include "wine/debug.h"
38 #include "cderr.h"
39 #include "cdlg.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
42
43 static INT_PTR CALLBACK ColorDlgProc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam );
44
45 #define CONV_LPARAMTOPOINT(lp,p) do { (p)->x = (short)LOWORD(lp); (p)->y = (short)HIWORD(lp); } while(0)
46
47 static const COLORREF predefcolors[6][8]=
48 {
49  { 0x008080FFL, 0x0080FFFFL, 0x0080FF80L, 0x0080FF00L,
50    0x00FFFF80L, 0x00FF8000L, 0x00C080FFL, 0x00FF80FFL },
51  { 0x000000FFL, 0x0000FFFFL, 0x0000FF80L, 0x0040FF00L,
52    0x00FFFF00L, 0x00C08000L, 0x00C08080L, 0x00FF00FFL },
53
54  { 0x00404080L, 0x004080FFL, 0x0000FF00L, 0x00808000L,
55    0x00804000L, 0x00FF8080L, 0x00400080L, 0x008000FFL },
56  { 0x00000080L, 0x000080FFL, 0x00008000L, 0x00408000L,
57    0x00FF0000L, 0x00A00000L, 0x00800080L, 0x00FF0080L },
58
59  { 0x00000040L, 0x00004080L, 0x00004000L, 0x00404000L,
60    0x00800000L, 0x00400000L, 0x00400040L, 0x00800040L },
61  { 0x00000000L, 0x00008080L, 0x00408080L, 0x00808080L,
62    0x00808040L, 0x00C0C0C0L, 0x00400040L, 0x00FFFFFFL },
63 };
64
65 static const WCHAR szColourDialogProp[] = {
66     'c','o','l','o','u','r','d','i','a','l','o','g','p','r','o','p',0 };
67
68 /* Chose Color PRIVATE Structure:
69  *
70  * This structure is duplicated in the 16 bit code with
71  * an extra member
72  */
73
74 typedef struct CCPRIVATE
75 {
76     LPCHOOSECOLORW lpcc; /* points to public known data structure */
77     int nextuserdef;     /* next free place in user defined color array */
78     HDC hdcMem;          /* color graph used for BitBlt() */
79     HBITMAP hbmMem;      /* color graph bitmap */
80     RECT fullsize;       /* original dialog window size */
81     UINT msetrgb;        /* # of SETRGBSTRING message (today not used)  */
82     RECT old3angle;      /* last position of l-marker */
83     RECT oldcross;       /* last position of color/saturation marker */
84     BOOL updating;       /* to prevent recursive WM_COMMAND/EN_UPDATE processing */
85     int h;
86     int s;
87     int l;               /* for temporary storing of hue,sat,lum */
88     int capturedGraph;   /* control mouse captured */
89     RECT focusRect;      /* rectangle last focused item */
90     HWND hwndFocus;      /* handle last focused item */
91 } CCPRIV, *LPCCPRIV;
92
93 /***********************************************************************
94  *                             CC_HSLtoRGB                    [internal]
95  */
96 int CC_HSLtoRGB(char c, int hue, int sat, int lum)
97 {
98  int res = 0, maxrgb;
99
100  /* hue */
101  switch(c)
102  {
103   case 'R': if (hue > 80)  hue -= 80; else hue += 160; break;
104   case 'G': if (hue > 160) hue -= 160; else hue += 80; break;
105   case 'B': break;
106  }
107
108  /* l below 120 */
109  maxrgb = (256 * min(120,lum)) / 120;  /* 0 .. 256 */
110  if (hue < 80)
111   res = 0;
112  else
113   if (hue < 120)
114   {
115    res = (hue - 80) * maxrgb;           /* 0...10240 */
116    res /= 40;                        /* 0...256 */
117   }
118   else
119    if (hue < 200)
120     res = maxrgb;
121    else
122     {
123      res= (240 - hue) * maxrgb;
124      res /= 40;
125     }
126  res = res - maxrgb / 2;                 /* -128...128 */
127
128  /* saturation */
129  res = maxrgb / 2 + (sat * res) / 240;    /* 0..256 */
130
131  /* lum above 120 */
132  if (lum > 120 && res < 256)
133   res += ((lum - 120) * (256 - res)) / 120;
134
135  return min(res, 255);
136 }
137
138 /***********************************************************************
139  *                             CC_RGBtoHSL                    [internal]
140  */
141 int CC_RGBtoHSL(char c, int r, int g, int b)
142 {
143  WORD maxi, mini, mmsum, mmdif, result = 0;
144  int iresult = 0;
145
146  maxi = max(r, b);
147  maxi = max(maxi, g);
148  mini = min(r, b);
149  mini = min(mini, g);
150
151  mmsum = maxi + mini;
152  mmdif = maxi - mini;
153
154  switch(c)
155  {
156   /* lum */
157   case 'L': mmsum *= 120;              /* 0...61200=(255+255)*120 */
158            result = mmsum / 255;        /* 0...240 */
159            break;
160   /* saturation */
161   case 'S': if (!mmsum)
162             result = 0;
163            else
164             if (!mini || maxi == 255)
165              result = 240;
166            else
167            {
168             result = mmdif * 240;       /* 0...61200=255*240 */
169             result /= (mmsum > 255 ? mmsum = 510 - mmsum : mmsum); /* 0..255 */
170            }
171            break;
172   /* hue */
173   case 'H': if (!mmdif)
174             result = 160;
175            else
176            {
177             if (maxi == r)
178             {
179              iresult = 40 * (g - b);       /* -10200 ... 10200 */
180              iresult /= (int) mmdif;    /* -40 .. 40 */
181              if (iresult < 0)
182               iresult += 240;          /* 0..40 and 200..240 */
183             }
184             else
185              if (maxi == g)
186              {
187               iresult = 40 * (b - r);
188               iresult /= (int) mmdif;
189               iresult += 80;           /* 40 .. 120 */
190              }
191              else
192               if (maxi == b)
193               {
194                iresult = 40 * (r - g);
195                iresult /= (int) mmdif;
196                iresult += 160;         /* 120 .. 200 */
197               }
198             result = iresult;
199            }
200            break;
201  }
202  return result;    /* is this integer arithmetic precise enough ? */
203 }
204
205
206 /***********************************************************************
207  *                  CC_DrawCurrentFocusRect                       [internal]
208  */
209 static void CC_DrawCurrentFocusRect( const CCPRIV *lpp )
210 {
211   if (lpp->hwndFocus)
212   {
213     HDC hdc = GetDC(lpp->hwndFocus);
214     DrawFocusRect(hdc, &lpp->focusRect);
215     ReleaseDC(lpp->hwndFocus, hdc);
216   }
217 }
218
219 /***********************************************************************
220  *                  CC_DrawFocusRect                       [internal]
221  */
222 static void CC_DrawFocusRect( LPCCPRIV lpp, HWND hwnd, int x, int y, int rows, int cols)
223 {
224   RECT rect;
225   int dx, dy;
226   HDC hdc;
227
228   CC_DrawCurrentFocusRect(lpp); /* remove current focus rect */
229   /* calculate new rect */
230   GetClientRect(hwnd, &rect);
231   dx = (rect.right - rect.left) / cols;
232   dy = (rect.bottom - rect.top) / rows;
233   rect.left += (x * dx) - 2;
234   rect.top += (y * dy) - 2;
235   rect.right = rect.left + dx;
236   rect.bottom = rect.top + dy;
237   /* draw it */
238   hdc = GetDC(hwnd);
239   DrawFocusRect(hdc, &rect);
240   CopyRect(&lpp->focusRect, &rect);
241   lpp->hwndFocus = hwnd;
242   ReleaseDC(hwnd, hdc);
243 }
244
245 #define DISTANCE 4
246
247 /***********************************************************************
248  *                CC_MouseCheckPredefColorArray               [internal]
249  *                returns 1 if one of the predefined colors is clicked
250  */
251 static int CC_MouseCheckPredefColorArray( LPCCPRIV lpp, HWND hDlg, int dlgitem, int rows, int cols,
252             LPARAM lParam )
253 {
254  HWND hwnd;
255  POINT point;
256  RECT rect;
257  int dx, dy, x, y;
258
259  CONV_LPARAMTOPOINT(lParam, &point);
260  ClientToScreen(hDlg, &point);
261  hwnd = GetDlgItem(hDlg, dlgitem);
262  GetWindowRect(hwnd, &rect);
263  if (PtInRect(&rect, point))
264  {
265   dx = (rect.right - rect.left) / cols;
266   dy = (rect.bottom - rect.top) / rows;
267   ScreenToClient(hwnd, &point);
268
269   if (point.x % dx < ( dx - DISTANCE) && point.y % dy < ( dy - DISTANCE))
270   {
271    x = point.x / dx;
272    y = point.y / dy;
273    lpp->lpcc->rgbResult = predefcolors[y][x];
274    CC_DrawFocusRect(lpp, hwnd, x, y, rows, cols);
275    return 1;
276   }
277  }
278  return 0;
279 }
280
281 /***********************************************************************
282  *                  CC_MouseCheckUserColorArray               [internal]
283  *                  return 1 if the user clicked a color
284  */
285 static int CC_MouseCheckUserColorArray( LPCCPRIV lpp, HWND hDlg, int dlgitem, int rows, int cols,
286             LPARAM lParam )
287 {
288  HWND hwnd;
289  POINT point;
290  RECT rect;
291  int dx, dy, x, y;
292  COLORREF *crarr = lpp->lpcc->lpCustColors;
293
294  CONV_LPARAMTOPOINT(lParam, &point);
295  ClientToScreen(hDlg, &point);
296  hwnd = GetDlgItem(hDlg, dlgitem);
297  GetWindowRect(hwnd, &rect);
298  if (PtInRect(&rect, point))
299  {
300   dx = (rect.right - rect.left) / cols;
301   dy = (rect.bottom - rect.top) / rows;
302   ScreenToClient(hwnd, &point);
303
304   if (point.x % dx < (dx - DISTANCE) && point.y % dy < (dy - DISTANCE))
305   {
306    x = point.x / dx;
307    y = point.y / dy;
308    lpp->lpcc->rgbResult = crarr[x + (cols * y) ];
309    CC_DrawFocusRect(lpp, hwnd, x, y, rows, cols);
310    return 1;
311   }
312  }
313  return 0;
314 }
315
316 #define MAXVERT  240
317 #define MAXHORI  239
318
319 /*  240  ^......        ^^ 240
320          |     .        ||
321     SAT  |     .        || LUM
322          |     .        ||
323          +-----> 239   ----
324            HUE
325 */
326 /***********************************************************************
327  *                  CC_MouseCheckColorGraph                   [internal]
328  */
329 static int CC_MouseCheckColorGraph( HWND hDlg, int dlgitem, int *hori, int *vert, LPARAM lParam )
330 {
331  HWND hwnd;
332  POINT point;
333  RECT rect;
334  long x,y;
335
336  CONV_LPARAMTOPOINT(lParam, &point);
337  ClientToScreen(hDlg, &point);
338  hwnd = GetDlgItem( hDlg, dlgitem );
339  GetWindowRect(hwnd, &rect);
340
341  if (!PtInRect(&rect, point))
342   return 0;
343
344  GetClientRect(hwnd, &rect);
345  ScreenToClient(hwnd, &point);
346
347  x = (long) point.x * MAXHORI;
348  x /= rect.right;
349  y = (long) (rect.bottom - point.y) * MAXVERT;
350  y /= rect.bottom;
351
352  if (x < 0) x = 0;
353  if (y < 0) y = 0;
354  if (x > MAXHORI) x = MAXHORI;
355  if (y > MAXVERT) y = MAXVERT;
356
357  if (hori)
358   *hori = x;
359  if (vert)
360   *vert = y;
361
362  return 1;
363 }
364 /***********************************************************************
365  *                  CC_MouseCheckResultWindow                 [internal]
366  *                  test if double click one of the result colors
367  */
368 int CC_MouseCheckResultWindow( HWND hDlg, LPARAM lParam )
369 {
370  HWND hwnd;
371  POINT point;
372  RECT rect;
373
374  CONV_LPARAMTOPOINT(lParam, &point);
375  ClientToScreen(hDlg, &point);
376  hwnd = GetDlgItem(hDlg, 0x2c5);
377  GetWindowRect(hwnd, &rect);
378  if (PtInRect(&rect, point))
379  {
380   PostMessageA(hDlg, WM_COMMAND, 0x2c9, 0);
381   return 1;
382  }
383  return 0;
384 }
385
386 /***********************************************************************
387  *                       CC_CheckDigitsInEdit                 [internal]
388  */
389 int CC_CheckDigitsInEdit( HWND hwnd, int maxval )
390 {
391  int i, k, m, result, value;
392  long editpos;
393  char buffer[30];
394
395  GetWindowTextA(hwnd, buffer, sizeof(buffer));
396  m = strlen(buffer);
397  result = 0;
398
399  for (i = 0 ; i < m ; i++)
400   if (buffer[i] < '0' || buffer[i] > '9')
401   {
402    for (k = i + 1; k <= m; k++)  /* delete bad character */
403    {
404     buffer[i] = buffer[k];
405     m--;
406    }
407    buffer[m] = 0;
408    result = 1;
409   }
410
411  value = atoi(buffer);
412  if (value > maxval)       /* build a new string */
413  {
414   sprintf(buffer, "%d", maxval);
415   result = 2;
416  }
417  if (result)
418  {
419   editpos = SendMessageA(hwnd, EM_GETSEL, 0, 0);
420   SetWindowTextA(hwnd, buffer );
421   SendMessageA(hwnd, EM_SETSEL, 0, editpos);
422  }
423  return value;
424 }
425
426
427
428 /***********************************************************************
429  *                    CC_PaintSelectedColor                   [internal]
430  */
431 void CC_PaintSelectedColor( HWND hDlg, COLORREF cr )
432 {
433  RECT rect;
434  HDC  hdc;
435  HBRUSH hBrush;
436  HWND hwnd = GetDlgItem(hDlg, 0x2c5);
437  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) ))   /* if full size */
438  {
439   hdc = GetDC(hwnd);
440   GetClientRect(hwnd, &rect) ;
441   hBrush = CreateSolidBrush(cr);
442   if (hBrush)
443   {
444    FillRect(hdc, &rect, hBrush);
445    DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
446    DeleteObject(hBrush);
447   }
448   ReleaseDC(hwnd, hdc);
449  }
450 }
451
452 /***********************************************************************
453  *                    CC_PaintTriangle                        [internal]
454  */
455 void CC_PaintTriangle( HWND hDlg, int y)
456 {
457  HDC hDC;
458  long temp;
459  int w = LOWORD(GetDialogBaseUnits()) / 2;
460  POINT points[3];
461  int height;
462  int oben;
463  RECT rect;
464  HBRUSH hbr;
465  HWND hwnd = GetDlgItem(hDlg, 0x2be);
466  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
467
468  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6)))   /* if full size */
469  {
470    GetClientRect(hwnd, &rect);
471    height = rect.bottom;
472    hDC = GetDC(hDlg);
473    points[0].y = rect.top;
474    points[0].x = rect.right;     /*  |  /|  */
475    ClientToScreen(hwnd, points); /*  | / |  */
476    ScreenToClient(hDlg, points); /*  |<  |  */
477    oben = points[0].y;           /*  | \ |  */
478                                  /*  |  \|  */
479    temp = (long)height * (long)y;
480    points[0].x += 1;
481    points[0].y = oben + height - temp / (long)MAXVERT;
482    points[1].y = points[0].y + w;
483    points[2].y = points[0].y - w;
484    points[2].x = points[1].x = points[0].x + w;
485
486    hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
487    if (!hbr) hbr = GetSysColorBrush(COLOR_BTNFACE);
488    FillRect(hDC, &lpp->old3angle, hbr);
489    lpp->old3angle.left  = points[0].x;
490    lpp->old3angle.right = points[1].x + 1;
491    lpp->old3angle.top   = points[2].y - 1;
492    lpp->old3angle.bottom= points[1].y + 1;
493
494    hbr = SelectObject(hDC, GetStockObject(BLACK_BRUSH));
495    Polygon(hDC, points, 3);
496    SelectObject(hDC, hbr);
497
498    ReleaseDC(hDlg, hDC);
499  }
500 }
501
502
503 /***********************************************************************
504  *                    CC_PaintCross                           [internal]
505  */
506 void CC_PaintCross( HWND hDlg, int x, int y)
507 {
508  HDC hDC;
509  int w = GetDialogBaseUnits() - 1;
510  int wc = GetDialogBaseUnits() * 3 / 4;
511  HWND hwnd = GetDlgItem(hDlg, 0x2c6);
512  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
513  RECT rect;
514  POINT point, p;
515  HPEN hPen;
516
517  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) ))   /* if full size */
518  {
519    GetClientRect(hwnd, &rect);
520    hDC = GetDC(hwnd);
521    SelectClipRgn( hDC, CreateRectRgnIndirect(&rect));
522
523    point.x = ((long)rect.right * (long)x) / (long)MAXHORI;
524    point.y = rect.bottom - ((long)rect.bottom * (long)y) / (long)MAXVERT;
525    if ( lpp->oldcross.left != lpp->oldcross.right )
526      BitBlt(hDC, lpp->oldcross.left, lpp->oldcross.top,
527               lpp->oldcross.right - lpp->oldcross.left,
528               lpp->oldcross.bottom - lpp->oldcross.top,
529               lpp->hdcMem, lpp->oldcross.left, lpp->oldcross.top, SRCCOPY);
530    lpp->oldcross.left   = point.x - w - 1;
531    lpp->oldcross.right  = point.x + w + 1;
532    lpp->oldcross.top    = point.y - w - 1;
533    lpp->oldcross.bottom = point.y + w + 1;
534
535    hPen = CreatePen(PS_SOLID, 3, 0x000000); /* -black- color */
536    hPen = SelectObject(hDC, hPen);
537    MoveToEx(hDC, point.x - w, point.y, &p);
538    LineTo(hDC, point.x - wc, point.y);
539    MoveToEx(hDC, point.x + wc, point.y, &p);
540    LineTo(hDC, point.x + w, point.y);
541    MoveToEx(hDC, point.x, point.y - w, &p);
542    LineTo(hDC, point.x, point.y - wc);
543    MoveToEx(hDC, point.x, point.y + wc, &p);
544    LineTo(hDC, point.x, point.y + w);
545    DeleteObject( SelectObject(hDC, hPen));
546
547    ReleaseDC(hwnd, hDC);
548  }
549 }
550
551
552 #define XSTEPS 48
553 #define YSTEPS 24
554
555
556 /***********************************************************************
557  *                    CC_PrepareColorGraph                    [internal]
558  */
559 static void CC_PrepareColorGraph( HWND hDlg )
560 {
561  int sdif, hdif, xdif, ydif, r, g, b, hue, sat;
562  HWND hwnd = GetDlgItem(hDlg, 0x2c6);
563  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
564  HBRUSH hbrush;
565  HDC hdc ;
566  RECT rect, client;
567  HCURSOR hcursor = SetCursor( LoadCursorW(0, (LPCWSTR)IDC_WAIT) );
568
569  GetClientRect(hwnd, &client);
570  hdc = GetDC(hwnd);
571  lpp->hdcMem = CreateCompatibleDC(hdc);
572  lpp->hbmMem = CreateCompatibleBitmap(hdc, client.right, client.bottom);
573  SelectObject(lpp->hdcMem, lpp->hbmMem);
574
575  xdif = client.right / XSTEPS;
576  ydif = client.bottom / YSTEPS+1;
577  hdif = 239 / XSTEPS;
578  sdif = 240 / YSTEPS;
579  for (rect.left = hue = 0; hue < 239 + hdif; hue += hdif)
580  {
581   rect.right = rect.left + xdif;
582   rect.bottom = client.bottom;
583   for(sat = 0; sat < 240 + sdif; sat += sdif)
584   {
585    rect.top = rect.bottom - ydif;
586    r = CC_HSLtoRGB('R', hue, sat, 120);
587    g = CC_HSLtoRGB('G', hue, sat, 120);
588    b = CC_HSLtoRGB('B', hue, sat, 120);
589    hbrush = CreateSolidBrush( RGB(r, g, b));
590    FillRect(lpp->hdcMem, &rect, hbrush);
591    DeleteObject(hbrush);
592    rect.bottom = rect.top;
593   }
594   rect.left = rect.right;
595  }
596  ReleaseDC(hwnd, hdc);
597  SetCursor(hcursor);
598 }
599
600 /***********************************************************************
601  *                          CC_PaintColorGraph                [internal]
602  */
603 static void CC_PaintColorGraph( HWND hDlg )
604 {
605  HWND hwnd = GetDlgItem( hDlg, 0x2c6 );
606  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
607  HDC  hDC;
608  RECT rect;
609  if (IsWindowVisible(hwnd))   /* if full size */
610  {
611   if (!lpp->hdcMem)
612    CC_PrepareColorGraph(hDlg);   /* should not be necessary */
613
614   hDC = GetDC(hwnd);
615   GetClientRect(hwnd, &rect);
616   if (lpp->hdcMem)
617       BitBlt(hDC, 0, 0, rect.right, rect.bottom, lpp->hdcMem, 0, 0, SRCCOPY);
618   else
619       WARN("choose color: hdcMem is not defined\n");
620   ReleaseDC(hwnd, hDC);
621  }
622 }
623
624 /***********************************************************************
625  *                           CC_PaintLumBar                   [internal]
626  */
627 static void CC_PaintLumBar( HWND hDlg, int hue, int sat )
628 {
629  HWND hwnd = GetDlgItem(hDlg, 0x2be);
630  RECT rect, client;
631  int lum, ldif, ydif, r, g, b;
632  HBRUSH hbrush;
633  HDC hDC;
634
635  if (IsWindowVisible(hwnd))
636  {
637   hDC = GetDC(hwnd);
638   GetClientRect(hwnd, &client);
639   rect = client;
640
641   ldif = 240 / YSTEPS;
642   ydif = client.bottom / YSTEPS+1;
643   for (lum = 0; lum < 240 + ldif; lum += ldif)
644   {
645    rect.top = max(0, rect.bottom - ydif);
646    r = CC_HSLtoRGB('R', hue, sat, lum);
647    g = CC_HSLtoRGB('G', hue, sat, lum);
648    b = CC_HSLtoRGB('B', hue, sat, lum);
649    hbrush = CreateSolidBrush( RGB(r, g, b) );
650    FillRect(hDC, &rect, hbrush);
651    DeleteObject(hbrush);
652    rect.bottom = rect.top;
653   }
654   GetClientRect(hwnd, &rect);
655   DrawEdge(hDC, &rect, BDR_SUNKENOUTER, BF_RECT);
656   ReleaseDC(hwnd, hDC);
657  }
658 }
659
660 /***********************************************************************
661  *                             CC_EditSetRGB                  [internal]
662  */
663 void CC_EditSetRGB( HWND hDlg, COLORREF cr )
664 {
665  char buffer[10];
666  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
667  int r = GetRValue(cr);
668  int g = GetGValue(cr);
669  int b = GetBValue(cr);
670  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) ))   /* if full size */
671  {
672    lpp->updating = TRUE;
673    sprintf(buffer, "%d", r);
674    SetWindowTextA( GetDlgItem(hDlg, 0x2c2), buffer);
675    sprintf(buffer, "%d", g);
676    SetWindowTextA( GetDlgItem(hDlg, 0x2c3), buffer);
677    sprintf( buffer, "%d", b );
678    SetWindowTextA( GetDlgItem(hDlg, 0x2c4),buffer);
679    lpp->updating = FALSE;
680  }
681 }
682
683 /***********************************************************************
684  *                             CC_EditSetHSL                  [internal]
685  */
686 void CC_EditSetHSL( HWND hDlg, int h, int s, int l )
687 {
688  char buffer[10];
689  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
690
691  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) ))   /* if full size */
692  {
693    lpp->updating = TRUE;
694    sprintf(buffer, "%d", h);
695    SetWindowTextA( GetDlgItem(hDlg, 0x2bf), buffer);
696    sprintf(buffer, "%d", s);
697    SetWindowTextA( GetDlgItem(hDlg, 0x2c0), buffer);
698    sprintf(buffer, "%d", l);
699    SetWindowTextA( GetDlgItem(hDlg, 0x2c1), buffer);
700    lpp->updating = FALSE;
701  }
702  CC_PaintLumBar(hDlg, h, s);
703 }
704
705 /***********************************************************************
706  *                       CC_SwitchToFullSize                  [internal]
707  */
708 void CC_SwitchToFullSize( HWND hDlg, COLORREF result, LPCRECT lprect )
709 {
710  int i;
711  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
712
713  EnableWindow( GetDlgItem(hDlg, 0x2cf), FALSE);
714  CC_PrepareColorGraph(hDlg);
715  for (i = 0x2bf; i < 0x2c5; i++)
716    ShowWindow( GetDlgItem(hDlg, i), SW_SHOW);
717  for (i = 0x2d3; i < 0x2d9; i++)
718    ShowWindow( GetDlgItem(hDlg, i), SW_SHOW);
719  ShowWindow( GetDlgItem(hDlg, 0x2c9), SW_SHOW);
720  ShowWindow( GetDlgItem(hDlg, 0x2c8), SW_SHOW);
721  ShowWindow( GetDlgItem(hDlg, 1090), SW_SHOW);
722
723  if (lprect)
724   SetWindowPos(hDlg, 0, 0, 0, lprect->right-lprect->left,
725    lprect->bottom-lprect->top, SWP_NOMOVE|SWP_NOZORDER);
726
727  ShowWindow( GetDlgItem(hDlg, 0x2be), SW_SHOW);
728  ShowWindow( GetDlgItem(hDlg, 0x2c5), SW_SHOW);
729
730  CC_EditSetRGB(hDlg, result);
731  CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
732  ShowWindow( GetDlgItem( hDlg, 0x2c6), SW_SHOW);
733  UpdateWindow( GetDlgItem(hDlg, 0x2c6) );
734 }
735
736 /***********************************************************************
737  *                           CC_PaintPredefColorArray         [internal]
738  *                Paints the default standard 48 colors
739  */
740 static void CC_PaintPredefColorArray( HWND hDlg, int rows, int cols)
741 {
742  HWND hwnd = GetDlgItem(hDlg, 0x2d0);
743  RECT rect, blockrect;
744  HDC  hdc;
745  HBRUSH hBrush;
746  int dx, dy, i, j, k;
747  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
748
749  GetClientRect(hwnd, &rect);
750  dx = rect.right / cols;
751  dy = rect.bottom / rows;
752  k = rect.left;
753
754  hdc = GetDC(hwnd);
755  GetClientRect(hwnd, &rect);
756  hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
757  if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
758  FillRect(hdc, &rect, hBrush);
759  for ( j = 0; j < rows; j++ )
760  {
761   for ( i = 0; i < cols; i++ )
762   {
763    hBrush = CreateSolidBrush(predefcolors[j][i]);
764    if (hBrush)
765    {
766     blockrect.left = rect.left;
767     blockrect.top = rect.top;
768     blockrect.right = rect.left + dx - DISTANCE;
769     blockrect.bottom = rect.top + dy - DISTANCE;
770     FillRect(hdc, &blockrect, hBrush);
771     DrawEdge(hdc, &blockrect, BDR_SUNKEN, BF_RECT);
772     DeleteObject(hBrush);
773    }
774    rect.left += dx;
775   }
776   rect.top += dy;
777   rect.left = k;
778  }
779  ReleaseDC(hwnd, hdc);
780  if (lpp->hwndFocus == hwnd)
781    CC_DrawCurrentFocusRect(lpp);
782 }
783 /***********************************************************************
784  *                             CC_PaintUserColorArray         [internal]
785  *               Paint the 16 user-selected colors
786  */
787 void CC_PaintUserColorArray( HWND hDlg, int rows, int cols, const COLORREF *lpcr )
788 {
789  HWND hwnd = GetDlgItem(hDlg, 0x2d1);
790  RECT rect, blockrect;
791  HDC  hdc;
792  HBRUSH hBrush;
793  int dx, dy, i, j, k;
794  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
795
796  GetClientRect(hwnd, &rect);
797
798  dx = rect.right / cols;
799  dy = rect.bottom / rows;
800  k = rect.left;
801
802  hdc = GetDC(hwnd);
803  if (hdc)
804  {
805   hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
806   if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
807   FillRect( hdc, &rect, hBrush );
808   for (j = 0; j < rows; j++)
809   {
810    for (i = 0; i < cols; i++)
811    {
812     hBrush = CreateSolidBrush(lpcr[i+j*cols]);
813     if (hBrush)
814     {
815      blockrect.left = rect.left;
816      blockrect.top = rect.top;
817      blockrect.right = rect.left + dx - DISTANCE;
818      blockrect.bottom = rect.top + dy - DISTANCE;
819      FillRect(hdc, &blockrect, hBrush);
820      DrawEdge(hdc, &blockrect, BDR_SUNKEN, BF_RECT);
821      DeleteObject(hBrush);
822     }
823     rect.left += dx;
824    }
825    rect.top += dy;
826    rect.left = k;
827   }
828   ReleaseDC(hwnd, hdc);
829  }
830  if (lpp->hwndFocus == hwnd)
831    CC_DrawCurrentFocusRect(lpp);
832 }
833
834
835 /***********************************************************************
836  *                             CC_HookCallChk                 [internal]
837  */
838 BOOL CC_HookCallChk( const CHOOSECOLORW *lpcc )
839 {
840  if (lpcc)
841   if(lpcc->Flags & CC_ENABLEHOOK)
842    if (lpcc->lpfnHook)
843     return TRUE;
844  return FALSE;
845 }
846
847 /***********************************************************************
848  *                              CC_WMInitDialog                  [internal]
849  */
850 static LONG CC_WMInitDialog( HWND hDlg, WPARAM wParam, LPARAM lParam )
851 {
852    int i, res;
853    int r, g, b;
854    HWND hwnd;
855    RECT rect;
856    POINT point;
857    LPCCPRIV lpp;
858
859    TRACE("WM_INITDIALOG lParam=%08lX\n", lParam);
860    lpp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct CCPRIVATE) );
861
862    lpp->lpcc = (LPCHOOSECOLORW) lParam;
863    if (lpp->lpcc->lStructSize != sizeof(CHOOSECOLORW) )
864    {
865        HeapFree(GetProcessHeap(), 0, lpp);
866        EndDialog (hDlg, 0) ;
867        return FALSE;
868    }
869
870    SetPropW( hDlg, szColourDialogProp, lpp );
871
872    if (!(lpp->lpcc->Flags & CC_SHOWHELP))
873       ShowWindow( GetDlgItem(hDlg,0x40e), SW_HIDE);
874    lpp->msetrgb = RegisterWindowMessageA(SETRGBSTRINGA);
875
876 #if 0
877    cpos = MAKELONG(5,7); /* init */
878    if (lpp->lpcc->Flags & CC_RGBINIT)
879    {
880      for (i = 0; i < 6; i++)
881        for (j = 0; j < 8; j++)
882         if (predefcolors[i][j] == lpp->lpcc->rgbResult)
883         {
884           cpos = MAKELONG(i,j);
885           goto found;
886         }
887    }
888    found:
889    /* FIXME: Draw_a_focus_rect & set_init_values */
890 #endif
891
892    GetWindowRect(hDlg, &lpp->fullsize);
893    if (lpp->lpcc->Flags & CC_FULLOPEN || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
894    {
895       hwnd = GetDlgItem(hDlg, 0x2cf);
896       EnableWindow(hwnd, FALSE);
897    }
898    if (!(lpp->lpcc->Flags & CC_FULLOPEN ) || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
899    {
900       rect = lpp->fullsize;
901       res = rect.bottom - rect.top;
902       hwnd = GetDlgItem(hDlg, 0x2c6); /* cut at left border */
903       point.x = point.y = 0;
904       ClientToScreen(hwnd, &point);
905       ScreenToClient(hDlg,&point);
906       GetClientRect(hDlg, &rect);
907       point.x += GetSystemMetrics(SM_CXDLGFRAME);
908       SetWindowPos(hDlg, 0, 0, 0, point.x, res, SWP_NOMOVE|SWP_NOZORDER);
909
910       for (i = 0x2bf; i < 0x2c5; i++)
911          ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
912       for (i = 0x2d3; i < 0x2d9; i++)
913          ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
914       ShowWindow( GetDlgItem(hDlg, 0x2c9), SW_HIDE);
915       ShowWindow( GetDlgItem(hDlg, 0x2c8), SW_HIDE);
916       ShowWindow( GetDlgItem(hDlg, 0x2c6), SW_HIDE);
917       ShowWindow( GetDlgItem(hDlg, 0x2c5), SW_HIDE);
918       ShowWindow( GetDlgItem(hDlg, 1090 ), SW_HIDE);
919    }
920    else
921       CC_SwitchToFullSize(hDlg, lpp->lpcc->rgbResult, NULL);
922    res = TRUE;
923    for (i = 0x2bf; i < 0x2c5; i++)
924      SendMessageA( GetDlgItem(hDlg, i), EM_LIMITTEXT, 3, 0);  /* max 3 digits:  xyz  */
925    if (CC_HookCallChk(lpp->lpcc))
926    {
927           res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, WM_INITDIALOG, wParam, lParam);
928    }
929
930    /* Set the initial values of the color chooser dialog */
931    r = GetRValue(lpp->lpcc->rgbResult);
932    g = GetGValue(lpp->lpcc->rgbResult);
933    b = GetBValue(lpp->lpcc->rgbResult);
934
935    CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
936    lpp->h = CC_RGBtoHSL('H', r, g, b);
937    lpp->s = CC_RGBtoHSL('S', r, g, b);
938    lpp->l = CC_RGBtoHSL('L', r, g, b);
939
940    /* Doing it the long way because CC_EditSetRGB/HSL doesn't seem to work */
941    SetDlgItemInt(hDlg, 703, lpp->h, TRUE);
942    SetDlgItemInt(hDlg, 704, lpp->s, TRUE);
943    SetDlgItemInt(hDlg, 705, lpp->l, TRUE);
944    SetDlgItemInt(hDlg, 706, r, TRUE);
945    SetDlgItemInt(hDlg, 707, g, TRUE);
946    SetDlgItemInt(hDlg, 708, b, TRUE);
947
948    CC_PaintCross(hDlg, lpp->h, lpp->s);
949    CC_PaintTriangle(hDlg, lpp->l);
950
951    return res;
952 }
953
954
955 /***********************************************************************
956  *                              CC_WMCommand                  [internal]
957  */
958 LRESULT CC_WMCommand( HWND hDlg, WPARAM wParam, LPARAM lParam, WORD notifyCode, HWND hwndCtl )
959 {
960     int  r, g, b, i, xx;
961     UINT cokmsg;
962     HDC hdc;
963     COLORREF *cr;
964     LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
965     TRACE("CC_WMCommand wParam=%lx lParam=%lx\n", wParam, lParam);
966     switch (LOWORD(wParam))
967     {
968           case 0x2c2:  /* edit notify RGB */
969           case 0x2c3:
970           case 0x2c4:
971                if (notifyCode == EN_UPDATE && !lpp->updating)
972                          {
973                            i = CC_CheckDigitsInEdit(hwndCtl, 255);
974                            r = GetRValue(lpp->lpcc->rgbResult);
975                            g = GetGValue(lpp->lpcc->rgbResult);
976                            b= GetBValue(lpp->lpcc->rgbResult);
977                            xx = 0;
978                            switch (LOWORD(wParam))
979                            {
980                             case 0x2c2: if ((xx = (i != r))) r = i; break;
981                             case 0x2c3: if ((xx = (i != g))) g = i; break;
982                             case 0x2c4: if ((xx = (i != b))) b = i; break;
983                            }
984                            if (xx) /* something has changed */
985                            {
986                             lpp->lpcc->rgbResult = RGB(r, g, b);
987                             CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
988                             lpp->h = CC_RGBtoHSL('H', r, g, b);
989                             lpp->s = CC_RGBtoHSL('S', r, g, b);
990                             lpp->l = CC_RGBtoHSL('L', r, g, b);
991                             CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
992                             CC_PaintCross(hDlg, lpp->h, lpp->s);
993                             CC_PaintTriangle(hDlg, lpp->l);
994                            }
995                          }
996                  break;
997
998           case 0x2bf:  /* edit notify HSL */
999           case 0x2c0:
1000           case 0x2c1:
1001                if (notifyCode == EN_UPDATE && !lpp->updating)
1002                          {
1003                            i = CC_CheckDigitsInEdit(hwndCtl , LOWORD(wParam) == 0x2bf ? 239:240);
1004                            xx = 0;
1005                            switch (LOWORD(wParam))
1006                            {
1007                             case 0x2bf: if ((xx = ( i != lpp->h))) lpp->h = i; break;
1008                             case 0x2c0: if ((xx = ( i != lpp->s))) lpp->s = i; break;
1009                             case 0x2c1: if ((xx = ( i != lpp->l))) lpp->l = i; break;
1010                            }
1011                            if (xx) /* something has changed */
1012                            {
1013                             r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1014                             g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1015                             b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1016                             lpp->lpcc->rgbResult = RGB(r, g, b);
1017                             CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1018                             CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1019                             CC_PaintCross(hDlg, lpp->h, lpp->s);
1020                             CC_PaintTriangle(hDlg, lpp->l);
1021                            }
1022                          }
1023                break;
1024
1025           case 0x2cf:
1026                CC_SwitchToFullSize(hDlg, lpp->lpcc->rgbResult, &lpp->fullsize);
1027                SetFocus( GetDlgItem(hDlg, 0x2bf));
1028                break;
1029
1030           case 0x2c8:    /* add colors ... column by column */
1031                cr = lpp->lpcc->lpCustColors;
1032                cr[(lpp->nextuserdef % 2) * 8 + lpp->nextuserdef / 2] = lpp->lpcc->rgbResult;
1033                if (++lpp->nextuserdef == 16)
1034                    lpp->nextuserdef = 0;
1035                CC_PaintUserColorArray(hDlg, 2, 8, lpp->lpcc->lpCustColors);
1036                break;
1037
1038           case 0x2c9:              /* resulting color */
1039                hdc = GetDC(hDlg);
1040                lpp->lpcc->rgbResult = GetNearestColor(hdc, lpp->lpcc->rgbResult);
1041                ReleaseDC(hDlg, hdc);
1042                CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1043                CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1044                r = GetRValue(lpp->lpcc->rgbResult);
1045                g = GetGValue(lpp->lpcc->rgbResult);
1046                b = GetBValue(lpp->lpcc->rgbResult);
1047                lpp->h = CC_RGBtoHSL('H', r, g, b);
1048                lpp->s = CC_RGBtoHSL('S', r, g, b);
1049                lpp->l = CC_RGBtoHSL('L', r, g, b);
1050                CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
1051                CC_PaintCross(hDlg, lpp->h, lpp->s);
1052                CC_PaintTriangle(hDlg, lpp->l);
1053                break;
1054
1055           case 0x40e:           /* Help! */ /* The Beatles, 1965  ;-) */
1056                i = RegisterWindowMessageA(HELPMSGSTRINGA);
1057                    if (lpp->lpcc->hwndOwner)
1058                        SendMessageA(lpp->lpcc->hwndOwner, i, 0, (LPARAM)lpp->lpcc);
1059                    if ( CC_HookCallChk(lpp->lpcc))
1060                        CallWindowProcA( (WNDPROC) lpp->lpcc->lpfnHook, hDlg,
1061                           WM_COMMAND, psh15, (LPARAM)lpp->lpcc);
1062                break;
1063
1064           case IDOK :
1065                 cokmsg = RegisterWindowMessageA(COLOROKSTRINGA);
1066                     if (lpp->lpcc->hwndOwner)
1067                         if (SendMessageA(lpp->lpcc->hwndOwner, cokmsg, 0, (LPARAM)lpp->lpcc))
1068                         break;    /* do NOT close */
1069                 EndDialog(hDlg, 1) ;
1070                 return TRUE ;
1071
1072           case IDCANCEL :
1073                 EndDialog(hDlg, 0) ;
1074                 return TRUE ;
1075
1076        }
1077        return FALSE;
1078 }
1079
1080 /***********************************************************************
1081  *                              CC_WMPaint                    [internal]
1082  */
1083 LRESULT CC_WMPaint( HWND hDlg, WPARAM wParam, LPARAM lParam )
1084 {
1085     PAINTSTRUCT ps;
1086     LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1087
1088     BeginPaint(hDlg, &ps);
1089     /* we have to paint dialog children except text and buttons */
1090     CC_PaintPredefColorArray(hDlg, 6, 8);
1091     CC_PaintUserColorArray(hDlg, 2, 8, lpp->lpcc->lpCustColors);
1092     CC_PaintLumBar(hDlg, lpp->h, lpp->s);
1093     CC_PaintTriangle(hDlg, lpp->l);
1094     CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1095     CC_PaintColorGraph(hDlg);
1096     CC_PaintCross(hDlg, lpp->h, lpp->s);
1097     EndPaint(hDlg, &ps);
1098
1099     return TRUE;
1100 }
1101
1102 /***********************************************************************
1103  *                              CC_WMLButtonUp              [internal]
1104  */
1105 LRESULT CC_WMLButtonUp( HWND hDlg, WPARAM wParam, LPARAM lParam )
1106 {
1107    LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1108    if (lpp->capturedGraph)
1109    {
1110        lpp->capturedGraph = 0;
1111        ReleaseCapture();
1112        CC_PaintCross(hDlg, lpp->h, lpp->s);
1113        return 1;
1114    }
1115    return 0;
1116 }
1117
1118 /***********************************************************************
1119  *                              CC_WMMouseMove              [internal]
1120  */
1121 LRESULT CC_WMMouseMove( HWND hDlg, LPARAM lParam )
1122 {
1123    LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1124    int r, g, b;
1125
1126    if (lpp->capturedGraph)
1127    {
1128       int *ptrh = NULL, *ptrs = &lpp->l;
1129       if (lpp->capturedGraph == 0x2c6)
1130       {
1131           ptrh = &lpp->h;
1132           ptrs = &lpp->s;
1133       }
1134       if (CC_MouseCheckColorGraph( hDlg, lpp->capturedGraph, ptrh, ptrs, lParam))
1135       {
1136           r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1137           g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1138           b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1139           lpp->lpcc->rgbResult = RGB(r, g, b);
1140           CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1141           CC_EditSetHSL(hDlg,lpp->h, lpp->s, lpp->l);
1142           CC_PaintCross(hDlg, lpp->h, lpp->s);
1143           CC_PaintTriangle(hDlg, lpp->l);
1144           CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1145       }
1146       else
1147       {
1148           ReleaseCapture();
1149           lpp->capturedGraph = 0;
1150       }
1151       return 1;
1152    }
1153    return 0;
1154 }
1155
1156 /***********************************************************************
1157  *                              CC_WMLButtonDown              [internal]
1158  */
1159 LRESULT CC_WMLButtonDown( HWND hDlg, WPARAM wParam, LPARAM lParam )
1160 {
1161    LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1162    int r, g, b, i;
1163    i = 0;
1164
1165    if (CC_MouseCheckPredefColorArray(lpp, hDlg, 0x2d0, 6, 8, lParam))
1166       i = 1;
1167    else
1168       if (CC_MouseCheckUserColorArray(lpp, hDlg, 0x2d1, 2, 8, lParam))
1169          i = 1;
1170       else
1171          if (CC_MouseCheckColorGraph(hDlg, 0x2c6, &lpp->h, &lpp->s, lParam))
1172          {
1173             i = 2;
1174             lpp->capturedGraph = 0x2c6;
1175          }
1176          else
1177             if (CC_MouseCheckColorGraph(hDlg, 0x2be, NULL, &lpp->l, lParam))
1178             {
1179                i = 2;
1180                lpp->capturedGraph = 0x2be;
1181             }
1182    if ( i == 2 )
1183    {
1184       SetCapture(hDlg);
1185       r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1186       g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1187       b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1188       lpp->lpcc->rgbResult = RGB(r, g, b);
1189    }
1190    if ( i == 1 )
1191    {
1192       r = GetRValue(lpp->lpcc->rgbResult);
1193       g = GetGValue(lpp->lpcc->rgbResult);
1194       b = GetBValue(lpp->lpcc->rgbResult);
1195       lpp->h = CC_RGBtoHSL('H', r, g, b);
1196       lpp->s = CC_RGBtoHSL('S', r, g, b);
1197       lpp->l = CC_RGBtoHSL('L', r, g, b);
1198    }
1199    if (i)
1200    {
1201       CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1202       CC_EditSetHSL(hDlg,lpp->h, lpp->s, lpp->l);
1203       CC_PaintCross(hDlg, lpp->h, lpp->s);
1204       CC_PaintTriangle(hDlg, lpp->l);
1205       CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1206       return TRUE;
1207    }
1208    return FALSE;
1209 }
1210
1211 /***********************************************************************
1212  *           ColorDlgProc32 [internal]
1213  *
1214  */
1215 static INT_PTR CALLBACK ColorDlgProc( HWND hDlg, UINT message,
1216                                    WPARAM wParam, LPARAM lParam )
1217 {
1218
1219  int res;
1220  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1221  if (message != WM_INITDIALOG)
1222  {
1223   if (!lpp)
1224      return FALSE;
1225   res = 0;
1226   if (CC_HookCallChk(lpp->lpcc))
1227      res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, message, wParam, lParam);
1228   if ( res )
1229      return res;
1230  }
1231
1232  /* FIXME: SetRGB message
1233  if (message && message == msetrgb)
1234     return HandleSetRGB(hDlg, lParam);
1235  */
1236
1237  switch (message)
1238         {
1239           case WM_INITDIALOG:
1240                         return CC_WMInitDialog(hDlg, wParam, lParam);
1241           case WM_NCDESTROY:
1242                         DeleteDC(lpp->hdcMem);
1243                         DeleteObject(lpp->hbmMem);
1244                         HeapFree(GetProcessHeap(), 0, lpp);
1245                         RemovePropW( hDlg, szColourDialogProp );
1246                         break;
1247           case WM_COMMAND:
1248                         if (CC_WMCommand( hDlg, wParam, lParam, HIWORD(wParam), (HWND) lParam))
1249                            return TRUE;
1250                         break;
1251           case WM_PAINT:
1252                         if ( CC_WMPaint(hDlg, wParam, lParam))
1253                            return TRUE;
1254                         break;
1255           case WM_LBUTTONDBLCLK:
1256                         if (CC_MouseCheckResultWindow(hDlg, lParam))
1257                           return TRUE;
1258                         break;
1259           case WM_MOUSEMOVE:
1260                         if (CC_WMMouseMove(hDlg, lParam))
1261                           return TRUE;
1262                         break;
1263           case WM_LBUTTONUP:  /* FIXME: ClipCursor off (if in color graph)*/
1264                         if (CC_WMLButtonUp(hDlg, wParam, lParam))
1265                            return TRUE;
1266                         break;
1267           case WM_LBUTTONDOWN:/* FIXME: ClipCursor on  (if in color graph)*/
1268                         if (CC_WMLButtonDown(hDlg, wParam, lParam))
1269                            return TRUE;
1270                         break;
1271         }
1272      return FALSE ;
1273 }
1274
1275 /***********************************************************************
1276  *            ChooseColorW  (COMDLG32.@)
1277  *
1278  * Create a color dialog box.
1279  *
1280  * PARAMS
1281  *  lpChCol [I/O] in:  information to initialize the dialog box.
1282  *                out: User's color selection
1283  *
1284  * RETURNS
1285  *  TRUE:  Ok button clicked.
1286  *  FALSE: Cancel button clicked, or error.
1287  */
1288 BOOL WINAPI ChooseColorW( LPCHOOSECOLORW lpChCol )
1289 {
1290     HANDLE hDlgTmpl = 0;
1291     BOOL bRet = FALSE;
1292     LPCVOID template;
1293
1294     TRACE("ChooseColor\n");
1295     if (!lpChCol) return FALSE;
1296
1297     if (lpChCol->Flags & CC_ENABLETEMPLATEHANDLE)
1298     {
1299         if (!(template = LockResource(lpChCol->hInstance)))
1300         {
1301             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1302             return FALSE;
1303         }
1304     }
1305     else if (lpChCol->Flags & CC_ENABLETEMPLATE)
1306     {
1307         HRSRC hResInfo;
1308         if (!(hResInfo = FindResourceW((HINSTANCE)lpChCol->hInstance,
1309                                         lpChCol->lpTemplateName,
1310                                         (LPWSTR)RT_DIALOG)))
1311         {
1312             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1313             return FALSE;
1314         }
1315         if (!(hDlgTmpl = LoadResource((HINSTANCE)lpChCol->hInstance, hResInfo)) ||
1316             !(template = LockResource(hDlgTmpl)))
1317         {
1318             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1319             return FALSE;
1320         }
1321     }
1322     else
1323     {
1324         HRSRC hResInfo;
1325         HGLOBAL hDlgTmpl;
1326         static const WCHAR wszCHOOSE_COLOR[] = {'C','H','O','O','S','E','_','C','O','L','O','R',0};
1327         if (!(hResInfo = FindResourceW(COMDLG32_hInstance, wszCHOOSE_COLOR, (LPWSTR)RT_DIALOG)))
1328         {
1329             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1330             return FALSE;
1331         }
1332         if (!(hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo )) ||
1333             !(template = LockResource(hDlgTmpl)))
1334         {
1335             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1336             return FALSE;
1337         }
1338     }
1339
1340     bRet = DialogBoxIndirectParamW(COMDLG32_hInstance, template, lpChCol->hwndOwner,
1341                      ColorDlgProc, (LPARAM)lpChCol);
1342     return bRet;
1343 }
1344
1345 /***********************************************************************
1346  *            ChooseColorA  (COMDLG32.@)
1347  *
1348  * See ChooseColorW.
1349  */
1350 BOOL WINAPI ChooseColorA( LPCHOOSECOLORA lpChCol )
1351
1352 {
1353   LPWSTR template_name = NULL;
1354   BOOL ret;
1355
1356   LPCHOOSECOLORW lpcc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHOOSECOLORW));
1357   lpcc->lStructSize = sizeof(*lpcc);
1358   lpcc->hwndOwner = lpChCol->hwndOwner;
1359   lpcc->hInstance = lpChCol->hInstance;
1360   lpcc->rgbResult = lpChCol->rgbResult;
1361   lpcc->lpCustColors = lpChCol->lpCustColors;
1362   lpcc->Flags = lpChCol->Flags;
1363   lpcc->lCustData = lpChCol->lCustData;
1364   lpcc->lpfnHook = lpChCol->lpfnHook;
1365   if ((lpcc->Flags & CC_ENABLETEMPLATE) && (lpChCol->lpTemplateName)) {
1366       if (HIWORD(lpChCol->lpTemplateName)) {
1367           INT len = MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, NULL, 0);
1368           template_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1369           MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, template_name, len );
1370           lpcc->lpTemplateName = template_name;
1371       } else {
1372           lpcc->lpTemplateName = (LPCWSTR)lpChCol->lpTemplateName;
1373       }
1374   }
1375
1376   ret = ChooseColorW(lpcc);
1377
1378   if (ret)
1379       lpChCol->rgbResult = lpcc->rgbResult;
1380   HeapFree(GetProcessHeap(), 0, template_name);
1381   HeapFree(GetProcessHeap(), 0, lpcc);
1382   return ret;
1383 }