user32/tests: Avoid strlen in trace.
[wine] / dlls / shell32 / control.c
1 /* Control Panel management
2  *
3  * Copyright 2001 Eric Pouech
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
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"
35
36 #define NO_SHLWAPI_REG
37 #include "shlwapi.h"
38
39 #include "cpanel.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
42
43 CPlApplet*      Control_UnloadApplet(CPlApplet* applet)
44 {
45     unsigned    i;
46     CPlApplet*  next;
47
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);
51     }
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;
57 }
58
59 CPlApplet*      Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
60 {
61     CPlApplet*  applet;
62     unsigned    i;
63     CPLINFO     info;
64     NEWCPLINFOW newinfo;
65
66     if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
67        return applet;
68
69     applet->hWnd = hWnd;
70
71     if (!(applet->hModule = LoadLibraryW(cmd))) {
72         WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
73         goto theError;
74     }
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;
78     }
79     if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
80         WARN("Init of applet has failed\n");
81         goto theError;
82     }
83     if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
84         WARN("No subprogram in applet\n");
85         goto theError;
86     }
87
88     applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
89                          sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
90
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        /* proc is supposed to return a null value upon success for
96         * CPL_INQUIRE and CPL_NEWINQUIRE
97         * However, real drivers don't seem to behave like this
98         * So, use introspection rather than return value
99         */
100        applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
101        if (newinfo.hIcon == 0) {
102            applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
103            if (info.idIcon == 0 || info.idName == 0) {
104                WARN("Couldn't get info from sp %u\n", i);
105                applet->info[i].dwSize = 0;
106            } else {
107                /* convert the old data into the new structure */
108                applet->info[i].dwFlags = 0;
109                applet->info[i].dwHelpContext = 0;
110                applet->info[i].lData = info.lData;
111                applet->info[i].hIcon = LoadIconW(applet->hModule,
112                                                  MAKEINTRESOURCEW(info.idIcon));
113                LoadStringW(applet->hModule, info.idName,
114                            applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
115                LoadStringW(applet->hModule, info.idInfo,
116                            applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
117                applet->info[i].szHelpFile[0] = '\0';
118            }
119        }
120        else
121        {
122            CopyMemory(&applet->info[i], &newinfo, newinfo.dwSize);
123            if (newinfo.dwSize != sizeof(NEWCPLINFOW))
124            {
125                applet->info[i].dwSize = sizeof(NEWCPLINFOW);
126                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
127                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
128                                    applet->info[i].szName,
129                                    sizeof(applet->info[i].szName) / sizeof(WCHAR));
130                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
131                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
132                                    applet->info[i].szInfo,
133                                    sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
134                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
135                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
136                                    applet->info[i].szHelpFile,
137                                    sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
138            }
139        }
140     }
141
142     applet->next = panel->first;
143     panel->first = applet;
144
145     return applet;
146
147  theError:
148     Control_UnloadApplet(applet);
149     return NULL;
150 }
151
152 static void      Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
153 {
154    CPanel*      panel = (CPanel*)cs->lpCreateParams;
155
156    SetWindowLongPtrA(hWnd, 0, (LONG_PTR)panel);
157    panel->status = 0;
158    panel->hWnd = hWnd;
159 }
160
161 #define XICON   32
162 #define XSTEP   128
163 #define YICON   32
164 #define YSTEP   64
165
166 static BOOL     Control_Localize(const CPanel* panel, int cx, int cy,
167                                  CPlApplet** papplet, unsigned* psp)
168 {
169     unsigned    i, x = (XSTEP-XICON)/2, y = 0;
170     CPlApplet*  applet;
171     RECT        rc;
172
173     GetClientRect(panel->hWnd, &rc);
174     for (applet = panel->first; applet; applet = applet->next) {
175         for (i = 0; i < applet->count; i++) {
176             if (!applet->info[i].dwSize) continue;
177             if (x + XSTEP >= rc.right - rc.left) {
178                 x = (XSTEP-XICON)/2;
179                 y += YSTEP;
180             }
181             if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
182                 *papplet = applet;
183                 *psp = i;
184                 return TRUE;
185             }
186             x += XSTEP;
187         }
188     }
189     return FALSE;
190 }
191
192 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
193 {
194     HDC         hdc;
195     PAINTSTRUCT ps;
196     RECT        rc, txtRect;
197     unsigned    i, x = 0, y = 0;
198     CPlApplet*  applet;
199     HGDIOBJ     hOldFont;
200
201     hdc = (wParam) ? (HDC)wParam : BeginPaint(panel->hWnd, &ps);
202     hOldFont = SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
203     GetClientRect(panel->hWnd, &rc);
204     for (applet = panel->first; applet; applet = applet->next) {
205         for (i = 0; i < applet->count; i++) {
206             if (x + XSTEP >= rc.right - rc.left) {
207                 x = 0;
208                 y += YSTEP;
209             }
210             if (!applet->info[i].dwSize) continue;
211             DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
212             txtRect.left = x;
213             txtRect.right = x + XSTEP;
214             txtRect.top = y + YICON;
215             txtRect.bottom = y + YSTEP;
216             DrawTextW(hdc, applet->info[i].szName, -1, &txtRect,
217                       DT_CENTER | DT_VCENTER);
218             x += XSTEP;
219         }
220     }
221     SelectObject(hdc, hOldFont);
222     if (!wParam) EndPaint(panel->hWnd, &ps);
223     return 0;
224 }
225
226 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
227 {
228     unsigned    i;
229     CPlApplet*  applet;
230
231     if (Control_Localize(panel, (short)LOWORD(lParam), (short)HIWORD(lParam), &applet, &i)) {
232        if (up) {
233            if (panel->clkApplet == applet && panel->clkSP == i) {
234                applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
235            }
236        } else {
237            panel->clkApplet = applet;
238            panel->clkSP = i;
239        }
240     }
241     return 0;
242 }
243
244 static LRESULT WINAPI   Control_WndProc(HWND hWnd, UINT wMsg,
245                                         WPARAM lParam1, LPARAM lParam2)
246 {
247    CPanel*      panel = (CPanel*)GetWindowLongPtrA(hWnd, 0);
248
249    if (panel || wMsg == WM_CREATE) {
250       switch (wMsg) {
251       case WM_CREATE:
252          Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
253          return 0;
254       case WM_DESTROY:
255          {
256             CPlApplet*  applet = panel->first;
257             while (applet)
258                applet = Control_UnloadApplet(applet);
259          }
260          PostQuitMessage(0);
261          break;
262       case WM_PAINT:
263          return Control_WndProc_Paint(panel, lParam1);
264       case WM_LBUTTONUP:
265          return Control_WndProc_LButton(panel, lParam2, TRUE);
266       case WM_LBUTTONDOWN:
267          return Control_WndProc_LButton(panel, lParam2, FALSE);
268 /* EPP       case WM_COMMAND: */
269 /* EPP   return Control_WndProc_Command(mwi, lParam1, lParam2); */
270       }
271    }
272
273    return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
274 }
275
276 static void    Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
277 {
278     WNDCLASSA   wc;
279     MSG         msg;
280     const CHAR* appName = "Wine Control Panel";
281     wc.style = CS_HREDRAW|CS_VREDRAW;
282     wc.lpfnWndProc = Control_WndProc;
283     wc.cbClsExtra = 0;
284     wc.cbWndExtra = sizeof(CPlApplet*);
285     wc.hInstance = hInst;
286     wc.hIcon = 0;
287     wc.hCursor = 0;
288     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
289     wc.lpszMenuName = NULL;
290     wc.lpszClassName = "Shell_Control_WndClass";
291
292     if (!RegisterClassA(&wc)) return;
293
294     CreateWindowExA(0, wc.lpszClassName, appName,
295                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
296                     CW_USEDEFAULT, CW_USEDEFAULT,
297                     CW_USEDEFAULT, CW_USEDEFAULT,
298                     hWnd, NULL, hInst, panel);
299     if (!panel->hWnd) return;
300
301     if (!panel->first) {
302         /* FIXME appName & message should be localized  */
303         MessageBoxA(panel->hWnd, "Cannot load any applets", appName, MB_OK);
304         return;
305     }
306
307     while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
308         TranslateMessage(&msg);
309         DispatchMessageA(&msg);
310     }
311 }
312
313 static  void    Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
314 {
315     HANDLE              h;
316     WIN32_FIND_DATAW    fd;
317     WCHAR               buffer[MAX_PATH];
318     static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
319     WCHAR *p;
320
321     GetSystemDirectoryW( buffer, MAX_PATH );
322     p = buffer + strlenW(buffer);
323     *p++ = '\\';
324     lstrcpyW(p, wszAllCpl);
325
326     if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
327         do {
328            lstrcpyW(p, fd.cFileName);
329            Control_LoadApplet(hWnd, buffer, panel);
330         } while (FindNextFileW(h, &fd));
331         FindClose(h);
332     }
333
334     Control_DoInterface(panel, hWnd, hInst);
335 }
336
337 static  void    Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
338    /* forms to parse:
339     *   foo.cpl,@sp,str
340     *   foo.cpl,@sp
341     *   foo.cpl,,str
342     *   foo.cpl @sp
343     *   foo.cpl str
344     *   "a path\foo.cpl"
345     */
346 {
347     LPWSTR      buffer;
348     LPWSTR      beg = NULL;
349     LPWSTR      end;
350     WCHAR       ch;
351     LPWSTR       ptr;
352     unsigned    sp = 0;
353     LPWSTR      extraPmts = NULL;
354     int        quoted = 0;
355
356     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
357     if (!buffer) return;
358
359     end = lstrcpyW(buffer, wszCmd);
360
361     for (;;) {
362         ch = *end;
363         if (ch == '"') quoted = !quoted;
364         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
365             *end = '\0';
366             if (beg) {
367                 if (*beg == '@') {
368                     sp = atoiW(beg + 1);
369                 } else if (*beg == '\0') {
370                     sp = 0;
371                 } else {
372                     extraPmts = beg;
373                 }
374             }
375             if (ch == '\0') break;
376             beg = end + 1;
377             if (ch == ' ') while (end[1] == ' ') end++;
378         }
379         end++;
380     }
381     while ((ptr = StrChrW(buffer, '"')))
382         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
383
384     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
385
386     Control_LoadApplet(hWnd, buffer, panel);
387
388     if (panel->first) {
389        CPlApplet* applet = panel->first;
390
391        assert(applet && applet->next == NULL);
392        if (sp >= applet->count) {
393           WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
394           sp = 0;
395        }
396        if (applet->info[sp].dwSize) {
397           if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
398              applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
399        }
400        Control_UnloadApplet(applet);
401     }
402     HeapFree(GetProcessHeap(), 0, buffer);
403 }
404
405 /*************************************************************************
406  * Control_RunDLLW                      [SHELL32.@]
407  *
408  */
409 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
410 {
411     CPanel      panel;
412
413     TRACE("(%p, %p, %s, 0x%08x)\n",
414           hWnd, hInst, debugstr_w(cmd), nCmdShow);
415
416     memset(&panel, 0, sizeof(panel));
417
418     if (!cmd || !*cmd) {
419         Control_DoWindow(&panel, hWnd, hInst);
420     } else {
421         Control_DoLaunch(&panel, hWnd, cmd);
422     }
423 }
424
425 /*************************************************************************
426  * Control_RunDLLA                      [SHELL32.@]
427  *
428  */
429 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
430 {
431     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
432     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
433     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
434     {
435         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
436     }
437     HeapFree(GetProcessHeap(), 0, wszCmd);
438 }
439
440 /*************************************************************************
441  * Control_FillCache_RunDLLW                    [SHELL32.@]
442  *
443  */
444 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
445 {
446     FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
447     return 0;
448 }
449
450 /*************************************************************************
451  * Control_FillCache_RunDLLA                    [SHELL32.@]
452  *
453  */
454 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
455 {
456     return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
457 }
458
459
460 /*************************************************************************
461  * RunDLL_CallEntry16                           [SHELL32.122]
462  * the name is probably wrong
463  */
464 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
465                                 LPCSTR cmdline, INT cmdshow )
466 {
467     WORD args[5];
468     SEGPTR cmdline_seg;
469
470     TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
471            proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
472
473     cmdline_seg = MapLS( cmdline );
474     args[4] = HWND_16(hwnd);
475     args[3] = MapHModuleLS(inst);
476     args[2] = SELECTOROF(cmdline_seg);
477     args[1] = OFFSETOF(cmdline_seg);
478     args[0] = cmdshow;
479     WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
480     UnMapLS( cmdline_seg );
481 }
482
483 /*************************************************************************
484  * CallCPLEntry16                               [SHELL32.166]
485  *
486  * called by desk.cpl on "Advanced" with:
487  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
488  *
489  */
490 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
491 {
492     FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
493     return 0x0deadbee;
494 }