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