Correct the test for the ODS_SELECTED bit in the WM_DRAWITEM message
[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          while ((panel->first = Control_UnloadApplet(panel->first)));
272          break;
273       case WM_PAINT:
274          return Control_WndProc_Paint(panel, lParam1);
275       case WM_LBUTTONUP:
276          return Control_WndProc_LButton(panel, lParam2, TRUE);
277       case WM_LBUTTONDOWN:
278          return Control_WndProc_LButton(panel, lParam2, FALSE);
279 /* EPP       case WM_COMMAND: */
280 /* EPP   return Control_WndProc_Command(mwi, lParam1, lParam2); */
281       }
282    }
283
284    return DefWindowProcA(hWnd, wMsg, lParam1, lParam2);
285 }
286
287 static void    Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
288 {
289     WNDCLASSA   wc;
290     MSG         msg;
291
292     wc.style = CS_HREDRAW|CS_VREDRAW;
293     wc.lpfnWndProc = Control_WndProc;
294     wc.cbClsExtra = 0;
295     wc.cbWndExtra = sizeof(CPlApplet*);
296     wc.hInstance = hInst;
297     wc.hIcon = 0;
298     wc.hCursor = 0;
299     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
300     wc.lpszMenuName = NULL;
301     wc.lpszClassName = "Shell_Control_WndClass";
302
303     if (!RegisterClassA(&wc)) return;
304
305     CreateWindowExA(0, wc.lpszClassName, "Wine Control Panel",
306                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
307                     CW_USEDEFAULT, CW_USEDEFAULT,
308                     CW_USEDEFAULT, CW_USEDEFAULT,
309                     hWnd, NULL, hInst, panel);
310     if (!panel->hWnd) return;
311     while (GetMessageA(&msg, panel->hWnd, 0, 0)) {
312         TranslateMessage(&msg);
313         DispatchMessageA(&msg);
314         if (!panel->first) break;
315     }
316 }
317
318 static  void    Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
319 {
320     HANDLE              h;
321     WIN32_FIND_DATAW    fd;
322     WCHAR               buffer[MAX_PATH];
323     static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
324     WCHAR *p;
325
326     GetSystemDirectoryW( buffer, MAX_PATH );
327     p = buffer + strlenW(buffer);
328     *p++ = '\\';
329     lstrcpyW(p, wszAllCpl);
330
331     if ((h = FindFirstFileW(buffer, &fd)) != 0) {
332         do {
333            lstrcpyW(p, fd.cFileName);
334            Control_LoadApplet(hWnd, buffer, panel);
335         } while (FindNextFileW(h, &fd));
336         FindClose(h);
337     }
338
339     if (panel->first) Control_DoInterface(panel, hWnd, hInst);
340 }
341
342 static  void    Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
343    /* forms to parse:
344     *   foo.cpl,@sp,str
345     *   foo.cpl,@sp
346     *   foo.cpl,,str
347     *   foo.cpl @sp
348     *   foo.cpl str
349     *   "a path\foo.cpl"
350     */
351 {
352     LPWSTR      buffer;
353     LPWSTR      beg = NULL;
354     LPWSTR      end;
355     WCHAR       ch;
356     LPWSTR       ptr;
357     unsigned    sp = 0;
358     LPWSTR      extraPmts = NULL;
359     int        quoted = 0;
360
361     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
362     if (!buffer) return;
363
364     end = lstrcpyW(buffer, wszCmd);
365
366     for (;;) {
367         ch = *end;
368         if (ch == '"') quoted = !quoted;
369         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
370             *end = '\0';
371             if (beg) {
372                 if (*beg == '@') {
373                     sp = atoiW(beg + 1);
374                 } else if (*beg == '\0') {
375                     sp = 0;
376                 } else {
377                     extraPmts = beg;
378                 }
379             }
380             if (ch == '\0') break;
381             beg = end + 1;
382             if (ch == ' ') while (end[1] == ' ') end++;
383         }
384         end++;
385     }
386     while ((ptr = StrChrW(buffer, '"')))
387         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
388
389     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
390
391     Control_LoadApplet(hWnd, buffer, panel);
392
393     if (panel->first) {
394        CPlApplet* applet = panel->first;
395
396        assert(applet && applet->next == NULL);
397        if (sp >= applet->count) {
398           WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
399           sp = 0;
400        }
401        if (applet->info[sp].dwSize) {
402           if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
403              applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
404        }
405        Control_UnloadApplet(applet);
406     }
407     HeapFree(GetProcessHeap(), 0, buffer);
408 }
409
410 /*************************************************************************
411  * Control_RunDLLW                      [SHELL32.@]
412  *
413  */
414 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
415 {
416     CPanel      panel;
417
418     TRACE("(%p, %p, %s, 0x%08lx)\n",
419           hWnd, hInst, debugstr_w(cmd), nCmdShow);
420
421     memset(&panel, 0, sizeof(panel));
422
423     if (!cmd || !*cmd) {
424         Control_DoWindow(&panel, hWnd, hInst);
425     } else {
426         Control_DoLaunch(&panel, hWnd, cmd);
427     }
428 }
429
430 /*************************************************************************
431  * Control_RunDLLA                      [SHELL32.@]
432  *
433  */
434 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
435 {
436     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
437     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
438     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
439     {
440         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
441     }
442     HeapFree(GetProcessHeap(), 0, wszCmd);
443 }
444
445 /*************************************************************************
446  * Control_FillCache_RunDLLA                    [SHELL32.@]
447  *
448  */
449 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
450 {
451     FIXME("%p %p 0x%04lx 0x%04lx stub\n", hWnd, hModule, w, x);
452     return 0;
453 }
454
455 /*************************************************************************
456  * Control_FillCache_RunDLLW                    [SHELL32.@]
457  *
458  */
459 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
460 {
461     return Control_FillCache_RunDLLA(hWnd, hModule, w, x);
462 }
463
464
465 /*************************************************************************
466  * RunDLL_CallEntry16                           [SHELL32.122]
467  * the name is probably wrong
468  */
469 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
470                                 LPCSTR cmdline, INT cmdshow )
471 {
472     WORD args[5];
473     SEGPTR cmdline_seg;
474
475     TRACE( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
476            proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
477
478     cmdline_seg = MapLS( cmdline );
479     args[4] = HWND_16(hwnd);
480     args[3] = MapHModuleLS(inst);
481     args[2] = SELECTOROF(cmdline_seg);
482     args[1] = OFFSETOF(cmdline_seg);
483     args[0] = cmdshow;
484     WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
485     UnMapLS( cmdline_seg );
486 }
487
488 /*************************************************************************
489  * CallCPLEntry16                               [SHELL32.166]
490  *
491  * called by desk.cpl on "Advanced" with:
492  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
493  *
494  */
495 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
496 {
497     FIXME("(%p, %p, %08lx, %08lx, %08lx, %08lx): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
498     return 0x0deadbee;
499 }