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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/winbase16.h"
32 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #define NO_SHLWAPI_REG
39 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
41 typedef struct CPlApplet {
42 struct CPlApplet* next; /* linked list */
44 unsigned count; /* number of subprograms */
45 HMODULE hModule; /* module of loaded applet */
46 APPLET_PROC proc; /* entry point address */
47 NEWCPLINFOW info[1]; /* array of count information.
48 * dwSize field is 0 if entry is invalid */
51 typedef struct CPanel {
52 CPlApplet* first; /* linked list */
59 static CPlApplet* Control_UnloadApplet(CPlApplet* applet)
64 for (i = 0; i < applet->count; i++) {
65 if (!applet->info[i].dwSize) continue;
66 applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
68 if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
69 FreeLibrary(applet->hModule);
71 HeapFree(GetProcessHeap(), 0, applet);
75 static CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
82 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
87 if (!(applet->hModule = LoadLibraryW(cmd))) {
88 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
91 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
92 WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
95 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
96 WARN("Init of applet has failed\n");
99 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
100 WARN("No subprogram in applet\n");
104 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
105 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
107 for (i = 0; i < applet->count; i++) {
108 ZeroMemory(&newinfo, sizeof(newinfo));
109 newinfo.dwSize = sizeof(NEWCPLINFOA);
110 applet->info[i].dwSize = sizeof(NEWCPLINFOW);
111 /* proc is supposed to return a null value upon success for
112 * CPL_INQUIRE and CPL_NEWINQUIRE
113 * However, real drivers don't seem to behave like this
114 * So, use introspection rather than return value
116 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
117 if (newinfo.hIcon == 0) {
118 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
119 if (info.idIcon == 0 || info.idName == 0) {
120 WARN("Couldn't get info from sp %u\n", i);
121 applet->info[i].dwSize = 0;
123 /* convert the old data into the new structure */
124 applet->info[i].dwFlags = 0;
125 applet->info[i].dwHelpContext = 0;
126 applet->info[i].lData = info.lData;
127 applet->info[i].hIcon = LoadIconW(applet->hModule,
128 MAKEINTRESOURCEW(info.idIcon));
129 LoadStringW(applet->hModule, info.idName,
130 applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
131 LoadStringW(applet->hModule, info.idInfo,
132 applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
133 applet->info[i].szHelpFile[0] = '\0';
138 CopyMemory(&applet->info[i], &newinfo, newinfo.dwSize);
139 if (newinfo.dwSize != sizeof(NEWCPLINFOW))
141 applet->info[i].dwSize = sizeof(NEWCPLINFOW);
142 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
143 sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
144 applet->info[i].szName,
145 sizeof(applet->info[i].szName) / sizeof(WCHAR));
146 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
147 sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
148 applet->info[i].szInfo,
149 sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
150 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
151 sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
152 applet->info[i].szHelpFile,
153 sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
158 applet->next = panel->first;
159 panel->first = applet;
164 Control_UnloadApplet(applet);
168 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
170 CPanel* panel = (CPanel*)cs->lpCreateParams;
172 SetWindowLongA(hWnd, 0, (LPARAM)panel);
182 static BOOL Control_Localize(const CPanel* panel, unsigned cx, unsigned cy,
183 CPlApplet** papplet, unsigned* psp)
185 unsigned i, x = (XSTEP-XICON)/2, y = 0;
189 GetClientRect(panel->hWnd, &rc);
190 for (applet = panel->first; applet; applet = applet = applet->next) {
191 for (i = 0; i < applet->count; i++) {
192 if (!applet->info[i].dwSize) continue;
193 if (x + XSTEP >= rc.right - rc.left) {
197 if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
208 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
213 unsigned i, x = 0, y = 0;
217 hdc = (wParam) ? (HDC)wParam : BeginPaint(panel->hWnd, &ps);
218 hOldFont = SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
219 GetClientRect(panel->hWnd, &rc);
220 for (applet = panel->first; applet; applet = applet = applet->next) {
221 for (i = 0; i < applet->count; i++) {
222 if (x + XSTEP >= rc.right - rc.left) {
226 if (!applet->info[i].dwSize) continue;
227 DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
229 txtRect.right = x + XSTEP;
230 txtRect.top = y + YICON;
231 txtRect.bottom = y + YSTEP;
232 DrawTextW(hdc, applet->info[i].szName, -1, &txtRect,
233 DT_CENTER | DT_VCENTER);
237 SelectObject(hdc, hOldFont);
238 if (!wParam) EndPaint(panel->hWnd, &ps);
242 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
247 if (Control_Localize(panel, LOWORD(lParam), HIWORD(lParam), &applet, &i)) {
249 if (panel->clkApplet == applet && panel->clkSP == i) {
250 applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
253 panel->clkApplet = applet;
260 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
261 WPARAM lParam1, LPARAM lParam2)
263 CPanel* panel = (CPanel*)GetWindowLongA(hWnd, 0);
265 if (panel || wMsg == WM_CREATE) {
268 Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
272 CPlApplet* applet = panel->first;
274 applet = Control_UnloadApplet(applet);
279 return Control_WndProc_Paint(panel, lParam1);
281 return Control_WndProc_LButton(panel, lParam2, TRUE);
283 return Control_WndProc_LButton(panel, lParam2, FALSE);
284 /* EPP case WM_COMMAND: */
285 /* EPP return Control_WndProc_Command(mwi, lParam1, lParam2); */
289 return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
292 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
296 const CHAR* appName = "Wine Control Panel";
297 wc.style = CS_HREDRAW|CS_VREDRAW;
298 wc.lpfnWndProc = Control_WndProc;
300 wc.cbWndExtra = sizeof(CPlApplet*);
301 wc.hInstance = hInst;
304 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
305 wc.lpszMenuName = NULL;
306 wc.lpszClassName = "Shell_Control_WndClass";
308 if (!RegisterClassA(&wc)) return;
310 CreateWindowExA(0, wc.lpszClassName, appName,
311 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
312 CW_USEDEFAULT, CW_USEDEFAULT,
313 CW_USEDEFAULT, CW_USEDEFAULT,
314 hWnd, NULL, hInst, panel);
315 if (!panel->hWnd) return;
318 /* FIXME appName & message should be localized */
319 MessageBoxA(panel->hWnd, "Cannot load any applets", appName, MB_OK);
323 while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
324 TranslateMessage(&msg);
325 DispatchMessageA(&msg);
329 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
333 WCHAR buffer[MAX_PATH];
334 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
337 GetSystemDirectoryW( buffer, MAX_PATH );
338 p = buffer + strlenW(buffer);
340 lstrcpyW(p, wszAllCpl);
342 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
344 lstrcpyW(p, fd.cFileName);
345 Control_LoadApplet(hWnd, buffer, panel);
346 } while (FindNextFileW(h, &fd));
350 Control_DoInterface(panel, hWnd, hInst);
353 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
369 LPWSTR extraPmts = NULL;
372 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
375 end = lstrcpyW(buffer, wszCmd);
379 if (ch == '"') quoted = !quoted;
380 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
385 } else if (*beg == '\0') {
391 if (ch == '\0') break;
393 if (ch == ' ') while (end[1] == ' ') end++;
397 while ((ptr = StrChrW(buffer, '"')))
398 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
400 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
402 Control_LoadApplet(hWnd, buffer, panel);
405 CPlApplet* applet = panel->first;
407 assert(applet && applet->next == NULL);
408 if (sp >= applet->count) {
409 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
412 if (applet->info[sp].dwSize) {
413 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
414 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
416 Control_UnloadApplet(applet);
418 HeapFree(GetProcessHeap(), 0, buffer);
421 /*************************************************************************
422 * Control_RunDLLW [SHELL32.@]
425 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
429 TRACE("(%p, %p, %s, 0x%08lx)\n",
430 hWnd, hInst, debugstr_w(cmd), nCmdShow);
432 memset(&panel, 0, sizeof(panel));
435 Control_DoWindow(&panel, hWnd, hInst);
437 Control_DoLaunch(&panel, hWnd, cmd);
441 /*************************************************************************
442 * Control_RunDLLA [SHELL32.@]
445 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
447 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
448 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
449 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
451 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
453 HeapFree(GetProcessHeap(), 0, wszCmd);
456 /*************************************************************************
457 * Control_FillCache_RunDLLA [SHELL32.@]
460 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
462 FIXME("%p %p 0x%04lx 0x%04lx stub\n", hWnd, hModule, w, x);
466 /*************************************************************************
467 * Control_FillCache_RunDLLW [SHELL32.@]
470 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
472 return Control_FillCache_RunDLLA(hWnd, hModule, w, x);
476 /*************************************************************************
477 * RunDLL_CallEntry16 [SHELL32.122]
478 * the name is probably wrong
480 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
481 LPCSTR cmdline, INT cmdshow )
486 TRACE( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
487 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
489 cmdline_seg = MapLS( cmdline );
490 args[4] = HWND_16(hwnd);
491 args[3] = MapHModuleLS(inst);
492 args[2] = SELECTOROF(cmdline_seg);
493 args[1] = OFFSETOF(cmdline_seg);
495 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
496 UnMapLS( cmdline_seg );
499 /*************************************************************************
500 * CallCPLEntry16 [SHELL32.166]
502 * called by desk.cpl on "Advanced" with:
503 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
506 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
508 FIXME("(%p, %p, %08lx, %08lx, %08lx, %08lx): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);