More build fixes.
[reactos.git] / reactos / dll / cpl / desk / desk.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Display Control Panel
4 * FILE: dll/cpl/desk/desk.c
5 * PURPOSE: ReactOS Display Control Panel
6 *
7 * PROGRAMMERS: Trevor McCort (lycan359@gmail.com)
8 */
9
10 #include "desk.h"
11 #include <shellapi.h>
12 #include <cplext.h>
13 #include <debug.h>
14
15 #define NUM_APPLETS (1)
16
17 static LONG APIENTRY DisplayApplet(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam);
18
19 INT_PTR CALLBACK BackgroundPageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
20 INT_PTR CALLBACK ScreenSaverPageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
21 INT_PTR CALLBACK AppearancePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
22 INT_PTR CALLBACK SettingsPageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
23 UINT CALLBACK SettingsPageCallbackProc(HWND hwnd, UINT uMsg, LPPROPSHEETPAGE ppsp);
24
25 HINSTANCE hApplet = 0;
26 HWND hCPLWindow;
27
28 /* Applets */
29 APPLET Applets[NUM_APPLETS] =
30 {
31 {
32 IDC_DESK_ICON,
33 IDS_CPLNAME,
34 IDS_CPLDESCRIPTION,
35 DisplayApplet
36 }
37 };
38
39 HMENU
40 LoadPopupMenu(IN HINSTANCE hInstance,
41 IN LPCTSTR lpMenuName)
42 {
43 HMENU hMenu, hSubMenu = NULL;
44
45 hMenu = LoadMenu(hInstance,
46 lpMenuName);
47
48 if (hMenu != NULL)
49 {
50 hSubMenu = GetSubMenu(hMenu,
51 0);
52 if (hSubMenu != NULL &&
53 !RemoveMenu(hMenu,
54 0,
55 MF_BYPOSITION))
56 {
57 hSubMenu = NULL;
58 }
59
60 DestroyMenu(hMenu);
61 }
62
63 return hSubMenu;
64 }
65
66 static BOOL CALLBACK
67 PropSheetAddPage(HPROPSHEETPAGE hpage, LPARAM lParam)
68 {
69 PROPSHEETHEADER *ppsh = (PROPSHEETHEADER *)lParam;
70 if (ppsh != NULL && ppsh->nPages < MAX_DESK_PAGES)
71 {
72 ppsh->phpage[ppsh->nPages++] = hpage;
73 return TRUE;
74 }
75
76 return FALSE;
77 }
78
79 static BOOL
80 InitPropSheetPage(PROPSHEETHEADER *ppsh, WORD idDlg, DLGPROC DlgProc, LPFNPSPCALLBACK pfnCallback)
81 {
82 HPROPSHEETPAGE hPage;
83 PROPSHEETPAGE psp;
84
85 if (ppsh->nPages < MAX_DESK_PAGES)
86 {
87 ZeroMemory(&psp, sizeof(psp));
88 psp.dwSize = sizeof(psp);
89 psp.dwFlags = PSP_DEFAULT;
90 if (pfnCallback != NULL)
91 psp.dwFlags |= PSP_USECALLBACK;
92 psp.hInstance = hApplet;
93 psp.pszTemplate = MAKEINTRESOURCE(idDlg);
94 psp.pfnDlgProc = DlgProc;
95 psp.pfnCallback = pfnCallback;
96
97 hPage = CreatePropertySheetPage(&psp);
98 if (hPage != NULL)
99 {
100 return PropSheetAddPage(hPage, (LPARAM)ppsh);
101 }
102 }
103
104 return FALSE;
105 }
106
107 static const struct
108 {
109 WORD idDlg;
110 DLGPROC DlgProc;
111 LPFNPSPCALLBACK Callback;
112 LPWSTR Name;
113 } PropPages[] =
114 {
115 { IDD_BACKGROUND, BackgroundPageProc, NULL, L"Desktop" },
116 { IDD_SCREENSAVER, ScreenSaverPageProc, NULL, L"Screen Saver" },
117 { IDD_APPEARANCE, AppearancePageProc, NULL, L"Appearance" },
118 { IDD_SETTINGS, SettingsPageProc, SettingsPageCallbackProc, L"Settings" },
119 };
120
121 /* Display Applet */
122 static LONG APIENTRY
123 DisplayApplet(HWND hwnd, UINT uMsg, LPARAM wParam, LPARAM lParam)
124 {
125 HPROPSHEETPAGE hpsp[MAX_DESK_PAGES];
126 PROPSHEETHEADER psh;
127 HPSXA hpsxa;
128 TCHAR Caption[1024];
129 UINT i;
130 LPWSTR *argv = NULL;
131 LPCWSTR pwszSelectedTab = NULL;
132 LPCWSTR pwszFile = NULL;
133 LPCWSTR pwszAction = NULL;
134
135 UNREFERENCED_PARAMETER(wParam);
136
137 hCPLWindow = hwnd;
138
139 if (uMsg == CPL_STARTWPARMSW && lParam)
140 {
141 int argc;
142 int i;
143 LPCWSTR pszCommandLine = (LPCWSTR)lParam;
144
145 argv = CommandLineToArgvW(pszCommandLine, &argc);
146
147 if (argv && argc)
148 {
149 for (i = 0; i<argc; i++)
150 {
151 if (argv[i][0] == L'@')
152 pwszSelectedTab = &argv[i][1];
153 else if (wcsncmp(argv[i], L"/Action:", 8) == 0)
154 pwszAction = &argv[i][8];
155 else if (wcsncmp(argv[i], L"/file:", 6) == 0)
156 pwszFile = &argv[i][6];
157 }
158 }
159
160 /* HACK: shell32 doesn't give the correct params to CPL_STARTWPARMSW so we need to ... improvise */
161 if (wcsncmp(pszCommandLine, L"/file:", 6) == 0)
162 {
163 LPCWSTR pwszType = wcsrchr(pszCommandLine, L'.');
164 if (pwszType && wcsicmp(pwszType, L".msstyles") == 0)
165 {
166 pwszFile = &pszCommandLine[6];
167 pwszSelectedTab = L"Appearance";
168 pwszAction = L"OpenMSTheme";
169 }
170 }
171 }
172
173 g_GlobalData.pwszFile = pwszFile;
174 g_GlobalData.pwszAction = pwszAction;
175 g_GlobalData.desktop_color = GetSysColor(COLOR_DESKTOP);
176
177 LoadString(hApplet, IDS_CPLNAME, Caption, sizeof(Caption) / sizeof(TCHAR));
178
179 ZeroMemory(&psh, sizeof(PROPSHEETHEADER));
180 psh.dwSize = sizeof(PROPSHEETHEADER);
181 psh.dwFlags = PSH_USECALLBACK | PSH_PROPTITLE;
182 psh.hwndParent = hCPLWindow;
183 psh.hInstance = hApplet;
184 psh.hIcon = LoadIcon(hApplet, MAKEINTRESOURCE(IDC_DESK_ICON));
185 psh.pszCaption = Caption;
186 psh.nPages = 0;
187 psh.nStartPage = 0;
188 psh.phpage = hpsp;
189
190 /* Allow shell extensions to replace the background page */
191 hpsxa = SHCreatePropSheetExtArray(HKEY_LOCAL_MACHINE, REGSTR_PATH_CONTROLSFOLDER TEXT("\\Desk"), MAX_DESK_PAGES - psh.nPages);
192
193 for (i = 0; i != sizeof(PropPages) / sizeof(PropPages[0]); i++)
194 {
195 if (pwszSelectedTab && wcsicmp(pwszSelectedTab, PropPages[i].Name) == 0)
196 psh.nStartPage = i;
197
198 /* Override the background page if requested by a shell extension */
199 if (PropPages[i].idDlg == IDD_BACKGROUND && hpsxa != NULL &&
200 SHReplaceFromPropSheetExtArray(hpsxa, CPLPAGE_DISPLAY_BACKGROUND, PropSheetAddPage, (LPARAM)&psh) != 0)
201 {
202 /* The shell extension added one or more pages to replace the background page.
203 Don't create the built-in page anymore! */
204 continue;
205 }
206
207 InitPropSheetPage(&psh, PropPages[i].idDlg, PropPages[i].DlgProc, PropPages[i].Callback);
208 }
209
210 /* NOTE: Don't call SHAddFromPropSheetExtArray here because this applet only allows
211 replacing the background page but not extending the applet by more pages */
212
213 PropertySheet(&psh);
214
215 if (hpsxa != NULL)
216 SHDestroyPropSheetExtArray(hpsxa);
217
218 if (argv)
219 LocalFree(argv);
220
221 return TRUE;
222 }
223
224
225 /* Control Panel Callback */
226 LONG CALLBACK
227 CPlApplet(HWND hwndCPl, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
228 {
229 int i = (int)lParam1;
230
231 switch (uMsg)
232 {
233 case CPL_INIT:
234 return TRUE;
235
236 case CPL_GETCOUNT:
237 return NUM_APPLETS;
238
239 case CPL_INQUIRE:
240 {
241 CPLINFO *CPlInfo = (CPLINFO*)lParam2;
242 CPlInfo->lData = 0;
243 CPlInfo->idIcon = Applets[i].idIcon;
244 CPlInfo->idName = Applets[i].idName;
245 CPlInfo->idInfo = Applets[i].idDescription;
246 }
247 break;
248
249 case CPL_DBLCLK:
250 Applets[i].AppletProc(hwndCPl, uMsg, lParam1, lParam2);
251 break;
252 case CPL_STARTWPARMSW:
253 return Applets[i].AppletProc(hwndCPl, uMsg, lParam1, lParam2);
254 }
255
256 return FALSE;
257 }
258
259 void
260 WINAPI
261 InstallScreenSaverW(
262 IN HWND hWindow,
263 IN HANDLE hInstance,
264 IN LPCWSTR pszFile,
265 IN UINT nCmdShow)
266 {
267 WCHAR pszSystemDir[MAX_PATH];
268 WCHAR pszDrive[3];
269 WCHAR pszPath[MAX_PATH];
270 WCHAR pszFilename[MAX_PATH];
271 WCHAR pszExt[MAX_PATH];
272 LPWSTR pszOutName;
273 UINT uCompressionType=FILE_COMPRESSION_NONE;
274 DWORD dwSourceSize;
275 DWORD dwTargetSize;
276 DWORD rc;
277
278 if (!pszFile)
279 {
280 DPRINT("InstallScreenSaver() null file\n");
281 SetLastError(ERROR_INVALID_PARAMETER);
282 return;
283 }
284 DPRINT("InstallScreenSaver() Installing screensaver %ls\n", pszFile);
285
286 rc = SetupGetFileCompressionInfoW(pszFile, &pszOutName, &dwSourceSize, &dwTargetSize, &uCompressionType);
287 if (ERROR_SUCCESS != rc)
288 {
289 DPRINT("InstallScreenSaver() SetupGetFileCompressionInfo failed with error 0x%lx\n", rc);
290 SetLastError(rc);
291 return;
292 }
293 if (!GetSystemDirectoryW((LPWSTR)pszSystemDir, sizeof(pszSystemDir)/sizeof(WCHAR)))
294 {
295 MyFree(pszOutName);
296 DPRINT("InstallScreenSaver() GetSystemDirectory failed with error 0x%lx\n", GetLastError());
297 return;
298 }
299 _wsplitpath(pszOutName, pszDrive, pszPath, pszFilename, pszExt);
300 MyFree(pszOutName);
301 StringCbCatW(pszSystemDir, sizeof(pszSystemDir), L"\\");
302 StringCbCatW(pszSystemDir, sizeof(pszSystemDir), pszFilename);
303 StringCbCatW(pszSystemDir, sizeof(pszSystemDir), pszExt);
304 rc = SetupDecompressOrCopyFileW(pszFile, pszSystemDir, &uCompressionType);
305 DPRINT("InstallScreenSaver() Copying to %ls, compression type %d return 0x%lx\n", pszFile, uCompressionType, rc);
306 }
307
308 void
309 WINAPI
310 InstallScreenSaverA(
311 IN HWND hWindow,
312 IN HANDLE hInstance,
313 IN LPCSTR pszFile,
314 IN UINT nCmdShow)
315 {
316 LPWSTR lpwString;
317
318 if (!pszFile)
319 {
320 DPRINT("InstallScreenSaver() null file\n");
321 SetLastError(ERROR_INVALID_PARAMETER);
322 return;
323 }
324 DPRINT("InstallScreenSaver() Install from file %s\n", pszFile);
325 lpwString = pSetupMultiByteToUnicode(pszFile, 0);
326 if (!lpwString)
327 {
328 DPRINT("InstallScreenSaver() not enough memory to convert string to unicode\n");
329 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
330 return;
331 }
332 InstallScreenSaverW(hWindow, hInstance, lpwString, nCmdShow);
333 MyFree(lpwString);
334 }
335
336 BOOL WINAPI
337 DllMain(HINSTANCE hInstDLL, DWORD dwReason, LPVOID lpvReserved)
338 {
339 UNREFERENCED_PARAMETER(lpvReserved);
340
341 switch (dwReason)
342 {
343 case DLL_PROCESS_ATTACH:
344 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
345 RegisterPreviewControl(hInstDLL);
346 // case DLL_THREAD_ATTACH:
347 hApplet = hInstDLL;
348 break;
349
350 case DLL_PROCESS_DETACH:
351 UnregisterPreviewControl(hInstDLL);
352 CoUninitialize();
353 break;
354 }
355
356 return TRUE;
357 }