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
23 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl
);
25 CPlApplet
* Control_UnloadApplet(CPlApplet
* applet
)
30 for (i
= 0; i
< applet
->count
; i
++)
32 if (!applet
->info
[i
].dwSize
)
34 applet
->proc(applet
->hWnd
, CPL_STOP
, i
, applet
->info
[i
].lData
);
37 applet
->proc(applet
->hWnd
, CPL_EXIT
, 0L, 0L);
39 FreeLibrary(applet
->hModule
);
41 HeapFree(GetProcessHeap(), 0, applet
);
45 CPlApplet
* Control_LoadApplet(HWND hWnd
, LPCWSTR cmd
, CPanel
* panel
)
52 if (!(applet
= (CPlApplet
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*applet
))))
57 if (!(applet
->hModule
= LoadLibraryW(cmd
)))
59 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd
));
62 if (!(applet
->proc
= (APPLET_PROC
)GetProcAddress(applet
->hModule
, "CPlApplet")))
64 WARN("Not a valid control panel applet %s\n", debugstr_w(cmd
));
67 if (!applet
->proc(hWnd
, CPL_INIT
, 0L, 0L))
69 WARN("Init of applet has failed\n");
72 if ((applet
->count
= applet
->proc(hWnd
, CPL_GETCOUNT
, 0L, 0L)) == 0)
74 WARN("No subprogram in applet\n");
78 applet
= (CPlApplet
*)HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, applet
,
79 sizeof(*applet
) + (applet
->count
- 1) * sizeof(NEWCPLINFOW
));
81 for (i
= 0; i
< applet
->count
; i
++)
83 ZeroMemory(&newinfo
, sizeof(newinfo
));
84 newinfo
.dwSize
= sizeof(NEWCPLINFOW
);
85 applet
->info
[i
].dwSize
= sizeof(NEWCPLINFOW
);
86 /* proc is supposed to return a null value upon success for
87 * CPL_INQUIRE and CPL_NEWINQUIRE
88 * However, real drivers don't seem to behave like this
89 * So, use introspection rather than return value
91 applet
->proc(hWnd
, CPL_NEWINQUIRE
, i
, (LPARAM
)&newinfo
);
92 if (newinfo
.hIcon
== 0)
94 applet
->proc(hWnd
, CPL_INQUIRE
, i
, (LPARAM
)&info
);
95 if (info
.idIcon
== 0 || info
.idName
== 0)
97 WARN("Couldn't get info from sp %u\n", i
);
98 applet
->info
[i
].dwSize
= 0;
102 /* convert the old data into the new structure */
103 applet
->info
[i
].dwFlags
= 0;
104 applet
->info
[i
].dwHelpContext
= 0;
105 applet
->info
[i
].lData
= info
.lData
;
106 applet
->info
[i
].hIcon
= LoadIconW(applet
->hModule
,
107 MAKEINTRESOURCEW(info
.idIcon
));
108 LoadStringW(applet
->hModule
, info
.idName
,
109 applet
->info
[i
].szName
, sizeof(applet
->info
[i
].szName
) / sizeof(WCHAR
));
110 LoadStringW(applet
->hModule
, info
.idInfo
,
111 applet
->info
[i
].szInfo
, sizeof(applet
->info
[i
].szInfo
) / sizeof(WCHAR
));
112 applet
->info
[i
].szHelpFile
[0] = '\0';
117 CopyMemory(&applet
->info
[i
], &newinfo
, newinfo
.dwSize
);
118 if (newinfo
.dwSize
!= sizeof(NEWCPLINFOW
))
120 applet
->info
[i
].dwSize
= sizeof(NEWCPLINFOW
);
121 lstrcpyW(applet
->info
[i
].szName
, newinfo
.szName
);
122 lstrcpyW(applet
->info
[i
].szInfo
, newinfo
.szInfo
);
123 lstrcpyW(applet
->info
[i
].szHelpFile
, newinfo
.szHelpFile
);
128 applet
->next
= panel
->first
;
129 panel
->first
= applet
;
134 Control_UnloadApplet(applet
);
138 static void Control_WndProc_Create(HWND hWnd
, const CREATESTRUCTW
* cs
)
140 CPanel
* panel
= (CPanel
*)cs
->lpCreateParams
;
142 SetWindowLongPtrW(hWnd
, 0, (LONG_PTR
)panel
);
152 static BOOL
Control_Localize(const CPanel
* panel
, int cx
, int cy
,
153 CPlApplet
** papplet
, unsigned* psp
)
156 int x
= (XSTEP
-XICON
)/2, y
= 0;
160 GetClientRect(panel
->hWnd
, &rc
);
161 for (applet
= panel
->first
; applet
; applet
= applet
->next
)
163 for (i
= 0; i
< applet
->count
; i
++)
165 if (!applet
->info
[i
].dwSize
)
167 if (x
+ XSTEP
>= rc
.right
- rc
.left
)
172 if (cx
>= x
&& cx
< x
+ XICON
&& cy
>= y
&& cy
< y
+ YSTEP
)
184 static LRESULT
Control_WndProc_Paint(const CPanel
* panel
, WPARAM wParam
)
194 hdc
= (wParam
) ? (HDC
)wParam
: BeginPaint(panel
->hWnd
, &ps
);
195 hOldFont
= SelectObject(hdc
, GetStockObject(ANSI_VAR_FONT
));
196 GetClientRect(panel
->hWnd
, &rc
);
198 for (applet
= panel
->first
; applet
; applet
= applet
->next
)
200 for (i
= 0; i
< applet
->count
; i
++)
202 if (x
+ XSTEP
>= rc
.right
- rc
.left
)
207 if (!applet
->info
[i
].dwSize
)
209 DrawIcon(hdc
, x
+ (XSTEP
-XICON
)/2, y
, applet
->info
[i
].hIcon
);
211 txtRect
.right
= x
+ XSTEP
;
212 txtRect
.top
= y
+ YICON
;
213 txtRect
.bottom
= y
+ YSTEP
;
214 DrawTextW(hdc
, applet
->info
[i
].szName
, -1, &txtRect
,
215 DT_CENTER
| DT_VCENTER
);
220 SelectObject(hdc
, hOldFont
);
222 EndPaint(panel
->hWnd
, &ps
);
227 static LRESULT
Control_WndProc_LButton(CPanel
* panel
, LPARAM lParam
, BOOL up
)
232 if (Control_Localize(panel
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), &applet
, &i
))
236 if (panel
->clkApplet
== applet
&& panel
->clkSP
== i
)
238 applet
->proc(applet
->hWnd
, CPL_DBLCLK
, i
, applet
->info
[i
].lData
);
243 panel
->clkApplet
= applet
;
250 static LRESULT WINAPI
Control_WndProc(HWND hWnd
, UINT wMsg
,
251 WPARAM lParam1
, LPARAM lParam2
)
253 CPanel
* panel
= (CPanel
*)GetWindowLongPtrW(hWnd
, 0);
255 if (panel
|| wMsg
== WM_CREATE
)
260 Control_WndProc_Create(hWnd
, (CREATESTRUCTW
*)lParam2
);
264 CPlApplet
*applet
= panel
->first
;
266 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 DefWindowProcW(hWnd
, wMsg
, lParam1
, lParam2
);
285 static void Control_DoInterface(CPanel
* panel
, HWND hWnd
, HINSTANCE hInst
)
289 const WCHAR
* appName
= L
"ReactOS 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
= (HBRUSH
)GetStockObject(WHITE_BRUSH
);
298 wc
.lpszMenuName
= NULL
;
299 wc
.lpszClassName
= L
"Shell_Control_WndClass";
301 if (!RegisterClassW(&wc
)) return;
303 CreateWindowExW(0, wc
.lpszClassName
, appName
,
304 WS_OVERLAPPEDWINDOW
| WS_VISIBLE
,
305 CW_USEDEFAULT
, CW_USEDEFAULT
,
306 CW_USEDEFAULT
, CW_USEDEFAULT
,
307 hWnd
, NULL
, hInst
, panel
);
314 /* FIXME appName & message should be localized */
315 MessageBoxW(panel
->hWnd
, L
"Cannot load any applets", appName
, MB_OK
);
319 while (GetMessageW(&msg
, panel
->hWnd
, 0, 0))
321 TranslateMessage(&msg
);
322 DispatchMessageW(&msg
);
326 static void Control_DoWindow(CPanel
*panel
, HWND hWnd
, HINSTANCE hInst
)
329 WIN32_FIND_DATAW wfd
;
330 WCHAR wszPath
[MAX_PATH
];
331 WCHAR
*Ptr
= wszPath
;
333 Ptr
+= GetSystemDirectoryW(wszPath
, MAX_PATH
);
335 wcscpy(Ptr
, L
"*.cpl");
337 hFind
= FindFirstFileW(wszPath
, &wfd
);
338 if (hFind
!= INVALID_HANDLE_VALUE
)
342 wcscpy(Ptr
, wfd
.cFileName
);
343 Control_LoadApplet(hWnd
, wszPath
, panel
);
344 } while (FindNextFileW(hFind
, &wfd
));
348 Control_DoInterface(panel
, hWnd
, hInst
);
351 static void Control_DoLaunch(CPanel
*pPanel
, HWND hWnd
, LPCWSTR pwszCmd
)
353 /* Make a pwszCmd copy so we can modify it */
354 LPWSTR pwszCmdCopy
= _wcsdup(pwszCmd
);
358 LPWSTR pwszPath
= pwszCmdCopy
, pwszArg
= NULL
, pwszArg2
= NULL
;
360 /* Path can be quoted */
361 if (pwszPath
[0] == L
'"')
364 pwszArg
= wcschr(pwszPath
, L
'"');
369 pwszArg
= pwszCmdCopy
;
371 /* First argument starts after space or ','. Note: we ignore characters between '"' and ',' or ' '. */
373 pwszArg
= wcspbrk(pwszArg
, L
" ,");
376 /* NULL terminate path and find first character of arg */
377 *(pwszArg
++) = L
'\0';
378 if (pwszArg
[0] == L
'"')
381 pwszArg2
= wcschr(pwszArg
, L
'"');
383 *(pwszArg2
++) = L
'\0';
387 /* Second argument always starts with ','. Note: we ignore characters between '"' and ','. */
389 pwszArg2
= wcschr(pwszArg2
, L
',');
392 TRACE("Launch %ls, arg %ls, arg2 %ls\n", pwszPath
, pwszArg
, pwszArg2
);
394 /* Create a mutex to disallow running multiple instances */
395 HANDLE hMutex
= CreateMutexW(NULL
, TRUE
, PathFindFileNameW(pwszPath
));
396 if (!hMutex
|| GetLastError() == ERROR_ALREADY_EXISTS
)
398 TRACE("Next instance disallowed\n");
404 /* Load applet cpl */
405 TRACE("Load applet %ls\n", pwszPath
);
406 Control_LoadApplet(hWnd
, pwszPath
, pPanel
);
409 /* First pPanel applet is the new one */
410 CPlApplet
*pApplet
= pPanel
->first
;
411 assert(pApplet
&& pApplet
->next
== NULL
);
412 TRACE("pApplet->count %d\n", pApplet
->count
);
414 /* Note: if there is only one applet, first argument is ignored */
416 if (pApplet
->count
> 1 && pwszArg
&& pwszArg
[0])
418 /* If arg begins with '@', number specifies applet index */
419 if (pwszArg
[0] == L
'@')
420 i
= _wtoi(pwszArg
+ 1);
423 /* Otherwise it's applet name */
424 for (i
= 0; i
< (INT
)pApplet
->count
; ++i
)
425 if (!wcscmp(pwszArg
, pApplet
->info
[i
].szName
))
430 if (i
>= 0 && i
< (INT
)pApplet
->count
&& pApplet
->info
[i
].dwSize
)
432 /* Start the applet */
433 TRACE("Starting applet %d\n", i
);
434 if (!pApplet
->proc(pApplet
->hWnd
, CPL_DBLCLK
, i
, pApplet
->info
[i
].lData
))
435 pApplet
->proc(pApplet
->hWnd
, CPL_STARTWPARMSA
, i
, (LPARAM
)pwszArg
);
437 ERR("Applet not found: %ls\n", pwszArg
? pwszArg
: L
"NULL");
439 Control_UnloadApplet(pApplet
);
442 ERR("Failed to load applet %ls\n", pwszPath
);
444 ReleaseMutex(hMutex
);
449 /*************************************************************************
450 * Control_RunDLLW [SHELL32.@]
453 EXTERN_C
void WINAPI
Control_RunDLLW(HWND hWnd
, HINSTANCE hInst
, LPCWSTR cmd
, DWORD nCmdShow
)
457 TRACE("(%p, %p, %s, 0x%08x)\n",
458 hWnd
, hInst
, debugstr_w(cmd
), nCmdShow
);
460 memset(&Panel
, 0, sizeof(Panel
));
464 TRACE("[shell32, Control_RunDLLW] Calling Control_DoWindow\n");
465 Control_DoWindow(&Panel
, hWnd
, hInst
);
469 TRACE("[shell32, Control_RunDLLW] Calling Control_DoLaunch\n");
470 Control_DoLaunch(&Panel
, hWnd
, cmd
);
474 /*************************************************************************
475 * Control_RunDLLA [SHELL32.@]
478 EXTERN_C
void WINAPI
Control_RunDLLA(HWND hWnd
, HINSTANCE hInst
, LPCSTR cmd
, DWORD nCmdShow
)
480 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, cmd
, -1, NULL
, 0 );
481 LPWSTR wszCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
483 if (wszCmd
&& MultiByteToWideChar(CP_ACP
, 0, cmd
, -1, wszCmd
, len
))
485 Control_RunDLLW(hWnd
, hInst
, wszCmd
, nCmdShow
);
488 HeapFree(GetProcessHeap(), 0, wszCmd
);
491 /*************************************************************************
492 * Control_FillCache_RunDLLW [SHELL32.@]
495 EXTERN_C HRESULT WINAPI
Control_FillCache_RunDLLW(HWND hWnd
, HANDLE hModule
, DWORD w
, DWORD x
)
497 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd
, hModule
, w
, x
);
501 /*************************************************************************
502 * Control_FillCache_RunDLLA [SHELL32.@]
505 EXTERN_C HRESULT WINAPI
Control_FillCache_RunDLLA(HWND hWnd
, HANDLE hModule
, DWORD w
, DWORD x
)
507 return Control_FillCache_RunDLLW(hWnd
, hModule
, w
, x
);
511 /*************************************************************************
512 * RunDLL_CallEntry16 [SHELL32.122]
513 * the name is probably wrong
515 EXTERN_C
void WINAPI
RunDLL_CallEntry16( DWORD proc
, HWND hwnd
, HINSTANCE inst
,
516 LPCSTR cmdline
, INT cmdshow
)
518 #if !defined(__CYGWIN__) && !defined (__MINGW32__) && !defined(_MSC_VER)
522 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
523 proc
, hwnd
, inst
, debugstr_a(cmdline
), cmdshow
);
525 cmdline_seg
= MapLS( cmdline
);
526 args
[4] = HWND_16(hwnd
);
527 args
[3] = MapHModuleLS(inst
);
528 args
[2] = SELECTOROF(cmdline_seg
);
529 args
[1] = OFFSETOF(cmdline_seg
);
531 WOWCallback16Ex( proc
, WCB16_PASCAL
, sizeof(args
), args
, NULL
);
532 UnMapLS( cmdline_seg
);
534 FIXME( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
535 proc
, hwnd
, inst
, debugstr_a(cmdline
), cmdshow
);
539 /*************************************************************************
540 * CallCPLEntry16 [SHELL32.166]
542 * called by desk.cpl on "Advanced" with:
543 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
546 LRESULT WINAPI
CallCPLEntry16(HINSTANCE hMod
, FARPROC pFunc
, HWND dw3
, UINT dw4
, LPARAM dw5
, LPARAM dw6
)
548 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod
, pFunc
, dw3
, dw4
, dw5
, dw6
);