Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

579 lines
14 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1995
  4. * TITLE: BNDWIDTH.CPP
  5. * VERSION: 1.0
  6. * AUTHOR: jsenior
  7. * DATE: 10/28/1998
  8. *
  9. ********************************************************************************
  10. *
  11. * CHANGE LOG:
  12. *
  13. * DATE REV DESCRIPTION
  14. * ---------- ------- ----------------------------------------------------------
  15. * 10/28/1998 jsenior Original implementation.
  16. *
  17. *******************************************************************************/
  18. #include "resource.h"
  19. #include "itemfind.h"
  20. #include "debug.h"
  21. #include "powrpage.h"
  22. #include "bandpage.h"
  23. #include "usbapp.h"
  24. #include <cpl.h>
  25. #include <dbt.h>
  26. #define WINDOWSCALEFACTOR 15
  27. UINT CALLBACK
  28. UsbApplet::StaticDialogCallback(HWND Hwnd,
  29. UINT Msg,
  30. LPPROPSHEETPAGE Page)
  31. {
  32. /* UsbApplet *that;
  33. switch (Msg) {
  34. case PSPCB_CREATE:
  35. return TRUE; // return TRUE to continue with creation of page
  36. case PSPCB_RELEASE:
  37. that = (UsbPopup*) Page->lParam;
  38. DeleteChunk(that);
  39. delete that;
  40. return 0; // return value ignored
  41. default:
  42. break;
  43. }
  44. */
  45. return TRUE;
  46. }
  47. BOOL
  48. UsbApplet::CustomDialog()
  49. {
  50. InitCommonControls();
  51. if (NULL == (hSplitCursor = LoadCursor(gHInst, MAKEINTRESOURCE(IDC_SPLIT)))) {
  52. return FALSE;
  53. }
  54. if (-1 == DialogBoxParam(gHInst,
  55. MAKEINTRESOURCE(IDD_CPL_USB),
  56. NULL,
  57. StaticDialogProc,
  58. (LPARAM) this)) {
  59. return FALSE;
  60. }
  61. return TRUE;
  62. }
  63. USBINT_PTR APIENTRY UsbApplet::StaticDialogProc(IN HWND hDlg,
  64. IN UINT uMessage,
  65. IN WPARAM wParam,
  66. IN LPARAM lParam)
  67. {
  68. UsbApplet *that;
  69. that = (UsbApplet *) UsbGetWindowLongPtr(hDlg, USBDWLP_USER);
  70. if (!that && uMessage != WM_INITDIALOG)
  71. return FALSE; //DefDlgProc(hDlg, uMessage, wParam, lParam);
  72. switch (uMessage) {
  73. HANDLE_MSG(hDlg, WM_SIZE, that->OnSize);
  74. HANDLE_MSG(hDlg, WM_LBUTTONDOWN, that->OnLButtonDown);
  75. HANDLE_MSG(hDlg, WM_LBUTTONUP, that->OnLButtonUp);
  76. HANDLE_MSG(hDlg, WM_MOUSEMOVE, that->OnMouseMove);
  77. HANDLE_MSG(hDlg, WM_CLOSE, that->OnClose);
  78. HANDLE_MSG(hDlg, WM_NOTIFY, that->OnNotify);
  79. case WM_DEVICECHANGE:
  80. return that->OnDeviceChange(hDlg, (UINT)wParam, (DWORD)wParam);
  81. case WM_COMMAND:
  82. return that->OnCommand(HIWORD(wParam),
  83. LOWORD(wParam),
  84. (HWND) lParam);
  85. case USBWM_NOTIFYREFRESH:
  86. return that->Refresh();
  87. case WM_INITDIALOG:
  88. that = (UsbApplet *) lParam;
  89. UsbSetWindowLongPtr(hDlg, USBDWLP_USER, (USBLONG_PTR) that);
  90. that->hMainWnd = hDlg;
  91. return that->OnInitDialog(hDlg);
  92. default:
  93. break;
  94. }
  95. return that->ActualDialogProc(hDlg, uMessage, wParam, lParam);
  96. }
  97. LRESULT
  98. UsbApplet::OnDeviceChange(HWND hWnd, UINT wParam, DWORD lParam)
  99. {
  100. if ((wParam == DBT_DEVICEARRIVAL) ||
  101. (wParam == DBT_DEVICEREMOVECOMPLETE)) {
  102. Refresh();
  103. }
  104. return TRUE;
  105. }
  106. BOOL
  107. UsbApplet::OnCommand(INT wNotifyCode,
  108. INT wID,
  109. HWND hCtl)
  110. {
  111. /* switch (wID) {
  112. case IDOK:
  113. EndDialog(hWnd, wID);
  114. return TRUE;
  115. }*/
  116. return FALSE;
  117. }
  118. BOOL
  119. UsbApplet::OnInitDialog(HWND HWnd)
  120. {
  121. hMainWnd = HWnd;
  122. RECT rc;
  123. HICON hIcon = LoadIcon(gHInst, MAKEINTRESOURCE(IDI_USB));
  124. if (hIcon) {
  125. SendMessage(HWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
  126. SendMessage(HWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
  127. }
  128. //
  129. // Get a persistent handle to the tree view control
  130. //
  131. if (NULL == (hTreeDevices = GetDlgItem(HWnd, IDC_TREE_USB)) ||
  132. NULL == (hEditControl = GetDlgItem(HWnd, IDC_EDIT1))) {
  133. return FALSE;
  134. }
  135. TreeView_SetImageList(hTreeDevices, ImageList.ImageList(), TVSIL_NORMAL);
  136. GetWindowRect(HWnd, &rc);
  137. barLocation = (rc.right - rc.left) / 3;
  138. ResizeWindows(HWnd, FALSE, 0);
  139. if (!Refresh()) {
  140. return FALSE;
  141. }
  142. //
  143. // Everything seems to be working fine; let's register for device change
  144. // notification
  145. //
  146. return RegisterForDeviceNotification(HWnd);
  147. }
  148. BOOL
  149. UsbApplet::RegisterForDeviceNotification(HWND hWnd)
  150. {
  151. DEV_BROADCAST_DEVICEINTERFACE dbc;
  152. memset(&dbc, 0, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
  153. dbc.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  154. dbc.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  155. dbc.dbcc_classguid = GUID_CLASS_USBHUB;
  156. hDevNotify = RegisterDeviceNotification(hWnd,
  157. &dbc,
  158. DEVICE_NOTIFY_WINDOW_HANDLE);
  159. if (!hDevNotify) {
  160. return FALSE;
  161. }
  162. return TRUE;
  163. }
  164. HTREEITEM
  165. UsbApplet::InsertRoot(LPTV_INSERTSTRUCT item,
  166. UsbItem *firstController)
  167. {
  168. HTREEITEM hItem;
  169. ZeroMemory(item, sizeof(TV_INSERTSTRUCT));
  170. // Get the image index
  171. item->hParent = NULL;
  172. item->hInsertAfter = TVI_LAST;
  173. item->item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE; // TVIF_CHILDREN
  174. // item->itemex.state = TVIS_BOLD;
  175. item->itemex.state |= TVIS_EXPANDED;
  176. item->itemex.stateMask = (UINT)~(TVIS_STATEIMAGEMASK | TVIS_OVERLAYMASK);
  177. item->itemex.pszText = TEXT("My Computer");
  178. item->itemex.cchTextMax = _tcsclen(TEXT("My Computer"));
  179. item->itemex.iImage = 0;
  180. item->itemex.iSelectedImage = 0;
  181. if (firstController) {
  182. item->itemex.cChildren = 1;
  183. }
  184. //
  185. // We will be able to recognize this from usbitems because we assign it the
  186. // lParam value INVALID_HANDLE_VALUE instead of the address of a valid
  187. // usbItem. Cunning, no? Ok, not really...
  188. //
  189. item->itemex.lParam = (LPARAM) INVALID_HANDLE_VALUE;
  190. if (NULL == (hItem = (HTREEITEM)
  191. SendMessage(hTreeDevices,
  192. TVM_INSERTITEM,
  193. 0,
  194. (LPARAM)(LPTV_INSERTSTRUCT)item))) {
  195. int i = GetLastError();
  196. }
  197. return hItem;
  198. }
  199. BOOL
  200. UsbApplet::Refresh()
  201. {
  202. TV_INSERTSTRUCT item;
  203. UsbItem deviceItem;
  204. HTREEITEM hTreeRoot = NULL;
  205. // CWaitCursor wait;
  206. //
  207. // Clear all UI components, and then recreate the rootItem
  208. //
  209. TreeView_DeleteAllItems(hTreeDevices);
  210. if (rootItem) {
  211. DeleteChunk(rootItem);
  212. delete rootItem;
  213. }
  214. rootItem = new UsbItem;
  215. if (!rootItem) {
  216. goto UsbAppletRefreshError;
  217. }
  218. AddChunk(rootItem);
  219. if (!rootItem->EnumerateAll(&ImageList)) {
  220. goto UsbAppletRefreshError;
  221. }
  222. hTreeRoot = InsertRoot(&item, rootItem->child);
  223. if (rootItem->child) {
  224. return UsbItem::InsertTreeItem (hTreeDevices,
  225. rootItem->child,
  226. hTreeRoot,
  227. &item,
  228. IsValid,
  229. IsBold,
  230. IsExpanded);
  231. }
  232. return TRUE;
  233. UsbAppletRefreshError:
  234. return FALSE;
  235. }
  236. VOID
  237. UsbApplet::OnSize (HWND hWnd,
  238. UINT state,
  239. int cx,
  240. int cy)
  241. {
  242. ResizeWindows(hWnd, FALSE, 0);
  243. }
  244. //*****************************************************************************
  245. //
  246. // ResizeWindows()
  247. //
  248. // Handles resizing the two child windows of the main window. If
  249. // bSizeBar is true, then the sizing is happening because the user is
  250. // moving the bar. If bSizeBar is false, the sizing is happening
  251. // because of the WM_SIZE or something like that.
  252. //
  253. //*****************************************************************************
  254. VOID
  255. UsbApplet::ResizeWindows (HWND hWnd,
  256. BOOL bSizeBar,
  257. int BarLocation)
  258. {
  259. RECT MainClientRect;
  260. RECT MainWindowRect;
  261. RECT TreeWindowRect;
  262. // RECT StatusWindowRect;
  263. int right;
  264. // Is the user moving the bar?
  265. //
  266. if (!bSizeBar)
  267. {
  268. BarLocation = barLocation;
  269. }
  270. GetClientRect(hWnd, &MainClientRect);
  271. // GetWindowRect(ghStatusWnd, &StatusWindowRect);
  272. // Make sure the bar is in a OK location
  273. //
  274. if (bSizeBar)
  275. {
  276. if (BarLocation <
  277. GetSystemMetrics(SM_CXSCREEN)/WINDOWSCALEFACTOR)
  278. {
  279. return;
  280. }
  281. if ((MainClientRect.right - BarLocation) <
  282. GetSystemMetrics(SM_CXSCREEN)/WINDOWSCALEFACTOR)
  283. {
  284. return;
  285. }
  286. }
  287. // Save the bar location
  288. //
  289. barLocation = BarLocation;
  290. // Move the tree window
  291. //
  292. MoveWindow(hTreeDevices,
  293. 0,
  294. 0,
  295. BarLocation,
  296. MainClientRect.bottom,// - StatusWindowRect.bottom + StatusWindowRect.top,
  297. TRUE);
  298. // Get the size of the window (in case move window failed
  299. //
  300. GetWindowRect(hTreeDevices, &TreeWindowRect);
  301. GetWindowRect(hWnd, &MainWindowRect);
  302. right = TreeWindowRect.right - MainWindowRect.left;
  303. // Move the edit window with respect to the tree window
  304. //
  305. MoveWindow(hEditControl,
  306. right,
  307. 0,
  308. MainClientRect.right-(right),
  309. MainClientRect.bottom, // - StatusWindowRect.bottom + StatusWindowRect.top,
  310. TRUE);
  311. if (propPage) {
  312. propPage->SizeWindow(right,
  313. 0,
  314. MainClientRect.right-(right),
  315. MainClientRect.bottom);
  316. }
  317. // Move the Status window with respect to the tree window
  318. //
  319. /* MoveWindow(ghStatusWnd,
  320. 0,
  321. MainClientRect.bottom - StatusWindowRect.bottom + StatusWindowRect.top,
  322. MainClientRect.right,
  323. StatusWindowRect.bottom - StatusWindowRect.top,
  324. TRUE);*/
  325. }
  326. VOID
  327. UsbApplet::OnMouseMove (HWND hWnd,
  328. int x,
  329. int y,
  330. UINT keyFlags)
  331. {
  332. SetCursor(hSplitCursor);
  333. if (bButtonDown)
  334. {
  335. ResizeWindows(hMainWnd, TRUE, x);
  336. }
  337. }
  338. VOID
  339. UsbApplet::OnLButtonDown (
  340. HWND hWnd,
  341. BOOL fDoubleClick,
  342. int x,
  343. int y,
  344. UINT keyFlags
  345. )
  346. {
  347. bButtonDown = TRUE;
  348. SetCapture(hMainWnd);
  349. }
  350. VOID
  351. UsbApplet::OnLButtonUp (
  352. HWND hWnd,
  353. int x,
  354. int y,
  355. UINT keyFlags
  356. )
  357. {
  358. bButtonDown = FALSE;
  359. ReleaseCapture();
  360. }
  361. VOID
  362. UsbApplet::OnClose (HWND hWnd)
  363. {
  364. // DestroyTree();
  365. if (hDevNotify) {
  366. UnregisterDeviceNotification(hDevNotify);
  367. hDevNotify = NULL;
  368. }
  369. PostQuitMessage(0);
  370. EndDialog(hMainWnd, 0);
  371. }
  372. LRESULT
  373. UsbApplet::OnNotify (
  374. HWND hWnd,
  375. int DlgItem,
  376. LPNMHDR lpNMHdr
  377. )
  378. {
  379. switch(lpNMHdr->code){
  380. case TVN_SELCHANGED: {
  381. UsbItem *usbItem;
  382. // HTREEITEM hTreeItem;
  383. // hTreeItem = ((NM_TREEVIEW *)lpNMHdr)->itemNew.hItem;
  384. usbItem = (UsbItem*) ((NM_TREEVIEW *)lpNMHdr)->itemNew.lParam;
  385. if (usbItem)
  386. {
  387. UpdateEditControl((UsbItem *) usbItem);
  388. }
  389. SetActiveWindow(hTreeDevices);
  390. }
  391. case LVN_KEYDOWN: {
  392. LPNMLVKEYDOWN pKey = (LPNMLVKEYDOWN) lpNMHdr;
  393. if (VK_F5 == pKey->wVKey) {
  394. return Refresh();
  395. }
  396. }
  397. case TVN_KEYDOWN: {
  398. LPNMTVKEYDOWN pKey = (LPNMTVKEYDOWN) lpNMHdr;
  399. if (VK_F5 == pKey->wVKey) {
  400. return Refresh();
  401. }
  402. }
  403. /* case NM_KEYDOWN: {
  404. LPNMKEY pKey = (LPNMKEY) lpNMHdr;
  405. if (VK_F5 == pKey->nVKey) {
  406. return Refresh();
  407. }
  408. }*/
  409. /* if (DlgItem == IDC_TREE_USB &&
  410. lpNMHdr->code == NM_RCLICK)
  411. {
  412. HMENU hMenu
  413. CreateMenu();
  414. return TRUE;
  415. }
  416. */
  417. }
  418. return 0;
  419. }
  420. VOID
  421. UsbApplet::UpdateEditControl(UsbItem *usbItem)
  422. {
  423. if (propPage) {
  424. if (propPage->DestroyChild()) {
  425. delete propPage;
  426. propPage = NULL;
  427. }
  428. }
  429. if (usbItem == INVALID_HANDLE_VALUE) {
  430. propPage = new RootPage(usbItem);
  431. } else if (usbItem->IsHub()) {
  432. propPage = new PowerPage(usbItem);
  433. } else if (usbItem->IsController()) {
  434. propPage = new BandwidthPage(usbItem);
  435. } else {
  436. propPage = new GenericPage(usbItem);
  437. }
  438. if (propPage) {
  439. propPage->CreateAsChild(hMainWnd, hEditControl, usbItem);
  440. }
  441. }
  442. BOOL
  443. UsbApplet::IsBold(UsbItem *Item)
  444. {
  445. return FALSE;
  446. }
  447. BOOL
  448. UsbApplet::IsValid(UsbItem *Item)
  449. {
  450. return !Item->IsUnusedPort();
  451. }
  452. BOOL
  453. UsbApplet::IsExpanded(UsbItem *Item)
  454. {
  455. if (Item->IsHub() || Item->IsController()) {
  456. return TRUE;
  457. }
  458. return FALSE;
  459. }
  460. extern "C" {
  461. LONG APIENTRY
  462. CPlApplet(HWND hwndCPl,
  463. UINT uMsg,
  464. LPARAM lParam1,
  465. LPARAM lParam2)
  466. {
  467. UsbApplet *applet;
  468. applet = (UsbApplet*) lParam2;
  469. switch (uMsg) {
  470. case CPL_EXIT:
  471. return 0;
  472. case CPL_INQUIRE:
  473. {
  474. CPLINFO *info = (CPLINFO *) lParam2;
  475. assert(lParam1 == 0);
  476. applet = new UsbApplet();
  477. info->idIcon = IDI_USB;
  478. info->idName = IDS_USB;
  479. info->idInfo = IDS_USB;
  480. info->lData = (USBLONG_PTR) applet;
  481. return 0;
  482. }
  483. case CPL_GETCOUNT:
  484. return 1;
  485. case CPL_INIT:
  486. return TRUE;
  487. case CPL_DBLCLK:
  488. assert(lParam1 == 0);
  489. if (applet->CustomDialog()) {
  490. return 0;
  491. }
  492. return 1;
  493. case CPL_STOP:
  494. assert(lParam1 == 0);
  495. applet->OnClose(hwndCPl);
  496. delete applet;
  497. return 0;
  498. }
  499. return 0;
  500. }
  501. }