2 * ReactOS Device Manager Applet
3 * Copyright (C) 2004 - 2005 ReactOS Team
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * PROJECT: ReactOS devmgr.dll
22 * FILE: lib/devmgr/hwpage.c
23 * PURPOSE: ReactOS Device Manager
24 * PROGRAMMER: Thomas Weidenmueller <w3seek@reactos.com>
33 typedef VOID (WINAPI
*PINITCOMMONCONTROLS
)(VOID
);
37 HWPD_STANDARDLIST
= 0,
39 HWPD_MAX
= HWPD_LARGELIST
40 } HWPAGE_DISPLAYMODE
, *PHWPAGE_DISPLAYMODE
;
42 typedef struct _HWDEVINFO
44 struct _HWCLASSDEVINFO
*ClassDevInfo
;
45 SP_DEVINFO_DATA DevInfoData
;
46 } HWDEVINFO
, *PHWDEVINFO
;
48 typedef struct _HWCLASSDEVINFO
55 } HWCLASSDEVINFO
, *PHWCLASSDEVINFO
;
57 typedef struct _HARDWARE_PAGE_DATA
61 HINSTANCE hComCtl32
; /* only save this to keep track of the references */
62 INT DevListViewHeight
;
63 SP_CLASSIMAGELIST_DATA ClassImageListData
;
64 HWPAGE_DISPLAYMODE DisplayMode
;
66 /* parent window subclass info */
67 WNDPROC ParentOldWndProc
;
71 HWCLASSDEVINFO ClassDevInfo
[1];
72 /* struct may be dynamically expanded here! */
73 } HARDWARE_PAGE_DATA
, *PHARDWARE_PAGE_DATA
;
75 #define CX_TYPECOLUMN_WIDTH 80
78 InitializeDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
85 /* set the list view style */
86 ListView_SetExtendedListViewStyle(hpd
->hWndDevList
,
87 LVS_EX_FULLROWSELECT
);
89 /* set the list view image list */
90 if (hpd
->ClassImageListData
.ImageList
!= NULL
)
92 ListView_SetImageList(hpd
->hWndDevList
,
93 hpd
->ClassImageListData
.ImageList
,
97 GetClientRect(hpd
->hWndDevList
,
100 /* add the list view columns */
101 lvc
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
102 lvc
.fmt
= LVCFMT_LEFT
;
103 lvc
.pszText
= szColName
;
105 if (LoadString(hDllInstance
,
108 sizeof(szColName
) / sizeof(szColName
[0])))
110 lvc
.cx
= rcClient
.right
- CX_TYPECOLUMN_WIDTH
-
111 GetSystemMetrics(SM_CXVSCROLL
);
112 ListView_InsertColumn(hpd
->hWndDevList
,
116 if (LoadString(hDllInstance
,
119 sizeof(szColName
) / sizeof(szColName
[0])))
121 lvc
.cx
= CX_TYPECOLUMN_WIDTH
;
122 ListView_InsertColumn(hpd
->hWndDevList
,
130 UpdateControlStates(IN PHARDWARE_PAGE_DATA hpd
)
132 PHWDEVINFO HwDevInfo
;
133 HWND hBtnTroubleShoot
, hBtnProperties
;
136 hBtnTroubleShoot
= GetDlgItem(hpd
->hWnd
,
138 hBtnProperties
= GetDlgItem(hpd
->hWnd
,
141 HwDevInfo
= (PHWDEVINFO
)ListViewGetSelectedItemData(hpd
->hWndDevList
);
142 if (HwDevInfo
!= NULL
)
144 /* update static controls */
146 LPWSTR szFormatted
= NULL
;
148 if (!SetupDiGetDeviceRegistryProperty(HwDevInfo
->ClassDevInfo
->hDevInfo
,
149 &HwDevInfo
->DevInfoData
,
155 RegDataType
!= REG_SZ
)
157 /* FIXME - check string for NULL termination! */
160 if (LoadAndFormatString(hDllInstance
,
165 SetDlgItemText(hpd
->hWnd
,
168 LocalFree((HLOCAL
)szFormatted
);
171 /* FIXME - Display location and status */
175 /* clear static controls */
176 SetDlgItemText(hpd
->hWnd
,
179 SetDlgItemText(hpd
->hWnd
,
182 SetDlgItemText(hpd
->hWnd
,
187 EnableWindow(hBtnTroubleShoot
,
189 EnableWindow(hBtnTroubleShoot
,
190 hBtnProperties
!= NULL
);
194 FreeDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
196 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
198 ClassDevInfo
= hpd
->ClassDevInfo
;
199 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
201 /* free the device info set handles */
202 while (ClassDevInfo
!= LastClassDevInfo
)
204 if (ClassDevInfo
->hDevInfo
!= INVALID_HANDLE_VALUE
)
206 SetupDiDestroyDeviceInfoList(ClassDevInfo
->hDevInfo
);
208 ClassDevInfo
->hDevInfo
= NULL
;
209 ClassDevInfo
->ItemCount
= 0;
210 ClassDevInfo
->ImageIndex
= 0;
212 if (ClassDevInfo
->HwDevInfo
!= NULL
)
214 HeapFree(GetProcessHeap(),
216 ClassDevInfo
->HwDevInfo
);
217 ClassDevInfo
->HwDevInfo
= NULL
;
226 BuildDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
228 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
229 SP_DEVINFO_DATA DevInfoData
;
231 DevInfoData
.cbSize
= sizeof(SP_DEVINFO_DATA
);
233 ClassDevInfo
= hpd
->ClassDevInfo
;
234 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
236 while (ClassDevInfo
!= LastClassDevInfo
)
238 ClassDevInfo
->ImageIndex
= -1;
239 ClassDevInfo
->hDevInfo
= SetupDiGetClassDevs(&ClassDevInfo
->Guid
,
243 if (ClassDevInfo
->hDevInfo
!= INVALID_HANDLE_VALUE
)
245 DWORD MemberIndex
= 0;
247 SetupDiGetClassImageIndex(&hpd
->ClassImageListData
,
249 &ClassDevInfo
->ImageIndex
);
251 while (SetupDiEnumDeviceInfo(ClassDevInfo
->hDevInfo
,
255 if (ClassDevInfo
->HwDevInfo
!= NULL
)
257 PHWDEVINFO HwNewDevInfo
= HeapReAlloc(GetProcessHeap(),
259 ClassDevInfo
->HwDevInfo
,
260 (ClassDevInfo
->ItemCount
+ 1) *
262 if (HwNewDevInfo
!= NULL
)
264 ClassDevInfo
->HwDevInfo
= HwNewDevInfo
;
268 DPRINT1("Unable to allocate memory for %d SP_DEVINFO_DATA structures!\n",
269 ClassDevInfo
->ItemCount
+ 1);
275 ClassDevInfo
->HwDevInfo
= HeapAlloc(GetProcessHeap(),
278 if (ClassDevInfo
->HwDevInfo
== NULL
)
280 DPRINT1("Unable to allocate memory for a SP_DEVINFO_DATA structures!\n");
285 ClassDevInfo
->HwDevInfo
[ClassDevInfo
->ItemCount
].ClassDevInfo
= ClassDevInfo
;
286 ClassDevInfo
->HwDevInfo
[ClassDevInfo
->ItemCount
++].DevInfoData
= DevInfoData
;
296 FillDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
298 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
299 PHWDEVINFO HwDevInfo
, LastHwDevInfo
;
303 BuildDevicesList(hpd
);
305 ClassDevInfo
= hpd
->ClassDevInfo
;
306 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
308 while (ClassDevInfo
!= LastClassDevInfo
)
310 if (ClassDevInfo
->HwDevInfo
!= NULL
)
312 HwDevInfo
= ClassDevInfo
->HwDevInfo
;
313 LastHwDevInfo
= HwDevInfo
+ ClassDevInfo
->ItemCount
;
315 while (HwDevInfo
!= LastHwDevInfo
)
321 if ((SetupDiGetDeviceRegistryProperty(ClassDevInfo
->hDevInfo
,
322 &HwDevInfo
->DevInfoData
,
328 SetupDiGetDeviceRegistryProperty(ClassDevInfo
->hDevInfo
,
329 &HwDevInfo
->DevInfoData
,
335 RegDataType
== REG_SZ
)
337 /* FIXME - check string for NULL termination! */
339 li
.mask
= LVIF_PARAM
| LVIF_STATE
| LVIF_TEXT
| LVIF_IMAGE
;
340 li
.iItem
= ItemCount
;
342 li
.state
= (ItemCount
== 0 ? LVIS_SELECTED
: 0);
343 li
.stateMask
= LVIS_SELECTED
;
344 li
.pszText
= szBuffer
;
345 li
.iImage
= ClassDevInfo
->ImageIndex
;
346 li
.lParam
= (LPARAM
)HwDevInfo
;
348 iItem
= ListView_InsertItem(hpd
->hWndDevList
,
354 if (SetupDiGetClassDescription(&ClassDevInfo
->Guid
,
356 sizeof(szBuffer
) / sizeof(szBuffer
[0]),
363 ListView_SetItem(hpd
->hWndDevList
,
376 /* update the controls */
377 UpdateControlStates(hpd
);
383 ParentSubWndProc(IN HWND hwnd
,
388 PHARDWARE_PAGE_DATA hpd
;
390 hpd
= (PHARDWARE_PAGE_DATA
)GetProp(hwnd
,
391 L
"DevMgrSubClassInfo");
396 /* resize the hardware page */
397 SetWindowPos(hpd
->hWnd
,
406 /* pass the message the the old window proc */
407 return CallWindowProc(hpd
->ParentOldWndProc
,
415 /* this is not a good idea if the subclassed window was an ansi
416 window, but we failed finding out the previous window proc
417 so we can't use CallWindowProc. This should rarely - if ever -
420 return DefWindowProc(hwnd
,
429 HardwareDlgResize(IN PHARDWARE_PAGE_DATA hpd
,
434 HWND hControl
, hButton
;
438 POINT ptMargin
= {0};
439 POINT ptMarginGroup
= {0};
441 /* use left margin of the IDC_DEVICES label as the right
442 margin of all controls outside the group box */
443 hControl
= GetDlgItem(hpd
->hWnd
,
445 GetWindowRect(hControl
,
447 MapWindowPoints(hControl
,
452 Width
= cx
- (2 * ptMargin
.x
);
454 if ((dwp
= BeginDeferWindowPos(8)))
456 /* rc already has the window rect of IDC_DEVICES! */
457 if (!(dwp
= DeferWindowPos(dwp
,
464 SWP_NOMOVE
| SWP_NOZORDER
)))
469 /* resize the devices list view control */
470 GetWindowRect(hpd
->hWndDevList
,
472 MapWindowPoints(hpd
->hWndDevList
,
476 y
= pt
.y
+ hpd
->DevListViewHeight
+ ptMargin
.y
;
477 if (!(dwp
= DeferWindowPos(dwp
,
483 hpd
->DevListViewHeight
,
484 SWP_NOMOVE
| SWP_NOZORDER
)))
489 /* resize the group box control */
490 hControl
= GetDlgItem(hpd
->hWnd
,
491 IDC_PROPERTIESGROUP
);
492 GetWindowRect(hControl
,
494 if (!(dwp
= DeferWindowPos(dwp
,
506 /* use left margin of the IDC_MANUFACTURER label as the right
507 margin of all controls inside the group box */
508 hControl
= GetDlgItem(hpd
->hWnd
,
510 GetWindowRect(hControl
,
512 MapWindowPoints(hControl
,
517 ptMarginGroup
.y
= ptMargin
.y
* 2;
518 Width
= cx
- (2 * ptMarginGroup
.x
);
519 y
+= ptMarginGroup
.y
;
520 if (!(dwp
= DeferWindowPos(dwp
,
531 y
+= rc
.bottom
- rc
.top
+ (ptMargin
.y
/ 2);
533 /* resize the IDC_LOCATION label */
534 hControl
= GetDlgItem(hpd
->hWnd
,
536 GetWindowRect(hControl
,
538 if (!(dwp
= DeferWindowPos(dwp
,
549 y
+= rc
.bottom
- rc
.top
+ (ptMargin
.y
/ 2);
551 /* measure the size of the buttons */
552 hButton
= GetDlgItem(hpd
->hWnd
,
554 GetWindowRect(hButton
,
557 /* resize the IDC_STATUS label */
558 hControl
= GetDlgItem(hpd
->hWnd
,
560 GetWindowRect(hControl
,
562 if (!(dwp
= DeferWindowPos(dwp
,
568 cy
- y
- (3 * ptMargin
.y
) -
569 (rcButton
.bottom
- rcButton
.top
),
575 /* move the IDC_PROPERTIES button */
576 y
= cy
- (2 * ptMargin
.y
) - (rcButton
.bottom
- rcButton
.top
);
577 x
= cx
- ptMarginGroup
.x
- (rcButton
.right
- rcButton
.left
);
578 if (!(dwp
= DeferWindowPos(dwp
,
585 SWP_NOSIZE
| SWP_NOZORDER
)))
590 /* move the IDC_TROUBLESHOOT button */
591 hButton
= GetDlgItem(hpd
->hWnd
,
593 GetWindowRect(hButton
,
595 x
-= (ptMargin
.x
/ 2) + (rcButton
.right
- rcButton
.left
);
596 if (!(dwp
= DeferWindowPos(dwp
,
603 SWP_NOSIZE
| SWP_NOZORDER
)))
608 EndDeferWindowPos(dwp
);
614 EnableTroubleShoot(PHARDWARE_PAGE_DATA hpd
,
617 HWND hBtnTroubleShoot
= GetDlgItem(hpd
->hWnd
,
620 ShowWindow(hBtnTroubleShoot
,
621 Enable
? SW_SHOW
: SW_HIDE
);
627 HardwareDlgProc(IN HWND hwndDlg
,
632 PHARDWARE_PAGE_DATA hpd
;
634 hpd
= (PHARDWARE_PAGE_DATA
)GetWindowLongPtr(hwndDlg
,
637 if (hpd
!= NULL
|| uMsg
== WM_INITDIALOG
)
643 NMHDR
*pnmh
= (NMHDR
*)lParam
;
644 if (pnmh
->hwndFrom
== hpd
->hWndDevList
)
648 case LVN_ITEMCHANGED
:
650 LPNMLISTVIEW pnmv
= (LPNMLISTVIEW
)lParam
;
652 if ((pnmv
->uChanged
& LVIF_STATE
) &&
653 ((pnmv
->uOldState
& (LVIS_FOCUSED
| LVIS_SELECTED
)) ||
654 (pnmv
->uNewState
& (LVIS_FOCUSED
| LVIS_SELECTED
))))
656 UpdateControlStates(hpd
);
666 HardwareDlgResize(hpd
,
668 (INT
)HIWORD(lParam
));
673 LPCWSTR szWndText
= (LPCWSTR
)lParam
;
674 EnableTroubleShoot(hpd
,
675 (szWndText
!= NULL
&& szWndText
[0] != L
'\0'));
681 hpd
= (PHARDWARE_PAGE_DATA
)lParam
;
687 SetWindowLongPtr(hwndDlg
,
691 hpd
->ClassImageListData
.cbSize
= sizeof(SP_CLASSIMAGELIST_DATA
);
693 SetupDiGetClassImageList(&hpd
->ClassImageListData
);
695 /* calculate the size of the devices list view control */
696 hpd
->hWndDevList
= GetDlgItem(hwndDlg
,
698 if (hpd
->hWndDevList
!= NULL
)
701 GetClientRect(hpd
->hWndDevList
,
703 hpd
->DevListViewHeight
= rcClient
.bottom
;
705 if (hpd
->DisplayMode
== HWPD_LARGELIST
)
707 hpd
->DevListViewHeight
= (hpd
->DevListViewHeight
* 3) / 2;
711 /* subclass the parent window */
712 hWndParent
= GetAncestor(hwndDlg
,
714 if (hWndParent
!= NULL
)
718 if (GetClientRect(hWndParent
,
720 SetWindowPos(hwndDlg
,
728 /* subclass the parent window. This is not safe
729 if the parent window belongs to another thread! */
730 hpd
->ParentOldWndProc
= (WNDPROC
)SetWindowLongPtr(hWndParent
,
732 (LONG_PTR
)ParentSubWndProc
);
734 if (hpd
->ParentOldWndProc
!= NULL
&&
736 L
"DevMgrSubClassInfo",
739 hpd
->hWndParent
= hWndParent
;
744 /* initialize the devices list view control */
745 InitializeDevicesList(hpd
);
747 /* fill the devices list view control */
748 FillDevicesList(hpd
);
750 /* decide whether to show or hide the troubleshoot button */
751 EnableTroubleShoot(hpd
,
752 GetWindowTextLength(hwndDlg
) != 0);
759 /* free devices list */
760 FreeDevicesList(hpd
);
762 /* restore the old window proc of the subclassed parent window */
763 if (hpd
->hWndParent
!= NULL
&& hpd
->ParentOldWndProc
!= NULL
)
765 SetWindowLongPtr(hpd
->hWndParent
,
767 (LONG_PTR
)hpd
->ParentOldWndProc
);
770 if (hpd
->ClassImageListData
.ImageList
!= NULL
)
772 SetupDiDestroyClassImageList(&hpd
->ClassImageListData
);
775 /* free the reference to comctl32 */
776 FreeLibrary(hpd
->hComCtl32
);
777 hpd
->hComCtl32
= NULL
;
779 /* free the allocated resources */
780 HeapFree(GetProcessHeap(),
792 /***************************************************************************
794 * DeviceCreateHardwarePageEx
797 * Creates a hardware page
800 * hWndParent: Handle to the parent window
801 * lpGuids: An array of guids of devices that are to be listed
802 * uNumberOfGuids: Numbers of guids in the Guids array
803 * Unknown: Unknown parameter, see NOTEs
806 * Returns the handle of the hardware page window that has been created or
810 * 13-05-2005 first working version (Sebastian Gasiorek <zebasoftis@gmail.com>)
813 * missing: device icon in list view, Troubleshoot button, device properties,
814 * status description,
815 * devices should be visible afer PSN_SETACTIVE message
820 DeviceCreateHardwarePageEx(IN HWND hWndParent
,
822 IN UINT uNumberOfGuids
,
823 IN HWPAGE_DISPLAYMODE DisplayMode
)
825 PHARDWARE_PAGE_DATA hpd
;
826 PINITCOMMONCONTROLS pInitCommonControls
;
828 /* allocate the HARDWARE_PAGE_DATA structure. Make sure it is
829 zeroed because the initialization code assumes that in
831 hpd
= HeapAlloc(GetProcessHeap(),
833 FIELD_OFFSET(HARDWARE_PAGE_DATA
,
835 (uNumberOfGuids
* sizeof(HWCLASSDEVINFO
)));
841 hpd
->DisplayMode
= ((DisplayMode
> HWPD_MAX
) ? HWPD_STANDARDLIST
: DisplayMode
);
843 /* initialize the HARDWARE_PAGE_DATA structure */
844 hpd
->NumberOfGuids
= uNumberOfGuids
;
849 hpd
->ClassDevInfo
[i
].Guid
= lpGuids
[i
];
852 /* load comctl32.dll dynamically */
853 hpd
->hComCtl32
= LoadLibrary(TEXT("comctl32.dll"));
854 if (hpd
->hComCtl32
== NULL
)
859 /* initialize the common controls */
860 pInitCommonControls
= (PINITCOMMONCONTROLS
)GetProcAddress(hpd
->hComCtl32
,
861 "InitCommonControls");
862 if (pInitCommonControls
== NULL
)
866 pInitCommonControls();
868 /* create the dialog */
869 hWnd
= CreateDialogParam(hDllInstance
,
870 MAKEINTRESOURCE(IDD_HARDWARE
),
881 /* oops, something went wrong... */
882 if (hpd
->hComCtl32
!= NULL
)
884 FreeLibrary(hpd
->hComCtl32
);
887 HeapFree(GetProcessHeap(),
897 /***************************************************************************
899 * DeviceCreateHardwarePage
902 * Creates a hardware page
905 * hWndParent: Handle to the parent window
906 * lpGuid: Guid of the device
909 * Returns the handle of the hardware page window that has been created or
919 DeviceCreateHardwarePage(IN HWND hWndParent
,
922 return DeviceCreateHardwarePageEx(hWndParent
,