crypt32: Add tests for encoding/decoding cert policy constraints.
[wine] / dlls / shell32 / control.c
1 /* Control Panel management
2  *
3  * Copyright 2001 Eric Pouech
4  * Copyright 2008 Owen Rudge
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #include "wine/winbase16.h"
33 #include "wownt32.h"
34 #include "wine/debug.h"
35 #include "cpl.h"
36 #include "wine/unicode.h"
37 #include "commctrl.h"
38
39 #define NO_SHLWAPI_REG
40 #include "shlwapi.h"
41
42 #include "cpanel.h"
43 #include "shresdef.h"
44 #include "shell32_main.h"
45
46 #define MAX_STRING_LEN      1024
47
48 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
49
50 CPlApplet*      Control_UnloadApplet(CPlApplet* applet)
51 {
52     unsigned    i;
53     CPlApplet*  next;
54
55     for (i = 0; i < applet->count; i++) {
56         if (!applet->info[i].dwSize) continue;
57         applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
58     }
59     if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
60     FreeLibrary(applet->hModule);
61     next = applet->next;
62     HeapFree(GetProcessHeap(), 0, applet->cmd);
63     HeapFree(GetProcessHeap(), 0, applet);
64     return next;
65 }
66
67 CPlApplet*      Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
68 {
69     CPlApplet*  applet;
70     unsigned    i;
71     CPLINFO     info;
72     NEWCPLINFOW newinfo;
73
74     if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
75        return applet;
76
77     applet->hWnd = hWnd;
78
79     if (!(applet->hModule = LoadLibraryW(cmd))) {
80         WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
81         goto theError;
82     }
83     if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
84         WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
85         goto theError;
86     }
87     if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
88         WARN("Init of applet has failed\n");
89         goto theError;
90     }
91     if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
92         WARN("No subprogram in applet\n");
93         goto theError;
94     }
95
96     applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
97                          sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
98
99     if (!(applet->cmd = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmd)+1) * sizeof(WCHAR)))) {
100         WARN("Cannot allocate memory for applet path\n");
101         goto theError;
102     }
103
104     lstrcpyW(applet->cmd, cmd);
105
106     for (i = 0; i < applet->count; i++) {
107        ZeroMemory(&newinfo, sizeof(newinfo));
108        newinfo.dwSize = sizeof(NEWCPLINFOA);
109        applet->info[i].dwSize = sizeof(NEWCPLINFOW);
110        applet->info[i].dwFlags = 0;
111        applet->info[i].dwHelpContext = 0;
112        applet->info[i].szHelpFile[0] = '\0';
113        /* proc is supposed to return a null value upon success for
114         * CPL_INQUIRE and CPL_NEWINQUIRE
115         * However, real drivers don't seem to behave like this
116         * So, use introspection rather than return value
117         */
118        applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
119        applet->info[i].lData = info.lData;
120        if (info.idIcon != CPL_DYNAMIC_RES)
121            applet->info[i].hIcon = LoadIconW(applet->hModule,
122                                              MAKEINTRESOURCEW(info.idIcon));
123        if (info.idName != CPL_DYNAMIC_RES)
124            LoadStringW(applet->hModule, info.idName,
125                        applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
126        if (info.idInfo != CPL_DYNAMIC_RES)
127            LoadStringW(applet->hModule, info.idInfo,
128                        applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
129
130        /* some broken control panels seem to return incorrect values in CPL_INQUIRE,
131           but proper data in CPL_NEWINQUIRE. if we get an empty string or a null
132           icon, see what we can get from CPL_NEWINQUIRE */
133
134        if ((applet->info[i].szName == 0) || (lstrlenW(applet->info[i].szName) == 0))
135            info.idName = CPL_DYNAMIC_RES;
136
137        /* zero-length szInfo may not be a buggy applet, but it doesn't hurt for us
138           to check anyway */
139
140        if ((applet->info[i].szInfo == 0) || (lstrlenW(applet->info[i].szInfo) == 0))
141            info.idInfo = CPL_DYNAMIC_RES;
142
143        if (applet->info[i].hIcon == NULL)
144            info.idIcon = CPL_DYNAMIC_RES;
145
146        if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
147            (info.idInfo == CPL_DYNAMIC_RES)) {
148            applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
149
150            applet->info[i].dwFlags = newinfo.dwFlags;
151            applet->info[i].dwHelpContext = newinfo.dwHelpContext;
152            applet->info[i].lData = newinfo.lData;
153            if (info.idIcon == CPL_DYNAMIC_RES) {
154                if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
155                applet->info[i].hIcon = newinfo.hIcon;
156            }
157            if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
158                if (info.idName == CPL_DYNAMIC_RES)
159                    memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
160                if (info.idInfo == CPL_DYNAMIC_RES)
161                    memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
162                memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
163            } else {
164                if (info.idName == CPL_DYNAMIC_RES)
165                    MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
166                                        sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
167                                        applet->info[i].szName,
168                                        sizeof(applet->info[i].szName) / sizeof(WCHAR));
169                if (info.idInfo == CPL_DYNAMIC_RES)
170                    MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
171                                        sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
172                                        applet->info[i].szInfo,
173                                        sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
174                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
175                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
176                                    applet->info[i].szHelpFile,
177                                    sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
178            }
179        }
180     }
181
182     applet->next = panel->first;
183     panel->first = applet;
184
185     return applet;
186
187  theError:
188     Control_UnloadApplet(applet);
189     return NULL;
190 }
191
192 #define IDC_LISTVIEW        1000
193 #define IDC_STATUSBAR       1001
194
195 #define NUM_COLUMNS            2
196 #define LISTVIEW_DEFSTYLE   (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
197                              LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)
198
199 static BOOL Control_CreateListView (CPanel *panel)
200 {
201     RECT ws, sb;
202     WCHAR empty_string[] = {0};
203     WCHAR buf[MAX_STRING_LEN];
204     LVCOLUMNW lvc;
205
206     /* Create list view */
207     GetClientRect(panel->hWndStatusBar, &sb);
208     GetClientRect(panel->hWnd, &ws);
209
210     panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
211                           empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
212                           0, 0, ws.right - ws.left, ws.bottom - ws.top -
213                           (sb.bottom - sb.top), panel->hWnd,
214                           (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
215
216     if (!panel->hWndListView)
217         return FALSE;
218
219     /* Create image lists for list view */
220     panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
221         GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
222     panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
223         GetSystemMetrics(SM_CYICON), ILC_MASK, 1, 1);
224
225     SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)panel->hImageListSmall);
226     SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)panel->hImageListLarge);
227
228     /* Create columns for list view */
229     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
230     lvc.pszText = buf;
231     lvc.fmt = LVCFMT_LEFT;
232
233     /* Name column */
234     lvc.iSubItem = 0;
235     lvc.cx = (ws.right - ws.left) / 3;
236     LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));
237
238     if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
239         return FALSE;
240
241     /* Description column */
242     lvc.iSubItem = 1;
243     lvc.cx = ((ws.right - ws.left) / 3) * 2;
244     LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
245         sizeof(buf[0]));
246
247     if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
248         return FALSE;
249
250     return(TRUE);
251 }
252
253 static void      Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
254 {
255    CPanel* panel = cs->lpCreateParams;
256    HMENU hMenu, hSubMenu;
257    CPlApplet* applet;
258    MENUITEMINFOW mii;
259    unsigned int i;
260    int menucount, index;
261    CPlItem *item;
262    LVITEMW lvItem;
263    INITCOMMONCONTROLSEX icex;
264    INT sb_parts;
265    int itemidx;
266
267    SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
268    panel->hWnd = hWnd;
269
270    /* Initialise common control DLL */
271    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
272    icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
273    InitCommonControlsEx(&icex);
274
275    /* create the status bar */
276    if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
277        return;
278
279    sb_parts = -1;
280    SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);
281
282    /* create the list view */
283    if (!Control_CreateListView(panel))
284        return;
285
286    hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));
287
288    /* insert menu items for applets */
289    hSubMenu = GetSubMenu(hMenu, 0);
290    menucount = 0;
291
292    for (applet = panel->first; applet; applet = applet->next) {
293       for (i = 0; i < applet->count; i++) {
294          if (!applet->info[i].dwSize)
295             continue;
296
297          /* set up a CPlItem for this particular subprogram */
298          item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));
299
300          if (!item)
301             continue;
302
303          item->applet = applet;
304          item->id = i;
305
306          mii.cbSize = sizeof(MENUITEMINFOW);
307          mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
308          mii.dwTypeData = applet->info[i].szName;
309          mii.cch = sizeof(applet->info[i].szName) / sizeof(applet->info[i].szName[0]);
310          mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
311          mii.dwItemData = (ULONG_PTR)item;
312
313          if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
314             /* add the list view item */
315             index = ImageList_AddIcon(panel->hImageListLarge, applet->info[i].hIcon);
316             ImageList_AddIcon(panel->hImageListSmall, applet->info[i].hIcon);
317
318             lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
319             lvItem.iItem = menucount;
320             lvItem.iSubItem = 0;
321             lvItem.pszText = applet->info[i].szName;
322             lvItem.iImage = index;
323             lvItem.lParam = (LPARAM) item;
324
325             itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
326
327             /* add the description */
328             ListView_SetItemTextW(panel->hWndListView, itemidx, 1,
329                 applet->info[i].szInfo);
330
331             /* update menu bar, increment count */
332             DrawMenuBar(hWnd);
333             menucount++;
334          }
335       }
336    }
337
338    panel->total_subprogs = menucount;
339
340    /* check the "large items" icon in the View menu */
341    hSubMenu = GetSubMenu(hMenu, 1);
342    CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
343       FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);
344
345    SetMenu(hWnd, hMenu);
346 }
347
348 static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
349 {
350     HMENU hMenu, hSubMenu;
351     MENUITEMINFOW mii;
352     unsigned int i;
353
354     /* get the File menu */
355     hMenu = GetMenu(hWnd);
356
357     if (!hMenu)
358         return;
359
360     hSubMenu = GetSubMenu(hMenu, 0);
361
362     if (!hSubMenu)
363         return;
364
365     /* loop and free the item data */
366     for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
367     {
368         mii.cbSize = sizeof(MENUITEMINFOW);
369         mii.fMask = MIIM_DATA;
370
371         if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
372             continue;
373
374         HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
375     }
376 }
377
378 static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
379 {
380     HMENU hMenu, hSubMenu;
381
382     SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);
383
384     /* update the menu */
385     hMenu = GetMenu(panel->hWnd);
386     hSubMenu = GetSubMenu(hMenu, 1);
387
388     CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
389         id, MF_BYCOMMAND);
390 }
391
392 static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
393 {
394     HMENU hMenu, hSubMenu;
395     MENUITEMINFOW mii;
396
397     /* retrieve the CPlItem structure from the menu item data */
398     hMenu = GetMenu(hWnd);
399
400     if (!hMenu)
401         return NULL;
402
403     hSubMenu = GetSubMenu(hMenu, 0);
404
405     if (!hSubMenu)
406         return NULL;
407
408     mii.cbSize = sizeof(MENUITEMINFOW);
409     mii.fMask = MIIM_DATA;
410
411     if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
412         return NULL;
413
414     return (CPlItem *) mii.dwItemData;
415 }
416
417 static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
418 {
419     LVITEMW lvItem;
420     int selitem;
421
422     selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
423         | LVNI_SELECTED);
424
425     if (selitem != -1)
426     {
427         lvItem.iItem = selitem;
428         lvItem.mask = LVIF_PARAM;
429
430         if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
431             return (CPlItem *) lvItem.lParam;
432     }
433
434     return NULL;
435 }
436
437 static void Control_StartApplet(HWND hWnd, CPlItem *item)
438 {
439     WCHAR verbOpen[] = {'c','p','l','o','p','e','n',0};
440     WCHAR format[] = {'@','%','d',0};
441     WCHAR param[MAX_PATH];
442
443     /* execute the applet if item is valid */
444     if (item)
445     {
446         wsprintfW(param, format, item->id);
447         ShellExecuteW(hWnd, verbOpen, item->applet->cmd, param, NULL, SW_SHOW);
448     }
449 }
450
451 static LRESULT WINAPI   Control_WndProc(HWND hWnd, UINT wMsg,
452                                         WPARAM lParam1, LPARAM lParam2)
453 {
454    CPanel*      panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
455
456    if (panel || wMsg == WM_CREATE) {
457       switch (wMsg) {
458       case WM_CREATE:
459          Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
460          return 0;
461       case WM_DESTROY:
462          {
463             CPlApplet*  applet = panel->first;
464             while (applet)
465                applet = Control_UnloadApplet(applet);
466          }
467          Control_FreeCPlItems(hWnd, panel);
468          PostQuitMessage(0);
469          break;
470       case WM_COMMAND:
471          switch (LOWORD(lParam1))
472          {
473              case IDM_CPANEL_EXIT:
474                  SendMessageW(hWnd, WM_CLOSE, 0, 0);
475                  return 0;
476
477              case IDM_CPANEL_ABOUT:
478                  {
479                      WCHAR appName[MAX_STRING_LEN];
480
481                      LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
482                          sizeof(appName) / sizeof(appName[0]));
483                      ShellAboutW(hWnd, appName, NULL, NULL);
484
485                      return 0;
486                  }
487
488              case FCIDM_SHVIEW_BIGICON:
489                  Control_UpdateListViewStyle(panel, LVS_ICON, FCIDM_SHVIEW_BIGICON);
490                  return 0;
491
492              case FCIDM_SHVIEW_SMALLICON:
493                  Control_UpdateListViewStyle(panel, LVS_SMALLICON, FCIDM_SHVIEW_SMALLICON);
494                  return 0;
495
496              case FCIDM_SHVIEW_LISTVIEW:
497                  Control_UpdateListViewStyle(panel, LVS_LIST, FCIDM_SHVIEW_LISTVIEW);
498                  return 0;
499
500              case FCIDM_SHVIEW_REPORTVIEW:
501                  Control_UpdateListViewStyle(panel, LVS_REPORT, FCIDM_SHVIEW_REPORTVIEW);
502                  return 0;
503
504              default:
505                  /* check if this is an applet */
506                  if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
507                      (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
508                  {
509                      Control_StartApplet(hWnd, Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1)));
510                      return 0;
511                  }
512
513                  break;
514          }
515
516          break;
517
518       case WM_NOTIFY:
519       {
520           LPNMHDR nmh = (LPNMHDR) lParam2;
521
522           switch (nmh->idFrom)
523           {
524               case IDC_LISTVIEW:
525                   switch (nmh->code)
526                   {
527                       case NM_RETURN:
528                       case NM_DBLCLK:
529                       {
530                           Control_StartApplet(hWnd, Control_GetCPlItem_From_ListView(panel));
531                           return 0;
532                       }
533                       case LVN_ITEMCHANGED:
534                       {
535                           CPlItem *item = Control_GetCPlItem_From_ListView(panel);
536
537                           /* update the status bar if item is valid */
538                           if (item)
539                               SetWindowTextW(panel->hWndStatusBar,
540                                   item->applet->info[item->id].szInfo);
541                           else
542                               SetWindowTextW(panel->hWndStatusBar, NULL);
543
544                           return 0;
545                       }
546                   }
547
548                   break;
549           }
550
551           break;
552       }
553
554       case WM_MENUSELECT:
555           /* check if this is an applet */
556           if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
557               (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
558           {
559               CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
560
561               /* update the status bar if item is valid */
562               if (item)
563                   SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].szInfo);
564           }
565           else if ((HIWORD(lParam1) == 0xFFFF) && (lParam2 == 0))
566           {
567               /* reset status bar description to that of the selected icon */
568               CPlItem *item = Control_GetCPlItem_From_ListView(panel);
569
570               if (item)
571                   SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].szInfo);
572               else
573                   SetWindowTextW(panel->hWndStatusBar, NULL);
574
575               return 0;
576           }
577           else
578               SetWindowTextW(panel->hWndStatusBar, NULL);
579
580           return 0;
581
582       case WM_SIZE:
583       {
584           HDWP hdwp;
585           RECT sb;
586
587           hdwp = BeginDeferWindowPos(2);
588
589           if (hdwp == NULL)
590               break;
591
592           GetClientRect(panel->hWndStatusBar, &sb);
593
594           hdwp = DeferWindowPos(hdwp, panel->hWndListView, NULL, 0, 0,
595               LOWORD(lParam2), HIWORD(lParam2) - (sb.bottom - sb.top),
596               SWP_NOZORDER | SWP_NOMOVE);
597
598           if (hdwp == NULL)
599               break;
600
601           hdwp = DeferWindowPos(hdwp, panel->hWndStatusBar, NULL, 0, 0,
602               LOWORD(lParam2), LOWORD(lParam1), SWP_NOZORDER | SWP_NOMOVE);
603
604           if (hdwp != NULL)
605               EndDeferWindowPos(hdwp);
606
607           return 0;
608       }
609      }
610    }
611
612    return DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
613 }
614
615 static void    Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
616 {
617     WNDCLASSW   wc;
618     MSG         msg;
619     WCHAR appName[MAX_STRING_LEN];
620     const WCHAR className[] = {'S','h','e','l','l','_','C','o','n','t','r','o',
621         'l','_','W','n','d','C','l','a','s','s',0};
622
623     LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName, sizeof(appName) / sizeof(appName[0]));
624
625     wc.style = CS_HREDRAW|CS_VREDRAW;
626     wc.lpfnWndProc = Control_WndProc;
627     wc.cbClsExtra = 0;
628     wc.cbWndExtra = sizeof(CPlApplet*);
629     wc.hInstance = panel->hInst = hInst;
630     wc.hIcon = 0;
631     wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
632     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
633     wc.lpszMenuName = NULL;
634     wc.lpszClassName = className;
635
636     if (!RegisterClassW(&wc)) return;
637
638     CreateWindowExW(0, wc.lpszClassName, appName,
639                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
640                     CW_USEDEFAULT, CW_USEDEFAULT,
641                     CW_USEDEFAULT, CW_USEDEFAULT,
642                     hWnd, NULL, hInst, panel);
643     if (!panel->hWnd) return;
644
645     while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
646         TranslateMessage(&msg);
647         DispatchMessageW(&msg);
648     }
649 }
650
651 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
652 {
653     WCHAR name[MAX_PATH];
654     WCHAR value[MAX_PATH];
655     HKEY hkey;
656
657     if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
658     {
659         int idx = 0;
660
661         for(;; ++idx)
662         {
663             DWORD nameLen = MAX_PATH;
664             DWORD valueLen = MAX_PATH;
665
666             if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
667                 break;
668
669             Control_LoadApplet(hWnd, value, panel);
670         }
671         RegCloseKey(hkey);
672     }
673 }
674
675 static  void    Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
676 {
677     HANDLE              h;
678     WIN32_FIND_DATAW    fd;
679     WCHAR               buffer[MAX_PATH];
680     static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
681     static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
682             '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
683             '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
684     WCHAR *p;
685
686     /* first add .cpl files in the system directory */
687     GetSystemDirectoryW( buffer, MAX_PATH );
688     p = buffer + strlenW(buffer);
689     *p++ = '\\';
690     lstrcpyW(p, wszAllCpl);
691
692     if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
693         do {
694            lstrcpyW(p, fd.cFileName);
695            Control_LoadApplet(hWnd, buffer, panel);
696         } while (FindNextFileW(h, &fd));
697         FindClose(h);
698     }
699
700     /* now check for cpls in the registry */
701     Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
702     Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
703
704     Control_DoInterface(panel, hWnd, hInst);
705 }
706
707 static  void    Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
708    /* forms to parse:
709     *   foo.cpl,@sp,str
710     *   foo.cpl,@sp
711     *   foo.cpl,,str
712     *   foo.cpl @sp
713     *   foo.cpl str
714     *   "a path\foo.cpl"
715     */
716 {
717     LPWSTR      buffer;
718     LPWSTR      beg = NULL;
719     LPWSTR      end;
720     WCHAR       ch;
721     LPWSTR       ptr;
722     signed      sp = -1;
723     LPWSTR      extraPmtsBuf = NULL;
724     LPWSTR      extraPmts = NULL;
725     int        quoted = 0;
726
727     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
728     if (!buffer) return;
729
730     end = lstrcpyW(buffer, wszCmd);
731
732     for (;;) {
733         ch = *end;
734         if (ch == '"') quoted = !quoted;
735         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
736             *end = '\0';
737             if (beg) {
738                 if (*beg == '@') {
739                     sp = atoiW(beg + 1);
740                 } else if (*beg == '\0') {
741                     sp = -1;
742                 } else {
743                     extraPmtsBuf = beg;
744                 }
745             }
746             if (ch == '\0') break;
747             beg = end + 1;
748             if (ch == ' ') while (end[1] == ' ') end++;
749         }
750         end++;
751     }
752     while ((ptr = StrChrW(buffer, '"')))
753         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
754
755     /* now check for any quotes in extraPmtsBuf and remove */
756     if (extraPmtsBuf != NULL)
757     {
758         beg = end = extraPmtsBuf;
759         quoted = 0;
760
761         for (;;) {
762             ch = *end;
763             if (ch == '"') quoted = !quoted;
764             if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
765                 *end = '\0';
766                 if (beg) {
767                     if (*beg != '\0') {
768                         extraPmts = beg;
769                     }
770                 }
771                 if (ch == '\0') break;
772                     beg = end + 1;
773                 if (ch == ' ') while (end[1] == ' ') end++;
774             }
775             end++;
776         }
777
778         while ((ptr = StrChrW(extraPmts, '"')))
779             memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
780
781         if (extraPmts == NULL)
782             extraPmts = extraPmtsBuf;
783     }
784
785     /* Now check if there had been a numerical value in the extra params */
786     if ((extraPmts) && (*extraPmts == '@') && (sp == -1)) {
787         sp = atoiW(extraPmts + 1);
788     }
789
790     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
791
792     Control_LoadApplet(hWnd, buffer, panel);
793
794     if (panel->first) {
795         CPlApplet* applet = panel->first;
796
797         assert(applet && applet->next == NULL);
798
799         /* we've been given a textual parameter (or none at all) */
800         if (sp == -1) {
801             while ((++sp) != applet->count) {
802                 if (applet->info[sp].dwSize) {
803                     TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
804
805                     if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
806                         break;
807                 }
808             }
809         }
810
811         if (sp >= applet->count) {
812             WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
813             sp = 0;
814         }
815
816         if (applet->info[sp].dwSize) {
817             if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
818                 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
819         }
820
821         Control_UnloadApplet(applet);
822     }
823
824     HeapFree(GetProcessHeap(), 0, buffer);
825 }
826
827 /*************************************************************************
828  * Control_RunDLLW                      [SHELL32.@]
829  *
830  */
831 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
832 {
833     CPanel      panel;
834
835     TRACE("(%p, %p, %s, 0x%08x)\n",
836           hWnd, hInst, debugstr_w(cmd), nCmdShow);
837
838     memset(&panel, 0, sizeof(panel));
839
840     if (!cmd || !*cmd) {
841         Control_DoWindow(&panel, hWnd, hInst);
842     } else {
843         Control_DoLaunch(&panel, hWnd, cmd);
844     }
845 }
846
847 /*************************************************************************
848  * Control_RunDLLA                      [SHELL32.@]
849  *
850  */
851 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
852 {
853     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
854     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
855     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
856     {
857         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
858     }
859     HeapFree(GetProcessHeap(), 0, wszCmd);
860 }
861
862 /*************************************************************************
863  * Control_FillCache_RunDLLW                    [SHELL32.@]
864  *
865  */
866 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
867 {
868     FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
869     return 0;
870 }
871
872 /*************************************************************************
873  * Control_FillCache_RunDLLA                    [SHELL32.@]
874  *
875  */
876 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
877 {
878     return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
879 }
880
881
882 /*************************************************************************
883  * RunDLL_CallEntry16                           [SHELL32.122]
884  * the name is probably wrong
885  */
886 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
887                                 LPCSTR cmdline, INT cmdshow )
888 {
889 #ifdef __i386__
890     WORD args[5];
891     SEGPTR cmdline_seg;
892
893     TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
894            proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
895
896     cmdline_seg = MapLS( cmdline );
897     args[4] = HWND_16(hwnd);
898     args[3] = MapHModuleLS(inst);
899     args[2] = SELECTOROF(cmdline_seg);
900     args[1] = OFFSETOF(cmdline_seg);
901     args[0] = cmdshow;
902     WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
903     UnMapLS( cmdline_seg );
904 #endif
905 }
906
907 /*************************************************************************
908  * CallCPLEntry16                               [SHELL32.166]
909  *
910  * called by desk.cpl on "Advanced" with:
911  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
912  *
913  */
914 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
915 {
916     FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
917     return 0x0deadbee;
918 }