xref: /reactos/dll/cpl/console/colors.c (revision 38f6fd9d)
1 /*
2  * PROJECT:         ReactOS Console Configuration DLL
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * FILE:            dll/cpl/console/colors.c
5  * PURPOSE:         Colors dialog
6  * PROGRAMMERS:     Johannes Anderwald (johannes.anderwald@reactos.org)
7  *                  Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8  */
9 
10 #include "console.h"
11 
12 #define NDEBUG
13 #include <debug.h>
14 
15 static DWORD ActiveStaticControl = 0;
16 
17 static VOID
PaintStaticControls(IN LPDRAWITEMSTRUCT drawItem,IN PCONSOLE_STATE_INFO pConInfo)18 PaintStaticControls(
19     IN LPDRAWITEMSTRUCT drawItem,
20     IN PCONSOLE_STATE_INFO pConInfo)
21 {
22     HBRUSH hBrush;
23     DWORD index;
24 
25     index = min(drawItem->CtlID - IDC_STATIC_COLOR1,
26                 ARRAYSIZE(pConInfo->ColorTable) - 1);
27 
28     hBrush = CreateSolidBrush(pConInfo->ColorTable[index]);
29     FillRect(drawItem->hDC, &drawItem->rcItem, hBrush ? hBrush : GetStockObject(BLACK_BRUSH));
30     if (hBrush) DeleteObject(hBrush);
31 
32     if (ActiveStaticControl == index)
33         DrawFocusRect(drawItem->hDC, &drawItem->rcItem);
34 }
35 
36 INT_PTR CALLBACK
ColorsProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)37 ColorsProc(HWND hDlg,
38            UINT uMsg,
39            WPARAM wParam,
40            LPARAM lParam)
41 {
42     DWORD colorIndex;
43     COLORREF color;
44 
45     switch (uMsg)
46     {
47         case WM_INITDIALOG:
48         {
49             /* Set the valid range of the colour indicators */
50             SendDlgItemMessageW(hDlg, IDC_UPDOWN_COLOR_RED  , UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
51             SendDlgItemMessageW(hDlg, IDC_UPDOWN_COLOR_GREEN, UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
52             SendDlgItemMessageW(hDlg, IDC_UPDOWN_COLOR_BLUE , UDM_SETRANGE, 0, (LPARAM)MAKELONG(255, 0));
53 
54             /* Select by default the screen background option */
55             CheckRadioButton(hDlg, IDC_RADIO_SCREEN_TEXT, IDC_RADIO_POPUP_BACKGROUND, IDC_RADIO_SCREEN_BACKGROUND);
56             SendMessageW(hDlg, WM_COMMAND, IDC_RADIO_SCREEN_BACKGROUND, 0);
57 
58             return TRUE;
59         }
60 
61         case WM_DRAWITEM:
62         {
63             LPDRAWITEMSTRUCT drawItem = (LPDRAWITEMSTRUCT)lParam;
64 
65             if (IDC_STATIC_COLOR1 <= drawItem->CtlID && drawItem->CtlID <= IDC_STATIC_COLOR16)
66                 PaintStaticControls(drawItem, ConInfo);
67             else if (drawItem->CtlID == IDC_STATIC_SCREEN_COLOR)
68                 PaintText(drawItem, ConInfo, Screen);
69             else if (drawItem->CtlID == IDC_STATIC_POPUP_COLOR)
70                 PaintText(drawItem, ConInfo, Popup);
71 
72             return TRUE;
73         }
74 
75         case WM_NOTIFY:
76         {
77             switch (((LPNMHDR)lParam)->code)
78             {
79                 case PSN_APPLY:
80                 {
81                     ApplyConsoleInfo(hDlg);
82                     return TRUE;
83                 }
84 
85                 case UDN_DELTAPOS:
86                 {
87                     LPNMUPDOWN lpnmud = (LPNMUPDOWN)lParam;
88 
89                     /* Get the current color */
90                     colorIndex = ActiveStaticControl;
91                     color = ConInfo->ColorTable[colorIndex];
92 
93                     if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_RED)
94                     {
95                         lpnmud->iPos = min(max(lpnmud->iPos + lpnmud->iDelta, 0), 255);
96                         color = RGB(lpnmud->iPos, GetGValue(color), GetBValue(color));
97                     }
98                     else if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_GREEN)
99                     {
100                         lpnmud->iPos = min(max(lpnmud->iPos + lpnmud->iDelta, 0), 255);
101                         color = RGB(GetRValue(color), lpnmud->iPos, GetBValue(color));
102                     }
103                     else if (lpnmud->hdr.idFrom == IDC_UPDOWN_COLOR_BLUE)
104                     {
105                         lpnmud->iPos = min(max(lpnmud->iPos + lpnmud->iDelta, 0), 255);
106                         color = RGB(GetRValue(color), GetGValue(color), lpnmud->iPos);
107                     }
108                     else
109                     {
110                         break;
111                     }
112 
113                     ConInfo->ColorTable[colorIndex] = color;
114                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_COLOR1 + colorIndex), NULL, TRUE);
115                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
116                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
117 
118                     PropSheet_Changed(GetParent(hDlg), hDlg);
119                     break;
120                 }
121             }
122 
123             break;
124         }
125 
126         case WM_COMMAND:
127         {
128             /* NOTE: both BN_CLICKED and STN_CLICKED == 0 */
129             if (HIWORD(wParam) == BN_CLICKED /* || HIWORD(wParam) == STN_CLICKED */)
130             {
131                 WORD ctrlIndex = LOWORD(wParam);
132 
133                 if (ctrlIndex == IDC_RADIO_SCREEN_TEXT       ||
134                     ctrlIndex == IDC_RADIO_SCREEN_BACKGROUND ||
135                     ctrlIndex == IDC_RADIO_POPUP_TEXT        ||
136                     ctrlIndex == IDC_RADIO_POPUP_BACKGROUND)
137                 {
138                     switch (ctrlIndex)
139                     {
140                     case IDC_RADIO_SCREEN_TEXT:
141                         /* Get the colour of the screen foreground */
142                         colorIndex = TextAttribFromAttrib(ConInfo->ScreenAttributes);
143                         break;
144 
145                     case IDC_RADIO_SCREEN_BACKGROUND:
146                         /* Get the colour of the screen background */
147                         colorIndex = BkgdAttribFromAttrib(ConInfo->ScreenAttributes);
148                         break;
149 
150                     case IDC_RADIO_POPUP_TEXT:
151                         /* Get the colour of the popup foreground */
152                         colorIndex = TextAttribFromAttrib(ConInfo->PopupAttributes);
153                         break;
154 
155                     case IDC_RADIO_POPUP_BACKGROUND:
156                         /* Get the colour of the popup background */
157                         colorIndex = BkgdAttribFromAttrib(ConInfo->PopupAttributes);
158                         break;
159                     }
160 
161                     color = ConInfo->ColorTable[colorIndex];
162 
163                     /* Set the values of the colour indicators */
164                     SetDlgItemInt(hDlg, IDC_EDIT_COLOR_RED  , GetRValue(color), FALSE);
165                     SetDlgItemInt(hDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
166                     SetDlgItemInt(hDlg, IDC_EDIT_COLOR_BLUE , GetBValue(color), FALSE);
167 
168                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_COLOR1 + ActiveStaticControl), NULL, TRUE);
169                     ActiveStaticControl = colorIndex;
170                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_COLOR1 + ActiveStaticControl), NULL, TRUE);
171                     break;
172                 }
173                 else
174                 if (IDC_STATIC_COLOR1 <= ctrlIndex && ctrlIndex <= IDC_STATIC_COLOR16)
175                 {
176                     colorIndex = ctrlIndex - IDC_STATIC_COLOR1;
177 
178                     /* If the same static control was re-clicked, don't take it into account */
179                     if (colorIndex == ActiveStaticControl)
180                         break;
181 
182                     color = ConInfo->ColorTable[colorIndex];
183 
184                     /* Set the values of the colour indicators */
185                     SetDlgItemInt(hDlg, IDC_EDIT_COLOR_RED  , GetRValue(color), FALSE);
186                     SetDlgItemInt(hDlg, IDC_EDIT_COLOR_GREEN, GetGValue(color), FALSE);
187                     SetDlgItemInt(hDlg, IDC_EDIT_COLOR_BLUE , GetBValue(color), FALSE);
188 
189                     if (IsDlgButtonChecked(hDlg, IDC_RADIO_SCREEN_TEXT))
190                     {
191                         ConInfo->ScreenAttributes = MakeAttrib(colorIndex, BkgdAttribFromAttrib(ConInfo->ScreenAttributes));
192                     }
193                     else if (IsDlgButtonChecked(hDlg, IDC_RADIO_SCREEN_BACKGROUND))
194                     {
195                         ConInfo->ScreenAttributes = MakeAttrib(TextAttribFromAttrib(ConInfo->ScreenAttributes), colorIndex);
196                     }
197                     else if (IsDlgButtonChecked(hDlg, IDC_RADIO_POPUP_TEXT))
198                     {
199                         ConInfo->PopupAttributes = MakeAttrib(colorIndex, BkgdAttribFromAttrib(ConInfo->PopupAttributes));
200                     }
201                     else if (IsDlgButtonChecked(hDlg, IDC_RADIO_POPUP_BACKGROUND))
202                     {
203                         ConInfo->PopupAttributes = MakeAttrib(TextAttribFromAttrib(ConInfo->PopupAttributes), colorIndex);
204                     }
205 
206                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_COLOR1 + ActiveStaticControl), NULL, TRUE);
207                     ActiveStaticControl = colorIndex;
208                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_COLOR1 + ActiveStaticControl), NULL, TRUE);
209                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
210                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
211 
212                     PropSheet_Changed(GetParent(hDlg), hDlg);
213                     break;
214                 }
215             }
216             else if (HIWORD(wParam) == EN_KILLFOCUS)
217             {
218                 WORD ctrlIndex = LOWORD(wParam);
219 
220                 if (ctrlIndex == IDC_EDIT_COLOR_RED   ||
221                     ctrlIndex == IDC_EDIT_COLOR_GREEN ||
222                     ctrlIndex == IDC_EDIT_COLOR_BLUE)
223                 {
224                     DWORD value;
225 
226                     /* Get the current colour */
227                     colorIndex = ActiveStaticControl;
228                     color = ConInfo->ColorTable[colorIndex];
229 
230                     /* Modify the colour component */
231                     switch (ctrlIndex)
232                     {
233                     case IDC_EDIT_COLOR_RED:
234                         value = GetDlgItemInt(hDlg, IDC_EDIT_COLOR_RED, NULL, FALSE);
235                         value = min(max(value, 0), 255);
236                         color = RGB(value, GetGValue(color), GetBValue(color));
237                         break;
238 
239                     case IDC_EDIT_COLOR_GREEN:
240                         value = GetDlgItemInt(hDlg, IDC_EDIT_COLOR_GREEN, NULL, FALSE);
241                         value = min(max(value, 0), 255);
242                         color = RGB(GetRValue(color), value, GetBValue(color));
243                         break;
244 
245                     case IDC_EDIT_COLOR_BLUE:
246                         value = GetDlgItemInt(hDlg, IDC_EDIT_COLOR_BLUE, NULL, FALSE);
247                         value = min(max(value, 0), 255);
248                         color = RGB(GetRValue(color), GetGValue(color), value);
249                         break;
250                     }
251 
252                     ConInfo->ColorTable[colorIndex] = color;
253                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_COLOR1 + colorIndex), NULL, TRUE);
254                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_SCREEN_COLOR), NULL, TRUE);
255                     InvalidateRect(GetDlgItem(hDlg, IDC_STATIC_POPUP_COLOR) , NULL, TRUE);
256 
257                     PropSheet_Changed(GetParent(hDlg), hDlg);
258                     break;
259                 }
260             }
261 
262             break;
263         }
264 
265         default:
266             break;
267     }
268 
269     return FALSE;
270 }
271