shell32: Check registry entry for cpl files (in standard control panel).
[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 "winreg.h"
31 #include "wine/winbase16.h"
32 #include "wownt32.h"
33 #include "wine/debug.h"
34 #include "cpl.h"
35 #include "wine/unicode.h"
36
37 #define NO_SHLWAPI_REG
38 #include "shlwapi.h"
39
40 #include "cpanel.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
43
44 CPlApplet*      Control_UnloadApplet(CPlApplet* applet)
45 {
46     unsigned    i;
47     CPlApplet*  next;
48
49     for (i = 0; i < applet->count; i++) {
50         if (!applet->info[i].dwSize) continue;
51         applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
52     }
53     if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
54     FreeLibrary(applet->hModule);
55     next = applet->next;
56     HeapFree(GetProcessHeap(), 0, applet);
57     return next;
58 }
59
60 CPlApplet*      Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
61 {
62     CPlApplet*  applet;
63     unsigned    i;
64     CPLINFO     info;
65     NEWCPLINFOW newinfo;
66
67     if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
68        return applet;
69
70     applet->hWnd = hWnd;
71
72     if (!(applet->hModule = LoadLibraryW(cmd))) {
73         WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
74         goto theError;
75     }
76     if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
77         WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
78         goto theError;
79     }
80     if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
81         WARN("Init of applet has failed\n");
82         goto theError;
83     }
84     if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
85         WARN("No subprogram in applet\n");
86         goto theError;
87     }
88
89     applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
90                          sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
91
92     for (i = 0; i < applet->count; i++) {
93        ZeroMemory(&newinfo, sizeof(newinfo));
94        newinfo.dwSize = sizeof(NEWCPLINFOA);
95        applet->info[i].dwSize = sizeof(NEWCPLINFOW);
96        applet->info[i].dwFlags = 0;
97        applet->info[i].dwHelpContext = 0;
98        applet->info[i].szHelpFile[0] = '\0';
99        /* proc is supposed to return a null value upon success for
100         * CPL_INQUIRE and CPL_NEWINQUIRE
101         * However, real drivers don't seem to behave like this
102         * So, use introspection rather than return value
103         */
104        applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
105        applet->info[i].lData = info.lData;
106        if (info.idIcon != CPL_DYNAMIC_RES)
107            applet->info[i].hIcon = LoadIconW(applet->hModule,
108                                              MAKEINTRESOURCEW(info.idIcon));
109        if (info.idName != CPL_DYNAMIC_RES)
110            LoadStringW(applet->hModule, info.idName,
111                        applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
112        if (info.idInfo != CPL_DYNAMIC_RES)
113            LoadStringW(applet->hModule, info.idInfo,
114                        applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
115
116        if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
117            (info.idInfo == CPL_DYNAMIC_RES)) {
118            applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
119
120            applet->info[i].dwFlags = newinfo.dwFlags;
121            applet->info[i].dwHelpContext = newinfo.dwHelpContext;
122            applet->info[i].lData = newinfo.lData;
123            if (info.idIcon == CPL_DYNAMIC_RES) {
124                if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
125                applet->info[i].hIcon = newinfo.hIcon;
126            }
127            if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
128                if (info.idName == CPL_DYNAMIC_RES)
129                    memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
130                if (info.idInfo == CPL_DYNAMIC_RES)
131                    memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
132                memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
133            } else {
134                if (info.idName == CPL_DYNAMIC_RES)
135                    MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
136                                        sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
137                                        applet->info[i].szName,
138                                        sizeof(applet->info[i].szName) / sizeof(WCHAR));
139                if (info.idInfo == CPL_DYNAMIC_RES)
140                    MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
141                                        sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
142                                        applet->info[i].szInfo,
143                                        sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
144                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
145                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
146                                    applet->info[i].szHelpFile,
147                                    sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
148            }
149        }
150     }
151
152     applet->next = panel->first;
153     panel->first = applet;
154
155     return applet;
156
157  theError:
158     Control_UnloadApplet(applet);
159     return NULL;
160 }
161
162 static void      Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
163 {
164    CPanel*      panel = (CPanel*)cs->lpCreateParams;
165
166    SetWindowLongPtrA(hWnd, 0, (LONG_PTR)panel);
167    panel->status = 0;
168    panel->hWnd = hWnd;
169 }
170
171 #define XICON   32
172 #define XSTEP   128
173 #define YICON   32
174 #define YSTEP   64
175
176 static BOOL     Control_Localize(const CPanel* panel, int cx, int cy,
177                                  CPlApplet** papplet, unsigned* psp)
178 {
179     unsigned    i, x = (XSTEP-XICON)/2, y = 0;
180     CPlApplet*  applet;
181     RECT        rc;
182
183     GetClientRect(panel->hWnd, &rc);
184     for (applet = panel->first; applet; applet = applet->next) {
185         for (i = 0; i < applet->count; i++) {
186             if (!applet->info[i].dwSize) continue;
187             if (x + XSTEP >= rc.right - rc.left) {
188                 x = (XSTEP-XICON)/2;
189                 y += YSTEP;
190             }
191             if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
192                 *papplet = applet;
193                 *psp = i;
194                 return TRUE;
195             }
196             x += XSTEP;
197         }
198     }
199     return FALSE;
200 }
201
202 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
203 {
204     HDC         hdc;
205     PAINTSTRUCT ps;
206     RECT        rc, txtRect;
207     unsigned    i, x = 0, y = 0;
208     CPlApplet*  applet;
209     HGDIOBJ     hOldFont;
210
211     hdc = (wParam) ? (HDC)wParam : BeginPaint(panel->hWnd, &ps);
212     hOldFont = SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
213     GetClientRect(panel->hWnd, &rc);
214     for (applet = panel->first; applet; applet = applet->next) {
215         for (i = 0; i < applet->count; i++) {
216             if (x + XSTEP >= rc.right - rc.left) {
217                 x = 0;
218                 y += YSTEP;
219             }
220             if (!applet->info[i].dwSize) continue;
221             DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
222             txtRect.left = x;
223             txtRect.right = x + XSTEP;
224             txtRect.top = y + YICON;
225             txtRect.bottom = y + YSTEP;
226             DrawTextW(hdc, applet->info[i].szName, -1, &txtRect,
227                       DT_CENTER | DT_VCENTER);
228             x += XSTEP;
229         }
230     }
231     SelectObject(hdc, hOldFont);
232     if (!wParam) EndPaint(panel->hWnd, &ps);
233     return 0;
234 }
235
236 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
237 {
238     unsigned    i;
239     CPlApplet*  applet;
240
241     if (Control_Localize(panel, (short)LOWORD(lParam), (short)HIWORD(lParam), &applet, &i)) {
242        if (up) {
243            if (panel->clkApplet == applet && panel->clkSP == i) {
244                applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
245            }
246        } else {
247            panel->clkApplet = applet;
248            panel->clkSP = i;
249        }
250     }
251     return 0;
252 }
253
254 static LRESULT WINAPI   Control_WndProc(HWND hWnd, UINT wMsg,
255                                         WPARAM lParam1, LPARAM lParam2)
256 {
257    CPanel*      panel = (CPanel*)GetWindowLongPtrA(hWnd, 0);
258
259    if (panel || wMsg == WM_CREATE) {
260       switch (wMsg) {
261       case WM_CREATE:
262          Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
263          return 0;
264       case WM_DESTROY:
265          {
266             CPlApplet*  applet = panel->first;
267             while (applet)
268                applet = Control_UnloadApplet(applet);
269          }
270          PostQuitMessage(0);
271          break;
272       case WM_PAINT:
273          return Control_WndProc_Paint(panel, lParam1);
274       case WM_LBUTTONUP:
275          return Control_WndProc_LButton(panel, lParam2, TRUE);
276       case WM_LBUTTONDOWN:
277          return Control_WndProc_LButton(panel, lParam2, FALSE);
278 /* EPP       case WM_COMMAND: */
279 /* EPP   return Control_WndProc_Command(mwi, lParam1, lParam2); */
280       }
281    }
282
283    return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
284 }
285
286 static void    Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
287 {
288     WNDCLASSA   wc;
289     MSG         msg;
290     const CHAR* appName = "Wine Control Panel";
291     wc.style = CS_HREDRAW|CS_VREDRAW;
292     wc.lpfnWndProc = Control_WndProc;
293     wc.cbClsExtra = 0;
294     wc.cbWndExtra = sizeof(CPlApplet*);
295     wc.hInstance = hInst;
296     wc.hIcon = 0;
297     wc.hCursor = 0;
298     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
299     wc.lpszMenuName = NULL;
300     wc.lpszClassName = "Shell_Control_WndClass";
301
302     if (!RegisterClassA(&wc)) return;
303
304     CreateWindowExA(0, wc.lpszClassName, appName,
305                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
306                     CW_USEDEFAULT, CW_USEDEFAULT,
307                     CW_USEDEFAULT, CW_USEDEFAULT,
308                     hWnd, NULL, hInst, panel);
309     if (!panel->hWnd) return;
310
311     if (!panel->first) {
312         /* FIXME appName & message should be localized  */
313         MessageBoxA(panel->hWnd, "Cannot load any applets", appName, MB_OK);
314         return;
315     }
316
317     while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
318         TranslateMessage(&msg);
319         DispatchMessageA(&msg);
320     }
321 }
322
323 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
324 {
325     WCHAR name[MAX_PATH];
326     WCHAR value[MAX_PATH];
327     HKEY hkey;
328
329     if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
330     {
331         int idx = 0;
332
333         for(;; ++idx)
334         {
335             DWORD nameLen = MAX_PATH;
336             DWORD valueLen = MAX_PATH;
337
338             if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)&value, &valueLen) != ERROR_SUCCESS)
339                 break;
340
341             Control_LoadApplet(hWnd, value, panel);
342         }
343         RegCloseKey(hkey);
344     }
345 }
346
347 static  void    Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
348 {
349     HANDLE              h;
350     WIN32_FIND_DATAW    fd;
351     WCHAR               buffer[MAX_PATH];
352     static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
353     static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
354             '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
355             '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
356     WCHAR *p;
357
358     /* first add .cpl files in the system directory */
359     GetSystemDirectoryW( buffer, MAX_PATH );
360     p = buffer + strlenW(buffer);
361     *p++ = '\\';
362     lstrcpyW(p, wszAllCpl);
363
364     if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
365         do {
366            lstrcpyW(p, fd.cFileName);
367            Control_LoadApplet(hWnd, buffer, panel);
368         } while (FindNextFileW(h, &fd));
369         FindClose(h);
370     }
371
372     /* now check for cpls in the registry */
373     Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
374     Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
375
376     Control_DoInterface(panel, hWnd, hInst);
377 }
378
379 static  void    Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
380    /* forms to parse:
381     *   foo.cpl,@sp,str
382     *   foo.cpl,@sp
383     *   foo.cpl,,str
384     *   foo.cpl @sp
385     *   foo.cpl str
386     *   "a path\foo.cpl"
387     */
388 {
389     LPWSTR      buffer;
390     LPWSTR      beg = NULL;
391     LPWSTR      end;
392     WCHAR       ch;
393     LPWSTR       ptr;
394     signed      sp = -1;
395     LPWSTR      extraPmtsBuf = NULL;
396     LPWSTR      extraPmts = NULL;
397     int        quoted = 0;
398
399     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
400     if (!buffer) return;
401
402     end = lstrcpyW(buffer, wszCmd);
403
404     for (;;) {
405         ch = *end;
406         if (ch == '"') quoted = !quoted;
407         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
408             *end = '\0';
409             if (beg) {
410                 if (*beg == '@') {
411                     sp = atoiW(beg + 1);
412                 } else if (*beg == '\0') {
413                     sp = -1;
414                 } else {
415                     extraPmtsBuf = beg;
416                 }
417             }
418             if (ch == '\0') break;
419             beg = end + 1;
420             if (ch == ' ') while (end[1] == ' ') end++;
421         }
422         end++;
423     }
424     while ((ptr = StrChrW(buffer, '"')))
425         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
426
427     /* now check for any quotes in extraPmtsBuf and remove */
428     if (extraPmtsBuf != NULL)
429     {
430         beg = end = extraPmtsBuf;
431         quoted = 0;
432
433         for (;;) {
434             ch = *end;
435             if (ch == '"') quoted = !quoted;
436             if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
437                 *end = '\0';
438                 if (beg) {
439                     if (*beg != '\0') {
440                         extraPmts = beg;
441                     }
442                 }
443                 if (ch == '\0') break;
444                     beg = end + 1;
445                 if (ch == ' ') while (end[1] == ' ') end++;
446             }
447             end++;
448         }
449
450         while ((ptr = StrChrW(extraPmts, '"')))
451             memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
452
453         if (extraPmts == NULL)
454             extraPmts = extraPmtsBuf;
455     }
456
457     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
458
459     Control_LoadApplet(hWnd, buffer, panel);
460
461     if (panel->first) {
462         CPlApplet* applet = panel->first;
463
464         assert(applet && applet->next == NULL);
465
466         /* we've been given a textual parameter (or none at all) */
467         if (sp == -1) {
468             while ((++sp) != applet->count) {
469                 if (applet->info[sp].dwSize) {
470                     TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
471
472                     if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
473                         break;
474                 }
475             }
476         }
477
478         if (sp >= applet->count) {
479             WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
480             sp = 0;
481         }
482
483         if (applet->info[sp].dwSize) {
484             if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
485                 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
486         }
487
488         Control_UnloadApplet(applet);
489     }
490
491     HeapFree(GetProcessHeap(), 0, buffer);
492 }
493
494 /*************************************************************************
495  * Control_RunDLLW                      [SHELL32.@]
496  *
497  */
498 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
499 {
500     CPanel      panel;
501
502     TRACE("(%p, %p, %s, 0x%08x)\n",
503           hWnd, hInst, debugstr_w(cmd), nCmdShow);
504
505     memset(&panel, 0, sizeof(panel));
506
507     if (!cmd || !*cmd) {
508         Control_DoWindow(&panel, hWnd, hInst);
509     } else {
510         Control_DoLaunch(&panel, hWnd, cmd);
511     }
512 }
513
514 /*************************************************************************
515  * Control_RunDLLA                      [SHELL32.@]
516  *
517  */
518 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
519 {
520     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
521     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
522     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
523     {
524         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
525     }
526     HeapFree(GetProcessHeap(), 0, wszCmd);
527 }
528
529 /*************************************************************************
530  * Control_FillCache_RunDLLW                    [SHELL32.@]
531  *
532  */
533 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
534 {
535     FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
536     return 0;
537 }
538
539 /*************************************************************************
540  * Control_FillCache_RunDLLA                    [SHELL32.@]
541  *
542  */
543 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
544 {
545     return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
546 }
547
548
549 /*************************************************************************
550  * RunDLL_CallEntry16                           [SHELL32.122]
551  * the name is probably wrong
552  */
553 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
554                                 LPCSTR cmdline, INT cmdshow )
555 {
556     WORD args[5];
557     SEGPTR cmdline_seg;
558
559     TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
560            proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
561
562     cmdline_seg = MapLS( cmdline );
563     args[4] = HWND_16(hwnd);
564     args[3] = MapHModuleLS(inst);
565     args[2] = SELECTOROF(cmdline_seg);
566     args[1] = OFFSETOF(cmdline_seg);
567     args[0] = cmdshow;
568     WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
569     UnMapLS( cmdline_seg );
570 }
571
572 /*************************************************************************
573  * CallCPLEntry16                               [SHELL32.166]
574  *
575  * called by desk.cpl on "Advanced" with:
576  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
577  *
578  */
579 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
580 {
581     FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
582     return 0x0deadbee;
583 }