dinput: Avoid size_t in traces.
[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 ? 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 static 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 = GetPropW( hDlg, szColourDialogProp );
965
966     TRACE("CC_WMCommand wParam=%lx lParam=%lx\n", wParam, lParam);
967     switch (LOWORD(wParam))
968     {
969           case 0x2c2:  /* edit notify RGB */
970           case 0x2c3:
971           case 0x2c4:
972                if (notifyCode == EN_UPDATE && !lpp->updating)
973                          {
974                            i = CC_CheckDigitsInEdit(hwndCtl, 255);
975                            r = GetRValue(lpp->lpcc->rgbResult);
976                            g = GetGValue(lpp->lpcc->rgbResult);
977                            b= GetBValue(lpp->lpcc->rgbResult);
978                            xx = 0;
979                            switch (LOWORD(wParam))
980                            {
981                             case 0x2c2: if ((xx = (i != r))) r = i; break;
982                             case 0x2c3: if ((xx = (i != g))) g = i; break;
983                             case 0x2c4: if ((xx = (i != b))) b = i; break;
984                            }
985                            if (xx) /* something has changed */
986                            {
987                             lpp->lpcc->rgbResult = RGB(r, g, b);
988                             CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
989                             lpp->h = CC_RGBtoHSL('H', r, g, b);
990                             lpp->s = CC_RGBtoHSL('S', r, g, b);
991                             lpp->l = CC_RGBtoHSL('L', r, g, b);
992                             CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
993                             CC_PaintCross(hDlg, lpp->h, lpp->s);
994                             CC_PaintTriangle(hDlg, lpp->l);
995                            }
996                          }
997                  break;
998
999           case 0x2bf:  /* edit notify HSL */
1000           case 0x2c0:
1001           case 0x2c1:
1002                if (notifyCode == EN_UPDATE && !lpp->updating)
1003                          {
1004                            i = CC_CheckDigitsInEdit(hwndCtl , LOWORD(wParam) == 0x2bf ? 239:240);
1005                            xx = 0;
1006                            switch (LOWORD(wParam))
1007                            {
1008                             case 0x2bf: if ((xx = ( i != lpp->h))) lpp->h = i; break;
1009                             case 0x2c0: if ((xx = ( i != lpp->s))) lpp->s = i; break;
1010                             case 0x2c1: if ((xx = ( i != lpp->l))) lpp->l = i; break;
1011                            }
1012                            if (xx) /* something has changed */
1013                            {
1014                             r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1015                             g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1016                             b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1017                             lpp->lpcc->rgbResult = RGB(r, g, b);
1018                             CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1019                             CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1020                             CC_PaintCross(hDlg, lpp->h, lpp->s);
1021                             CC_PaintTriangle(hDlg, lpp->l);
1022                            }
1023                          }
1024                break;
1025
1026           case 0x2cf:
1027                CC_SwitchToFullSize(hDlg, lpp->lpcc->rgbResult, &lpp->fullsize);
1028                SetFocus( GetDlgItem(hDlg, 0x2bf));
1029                break;
1030
1031           case 0x2c8:    /* add colors ... column by column */
1032                cr = lpp->lpcc->lpCustColors;
1033                cr[(lpp->nextuserdef % 2) * 8 + lpp->nextuserdef / 2] = lpp->lpcc->rgbResult;
1034                if (++lpp->nextuserdef == 16)
1035                    lpp->nextuserdef = 0;
1036                CC_PaintUserColorArray(hDlg, 2, 8, lpp->lpcc->lpCustColors);
1037                break;
1038
1039           case 0x2c9:              /* resulting color */
1040                hdc = GetDC(hDlg);
1041                lpp->lpcc->rgbResult = GetNearestColor(hdc, lpp->lpcc->rgbResult);
1042                ReleaseDC(hDlg, hdc);
1043                CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1044                CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1045                r = GetRValue(lpp->lpcc->rgbResult);
1046                g = GetGValue(lpp->lpcc->rgbResult);
1047                b = GetBValue(lpp->lpcc->rgbResult);
1048                lpp->h = CC_RGBtoHSL('H', r, g, b);
1049                lpp->s = CC_RGBtoHSL('S', r, g, b);
1050                lpp->l = CC_RGBtoHSL('L', r, g, b);
1051                CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
1052                CC_PaintCross(hDlg, lpp->h, lpp->s);
1053                CC_PaintTriangle(hDlg, lpp->l);
1054                break;
1055
1056           case 0x40e:           /* Help! */ /* The Beatles, 1965  ;-) */
1057                i = RegisterWindowMessageA(HELPMSGSTRINGA);
1058                    if (lpp->lpcc->hwndOwner)
1059                        SendMessageA(lpp->lpcc->hwndOwner, i, 0, (LPARAM)lpp->lpcc);
1060                    if ( CC_HookCallChk(lpp->lpcc))
1061                        CallWindowProcA( (WNDPROC) lpp->lpcc->lpfnHook, hDlg,
1062                           WM_COMMAND, psh15, (LPARAM)lpp->lpcc);
1063                break;
1064
1065           case IDOK :
1066                 cokmsg = RegisterWindowMessageA(COLOROKSTRINGA);
1067                     if (lpp->lpcc->hwndOwner)
1068                         if (SendMessageA(lpp->lpcc->hwndOwner, cokmsg, 0, (LPARAM)lpp->lpcc))
1069                         break;    /* do NOT close */
1070                 EndDialog(hDlg, 1) ;
1071                 return TRUE ;
1072
1073           case IDCANCEL :
1074                 EndDialog(hDlg, 0) ;
1075                 return TRUE ;
1076
1077        }
1078        return FALSE;
1079 }
1080
1081 /***********************************************************************
1082  *                              CC_WMPaint                    [internal]
1083  */
1084 LRESULT CC_WMPaint( HWND hDlg, WPARAM wParam, LPARAM lParam )
1085 {
1086     PAINTSTRUCT ps;
1087     LPCCPRIV lpp = GetPropW( hDlg, szColourDialogProp );
1088
1089     BeginPaint(hDlg, &ps);
1090     /* we have to paint dialog children except text and buttons */
1091     CC_PaintPredefColorArray(hDlg, 6, 8);
1092     CC_PaintUserColorArray(hDlg, 2, 8, lpp->lpcc->lpCustColors);
1093     CC_PaintLumBar(hDlg, lpp->h, lpp->s);
1094     CC_PaintTriangle(hDlg, lpp->l);
1095     CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1096     CC_PaintColorGraph(hDlg);
1097     CC_PaintCross(hDlg, lpp->h, lpp->s);
1098     EndPaint(hDlg, &ps);
1099
1100     return TRUE;
1101 }
1102
1103 /***********************************************************************
1104  *                              CC_WMLButtonUp              [internal]
1105  */
1106 LRESULT CC_WMLButtonUp( HWND hDlg, WPARAM wParam, LPARAM lParam )
1107 {
1108    LPCCPRIV lpp = GetPropW( hDlg, szColourDialogProp );
1109
1110    if (lpp->capturedGraph)
1111    {
1112        lpp->capturedGraph = 0;
1113        ReleaseCapture();
1114        CC_PaintCross(hDlg, lpp->h, lpp->s);
1115        return 1;
1116    }
1117    return 0;
1118 }
1119
1120 /***********************************************************************
1121  *                              CC_WMMouseMove              [internal]
1122  */
1123 LRESULT CC_WMMouseMove( HWND hDlg, LPARAM lParam )
1124 {
1125    LPCCPRIV lpp = GetPropW( hDlg, szColourDialogProp );
1126    int r, g, b;
1127
1128    if (lpp->capturedGraph)
1129    {
1130       int *ptrh = NULL, *ptrs = &lpp->l;
1131       if (lpp->capturedGraph == 0x2c6)
1132       {
1133           ptrh = &lpp->h;
1134           ptrs = &lpp->s;
1135       }
1136       if (CC_MouseCheckColorGraph( hDlg, lpp->capturedGraph, ptrh, ptrs, lParam))
1137       {
1138           r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1139           g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1140           b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1141           lpp->lpcc->rgbResult = RGB(r, g, b);
1142           CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1143           CC_EditSetHSL(hDlg,lpp->h, lpp->s, lpp->l);
1144           CC_PaintCross(hDlg, lpp->h, lpp->s);
1145           CC_PaintTriangle(hDlg, lpp->l);
1146           CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1147       }
1148       else
1149       {
1150           ReleaseCapture();
1151           lpp->capturedGraph = 0;
1152       }
1153       return 1;
1154    }
1155    return 0;
1156 }
1157
1158 /***********************************************************************
1159  *                              CC_WMLButtonDown              [internal]
1160  */
1161 LRESULT CC_WMLButtonDown( HWND hDlg, WPARAM wParam, LPARAM lParam )
1162 {
1163    LPCCPRIV lpp = GetPropW( hDlg, szColourDialogProp );
1164    int r, g, b, i;
1165    i = 0;
1166
1167    if (CC_MouseCheckPredefColorArray(lpp, hDlg, 0x2d0, 6, 8, lParam))
1168       i = 1;
1169    else
1170       if (CC_MouseCheckUserColorArray(lpp, hDlg, 0x2d1, 2, 8, lParam))
1171          i = 1;
1172       else
1173          if (CC_MouseCheckColorGraph(hDlg, 0x2c6, &lpp->h, &lpp->s, lParam))
1174          {
1175             i = 2;
1176             lpp->capturedGraph = 0x2c6;
1177          }
1178          else
1179             if (CC_MouseCheckColorGraph(hDlg, 0x2be, NULL, &lpp->l, lParam))
1180             {
1181                i = 2;
1182                lpp->capturedGraph = 0x2be;
1183             }
1184    if ( i == 2 )
1185    {
1186       SetCapture(hDlg);
1187       r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1188       g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1189       b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1190       lpp->lpcc->rgbResult = RGB(r, g, b);
1191    }
1192    if ( i == 1 )
1193    {
1194       r = GetRValue(lpp->lpcc->rgbResult);
1195       g = GetGValue(lpp->lpcc->rgbResult);
1196       b = GetBValue(lpp->lpcc->rgbResult);
1197       lpp->h = CC_RGBtoHSL('H', r, g, b);
1198       lpp->s = CC_RGBtoHSL('S', r, g, b);
1199       lpp->l = CC_RGBtoHSL('L', r, g, b);
1200    }
1201    if (i)
1202    {
1203       CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1204       CC_EditSetHSL(hDlg,lpp->h, lpp->s, lpp->l);
1205       CC_PaintCross(hDlg, lpp->h, lpp->s);
1206       CC_PaintTriangle(hDlg, lpp->l);
1207       CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1208       return TRUE;
1209    }
1210    return FALSE;
1211 }
1212
1213 /***********************************************************************
1214  *           ColorDlgProc32 [internal]
1215  *
1216  */
1217 static INT_PTR CALLBACK ColorDlgProc( HWND hDlg, UINT message,
1218                                    WPARAM wParam, LPARAM lParam )
1219 {
1220
1221  int res;
1222  LPCCPRIV lpp = GetPropW( hDlg, szColourDialogProp );
1223
1224  if (message != WM_INITDIALOG)
1225  {
1226   if (!lpp)
1227      return FALSE;
1228   res = 0;
1229   if (CC_HookCallChk(lpp->lpcc))
1230      res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, message, wParam, lParam);
1231   if ( res )
1232      return res;
1233  }
1234
1235  /* FIXME: SetRGB message
1236  if (message && message == msetrgb)
1237     return HandleSetRGB(hDlg, lParam);
1238  */
1239
1240  switch (message)
1241         {
1242           case WM_INITDIALOG:
1243                         return CC_WMInitDialog(hDlg, wParam, lParam);
1244           case WM_NCDESTROY:
1245                         DeleteDC(lpp->hdcMem);
1246                         DeleteObject(lpp->hbmMem);
1247                         HeapFree(GetProcessHeap(), 0, lpp);
1248                         RemovePropW( hDlg, szColourDialogProp );
1249                         break;
1250           case WM_COMMAND:
1251                         if (CC_WMCommand( hDlg, wParam, lParam, HIWORD(wParam), (HWND) lParam))
1252                            return TRUE;
1253                         break;
1254           case WM_PAINT:
1255                         if ( CC_WMPaint(hDlg, wParam, lParam))
1256                            return TRUE;
1257                         break;
1258           case WM_LBUTTONDBLCLK:
1259                         if (CC_MouseCheckResultWindow(hDlg, lParam))
1260                           return TRUE;
1261                         break;
1262           case WM_MOUSEMOVE:
1263                         if (CC_WMMouseMove(hDlg, lParam))
1264                           return TRUE;
1265                         break;
1266           case WM_LBUTTONUP:  /* FIXME: ClipCursor off (if in color graph)*/
1267                         if (CC_WMLButtonUp(hDlg, wParam, lParam))
1268                            return TRUE;
1269                         break;
1270           case WM_LBUTTONDOWN:/* FIXME: ClipCursor on  (if in color graph)*/
1271                         if (CC_WMLButtonDown(hDlg, wParam, lParam))
1272                            return TRUE;
1273                         break;
1274         }
1275      return FALSE ;
1276 }
1277
1278 /***********************************************************************
1279  *            ChooseColorW  (COMDLG32.@)
1280  *
1281  * Create a color dialog box.
1282  *
1283  * PARAMS
1284  *  lpChCol [I/O] in:  information to initialize the dialog box.
1285  *                out: User's color selection
1286  *
1287  * RETURNS
1288  *  TRUE:  Ok button clicked.
1289  *  FALSE: Cancel button clicked, or error.
1290  */
1291 BOOL WINAPI ChooseColorW( LPCHOOSECOLORW lpChCol )
1292 {
1293     HANDLE hDlgTmpl = 0;
1294     BOOL bRet = FALSE;
1295     LPCVOID template;
1296
1297     TRACE("ChooseColor\n");
1298     if (!lpChCol) return FALSE;
1299
1300     if (lpChCol->Flags & CC_ENABLETEMPLATEHANDLE)
1301     {
1302         if (!(template = LockResource(lpChCol->hInstance)))
1303         {
1304             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1305             return FALSE;
1306         }
1307     }
1308     else if (lpChCol->Flags & CC_ENABLETEMPLATE)
1309     {
1310         HRSRC hResInfo;
1311         if (!(hResInfo = FindResourceW((HINSTANCE)lpChCol->hInstance,
1312                                         lpChCol->lpTemplateName,
1313                                         (LPWSTR)RT_DIALOG)))
1314         {
1315             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1316             return FALSE;
1317         }
1318         if (!(hDlgTmpl = LoadResource((HINSTANCE)lpChCol->hInstance, hResInfo)) ||
1319             !(template = LockResource(hDlgTmpl)))
1320         {
1321             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1322             return FALSE;
1323         }
1324     }
1325     else
1326     {
1327         HRSRC hResInfo;
1328         HGLOBAL hDlgTmpl;
1329         static const WCHAR wszCHOOSE_COLOR[] = {'C','H','O','O','S','E','_','C','O','L','O','R',0};
1330         if (!(hResInfo = FindResourceW(COMDLG32_hInstance, wszCHOOSE_COLOR, (LPWSTR)RT_DIALOG)))
1331         {
1332             COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1333             return FALSE;
1334         }
1335         if (!(hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo )) ||
1336             !(template = LockResource(hDlgTmpl)))
1337         {
1338             COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1339             return FALSE;
1340         }
1341     }
1342
1343     bRet = DialogBoxIndirectParamW(COMDLG32_hInstance, template, lpChCol->hwndOwner,
1344                      ColorDlgProc, (LPARAM)lpChCol);
1345     return bRet;
1346 }
1347
1348 /***********************************************************************
1349  *            ChooseColorA  (COMDLG32.@)
1350  *
1351  * See ChooseColorW.
1352  */
1353 BOOL WINAPI ChooseColorA( LPCHOOSECOLORA lpChCol )
1354
1355 {
1356   LPWSTR template_name = NULL;
1357   BOOL ret;
1358
1359   LPCHOOSECOLORW lpcc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHOOSECOLORW));
1360   lpcc->lStructSize = sizeof(*lpcc);
1361   lpcc->hwndOwner = lpChCol->hwndOwner;
1362   lpcc->hInstance = lpChCol->hInstance;
1363   lpcc->rgbResult = lpChCol->rgbResult;
1364   lpcc->lpCustColors = lpChCol->lpCustColors;
1365   lpcc->Flags = lpChCol->Flags;
1366   lpcc->lCustData = lpChCol->lCustData;
1367   lpcc->lpfnHook = lpChCol->lpfnHook;
1368   if ((lpcc->Flags & CC_ENABLETEMPLATE) && (lpChCol->lpTemplateName)) {
1369       if (HIWORD(lpChCol->lpTemplateName)) {
1370           INT len = MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, NULL, 0);
1371           template_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1372           MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, template_name, len );
1373           lpcc->lpTemplateName = template_name;
1374       } else {
1375           lpcc->lpTemplateName = (LPCWSTR)lpChCol->lpTemplateName;
1376       }
1377   }
1378
1379   ret = ChooseColorW(lpcc);
1380
1381   if (ret)
1382       lpChCol->rgbResult = lpcc->rgbResult;
1383   HeapFree(GetProcessHeap(), 0, template_name);
1384   HeapFree(GetProcessHeap(), 0, lpcc);
1385   return ret;
1386 }