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);
271 while ((panel->first = Control_UnloadApplet(panel->first)));
274 return Control_WndProc_Paint(panel, lParam1);
276 return Control_WndProc_LButton(panel, lParam2, TRUE);
278 return Control_WndProc_LButton(panel, lParam2, FALSE);
279 /* EPP case WM_COMMAND: */
280 /* EPP return Control_WndProc_Command(mwi, lParam1, lParam2); */
284 return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
287 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
292 wc.style = CS_HREDRAW|CS_VREDRAW;
293 wc.lpfnWndProc = Control_WndProc;
295 wc.cbWndExtra = sizeof(CPlApplet*);
296 wc.hInstance = hInst;
299 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
300 wc.lpszMenuName = NULL;
301 wc.lpszClassName = "Shell_Control_WndClass";
303 if (!RegisterClassA(&wc)) return;
305 CreateWindowExA(0, wc.lpszClassName, "Wine Control Panel",
306 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
307 CW_USEDEFAULT, CW_USEDEFAULT,
308 CW_USEDEFAULT, CW_USEDEFAULT,
309 hWnd, NULL, hInst, panel);
310 if (!panel->hWnd) return;
311 while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
312 TranslateMessage(&msg);
313 DispatchMessageA(&msg);
314 if (!panel->first) break;
318 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
322 WCHAR buffer[MAX_PATH];
323 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
326 GetSystemDirectoryW( buffer, MAX_PATH );
327 p = buffer + strlenW(buffer);
329 lstrcpyW(p, wszAllCpl);
331 if ((h = FindFirstFileW(buffer, &fd)) != 0) {
333 lstrcpyW(p, fd.cFileName);
334 Control_LoadApplet(hWnd, buffer, panel);
335 } while (FindNextFileW(h, &fd));
339 if (panel->first) Control_DoInterface(panel, hWnd, hInst);
342 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
358 LPWSTR extraPmts = NULL;
361 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
364 end = lstrcpyW(buffer, wszCmd);
368 if (ch == '"') quoted = !quoted;
369 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
374 } else if (*beg == '\0') {
380 if (ch == '\0') break;
382 if (ch == ' ') while (end[1] == ' ') end++;
386 while ((ptr = StrChrW(buffer, '"')))
387 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
389 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
391 Control_LoadApplet(hWnd, buffer, panel);
394 CPlApplet* applet = panel->first;
396 assert(applet && applet->next == NULL);
397 if (sp >= applet->count) {
398 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
401 if (applet->info[sp].dwSize) {
402 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
403 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
405 Control_UnloadApplet(applet);
407 HeapFree(GetProcessHeap(), 0, buffer);
410 /*************************************************************************
411 * Control_RunDLLW [SHELL32.@]
414 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
418 TRACE("(%p, %p, %s, 0x%08lx)\n",
419 hWnd, hInst, debugstr_w(cmd), nCmdShow);
421 memset(&panel, 0, sizeof(panel));
424 Control_DoWindow(&panel, hWnd, hInst);
426 Control_DoLaunch(&panel, hWnd, cmd);
430 /*************************************************************************
431 * Control_RunDLLA [SHELL32.@]
434 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
436 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
437 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
438 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
440 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
442 HeapFree(GetProcessHeap(), 0, wszCmd);
445 /*************************************************************************
446 * Control_FillCache_RunDLLA [SHELL32.@]
449 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
451 FIXME("%p %p 0x%04lx 0x%04lx stub\n", hWnd, hModule, w, x);
455 /*************************************************************************
456 * Control_FillCache_RunDLLW [SHELL32.@]
459 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
461 return Control_FillCache_RunDLLA(hWnd, hModule, w, x);
465 /*************************************************************************
466 * RunDLL_CallEntry16 [SHELL32.122]
467 * the name is probably wrong
469 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
470 LPCSTR cmdline, INT cmdshow )
475 TRACE( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
476 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
478 cmdline_seg = MapLS( cmdline );
479 args[4] = HWND_16(hwnd);
480 args[3] = MapHModuleLS(inst);
481 args[2] = SELECTOROF(cmdline_seg);
482 args[1] = OFFSETOF(cmdline_seg);
484 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
485 UnMapLS( cmdline_seg );
488 /*************************************************************************
489 * CallCPLEntry16 [SHELL32.166]
491 * called by desk.cpl on "Advanced" with:
492 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
495 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
497 FIXME("(%p, %p, %08lx, %08lx, %08lx, %08lx): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);