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