Sync with trunk r43123
[reactos.git] / reactos / dll / win32 / shell32 / control.c
1 /* Control Panel management
2 *
3 * Copyright 2001 Eric Pouech
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20
21 #include <precomp.h>
22
23 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
24
25 CPlApplet* Control_UnloadApplet(CPlApplet* applet)
26 {
27 unsigned i;
28 CPlApplet* next;
29
30 for (i = 0; i < applet->count; i++) {
31 if (!applet->info[i].dwSize) continue;
32 applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
33 }
34 if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
35 FreeLibrary(applet->hModule);
36 next = applet->next;
37 HeapFree(GetProcessHeap(), 0, applet);
38 return next;
39 }
40
41 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
42 {
43 CPlApplet* applet;
44 unsigned i;
45 CPLINFO info;
46 NEWCPLINFOW newinfo;
47
48 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
49 return applet;
50
51 applet->hWnd = hWnd;
52
53 if (!(applet->hModule = LoadLibraryW(cmd))) {
54 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
55 goto theError;
56 }
57 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
58 WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
59 goto theError;
60 }
61 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
62 WARN("Init of applet has failed\n");
63 goto theError;
64 }
65 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
66 WARN("No subprogram in applet\n");
67 goto theError;
68 }
69
70 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
71 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
72
73 for (i = 0; i < applet->count; i++) {
74 ZeroMemory(&newinfo, sizeof(newinfo));
75 newinfo.dwSize = sizeof(NEWCPLINFOW);
76 applet->info[i].dwSize = sizeof(NEWCPLINFOW);
77 /* proc is supposed to return a null value upon success for
78 * CPL_INQUIRE and CPL_NEWINQUIRE
79 * However, real drivers don't seem to behave like this
80 * So, use introspection rather than return value
81 */
82 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
83 if (newinfo.hIcon == 0) {
84 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
85 if (info.idIcon == 0 || info.idName == 0) {
86 WARN("Couldn't get info from sp %u\n", i);
87 applet->info[i].dwSize = 0;
88 } else {
89 /* convert the old data into the new structure */
90 applet->info[i].dwFlags = 0;
91 applet->info[i].dwHelpContext = 0;
92 applet->info[i].lData = info.lData;
93 applet->info[i].hIcon = LoadIconW(applet->hModule,
94 MAKEINTRESOURCEW(info.idIcon));
95 LoadStringW(applet->hModule, info.idName,
96 applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
97 LoadStringW(applet->hModule, info.idInfo,
98 applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
99 applet->info[i].szHelpFile[0] = '\0';
100 }
101 }
102 else
103 {
104 CopyMemory(&applet->info[i], &newinfo, newinfo.dwSize);
105 if (newinfo.dwSize != sizeof(NEWCPLINFOW))
106 {
107 applet->info[i].dwSize = sizeof(NEWCPLINFOW);
108 lstrcpyW(applet->info[i].szName, newinfo.szName);
109 lstrcpyW(applet->info[i].szInfo, newinfo.szInfo);
110 lstrcpyW(applet->info[i].szHelpFile, newinfo.szHelpFile);
111 }
112 }
113 }
114
115 applet->next = panel->first;
116 panel->first = applet;
117
118 return applet;
119
120 theError:
121 Control_UnloadApplet(applet);
122 return NULL;
123 }
124
125 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
126 {
127 CPanel* panel = (CPanel*)cs->lpCreateParams;
128
129 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
130 panel->status = 0;
131 panel->hWnd = hWnd;
132 }
133
134 #define XICON 32
135 #define XSTEP 128
136 #define YICON 32
137 #define YSTEP 64
138
139 static BOOL Control_Localize(const CPanel* panel, int cx, int cy,
140 CPlApplet** papplet, unsigned* psp)
141 {
142 unsigned i, x = (XSTEP-XICON)/2, y = 0;
143 CPlApplet* applet;
144 RECT rc;
145
146 GetClientRect(panel->hWnd, &rc);
147 for (applet = panel->first; applet; applet = applet->next) {
148 for (i = 0; i < applet->count; i++) {
149 if (!applet->info[i].dwSize) continue;
150 if (x + XSTEP >= rc.right - rc.left) {
151 x = (XSTEP-XICON)/2;
152 y += YSTEP;
153 }
154 if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
155 *papplet = applet;
156 *psp = i;
157 return TRUE;
158 }
159 x += XSTEP;
160 }
161 }
162 return FALSE;
163 }
164
165 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
166 {
167 HDC hdc;
168 PAINTSTRUCT ps;
169 RECT rc, txtRect;
170 unsigned i, x = 0, y = 0;
171 CPlApplet* applet;
172 HGDIOBJ hOldFont;
173
174 hdc = (wParam) ? (HDC)wParam : BeginPaint(panel->hWnd, &ps);
175 hOldFont = SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
176 GetClientRect(panel->hWnd, &rc);
177 for (applet = panel->first; applet; applet = applet->next) {
178 for (i = 0; i < applet->count; i++) {
179 if (x + XSTEP >= rc.right - rc.left) {
180 x = 0;
181 y += YSTEP;
182 }
183 if (!applet->info[i].dwSize) continue;
184 DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
185 txtRect.left = x;
186 txtRect.right = x + XSTEP;
187 txtRect.top = y + YICON;
188 txtRect.bottom = y + YSTEP;
189 DrawTextW(hdc, applet->info[i].szName, -1, &txtRect,
190 DT_CENTER | DT_VCENTER);
191 x += XSTEP;
192 }
193 }
194 SelectObject(hdc, hOldFont);
195 if (!wParam) EndPaint(panel->hWnd, &ps);
196 return 0;
197 }
198
199 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
200 {
201 unsigned i;
202 CPlApplet* applet;
203
204 if (Control_Localize(panel, (short)LOWORD(lParam), (short)HIWORD(lParam), &applet, &i)) {
205 if (up) {
206 if (panel->clkApplet == applet && panel->clkSP == i) {
207 applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
208 }
209 } else {
210 panel->clkApplet = applet;
211 panel->clkSP = i;
212 }
213 }
214 return 0;
215 }
216
217 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
218 WPARAM lParam1, LPARAM lParam2)
219 {
220 CPanel* panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
221
222 if (panel || wMsg == WM_CREATE) {
223 switch (wMsg) {
224 case WM_CREATE:
225 Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
226 return 0;
227 case WM_DESTROY:
228 {
229 CPlApplet* applet = panel->first;
230 while (applet)
231 applet = Control_UnloadApplet(applet);
232 }
233 PostQuitMessage(0);
234 break;
235 case WM_PAINT:
236 return Control_WndProc_Paint(panel, lParam1);
237 case WM_LBUTTONUP:
238 return Control_WndProc_LButton(panel, lParam2, TRUE);
239 case WM_LBUTTONDOWN:
240 return Control_WndProc_LButton(panel, lParam2, FALSE);
241 /* EPP case WM_COMMAND: */
242 /* EPP return Control_WndProc_Command(mwi, lParam1, lParam2); */
243 }
244 }
245
246 return DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
247 }
248
249 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
250 {
251 WNDCLASSW wc;
252 MSG msg;
253 const WCHAR* appName = L"ReactOS Control Panel";
254 wc.style = CS_HREDRAW|CS_VREDRAW;
255 wc.lpfnWndProc = Control_WndProc;
256 wc.cbClsExtra = 0;
257 wc.cbWndExtra = sizeof(CPlApplet*);
258 wc.hInstance = hInst;
259 wc.hIcon = 0;
260 wc.hCursor = 0;
261 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
262 wc.lpszMenuName = NULL;
263 wc.lpszClassName = L"Shell_Control_WndClass";
264
265 if (!RegisterClassW(&wc)) return;
266
267 CreateWindowExW(0, wc.lpszClassName, appName,
268 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
269 CW_USEDEFAULT, CW_USEDEFAULT,
270 CW_USEDEFAULT, CW_USEDEFAULT,
271 hWnd, NULL, hInst, panel);
272 if (!panel->hWnd) return;
273
274 if (!panel->first) {
275 /* FIXME appName & message should be localized */
276 MessageBoxW(panel->hWnd, L"Cannot load any applets", appName, MB_OK);
277 return;
278 }
279
280 while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
281 TranslateMessage(&msg);
282 DispatchMessageW(&msg);
283 }
284 }
285
286 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
287 {
288 HANDLE h;
289 WIN32_FIND_DATAW fd;
290 WCHAR buffer[MAX_PATH];
291 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
292 WCHAR *p;
293
294 GetSystemDirectoryW( buffer, MAX_PATH );
295 p = buffer + wcslen(buffer);
296 *p++ = '\\';
297 wcscpy(p, wszAllCpl);
298
299 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
300 do {
301 wcscpy(p, fd.cFileName);
302 Control_LoadApplet(hWnd, buffer, panel);
303 } while (FindNextFileW(h, &fd));
304 FindClose(h);
305 }
306
307 Control_DoInterface(panel, hWnd, hInst);
308 }
309
310 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
311 /* forms to parse:
312 * foo.cpl,@sp,str
313 * foo.cpl,@sp
314 * foo.cpl,,str
315 * foo.cpl @sp
316 * foo.cpl str
317 * "a path\foo.cpl"
318 */
319 {
320 LPWSTR buffer;
321 LPWSTR beg = NULL;
322 LPWSTR end;
323 WCHAR ch;
324 LPCWSTR ptr, ptr2;
325 WCHAR szName[MAX_PATH];
326 unsigned sp = 0;
327 LPWSTR extraPmts = NULL;
328 int quoted = 0;
329 BOOL spSet = FALSE;
330 HANDLE hMutex;
331 UINT Length;
332
333 ptr = wcsrchr(wszCmd, L'\\');
334 ptr2 = wcsrchr(wszCmd, L',');
335 if (!ptr2)
336 {
337 ptr2 = wszCmd + wcslen(wszCmd) + 1;
338 }
339
340 if (ptr)
341 ptr++;
342 else
343 ptr = wszCmd;
344
345 Length = (ptr2 - ptr);
346 if (Length >= MAX_PATH)
347 return;
348
349 memcpy(szName, (LPVOID)ptr, Length * sizeof(WCHAR));
350 szName[Length] = L'\0';
351 hMutex = CreateMutexW(NULL, TRUE, szName);
352
353 if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
354 return;
355 buffer = HeapAlloc(GetProcessHeap(), 0, (wcslen(wszCmd) + 1) * sizeof(*wszCmd));
356 if (!buffer)
357 {
358 CloseHandle(hMutex);
359 return;
360 }
361 end = wcscpy(buffer, wszCmd);
362 for (;;) {
363 ch = *end;
364 if (ch == '"') quoted = !quoted;
365 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
366 *end = '\0';
367 if (beg) {
368 if (*beg == '@') {
369 sp = atoiW(beg + 1);
370 spSet = TRUE;
371 } else if (*beg == '\0') {
372 sp = 0;
373 spSet = TRUE;
374 } else {
375 extraPmts = beg;
376 }
377 }
378 if (ch == '\0') break;
379 beg = end + 1;
380 if (ch == ' ') while (end[1] == ' ') end++;
381 }
382 end++;
383 }
384 while ((ptr = StrChrW(buffer, '"')))
385 memmove((LPVOID)ptr, ptr+1, wcslen(ptr)*sizeof(WCHAR));
386
387 while ((ptr = StrChrW(extraPmts, '"')))
388 memmove((LPVOID)ptr, ptr+1, wcslen(ptr)*sizeof(WCHAR));
389
390 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
391
392 Control_LoadApplet(hWnd, buffer, panel);
393
394 if (panel->first) {
395 CPlApplet* applet = panel->first;
396
397 assert(applet && applet->next == NULL);
398 if (sp >= applet->count) {
399 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
400 sp = 0;
401 }
402
403 if ((extraPmts) && extraPmts[0] &&(!spSet))
404 {
405 while ((lstrcmpiW(extraPmts, applet->info[sp].szName)) && (sp < applet->count))
406 sp++;
407
408 if (sp >= applet->count)
409 {
410 ReleaseMutex(hMutex);
411 CloseHandle(hMutex);
412 Control_UnloadApplet(applet);
413 HeapFree(GetProcessHeap(), 0, buffer);
414 return;
415 }
416 }
417 if (applet->info[sp].dwSize) {
418 if (!applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData))
419 applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts);
420 }
421 Control_UnloadApplet(applet);
422 }
423 ReleaseMutex(hMutex);
424 CloseHandle(hMutex);
425 HeapFree(GetProcessHeap(), 0, buffer);
426 }
427
428 /*************************************************************************
429 * Control_RunDLLW [SHELL32.@]
430 *
431 */
432 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
433 {
434 CPanel panel;
435
436 TRACE("(%p, %p, %s, 0x%08x)\n",
437 hWnd, hInst, debugstr_w(cmd), nCmdShow);
438
439 memset(&panel, 0, sizeof(panel));
440
441 if (!cmd || !*cmd) {
442 Control_DoWindow(&panel, hWnd, hInst);
443 } else {
444 Control_DoLaunch(&panel, hWnd, cmd);
445 }
446 }
447
448 /*************************************************************************
449 * Control_RunDLLA [SHELL32.@]
450 *
451 */
452 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
453 {
454 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
455 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
456 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
457 {
458 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
459 }
460 HeapFree(GetProcessHeap(), 0, wszCmd);
461 }
462
463 /*************************************************************************
464 * Control_FillCache_RunDLLW [SHELL32.@]
465 *
466 */
467 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
468 {
469 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
470 return 0;
471 }
472
473 /*************************************************************************
474 * Control_FillCache_RunDLLA [SHELL32.@]
475 *
476 */
477 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
478 {
479 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
480 }
481
482
483 /*************************************************************************
484 * RunDLL_CallEntry16 [SHELL32.122]
485 * the name is probably wrong
486 */
487 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
488 LPCSTR cmdline, INT cmdshow )
489 {
490 #if !defined(__CYGWIN__) && !defined (__MINGW32__) && !defined(_MSC_VER)
491 WORD args[5];
492 SEGPTR cmdline_seg;
493
494 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
495 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
496
497 cmdline_seg = MapLS( cmdline );
498 args[4] = HWND_16(hwnd);
499 args[3] = MapHModuleLS(inst);
500 args[2] = SELECTOROF(cmdline_seg);
501 args[1] = OFFSETOF(cmdline_seg);
502 args[0] = cmdshow;
503 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
504 UnMapLS( cmdline_seg );
505 #else
506 FIXME( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
507 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
508 #endif
509 }
510
511 /*************************************************************************
512 * CallCPLEntry16 [SHELL32.166]
513 *
514 * called by desk.cpl on "Advanced" with:
515 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
516 *
517 */
518 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
519 {
520 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
521 return 0x0deadbee;
522 }