regedit: Convert AddEntryToTree to unicode.
[wine/wine64.git] / dlls / shell32 / control.c
blobaa5abfe5b9c89b2f2313b98721d769455e062cbc
1 /* Control Panel management
3 * Copyright 2001 Eric Pouech
4 * Copyright 2008 Owen Rudge
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.
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.
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
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
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"
39 #define NO_SHLWAPI_REG
40 #include "shlwapi.h"
42 #include "cpanel.h"
43 #include "shresdef.h"
44 #include "shell32_main.h"
46 #define MAX_STRING_LEN 1024
48 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
50 CPlApplet* Control_UnloadApplet(CPlApplet* applet)
52 unsigned i;
53 CPlApplet* next;
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);
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;
66 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
68 CPlApplet* applet;
69 unsigned i;
70 CPLINFO info;
71 NEWCPLINFOW newinfo;
73 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
74 return applet;
76 applet->hWnd = hWnd;
78 if (!(applet->hModule = LoadLibraryW(cmd))) {
79 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
80 goto theError;
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;
86 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
87 WARN("Init of applet has failed\n");
88 goto theError;
90 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
91 WARN("No subprogram in applet\n");
92 goto theError;
95 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
96 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
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
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));
122 if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
123 (info.idInfo == CPL_DYNAMIC_RES)) {
124 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
126 applet->info[i].dwFlags = newinfo.dwFlags;
127 applet->info[i].dwHelpContext = newinfo.dwHelpContext;
128 applet->info[i].lData = newinfo.lData;
129 if (info.idIcon == CPL_DYNAMIC_RES) {
130 if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
131 applet->info[i].hIcon = newinfo.hIcon;
133 if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
134 if (info.idName == CPL_DYNAMIC_RES)
135 memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
136 if (info.idInfo == CPL_DYNAMIC_RES)
137 memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
138 memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
139 } else {
140 if (info.idName == CPL_DYNAMIC_RES)
141 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
142 sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
143 applet->info[i].szName,
144 sizeof(applet->info[i].szName) / sizeof(WCHAR));
145 if (info.idInfo == CPL_DYNAMIC_RES)
146 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
147 sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
148 applet->info[i].szInfo,
149 sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
150 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
151 sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
152 applet->info[i].szHelpFile,
153 sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
158 applet->next = panel->first;
159 panel->first = applet;
161 return applet;
163 theError:
164 Control_UnloadApplet(applet);
165 return NULL;
168 #define IDC_LISTVIEW 1000
169 #define IDC_STATUSBAR 1001
171 #define NUM_COLUMNS 2
172 #define LISTVIEW_DEFSTYLE (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
173 LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)
175 static BOOL Control_CreateListView (CPanel *panel)
177 RECT ws, sb;
178 WCHAR empty_string[] = {0};
179 WCHAR buf[MAX_STRING_LEN];
180 LVCOLUMNW lvc;
182 /* Create list view */
183 GetClientRect(panel->hWndStatusBar, &sb);
184 GetClientRect(panel->hWnd, &ws);
186 panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
187 empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
188 0, 0, ws.right - ws.left, ws.bottom - ws.top -
189 (sb.bottom - sb.top), panel->hWnd,
190 (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
192 if (!panel->hWndListView)
193 return FALSE;
195 /* Create image lists for list view */
196 panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
197 GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
198 panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
199 GetSystemMetrics(SM_CYICON), ILC_MASK, 1, 1);
201 (void) ListView_SetImageList(panel->hWndListView, panel->hImageListSmall, LVSIL_SMALL);
202 (void) ListView_SetImageList(panel->hWndListView, panel->hImageListLarge, LVSIL_NORMAL);
204 /* Create columns for list view */
205 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
206 lvc.pszText = buf;
207 lvc.fmt = LVCFMT_LEFT;
209 /* Name column */
210 lvc.iSubItem = 0;
211 lvc.cx = (ws.right - ws.left) / 3;
212 LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));
214 if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
215 return FALSE;
217 /* Description column */
218 lvc.iSubItem = 1;
219 lvc.cx = ((ws.right - ws.left) / 3) * 2;
220 LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
221 sizeof(buf[0]));
223 if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
224 return FALSE;
226 return(TRUE);
229 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
231 CPanel* panel = (CPanel*)cs->lpCreateParams;
232 HMENU hMenu, hSubMenu;
233 CPlApplet* applet;
234 MENUITEMINFOW mii;
235 int menucount, i, index;
236 CPlItem *item;
237 LVITEMW lvItem;
238 INITCOMMONCONTROLSEX icex;
239 INT sb_parts;
240 int itemidx;
242 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
243 panel->hWnd = hWnd;
245 /* Initialise common control DLL */
246 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
247 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
248 InitCommonControlsEx(&icex);
250 /* create the status bar */
251 if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
252 return;
254 sb_parts = -1;
255 SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);
257 /* create the list view */
258 if (!Control_CreateListView(panel))
259 return;
261 hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));
263 /* insert menu items for applets */
264 hSubMenu = GetSubMenu(hMenu, 0);
265 menucount = 0;
267 for (applet = panel->first; applet; applet = applet->next) {
268 for (i = 0; i < applet->count; i++) {
269 if (!applet->info[i].dwSize)
270 continue;
272 /* set up a CPlItem for this particular subprogram */
273 item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));
275 if (!item)
276 continue;
278 item->applet = (CPlApplet *) applet;
279 item->id = i;
281 mii.cbSize = sizeof(MENUITEMINFOW);
282 mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
283 mii.dwTypeData = applet->info[i].szName;
284 mii.cch = sizeof(applet->info[i].szName) / sizeof(applet->info[i].szName[0]);
285 mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
286 mii.dwItemData = (DWORD) item;
288 if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
289 /* add the list view item */
290 index = ImageList_AddIcon(panel->hImageListLarge, applet->info[i].hIcon);
291 ImageList_AddIcon(panel->hImageListSmall, applet->info[i].hIcon);
293 lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
294 lvItem.iItem = menucount;
295 lvItem.iSubItem = 0;
296 lvItem.pszText = applet->info[i].szName;
297 lvItem.iImage = index;
298 lvItem.lParam = (LPARAM) item;
300 itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
302 /* add the description */
303 ListView_SetItemTextW(panel->hWndListView, itemidx, 1,
304 applet->info[i].szInfo);
306 /* update menu bar, increment count */
307 DrawMenuBar(hWnd);
308 menucount++;
313 panel->total_subprogs = menucount;
315 /* check the "large items" icon in the View menu */
316 hSubMenu = GetSubMenu(hMenu, 1);
317 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
318 FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);
320 SetMenu(hWnd, hMenu);
323 static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
325 HMENU hMenu, hSubMenu;
326 MENUITEMINFOW mii;
327 int i;
329 /* get the File menu */
330 hMenu = GetMenu(hWnd);
332 if (!hMenu)
333 return;
335 hSubMenu = GetSubMenu(hMenu, 0);
337 if (!hSubMenu)
338 return;
340 /* loop and free the item data */
341 for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
343 mii.cbSize = sizeof(MENUITEMINFOW);
344 mii.fMask = MIIM_DATA;
346 if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
347 continue;
349 HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
353 static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
355 HMENU hMenu, hSubMenu;
357 SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);
359 /* update the menu */
360 hMenu = GetMenu(panel->hWnd);
361 hSubMenu = GetSubMenu(hMenu, 1);
363 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
364 id, MF_BYCOMMAND);
367 static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
369 HMENU hMenu, hSubMenu;
370 MENUITEMINFOW mii;
372 /* retrieve the CPlItem structure from the menu item data */
373 hMenu = GetMenu(hWnd);
375 if (!hMenu)
376 return NULL;
378 hSubMenu = GetSubMenu(hMenu, 0);
380 if (!hSubMenu)
381 return NULL;
383 mii.cbSize = sizeof(MENUITEMINFOW);
384 mii.fMask = MIIM_DATA;
386 if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
387 return NULL;
389 return (CPlItem *) mii.dwItemData;
392 static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
394 LVITEMW lvItem;
395 int selitem;
397 selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
398 | LVNI_SELECTED);
400 if (selitem != -1)
402 lvItem.iItem = selitem;
403 lvItem.mask = LVIF_PARAM;
405 if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
406 return (CPlItem *) lvItem.lParam;
409 return NULL;
412 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
413 WPARAM lParam1, LPARAM lParam2)
415 CPanel* panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
417 if (panel || wMsg == WM_CREATE) {
418 switch (wMsg) {
419 case WM_CREATE:
420 Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
421 return 0;
422 case WM_DESTROY:
424 CPlApplet* applet = panel->first;
425 while (applet)
426 applet = Control_UnloadApplet(applet);
428 Control_FreeCPlItems(hWnd, panel);
429 PostQuitMessage(0);
430 break;
431 case WM_COMMAND:
432 switch (LOWORD(lParam1))
434 case IDM_CPANEL_EXIT:
435 SendMessageW(hWnd, WM_CLOSE, 0, 0);
436 return 0;
438 case IDM_CPANEL_ABOUT:
440 WCHAR appName[MAX_STRING_LEN];
442 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
443 sizeof(appName) / sizeof(appName[0]));
444 ShellAboutW(hWnd, appName, NULL, NULL);
446 return 0;
449 case FCIDM_SHVIEW_BIGICON:
450 Control_UpdateListViewStyle(panel, LVS_ICON, FCIDM_SHVIEW_BIGICON);
451 return 0;
453 case FCIDM_SHVIEW_SMALLICON:
454 Control_UpdateListViewStyle(panel, LVS_SMALLICON, FCIDM_SHVIEW_SMALLICON);
455 return 0;
457 case FCIDM_SHVIEW_LISTVIEW:
458 Control_UpdateListViewStyle(panel, LVS_LIST, FCIDM_SHVIEW_LISTVIEW);
459 return 0;
461 case FCIDM_SHVIEW_REPORTVIEW:
462 Control_UpdateListViewStyle(panel, LVS_REPORT, FCIDM_SHVIEW_REPORTVIEW);
463 return 0;
465 default:
466 /* check if this is an applet */
467 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
468 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
470 CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
472 /* execute the applet if item is valid */
473 if (item)
474 item->applet->proc(item->applet->hWnd, CPL_DBLCLK, item->id,
475 item->applet->info[item->id].lData);
477 return 0;
480 break;
483 break;
485 case WM_NOTIFY:
487 LPNMHDR nmh = (LPNMHDR) lParam2;
489 switch (nmh->idFrom)
491 case IDC_LISTVIEW:
492 switch (nmh->code)
494 case NM_RETURN:
495 case NM_DBLCLK:
497 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
499 /* execute the applet if item is valid */
500 if (item)
501 item->applet->proc(item->applet->hWnd, CPL_DBLCLK,
502 item->id, item->applet->info[item->id].lData);
504 return 0;
506 case LVN_ITEMCHANGED:
508 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
510 /* update the status bar if item is valid */
511 if (item)
512 SetWindowTextW(panel->hWndStatusBar,
513 item->applet->info[item->id].szInfo);
514 else
515 SetWindowTextW(panel->hWndStatusBar, NULL);
517 return 0;
521 break;
524 break;
527 case WM_MENUSELECT:
528 /* check if this is an applet */
529 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
530 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
532 CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
534 /* update the status bar if item is valid */
535 if (item)
536 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].szInfo);
538 else if ((HIWORD(lParam1) == 0xFFFF) && (lParam2 == 0))
540 /* reset status bar description to that of the selected icon */
541 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
543 if (item)
544 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].szInfo);
545 else
546 SetWindowTextW(panel->hWndStatusBar, NULL);
548 return 0;
550 else
551 SetWindowTextW(panel->hWndStatusBar, NULL);
553 return 0;
555 case WM_SIZE:
557 HDWP hdwp;
558 RECT sb;
560 hdwp = BeginDeferWindowPos(2);
562 if (hdwp == NULL)
563 break;
565 GetClientRect(panel->hWndStatusBar, &sb);
567 hdwp = DeferWindowPos(hdwp, panel->hWndListView, NULL, 0, 0,
568 LOWORD(lParam2), HIWORD(lParam2) - (sb.bottom - sb.top),
569 SWP_NOZORDER | SWP_NOMOVE);
571 if (hdwp == NULL)
572 break;
574 hdwp = DeferWindowPos(hdwp, panel->hWndStatusBar, NULL, 0, 0,
575 LOWORD(lParam2), LOWORD(lParam1), SWP_NOZORDER | SWP_NOMOVE);
577 if (hdwp != NULL)
578 EndDeferWindowPos(hdwp);
580 return 0;
585 return DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
588 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
590 WNDCLASSW wc;
591 MSG msg;
592 WCHAR appName[MAX_STRING_LEN];
593 const WCHAR className[] = {'S','h','e','l','l','_','C','o','n','t','r','o',
594 'l','_','W','n','d','C','l','a','s','s',0};
596 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName, sizeof(appName) / sizeof(appName[0]));
598 wc.style = CS_HREDRAW|CS_VREDRAW;
599 wc.lpfnWndProc = Control_WndProc;
600 wc.cbClsExtra = 0;
601 wc.cbWndExtra = sizeof(CPlApplet*);
602 wc.hInstance = panel->hInst = hInst;
603 wc.hIcon = 0;
604 wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
605 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
606 wc.lpszMenuName = NULL;
607 wc.lpszClassName = className;
609 if (!RegisterClassW(&wc)) return;
611 CreateWindowExW(0, wc.lpszClassName, appName,
612 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
613 CW_USEDEFAULT, CW_USEDEFAULT,
614 CW_USEDEFAULT, CW_USEDEFAULT,
615 hWnd, NULL, hInst, panel);
616 if (!panel->hWnd) return;
618 while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
619 TranslateMessage(&msg);
620 DispatchMessageW(&msg);
624 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
626 WCHAR name[MAX_PATH];
627 WCHAR value[MAX_PATH];
628 HKEY hkey;
630 if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
632 int idx = 0;
634 for(;; ++idx)
636 DWORD nameLen = MAX_PATH;
637 DWORD valueLen = MAX_PATH;
639 if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
640 break;
642 Control_LoadApplet(hWnd, value, panel);
644 RegCloseKey(hkey);
648 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
650 HANDLE h;
651 WIN32_FIND_DATAW fd;
652 WCHAR buffer[MAX_PATH];
653 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
654 static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
655 '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
656 '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
657 WCHAR *p;
659 /* first add .cpl files in the system directory */
660 GetSystemDirectoryW( buffer, MAX_PATH );
661 p = buffer + strlenW(buffer);
662 *p++ = '\\';
663 lstrcpyW(p, wszAllCpl);
665 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
666 do {
667 lstrcpyW(p, fd.cFileName);
668 Control_LoadApplet(hWnd, buffer, panel);
669 } while (FindNextFileW(h, &fd));
670 FindClose(h);
673 /* now check for cpls in the registry */
674 Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
675 Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
677 Control_DoInterface(panel, hWnd, hInst);
680 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
681 /* forms to parse:
682 * foo.cpl,@sp,str
683 * foo.cpl,@sp
684 * foo.cpl,,str
685 * foo.cpl @sp
686 * foo.cpl str
687 * "a path\foo.cpl"
690 LPWSTR buffer;
691 LPWSTR beg = NULL;
692 LPWSTR end;
693 WCHAR ch;
694 LPWSTR ptr;
695 signed sp = -1;
696 LPWSTR extraPmtsBuf = NULL;
697 LPWSTR extraPmts = NULL;
698 int quoted = 0;
700 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
701 if (!buffer) return;
703 end = lstrcpyW(buffer, wszCmd);
705 for (;;) {
706 ch = *end;
707 if (ch == '"') quoted = !quoted;
708 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
709 *end = '\0';
710 if (beg) {
711 if (*beg == '@') {
712 sp = atoiW(beg + 1);
713 } else if (*beg == '\0') {
714 sp = -1;
715 } else {
716 extraPmtsBuf = beg;
719 if (ch == '\0') break;
720 beg = end + 1;
721 if (ch == ' ') while (end[1] == ' ') end++;
723 end++;
725 while ((ptr = StrChrW(buffer, '"')))
726 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
728 /* now check for any quotes in extraPmtsBuf and remove */
729 if (extraPmtsBuf != NULL)
731 beg = end = extraPmtsBuf;
732 quoted = 0;
734 for (;;) {
735 ch = *end;
736 if (ch == '"') quoted = !quoted;
737 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
738 *end = '\0';
739 if (beg) {
740 if (*beg != '\0') {
741 extraPmts = beg;
744 if (ch == '\0') break;
745 beg = end + 1;
746 if (ch == ' ') while (end[1] == ' ') end++;
748 end++;
751 while ((ptr = StrChrW(extraPmts, '"')))
752 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
754 if (extraPmts == NULL)
755 extraPmts = extraPmtsBuf;
758 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
760 Control_LoadApplet(hWnd, buffer, panel);
762 if (panel->first) {
763 CPlApplet* applet = panel->first;
765 assert(applet && applet->next == NULL);
767 /* we've been given a textual parameter (or none at all) */
768 if (sp == -1) {
769 while ((++sp) != applet->count) {
770 if (applet->info[sp].dwSize) {
771 TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
773 if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
774 break;
779 if (sp >= applet->count) {
780 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
781 sp = 0;
784 if (applet->info[sp].dwSize) {
785 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
786 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
789 Control_UnloadApplet(applet);
792 HeapFree(GetProcessHeap(), 0, buffer);
795 /*************************************************************************
796 * Control_RunDLLW [SHELL32.@]
799 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
801 CPanel panel;
803 TRACE("(%p, %p, %s, 0x%08x)\n",
804 hWnd, hInst, debugstr_w(cmd), nCmdShow);
806 memset(&panel, 0, sizeof(panel));
808 if (!cmd || !*cmd) {
809 Control_DoWindow(&panel, hWnd, hInst);
810 } else {
811 Control_DoLaunch(&panel, hWnd, cmd);
815 /*************************************************************************
816 * Control_RunDLLA [SHELL32.@]
819 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
821 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
822 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
823 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
825 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
827 HeapFree(GetProcessHeap(), 0, wszCmd);
830 /*************************************************************************
831 * Control_FillCache_RunDLLW [SHELL32.@]
834 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
836 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
837 return 0;
840 /*************************************************************************
841 * Control_FillCache_RunDLLA [SHELL32.@]
844 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
846 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
850 /*************************************************************************
851 * RunDLL_CallEntry16 [SHELL32.122]
852 * the name is probably wrong
854 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
855 LPCSTR cmdline, INT cmdshow )
857 WORD args[5];
858 SEGPTR cmdline_seg;
860 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
861 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
863 cmdline_seg = MapLS( cmdline );
864 args[4] = HWND_16(hwnd);
865 args[3] = MapHModuleLS(inst);
866 args[2] = SELECTOROF(cmdline_seg);
867 args[1] = OFFSETOF(cmdline_seg);
868 args[0] = cmdshow;
869 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
870 UnMapLS( cmdline_seg );
873 /*************************************************************************
874 * CallCPLEntry16 [SHELL32.166]
876 * called by desk.cpl on "Advanced" with:
877 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
880 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
882 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
883 return 0x0deadbee;