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
= (long) point
.x
* MAXHORI
;
358 y
= (long) (rect
.bottom
- point
.y
) * MAXVERT
;
363 if (x
> MAXHORI
) x
= MAXHORI
;
364 if (y
> MAXVERT
) y
= MAXVERT
;
373 /***********************************************************************
374 * CC_MouseCheckResultWindow [internal]
375 * test if double click one of the result colors
377 static BOOL
CC_MouseCheckResultWindow( HWND hDlg
, LPARAM lParam
)
383 CONV_LPARAMTOPOINT(lParam
, &point
);
384 ClientToScreen(hDlg
, &point
);
385 hwnd
= GetDlgItem(hDlg
, COLOR_CURRENT
);
386 GetWindowRect(hwnd
, &rect
);
387 if (PtInRect(&rect
, point
))
389 PostMessageA(hDlg
, WM_COMMAND
, COLOR_SOLID
, 0);
395 /***********************************************************************
396 * CC_CheckDigitsInEdit [internal]
398 static int CC_CheckDigitsInEdit( HWND hwnd
, int maxval
)
400 int i
, k
, m
, result
, value
;
404 GetWindowTextA(hwnd
, buffer
, ARRAY_SIZE(buffer
));
408 for (i
= 0 ; i
< m
; i
++)
409 if (buffer
[i
] < '0' || buffer
[i
] > '9')
411 for (k
= i
+ 1; k
<= m
; k
++) /* delete bad character */
413 buffer
[i
] = buffer
[k
];
420 value
= atoi(buffer
);
421 if (value
> maxval
) /* build a new string */
423 sprintf(buffer
, "%d", maxval
);
428 editpos
= SendMessageA(hwnd
, EM_GETSEL
, 0, 0);
429 SetWindowTextA(hwnd
, buffer
);
430 SendMessageA(hwnd
, EM_SETSEL
, 0, editpos
);
437 /***********************************************************************
438 * CC_PaintSelectedColor [internal]
440 static void CC_PaintSelectedColor(const CCPRIV
*infoPtr
)
442 if (IsWindowVisible( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
) )) /* if full size */
447 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_CURRENT
);
450 GetClientRect(hwnd
, &rect
) ;
451 hBrush
= CreateSolidBrush(infoPtr
->lpcc
->rgbResult
);
454 FillRect(hdc
, &rect
, hBrush
);
455 DrawEdge(hdc
, &rect
, BDR_SUNKENOUTER
, BF_RECT
);
456 DeleteObject(hBrush
);
458 ReleaseDC(hwnd
, hdc
);
462 /***********************************************************************
463 * CC_PaintTriangle [internal]
465 static void CC_PaintTriangle(CCPRIV
*infoPtr
)
469 int w
= LOWORD(GetDialogBaseUnits()) / 2;
475 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_LUMSCROLL
);
477 if (IsWindowVisible( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
))) /* if full size */
479 GetClientRect(hwnd
, &rect
);
480 height
= rect
.bottom
;
481 hDC
= GetDC(infoPtr
->hwndSelf
);
482 points
[0].y
= rect
.top
;
483 points
[0].x
= rect
.right
; /* | /| */
484 ClientToScreen(hwnd
, points
); /* | / | */
485 ScreenToClient(infoPtr
->hwndSelf
, points
); /* |< | */
486 oben
= points
[0].y
; /* | \ | */
488 temp
= (long)height
* (long)infoPtr
->l
;
490 points
[0].y
= oben
+ height
- temp
/ (long)MAXVERT
;
491 points
[1].y
= points
[0].y
+ w
;
492 points
[2].y
= points
[0].y
- w
;
493 points
[2].x
= points
[1].x
= points
[0].x
+ w
;
495 hbr
= (HBRUSH
)GetClassLongPtrW( hwnd
, GCLP_HBRBACKGROUND
);
496 if (!hbr
) hbr
= GetSysColorBrush(COLOR_BTNFACE
);
497 FillRect(hDC
, &infoPtr
->old3angle
, hbr
);
498 infoPtr
->old3angle
.left
= points
[0].x
;
499 infoPtr
->old3angle
.right
= points
[1].x
+ 1;
500 infoPtr
->old3angle
.top
= points
[2].y
- 1;
501 infoPtr
->old3angle
.bottom
= points
[1].y
+ 1;
503 hbr
= SelectObject(hDC
, GetStockObject(BLACK_BRUSH
));
504 Polygon(hDC
, points
, 3);
505 SelectObject(hDC
, hbr
);
507 ReleaseDC(infoPtr
->hwndSelf
, hDC
);
512 /***********************************************************************
513 * CC_PaintCross [internal]
515 static void CC_PaintCross(CCPRIV
*infoPtr
)
517 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
);
519 if (IsWindowVisible(hwnd
)) /* if full size */
522 int w
= GetDialogBaseUnits() - 1;
523 int wc
= GetDialogBaseUnits() * 3 / 4;
533 GetClientRect(hwnd
, &rect
);
535 region
= CreateRectRgnIndirect(&rect
);
536 SelectClipRgn(hDC
, region
);
537 DeleteObject(region
);
539 point
.x
= ((long)rect
.right
* (long)x
) / (long)MAXHORI
;
540 point
.y
= rect
.bottom
- ((long)rect
.bottom
* (long)y
) / (long)MAXVERT
;
541 if ( infoPtr
->oldcross
.left
!= infoPtr
->oldcross
.right
)
542 BitBlt(hDC
, infoPtr
->oldcross
.left
, infoPtr
->oldcross
.top
,
543 infoPtr
->oldcross
.right
- infoPtr
->oldcross
.left
,
544 infoPtr
->oldcross
.bottom
- infoPtr
->oldcross
.top
,
545 infoPtr
->hdcMem
, infoPtr
->oldcross
.left
, infoPtr
->oldcross
.top
, SRCCOPY
);
546 infoPtr
->oldcross
.left
= point
.x
- w
- 1;
547 infoPtr
->oldcross
.right
= point
.x
+ w
+ 1;
548 infoPtr
->oldcross
.top
= point
.y
- w
- 1;
549 infoPtr
->oldcross
.bottom
= point
.y
+ w
+ 1;
551 hPen
= CreatePen(PS_SOLID
, 3, RGB(0, 0, 0)); /* -black- color */
552 hPen
= SelectObject(hDC
, hPen
);
553 MoveToEx(hDC
, point
.x
- w
, point
.y
, &p
);
554 LineTo(hDC
, point
.x
- wc
, point
.y
);
555 MoveToEx(hDC
, point
.x
+ wc
, point
.y
, &p
);
556 LineTo(hDC
, point
.x
+ w
, point
.y
);
557 MoveToEx(hDC
, point
.x
, point
.y
- w
, &p
);
558 LineTo(hDC
, point
.x
, point
.y
- wc
);
559 MoveToEx(hDC
, point
.x
, point
.y
+ wc
, &p
);
560 LineTo(hDC
, point
.x
, point
.y
+ w
);
561 DeleteObject( SelectObject(hDC
, hPen
));
563 ReleaseDC(hwnd
, hDC
);
572 /***********************************************************************
573 * CC_PrepareColorGraph [internal]
575 static void CC_PrepareColorGraph(CCPRIV
*infoPtr
)
577 int sdif
, hdif
, xdif
, ydif
, hue
, sat
;
578 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
);
582 HCURSOR hcursor
= SetCursor( LoadCursorW(0, (LPCWSTR
)IDC_WAIT
) );
584 GetClientRect(hwnd
, &client
);
586 infoPtr
->hdcMem
= CreateCompatibleDC(hdc
);
587 infoPtr
->hbmMem
= CreateCompatibleBitmap(hdc
, client
.right
, client
.bottom
);
588 SelectObject(infoPtr
->hdcMem
, infoPtr
->hbmMem
);
590 xdif
= client
.right
/ XSTEPS
;
591 ydif
= client
.bottom
/ YSTEPS
+1;
594 for (rect
.left
= hue
= 0; hue
< 239 + hdif
; hue
+= hdif
)
596 rect
.right
= rect
.left
+ xdif
;
597 rect
.bottom
= client
.bottom
;
598 for(sat
= 0; sat
< 240 + sdif
; sat
+= sdif
)
600 rect
.top
= rect
.bottom
- ydif
;
601 hbrush
= CreateSolidBrush(CC_HSLtoRGB(hue
, sat
, 120));
602 FillRect(infoPtr
->hdcMem
, &rect
, hbrush
);
603 DeleteObject(hbrush
);
604 rect
.bottom
= rect
.top
;
606 rect
.left
= rect
.right
;
608 ReleaseDC(hwnd
, hdc
);
612 /***********************************************************************
613 * CC_PaintColorGraph [internal]
615 static void CC_PaintColorGraph(CCPRIV
*infoPtr
)
617 HWND hwnd
= GetDlgItem( infoPtr
->hwndSelf
, COLOR_RAINBOW
);
621 if (IsWindowVisible(hwnd
)) /* if full size */
623 if (!infoPtr
->hdcMem
)
624 CC_PrepareColorGraph(infoPtr
); /* should not be necessary */
627 GetClientRect(hwnd
, &rect
);
629 BitBlt(hDC
, 0, 0, rect
.right
, rect
.bottom
, infoPtr
->hdcMem
, 0, 0, SRCCOPY
);
631 WARN("choose color: hdcMem is not defined\n");
632 ReleaseDC(hwnd
, hDC
);
636 /***********************************************************************
637 * CC_PaintLumBar [internal]
639 static void CC_PaintLumBar(const CCPRIV
*infoPtr
)
641 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_LUMSCROLL
);
647 if (IsWindowVisible(hwnd
))
650 GetClientRect(hwnd
, &client
);
654 ydif
= client
.bottom
/ YSTEPS
+1;
655 for (lum
= 0; lum
< 240 + ldif
; lum
+= ldif
)
657 rect
.top
= max(0, rect
.bottom
- ydif
);
658 hbrush
= CreateSolidBrush(CC_HSLtoRGB(infoPtr
->h
, infoPtr
->s
, lum
));
659 FillRect(hDC
, &rect
, hbrush
);
660 DeleteObject(hbrush
);
661 rect
.bottom
= rect
.top
;
663 GetClientRect(hwnd
, &rect
);
664 DrawEdge(hDC
, &rect
, BDR_SUNKENOUTER
, BF_RECT
);
665 ReleaseDC(hwnd
, hDC
);
669 /***********************************************************************
670 * CC_EditSetRGB [internal]
672 static void CC_EditSetRGB( CCPRIV
*infoPtr
)
674 if (IsWindowVisible( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
) )) /* if full size */
676 COLORREF cr
= infoPtr
->lpcc
->rgbResult
;
677 int r
= GetRValue(cr
);
678 int g
= GetGValue(cr
);
679 int b
= GetBValue(cr
);
681 infoPtr
->updating
= TRUE
;
682 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_RED
, r
, TRUE
);
683 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_GREEN
, g
, TRUE
);
684 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_BLUE
, b
, TRUE
);
685 infoPtr
->updating
= FALSE
;
689 /***********************************************************************
690 * CC_EditSetHSL [internal]
692 static void CC_EditSetHSL( CCPRIV
*infoPtr
)
694 if (IsWindowVisible( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
) )) /* if full size */
696 infoPtr
->updating
= TRUE
;
697 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_HUE
, infoPtr
->h
, TRUE
);
698 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_SAT
, infoPtr
->s
, TRUE
);
699 SetDlgItemInt(infoPtr
->hwndSelf
, COLOR_LUM
, infoPtr
->l
, TRUE
);
700 infoPtr
->updating
= FALSE
;
702 CC_PaintLumBar(infoPtr
);
705 /***********************************************************************
706 * CC_SwitchToFullSize [internal]
708 static void CC_SwitchToFullSize( CCPRIV
*infoPtr
, const RECT
*lprect
)
712 EnableWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_MIX
), FALSE
);
713 CC_PrepareColorGraph(infoPtr
);
714 for (i
= COLOR_HUE
; i
<= COLOR_BLUE
; i
++)
715 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, i
), SW_SHOW
);
716 for (i
= COLOR_HUEACCEL
; i
<= COLOR_BLUEACCEL
; i
++)
717 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, i
), SW_SHOW
);
718 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_SOLID
), SW_SHOW
);
719 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_ADD
), SW_SHOW
);
720 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, 1090), SW_SHOW
);
723 SetWindowPos(infoPtr
->hwndSelf
, 0, 0, 0, lprect
->right
-lprect
->left
,
724 lprect
->bottom
-lprect
->top
, SWP_NOMOVE
|SWP_NOZORDER
);
726 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_LUMSCROLL
), SW_SHOW
);
727 ShowWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_CURRENT
), SW_SHOW
);
729 CC_EditSetRGB(infoPtr
);
730 CC_EditSetHSL(infoPtr
);
731 ShowWindow( GetDlgItem( infoPtr
->hwndSelf
, COLOR_RAINBOW
), SW_SHOW
);
732 UpdateWindow( GetDlgItem(infoPtr
->hwndSelf
, COLOR_RAINBOW
) );
735 /***********************************************************************
736 * CC_PaintPredefColorArray [internal]
737 * Paints the default standard 48 colors
739 static void CC_PaintPredefColorArray(const CCPRIV
*infoPtr
, int rows
, int cols
)
741 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_BOX1
);
742 RECT rect
, blockrect
;
747 GetClientRect(hwnd
, &rect
);
748 dx
= rect
.right
/ cols
;
749 dy
= rect
.bottom
/ rows
;
753 GetClientRect(hwnd
, &rect
);
754 hBrush
= (HBRUSH
)GetClassLongPtrW( hwnd
, GCLP_HBRBACKGROUND
);
755 if (!hBrush
) hBrush
= GetSysColorBrush(COLOR_BTNFACE
);
756 FillRect(hdc
, &rect
, hBrush
);
757 for ( j
= 0; j
< rows
; j
++ )
759 for ( i
= 0; i
< cols
; i
++ )
761 hBrush
= CreateSolidBrush(predefcolors
[j
][i
]);
764 blockrect
.left
= rect
.left
;
765 blockrect
.top
= rect
.top
;
766 blockrect
.right
= rect
.left
+ dx
- DISTANCE
;
767 blockrect
.bottom
= rect
.top
+ dy
- DISTANCE
;
768 FillRect(hdc
, &blockrect
, hBrush
);
769 DrawEdge(hdc
, &blockrect
, BDR_SUNKEN
, BF_RECT
);
770 DeleteObject(hBrush
);
777 ReleaseDC(hwnd
, hdc
);
778 if (infoPtr
->hwndFocus
== hwnd
)
779 CC_DrawCurrentFocusRect(infoPtr
);
781 /***********************************************************************
782 * CC_PaintUserColorArray [internal]
783 * Paint the 16 user-selected colors
785 static void CC_PaintUserColorArray(const CCPRIV
*infoPtr
, int rows
, int cols
)
787 HWND hwnd
= GetDlgItem(infoPtr
->hwndSelf
, COLOR_CUSTOM1
);
788 RECT rect
, blockrect
;
793 GetClientRect(hwnd
, &rect
);
795 dx
= rect
.right
/ cols
;
796 dy
= rect
.bottom
/ rows
;
802 hBrush
= (HBRUSH
)GetClassLongPtrW( hwnd
, GCLP_HBRBACKGROUND
);
803 if (!hBrush
) hBrush
= GetSysColorBrush(COLOR_BTNFACE
);
804 FillRect( hdc
, &rect
, hBrush
);
805 for (j
= 0; j
< rows
; j
++)
807 for (i
= 0; i
< cols
; i
++)
809 hBrush
= CreateSolidBrush(infoPtr
->lpcc
->lpCustColors
[i
+j
*cols
]);
812 blockrect
.left
= rect
.left
;
813 blockrect
.top
= rect
.top
;
814 blockrect
.right
= rect
.left
+ dx
- DISTANCE
;
815 blockrect
.bottom
= rect
.top
+ dy
- DISTANCE
;
816 FillRect(hdc
, &blockrect
, hBrush
);
817 DrawEdge(hdc
, &blockrect
, BDR_SUNKEN
, BF_RECT
);
818 DeleteObject(hBrush
);
825 ReleaseDC(hwnd
, hdc
);
827 if (infoPtr
->hwndFocus
== hwnd
)
828 CC_DrawCurrentFocusRect(infoPtr
);
832 /***********************************************************************
833 * CC_HookCallChk [internal]
835 static BOOL
CC_HookCallChk( const CHOOSECOLORW
*lpcc
)
838 if(lpcc
->Flags
& CC_ENABLEHOOK
)
844 /***********************************************************************
845 * CC_WMInitDialog [internal]
847 static LRESULT
CC_WMInitDialog( HWND hDlg
, WPARAM wParam
, LPARAM lParam
)
849 CHOOSECOLORW
*cc
= (CHOOSECOLORW
*)lParam
;
857 TRACE("WM_INITDIALOG lParam=%08lX\n", lParam
);
859 if (cc
->lStructSize
!= sizeof(CHOOSECOLORW
))
865 lpp
= heap_alloc_zero(sizeof(*lpp
));
867 lpp
->hwndSelf
= hDlg
;
869 SetPropW( hDlg
, L
"colourdialogprop", lpp
);
871 if (!(lpp
->lpcc
->Flags
& CC_SHOWHELP
))
872 ShowWindow(GetDlgItem(hDlg
, pshHelp
), SW_HIDE
);
873 lpp
->msetrgb
= RegisterWindowMessageA(SETRGBSTRINGA
);
876 cpos
= MAKELONG(5,7); /* init */
877 if (lpp
->lpcc
->Flags
& CC_RGBINIT
)
879 for (i
= 0; i
< 6; i
++)
880 for (j
= 0; j
< 8; j
++)
881 if (predefcolors
[i
][j
] == lpp
->lpcc
->rgbResult
)
883 cpos
= MAKELONG(i
,j
);
888 /* FIXME: Draw_a_focus_rect & set_init_values */
891 GetWindowRect(hDlg
, &lpp
->fullsize
);
892 if (lpp
->lpcc
->Flags
& CC_FULLOPEN
|| lpp
->lpcc
->Flags
& CC_PREVENTFULLOPEN
)
894 hwnd
= GetDlgItem(hDlg
, COLOR_MIX
);
895 EnableWindow(hwnd
, FALSE
);
897 if (!(lpp
->lpcc
->Flags
& CC_FULLOPEN
) || lpp
->lpcc
->Flags
& CC_PREVENTFULLOPEN
)
899 rect
= lpp
->fullsize
;
900 res
= rect
.bottom
- rect
.top
;
901 hwnd
= GetDlgItem(hDlg
, COLOR_RAINBOW
); /* cut at left border */
902 point
.x
= point
.y
= 0;
903 ClientToScreen(hwnd
, &point
);
904 ScreenToClient(hDlg
,&point
);
905 GetClientRect(hDlg
, &rect
);
906 point
.x
+= GetSystemMetrics(SM_CXDLGFRAME
);
907 SetWindowPos(hDlg
, 0, 0, 0, point
.x
, res
, SWP_NOMOVE
|SWP_NOZORDER
);
909 for (i
= COLOR_HUE
; i
<= COLOR_BLUE
; i
++)
910 ShowWindow( GetDlgItem(hDlg
, i
), SW_HIDE
);
911 for (i
= COLOR_HUEACCEL
; i
<= COLOR_BLUEACCEL
; i
++)
912 ShowWindow( GetDlgItem(hDlg
, i
), SW_HIDE
);
913 ShowWindow( GetDlgItem(hDlg
, COLOR_SOLID
), SW_HIDE
);
914 ShowWindow( GetDlgItem(hDlg
, COLOR_ADD
), SW_HIDE
);
915 ShowWindow( GetDlgItem(hDlg
, COLOR_RAINBOW
), SW_HIDE
);
916 ShowWindow( GetDlgItem(hDlg
, COLOR_CURRENT
), SW_HIDE
);
917 ShowWindow( GetDlgItem(hDlg
, 1090 ), SW_HIDE
);
920 CC_SwitchToFullSize(lpp
, NULL
);
922 for (i
= COLOR_HUE
; i
<= COLOR_BLUE
; i
++)
923 SendMessageA( GetDlgItem(hDlg
, i
), EM_LIMITTEXT
, 3, 0); /* max 3 digits: xyz */
924 if (CC_HookCallChk(lpp
->lpcc
))
926 res
= CallWindowProcA( (WNDPROC
)lpp
->lpcc
->lpfnHook
, hDlg
, WM_INITDIALOG
, wParam
, lParam
);
929 /* Set the initial values of the color chooser dialog */
930 r
= GetRValue(lpp
->lpcc
->rgbResult
);
931 g
= GetGValue(lpp
->lpcc
->rgbResult
);
932 b
= GetBValue(lpp
->lpcc
->rgbResult
);
934 CC_PaintSelectedColor(lpp
);
935 lpp
->h
= CC_RGBtoHSL('H', lpp
->lpcc
->rgbResult
);
936 lpp
->s
= CC_RGBtoHSL('S', lpp
->lpcc
->rgbResult
);
937 lpp
->l
= CC_RGBtoHSL('L', lpp
->lpcc
->rgbResult
);
939 /* Doing it the long way because CC_EditSetRGB/HSL doesn't seem to work */
940 SetDlgItemInt(hDlg
, COLOR_HUE
, lpp
->h
, TRUE
);
941 SetDlgItemInt(hDlg
, COLOR_SAT
, lpp
->s
, TRUE
);
942 SetDlgItemInt(hDlg
, COLOR_LUM
, lpp
->l
, TRUE
);
943 SetDlgItemInt(hDlg
, COLOR_RED
, r
, TRUE
);
944 SetDlgItemInt(hDlg
, COLOR_GREEN
, g
, TRUE
);
945 SetDlgItemInt(hDlg
, COLOR_BLUE
, b
, TRUE
);
948 CC_PaintTriangle(lpp
);
954 /***********************************************************************
955 * CC_WMCommand [internal]
957 static LRESULT
CC_WMCommand(CCPRIV
*lpp
, WPARAM wParam
, LPARAM lParam
, WORD notifyCode
, HWND hwndCtl
)
964 TRACE("CC_WMCommand wParam=%lx lParam=%lx\n", wParam
, lParam
);
965 switch (LOWORD(wParam
))
967 case COLOR_RED
: /* edit notify RGB */
970 if (notifyCode
== EN_UPDATE
&& !lpp
->updating
)
972 i
= CC_CheckDigitsInEdit(hwndCtl
, 255);
973 r
= GetRValue(lpp
->lpcc
->rgbResult
);
974 g
= GetGValue(lpp
->lpcc
->rgbResult
);
975 b
= GetBValue(lpp
->lpcc
->rgbResult
);
977 switch (LOWORD(wParam
))
979 case COLOR_RED
: if ((xx
= (i
!= r
))) r
= i
; break;
980 case COLOR_GREEN
: if ((xx
= (i
!= g
))) g
= i
; break;
981 case COLOR_BLUE
: if ((xx
= (i
!= b
))) b
= i
; break;
983 if (xx
) /* something has changed */
985 lpp
->lpcc
->rgbResult
= RGB(r
, g
, b
);
986 CC_PaintSelectedColor(lpp
);
987 lpp
->h
= CC_RGBtoHSL('H', lpp
->lpcc
->rgbResult
);
988 lpp
->s
= CC_RGBtoHSL('S', lpp
->lpcc
->rgbResult
);
989 lpp
->l
= CC_RGBtoHSL('L', lpp
->lpcc
->rgbResult
);
992 CC_PaintTriangle(lpp
);
997 case COLOR_HUE
: /* edit notify HSL */
1000 if (notifyCode
== EN_UPDATE
&& !lpp
->updating
)
1002 i
= CC_CheckDigitsInEdit(hwndCtl
, LOWORD(wParam
) == COLOR_HUE
? 239 : 240);
1004 switch (LOWORD(wParam
))
1006 case COLOR_HUE
: if ((xx
= ( i
!= lpp
->h
))) lpp
->h
= i
; break;
1007 case COLOR_SAT
: if ((xx
= ( i
!= lpp
->s
))) lpp
->s
= i
; break;
1008 case COLOR_LUM
: if ((xx
= ( i
!= lpp
->l
))) lpp
->l
= i
; break;
1010 if (xx
) /* something has changed */
1012 lpp
->lpcc
->rgbResult
= CC_HSLtoRGB(lpp
->h
, lpp
->s
, lpp
->l
);
1013 CC_PaintSelectedColor(lpp
);
1016 CC_PaintTriangle(lpp
);
1022 CC_SwitchToFullSize(lpp
, &lpp
->fullsize
);
1023 SetFocus( GetDlgItem(lpp
->hwndSelf
, COLOR_HUE
));
1026 case COLOR_ADD
: /* add colors ... column by column */
1027 cr
= lpp
->lpcc
->lpCustColors
;
1028 cr
[(lpp
->nextuserdef
% 2) * 8 + lpp
->nextuserdef
/ 2] = lpp
->lpcc
->rgbResult
;
1029 if (++lpp
->nextuserdef
== 16)
1030 lpp
->nextuserdef
= 0;
1031 CC_PaintUserColorArray(lpp
, 2, 8);
1034 case COLOR_SOLID
: /* resulting color */
1035 hdc
= GetDC(lpp
->hwndSelf
);
1036 lpp
->lpcc
->rgbResult
= GetNearestColor(hdc
, lpp
->lpcc
->rgbResult
);
1037 ReleaseDC(lpp
->hwndSelf
, hdc
);
1039 CC_PaintSelectedColor(lpp
);
1040 lpp
->h
= CC_RGBtoHSL('H', lpp
->lpcc
->rgbResult
);
1041 lpp
->s
= CC_RGBtoHSL('S', lpp
->lpcc
->rgbResult
);
1042 lpp
->l
= CC_RGBtoHSL('L', lpp
->lpcc
->rgbResult
);
1045 CC_PaintTriangle(lpp
);
1048 case pshHelp
: /* Help! */ /* The Beatles, 1965 ;-) */
1049 i
= RegisterWindowMessageA(HELPMSGSTRINGA
);
1050 if (lpp
->lpcc
->hwndOwner
)
1051 SendMessageA(lpp
->lpcc
->hwndOwner
, i
, 0, (LPARAM
)lpp
->lpcc
);
1052 if ( CC_HookCallChk(lpp
->lpcc
))
1053 CallWindowProcA( (WNDPROC
) lpp
->lpcc
->lpfnHook
, lpp
->hwndSelf
,
1054 WM_COMMAND
, psh15
, (LPARAM
)lpp
->lpcc
);
1058 cokmsg
= RegisterWindowMessageA(COLOROKSTRINGA
);
1059 if (lpp
->lpcc
->hwndOwner
)
1060 if (SendMessageA(lpp
->lpcc
->hwndOwner
, cokmsg
, 0, (LPARAM
)lpp
->lpcc
))
1061 break; /* do NOT close */
1062 EndDialog(lpp
->hwndSelf
, 1) ;
1066 EndDialog(lpp
->hwndSelf
, 0) ;
1073 /***********************************************************************
1074 * CC_WMPaint [internal]
1076 static LRESULT
CC_WMPaint( CCPRIV
*lpp
)
1080 BeginPaint(lpp
->hwndSelf
, &ps
);
1081 /* we have to paint dialog children except text and buttons */
1082 CC_PaintPredefColorArray(lpp
, 6, 8);
1083 CC_PaintUserColorArray(lpp
, 2, 8);
1084 CC_PaintLumBar(lpp
);
1085 CC_PaintTriangle(lpp
);
1086 CC_PaintSelectedColor(lpp
);
1087 CC_PaintColorGraph(lpp
);
1089 EndPaint(lpp
->hwndSelf
, &ps
);
1094 /***********************************************************************
1095 * CC_WMLButtonUp [internal]
1097 static LRESULT
CC_WMLButtonUp( CCPRIV
*infoPtr
)
1099 if (infoPtr
->capturedGraph
)
1101 infoPtr
->capturedGraph
= 0;
1103 CC_PaintCross(infoPtr
);
1109 /***********************************************************************
1110 * CC_WMMouseMove [internal]
1112 static LRESULT
CC_WMMouseMove( CCPRIV
*infoPtr
, LPARAM lParam
)
1114 if (infoPtr
->capturedGraph
)
1116 int *ptrh
= NULL
, *ptrs
= &infoPtr
->l
;
1117 if (infoPtr
->capturedGraph
== COLOR_RAINBOW
)
1122 if (CC_MouseCheckColorGraph( infoPtr
->hwndSelf
, infoPtr
->capturedGraph
, ptrh
, ptrs
, lParam
))
1124 infoPtr
->lpcc
->rgbResult
= CC_HSLtoRGB(infoPtr
->h
, infoPtr
->s
, infoPtr
->l
);
1125 CC_EditSetRGB(infoPtr
);
1126 CC_EditSetHSL(infoPtr
);
1127 CC_PaintCross(infoPtr
);
1128 CC_PaintTriangle(infoPtr
);
1129 CC_PaintSelectedColor(infoPtr
);
1134 infoPtr
->capturedGraph
= 0;
1141 /***********************************************************************
1142 * CC_WMLButtonDown [internal]
1144 static LRESULT
CC_WMLButtonDown( CCPRIV
*infoPtr
, LPARAM lParam
)
1148 if (CC_MouseCheckPredefColorArray(infoPtr
, 6, 8, lParam
))
1151 if (CC_MouseCheckUserColorArray(infoPtr
, 2, 8, lParam
))
1154 if (CC_MouseCheckColorGraph(infoPtr
->hwndSelf
, COLOR_RAINBOW
, &infoPtr
->h
, &infoPtr
->s
, lParam
))
1157 infoPtr
->capturedGraph
= COLOR_RAINBOW
;
1160 if (CC_MouseCheckColorGraph(infoPtr
->hwndSelf
, COLOR_LUMSCROLL
, NULL
, &infoPtr
->l
, lParam
))
1163 infoPtr
->capturedGraph
= COLOR_LUMSCROLL
;
1167 SetCapture(infoPtr
->hwndSelf
);
1168 infoPtr
->lpcc
->rgbResult
= CC_HSLtoRGB(infoPtr
->h
, infoPtr
->s
, infoPtr
->l
);
1172 infoPtr
->h
= CC_RGBtoHSL('H', infoPtr
->lpcc
->rgbResult
);
1173 infoPtr
->s
= CC_RGBtoHSL('S', infoPtr
->lpcc
->rgbResult
);
1174 infoPtr
->l
= CC_RGBtoHSL('L', infoPtr
->lpcc
->rgbResult
);
1178 CC_EditSetRGB(infoPtr
);
1179 CC_EditSetHSL(infoPtr
);
1180 CC_PaintCross(infoPtr
);
1181 CC_PaintTriangle(infoPtr
);
1182 CC_PaintSelectedColor(infoPtr
);
1188 /***********************************************************************
1189 * ColorDlgProc32 [internal]
1192 static INT_PTR CALLBACK
ColorDlgProc( HWND hDlg
, UINT message
,
1193 WPARAM wParam
, LPARAM lParam
)
1197 CCPRIV
*lpp
= GetPropW( hDlg
, L
"colourdialogprop" );
1199 if (message
!= WM_INITDIALOG
)
1204 if (CC_HookCallChk(lpp
->lpcc
))
1205 res
= CallWindowProcA( (WNDPROC
)lpp
->lpcc
->lpfnHook
, hDlg
, message
, wParam
, lParam
);
1210 /* FIXME: SetRGB message
1211 if (message && message == msetrgb)
1212 return HandleSetRGB(hDlg, lParam);
1218 return CC_WMInitDialog(hDlg
, wParam
, lParam
);
1220 DeleteDC(lpp
->hdcMem
);
1221 DeleteObject(lpp
->hbmMem
);
1223 RemovePropW( hDlg
, L
"colourdialogprop" );
1226 if (CC_WMCommand(lpp
, wParam
, lParam
, HIWORD(wParam
), (HWND
) lParam
))
1230 if (CC_WMPaint(lpp
))
1233 case WM_LBUTTONDBLCLK
:
1234 if (CC_MouseCheckResultWindow(hDlg
, lParam
))
1238 if (CC_WMMouseMove(lpp
, lParam
))
1241 case WM_LBUTTONUP
: /* FIXME: ClipCursor off (if in color graph)*/
1242 if (CC_WMLButtonUp(lpp
))
1245 case WM_LBUTTONDOWN
:/* FIXME: ClipCursor on (if in color graph)*/
1246 if (CC_WMLButtonDown(lpp
, lParam
))
1253 /***********************************************************************
1254 * ChooseColorW (COMDLG32.@)
1256 * Create a color dialog box.
1259 * lpChCol [I/O] in: information to initialize the dialog box.
1260 * out: User's color selection
1263 * TRUE: Ok button clicked.
1264 * FALSE: Cancel button clicked, or error.
1266 BOOL WINAPI
ChooseColorW( CHOOSECOLORW
*lpChCol
)
1268 HANDLE hDlgTmpl
= 0;
1269 const void *template;
1271 TRACE("(%p)\n", lpChCol
);
1273 if (!lpChCol
) return FALSE
;
1275 if (lpChCol
->Flags
& CC_ENABLETEMPLATEHANDLE
)
1277 if (!(template = LockResource(lpChCol
->hInstance
)))
1279 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
1283 else if (lpChCol
->Flags
& CC_ENABLETEMPLATE
)
1286 if (!(hResInfo
= FindResourceW((HINSTANCE
)lpChCol
->hInstance
,
1287 lpChCol
->lpTemplateName
,
1288 (LPWSTR
)RT_DIALOG
)))
1290 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE
);
1293 if (!(hDlgTmpl
= LoadResource((HINSTANCE
)lpChCol
->hInstance
, hResInfo
)) ||
1294 !(template = LockResource(hDlgTmpl
)))
1296 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
1304 if (!(hResInfo
= FindResourceW(COMDLG32_hInstance
, L
"CHOOSE_COLOR", (LPWSTR
)RT_DIALOG
)))
1306 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE
);
1309 if (!(hDlgTmpl
= LoadResource(COMDLG32_hInstance
, hResInfo
)) ||
1310 !(template = LockResource(hDlgTmpl
)))
1312 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE
);
1317 return DialogBoxIndirectParamW(COMDLG32_hInstance
, template, lpChCol
->hwndOwner
,
1318 ColorDlgProc
, (LPARAM
)lpChCol
);
1321 /***********************************************************************
1322 * ChooseColorA (COMDLG32.@)
1326 BOOL WINAPI
ChooseColorA( LPCHOOSECOLORA lpChCol
)
1329 LPWSTR template_name
= NULL
;
1332 CHOOSECOLORW
*lpcc
= heap_alloc_zero(sizeof(*lpcc
));
1333 lpcc
->lStructSize
= sizeof(*lpcc
);
1334 lpcc
->hwndOwner
= lpChCol
->hwndOwner
;
1335 lpcc
->hInstance
= lpChCol
->hInstance
;
1336 lpcc
->rgbResult
= lpChCol
->rgbResult
;
1337 lpcc
->lpCustColors
= lpChCol
->lpCustColors
;
1338 lpcc
->Flags
= lpChCol
->Flags
;
1339 lpcc
->lCustData
= lpChCol
->lCustData
;
1340 lpcc
->lpfnHook
= lpChCol
->lpfnHook
;
1341 if ((lpcc
->Flags
& CC_ENABLETEMPLATE
) && (lpChCol
->lpTemplateName
)) {
1342 if (!IS_INTRESOURCE(lpChCol
->lpTemplateName
)) {
1343 INT len
= MultiByteToWideChar( CP_ACP
, 0, lpChCol
->lpTemplateName
, -1, NULL
, 0);
1344 template_name
= heap_alloc( len
* sizeof(WCHAR
) );
1345 MultiByteToWideChar( CP_ACP
, 0, lpChCol
->lpTemplateName
, -1, template_name
, len
);
1346 lpcc
->lpTemplateName
= template_name
;
1348 lpcc
->lpTemplateName
= (LPCWSTR
)lpChCol
->lpTemplateName
;
1352 ret
= ChooseColorW(lpcc
);
1355 lpChCol
->rgbResult
= lpcc
->rgbResult
;
1357 heap_free(template_name
);