dplayx: Code to parse PlayerEnumeration messages
[wine/gsoc_dplay.git] / dlls / shell32 / control.c
blob9b566b65ed24411d03dd4a51c2c34d5dd9f37f0e
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->cmd);
63 HeapFree(GetProcessHeap(), 0, applet);
64 return next;
67 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
69 CPlApplet* applet;
70 unsigned i;
71 CPLINFO info;
72 NEWCPLINFOW newinfo;
74 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
75 return applet;
77 applet->hWnd = hWnd;
79 if (!(applet->hModule = LoadLibraryW(cmd))) {
80 WARN("Cannot load control panel applet %s\n", debugstr_w(cmd));
81 goto theError;
83 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
84 WARN("Not a valid control panel applet %s\n", debugstr_w(cmd));
85 goto theError;
87 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
88 WARN("Init of applet has failed\n");
89 goto theError;
91 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
92 WARN("No subprogram in applet\n");
93 goto theError;
96 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
97 sizeof(*applet) + (applet->count - 1) * sizeof(NEWCPLINFOW));
99 if (!(applet->cmd = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmd)+1) * sizeof(WCHAR)))) {
100 WARN("Cannot allocate memory for applet path\n");
101 goto theError;
104 lstrcpyW(applet->cmd, cmd);
106 for (i = 0; i < applet->count; i++) {
107 ZeroMemory(&newinfo, sizeof(newinfo));
108 newinfo.dwSize = sizeof(NEWCPLINFOA);
109 applet->info[i].dwSize = sizeof(NEWCPLINFOW);
110 applet->info[i].dwFlags = 0;
111 applet->info[i].dwHelpContext = 0;
112 applet->info[i].szHelpFile[0] = '\0';
113 /* proc is supposed to return a null value upon success for
114 * CPL_INQUIRE and CPL_NEWINQUIRE
115 * However, real drivers don't seem to behave like this
116 * So, use introspection rather than return value
118 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
119 applet->info[i].lData = info.lData;
120 if (info.idIcon != CPL_DYNAMIC_RES)
121 applet->info[i].hIcon = LoadIconW(applet->hModule,
122 MAKEINTRESOURCEW(info.idIcon));
123 if (info.idName != CPL_DYNAMIC_RES)
124 LoadStringW(applet->hModule, info.idName,
125 applet->info[i].szName, sizeof(applet->info[i].szName) / sizeof(WCHAR));
126 if (info.idInfo != CPL_DYNAMIC_RES)
127 LoadStringW(applet->hModule, info.idInfo,
128 applet->info[i].szInfo, sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
130 /* some broken control panels seem to return incorrect values in CPL_INQUIRE,
131 but proper data in CPL_NEWINQUIRE. if we get an empty string or a null
132 icon, see what we can get from CPL_NEWINQUIRE */
134 if ((applet->info[i].szName == 0) || (lstrlenW(applet->info[i].szName) == 0))
135 info.idName = CPL_DYNAMIC_RES;
137 /* zero-length szInfo may not be a buggy applet, but it doesn't hurt for us
138 to check anyway */
140 if ((applet->info[i].szInfo == 0) || (lstrlenW(applet->info[i].szInfo) == 0))
141 info.idInfo = CPL_DYNAMIC_RES;
143 if (applet->info[i].hIcon == NULL)
144 info.idIcon = CPL_DYNAMIC_RES;
146 if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
147 (info.idInfo == CPL_DYNAMIC_RES)) {
148 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
150 applet->info[i].dwFlags = newinfo.dwFlags;
151 applet->info[i].dwHelpContext = newinfo.dwHelpContext;
152 applet->info[i].lData = newinfo.lData;
153 if (info.idIcon == CPL_DYNAMIC_RES) {
154 if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
155 applet->info[i].hIcon = newinfo.hIcon;
157 if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
158 if (info.idName == CPL_DYNAMIC_RES)
159 memcpy(applet->info[i].szName, newinfo.szName, sizeof(newinfo.szName));
160 if (info.idInfo == CPL_DYNAMIC_RES)
161 memcpy(applet->info[i].szInfo, newinfo.szInfo, sizeof(newinfo.szInfo));
162 memcpy(applet->info[i].szHelpFile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
163 } else {
164 if (info.idName == CPL_DYNAMIC_RES)
165 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
166 sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
167 applet->info[i].szName,
168 sizeof(applet->info[i].szName) / sizeof(WCHAR));
169 if (info.idInfo == CPL_DYNAMIC_RES)
170 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
171 sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
172 applet->info[i].szInfo,
173 sizeof(applet->info[i].szInfo) / sizeof(WCHAR));
174 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
175 sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
176 applet->info[i].szHelpFile,
177 sizeof(applet->info[i].szHelpFile) / sizeof(WCHAR));
182 applet->next = panel->first;
183 panel->first = applet;
185 return applet;
187 theError:
188 Control_UnloadApplet(applet);
189 return NULL;
192 #define IDC_LISTVIEW 1000
193 #define IDC_STATUSBAR 1001
195 #define NUM_COLUMNS 2
196 #define LISTVIEW_DEFSTYLE (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
197 LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)
199 static BOOL Control_CreateListView (CPanel *panel)
201 RECT ws, sb;
202 WCHAR empty_string[] = {0};
203 WCHAR buf[MAX_STRING_LEN];
204 LVCOLUMNW lvc;
206 /* Create list view */
207 GetClientRect(panel->hWndStatusBar, &sb);
208 GetClientRect(panel->hWnd, &ws);
210 panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
211 empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
212 0, 0, ws.right - ws.left, ws.bottom - ws.top -
213 (sb.bottom - sb.top), panel->hWnd,
214 (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
216 if (!panel->hWndListView)
217 return FALSE;
219 /* Create image lists for list view */
220 panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
221 GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
222 panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
223 GetSystemMetrics(SM_CYICON), ILC_MASK, 1, 1);
225 SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)panel->hImageListSmall);
226 SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)panel->hImageListLarge);
228 /* Create columns for list view */
229 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
230 lvc.pszText = buf;
231 lvc.fmt = LVCFMT_LEFT;
233 /* Name column */
234 lvc.iSubItem = 0;
235 lvc.cx = (ws.right - ws.left) / 3;
236 LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));
238 if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
239 return FALSE;
241 /* Description column */
242 lvc.iSubItem = 1;
243 lvc.cx = ((ws.right - ws.left) / 3) * 2;
244 LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
245 sizeof(buf[0]));
247 if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
248 return FALSE;
250 return(TRUE);
253 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
255 CPanel* panel = cs->lpCreateParams;
256 HMENU hMenu, hSubMenu;
257 CPlApplet* applet;
258 MENUITEMINFOW mii;
259 unsigned int i;
260 int menucount, index;
261 CPlItem *item;
262 LVITEMW lvItem;
263 INITCOMMONCONTROLSEX icex;
264 INT sb_parts;
265 int itemidx;
267 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
268 panel->hWnd = hWnd;
270 /* Initialise common control DLL */
271 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
272 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
273 InitCommonControlsEx(&icex);
275 /* create the status bar */
276 if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
277 return;
279 sb_parts = -1;
280 SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);
282 /* create the list view */
283 if (!Control_CreateListView(panel))
284 return;
286 hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));
288 /* insert menu items for applets */
289 hSubMenu = GetSubMenu(hMenu, 0);
290 menucount = 0;
292 for (applet = panel->first; applet; applet = applet->next) {
293 for (i = 0; i < applet->count; i++) {
294 if (!applet->info[i].dwSize)
295 continue;
297 /* set up a CPlItem for this particular subprogram */
298 item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));
300 if (!item)
301 continue;
303 item->applet = applet;
304 item->id = i;
306 mii.cbSize = sizeof(MENUITEMINFOW);
307 mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
308 mii.dwTypeData = applet->info[i].szName;
309 mii.cch = sizeof(applet->info[i].szName) / sizeof(applet->info[i].szName[0]);
310 mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
311 mii.dwItemData = (ULONG_PTR)item;
313 if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
314 /* add the list view item */
315 index = ImageList_AddIcon(panel->hImageListLarge, applet->info[i].hIcon);
316 ImageList_AddIcon(panel->hImageListSmall, applet->info[i].hIcon);
318 lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
319 lvItem.iItem = menucount;
320 lvItem.iSubItem = 0;
321 lvItem.pszText = applet->info[i].szName;
322 lvItem.iImage = index;
323 lvItem.lParam = (LPARAM) item;
325 itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
327 /* add the description */
328 ListView_SetItemTextW(panel->hWndListView, itemidx, 1,
329 applet->info[i].szInfo);
331 /* update menu bar, increment count */
332 DrawMenuBar(hWnd);
333 menucount++;
338 panel->total_subprogs = menucount;
340 /* check the "large items" icon in the View menu */
341 hSubMenu = GetSubMenu(hMenu, 1);
342 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
343 FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);
345 SetMenu(hWnd, hMenu);
348 static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
350 HMENU hMenu, hSubMenu;
351 MENUITEMINFOW mii;
352 unsigned int i;
354 /* get the File menu */
355 hMenu = GetMenu(hWnd);
357 if (!hMenu)
358 return;
360 hSubMenu = GetSubMenu(hMenu, 0);
362 if (!hSubMenu)
363 return;
365 /* loop and free the item data */
366 for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
368 mii.cbSize = sizeof(MENUITEMINFOW);
369 mii.fMask = MIIM_DATA;
371 if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
372 continue;
374 HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
378 static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
380 HMENU hMenu, hSubMenu;
382 SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);
384 /* update the menu */
385 hMenu = GetMenu(panel->hWnd);
386 hSubMenu = GetSubMenu(hMenu, 1);
388 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
389 id, MF_BYCOMMAND);
392 static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
394 HMENU hMenu, hSubMenu;
395 MENUITEMINFOW mii;
397 /* retrieve the CPlItem structure from the menu item data */
398 hMenu = GetMenu(hWnd);
400 if (!hMenu)
401 return NULL;
403 hSubMenu = GetSubMenu(hMenu, 0);
405 if (!hSubMenu)
406 return NULL;
408 mii.cbSize = sizeof(MENUITEMINFOW);
409 mii.fMask = MIIM_DATA;
411 if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
412 return NULL;
414 return (CPlItem *) mii.dwItemData;
417 static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
419 LVITEMW lvItem;
420 int selitem;
422 selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
423 | LVNI_SELECTED);
425 if (selitem != -1)
427 lvItem.iItem = selitem;
428 lvItem.mask = LVIF_PARAM;
430 if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
431 return (CPlItem *) lvItem.lParam;
434 return NULL;
437 static void Control_StartApplet(HWND hWnd, CPlItem *item)
439 WCHAR verbOpen[] = {'c','p','l','o','p','e','n',0};
440 WCHAR format[] = {'@','%','d',0};
441 WCHAR param[MAX_PATH];
443 /* execute the applet if item is valid */
444 if (item)
446 wsprintfW(param, format, item->id);
447 ShellExecuteW(hWnd, verbOpen, item->applet->cmd, param, NULL, SW_SHOW);
451 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
452 WPARAM lParam1, LPARAM lParam2)
454 CPanel* panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
456 if (panel || wMsg == WM_CREATE) {
457 switch (wMsg) {
458 case WM_CREATE:
459 Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
460 return 0;
461 case WM_DESTROY:
463 CPlApplet* applet = panel->first;
464 while (applet)
465 applet = Control_UnloadApplet(applet);
467 Control_FreeCPlItems(hWnd, panel);
468 PostQuitMessage(0);
469 break;
470 case WM_COMMAND:
471 switch (LOWORD(lParam1))
473 case IDM_CPANEL_EXIT:
474 SendMessageW(hWnd, WM_CLOSE, 0, 0);
475 return 0;
477 case IDM_CPANEL_ABOUT:
479 WCHAR appName[MAX_STRING_LEN];
481 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
482 sizeof(appName) / sizeof(appName[0]));
483 ShellAboutW(hWnd, appName, NULL, NULL);
485 return 0;
488 case FCIDM_SHVIEW_BIGICON:
489 Control_UpdateListViewStyle(panel, LVS_ICON, FCIDM_SHVIEW_BIGICON);
490 return 0;
492 case FCIDM_SHVIEW_SMALLICON:
493 Control_UpdateListViewStyle(panel, LVS_SMALLICON, FCIDM_SHVIEW_SMALLICON);
494 return 0;
496 case FCIDM_SHVIEW_LISTVIEW:
497 Control_UpdateListViewStyle(panel, LVS_LIST, FCIDM_SHVIEW_LISTVIEW);
498 return 0;
500 case FCIDM_SHVIEW_REPORTVIEW:
501 Control_UpdateListViewStyle(panel, LVS_REPORT, FCIDM_SHVIEW_REPORTVIEW);
502 return 0;
504 default:
505 /* check if this is an applet */
506 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
507 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
509 Control_StartApplet(hWnd, Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1)));
510 return 0;
513 break;
516 break;
518 case WM_NOTIFY:
520 LPNMHDR nmh = (LPNMHDR) lParam2;
522 switch (nmh->idFrom)
524 case IDC_LISTVIEW:
525 switch (nmh->code)
527 case NM_RETURN:
528 case NM_DBLCLK:
530 Control_StartApplet(hWnd, Control_GetCPlItem_From_ListView(panel));
531 return 0;
533 case LVN_ITEMCHANGED:
535 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
537 /* update the status bar if item is valid */
538 if (item)
539 SetWindowTextW(panel->hWndStatusBar,
540 item->applet->info[item->id].szInfo);
541 else
542 SetWindowTextW(panel->hWndStatusBar, NULL);
544 return 0;
548 break;
551 break;
554 case WM_MENUSELECT:
555 /* check if this is an applet */
556 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
557 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
559 CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
561 /* update the status bar if item is valid */
562 if (item)
563 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].szInfo);
565 else if ((HIWORD(lParam1) == 0xFFFF) && (lParam2 == 0))
567 /* reset status bar description to that of the selected icon */
568 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
570 if (item)
571 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].szInfo);
572 else
573 SetWindowTextW(panel->hWndStatusBar, NULL);
575 return 0;
577 else
578 SetWindowTextW(panel->hWndStatusBar, NULL);
580 return 0;
582 case WM_SIZE:
584 HDWP hdwp;
585 RECT sb;
587 hdwp = BeginDeferWindowPos(2);
589 if (hdwp == NULL)
590 break;
592 GetClientRect(panel->hWndStatusBar, &sb);
594 hdwp = DeferWindowPos(hdwp, panel->hWndListView, NULL, 0, 0,
595 LOWORD(lParam2), HIWORD(lParam2) - (sb.bottom - sb.top),
596 SWP_NOZORDER | SWP_NOMOVE);
598 if (hdwp == NULL)
599 break;
601 hdwp = DeferWindowPos(hdwp, panel->hWndStatusBar, NULL, 0, 0,
602 LOWORD(lParam2), LOWORD(lParam1), SWP_NOZORDER | SWP_NOMOVE);
604 if (hdwp != NULL)
605 EndDeferWindowPos(hdwp);
607 return 0;
612 return DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
615 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
617 WNDCLASSW wc;
618 MSG msg;
619 WCHAR appName[MAX_STRING_LEN];
620 const WCHAR className[] = {'S','h','e','l','l','_','C','o','n','t','r','o',
621 'l','_','W','n','d','C','l','a','s','s',0};
623 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName, sizeof(appName) / sizeof(appName[0]));
625 wc.style = CS_HREDRAW|CS_VREDRAW;
626 wc.lpfnWndProc = Control_WndProc;
627 wc.cbClsExtra = 0;
628 wc.cbWndExtra = sizeof(CPlApplet*);
629 wc.hInstance = panel->hInst = hInst;
630 wc.hIcon = 0;
631 wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
632 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
633 wc.lpszMenuName = NULL;
634 wc.lpszClassName = className;
636 if (!RegisterClassW(&wc)) return;
638 CreateWindowExW(0, wc.lpszClassName, appName,
639 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
640 CW_USEDEFAULT, CW_USEDEFAULT,
641 CW_USEDEFAULT, CW_USEDEFAULT,
642 hWnd, NULL, hInst, panel);
643 if (!panel->hWnd) return;
645 while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
646 TranslateMessage(&msg);
647 DispatchMessageW(&msg);
651 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
653 WCHAR name[MAX_PATH];
654 WCHAR value[MAX_PATH];
655 HKEY hkey;
657 if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
659 int idx = 0;
661 for(;; ++idx)
663 DWORD nameLen = MAX_PATH;
664 DWORD valueLen = MAX_PATH;
666 if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
667 break;
669 Control_LoadApplet(hWnd, value, panel);
671 RegCloseKey(hkey);
675 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
677 HANDLE h;
678 WIN32_FIND_DATAW fd;
679 WCHAR buffer[MAX_PATH];
680 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
681 static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
682 '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
683 '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
684 WCHAR *p;
686 /* first add .cpl files in the system directory */
687 GetSystemDirectoryW( buffer, MAX_PATH );
688 p = buffer + strlenW(buffer);
689 *p++ = '\\';
690 lstrcpyW(p, wszAllCpl);
692 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
693 do {
694 lstrcpyW(p, fd.cFileName);
695 Control_LoadApplet(hWnd, buffer, panel);
696 } while (FindNextFileW(h, &fd));
697 FindClose(h);
700 /* now check for cpls in the registry */
701 Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
702 Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
704 Control_DoInterface(panel, hWnd, hInst);
707 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
708 /* forms to parse:
709 * foo.cpl,@sp,str
710 * foo.cpl,@sp
711 * foo.cpl,,str
712 * foo.cpl @sp
713 * foo.cpl str
714 * "a path\foo.cpl"
717 LPWSTR buffer;
718 LPWSTR beg = NULL;
719 LPWSTR end;
720 WCHAR ch;
721 LPWSTR ptr;
722 signed sp = -1;
723 LPWSTR extraPmtsBuf = NULL;
724 LPWSTR extraPmts = NULL;
725 int quoted = 0;
727 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
728 if (!buffer) return;
730 end = lstrcpyW(buffer, wszCmd);
732 for (;;) {
733 ch = *end;
734 if (ch == '"') quoted = !quoted;
735 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
736 *end = '\0';
737 if (beg) {
738 if (*beg == '@') {
739 sp = atoiW(beg + 1);
740 } else if (*beg == '\0') {
741 sp = -1;
742 } else {
743 extraPmtsBuf = beg;
746 if (ch == '\0') break;
747 beg = end + 1;
748 if (ch == ' ') while (end[1] == ' ') end++;
750 end++;
752 while ((ptr = StrChrW(buffer, '"')))
753 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
755 /* now check for any quotes in extraPmtsBuf and remove */
756 if (extraPmtsBuf != NULL)
758 beg = end = extraPmtsBuf;
759 quoted = 0;
761 for (;;) {
762 ch = *end;
763 if (ch == '"') quoted = !quoted;
764 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
765 *end = '\0';
766 if (beg) {
767 if (*beg != '\0') {
768 extraPmts = beg;
771 if (ch == '\0') break;
772 beg = end + 1;
773 if (ch == ' ') while (end[1] == ' ') end++;
775 end++;
778 while ((ptr = StrChrW(extraPmts, '"')))
779 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
781 if (extraPmts == NULL)
782 extraPmts = extraPmtsBuf;
785 /* Now check if there had been a numerical value in the extra params */
786 if ((extraPmts) && (*extraPmts == '@') && (sp == -1)) {
787 sp = atoiW(extraPmts + 1);
790 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
792 Control_LoadApplet(hWnd, buffer, panel);
794 if (panel->first) {
795 CPlApplet* applet = panel->first;
797 assert(applet && applet->next == NULL);
799 /* we've been given a textual parameter (or none at all) */
800 if (sp == -1) {
801 while ((++sp) != applet->count) {
802 if (applet->info[sp].dwSize) {
803 TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].szName));
805 if (StrCmpIW(extraPmts, applet->info[sp].szName) == 0)
806 break;
811 if (sp >= applet->count) {
812 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
813 sp = 0;
816 if (applet->info[sp].dwSize) {
817 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSA, sp, (LPARAM)extraPmts))
818 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].lData);
821 Control_UnloadApplet(applet);
824 HeapFree(GetProcessHeap(), 0, buffer);
827 /*************************************************************************
828 * Control_RunDLLW [SHELL32.@]
831 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
833 CPanel panel;
835 TRACE("(%p, %p, %s, 0x%08x)\n",
836 hWnd, hInst, debugstr_w(cmd), nCmdShow);
838 memset(&panel, 0, sizeof(panel));
840 if (!cmd || !*cmd) {
841 Control_DoWindow(&panel, hWnd, hInst);
842 } else {
843 Control_DoLaunch(&panel, hWnd, cmd);
847 /*************************************************************************
848 * Control_RunDLLA [SHELL32.@]
851 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
853 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
854 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
855 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
857 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
859 HeapFree(GetProcessHeap(), 0, wszCmd);
862 /*************************************************************************
863 * Control_FillCache_RunDLLW [SHELL32.@]
866 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
868 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
869 return 0;
872 /*************************************************************************
873 * Control_FillCache_RunDLLA [SHELL32.@]
876 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
878 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
882 /*************************************************************************
883 * RunDLL_CallEntry16 [SHELL32.122]
884 * the name is probably wrong
886 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
887 LPCSTR cmdline, INT cmdshow )
889 WORD args[5];
890 SEGPTR cmdline_seg;
892 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
893 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
895 cmdline_seg = MapLS( cmdline );
896 args[4] = HWND_16(hwnd);
897 args[3] = MapHModuleLS(inst);
898 args[2] = SELECTOROF(cmdline_seg);
899 args[1] = OFFSETOF(cmdline_seg);
900 args[0] = cmdshow;
901 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
902 UnMapLS( cmdline_seg );
905 /*************************************************************************
906 * CallCPLEntry16 [SHELL32.166]
908 * called by desk.cpl on "Advanced" with:
909 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
912 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
914 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
915 return 0x0deadbee;