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