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