Added tests for GetSystemDirectoryA/W and GetWindowsDirectoryA/W.
[wine/hacks.git] / dlls / shell32 / control.c
blob841c0de8c482d36286ca849169d978b3a66f29a6
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
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "wine/debug.h"
29 #include "cpl.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
33 typedef struct CPlApplet {
34 struct CPlApplet* next; /* linked list */
35 HWND hWnd;
36 unsigned count; /* number of subprograms */
37 HMODULE hModule; /* module of loaded applet */
38 APPLET_PROC proc; /* entry point address */
39 NEWCPLINFOA info[1]; /* array of count information.
40 * dwSize field is 0 if entry is invalid */
41 } CPlApplet;
43 typedef struct CPanel {
44 CPlApplet* first; /* linked list */
45 HWND hWnd;
46 unsigned status;
47 CPlApplet* clkApplet;
48 unsigned clkSP;
49 } CPanel;
51 static CPlApplet* Control_UnloadApplet(CPlApplet* applet)
53 unsigned i;
54 CPlApplet* next;
56 for (i = 0; i < applet->count; i++) {
57 if (!applet->info[i].dwSize) continue;
58 applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
60 if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
61 FreeLibrary(applet->hModule);
62 next = applet->next;
63 HeapFree(GetProcessHeap(), 0, applet);
64 return next;
67 static CPlApplet* Control_LoadApplet(HWND hWnd, LPCSTR cmd, CPanel* panel)
69 CPlApplet* applet;
70 unsigned i;
71 CPLINFO info;
73 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
74 return applet;
76 applet->hWnd = hWnd;
78 if (!(applet->hModule = LoadLibraryA(cmd))) {
79 WARN("Cannot load control panel applet %s\n", cmd);
80 goto theError;
82 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
83 WARN("Not a valid control panel applet %s\n", cmd);
84 goto theError;
86 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
87 WARN("Init of applet has failed\n");
88 goto theError;
90 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
91 WARN("No subprogram in applet\n");
92 goto theError;
95 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
96 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOA));
98 for (i = 0; i < applet->count; i++) {
99 applet->info[i].dwSize = sizeof(NEWCPLINFOA);
100 /* proc is supposed to return a null value upon success for
101 * CPL_INQUIRE and CPL_NEWINQUIRE
102 * However, real drivers don't seem to behave like this
103 * So, use introspection rather than return value
105 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&applet->info[i]);
106 if (applet->info[i].hIcon == 0) {
107 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
108 if (info.idIcon == 0 || info.idName == 0) {
109 WARN("Couldn't get info from sp %u\n", i);
110 applet->info[i].dwSize = 0;
111 } else {
112 /* convert the old data into the new structure */
113 applet->info[i].dwFlags = 0;
114 applet->info[i].dwHelpContext = 0;
115 applet->info[i].lData = info.lData;
116 applet->info[i].hIcon = LoadIconA(applet->hModule,
117 MAKEINTRESOURCEA(info.idIcon));
118 LoadStringA(applet->hModule, info.idName,
119 applet->info[i].szName, sizeof(applet->info[i].szName));
120 LoadStringA(applet->hModule, info.idInfo,
121 applet->info[i].szInfo, sizeof(applet->info[i].szInfo));
122 applet->info[i].szHelpFile[0] = '\0';
127 applet->next = panel->first;
128 panel->first = applet;
130 return applet;
132 theError:
133 Control_UnloadApplet(applet);
134 return NULL;
137 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
139 CPanel* panel = (CPanel*)cs->lpCreateParams;
141 SetWindowLongA(hWnd, 0, (LPARAM)panel);
142 panel->status = 0;
143 panel->hWnd = hWnd;
146 #define XICON 32
147 #define XSTEP 128
148 #define YICON 32
149 #define YSTEP 64
151 static BOOL Control_Localize(const CPanel* panel, unsigned cx, unsigned cy,
152 CPlApplet** papplet, unsigned* psp)
154 unsigned i, x = (XSTEP-XICON)/2, y = 0;
155 CPlApplet* applet;
156 RECT rc;
158 GetClientRect(panel->hWnd, &rc);
159 for (applet = panel->first; applet; applet = applet = applet->next) {
160 for (i = 0; i < applet->count; i++) {
161 if (!applet->info[i].dwSize) continue;
162 if (x + XSTEP >= rc.right - rc.left) {
163 x = (XSTEP-XICON)/2;
164 y += YSTEP;
166 if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
167 *papplet = applet;
168 *psp = i;
169 return TRUE;
171 x += XSTEP;
174 return FALSE;
177 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
179 HDC hdc;
180 PAINTSTRUCT ps;
181 RECT rc, txtRect;
182 unsigned i, x = 0, y = 0;
183 CPlApplet* applet;
184 HGDIOBJ hOldFont;
186 hdc = (wParam) ? (HDC)wParam : BeginPaint(panel->hWnd, &ps);
187 hOldFont = SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
188 GetClientRect(panel->hWnd, &rc);
189 for (applet = panel->first; applet; applet = applet = applet->next) {
190 for (i = 0; i < applet->count; i++) {
191 if (x + XSTEP >= rc.right - rc.left) {
192 x = 0;
193 y += YSTEP;
195 if (!applet->info[i].dwSize) continue;
196 DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
197 txtRect.left = x;
198 txtRect.right = x + XSTEP;
199 txtRect.top = y + YICON;
200 txtRect.bottom = y + YSTEP;
201 DrawTextA(hdc, applet->info[i].szName, -1, &txtRect,
202 DT_CENTER | DT_VCENTER);
203 x += XSTEP;
206 SelectObject(hdc, hOldFont);
207 if (!wParam) EndPaint(panel->hWnd, &ps);
208 return 0;
211 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
213 unsigned i;
214 CPlApplet* applet;
216 if (Control_Localize(panel, LOWORD(lParam), HIWORD(lParam), &applet, &i)) {
217 if (up) {
218 if (panel->clkApplet == applet && panel->clkSP == i) {
219 applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
221 } else {
222 panel->clkApplet = applet;
223 panel->clkSP = i;
226 return 0;
229 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
230 WPARAM lParam1, LPARAM lParam2)
232 CPanel* panel = (CPanel*)GetWindowLongA(hWnd, 0);
234 if (panel || wMsg == WM_CREATE) {
235 switch (wMsg) {
236 case WM_CREATE:
237 Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
238 return 0;
239 case WM_DESTROY:
240 while ((panel->first = Control_UnloadApplet(panel->first)));
241 break;
242 case WM_PAINT:
243 return Control_WndProc_Paint(panel, lParam1);
244 case WM_LBUTTONUP:
245 return Control_WndProc_LButton(panel, lParam2, TRUE);
246 case WM_LBUTTONDOWN:
247 return Control_WndProc_LButton(panel, lParam2, FALSE);
248 /* EPP case WM_COMMAND: */
249 /* EPP return Control_WndProc_Command(mwi, lParam1, lParam2); */
253 return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
256 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
258 WNDCLASSA wc;
259 MSG msg;
261 wc.style = CS_HREDRAW|CS_VREDRAW;
262 wc.lpfnWndProc = Control_WndProc;
263 wc.cbClsExtra = 0;
264 wc.cbWndExtra = sizeof(CPlApplet*);
265 wc.hInstance = hInst;
266 wc.hIcon = 0;
267 wc.hCursor = 0;
268 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
269 wc.lpszMenuName = NULL;
270 wc.lpszClassName = "Shell_Control_WndClass";
272 if (!RegisterClassA(&wc)) return;
274 CreateWindowExA(0, wc.lpszClassName, "Wine Control Panel",
275 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
276 CW_USEDEFAULT, CW_USEDEFAULT,
277 CW_USEDEFAULT, CW_USEDEFAULT,
278 hWnd, (HMENU)0, hInst, panel);
279 if (!panel->hWnd) return;
280 while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
281 TranslateMessage(&msg);
282 DispatchMessageA(&msg);
283 if (!panel->first) break;
287 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
289 HANDLE h;
290 WIN32_FIND_DATAA fd;
291 char buffer[MAX_PATH];
293 /* FIXME: should grab path somewhere from configuration */
294 if ((h = FindFirstFileA("c:\\windows\\system\\*.cpl", &fd)) != 0) {
295 do {
296 sprintf(buffer, "c:\\windows\\system\\%s", fd.cFileName);
297 Control_LoadApplet(hWnd, buffer, panel);
298 } while (FindNextFileA(h, &fd));
299 FindClose(h);
302 if (panel->first) Control_DoInterface(panel, hWnd, hInst);
305 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCSTR cmd)
306 /* forms to parse:
307 * foo.cpl,@sp,str
308 * foo.cpl,@sp
309 * foo.cpl,,str
310 * foo.cpl @sp
311 * foo.cpl str
314 char* buffer;
315 char* beg = NULL;
316 char* end;
317 char ch;
318 unsigned sp = 0;
319 char* extraPmts = NULL;
321 buffer = HeapAlloc(GetProcessHeap(), 0, strlen(cmd) + 1);
322 if (!buffer) return;
324 end = strcpy(buffer, cmd);
326 for (;;) {
327 ch = *end;
328 if (ch == ' ' || ch == ',' || ch == '\0') {
329 *end = '\0';
330 if (beg) {
331 if (*beg == '@') {
332 sp = atoi(beg + 1);
333 } else if (*beg == '\0') {
334 sp = 0;
335 } else {
336 extraPmts = beg;
339 if (ch == '\0') break;
340 beg = end + 1;
341 if (ch == ' ') while (end[1] == ' ') end++;
343 end++;
345 Control_LoadApplet(hWnd, buffer, panel);
347 if (panel->first) {
348 CPlApplet* applet = panel->first;
350 assert(applet && applet->next == NULL);
351 if (sp >= applet->count) {
352 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
353 sp = 0;
355 if (applet->info[sp].dwSize) {
356 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
357 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
359 Control_UnloadApplet(applet);
361 HeapFree(GetProcessHeap(), 0, buffer);
364 /*************************************************************************
365 * Control_RunDLL [SHELL32.@]
368 void WINAPI Control_RunDLL(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
370 CPanel panel;
372 TRACE("(0x%08x, 0x%08lx, %s, 0x%08lx)\n",
373 hWnd, (DWORD)hInst, debugstr_a(cmd), nCmdShow);
375 memset(&panel, 0, sizeof(panel));
377 if (!cmd || !*cmd) {
378 Control_DoWindow(&panel, hWnd, hInst);
379 } else {
380 Control_DoLaunch(&panel, hWnd, cmd);
384 /*************************************************************************
385 * Control_FillCache_RunDLL [SHELL32.@]
388 HRESULT WINAPI Control_FillCache_RunDLL(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
390 FIXME("0x%04x 0x%04x 0x%04lx 0x%04lx stub\n",hWnd, hModule, w, x);
391 return 0;
394 /*************************************************************************
395 * RunDLL_CallEntry16 [SHELL32.122]
396 * the name is probably wrong
398 HRESULT WINAPI RunDLL_CallEntry16(DWORD v, DWORD w, DWORD x, DWORD y, DWORD z)
400 FIXME("0x%04lx 0x%04lx 0x%04lx 0x%04lx 0x%04lx stub\n",v,w,x,y,z);
401 return 0;
404 /*************************************************************************
405 * CallCPLEntry16 [SHELL32.166]
407 * called by desk.cpl on "Advanced" with:
408 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
411 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
413 FIXME("(%04x, %p, %08lx, %08lx, %08lx, %08lx): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
414 return 0x0deadbee;