shell32: Use wide functions when creating 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 CREATESTRUCTW* cs)
163 {
164    CPanel*      panel = (CPanel*)cs->lpCreateParams;
165
166    SetWindowLongPtrW(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*)GetWindowLongPtrW(hWnd, 0);
258
259    if (panel || wMsg == WM_CREATE) {
260       switch (wMsg) {
261       case WM_CREATE:
262          Control_WndProc_Create(hWnd, (CREATESTRUCTW*)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 DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
284 }
285
286 static void    Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
287 {
288     WNDCLASSW   wc;
289     MSG         msg;
290     const WCHAR appName[] = {'W','i','n','e',' ','C','o','n','t','r','o','l',
291         ' ','P','a','n','e','l',0};
292     const WCHAR className[] = {'S','h','e','l','l','_','C','o','n','t','r','o',
293         'l','_','W','n','d','C','l','a','s','s',0};
294
295     wc.style = CS_HREDRAW|CS_VREDRAW;
296     wc.lpfnWndProc = Control_WndProc;
297     wc.cbClsExtra = 0;
298     wc.cbWndExtra = sizeof(CPlApplet*);
299     wc.hInstance = hInst;
300     wc.hIcon = 0;
301     wc.hCursor = 0;
302     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
303     wc.lpszMenuName = NULL;
304     wc.lpszClassName = className;
305
306     if (!RegisterClassW(&wc)) return;
307
308     CreateWindowExW(0, wc.lpszClassName, appName,
309                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
310                     CW_USEDEFAULT, CW_USEDEFAULT,
311                     CW_USEDEFAULT, CW_USEDEFAULT,
312                     hWnd, NULL, hInst, panel);
313     if (!panel->hWnd) return;
314
315     while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
316         TranslateMessage(&msg);
317         DispatchMessageW(&msg);
318     }
319 }
320
321 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
322 {
323     WCHAR name[MAX_PATH];
324     WCHAR value[MAX_PATH];
325     HKEY hkey;
326
327     if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
328     {
329         int idx = 0;
330
331         for(;; ++idx)
332         {
333             DWORD nameLen = MAX_PATH;
334             DWORD valueLen = MAX_PATH;
335
336             if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
337                 break;
338
339             Control_LoadApplet(hWnd, value, panel);
340         }
341         RegCloseKey(hkey);
342     }
343 }
344
345 static  void    Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
346 {
347     HANDLE              h;
348     WIN32_FIND_DATAW    fd;
349     WCHAR               buffer[MAX_PATH];
350     static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
351     static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
352             '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
353             '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
354     WCHAR *p;
355
356     /* first add .cpl files in the system directory */
357     GetSystemDirectoryW( buffer, MAX_PATH );
358     p = buffer + strlenW(buffer);
359     *p++ = '\\';
360     lstrcpyW(p, wszAllCpl);
361
362     if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
363         do {
364            lstrcpyW(p, fd.cFileName);
365            Control_LoadApplet(hWnd, buffer, panel);
366         } while (FindNextFileW(h, &fd));
367         FindClose(h);
368     }
369
370     /* now check for cpls in the registry */
371     Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
372     Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
373
374     Control_DoInterface(panel, hWnd, hInst);
375 }
376
377 static  void    Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
378    /* forms to parse:
379     *   foo.cpl,@sp,str
380     *   foo.cpl,@sp
381     *   foo.cpl,,str
382     *   foo.cpl @sp
383     *   foo.cpl str
384     *   "a path\foo.cpl"
385     */
386 {
387     LPWSTR      buffer;
388     LPWSTR      beg = NULL;
389     LPWSTR      end;
390     WCHAR       ch;
391     LPWSTR       ptr;
392     signed      sp = -1;
393     LPWSTR      extraPmtsBuf = NULL;
394     LPWSTR      extraPmts = NULL;
395     int        quoted = 0;
396
397     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
398     if (!buffer) return;
399
400     end = lstrcpyW(buffer, wszCmd);
401
402     for (;;) {
403         ch = *end;
404         if (ch == '"') quoted = !quoted;
405         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
406             *end = '\0';
407             if (beg) {
408                 if (*beg == '@') {
409                     sp = atoiW(beg + 1);
410                 } else if (*beg == '\0') {
411                     sp = -1;
412                 } else {
413                     extraPmtsBuf = beg;
414                 }
415             }
416             if (ch == '\0') break;
417             beg = end + 1;
418             if (ch == ' ') while (end[1] == ' ') end++;
419         }
420         end++;
421     }
422     while ((ptr = StrChrW(buffer, '"')))
423         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
424
425     /* now check for any quotes in extraPmtsBuf and remove */
426     if (extraPmtsBuf != NULL)
427     {
428         beg = end = extraPmtsBuf;
429         quoted = 0;
430
431         for (;;) {
432             ch = *end;
433             if (ch == '"') quoted = !quoted;
434             if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
435                 *end = '\0';
436                 if (beg) {
437                     if (*beg != '\0') {
438                         extraPmts = beg;
439                     }
440                 }
441                 if (ch == '\0') break;
442                     beg = end + 1;
443                 if (ch == ' ') while (end[1] == ' ') end++;
444             }
445             end++;
446         }
447
448         while ((ptr = StrChrW(extraPmts, '"')))
449             memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
450
451         if (extraPmts == NULL)
452             extraPmts = extraPmtsBuf;
453     }
454
455     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
456
457     Control_LoadApplet(hWnd, buffer, panel);
458
459     if (panel->first) {
460         CPlApplet* applet = panel->first;
461
462         assert(applet && applet->next == NULL);
463
464         /* we've been given a textual parameter (or none at all) */
465         if (sp == -1) {
466             while ((++sp) != applet->count) {
467                 if (applet->info[sp].dwSize) {
468                     TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
469
470                     if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
471                         break;
472                 }
473             }
474         }
475
476         if (sp >= applet->count) {
477             WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
478             sp = 0;
479         }
480
481         if (applet->info[sp].dwSize) {
482             if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
483                 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
484         }
485
486         Control_UnloadApplet(applet);
487     }
488
489     HeapFree(GetProcessHeap(), 0, buffer);
490 }
491
492 /*************************************************************************
493  * Control_RunDLLW                      [SHELL32.@]
494  *
495  */
496 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
497 {
498     CPanel      panel;
499
500     TRACE("(%p, %p, %s, 0x%08x)\n",
501           hWnd, hInst, debugstr_w(cmd), nCmdShow);
502
503     memset(&panel, 0, sizeof(panel));
504
505     if (!cmd || !*cmd) {
506         Control_DoWindow(&panel, hWnd, hInst);
507     } else {
508         Control_DoLaunch(&panel, hWnd, cmd);
509     }
510 }
511
512 /*************************************************************************
513  * Control_RunDLLA                      [SHELL32.@]
514  *
515  */
516 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
517 {
518     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
519     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
520     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
521     {
522         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
523     }
524     HeapFree(GetProcessHeap(), 0, wszCmd);
525 }
526
527 /*************************************************************************
528  * Control_FillCache_RunDLLW                    [SHELL32.@]
529  *
530  */
531 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
532 {
533     FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
534     return 0;
535 }
536
537 /*************************************************************************
538  * Control_FillCache_RunDLLA                    [SHELL32.@]
539  *
540  */
541 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
542 {
543     return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
544 }
545
546
547 /*************************************************************************
548  * RunDLL_CallEntry16                           [SHELL32.122]
549  * the name is probably wrong
550  */
551 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
552                                 LPCSTR cmdline, INT cmdshow )
553 {
554     WORD args[5];
555     SEGPTR cmdline_seg;
556
557     TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
558            proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
559
560     cmdline_seg = MapLS( cmdline );
561     args[4] = HWND_16(hwnd);
562     args[3] = MapHModuleLS(inst);
563     args[2] = SELECTOROF(cmdline_seg);
564     args[1] = OFFSETOF(cmdline_seg);
565     args[0] = cmdshow;
566     WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
567     UnMapLS( cmdline_seg );
568 }
569
570 /*************************************************************************
571  * CallCPLEntry16                               [SHELL32.166]
572  *
573  * called by desk.cpl on "Advanced" with:
574  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
575  *
576  */
577 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
578 {
579     FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
580     return 0x0deadbee;
581 }