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 */
162 LPWSTR szFormatted
= NULL
;
164 /* get the manufacturer string */
165 if (GetDeviceManufacturerString(HwDevInfo
->ClassDevInfo
->hDevInfo
,
166 &HwDevInfo
->DevInfoData
,
168 sizeof(szBuffer
) / sizeof(szBuffer
[0])) &&
169 LoadAndFormatString(hDllInstance
,
174 SetDlgItemText(hpd
->hWnd
,
177 LocalFree((HLOCAL
)szFormatted
);
180 /* get the location string */
181 if (GetDeviceLocationString(HwDevInfo
->DevInfoData
.DevInst
,
183 sizeof(szBuffer
) / sizeof(szBuffer
[0])) &&
184 LoadAndFormatString(hDllInstance
,
189 SetDlgItemText(hpd
->hWnd
,
192 LocalFree((HLOCAL
)szFormatted
);
195 if (GetDeviceStatusString(HwDevInfo
->ClassDevInfo
->hDevInfo
,
196 &HwDevInfo
->DevInfoData
,
198 sizeof(szBuffer
) / sizeof(szBuffer
[0])) &&
199 LoadAndFormatString(hDllInstance
,
204 SetDlgItemText(hpd
->hWnd
,
207 LocalFree((HLOCAL
)szFormatted
);
212 /* clear static controls */
213 SetDlgItemText(hpd
->hWnd
,
216 SetDlgItemText(hpd
->hWnd
,
219 SetDlgItemText(hpd
->hWnd
,
224 EnableWindow(hBtnTroubleShoot
,
226 EnableWindow(hBtnProperties
,
232 FreeDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
234 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
236 ClassDevInfo
= hpd
->ClassDevInfo
;
237 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
239 /* free the device info set handles and structures */
240 while (ClassDevInfo
!= LastClassDevInfo
)
242 if (ClassDevInfo
->hDevInfo
!= INVALID_HANDLE_VALUE
)
244 SetupDiDestroyDeviceInfoList(ClassDevInfo
->hDevInfo
);
245 ClassDevInfo
->hDevInfo
= INVALID_HANDLE_VALUE
;
248 ClassDevInfo
->ItemCount
= 0;
249 ClassDevInfo
->ImageIndex
= 0;
251 if (ClassDevInfo
->HwDevInfo
!= NULL
)
253 HeapFree(GetProcessHeap(),
255 ClassDevInfo
->HwDevInfo
);
256 ClassDevInfo
->HwDevInfo
= NULL
;
265 BuildDevicesList(IN PHARDWARE_PAGE_DATA hpd
)
267 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
268 SP_DEVINFO_DATA DevInfoData
;
270 DevInfoData
.cbSize
= sizeof(SP_DEVINFO_DATA
);
272 ClassDevInfo
= hpd
->ClassDevInfo
;
273 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
275 while (ClassDevInfo
!= LastClassDevInfo
)
277 ClassDevInfo
->ImageIndex
= -1;
279 /* open a class device handle for the GUID we're processing */
280 ClassDevInfo
->hDevInfo
= SetupDiGetClassDevs(&ClassDevInfo
->Guid
,
284 if (ClassDevInfo
->hDevInfo
!= INVALID_HANDLE_VALUE
)
286 DWORD MemberIndex
= 0;
288 SetupDiGetClassImageIndex(&hpd
->ClassImageListData
,
290 &ClassDevInfo
->ImageIndex
);
292 /* enumerate all devices in the class */
293 while (SetupDiEnumDeviceInfo(ClassDevInfo
->hDevInfo
,
297 if (ClassDevInfo
->HwDevInfo
!= NULL
)
299 PHWDEVINFO HwNewDevInfo
= HeapReAlloc(GetProcessHeap(),
301 ClassDevInfo
->HwDevInfo
,
302 (ClassDevInfo
->ItemCount
+ 1) *
304 if (HwNewDevInfo
!= NULL
)
306 ClassDevInfo
->HwDevInfo
= HwNewDevInfo
;
310 DPRINT1("Unable to allocate memory for %d SP_DEVINFO_DATA structures!\n",
311 ClassDevInfo
->ItemCount
+ 1);
317 ClassDevInfo
->HwDevInfo
= HeapAlloc(GetProcessHeap(),
320 if (ClassDevInfo
->HwDevInfo
== NULL
)
322 DPRINT1("Unable to allocate memory for a SP_DEVINFO_DATA structures!\n");
327 /* save all information for the current device */
328 ClassDevInfo
->HwDevInfo
[ClassDevInfo
->ItemCount
].ClassDevInfo
= ClassDevInfo
;
329 ClassDevInfo
->HwDevInfo
[ClassDevInfo
->ItemCount
++].DevInfoData
= DevInfoData
;
339 FillDevicesListViewControl(IN PHARDWARE_PAGE_DATA hpd
)
341 PHWCLASSDEVINFO ClassDevInfo
, LastClassDevInfo
;
342 PHWDEVINFO HwDevInfo
, LastHwDevInfo
;
346 BuildDevicesList(hpd
);
348 ClassDevInfo
= hpd
->ClassDevInfo
;
349 LastClassDevInfo
= ClassDevInfo
+ hpd
->NumberOfGuids
;
351 while (ClassDevInfo
!= LastClassDevInfo
)
353 if (ClassDevInfo
->HwDevInfo
!= NULL
)
355 HwDevInfo
= ClassDevInfo
->HwDevInfo
;
356 LastHwDevInfo
= HwDevInfo
+ ClassDevInfo
->ItemCount
;
358 while (HwDevInfo
!= LastHwDevInfo
)
364 /* get the device name */
365 if ((SetupDiGetDeviceRegistryProperty(ClassDevInfo
->hDevInfo
,
366 &HwDevInfo
->DevInfoData
,
372 SetupDiGetDeviceRegistryProperty(ClassDevInfo
->hDevInfo
,
373 &HwDevInfo
->DevInfoData
,
379 RegDataType
== REG_SZ
)
381 /* FIXME - check string for NULL termination! */
383 li
.mask
= LVIF_PARAM
| LVIF_STATE
| LVIF_TEXT
| LVIF_IMAGE
;
384 li
.iItem
= ItemCount
;
386 li
.state
= (ItemCount
== 0 ? LVIS_SELECTED
: 0);
387 li
.stateMask
= LVIS_SELECTED
;
388 li
.pszText
= szBuffer
;
389 li
.iImage
= ClassDevInfo
->ImageIndex
;
390 li
.lParam
= (LPARAM
)HwDevInfo
;
392 iItem
= ListView_InsertItem(hpd
->hWndDevList
,
398 /* get the device type for the second column */
399 if (GetDeviceTypeString(&HwDevInfo
->DevInfoData
,
401 sizeof(szBuffer
) / sizeof(szBuffer
[0])))
407 ListView_SetItem(hpd
->hWndDevList
,
420 /* update the controls */
421 UpdateControlStates(hpd
);
427 ParentSubWndProc(IN HWND hwnd
,
432 PHARDWARE_PAGE_DATA hpd
;
434 hpd
= (PHARDWARE_PAGE_DATA
)GetProp(hwnd
,
435 L
"DevMgrSubClassInfo");
440 /* resize the hardware page */
441 SetWindowPos(hpd
->hWnd
,
450 /* pass the message the the old window proc */
451 return CallWindowProc(hpd
->ParentOldWndProc
,
459 /* this is not a good idea if the subclassed window was an ansi
460 window, but we failed finding out the previous window proc
461 so we can't use CallWindowProc. This should rarely - if ever -
464 return DefWindowProc(hwnd
,
473 HardwareDlgResize(IN PHARDWARE_PAGE_DATA hpd
,
478 HWND hControl
, hButton
;
482 POINT ptMargin
= {0};
483 POINT ptMarginGroup
= {0};
485 /* use left margin of the IDC_DEVICES label as the right
486 margin of all controls outside the group box */
487 hControl
= GetDlgItem(hpd
->hWnd
,
489 GetWindowRect(hControl
,
491 MapWindowPoints(hControl
,
496 Width
= cx
- (2 * ptMargin
.x
);
498 if ((dwp
= BeginDeferWindowPos(8)))
500 /* rc already has the window rect of IDC_DEVICES! */
501 if (!(dwp
= DeferWindowPos(dwp
,
508 SWP_NOMOVE
| SWP_NOZORDER
)))
513 /* resize the devices list view control */
514 GetWindowRect(hpd
->hWndDevList
,
516 MapWindowPoints(hpd
->hWndDevList
,
520 y
= pt
.y
+ hpd
->DevListViewHeight
+ ptMargin
.y
;
521 if (!(dwp
= DeferWindowPos(dwp
,
527 hpd
->DevListViewHeight
,
528 SWP_NOMOVE
| SWP_NOZORDER
)))
533 /* resize the group box control */
534 hControl
= GetDlgItem(hpd
->hWnd
,
535 IDC_PROPERTIESGROUP
);
536 GetWindowRect(hControl
,
538 if (!(dwp
= DeferWindowPos(dwp
,
550 /* use left margin of the IDC_MANUFACTURER label as the right
551 margin of all controls inside the group box */
552 hControl
= GetDlgItem(hpd
->hWnd
,
554 GetWindowRect(hControl
,
556 MapWindowPoints(hControl
,
561 ptMarginGroup
.y
= ptMargin
.y
* 2;
562 Width
= cx
- (2 * ptMarginGroup
.x
);
563 y
+= ptMarginGroup
.y
;
564 if (!(dwp
= DeferWindowPos(dwp
,
575 y
+= rc
.bottom
- rc
.top
+ (ptMargin
.y
/ 2);
577 /* resize the IDC_LOCATION label */
578 hControl
= GetDlgItem(hpd
->hWnd
,
580 GetWindowRect(hControl
,
582 if (!(dwp
= DeferWindowPos(dwp
,
593 y
+= rc
.bottom
- rc
.top
+ (ptMargin
.y
/ 2);
595 /* measure the size of the buttons */
596 hButton
= GetDlgItem(hpd
->hWnd
,
598 GetWindowRect(hButton
,
601 /* resize the IDC_STATUS label */
602 hControl
= GetDlgItem(hpd
->hWnd
,
604 GetWindowRect(hControl
,
606 if (!(dwp
= DeferWindowPos(dwp
,
612 cy
- y
- (3 * ptMargin
.y
) -
613 (rcButton
.bottom
- rcButton
.top
),
619 /* move the IDC_PROPERTIES button */
620 y
= cy
- (2 * ptMargin
.y
) - (rcButton
.bottom
- rcButton
.top
);
621 x
= cx
- ptMarginGroup
.x
- (rcButton
.right
- rcButton
.left
);
622 if (!(dwp
= DeferWindowPos(dwp
,
629 SWP_NOSIZE
| SWP_NOZORDER
)))
634 /* move the IDC_TROUBLESHOOT button */
635 hButton
= GetDlgItem(hpd
->hWnd
,
637 GetWindowRect(hButton
,
639 x
-= (ptMargin
.x
/ 2) + (rcButton
.right
- rcButton
.left
);
640 if (!(dwp
= DeferWindowPos(dwp
,
647 SWP_NOSIZE
| SWP_NOZORDER
)))
652 EndDeferWindowPos(dwp
);
658 EnableTroubleShoot(PHARDWARE_PAGE_DATA hpd
,
661 HWND hBtnTroubleShoot
= GetDlgItem(hpd
->hWnd
,
664 ShowWindow(hBtnTroubleShoot
,
665 Enable
? SW_SHOW
: SW_HIDE
);
671 HardwareDlgProc(IN HWND hwndDlg
,
676 PHARDWARE_PAGE_DATA hpd
;
678 hpd
= (PHARDWARE_PAGE_DATA
)GetWindowLongPtr(hwndDlg
,
681 if (hpd
!= NULL
|| uMsg
== WM_INITDIALOG
)
687 NMHDR
*pnmh
= (NMHDR
*)lParam
;
688 if (pnmh
->hwndFrom
== hpd
->hWndDevList
)
692 case LVN_ITEMCHANGED
:
694 LPNMLISTVIEW pnmv
= (LPNMLISTVIEW
)lParam
;
696 if ((pnmv
->uChanged
& LVIF_STATE
) &&
697 ((pnmv
->uOldState
& (LVIS_FOCUSED
| LVIS_SELECTED
)) ||
698 (pnmv
->uNewState
& (LVIS_FOCUSED
| LVIS_SELECTED
))))
700 UpdateControlStates(hpd
);
711 switch (LOWORD(wParam
))
713 case IDC_TROUBLESHOOT
:
715 /* FIXME - start the help using the command in the window text */
721 DisplaySelectedDeviceProperties(hpd
);
729 HardwareDlgResize(hpd
,
731 (INT
)HIWORD(lParam
));
736 LPCWSTR szWndText
= (LPCWSTR
)lParam
;
737 EnableTroubleShoot(hpd
,
738 (szWndText
!= NULL
&& szWndText
[0] != L
'\0'));
744 hpd
= (PHARDWARE_PAGE_DATA
)lParam
;
750 SetWindowLongPtr(hwndDlg
,
754 hpd
->ClassImageListData
.cbSize
= sizeof(SP_CLASSIMAGELIST_DATA
);
756 SetupDiGetClassImageList(&hpd
->ClassImageListData
);
758 /* calculate the size of the devices list view control */
759 hpd
->hWndDevList
= GetDlgItem(hwndDlg
,
761 if (hpd
->hWndDevList
!= NULL
)
764 GetClientRect(hpd
->hWndDevList
,
766 hpd
->DevListViewHeight
= rcClient
.bottom
;
768 if (hpd
->DisplayMode
== HWPD_LARGELIST
)
770 hpd
->DevListViewHeight
= (hpd
->DevListViewHeight
* 3) / 2;
774 /* subclass the parent window */
775 hWndParent
= GetAncestor(hwndDlg
,
777 if (hWndParent
!= NULL
)
781 if (GetClientRect(hWndParent
,
783 SetWindowPos(hwndDlg
,
791 /* subclass the parent window. This is not safe
792 if the parent window belongs to another thread! */
793 hpd
->ParentOldWndProc
= (WNDPROC
)SetWindowLongPtr(hWndParent
,
795 (LONG_PTR
)ParentSubWndProc
);
797 if (hpd
->ParentOldWndProc
!= NULL
&&
799 L
"DevMgrSubClassInfo",
802 hpd
->hWndParent
= hWndParent
;
807 /* initialize the devices list view control */
808 InitializeDevicesList(hpd
);
810 /* fill the devices list view control */
811 FillDevicesListViewControl(hpd
);
813 /* decide whether to show or hide the troubleshoot button */
814 EnableTroubleShoot(hpd
,
815 GetWindowTextLength(hwndDlg
) != 0);
822 /* free devices list */
823 FreeDevicesList(hpd
);
825 /* restore the old window proc of the subclassed parent window */
826 if (hpd
->hWndParent
!= NULL
&& hpd
->ParentOldWndProc
!= NULL
)
828 SetWindowLongPtr(hpd
->hWndParent
,
830 (LONG_PTR
)hpd
->ParentOldWndProc
);
833 if (hpd
->ClassImageListData
.ImageList
!= NULL
)
835 SetupDiDestroyClassImageList(&hpd
->ClassImageListData
);
838 /* free the reference to comctl32 */
839 FreeLibrary(hpd
->hComCtl32
);
840 hpd
->hComCtl32
= NULL
;
842 /* free the allocated resources */
843 HeapFree(GetProcessHeap(),
855 /***************************************************************************
857 * DeviceCreateHardwarePageEx
860 * Creates a hardware page
863 * hWndParent: Handle to the parent window
864 * lpGuids: An array of guids of devices that are to be listed
865 * uNumberOfGuids: Numbers of guids in the Guids array
866 * DisplayMode: Sets the size of the device list view control
869 * Returns the handle of the hardware page window that has been created or
876 DeviceCreateHardwarePageEx(IN HWND hWndParent
,
878 IN UINT uNumberOfGuids
,
879 IN HWPAGE_DISPLAYMODE DisplayMode
)
881 PHARDWARE_PAGE_DATA hpd
;
883 /* allocate the HARDWARE_PAGE_DATA structure. Make sure it is
884 zeroed because the initialization code assumes that in
886 hpd
= HeapAlloc(GetProcessHeap(),
888 FIELD_OFFSET(HARDWARE_PAGE_DATA
,
890 (uNumberOfGuids
* sizeof(HWCLASSDEVINFO
)));
896 hpd
->DisplayMode
= ((DisplayMode
> HWPD_MAX
) ? HWPD_STANDARDLIST
: DisplayMode
);
898 /* initialize the HARDWARE_PAGE_DATA structure */
899 hpd
->NumberOfGuids
= uNumberOfGuids
;
904 hpd
->ClassDevInfo
[i
].hDevInfo
= INVALID_HANDLE_VALUE
;
905 hpd
->ClassDevInfo
[i
].Guid
= lpGuids
[i
];
908 /* load comctl32.dll dynamically */
909 hpd
->hComCtl32
= LoadAndInitComctl32();
910 if (hpd
->hComCtl32
== NULL
)
915 /* create the dialog */
916 hWnd
= CreateDialogParam(hDllInstance
,
917 MAKEINTRESOURCE(IDD_HARDWARE
),
928 /* oops, something went wrong... */
929 if (hpd
->hComCtl32
!= NULL
)
931 FreeLibrary(hpd
->hComCtl32
);
934 HeapFree(GetProcessHeap(),
944 /***************************************************************************
946 * DeviceCreateHardwarePage
949 * Creates a hardware page
952 * hWndParent: Handle to the parent window
953 * lpGuid: Guid of the device
956 * Returns the handle of the hardware page window that has been created or
963 DeviceCreateHardwarePage(IN HWND hWndParent
,
966 return DeviceCreateHardwarePageEx(hWndParent
,