2 * PROJECT: ReactOS Console Configuration DLL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/cpl/console/console.c
5 * PURPOSE: Initialization
6 * PROGRAMMERS: Johannes Anderwald (johannes.anderwald@reactos.org)
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
15 INT_PTR CALLBACK
OptionsProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
16 INT_PTR CALLBACK
FontProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
17 INT_PTR CALLBACK
LayoutProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
18 INT_PTR CALLBACK
ColorsProc(HWND hwndDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
);
20 HINSTANCE hApplet
= NULL
;
22 /* Local copy of the console information */
23 PCONSOLE_STATE_INFO ConInfo
= NULL
;
24 /* What to do with the console information */
25 static BOOL SetConsoleInfo
= FALSE
;
26 static BOOL SaveConsoleInfo
= FALSE
;
29 InitPropSheetPage(PROPSHEETPAGEW
*psp
,
33 ZeroMemory(psp
, sizeof(*psp
));
34 psp
->dwSize
= sizeof(*psp
);
35 psp
->dwFlags
= PSP_DEFAULT
;
36 psp
->hInstance
= hApplet
;
37 psp
->pszTemplate
= MAKEINTRESOURCEW(idDlg
);
38 psp
->pfnDlgProc
= DlgProc
;
43 InitDefaultConsoleInfo(PCONSOLE_STATE_INFO pConInfo
)
45 // FIXME: Also retrieve the value of REG_DWORD CurrentPage.
46 ConCfgGetDefaultSettings(pConInfo
);
51 ApplyProc(HWND hwndDlg
,
56 UNREFERENCED_PARAMETER(lParam
);
62 CheckDlgButton(hwndDlg
, IDC_RADIO_APPLY_CURRENT
, BST_CHECKED
);
67 if (LOWORD(wParam
) == IDOK
)
69 if (IsDlgButtonChecked(hwndDlg
, IDC_RADIO_APPLY_CURRENT
) == BST_CHECKED
)
70 EndDialog(hwndDlg
, IDC_RADIO_APPLY_CURRENT
);
72 EndDialog(hwndDlg
, IDC_RADIO_APPLY_ALL
);
74 else if (LOWORD(wParam
) == IDCANCEL
)
76 EndDialog(hwndDlg
, IDCANCEL
);
88 ApplyConsoleInfo(HWND hwndDlg
)
90 static BOOL ConsoleInfoAlreadySaved
= FALSE
;
93 * We already applied all the console properties (and saved if needed).
94 * Nothing more needs to be done.
96 if (ConsoleInfoAlreadySaved
)
100 * If we are setting the default parameters, just save them,
101 * otherwise display the confirmation & apply dialog.
103 if (ConInfo
->hWnd
== NULL
)
105 SetConsoleInfo
= FALSE
;
106 SaveConsoleInfo
= TRUE
;
110 INT_PTR res
= DialogBoxW(hApplet
, MAKEINTRESOURCEW(IDD_APPLYOPTIONS
), hwndDlg
, ApplyProc
);
112 SetConsoleInfo
= (res
!= IDCANCEL
);
113 SaveConsoleInfo
= (res
== IDC_RADIO_APPLY_ALL
);
115 if (SetConsoleInfo
== FALSE
)
117 /* Don't destroy when the user presses cancel */
118 SetWindowLongPtr(hwndDlg
, DWLP_MSGRESULT
, PSNRET_INVALID_NOCHANGEPAGE
);
124 * We applied all the console properties (and saved if needed).
125 * Set the flag so that if this function is called again, we won't
126 * need to redo everything again.
128 ConsoleInfoAlreadySaved
= TRUE
;
131 /* Options have been applied */
132 SetWindowLongPtr(hwndDlg
, DWLP_MSGRESULT
, PSNRET_NOERROR
);
137 PropSheetProc(HWND hwndDlg
, UINT uMsg
, LPARAM lParam
)
139 // NOTE: This callback is needed to set large icon correctly.
143 case PSCB_INITIALIZED
:
145 hIcon
= LoadIconW(hApplet
, MAKEINTRESOURCEW(IDC_CPLICON
));
146 SendMessageW(hwndDlg
, WM_SETICON
, ICON_BIG
, (LPARAM
)hIcon
);
156 InitApplet(HANDLE hSectionOrWnd
)
159 PCONSOLE_STATE_INFO pSharedInfo
= NULL
;
160 WCHAR szTitle
[MAX_PATH
+ 1];
161 PROPSHEETPAGEW psp
[4];
162 PROPSHEETHEADERW psh
;
166 * Because of Windows compatibility, we need to behave the same concerning
167 * information sharing with CONSRV. For some obscure reason the designers
168 * decided to use the CPlApplet hWnd parameter as being either a handle to
169 * the applet's parent caller's window (in case we ask for displaying
170 * the global console settings), or a handle to a shared section holding
171 * a CONSOLE_STATE_INFO structure (they don't use the extra l/wParams).
175 * Try to open the shared section via the handle parameter. If we succeed,
176 * it means we were called by CONSRV for retrieving/setting parameters for
177 * a given console. If we fail, it means we are retrieving/setting default
178 * global parameters (and we were either called by CONSRV or directly by
179 * the user via the Control Panel, etc...)
181 pSharedInfo
= MapViewOfFile(hSectionOrWnd
, FILE_MAP_READ
, 0, 0, 0);
182 if (pSharedInfo
!= NULL
)
185 * We succeeded. We were called by CONSRV and are retrieving
186 * parameters for a given console.
189 /* Copy the shared data into our allocated buffer */
190 DPRINT1("pSharedInfo->cbSize == %lu ; sizeof(CONSOLE_STATE_INFO) == %u\n",
191 pSharedInfo
->cbSize
, sizeof(CONSOLE_STATE_INFO
));
192 ASSERT(pSharedInfo
->cbSize
>= sizeof(CONSOLE_STATE_INFO
));
194 /* Allocate a local buffer to hold console information */
195 ConInfo
= HeapAlloc(GetProcessHeap(),
197 pSharedInfo
->cbSize
);
199 RtlCopyMemory(ConInfo
, pSharedInfo
, pSharedInfo
->cbSize
);
201 /* Close the section */
202 UnmapViewOfFile(pSharedInfo
);
203 CloseHandle(hSectionOrWnd
);
205 if (!ConInfo
) return 0;
210 * We failed. We are retrieving the default global parameters.
213 /* Allocate a local buffer to hold console information */
214 ConInfo
= HeapAlloc(GetProcessHeap(),
216 sizeof(CONSOLE_STATE_INFO
));
217 if (!ConInfo
) return 0;
220 * Setting the console window handle to NULL indicates we are
221 * retrieving/setting the default console parameters.
223 ConInfo
->hWnd
= NULL
;
224 ConInfo
->ConsoleTitle
[0] = UNICODE_NULL
;
227 InitDefaultConsoleInfo(ConInfo
);
230 /* Initialize the font support */
231 hCurrentFont
= CreateConsoleFont(ConInfo
);
232 if (hCurrentFont
== NULL
)
233 DPRINT1("InitApplet: CreateConsoleFont failed\n");
235 /* Initialize the property sheet structure */
236 ZeroMemory(&psh
, sizeof(psh
));
237 psh
.dwSize
= sizeof(psh
);
238 psh
.dwFlags
= PSH_PROPSHEETPAGE
| PSH_PROPTITLE
| /* PSH_USEHICON */ PSH_USEICONID
| PSH_NOAPPLYNOW
| PSH_USECALLBACK
;
240 if (ConInfo
->ConsoleTitle
[0] != UNICODE_NULL
)
242 wcsncpy(szTitle
, L
"\"", MAX_PATH
);
243 wcsncat(szTitle
, ConInfo
->ConsoleTitle
, MAX_PATH
- wcslen(szTitle
));
244 wcsncat(szTitle
, L
"\"", MAX_PATH
- wcslen(szTitle
));
248 wcscpy(szTitle
, L
"ReactOS Console");
250 psh
.pszCaption
= szTitle
;
252 if (pSharedInfo
!= NULL
)
254 /* We were started from a console window: this is our parent (or ConInfo->hWnd is NULL) */
255 psh
.hwndParent
= ConInfo
->hWnd
;
259 /* We were started in another way (--> default parameters). Caller's window is our parent. */
260 psh
.hwndParent
= (HWND
)hSectionOrWnd
;
263 psh
.hInstance
= hApplet
;
264 // psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCEW(IDC_CPLICON));
265 psh
.pszIcon
= MAKEINTRESOURCEW(IDC_CPLICON
);
266 psh
.nPages
= ARRAYSIZE(psp
);
269 psh
.pfnCallback
= PropSheetProc
;
271 InitPropSheetPage(&psp
[i
++], IDD_PROPPAGEOPTIONS
, OptionsProc
);
272 InitPropSheetPage(&psp
[i
++], IDD_PROPPAGEFONT
, FontProc
);
273 InitPropSheetPage(&psp
[i
++], IDD_PROPPAGELAYOUT
, LayoutProc
);
274 InitPropSheetPage(&psp
[i
++], IDD_PROPPAGECOLORS
, ColorsProc
);
276 /* Display the property sheet */
277 RegisterWinPrevClass(hApplet
);
278 Result
= PropertySheetW(&psh
);
279 UnRegisterWinPrevClass(hApplet
);
282 if (hCurrentFont
) DeleteObject(hCurrentFont
);
285 /* Save the console settings */
291 * Create a memory section to share with CONSRV, and map it.
293 hSection
= CreateFileMappingW(INVALID_HANDLE_VALUE
,
301 DPRINT1("Error when creating file mapping, error = %d\n", GetLastError());
305 pSharedInfo
= MapViewOfFile(hSection
, FILE_MAP_ALL_ACCESS
, 0, 0, 0);
308 DPRINT1("Error when mapping view of file, error = %d\n", GetLastError());
309 CloseHandle(hSection
);
313 /* Copy the console information into the section */
314 RtlCopyMemory(pSharedInfo
, ConInfo
, ConInfo
->cbSize
);
317 UnmapViewOfFile(pSharedInfo
);
319 /* Signal to CONSRV that it can apply the new configuration */
320 SendMessageW(ConInfo
->hWnd
, WM_SETCONSOLEINFO
, (WPARAM
)hSection
, 0);
322 /* Close the section and return */
323 CloseHandle(hSection
);
328 /* Default settings saved when ConInfo->hWnd == NULL */
329 ConCfgWriteUserSettings(ConInfo
, ConInfo
->hWnd
== NULL
);
334 HeapFree(GetProcessHeap(), 0, ConInfo
);
337 return (Result
!= -1);
340 /* Control Panel Callback */
343 CPlApplet(HWND hwndCPl
,
354 // TODO: Free allocated memory
362 CPLINFO
*CPlInfo
= (CPLINFO
*)lParam2
;
363 CPlInfo
->idIcon
= IDC_CPLICON
;
364 CPlInfo
->idName
= IDS_CPLNAME
;
365 CPlInfo
->idInfo
= IDS_CPLDESCRIPTION
;
370 InitApplet((HANDLE
)hwndCPl
);
379 DllMain(HINSTANCE hinstDLL
,
383 UNREFERENCED_PARAMETER(lpvReserved
);
387 case DLL_PROCESS_ATTACH
:
389 DisableThreadLibraryCalls(hinstDLL
);