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