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>
35 HWPD_STANDARDLIST
= 0,
37 HWPD_MAX
= HWPD_LARGELIST
38 } HWPAGE_DISPLAYMODE
, *PHWPAGE_DISPLAYMODE
;
40 typedef struct _HWDEVINFO
42 struct _HWCLASSDEVINFO
*ClassDevInfo
;
43 SP_DEVINFO_DATA DevInfoData
;
44 } HWDEVINFO
, *PHWDEVINFO
;
46 typedef struct _HWCLASSDEVINFO
53 } HWCLASSDEVINFO
, *PHWCLASSDEVINFO
;
55 typedef struct _HARDWARE_PAGE_DATA
59 HINSTANCE hComCtl32
; /* only save this to keep track of the references */
60 INT DevListViewHeight
;
61 SP_CLASSIMAGELIST_DATA ClassImageListData
;
62 HWPAGE_DISPLAYMODE DisplayMode
;
64 /* parent window subclass info */
65 WNDPROC ParentOldWndProc
;
69 HWCLASSDEVINFO ClassDevInfo
[1];
70 /* struct may be dynamically expanded here! */
71 } HARDWARE_PAGE_DATA
, *PHARDWARE_PAGE_DATA
;
73 #define CX_TYPECOLUMN_WIDTH 80
76 InitializeDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
83 /* set the list view style */
84 ListView_SetExtendedListViewStyle(hpd
->hWndDevList
,
85 LVS_EX_FULLROWSELECT
);
87 /* set the list view image list */
88 if (hpd
->ClassImageListData
.ImageList
!= NULL
)
90 ListView_SetImageList(hpd
->hWndDevList
,
91 hpd
->ClassImageListData
.ImageList
,
95 GetClientRect(hpd
->hWndDevList
,
98 /* add the list view columns */
99 lvc
.mask
= LVCF_TEXT
| LVCF_WIDTH
;
100 lvc
.fmt
= LVCFMT_LEFT
;
101 lvc
.pszText
= szColName
;
103 if (LoadString(hDllInstance
,
106 sizeof(szColName
) / sizeof(szColName
[0])))
108 lvc
.cx
= rcClient
.right
- CX_TYPECOLUMN_WIDTH
-
109 GetSystemMetrics(SM_CXVSCROLL
);
110 ListView_InsertColumn(hpd
->hWndDevList
,
114 if (LoadString(hDllInstance
,
117 sizeof(szColName
) / sizeof(szColName
[0])))
119 lvc
.cx
= CX_TYPECOLUMN_WIDTH
;
120 ListView_InsertColumn(hpd
->hWndDevList
,
128 DisplaySelectedDeviceProperties(IN PHARDWARE_PAGE_DATA hpd
)
130 PHWDEVINFO HwDevInfo
;
133 HwDevInfo
= (PHWDEVINFO
)ListViewGetSelectedItemData(hpd
->hWndDevList
);
134 if (HwDevInfo
!= NULL
)
136 Ret
= DisplayDeviceAdvancedProperties(hpd
->hWnd
,
137 HwDevInfo
->ClassDevInfo
->hDevInfo
,
138 &HwDevInfo
->DevInfoData
,
139 hpd
->hComCtl32
) != -1;
147 UpdateControlStates(IN PHARDWARE_PAGE_DATA hpd
)
149 PHWDEVINFO HwDevInfo
;
150 HWND hBtnTroubleShoot
, hBtnProperties
;
152 hBtnTroubleShoot
= GetDlgItem(hpd
->hWnd
,
154 hBtnProperties
= GetDlgItem(hpd
->hWnd
,
157 HwDevInfo
= (PHWDEVINFO
)ListViewGetSelectedItemData(hpd
->hWndDevList
);
158 if (HwDevInfo
!= NULL
)
160 /* update static controls */
165 LPWSTR szFormatted
= NULL
;
167 /* get the manufacturer string */
168 if (!SetupDiGetDeviceRegistryProperty(HwDevInfo
->ClassDevInfo
->hDevInfo
,
169 &HwDevInfo
->DevInfoData
,
175 RegDataType
!= REG_SZ
)
178 LoadString(hDllInstance
,
181 sizeof(szBuffer
) / sizeof(szBuffer
[0]));
183 /* FIXME - check string for NULL termination! */
184 if (LoadAndFormatString(hDllInstance
,
189 SetDlgItemText(hpd
->hWnd
,
192 LocalFree((HLOCAL
)szFormatted
);
195 /* get the location string */
196 DataSize
= sizeof(szBuffer
);
197 cRet
= CM_Get_DevNode_Registry_Property(HwDevInfo
->DevInfoData
.DevInst
,
198 CM_DRP_LOCATION_INFORMATION
,
203 if (cRet
!= CR_SUCCESS
||
204 RegDataType
!= REG_SZ
)
207 LoadString(hDllInstance
,
210 sizeof(szBuffer
) / sizeof(szBuffer
[0]));
212 /* FIXME - check string for NULL termination! */
214 if (szBuffer
[0] >= L
'0' && szBuffer
[0] <= L
'9')
216 /* convert the string to an integer value and create a
218 ULONG ulLocation
= (ULONG
)wcstoul(szBuffer
,
221 if (LoadAndFormatString(hDllInstance
,
229 (sizeof(szBuffer
) / sizeof(szBuffer
[0])) - 1);
230 szBuffer
[(sizeof(szBuffer
) / sizeof(szBuffer
[0])) - 1] = L
'\0';
231 LocalFree((HLOCAL
)szFormatted
);
235 if (LoadAndFormatString(hDllInstance
,
240 SetDlgItemText(hpd
->hWnd
,
243 LocalFree((HLOCAL
)szFormatted
);
246 /* FIXME - get the device status text */
247 LoadString(hDllInstance
,
250 sizeof(szBuffer
) / sizeof(szBuffer
[0]));
252 if (LoadAndFormatString(hDllInstance
,
257 SetDlgItemText(hpd
->hWnd
,
260 LocalFree((HLOCAL
)szFormatted
);
265 /* clear static controls */
266 SetDlgItemText(hpd
->hWnd
,
269 SetDlgItemText(hpd
->hWnd
,
272 SetDlgItemText(hpd
->hWnd
,
277 EnableWindow(hBtnTroubleShoot
,
279 EnableWindow(hBtnProperties
,
285 FreeDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
287 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
289 ClassDevInfo
= hpd
->ClassDevInfo
;
290 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
292 /* free the device info set handles and structures */
293 while (ClassDevInfo
!= LastClassDevInfo
)
295 if (ClassDevInfo
->hDevInfo
!= INVALID_HANDLE_VALUE
)
297 SetupDiDestroyDeviceInfoList(ClassDevInfo
->hDevInfo
);
298 ClassDevInfo
->hDevInfo
= INVALID_HANDLE_VALUE
;
301 ClassDevInfo
->ItemCount
= 0;
302 ClassDevInfo
->ImageIndex
= 0;
304 if (ClassDevInfo
->HwDevInfo
!= NULL
)
306 HeapFree(GetProcessHeap(),
308 ClassDevInfo
->HwDevInfo
);
309 ClassDevInfo
->HwDevInfo
= NULL
;
318 BuildDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
320 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
321 SP_DEVINFO_DATA DevInfoData
;
323 DevInfoData
.cbSize
= sizeof(SP_DEVINFO_DATA
);
325 ClassDevInfo
= hpd
->ClassDevInfo
;
326 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
328 while (ClassDevInfo
!= LastClassDevInfo
)
330 ClassDevInfo
->ImageIndex
= -1;
332 /* open a class device handle for the GUID we're processing */
333 ClassDevInfo
->hDevInfo
= SetupDiGetClassDevs(&ClassDevInfo
->Guid
,
337 if (ClassDevInfo
->hDevInfo
!= INVALID_HANDLE_VALUE
)
339 DWORD MemberIndex
= 0;
341 SetupDiGetClassImageIndex(&hpd
->ClassImageListData
,
343 &ClassDevInfo
->ImageIndex
);
345 /* enumerate all devices in the class */
346 while (SetupDiEnumDeviceInfo(ClassDevInfo
->hDevInfo
,
350 if (ClassDevInfo
->HwDevInfo
!= NULL
)
352 PHWDEVINFO HwNewDevInfo
= HeapReAlloc(GetProcessHeap(),
354 ClassDevInfo
->HwDevInfo
,
355 (ClassDevInfo
->ItemCount
+ 1) *
357 if (HwNewDevInfo
!= NULL
)
359 ClassDevInfo
->HwDevInfo
= HwNewDevInfo
;
363 DPRINT1("Unable to allocate memory for %d SP_DEVINFO_DATA structures!\n",
364 ClassDevInfo
->ItemCount
+ 1);
370 ClassDevInfo
->HwDevInfo
= HeapAlloc(GetProcessHeap(),
373 if (ClassDevInfo
->HwDevInfo
== NULL
)
375 DPRINT1("Unable to allocate memory for a SP_DEVINFO_DATA structures!\n");
380 /* save all information for the current device */
381 ClassDevInfo
->HwDevInfo
[ClassDevInfo
->ItemCount
].ClassDevInfo
= ClassDevInfo
;
382 ClassDevInfo
->HwDevInfo
[ClassDevInfo
->ItemCount
++].DevInfoData
= DevInfoData
;
392 FillDevicesListViewControl(IN PHARDWARE_PAGE_DATA hpd
)
394 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
395 PHWDEVINFO HwDevInfo
, LastHwDevInfo
;
399 BuildDevicesList(hpd
);
401 ClassDevInfo
= hpd
->ClassDevInfo
;
402 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
404 while (ClassDevInfo
!= LastClassDevInfo
)
406 if (ClassDevInfo
->HwDevInfo
!= NULL
)
408 HwDevInfo
= ClassDevInfo
->HwDevInfo
;
409 LastHwDevInfo
= HwDevInfo
+ ClassDevInfo
->ItemCount
;
411 while (HwDevInfo
!= LastHwDevInfo
)
417 /* get the device name */
418 if ((SetupDiGetDeviceRegistryProperty(ClassDevInfo
->hDevInfo
,
419 &HwDevInfo
->DevInfoData
,
425 SetupDiGetDeviceRegistryProperty(ClassDevInfo
->hDevInfo
,
426 &HwDevInfo
->DevInfoData
,
432 RegDataType
== REG_SZ
)
434 /* FIXME - check string for NULL termination! */
436 li
.mask
= LVIF_PARAM
| LVIF_STATE
| LVIF_TEXT
| LVIF_IMAGE
;
437 li
.iItem
= ItemCount
;
439 li
.state
= (ItemCount
== 0 ? LVIS_SELECTED
: 0);
440 li
.stateMask
= LVIS_SELECTED
;
441 li
.pszText
= szBuffer
;
442 li
.iImage
= ClassDevInfo
->ImageIndex
;
443 li
.lParam
= (LPARAM
)HwDevInfo
;
445 iItem
= ListView_InsertItem(hpd
->hWndDevList
,
451 /* get the device type for the second column */
452 if (SetupDiGetClassDescription(&ClassDevInfo
->Guid
,
454 sizeof(szBuffer
) / sizeof(szBuffer
[0]),
461 ListView_SetItem(hpd
->hWndDevList
,
474 /* update the controls */
475 UpdateControlStates(hpd
);
481 ParentSubWndProc(IN HWND hwnd
,
486 PHARDWARE_PAGE_DATA hpd
;
488 hpd
= (PHARDWARE_PAGE_DATA
)GetProp(hwnd
,
489 L
"DevMgrSubClassInfo");
494 /* resize the hardware page */
495 SetWindowPos(hpd
->hWnd
,
504 /* pass the message the the old window proc */
505 return CallWindowProc(hpd
->ParentOldWndProc
,
513 /* this is not a good idea if the subclassed window was an ansi
514 window, but we failed finding out the previous window proc
515 so we can't use CallWindowProc. This should rarely - if ever -
518 return DefWindowProc(hwnd
,
527 HardwareDlgResize(IN PHARDWARE_PAGE_DATA hpd
,
532 HWND hControl
, hButton
;
536 POINT ptMargin
= {0};
537 POINT ptMarginGroup
= {0};
539 /* use left margin of the IDC_DEVICES label as the right
540 margin of all controls outside the group box */
541 hControl
= GetDlgItem(hpd
->hWnd
,
543 GetWindowRect(hControl
,
545 MapWindowPoints(hControl
,
550 Width
= cx
- (2 * ptMargin
.x
);
552 if ((dwp
= BeginDeferWindowPos(8)))
554 /* rc already has the window rect of IDC_DEVICES! */
555 if (!(dwp
= DeferWindowPos(dwp
,
562 SWP_NOMOVE
| SWP_NOZORDER
)))
567 /* resize the devices list view control */
568 GetWindowRect(hpd
->hWndDevList
,
570 MapWindowPoints(hpd
->hWndDevList
,
574 y
= pt
.y
+ hpd
->DevListViewHeight
+ ptMargin
.y
;
575 if (!(dwp
= DeferWindowPos(dwp
,
581 hpd
->DevListViewHeight
,
582 SWP_NOMOVE
| SWP_NOZORDER
)))
587 /* resize the group box control */
588 hControl
= GetDlgItem(hpd
->hWnd
,
589 IDC_PROPERTIESGROUP
);
590 GetWindowRect(hControl
,
592 if (!(dwp
= DeferWindowPos(dwp
,
604 /* use left margin of the IDC_MANUFACTURER label as the right
605 margin of all controls inside the group box */
606 hControl
= GetDlgItem(hpd
->hWnd
,
608 GetWindowRect(hControl
,
610 MapWindowPoints(hControl
,
615 ptMarginGroup
.y
= ptMargin
.y
* 2;
616 Width
= cx
- (2 * ptMarginGroup
.x
);
617 y
+= ptMarginGroup
.y
;
618 if (!(dwp
= DeferWindowPos(dwp
,
629 y
+= rc
.bottom
- rc
.top
+ (ptMargin
.y
/ 2);
631 /* resize the IDC_LOCATION label */
632 hControl
= GetDlgItem(hpd
->hWnd
,
634 GetWindowRect(hControl
,
636 if (!(dwp
= DeferWindowPos(dwp
,
647 y
+= rc
.bottom
- rc
.top
+ (ptMargin
.y
/ 2);
649 /* measure the size of the buttons */
650 hButton
= GetDlgItem(hpd
->hWnd
,
652 GetWindowRect(hButton
,
655 /* resize the IDC_STATUS label */
656 hControl
= GetDlgItem(hpd
->hWnd
,
658 GetWindowRect(hControl
,
660 if (!(dwp
= DeferWindowPos(dwp
,
666 cy
- y
- (3 * ptMargin
.y
) -
667 (rcButton
.bottom
- rcButton
.top
),
673 /* move the IDC_PROPERTIES button */
674 y
= cy
- (2 * ptMargin
.y
) - (rcButton
.bottom
- rcButton
.top
);
675 x
= cx
- ptMarginGroup
.x
- (rcButton
.right
- rcButton
.left
);
676 if (!(dwp
= DeferWindowPos(dwp
,
683 SWP_NOSIZE
| SWP_NOZORDER
)))
688 /* move the IDC_TROUBLESHOOT button */
689 hButton
= GetDlgItem(hpd
->hWnd
,
691 GetWindowRect(hButton
,
693 x
-= (ptMargin
.x
/ 2) + (rcButton
.right
- rcButton
.left
);
694 if (!(dwp
= DeferWindowPos(dwp
,
701 SWP_NOSIZE
| SWP_NOZORDER
)))
706 EndDeferWindowPos(dwp
);
712 EnableTroubleShoot(PHARDWARE_PAGE_DATA hpd
,
715 HWND hBtnTroubleShoot
= GetDlgItem(hpd
->hWnd
,
718 ShowWindow(hBtnTroubleShoot
,
719 Enable
? SW_SHOW
: SW_HIDE
);
725 HardwareDlgProc(IN HWND hwndDlg
,
730 PHARDWARE_PAGE_DATA hpd
;
732 hpd
= (PHARDWARE_PAGE_DATA
)GetWindowLongPtr(hwndDlg
,
735 if (hpd
!= NULL
|| uMsg
== WM_INITDIALOG
)
741 NMHDR
*pnmh
= (NMHDR
*)lParam
;
742 if (pnmh
->hwndFrom
== hpd
->hWndDevList
)
746 case LVN_ITEMCHANGED
:
748 LPNMLISTVIEW pnmv
= (LPNMLISTVIEW
)lParam
;
750 if ((pnmv
->uChanged
& LVIF_STATE
) &&
751 ((pnmv
->uOldState
& (LVIS_FOCUSED
| LVIS_SELECTED
)) ||
752 (pnmv
->uNewState
& (LVIS_FOCUSED
| LVIS_SELECTED
))))
754 UpdateControlStates(hpd
);
765 switch (LOWORD(wParam
))
767 case IDC_TROUBLESHOOT
:
769 /* FIXME - start the help using the command in the window text */
775 DisplaySelectedDeviceProperties(hpd
);
783 HardwareDlgResize(hpd
,
785 (INT
)HIWORD(lParam
));
790 LPCWSTR szWndText
= (LPCWSTR
)lParam
;
791 EnableTroubleShoot(hpd
,
792 (szWndText
!= NULL
&& szWndText
[0] != L
'\0'));
798 hpd
= (PHARDWARE_PAGE_DATA
)lParam
;
804 SetWindowLongPtr(hwndDlg
,
808 hpd
->ClassImageListData
.cbSize
= sizeof(SP_CLASSIMAGELIST_DATA
);
810 SetupDiGetClassImageList(&hpd
->ClassImageListData
);
812 /* calculate the size of the devices list view control */
813 hpd
->hWndDevList
= GetDlgItem(hwndDlg
,
815 if (hpd
->hWndDevList
!= NULL
)
818 GetClientRect(hpd
->hWndDevList
,
820 hpd
->DevListViewHeight
= rcClient
.bottom
;
822 if (hpd
->DisplayMode
== HWPD_LARGELIST
)
824 hpd
->DevListViewHeight
= (hpd
->DevListViewHeight
* 3) / 2;
828 /* subclass the parent window */
829 hWndParent
= GetAncestor(hwndDlg
,
831 if (hWndParent
!= NULL
)
835 if (GetClientRect(hWndParent
,
837 SetWindowPos(hwndDlg
,
845 /* subclass the parent window. This is not safe
846 if the parent window belongs to another thread! */
847 hpd
->ParentOldWndProc
= (WNDPROC
)SetWindowLongPtr(hWndParent
,
849 (LONG_PTR
)ParentSubWndProc
);
851 if (hpd
->ParentOldWndProc
!= NULL
&&
853 L
"DevMgrSubClassInfo",
856 hpd
->hWndParent
= hWndParent
;
861 /* initialize the devices list view control */
862 InitializeDevicesList(hpd
);
864 /* fill the devices list view control */
865 FillDevicesListViewControl(hpd
);
867 /* decide whether to show or hide the troubleshoot button */
868 EnableTroubleShoot(hpd
,
869 GetWindowTextLength(hwndDlg
) != 0);
876 /* free devices list */
877 FreeDevicesList(hpd
);
879 /* restore the old window proc of the subclassed parent window */
880 if (hpd
->hWndParent
!= NULL
&& hpd
->ParentOldWndProc
!= NULL
)
882 SetWindowLongPtr(hpd
->hWndParent
,
884 (LONG_PTR
)hpd
->ParentOldWndProc
);
887 if (hpd
->ClassImageListData
.ImageList
!= NULL
)
889 SetupDiDestroyClassImageList(&hpd
->ClassImageListData
);
892 /* free the reference to comctl32 */
893 FreeLibrary(hpd
->hComCtl32
);
894 hpd
->hComCtl32
= NULL
;
896 /* free the allocated resources */
897 HeapFree(GetProcessHeap(),
909 /***************************************************************************
911 * DeviceCreateHardwarePageEx
914 * Creates a hardware page
917 * hWndParent: Handle to the parent window
918 * lpGuids: An array of guids of devices that are to be listed
919 * uNumberOfGuids: Numbers of guids in the Guids array
920 * DisplayMode: Sets the size of the device list view control
923 * Returns the handle of the hardware page window that has been created or
930 DeviceCreateHardwarePageEx(IN HWND hWndParent
,
932 IN UINT uNumberOfGuids
,
933 IN HWPAGE_DISPLAYMODE DisplayMode
)
935 PHARDWARE_PAGE_DATA hpd
;
937 /* allocate the HARDWARE_PAGE_DATA structure. Make sure it is
938 zeroed because the initialization code assumes that in
940 hpd
= HeapAlloc(GetProcessHeap(),
942 FIELD_OFFSET(HARDWARE_PAGE_DATA
,
944 (uNumberOfGuids
* sizeof(HWCLASSDEVINFO
)));
950 hpd
->DisplayMode
= ((DisplayMode
> HWPD_MAX
) ? HWPD_STANDARDLIST
: DisplayMode
);
952 /* initialize the HARDWARE_PAGE_DATA structure */
953 hpd
->NumberOfGuids
= uNumberOfGuids
;
958 hpd
->ClassDevInfo
[i
].hDevInfo
= INVALID_HANDLE_VALUE
;
959 hpd
->ClassDevInfo
[i
].Guid
= lpGuids
[i
];
962 /* load comctl32.dll dynamically */
963 hpd
->hComCtl32
= LoadAndInitComctl32();
964 if (hpd
->hComCtl32
== NULL
)
969 /* create the dialog */
970 hWnd
= CreateDialogParam(hDllInstance
,
971 MAKEINTRESOURCE(IDD_HARDWARE
),
982 /* oops, something went wrong... */
983 if (hpd
->hComCtl32
!= NULL
)
985 FreeLibrary(hpd
->hComCtl32
);
988 HeapFree(GetProcessHeap(),
998 /***************************************************************************
1000 * DeviceCreateHardwarePage
1003 * Creates a hardware page
1006 * hWndParent: Handle to the parent window
1007 * lpGuid: Guid of the device
1010 * Returns the handle of the hardware page window that has been created or
1011 * NULL if it failed.
1017 DeviceCreateHardwarePage(IN HWND hWndParent
,
1020 return DeviceCreateHardwarePageEx(hWndParent
,