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