user32: Define WM_LBTRACKPOINT if it's not previously defined.
[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/satuation 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  if (PtInRect(&rect, point))
341  {
342   GetClientRect(hwnd, &rect);
343   ScreenToClient(hwnd, &point);
344
345   x = (long) point.x * MAXHORI;
346   x /= rect.right;
347   y = (long) (rect.bottom - point.y) * MAXVERT;
348   y /= rect.bottom;
349
350   if (hori)
351    *hori = x;
352   if (vert)
353    *vert = y;
354   return 1;
355  }
356  else
357   return 0;
358 }
359 /***********************************************************************
360  *                  CC_MouseCheckResultWindow                 [internal]
361  *                  test if double click one of the result colors
362  */
363 int CC_MouseCheckResultWindow( HWND hDlg, LPARAM lParam )
364 {
365  HWND hwnd;
366  POINT point;
367  RECT rect;
368
369  CONV_LPARAMTOPOINT(lParam, &point);
370  ClientToScreen(hDlg, &point);
371  hwnd = GetDlgItem(hDlg, 0x2c5);
372  GetWindowRect(hwnd, &rect);
373  if (PtInRect(&rect, point))
374  {
375   PostMessageA(hDlg, WM_COMMAND, 0x2c9, 0);
376   return 1;
377  }
378  return 0;
379 }
380
381 /***********************************************************************
382  *                       CC_CheckDigitsInEdit                 [internal]
383  */
384 int CC_CheckDigitsInEdit( HWND hwnd, int maxval )
385 {
386  int i, k, m, result, value;
387  long editpos;
388  char buffer[30];
389
390  GetWindowTextA(hwnd, buffer, sizeof(buffer));
391  m = strlen(buffer);
392  result = 0;
393
394  for (i = 0 ; i < m ; i++)
395   if (buffer[i] < '0' || buffer[i] > '9')
396   {
397    for (k = i + 1; k <= m; k++)  /* delete bad character */
398    {
399     buffer[i] = buffer[k];
400     m--;
401    }
402    buffer[m] = 0;
403    result = 1;
404   }
405
406  value = atoi(buffer);
407  if (value > maxval)       /* build a new string */
408  {
409   sprintf(buffer, "%d", maxval);
410   result = 2;
411  }
412  if (result)
413  {
414   editpos = SendMessageA(hwnd, EM_GETSEL, 0, 0);
415   SetWindowTextA(hwnd, buffer );
416   SendMessageA(hwnd, EM_SETSEL, 0, editpos);
417  }
418  return value;
419 }
420
421
422
423 /***********************************************************************
424  *                    CC_PaintSelectedColor                   [internal]
425  */
426 void CC_PaintSelectedColor( HWND hDlg, COLORREF cr )
427 {
428  RECT rect;
429  HDC  hdc;
430  HBRUSH hBrush;
431  HWND hwnd = GetDlgItem(hDlg, 0x2c5);
432  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) ))   /* if full size */
433  {
434   hdc = GetDC(hwnd);
435   GetClientRect(hwnd, &rect) ;
436   hBrush = CreateSolidBrush(cr);
437   if (hBrush)
438   {
439    hBrush = SelectObject(hdc, hBrush) ;
440    Rectangle(hdc, rect.left, rect.top, rect.right/2, rect.bottom);
441    DeleteObject ( SelectObject(hdc, hBrush) ) ;
442    hBrush = CreateSolidBrush( GetNearestColor(hdc, cr) );
443    if (hBrush)
444    {
445     hBrush = SelectObject(hdc, hBrush) ;
446     Rectangle(hdc, rect.right/2-1, rect.top, rect.right, rect.bottom);
447     DeleteObject(SelectObject(hdc, hBrush)) ;
448    }
449   }
450   ReleaseDC(hwnd, hdc);
451  }
452 }
453
454 /***********************************************************************
455  *                    CC_PaintTriangle                        [internal]
456  */
457 void CC_PaintTriangle( HWND hDlg, int y)
458 {
459  HDC hDC;
460  long temp;
461  int w = LOWORD(GetDialogBaseUnits());
462  POINT points[3];
463  int height;
464  int oben;
465  RECT rect;
466  HBRUSH hbr;
467  HWND hwnd = GetDlgItem(hDlg, 0x2be);
468  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
469
470  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6)))   /* if full size */
471  {
472    GetClientRect(hwnd, &rect);
473    height = rect.bottom;
474    hDC = GetDC(hDlg);
475    points[0].y = rect.top;
476    points[0].x = rect.right;     /*  |  /|  */
477    ClientToScreen(hwnd, points); /*  | / |  */
478    ScreenToClient(hDlg, points); /*  |<  |  */
479    oben = points[0].y;           /*  | \ |  */
480                                  /*  |  \|  */
481    temp = (long)height * (long)y;
482    points[0].y = oben + height - temp / (long)MAXVERT;
483    points[1].y = points[0].y + w;
484    points[2].y = points[0].y - w;
485    points[2].x = points[1].x = points[0].x + w;
486
487    hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
488    if (!hbr) hbr = GetSysColorBrush(COLOR_BTNFACE);
489    FillRect(hDC, &lpp->old3angle, hbr);
490    lpp->old3angle.left  = points[0].x;
491    lpp->old3angle.right = points[1].x + 1;
492    lpp->old3angle.top   = points[2].y - 1;
493    lpp->old3angle.bottom= points[1].y + 1;
494    Polygon(hDC, points, 3);
495    ReleaseDC(hDlg, hDC);
496  }
497 }
498
499
500 /***********************************************************************
501  *                    CC_PaintCross                           [internal]
502  */
503 void CC_PaintCross( HWND hDlg, int x, int y)
504 {
505  HDC hDC;
506  int w = GetDialogBaseUnits();
507  HWND hwnd = GetDlgItem(hDlg, 0x2c6);
508  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
509  RECT rect;
510  POINT point, p;
511  HPEN hPen;
512
513  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) ))   /* if full size */
514  {
515    GetClientRect(hwnd, &rect);
516    hDC = GetDC(hwnd);
517    SelectClipRgn( hDC, CreateRectRgnIndirect(&rect));
518    hPen = CreatePen(PS_SOLID, 2, 0xffffff); /* -white- color */
519    hPen = SelectObject(hDC, hPen);
520    point.x = ((long)rect.right * (long)x) / (long)MAXHORI;
521    point.y = rect.bottom - ((long)rect.bottom * (long)y) / (long)MAXVERT;
522    if ( lpp->oldcross.left != lpp->oldcross.right )
523      BitBlt(hDC, lpp->oldcross.left, lpp->oldcross.top,
524               lpp->oldcross.right - lpp->oldcross.left,
525               lpp->oldcross.bottom - lpp->oldcross.top,
526               lpp->hdcMem, lpp->oldcross.left, lpp->oldcross.top, SRCCOPY);
527    lpp->oldcross.left   = point.x - w - 1;
528    lpp->oldcross.right  = point.x + w + 1;
529    lpp->oldcross.top    = point.y - w - 1;
530    lpp->oldcross.bottom = point.y + w + 1;
531
532    MoveToEx(hDC, point.x - w, point.y, &p);
533    LineTo(hDC, point.x + w, point.y);
534    MoveToEx(hDC, point.x, point.y - w, &p);
535    LineTo(hDC, point.x, point.y + w);
536    DeleteObject( SelectObject(hDC, hPen)) ;
537    ReleaseDC(hwnd, hDC);
538  }
539 }
540
541
542 #define XSTEPS 48
543 #define YSTEPS 24
544
545
546 /***********************************************************************
547  *                    CC_PrepareColorGraph                    [internal]
548  */
549 static void CC_PrepareColorGraph( HWND hDlg )
550 {
551  int sdif, hdif, xdif, ydif, r, g, b, hue, sat;
552  HWND hwnd = GetDlgItem(hDlg, 0x2c6);
553  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
554  HBRUSH hbrush;
555  HDC hdc ;
556  RECT rect, client;
557  HCURSOR hcursor = SetCursor( LoadCursorW(0, (LPCWSTR)IDC_WAIT) );
558
559  GetClientRect(hwnd, &client);
560  hdc = GetDC(hwnd);
561  lpp->hdcMem = CreateCompatibleDC(hdc);
562  lpp->hbmMem = CreateCompatibleBitmap(hdc, client.right, client.bottom);
563  SelectObject(lpp->hdcMem, lpp->hbmMem);
564
565  xdif = client.right / XSTEPS;
566  ydif = client.bottom / YSTEPS+1;
567  hdif = 239 / XSTEPS;
568  sdif = 240 / YSTEPS;
569  for (rect.left = hue = 0; hue < 239 + hdif; hue += hdif)
570  {
571   rect.right = rect.left + xdif;
572   rect.bottom = client.bottom;
573   for(sat = 0; sat < 240 + sdif; sat += sdif)
574   {
575    rect.top = rect.bottom - ydif;
576    r = CC_HSLtoRGB('R', hue, sat, 120);
577    g = CC_HSLtoRGB('G', hue, sat, 120);
578    b = CC_HSLtoRGB('B', hue, sat, 120);
579    hbrush = CreateSolidBrush( RGB(r, g, b));
580    FillRect(lpp->hdcMem, &rect, hbrush);
581    DeleteObject(hbrush);
582    rect.bottom = rect.top;
583   }
584   rect.left = rect.right;
585  }
586  ReleaseDC(hwnd, hdc);
587  SetCursor(hcursor);
588 }
589
590 /***********************************************************************
591  *                          CC_PaintColorGraph                [internal]
592  */
593 static void CC_PaintColorGraph( HWND hDlg )
594 {
595  HWND hwnd = GetDlgItem( hDlg, 0x2c6 );
596  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
597  HDC  hDC;
598  RECT rect;
599  if (IsWindowVisible(hwnd))   /* if full size */
600  {
601   if (!lpp->hdcMem)
602    CC_PrepareColorGraph(hDlg);   /* should not be necessary */
603
604   hDC = GetDC(hwnd);
605   GetClientRect(hwnd, &rect);
606   if (lpp->hdcMem)
607       BitBlt(hDC, 0, 0, rect.right, rect.bottom, lpp->hdcMem, 0, 0, SRCCOPY);
608   else
609       WARN("choose color: hdcMem is not defined\n");
610   ReleaseDC(hwnd, hDC);
611  }
612 }
613
614 /***********************************************************************
615  *                           CC_PaintLumBar                   [internal]
616  */
617 static void CC_PaintLumBar( HWND hDlg, int hue, int sat )
618 {
619  HWND hwnd = GetDlgItem(hDlg, 0x2be);
620  RECT rect, client;
621  int lum, ldif, ydif, r, g, b;
622  HBRUSH hbrush;
623  HDC hDC;
624
625  if (IsWindowVisible(hwnd))
626  {
627   hDC = GetDC(hwnd);
628   GetClientRect(hwnd, &client);
629   rect = client;
630
631   ldif = 240 / YSTEPS;
632   ydif = client.bottom / YSTEPS+1;
633   for (lum = 0; lum < 240 + ldif; lum += ldif)
634   {
635    rect.top = max(0, rect.bottom - ydif);
636    r = CC_HSLtoRGB('R', hue, sat, lum);
637    g = CC_HSLtoRGB('G', hue, sat, lum);
638    b = CC_HSLtoRGB('B', hue, sat, lum);
639    hbrush = CreateSolidBrush( RGB(r, g, b) );
640    FillRect(hDC, &rect, hbrush);
641    DeleteObject(hbrush);
642    rect.bottom = rect.top;
643   }
644   GetClientRect(hwnd, &rect);
645   FrameRect(hDC, &rect, GetStockObject(BLACK_BRUSH) );
646   ReleaseDC(hwnd, hDC);
647  }
648 }
649
650 /***********************************************************************
651  *                             CC_EditSetRGB                  [internal]
652  */
653 void CC_EditSetRGB( HWND hDlg, COLORREF cr )
654 {
655  char buffer[10];
656  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
657  int r = GetRValue(cr);
658  int g = GetGValue(cr);
659  int b = GetBValue(cr);
660  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) ))   /* if full size */
661  {
662    lpp->updating = TRUE;
663    sprintf(buffer, "%d", r);
664    SetWindowTextA( GetDlgItem(hDlg, 0x2c2), buffer);
665    sprintf(buffer, "%d", g);
666    SetWindowTextA( GetDlgItem(hDlg, 0x2c3), buffer);
667    sprintf( buffer, "%d", b );
668    SetWindowTextA( GetDlgItem(hDlg, 0x2c4),buffer);
669    lpp->updating = FALSE;
670  }
671 }
672
673 /***********************************************************************
674  *                             CC_EditSetHSL                  [internal]
675  */
676 void CC_EditSetHSL( HWND hDlg, int h, int s, int l )
677 {
678  char buffer[10];
679  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
680
681  if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) ))   /* if full size */
682  {
683    lpp->updating = TRUE;
684    sprintf(buffer, "%d", h);
685    SetWindowTextA( GetDlgItem(hDlg, 0x2bf), buffer);
686    sprintf(buffer, "%d", s);
687    SetWindowTextA( GetDlgItem(hDlg, 0x2c0), buffer);
688    sprintf(buffer, "%d", l);
689    SetWindowTextA( GetDlgItem(hDlg, 0x2c1), buffer);
690    lpp->updating = FALSE;
691  }
692  CC_PaintLumBar(hDlg, h, s);
693 }
694
695 /***********************************************************************
696  *                       CC_SwitchToFullSize                  [internal]
697  */
698 void CC_SwitchToFullSize( HWND hDlg, COLORREF result, LPCRECT lprect )
699 {
700  int i;
701  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
702
703  EnableWindow( GetDlgItem(hDlg, 0x2cf), FALSE);
704  CC_PrepareColorGraph(hDlg);
705  for (i = 0x2bf; i < 0x2c5; i++)
706    ShowWindow( GetDlgItem(hDlg, i), SW_SHOW);
707  for (i = 0x2d3; i < 0x2d9; i++)
708    ShowWindow( GetDlgItem(hDlg, i), SW_SHOW);
709  ShowWindow( GetDlgItem(hDlg, 0x2c9), SW_SHOW);
710  ShowWindow( GetDlgItem(hDlg, 0x2c8), SW_SHOW);
711  ShowWindow( GetDlgItem(hDlg, 1090), SW_SHOW);
712
713  if (lprect)
714   SetWindowPos(hDlg, 0, 0, 0, lprect->right-lprect->left,
715    lprect->bottom-lprect->top, SWP_NOMOVE|SWP_NOZORDER);
716
717  ShowWindow( GetDlgItem(hDlg, 0x2be), SW_SHOW);
718  ShowWindow( GetDlgItem(hDlg, 0x2c5), SW_SHOW);
719
720  CC_EditSetRGB(hDlg, result);
721  CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
722  ShowWindow( GetDlgItem( hDlg, 0x2c6), SW_SHOW);
723  UpdateWindow( GetDlgItem(hDlg, 0x2c6) );
724 }
725
726 /***********************************************************************
727  *                           CC_PaintPredefColorArray         [internal]
728  *                Paints the default standard 48 colors
729  */
730 static void CC_PaintPredefColorArray( HWND hDlg, int rows, int cols)
731 {
732  HWND hwnd = GetDlgItem(hDlg, 0x2d0);
733  RECT rect;
734  HDC  hdc;
735  HBRUSH hBrush;
736  int dx, dy, i, j, k;
737  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
738
739  GetClientRect(hwnd, &rect);
740  dx = rect.right / cols;
741  dy = rect.bottom / rows;
742  k = rect.left;
743
744  hdc = GetDC(hwnd);
745  GetClientRect(hwnd, &rect);
746  hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
747  if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
748  FillRect(hdc, &rect, hBrush);
749  for ( j = 0; j < rows; j++ )
750  {
751   for ( i = 0; i < cols; i++ )
752   {
753    hBrush = CreateSolidBrush(predefcolors[j][i]);
754    if (hBrush)
755    {
756     hBrush = SelectObject(hdc, hBrush);
757     Rectangle(hdc, rect.left, rect.top,
758                 rect.left + dx - DISTANCE, rect.top + dy - DISTANCE);
759     rect.left = rect.left + dx;
760     DeleteObject(SelectObject(hdc, hBrush)) ;
761    }
762   }
763   rect.top = rect.top + dy;
764   rect.left = k;
765  }
766  ReleaseDC(hwnd, hdc);
767  if (lpp->hwndFocus == hwnd)
768    CC_DrawCurrentFocusRect(lpp);
769 }
770 /***********************************************************************
771  *                             CC_PaintUserColorArray         [internal]
772  *               Paint the 16 user-selected colors
773  */
774 void CC_PaintUserColorArray( HWND hDlg, int rows, int cols, const COLORREF *lpcr )
775 {
776  HWND hwnd = GetDlgItem(hDlg, 0x2d1);
777  RECT rect;
778  HDC  hdc;
779  HBRUSH hBrush;
780  int dx, dy, i, j, k;
781  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
782
783  GetClientRect(hwnd, &rect);
784
785  dx = rect.right / cols;
786  dy = rect.bottom / rows;
787  k = rect.left;
788
789  hdc = GetDC(hwnd);
790  if (hdc)
791  {
792   hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
793   if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
794   FillRect( hdc, &rect, hBrush );
795   for (j = 0; j < rows; j++)
796   {
797    for (i = 0; i < cols; i++)
798    {
799     hBrush = CreateSolidBrush(lpcr[i+j*cols]);
800     if (hBrush)
801     {
802      hBrush = SelectObject(hdc, hBrush) ;
803      Rectangle(hdc, rect.left, rect.top,
804                   rect.left + dx - DISTANCE, rect.top + dy - DISTANCE);
805      rect.left = rect.left + dx;
806      DeleteObject( SelectObject(hdc, hBrush) ) ;
807     }
808    }
809    rect.top = rect.top + dy;
810    rect.left = k;
811   }
812   ReleaseDC(hwnd, hdc);
813  }
814  if (lpp->hwndFocus == hwnd)
815    CC_DrawCurrentFocusRect(lpp);
816 }
817
818
819
820 /***********************************************************************
821  *                             CC_HookCallChk                 [internal]
822  */
823 BOOL CC_HookCallChk( const CHOOSECOLORW *lpcc )
824 {
825  if (lpcc)
826   if(lpcc->Flags & CC_ENABLEHOOK)
827    if (lpcc->lpfnHook)
828     return TRUE;
829  return FALSE;
830 }
831
832 /***********************************************************************
833  *                              CC_WMInitDialog                  [internal]
834  */
835 static LONG CC_WMInitDialog( HWND hDlg, WPARAM wParam, LPARAM lParam )
836 {
837    int i, res;
838    int r, g, b;
839    HWND hwnd;
840    RECT rect;
841    POINT point;
842    LPCCPRIV lpp;
843
844    TRACE("WM_INITDIALOG lParam=%08lX\n", lParam);
845    lpp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct CCPRIVATE) );
846
847    lpp->lpcc = (LPCHOOSECOLORW) lParam;
848    if (lpp->lpcc->lStructSize != sizeof(CHOOSECOLORW) )
849    {
850        HeapFree(GetProcessHeap(), 0, lpp);
851        EndDialog (hDlg, 0) ;
852        return FALSE;
853    }
854
855    SetPropW( hDlg, szColourDialogProp, lpp );
856
857    if (!(lpp->lpcc->Flags & CC_SHOWHELP))
858       ShowWindow( GetDlgItem(hDlg,0x40e), SW_HIDE);
859    lpp->msetrgb = RegisterWindowMessageA(SETRGBSTRINGA);
860
861 #if 0
862    cpos = MAKELONG(5,7); /* init */
863    if (lpp->lpcc->Flags & CC_RGBINIT)
864    {
865      for (i = 0; i < 6; i++)
866        for (j = 0; j < 8; j++)
867         if (predefcolors[i][j] == lpp->lpcc->rgbResult)
868         {
869           cpos = MAKELONG(i,j);
870           goto found;
871         }
872    }
873    found:
874    /* FIXME: Draw_a_focus_rect & set_init_values */
875 #endif
876
877    GetWindowRect(hDlg, &lpp->fullsize);
878    if (lpp->lpcc->Flags & CC_FULLOPEN || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
879    {
880       hwnd = GetDlgItem(hDlg, 0x2cf);
881       EnableWindow(hwnd, FALSE);
882    }
883    if (!(lpp->lpcc->Flags & CC_FULLOPEN ) || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
884    {
885       rect = lpp->fullsize;
886       res = rect.bottom - rect.top;
887       hwnd = GetDlgItem(hDlg, 0x2c6); /* cut at left border */
888       point.x = point.y = 0;
889       ClientToScreen(hwnd, &point);
890       ScreenToClient(hDlg,&point);
891       GetClientRect(hDlg, &rect);
892       point.x += GetSystemMetrics(SM_CXDLGFRAME);
893       SetWindowPos(hDlg, 0, 0, 0, point.x, res, SWP_NOMOVE|SWP_NOZORDER);
894
895       for (i = 0x2bf; i < 0x2c5; i++)
896          ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
897       for (i = 0x2d3; i < 0x2d9; i++)
898          ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
899       ShowWindow( GetDlgItem(hDlg, 0x2c9), SW_HIDE);
900       ShowWindow( GetDlgItem(hDlg, 0x2c8), SW_HIDE);
901       ShowWindow( GetDlgItem(hDlg, 0x2c6), SW_HIDE);
902       ShowWindow( GetDlgItem(hDlg, 0x2c5), SW_HIDE);
903       ShowWindow( GetDlgItem(hDlg, 1090 ), SW_HIDE);
904    }
905    else
906       CC_SwitchToFullSize(hDlg, lpp->lpcc->rgbResult, NULL);
907    res = TRUE;
908    for (i = 0x2bf; i < 0x2c5; i++)
909      SendMessageA( GetDlgItem(hDlg, i), EM_LIMITTEXT, 3, 0);  /* max 3 digits:  xyz  */
910    if (CC_HookCallChk(lpp->lpcc))
911    {
912           res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, WM_INITDIALOG, wParam, lParam);
913    }
914
915    /* Set the initial values of the color chooser dialog */
916    r = GetRValue(lpp->lpcc->rgbResult);
917    g = GetGValue(lpp->lpcc->rgbResult);
918    b = GetBValue(lpp->lpcc->rgbResult);
919
920    CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
921    lpp->h = CC_RGBtoHSL('H', r, g, b);
922    lpp->s = CC_RGBtoHSL('S', r, g, b);
923    lpp->l = CC_RGBtoHSL('L', r, g, b);
924
925    /* Doing it the long way because CC_EditSetRGB/HSL doesn't seem to work */
926    SetDlgItemInt(hDlg, 703, lpp->h, TRUE);
927    SetDlgItemInt(hDlg, 704, lpp->s, TRUE);
928    SetDlgItemInt(hDlg, 705, lpp->l, TRUE);
929    SetDlgItemInt(hDlg, 706, r, TRUE);
930    SetDlgItemInt(hDlg, 707, g, TRUE);
931    SetDlgItemInt(hDlg, 708, b, TRUE);
932
933    CC_PaintCross(hDlg, lpp->h, lpp->s);
934    CC_PaintTriangle(hDlg, lpp->l);
935
936    return res;
937 }
938
939
940 /***********************************************************************
941  *                              CC_WMCommand                  [internal]
942  */
943 LRESULT CC_WMCommand( HWND hDlg, WPARAM wParam, LPARAM lParam, WORD notifyCode, HWND hwndCtl )
944 {
945     int  r, g, b, i, xx;
946     UINT cokmsg;
947     HDC hdc;
948     COLORREF *cr;
949     LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
950     TRACE("CC_WMCommand wParam=%lx lParam=%lx\n", wParam, lParam);
951     switch (LOWORD(wParam))
952     {
953           case 0x2c2:  /* edit notify RGB */
954           case 0x2c3:
955           case 0x2c4:
956                if (notifyCode == EN_UPDATE && !lpp->updating)
957                          {
958                            i = CC_CheckDigitsInEdit(hwndCtl, 255);
959                            r = GetRValue(lpp->lpcc->rgbResult);
960                            g = GetGValue(lpp->lpcc->rgbResult);
961                            b= GetBValue(lpp->lpcc->rgbResult);
962                            xx = 0;
963                            switch (LOWORD(wParam))
964                            {
965                             case 0x2c2: if ((xx = (i != r))) r = i; break;
966                             case 0x2c3: if ((xx = (i != g))) g = i; break;
967                             case 0x2c4: if ((xx = (i != b))) b = i; break;
968                            }
969                            if (xx) /* something has changed */
970                            {
971                             lpp->lpcc->rgbResult = RGB(r, g, b);
972                             CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
973                             lpp->h = CC_RGBtoHSL('H', r, g, b);
974                             lpp->s = CC_RGBtoHSL('S', r, g, b);
975                             lpp->l = CC_RGBtoHSL('L', r, g, b);
976                             CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
977                             CC_PaintCross(hDlg, lpp->h, lpp->s);
978                             CC_PaintTriangle(hDlg, lpp->l);
979                            }
980                          }
981                  break;
982
983           case 0x2bf:  /* edit notify HSL */
984           case 0x2c0:
985           case 0x2c1:
986                if (notifyCode == EN_UPDATE && !lpp->updating)
987                          {
988                            i = CC_CheckDigitsInEdit(hwndCtl , LOWORD(wParam) == 0x2bf ? 239:240);
989                            xx = 0;
990                            switch (LOWORD(wParam))
991                            {
992                             case 0x2bf: if ((xx = ( i != lpp->h))) lpp->h = i; break;
993                             case 0x2c0: if ((xx = ( i != lpp->s))) lpp->s = i; break;
994                             case 0x2c1: if ((xx = ( i != lpp->l))) lpp->l = i; break;
995                            }
996                            if (xx) /* something has changed */
997                            {
998                             r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
999                             g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1000                             b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1001                             lpp->lpcc->rgbResult = RGB(r, g, b);
1002                             CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1003                             CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1004                             CC_PaintCross(hDlg, lpp->h, lpp->s);
1005                             CC_PaintTriangle(hDlg, lpp->l);
1006                            }
1007                          }
1008                break;
1009
1010           case 0x2cf:
1011                CC_SwitchToFullSize(hDlg, lpp->lpcc->rgbResult, &lpp->fullsize);
1012                SetFocus( GetDlgItem(hDlg, 0x2bf));
1013                break;
1014
1015           case 0x2c8:    /* add colors ... column by column */
1016                cr = lpp->lpcc->lpCustColors;
1017                cr[(lpp->nextuserdef % 2) * 8 + lpp->nextuserdef / 2] = lpp->lpcc->rgbResult;
1018                if (++lpp->nextuserdef == 16)
1019                    lpp->nextuserdef = 0;
1020                CC_PaintUserColorArray(hDlg, 2, 8, lpp->lpcc->lpCustColors);
1021                break;
1022
1023           case 0x2c9:              /* resulting color */
1024                hdc = GetDC(hDlg);
1025                lpp->lpcc->rgbResult = GetNearestColor(hdc, lpp->lpcc->rgbResult);
1026                ReleaseDC(hDlg, hdc);
1027                CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1028                CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1029                r = GetRValue(lpp->lpcc->rgbResult);
1030                g = GetGValue(lpp->lpcc->rgbResult);
1031                b = GetBValue(lpp->lpcc->rgbResult);
1032                lpp->h = CC_RGBtoHSL('H', r, g, b);
1033                lpp->s = CC_RGBtoHSL('S', r, g, b);
1034                lpp->l = CC_RGBtoHSL('L', r, g, b);
1035                CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
1036                CC_PaintCross(hDlg, lpp->h, lpp->s);
1037                CC_PaintTriangle(hDlg, lpp->l);
1038                break;
1039
1040           case 0x40e:           /* Help! */ /* The Beatles, 1965  ;-) */
1041                i = RegisterWindowMessageA(HELPMSGSTRINGA);
1042                    if (lpp->lpcc->hwndOwner)
1043                        SendMessageA(lpp->lpcc->hwndOwner, i, 0, (LPARAM)lpp->lpcc);
1044                    if ( CC_HookCallChk(lpp->lpcc))
1045                        CallWindowProcA( (WNDPROC) lpp->lpcc->lpfnHook, hDlg,
1046                           WM_COMMAND, psh15, (LPARAM)lpp->lpcc);
1047                break;
1048
1049           case IDOK :
1050                 cokmsg = RegisterWindowMessageA(COLOROKSTRINGA);
1051                     if (lpp->lpcc->hwndOwner)
1052                         if (SendMessageA(lpp->lpcc->hwndOwner, cokmsg, 0, (LPARAM)lpp->lpcc))
1053                         break;    /* do NOT close */
1054                 EndDialog(hDlg, 1) ;
1055                 return TRUE ;
1056
1057           case IDCANCEL :
1058                 EndDialog(hDlg, 0) ;
1059                 return TRUE ;
1060
1061        }
1062        return FALSE;
1063 }
1064
1065 /***********************************************************************
1066  *                              CC_WMPaint                    [internal]
1067  */
1068 LRESULT CC_WMPaint( HWND hDlg, WPARAM wParam, LPARAM lParam )
1069 {
1070     PAINTSTRUCT ps;
1071     LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1072
1073     BeginPaint(hDlg, &ps);
1074     /* we have to paint dialog children except text and buttons */
1075     CC_PaintPredefColorArray(hDlg, 6, 8);
1076     CC_PaintUserColorArray(hDlg, 2, 8, lpp->lpcc->lpCustColors);
1077     CC_PaintLumBar(hDlg, lpp->h, lpp->s);
1078     CC_PaintCross(hDlg, lpp->h, lpp->s);
1079     CC_PaintTriangle(hDlg, lpp->l);
1080     CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1081     CC_PaintColorGraph(hDlg);
1082     EndPaint(hDlg, &ps);
1083
1084     return TRUE;
1085 }
1086
1087 /***********************************************************************
1088  *                              CC_WMLButtonUp              [internal]
1089  */
1090 LRESULT CC_WMLButtonUp( HWND hDlg, WPARAM wParam, LPARAM lParam )
1091 {
1092    LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1093    if (lpp->capturedGraph)
1094    {
1095        lpp->capturedGraph = 0;
1096        ReleaseCapture();
1097        CC_PaintCross(hDlg, lpp->h, lpp->s);
1098        return 1;
1099    }
1100    return 0;
1101 }
1102
1103 /***********************************************************************
1104  *                              CC_WMMouseMove              [internal]
1105  */
1106 LRESULT CC_WMMouseMove( HWND hDlg, LPARAM lParam )
1107 {
1108    LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1109    int r, g, b;
1110
1111    if (lpp->capturedGraph)
1112    {
1113       int *ptrh = NULL, *ptrs = &lpp->l;
1114       if (lpp->capturedGraph == 0x2c6)
1115       {
1116           ptrh = &lpp->h;
1117           ptrs = &lpp->s;
1118       }
1119       if (CC_MouseCheckColorGraph( hDlg, lpp->capturedGraph, ptrh, ptrs, lParam))
1120       {
1121           r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1122           g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1123           b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1124           lpp->lpcc->rgbResult = RGB(r, g, b);
1125           CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1126           CC_EditSetHSL(hDlg,lpp->h, lpp->s, lpp->l);
1127           CC_PaintCross(hDlg, lpp->h, lpp->s);
1128           CC_PaintTriangle(hDlg, lpp->l);
1129           CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1130       }
1131       else
1132       {
1133           ReleaseCapture();
1134           lpp->capturedGraph = 0;
1135       }
1136       return 1;
1137    }
1138    return 0;
1139 }
1140
1141 /***********************************************************************
1142  *                              CC_WMLButtonDown              [internal]
1143  */
1144 LRESULT CC_WMLButtonDown( HWND hDlg, WPARAM wParam, LPARAM lParam )
1145 {
1146    LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1147    int r, g, b, i;
1148    i = 0;
1149
1150    if (CC_MouseCheckPredefColorArray(lpp, hDlg, 0x2d0, 6, 8, lParam))
1151       i = 1;
1152    else
1153       if (CC_MouseCheckUserColorArray(lpp, hDlg, 0x2d1, 2, 8, lParam))
1154          i = 1;
1155       else
1156          if (CC_MouseCheckColorGraph(hDlg, 0x2c6, &lpp->h, &lpp->s, lParam))
1157          {
1158             i = 2;
1159             lpp->capturedGraph = 0x2c6;
1160          }
1161          else
1162             if (CC_MouseCheckColorGraph(hDlg, 0x2be, NULL, &lpp->l, lParam))
1163             {
1164                i = 2;
1165                lpp->capturedGraph = 0x2be;
1166             }
1167    if ( i == 2 )
1168    {
1169       SetCapture(hDlg);
1170       r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1171       g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1172       b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1173       lpp->lpcc->rgbResult = RGB(r, g, b);
1174    }
1175    if ( i == 1 )
1176    {
1177       r = GetRValue(lpp->lpcc->rgbResult);
1178       g = GetGValue(lpp->lpcc->rgbResult);
1179       b = GetBValue(lpp->lpcc->rgbResult);
1180       lpp->h = CC_RGBtoHSL('H', r, g, b);
1181       lpp->s = CC_RGBtoHSL('S', r, g, b);
1182       lpp->l = CC_RGBtoHSL('L', r, g, b);
1183    }
1184    if (i)
1185    {
1186       CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1187       CC_EditSetHSL(hDlg,lpp->h, lpp->s, lpp->l);
1188       CC_PaintCross(hDlg, lpp->h, lpp->s);
1189       CC_PaintTriangle(hDlg, lpp->l);
1190       CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1191       return TRUE;
1192    }
1193    return FALSE;
1194 }
1195
1196 /***********************************************************************
1197  *           ColorDlgProc32 [internal]
1198  *
1199  */
1200 static INT_PTR CALLBACK ColorDlgProc( HWND hDlg, UINT message,
1201                                    WPARAM wParam, LPARAM lParam )
1202 {
1203
1204  int res;
1205  LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1206  if (message != WM_INITDIALOG)
1207  {
1208   if (!lpp)
1209      return FALSE;
1210   res = 0;
1211   if (CC_HookCallChk(lpp->lpcc))
1212      res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, message, wParam, lParam);
1213   if ( res )
1214      return res;
1215  }
1216
1217  /* FIXME: SetRGB message
1218  if (message && message == msetrgb)
1219     return HandleSetRGB(hDlg, lParam);
1220  */
1221
1222  switch (message)
1223         {
1224           case WM_INITDIALOG:
1225                         return CC_WMInitDialog(hDlg, wParam, lParam);
1226           case WM_NCDESTROY:
1227                         DeleteDC(lpp->hdcMem);
1228                         DeleteObject(lpp->hbmMem);
1229                         HeapFree(GetProcessHeap(), 0, lpp);
1230                         RemovePropW( hDlg, szColourDialogProp );
1231                         break;
1232           case WM_COMMAND:
1233                         if (CC_WMCommand( hDlg, wParam, lParam, HIWORD(wParam), (HWND) lParam))
1234                            return TRUE;
1235                         break;
1236           case WM_PAINT:
1237                         if ( CC_WMPaint(hDlg, wParam, lParam))
1238                            return TRUE;
1239                         break;
1240           case WM_LBUTTONDBLCLK:
1241                         if (CC_MouseCheckResultWindow(hDlg, lParam))
1242                           return TRUE;
1243                         break;
1244           case WM_MOUSEMOVE:
1245                         if (CC_WMMouseMove(hDlg, lParam))
1246                           return TRUE;
1247                         break;
1248           case WM_LBUTTONUP:  /* FIXME: ClipCursor off (if in color graph)*/
1249                         if (CC_WMLButtonUp(hDlg, wParam, lParam))
1250                            return TRUE;
1251                         break;
1252           case WM_LBUTTONDOWN:/* FIXME: ClipCursor on  (if in color graph)*/
1253                         if (CC_WMLButtonDown(hDlg, wParam, lParam))
1254                            return TRUE;
1255                         break;
1256         }
1257      return FALSE ;
1258 }
1259
1260 /***********************************************************************
1261  *            ChooseColorW  (COMDLG32.@)
1262  *
1263  * Create a color dialog box.
1264  *
1265  * PARAMS
1266  *  lpChCol [I/O] in:  information to initialize the dialog box.
1267  *                out: User's color selection
1268  *
1269  * RETURNS
1270  *  TRUE:  Ok button clicked.
1271  *  FALSE: Cancel button clicked, or error.
1272  */
1273 BOOL WINAPI ChooseColorW( LPCHOOSECOLORW lpChCol )
1274 {
1275     HANDLE hDlgTmpl = 0;
1276     BOOL bRet = FALSE;
1277     LPCVOID template;
1278
1279     TRACE("ChooseColor\n");
1280     if (!lpChCol) return FALSE;
1281
1282     if (lpChCol->Flags & CC_ENABLETEMPLATEHANDLE)
1283     {
1284         if (!(template = LockResource(lpChCol->hInstance)))
1285         {
1286             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1287             return FALSE;
1288         }
1289     }
1290     else if (lpChCol->Flags & CC_ENABLETEMPLATE)
1291     {
1292         HRSRC hResInfo;
1293         if (!(hResInfo = FindResourceW((HINSTANCE)lpChCol->hInstance,
1294                                         lpChCol->lpTemplateName,
1295                                         (LPWSTR)RT_DIALOG)))
1296         {
1297             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1298             return FALSE;
1299         }
1300         if (!(hDlgTmpl = LoadResource((HINSTANCE)lpChCol->hInstance, hResInfo)) ||
1301             !(template = LockResource(hDlgTmpl)))
1302         {
1303             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1304             return FALSE;
1305         }
1306     }
1307     else
1308     {
1309         HRSRC hResInfo;
1310         HGLOBAL hDlgTmpl;
1311         static const WCHAR wszCHOOSE_COLOR[] = {'C','H','O','O','S','E','_','C','O','L','O','R',0};
1312         if (!(hResInfo = FindResourceW(COMDLG32_hInstance, wszCHOOSE_COLOR, (LPWSTR)RT_DIALOG)))
1313         {
1314             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1315             return FALSE;
1316         }
1317         if (!(hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo )) ||
1318             !(template = LockResource(hDlgTmpl)))
1319         {
1320             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1321             return FALSE;
1322         }
1323     }
1324
1325     bRet = DialogBoxIndirectParamW(COMDLG32_hInstance, template, lpChCol->hwndOwner,
1326                      ColorDlgProc, (LPARAM)lpChCol);
1327     return bRet;
1328 }
1329
1330 /***********************************************************************
1331  *            ChooseColorA  (COMDLG32.@)
1332  *
1333  * See ChooseColorW.
1334  */
1335 BOOL WINAPI ChooseColorA( LPCHOOSECOLORA lpChCol )
1336
1337 {
1338   LPWSTR template_name = NULL;
1339   BOOL ret;
1340
1341   LPCHOOSECOLORW lpcc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHOOSECOLORW));
1342   lpcc->lStructSize = sizeof(*lpcc);
1343   lpcc->hwndOwner = lpChCol->hwndOwner;
1344   lpcc->hInstance = lpChCol->hInstance;
1345   lpcc->rgbResult = lpChCol->rgbResult;
1346   lpcc->lpCustColors = lpChCol->lpCustColors;
1347   lpcc->Flags = lpChCol->Flags;
1348   lpcc->lCustData = lpChCol->lCustData;
1349   lpcc->lpfnHook = (LPCCHOOKPROC) lpChCol->lpfnHook;
1350   if ((lpcc->Flags & CC_ENABLETEMPLATE) && (lpChCol->lpTemplateName)) {
1351       if (HIWORD(lpChCol->lpTemplateName)) {
1352           INT len = MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, NULL, 0);
1353           template_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1354           MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, template_name, len );
1355           lpcc->lpTemplateName = template_name;
1356       } else {
1357           lpcc->lpTemplateName = (LPCWSTR)lpChCol->lpTemplateName;
1358       }
1359   }
1360
1361   ret = ChooseColorW(lpcc);
1362
1363   if (ret)
1364       lpChCol->rgbResult = lpcc->rgbResult;
1365   HeapFree(GetProcessHeap(), 0, template_name);
1366   HeapFree(GetProcessHeap(), 0, lpcc);
1367   return ret;
1368 }