Intern all the atoms we'll need in one step to avoid multiple server
[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/debug.h"
31 #include "cpl.h"
32 #include "wine/unicode.h"
33
34 #define NO_SHLWAPI_REG
35 #include "shlwapi.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
38
39 typedef struct CPlApplet {
40     struct CPlApplet*   next;           /* linked list */
41     HWND                hWnd;
42     unsigned            count;          /* number of subprograms */
43     HMODULE             hModule;        /* module of loaded applet */
44     APPLET_PROC         proc;           /* entry point address */
45     NEWCPLINFOW         info[1];        /* array of count information.
46                                          * dwSize field is 0 if entry is invalid */
47 } CPlApplet;
48
49 typedef struct CPanel {
50     CPlApplet*          first;          /* linked list */
51     HWND                hWnd;
52     unsigned            status;
53     CPlApplet*          clkApplet;
54     unsigned            clkSP;
55 } CPanel;
56
57 static  CPlApplet*      Control_UnloadApplet(CPlApplet* applet)
58 {
59     unsigned    i;
60     CPlApplet*  next;
61
62     for (i = 0; i < applet->count; i++) {
63         if (!applet->info[i].dwSize) continue;
64         applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
65     }
66     if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
67     FreeLibrary(applet->hModule);
68     next = applet->next;
69     HeapFree(GetProcessHeap(), 0, applet);
70     return next;
71 }
72
73 static CPlApplet*       Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
74 {
75     CPlApplet*  applet;
76     unsigned    i;
77     CPLINFO     info;
78     NEWCPLINFOW newinfo;
79
80     if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
81        return applet;
82
83     applet->hWnd = hWnd;
84
85     if (!(applet->hModule = LoadLibraryW(cmd))) {
86         WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
87         goto theError;
88     }
89     if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
90         WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
91         goto theError;
92     }
93     if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
94         WARN("Init of applet has failed\n");
95         goto theError;
96     }
97     if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
98         WARN("No subprogram in applet\n");
99         goto theError;
100     }
101
102     applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
103                          sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
104
105     for (i = 0; i < applet->count; i++) {
106        ZeroMemory(&newinfo, sizeof(newinfo));
107        newinfo.dwSize = sizeof(NEWCPLINFOA);
108        applet->info[i].dwSize = sizeof(NEWCPLINFOW);
109        /* proc is supposed to return a null value upon success for
110         * CPL_INQUIRE and CPL_NEWINQUIRE
111         * However, real drivers don't seem to behave like this
112         * So, use introspection rather than return value
113         */
114        applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
115        if (newinfo.hIcon == 0) {
116            applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
117            if (info.idIcon == 0 || info.idName == 0) {
118                WARN("Couldn't get info from sp %u\n", i);
119                applet->info[i].dwSize = 0;
120            } else {
121                /* convert the old data into the new structure */
122                applet->info[i].dwFlags = 0;
123                applet->info[i].dwHelpContext = 0;
124                applet->info[i].lData = info.lData;
125                applet->info[i].hIcon = LoadIconW(applet->hModule,
126                                                  MAKEINTRESOURCEW(info.idIcon));
127                LoadStringW(applet->hModule, info.idName,
128                            applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
129                LoadStringW(applet->hModule, info.idInfo,
130                            applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
131                applet->info[i].szHelpFile[0] = '\0';
132            }
133        }
134        else
135        {
136            CopyMemory(&applet->info[i], &newinfo, newinfo.dwSize);
137            if (newinfo.dwSize != sizeof(NEWCPLINFOW))
138            {
139                applet->info[i].dwSize = sizeof(NEWCPLINFOW);
140                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
141                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
142                                    applet->info[i].szName,
143                                    sizeof(applet->info[i].szName) / sizeof(WCHAR));
144                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
145                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
146                                    applet->info[i].szInfo,
147                                    sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
148                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
149                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
150                                    applet->info[i].szHelpFile,
151                                    sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
152            }
153        }
154     }
155
156     applet->next = panel->first;
157     panel->first = applet;
158
159     return applet;
160
161  theError:
162     Control_UnloadApplet(applet);
163     return NULL;
164 }
165
166 static void      Control_WndProc_Create(HWND hWnd, const CREATESTRUCTA* cs)
167 {
168    CPanel*      panel = (CPanel*)cs->lpCreateParams;
169
170    SetWindowLongA(hWnd, 0, (LPARAM)panel);
171    panel->status = 0;
172    panel->hWnd = hWnd;
173 }
174
175 #define XICON   32
176 #define XSTEP   128
177 #define YICON   32
178 #define YSTEP   64
179
180 static BOOL     Control_Localize(const CPanel* panel, unsigned cx, unsigned cy,
181                                  CPlApplet** papplet, unsigned* psp)
182 {
183     unsigned    i, x = (XSTEP-XICON)/2, y = 0;
184     CPlApplet*  applet;
185     RECT        rc;
186
187     GetClientRect(panel->hWnd, &rc);
188     for (applet = panel->first; applet; applet = applet = applet->next) {
189         for (i = 0; i < applet->count; i++) {
190             if (!applet->info[i].dwSize) continue;
191             if (x + XSTEP >= rc.right - rc.left) {
192                 x = (XSTEP-XICON)/2;
193                 y += YSTEP;
194             }
195             if (cx >= x && cx < x + XICON && cy >= y && cy < y + YSTEP) {
196                 *papplet = applet;
197                 *psp = i;
198                 return TRUE;
199             }
200             x += XSTEP;
201         }
202     }
203     return FALSE;
204 }
205
206 static LRESULT Control_WndProc_Paint(const CPanel* panel, WPARAM wParam)
207 {
208     HDC         hdc;
209     PAINTSTRUCT ps;
210     RECT        rc, txtRect;
211     unsigned    i, x = 0, y = 0;
212     CPlApplet*  applet;
213     HGDIOBJ     hOldFont;
214
215     hdc = (wParam) ? (HDC)wParam : BeginPaint(panel->hWnd, &ps);
216     hOldFont = SelectObject(hdc, GetStockObject(ANSI_VAR_FONT));
217     GetClientRect(panel->hWnd, &rc);
218     for (applet = panel->first; applet; applet = applet = applet->next) {
219         for (i = 0; i < applet->count; i++) {
220             if (x + XSTEP >= rc.right - rc.left) {
221                 x = 0;
222                 y += YSTEP;
223             }
224             if (!applet->info[i].dwSize) continue;
225             DrawIcon(hdc, x + (XSTEP-XICON)/2, y, applet->info[i].hIcon);
226             txtRect.left = x;
227             txtRect.right = x + XSTEP;
228             txtRect.top = y + YICON;
229             txtRect.bottom = y + YSTEP;
230             DrawTextW(hdc, applet->info[i].szName, -1, &txtRect,
231                       DT_CENTER | DT_VCENTER);
232             x += XSTEP;
233         }
234     }
235     SelectObject(hdc, hOldFont);
236     if (!wParam) EndPaint(panel->hWnd, &ps);
237     return 0;
238 }
239
240 static LRESULT Control_WndProc_LButton(CPanel* panel, LPARAM lParam, BOOL up)
241 {
242     unsigned    i;
243     CPlApplet*  applet;
244
245     if (Control_Localize(panel, LOWORD(lParam), HIWORD(lParam), &applet, &i)) {
246        if (up) {
247            if (panel->clkApplet == applet && panel->clkSP == i) {
248                applet->proc(applet->hWnd, CPL_DBLCLK, i, applet->info[i].lData);
249            }
250        } else {
251            panel->clkApplet = applet;
252            panel->clkSP = i;
253        }
254     }
255     return 0;
256 }
257
258 static LRESULT WINAPI   Control_WndProc(HWND hWnd, UINT wMsg,
259                                         WPARAM lParam1, LPARAM lParam2)
260 {
261    CPanel*      panel = (CPanel*)GetWindowLongA(hWnd, 0);
262
263    if (panel || wMsg == WM_CREATE) {
264       switch (wMsg) {
265       case WM_CREATE:
266          Control_WndProc_Create(hWnd, (CREATESTRUCTA*)lParam2);
267          return 0;
268       case WM_DESTROY:
269          while ((panel->first = Control_UnloadApplet(panel->first)));
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
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, "Wine Control Panel",
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     while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
310         TranslateMessage(&msg);
311         DispatchMessageA(&msg);
312         if (!panel->first) break;
313     }
314 }
315
316 static  void    Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
317 {
318     HANDLE              h;
319     WIN32_FIND_DATAW    fd;
320     WCHAR               buffer[MAX_PATH];
321     static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
322     WCHAR *p;
323
324     GetSystemDirectoryW( buffer, MAX_PATH );
325     p = buffer + strlenW(buffer);
326     *p++ = '\\';
327     lstrcpyW(p, wszAllCpl);
328
329     if ((h = FindFirstFileW(buffer, &fd)) != 0) {
330         do {
331            lstrcpyW(p, fd.cFileName);
332            Control_LoadApplet(hWnd, buffer, panel);
333         } while (FindNextFileW(h, &fd));
334         FindClose(h);
335     }
336
337     if (panel->first) Control_DoInterface(panel, hWnd, hInst);
338 }
339
340 static  void    Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
341    /* forms to parse:
342     *   foo.cpl,@sp,str
343     *   foo.cpl,@sp
344     *   foo.cpl,,str
345     *   foo.cpl @sp
346     *   foo.cpl str
347     *   "a path\foo.cpl"
348     */
349 {
350     LPWSTR      buffer;
351     LPWSTR      beg = NULL;
352     LPWSTR      end;
353     WCHAR       ch;
354     LPWSTR       ptr;
355     unsigned    sp = 0;
356     LPWSTR      extraPmts = NULL;
357     int        quoted = 0;
358
359     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
360     if (!buffer) return;
361
362     end = lstrcpyW(buffer, wszCmd);
363
364     for (;;) {
365         ch = *end;
366         if (ch == '"') quoted = !quoted;
367         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
368             *end = '\0';
369             if (beg) {
370                 if (*beg == '@') {
371                     sp = atoiW(beg + 1);
372                 } else if (*beg == '\0') {
373                     sp = 0;
374                 } else {
375                     extraPmts = beg;
376                 }
377             }
378             if (ch == '\0') break;
379             beg = end + 1;
380             if (ch == ' ') while (end[1] == ' ') end++;
381         }
382         end++;
383     }
384     while ((ptr = StrChrW(buffer, '"')))
385         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
386
387     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
388
389     Control_LoadApplet(hWnd, buffer, panel);
390
391     if (panel->first) {
392        CPlApplet* applet = panel->first;
393
394        assert(applet && applet->next == NULL);
395        if (sp >= applet->count) {
396           WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
397           sp = 0;
398        }
399        if (applet->info[sp].dwSize) {
400           if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
401              applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
402        }
403        Control_UnloadApplet(applet);
404     }
405     HeapFree(GetProcessHeap(), 0, buffer);
406 }
407
408 /*************************************************************************
409  * Control_RunDLLW                      [SHELL32.@]
410  *
411  */
412 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
413 {
414     CPanel      panel;
415
416     TRACE("(%p, %p, %s, 0x%08lx)\n",
417           hWnd, hInst, debugstr_w(cmd), nCmdShow);
418
419     memset(&panel, 0, sizeof(panel));
420
421     if (!cmd || !*cmd) {
422         Control_DoWindow(&panel, hWnd, hInst);
423     } else {
424         Control_DoLaunch(&panel, hWnd, cmd);
425     }
426 }
427
428 /*************************************************************************
429  * Control_RunDLLA                      [SHELL32.@]
430  *
431  */
432 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
433 {
434     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
435     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
436     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
437     {
438         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
439     }
440     HeapFree(GetProcessHeap(), 0, wszCmd);
441 }
442
443 /*************************************************************************
444  * Control_FillCache_RunDLLA                    [SHELL32.@]
445  *
446  */
447 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
448 {
449     FIXME("%p %p 0x%04lx 0x%04lx stub\n", hWnd, hModule, w, x);
450     return 0;
451 }
452
453 /*************************************************************************
454  * Control_FillCache_RunDLLW                    [SHELL32.@]
455  *
456  */
457 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
458 {
459     return Control_FillCache_RunDLLA(hWnd, hModule, w, x);
460 }
461
462
463 /*************************************************************************
464  * RunDLL_CallEntry16                           [SHELL32.122]
465  * the name is probably wrong
466  */
467 HRESULT WINAPI RunDLL_CallEntry16(DWORD v, DWORD w, DWORD x, DWORD y, DWORD z)
468 {
469     FIXME("0x%04lx 0x%04lx 0x%04lx 0x%04lx 0x%04lx stub\n",v,w,x,y,z);
470     return 0;
471 }
472
473 /*************************************************************************
474  * CallCPLEntry16                               [SHELL32.166]
475  *
476  * called by desk.cpl on "Advanced" with:
477  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
478  *
479  */
480 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
481 {
482     FIXME("(%p, %p, %08lx, %08lx, %08lx, %08lx): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
483     return 0x0deadbee;
484 }