94f549a6eb1330f6f16fa1b475564a764fdca587
[reactos.git] / reactos / 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: displays colors dialog
6 * PROGRAMMERS: Johannes Anderwald (johannes.anderwald@student.tugraz.at)
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)
26 {
27 return FALSE;
28 }
29
30 FillRect(drawItem->hDC, &drawItem->rcItem, hBrush);
31 DeleteObject((HGDIOBJ)hBrush);
32 if (pConInfo->ActiveStaticControl == index)
33 {
34 DrawFocusRect(drawItem->hDC, &drawItem->rcItem);
35 }
36
37 return TRUE;
38 }
39
40 INT_PTR CALLBACK
41 ColorsProc(HWND hwndDlg,
42 UINT uMsg,
43 WPARAM wParam,
44 LPARAM lParam)
45 {
46 PCONSOLE_PROPS pConInfo;
47 LPDRAWITEMSTRUCT drawItem;
48 DWORD colorIndex;
49 COLORREF color;
50
51 pConInfo = (PCONSOLE_PROPS)GetWindowLongPtr(hwndDlg, DWLP_USER);
52
53 switch (uMsg)
54 {
55 case WM_INITDIALOG:
56 {
57 pConInfo = (PCONSOLE_PROPS)((LPPROPSHEETPAGE)lParam)->lParam;
58 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pConInfo);
59
60 /* Set the valid range of the colour indicators */
61 SendMessage(GetDlgItem(hwndDlg, IDC_UPDOWN_COLOR_RED), UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
62 SendMessage(GetDlgItem(hwndDlg, IDC_UPDOWN_COLOR_GREEN), UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
63 SendMessage(GetDlgItem(hwndDlg, IDC_UPDOWN_COLOR_BLUE), UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
64
65 /* Select by default the screen background option */
66 CheckRadioButton(hwndDlg, IDC_RADIO_SCREEN_TEXT, IDC_RADIO_POPUP_BACKGROUND, IDC_RADIO_SCREEN_BACKGROUND);
67 SendMessage(hwndDlg, WM_COMMAND, IDC_RADIO_SCREEN_BACKGROUND, 0);
68
69 return TRUE;
70 }
71
72 case WM_DRAWITEM:
73 {
74 drawItem = (LPDRAWITEMSTRUCT)lParam;
75 if (drawItem->CtlID >= IDC_STATIC_COLOR1 && drawItem->CtlID <= IDC_STATIC_COLOR16)
76 {
77 return PaintStaticControls(hwndDlg, pConInfo, drawItem);
78 }
79 else if (drawItem->CtlID == IDC_STATIC_SCREEN_COLOR || drawItem->CtlID == IDC_STATIC_POPUP_COLOR)
80 {
81 PaintText(drawItem, pConInfo);
82 return TRUE;
83 }
84 break;
85 }
86
87 case WM_NOTIFY:
88 {
89 switch (((LPNMHDR)lParam)->code)
90 {
91 case PSN_APPLY:
92 {
93 // LPPSHNOTIFY lppsn;
94 if (!pConInfo->AppliedConfig)
95 {
96 return ApplyConsoleInfo(hwndDlg, pConInfo);
97 }
98 else
99 {
100 /* Options have already been applied */
101 SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
102 return TRUE;
103 }
104 break;
105 }
106
107 case UDN_DELTAPOS:
108 {
109 LPNMUPDOWN lpnmud = (LPNMUPDOWN)lParam;
110
111 /* Get the current color */
112 colorIndex = pConInfo->ActiveStaticControl;
113 color = pConInfo->ci.Colors[colorIndex];
114
115 if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_RED)
116 {
117 if (lpnmud->iPos < 0) lpnmud->iPos = 0;
118 else if (lpnmud->iPos > 255) lpnmud->iPos = 255;
119
120 color = RGB(lpnmud->iPos, GetGValue(color), GetBValue(color));
121 }
122 else if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_GREEN)
123 {
124 if (lpnmud->iPos < 0) lpnmud->iPos = 0;
125 else if (lpnmud->iPos > 255) lpnmud->iPos = 255;
126
127 color = RGB(GetRValue(color), lpnmud->iPos, GetBValue(color));
128 }
129 else if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_BLUE)
130 {
131 if (lpnmud->iPos < 0) lpnmud->iPos = 0;
132 else if (lpnmud->iPos > 255) lpnmud->iPos = 255;
133
134 color = RGB(GetRValue(color), GetGValue(color), lpnmud->iPos);
135 }
136 else
137 {
138 break;
139 }
140
141 pConInfo->ci.Colors[colorIndex] = color;
142 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + colorIndex), NULL, TRUE);
143 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
144 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR), NULL, TRUE);
145
146 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
147 break;
148 }
149 }
150
151 break;
152 }
153
154 case WM_COMMAND:
155 {
156 switch (LOWORD(wParam))
157 {
158 case IDC_RADIO_SCREEN_TEXT:
159 {
160 /* Get the color of the screen foreground */
161 colorIndex = TextAttribFromAttrib(pConInfo->ci.ScreenAttrib);
162 color = pConInfo->ci.Colors[colorIndex];
163
164 /* Set the values of the colour indicators */
165 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED, GetRValue(color), FALSE);
166 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
167 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE, GetBValue(color), FALSE);
168
169 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
170 pConInfo->ActiveStaticControl = colorIndex;
171 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
172 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
173 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR), NULL, TRUE);
174 break;
175 }
176
177 case IDC_RADIO_SCREEN_BACKGROUND:
178 {
179 /* Get the color of the screen background */
180 colorIndex = BkgdAttribFromAttrib(pConInfo->ci.ScreenAttrib);
181 color = pConInfo->ci.Colors[colorIndex];
182
183 /* Set the values of the colour indicators */
184 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED, GetRValue(color), FALSE);
185 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
186 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE, GetBValue(color), FALSE);
187
188 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
189 pConInfo->ActiveStaticControl = colorIndex;
190 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
191 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
192 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR), NULL, TRUE);
193 break;
194 }
195
196 case IDC_RADIO_POPUP_TEXT:
197 {
198 /* Get the color of the popup foreground */
199 colorIndex = TextAttribFromAttrib(pConInfo->ci.PopupAttrib);
200 color = pConInfo->ci.Colors[colorIndex];
201
202 /* Set the values of the colour indicators */
203 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED, GetRValue(color), FALSE);
204 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
205 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE, GetBValue(color), FALSE);
206
207 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
208 pConInfo->ActiveStaticControl = colorIndex;
209 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
210 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
211 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR), NULL, TRUE);
212 break;
213 }
214
215 case IDC_RADIO_POPUP_BACKGROUND:
216 {
217 /* Get the color of the popup background */
218 colorIndex = BkgdAttribFromAttrib(pConInfo->ci.PopupAttrib);
219 color = pConInfo->ci.Colors[colorIndex];
220
221 /* Set the values of the colour indicators */
222 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED, GetRValue(color), FALSE);
223 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
224 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE, GetBValue(color), FALSE);
225
226 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
227 pConInfo->ActiveStaticControl = colorIndex;
228 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
229 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
230 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR), NULL, TRUE);
231 break;
232 }
233 }
234
235 if ( HIWORD(wParam) == STN_CLICKED &&
236 IDC_STATIC_COLOR1 <= LOWORD(wParam) && LOWORD(wParam) <= IDC_STATIC_COLOR16 )
237 {
238 colorIndex = LOWORD(wParam) - IDC_STATIC_COLOR1;
239
240 if (colorIndex == pConInfo->ActiveStaticControl)
241 {
242 /* Same static control was re-clicked */
243 break;
244 }
245
246 color = pConInfo->ci.Colors[colorIndex];
247
248 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_RED, GetRValue(color), FALSE);
249 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
250 SetDlgItemInt(hwndDlg, IDC_EDIT_COLOR_BLUE, GetBValue(color), FALSE);
251
252 /* Update global struct */
253 if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_SCREEN_TEXT))
254 {
255 pConInfo->ci.ScreenAttrib = MakeAttrib(colorIndex, BkgdAttribFromAttrib(pConInfo->ci.ScreenAttrib));
256 }
257 else if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_SCREEN_BACKGROUND))
258 {
259 pConInfo->ci.ScreenAttrib = MakeAttrib(TextAttribFromAttrib(pConInfo->ci.ScreenAttrib), colorIndex);
260 }
261 else if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_POPUP_TEXT))
262 {
263 pConInfo->ci.PopupAttrib = MakeAttrib(colorIndex, BkgdAttribFromAttrib(pConInfo->ci.PopupAttrib));
264 }
265 else if (IsDlgButtonChecked(hwndDlg, IDC_RADIO_POPUP_BACKGROUND))
266 {
267 pConInfo->ci.PopupAttrib = MakeAttrib(TextAttribFromAttrib(pConInfo->ci.PopupAttrib), colorIndex);
268 }
269
270 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
271 pConInfo->ActiveStaticControl = colorIndex;
272 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_COLOR1 + pConInfo->ActiveStaticControl), NULL, TRUE);
273 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
274 InvalidateRect(GetDlgItem(hwndDlg, IDC_STATIC_POPUP_COLOR), NULL, TRUE);
275
276 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
277 break;
278 }
279 }
280
281 default:
282 break;
283 }
284
285 return FALSE;
286 }