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