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
30 #include "wine/winbase16.h"
32 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #define NO_SHLWAPI_REG
41 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
43 CPlApplet* Control_UnloadApplet(CPlApplet* applet)
48 for (i = 0; i < applet->count; i++) {
49 if (!applet->info[i].dwSize) continue;
50 applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
52 if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
53 FreeLibrary(applet->hModule);
55 HeapFree(GetProcessHeap(), 0, applet);
59 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
66 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
71 if (!(applet->hModule = LoadLibraryW(cmd))) {
72 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
75 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
76 WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
79 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
80 WARN("Init of applet has failed\n");
83 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
84 WARN("No subprogram in applet\n");
88 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
89 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
91 for (i = 0; i < applet->count; i++) {
92 ZeroMemory(&newinfo, sizeof(newinfo));
93 newinfo.dwSize = sizeof(NEWCPLINFOA);
94 applet->info[i].dwSize = sizeof(NEWCPLINFOW);
95 applet->info[i].dwFlags = 0;
96 applet->info[i].dwHelpContext = 0;
97 applet->info[i].szHelpFile[0] = '\0';
98 /* proc is supposed to return a null value upon success for
99 * CPL_INQUIRE and CPL_NEWINQUIRE
100 * However, real drivers don't seem to behave like this
101 * So, use introspection rather than return value
103 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
104 applet->info[i].lData = info.lData;
105 if (info.idIcon != CPL_DYNAMIC_RES)
106 applet->info[i].hIcon = LoadIconW(applet->hModule,
107 MAKEINTRESOURCEW(info.idIcon));
108 if (info.idName != CPL_DYNAMIC_RES)
109 LoadStringW(applet->hModule, info.idName,
110 applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
111 if (info.idInfo != CPL_DYNAMIC_RES)
112 LoadStringW(applet->hModule, info.idInfo,
113 applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
115 if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
116 (info.idInfo == CPL_DYNAMIC_RES)) {
117 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
119 applet->info[i].dwFlags = newinfo.dwFlags;
120 applet->info[i].dwHelpContext = newinfo.dwHelpContext;
121 applet->info[i].lData = newinfo.lData;
122 if (info.idIcon == CPL_DYNAMIC_RES) {
123 if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
124 applet->info[i].hIcon = newinfo.hIcon;
126 if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
127 if (info.idName == CPL_DYNAMIC_RES)
128 memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
129 if (info.idInfo == CPL_DYNAMIC_RES)
130 memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
131 memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
133 if (info.idName == CPL_DYNAMIC_RES)
134 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
135 sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
136 applet->info[i].szName,
137 sizeof(applet->info[i].szName) / sizeof(WCHAR));
138 if (info.idInfo == CPL_DYNAMIC_RES)
139 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
140 sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
141 applet->info[i].szInfo,
142 sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
143 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
144 sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
145 applet->info[i].szHelpFile,
146 sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
151 applet->next = panel->first;
152 panel->first = applet;
157 Control_UnloadApplet(applet);
161 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
163 CPanel* panel = (CPanel*)cs->lpCreateParams;
165 SetWindowLongPtrA(hWnd, 0, (LONG_PTR)panel);
175 static BOOL Control_Localize(const CPanel* panel, int cx, int cy,
176 CPlApplet** papplet, unsigned* psp)
178 unsigned i, x = (XSTEP-XICON)/2, y = 0;
182 GetClientRect(panel->hWnd, &rc);
183 for (applet = panel->first; applet; applet = applet->next) {
184 for (i = 0; i < applet->count; i++) {
185 if (!applet->info[i].dwSize) continue;
186 if (x + XSTEP >= rc.right - rc.left) {
190 if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
201 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
206 unsigned i, x = 0, y = 0;
210 hdc = (wParam) ? (HDC)wParam : BeginPaint(panel->hWnd, &ps);
211 hOldFont = SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
212 GetClientRect(panel->hWnd, &rc);
213 for (applet = panel->first; applet; applet = applet->next) {
214 for (i = 0; i < applet->count; i++) {
215 if (x + XSTEP >= rc.right - rc.left) {
219 if (!applet->info[i].dwSize) continue;
220 DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
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);
230 SelectObject(hdc, hOldFont);
231 if (!wParam) EndPaint(panel->hWnd, &ps);
235 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
240 if (Control_Localize(panel, (short)LOWORD(lParam), (short)HIWORD(lParam), &applet, &i)) {
242 if (panel->clkApplet == applet && panel->clkSP == i) {
243 applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
246 panel->clkApplet = applet;
253 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
254 WPARAM lParam1, LPARAM lParam2)
256 CPanel* panel = (CPanel*)GetWindowLongPtrA(hWnd, 0);
258 if (panel || wMsg == WM_CREATE) {
261 Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
265 CPlApplet* applet = panel->first;
267 applet = Control_UnloadApplet(applet);
272 return Control_WndProc_Paint(panel, lParam1);
274 return Control_WndProc_LButton(panel, lParam2, TRUE);
276 return Control_WndProc_LButton(panel, lParam2, FALSE);
277 /* EPP case WM_COMMAND: */
278 /* EPP return Control_WndProc_Command(mwi, lParam1, lParam2); */
282 return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
285 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
289 const CHAR* appName = "Wine Control Panel";
290 wc.style = CS_HREDRAW|CS_VREDRAW;
291 wc.lpfnWndProc = Control_WndProc;
293 wc.cbWndExtra = sizeof(CPlApplet*);
294 wc.hInstance = hInst;
297 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
298 wc.lpszMenuName = NULL;
299 wc.lpszClassName = "Shell_Control_WndClass";
301 if (!RegisterClassA(&wc)) return;
303 CreateWindowExA(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 if (!panel->hWnd) return;
311 /* FIXME appName & message should be localized */
312 MessageBoxA(panel->hWnd, "Cannot load any applets", appName, MB_OK);
316 while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
317 TranslateMessage(&msg);
318 DispatchMessageA(&msg);
322 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
326 WCHAR buffer[MAX_PATH];
327 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
330 GetSystemDirectoryW( buffer, MAX_PATH );
331 p = buffer + strlenW(buffer);
333 lstrcpyW(p, wszAllCpl);
335 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
337 lstrcpyW(p, fd.cFileName);
338 Control_LoadApplet(hWnd, buffer, panel);
339 } while (FindNextFileW(h, &fd));
343 Control_DoInterface(panel, hWnd, hInst);
346 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
362 LPWSTR extraPmts = NULL;
365 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
368 end = lstrcpyW(buffer, wszCmd);
372 if (ch == '"') quoted = !quoted;
373 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
378 } else if (*beg == '\0') {
384 if (ch == '\0') break;
386 if (ch == ' ') while (end[1] == ' ') end++;
390 while ((ptr = StrChrW(buffer, '"')))
391 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
393 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
395 Control_LoadApplet(hWnd, buffer, panel);
398 CPlApplet* applet = panel->first;
400 assert(applet && applet->next == NULL);
401 if (sp >= applet->count) {
402 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
405 if (applet->info[sp].dwSize) {
406 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
407 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
409 Control_UnloadApplet(applet);
411 HeapFree(GetProcessHeap(), 0, buffer);
414 /*************************************************************************
415 * Control_RunDLLW [SHELL32.@]
418 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
422 TRACE("(%p, %p, %s, 0x%08x)\n",
423 hWnd, hInst, debugstr_w(cmd), nCmdShow);
425 memset(&panel, 0, sizeof(panel));
428 Control_DoWindow(&panel, hWnd, hInst);
430 Control_DoLaunch(&panel, hWnd, cmd);
434 /*************************************************************************
435 * Control_RunDLLA [SHELL32.@]
438 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
440 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
441 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
442 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
444 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
446 HeapFree(GetProcessHeap(), 0, wszCmd);
449 /*************************************************************************
450 * Control_FillCache_RunDLLW [SHELL32.@]
453 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
455 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
459 /*************************************************************************
460 * Control_FillCache_RunDLLA [SHELL32.@]
463 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
465 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
469 /*************************************************************************
470 * RunDLL_CallEntry16 [SHELL32.122]
471 * the name is probably wrong
473 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
474 LPCSTR cmdline, INT cmdshow )
479 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
480 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
482 cmdline_seg = MapLS( cmdline );
483 args[4] = HWND_16(hwnd);
484 args[3] = MapHModuleLS(inst);
485 args[2] = SELECTOROF(cmdline_seg);
486 args[1] = OFFSETOF(cmdline_seg);
488 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
489 UnMapLS( cmdline_seg );
492 /*************************************************************************
493 * CallCPLEntry16 [SHELL32.166]
495 * called by desk.cpl on "Advanced" with:
496 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
499 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
501 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);