2 * COMMDLG - Color Dialog
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1996 Albrecht Kleine
6 * Copyright 2019 Vijay Kiran Kamuju
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 /* BUGS : still seems to not refresh correctly
24 sometimes, especially when 2 instances of the
25 dialog are loaded at the same time */
39 #include "wine/debug.h"
40 #include "wine/heap.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(commdlg
);
44 static INT_PTR CALLBACK
ColorDlgProc( HWND hWnd
, UINT wMsg
, WPARAM wParam
, LPARAM lParam
);
46 #define CONV_LPARAMTOPOINT(lp,p) do { (p)->x = (short)LOWORD(lp); (p)->y = (short)HIWORD(lp); } while(0)
48 static const COLORREF predefcolors
[6][8]=
50 { 0x008080FFL
, 0x0080FFFFL
, 0x0080FF80L
, 0x0080FF00L
,
51 0x00FFFF80L
, 0x00FF8000L
, 0x00C080FFL
, 0x00FF80FFL
},
52 { 0x000000FFL
, 0x0000FFFFL
, 0x0000FF80L
, 0x0040FF00L
,
53 0x00FFFF00L
, 0x00C08000L
, 0x00C08080L
, 0x00FF00FFL
},
55 { 0x00404080L
, 0x004080FFL
, 0x0000FF00L
, 0x00808000L
,
56 0x00804000L
, 0x00FF8080L
, 0x00400080L
, 0x008000FFL
},
57 { 0x00000080L
, 0x000080FFL
, 0x00008000L
, 0x00408000L
,
58 0x00FF0000L
, 0x00A00000L
, 0x00800080L
, 0x00FF0080L
},
60 { 0x00000040L
, 0x00004080L
, 0x00004000L
, 0x00404000L
,
61 0x00800000L
, 0x00400000L
, 0x00400040L
, 0x00800040L
},
62 { 0x00000000L
, 0x00008080L
, 0x00408080L
, 0x00808080L
,
63 0x00808040L
, 0x00C0C0C0L
, 0x00400040L
, 0x00FFFFFFL
},
66 /* Chose Color PRIVATE Structure:
68 * This structure is duplicated in the 16 bit code with
72 typedef struct CCPRIVATE
74 LPCHOOSECOLORW lpcc
; /* points to public known data structure */
75 HWND hwndSelf
; /* dialog window */
76 int nextuserdef
; /* next free place in user defined color array */
77 HDC hdcMem
; /* color graph used for BitBlt() */
78 HBITMAP hbmMem
; /* color graph bitmap */
79 RECT fullsize
; /* original dialog window size */
80 UINT msetrgb
; /* # of SETRGBSTRING message (today not used) */
81 RECT old3angle
; /* last position of l-marker */
82 RECT oldcross
; /* last position of color/saturation marker */
83 BOOL updating
; /* to prevent recursive WM_COMMAND/EN_UPDATE processing */
86 int l
; /* for temporary storing of hue,sat,lum */
87 int capturedGraph
; /* control mouse captured */
88 RECT focusRect
; /* rectangle last focused item */
89 HWND hwndFocus
; /* handle last focused item */
92 static int hsl_to_x(int hue
, int sat
, int lum
)
97 maxrgb
= (256 * min(120,lum
)) / 120; /* 0 .. 256 */
103 res
= (hue
- 80) * maxrgb
; /* 0...10240 */
104 res
/= 40; /* 0...256 */
111 res
= (240 - hue
) * maxrgb
;
114 res
= res
- maxrgb
/ 2; /* -128...128 */
117 res
= maxrgb
/ 2 + (sat
* res
) / 240; /* 0..256 */
120 if (lum
> 120 && res
< 256)
121 res
+= ((lum
- 120) * (256 - res
)) / 120;
123 return min(res
, 255);
126 /***********************************************************************
127 * CC_HSLtoRGB [internal]
129 static COLORREF
CC_HSLtoRGB(int hue
, int sat
, int lum
)
134 h
= hue
> 80 ? hue
-80 : hue
+160;
135 r
= hsl_to_x(h
, sat
, lum
);
137 h
= hue
> 160 ? hue
-160 : hue
+80;
138 g
= hsl_to_x(h
, sat
, lum
);
140 b
= hsl_to_x(hue
, sat
, lum
);
145 /***********************************************************************
146 * CC_RGBtoHSL [internal]
148 static int CC_RGBtoHSL(char c
, COLORREF rgb
)
150 WORD maxi
, mini
, mmsum
, mmdif
, result
= 0;
151 int iresult
= 0, r
, g
, b
;
168 case 'L': mmsum
*= 120; /* 0...61200=(255+255)*120 */
169 result
= mmsum
/ 255; /* 0...240 */
172 case 'S': if (!mmsum
)
175 if (!mini
|| maxi
== 255)
179 result
= mmdif
* 240; /* 0...61200=255*240 */
180 result
/= (mmsum
> 255 ? 510 - mmsum
: mmsum
); /* 0..255 */
184 case 'H': if (!mmdif
)
190 iresult
= 40 * (g
- b
); /* -10200 ... 10200 */
191 iresult
/= (int) mmdif
; /* -40 .. 40 */
193 iresult
+= 240; /* 0..40 and 200..240 */
198 iresult
= 40 * (b
- r
);
199 iresult
/= (int) mmdif
;
200 iresult
+= 80; /* 40 .. 120 */
205 iresult
= 40 * (r
- g
);
206 iresult
/= (int) mmdif
;
207 iresult
+= 160; /* 120 .. 200 */
213 return result
; /* is this integer arithmetic precise enough ? */
217 /***********************************************************************
218 * CC_DrawCurrentFocusRect [internal]
220 static void CC_DrawCurrentFocusRect( const CCPRIV
*lpp
)
224 HDC hdc
= GetDC(lpp
->hwndFocus
);
225 DrawFocusRect(hdc
, &lpp
->focusRect
);
226 ReleaseDC(lpp
->hwndFocus
, hdc
);
230 /***********************************************************************
231 * CC_DrawFocusRect [internal]
233 static void CC_DrawFocusRect(CCPRIV
*lpp
, HWND hwnd
, int x
, int y
, int rows
, int cols
)
239 CC_DrawCurrentFocusRect(lpp
); /* remove current focus rect */
240 /* calculate new rect */
241 GetClientRect(hwnd
, &rect
);
242 dx
= (rect
.right
- rect
.left
) / cols
;
243 dy
= (rect
.bottom
- rect
.top
) / rows
;
244 rect
.left
+= (x
* dx
) - 2;
245 rect
.top
+= (y
* dy
) - 2;
246 rect
.right
= rect
.left
+ dx
;
247 rect
.bottom
= rect
.top
+ dy
;
250 DrawFocusRect(hdc
, &rect
);
251 lpp
->focusRect
= rect
;
252 lpp
->hwndFocus
= hwnd
;
253 ReleaseDC(hwnd
, hdc
);
258 /***********************************************************************
259 * CC_MouseCheckPredefColorArray [internal]
260 * returns TRUE if one of the predefined colors is clicked
262 static BOOL
CC_MouseCheckPredefColorArray(CCPRIV
*lpp
, int rows
, int cols
, LPARAM lParam
)
269 CONV_LPARAMTOPOINT(lParam
, &point
);
270 ClientToScreen(lpp
->hwndSelf
, &point
);
271 hwnd
= GetDlgItem(lpp
->hwndSelf
, COLOR_BOX1
);
272 GetWindowRect(hwnd
, &rect
);
273 if (PtInRect(&rect
, point
))
275 dx
= (rect
.right
- rect
.left
) / cols
;
276 dy
= (rect
.bottom
- rect
.top
) / rows
;
277 ScreenToClient(hwnd
, &point
);
279 if (point
.x
% dx
< ( dx
- DISTANCE
) && point
.y
% dy
< ( dy
- DISTANCE
))
283 lpp
->lpcc
->rgbResult
= predefcolors
[y
][x
];
284 CC_DrawFocusRect(lpp
, hwnd
, x
, y
, rows
, cols
);
291 /***********************************************************************
292 * CC_MouseCheckUserColorArray [internal]
293 * return TRUE if the user clicked a color
295 static BOOL
CC_MouseCheckUserColorArray(CCPRIV
*lpp
, int rows
, int cols
, LPARAM lParam
)
301 COLORREF
*crarr
= lpp
->lpcc
->lpCustColors
;
303 CONV_LPARAMTOPOINT(lParam
, &point
);
304 ClientToScreen(lpp
->hwndSelf
, &point
);
305 hwnd
= GetDlgItem(lpp
->hwndSelf
, COLOR_CUSTOM1
);
306 GetWindowRect(hwnd
, &rect
);
307 if (PtInRect(&rect
, point
))
309 dx
= (rect
.right
- rect
.left
) / cols
;
310 dy
= (rect
.bottom
- rect
.top
) / rows
;
311 ScreenToClient(hwnd
, &point
);
313 if (point
.x
% dx
< (dx
- DISTANCE
) && point
.y
% dy
< (dy
- DISTANCE
))
317 lpp
->lpcc
->rgbResult
= crarr
[x
+ (cols
* y
) ];
318 CC_DrawFocusRect(lpp
, hwnd
, x
, y
, rows
, cols
);
328 /* 240 ^...... ^^ 240
335 /***********************************************************************
336 * CC_MouseCheckColorGraph [internal]
338 static BOOL
CC_MouseCheckColorGraph( HWND hDlg
, int dlgitem
, int *hori
, int *vert
, LPARAM lParam
)
345 CONV_LPARAMTOPOINT(lParam
, &point
);
346 ClientToScreen(hDlg
, &point
);
347 hwnd
= GetDlgItem( hDlg
, dlgitem
);
348 GetWindowRect(hwnd
, &rect
);
350 if (!PtInRect(&rect
, point
))
353 GetClientRect(hwnd
, &rect
);
354 ScreenToClient(hwnd
, &point
);
356 x
= (point
.x
* MAXHORI
) / rect
.right
;
357 y
= ((rect
.bottom
- point
.y
) * MAXVERT
) / rect
.bottom
;
361 if (x
> MAXHORI
) x
= MAXHORI
;
362 if (y
> MAXVERT
) y
= MAXVERT
;
371 /***********************************************************************
372 * CC_MouseCheckResultWindow [internal]
373 * test if double click one of the result colors
375 static BOOL
CC_MouseCheckResultWindow( HWND hDlg
, LPARAM lParam
)
381 CONV_LPARAMTOPOINT(lParam
, &point
);
382 ClientToScreen(hDlg
, &point
);
383 hwnd
= GetDlgItem(hDlg
, COLOR_CURRENT
);
384 GetWindowRect(hwnd
, &rect
);
385 if (PtInRect(&rect
, point
))
387 PostMessageA(hDlg
, WM_COMMAND
, COLOR_SOLID
, 0);
393 /***********************************************************************
394 * CC_CheckDigitsInEdit [internal]
396 static int CC_CheckDigitsInEdit( HWND hwnd
, int maxval
)
398 int i
, k
, m
, result
, value
;
401 GetWindowTextA(hwnd
, buffer
, ARRAY_SIZE(buffer
));
405 for (i
= 0 ; i
< m
; i
++)
406 if (buffer
[i
] < '0' || buffer
[i
] > '9')
408 for (k
= i
+ 1; k
<= m
; k
++) /* delete bad character */
410 buffer
[i
] = buffer
[k
];
417 value
= atoi(buffer
);
418 if (value
> maxval
) /* build a new string */
420 sprintf(buffer
, "%d", maxval
);
425 LRESULT editpos
= SendMessageA(hwnd
, EM_GETSEL
, 0, 0);
426 SetWindowTextA(hwnd
, buffer
);
427 SendMessageA(hwnd
, EM_SETSEL
, 0, editpos
);
434 /***********************************************************************
435 * CC_PaintSelectedColor [internal]
437 static void CC_PaintSelectedColor(const CCPRIV
*infoPtr
)
439 if (IsWindowVisible( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
) )) /* if full size */
444 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_CURRENT
);
447 GetClientRect(hwnd
, &rect
) ;
448 hBrush
= CreateSolidBrush(infoPtr
->lpcc
->rgbResult
);
451 FillRect(hdc
, &rect
, hBrush
);
452 DrawEdge(hdc
, &rect
, BDR_SUNKENOUTER
, BF_RECT
);
453 DeleteObject(hBrush
);
455 ReleaseDC(hwnd
, hdc
);
459 /***********************************************************************
460 * CC_PaintTriangle [internal]
462 static void CC_PaintTriangle(CCPRIV
*infoPtr
)
466 int w
= LOWORD(GetDialogBaseUnits()) / 2;
472 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_LUMSCROLL
);
474 if (IsWindowVisible( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
))) /* if full size */
476 GetClientRect(hwnd
, &rect
);
477 height
= rect
.bottom
;
478 hDC
= GetDC(infoPtr
->hwndSelf
);
479 points
[0].y
= rect
.top
;
480 points
[0].x
= rect
.right
; /* | /| */
481 ClientToScreen(hwnd
, points
); /* | / | */
482 ScreenToClient(infoPtr
->hwndSelf
, points
); /* |< | */
483 oben
= points
[0].y
; /* | \ | */
485 temp
= height
* infoPtr
->l
;
487 points
[0].y
= oben
+ height
- temp
/ MAXVERT
;
488 points
[1].y
= points
[0].y
+ w
;
489 points
[2].y
= points
[0].y
- w
;
490 points
[2].x
= points
[1].x
= points
[0].x
+ w
;
492 hbr
= (HBRUSH
)GetClassLongPtrW( hwnd
, GCLP_HBRBACKGROUND
);
493 if (!hbr
) hbr
= GetSysColorBrush(COLOR_BTNFACE
);
494 FillRect(hDC
, &infoPtr
->old3angle
, hbr
);
495 infoPtr
->old3angle
.left
= points
[0].x
;
496 infoPtr
->old3angle
.right
= points
[1].x
+ 1;
497 infoPtr
->old3angle
.top
= points
[2].y
- 1;
498 infoPtr
->old3angle
.bottom
= points
[1].y
+ 1;
500 hbr
= SelectObject(hDC
, GetStockObject(BLACK_BRUSH
));
501 Polygon(hDC
, points
, 3);
502 SelectObject(hDC
, hbr
);
504 ReleaseDC(infoPtr
->hwndSelf
, hDC
);
509 /***********************************************************************
510 * CC_PaintCross [internal]
512 static void CC_PaintCross(CCPRIV
*infoPtr
)
514 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
);
516 if (IsWindowVisible(hwnd
)) /* if full size */
519 int w
= GetDialogBaseUnits() - 1;
520 int wc
= GetDialogBaseUnits() * 3 / 4;
530 GetClientRect(hwnd
, &rect
);
532 region
= CreateRectRgnIndirect(&rect
);
533 SelectClipRgn(hDC
, region
);
534 DeleteObject(region
);
536 point
.x
= (rect
.right
* x
) / MAXHORI
;
537 point
.y
= rect
.bottom
- (rect
.bottom
* y
) / MAXVERT
;
538 if ( infoPtr
->oldcross
.left
!= infoPtr
->oldcross
.right
)
539 BitBlt(hDC
, infoPtr
->oldcross
.left
, infoPtr
->oldcross
.top
,
540 infoPtr
->oldcross
.right
- infoPtr
->oldcross
.left
,
541 infoPtr
->oldcross
.bottom
- infoPtr
->oldcross
.top
,
542 infoPtr
->hdcMem
, infoPtr
->oldcross
.left
, infoPtr
->oldcross
.top
, SRCCOPY
);
543 infoPtr
->oldcross
.left
= point
.x
- w
- 1;
544 infoPtr
->oldcross
.right
= point
.x
+ w
+ 1;
545 infoPtr
->oldcross
.top
= point
.y
- w
- 1;
546 infoPtr
->oldcross
.bottom
= point
.y
+ w
+ 1;
548 hPen
= CreatePen(PS_SOLID
, 3, RGB(0, 0, 0)); /* -black- color */
549 hPen
= SelectObject(hDC
, hPen
);
550 MoveToEx(hDC
, point
.x
- w
, point
.y
, &p
);
551 LineTo(hDC
, point
.x
- wc
, point
.y
);
552 MoveToEx(hDC
, point
.x
+ wc
, point
.y
, &p
);
553 LineTo(hDC
, point
.x
+ w
, point
.y
);
554 MoveToEx(hDC
, point
.x
, point
.y
- w
, &p
);
555 LineTo(hDC
, point
.x
, point
.y
- wc
);
556 MoveToEx(hDC
, point
.x
, point
.y
+ wc
, &p
);
557 LineTo(hDC
, point
.x
, point
.y
+ w
);
558 DeleteObject( SelectObject(hDC
, hPen
));
560 ReleaseDC(hwnd
, hDC
);
569 /***********************************************************************
570 * CC_PrepareColorGraph [internal]
572 static void CC_PrepareColorGraph(CCPRIV
*infoPtr
)
574 int sdif
, hdif
, xdif
, ydif
, hue
, sat
;
575 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
);
579 HCURSOR hcursor
= SetCursor( LoadCursorW(0, (LPCWSTR
)IDC_WAIT
) );
581 GetClientRect(hwnd
, &client
);
583 infoPtr
->hdcMem
= CreateCompatibleDC(hdc
);
584 infoPtr
->hbmMem
= CreateCompatibleBitmap(hdc
, client
.right
, client
.bottom
);
585 SelectObject(infoPtr
->hdcMem
, infoPtr
->hbmMem
);
587 xdif
= client
.right
/ XSTEPS
;
588 ydif
= client
.bottom
/ YSTEPS
+1;
591 for (rect
.left
= hue
= 0; hue
< 239 + hdif
; hue
+= hdif
)
593 rect
.right
= rect
.left
+ xdif
;
594 rect
.bottom
= client
.bottom
;
595 for(sat
= 0; sat
< 240 + sdif
; sat
+= sdif
)
597 rect
.top
= rect
.bottom
- ydif
;
598 hbrush
= CreateSolidBrush(CC_HSLtoRGB(hue
, sat
, 120));
599 FillRect(infoPtr
->hdcMem
, &rect
, hbrush
);
600 DeleteObject(hbrush
);
601 rect
.bottom
= rect
.top
;
603 rect
.left
= rect
.right
;
605 ReleaseDC(hwnd
, hdc
);
609 /***********************************************************************
610 * CC_PaintColorGraph [internal]
612 static void CC_PaintColorGraph(CCPRIV
*infoPtr
)
614 HWND hwnd
= GetDlgItem( infoPtr
->hwndSelf
, COLOR_RAINBOW
);
618 if (IsWindowVisible(hwnd
)) /* if full size */
620 if (!infoPtr
->hdcMem
)
621 CC_PrepareColorGraph(infoPtr
); /* should not be necessary */
624 GetClientRect(hwnd
, &rect
);
626 BitBlt(hDC
, 0, 0, rect
.right
, rect
.bottom
, infoPtr
->hdcMem
, 0, 0, SRCCOPY
);
628 WARN("choose color: hdcMem is not defined\n");
629 ReleaseDC(hwnd
, hDC
);
633 /***********************************************************************
634 * CC_PaintLumBar [internal]
636 static void CC_PaintLumBar(const CCPRIV
*infoPtr
)
638 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_LUMSCROLL
);
644 if (IsWindowVisible(hwnd
))
647 GetClientRect(hwnd
, &client
);
651 ydif
= client
.bottom
/ YSTEPS
+1;
652 for (lum
= 0; lum
< 240 + ldif
; lum
+= ldif
)
654 rect
.top
= max(0, rect
.bottom
- ydif
);
655 hbrush
= CreateSolidBrush(CC_HSLtoRGB(infoPtr
->h
, infoPtr
->s
, lum
));
656 FillRect(hDC
, &rect
, hbrush
);
657 DeleteObject(hbrush
);
658 rect
.bottom
= rect
.top
;
660 GetClientRect(hwnd
, &rect
);
661 DrawEdge(hDC
, &rect
, BDR_SUNKENOUTER
, BF_RECT
);
662 ReleaseDC(hwnd
, hDC
);
666 /***********************************************************************
667 * CC_EditSetRGB [internal]
669 static void CC_EditSetRGB( CCPRIV
*infoPtr
)
671 if (IsWindowVisible( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
) )) /* if full size */
673 COLORREF cr
= infoPtr
->lpcc
->rgbResult
;
674 int r
= GetRValue(cr
);
675 int g
= GetGValue(cr
);
676 int b
= GetBValue(cr
);
678 infoPtr
->updating
= TRUE
;
679 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_RED
, r
, TRUE
);
680 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_GREEN
, g
, TRUE
);
681 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_BLUE
, b
, TRUE
);
682 infoPtr
->updating
= FALSE
;
686 /***********************************************************************
687 * CC_EditSetHSL [internal]
689 static void CC_EditSetHSL( CCPRIV
*infoPtr
)
691 if (IsWindowVisible( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
) )) /* if full size */
693 infoPtr
->updating
= TRUE
;
694 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_HUE
, infoPtr
->h
, TRUE
);
695 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_SAT
, infoPtr
->s
, TRUE
);
696 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_LUM
, infoPtr
->l
, TRUE
);
697 infoPtr
->updating
= FALSE
;
699 CC_PaintLumBar(infoPtr
);
702 /***********************************************************************
703 * CC_SwitchToFullSize [internal]
705 static void CC_SwitchToFullSize( CCPRIV
*infoPtr
, const RECT
*lprect
)
709 EnableWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_MIX
), FALSE
);
710 CC_PrepareColorGraph(infoPtr
);
711 for (i
= COLOR_HUE
; i
<= COLOR_BLUE
; i
++)
712 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, i
), SW_SHOW
);
713 for (i
= COLOR_HUEACCEL
; i
<= COLOR_BLUEACCEL
; i
++)
714 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, i
), SW_SHOW
);
715 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_SOLID
), SW_SHOW
);
716 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_ADD
), SW_SHOW
);
717 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, 1090), SW_SHOW
);
720 SetWindowPos(infoPtr
->hwndSelf
, 0, 0, 0, lprect
->right
-lprect
->left
,
721 lprect
->bottom
-lprect
->top
, SWP_NOMOVE
|SWP_NOZORDER
);
723 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_LUMSCROLL
), SW_SHOW
);
724 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_CURRENT
), SW_SHOW
);
726 CC_EditSetRGB(infoPtr
);
727 CC_EditSetHSL(infoPtr
);
728 ShowWindow( GetDlgItem( infoPtr
->hwndSelf
, COLOR_RAINBOW
), SW_SHOW
);
729 UpdateWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
) );
732 /***********************************************************************
733 * CC_PaintPredefColorArray [internal]
734 * Paints the default standard 48 colors
736 static void CC_PaintPredefColorArray(const CCPRIV
*infoPtr
, int rows
, int cols
)
738 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_BOX1
);
739 RECT rect
, blockrect
;
744 GetClientRect(hwnd
, &rect
);
745 dx
= rect
.right
/ cols
;
746 dy
= rect
.bottom
/ rows
;
750 GetClientRect(hwnd
, &rect
);
751 hBrush
= (HBRUSH
)GetClassLongPtrW( hwnd
, GCLP_HBRBACKGROUND
);
752 if (!hBrush
) hBrush
= GetSysColorBrush(COLOR_BTNFACE
);
753 FillRect(hdc
, &rect
, hBrush
);
754 for ( j
= 0; j
< rows
; j
++ )
756 for ( i
= 0; i
< cols
; i
++ )
758 hBrush
= CreateSolidBrush(predefcolors
[j
][i
]);
761 blockrect
.left
= rect
.left
;
762 blockrect
.top
= rect
.top
;
763 blockrect
.right
= rect
.left
+ dx
- DISTANCE
;
764 blockrect
.bottom
= rect
.top
+ dy
- DISTANCE
;
765 FillRect(hdc
, &blockrect
, hBrush
);
766 DrawEdge(hdc
, &blockrect
, BDR_SUNKEN
, BF_RECT
);
767 DeleteObject(hBrush
);
774 ReleaseDC(hwnd
, hdc
);
775 if (infoPtr
->hwndFocus
== hwnd
)
776 CC_DrawCurrentFocusRect(infoPtr
);
778 /***********************************************************************
779 * CC_PaintUserColorArray [internal]
780 * Paint the 16 user-selected colors
782 static void CC_PaintUserColorArray(const CCPRIV
*infoPtr
, int rows
, int cols
)
784 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_CUSTOM1
);
785 RECT rect
, blockrect
;
790 GetClientRect(hwnd
, &rect
);
792 dx
= rect
.right
/ cols
;
793 dy
= rect
.bottom
/ rows
;
799 hBrush
= (HBRUSH
)GetClassLongPtrW( hwnd
, GCLP_HBRBACKGROUND
);
800 if (!hBrush
) hBrush
= GetSysColorBrush(COLOR_BTNFACE
);
801 FillRect( hdc
, &rect
, hBrush
);
802 for (j
= 0; j
< rows
; j
++)
804 for (i
= 0; i
< cols
; i
++)
806 hBrush
= CreateSolidBrush(infoPtr
->lpcc
->lpCustColors
[i
+j
*cols
]);
809 blockrect
.left
= rect
.left
;
810 blockrect
.top
= rect
.top
;
811 blockrect
.right
= rect
.left
+ dx
- DISTANCE
;
812 blockrect
.bottom
= rect
.top
+ dy
- DISTANCE
;
813 FillRect(hdc
, &blockrect
, hBrush
);
814 DrawEdge(hdc
, &blockrect
, BDR_SUNKEN
, BF_RECT
);
815 DeleteObject(hBrush
);
822 ReleaseDC(hwnd
, hdc
);
824 if (infoPtr
->hwndFocus
== hwnd
)
825 CC_DrawCurrentFocusRect(infoPtr
);
829 /***********************************************************************
830 * CC_HookCallChk [internal]
832 static BOOL
CC_HookCallChk( const CHOOSECOLORW
*lpcc
)
835 if(lpcc
->Flags
& CC_ENABLEHOOK
)
841 /***********************************************************************
842 * CC_WMInitDialog [internal]
844 static LRESULT
CC_WMInitDialog( HWND hDlg
, WPARAM wParam
, LPARAM lParam
)
846 CHOOSECOLORW
*cc
= (CHOOSECOLORW
*)lParam
;
854 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam
);
856 if (cc
->lStructSize
!= sizeof(CHOOSECOLORW
))
862 lpp
= heap_alloc_zero(sizeof(*lpp
));
864 lpp
->hwndSelf
= hDlg
;
866 SetPropW( hDlg
, L
"colourdialogprop", lpp
);
868 if (!(lpp
->lpcc
->Flags
& CC_SHOWHELP
))
869 ShowWindow(GetDlgItem(hDlg
, pshHelp
), SW_HIDE
);
870 lpp
->msetrgb
= RegisterWindowMessageA(SETRGBSTRINGA
);
873 cpos
= MAKELONG(5,7); /* init */
874 if (lpp
->lpcc
->Flags
& CC_RGBINIT
)
876 for (i
= 0; i
< 6; i
++)
877 for (j
= 0; j
< 8; j
++)
878 if (predefcolors
[i
][j
] == lpp
->lpcc
->rgbResult
)
880 cpos
= MAKELONG(i
,j
);
885 /* FIXME: Draw_a_focus_rect & set_init_values */
888 GetWindowRect(hDlg
, &lpp
->fullsize
);
889 if (lpp
->lpcc
->Flags
& CC_FULLOPEN
|| lpp
->lpcc
->Flags
& CC_PREVENTFULLOPEN
)
891 hwnd
= GetDlgItem(hDlg
, COLOR_MIX
);
892 EnableWindow(hwnd
, FALSE
);
894 if (!(lpp
->lpcc
->Flags
& CC_FULLOPEN
) || lpp
->lpcc
->Flags
& CC_PREVENTFULLOPEN
)
896 rect
= lpp
->fullsize
;
897 res
= rect
.bottom
- rect
.top
;
898 hwnd
= GetDlgItem(hDlg
, COLOR_RAINBOW
); /* cut at left border */
899 point
.x
= point
.y
= 0;
900 ClientToScreen(hwnd
, &point
);
901 ScreenToClient(hDlg
,&point
);
902 GetClientRect(hDlg
, &rect
);
903 point
.x
+= GetSystemMetrics(SM_CXDLGFRAME
);
904 SetWindowPos(hDlg
, 0, 0, 0, point
.x
, res
, SWP_NOMOVE
|SWP_NOZORDER
);
906 for (i
= COLOR_HUE
; i
<= COLOR_BLUE
; i
++)
907 ShowWindow( GetDlgItem(hDlg
, i
), SW_HIDE
);
908 for (i
= COLOR_HUEACCEL
; i
<= COLOR_BLUEACCEL
; i
++)
909 ShowWindow( GetDlgItem(hDlg
, i
), SW_HIDE
);
910 ShowWindow( GetDlgItem(hDlg
, COLOR_SOLID
), SW_HIDE
);
911 ShowWindow( GetDlgItem(hDlg
, COLOR_ADD
), SW_HIDE
);
912 ShowWindow( GetDlgItem(hDlg
, COLOR_RAINBOW
), SW_HIDE
);
913 ShowWindow( GetDlgItem(hDlg
, COLOR_CURRENT
), SW_HIDE
);
914 ShowWindow( GetDlgItem(hDlg
, 1090 ), SW_HIDE
);
917 CC_SwitchToFullSize(lpp
, NULL
);
919 for (i
= COLOR_HUE
; i
<= COLOR_BLUE
; i
++)
920 SendMessageA( GetDlgItem(hDlg
, i
), EM_LIMITTEXT
, 3, 0); /* max 3 digits: xyz */
921 if (CC_HookCallChk(lpp
->lpcc
))
923 res
= CallWindowProcA( (WNDPROC
)lpp
->lpcc
->lpfnHook
, hDlg
, WM_INITDIALOG
, wParam
, lParam
);
926 /* Set the initial values of the color chooser dialog */
927 r
= GetRValue(lpp
->lpcc
->rgbResult
);
928 g
= GetGValue(lpp
->lpcc
->rgbResult
);
929 b
= GetBValue(lpp
->lpcc
->rgbResult
);
931 CC_PaintSelectedColor(lpp
);
932 lpp
->h
= CC_RGBtoHSL('H', lpp
->lpcc
->rgbResult
);
933 lpp
->s
= CC_RGBtoHSL('S', lpp
->lpcc
->rgbResult
);
934 lpp
->l
= CC_RGBtoHSL('L', lpp
->lpcc
->rgbResult
);
936 /* Doing it the long way because CC_EditSetRGB/HSL doesn't seem to work */
937 SetDlgItemInt(hDlg
, COLOR_HUE
, lpp
->h
, TRUE
);
938 SetDlgItemInt(hDlg
, COLOR_SAT
, lpp
->s
, TRUE
);
939 SetDlgItemInt(hDlg
, COLOR_LUM
, lpp
->l
, TRUE
);
940 SetDlgItemInt(hDlg
, COLOR_RED
, r
, TRUE
);
941 SetDlgItemInt(hDlg
, COLOR_GREEN
, g
, TRUE
);
942 SetDlgItemInt(hDlg
, COLOR_BLUE
, b
, TRUE
);
945 CC_PaintTriangle(lpp
);
951 /***********************************************************************
952 * CC_WMCommand [internal]
954 static LRESULT
CC_WMCommand(CCPRIV
*lpp
, WPARAM wParam
, LPARAM lParam
, WORD notifyCode
, HWND hwndCtl
)
961 TRACE("CC_WMCommand wParam=%lx lParam=%lx\n", wParam
, lParam
);
962 switch (LOWORD(wParam
))
964 case COLOR_RED
: /* edit notify RGB */
967 if (notifyCode
== EN_UPDATE
&& !lpp
->updating
)
969 i
= CC_CheckDigitsInEdit(hwndCtl
, 255);
970 r
= GetRValue(lpp
->lpcc
->rgbResult
);
971 g
= GetGValue(lpp
->lpcc
->rgbResult
);
972 b
= GetBValue(lpp
->lpcc
->rgbResult
);
974 switch (LOWORD(wParam
))
976 case COLOR_RED
: if ((xx
= (i
!= r
))) r
= i
; break;
977 case COLOR_GREEN
: if ((xx
= (i
!= g
))) g
= i
; break;
978 case COLOR_BLUE
: if ((xx
= (i
!= b
))) b
= i
; break;
980 if (xx
) /* something has changed */
982 lpp
->lpcc
->rgbResult
= RGB(r
, g
, b
);
983 CC_PaintSelectedColor(lpp
);
984 lpp
->h
= CC_RGBtoHSL('H', lpp
->lpcc
->rgbResult
);
985 lpp
->s
= CC_RGBtoHSL('S', lpp
->lpcc
->rgbResult
);
986 lpp
->l
= CC_RGBtoHSL('L', lpp
->lpcc
->rgbResult
);
989 CC_PaintTriangle(lpp
);
994 case COLOR_HUE
: /* edit notify HSL */
997 if (notifyCode
== EN_UPDATE
&& !lpp
->updating
)
999 i
= CC_CheckDigitsInEdit(hwndCtl
, LOWORD(wParam
) == COLOR_HUE
? 239 : 240);
1001 switch (LOWORD(wParam
))
1003 case COLOR_HUE
: if ((xx
= ( i
!= lpp
->h
))) lpp
->h
= i
; break;
1004 case COLOR_SAT
: if ((xx
= ( i
!= lpp
->s
))) lpp
->s
= i
; break;
1005 case COLOR_LUM
: if ((xx
= ( i
!= lpp
->l
))) lpp
->l
= i
; break;
1007 if (xx
) /* something has changed */
1009 lpp
->lpcc
->rgbResult
= CC_HSLtoRGB(lpp
->h
, lpp
->s
, lpp
->l
);
1010 CC_PaintSelectedColor(lpp
);
1013 CC_PaintTriangle(lpp
);
1019 CC_SwitchToFullSize(lpp
, &lpp
->fullsize
);
1020 SetFocus( GetDlgItem(lpp
->hwndSelf
, COLOR_HUE
));
1023 case COLOR_ADD
: /* add colors ... column by column */
1024 cr
= lpp
->lpcc
->lpCustColors
;
1025 cr
[(lpp
->nextuserdef
% 2) * 8 + lpp
->nextuserdef
/ 2] = lpp
->lpcc
->rgbResult
;
1026 if (++lpp
->nextuserdef
== 16)
1027 lpp
->nextuserdef
= 0;
1028 CC_PaintUserColorArray(lpp
, 2, 8);
1031 case COLOR_SOLID
: /* resulting color */
1032 hdc
= GetDC(lpp
->hwndSelf
);
1033 lpp
->lpcc
->rgbResult
= GetNearestColor(hdc
, lpp
->lpcc
->rgbResult
);
1034 ReleaseDC(lpp
->hwndSelf
, hdc
);
1036 CC_PaintSelectedColor(lpp
);
1037 lpp
->h
= CC_RGBtoHSL('H', lpp
->lpcc
->rgbResult
);
1038 lpp
->s
= CC_RGBtoHSL('S', lpp
->lpcc
->rgbResult
);
1039 lpp
->l
= CC_RGBtoHSL('L', lpp
->lpcc
->rgbResult
);
1042 CC_PaintTriangle(lpp
);
1045 case pshHelp
: /* Help! */ /* The Beatles, 1965 ;-) */
1046 i
= RegisterWindowMessageA(HELPMSGSTRINGA
);
1047 if (lpp
->lpcc
->hwndOwner
)
1048 SendMessageA(lpp
->lpcc
->hwndOwner
, i
, 0, (LPARAM
)lpp
->lpcc
);
1049 if ( CC_HookCallChk(lpp
->lpcc
))
1050 CallWindowProcA( (WNDPROC
) lpp
->lpcc
->lpfnHook
, lpp
->hwndSelf
,
1051 WM_COMMAND
, psh15
, (LPARAM
)lpp
->lpcc
);
1055 cokmsg
= RegisterWindowMessageA(COLOROKSTRINGA
);
1056 if (lpp
->lpcc
->hwndOwner
)
1057 if (SendMessageA(lpp
->lpcc
->hwndOwner
, cokmsg
, 0, (LPARAM
)lpp
->lpcc
))
1058 break; /* do NOT close */
1059 EndDialog(lpp
->hwndSelf
, 1) ;
1063 EndDialog(lpp
->hwndSelf
, 0) ;
1070 /***********************************************************************
1071 * CC_WMPaint [internal]
1073 static LRESULT
CC_WMPaint( CCPRIV
*lpp
)
1077 BeginPaint(lpp
->hwndSelf
, &ps
);
1078 /* we have to paint dialog children except text and buttons */
1079 CC_PaintPredefColorArray(lpp
, 6, 8);
1080 CC_PaintUserColorArray(lpp
, 2, 8);
1081 CC_PaintLumBar(lpp
);
1082 CC_PaintTriangle(lpp
);
1083 CC_PaintSelectedColor(lpp
);
1084 CC_PaintColorGraph(lpp
);
1086 EndPaint(lpp
->hwndSelf
, &ps
);
1091 /***********************************************************************
1092 * CC_WMLButtonUp [internal]
1094 static LRESULT
CC_WMLButtonUp( CCPRIV
*infoPtr
)
1096 if (infoPtr
->capturedGraph
)
1098 infoPtr
->capturedGraph
= 0;
1100 CC_PaintCross(infoPtr
);
1106 /***********************************************************************
1107 * CC_WMMouseMove [internal]
1109 static LRESULT
CC_WMMouseMove( CCPRIV
*infoPtr
, LPARAM lParam
)
1111 if (infoPtr
->capturedGraph
)
1113 int *ptrh
= NULL
, *ptrs
= &infoPtr
->l
;
1114 if (infoPtr
->capturedGraph
== COLOR_RAINBOW
)
1119 if (CC_MouseCheckColorGraph( infoPtr
->hwndSelf
, infoPtr
->capturedGraph
, ptrh
, ptrs
, lParam
))
1121 infoPtr
->lpcc
->rgbResult
= CC_HSLtoRGB(infoPtr
->h
, infoPtr
->s
, infoPtr
->l
);
1122 CC_EditSetRGB(infoPtr
);
1123 CC_EditSetHSL(infoPtr
);
1124 CC_PaintCross(infoPtr
);
1125 CC_PaintTriangle(infoPtr
);
1126 CC_PaintSelectedColor(infoPtr
);
1131 infoPtr
->capturedGraph
= 0;
1138 /***********************************************************************
1139 * CC_WMLButtonDown [internal]
1141 static LRESULT
CC_WMLButtonDown( CCPRIV
*infoPtr
, LPARAM lParam
)
1145 if (CC_MouseCheckPredefColorArray(infoPtr
, 6, 8, lParam
))
1148 if (CC_MouseCheckUserColorArray(infoPtr
, 2, 8, lParam
))
1151 if (CC_MouseCheckColorGraph(infoPtr
->hwndSelf
, COLOR_RAINBOW
, &infoPtr
->h
, &infoPtr
->s
, lParam
))
1154 infoPtr
->capturedGraph
= COLOR_RAINBOW
;
1157 if (CC_MouseCheckColorGraph(infoPtr
->hwndSelf
, COLOR_LUMSCROLL
, NULL
, &infoPtr
->l
, lParam
))
1160 infoPtr
->capturedGraph
= COLOR_LUMSCROLL
;
1164 SetCapture(infoPtr
->hwndSelf
);
1165 infoPtr
->lpcc
->rgbResult
= CC_HSLtoRGB(infoPtr
->h
, infoPtr
->s
, infoPtr
->l
);
1169 infoPtr
->h
= CC_RGBtoHSL('H', infoPtr
->lpcc
->rgbResult
);
1170 infoPtr
->s
= CC_RGBtoHSL('S', infoPtr
->lpcc
->rgbResult
);
1171 infoPtr
->l
= CC_RGBtoHSL('L', infoPtr
->lpcc
->rgbResult
);
1175 CC_EditSetRGB(infoPtr
);
1176 CC_EditSetHSL(infoPtr
);
1177 CC_PaintCross(infoPtr
);
1178 CC_PaintTriangle(infoPtr
);
1179 CC_PaintSelectedColor(infoPtr
);
1185 /***********************************************************************
1186 * ColorDlgProc32 [internal]
1189 static INT_PTR CALLBACK
ColorDlgProc( HWND hDlg
, UINT message
,
1190 WPARAM wParam
, LPARAM lParam
)
1194 CCPRIV
*lpp
= GetPropW( hDlg
, L
"colourdialogprop" );
1196 if (message
!= WM_INITDIALOG
)
1201 if (CC_HookCallChk(lpp
->lpcc
))
1202 res
= CallWindowProcA( (WNDPROC
)lpp
->lpcc
->lpfnHook
, hDlg
, message
, wParam
, lParam
);
1207 /* FIXME: SetRGB message
1208 if (message && message == msetrgb)
1209 return HandleSetRGB(hDlg, lParam);
1215 return CC_WMInitDialog(hDlg
, wParam
, lParam
);
1217 DeleteDC(lpp
->hdcMem
);
1218 DeleteObject(lpp
->hbmMem
);
1220 RemovePropW( hDlg
, L
"colourdialogprop" );
1223 if (CC_WMCommand(lpp
, wParam
, lParam
, HIWORD(wParam
), (HWND
) lParam
))
1227 if (CC_WMPaint(lpp
))
1230 case WM_LBUTTONDBLCLK
:
1231 if (CC_MouseCheckResultWindow(hDlg
, lParam
))
1235 if (CC_WMMouseMove(lpp
, lParam
))
1238 case WM_LBUTTONUP
: /* FIXME: ClipCursor off (if in color graph)*/
1239 if (CC_WMLButtonUp(lpp
))
1242 case WM_LBUTTONDOWN
:/* FIXME: ClipCursor on (if in color graph)*/
1243 if (CC_WMLButtonDown(lpp
, lParam
))
1250 /***********************************************************************
1251 * ChooseColorW (COMDLG32.@)
1253 * Create a color dialog box.
1256 * lpChCol [I/O] in: information to initialize the dialog box.
1257 * out: User's color selection
1260 * TRUE: Ok button clicked.
1261 * FALSE: Cancel button clicked, or error.
1263 BOOL WINAPI
ChooseColorW( CHOOSECOLORW
*lpChCol
)
1265 HANDLE hDlgTmpl
= 0;
1266 const void *template;
1268 TRACE("(%p)\n", lpChCol
);
1270 if (!lpChCol
) return FALSE
;
1272 if (lpChCol
->Flags
& CC_ENABLETEMPLATEHANDLE
)
1274 if (!(template = LockResource(lpChCol
->hInstance
)))
1276 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
1280 else if (lpChCol
->Flags
& CC_ENABLETEMPLATE
)
1283 if (!(hResInfo
= FindResourceW((HINSTANCE
)lpChCol
->hInstance
,
1284 lpChCol
->lpTemplateName
,
1285 (LPWSTR
)RT_DIALOG
)))
1287 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE
);
1290 if (!(hDlgTmpl
= LoadResource((HINSTANCE
)lpChCol
->hInstance
, hResInfo
)) ||
1291 !(template = LockResource(hDlgTmpl
)))
1293 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
1301 if (!(hResInfo
= FindResourceW(COMDLG32_hInstance
, L
"CHOOSE_COLOR", (LPWSTR
)RT_DIALOG
)))
1303 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE
);
1306 if (!(hDlgTmpl
= LoadResource(COMDLG32_hInstance
, hResInfo
)) ||
1307 !(template = LockResource(hDlgTmpl
)))
1309 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
1314 return DialogBoxIndirectParamW(COMDLG32_hInstance
, template, lpChCol
->hwndOwner
,
1315 ColorDlgProc
, (LPARAM
)lpChCol
);
1318 /***********************************************************************
1319 * ChooseColorA (COMDLG32.@)
1323 BOOL WINAPI
ChooseColorA( LPCHOOSECOLORA lpChCol
)
1326 LPWSTR template_name
= NULL
;
1329 CHOOSECOLORW
*lpcc
= heap_alloc_zero(sizeof(*lpcc
));
1330 lpcc
->lStructSize
= sizeof(*lpcc
);
1331 lpcc
->hwndOwner
= lpChCol
->hwndOwner
;
1332 lpcc
->hInstance
= lpChCol
->hInstance
;
1333 lpcc
->rgbResult
= lpChCol
->rgbResult
;
1334 lpcc
->lpCustColors
= lpChCol
->lpCustColors
;
1335 lpcc
->Flags
= lpChCol
->Flags
;
1336 lpcc
->lCustData
= lpChCol
->lCustData
;
1337 lpcc
->lpfnHook
= lpChCol
->lpfnHook
;
1338 if ((lpcc
->Flags
& CC_ENABLETEMPLATE
) && (lpChCol
->lpTemplateName
)) {
1339 if (!IS_INTRESOURCE(lpChCol
->lpTemplateName
)) {
1340 INT len
= MultiByteToWideChar( CP_ACP
, 0, lpChCol
->lpTemplateName
, -1, NULL
, 0);
1341 template_name
= heap_alloc( len
* sizeof(WCHAR
) );
1342 MultiByteToWideChar( CP_ACP
, 0, lpChCol
->lpTemplateName
, -1, template_name
, len
);
1343 lpcc
->lpTemplateName
= template_name
;
1345 lpcc
->lpTemplateName
= (LPCWSTR
)lpChCol
->lpTemplateName
;
1349 ret
= ChooseColorW(lpcc
);
1352 lpChCol
->rgbResult
= lpcc
->rgbResult
;
1354 heap_free(template_name
);