gdi32: Add an AlphaBlend entry point in the DIB driver.
[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/debug.h"
33 #include "cpl.h"
34 #include "wine/unicode.h"
35 #include "commctrl.h"
36
37 #define NO_SHLWAPI_REG
38 #include "shlwapi.h"
39
40 #include "cpanel.h"
41 #include "shresdef.h"
42 #include "shell32_main.h"
43
44 #define MAX_STRING_LEN      1024
45
46 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
47
48 CPlApplet*      Control_UnloadApplet(CPlApplet* applet)
49 {
50     unsigned    i;
51     CPlApplet*  next;
52
53     for (i = 0; i < applet->count; i++) {
54         if (!applet->info[i].dwSize) continue;
55         applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
56     }
57     if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
58     FreeLibrary(applet->hModule);
59     next = applet->next;
60     HeapFree(GetProcessHeap(), 0, applet->cmd);
61     HeapFree(GetProcessHeap(), 0, applet);
62     return next;
63 }
64
65 CPlApplet*      Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
66 {
67     CPlApplet*  applet;
68     unsigned    i;
69     CPLINFO     info;
70     NEWCPLINFOW newinfo;
71
72     if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
73        return applet;
74
75     applet->hWnd = hWnd;
76
77     if (!(applet->hModule = LoadLibraryW(cmd))) {
78         WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
79         goto theError;
80     }
81     if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
82         WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
83         goto theError;
84     }
85     if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
86         WARN("Init of applet has failed\n");
87         goto theError;
88     }
89     if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
90         WARN("No subprogram in applet\n");
91         goto theError;
92     }
93
94     applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
95                          sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
96
97     if (!(applet->cmd = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmd)+1) * sizeof(WCHAR)))) {
98         WARN("Cannot allocate memory for applet path\n");
99         goto theError;
100     }
101
102     lstrcpyW(applet->cmd, cmd);
103
104     for (i = 0; i < applet->count; i++) {
105        ZeroMemory(&newinfo, sizeof(newinfo));
106        newinfo.dwSize = sizeof(NEWCPLINFOA);
107        applet->info[i].dwSize = sizeof(NEWCPLINFOW);
108        applet->info[i].dwFlags = 0;
109        applet->info[i].dwHelpContext = 0;
110        applet->info[i].szHelpFile[0] = '\0';
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_INQUIRE, i, (LPARAM)&info);
117        applet->info[i].lData = info.lData;
118        if (info.idIcon != CPL_DYNAMIC_RES)
119            applet->info[i].hIcon = LoadIconW(applet->hModule,
120                                              MAKEINTRESOURCEW(info.idIcon));
121        if (info.idName != CPL_DYNAMIC_RES)
122            LoadStringW(applet->hModule, info.idName,
123                        applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
124        if (info.idInfo != CPL_DYNAMIC_RES)
125            LoadStringW(applet->hModule, info.idInfo,
126                        applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
127
128        /* some broken control panels seem to return incorrect values in CPL_INQUIRE,
129           but proper data in CPL_NEWINQUIRE. if we get an empty string or a null
130           icon, see what we can get from CPL_NEWINQUIRE */
131
132        if (lstrlenW(applet->info[i].szName) == 0)
133            info.idName = CPL_DYNAMIC_RES;
134
135        /* zero-length szInfo may not be a buggy applet, but it doesn't hurt for us
136           to check anyway */
137
138        if (lstrlenW(applet->info[i].szInfo) == 0)
139            info.idInfo = CPL_DYNAMIC_RES;
140
141        if (applet->info[i].hIcon == NULL)
142            info.idIcon = CPL_DYNAMIC_RES;
143
144        if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
145            (info.idInfo == CPL_DYNAMIC_RES)) {
146            applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
147
148            applet->info[i].dwFlags = newinfo.dwFlags;
149            applet->info[i].dwHelpContext = newinfo.dwHelpContext;
150            applet->info[i].lData = newinfo.lData;
151            if (info.idIcon == CPL_DYNAMIC_RES) {
152                if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
153                applet->info[i].hIcon = newinfo.hIcon;
154            }
155            if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
156                if (info.idName == CPL_DYNAMIC_RES)
157                    memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
158                if (info.idInfo == CPL_DYNAMIC_RES)
159                    memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
160                memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
161            } else {
162                if (info.idName == CPL_DYNAMIC_RES)
163                    MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
164                                        sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
165                                        applet->info[i].szName,
166                                        sizeof(applet->info[i].szName) / sizeof(WCHAR));
167                if (info.idInfo == CPL_DYNAMIC_RES)
168                    MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
169                                        sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
170                                        applet->info[i].szInfo,
171                                        sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
172                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
173                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
174                                    applet->info[i].szHelpFile,
175                                    sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
176            }
177        }
178     }
179
180     applet->next = panel->first;
181     panel->first = applet;
182
183     return applet;
184
185  theError:
186     Control_UnloadApplet(applet);
187     return NULL;
188 }
189
190 #define IDC_LISTVIEW        1000
191 #define IDC_STATUSBAR       1001
192
193 #define NUM_COLUMNS            2
194 #define LISTVIEW_DEFSTYLE   (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
195                              LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)
196
197 static BOOL Control_CreateListView (CPanel *panel)
198 {
199     RECT ws, sb;
200     WCHAR empty_string[] = {0};
201     WCHAR buf[MAX_STRING_LEN];
202     LVCOLUMNW lvc;
203
204     /* Create list view */
205     GetClientRect(panel->hWndStatusBar, &sb);
206     GetClientRect(panel->hWnd, &ws);
207
208     panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
209                           empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
210                           0, 0, ws.right - ws.left, ws.bottom - ws.top -
211                           (sb.bottom - sb.top), panel->hWnd,
212                           (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
213
214     if (!panel->hWndListView)
215         return FALSE;
216
217     /* Create image lists for list view */
218     panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
219         GetSystemMetrics(SM_CYSMICON), ILC_COLOR32 | ILC_MASK, 1, 1);
220     panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
221         GetSystemMetrics(SM_CYICON), ILC_COLOR32 | ILC_MASK, 1, 1);
222
223     SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)panel->hImageListSmall);
224     SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)panel->hImageListLarge);
225
226     /* Create columns for list view */
227     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
228     lvc.pszText = buf;
229     lvc.fmt = LVCFMT_LEFT;
230
231     /* Name column */
232     lvc.iSubItem = 0;
233     lvc.cx = (ws.right - ws.left) / 3;
234     LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));
235
236     if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
237         return FALSE;
238
239     /* Description column */
240     lvc.iSubItem = 1;
241     lvc.cx = ((ws.right - ws.left) / 3) * 2;
242     LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
243         sizeof(buf[0]));
244
245     if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
246         return FALSE;
247
248     return(TRUE);
249 }
250
251 static void      Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
252 {
253    CPanel* panel = cs->lpCreateParams;
254    HMENU hMenu, hSubMenu;
255    CPlApplet* applet;
256    MENUITEMINFOW mii;
257    unsigned int i;
258    int menucount, index;
259    CPlItem *item;
260    LVITEMW lvItem;
261    INITCOMMONCONTROLSEX icex;
262    INT sb_parts;
263    int itemidx;
264
265    SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
266    panel->hWnd = hWnd;
267
268    /* Initialise common control DLL */
269    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
270    icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
271    InitCommonControlsEx(&icex);
272
273    /* create the status bar */
274    if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
275        return;
276
277    sb_parts = -1;
278    SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);
279
280    /* create the list view */
281    if (!Control_CreateListView(panel))
282        return;
283
284    hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));
285
286    /* insert menu items for applets */
287    hSubMenu = GetSubMenu(hMenu, 0);
288    menucount = 0;
289
290    for (applet = panel->first; applet; applet = applet->next) {
291       for (i = 0; i < applet->count; i++) {
292          if (!applet->info[i].dwSize)
293             continue;
294
295          /* set up a CPlItem for this particular subprogram */
296          item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));
297
298          if (!item)
299             continue;
300
301          item->applet = applet;
302          item->id = i;
303
304          mii.cbSize = sizeof(MENUITEMINFOW);
305          mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
306          mii.dwTypeData = applet->info[i].szName;
307          mii.cch = sizeof(applet->info[i].szName) / sizeof(applet->info[i].szName[0]);
308          mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
309          mii.dwItemData = (ULONG_PTR)item;
310
311          if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
312             /* add the list view item */
313             index = ImageList_AddIcon(panel->hImageListLarge, applet->info[i].hIcon);
314             ImageList_AddIcon(panel->hImageListSmall, applet->info[i].hIcon);
315
316             lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
317             lvItem.iItem = menucount;
318             lvItem.iSubItem = 0;
319             lvItem.pszText = applet->info[i].szName;
320             lvItem.iImage = index;
321             lvItem.lParam = (LPARAM) item;
322
323             itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
324
325             /* add the description */
326             ListView_SetItemTextW(panel->hWndListView, itemidx, 1,
327                 applet->info[i].szInfo);
328
329             /* update menu bar, increment count */
330             DrawMenuBar(hWnd);
331             menucount++;
332          }
333       }
334    }
335
336    panel->total_subprogs = menucount;
337
338    /* check the "large items" icon in the View menu */
339    hSubMenu = GetSubMenu(hMenu, 1);
340    CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
341       FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);
342
343    SetMenu(hWnd, hMenu);
344 }
345
346 static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
347 {
348     HMENU hMenu, hSubMenu;
349     MENUITEMINFOW mii;
350     unsigned int i;
351
352     /* get the File menu */
353     hMenu = GetMenu(hWnd);
354
355     if (!hMenu)
356         return;
357
358     hSubMenu = GetSubMenu(hMenu, 0);
359
360     if (!hSubMenu)
361         return;
362
363     /* loop and free the item data */
364     for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
365     {
366         mii.cbSize = sizeof(MENUITEMINFOW);
367         mii.fMask = MIIM_DATA;
368
369         if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
370             continue;
371
372         HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
373     }
374 }
375
376 static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
377 {
378     HMENU hMenu, hSubMenu;
379
380     SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);
381
382     /* update the menu */
383     hMenu = GetMenu(panel->hWnd);
384     hSubMenu = GetSubMenu(hMenu, 1);
385
386     CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
387         id, MF_BYCOMMAND);
388 }
389
390 static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
391 {
392     HMENU hMenu, hSubMenu;
393     MENUITEMINFOW mii;
394
395     /* retrieve the CPlItem structure from the menu item data */
396     hMenu = GetMenu(hWnd);
397
398     if (!hMenu)
399         return NULL;
400
401     hSubMenu = GetSubMenu(hMenu, 0);
402
403     if (!hSubMenu)
404         return NULL;
405
406     mii.cbSize = sizeof(MENUITEMINFOW);
407     mii.fMask = MIIM_DATA;
408
409     if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
410         return NULL;
411
412     return (CPlItem *) mii.dwItemData;
413 }
414
415 static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
416 {
417     LVITEMW lvItem;
418     int selitem;
419
420     selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
421         | LVNI_SELECTED);
422
423     if (selitem != -1)
424     {
425         lvItem.iItem = selitem;
426         lvItem.mask = LVIF_PARAM;
427
428         if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
429             return (CPlItem *) lvItem.lParam;
430     }
431
432     return NULL;
433 }
434
435 static void Control_StartApplet(HWND hWnd, CPlItem *item)
436 {
437     WCHAR verbOpen[] = {'c','p','l','o','p','e','n',0};
438     WCHAR format[] = {'@','%','d',0};
439     WCHAR param[MAX_PATH];
440
441     /* execute the applet if item is valid */
442     if (item)
443     {
444         wsprintfW(param, format, item->id);
445         ShellExecuteW(hWnd, verbOpen, item->applet->cmd, param, NULL, SW_SHOW);
446     }
447 }
448
449 static LRESULT WINAPI   Control_WndProc(HWND hWnd, UINT wMsg,
450                                         WPARAM lParam1, LPARAM lParam2)
451 {
452    CPanel*      panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
453
454    if (panel || wMsg == WM_CREATE) {
455       switch (wMsg) {
456       case WM_CREATE:
457          Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
458          return 0;
459       case WM_DESTROY:
460          {
461             CPlApplet*  applet = panel->first;
462             while (applet)
463                applet = Control_UnloadApplet(applet);
464          }
465          Control_FreeCPlItems(hWnd, panel);
466          PostQuitMessage(0);
467          break;
468       case WM_COMMAND:
469          switch (LOWORD(lParam1))
470          {
471              case IDM_CPANEL_EXIT:
472                  SendMessageW(hWnd, WM_CLOSE, 0, 0);
473                  return 0;
474
475              case IDM_CPANEL_ABOUT:
476                  {
477                      WCHAR appName[MAX_STRING_LEN];
478                      HICON icon = LoadImageW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL),
479                                              IMAGE_ICON, 48, 48, LR_SHARED);
480
481                      LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
482                          sizeof(appName) / sizeof(appName[0]));
483                      ShellAboutW(hWnd, appName, NULL, icon);
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     WNDCLASSEXW 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.cbSize = sizeof(wc);
626     wc.style = CS_HREDRAW|CS_VREDRAW;
627     wc.lpfnWndProc = Control_WndProc;
628     wc.cbClsExtra = 0;
629     wc.cbWndExtra = sizeof(CPlApplet*);
630     wc.hInstance = panel->hInst = hInst;
631     wc.hIcon = LoadIconW( shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL) );
632     wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
633     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
634     wc.lpszMenuName = NULL;
635     wc.lpszClassName = className;
636     wc.hIconSm = LoadImageW( shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL), IMAGE_ICON,
637                              GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
638
639     if (!RegisterClassExW(&wc)) return;
640
641     CreateWindowExW(0, wc.lpszClassName, appName,
642                     WS_OVERLAPPEDWINDOW | WS_VISIBLE,
643                     CW_USEDEFAULT, CW_USEDEFAULT,
644                     CW_USEDEFAULT, CW_USEDEFAULT,
645                     hWnd, NULL, hInst, panel);
646     if (!panel->hWnd) return;
647
648     while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
649         TranslateMessage(&msg);
650         DispatchMessageW(&msg);
651     }
652 }
653
654 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
655 {
656     WCHAR name[MAX_PATH];
657     WCHAR value[MAX_PATH];
658     HKEY hkey;
659
660     if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
661     {
662         int idx = 0;
663
664         for(;; ++idx)
665         {
666             DWORD nameLen = MAX_PATH;
667             DWORD valueLen = MAX_PATH;
668
669             if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
670                 break;
671
672             Control_LoadApplet(hWnd, value, panel);
673         }
674         RegCloseKey(hkey);
675     }
676 }
677
678 static  void    Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
679 {
680     HANDLE              h;
681     WIN32_FIND_DATAW    fd;
682     WCHAR               buffer[MAX_PATH];
683     static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
684     static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
685             '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
686             '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
687     WCHAR *p;
688
689     /* first add .cpl files in the system directory */
690     GetSystemDirectoryW( buffer, MAX_PATH );
691     p = buffer + strlenW(buffer);
692     *p++ = '\\';
693     lstrcpyW(p, wszAllCpl);
694
695     if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
696         do {
697            lstrcpyW(p, fd.cFileName);
698            Control_LoadApplet(hWnd, buffer, panel);
699         } while (FindNextFileW(h, &fd));
700         FindClose(h);
701     }
702
703     /* now check for cpls in the registry */
704     Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
705     Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
706
707     Control_DoInterface(panel, hWnd, hInst);
708 }
709
710 static  void    Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
711    /* forms to parse:
712     *   foo.cpl,@sp,str
713     *   foo.cpl,@sp
714     *   foo.cpl,,str
715     *   foo.cpl @sp
716     *   foo.cpl str
717     *   "a path\foo.cpl"
718     */
719 {
720     LPWSTR      buffer;
721     LPWSTR      beg = NULL;
722     LPWSTR      end;
723     WCHAR       ch;
724     LPWSTR       ptr;
725     signed      sp = -1;
726     LPWSTR      extraPmtsBuf = NULL;
727     LPWSTR      extraPmts = NULL;
728     int        quoted = 0;
729
730     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
731     if (!buffer) return;
732
733     end = lstrcpyW(buffer, wszCmd);
734
735     for (;;) {
736         ch = *end;
737         if (ch == '"') quoted = !quoted;
738         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
739             *end = '\0';
740             if (beg) {
741                 if (*beg == '@') {
742                     sp = atoiW(beg + 1);
743                 } else if (*beg == '\0') {
744                     sp = -1;
745                 } else {
746                     extraPmtsBuf = beg;
747                 }
748             }
749             if (ch == '\0') break;
750             beg = end + 1;
751             if (ch == ' ') while (end[1] == ' ') end++;
752         }
753         end++;
754     }
755     while ((ptr = StrChrW(buffer, '"')))
756         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
757
758     /* now check for any quotes in extraPmtsBuf and remove */
759     if (extraPmtsBuf != NULL)
760     {
761         beg = end = extraPmtsBuf;
762         quoted = 0;
763
764         for (;;) {
765             ch = *end;
766             if (ch == '"') quoted = !quoted;
767             if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
768                 *end = '\0';
769                 if (beg) {
770                     if (*beg != '\0') {
771                         extraPmts = beg;
772                     }
773                 }
774                 if (ch == '\0') break;
775                     beg = end + 1;
776                 if (ch == ' ') while (end[1] == ' ') end++;
777             }
778             end++;
779         }
780
781         while ((ptr = StrChrW(extraPmts, '"')))
782             memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
783
784         if (extraPmts == NULL)
785             extraPmts = extraPmtsBuf;
786     }
787
788     /* Now check if there had been a numerical value in the extra params */
789     if ((extraPmts) && (*extraPmts == '@') && (sp == -1)) {
790         sp = atoiW(extraPmts + 1);
791     }
792
793     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
794
795     Control_LoadApplet(hWnd, buffer, panel);
796
797     if (panel->first) {
798         CPlApplet* applet = panel->first;
799
800         assert(applet && applet->next == NULL);
801
802         /* we've been given a textual parameter (or none at all) */
803         if (sp == -1) {
804             while ((++sp) != applet->count) {
805                 if (applet->info[sp].dwSize) {
806                     TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
807
808                     if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
809                         break;
810                 }
811             }
812         }
813
814         if (sp >= applet->count) {
815             WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
816             sp = 0;
817         }
818
819         if (applet->info[sp].dwSize) {
820             if (!applet->proc(applet->hWnd, CPL_STARTWPARMSW, sp, (LPARAM)extraPmts))
821                 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
822         }
823
824         Control_UnloadApplet(applet);
825     }
826
827     HeapFree(GetProcessHeap(), 0, buffer);
828 }
829
830 /*************************************************************************
831  * Control_RunDLLW                      [SHELL32.@]
832  *
833  */
834 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
835 {
836     CPanel      panel;
837
838     TRACE("(%p, %p, %s, 0x%08x)\n",
839           hWnd, hInst, debugstr_w(cmd), nCmdShow);
840
841     memset(&panel, 0, sizeof(panel));
842
843     if (!cmd || !*cmd) {
844         Control_DoWindow(&panel, hWnd, hInst);
845     } else {
846         Control_DoLaunch(&panel, hWnd, cmd);
847     }
848 }
849
850 /*************************************************************************
851  * Control_RunDLLA                      [SHELL32.@]
852  *
853  */
854 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
855 {
856     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
857     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
858     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
859     {
860         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
861     }
862     HeapFree(GetProcessHeap(), 0, wszCmd);
863 }
864
865 /*************************************************************************
866  * Control_FillCache_RunDLLW                    [SHELL32.@]
867  *
868  */
869 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
870 {
871     FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
872     return 0;
873 }
874
875 /*************************************************************************
876  * Control_FillCache_RunDLLA                    [SHELL32.@]
877  *
878  */
879 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
880 {
881     return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
882 }
883
884 /*************************************************************************
885  * CallCPLEntry16                               [SHELL32.166]
886  *
887  * called by desk.cpl on "Advanced" with:
888  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
889  *
890  */
891 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
892 {
893     FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
894     return 0x0deadbee;
895 }