1 /* Control Panel management
3 * Copyright 2001 Eric Pouech
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.
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.
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
31 #include "wine/winbase16.h"
33 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 #define NO_SHLWAPI_REG
42 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
44 CPlApplet* Control_UnloadApplet(CPlApplet* applet)
49 for (i = 0; i < applet->count; i++) {
50 if (!applet->info[i].dwSize) continue;
51 applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
53 if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
54 FreeLibrary(applet->hModule);
56 HeapFree(GetProcessHeap(), 0, applet);
60 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
67 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
72 if (!(applet->hModule = LoadLibraryW(cmd))) {
73 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
76 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
77 WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
80 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
81 WARN("Init of applet has failed\n");
84 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
85 WARN("No subprogram in applet\n");
89 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
90 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
92 for (i = 0; i < applet->count; i++) {
93 ZeroMemory(&newinfo, sizeof(newinfo));
94 newinfo.dwSize = sizeof(NEWCPLINFOA);
95 applet->info[i].dwSize = sizeof(NEWCPLINFOW);
96 applet->info[i].dwFlags = 0;
97 applet->info[i].dwHelpContext = 0;
98 applet->info[i].szHelpFile[0] = '\0';
99 /* proc is supposed to return a null value upon success for
100 * CPL_INQUIRE and CPL_NEWINQUIRE
101 * However, real drivers don't seem to behave like this
102 * So, use introspection rather than return value
104 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
105 applet->info[i].lData = info.lData;
106 if (info.idIcon != CPL_DYNAMIC_RES)
107 applet->info[i].hIcon = LoadIconW(applet->hModule,
108 MAKEINTRESOURCEW(info.idIcon));
109 if (info.idName != CPL_DYNAMIC_RES)
110 LoadStringW(applet->hModule, info.idName,
111 applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
112 if (info.idInfo != CPL_DYNAMIC_RES)
113 LoadStringW(applet->hModule, info.idInfo,
114 applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
116 if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
117 (info.idInfo == CPL_DYNAMIC_RES)) {
118 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
120 applet->info[i].dwFlags = newinfo.dwFlags;
121 applet->info[i].dwHelpContext = newinfo.dwHelpContext;
122 applet->info[i].lData = newinfo.lData;
123 if (info.idIcon == CPL_DYNAMIC_RES) {
124 if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
125 applet->info[i].hIcon = newinfo.hIcon;
127 if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
128 if (info.idName == CPL_DYNAMIC_RES)
129 memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
130 if (info.idInfo == CPL_DYNAMIC_RES)
131 memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
132 memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
134 if (info.idName == CPL_DYNAMIC_RES)
135 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
136 sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
137 applet->info[i].szName,
138 sizeof(applet->info[i].szName) / sizeof(WCHAR));
139 if (info.idInfo == CPL_DYNAMIC_RES)
140 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
141 sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
142 applet->info[i].szInfo,
143 sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
144 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
145 sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
146 applet->info[i].szHelpFile,
147 sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
152 applet->next = panel->first;
153 panel->first = applet;
158 Control_UnloadApplet(applet);
162 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
164 CPanel* panel = (CPanel*)cs->lpCreateParams;
166 SetWindowLongPtrA(hWnd, 0, (LONG_PTR)panel);
176 static BOOL Control_Localize(const CPanel* panel, int cx, int cy,
177 CPlApplet** papplet, unsigned* psp)
179 unsigned i, x = (XSTEP-XICON)/2, y = 0;
183 GetClientRect(panel->hWnd, &rc);
184 for (applet = panel->first; applet; applet = applet->next) {
185 for (i = 0; i < applet->count; i++) {
186 if (!applet->info[i].dwSize) continue;
187 if (x + XSTEP >= rc.right - rc.left) {
191 if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
202 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
207 unsigned i, x = 0, y = 0;
211 hdc = (wParam) ? (HDC)wParam : BeginPaint(panel->hWnd, &ps);
212 hOldFont = SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
213 GetClientRect(panel->hWnd, &rc);
214 for (applet = panel->first; applet; applet = applet->next) {
215 for (i = 0; i < applet->count; i++) {
216 if (x + XSTEP >= rc.right - rc.left) {
220 if (!applet->info[i].dwSize) continue;
221 DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
223 txtRect.right = x + XSTEP;
224 txtRect.top = y + YICON;
225 txtRect.bottom = y + YSTEP;
226 DrawTextW(hdc, applet->info[i].szName, -1, &txtRect,
227 DT_CENTER | DT_VCENTER);
231 SelectObject(hdc, hOldFont);
232 if (!wParam) EndPaint(panel->hWnd, &ps);
236 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
241 if (Control_Localize(panel, (short)LOWORD(lParam), (short)HIWORD(lParam), &applet, &i)) {
243 if (panel->clkApplet == applet && panel->clkSP == i) {
244 applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
247 panel->clkApplet = applet;
254 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
255 WPARAM lParam1, LPARAM lParam2)
257 CPanel* panel = (CPanel*)GetWindowLongPtrA(hWnd, 0);
259 if (panel || wMsg == WM_CREATE) {
262 Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
266 CPlApplet* applet = panel->first;
268 applet = Control_UnloadApplet(applet);
273 return Control_WndProc_Paint(panel, lParam1);
275 return Control_WndProc_LButton(panel, lParam2, TRUE);
277 return Control_WndProc_LButton(panel, lParam2, FALSE);
278 /* EPP case WM_COMMAND: */
279 /* EPP return Control_WndProc_Command(mwi, lParam1, lParam2); */
283 return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
286 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
290 const CHAR* appName = "Wine Control Panel";
291 wc.style = CS_HREDRAW|CS_VREDRAW;
292 wc.lpfnWndProc = Control_WndProc;
294 wc.cbWndExtra = sizeof(CPlApplet*);
295 wc.hInstance = hInst;
298 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
299 wc.lpszMenuName = NULL;
300 wc.lpszClassName = "Shell_Control_WndClass";
302 if (!RegisterClassA(&wc)) return;
304 CreateWindowExA(0, wc.lpszClassName, appName,
305 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
306 CW_USEDEFAULT, CW_USEDEFAULT,
307 CW_USEDEFAULT, CW_USEDEFAULT,
308 hWnd, NULL, hInst, panel);
309 if (!panel->hWnd) return;
312 /* FIXME appName & message should be localized */
313 MessageBoxA(panel->hWnd, "Cannot load any applets", appName, MB_OK);
317 while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
318 TranslateMessage(&msg);
319 DispatchMessageA(&msg);
323 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
325 WCHAR name[MAX_PATH];
326 WCHAR value[MAX_PATH];
329 if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
335 DWORD nameLen = MAX_PATH;
336 DWORD valueLen = MAX_PATH;
338 if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)&value, &valueLen) != ERROR_SUCCESS)
341 Control_LoadApplet(hWnd, value, panel);
347 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
351 WCHAR buffer[MAX_PATH];
352 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
353 static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
354 '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
355 '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
358 /* first add .cpl files in the system directory */
359 GetSystemDirectoryW( buffer, MAX_PATH );
360 p = buffer + strlenW(buffer);
362 lstrcpyW(p, wszAllCpl);
364 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
366 lstrcpyW(p, fd.cFileName);
367 Control_LoadApplet(hWnd, buffer, panel);
368 } while (FindNextFileW(h, &fd));
372 /* now check for cpls in the registry */
373 Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
374 Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
376 Control_DoInterface(panel, hWnd, hInst);
379 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
395 LPWSTR extraPmtsBuf = NULL;
396 LPWSTR extraPmts = NULL;
399 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
402 end = lstrcpyW(buffer, wszCmd);
406 if (ch == '"') quoted = !quoted;
407 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
412 } else if (*beg == '\0') {
418 if (ch == '\0') break;
420 if (ch == ' ') while (end[1] == ' ') end++;
424 while ((ptr = StrChrW(buffer, '"')))
425 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
427 /* now check for any quotes in extraPmtsBuf and remove */
428 if (extraPmtsBuf != NULL)
430 beg = end = extraPmtsBuf;
435 if (ch == '"') quoted = !quoted;
436 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
443 if (ch == '\0') break;
445 if (ch == ' ') while (end[1] == ' ') end++;
450 while ((ptr = StrChrW(extraPmts, '"')))
451 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
453 if (extraPmts == NULL)
454 extraPmts = extraPmtsBuf;
457 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
459 Control_LoadApplet(hWnd, buffer, panel);
462 CPlApplet* applet = panel->first;
464 assert(applet && applet->next == NULL);
466 /* we've been given a textual parameter (or none at all) */
468 while ((++sp) != applet->count) {
469 if (applet->info[sp].dwSize) {
470 TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
472 if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
478 if (sp >= applet->count) {
479 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
483 if (applet->info[sp].dwSize) {
484 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
485 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
488 Control_UnloadApplet(applet);
491 HeapFree(GetProcessHeap(), 0, buffer);
494 /*************************************************************************
495 * Control_RunDLLW [SHELL32.@]
498 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
502 TRACE("(%p, %p, %s, 0x%08x)\n",
503 hWnd, hInst, debugstr_w(cmd), nCmdShow);
505 memset(&panel, 0, sizeof(panel));
508 Control_DoWindow(&panel, hWnd, hInst);
510 Control_DoLaunch(&panel, hWnd, cmd);
514 /*************************************************************************
515 * Control_RunDLLA [SHELL32.@]
518 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
520 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
521 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
522 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
524 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
526 HeapFree(GetProcessHeap(), 0, wszCmd);
529 /*************************************************************************
530 * Control_FillCache_RunDLLW [SHELL32.@]
533 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
535 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
539 /*************************************************************************
540 * Control_FillCache_RunDLLA [SHELL32.@]
543 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
545 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
549 /*************************************************************************
550 * RunDLL_CallEntry16 [SHELL32.122]
551 * the name is probably wrong
553 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
554 LPCSTR cmdline, INT cmdshow )
559 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
560 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
562 cmdline_seg = MapLS( cmdline );
563 args[4] = HWND_16(hwnd);
564 args[3] = MapHModuleLS(inst);
565 args[2] = SELECTOROF(cmdline_seg);
566 args[1] = OFFSETOF(cmdline_seg);
568 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
569 UnMapLS( cmdline_seg );
572 /*************************************************************************
573 * CallCPLEntry16 [SHELL32.166]
575 * called by desk.cpl on "Advanced" with:
576 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
579 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
581 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);