shell32: Try to guess the working directory in the run dialog.
[wine] / dlls / shell32 / control.c
1 /* Control Panel management
2  *
3  * Copyright 2001 Eric Pouech
4  * Copyright 2008 Owen Rudge
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #include "wine/winbase16.h"
33 #include "wownt32.h"
34 #include "wine/debug.h"
35 #include "cpl.h"
36 #include "wine/unicode.h"
37 #include "commctrl.h"
38
39 #define NO_SHLWAPI_REG
40 #include "shlwapi.h"
41
42 #include "cpanel.h"
43 #include "shresdef.h"
44 #include "shell32_main.h"
45
46 #define MAX_STRING_LEN      1024
47
48 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
49
50 CPlApplet*      Control_UnloadApplet(CPlApplet* applet)
51 {
52     unsigned    i;
53     CPlApplet*  next;
54
55     for (i = 0; i < applet->count; i++) {
56         if (!applet->info[i].dwSize) continue;
57         applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].lData);
58     }
59     if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
60     FreeLibrary(applet->hModule);
61     next = applet->next;
62     HeapFree(GetProcessHeap(), 0, applet);
63     return next;
64 }
65
66 CPlApplet*      Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
67 {
68     CPlApplet*  applet;
69     unsigned    i;
70     CPLINFO     info;
71     NEWCPLINFOW newinfo;
72
73     if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
74        return applet;
75
76     applet->hWnd = hWnd;
77
78     if (!(applet->hModule = LoadLibraryW(cmd))) {
79         WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
80         goto theError;
81     }
82     if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
83         WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
84         goto theError;
85     }
86     if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
87         WARN("Init of applet has failed\n");
88         goto theError;
89     }
90     if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
91         WARN("No subprogram in applet\n");
92         goto theError;
93     }
94
95     applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
96                          sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
97
98     for (i = 0; i < applet->count; i++) {
99        ZeroMemory(&newinfo, sizeof(newinfo));
100        newinfo.dwSize = sizeof(NEWCPLINFOA);
101        applet->info[i].dwSize = sizeof(NEWCPLINFOW);
102        applet->info[i].dwFlags = 0;
103        applet->info[i].dwHelpContext = 0;
104        applet->info[i].szHelpFile[0] = '\0';
105        /* proc is supposed to return a null value upon success for
106         * CPL_INQUIRE and CPL_NEWINQUIRE
107         * However, real drivers don't seem to behave like this
108         * So, use introspection rather than return value
109         */
110        applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
111        applet->info[i].lData = info.lData;
112        if (info.idIcon != CPL_DYNAMIC_RES)
113            applet->info[i].hIcon = LoadIconW(applet->hModule,
114                                              MAKEINTRESOURCEW(info.idIcon));
115        if (info.idName != CPL_DYNAMIC_RES)
116            LoadStringW(applet->hModule, info.idName,
117                        applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
118        if (info.idInfo != CPL_DYNAMIC_RES)
119            LoadStringW(applet->hModule, info.idInfo,
120                        applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / 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].szName == 0) || (lstrlenW(applet->info[i].szName) == 0))
127            info.idName = CPL_DYNAMIC_RES;
128
129        /* zero-length szInfo may not be a buggy applet, but it doesn't hurt for us
130           to check anyway */
131
132        if ((applet->info[i].szInfo == 0) || (lstrlenW(applet->info[i].szInfo) == 0))
133            info.idInfo = CPL_DYNAMIC_RES;
134
135        if (applet->info[i].hIcon == NULL)
136            info.idIcon = CPL_DYNAMIC_RES;
137
138        if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
139            (info.idInfo == CPL_DYNAMIC_RES)) {
140            applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
141
142            applet->info[i].dwFlags = newinfo.dwFlags;
143            applet->info[i].dwHelpContext = newinfo.dwHelpContext;
144            applet->info[i].lData = newinfo.lData;
145            if (info.idIcon == CPL_DYNAMIC_RES) {
146                if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
147                applet->info[i].hIcon = newinfo.hIcon;
148            }
149            if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
150                if (info.idName == CPL_DYNAMIC_RES)
151                    memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
152                if (info.idInfo == CPL_DYNAMIC_RES)
153                    memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
154                memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
155            } else {
156                if (info.idName == CPL_DYNAMIC_RES)
157                    MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
158                                        sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
159                                        applet->info[i].szName,
160                                        sizeof(applet->info[i].szName) / sizeof(WCHAR));
161                if (info.idInfo == CPL_DYNAMIC_RES)
162                    MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
163                                        sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
164                                        applet->info[i].szInfo,
165                                        sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
166                MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
167                                    sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
168                                    applet->info[i].szHelpFile,
169                                    sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
170            }
171        }
172     }
173
174     applet->next = panel->first;
175     panel->first = applet;
176
177     return applet;
178
179  theError:
180     Control_UnloadApplet(applet);
181     return NULL;
182 }
183
184 #define IDC_LISTVIEW        1000
185 #define IDC_STATUSBAR       1001
186
187 #define NUM_COLUMNS            2
188 #define LISTVIEW_DEFSTYLE   (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
189                              LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)
190
191 static BOOL Control_CreateListView (CPanel *panel)
192 {
193     RECT ws, sb;
194     WCHAR empty_string[] = {0};
195     WCHAR buf[MAX_STRING_LEN];
196     LVCOLUMNW lvc;
197
198     /* Create list view */
199     GetClientRect(panel->hWndStatusBar, &sb);
200     GetClientRect(panel->hWnd, &ws);
201
202     panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
203                           empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
204                           0, 0, ws.right - ws.left, ws.bottom - ws.top -
205                           (sb.bottom - sb.top), panel->hWnd,
206                           (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
207
208     if (!panel->hWndListView)
209         return FALSE;
210
211     /* Create image lists for list view */
212     panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
213         GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
214     panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
215         GetSystemMetrics(SM_CYICON), ILC_MASK, 1, 1);
216
217     (void) ListView_SetImageList(panel->hWndListView, panel->hImageListSmall, LVSIL_SMALL);
218     (void) ListView_SetImageList(panel->hWndListView, panel->hImageListLarge, LVSIL_NORMAL);
219
220     /* Create columns for list view */
221     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
222     lvc.pszText = buf;
223     lvc.fmt = LVCFMT_LEFT;
224
225     /* Name column */
226     lvc.iSubItem = 0;
227     lvc.cx = (ws.right - ws.left) / 3;
228     LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));
229
230     if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
231         return FALSE;
232
233     /* Description column */
234     lvc.iSubItem = 1;
235     lvc.cx = ((ws.right - ws.left) / 3) * 2;
236     LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
237         sizeof(buf[0]));
238
239     if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
240         return FALSE;
241
242     return(TRUE);
243 }
244
245 static void      Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
246 {
247    CPanel*      panel = (CPanel*)cs->lpCreateParams;
248    HMENU hMenu, hSubMenu;
249    CPlApplet*   applet;
250    MENUITEMINFOW mii;
251    int menucount, i, index;
252    CPlItem *item;
253    LVITEMW lvItem;
254    INITCOMMONCONTROLSEX icex;
255    INT sb_parts;
256    int itemidx;
257
258    SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
259    panel->hWnd = hWnd;
260
261    /* Initialise common control DLL */
262    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
263    icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
264    InitCommonControlsEx(&icex);
265
266    /* create the status bar */
267    if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
268        return;
269
270    sb_parts = -1;
271    SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);
272
273    /* create the list view */
274    if (!Control_CreateListView(panel))
275        return;
276
277    hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));
278
279    /* insert menu items for applets */
280    hSubMenu = GetSubMenu(hMenu, 0);
281    menucount = 0;
282
283    for (applet = panel->first; applet; applet = applet->next) {
284       for (i = 0; i < applet->count; i++) {
285          if (!applet->info[i].dwSize)
286             continue;
287
288          /* set up a CPlItem for this particular subprogram */
289          item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));
290
291          if (!item)
292             continue;
293
294          item->applet = (CPlApplet *) applet;
295          item->id = i;
296
297          mii.cbSize = sizeof(MENUITEMINFOW);
298          mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
299          mii.dwTypeData = applet->info[i].szName;
300          mii.cch = sizeof(applet->info[i].szName) / sizeof(applet->info[i].szName[0]);
301          mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
302          mii.dwItemData = (DWORD) item;
303
304          if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
305             /* add the list view item */
306             index = ImageList_AddIcon(panel->hImageListLarge, applet->info[i].hIcon);
307             ImageList_AddIcon(panel->hImageListSmall, applet->info[i].hIcon);
308
309             lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
310             lvItem.iItem = menucount;
311             lvItem.iSubItem = 0;
312             lvItem.pszText = applet->info[i].szName;
313             lvItem.iImage = index;
314             lvItem.lParam = (LPARAM) item;
315
316             itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
317
318             /* add the description */
319             ListView_SetItemTextW(panel->hWndListView, itemidx, 1,
320                 applet->info[i].szInfo);
321
322             /* update menu bar, increment count */
323             DrawMenuBar(hWnd);
324             menucount++;
325          }
326       }
327    }
328
329    panel->total_subprogs = menucount;
330
331    /* check the "large items" icon in the View menu */
332    hSubMenu = GetSubMenu(hMenu, 1);
333    CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
334       FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);
335
336    SetMenu(hWnd, hMenu);
337 }
338
339 static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
340 {
341     HMENU hMenu, hSubMenu;
342     MENUITEMINFOW mii;
343     int i;
344
345     /* get the File menu */
346     hMenu = GetMenu(hWnd);
347
348     if (!hMenu)
349         return;
350
351     hSubMenu = GetSubMenu(hMenu, 0);
352
353     if (!hSubMenu)
354         return;
355
356     /* loop and free the item data */
357     for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
358     {
359         mii.cbSize = sizeof(MENUITEMINFOW);
360         mii.fMask = MIIM_DATA;
361
362         if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
363             continue;
364
365         HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
366     }
367 }
368
369 static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
370 {
371     HMENU hMenu, hSubMenu;
372
373     SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);
374
375     /* update the menu */
376     hMenu = GetMenu(panel->hWnd);
377     hSubMenu = GetSubMenu(hMenu, 1);
378
379     CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
380         id, MF_BYCOMMAND);
381 }
382
383 static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
384 {
385     HMENU hMenu, hSubMenu;
386     MENUITEMINFOW mii;
387
388     /* retrieve the CPlItem structure from the menu item data */
389     hMenu = GetMenu(hWnd);
390
391     if (!hMenu)
392         return NULL;
393
394     hSubMenu = GetSubMenu(hMenu, 0);
395
396     if (!hSubMenu)
397         return NULL;
398
399     mii.cbSize = sizeof(MENUITEMINFOW);
400     mii.fMask = MIIM_DATA;
401
402     if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
403         return NULL;
404
405     return (CPlItem *) mii.dwItemData;
406 }
407
408 static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
409 {
410     LVITEMW lvItem;
411     int selitem;
412
413     selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
414         | LVNI_SELECTED);
415
416     if (selitem != -1)
417     {
418         lvItem.iItem = selitem;
419         lvItem.mask = LVIF_PARAM;
420
421         if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
422             return (CPlItem *) lvItem.lParam;
423     }
424
425     return NULL;
426 }
427
428 static LRESULT WINAPI   Control_WndProc(HWND hWnd, UINT wMsg,
429                                         WPARAM lParam1, LPARAM lParam2)
430 {
431    CPanel*      panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
432
433    if (panel || wMsg == WM_CREATE) {
434       switch (wMsg) {
435       case WM_CREATE:
436          Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
437          return 0;
438       case WM_DESTROY:
439          {
440             CPlApplet*  applet = panel->first;
441             while (applet)
442                applet = Control_UnloadApplet(applet);
443          }
444          Control_FreeCPlItems(hWnd, panel);
445          PostQuitMessage(0);
446          break;
447       case WM_COMMAND:
448          switch (LOWORD(lParam1))
449          {
450              case IDM_CPANEL_EXIT:
451                  SendMessageW(hWnd, WM_CLOSE, 0, 0);
452                  return 0;
453
454              case IDM_CPANEL_ABOUT:
455                  {
456                      WCHAR appName[MAX_STRING_LEN];
457
458                      LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
459                          sizeof(appName) / sizeof(appName[0]));
460                      ShellAboutW(hWnd, appName, NULL, NULL);
461
462                      return 0;
463                  }
464
465              case FCIDM_SHVIEW_BIGICON:
466                  Control_UpdateListViewStyle(panel, LVS_ICON, FCIDM_SHVIEW_BIGICON);
467                  return 0;
468
469              case FCIDM_SHVIEW_SMALLICON:
470                  Control_UpdateListViewStyle(panel, LVS_SMALLICON, FCIDM_SHVIEW_SMALLICON);
471                  return 0;
472
473              case FCIDM_SHVIEW_LISTVIEW:
474                  Control_UpdateListViewStyle(panel, LVS_LIST, FCIDM_SHVIEW_LISTVIEW);
475                  return 0;
476
477              case FCIDM_SHVIEW_REPORTVIEW:
478                  Control_UpdateListViewStyle(panel, LVS_REPORT, FCIDM_SHVIEW_REPORTVIEW);
479                  return 0;
480
481              default:
482                  /* check if this is an applet */
483                  if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
484                      (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
485                  {
486                      CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
487
488                      /* execute the applet if item is valid */
489                      if (item)
490                          item->applet->proc(item->applet->hWnd, CPL_DBLCLK, item->id,
491                              item->applet->info[item->id].lData);
492
493                      return 0;
494                  }
495
496                  break;
497          }
498
499          break;
500
501       case WM_NOTIFY:
502       {
503           LPNMHDR nmh = (LPNMHDR) lParam2;
504
505           switch (nmh->idFrom)
506           {
507               case IDC_LISTVIEW:
508                   switch (nmh->code)
509                   {
510                       case NM_RETURN:
511                       case NM_DBLCLK:
512                       {
513                           CPlItem *item = Control_GetCPlItem_From_ListView(panel);
514
515                           /* execute the applet if item is valid */
516                           if (item)
517                               item->applet->proc(item->applet->hWnd, CPL_DBLCLK,
518                                   item->id, item->applet->info[item->id].lData);
519
520                           return 0;
521                       }
522                       case LVN_ITEMCHANGED:
523                       {
524                           CPlItem *item = Control_GetCPlItem_From_ListView(panel);
525
526                           /* update the status bar if item is valid */
527                           if (item)
528                               SetWindowTextW(panel->hWndStatusBar,
529                                   item->applet->info[item->id].szInfo);
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].szInfo);
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].szInfo);
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     WNDCLASSW   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.style = CS_HREDRAW|CS_VREDRAW;
615     wc.lpfnWndProc = Control_WndProc;
616     wc.cbClsExtra = 0;
617     wc.cbWndExtra = sizeof(CPlApplet*);
618     wc.hInstance = panel->hInst = hInst;
619     wc.hIcon = 0;
620     wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
621     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
622     wc.lpszMenuName = NULL;
623     wc.lpszClassName = className;
624
625     if (!RegisterClassW(&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
716     buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
717     if (!buffer) return;
718
719     end = lstrcpyW(buffer, wszCmd);
720
721     for (;;) {
722         ch = *end;
723         if (ch == '"') quoted = !quoted;
724         if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
725             *end = '\0';
726             if (beg) {
727                 if (*beg == '@') {
728                     sp = atoiW(beg + 1);
729                 } else if (*beg == '\0') {
730                     sp = -1;
731                 } else {
732                     extraPmtsBuf = beg;
733                 }
734             }
735             if (ch == '\0') break;
736             beg = end + 1;
737             if (ch == ' ') while (end[1] == ' ') end++;
738         }
739         end++;
740     }
741     while ((ptr = StrChrW(buffer, '"')))
742         memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
743
744     /* now check for any quotes in extraPmtsBuf and remove */
745     if (extraPmtsBuf != NULL)
746     {
747         beg = end = extraPmtsBuf;
748         quoted = 0;
749
750         for (;;) {
751             ch = *end;
752             if (ch == '"') quoted = !quoted;
753             if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
754                 *end = '\0';
755                 if (beg) {
756                     if (*beg != '\0') {
757                         extraPmts = beg;
758                     }
759                 }
760                 if (ch == '\0') break;
761                     beg = end + 1;
762                 if (ch == ' ') while (end[1] == ' ') end++;
763             }
764             end++;
765         }
766
767         while ((ptr = StrChrW(extraPmts, '"')))
768             memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
769
770         if (extraPmts == NULL)
771             extraPmts = extraPmtsBuf;
772     }
773
774     TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
775
776     Control_LoadApplet(hWnd, buffer, panel);
777
778     if (panel->first) {
779         CPlApplet* applet = panel->first;
780
781         assert(applet && applet->next == NULL);
782
783         /* we've been given a textual parameter (or none at all) */
784         if (sp == -1) {
785             while ((++sp) != applet->count) {
786                 if (applet->info[sp].dwSize) {
787                     TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
788
789                     if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
790                         break;
791                 }
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->info[sp].dwSize) {
801             if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
802                 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
803         }
804
805         Control_UnloadApplet(applet);
806     }
807
808     HeapFree(GetProcessHeap(), 0, buffer);
809 }
810
811 /*************************************************************************
812  * Control_RunDLLW                      [SHELL32.@]
813  *
814  */
815 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
816 {
817     CPanel      panel;
818
819     TRACE("(%p, %p, %s, 0x%08x)\n",
820           hWnd, hInst, debugstr_w(cmd), nCmdShow);
821
822     memset(&panel, 0, sizeof(panel));
823
824     if (!cmd || !*cmd) {
825         Control_DoWindow(&panel, hWnd, hInst);
826     } else {
827         Control_DoLaunch(&panel, hWnd, cmd);
828     }
829 }
830
831 /*************************************************************************
832  * Control_RunDLLA                      [SHELL32.@]
833  *
834  */
835 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
836 {
837     DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
838     LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
839     if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
840     {
841         Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
842     }
843     HeapFree(GetProcessHeap(), 0, wszCmd);
844 }
845
846 /*************************************************************************
847  * Control_FillCache_RunDLLW                    [SHELL32.@]
848  *
849  */
850 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
851 {
852     FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
853     return 0;
854 }
855
856 /*************************************************************************
857  * Control_FillCache_RunDLLA                    [SHELL32.@]
858  *
859  */
860 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
861 {
862     return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
863 }
864
865
866 /*************************************************************************
867  * RunDLL_CallEntry16                           [SHELL32.122]
868  * the name is probably wrong
869  */
870 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
871                                 LPCSTR cmdline, INT cmdshow )
872 {
873     WORD args[5];
874     SEGPTR cmdline_seg;
875
876     TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
877            proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
878
879     cmdline_seg = MapLS( cmdline );
880     args[4] = HWND_16(hwnd);
881     args[3] = MapHModuleLS(inst);
882     args[2] = SELECTOROF(cmdline_seg);
883     args[1] = OFFSETOF(cmdline_seg);
884     args[0] = cmdshow;
885     WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
886     UnMapLS( cmdline_seg );
887 }
888
889 /*************************************************************************
890  * CallCPLEntry16                               [SHELL32.166]
891  *
892  * called by desk.cpl on "Advanced" with:
893  * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
894  *
895  */
896 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
897 {
898     FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
899     return 0x0deadbee;
900 }