winedevice: Get rid of global driver_hkey variable and fix some leaks.
[wine.git] / dlls / comdlg32 / colordlg.c
blob8b307aecb1639c29b9d76100b307b2f1f6924164
1 /*
2 * COMMDLG - Color Dialog
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
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.
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.
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
22 /* BUGS : still seems to not refresh correctly
23 sometimes, especially when 2 instances of the
24 dialog are loaded at the same time */
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "commdlg.h"
33 #include "dlgs.h"
34 #include "wine/debug.h"
35 #include "cderr.h"
36 #include "cdlg.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
40 static INT_PTR CALLBACK ColorDlgProc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam );
42 #define CONV_LPARAMTOPOINT(lp,p) do { (p)->x = (short)LOWORD(lp); (p)->y = (short)HIWORD(lp); } while(0)
44 static const COLORREF predefcolors[6][8]=
46 { 0x008080FFL, 0x0080FFFFL, 0x0080FF80L, 0x0080FF00L,
47 0x00FFFF80L, 0x00FF8000L, 0x00C080FFL, 0x00FF80FFL },
48 { 0x000000FFL, 0x0000FFFFL, 0x0000FF80L, 0x0040FF00L,
49 0x00FFFF00L, 0x00C08000L, 0x00C08080L, 0x00FF00FFL },
51 { 0x00404080L, 0x004080FFL, 0x0000FF00L, 0x00808000L,
52 0x00804000L, 0x00FF8080L, 0x00400080L, 0x008000FFL },
53 { 0x00000080L, 0x000080FFL, 0x00008000L, 0x00408000L,
54 0x00FF0000L, 0x00A00000L, 0x00800080L, 0x00FF0080L },
56 { 0x00000040L, 0x00004080L, 0x00004000L, 0x00404000L,
57 0x00800000L, 0x00400000L, 0x00400040L, 0x00800040L },
58 { 0x00000000L, 0x00008080L, 0x00408080L, 0x00808080L,
59 0x00808040L, 0x00C0C0C0L, 0x00400040L, 0x00FFFFFFL },
62 static const WCHAR szColourDialogProp[] = {
63 'c','o','l','o','u','r','d','i','a','l','o','g','p','r','o','p',0 };
65 /* Chose Color PRIVATE Structure:
67 * This structure is duplicated in the 16 bit code with
68 * an extra member
71 typedef struct CCPRIVATE
73 LPCHOOSECOLORW lpcc; /* points to public known data structure */
74 HWND hwndSelf; /* dialog window */
75 int nextuserdef; /* next free place in user defined color array */
76 HDC hdcMem; /* color graph used for BitBlt() */
77 HBITMAP hbmMem; /* color graph bitmap */
78 RECT fullsize; /* original dialog window size */
79 UINT msetrgb; /* # of SETRGBSTRING message (today not used) */
80 RECT old3angle; /* last position of l-marker */
81 RECT oldcross; /* last position of color/saturation marker */
82 BOOL updating; /* to prevent recursive WM_COMMAND/EN_UPDATE processing */
83 int h;
84 int s;
85 int l; /* for temporary storing of hue,sat,lum */
86 int capturedGraph; /* control mouse captured */
87 RECT focusRect; /* rectangle last focused item */
88 HWND hwndFocus; /* handle last focused item */
89 } CCPRIV;
91 static int hsl_to_x(int hue, int sat, int lum)
93 int res = 0, maxrgb;
95 /* l below 120 */
96 maxrgb = (256 * min(120,lum)) / 120; /* 0 .. 256 */
97 if (hue < 80)
98 res = 0;
99 else
100 if (hue < 120)
102 res = (hue - 80) * maxrgb; /* 0...10240 */
103 res /= 40; /* 0...256 */
105 else
106 if (hue < 200)
107 res = maxrgb;
108 else
110 res= (240 - hue) * maxrgb;
111 res /= 40;
113 res = res - maxrgb / 2; /* -128...128 */
115 /* saturation */
116 res = maxrgb / 2 + (sat * res) / 240; /* 0..256 */
118 /* lum above 120 */
119 if (lum > 120 && res < 256)
120 res += ((lum - 120) * (256 - res)) / 120;
122 return min(res, 255);
125 /***********************************************************************
126 * CC_HSLtoRGB [internal]
128 static COLORREF CC_HSLtoRGB(int hue, int sat, int lum)
130 int h, r, g, b;
132 /* r */
133 h = hue > 80 ? hue-80 : hue+160;
134 r = hsl_to_x(h, sat, lum);
135 /* g */
136 h = hue > 160 ? hue-160 : hue+80;
137 g = hsl_to_x(h, sat, lum);
138 /* b */
139 b = hsl_to_x(hue, sat, lum);
141 return RGB(r, g, b);
144 /***********************************************************************
145 * CC_RGBtoHSL [internal]
147 static int CC_RGBtoHSL(char c, COLORREF rgb)
149 WORD maxi, mini, mmsum, mmdif, result = 0;
150 int iresult = 0, r, g, b;
152 r = GetRValue(rgb);
153 g = GetGValue(rgb);
154 b = GetBValue(rgb);
156 maxi = max(r, b);
157 maxi = max(maxi, g);
158 mini = min(r, b);
159 mini = min(mini, g);
161 mmsum = maxi + mini;
162 mmdif = maxi - mini;
164 switch(c)
166 /* lum */
167 case 'L': mmsum *= 120; /* 0...61200=(255+255)*120 */
168 result = mmsum / 255; /* 0...240 */
169 break;
170 /* saturation */
171 case 'S': if (!mmsum)
172 result = 0;
173 else
174 if (!mini || maxi == 255)
175 result = 240;
176 else
178 result = mmdif * 240; /* 0...61200=255*240 */
179 result /= (mmsum > 255 ? 510 - mmsum : mmsum); /* 0..255 */
181 break;
182 /* hue */
183 case 'H': if (!mmdif)
184 result = 160;
185 else
187 if (maxi == r)
189 iresult = 40 * (g - b); /* -10200 ... 10200 */
190 iresult /= (int) mmdif; /* -40 .. 40 */
191 if (iresult < 0)
192 iresult += 240; /* 0..40 and 200..240 */
194 else
195 if (maxi == g)
197 iresult = 40 * (b - r);
198 iresult /= (int) mmdif;
199 iresult += 80; /* 40 .. 120 */
201 else
202 if (maxi == b)
204 iresult = 40 * (r - g);
205 iresult /= (int) mmdif;
206 iresult += 160; /* 120 .. 200 */
208 result = iresult;
210 break;
212 return result; /* is this integer arithmetic precise enough ? */
216 /***********************************************************************
217 * CC_DrawCurrentFocusRect [internal]
219 static void CC_DrawCurrentFocusRect( const CCPRIV *lpp )
221 if (lpp->hwndFocus)
223 HDC hdc = GetDC(lpp->hwndFocus);
224 DrawFocusRect(hdc, &lpp->focusRect);
225 ReleaseDC(lpp->hwndFocus, hdc);
229 /***********************************************************************
230 * CC_DrawFocusRect [internal]
232 static void CC_DrawFocusRect(CCPRIV *lpp, HWND hwnd, int x, int y, int rows, int cols)
234 RECT rect;
235 int dx, dy;
236 HDC hdc;
238 CC_DrawCurrentFocusRect(lpp); /* remove current focus rect */
239 /* calculate new rect */
240 GetClientRect(hwnd, &rect);
241 dx = (rect.right - rect.left) / cols;
242 dy = (rect.bottom - rect.top) / rows;
243 rect.left += (x * dx) - 2;
244 rect.top += (y * dy) - 2;
245 rect.right = rect.left + dx;
246 rect.bottom = rect.top + dy;
247 /* draw it */
248 hdc = GetDC(hwnd);
249 DrawFocusRect(hdc, &rect);
250 CopyRect(&lpp->focusRect, &rect);
251 lpp->hwndFocus = hwnd;
252 ReleaseDC(hwnd, hdc);
255 #define DISTANCE 4
257 /***********************************************************************
258 * CC_MouseCheckPredefColorArray [internal]
259 * returns TRUE if one of the predefined colors is clicked
261 static BOOL CC_MouseCheckPredefColorArray(CCPRIV *lpp, int rows, int cols, LPARAM lParam)
263 HWND hwnd;
264 POINT point;
265 RECT rect;
266 int dx, dy, x, y;
268 CONV_LPARAMTOPOINT(lParam, &point);
269 ClientToScreen(lpp->hwndSelf, &point);
270 hwnd = GetDlgItem(lpp->hwndSelf, IDC_COLOR_PREDEF);
271 GetWindowRect(hwnd, &rect);
272 if (PtInRect(&rect, point))
274 dx = (rect.right - rect.left) / cols;
275 dy = (rect.bottom - rect.top) / rows;
276 ScreenToClient(hwnd, &point);
278 if (point.x % dx < ( dx - DISTANCE) && point.y % dy < ( dy - DISTANCE))
280 x = point.x / dx;
281 y = point.y / dy;
282 lpp->lpcc->rgbResult = predefcolors[y][x];
283 CC_DrawFocusRect(lpp, hwnd, x, y, rows, cols);
284 return TRUE;
287 return FALSE;
290 /***********************************************************************
291 * CC_MouseCheckUserColorArray [internal]
292 * return TRUE if the user clicked a color
294 static BOOL CC_MouseCheckUserColorArray(CCPRIV *lpp, int rows, int cols, LPARAM lParam)
296 HWND hwnd;
297 POINT point;
298 RECT rect;
299 int dx, dy, x, y;
300 COLORREF *crarr = lpp->lpcc->lpCustColors;
302 CONV_LPARAMTOPOINT(lParam, &point);
303 ClientToScreen(lpp->hwndSelf, &point);
304 hwnd = GetDlgItem(lpp->hwndSelf, IDC_COLOR_USRDEF);
305 GetWindowRect(hwnd, &rect);
306 if (PtInRect(&rect, point))
308 dx = (rect.right - rect.left) / cols;
309 dy = (rect.bottom - rect.top) / rows;
310 ScreenToClient(hwnd, &point);
312 if (point.x % dx < (dx - DISTANCE) && point.y % dy < (dy - DISTANCE))
314 x = point.x / dx;
315 y = point.y / dy;
316 lpp->lpcc->rgbResult = crarr[x + (cols * y) ];
317 CC_DrawFocusRect(lpp, hwnd, x, y, rows, cols);
318 return TRUE;
321 return FALSE;
324 #define MAXVERT 240
325 #define MAXHORI 239
327 /* 240 ^...... ^^ 240
328 | . ||
329 SAT | . || LUM
330 | . ||
331 +-----> 239 ----
334 /***********************************************************************
335 * CC_MouseCheckColorGraph [internal]
337 static BOOL CC_MouseCheckColorGraph( HWND hDlg, int dlgitem, int *hori, int *vert, LPARAM lParam )
339 HWND hwnd;
340 POINT point;
341 RECT rect;
342 long x,y;
344 CONV_LPARAMTOPOINT(lParam, &point);
345 ClientToScreen(hDlg, &point);
346 hwnd = GetDlgItem( hDlg, dlgitem );
347 GetWindowRect(hwnd, &rect);
349 if (!PtInRect(&rect, point))
350 return FALSE;
352 GetClientRect(hwnd, &rect);
353 ScreenToClient(hwnd, &point);
355 x = (long) point.x * MAXHORI;
356 x /= rect.right;
357 y = (long) (rect.bottom - point.y) * MAXVERT;
358 y /= rect.bottom;
360 if (x < 0) x = 0;
361 if (y < 0) y = 0;
362 if (x > MAXHORI) x = MAXHORI;
363 if (y > MAXVERT) y = MAXVERT;
365 if (hori)
366 *hori = x;
367 if (vert)
368 *vert = y;
370 return TRUE;
372 /***********************************************************************
373 * CC_MouseCheckResultWindow [internal]
374 * test if double click one of the result colors
376 static BOOL CC_MouseCheckResultWindow( HWND hDlg, LPARAM lParam )
378 HWND hwnd;
379 POINT point;
380 RECT rect;
382 CONV_LPARAMTOPOINT(lParam, &point);
383 ClientToScreen(hDlg, &point);
384 hwnd = GetDlgItem(hDlg, IDC_COLOR_RESULT);
385 GetWindowRect(hwnd, &rect);
386 if (PtInRect(&rect, point))
388 PostMessageA(hDlg, WM_COMMAND, IDC_COLOR_RES, 0);
389 return TRUE;
391 return FALSE;
394 /***********************************************************************
395 * CC_CheckDigitsInEdit [internal]
397 static int CC_CheckDigitsInEdit( HWND hwnd, int maxval )
399 int i, k, m, result, value;
400 long editpos;
401 char buffer[30];
403 GetWindowTextA(hwnd, buffer, sizeof(buffer));
404 m = strlen(buffer);
405 result = 0;
407 for (i = 0 ; i < m ; i++)
408 if (buffer[i] < '0' || buffer[i] > '9')
410 for (k = i + 1; k <= m; k++) /* delete bad character */
412 buffer[i] = buffer[k];
413 m--;
415 buffer[m] = 0;
416 result = 1;
419 value = atoi(buffer);
420 if (value > maxval) /* build a new string */
422 sprintf(buffer, "%d", maxval);
423 result = 2;
425 if (result)
427 editpos = SendMessageA(hwnd, EM_GETSEL, 0, 0);
428 SetWindowTextA(hwnd, buffer );
429 SendMessageA(hwnd, EM_SETSEL, 0, editpos);
431 return value;
436 /***********************************************************************
437 * CC_PaintSelectedColor [internal]
439 static void CC_PaintSelectedColor(const CCPRIV *infoPtr)
441 if (IsWindowVisible( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_GRAPH) )) /* if full size */
443 RECT rect;
444 HDC hdc;
445 HBRUSH hBrush;
446 HWND hwnd = GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_RESULT);
448 hdc = GetDC(hwnd);
449 GetClientRect(hwnd, &rect) ;
450 hBrush = CreateSolidBrush(infoPtr->lpcc->rgbResult);
451 if (hBrush)
453 FillRect(hdc, &rect, hBrush);
454 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
455 DeleteObject(hBrush);
457 ReleaseDC(hwnd, hdc);
461 /***********************************************************************
462 * CC_PaintTriangle [internal]
464 static void CC_PaintTriangle(CCPRIV *infoPtr)
466 HDC hDC;
467 long temp;
468 int w = LOWORD(GetDialogBaseUnits()) / 2;
469 POINT points[3];
470 int height;
471 int oben;
472 RECT rect;
473 HBRUSH hbr;
474 HWND hwnd = GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_LUMBAR);
476 if (IsWindowVisible( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_GRAPH))) /* if full size */
478 GetClientRect(hwnd, &rect);
479 height = rect.bottom;
480 hDC = GetDC(infoPtr->hwndSelf);
481 points[0].y = rect.top;
482 points[0].x = rect.right; /* | /| */
483 ClientToScreen(hwnd, points); /* | / | */
484 ScreenToClient(infoPtr->hwndSelf, points); /* |< | */
485 oben = points[0].y; /* | \ | */
486 /* | \| */
487 temp = (long)height * (long)infoPtr->l;
488 points[0].x += 1;
489 points[0].y = oben + height - temp / (long)MAXVERT;
490 points[1].y = points[0].y + w;
491 points[2].y = points[0].y - w;
492 points[2].x = points[1].x = points[0].x + w;
494 hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
495 if (!hbr) hbr = GetSysColorBrush(COLOR_BTNFACE);
496 FillRect(hDC, &infoPtr->old3angle, hbr);
497 infoPtr->old3angle.left = points[0].x;
498 infoPtr->old3angle.right = points[1].x + 1;
499 infoPtr->old3angle.top = points[2].y - 1;
500 infoPtr->old3angle.bottom= points[1].y + 1;
502 hbr = SelectObject(hDC, GetStockObject(BLACK_BRUSH));
503 Polygon(hDC, points, 3);
504 SelectObject(hDC, hbr);
506 ReleaseDC(infoPtr->hwndSelf, hDC);
511 /***********************************************************************
512 * CC_PaintCross [internal]
514 static void CC_PaintCross(CCPRIV *infoPtr)
516 HWND hwnd = GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_GRAPH);
518 if (IsWindowVisible(hwnd)) /* if full size */
520 HDC hDC;
521 int w = GetDialogBaseUnits() - 1;
522 int wc = GetDialogBaseUnits() * 3 / 4;
523 RECT rect;
524 POINT point, p;
525 HPEN hPen;
526 int x, y;
528 x = infoPtr->h;
529 y = infoPtr->s;
531 GetClientRect(hwnd, &rect);
532 hDC = GetDC(hwnd);
533 SelectClipRgn( hDC, CreateRectRgnIndirect(&rect));
535 point.x = ((long)rect.right * (long)x) / (long)MAXHORI;
536 point.y = rect.bottom - ((long)rect.bottom * (long)y) / (long)MAXVERT;
537 if ( infoPtr->oldcross.left != infoPtr->oldcross.right )
538 BitBlt(hDC, infoPtr->oldcross.left, infoPtr->oldcross.top,
539 infoPtr->oldcross.right - infoPtr->oldcross.left,
540 infoPtr->oldcross.bottom - infoPtr->oldcross.top,
541 infoPtr->hdcMem, infoPtr->oldcross.left, infoPtr->oldcross.top, SRCCOPY);
542 infoPtr->oldcross.left = point.x - w - 1;
543 infoPtr->oldcross.right = point.x + w + 1;
544 infoPtr->oldcross.top = point.y - w - 1;
545 infoPtr->oldcross.bottom = point.y + w + 1;
547 hPen = CreatePen(PS_SOLID, 3, RGB(0, 0, 0)); /* -black- color */
548 hPen = SelectObject(hDC, hPen);
549 MoveToEx(hDC, point.x - w, point.y, &p);
550 LineTo(hDC, point.x - wc, point.y);
551 MoveToEx(hDC, point.x + wc, point.y, &p);
552 LineTo(hDC, point.x + w, point.y);
553 MoveToEx(hDC, point.x, point.y - w, &p);
554 LineTo(hDC, point.x, point.y - wc);
555 MoveToEx(hDC, point.x, point.y + wc, &p);
556 LineTo(hDC, point.x, point.y + w);
557 DeleteObject( SelectObject(hDC, hPen));
559 ReleaseDC(hwnd, hDC);
564 #define XSTEPS 48
565 #define YSTEPS 24
568 /***********************************************************************
569 * CC_PrepareColorGraph [internal]
571 static void CC_PrepareColorGraph(CCPRIV *infoPtr)
573 int sdif, hdif, xdif, ydif, hue, sat;
574 HWND hwnd = GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_GRAPH);
575 HBRUSH hbrush;
576 HDC hdc ;
577 RECT rect, client;
578 HCURSOR hcursor = SetCursor( LoadCursorW(0, (LPCWSTR)IDC_WAIT) );
580 GetClientRect(hwnd, &client);
581 hdc = GetDC(hwnd);
582 infoPtr->hdcMem = CreateCompatibleDC(hdc);
583 infoPtr->hbmMem = CreateCompatibleBitmap(hdc, client.right, client.bottom);
584 SelectObject(infoPtr->hdcMem, infoPtr->hbmMem);
586 xdif = client.right / XSTEPS;
587 ydif = client.bottom / YSTEPS+1;
588 hdif = 239 / XSTEPS;
589 sdif = 240 / YSTEPS;
590 for (rect.left = hue = 0; hue < 239 + hdif; hue += hdif)
592 rect.right = rect.left + xdif;
593 rect.bottom = client.bottom;
594 for(sat = 0; sat < 240 + sdif; sat += sdif)
596 rect.top = rect.bottom - ydif;
597 hbrush = CreateSolidBrush(CC_HSLtoRGB(hue, sat, 120));
598 FillRect(infoPtr->hdcMem, &rect, hbrush);
599 DeleteObject(hbrush);
600 rect.bottom = rect.top;
602 rect.left = rect.right;
604 ReleaseDC(hwnd, hdc);
605 SetCursor(hcursor);
608 /***********************************************************************
609 * CC_PaintColorGraph [internal]
611 static void CC_PaintColorGraph(CCPRIV *infoPtr)
613 HWND hwnd = GetDlgItem( infoPtr->hwndSelf, IDC_COLOR_GRAPH );
614 HDC hDC;
615 RECT rect;
617 if (IsWindowVisible(hwnd)) /* if full size */
619 if (!infoPtr->hdcMem)
620 CC_PrepareColorGraph(infoPtr); /* should not be necessary */
622 hDC = GetDC(hwnd);
623 GetClientRect(hwnd, &rect);
624 if (infoPtr->hdcMem)
625 BitBlt(hDC, 0, 0, rect.right, rect.bottom, infoPtr->hdcMem, 0, 0, SRCCOPY);
626 else
627 WARN("choose color: hdcMem is not defined\n");
628 ReleaseDC(hwnd, hDC);
632 /***********************************************************************
633 * CC_PaintLumBar [internal]
635 static void CC_PaintLumBar(const CCPRIV *infoPtr)
637 HWND hwnd = GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_LUMBAR);
638 RECT rect, client;
639 int lum, ldif, ydif;
640 HBRUSH hbrush;
641 HDC hDC;
643 if (IsWindowVisible(hwnd))
645 hDC = GetDC(hwnd);
646 GetClientRect(hwnd, &client);
647 rect = client;
649 ldif = 240 / YSTEPS;
650 ydif = client.bottom / YSTEPS+1;
651 for (lum = 0; lum < 240 + ldif; lum += ldif)
653 rect.top = max(0, rect.bottom - ydif);
654 hbrush = CreateSolidBrush(CC_HSLtoRGB(infoPtr->h, infoPtr->s, lum));
655 FillRect(hDC, &rect, hbrush);
656 DeleteObject(hbrush);
657 rect.bottom = rect.top;
659 GetClientRect(hwnd, &rect);
660 DrawEdge(hDC, &rect, BDR_SUNKENOUTER, BF_RECT);
661 ReleaseDC(hwnd, hDC);
665 /***********************************************************************
666 * CC_EditSetRGB [internal]
668 static void CC_EditSetRGB( CCPRIV *infoPtr )
670 if (IsWindowVisible( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_GRAPH) )) /* if full size */
672 COLORREF cr = infoPtr->lpcc->rgbResult;
673 int r = GetRValue(cr);
674 int g = GetGValue(cr);
675 int b = GetBValue(cr);
676 char buffer[10];
678 infoPtr->updating = TRUE;
679 sprintf(buffer, "%d", r);
680 SetWindowTextA( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_EDIT_R), buffer);
681 sprintf(buffer, "%d", g);
682 SetWindowTextA( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_EDIT_G), buffer);
683 sprintf( buffer, "%d", b );
684 SetWindowTextA( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_EDIT_B), buffer);
685 infoPtr->updating = FALSE;
689 /***********************************************************************
690 * CC_EditSetHSL [internal]
692 static void CC_EditSetHSL( CCPRIV *infoPtr )
694 if (IsWindowVisible( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_GRAPH) )) /* if full size */
696 char buffer[10];
698 infoPtr->updating = TRUE;
699 sprintf(buffer, "%d", infoPtr->h);
700 SetWindowTextA( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_EDIT_H), buffer);
701 sprintf(buffer, "%d", infoPtr->s);
702 SetWindowTextA( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_EDIT_S), buffer);
703 sprintf(buffer, "%d", infoPtr->l);
704 SetWindowTextA( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_EDIT_L), buffer);
705 infoPtr->updating = FALSE;
707 CC_PaintLumBar(infoPtr);
710 /***********************************************************************
711 * CC_SwitchToFullSize [internal]
713 static void CC_SwitchToFullSize( CCPRIV *infoPtr, const RECT *lprect )
715 int i;
717 EnableWindow( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_DEFINE), FALSE);
718 CC_PrepareColorGraph(infoPtr);
719 for (i = IDC_COLOR_EDIT_H; i <= IDC_COLOR_EDIT_B; i++)
720 ShowWindow( GetDlgItem(infoPtr->hwndSelf, i), SW_SHOW);
721 for (i = IDC_COLOR_HL; i <= IDC_COLOR_BL; i++)
722 ShowWindow( GetDlgItem(infoPtr->hwndSelf, i), SW_SHOW);
723 ShowWindow( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_RES), SW_SHOW);
724 ShowWindow( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_ADD), SW_SHOW);
725 ShowWindow( GetDlgItem(infoPtr->hwndSelf, 1090), SW_SHOW);
727 if (lprect)
728 SetWindowPos(infoPtr->hwndSelf, 0, 0, 0, lprect->right-lprect->left,
729 lprect->bottom-lprect->top, SWP_NOMOVE|SWP_NOZORDER);
731 ShowWindow( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_LUMBAR), SW_SHOW);
732 ShowWindow( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_RESULT), SW_SHOW);
734 CC_EditSetRGB(infoPtr);
735 CC_EditSetHSL(infoPtr);
736 ShowWindow( GetDlgItem( infoPtr->hwndSelf, IDC_COLOR_GRAPH), SW_SHOW);
737 UpdateWindow( GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_GRAPH) );
740 /***********************************************************************
741 * CC_PaintPredefColorArray [internal]
742 * Paints the default standard 48 colors
744 static void CC_PaintPredefColorArray(const CCPRIV *infoPtr, int rows, int cols)
746 HWND hwnd = GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_PREDEF);
747 RECT rect, blockrect;
748 HDC hdc;
749 HBRUSH hBrush;
750 int dx, dy, i, j, k;
752 GetClientRect(hwnd, &rect);
753 dx = rect.right / cols;
754 dy = rect.bottom / rows;
755 k = rect.left;
757 hdc = GetDC(hwnd);
758 GetClientRect(hwnd, &rect);
759 hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
760 if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
761 FillRect(hdc, &rect, hBrush);
762 for ( j = 0; j < rows; j++ )
764 for ( i = 0; i < cols; i++ )
766 hBrush = CreateSolidBrush(predefcolors[j][i]);
767 if (hBrush)
769 blockrect.left = rect.left;
770 blockrect.top = rect.top;
771 blockrect.right = rect.left + dx - DISTANCE;
772 blockrect.bottom = rect.top + dy - DISTANCE;
773 FillRect(hdc, &blockrect, hBrush);
774 DrawEdge(hdc, &blockrect, BDR_SUNKEN, BF_RECT);
775 DeleteObject(hBrush);
777 rect.left += dx;
779 rect.top += dy;
780 rect.left = k;
782 ReleaseDC(hwnd, hdc);
783 if (infoPtr->hwndFocus == hwnd)
784 CC_DrawCurrentFocusRect(infoPtr);
786 /***********************************************************************
787 * CC_PaintUserColorArray [internal]
788 * Paint the 16 user-selected colors
790 static void CC_PaintUserColorArray(const CCPRIV *infoPtr, int rows, int cols)
792 HWND hwnd = GetDlgItem(infoPtr->hwndSelf, IDC_COLOR_USRDEF);
793 RECT rect, blockrect;
794 HDC hdc;
795 HBRUSH hBrush;
796 int dx, dy, i, j, k;
798 GetClientRect(hwnd, &rect);
800 dx = rect.right / cols;
801 dy = rect.bottom / rows;
802 k = rect.left;
804 hdc = GetDC(hwnd);
805 if (hdc)
807 hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
808 if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
809 FillRect( hdc, &rect, hBrush );
810 for (j = 0; j < rows; j++)
812 for (i = 0; i < cols; i++)
814 hBrush = CreateSolidBrush(infoPtr->lpcc->lpCustColors[i+j*cols]);
815 if (hBrush)
817 blockrect.left = rect.left;
818 blockrect.top = rect.top;
819 blockrect.right = rect.left + dx - DISTANCE;
820 blockrect.bottom = rect.top + dy - DISTANCE;
821 FillRect(hdc, &blockrect, hBrush);
822 DrawEdge(hdc, &blockrect, BDR_SUNKEN, BF_RECT);
823 DeleteObject(hBrush);
825 rect.left += dx;
827 rect.top += dy;
828 rect.left = k;
830 ReleaseDC(hwnd, hdc);
832 if (infoPtr->hwndFocus == hwnd)
833 CC_DrawCurrentFocusRect(infoPtr);
837 /***********************************************************************
838 * CC_HookCallChk [internal]
840 static BOOL CC_HookCallChk( const CHOOSECOLORW *lpcc )
842 if (lpcc)
843 if(lpcc->Flags & CC_ENABLEHOOK)
844 if (lpcc->lpfnHook)
845 return TRUE;
846 return FALSE;
849 /***********************************************************************
850 * CC_WMInitDialog [internal]
852 static LRESULT CC_WMInitDialog( HWND hDlg, WPARAM wParam, LPARAM lParam )
854 CHOOSECOLORW *cc = (CHOOSECOLORW*)lParam;
855 int i, res;
856 int r, g, b;
857 HWND hwnd;
858 RECT rect;
859 POINT point;
860 CCPRIV *lpp;
862 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam);
864 if (cc->lStructSize != sizeof(CHOOSECOLORW))
866 EndDialog(hDlg, 0);
867 return FALSE;
870 lpp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct CCPRIVATE) );
871 lpp->lpcc = cc;
872 lpp->hwndSelf = hDlg;
874 SetPropW( hDlg, szColourDialogProp, lpp );
876 if (!(lpp->lpcc->Flags & CC_SHOWHELP))
877 ShowWindow( GetDlgItem(hDlg,0x40e), SW_HIDE);
878 lpp->msetrgb = RegisterWindowMessageA(SETRGBSTRINGA);
880 #if 0
881 cpos = MAKELONG(5,7); /* init */
882 if (lpp->lpcc->Flags & CC_RGBINIT)
884 for (i = 0; i < 6; i++)
885 for (j = 0; j < 8; j++)
886 if (predefcolors[i][j] == lpp->lpcc->rgbResult)
888 cpos = MAKELONG(i,j);
889 goto found;
892 found:
893 /* FIXME: Draw_a_focus_rect & set_init_values */
894 #endif
896 GetWindowRect(hDlg, &lpp->fullsize);
897 if (lpp->lpcc->Flags & CC_FULLOPEN || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
899 hwnd = GetDlgItem(hDlg, IDC_COLOR_DEFINE);
900 EnableWindow(hwnd, FALSE);
902 if (!(lpp->lpcc->Flags & CC_FULLOPEN ) || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
904 rect = lpp->fullsize;
905 res = rect.bottom - rect.top;
906 hwnd = GetDlgItem(hDlg, IDC_COLOR_GRAPH); /* cut at left border */
907 point.x = point.y = 0;
908 ClientToScreen(hwnd, &point);
909 ScreenToClient(hDlg,&point);
910 GetClientRect(hDlg, &rect);
911 point.x += GetSystemMetrics(SM_CXDLGFRAME);
912 SetWindowPos(hDlg, 0, 0, 0, point.x, res, SWP_NOMOVE|SWP_NOZORDER);
914 for (i = IDC_COLOR_EDIT_H; i <= IDC_COLOR_EDIT_B; i++)
915 ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
916 for (i = IDC_COLOR_HL; i <= IDC_COLOR_BL; i++)
917 ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
918 ShowWindow( GetDlgItem(hDlg, IDC_COLOR_RES), SW_HIDE);
919 ShowWindow( GetDlgItem(hDlg, IDC_COLOR_ADD), SW_HIDE);
920 ShowWindow( GetDlgItem(hDlg, IDC_COLOR_GRAPH), SW_HIDE);
921 ShowWindow( GetDlgItem(hDlg, IDC_COLOR_RESULT), SW_HIDE);
922 ShowWindow( GetDlgItem(hDlg, 1090 ), SW_HIDE);
924 else
925 CC_SwitchToFullSize(lpp, NULL);
926 res = TRUE;
927 for (i = IDC_COLOR_EDIT_H; i <= IDC_COLOR_EDIT_B; i++)
928 SendMessageA( GetDlgItem(hDlg, i), EM_LIMITTEXT, 3, 0); /* max 3 digits: xyz */
929 if (CC_HookCallChk(lpp->lpcc))
931 res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, WM_INITDIALOG, wParam, lParam);
934 /* Set the initial values of the color chooser dialog */
935 r = GetRValue(lpp->lpcc->rgbResult);
936 g = GetGValue(lpp->lpcc->rgbResult);
937 b = GetBValue(lpp->lpcc->rgbResult);
939 CC_PaintSelectedColor(lpp);
940 lpp->h = CC_RGBtoHSL('H', lpp->lpcc->rgbResult);
941 lpp->s = CC_RGBtoHSL('S', lpp->lpcc->rgbResult);
942 lpp->l = CC_RGBtoHSL('L', lpp->lpcc->rgbResult);
944 /* Doing it the long way because CC_EditSetRGB/HSL doesn't seem to work */
945 SetDlgItemInt(hDlg, IDC_COLOR_EDIT_H, lpp->h, TRUE);
946 SetDlgItemInt(hDlg, IDC_COLOR_EDIT_S, lpp->s, TRUE);
947 SetDlgItemInt(hDlg, IDC_COLOR_EDIT_L, lpp->l, TRUE);
948 SetDlgItemInt(hDlg, IDC_COLOR_EDIT_R, r, TRUE);
949 SetDlgItemInt(hDlg, IDC_COLOR_EDIT_G, g, TRUE);
950 SetDlgItemInt(hDlg, IDC_COLOR_EDIT_B, b, TRUE);
952 CC_PaintCross(lpp);
953 CC_PaintTriangle(lpp);
955 return res;
959 /***********************************************************************
960 * CC_WMCommand [internal]
962 static LRESULT CC_WMCommand(CCPRIV *lpp, WPARAM wParam, LPARAM lParam, WORD notifyCode, HWND hwndCtl)
964 int r, g, b, i, xx;
965 UINT cokmsg;
966 HDC hdc;
967 COLORREF *cr;
969 TRACE("CC_WMCommand wParam=%lx lParam=%lx\n", wParam, lParam);
970 switch (LOWORD(wParam))
972 case IDC_COLOR_EDIT_R: /* edit notify RGB */
973 case IDC_COLOR_EDIT_G:
974 case IDC_COLOR_EDIT_B:
975 if (notifyCode == EN_UPDATE && !lpp->updating)
977 i = CC_CheckDigitsInEdit(hwndCtl, 255);
978 r = GetRValue(lpp->lpcc->rgbResult);
979 g = GetGValue(lpp->lpcc->rgbResult);
980 b= GetBValue(lpp->lpcc->rgbResult);
981 xx = 0;
982 switch (LOWORD(wParam))
984 case IDC_COLOR_EDIT_R: if ((xx = (i != r))) r = i; break;
985 case IDC_COLOR_EDIT_G: if ((xx = (i != g))) g = i; break;
986 case IDC_COLOR_EDIT_B: if ((xx = (i != b))) b = i; break;
988 if (xx) /* something has changed */
990 lpp->lpcc->rgbResult = RGB(r, g, b);
991 CC_PaintSelectedColor(lpp);
992 lpp->h = CC_RGBtoHSL('H', lpp->lpcc->rgbResult);
993 lpp->s = CC_RGBtoHSL('S', lpp->lpcc->rgbResult);
994 lpp->l = CC_RGBtoHSL('L', lpp->lpcc->rgbResult);
995 CC_EditSetHSL(lpp);
996 CC_PaintCross(lpp);
997 CC_PaintTriangle(lpp);
1000 break;
1002 case IDC_COLOR_EDIT_H: /* edit notify HSL */
1003 case IDC_COLOR_EDIT_S:
1004 case IDC_COLOR_EDIT_L:
1005 if (notifyCode == EN_UPDATE && !lpp->updating)
1007 i = CC_CheckDigitsInEdit(hwndCtl , LOWORD(wParam) == IDC_COLOR_EDIT_H ? 239 : 240);
1008 xx = 0;
1009 switch (LOWORD(wParam))
1011 case IDC_COLOR_EDIT_H: if ((xx = ( i != lpp->h))) lpp->h = i; break;
1012 case IDC_COLOR_EDIT_S: if ((xx = ( i != lpp->s))) lpp->s = i; break;
1013 case IDC_COLOR_EDIT_L: if ((xx = ( i != lpp->l))) lpp->l = i; break;
1015 if (xx) /* something has changed */
1017 lpp->lpcc->rgbResult = CC_HSLtoRGB(lpp->h, lpp->s, lpp->l);
1018 CC_PaintSelectedColor(lpp);
1019 CC_EditSetRGB(lpp);
1020 CC_PaintCross(lpp);
1021 CC_PaintTriangle(lpp);
1024 break;
1026 case IDC_COLOR_DEFINE:
1027 CC_SwitchToFullSize(lpp, &lpp->fullsize);
1028 SetFocus( GetDlgItem(lpp->hwndSelf, IDC_COLOR_EDIT_H));
1029 break;
1031 case IDC_COLOR_ADD: /* 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(lpp, 2, 8);
1037 break;
1039 case IDC_COLOR_RES: /* resulting color */
1040 hdc = GetDC(lpp->hwndSelf);
1041 lpp->lpcc->rgbResult = GetNearestColor(hdc, lpp->lpcc->rgbResult);
1042 ReleaseDC(lpp->hwndSelf, hdc);
1043 CC_EditSetRGB(lpp);
1044 CC_PaintSelectedColor(lpp);
1045 lpp->h = CC_RGBtoHSL('H', lpp->lpcc->rgbResult);
1046 lpp->s = CC_RGBtoHSL('S', lpp->lpcc->rgbResult);
1047 lpp->l = CC_RGBtoHSL('L', lpp->lpcc->rgbResult);
1048 CC_EditSetHSL(lpp);
1049 CC_PaintCross(lpp);
1050 CC_PaintTriangle(lpp);
1051 break;
1053 case 0x40e: /* Help! */ /* The Beatles, 1965 ;-) */
1054 i = RegisterWindowMessageA(HELPMSGSTRINGA);
1055 if (lpp->lpcc->hwndOwner)
1056 SendMessageA(lpp->lpcc->hwndOwner, i, 0, (LPARAM)lpp->lpcc);
1057 if ( CC_HookCallChk(lpp->lpcc))
1058 CallWindowProcA( (WNDPROC) lpp->lpcc->lpfnHook, lpp->hwndSelf,
1059 WM_COMMAND, psh15, (LPARAM)lpp->lpcc);
1060 break;
1062 case IDOK :
1063 cokmsg = RegisterWindowMessageA(COLOROKSTRINGA);
1064 if (lpp->lpcc->hwndOwner)
1065 if (SendMessageA(lpp->lpcc->hwndOwner, cokmsg, 0, (LPARAM)lpp->lpcc))
1066 break; /* do NOT close */
1067 EndDialog(lpp->hwndSelf, 1) ;
1068 return TRUE ;
1070 case IDCANCEL :
1071 EndDialog(lpp->hwndSelf, 0) ;
1072 return TRUE ;
1075 return FALSE;
1078 /***********************************************************************
1079 * CC_WMPaint [internal]
1081 static LRESULT CC_WMPaint( CCPRIV *lpp )
1083 PAINTSTRUCT ps;
1085 BeginPaint(lpp->hwndSelf, &ps);
1086 /* we have to paint dialog children except text and buttons */
1087 CC_PaintPredefColorArray(lpp, 6, 8);
1088 CC_PaintUserColorArray(lpp, 2, 8);
1089 CC_PaintLumBar(lpp);
1090 CC_PaintTriangle(lpp);
1091 CC_PaintSelectedColor(lpp);
1092 CC_PaintColorGraph(lpp);
1093 CC_PaintCross(lpp);
1094 EndPaint(lpp->hwndSelf, &ps);
1096 return TRUE;
1099 /***********************************************************************
1100 * CC_WMLButtonUp [internal]
1102 static LRESULT CC_WMLButtonUp( CCPRIV *infoPtr )
1104 if (infoPtr->capturedGraph)
1106 infoPtr->capturedGraph = 0;
1107 ReleaseCapture();
1108 CC_PaintCross(infoPtr);
1109 return 1;
1111 return 0;
1114 /***********************************************************************
1115 * CC_WMMouseMove [internal]
1117 static LRESULT CC_WMMouseMove( CCPRIV *infoPtr, LPARAM lParam )
1119 if (infoPtr->capturedGraph)
1121 int *ptrh = NULL, *ptrs = &infoPtr->l;
1122 if (infoPtr->capturedGraph == IDC_COLOR_GRAPH)
1124 ptrh = &infoPtr->h;
1125 ptrs = &infoPtr->s;
1127 if (CC_MouseCheckColorGraph( infoPtr->hwndSelf, infoPtr->capturedGraph, ptrh, ptrs, lParam))
1129 infoPtr->lpcc->rgbResult = CC_HSLtoRGB(infoPtr->h, infoPtr->s, infoPtr->l);
1130 CC_EditSetRGB(infoPtr);
1131 CC_EditSetHSL(infoPtr);
1132 CC_PaintCross(infoPtr);
1133 CC_PaintTriangle(infoPtr);
1134 CC_PaintSelectedColor(infoPtr);
1136 else
1138 ReleaseCapture();
1139 infoPtr->capturedGraph = 0;
1141 return 1;
1143 return 0;
1146 /***********************************************************************
1147 * CC_WMLButtonDown [internal]
1149 static LRESULT CC_WMLButtonDown( CCPRIV *infoPtr, LPARAM lParam )
1151 int i = 0;
1153 if (CC_MouseCheckPredefColorArray(infoPtr, 6, 8, lParam))
1154 i = 1;
1155 else
1156 if (CC_MouseCheckUserColorArray(infoPtr, 2, 8, lParam))
1157 i = 1;
1158 else
1159 if (CC_MouseCheckColorGraph(infoPtr->hwndSelf, IDC_COLOR_GRAPH, &infoPtr->h, &infoPtr->s, lParam))
1161 i = 2;
1162 infoPtr->capturedGraph = IDC_COLOR_GRAPH;
1164 else
1165 if (CC_MouseCheckColorGraph(infoPtr->hwndSelf, IDC_COLOR_LUMBAR, NULL, &infoPtr->l, lParam))
1167 i = 2;
1168 infoPtr->capturedGraph = IDC_COLOR_LUMBAR;
1170 if ( i == 2 )
1172 SetCapture(infoPtr->hwndSelf);
1173 infoPtr->lpcc->rgbResult = CC_HSLtoRGB(infoPtr->h, infoPtr->s, infoPtr->l);
1175 if ( i == 1 )
1177 infoPtr->h = CC_RGBtoHSL('H', infoPtr->lpcc->rgbResult);
1178 infoPtr->s = CC_RGBtoHSL('S', infoPtr->lpcc->rgbResult);
1179 infoPtr->l = CC_RGBtoHSL('L', infoPtr->lpcc->rgbResult);
1181 if (i)
1183 CC_EditSetRGB(infoPtr);
1184 CC_EditSetHSL(infoPtr);
1185 CC_PaintCross(infoPtr);
1186 CC_PaintTriangle(infoPtr);
1187 CC_PaintSelectedColor(infoPtr);
1188 return TRUE;
1190 return FALSE;
1193 /***********************************************************************
1194 * ColorDlgProc32 [internal]
1197 static INT_PTR CALLBACK ColorDlgProc( HWND hDlg, UINT message,
1198 WPARAM wParam, LPARAM lParam )
1201 int res;
1202 CCPRIV *lpp = GetPropW( hDlg, szColourDialogProp );
1204 if (message != WM_INITDIALOG)
1206 if (!lpp)
1207 return FALSE;
1208 res = 0;
1209 if (CC_HookCallChk(lpp->lpcc))
1210 res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, message, wParam, lParam);
1211 if ( res )
1212 return res;
1215 /* FIXME: SetRGB message
1216 if (message && message == msetrgb)
1217 return HandleSetRGB(hDlg, lParam);
1220 switch (message)
1222 case WM_INITDIALOG:
1223 return CC_WMInitDialog(hDlg, wParam, lParam);
1224 case WM_NCDESTROY:
1225 DeleteDC(lpp->hdcMem);
1226 DeleteObject(lpp->hbmMem);
1227 HeapFree(GetProcessHeap(), 0, lpp);
1228 RemovePropW( hDlg, szColourDialogProp );
1229 break;
1230 case WM_COMMAND:
1231 if (CC_WMCommand(lpp, wParam, lParam, HIWORD(wParam), (HWND) lParam))
1232 return TRUE;
1233 break;
1234 case WM_PAINT:
1235 if (CC_WMPaint(lpp))
1236 return TRUE;
1237 break;
1238 case WM_LBUTTONDBLCLK:
1239 if (CC_MouseCheckResultWindow(hDlg, lParam))
1240 return TRUE;
1241 break;
1242 case WM_MOUSEMOVE:
1243 if (CC_WMMouseMove(lpp, lParam))
1244 return TRUE;
1245 break;
1246 case WM_LBUTTONUP: /* FIXME: ClipCursor off (if in color graph)*/
1247 if (CC_WMLButtonUp(lpp))
1248 return TRUE;
1249 break;
1250 case WM_LBUTTONDOWN:/* FIXME: ClipCursor on (if in color graph)*/
1251 if (CC_WMLButtonDown(lpp, lParam))
1252 return TRUE;
1253 break;
1255 return FALSE ;
1258 /***********************************************************************
1259 * ChooseColorW (COMDLG32.@)
1261 * Create a color dialog box.
1263 * PARAMS
1264 * lpChCol [I/O] in: information to initialize the dialog box.
1265 * out: User's color selection
1267 * RETURNS
1268 * TRUE: Ok button clicked.
1269 * FALSE: Cancel button clicked, or error.
1271 BOOL WINAPI ChooseColorW( CHOOSECOLORW *lpChCol )
1273 HANDLE hDlgTmpl = 0;
1274 const void *template;
1276 TRACE("(%p)\n", lpChCol);
1278 if (!lpChCol) return FALSE;
1280 if (lpChCol->Flags & CC_ENABLETEMPLATEHANDLE)
1282 if (!(template = LockResource(lpChCol->hInstance)))
1284 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1285 return FALSE;
1288 else if (lpChCol->Flags & CC_ENABLETEMPLATE)
1290 HRSRC hResInfo;
1291 if (!(hResInfo = FindResourceW((HINSTANCE)lpChCol->hInstance,
1292 lpChCol->lpTemplateName,
1293 (LPWSTR)RT_DIALOG)))
1295 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1296 return FALSE;
1298 if (!(hDlgTmpl = LoadResource((HINSTANCE)lpChCol->hInstance, hResInfo)) ||
1299 !(template = LockResource(hDlgTmpl)))
1301 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1302 return FALSE;
1305 else
1307 HRSRC hResInfo;
1308 HGLOBAL hDlgTmpl;
1309 static const WCHAR wszCHOOSE_COLOR[] = {'C','H','O','O','S','E','_','C','O','L','O','R',0};
1310 if (!(hResInfo = FindResourceW(COMDLG32_hInstance, wszCHOOSE_COLOR, (LPWSTR)RT_DIALOG)))
1312 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1313 return FALSE;
1315 if (!(hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo )) ||
1316 !(template = LockResource(hDlgTmpl)))
1318 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1319 return FALSE;
1323 return DialogBoxIndirectParamW(COMDLG32_hInstance, template, lpChCol->hwndOwner,
1324 ColorDlgProc, (LPARAM)lpChCol);
1327 /***********************************************************************
1328 * ChooseColorA (COMDLG32.@)
1330 * See ChooseColorW.
1332 BOOL WINAPI ChooseColorA( LPCHOOSECOLORA lpChCol )
1335 LPWSTR template_name = NULL;
1336 BOOL ret;
1338 LPCHOOSECOLORW lpcc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHOOSECOLORW));
1339 lpcc->lStructSize = sizeof(*lpcc);
1340 lpcc->hwndOwner = lpChCol->hwndOwner;
1341 lpcc->hInstance = lpChCol->hInstance;
1342 lpcc->rgbResult = lpChCol->rgbResult;
1343 lpcc->lpCustColors = lpChCol->lpCustColors;
1344 lpcc->Flags = lpChCol->Flags;
1345 lpcc->lCustData = lpChCol->lCustData;
1346 lpcc->lpfnHook = lpChCol->lpfnHook;
1347 if ((lpcc->Flags & CC_ENABLETEMPLATE) && (lpChCol->lpTemplateName)) {
1348 if (!IS_INTRESOURCE(lpChCol->lpTemplateName)) {
1349 INT len = MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, NULL, 0);
1350 template_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1351 MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, template_name, len );
1352 lpcc->lpTemplateName = template_name;
1353 } else {
1354 lpcc->lpTemplateName = (LPCWSTR)lpChCol->lpTemplateName;
1358 ret = ChooseColorW(lpcc);
1360 if (ret)
1361 lpChCol->rgbResult = lpcc->rgbResult;
1362 HeapFree(GetProcessHeap(), 0, template_name);
1363 HeapFree(GetProcessHeap(), 0, lpcc);
1364 return ret;