Assorted spelling fixes.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
40
41 typedef struct CPlApplet {
42     struct CPlApplet*   next;           /* linked list */
43     HWND                hWnd;
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 */
49 } CPlApplet;
50
51 typedef struct CPanel {
52     CPlApplet*          first;          /* linked list */
53     HWND                hWnd;
54     unsigned            status;
55     CPlApplet*          clkApplet;
56     unsigned            clkSP;
57 } CPanel;
58
59 static  CPlApplet*      Control_UnloadApplet(CPlApplet* applet)
60 {
61     unsigned    i;
62     CPlApplet*  next;
63
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);
67     }
68     if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
69     FreeLibrary(applet->hModule);
70     next = applet->next;
71     HeapFree(GetProcessHeap(), 0, applet);
72     return next;
73 }
74
75 static CPlApplet*       Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
76 {
77     CPlApplet*  applet;
78     unsigned    i;
79     CPLINFO     info;
80     NEWCPLINFOW newinfo;
81
82     if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
83        return applet;
84
85     applet->hWnd = hWnd;
86
87     if (!(applet->hModule = LoadLibraryW(cmd))) {
88         WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
89         goto theError;
90     }
91     if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
92         WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
93         goto theError;
94     }
95     if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
96         WARN("Init of applet has failed\n");
97         goto theError;
98     }
99     if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
100         WARN("No subprogram in applet\n");
101         goto theError;
102     }
103
104     applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
105                          sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
106
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
115         */
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;
122            } else {
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';
134            }
135        }
136        else
137        {
138            CopyMemory(&applet->info[i], &newinfo, newinfo.dwSize);
139            if (newinfo.dwSize != sizeof(NEWCPLINFOW))
140            {
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));
154            }
155        }
156     }
157
158     applet->next = panel->first;
159     panel->first = applet;
160
161     return applet;
162
163  theError:
164     Control_UnloadApplet(applet);
165     return NULL;
166 }
167
168 static void      Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
169 {
170    CPanel*      panel = (CPanel*)cs->lpCreateParams;
171
172    SetWindowLongA(hWnd, 0, (LPARAM)panel);
173    panel->status = 0;
174    panel->hWnd = hWnd;
175 }
176
177 #define XICON   32
178 #define XSTEP   128
179 #define YICON   32
180 #define YSTEP   64
181
182 static BOOL     Control_Localize(const CPanel* panel, unsigned cx, unsigned cy,
183                                  CPlApplet** papplet, unsigned* psp)
184 {
185     unsigned    i, x = (XSTEP-XICON)/2, y = 0;
186     CPlApplet*  applet;
187     RECT        rc;
188
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) {
194                 x = (XSTEP-XICON)/2;
195                 y += YSTEP;
196             }
197             if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
198                 *papplet = applet;
199                 *psp = i;
200                 return TRUE;
201             }
202             x += XSTEP;
203         }
204     }
205     return FALSE;
206 }
207
208 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
209 {
210     HDC         hdc;
211     PAINTSTRUCT ps;
212     RECT        rc, txtRect;
213     unsigned    i, x = 0, y = 0;
214     CPlApplet*  applet;
215     HGDIOBJ     hOldFont;
216
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) {
223                 x = 0;
224                 y += YSTEP;
225             }
226             if (!applet->info[i].dwSize) continue;
227             DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
228             txtRect.left = x;
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);
234             x += XSTEP;
235         }
236     }
237     SelectObject(hdc, hOldFont);
238     if (!wParam) EndPaint(panel->hWnd, &ps);
239     return 0;
240 }
241
242 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
243 {
244     unsigned    i;
245     CPlApplet*  applet;
246
247     if (Control_Localize(panel, LOWORD(lParam), HIWORD(lParam), &applet, &i)) {
248        if (up) {
249            if (panel->clkApplet == applet && panel->clkSP == i) {
250                applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
251            }
252        } else {
253            panel->clkApplet = applet;
254            panel->clkSP = i;
255        }
256     }
257     return 0;
258 }
259
260 static LRESULT WINAPI   Control_WndProc(HWND hWnd, UINT wMsg,
261                                         WPARAM lParam1, LPARAM lParam2)
262 {
263    CPanel*      panel = (CPanel*)GetWindowLongA(hWnd, 0);
264
265    if (panel || wMsg == WM_CREATE) {
266       switch (wMsg) {
267       case WM_CREATE:
268          Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
269          return 0;
270       case WM_DESTROY:
271          {
272             CPlApplet*  applet = panel->first;
273             while (applet)
274                applet = Control_UnloadApplet(applet);
275          }
276          PostQuitMessage(0);
277          break;
278       case WM_PAINT:
279          return Control_WndProc_Paint(panel, lParam1);
280       case WM_LBUTTONUP:
281          return Control_WndProc_LButton(panel, lParam2, TRUE);
282       case WM_LBUTTONDOWN:
283          return Control_WndProc_LButton(panel, lParam2, FALSE);
284 /* EPP       case WM_COMMAND: */
285 /* EPP   return Control_WndProc_Command(mwi, lParam1, lParam2); */
286       }
287    }
288
289    return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
290 }
291
292 static void    Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
293 {
294     WNDCLASSA   wc;
295     MSG         msg;
296     const CHAR* appName = "Wine Control Panel";
297     wc.style = CS_HREDRAW|CS_VREDRAW;
298     wc.lpfnWndProc = Control_WndProc;
299     wc.cbClsExtra = 0;
300     wc.cbWndExtra = sizeof(CPlApplet*);
301     wc.hInstance = hInst;
302     wc.hIcon = 0;
303     wc.hCursor = 0;
304     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
305     wc.lpszMenuName = NULL;
306     wc.lpszClassName = "Shell_Control_WndClass";
307
308     if (!RegisterClassA(&wc)) return;
309
310     CreateWindowExA(0, wc.lpszClassName, appName,
311                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
312                     CW_USEDEFAULT, CW_USEDEFAULT,
313                     CW_USEDEFAULT, CW_USEDEFAULT,
314                     hWnd, NULL, hInst, panel);
315     if (!panel->hWnd) return;
316
317     if (!panel->first) {
318         /* FIXME appName & message should be localized  */
319         MessageBoxA(panel->hWnd, "Cannot load any applets", appName, MB_OK);
320         return;
321     }
322
323     while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
324         TranslateMessage(&msg);
325         DispatchMessageA(&msg);
326     }
327 }
328
329 static  void    Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
330 {
331     HANDLE              h;
332     WIN32_FIND_DATAW    fd;
333     WCHAR               buffer[MAX_PATH];
334     static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
335     WCHAR *p;
336
337     GetSystemDirectoryW( buffer, MAX_PATH );
338     p = buffer + strlenW(buffer);
339     *p++ = '\\';
340     lstrcpyW(p, wszAllCpl);
341
342     if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
343         do {
344            lstrcpyW(p, fd.cFileName);
345            Control_LoadApplet(hWnd, buffer, panel);
346         } while (FindNextFileW(h, &fd));
347         FindClose(h);
348     }
349
350     Control_DoInterface(panel, hWnd, hInst);
351 }
352
353 static  void    Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
354    /* forms to parse:
355     *   foo.cpl,@sp,str
356     *   foo.cpl,@sp
357     *   foo.cpl,,str
358     *   foo.cpl @sp
359     *   foo.cpl str
360     *   "a path\foo.cpl"
361     */
362 {
363     LPWSTR      buffer;
364     LPWSTR      beg = NULL;
365     LPWSTR      end;
366     WCHAR       ch;
367     LPWSTR       ptr;
368     unsigned    sp = 0;
369     LPWSTR      extraPmts = NULL;
370     int        quoted = 0;
371
372     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
373     if (!buffer) return;
374
375     end = lstrcpyW(buffer, wszCmd);
376
377     for (;;) {
378         ch = *end;
379         if (ch == '"') quoted = !quoted;
380         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
381             *end = '\0';
382             if (beg) {
383                 if (*beg == '@') {
384                     sp = atoiW(beg + 1);
385                 } else if (*beg == '\0') {
386                     sp = 0;
387                 } else {
388                     extraPmts = beg;
389                 }
390             }
391             if (ch == '\0') break;
392             beg = end + 1;
393             if (ch == ' ') while (end[1] == ' ') end++;
394         }
395         end++;
396     }
397     while ((ptr = StrChrW(buffer, '"')))
398         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
399
400     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
401
402     Control_LoadApplet(hWnd, buffer, panel);
403
404     if (panel->first) {
405        CPlApplet* applet = panel->first;
406
407        assert(applet && applet->next == NULL);
408        if (sp >= applet->count) {
409           WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
410           sp = 0;
411        }
412        if (applet->info[sp].dwSize) {
413           if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
414              applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
415        }
416        Control_UnloadApplet(applet);
417     }
418     HeapFree(GetProcessHeap(), 0, buffer);
419 }
420
421 /*************************************************************************
422  * Control_RunDLLW                      [SHELL32.@]
423  *
424  */
425 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
426 {
427     CPanel      panel;
428
429     TRACE("(%p, %p, %s, 0x%08lx)\n",
430           hWnd, hInst, debugstr_w(cmd), nCmdShow);
431
432     memset(&panel, 0, sizeof(panel));
433
434     if (!cmd || !*cmd) {
435         Control_DoWindow(&panel, hWnd, hInst);
436     } else {
437         Control_DoLaunch(&panel, hWnd, cmd);
438     }
439 }
440
441 /*************************************************************************
442  * Control_RunDLLA                      [SHELL32.@]
443  *
444  */
445 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
446 {
447     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
448     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
449     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
450     {
451         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
452     }
453     HeapFree(GetProcessHeap(), 0, wszCmd);
454 }
455
456 /*************************************************************************
457  * Control_FillCache_RunDLLA                    [SHELL32.@]
458  *
459  */
460 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
461 {
462     FIXME("%p %p 0x%04lx 0x%04lx stub\n", hWnd, hModule, w, x);
463     return 0;
464 }
465
466 /*************************************************************************
467  * Control_FillCache_RunDLLW                    [SHELL32.@]
468  *
469  */
470 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
471 {
472     return Control_FillCache_RunDLLA(hWnd, hModule, w, x);
473 }
474
475
476 /*************************************************************************
477  * RunDLL_CallEntry16                           [SHELL32.122]
478  * the name is probably wrong
479  */
480 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
481                                 LPCSTR cmdline, INT cmdshow )
482 {
483     WORD args[5];
484     SEGPTR cmdline_seg;
485
486     TRACE( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
487            proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
488
489     cmdline_seg = MapLS( cmdline );
490     args[4] = HWND_16(hwnd);
491     args[3] = MapHModuleLS(inst);
492     args[2] = SELECTOROF(cmdline_seg);
493     args[1] = OFFSETOF(cmdline_seg);
494     args[0] = cmdshow;
495     WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
496     UnMapLS( cmdline_seg );
497 }
498
499 /*************************************************************************
500  * CallCPLEntry16                               [SHELL32.166]
501  *
502  * called by desk.cpl on "Advanced" with:
503  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
504  *
505  */
506 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
507 {
508     FIXME("(%p, %p, %08lx, %08lx, %08lx, %08lx): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
509     return 0x0deadbee;
510 }