dwrite: Implement CreateFontFaceFromHdc().
[wine.git] / dlls / shell32 / control.c
blobb3931544e440591393061eb14bde1147784e37de
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 applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].data);
55 if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
56 FreeLibrary(applet->hModule);
57 list_remove( &applet->entry );
58 HeapFree(GetProcessHeap(), 0, applet->cmd);
59 HeapFree(GetProcessHeap(), 0, applet);
62 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
64 CPlApplet* applet;
65 DWORD len;
66 unsigned i;
67 CPLINFO info;
68 NEWCPLINFOW newinfo;
70 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
71 return applet;
73 len = ExpandEnvironmentStringsW(cmd, NULL, 0);
74 if (len > 0)
76 if (!(applet->cmd = HeapAlloc(GetProcessHeap(), 0, (len+1) * sizeof(WCHAR))))
78 WARN("Cannot allocate memory for applet path\n");
79 goto theError;
81 ExpandEnvironmentStringsW(cmd, applet->cmd, len+1);
83 else
85 WARN("Cannot expand applet path\n");
86 goto theError;
89 applet->hWnd = hWnd;
91 if (!(applet->hModule = LoadLibraryW(applet->cmd))) {
92 WARN("Cannot load control panel applet %s\n", debugstr_w(applet->cmd));
93 goto theError;
95 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
96 WARN("Not a valid control panel applet %s\n", debugstr_w(applet->cmd));
97 goto theError;
99 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
100 WARN("Init of applet has failed\n");
101 goto theError;
103 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
104 WARN("No subprogram in applet\n");
105 applet->proc(applet->hWnd, CPL_EXIT, 0, 0);
106 goto theError;
109 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
110 FIELD_OFFSET( CPlApplet, info[applet->count] ));
112 for (i = 0; i < applet->count; i++) {
113 ZeroMemory(&newinfo, sizeof(newinfo));
114 newinfo.dwSize = sizeof(NEWCPLINFOA);
115 applet->info[i].helpfile[0] = 0;
116 /* proc is supposed to return a null value upon success for
117 * CPL_INQUIRE and CPL_NEWINQUIRE
118 * However, real drivers don't seem to behave like this
119 * So, use introspection rather than return value
121 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
122 applet->info[i].data = info.lData;
123 if (info.idIcon != CPL_DYNAMIC_RES)
124 applet->info[i].icon = LoadIconW(applet->hModule, MAKEINTRESOURCEW(info.idIcon));
125 if (info.idName != CPL_DYNAMIC_RES)
126 LoadStringW(applet->hModule, info.idName,
127 applet->info[i].name, sizeof(applet->info[i].name) / sizeof(WCHAR));
128 if (info.idInfo != CPL_DYNAMIC_RES)
129 LoadStringW(applet->hModule, info.idInfo,
130 applet->info[i].info, sizeof(applet->info[i].info) / sizeof(WCHAR));
132 /* some broken control panels seem to return incorrect values in CPL_INQUIRE,
133 but proper data in CPL_NEWINQUIRE. if we get an empty string or a null
134 icon, see what we can get from CPL_NEWINQUIRE */
136 if (!applet->info[i].name[0]) info.idName = CPL_DYNAMIC_RES;
138 /* zero-length szInfo may not be a buggy applet, but it doesn't hurt for us
139 to check anyway */
141 if (!applet->info[i].info[0]) info.idInfo = CPL_DYNAMIC_RES;
143 if (applet->info[i].icon == 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].data = newinfo.lData;
151 if (info.idIcon == CPL_DYNAMIC_RES) {
152 if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
153 applet->info[i].icon = newinfo.hIcon;
155 if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
156 if (info.idName == CPL_DYNAMIC_RES)
157 memcpy(applet->info[i].name, newinfo.szName, sizeof(newinfo.szName));
158 if (info.idInfo == CPL_DYNAMIC_RES)
159 memcpy(applet->info[i].info, newinfo.szInfo, sizeof(newinfo.szInfo));
160 memcpy(applet->info[i].helpfile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
161 } else {
162 if (info.idName == CPL_DYNAMIC_RES)
163 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
164 sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
165 applet->info[i].name, sizeof(applet->info[i].name) / sizeof(WCHAR));
166 if (info.idInfo == CPL_DYNAMIC_RES)
167 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
168 sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
169 applet->info[i].info, sizeof(applet->info[i].info) / sizeof(WCHAR));
170 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
171 sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
172 applet->info[i].helpfile,
173 sizeof(applet->info[i].helpfile) / sizeof(WCHAR));
178 list_add_head( &panel->applets, &applet->entry );
179 return applet;
181 theError:
182 FreeLibrary(applet->hModule);
183 HeapFree(GetProcessHeap(), 0, applet->cmd);
184 HeapFree(GetProcessHeap(), 0, applet);
185 return NULL;
188 #define IDC_LISTVIEW 1000
189 #define IDC_STATUSBAR 1001
191 #define NUM_COLUMNS 2
192 #define LISTVIEW_DEFSTYLE (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
193 LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)
195 static BOOL Control_CreateListView (CPanel *panel)
197 RECT ws, sb;
198 WCHAR empty_string[] = {0};
199 WCHAR buf[MAX_STRING_LEN];
200 LVCOLUMNW lvc;
202 /* Create list view */
203 GetClientRect(panel->hWndStatusBar, &sb);
204 GetClientRect(panel->hWnd, &ws);
206 panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
207 empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
208 0, 0, ws.right - ws.left, ws.bottom - ws.top -
209 (sb.bottom - sb.top), panel->hWnd,
210 (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
212 if (!panel->hWndListView)
213 return FALSE;
215 /* Create image lists for list view */
216 panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
217 GetSystemMetrics(SM_CYSMICON), ILC_COLOR32 | ILC_MASK, 1, 1);
218 panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
219 GetSystemMetrics(SM_CYICON), ILC_COLOR32 | ILC_MASK, 1, 1);
221 SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)panel->hImageListSmall);
222 SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)panel->hImageListLarge);
224 /* Create columns for list view */
225 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
226 lvc.pszText = buf;
227 lvc.fmt = LVCFMT_LEFT;
229 /* Name column */
230 lvc.iSubItem = 0;
231 lvc.cx = (ws.right - ws.left) / 3;
232 LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));
234 if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
235 return FALSE;
237 /* Description column */
238 lvc.iSubItem = 1;
239 lvc.cx = ((ws.right - ws.left) / 3) * 2;
240 LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
241 sizeof(buf[0]));
243 if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
244 return FALSE;
246 return(TRUE);
249 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
251 CPanel* panel = cs->lpCreateParams;
252 HMENU hMenu, hSubMenu;
253 CPlApplet* applet;
254 MENUITEMINFOW mii;
255 unsigned int i;
256 int menucount, index;
257 CPlItem *item;
258 LVITEMW lvItem;
259 INITCOMMONCONTROLSEX icex;
260 INT sb_parts;
261 int itemidx;
263 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
264 panel->hWnd = hWnd;
266 /* Initialise common control DLL */
267 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
268 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
269 InitCommonControlsEx(&icex);
271 /* create the status bar */
272 if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
273 return;
275 sb_parts = -1;
276 SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);
278 /* create the list view */
279 if (!Control_CreateListView(panel))
280 return;
282 hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));
284 /* insert menu items for applets */
285 hSubMenu = GetSubMenu(hMenu, 0);
286 menucount = 0;
288 LIST_FOR_EACH_ENTRY( applet, &panel->applets, CPlApplet, entry )
290 for (i = 0; i < applet->count; i++) {
291 /* set up a CPlItem for this particular subprogram */
292 item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));
294 if (!item)
295 continue;
297 item->applet = applet;
298 item->id = i;
300 mii.cbSize = sizeof(MENUITEMINFOW);
301 mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
302 mii.dwTypeData = applet->info[i].name;
303 mii.cch = sizeof(applet->info[i].name) / sizeof(WCHAR);
304 mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
305 mii.dwItemData = (ULONG_PTR)item;
307 if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
308 /* add the list view item */
309 HICON icon = applet->info[i].icon;
310 if (!icon) icon = LoadImageW( 0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 0, 0, LR_SHARED );
311 index = ImageList_AddIcon(panel->hImageListLarge, icon);
312 ImageList_AddIcon(panel->hImageListSmall, icon);
314 lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
315 lvItem.iItem = menucount;
316 lvItem.iSubItem = 0;
317 lvItem.pszText = applet->info[i].name;
318 lvItem.iImage = index;
319 lvItem.lParam = (LPARAM) item;
321 itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
323 /* add the description */
324 ListView_SetItemTextW(panel->hWndListView, itemidx, 1, applet->info[i].info);
326 /* update menu bar, increment count */
327 DrawMenuBar(hWnd);
328 menucount++;
333 panel->total_subprogs = menucount;
335 /* check the "large items" icon in the View menu */
336 hSubMenu = GetSubMenu(hMenu, 1);
337 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
338 FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);
340 SetMenu(hWnd, hMenu);
343 static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
345 HMENU hMenu, hSubMenu;
346 MENUITEMINFOW mii;
347 unsigned int i;
349 /* get the File menu */
350 hMenu = GetMenu(hWnd);
352 if (!hMenu)
353 return;
355 hSubMenu = GetSubMenu(hMenu, 0);
357 if (!hSubMenu)
358 return;
360 /* loop and free the item data */
361 for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
363 mii.cbSize = sizeof(MENUITEMINFOW);
364 mii.fMask = MIIM_DATA;
366 if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
367 continue;
369 HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
373 static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
375 HMENU hMenu, hSubMenu;
377 SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);
379 /* update the menu */
380 hMenu = GetMenu(panel->hWnd);
381 hSubMenu = GetSubMenu(hMenu, 1);
383 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
384 id, MF_BYCOMMAND);
387 static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
389 HMENU hMenu, hSubMenu;
390 MENUITEMINFOW mii;
392 /* retrieve the CPlItem structure from the menu item data */
393 hMenu = GetMenu(hWnd);
395 if (!hMenu)
396 return NULL;
398 hSubMenu = GetSubMenu(hMenu, 0);
400 if (!hSubMenu)
401 return NULL;
403 mii.cbSize = sizeof(MENUITEMINFOW);
404 mii.fMask = MIIM_DATA;
406 if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
407 return NULL;
409 return (CPlItem *) mii.dwItemData;
412 static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
414 LVITEMW lvItem;
415 int selitem;
417 selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
418 | LVNI_SELECTED);
420 if (selitem != -1)
422 lvItem.iItem = selitem;
423 lvItem.mask = LVIF_PARAM;
425 if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
426 return (CPlItem *) lvItem.lParam;
429 return NULL;
432 static void Control_StartApplet(HWND hWnd, CPlItem *item)
434 WCHAR verbOpen[] = {'c','p','l','o','p','e','n',0};
435 WCHAR format[] = {'@','%','d',0};
436 WCHAR param[MAX_PATH];
438 /* execute the applet if item is valid */
439 if (item)
441 wsprintfW(param, format, item->id);
442 ShellExecuteW(hWnd, verbOpen, item->applet->cmd, param, NULL, SW_SHOW);
446 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
447 WPARAM lParam1, LPARAM lParam2)
449 CPanel* panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
451 if (panel || wMsg == WM_CREATE) {
452 switch (wMsg) {
453 case WM_CREATE:
454 Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
455 return 0;
456 case WM_DESTROY:
458 CPlApplet *applet, *next;
459 LIST_FOR_EACH_ENTRY_SAFE( applet, next, &panel->applets, CPlApplet, entry )
460 Control_UnloadApplet(applet);
462 Control_FreeCPlItems(hWnd, panel);
463 PostQuitMessage(0);
464 break;
465 case WM_COMMAND:
466 switch (LOWORD(lParam1))
468 case IDM_CPANEL_EXIT:
469 SendMessageW(hWnd, WM_CLOSE, 0, 0);
470 return 0;
472 case IDM_CPANEL_ABOUT:
474 WCHAR appName[MAX_STRING_LEN];
475 HICON icon = LoadImageW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL),
476 IMAGE_ICON, 48, 48, LR_SHARED);
478 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
479 sizeof(appName) / sizeof(appName[0]));
480 ShellAboutW(hWnd, appName, NULL, icon);
482 return 0;
485 case FCIDM_SHVIEW_BIGICON:
486 Control_UpdateListViewStyle(panel, LVS_ICON, FCIDM_SHVIEW_BIGICON);
487 return 0;
489 case FCIDM_SHVIEW_SMALLICON:
490 Control_UpdateListViewStyle(panel, LVS_SMALLICON, FCIDM_SHVIEW_SMALLICON);
491 return 0;
493 case FCIDM_SHVIEW_LISTVIEW:
494 Control_UpdateListViewStyle(panel, LVS_LIST, FCIDM_SHVIEW_LISTVIEW);
495 return 0;
497 case FCIDM_SHVIEW_REPORTVIEW:
498 Control_UpdateListViewStyle(panel, LVS_REPORT, FCIDM_SHVIEW_REPORTVIEW);
499 return 0;
501 default:
502 /* check if this is an applet */
503 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
504 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
506 Control_StartApplet(hWnd, Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1)));
507 return 0;
510 break;
513 break;
515 case WM_NOTIFY:
517 LPNMHDR nmh = (LPNMHDR) lParam2;
519 switch (nmh->idFrom)
521 case IDC_LISTVIEW:
522 switch (nmh->code)
524 case NM_RETURN:
525 case NM_DBLCLK:
527 Control_StartApplet(hWnd, Control_GetCPlItem_From_ListView(panel));
528 return 0;
530 case LVN_ITEMCHANGED:
532 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
534 /* update the status bar if item is valid */
535 if (item)
536 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
537 else
538 SetWindowTextW(panel->hWndStatusBar, NULL);
540 return 0;
544 break;
547 break;
550 case WM_MENUSELECT:
551 /* check if this is an applet */
552 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
553 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
555 CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
557 /* update the status bar if item is valid */
558 if (item)
559 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
561 else if ((HIWORD(lParam1) == 0xFFFF) && (lParam2 == 0))
563 /* reset status bar description to that of the selected icon */
564 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
566 if (item)
567 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
568 else
569 SetWindowTextW(panel->hWndStatusBar, NULL);
571 return 0;
573 else
574 SetWindowTextW(panel->hWndStatusBar, NULL);
576 return 0;
578 case WM_SIZE:
580 HDWP hdwp;
581 RECT sb;
583 hdwp = BeginDeferWindowPos(2);
585 if (hdwp == NULL)
586 break;
588 GetClientRect(panel->hWndStatusBar, &sb);
590 hdwp = DeferWindowPos(hdwp, panel->hWndListView, NULL, 0, 0,
591 LOWORD(lParam2), HIWORD(lParam2) - (sb.bottom - sb.top),
592 SWP_NOZORDER | SWP_NOMOVE);
594 if (hdwp == NULL)
595 break;
597 hdwp = DeferWindowPos(hdwp, panel->hWndStatusBar, NULL, 0, 0,
598 LOWORD(lParam2), LOWORD(lParam1), SWP_NOZORDER | SWP_NOMOVE);
600 if (hdwp != NULL)
601 EndDeferWindowPos(hdwp);
603 return 0;
608 return DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
611 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
613 WNDCLASSEXW wc;
614 MSG msg;
615 WCHAR appName[MAX_STRING_LEN];
616 const WCHAR className[] = {'S','h','e','l','l','_','C','o','n','t','r','o',
617 'l','_','W','n','d','C','l','a','s','s',0};
619 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName, sizeof(appName) / sizeof(appName[0]));
621 wc.cbSize = sizeof(wc);
622 wc.style = CS_HREDRAW|CS_VREDRAW;
623 wc.lpfnWndProc = Control_WndProc;
624 wc.cbClsExtra = 0;
625 wc.cbWndExtra = sizeof(CPlApplet*);
626 wc.hInstance = panel->hInst = hInst;
627 wc.hIcon = LoadIconW( shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL) );
628 wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
629 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
630 wc.lpszMenuName = NULL;
631 wc.lpszClassName = className;
632 wc.hIconSm = LoadImageW( shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL), IMAGE_ICON,
633 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
635 if (!RegisterClassExW(&wc)) return;
637 CreateWindowExW(0, wc.lpszClassName, appName,
638 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
639 CW_USEDEFAULT, CW_USEDEFAULT,
640 CW_USEDEFAULT, CW_USEDEFAULT,
641 hWnd, NULL, hInst, panel);
642 if (!panel->hWnd) return;
644 while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
645 TranslateMessage(&msg);
646 DispatchMessageW(&msg);
650 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
652 WCHAR name[MAX_PATH];
653 WCHAR value[MAX_PATH];
654 HKEY hkey;
656 if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
658 int idx = 0;
660 for(;; ++idx)
662 DWORD nameLen = MAX_PATH;
663 DWORD valueLen = MAX_PATH;
665 if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
666 break;
668 Control_LoadApplet(hWnd, value, panel);
670 RegCloseKey(hkey);
674 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
676 HANDLE h;
677 WIN32_FIND_DATAW fd;
678 WCHAR buffer[MAX_PATH];
679 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
680 static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
681 '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
682 '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
683 WCHAR *p;
685 /* first add .cpl files in the system directory */
686 GetSystemDirectoryW( buffer, MAX_PATH );
687 p = buffer + strlenW(buffer);
688 *p++ = '\\';
689 lstrcpyW(p, wszAllCpl);
691 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
692 do {
693 lstrcpyW(p, fd.cFileName);
694 Control_LoadApplet(hWnd, buffer, panel);
695 } while (FindNextFileW(h, &fd));
696 FindClose(h);
699 /* now check for cpls in the registry */
700 Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
701 Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
703 Control_DoInterface(panel, hWnd, hInst);
706 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
707 /* forms to parse:
708 * foo.cpl,@sp,str
709 * foo.cpl,@sp
710 * foo.cpl,,str
711 * foo.cpl @sp
712 * foo.cpl str
713 * "a path\foo.cpl"
716 LPWSTR buffer;
717 LPWSTR beg = NULL;
718 LPWSTR end;
719 WCHAR ch;
720 LPWSTR ptr;
721 signed sp = -1;
722 LPWSTR extraPmtsBuf = NULL;
723 LPWSTR extraPmts = NULL;
724 BOOL quoted = FALSE;
725 CPlApplet *applet;
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 = FALSE;
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 applet = Control_LoadApplet(hWnd, buffer, panel);
793 if (applet)
795 /* we've been given a textual parameter (or none at all) */
796 if (sp == -1) {
797 while ((++sp) != applet->count) {
798 TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].name));
800 if (StrCmpIW(extraPmts, applet->info[sp].name) == 0)
801 break;
805 if (sp >= applet->count) {
806 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
807 sp = 0;
810 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSW, sp, (LPARAM)extraPmts))
811 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].data);
813 Control_UnloadApplet(applet);
816 HeapFree(GetProcessHeap(), 0, buffer);
819 /*************************************************************************
820 * Control_RunDLLW [SHELL32.@]
823 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
825 CPanel panel;
827 TRACE("(%p, %p, %s, 0x%08x)\n",
828 hWnd, hInst, debugstr_w(cmd), nCmdShow);
830 memset(&panel, 0, sizeof(panel));
831 list_init( &panel.applets );
833 if (!cmd || !*cmd) {
834 Control_DoWindow(&panel, hWnd, hInst);
835 } else {
836 Control_DoLaunch(&panel, hWnd, cmd);
840 /*************************************************************************
841 * Control_RunDLLA [SHELL32.@]
844 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
846 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
847 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
848 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
850 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
852 HeapFree(GetProcessHeap(), 0, wszCmd);
855 /*************************************************************************
856 * Control_FillCache_RunDLLW [SHELL32.@]
859 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
861 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
862 return S_OK;
865 /*************************************************************************
866 * Control_FillCache_RunDLLA [SHELL32.@]
869 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
871 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
874 /*************************************************************************
875 * CallCPLEntry16 [SHELL32.166]
877 * called by desk.cpl on "Advanced" with:
878 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
881 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
883 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
884 return 0x0deadbee;