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