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