quartz: Remove dead code from DSoundRender.
[wine/wine64.git] / dlls / shell32 / control.c
blob7afe8e9c7dbf12b09e51aeea9c5363ec4ff30090
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
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "wine/winbase16.h"
31 #include "wownt32.h"
32 #include "wine/debug.h"
33 #include "cpl.h"
34 #include "wine/unicode.h"
36 #define NO_SHLWAPI_REG
37 #include "shlwapi.h"
39 #include "cpanel.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
43 CPlApplet* Control_UnloadApplet(CPlApplet* applet)
45 unsigned i;
46 CPlApplet* next;
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);
54 next = applet->next;
55 HeapFree(GetProcessHeap(), 0, applet);
56 return next;
59 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
61 CPlApplet* applet;
62 unsigned i;
63 CPLINFO info;
64 NEWCPLINFOW newinfo;
66 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
67 return applet;
69 applet->hWnd = hWnd;
71 if (!(applet->hModule = LoadLibraryW(cmd))) {
72 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
73 goto theError;
75 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
76 WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
77 goto theError;
79 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
80 WARN("Init of applet has failed\n");
81 goto theError;
83 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
84 WARN("No subprogram in applet\n");
85 goto theError;
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));
132 } else {
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;
154 return applet;
156 theError:
157 Control_UnloadApplet(applet);
158 return NULL;
161 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
163 CPanel* panel = (CPanel*)cs->lpCreateParams;
165 SetWindowLongPtrA(hWnd, 0, (LONG_PTR)panel);
166 panel->status = 0;
167 panel->hWnd = hWnd;
170 #define XICON 32
171 #define XSTEP 128
172 #define YICON 32
173 #define YSTEP 64
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;
179 CPlApplet* applet;
180 RECT rc;
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) {
187 x = (XSTEP-XICON)/2;
188 y += YSTEP;
190 if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
191 *papplet = applet;
192 *psp = i;
193 return TRUE;
195 x += XSTEP;
198 return FALSE;
201 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
203 HDC hdc;
204 PAINTSTRUCT ps;
205 RECT rc, txtRect;
206 unsigned i, x = 0, y = 0;
207 CPlApplet* applet;
208 HGDIOBJ hOldFont;
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) {
216 x = 0;
217 y += YSTEP;
219 if (!applet->info[i].dwSize) 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;
230 SelectObject(hdc, hOldFont);
231 if (!wParam) EndPaint(panel->hWnd, &ps);
232 return 0;
235 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
237 unsigned i;
238 CPlApplet* applet;
240 if (Control_Localize(panel, (short)LOWORD(lParam), (short)HIWORD(lParam), &applet, &i)) {
241 if (up) {
242 if (panel->clkApplet == applet && panel->clkSP == i) {
243 applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
245 } else {
246 panel->clkApplet = applet;
247 panel->clkSP = i;
250 return 0;
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) {
259 switch (wMsg) {
260 case WM_CREATE:
261 Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
262 return 0;
263 case WM_DESTROY:
265 CPlApplet* applet = panel->first;
266 while (applet)
267 applet = Control_UnloadApplet(applet);
269 PostQuitMessage(0);
270 break;
271 case WM_PAINT:
272 return Control_WndProc_Paint(panel, lParam1);
273 case WM_LBUTTONUP:
274 return Control_WndProc_LButton(panel, lParam2, TRUE);
275 case WM_LBUTTONDOWN:
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)
287 WNDCLASSA wc;
288 MSG msg;
289 const CHAR* appName = "Wine Control Panel";
290 wc.style = CS_HREDRAW|CS_VREDRAW;
291 wc.lpfnWndProc = Control_WndProc;
292 wc.cbClsExtra = 0;
293 wc.cbWndExtra = sizeof(CPlApplet*);
294 wc.hInstance = hInst;
295 wc.hIcon = 0;
296 wc.hCursor = 0;
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;
310 if (!panel->first) {
311 /* FIXME appName & message should be localized */
312 MessageBoxA(panel->hWnd, "Cannot load any applets", appName, MB_OK);
313 return;
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)
324 HANDLE h;
325 WIN32_FIND_DATAW fd;
326 WCHAR buffer[MAX_PATH];
327 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
328 WCHAR *p;
330 GetSystemDirectoryW( buffer, MAX_PATH );
331 p = buffer + strlenW(buffer);
332 *p++ = '\\';
333 lstrcpyW(p, wszAllCpl);
335 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
336 do {
337 lstrcpyW(p, fd.cFileName);
338 Control_LoadApplet(hWnd, buffer, panel);
339 } while (FindNextFileW(h, &fd));
340 FindClose(h);
343 Control_DoInterface(panel, hWnd, hInst);
346 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
347 /* forms to parse:
348 * foo.cpl,@sp,str
349 * foo.cpl,@sp
350 * foo.cpl,,str
351 * foo.cpl @sp
352 * foo.cpl str
353 * "a path\foo.cpl"
356 LPWSTR buffer;
357 LPWSTR beg = NULL;
358 LPWSTR end;
359 WCHAR ch;
360 LPWSTR ptr;
361 signed sp = -1;
362 LPWSTR extraPmtsBuf = NULL;
363 LPWSTR extraPmts = NULL;
364 int quoted = 0;
366 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
367 if (!buffer) return;
369 end = lstrcpyW(buffer, wszCmd);
371 for (;;) {
372 ch = *end;
373 if (ch == '"') quoted = !quoted;
374 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
375 *end = '\0';
376 if (beg) {
377 if (*beg == '@') {
378 sp = atoiW(beg + 1);
379 } else if (*beg == '\0') {
380 sp = -1;
381 } else {
382 extraPmtsBuf = beg;
385 if (ch == '\0') break;
386 beg = end + 1;
387 if (ch == ' ') while (end[1] == ' ') end++;
389 end++;
391 while ((ptr = StrChrW(buffer, '"')))
392 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
394 /* now check for any quotes in extraPmtsBuf and remove */
395 if (extraPmtsBuf != NULL)
397 beg = end = extraPmtsBuf;
398 quoted = 0;
400 for (;;) {
401 ch = *end;
402 if (ch == '"') quoted = !quoted;
403 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
404 *end = '\0';
405 if (beg) {
406 if (*beg != '\0') {
407 extraPmts = beg;
410 if (ch == '\0') break;
411 beg = end + 1;
412 if (ch == ' ') while (end[1] == ' ') end++;
414 end++;
417 while ((ptr = StrChrW(extraPmts, '"')))
418 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
420 if (extraPmts == NULL)
421 extraPmts = extraPmtsBuf;
424 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
426 Control_LoadApplet(hWnd, buffer, panel);
428 if (panel->first) {
429 CPlApplet* applet = panel->first;
431 assert(applet && applet->next == NULL);
433 /* we've been given a textual parameter (or none at all) */
434 if (sp == -1) {
435 while ((++sp) != applet->count) {
436 if (applet->info[sp].dwSize) {
437 TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
439 if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
440 break;
445 if (sp >= applet->count) {
446 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
447 sp = 0;
450 if (applet->info[sp].dwSize) {
451 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
452 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
455 Control_UnloadApplet(applet);
458 HeapFree(GetProcessHeap(), 0, buffer);
461 /*************************************************************************
462 * Control_RunDLLW [SHELL32.@]
465 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
467 CPanel panel;
469 TRACE("(%p, %p, %s, 0x%08x)\n",
470 hWnd, hInst, debugstr_w(cmd), nCmdShow);
472 memset(&panel, 0, sizeof(panel));
474 if (!cmd || !*cmd) {
475 Control_DoWindow(&panel, hWnd, hInst);
476 } else {
477 Control_DoLaunch(&panel, hWnd, cmd);
481 /*************************************************************************
482 * Control_RunDLLA [SHELL32.@]
485 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
487 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
488 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
489 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
491 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
493 HeapFree(GetProcessHeap(), 0, wszCmd);
496 /*************************************************************************
497 * Control_FillCache_RunDLLW [SHELL32.@]
500 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
502 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
503 return 0;
506 /*************************************************************************
507 * Control_FillCache_RunDLLA [SHELL32.@]
510 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
512 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
516 /*************************************************************************
517 * RunDLL_CallEntry16 [SHELL32.122]
518 * the name is probably wrong
520 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
521 LPCSTR cmdline, INT cmdshow )
523 WORD args[5];
524 SEGPTR cmdline_seg;
526 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
527 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
529 cmdline_seg = MapLS( cmdline );
530 args[4] = HWND_16(hwnd);
531 args[3] = MapHModuleLS(inst);
532 args[2] = SELECTOROF(cmdline_seg);
533 args[1] = OFFSETOF(cmdline_seg);
534 args[0] = cmdshow;
535 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
536 UnMapLS( cmdline_seg );
539 /*************************************************************************
540 * CallCPLEntry16 [SHELL32.166]
542 * called by desk.cpl on "Advanced" with:
543 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
546 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
548 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
549 return 0x0deadbee;