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 */
37 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
43 static INT_PTR CALLBACK ColorDlgProc( HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam );
45 #define CONV_LPARAMTOPOINT(lp,p) do { (p)->x = (short)LOWORD(lp); (p)->y = (short)HIWORD(lp); } while(0)
47 static const COLORREF predefcolors[6][8]=
49 { 0x008080FFL, 0x0080FFFFL, 0x0080FF80L, 0x0080FF00L,
50 0x00FFFF80L, 0x00FF8000L, 0x00C080FFL, 0x00FF80FFL },
51 { 0x000000FFL, 0x0000FFFFL, 0x0000FF80L, 0x0040FF00L,
52 0x00FFFF00L, 0x00C08000L, 0x00C08080L, 0x00FF00FFL },
54 { 0x00404080L, 0x004080FFL, 0x0000FF00L, 0x00808000L,
55 0x00804000L, 0x00FF8080L, 0x00400080L, 0x008000FFL },
56 { 0x00000080L, 0x000080FFL, 0x00008000L, 0x00408000L,
57 0x00FF0000L, 0x00A00000L, 0x00800080L, 0x00FF0080L },
59 { 0x00000040L, 0x00004080L, 0x00004000L, 0x00404000L,
60 0x00800000L, 0x00400000L, 0x00400040L, 0x00800040L },
61 { 0x00000000L, 0x00008080L, 0x00408080L, 0x00808080L,
62 0x00808040L, 0x00C0C0C0L, 0x00400040L, 0x00FFFFFFL },
65 static const WCHAR szColourDialogProp[] = {
66 'c','o','l','o','u','r','d','i','a','l','o','g','p','r','o','p',0 };
68 /* Chose Color PRIVATE Structure:
70 * This structure is duplicated in the 16 bit code with
74 typedef struct CCPRIVATE
76 LPCHOOSECOLORW lpcc; /* points to public known data structure */
77 int nextuserdef; /* next free place in user defined color array */
78 HDC hdcMem; /* color graph used for BitBlt() */
79 HBITMAP hbmMem; /* color graph bitmap */
80 RECT fullsize; /* original dialog window size */
81 UINT msetrgb; /* # of SETRGBSTRING message (today not used) */
82 RECT old3angle; /* last position of l-marker */
83 RECT oldcross; /* last position of color/satuation marker */
84 BOOL updating; /* to prevent recursive WM_COMMAND/EN_UPDATE processing */
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 */
93 /***********************************************************************
94 * CC_HSLtoRGB [internal]
96 int CC_HSLtoRGB(char c, int hue, int sat, int lum)
103 case 'R': if (hue > 80) hue -= 80; else hue += 160; break;
104 case 'G': if (hue > 160) hue -= 160; else hue += 80; break;
109 maxrgb = (256 * min(120,lum)) / 120; /* 0 .. 256 */
115 res = (hue - 80) * maxrgb; /* 0...10240 */
116 res /= 40; /* 0...256 */
123 res= (240 - hue) * maxrgb;
126 res = res - maxrgb / 2; /* -128...128 */
129 res = maxrgb / 2 + (sat * res) / 240; /* 0..256 */
132 if (lum > 120 && res < 256)
133 res += ((lum - 120) * (256 - res)) / 120;
135 return min(res, 255);
138 /***********************************************************************
139 * CC_RGBtoHSL [internal]
141 int CC_RGBtoHSL(char c, int r, int g, int b)
143 WORD maxi, mini, mmsum, mmdif, result = 0;
157 case 'L': mmsum *= 120; /* 0...61200=(255+255)*120 */
158 result = mmsum / 255; /* 0...240 */
161 case 'S': if (!mmsum)
164 if (!mini || maxi == 255)
168 result = mmdif * 240; /* 0...61200=255*240 */
169 result /= (mmsum > 255 ? mmsum = 510 - mmsum : mmsum); /* 0..255 */
173 case 'H': if (!mmdif)
179 iresult = 40 * (g - b); /* -10200 ... 10200 */
180 iresult /= (int) mmdif; /* -40 .. 40 */
182 iresult += 240; /* 0..40 and 200..240 */
187 iresult = 40 * (b - r);
188 iresult /= (int) mmdif;
189 iresult += 80; /* 40 .. 120 */
194 iresult = 40 * (r - g);
195 iresult /= (int) mmdif;
196 iresult += 160; /* 120 .. 200 */
202 return result; /* is this integer arithmetic precise enough ? */
206 /***********************************************************************
207 * CC_DrawCurrentFocusRect [internal]
209 static void CC_DrawCurrentFocusRect( const CCPRIV *lpp )
213 HDC hdc = GetDC(lpp->hwndFocus);
214 DrawFocusRect(hdc, &lpp->focusRect);
215 ReleaseDC(lpp->hwndFocus, hdc);
219 /***********************************************************************
220 * CC_DrawFocusRect [internal]
222 static void CC_DrawFocusRect( LPCCPRIV lpp, HWND hwnd, int x, int y, int rows, int cols)
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;
239 DrawFocusRect(hdc, &rect);
240 CopyRect(&lpp->focusRect, &rect);
241 lpp->hwndFocus = hwnd;
242 ReleaseDC(hwnd, hdc);
247 /***********************************************************************
248 * CC_MouseCheckPredefColorArray [internal]
249 * returns 1 if one of the predefined colors is clicked
251 static int CC_MouseCheckPredefColorArray( LPCCPRIV lpp, HWND hDlg, int dlgitem, int rows, int cols,
259 CONV_LPARAMTOPOINT(lParam, &point);
260 ClientToScreen(hDlg, &point);
261 hwnd = GetDlgItem(hDlg, dlgitem);
262 GetWindowRect(hwnd, &rect);
263 if (PtInRect(&rect, point))
265 dx = (rect.right - rect.left) / cols;
266 dy = (rect.bottom - rect.top) / rows;
267 ScreenToClient(hwnd, &point);
269 if (point.x % dx < ( dx - DISTANCE) && point.y % dy < ( dy - DISTANCE))
273 lpp->lpcc->rgbResult = predefcolors[y][x];
274 CC_DrawFocusRect(lpp, hwnd, x, y, rows, cols);
281 /***********************************************************************
282 * CC_MouseCheckUserColorArray [internal]
283 * return 1 if the user clicked a color
285 static int CC_MouseCheckUserColorArray( LPCCPRIV lpp, HWND hDlg, int dlgitem, int rows, int cols,
292 COLORREF *crarr = lpp->lpcc->lpCustColors;
294 CONV_LPARAMTOPOINT(lParam, &point);
295 ClientToScreen(hDlg, &point);
296 hwnd = GetDlgItem(hDlg, dlgitem);
297 GetWindowRect(hwnd, &rect);
298 if (PtInRect(&rect, point))
300 dx = (rect.right - rect.left) / cols;
301 dy = (rect.bottom - rect.top) / rows;
302 ScreenToClient(hwnd, &point);
304 if (point.x % dx < (dx - DISTANCE) && point.y % dy < (dy - DISTANCE))
308 lpp->lpcc->rgbResult = crarr[x + (cols * y) ];
309 CC_DrawFocusRect(lpp, hwnd, x, y, rows, cols);
319 /* 240 ^...... ^^ 240
326 /***********************************************************************
327 * CC_MouseCheckColorGraph [internal]
329 static int CC_MouseCheckColorGraph( HWND hDlg, int dlgitem, int *hori, int *vert, LPARAM lParam )
336 CONV_LPARAMTOPOINT(lParam, &point);
337 ClientToScreen(hDlg, &point);
338 hwnd = GetDlgItem( hDlg, dlgitem );
339 GetWindowRect(hwnd, &rect);
340 if (PtInRect(&rect, point))
342 GetClientRect(hwnd, &rect);
343 ScreenToClient(hwnd, &point);
345 x = (long) point.x * MAXHORI;
347 y = (long) (rect.bottom - point.y) * MAXVERT;
359 /***********************************************************************
360 * CC_MouseCheckResultWindow [internal]
361 * test if double click one of the result colors
363 int CC_MouseCheckResultWindow( HWND hDlg, LPARAM lParam )
369 CONV_LPARAMTOPOINT(lParam, &point);
370 ClientToScreen(hDlg, &point);
371 hwnd = GetDlgItem(hDlg, 0x2c5);
372 GetWindowRect(hwnd, &rect);
373 if (PtInRect(&rect, point))
375 PostMessageA(hDlg, WM_COMMAND, 0x2c9, 0);
381 /***********************************************************************
382 * CC_CheckDigitsInEdit [internal]
384 int CC_CheckDigitsInEdit( HWND hwnd, int maxval )
386 int i, k, m, result, value;
390 GetWindowTextA(hwnd, buffer, sizeof(buffer));
394 for (i = 0 ; i < m ; i++)
395 if (buffer[i] < '0' || buffer[i] > '9')
397 for (k = i + 1; k <= m; k++) /* delete bad character */
399 buffer[i] = buffer[k];
406 value = atoi(buffer);
407 if (value > maxval) /* build a new string */
409 sprintf(buffer, "%d", maxval);
414 editpos = SendMessageA(hwnd, EM_GETSEL, 0, 0);
415 SetWindowTextA(hwnd, buffer );
416 SendMessageA(hwnd, EM_SETSEL, 0, editpos);
423 /***********************************************************************
424 * CC_PaintSelectedColor [internal]
426 void CC_PaintSelectedColor( HWND hDlg, COLORREF cr )
431 HWND hwnd = GetDlgItem(hDlg, 0x2c5);
432 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) )) /* if full size */
435 GetClientRect(hwnd, &rect) ;
436 hBrush = CreateSolidBrush(cr);
439 hBrush = SelectObject(hdc, hBrush) ;
440 Rectangle(hdc, rect.left, rect.top, rect.right/2, rect.bottom);
441 DeleteObject ( SelectObject(hdc, hBrush) ) ;
442 hBrush = CreateSolidBrush( GetNearestColor(hdc, cr) );
445 hBrush = SelectObject(hdc, hBrush) ;
446 Rectangle(hdc, rect.right/2-1, rect.top, rect.right, rect.bottom);
447 DeleteObject(SelectObject(hdc, hBrush)) ;
450 ReleaseDC(hwnd, hdc);
454 /***********************************************************************
455 * CC_PaintTriangle [internal]
457 void CC_PaintTriangle( HWND hDlg, int y)
461 int w = LOWORD(GetDialogBaseUnits());
467 HWND hwnd = GetDlgItem(hDlg, 0x2be);
468 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
470 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6))) /* if full size */
472 GetClientRect(hwnd, &rect);
473 height = rect.bottom;
475 points[0].y = rect.top;
476 points[0].x = rect.right; /* | /| */
477 ClientToScreen(hwnd, points); /* | / | */
478 ScreenToClient(hDlg, points); /* |< | */
479 oben = points[0].y; /* | \ | */
481 temp = (long)height * (long)y;
482 points[0].y = oben + height - temp / (long)MAXVERT;
483 points[1].y = points[0].y + w;
484 points[2].y = points[0].y - w;
485 points[2].x = points[1].x = points[0].x + w;
487 hbr = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
488 if (!hbr) hbr = GetSysColorBrush(COLOR_BTNFACE);
489 FillRect(hDC, &lpp->old3angle, hbr);
490 lpp->old3angle.left = points[0].x;
491 lpp->old3angle.right = points[1].x + 1;
492 lpp->old3angle.top = points[2].y - 1;
493 lpp->old3angle.bottom= points[1].y + 1;
494 Polygon(hDC, points, 3);
495 ReleaseDC(hDlg, hDC);
500 /***********************************************************************
501 * CC_PaintCross [internal]
503 void CC_PaintCross( HWND hDlg, int x, int y)
506 int w = GetDialogBaseUnits();
507 HWND hwnd = GetDlgItem(hDlg, 0x2c6);
508 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
513 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) )) /* if full size */
515 GetClientRect(hwnd, &rect);
517 SelectClipRgn( hDC, CreateRectRgnIndirect(&rect));
518 hPen = CreatePen(PS_SOLID, 2, 0xffffff); /* -white- color */
519 hPen = SelectObject(hDC, hPen);
520 point.x = ((long)rect.right * (long)x) / (long)MAXHORI;
521 point.y = rect.bottom - ((long)rect.bottom * (long)y) / (long)MAXVERT;
522 if ( lpp->oldcross.left != lpp->oldcross.right )
523 BitBlt(hDC, lpp->oldcross.left, lpp->oldcross.top,
524 lpp->oldcross.right - lpp->oldcross.left,
525 lpp->oldcross.bottom - lpp->oldcross.top,
526 lpp->hdcMem, lpp->oldcross.left, lpp->oldcross.top, SRCCOPY);
527 lpp->oldcross.left = point.x - w - 1;
528 lpp->oldcross.right = point.x + w + 1;
529 lpp->oldcross.top = point.y - w - 1;
530 lpp->oldcross.bottom = point.y + w + 1;
532 MoveToEx(hDC, point.x - w, point.y, &p);
533 LineTo(hDC, point.x + w, point.y);
534 MoveToEx(hDC, point.x, point.y - w, &p);
535 LineTo(hDC, point.x, point.y + w);
536 DeleteObject( SelectObject(hDC, hPen)) ;
537 ReleaseDC(hwnd, hDC);
546 /***********************************************************************
547 * CC_PrepareColorGraph [internal]
549 static void CC_PrepareColorGraph( HWND hDlg )
551 int sdif, hdif, xdif, ydif, r, g, b, hue, sat;
552 HWND hwnd = GetDlgItem(hDlg, 0x2c6);
553 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
557 HCURSOR hcursor = SetCursor( LoadCursorW(0, (LPCWSTR)IDC_WAIT) );
559 GetClientRect(hwnd, &client);
561 lpp->hdcMem = CreateCompatibleDC(hdc);
562 lpp->hbmMem = CreateCompatibleBitmap(hdc, client.right, client.bottom);
563 SelectObject(lpp->hdcMem, lpp->hbmMem);
565 xdif = client.right / XSTEPS;
566 ydif = client.bottom / YSTEPS+1;
569 for (rect.left = hue = 0; hue < 239 + hdif; hue += hdif)
571 rect.right = rect.left + xdif;
572 rect.bottom = client.bottom;
573 for(sat = 0; sat < 240 + sdif; sat += sdif)
575 rect.top = rect.bottom - ydif;
576 r = CC_HSLtoRGB('R', hue, sat, 120);
577 g = CC_HSLtoRGB('G', hue, sat, 120);
578 b = CC_HSLtoRGB('B', hue, sat, 120);
579 hbrush = CreateSolidBrush( RGB(r, g, b));
580 FillRect(lpp->hdcMem, &rect, hbrush);
581 DeleteObject(hbrush);
582 rect.bottom = rect.top;
584 rect.left = rect.right;
586 ReleaseDC(hwnd, hdc);
590 /***********************************************************************
591 * CC_PaintColorGraph [internal]
593 static void CC_PaintColorGraph( HWND hDlg )
595 HWND hwnd = GetDlgItem( hDlg, 0x2c6 );
596 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
599 if (IsWindowVisible(hwnd)) /* if full size */
602 CC_PrepareColorGraph(hDlg); /* should not be necessary */
605 GetClientRect(hwnd, &rect);
607 BitBlt(hDC, 0, 0, rect.right, rect.bottom, lpp->hdcMem, 0, 0, SRCCOPY);
609 WARN("choose color: hdcMem is not defined\n");
610 ReleaseDC(hwnd, hDC);
614 /***********************************************************************
615 * CC_PaintLumBar [internal]
617 static void CC_PaintLumBar( HWND hDlg, int hue, int sat )
619 HWND hwnd = GetDlgItem(hDlg, 0x2be);
621 int lum, ldif, ydif, r, g, b;
625 if (IsWindowVisible(hwnd))
628 GetClientRect(hwnd, &client);
632 ydif = client.bottom / YSTEPS+1;
633 for (lum = 0; lum < 240 + ldif; lum += ldif)
635 rect.top = max(0, rect.bottom - ydif);
636 r = CC_HSLtoRGB('R', hue, sat, lum);
637 g = CC_HSLtoRGB('G', hue, sat, lum);
638 b = CC_HSLtoRGB('B', hue, sat, lum);
639 hbrush = CreateSolidBrush( RGB(r, g, b) );
640 FillRect(hDC, &rect, hbrush);
641 DeleteObject(hbrush);
642 rect.bottom = rect.top;
644 GetClientRect(hwnd, &rect);
645 FrameRect(hDC, &rect, GetStockObject(BLACK_BRUSH) );
646 ReleaseDC(hwnd, hDC);
650 /***********************************************************************
651 * CC_EditSetRGB [internal]
653 void CC_EditSetRGB( HWND hDlg, COLORREF cr )
656 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
657 int r = GetRValue(cr);
658 int g = GetGValue(cr);
659 int b = GetBValue(cr);
660 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) )) /* if full size */
662 lpp->updating = TRUE;
663 sprintf(buffer, "%d", r);
664 SetWindowTextA( GetDlgItem(hDlg, 0x2c2), buffer);
665 sprintf(buffer, "%d", g);
666 SetWindowTextA( GetDlgItem(hDlg, 0x2c3), buffer);
667 sprintf( buffer, "%d", b );
668 SetWindowTextA( GetDlgItem(hDlg, 0x2c4),buffer);
669 lpp->updating = FALSE;
673 /***********************************************************************
674 * CC_EditSetHSL [internal]
676 void CC_EditSetHSL( HWND hDlg, int h, int s, int l )
679 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
681 if (IsWindowVisible( GetDlgItem(hDlg, 0x2c6) )) /* if full size */
683 lpp->updating = TRUE;
684 sprintf(buffer, "%d", h);
685 SetWindowTextA( GetDlgItem(hDlg, 0x2bf), buffer);
686 sprintf(buffer, "%d", s);
687 SetWindowTextA( GetDlgItem(hDlg, 0x2c0), buffer);
688 sprintf(buffer, "%d", l);
689 SetWindowTextA( GetDlgItem(hDlg, 0x2c1), buffer);
690 lpp->updating = FALSE;
692 CC_PaintLumBar(hDlg, h, s);
695 /***********************************************************************
696 * CC_SwitchToFullSize [internal]
698 void CC_SwitchToFullSize( HWND hDlg, COLORREF result, LPCRECT lprect )
701 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
703 EnableWindow( GetDlgItem(hDlg, 0x2cf), FALSE);
704 CC_PrepareColorGraph(hDlg);
705 for (i = 0x2bf; i < 0x2c5; i++)
706 ShowWindow( GetDlgItem(hDlg, i), SW_SHOW);
707 for (i = 0x2d3; i < 0x2d9; i++)
708 ShowWindow( GetDlgItem(hDlg, i), SW_SHOW);
709 ShowWindow( GetDlgItem(hDlg, 0x2c9), SW_SHOW);
710 ShowWindow( GetDlgItem(hDlg, 0x2c8), SW_SHOW);
711 ShowWindow( GetDlgItem(hDlg, 1090), SW_SHOW);
714 SetWindowPos(hDlg, 0, 0, 0, lprect->right-lprect->left,
715 lprect->bottom-lprect->top, SWP_NOMOVE|SWP_NOZORDER);
717 ShowWindow( GetDlgItem(hDlg, 0x2be), SW_SHOW);
718 ShowWindow( GetDlgItem(hDlg, 0x2c5), SW_SHOW);
720 CC_EditSetRGB(hDlg, result);
721 CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
722 ShowWindow( GetDlgItem( hDlg, 0x2c6), SW_SHOW);
723 UpdateWindow( GetDlgItem(hDlg, 0x2c6) );
726 /***********************************************************************
727 * CC_PaintPredefColorArray [internal]
728 * Paints the default standard 48 colors
730 static void CC_PaintPredefColorArray( HWND hDlg, int rows, int cols)
732 HWND hwnd = GetDlgItem(hDlg, 0x2d0);
737 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
739 GetClientRect(hwnd, &rect);
740 dx = rect.right / cols;
741 dy = rect.bottom / rows;
745 GetClientRect(hwnd, &rect);
746 hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
747 if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
748 FillRect(hdc, &rect, hBrush);
749 for ( j = 0; j < rows; j++ )
751 for ( i = 0; i < cols; i++ )
753 hBrush = CreateSolidBrush(predefcolors[j][i]);
756 hBrush = SelectObject(hdc, hBrush);
757 Rectangle(hdc, rect.left, rect.top,
758 rect.left + dx - DISTANCE, rect.top + dy - DISTANCE);
759 rect.left = rect.left + dx;
760 DeleteObject(SelectObject(hdc, hBrush)) ;
763 rect.top = rect.top + dy;
766 ReleaseDC(hwnd, hdc);
767 if (lpp->hwndFocus == hwnd)
768 CC_DrawCurrentFocusRect(lpp);
770 /***********************************************************************
771 * CC_PaintUserColorArray [internal]
772 * Paint the 16 user-selected colors
774 void CC_PaintUserColorArray( HWND hDlg, int rows, int cols, const COLORREF *lpcr )
776 HWND hwnd = GetDlgItem(hDlg, 0x2d1);
781 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
783 GetClientRect(hwnd, &rect);
785 dx = rect.right / cols;
786 dy = rect.bottom / rows;
792 hBrush = (HBRUSH)GetClassLongPtrW( hwnd, GCLP_HBRBACKGROUND);
793 if (!hBrush) hBrush = GetSysColorBrush(COLOR_BTNFACE);
794 FillRect( hdc, &rect, hBrush );
795 for (j = 0; j < rows; j++)
797 for (i = 0; i < cols; i++)
799 hBrush = CreateSolidBrush(lpcr[i+j*cols]);
802 hBrush = SelectObject(hdc, hBrush) ;
803 Rectangle(hdc, rect.left, rect.top,
804 rect.left + dx - DISTANCE, rect.top + dy - DISTANCE);
805 rect.left = rect.left + dx;
806 DeleteObject( SelectObject(hdc, hBrush) ) ;
809 rect.top = rect.top + dy;
812 ReleaseDC(hwnd, hdc);
814 if (lpp->hwndFocus == hwnd)
815 CC_DrawCurrentFocusRect(lpp);
820 /***********************************************************************
821 * CC_HookCallChk [internal]
823 BOOL CC_HookCallChk( const CHOOSECOLORW *lpcc )
826 if(lpcc->Flags & CC_ENABLEHOOK)
832 /***********************************************************************
833 * CC_WMInitDialog [internal]
835 static LONG CC_WMInitDialog( HWND hDlg, WPARAM wParam, LPARAM lParam )
844 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam);
845 lpp = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct CCPRIVATE) );
847 lpp->lpcc = (LPCHOOSECOLORW) lParam;
848 if (lpp->lpcc->lStructSize != sizeof(CHOOSECOLORW) )
850 HeapFree(GetProcessHeap(), 0, lpp);
851 EndDialog (hDlg, 0) ;
855 SetPropW( hDlg, szColourDialogProp, lpp );
857 if (!(lpp->lpcc->Flags & CC_SHOWHELP))
858 ShowWindow( GetDlgItem(hDlg,0x40e), SW_HIDE);
859 lpp->msetrgb = RegisterWindowMessageA(SETRGBSTRINGA);
862 cpos = MAKELONG(5,7); /* init */
863 if (lpp->lpcc->Flags & CC_RGBINIT)
865 for (i = 0; i < 6; i++)
866 for (j = 0; j < 8; j++)
867 if (predefcolors[i][j] == lpp->lpcc->rgbResult)
869 cpos = MAKELONG(i,j);
874 /* FIXME: Draw_a_focus_rect & set_init_values */
877 GetWindowRect(hDlg, &lpp->fullsize);
878 if (lpp->lpcc->Flags & CC_FULLOPEN || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
880 hwnd = GetDlgItem(hDlg, 0x2cf);
881 EnableWindow(hwnd, FALSE);
883 if (!(lpp->lpcc->Flags & CC_FULLOPEN ) || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
885 rect = lpp->fullsize;
886 res = rect.bottom - rect.top;
887 hwnd = GetDlgItem(hDlg, 0x2c6); /* cut at left border */
888 point.x = point.y = 0;
889 ClientToScreen(hwnd, &point);
890 ScreenToClient(hDlg,&point);
891 GetClientRect(hDlg, &rect);
892 point.x += GetSystemMetrics(SM_CXDLGFRAME);
893 SetWindowPos(hDlg, 0, 0, 0, point.x, res, SWP_NOMOVE|SWP_NOZORDER);
895 for (i = 0x2bf; i < 0x2c5; i++)
896 ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
897 for (i = 0x2d3; i < 0x2d9; i++)
898 ShowWindow( GetDlgItem(hDlg, i), SW_HIDE);
899 ShowWindow( GetDlgItem(hDlg, 0x2c9), SW_HIDE);
900 ShowWindow( GetDlgItem(hDlg, 0x2c8), SW_HIDE);
901 ShowWindow( GetDlgItem(hDlg, 0x2c6), SW_HIDE);
902 ShowWindow( GetDlgItem(hDlg, 0x2c5), SW_HIDE);
903 ShowWindow( GetDlgItem(hDlg, 1090 ), SW_HIDE);
906 CC_SwitchToFullSize(hDlg, lpp->lpcc->rgbResult, NULL);
908 for (i = 0x2bf; i < 0x2c5; i++)
909 SendMessageA( GetDlgItem(hDlg, i), EM_LIMITTEXT, 3, 0); /* max 3 digits: xyz */
910 if (CC_HookCallChk(lpp->lpcc))
912 res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, WM_INITDIALOG, wParam, lParam);
915 /* Set the initial values of the color chooser dialog */
916 r = GetRValue(lpp->lpcc->rgbResult);
917 g = GetGValue(lpp->lpcc->rgbResult);
918 b = GetBValue(lpp->lpcc->rgbResult);
920 CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
921 lpp->h = CC_RGBtoHSL('H', r, g, b);
922 lpp->s = CC_RGBtoHSL('S', r, g, b);
923 lpp->l = CC_RGBtoHSL('L', r, g, b);
925 /* Doing it the long way because CC_EditSetRGB/HSL doesn't seem to work */
926 SetDlgItemInt(hDlg, 703, lpp->h, TRUE);
927 SetDlgItemInt(hDlg, 704, lpp->s, TRUE);
928 SetDlgItemInt(hDlg, 705, lpp->l, TRUE);
929 SetDlgItemInt(hDlg, 706, r, TRUE);
930 SetDlgItemInt(hDlg, 707, g, TRUE);
931 SetDlgItemInt(hDlg, 708, b, TRUE);
933 CC_PaintCross(hDlg, lpp->h, lpp->s);
934 CC_PaintTriangle(hDlg, lpp->l);
940 /***********************************************************************
941 * CC_WMCommand [internal]
943 LRESULT CC_WMCommand( HWND hDlg, WPARAM wParam, LPARAM lParam, WORD notifyCode, HWND hwndCtl )
949 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
950 TRACE("CC_WMCommand wParam=%lx lParam=%lx\n", wParam, lParam);
951 switch (LOWORD(wParam))
953 case 0x2c2: /* edit notify RGB */
956 if (notifyCode == EN_UPDATE && !lpp->updating)
958 i = CC_CheckDigitsInEdit(hwndCtl, 255);
959 r = GetRValue(lpp->lpcc->rgbResult);
960 g = GetGValue(lpp->lpcc->rgbResult);
961 b= GetBValue(lpp->lpcc->rgbResult);
963 switch (LOWORD(wParam))
965 case 0x2c2: if ((xx = (i != r))) r = i; break;
966 case 0x2c3: if ((xx = (i != g))) g = i; break;
967 case 0x2c4: if ((xx = (i != b))) b = i; break;
969 if (xx) /* something has changed */
971 lpp->lpcc->rgbResult = RGB(r, g, b);
972 CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
973 lpp->h = CC_RGBtoHSL('H', r, g, b);
974 lpp->s = CC_RGBtoHSL('S', r, g, b);
975 lpp->l = CC_RGBtoHSL('L', r, g, b);
976 CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
977 CC_PaintCross(hDlg, lpp->h, lpp->s);
978 CC_PaintTriangle(hDlg, lpp->l);
983 case 0x2bf: /* edit notify HSL */
986 if (notifyCode == EN_UPDATE && !lpp->updating)
988 i = CC_CheckDigitsInEdit(hwndCtl , LOWORD(wParam) == 0x2bf ? 239:240);
990 switch (LOWORD(wParam))
992 case 0x2bf: if ((xx = ( i != lpp->h))) lpp->h = i; break;
993 case 0x2c0: if ((xx = ( i != lpp->s))) lpp->s = i; break;
994 case 0x2c1: if ((xx = ( i != lpp->l))) lpp->l = i; break;
996 if (xx) /* something has changed */
998 r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
999 g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1000 b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1001 lpp->lpcc->rgbResult = RGB(r, g, b);
1002 CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1003 CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1004 CC_PaintCross(hDlg, lpp->h, lpp->s);
1005 CC_PaintTriangle(hDlg, lpp->l);
1011 CC_SwitchToFullSize(hDlg, lpp->lpcc->rgbResult, &lpp->fullsize);
1012 SetFocus( GetDlgItem(hDlg, 0x2bf));
1015 case 0x2c8: /* add colors ... column by column */
1016 cr = lpp->lpcc->lpCustColors;
1017 cr[(lpp->nextuserdef % 2) * 8 + lpp->nextuserdef / 2] = lpp->lpcc->rgbResult;
1018 if (++lpp->nextuserdef == 16)
1019 lpp->nextuserdef = 0;
1020 CC_PaintUserColorArray(hDlg, 2, 8, lpp->lpcc->lpCustColors);
1023 case 0x2c9: /* resulting color */
1025 lpp->lpcc->rgbResult = GetNearestColor(hdc, lpp->lpcc->rgbResult);
1026 ReleaseDC(hDlg, hdc);
1027 CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1028 CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1029 r = GetRValue(lpp->lpcc->rgbResult);
1030 g = GetGValue(lpp->lpcc->rgbResult);
1031 b = GetBValue(lpp->lpcc->rgbResult);
1032 lpp->h = CC_RGBtoHSL('H', r, g, b);
1033 lpp->s = CC_RGBtoHSL('S', r, g, b);
1034 lpp->l = CC_RGBtoHSL('L', r, g, b);
1035 CC_EditSetHSL(hDlg, lpp->h, lpp->s, lpp->l);
1036 CC_PaintCross(hDlg, lpp->h, lpp->s);
1037 CC_PaintTriangle(hDlg, lpp->l);
1040 case 0x40e: /* Help! */ /* The Beatles, 1965 ;-) */
1041 i = RegisterWindowMessageA(HELPMSGSTRINGA);
1042 if (lpp->lpcc->hwndOwner)
1043 SendMessageA(lpp->lpcc->hwndOwner, i, 0, (LPARAM)lpp->lpcc);
1044 if ( CC_HookCallChk(lpp->lpcc))
1045 CallWindowProcA( (WNDPROC) lpp->lpcc->lpfnHook, hDlg,
1046 WM_COMMAND, psh15, (LPARAM)lpp->lpcc);
1050 cokmsg = RegisterWindowMessageA(COLOROKSTRINGA);
1051 if (lpp->lpcc->hwndOwner)
1052 if (SendMessageA(lpp->lpcc->hwndOwner, cokmsg, 0, (LPARAM)lpp->lpcc))
1053 break; /* do NOT close */
1054 EndDialog(hDlg, 1) ;
1058 EndDialog(hDlg, 0) ;
1065 /***********************************************************************
1066 * CC_WMPaint [internal]
1068 LRESULT CC_WMPaint( HWND hDlg, WPARAM wParam, LPARAM lParam )
1071 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1073 BeginPaint(hDlg, &ps);
1074 /* we have to paint dialog children except text and buttons */
1075 CC_PaintPredefColorArray(hDlg, 6, 8);
1076 CC_PaintUserColorArray(hDlg, 2, 8, lpp->lpcc->lpCustColors);
1077 CC_PaintLumBar(hDlg, lpp->h, lpp->s);
1078 CC_PaintCross(hDlg, lpp->h, lpp->s);
1079 CC_PaintTriangle(hDlg, lpp->l);
1080 CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1081 CC_PaintColorGraph(hDlg);
1082 EndPaint(hDlg, &ps);
1087 /***********************************************************************
1088 * CC_WMLButtonUp [internal]
1090 LRESULT CC_WMLButtonUp( HWND hDlg, WPARAM wParam, LPARAM lParam )
1092 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1093 if (lpp->capturedGraph)
1095 lpp->capturedGraph = 0;
1097 CC_PaintCross(hDlg, lpp->h, lpp->s);
1103 /***********************************************************************
1104 * CC_WMMouseMove [internal]
1106 LRESULT CC_WMMouseMove( HWND hDlg, LPARAM lParam )
1108 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1111 if (lpp->capturedGraph)
1113 int *ptrh = NULL, *ptrs = &lpp->l;
1114 if (lpp->capturedGraph == 0x2c6)
1119 if (CC_MouseCheckColorGraph( hDlg, lpp->capturedGraph, ptrh, ptrs, lParam))
1121 r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1122 g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1123 b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1124 lpp->lpcc->rgbResult = RGB(r, g, b);
1125 CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1126 CC_EditSetHSL(hDlg,lpp->h, lpp->s, lpp->l);
1127 CC_PaintCross(hDlg, lpp->h, lpp->s);
1128 CC_PaintTriangle(hDlg, lpp->l);
1129 CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1134 lpp->capturedGraph = 0;
1141 /***********************************************************************
1142 * CC_WMLButtonDown [internal]
1144 LRESULT CC_WMLButtonDown( HWND hDlg, WPARAM wParam, LPARAM lParam )
1146 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1150 if (CC_MouseCheckPredefColorArray(lpp, hDlg, 0x2d0, 6, 8, lParam))
1153 if (CC_MouseCheckUserColorArray(lpp, hDlg, 0x2d1, 2, 8, lParam))
1156 if (CC_MouseCheckColorGraph(hDlg, 0x2c6, &lpp->h, &lpp->s, lParam))
1159 lpp->capturedGraph = 0x2c6;
1162 if (CC_MouseCheckColorGraph(hDlg, 0x2be, NULL, &lpp->l, lParam))
1165 lpp->capturedGraph = 0x2be;
1170 r = CC_HSLtoRGB('R', lpp->h, lpp->s, lpp->l);
1171 g = CC_HSLtoRGB('G', lpp->h, lpp->s, lpp->l);
1172 b = CC_HSLtoRGB('B', lpp->h, lpp->s, lpp->l);
1173 lpp->lpcc->rgbResult = RGB(r, g, b);
1177 r = GetRValue(lpp->lpcc->rgbResult);
1178 g = GetGValue(lpp->lpcc->rgbResult);
1179 b = GetBValue(lpp->lpcc->rgbResult);
1180 lpp->h = CC_RGBtoHSL('H', r, g, b);
1181 lpp->s = CC_RGBtoHSL('S', r, g, b);
1182 lpp->l = CC_RGBtoHSL('L', r, g, b);
1186 CC_EditSetRGB(hDlg, lpp->lpcc->rgbResult);
1187 CC_EditSetHSL(hDlg,lpp->h, lpp->s, lpp->l);
1188 CC_PaintCross(hDlg, lpp->h, lpp->s);
1189 CC_PaintTriangle(hDlg, lpp->l);
1190 CC_PaintSelectedColor(hDlg, lpp->lpcc->rgbResult);
1196 /***********************************************************************
1197 * ColorDlgProc32 [internal]
1200 static INT_PTR CALLBACK ColorDlgProc( HWND hDlg, UINT message,
1201 WPARAM wParam, LPARAM lParam )
1205 LPCCPRIV lpp = (LPCCPRIV) GetPropW( hDlg, szColourDialogProp );
1206 if (message != WM_INITDIALOG)
1211 if (CC_HookCallChk(lpp->lpcc))
1212 res = CallWindowProcA( (WNDPROC)lpp->lpcc->lpfnHook, hDlg, message, wParam, lParam);
1217 /* FIXME: SetRGB message
1218 if (message && message == msetrgb)
1219 return HandleSetRGB(hDlg, lParam);
1225 return CC_WMInitDialog(hDlg, wParam, lParam);
1227 DeleteDC(lpp->hdcMem);
1228 DeleteObject(lpp->hbmMem);
1229 HeapFree(GetProcessHeap(), 0, lpp);
1230 RemovePropW( hDlg, szColourDialogProp );
1233 if (CC_WMCommand( hDlg, wParam, lParam, HIWORD(wParam), (HWND) lParam))
1237 if ( CC_WMPaint(hDlg, wParam, lParam))
1240 case WM_LBUTTONDBLCLK:
1241 if (CC_MouseCheckResultWindow(hDlg, lParam))
1245 if (CC_WMMouseMove(hDlg, lParam))
1248 case WM_LBUTTONUP: /* FIXME: ClipCursor off (if in color graph)*/
1249 if (CC_WMLButtonUp(hDlg, wParam, lParam))
1252 case WM_LBUTTONDOWN:/* FIXME: ClipCursor on (if in color graph)*/
1253 if (CC_WMLButtonDown(hDlg, wParam, lParam))
1260 /***********************************************************************
1261 * ChooseColorW (COMDLG32.@)
1263 * Create a color dialog box.
1266 * lpChCol [I/O] in: information to initialize the dialog box.
1267 * out: User's color selection
1270 * TRUE: Ok button clicked.
1271 * FALSE: Cancel button clicked, or error.
1273 BOOL WINAPI ChooseColorW( LPCHOOSECOLORW lpChCol )
1275 HANDLE hDlgTmpl = 0;
1279 TRACE("ChooseColor\n");
1280 if (!lpChCol) return FALSE;
1282 if (lpChCol->Flags & CC_ENABLETEMPLATEHANDLE)
1284 if (!(template = LockResource(lpChCol->hInstance)))
1286 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1290 else if (lpChCol->Flags & CC_ENABLETEMPLATE)
1293 if (!(hResInfo = FindResourceW((HINSTANCE)lpChCol->hInstance,
1294 lpChCol->lpTemplateName,
1295 (LPWSTR)RT_DIALOG)))
1297 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1300 if (!(hDlgTmpl = LoadResource((HINSTANCE)lpChCol->hInstance, hResInfo)) ||
1301 !(template = LockResource(hDlgTmpl)))
1303 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1311 static const WCHAR wszCHOOSE_COLOR[] = {'C','H','O','O','S','E','_','C','O','L','O','R',0};
1312 if (!(hResInfo = FindResourceW(COMDLG32_hInstance, wszCHOOSE_COLOR, (LPWSTR)RT_DIALOG)))
1314 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
1317 if (!(hDlgTmpl = LoadResource(COMDLG32_hInstance, hResInfo )) ||
1318 !(template = LockResource(hDlgTmpl)))
1320 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
1325 bRet = DialogBoxIndirectParamW(COMDLG32_hInstance, template, lpChCol->hwndOwner,
1326 ColorDlgProc, (LPARAM)lpChCol);
1330 /***********************************************************************
1331 * ChooseColorA (COMDLG32.@)
1335 BOOL WINAPI ChooseColorA( LPCHOOSECOLORA lpChCol )
1338 LPWSTR template_name = NULL;
1341 LPCHOOSECOLORW lpcc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CHOOSECOLORW));
1342 lpcc->lStructSize = sizeof(*lpcc);
1343 lpcc->hwndOwner = lpChCol->hwndOwner;
1344 lpcc->hInstance = lpChCol->hInstance;
1345 lpcc->rgbResult = lpChCol->rgbResult;
1346 lpcc->lpCustColors = lpChCol->lpCustColors;
1347 lpcc->Flags = lpChCol->Flags;
1348 lpcc->lCustData = lpChCol->lCustData;
1349 lpcc->lpfnHook = (LPCCHOOKPROC) lpChCol->lpfnHook;
1350 if ((lpcc->Flags & CC_ENABLETEMPLATE) && (lpChCol->lpTemplateName)) {
1351 if (HIWORD(lpChCol->lpTemplateName)) {
1352 INT len = MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, NULL, 0);
1353 template_name = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1354 MultiByteToWideChar( CP_ACP, 0, lpChCol->lpTemplateName, -1, template_name, len );
1355 lpcc->lpTemplateName = template_name;
1357 lpcc->lpTemplateName = (LPCWSTR)lpChCol->lpTemplateName;
1361 ret = ChooseColorW(lpcc);
1364 lpChCol->rgbResult = lpcc->rgbResult;
1365 HeapFree(GetProcessHeap(), 0, template_name);
1366 HeapFree(GetProcessHeap(), 0, lpcc);