[SHELL32] -Expand the name of the cpl file because CreateActCtx needs a full path.
[reactos.git] / reactos / dll / win32 / shell32 / wine / control.c
1 /* Control Panel management
2 *
3 * Copyright 2001 Eric Pouech
4 * Copyright 2008 Owen Rudge
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <assert.h>
22
23 #define WIN32_NO_STATUS
24 #define _INC_WINDOWS
25
26 #include <windef.h>
27 #include <winbase.h>
28 #define NO_SHLWAPI_REG
29 #include <shlwapi.h>
30 #include <shellapi.h>
31 #include <wine/debug.h>
32
33 #include <strsafe.h>
34
35 #include "cpanel.h"
36 #include "wine/unicode.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(shlctrl);
39
40 void Control_UnloadApplet(CPlApplet* applet)
41 {
42 unsigned i;
43
44 for (i = 0; i < applet->count; i++)
45 applet->proc(applet->hWnd, CPL_STOP, i, applet->info[i].data);
46
47 if (applet->proc) applet->proc(applet->hWnd, CPL_EXIT, 0L, 0L);
48 FreeLibrary(applet->hModule);
49 #ifndef __REACTOS__
50 list_remove( &applet->entry );
51 #endif
52 HeapFree(GetProcessHeap(), 0, applet->cmd);
53 HeapFree(GetProcessHeap(), 0, applet);
54 }
55
56 CPlApplet* Control_LoadApplet(HWND hWnd, LPCWSTR cmd, CPanel* panel)
57 {
58 #ifdef __REACTOS__
59 ACTCTXW ActCtx = {sizeof(ACTCTX), ACTCTX_FLAG_RESOURCE_NAME_VALID};
60 ULONG_PTR cookie;
61 BOOL bActivated;
62 WCHAR fileBuffer[MAX_PATH];
63 #endif
64 CPlApplet* applet;
65 DWORD len;
66 unsigned i;
67 CPLINFO info;
68 NEWCPLINFOW newinfo;
69
70 if (!(applet = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*applet))))
71 return applet;
72
73 len = ExpandEnvironmentStringsW(cmd, NULL, 0);
74 if (len > 0)
75 {
76 if (!(applet->cmd = HeapAlloc(GetProcessHeap(), 0, (len+1) * sizeof(WCHAR))))
77 {
78 WARN("Cannot allocate memory for applet path\n");
79 goto theError;
80 }
81 ExpandEnvironmentStringsW(cmd, applet->cmd, len+1);
82 }
83 else
84 {
85 WARN("Cannot expand applet path\n");
86 goto theError;
87 }
88
89 applet->hWnd = hWnd;
90
91 #ifdef __REACTOS__
92 StringCchCopy(fileBuffer, MAX_PATH, applet->cmd);
93 SearchPath(NULL, fileBuffer, NULL, MAX_PATH, fileBuffer, NULL);
94 ActCtx.lpSource = fileBuffer;
95 ActCtx.lpResourceName = (LPCWSTR)123;
96 applet->hActCtx = CreateActCtx(&ActCtx);
97 bActivated = (applet->hActCtx != INVALID_HANDLE_VALUE ? ActivateActCtx(applet->hActCtx, &cookie) : FALSE);
98 #endif
99
100 if (!(applet->hModule = LoadLibraryW(applet->cmd))) {
101 WARN("Cannot load control panel applet %s\n", debugstr_w(applet->cmd));
102 goto theError;
103 }
104 if (!(applet->proc = (APPLET_PROC)GetProcAddress(applet->hModule, "CPlApplet"))) {
105 WARN("Not a valid control panel applet %s\n", debugstr_w(applet->cmd));
106 goto theError;
107 }
108 if (!applet->proc(hWnd, CPL_INIT, 0L, 0L)) {
109 WARN("Init of applet has failed\n");
110 goto theError;
111 }
112 if ((applet->count = applet->proc(hWnd, CPL_GETCOUNT, 0L, 0L)) == 0) {
113 WARN("No subprogram in applet\n");
114 applet->proc(applet->hWnd, CPL_EXIT, 0, 0);
115 goto theError;
116 }
117
118 applet = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, applet,
119 FIELD_OFFSET( CPlApplet, info[applet->count] ));
120
121 for (i = 0; i < applet->count; i++) {
122 ZeroMemory(&newinfo, sizeof(newinfo));
123 newinfo.dwSize = sizeof(NEWCPLINFOA);
124 applet->info[i].helpfile[0] = 0;
125 /* proc is supposed to return a null value upon success for
126 * CPL_INQUIRE and CPL_NEWINQUIRE
127 * However, real drivers don't seem to behave like this
128 * So, use introspection rather than return value
129 */
130 applet->proc(hWnd, CPL_INQUIRE, i, (LPARAM)&info);
131 applet->info[i].data = info.lData;
132 #ifdef __REACTOS__
133 applet->info[i].idIcon = info.idIcon;
134 #endif
135
136 if (info.idIcon != CPL_DYNAMIC_RES)
137 applet->info[i].icon = LoadIconW(applet->hModule, MAKEINTRESOURCEW(info.idIcon));
138 if (info.idName != CPL_DYNAMIC_RES)
139 LoadStringW(applet->hModule, info.idName,
140 applet->info[i].name, sizeof(applet->info[i].name) / sizeof(WCHAR));
141 if (info.idInfo != CPL_DYNAMIC_RES)
142 LoadStringW(applet->hModule, info.idInfo,
143 applet->info[i].info, sizeof(applet->info[i].info) / sizeof(WCHAR));
144
145 /* some broken control panels seem to return incorrect values in CPL_INQUIRE,
146 but proper data in CPL_NEWINQUIRE. if we get an empty string or a null
147 icon, see what we can get from CPL_NEWINQUIRE */
148
149 if (!applet->info[i].name[0]) info.idName = CPL_DYNAMIC_RES;
150
151 /* zero-length szInfo may not be a buggy applet, but it doesn't hurt for us
152 to check anyway */
153
154 if (!applet->info[i].info[0]) info.idInfo = CPL_DYNAMIC_RES;
155
156 if (applet->info[i].icon == NULL)
157 info.idIcon = CPL_DYNAMIC_RES;
158
159 if ((info.idIcon == CPL_DYNAMIC_RES) || (info.idName == CPL_DYNAMIC_RES) ||
160 (info.idInfo == CPL_DYNAMIC_RES)) {
161 applet->proc(hWnd, CPL_NEWINQUIRE, i, (LPARAM)&newinfo);
162
163 applet->info[i].data = newinfo.lData;
164 if (info.idIcon == CPL_DYNAMIC_RES) {
165 if (!newinfo.hIcon) WARN("couldn't get icon for applet %u\n", i);
166 applet->info[i].icon = newinfo.hIcon;
167 }
168 if (newinfo.dwSize == sizeof(NEWCPLINFOW)) {
169 if (info.idName == CPL_DYNAMIC_RES)
170 memcpy(applet->info[i].name, newinfo.szName, sizeof(newinfo.szName));
171 if (info.idInfo == CPL_DYNAMIC_RES)
172 memcpy(applet->info[i].info, newinfo.szInfo, sizeof(newinfo.szInfo));
173 memcpy(applet->info[i].helpfile, newinfo.szHelpFile, sizeof(newinfo.szHelpFile));
174 } else {
175 if (info.idName == CPL_DYNAMIC_RES)
176 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szName,
177 sizeof(((LPNEWCPLINFOA)&newinfo)->szName) / sizeof(CHAR),
178 applet->info[i].name, sizeof(applet->info[i].name) / sizeof(WCHAR));
179 if (info.idInfo == CPL_DYNAMIC_RES)
180 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szInfo,
181 sizeof(((LPNEWCPLINFOA)&newinfo)->szInfo) / sizeof(CHAR),
182 applet->info[i].info, sizeof(applet->info[i].info) / sizeof(WCHAR));
183 MultiByteToWideChar(CP_ACP, 0, ((LPNEWCPLINFOA)&newinfo)->szHelpFile,
184 sizeof(((LPNEWCPLINFOA)&newinfo)->szHelpFile) / sizeof(CHAR),
185 applet->info[i].helpfile,
186 sizeof(applet->info[i].helpfile) / sizeof(WCHAR));
187 }
188 }
189 }
190
191 #ifdef __REACTOS__
192 if (bActivated)
193 DeactivateActCtx(0, cookie);
194 #endif
195
196 #ifndef __REACTOS__
197 list_add_head( &panel->applets, &applet->entry );
198 #endif
199
200 return applet;
201
202 theError:
203 FreeLibrary(applet->hModule);
204 HeapFree(GetProcessHeap(), 0, applet->cmd);
205 HeapFree(GetProcessHeap(), 0, applet);
206 return NULL;
207 }
208
209 #ifndef __REACTOS__
210
211 #define IDC_LISTVIEW 1000
212 #define IDC_STATUSBAR 1001
213
214 #define NUM_COLUMNS 2
215 #define LISTVIEW_DEFSTYLE (WS_CHILD | WS_VISIBLE | WS_TABSTOP |\
216 LVS_SORTASCENDING | LVS_AUTOARRANGE | LVS_SINGLESEL)
217
218 static BOOL Control_CreateListView (CPanel *panel)
219 {
220 RECT ws, sb;
221 WCHAR empty_string[] = {0};
222 WCHAR buf[MAX_STRING_LEN];
223 LVCOLUMNW lvc;
224
225 /* Create list view */
226 GetClientRect(panel->hWndStatusBar, &sb);
227 GetClientRect(panel->hWnd, &ws);
228
229 panel->hWndListView = CreateWindowExW(WS_EX_CLIENTEDGE, WC_LISTVIEWW,
230 empty_string, LISTVIEW_DEFSTYLE | LVS_ICON,
231 0, 0, ws.right - ws.left, ws.bottom - ws.top -
232 (sb.bottom - sb.top), panel->hWnd,
233 (HMENU) IDC_LISTVIEW, panel->hInst, NULL);
234
235 if (!panel->hWndListView)
236 return FALSE;
237
238 /* Create image lists for list view */
239 panel->hImageListSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
240 GetSystemMetrics(SM_CYSMICON), ILC_COLOR32 | ILC_MASK, 1, 1);
241 panel->hImageListLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),
242 GetSystemMetrics(SM_CYICON), ILC_COLOR32 | ILC_MASK, 1, 1);
243
244 SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_SMALL, (LPARAM)panel->hImageListSmall);
245 SendMessageW(panel->hWndListView, LVM_SETIMAGELIST, LVSIL_NORMAL, (LPARAM)panel->hImageListLarge);
246
247 /* Create columns for list view */
248 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;
249 lvc.pszText = buf;
250 lvc.fmt = LVCFMT_LEFT;
251
252 /* Name column */
253 lvc.iSubItem = 0;
254 lvc.cx = (ws.right - ws.left) / 3;
255 LoadStringW(shell32_hInstance, IDS_CPANEL_NAME, buf, sizeof(buf) / sizeof(buf[0]));
256
257 if (ListView_InsertColumnW(panel->hWndListView, 0, &lvc) == -1)
258 return FALSE;
259
260 /* Description column */
261 lvc.iSubItem = 1;
262 lvc.cx = ((ws.right - ws.left) / 3) * 2;
263 LoadStringW(shell32_hInstance, IDS_CPANEL_DESCRIPTION, buf, sizeof(buf) /
264 sizeof(buf[0]));
265
266 if (ListView_InsertColumnW(panel->hWndListView, 1, &lvc) == -1)
267 return FALSE;
268
269 return(TRUE);
270 }
271
272 static void Control_WndProc_Create(HWND hWnd, const CREATESTRUCTW* cs)
273 {
274 CPanel* panel = cs->lpCreateParams;
275 HMENU hMenu, hSubMenu;
276 CPlApplet* applet;
277 MENUITEMINFOW mii;
278 unsigned int i;
279 int menucount, index;
280 CPlItem *item;
281 LVITEMW lvItem;
282 INITCOMMONCONTROLSEX icex;
283 INT sb_parts;
284 int itemidx;
285
286 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)panel);
287 panel->hWnd = hWnd;
288
289 /* Initialise common control DLL */
290 icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
291 icex.dwICC = ICC_LISTVIEW_CLASSES | ICC_BAR_CLASSES;
292 InitCommonControlsEx(&icex);
293
294 /* create the status bar */
295 if (!(panel->hWndStatusBar = CreateStatusWindowW(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP, NULL, hWnd, IDC_STATUSBAR)))
296 return;
297
298 sb_parts = -1;
299 SendMessageW(panel->hWndStatusBar, SB_SETPARTS, 1, (LPARAM) &sb_parts);
300
301 /* create the list view */
302 if (!Control_CreateListView(panel))
303 return;
304
305 hMenu = LoadMenuW(shell32_hInstance, MAKEINTRESOURCEW(MENU_CPANEL));
306
307 /* insert menu items for applets */
308 hSubMenu = GetSubMenu(hMenu, 0);
309 menucount = 0;
310
311 LIST_FOR_EACH_ENTRY( applet, &panel->applets, CPlApplet, entry )
312 {
313 for (i = 0; i < applet->count; i++) {
314 /* set up a CPlItem for this particular subprogram */
315 item = HeapAlloc(GetProcessHeap(), 0, sizeof(CPlItem));
316
317 if (!item)
318 continue;
319
320 item->applet = applet;
321 item->id = i;
322
323 mii.cbSize = sizeof(MENUITEMINFOW);
324 mii.fMask = MIIM_ID | MIIM_STRING | MIIM_DATA;
325 mii.dwTypeData = applet->info[i].name;
326 mii.cch = sizeof(applet->info[i].name) / sizeof(WCHAR);
327 mii.wID = IDM_CPANEL_APPLET_BASE + menucount;
328 mii.dwItemData = (ULONG_PTR)item;
329
330 if (InsertMenuItemW(hSubMenu, menucount, TRUE, &mii)) {
331 /* add the list view item */
332 HICON icon = applet->info[i].icon;
333 if (!icon) icon = LoadImageW( 0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 0, 0, LR_SHARED );
334 index = ImageList_AddIcon(panel->hImageListLarge, icon);
335 ImageList_AddIcon(panel->hImageListSmall, icon);
336
337 lvItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
338 lvItem.iItem = menucount;
339 lvItem.iSubItem = 0;
340 lvItem.pszText = applet->info[i].name;
341 lvItem.iImage = index;
342 lvItem.lParam = (LPARAM) item;
343
344 itemidx = ListView_InsertItemW(panel->hWndListView, &lvItem);
345
346 /* add the description */
347 ListView_SetItemTextW(panel->hWndListView, itemidx, 1, applet->info[i].info);
348
349 /* update menu bar, increment count */
350 DrawMenuBar(hWnd);
351 menucount++;
352 }
353 }
354 }
355
356 panel->total_subprogs = menucount;
357
358 /* check the "large items" icon in the View menu */
359 hSubMenu = GetSubMenu(hMenu, 1);
360 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
361 FCIDM_SHVIEW_BIGICON, MF_BYCOMMAND);
362
363 SetMenu(hWnd, hMenu);
364 }
365
366 static void Control_FreeCPlItems(HWND hWnd, CPanel *panel)
367 {
368 HMENU hMenu, hSubMenu;
369 MENUITEMINFOW mii;
370 unsigned int i;
371
372 /* get the File menu */
373 hMenu = GetMenu(hWnd);
374
375 if (!hMenu)
376 return;
377
378 hSubMenu = GetSubMenu(hMenu, 0);
379
380 if (!hSubMenu)
381 return;
382
383 /* loop and free the item data */
384 for (i = IDM_CPANEL_APPLET_BASE; i <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs; i++)
385 {
386 mii.cbSize = sizeof(MENUITEMINFOW);
387 mii.fMask = MIIM_DATA;
388
389 if (!GetMenuItemInfoW(hSubMenu, i, FALSE, &mii))
390 continue;
391
392 HeapFree(GetProcessHeap(), 0, (LPVOID) mii.dwItemData);
393 }
394 }
395
396 static void Control_UpdateListViewStyle(CPanel *panel, UINT style, UINT id)
397 {
398 HMENU hMenu, hSubMenu;
399
400 SetWindowLongW(panel->hWndListView, GWL_STYLE, LISTVIEW_DEFSTYLE | style);
401
402 /* update the menu */
403 hMenu = GetMenu(panel->hWnd);
404 hSubMenu = GetSubMenu(hMenu, 1);
405
406 CheckMenuRadioItem(hSubMenu, FCIDM_SHVIEW_BIGICON, FCIDM_SHVIEW_REPORTVIEW,
407 id, MF_BYCOMMAND);
408 }
409
410 static CPlItem* Control_GetCPlItem_From_MenuID(HWND hWnd, UINT id)
411 {
412 HMENU hMenu, hSubMenu;
413 MENUITEMINFOW mii;
414
415 /* retrieve the CPlItem structure from the menu item data */
416 hMenu = GetMenu(hWnd);
417
418 if (!hMenu)
419 return NULL;
420
421 hSubMenu = GetSubMenu(hMenu, 0);
422
423 if (!hSubMenu)
424 return NULL;
425
426 mii.cbSize = sizeof(MENUITEMINFOW);
427 mii.fMask = MIIM_DATA;
428
429 if (!GetMenuItemInfoW(hSubMenu, id, FALSE, &mii))
430 return NULL;
431
432 return (CPlItem *) mii.dwItemData;
433 }
434
435 static CPlItem* Control_GetCPlItem_From_ListView(CPanel *panel)
436 {
437 LVITEMW lvItem;
438 int selitem;
439
440 selitem = SendMessageW(panel->hWndListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
441 | LVNI_SELECTED);
442
443 if (selitem != -1)
444 {
445 lvItem.iItem = selitem;
446 lvItem.mask = LVIF_PARAM;
447
448 if (SendMessageW(panel->hWndListView, LVM_GETITEMW, 0, (LPARAM) &lvItem))
449 return (CPlItem *) lvItem.lParam;
450 }
451
452 return NULL;
453 }
454
455 static void Control_StartApplet(HWND hWnd, CPlItem *item)
456 {
457 WCHAR verbOpen[] = {'c','p','l','o','p','e','n',0};
458 WCHAR format[] = {'@','%','d',0};
459 WCHAR param[MAX_PATH];
460
461 /* execute the applet if item is valid */
462 if (item)
463 {
464 wsprintfW(param, format, item->id);
465 ShellExecuteW(hWnd, verbOpen, item->applet->cmd, param, NULL, SW_SHOW);
466 }
467 }
468
469 static LRESULT WINAPI Control_WndProc(HWND hWnd, UINT wMsg,
470 WPARAM lParam1, LPARAM lParam2)
471 {
472 CPanel* panel = (CPanel*)GetWindowLongPtrW(hWnd, 0);
473
474 if (panel || wMsg == WM_CREATE) {
475 switch (wMsg) {
476 case WM_CREATE:
477 Control_WndProc_Create(hWnd, (CREATESTRUCTW*)lParam2);
478 return 0;
479 case WM_DESTROY:
480 {
481 CPlApplet *applet, *next;
482 LIST_FOR_EACH_ENTRY_SAFE( applet, next, &panel->applets, CPlApplet, entry )
483 Control_UnloadApplet(applet);
484 }
485 Control_FreeCPlItems(hWnd, panel);
486 PostQuitMessage(0);
487 break;
488 case WM_COMMAND:
489 switch (LOWORD(lParam1))
490 {
491 case IDM_CPANEL_EXIT:
492 SendMessageW(hWnd, WM_CLOSE, 0, 0);
493 return 0;
494
495 case IDM_CPANEL_ABOUT:
496 {
497 WCHAR appName[MAX_STRING_LEN];
498 HICON icon = LoadImageW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL),
499 IMAGE_ICON, 48, 48, LR_SHARED);
500
501 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName,
502 sizeof(appName) / sizeof(appName[0]));
503 ShellAboutW(hWnd, appName, NULL, icon);
504
505 return 0;
506 }
507
508 case FCIDM_SHVIEW_BIGICON:
509 Control_UpdateListViewStyle(panel, LVS_ICON, FCIDM_SHVIEW_BIGICON);
510 return 0;
511
512 case FCIDM_SHVIEW_SMALLICON:
513 Control_UpdateListViewStyle(panel, LVS_SMALLICON, FCIDM_SHVIEW_SMALLICON);
514 return 0;
515
516 case FCIDM_SHVIEW_LISTVIEW:
517 Control_UpdateListViewStyle(panel, LVS_LIST, FCIDM_SHVIEW_LISTVIEW);
518 return 0;
519
520 case FCIDM_SHVIEW_REPORTVIEW:
521 Control_UpdateListViewStyle(panel, LVS_REPORT, FCIDM_SHVIEW_REPORTVIEW);
522 return 0;
523
524 default:
525 /* check if this is an applet */
526 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
527 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
528 {
529 Control_StartApplet(hWnd, Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1)));
530 return 0;
531 }
532
533 break;
534 }
535
536 break;
537
538 case WM_NOTIFY:
539 {
540 LPNMHDR nmh = (LPNMHDR) lParam2;
541
542 switch (nmh->idFrom)
543 {
544 case IDC_LISTVIEW:
545 switch (nmh->code)
546 {
547 case NM_RETURN:
548 case NM_DBLCLK:
549 {
550 Control_StartApplet(hWnd, Control_GetCPlItem_From_ListView(panel));
551 return 0;
552 }
553 case LVN_ITEMCHANGED:
554 {
555 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
556
557 /* update the status bar if item is valid */
558 if (item)
559 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
560 else
561 SetWindowTextW(panel->hWndStatusBar, NULL);
562
563 return 0;
564 }
565 }
566
567 break;
568 }
569
570 break;
571 }
572
573 case WM_MENUSELECT:
574 /* check if this is an applet */
575 if ((LOWORD(lParam1) >= IDM_CPANEL_APPLET_BASE) &&
576 (LOWORD(lParam1) <= IDM_CPANEL_APPLET_BASE + panel->total_subprogs))
577 {
578 CPlItem *item = Control_GetCPlItem_From_MenuID(hWnd, LOWORD(lParam1));
579
580 /* update the status bar if item is valid */
581 if (item)
582 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
583 }
584 else if ((HIWORD(lParam1) == 0xFFFF) && (lParam2 == 0))
585 {
586 /* reset status bar description to that of the selected icon */
587 CPlItem *item = Control_GetCPlItem_From_ListView(panel);
588
589 if (item)
590 SetWindowTextW(panel->hWndStatusBar, item->applet->info[item->id].info);
591 else
592 SetWindowTextW(panel->hWndStatusBar, NULL);
593
594 return 0;
595 }
596 else
597 SetWindowTextW(panel->hWndStatusBar, NULL);
598
599 return 0;
600
601 case WM_SIZE:
602 {
603 HDWP hdwp;
604 RECT sb;
605
606 hdwp = BeginDeferWindowPos(2);
607
608 if (hdwp == NULL)
609 break;
610
611 GetClientRect(panel->hWndStatusBar, &sb);
612
613 hdwp = DeferWindowPos(hdwp, panel->hWndListView, NULL, 0, 0,
614 LOWORD(lParam2), HIWORD(lParam2) - (sb.bottom - sb.top),
615 SWP_NOZORDER | SWP_NOMOVE);
616
617 if (hdwp == NULL)
618 break;
619
620 hdwp = DeferWindowPos(hdwp, panel->hWndStatusBar, NULL, 0, 0,
621 LOWORD(lParam2), LOWORD(lParam1), SWP_NOZORDER | SWP_NOMOVE);
622
623 if (hdwp != NULL)
624 EndDeferWindowPos(hdwp);
625
626 return 0;
627 }
628 }
629 }
630
631 return DefWindowProcW(hWnd, wMsg, lParam1, lParam2);
632 }
633
634 static void Control_DoInterface(CPanel* panel, HWND hWnd, HINSTANCE hInst)
635 {
636 WNDCLASSEXW wc;
637 MSG msg;
638 WCHAR appName[MAX_STRING_LEN];
639 const WCHAR className[] = {'S','h','e','l','l','_','C','o','n','t','r','o',
640 'l','_','W','n','d','C','l','a','s','s',0};
641
642 LoadStringW(shell32_hInstance, IDS_CPANEL_TITLE, appName, sizeof(appName) / sizeof(appName[0]));
643
644 wc.cbSize = sizeof(wc);
645 wc.style = CS_HREDRAW|CS_VREDRAW;
646 wc.lpfnWndProc = Control_WndProc;
647 wc.cbClsExtra = 0;
648 wc.cbWndExtra = sizeof(CPlApplet*);
649 wc.hInstance = panel->hInst = hInst;
650 wc.hIcon = LoadIconW( shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL) );
651 wc.hCursor = LoadCursorW( 0, (LPWSTR)IDC_ARROW );
652 wc.hbrBackground = GetStockObject(WHITE_BRUSH);
653 wc.lpszMenuName = NULL;
654 wc.lpszClassName = className;
655 wc.hIconSm = LoadImageW( shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_CONTROL_PANEL), IMAGE_ICON,
656 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED);
657
658 if (!RegisterClassExW(&wc)) return;
659
660 CreateWindowExW(0, wc.lpszClassName, appName,
661 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
662 CW_USEDEFAULT, CW_USEDEFAULT,
663 CW_USEDEFAULT, CW_USEDEFAULT,
664 hWnd, NULL, hInst, panel);
665 if (!panel->hWnd) return;
666
667 while (GetMessageW(&msg, panel->hWnd, 0, 0)) {
668 TranslateMessage(&msg);
669 DispatchMessageW(&msg);
670 }
671 }
672
673 static void Control_RegisterRegistryApplets(HWND hWnd, CPanel *panel, HKEY hkey_root, LPCWSTR szRepPath)
674 {
675 WCHAR name[MAX_PATH];
676 WCHAR value[MAX_PATH];
677 HKEY hkey;
678
679 if (RegOpenKeyW(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
680 {
681 int idx = 0;
682
683 for(;; ++idx)
684 {
685 DWORD nameLen = MAX_PATH;
686 DWORD valueLen = MAX_PATH;
687
688 if (RegEnumValueW(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)value, &valueLen) != ERROR_SUCCESS)
689 break;
690
691 Control_LoadApplet(hWnd, value, panel);
692 }
693 RegCloseKey(hkey);
694 }
695 }
696
697 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
698 {
699 HANDLE h;
700 WIN32_FIND_DATAW fd;
701 WCHAR buffer[MAX_PATH];
702 static const WCHAR wszAllCpl[] = {'*','.','c','p','l',0};
703 static const WCHAR wszRegPath[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t',
704 '\\','W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
705 '\\','C','o','n','t','r','o','l',' ','P','a','n','e','l','\\','C','p','l','s',0};
706 WCHAR *p;
707
708 /* first add .cpl files in the system directory */
709 GetSystemDirectoryW( buffer, MAX_PATH );
710 p = buffer + strlenW(buffer);
711 *p++ = '\\';
712 lstrcpyW(p, wszAllCpl);
713
714 if ((h = FindFirstFileW(buffer, &fd)) != INVALID_HANDLE_VALUE) {
715 do {
716 lstrcpyW(p, fd.cFileName);
717 Control_LoadApplet(hWnd, buffer, panel);
718 } while (FindNextFileW(h, &fd));
719 FindClose(h);
720 }
721
722 /* now check for cpls in the registry */
723 Control_RegisterRegistryApplets(hWnd, panel, HKEY_LOCAL_MACHINE, wszRegPath);
724 Control_RegisterRegistryApplets(hWnd, panel, HKEY_CURRENT_USER, wszRegPath);
725
726 Control_DoInterface(panel, hWnd, hInst);
727 }
728
729 #else
730 static void Control_DoWindow(CPanel* panel, HWND hWnd, HINSTANCE hInst)
731 {
732 ShellExecuteW(NULL,
733 L"open",
734 L"explorer.exe",
735 L"::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\::{21EC2020-3AEA-1069-A2DD-08002B30309D}",
736 NULL,
737 SW_SHOWDEFAULT);
738 }
739 #endif
740
741 static void Control_DoLaunch(CPanel* panel, HWND hWnd, LPCWSTR wszCmd)
742 /* forms to parse:
743 * foo.cpl,@sp,str
744 * foo.cpl,@sp
745 * foo.cpl,,str
746 * foo.cpl @sp
747 * foo.cpl str
748 * "a path\foo.cpl"
749 */
750 {
751 LPWSTR buffer;
752 LPWSTR beg = NULL;
753 LPWSTR end;
754 WCHAR ch;
755 LPWSTR ptr;
756 signed sp = -1;
757 LPWSTR extraPmtsBuf = NULL;
758 LPWSTR extraPmts = NULL;
759 BOOL quoted = FALSE;
760 CPlApplet *applet;
761
762 buffer = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(wszCmd) + 1) * sizeof(*wszCmd));
763 if (!buffer) return;
764
765 end = lstrcpyW(buffer, wszCmd);
766
767 for (;;) {
768 ch = *end;
769 if (ch == '"') quoted = !quoted;
770 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
771 *end = '\0';
772 if (beg) {
773 if (*beg == '@') {
774 sp = atoiW(beg + 1);
775 } else if (*beg == '\0') {
776 sp = -1;
777 } else {
778 extraPmtsBuf = beg;
779 }
780 }
781 if (ch == '\0') break;
782 beg = end + 1;
783 if (ch == ' ') while (end[1] == ' ') end++;
784 }
785 end++;
786 }
787 while ((ptr = StrChrW(buffer, '"')))
788 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
789
790 /* now check for any quotes in extraPmtsBuf and remove */
791 if (extraPmtsBuf != NULL)
792 {
793 beg = end = extraPmtsBuf;
794 quoted = FALSE;
795
796 for (;;) {
797 ch = *end;
798 if (ch == '"') quoted = !quoted;
799 if (!quoted && (ch == ' ' || ch == ',' || ch == '\0')) {
800 *end = '\0';
801 if (beg) {
802 if (*beg != '\0') {
803 extraPmts = beg;
804 }
805 }
806 if (ch == '\0') break;
807 beg = end + 1;
808 if (ch == ' ') while (end[1] == ' ') end++;
809 }
810 end++;
811 }
812
813 while ((ptr = StrChrW(extraPmts, '"')))
814 memmove(ptr, ptr+1, lstrlenW(ptr)*sizeof(WCHAR));
815
816 if (extraPmts == NULL)
817 extraPmts = extraPmtsBuf;
818 }
819
820 /* Now check if there had been a numerical value in the extra params */
821 if ((extraPmts) && (*extraPmts == '@') && (sp == -1)) {
822 sp = atoiW(extraPmts + 1);
823 }
824
825 TRACE("cmd %s, extra %s, sp %d\n", debugstr_w(buffer), debugstr_w(extraPmts), sp);
826
827 applet = Control_LoadApplet(hWnd, buffer, panel);
828 if (applet)
829 {
830 #ifdef __REACTOS__
831 ULONG_PTR cookie;
832 BOOL bActivated;
833 #endif
834 /* we've been given a textual parameter (or none at all) */
835 if (sp == -1) {
836 while ((++sp) != applet->count) {
837 TRACE("sp %d, name %s\n", sp, debugstr_w(applet->info[sp].name));
838
839 if (StrCmpIW(extraPmts, applet->info[sp].name) == 0)
840 break;
841 }
842 }
843
844 if (sp >= applet->count) {
845 WARN("Out of bounds (%u >= %u), setting to 0\n", sp, applet->count);
846 sp = 0;
847 }
848
849 #ifdef __REACTOS__
850 bActivated = (applet->hActCtx != INVALID_HANDLE_VALUE ? ActivateActCtx(applet->hActCtx, &cookie) : FALSE);
851 #endif
852
853 if (!applet->proc(applet->hWnd, CPL_STARTWPARMSW, sp, (LPARAM)extraPmts))
854 applet->proc(applet->hWnd, CPL_DBLCLK, sp, applet->info[sp].data);
855
856 Control_UnloadApplet(applet);
857
858 #ifdef __REACTOS__
859 if (bActivated)
860 DeactivateActCtx(0, cookie);
861 #endif
862
863 }
864
865 HeapFree(GetProcessHeap(), 0, buffer);
866 }
867
868 /*************************************************************************
869 * Control_RunDLLW [SHELL32.@]
870 *
871 */
872 void WINAPI Control_RunDLLW(HWND hWnd, HINSTANCE hInst, LPCWSTR cmd, DWORD nCmdShow)
873 {
874 CPanel panel;
875
876 TRACE("(%p, %p, %s, 0x%08x)\n",
877 hWnd, hInst, debugstr_w(cmd), nCmdShow);
878
879 #ifndef __REACTOS__
880 memset(&panel, 0, sizeof(panel));
881 list_init( &panel.applets );
882 #endif
883
884 if (!cmd || !*cmd) {
885 Control_DoWindow(&panel, hWnd, hInst);
886 } else {
887 Control_DoLaunch(&panel, hWnd, cmd);
888 }
889 }
890
891 /*************************************************************************
892 * Control_RunDLLA [SHELL32.@]
893 *
894 */
895 void WINAPI Control_RunDLLA(HWND hWnd, HINSTANCE hInst, LPCSTR cmd, DWORD nCmdShow)
896 {
897 DWORD len = MultiByteToWideChar(CP_ACP, 0, cmd, -1, NULL, 0 );
898 LPWSTR wszCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
899 if (wszCmd && MultiByteToWideChar(CP_ACP, 0, cmd, -1, wszCmd, len ))
900 {
901 Control_RunDLLW(hWnd, hInst, wszCmd, nCmdShow);
902 }
903 HeapFree(GetProcessHeap(), 0, wszCmd);
904 }
905
906 /*************************************************************************
907 * Control_FillCache_RunDLLW [SHELL32.@]
908 *
909 */
910 HRESULT WINAPI Control_FillCache_RunDLLW(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
911 {
912 FIXME("%p %p 0x%08x 0x%08x stub\n", hWnd, hModule, w, x);
913 return S_OK;
914 }
915
916 /*************************************************************************
917 * Control_FillCache_RunDLLA [SHELL32.@]
918 *
919 */
920 HRESULT WINAPI Control_FillCache_RunDLLA(HWND hWnd, HANDLE hModule, DWORD w, DWORD x)
921 {
922 return Control_FillCache_RunDLLW(hWnd, hModule, w, x);
923 }
924
925
926 #ifdef __REACTOS__
927 /*************************************************************************
928 * RunDll_CallEntry16 [SHELL32.122]
929 * the name is OK (when written with Dll, and not DLL as in Wine!)
930 */
931 void WINAPI RunDll_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst,
932 LPCSTR cmdline, INT cmdshow )
933 {
934 FIXME( "proc %lx hwnd %p inst %p cmdline %s cmdshow %d\n",
935 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
936 }
937 #endif
938
939 /*************************************************************************
940 * CallCPLEntry16 [SHELL32.166]
941 *
942 * called by desk.cpl on "Advanced" with:
943 * hMod("DeskCp16.Dll"), pFunc("CplApplet"), 0, 1, 0xc, 0
944 *
945 */
946 DWORD WINAPI CallCPLEntry16(HMODULE hMod, FARPROC pFunc, DWORD dw3, DWORD dw4, DWORD dw5, DWORD dw6)
947 {
948 FIXME("(%p, %p, %08x, %08x, %08x, %08x): stub.\n", hMod, pFunc, dw3, dw4, dw5, dw6);
949 return 0x0deadbee;
950 }