1 /*
2 * COMMDLG - Color Dialog
3 *
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 /* BUGS : still seems to not refresh correctly
23 sometimes, especially when 2 instances of the
24 dialog are loaded at the same time */
25
26 #include <ctype.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "commdlg.h"
36 #include "dlgs.h"
37 #include "wine/debug.h"
38 #include "cderr.h"
39 #include "cdlg.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
42
43 static INT_PTR CALLBACK ColorDlgProc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam );
44
45 #define CONV_LPARAMTOPOINT(lp,p) do { (p)->x = (short)LOWORD(lp); (p)->y = (short)HIWORD(lp); } while(0)
46
47 static const COLORREF predefcolors[6][8]=
48 {
49 { 0x008080FFL, 0x0080FFFFL, 0x0080FF80L, 0x0080FF00L,
50 0x00FFFF80L, 0x00FF8000L, 0x00C080FFL, 0x00FF80FFL },
51 { 0x000000FFL, 0x0000FFFFL, 0x0000FF80L, 0x0040FF00L,
52 0x00FFFF00L, 0x00C08000L, 0x00C08080L, 0x00FF00FFL },
53
54 { 0x00404080L, 0x004080FFL, 0x0000FF00L, 0x00808000L,
55 0x00804000L, 0x00FF8080L, 0x00400080L, 0x008000FFL },
56 { 0x00000080L, 0x000080FFL, 0x00008000L, 0x00408000L,
57 0x00FF0000L, 0x00A00000L, 0x00800080L, 0x00FF0080L },
58
59 { 0x00000040L, 0x00004080L, 0x00004000L, 0x00404000L,
60 0x00800000L, 0x00400000L, 0x00400040L, 0x00800040L },
61 { 0x00000000L, 0x00008080L, 0x00408080L, 0x00808080L,
62 0x00808040L, 0x00C0C0C0L, 0x00400040L, 0x00FFFFFFL },
63 };
64
65 static const WCHAR szColourDialogProp[] = {
66 'c','o','l','o','u','r','d','i','a','l','o','g','p','r','o','p',0 };
67
68 /* Chose Color PRIVATE Structure:
69 *
70 * This structure is duplicated in the 16 bit code with
71 * an extra member
72 */
73
74 typedef struct CCPRIVATE
75 {
76 LPCHOOSECOLORW lpcc; /* points to public known data structure */
77 int nextuserdef; /* next free place in user defined color array */
78 HDC hdcMem; /* color graph used for BitBlt() */
79 HBITMAP hbmMem; /* color graph bitmap */
80 RECT fullsize; /* original dialog window size */
81 UINT msetrgb; /* # of SETRGBSTRING message (today not used) */
82 RECT old3angle; /* last position of l-marker */
83 RECT oldcross; /* last position of color/saturation marker */
84 BOOL updating; /* to prevent recursive WM_COMMAND/EN_UPDATE processing */
85 int h;
86 int s;
87 int l; /* for temporary storing of hue,sat,lum */
88 int capturedGraph; /* control mouse captured */
89 RECT focusRect; /* rectangle last focused item */
90 HWND hwndFocus; /* handle last focused item */
91 } CCPRIV, *LPCCPRIV;
92
93 /***********************************************************************
94 * CC_HSLtoRGB [internal]
95 */
96 int CC_HSLtoRGB(char c, int hue, int sat, int lum)
97 {
98 int res = 0, maxrgb;
99
100 /* hue */
101 switch(c)
102 {
103 case 'R': if (hue > 80) hue -= 80; else hue += 160; break;
104 case 'G': if (hue > 160) hue -= 160; else hue += 80; break;
105 case 'B': break;
106 }
107
108 /* l below 120 */
109 maxrgb = (256 * min(120,lum)) / 120; /* 0 .. 256 */
110 if (hue < 80)
111 res = 0;
112 else
113 if (hue < 120)
114 {
115 res = (hue - 80) * maxrgb; /* 0...10240 */
116 res /= 40; /* 0...256 */
117 }
118 else
119 if (hue < 200)
120 res = maxrgb;
121 else
122 {
123 res= (240 - hue) * maxrgb;
124 res /= 40;
125 }
126 res = res - maxrgb / 2; /* -128...128 */
127
128 /* saturation */
129 res = maxrgb / 2 + (sat * res) / 240; /* 0..256 */
130
131 /* lum above 120 */
132 if (lum > 120 && res < 256)
133 res += ((lum - 120) * (256 - res)) / 120;
134
135 return min(res, 255);
136 }
137
138 /***********************************************************************
139 * CC_RGBtoHSL [internal]
140 */
141 int CC_RGBtoHSL(char c, int r, int g, int b)
142 {
143 WORD maxi, mini, mmsum, mmdif, result = 0;
144 int iresult = 0;
145
146 maxi = max(r, b);
147 maxi = max(maxi, g);
148 mini = min(r, b);
149 mini = min(mini, g);
150
151 mmsum = maxi + mini;
152 mmdif = maxi - mini;
153
154 switch(c)
155 {
156 /* lum */
157 case 'L': mmsum *= 120; /* 0...61200=(255+255)*120 */
158 result = mmsum / 255; /* 0...240 */
159 break;
160 /* saturation */
161 case 'S': if (!mmsum)
162 result = 0;
163 else
164 if (!mini || maxi == 255)
165 result = 240;
166 else
167 {
168 result = mmdif * 240; /* 0...61200=255*240 */
169 result /= (mmsum > 255 ? mmsum = 510 - mmsum : mmsum); /* 0..255 */
170 }
171 break;
172 /* hue */
173 case 'H': if (!mmdif)
174 result = 160;
175 else
176 {
177 if (maxi == r)
178 {
179 iresult = 40 * (g - b); /* -10200 ... 10200 */
180 iresult /= (int) mmdif; /* -40 .. 40 */
181 if (iresult < 0)
182 iresult += 240; /* 0..40 and 200..240 */
183 }
184 else
185 if (maxi == g)
186 {
187 iresult = 40 * (b - r);
188 iresult /= (int) mmdif;
189 iresult += 80; /* 40 .. 120 */
190 }
191 else
192 if (maxi == b)
193 {
194 iresult = 40 * (r - g);
195 iresult /= (int) mmdif;
196 iresult += 160; /* 120 .. 200 */
197 }
198 result = iresult;
199 }
200 break;
201 }
202 return result; /* is this integer arithmetic precise enough ? */
203 }
204
205
206 /***********************************************************************
207 * CC_DrawCurrentFocusRect [internal]
208 */
209 static void CC_DrawCurrentFocusRect( const CCPRIV *lpp )
210 {
211 if (lpp->hwndFocus)
212 {
213 HDC hdc = GetDC(lpp->hwndFocus);
214 DrawFocusRect(hdc, &lpp->focusRect);
215 ReleaseDC(lpp->hwndFocus, hdc);
216 }
217 }
218
219 /***********************************************************************
220 * CC_DrawFocusRect [internal]
221 */
222 static void CC_DrawFocusRect( LPCCPRIV lpp, HWND hwnd, int x, int y, int rows, int cols)
223 {
224 RECT rect;
225 int dx, dy;
226 HDC hdc;
227
228 CC_DrawCurrentFocusRect(lpp); /* remove current focus rect */
229 /* calculate new rect */
230 GetClientRect(hwnd, &rect);
231 dx = (rect.right - rect.left) / cols;
232 dy = (rect.bottom - rect.top) / rows;
233 rect.left += (x * dx) - 2;
234 rect.top += (y * dy) - 2;
235 rect.right = rect.left + dx;
236 rect.bottom = rect.top + dy;
237 /* draw it */
238 hdc = GetDC(hwnd);
239 DrawFocusRect(hdc, &rect);
240 CopyRect(&lpp->focusRect, &rect);
241 lpp->hwndFocus = hwnd;
242 ReleaseDC(hwnd, hdc);
243 }
244
245 #define DISTANCE 4
246
247 /***********************************************************************
248 * CC_MouseCheckPredefColorArray [internal]
249 * returns 1 if one of the predefined colors is clicked
250 */
251 static int CC_MouseCheckPredefColorArray( LPCCPRIV lpp, HWND hDlg, int dlgitem, int rows, int cols,
252 LPARAM lParam )
253 {
254 HWND hwnd;
255 POINT point;
256 RECT rect;
257 int dx, dy, x, y;
258
259 CONV_LPARAMTOPOINT(lParam, &point);
260 ClientToScreen(hDlg, &point);
261 hwnd = GetDlgItem(hDlg, dlgitem);
262 GetWindowRect(hwnd, &rect);
263 if (PtInRect(&rect, point))
264 {
265 dx = (rect.right - rect.left) / cols;
266 dy = (rect.bottom - rect.top) / rows;
267 ScreenToClient(hwnd, &point);
268
269 if (point.x % dx < ( dx - DISTANCE) && point.y % dy < ( dy - DISTANCE))
270 {
271 x = point.x / dx;
272 y = point.y / dy;
273 lpp->lpcc->rgbResult = predefcolors[y][x];
274 CC_DrawFocusRect(lpp, hwnd, x, y, rows, cols);
275 return 1;
276 }
277 }
278 return 0;
279 }
280
281 /***********************************************************************
282 * CC_MouseCheckUserColorArray [internal]
283 * return 1 if the user clicked a color
284 */
285 static int CC_MouseCheckUserColorArray( LPCCPRIV lpp, HWND hDlg, int dlgitem, int rows, int cols,
286 LPARAM lParam )
287 {
288 HWND hwnd;
289 POINT point;
290 RECT rect;
291 int dx, dy, x, y;
292 COLORREF *crarr = lpp->lpcc->lpCustColors;
293
294 CONV_LPARAMTOPOINT(lParam, &point);
295 ClientToScreen(hDlg, &point);
296 hwnd = GetDlgItem(hDlg, dlgitem);
297 GetWindowRect(hwnd, &rect);
298 if (PtInRect(&rect, point))
299 {
300 dx = (rect.right - rect.left) / cols;
301 dy = (rect.bottom - rect.top) / rows;
302 ScreenToClient(hwnd, &point);
303
304 if (point.x % dx < (dx - DISTANCE) && point.y % dy < (dy - DISTANCE))
305 {
306 x = point.x / dx;
307 y = point.y / dy;
308 lpp->lpcc->rgbResult = crarr[x + (cols * y) ];
309 CC_DrawFocusRect(lpp, hwnd, x, y, rows, cols);
310 return 1;
311 }
312 }
313 return 0;
314 }
315
316 #define MAXVERT 240
317 #define MAXHORI 239
318
319 /* 240 ^...... ^^ 240
320 | . ||
321 SAT | . || LUM
322 | . ||
323 +-----> 239 ----
324 HUE
325 */
326 /***********************************************************************
327 * CC_MouseCheckColorGraph [internal]
328 */
329 static int CC_MouseCheckColorGraph( HWND hDlg, int dlgitem, int *hori, int *vert, LPARAM lParam )
330 {
331 HWND hwnd;
332 POINT point;
333 RECT rect;
334 long x,y;
335
336 CONV_LPARAMTOPOINT(lParam, &point);
337 ClientToScreen(hDlg, &point);
338 hwnd = GetDlgItem( hDlg, dlgitem );
339 GetWindowRect(hwnd, &rect);
340
341 if (!PtInRect(&rect, point))
342 return 0;
343
344 GetClientRect(hwnd, &rect);
345 ScreenToClient(hwnd, &point);
346
347 x = (long) point.x * MAXHORI;
348 x /= rect.right;
349 y = (long) (rect.bottom - point.y) * MAXVERT;
350 y /= rect.bottom;
351
352 if (x < 0) x = 0;
353 if (y < 0) y = 0;
354 if (x > MAXHORI) x = MAXHORI;
355 if (y > MAXVERT) y = MAXVERT;
356
357 if (hori)
358 *hori = x;
359 if (vert)
360 *vert = y;
361
362 return 1;
363 }
364 /***********************************************************************
365 * CC_MouseCheckResultWindow [internal]
366 * test if double click one of the result colors
367 */
368 int CC_MouseCheckResultWindow( HWND hDlg, LPARAM lParam )
369 {
370 HWND hwnd;
371 POINT point;
372 RECT rect;
373
374 CONV_LPARAMTOPOINT(lParam, &point);
375 ClientToScreen(hDlg, &point);
376 hwnd = GetDlgItem(hDlg, 0x2c5);
377 GetWindowRect(hwnd, &rect);
378 if (PtInRect(&rect, point))
379 {
380 PostMessageA(hDlg, WM_COMMAND, 0x2c9, 0);
381 return 1;
382 }
383 return 0;
384 }
385
386 /***********************************************************************
387 * CC_CheckDigitsInEdit [internal]
388 */
389 int CC_CheckDigitsInEdit( HWND hwnd, int maxval )
390 {
391 int i, k, m, result, value;
392 long editpos;
393 char buffer[30];
394
395 GetWindowTextA(hwnd, buffer, sizeof(buffer));
396 m = strlen(buffer);
397 result = 0;
398
399 for (i = 0 ; i < m ; i++)
400 if (buffer[i] < '' || buffer[i] > '9')
401 {
402 for (k = i + 1; k <= m; k++) /* delete bad character */
403 {
404 buffer[i] = buffer[k];
405 m--;
406 }
407 buffer[m] = 0;
408 result = 1;
409 }
410
411 value = atoi(buffer);
412 if (value > maxval) /* build a new string */
413 {
414 sprintf(buffer, "%d", maxval);
415 result = 2;
416 }
417 if (result)
418 {
419 editpos = SendMessageA(hwnd, EM_GETSEL, 0, 0);
420 SetWindowTextA(hwnd, buffer );
421 SendMessageA(hwnd, EM_SETSEL, 0, editpos);
422 }
423 return value;
424 }
425
426
427
428 /***********************************************************************
429 * CC_PaintSelectedColor [internal]
430 */
431 void CC_PaintSelectedColor( HWND hDlg, COLORREF cr )
432 {
433 RECT rect;
434 HDC hdc;
435 HBRUSH hBrush;
436 HWND hwnd = GetDlgItem(hDlg, 0x2c5);
437 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) )) /* if full size */
438 {
439 hdc = GetDC(hwnd);
440 GetClientRect(hwnd, &rect) ;
441 hBrush = CreateSolidBrush(cr);
442 if (hBrush)
443 {
444 FillRect(hdc, &rect, hBrush);
445 DrawEdge(hdc, &rect, BDR_SUNKENOUTER, BF_RECT);
446 DeleteObject(hBrush);
447 }
448 ReleaseDC(hwnd, hdc);
449 }
450 }
451
452 /***********************************************************************
453 * CC_PaintTriangle [internal]
454 */
455 void CC_PaintTriangle( HWND hDlg, int y)
456 {
457 HDC hDC;
458 long temp;
459 int w = LOWORD(GetDialogBaseUnits()) / 2;
460 POINT points[3];
461 int height;
462 int oben;
463 RECT rect;
464 HBRUSH hbr;
465 HWND hwnd = GetDlgItem(hDlg, 0x2be);
466 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
467
468 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6))) /* if full size */
469 {
470 GetClientRect(hwnd, &rect);
471 height = rect.bottom;
472 hDC = GetDC(hDlg);
473 points[0].y = rect.top;
474 points[0].x = rect.right; /* | /| */
475 ClientToScreen(hwnd, points); /* | / | */
476 ScreenToClient(hDlg, points); /* |< | */
477 oben = points[0].y; /* | \ | */
478 /* | \| */
479 temp = (long)height * (long)y;
480 points[0].x += 1;
481 points[0].y = oben + height - temp / (long)MAXVERT;
482 points[1].y = points[0].y + w;
483 points[2].y = points[0].y - w;
484 points[2].x = points[1].x = points[0].x + w;
485
486 hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
487 if (!hbr) hbr = GetSysColorBrush(COLOR_BTNFACE);
488 FillRect(hDC, &lpp->old3angle, hbr);
489 lpp->old3angle.left = points[0].x;
490 lpp->old3angle.right = points[1].x + 1;
491 lpp->old3angle.top = points[2].y - 1;
492 lpp->old3angle.bottom= points[1].y + 1;
493
494 hbr = SelectObject(hDC, GetStockObject(BLACK_BRUSH));
495 Polygon(hDC, points, 3);
496 SelectObject(hDC, hbr);
497
498 ReleaseDC(hDlg, hDC);
499 }
500 }
501
502
503 /***********************************************************************
504 * CC_PaintCross [internal]
505 */
506 void CC_PaintCross( HWND hDlg, int x, int y)
507 {
508 HDC hDC;
509 int w = GetDialogBaseUnits() - 1;
510 int wc = GetDialogBaseUnits() * 3 / 4;
511 HWND hwnd = GetDlgItem(hDlg, 0x2c6);
512 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
513 RECT rect;
514 POINT point, p;
515 HPEN hPen;
516
517 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) )) /* if full size */
518 {
519 GetClientRect(hwnd, &rect);
520 hDC = GetDC(hwnd);
521 SelectClipRgn( hDC, CreateRectRgnIndirect(&rect));
522
523 point.x = ((long)rect.right * (long)x) / (long)MAXHORI;
524 point.y = rect.bottom - ((long)rect.bottom * (long)y) / (long)MAXVERT;
525 if ( lpp->oldcross.left != lpp->oldcross.right )
526 BitBlt(hDC, lpp->oldcross.left, lpp->oldcross.top,
527 lpp->oldcross.right - lpp->oldcross.left,
528 lpp->oldcross.bottom - lpp->oldcross.top,
529 lpp->hdcMem, lpp->oldcross.left, lpp->oldcross.top, SRCCOPY);
530 lpp->oldcross.left = point.x - w - 1;
531 lpp->oldcross.right = point.x + w + 1;
532 lpp->oldcross.top = point.y - w - 1;
533 lpp->oldcross.bottom = point.y + w + 1;
534
535 hPen = CreatePen(PS_SOLID, 3, 0x000000); /* -black- color */
536 hPen = SelectObject(hDC, hPen);
537 MoveToEx(hDC, point.x - w, point.y, &p);
538 LineTo(hDC, point.x - wc, point.y);
539 MoveToEx(hDC, point.x + wc, point.y, &p);
540 LineTo(hDC, point.x + w, point.y);
541 MoveToEx(hDC, point.x, point.y - w, &p);
542 LineTo(hDC, point.x, point.y - wc);
543 MoveToEx(hDC, point.x, point.y + wc, &p);
544 LineTo(hDC, point.x, point.y + w);
545 DeleteObject( SelectObject(hDC, hPen));
546
547 ReleaseDC(hwnd, hDC);
548 }
549 }
550
551
552 #define XSTEPS 48
553 #define YSTEPS 24
554
555
556 /***********************************************************************
557 * CC_PrepareColorGraph [internal]
558 */
559 static void CC_PrepareColorGraph( HWND hDlg )
560 {
561 int sdif, hdif, xdif, ydif, r, g, b, hue, sat;
562 HWND hwnd = GetDlgItem(hDlg, 0x2c6);
563 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
564 HBRUSH hbrush;
565 HDC hdc ;
566 RECT rect, client;
567 HCURSOR hcursor = SetCursor( LoadCursorW(0, (LPCWSTR)IDC_WAIT) );
568
569 GetClientRect(hwnd, &client);
570 hdc = GetDC(hwnd);
571 lpp->hdcMem = CreateCompatibleDC(hdc);
572 lpp->hbmMem = CreateCompatibleBitmap(hdc, client.right, client.bottom);
573 SelectObject(lpp->hdcMem, lpp->hbmMem);
574
575 xdif = client.right / XSTEPS;
576 ydif = client.bottom / YSTEPS+1;
577 hdif = 239 / XSTEPS;
578 sdif = 240 / YSTEPS;
579 for (rect.left = hue = 0; hue < 239 + hdif; hue += hdif)
580 {
581 rect.right = rect.left + xdif;
582 rect.bottom = client.bottom;
583 for(sat = 0; sat < 240 + sdif; sat += sdif)
584 {
585 rect.top = rect.bottom - ydif;
586 r = CC_HSLtoRGB('R', hue, sat, 120);
587 g = CC_HSLtoRGB('G', hue, sat, 120);
588 b = CC_HSLtoRGB('B', hue, sat, 120);
589 hbrush = CreateSolidBrush( RGB(r, g, b));
590 FillRect(lpp->hdcMem, &rect, hbrush);
591 DeleteObject(hbrush);
592 rect.bottom = rect.top;
593 }
594 rect.left = rect.right;
595 }
596 ReleaseDC(hwnd, hdc);
597 SetCursor(hcursor);
598 }
599
600 /***********************************************************************
601 * CC_PaintColorGraph [internal]
602 */
603 static void CC_PaintColorGraph( HWND hDlg )
604 {
605 HWND hwnd = GetDlgItem( hDlg, 0x2c6 );
606 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
607 HDC hDC;
608 RECT rect;
609 if (IsWindowVisible(hwnd)) /* if full size */
610 {
611 if (!lpp->hdcMem)
612 CC_PrepareColorGraph(hDlg); /* should not be necessary */
613
614 hDC = GetDC(hwnd);
615 GetClientRect(hwnd, &rect);
616 if (lpp->hdcMem)
617 BitBlt(hDC, 0, 0, rect.right, rect.bottom, lpp->hdcMem, 0, 0, SRCCOPY);
618 else
619 WARN("choose color: hdcMem is not defined\n");
620 ReleaseDC(hwnd, hDC);
621 }
622 }
623
624 /***********************************************************************
625 * CC_PaintLumBar [internal]
626 */
627 static void CC_PaintLumBar( HWND hDlg, int hue, int sat )
628 {
629 HWND hwnd = GetDlgItem(hDlg, 0x2be);
630 RECT rect, client;
631 int lum, ldif, ydif, r, g, b;
632 HBRUSH hbrush;
633 HDC hDC;
634
635 if (IsWindowVisible(hwnd))
636 {
637 hDC = GetDC(hwnd);
638 GetClientRect(hwnd, &client);
639 rect = client;
640
641 ldif = 240 / YSTEPS;
642 ydif = client.bottom / YSTEPS+1;
643 for (lum = 0; lum < 240 + ldif; lum += ldif)
644 {
645 rect.top = max(0, rect.bottom - ydif);
646 r = CC_HSLtoRGB('R', hue, sat, lum);
647 g = CC_HSLtoRGB('G', hue, sat, lum);
648 b = CC_HSLtoRGB('B', hue, sat, lum);
649 hbrush = CreateSolidBrush( RGB(r, g, b) );
650 FillRect(hDC, &rect, hbrush);
651 DeleteObject(hbrush);
652 rect.bottom = rect.top;
653 }
654 GetClientRect(hwnd, &rect);
655 DrawEdge(hDC, &rect, BDR_SUNKENOUTER, BF_RECT);
656 ReleaseDC(hwnd, hDC);
657 }
658 }
659
660 /***********************************************************************
661 * CC_EditSetRGB [internal]
662 */
663 void CC_EditSetRGB( HWND hDlg, COLORREF cr )
664 {
665 char buffer[10];
666 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
667 int r = GetRValue(cr);
668 int g = GetGValue(cr);
669 int b = GetBValue(cr);
670 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) )) /* if full size */
671 {
672 lpp->updating = TRUE;
673 sprintf(buffer, "%d", r);
674 SetWindowTextA( GetDlgItem(hDlg, 0x2c2), buffer);
675 sprintf(buffer, "%d", g);
676 SetWindowTextA( GetDlgItem(hDlg, 0x2c3), buffer);
677 sprintf( buffer, "%d", b );
678 SetWindowTextA( GetDlgItem(hDlg, 0x2c4),buffer);
679 lpp->updating = FALSE;
680 }
681 }
682
683 /***********************************************************************
684 * CC_EditSetHSL [internal]
685 */
686 void CC_EditSetHSL( HWND hDlg, int h, int s, int l )
687 {
688 char buffer[10];
689 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
690
691 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) )) /* if full size */
692 {
693 lpp->updating = TRUE;
694 sprintf(buffer, "%d", h);
695 SetWindowTextA( GetDlgItem(hDlg, 0x2bf), buffer);
696 sprintf(buffer, "%d", s);
697 SetWindowTextA( GetDlgItem(hDlg, 0x2c0), buffer);
698 sprintf(buffer, "%d", l);
699 SetWindowTextA( GetDlgItem(hDlg, 0x2c1), buffer);
700 lpp->updating = FALSE;
701 }
702 CC_PaintLumBar(hDlg, h, s);
703 }
704
705 /***********************************************************************
706 * CC_SwitchToFullSize [internal]
707 */
708 void CC_SwitchToFullSize( HWND hDlg, COLORREF result, LPCRECT lprect )
709 {
710 int i;
711 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
712
713 EnableWindow( GetDlgItem(hDlg, 0x2cf), FALSE);
714 CC_PrepareColorGraph(hDlg);
715 for (i = 0x2bf; i < 0x2c5; i++)
716 ShowWindow( GetDlgItem(hDlg, i), SW_SHOW);
717 for (i = 0x2d3; i < 0x2d9; i++)
718 ShowWindow( GetDlgItem(hDlg, i), SW_SHOW);
719 ShowWindow( GetDlgItem(hDlg, 0x2c9), SW_SHOW);
720 ShowWindow( GetDlgItem(hDlg, 0x2c8), SW_SHOW);
721 ShowWindow( GetDlgItem(hDlg, 1090), SW_SHOW);
722
723 if (lprect)
724 SetWindowPos(hDlg, 0, 0, 0, lprect->right-lprect->left,
725 lprect->bottom-lprect->top, SWP_NOMOVE|SWP_NOZORDER);
726
727 ShowWindow( GetDlgItem(hDlg, 0x2be), SW_SHOW);
728 ShowWindow( GetDlgItem(hDlg, 0x2c5), SW_SHOW);
729
730 CC_EditSetRGB(hDlg, result);
731 CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
732 ShowWindow( GetDlgItem( hDlg, 0x2c6), SW_SHOW);
733 UpdateWindow( GetDlgItem(hDlg, 0x2c6) );
734 }
735
736 /***********************************************************************
737 * CC_PaintPredefColorArray [internal]
738 * Paints the default standard 48 colors
739 */
740 static void CC_PaintPredefColorArray( HWND hDlg, int rows, int cols)
741 {
742 HWND hwnd = GetDlgItem(hDlg, 0x2d0);
743 RECT rect, blockrect;
744 HDC hdc;
745 HBRUSH hBrush;
746 int dx, dy, i, j, k;
747 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
748
749 GetClientRect(hwnd, &rect);
750 dx = rect.right / cols;
751 dy = rect.bottom / rows;
752 k = rect.left;
753
754 hdc = GetDC(hwnd);
755 GetClientRect(hwnd, &rect);
756 hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
757 if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
758 FillRect(hdc, &rect, hBrush);
759 for ( j = 0; j < rows; j++ )
760 {
761 for ( i = 0; i < cols; i++ )
762 {
763 hBrush = CreateSolidBrush(predefcolors[j][i]);
764 if (hBrush)
765 {
766 blockrect.left = rect.left;
767 blockrect.top = rect.top;
768 blockrect.right = rect.left + dx - DISTANCE;
769 blockrect.bottom = rect.top + dy - DISTANCE;
770 FillRect(hdc, &blockrect, hBrush);
771 DrawEdge(hdc, &blockrect, BDR_SUNKEN, BF_RECT);
772 DeleteObject(hBrush);
773 }
774 rect.left += dx;
775 }
776 rect.top += dy;
777 rect.left = k;
778 }
779 ReleaseDC(hwnd, hdc);
780 if (lpp->hwndFocus == hwnd)
781 CC_DrawCurrentFocusRect(lpp);
782 }
783 /***********************************************************************
784 * CC_PaintUserColorArray [internal]
785 * Paint the 16 user-selected colors
786 */
787 void CC_PaintUserColorArray( HWND hDlg, int rows, int cols, const COLORREF *lpcr )
788 {
789 HWND hwnd = GetDlgItem(hDlg, 0x2d1);
790 RECT rect, blockrect;
791 HDC hdc;
792 HBRUSH hBrush;
793 int dx, dy, i, j, k;
794 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
795
796 GetClientRect(hwnd, &rect);
797
798 dx = rect.right / cols;
799 dy = rect.bottom / rows;
800 k = rect.left;
801
802 hdc = GetDC(hwnd);
803 if (hdc)
804 {
805 hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
806 if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
807 FillRect( hdc, &rect, hBrush );
808 for (j = 0; j < rows; j++)
809 {
810 for (i = 0; i < cols; i++)
811 {
812 hBrush = CreateSolidBrush(lpcr[i+j*cols]);
813 if (hBrush)
814 {
815 blockrect.left = rect.left;
816 blockrect.top = rect.top;
817 blockrect.right = rect.left + dx - DISTANCE;
818 blockrect.bottom = rect.top + dy - DISTANCE;
819 FillRect(hdc, &blockrect, hBrush);
820 DrawEdge(hdc, &blockrect, BDR_SUNKEN, BF_RECT);
821 DeleteObject(hBrush);
822 }
823 rect.left += dx;
824 }
825 rect.top += dy;
826 rect.left = k;
827 }
828 ReleaseDC(hwnd, hdc);
829 }
830 if (lpp->hwndFocus == hwnd)
831 CC_DrawCurrentFocusRect(lpp);
832 }
833
834
835 /***********************************************************************
836 * CC_HookCallChk [internal]
837 */
838 BOOL CC_HookCallChk( const CHOOSECOLORW *lpcc )
839 {
840 if (lpcc)
841 if(lpcc->Flags & CC_ENABLEHOOK)
842 if (lpcc->lpfnHook)
843 return TRUE;
844 return FALSE;
845 }
846
847 /***********************************************************************
848 * CC_WMInitDialog [internal]
849 */
850 static LONG CC_WMInitDialog( HWND hDlg, WPARAM wParam, LPARAM lParam )
851 {
852 int i, res;
853 int r, g, b;
854 HWND hwnd;
855 RECT rect;
856 POINT point;
857 LPCCPRIV lpp;
858
859 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam);
860 lpp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct CCPRIVATE) );
861
862 lpp->lpcc = (LPCHOOSECOLORW) lParam;
863 if (lpp->lpcc->lStructSize != sizeof(CHOOSECOLORW) )
864 {
865 HeapFree(GetProcessHeap(), 0, lpp);
866 EndDialog (hDlg, 0) ;
867 return FALSE;
868 }
869
870 SetPropW( hDlg, szColourDialogProp, lpp );
871
872 if (!(lpp->lpcc->Flags & CC_SHOWHELP))
873 ShowWindow( GetDlgItem(hDlg,0x40e), SW_HIDE);
874 lpp->msetrgb = RegisterWindowMessageA(SETRGBSTRINGA);
875
876 #if 0
877 cpos = MAKELONG(5,7); /* init */
878 if (lpp->lpcc->Flags & CC_RGBINIT)
879 {
880 for (i = 0; i < 6; i++)
881 for (j = 0; j < 8; j++)
882 if (predefcolors[i][j] == lpp->lpcc->rgbResult)
883 {
884 cpos = MAKELONG(i,j);
885 goto found;
886 }
887 }
888 found:
889 /* FIXME: Draw_a_focus_rect & set_init_values */
890 #endif
891
892 GetWindowRect(hDlg, &lpp->fullsize);
893 if (lpp->lpcc->Flags & CC_FULLOPEN || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
894 {
895 hwnd = GetDlgItem(hDlg, 0x2cf);
896 EnableWindow(hwnd, FALSE);
897 }
898 if (!(lpp->lpcc->Flags & CC_FULLOPEN ) || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
899 {
900 rect = lpp->fullsize;
901 res = rect.bottom - rect.top;
902 hwnd = GetDlgItem(hDlg, 0x2c6); /* cut at left border */
903 point.x = point.y = 0;
904 ClientToScreen(hwnd, &point);
905 ScreenToClient(hDlg,&point);
906 GetClientRect(hDlg, &rect);
907 point.x += GetSystemMetrics(SM_CXDLGFRAME);
908 SetWindowPos(hDlg, 0, 0, 0, point.x, res, SWP_NOMOVE|SWP_NOZORDER);
909
910 for (i = 0x2bf; i < 0x2c5; i++)
911 ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
912 for (i = 0x2d3; i < 0x2d9; i++)
913 ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
914 ShowWindow( GetDlgItem(hDlg, 0x2c9), SW_HIDE);
915 ShowWindow( GetDlgItem(hDlg, 0x2c8), SW_HIDE);
916 ShowWindow( GetDlgItem(hDlg, 0x2c6), SW_HIDE);
917 ShowWindow( GetDlgItem(hDlg, 0x2c5), SW_HIDE);
918 ShowWindow( GetDlgItem(hDlg, 1090 ), SW_HIDE);
919 }
920 else
921 CC_SwitchToFullSize(hDlg, lpp->lpcc->rgbResult, NULL);
922 res = TRUE;
923 for (i = 0x2bf; i < 0x2c5; i++)
924 SendMessageA( GetDlgItem(hDlg, i), EM_LIMITTEXT, 3, 0); /* max 3 digits: xyz */
925 if (CC_HookCallChk(lpp->lpcc))
926 {
927 res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, WM_INITDIALOG, wParam, lParam);
928 }
929
930 /* Set the initial values of the color chooser dialog */
931 r = GetRValue(lpp->lpcc->rgbResult);
932 g = GetGValue(lpp->lpcc->rgbResult);
933 b = GetBValue(lpp->lpcc->rgbResult);
934
935 CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
936 lpp->h = CC_RGBtoHSL('H', r, g, b);