Sync with trunk r63270.
[reactos.git] / dll / cpl / console / colors.c
1 /*
2 * PROJECT: ReactOS Console Configuration DLL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/console/colors.c
5 * PURPOSE: Colors dialog
6 * PROGRAMMERS: Johannes Anderwald (johannes.anderwald@reactos.org)
7 */
8
9 #include "console.h"
10
11 #define NDEBUG
12 #include <debug.h>
13
14 static BOOL
15 PaintStaticControls(HWND hwndDlg,
16 PCONSOLE_PROPS pConInfo,
17 LPDRAWITEMSTRUCT drawItem)
18 {
19 HBRUSH hBrush;
20 DWORD index;
21
22 index = min(drawItem->CtlID - IDC_STATIC_COLOR1,
23 sizeof(pConInfo->ci.Colors) / sizeof(pConInfo->ci.Colors[0]) - 1);
24 hBrush = CreateSolidBrush(pConInfo->ci.Colors[index]);
25 if (!hBrush) return FALSE;
26
27 FillRect(drawItem->hDC, &drawItem->rcItem, hBrush);
28 DeleteObject((HGDIOBJ)hBrush);
29 if (pConInfo->ActiveStaticControl == index)
30 {
31 DrawFocusRect(drawItem->hDC, &drawItem->rcItem);
32 }
33
34 return TRUE;
35 }
36
37 INT_PTR CALLBACK
38 ColorsProc(HWND hwndDlg,
39 UINT uMsg,
40 WPARAM wParam,
41 LPARAM lParam)
42 {
43 PCONSOLE_PROPS pConInfo;
44 DWORD colorIndex;
45 COLORREF color;
46
47 pConInfo = (PCONSOLE_PROPS)GetWindowLongPtr(hwndDlg, DWLP_USER);
48
49 switch (uMsg)
50 {
51 case WM_INITDIALOG:
52 {
53 pConInfo = (PCONSOLE_PROPS)((LPPROPSHEETPAGE)lParam)->lParam;
54 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pConInfo);
55
56 /* Set the valid range of the colour indicators */
57 SendDlgItemMessageW(hwndDlg, IDC_UPDOWN_COLOR_RED , UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
58 SendDlgItemMessageW(hwndDlg, IDC_UPDOWN_COLOR_GREEN, UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
59 SendDlgItemMessageW(hwndDlg, IDC_UPDOWN_COLOR_BLUE , UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
60
61 /* Select by default the screen background option */
62 CheckRadioButton(hwndDlg, IDC_RADIO_SCREEN_TEXT, IDC_RADIO_POPUP_BACKGROUND, IDC_RADIO_SCREEN_BACKGROUND);
63 SendMessage(hwndDlg, WM_COMMAND, IDC_RADIO_SCREEN_BACKGROUND, 0);
64
65 return TRUE;
66 }
67
68 case WM_DRAWITEM:
69 {
70 LPDRAWITEMSTRUCT drawItem = (LPDRAWITEMSTRUCT)lParam;
71
72 if (drawItem->CtlID >= IDC_STATIC_COLOR1 && drawItem->CtlID <= IDC_STATIC_COLOR16)
73 return PaintStaticControls(hwndDlg, pConInfo, drawItem);
74 else if (drawItem->CtlID == IDC_STATIC_SCREEN_COLOR)
75 return PaintText(drawItem, pConInfo, Screen);
76 else if (drawItem->CtlID == IDC_STATIC_POPUP_COLOR)
77 return PaintText(drawItem, pConInfo, Popup);
78
79 break;
80 }
81
82 case WM_NOTIFY:
83 {
84 switch (((LPNMHDR)lParam)->code)
85 {
86 case PSN_APPLY:
87 {
88 if (!pConInfo->AppliedConfig)
89 {
90 return ApplyConsoleInfo(hwndDlg, pConInfo);
91 }
92 else
93 {
94 /* Options have already been applied */
95 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
96 return TRUE;
97 }
98 break;
99 }
100
101 case UDN_DELTAPOS:
102 {
103 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lParam;
104
105 /* Get the current color */
106 colorIndex = pConInfo->ActiveStaticControl;
107 color = pConInfo->ci.Colors[colorIndex];
108
109 if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_RED)
110 {
111 lpnmud->iPos = min(max(lpnmud->iPos + lpnmud->iDelta, 0), 255);
112 color = RGB(lpnmud->iPos, GetGValue(color), GetBValue(color));
113 }
114 else if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_GREEN)
115 {
116 lpnmud->iPos = min(max(lpnmud->iPos + lpnmud->iDelta, 0), 255);
117 color = RGB(GetRValue(color), lpnmud->iPos, GetBValue(color));
118 }
119 else if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_BLUE)
120 {
121 lpnmud->iPos = min(max(lpnmud->iPos + lpnmud->iDelta, 0), 255);
122 color = RGB(GetRValue(color), GetGValue(color), lpnmud->iPos);
123 }
124 else
125 {
126 break;
127 }
128
129 pConInfo->ci.Colors[colorIndex] = color;
130 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + colorIndex), NULL, TRUE);
131 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
132 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
133
134 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
135 break;
136 }
137 }
138
139 break;
140 }
141
142 case WM_COMMAND:
143 {
144 switch (LOWORD(wParam))
145 {
146 case IDC_RADIO_SCREEN_TEXT:
147 {
148 /* Get the color of the screen foreground */
149 colorIndex = TextAttribFromAttrib(pConInfo->ci.ScreenAttrib);
150 color = pConInfo->ci.Colors[colorIndex];
151
152 /* Set the values of the colour indicators */
153 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED , GetRValue(color), FALSE);
154 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
155 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE , GetBValue(color), FALSE);
156
157 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
158 pConInfo->ActiveStaticControl = colorIndex;
159 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
160 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
161 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
162 break;
163 }
164
165 case IDC_RADIO_SCREEN_BACKGROUND:
166 {
167 /* Get the color of the screen background */
168 colorIndex = BkgdAttribFromAttrib(pConInfo->ci.ScreenAttrib);
169 color = pConInfo->ci.Colors[colorIndex];
170
171 /* Set the values of the colour indicators */
172 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED , GetRValue(color), FALSE);
173 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
174 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE , GetBValue(color), FALSE);
175
176 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
177 pConInfo->ActiveStaticControl = colorIndex;
178 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
179 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
180 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
181 break;
182 }
183
184 case IDC_RADIO_POPUP_TEXT:
185 {
186 /* Get the color of the popup foreground */
187 colorIndex = TextAttribFromAttrib(pConInfo->ci.PopupAttrib);
188 color = pConInfo->ci.Colors[colorIndex];
189
190 /* Set the values of the colour indicators */
191 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED , GetRValue(color), FALSE);
192 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
193 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE , GetBValue(color), FALSE);
194
195 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
196 pConInfo->ActiveStaticControl = colorIndex;
197 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
198 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
199 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
200 break;
201 }
202
203 case IDC_RADIO_POPUP_BACKGROUND:
204 {
205 /* Get the color of the popup background */
206 colorIndex = BkgdAttribFromAttrib(pConInfo->ci.PopupAttrib);
207 color = pConInfo->ci.Colors[colorIndex];
208
209 /* Set the values of the colour indicators */
210 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED , GetRValue(color), FALSE);
211 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
212 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE , GetBValue(color), FALSE);
213
214 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
215 pConInfo->ActiveStaticControl = colorIndex;
216 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
217 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
218 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
219 break;
220 }
221
222 case IDC_EDIT_COLOR_RED:
223 {
224 if (HIWORD(wParam) == EN_KILLFOCUS)
225 {
226 DWORD red;
227
228 /* Get the current color */
229 colorIndex = pConInfo->ActiveStaticControl;
230 color = pConInfo->ci.Colors[colorIndex];
231
232 red = GetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED, NULL, FALSE);
233 red = min(max(red, 0), 255);
234
235 color = RGB(red, GetGValue(color), GetBValue(color));
236
237 pConInfo->ci.Colors[colorIndex] = color;
238 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + colorIndex), NULL, TRUE);
239 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
240 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
241
242 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
243 }
244 break;
245 }
246
247 case IDC_EDIT_COLOR_GREEN:
248 {
249 if (HIWORD(wParam) == EN_KILLFOCUS)
250 {
251 DWORD green;
252
253 /* Get the current color */
254 colorIndex = pConInfo->ActiveStaticControl;
255 color = pConInfo->ci.Colors[colorIndex];
256
257 green = GetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, NULL, FALSE);
258 green = min(max(green, 0), 255);
259
260 color = RGB(GetRValue(color), green, GetBValue(color));
261
262 pConInfo->ci.Colors[colorIndex] = color;
263 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + colorIndex), NULL, TRUE);
264 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
265 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
266
267 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
268 }
269 break;
270 }
271
272 case IDC_EDIT_COLOR_BLUE:
273 {
274 if (HIWORD(wParam) == EN_KILLFOCUS)
275 {
276 DWORD blue;
277
278 /* Get the current color */
279 colorIndex = pConInfo->ActiveStaticControl;
280 color = pConInfo->ci.Colors[colorIndex];
281
282 blue = GetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE, NULL, FALSE);
283 blue = min(max(blue, 0), 255);
284
285 color = RGB(GetRValue(color), GetGValue(color), blue);
286
287 pConInfo->ci.Colors[colorIndex] = color;
288 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + colorIndex), NULL, TRUE);
289 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
290 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
291
292 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
293 }
294 break;
295 }
296
297 }
298
299 if ( HIWORD(wParam) == STN_CLICKED &&
300 IDC_STATIC_COLOR1 <= LOWORD(wParam) && LOWORD(wParam) <= IDC_STATIC_COLOR16 )
301 {
302 colorIndex = LOWORD(wParam) - IDC_STATIC_COLOR1;
303
304 if (colorIndex == pConInfo->ActiveStaticControl)
305 {
306 /* Same static control was re-clicked */
307 break;
308 }
309
310 color = pConInfo->ci.Colors[colorIndex];
311
312 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED , GetRValue(color), FALSE);
313 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
314 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE , GetBValue(color), FALSE);
315
316 /* Update global struct */
317 if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_SCREEN_TEXT))
318 {
319 pConInfo->ci.ScreenAttrib = MakeAttrib(colorIndex, BkgdAttribFromAttrib(pConInfo->ci.ScreenAttrib));
320 }
321 else if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_SCREEN_BACKGROUND))
322 {
323 pConInfo->ci.ScreenAttrib = MakeAttrib(TextAttribFromAttrib(pConInfo->ci.ScreenAttrib), colorIndex);
324 }
325 else if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_POPUP_TEXT))
326 {
327 pConInfo->ci.PopupAttrib = MakeAttrib(colorIndex, BkgdAttribFromAttrib(pConInfo->ci.PopupAttrib));
328 }
329 else if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_POPUP_BACKGROUND))
330 {
331 pConInfo->ci.PopupAttrib = MakeAttrib(TextAttribFromAttrib(pConInfo->ci.PopupAttrib), colorIndex);
332 }
333
334 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
335 pConInfo->ActiveStaticControl = colorIndex;
336 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
337 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
338 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
339
340 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
341 break;
342 }
343 }
344
345 default:
346 break;
347 }
348
349 return FALSE;
350 }