shell32: Convert the control panel list to a standard list.
[wine.git] / dlls / shell32 / control.c
blobcb080d527ea604097fb31a95f3ecb110d3d3f501
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/debug.h"
33 #include "cpl.h"
34 #include "wine/unicode.h"
35 #include "commctrl.h"
37 #define NO_SHLWAPI_REG
38 #include "shlwapi.h"
40 #include "cpanel.h"
41 #include "shresdef.h"
42 #include "shell32_main.h"
44 #define MAX_STRING_LEN 1024
46 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
48 void Control_UnloadApplet(CPlApplet* applet)
50 unsigned i;
52 for (i = 0; i < applet->count; i++) {
53 if (!applet->info[i].valid) continue;
54 applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].data);
56 if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
57 FreeLibrary(applet->hModule);
58 list_remove( &applet->entry );
59 HeapFree(GetProcessHeap(), 0, applet->cmd);
60 HeapFree(GetProcessHeap(), 0, applet);
63 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
65 CPlApplet* applet;
66 unsigned i;
67 CPLINFO info;
68 NEWCPLINFOW newinfo;
70 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
71 return applet;
73 applet->hWnd = hWnd;
75 if (!(applet->hModule = LoadLibraryW(cmd))) {
76 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
77 goto theError;
79 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
80 WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
81 goto theError;
83 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
84 WARN("Init of applet has failed\n");
85 goto theError;
87 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
88 WARN("No subprogram in applet\n");
89 goto theError;
92 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
93 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
95 if (!(applet->cmd = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmd)+1) * sizeof(WCHAR)))) {
96 WARN("Cannot allocate memory for applet path\n");
97 goto theError;
100 lstrcpyW(applet->cmd, cmd);
102 for (i = 0; i < applet->count; i++) {
103 ZeroMemory(&newinfo, sizeof(newinfo));
104 newinfo.dwSize = sizeof(NEWCPLINFOA);
105 applet->info[i].valid = TRUE;
106 applet->info[i].helpfile[0] = 0;
107 /* proc is supposed to return a null value upon success for
108 * CPL_INQUIRE and CPL_NEWINQUIRE
109 * However, real drivers don't seem to behave like this
110 * So, use introspection rather than return value
112 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
113 applet->info[i].data = info.lData;
114 if (info.idIcon != CPL_DYNAMIC_RES)
115 applet->info[i].icon = LoadIconW(applet->hModule, MAKEINTRESOURCEW(info.idIcon));
116 if (info.idName != CPL_DYNAMIC_RES)
117 LoadStringW(applet->hModule, info.idName,
118 applet->info[i].name, sizeof(applet->info[i].name) / sizeof(WCHAR));
119 if (info.idInfo != CPL_DYNAMIC_RES)
120 LoadStringW(applet->hModule, info.idInfo,
121 applet->info[i].info, sizeof(applet->info[i].info) / sizeof(WCHAR));
123 /* some broken control panels seem to return incorrect values in CPL_INQUIRE,
124 but proper data in CPL_NEWINQUIRE. if we get an empty string or a null
125 icon, see what we can get from CPL_NEWINQUIRE */
127 if (!applet->info[i].name[0]) info.idName = CPL_DYNAMIC_RES;
129 /* zero-length szInfo may not be a buggy applet, but it doesn't hurt for us
130 to check anyway */
132 if (!applet->info[i].info[0]) info.idInfo = CPL_DYNAMIC_RES;
134 if (applet->info[i].icon == NULL)
135 info.idIcon = CPL_DYNAMIC_RES;
137 if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
138 (info.idInfo == CPL_DYNAMIC_RES)) {
139 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
141 applet->info[i].data = newinfo.lData;
142 if (info.idIcon == CPL_DYNAMIC_RES) {
143 if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
144 applet->info[i].icon = newinfo.hIcon;
146 if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
147 if (info.idName == CPL_DYNAMIC_RES)
148 memcpy(applet->info[i].name, newinfo.szName, sizeof(newinfo.szName));
149 if (info.idInfo == CPL_DYNAMIC_RES)
150 memcpy(applet->info[i].info, newinfo.szInfo, sizeof(newinfo.szInfo));
151 memcpy(applet->info[i].helpfile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
152 } else {
153 if (info.idName == CPL_DYNAMIC_RES)
154 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
155 sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
156 applet->info[i].name, sizeof(applet->info[i].name) / sizeof(WCHAR));
157 if (info.idInfo == CPL_DYNAMIC_RES)
158 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
159 sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
160 applet->info[i].info, sizeof(applet->info[i].info) / sizeof(WCHAR));
161 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
162 sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
163 applet->info[i].helpfile,
164 sizeof(applet->info[i].helpfile) / sizeof(WCHAR));
169 list_add_head( &panel->applets, &applet->entry );
170 return applet;
172 theError:
173 Control_UnloadApplet(applet);
174 return NULL;
177 #define IDC_LISTVIEW 1000
178 #define IDC_STATUSBAR 1001
180 #define NUM_COLUMNS 2
181 #define LISTVIEW_DEFSTYLE (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
182 LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)
184 static BOOL Control_CreateListView (CPanel *panel)
186 RECT ws, sb;
187 WCHAR empty_string[] = {0};
188 WCHAR buf[MAX_STRING_LEN];
189 LVCOLUMNW lvc;
191 /* Create list view */
192 GetClientRect(panel->hWndStatusBar, &sb);
193 GetClientRect(panel->hWnd, &ws);
195 panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
196 empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
197 0, 0, ws.right - ws.left, ws.bottom - ws.top -
198 (sb.bottom - sb.top), panel->hWnd,
199 (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
201 if (!panel->hWndListView)
202 return FALSE;
204 /* Create image lists for list view */
205 panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
206 GetSystemMetrics(SM_CYSMICON), ILC_COLOR32 | ILC_MASK, 1, 1);
207 panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
208 GetSystemMetrics(SM_CYICON), ILC_COLOR32 | ILC_MASK, 1, 1);
210 SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)panel->hImageListSmall);
211 SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)panel->hImageListLarge);
213 /* Create columns for list view */
214 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
215 lvc.pszText = buf;
216 lvc.fmt = LVCFMT_LEFT;
218 /* Name column */
219 lvc.iSubItem = 0;
220 lvc.cx = (ws.right - ws.left) / 3;
221 LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));
223 if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
224 return FALSE;
226 /* Description column */
227 lvc.iSubItem = 1;
228 lvc.cx = ((ws.right - ws.left) / 3) * 2;
229 LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
230 sizeof(buf[0]));
232 if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
233 return FALSE;
235 return(TRUE);
238 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
240 CPanel* panel = cs->lpCreateParams;
241 HMENU hMenu, hSubMenu;
242 CPlApplet* applet;
243 MENUITEMINFOW mii;
244 unsigned int i;
245 int menucount, index;
246 CPlItem *item;
247 LVITEMW lvItem;
248 INITCOMMONCONTROLSEX icex;
249 INT sb_parts;
250 int itemidx;
252 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
253 panel->hWnd = hWnd;
255 /* Initialise common control DLL */
256 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
257 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
258 InitCommonControlsEx(&icex);
260 /* create the status bar */
261 if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
262 return;
264 sb_parts = -1;
265 SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);
267 /* create the list view */
268 if (!Control_CreateListView(panel))
269 return;
271 hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));
273 /* insert menu items for applets */
274 hSubMenu = GetSubMenu(hMenu, 0);
275 menucount = 0;
277 LIST_FOR_EACH_ENTRY( applet, &panel->applets, CPlApplet, entry )
279 for (i = 0; i < applet->count; i++) {
280 if (!applet->info[i].valid)
281 continue;
283 /* set up a CPlItem for this particular subprogram */
284 item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));
286 if (!item)
287 continue;
289 item->applet = applet;
290 item->id = i;
292 mii.cbSize = sizeof(MENUITEMINFOW);
293 mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
294 mii.dwTypeData = applet->info[i].name;
295 mii.cch = sizeof(applet->info[i].name) / sizeof(WCHAR);
296 mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
297 mii.dwItemData = (ULONG_PTR)item;
299 if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
300 /* add the list view item */
301 HICON icon = applet->info[i].icon;
302 if (!icon) icon = LoadImageW( 0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 0, 0, LR_SHARED );
303 index = ImageList_AddIcon(panel->hImageListLarge, icon);
304 ImageList_AddIcon(panel->hImageListSmall, icon);
306 lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
307 lvItem.iItem = menucount;
308 lvItem.iSubItem = 0;
309 lvItem.pszText = applet->info[i].name;
310 lvItem.iImage = index;
311 lvItem.lParam = (LPARAM) item;
313 itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
315 /* add the description */
316 ListView_SetItemTextW(panel->hWndListView, itemidx, 1, applet->info[i].info);
318 /* update menu bar, increment count */
319 DrawMenuBar(hWnd);
320 menucount++;
325 panel->total_subprogs = menucount;
327 /* check the "large items" icon in the View menu */
328 hSubMenu = GetSubMenu(hMenu, 1);
329 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
330 FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);
332 SetMenu(hWnd, hMenu);
335 static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
337 HMENU hMenu, hSubMenu;
338 MENUITEMINFOW mii;
339 unsigned int i;
341 /* get the File menu */
342 hMenu = GetMenu(hWnd);
344 if (!hMenu)
345 return;
347 hSubMenu = GetSubMenu(hMenu, 0);
349 if (!hSubMenu)
350 return;
352 /* loop and free the item data */
353 for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
355 mii.cbSize = sizeof(MENUITEMINFOW);
356 mii.fMask = MIIM_DATA;
358 if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
359 continue;
361 HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
365 static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
367 HMENU hMenu, hSubMenu;
369 SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);
371 /* update the menu */
372 hMenu = GetMenu(panel->hWnd);
373 hSubMenu = GetSubMenu(hMenu, 1);
375 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
376 id, MF_BYCOMMAND);
379 static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
381 HMENU hMenu, hSubMenu;
382 MENUITEMINFOW mii;
384 /* retrieve the CPlItem structure from the menu item data */
385 hMenu = GetMenu(hWnd);
387 if (!hMenu)
388 return NULL;
390 hSubMenu = GetSubMenu(hMenu, 0);
392 if (!hSubMenu)
393 return NULL;
395 mii.cbSize = sizeof(MENUITEMINFOW);
396 mii.fMask = MIIM_DATA;
398 if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
399 return NULL;
401 return (CPlItem *) mii.dwItemData;
404 static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
406 LVITEMW lvItem;
407 int selitem;
409 selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
410 | LVNI_SELECTED);
412 if (selitem != -1)
414 lvItem.iItem = selitem;
415 lvItem.mask = LVIF_PARAM;
417 if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
418 return (CPlItem *) lvItem.lParam;
421 return NULL;
424 static void Control_StartApplet(HWND hWnd, CPlItem *item)
426 WCHAR verbOpen[] = {'c','p','l','o','p','e','n',0};
427 WCHAR format[] = {'@','%','d',0};
428 WCHAR param[MAX_PATH];
430 /* execute the applet if item is valid */
431 if (item)
433 wsprintfW(param, format, item->id);
434 ShellExecuteW(hWnd, verbOpen, item->applet->cmd, param, NULL, SW_SHOW);
438 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
439 WPARAM lParam1, LPARAM lParam2)
441 CPanel* panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
443 if (panel || wMsg == WM_CREATE) {
444 switch (wMsg) {
445 case WM_CREATE:
446 Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
447 return 0;
448 case WM_DESTROY:
450 CPlApplet *applet, *next;
451 LIST_FOR_EACH_ENTRY_SAFE( applet, next, &panel->applets, CPlApplet, entry )
452 Control_UnloadApplet(applet);
454 Control_FreeCPlItems(hWnd, panel);
455 PostQuitMessage(0);
456 break;
457 case WM_COMMAND:
458 switch (LOWORD(lParam1))
460 case IDM_CPANEL_EXIT:
461 SendMessageW(hWnd, WM_CLOSE, 0, 0);
462 return 0;
464 case IDM_CPANEL_ABOUT:
466 WCHAR appName[MAX_STRING_LEN];
467 HICON icon = LoadImageW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL),
468 IMAGE_ICON, 48, 48, LR_SHARED);
470 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
471 sizeof(appName) / sizeof(appName[0]));
472 ShellAboutW(hWnd, appName, NULL, icon);
474 return 0;
477 case FCIDM_SHVIEW_BIGICON:
478 Control_UpdateListViewStyle(panel, LVS_ICON, FCIDM_SHVIEW_BIGICON);
479 return 0;
481 case FCIDM_SHVIEW_SMALLICON:
482 Control_UpdateListViewStyle(panel, LVS_SMALLICON, FCIDM_SHVIEW_SMALLICON);
483 return 0;
485 case FCIDM_SHVIEW_LISTVIEW:
486 Control_UpdateListViewStyle(panel, LVS_LIST, FCIDM_SHVIEW_LISTVIEW);
487 return 0;
489 case FCIDM_SHVIEW_REPORTVIEW:
490 Control_UpdateListViewStyle(panel, LVS_REPORT, FCIDM_SHVIEW_REPORTVIEW);
491 return 0;
493 default:
494 /* check if this is an applet */
495 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
496 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
498 Control_StartApplet(hWnd, Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1)));
499 return 0;
502 break;
505 break;
507 case WM_NOTIFY:
509 LPNMHDR nmh = (LPNMHDR) lParam2;
511 switch (nmh->idFrom)
513 case IDC_LISTVIEW:
514 switch (nmh->code)
516 case NM_RETURN:
517 case NM_DBLCLK:
519 Control_StartApplet(hWnd, Control_GetCPlItem_From_ListView(panel));
520 return 0;
522 case LVN_ITEMCHANGED:
524 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
526 /* update the status bar if item is valid */
527 if (item)
528 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
529 else
530 SetWindowTextW(panel->hWndStatusBar, NULL);
532 return 0;
536 break;
539 break;
542 case WM_MENUSELECT:
543 /* check if this is an applet */
544 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
545 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
547 CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
549 /* update the status bar if item is valid */
550 if (item)
551 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
553 else if ((HIWORD(lParam1) == 0xFFFF) && (lParam2 == 0))
555 /* reset status bar description to that of the selected icon */
556 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
558 if (item)
559 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
560 else
561 SetWindowTextW(panel->hWndStatusBar, NULL);
563 return 0;
565 else
566 SetWindowTextW(panel->hWndStatusBar, NULL);
568 return 0;
570 case WM_SIZE:
572 HDWP hdwp;
573 RECT sb;
575 hdwp = BeginDeferWindowPos(2);
577 if (hdwp == NULL)
578 break;
580 GetClientRect(panel->hWndStatusBar, &sb);
582 hdwp = DeferWindowPos(hdwp, panel->hWndListView, NULL, 0, 0,
583 LOWORD(lParam2), HIWORD(lParam2) - (sb.bottom - sb.top),
584 SWP_NOZORDER | SWP_NOMOVE);
586 if (hdwp == NULL)
587 break;
589 hdwp = DeferWindowPos(hdwp, panel->hWndStatusBar, NULL, 0, 0,
590 LOWORD(lParam2), LOWORD(lParam1), SWP_NOZORDER | SWP_NOMOVE);
592 if (hdwp != NULL)
593 EndDeferWindowPos(hdwp);
595 return 0;
600 return DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
603 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
605 WNDCLASSEXW wc;
606 MSG msg;
607 WCHAR appName[MAX_STRING_LEN];
608 const WCHAR className[] = {'S','h','e','l','l','_','C','o','n','t','r','o',
609 'l','_','W','n','d','C','l','a','s','s',0};
611 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName, sizeof(appName) / sizeof(appName[0]));
613 wc.cbSize = sizeof(wc);
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 = LoadIconW( shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL) );
620 wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
621 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
622 wc.lpszMenuName = NULL;
623 wc.lpszClassName = className;
624 wc.hIconSm = LoadImageW( shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL), IMAGE_ICON,
625 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
627 if (!RegisterClassExW(&wc)) return;
629 CreateWindowExW(0, wc.lpszClassName, appName,
630 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
631 CW_USEDEFAULT, CW_USEDEFAULT,
632 CW_USEDEFAULT, CW_USEDEFAULT,
633 hWnd, NULL, hInst, panel);
634 if (!panel->hWnd) return;
636 while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
637 TranslateMessage(&msg);
638 DispatchMessageW(&msg);
642 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
644 WCHAR name[MAX_PATH];
645 WCHAR value[MAX_PATH];
646 HKEY hkey;
648 if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
650 int idx = 0;
652 for(;; ++idx)
654 DWORD nameLen = MAX_PATH;
655 DWORD valueLen = MAX_PATH;
657 if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
658 break;
660 Control_LoadApplet(hWnd, value, panel);
662 RegCloseKey(hkey);
666 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
668 HANDLE h;
669 WIN32_FIND_DATAW fd;
670 WCHAR buffer[MAX_PATH];
671 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
672 static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
673 '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
674 '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
675 WCHAR *p;
677 /* first add .cpl files in the system directory */
678 GetSystemDirectoryW( buffer, MAX_PATH );
679 p = buffer + strlenW(buffer);
680 *p++ = '\\';
681 lstrcpyW(p, wszAllCpl);
683 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
684 do {
685 lstrcpyW(p, fd.cFileName);
686 Control_LoadApplet(hWnd, buffer, panel);
687 } while (FindNextFileW(h, &fd));
688 FindClose(h);
691 /* now check for cpls in the registry */
692 Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
693 Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
695 Control_DoInterface(panel, hWnd, hInst);
698 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
699 /* forms to parse:
700 * foo.cpl,@sp,str
701 * foo.cpl,@sp
702 * foo.cpl,,str
703 * foo.cpl @sp
704 * foo.cpl str
705 * "a path\foo.cpl"
708 LPWSTR buffer;
709 LPWSTR beg = NULL;
710 LPWSTR end;
711 WCHAR ch;
712 LPWSTR ptr;
713 signed sp = -1;
714 LPWSTR extraPmtsBuf = NULL;
715 LPWSTR extraPmts = NULL;
716 int quoted = 0;
717 CPlApplet *applet;
719 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
720 if (!buffer) return;
722 end = lstrcpyW(buffer, wszCmd);
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;
738 if (ch == '\0') break;
739 beg = end + 1;
740 if (ch == ' ') while (end[1] == ' ') end++;
742 end++;
744 while ((ptr = StrChrW(buffer, '"')))
745 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
747 /* now check for any quotes in extraPmtsBuf and remove */
748 if (extraPmtsBuf != NULL)
750 beg = end = extraPmtsBuf;
751 quoted = 0;
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;
763 if (ch == '\0') break;
764 beg = end + 1;
765 if (ch == ' ') while (end[1] == ' ') end++;
767 end++;
770 while ((ptr = StrChrW(extraPmts, '"')))
771 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
773 if (extraPmts == NULL)
774 extraPmts = extraPmtsBuf;
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);
782 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
784 applet = Control_LoadApplet(hWnd, buffer, panel);
785 if (applet)
787 /* we've been given a textual parameter (or none at all) */
788 if (sp == -1) {
789 while ((++sp) != applet->count) {
790 if (applet->info[sp].valid) {
791 TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].name));
793 if (StrCmpIW(extraPmts, applet->info[sp].name) == 0)
794 break;
799 if (sp >= applet->count) {
800 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
801 sp = 0;
804 if (applet->info[sp].valid) {
805 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSW, sp, (LPARAM)extraPmts))
806 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].data);
809 Control_UnloadApplet(applet);
812 HeapFree(GetProcessHeap(), 0, buffer);
815 /*************************************************************************
816 * Control_RunDLLW [SHELL32.@]
819 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
821 CPanel panel;
823 TRACE("(%p, %p, %s, 0x%08x)\n",
824 hWnd, hInst, debugstr_w(cmd), nCmdShow);
826 memset(&panel, 0, sizeof(panel));
827 list_init( &panel.applets );
829 if (!cmd || !*cmd) {
830 Control_DoWindow(&panel, hWnd, hInst);
831 } else {
832 Control_DoLaunch(&panel, hWnd, cmd);
836 /*************************************************************************
837 * Control_RunDLLA [SHELL32.@]
840 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
842 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
843 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
844 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
846 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
848 HeapFree(GetProcessHeap(), 0, wszCmd);
851 /*************************************************************************
852 * Control_FillCache_RunDLLW [SHELL32.@]
855 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
857 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
858 return 0;
861 /*************************************************************************
862 * Control_FillCache_RunDLLA [SHELL32.@]
865 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
867 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
870 /*************************************************************************
871 * CallCPLEntry16 [SHELL32.166]
873 * called by desk.cpl on "Advanced" with:
874 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
877 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
879 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
880 return 0x0deadbee;