Source code of Windows XP (NT5)
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.

1486 lines
35 KiB

  1. /*
  2. ==============================================================================
  3. Application:
  4. Microsoft Windows NT (TM) Performance Monitor
  5. File:
  6. utils.c -- miscellaneous utility routines.
  7. This file contains miscellaneous utiltity routines, mostly
  8. low-level windows helpers. These routines are not specific
  9. to the perfmon utillity.
  10. Copyright 1992, Microsoft Corporation. All Rights Reserved.
  11. Microsoft Confidential.
  12. ==============================================================================
  13. */
  14. //==========================================================================//
  15. // Includes //
  16. //==========================================================================//
  17. #include <stdarg.h> // For ANSI variable args. Dont use UNIX <varargs.h>
  18. #include <stdlib.h> // For itoa
  19. #include <stdio.h> // for vsprintf.
  20. #include <string.h> // for strtok
  21. #include <wchar.h> // for swscanf
  22. #include "perfmon.h"
  23. #include "pmemory.h" // for MemoryXXX (mallloc-type) routines
  24. #include "utils.h"
  25. #include "pmhelpid.h" // IDs for WinHelp
  26. #include "perfmops.h" // for ConvertDecimalPoint() & ReconvertDecimalPoint()
  27. #include "sizes.h"
  28. //==========================================================================//
  29. // Constants //
  30. //==========================================================================//
  31. #define DOS_FILES 0x0000 // Ordinary files
  32. #define DOS_READONLY 0x0001 // Read-only files
  33. #define DOS_HIDDEN 0x0002 // Hidden files
  34. #define DOS_SYSTEM 0x0004 // System files
  35. #define DOS_SUBDIRECTORIES 0x0010 // Subdirectories
  36. #define DOS_ARCHIVES 0x0020 // Archives
  37. #define DOS_LIB_DIR 0x2000 // LB_DIR flag
  38. #define DOS_DRIVES 0x4000 // Drives
  39. #define DOS_EXCLUSIVE 0x8000 // Exclusive bit
  40. #define DOS_DRIVES_DIRECTORIES 0xC010 // Find drives and directories only
  41. #define WILD_ONE '?'
  42. #define WILD_ANY '*'
  43. //==========================================================================//
  44. // Local Functions //
  45. //==========================================================================//
  46. void
  47. ClientRectToScreen (
  48. HWND hWnd,
  49. LPRECT lpRect
  50. )
  51. /*
  52. Effect: Remaps lpRect from client coordinates to screen
  53. coordinates. Analogous to ClientToScreen for rectangles.
  54. Note: To convert a rectangle from the client coordinates of
  55. Wnd1 to the client coordinates of Wnd2, call:
  56. ClientRectToScreen (hWnd1, &rect) ;
  57. ScreenRectToClient (hWnd2, &rect) ;
  58. See Also: ClientToScreen (windows), ScreenRectToClient.
  59. Internals: Since a rectangle is really only two points, let
  60. windows do the work with ClientToScreen.
  61. */
  62. {
  63. POINT pt1, pt2 ;
  64. pt1.x = lpRect->left ;
  65. pt1.y = lpRect->top ;
  66. pt2.x = lpRect->right ;
  67. pt2.y = lpRect->bottom ;
  68. ClientToScreen (hWnd, &pt1) ;
  69. ClientToScreen (hWnd, &pt2) ;
  70. lpRect->left = pt1.x ;
  71. lpRect->top = pt1.y ;
  72. lpRect->right = pt2.x ;
  73. lpRect->bottom = pt2.y ;
  74. }
  75. void
  76. ScreenRectToClient (
  77. HWND hWnd,
  78. LPRECT lpRect
  79. )
  80. /*
  81. Effect: Remaps lpRect from screen coordinates to client
  82. coordinates. Analogous to ScreenToClient for rectangles.
  83. Note: To convert a rectangle from the client coordinates of
  84. Wnd1 to the client coordinates of Wnd2, call:
  85. ClientRectToScreen (hWnd1, &rect) ;
  86. ScreenRectToClient (hWnd2, &rect) ;
  87. See Also: ScreenToClient (windows), ClientRectToScreen.
  88. Internals: Since a rectangle is really only two points, let
  89. windows do the work with ScreenToClient.
  90. */
  91. {
  92. POINT pt1, pt2 ;
  93. pt1.x = lpRect->left ;
  94. pt1.y = lpRect->top ;
  95. pt2.x = lpRect->right ;
  96. pt2.y = lpRect->bottom ;
  97. ScreenToClient (hWnd, &pt1) ;
  98. ScreenToClient (hWnd, &pt2) ;
  99. lpRect->left = pt1.x ;
  100. lpRect->top = pt1.y ;
  101. lpRect->right = pt2.x ;
  102. lpRect->bottom = pt2.y ;
  103. } // ScreenRectToClient
  104. //==========================================================================//
  105. // Exported Functions //
  106. //==========================================================================//
  107. void
  108. Line (
  109. HDC hDC,
  110. HPEN hPen,
  111. int x1,
  112. int y1,
  113. int x2,
  114. int y2
  115. )
  116. {
  117. HPEN hPenPrevious = NULL;
  118. if (hPen)
  119. hPenPrevious = SelectPen (hDC, hPen) ;
  120. MoveToEx (hDC, x1, y1, NULL) ;
  121. LineTo (hDC, x2, y2) ;
  122. if (hPen)
  123. SelectObject (hDC, hPenPrevious) ;
  124. }
  125. #ifdef KEEP_UTIL
  126. void
  127. Fill (
  128. HDC hDC,
  129. DWORD rgbColor,
  130. LPRECT lpRect
  131. )
  132. {
  133. HBRUSH hBrush ;
  134. hBrush = CreateSolidBrush (rgbColor) ;
  135. FillRect (hDC, lpRect, hBrush) ;
  136. DeleteBrush (hBrush) ;
  137. }
  138. void
  139. ThreeDConvex (
  140. HDC hDC,
  141. int x1,
  142. int y1,
  143. int x2,
  144. int y2
  145. )
  146. {
  147. HBRUSH hBrushPrevious ;
  148. POINT aPoints [8] ;
  149. DWORD aCounts [2] ;
  150. HPEN hPenPrevious ;
  151. //����������������������������Ŀ
  152. //� Draw Face �
  153. //������������������������������
  154. hBrushPrevious = SelectBrush (hDC, hBrushFace) ;
  155. PatBlt (hDC,
  156. x1 + ThreeDPad, y1 + ThreeDPad,
  157. x2 - x1 - ThreeDPad, y2 - y1 - ThreeDPad,
  158. PATCOPY) ;
  159. SelectBrush (hDC, hBrushPrevious) ;
  160. //����������������������������Ŀ
  161. //� Draw Highlight �
  162. //������������������������������
  163. if (hPenHighlight)
  164. hPenPrevious = SelectPen (hDC, hPenHighlight) ;
  165. aPoints [0].x = x1 ;
  166. aPoints [0].y = y2 - 1 ; // this works slightly diff. than Line ??
  167. aPoints [1].x = x1 ;
  168. aPoints [1].y = y1 ;
  169. aPoints [2].x = x2 ;
  170. aPoints [2].y = y1 ;
  171. aPoints [3].x = x1 + 1 ;
  172. aPoints [3].y = y2 - 1 ;
  173. aPoints [4].x = x1 + 1 ;
  174. aPoints [4].y = y1 + 1 ;
  175. aPoints [5].x = x2 - 1 ;
  176. aPoints [5].y = y1 + 1 ;
  177. aCounts [0] = 3 ;
  178. aCounts [1] = 3 ;
  179. PolyPolyline (hDC, aPoints, aCounts, 2) ;
  180. if (hPenHighlight)
  181. hPenPrevious = SelectPen (hDC, hPenPrevious) ;
  182. // HLine (hDC, hPenHighlight, x1, x2, y1) ; // outside top line
  183. // HLine (hDC, hPenHighlight, x1 + 1, x2 - 1, y1 + 1) ; // inside top line
  184. // VLine (hDC, hPenHighlight, x1, y1, y2) ; // outside left line
  185. // VLine (hDC, hPenHighlight, x1 + 1, y1 + 1, y2 - 1) ; // inside left line
  186. //����������������������������Ŀ
  187. //� Draw Shadow �
  188. //������������������������������
  189. if (hPenShadow)
  190. hPenPrevious = SelectPen (hDC, hPenShadow) ;
  191. aPoints [0].x = x1 + 1 ;
  192. aPoints [0].y = y2 - 1 ;
  193. aPoints [1].x = x2 ;
  194. aPoints [1].y = y2 - 1 ;
  195. aPoints [2].x = x2 ;
  196. aPoints [2].y = y2 - 2 ;
  197. aPoints [3].x = x1 + 2 ;
  198. aPoints [3].y = y2 - 2 ;
  199. aPoints [4].x = x2 - 1 ;
  200. aPoints [4].y = y1 ;
  201. aPoints [5].x = x2 - 1 ;
  202. aPoints [5].y = y2 - 1;
  203. aPoints [6].x = x2 - 2 ;
  204. aPoints [6].y = y2 - 1 ;
  205. aPoints [7].x = x2 - 2 ;
  206. aPoints [7].y = y1 ;
  207. aCounts [0] = 4 ;
  208. aCounts [1] = 4 ;
  209. PolyPolyline (hDC, aPoints, aCounts, 2) ;
  210. if (hPenShadow)
  211. hPenPrevious = SelectPen (hDC, hPenPrevious) ;
  212. // HLine (hDC, hPenShadow, x1 + 1, x2, y2 - 1) ; // outside bottom line
  213. // HLine (hDC, hPenShadow, x1 + 2, x2, y2 - 2) ; // inside bottom line
  214. // VLine (hDC, hPenShadow, x2 - 1, y1 + 1, y2) ; // outside right line
  215. // VLine (hDC, hPenShadow, x2 - 2, y1 + 2, y2) ; // inside right line
  216. }
  217. void
  218. ThreeDConcave (
  219. HDC hDC,
  220. int x1,
  221. int y1,
  222. int x2,
  223. int y2,
  224. BOOL bFace
  225. )
  226. {
  227. HBRUSH hBrushPrevious ;
  228. POINT aPoints [6] ;
  229. DWORD aCounts [2] ;
  230. HPEN hPenPrevious ;
  231. //����������������������������Ŀ
  232. //� Draw Face �
  233. //������������������������������
  234. if (bFace) {
  235. hBrushPrevious = SelectBrush (hDC, hBrushFace) ;
  236. PatBlt (hDC,
  237. x1 + ThreeDPad, y1 + ThreeDPad,
  238. x2 - x1 - ThreeDPad, y2 - y1 - ThreeDPad,
  239. PATCOPY) ;
  240. SelectBrush (hDC, hBrushPrevious) ;
  241. }
  242. //����������������������������Ŀ
  243. //� Draw Shadow �
  244. //������������������������������
  245. #if 1
  246. if (hPenShadow)
  247. hPenPrevious = SelectPen (hDC, hPenShadow) ;
  248. aPoints [0].x = x1 ;
  249. aPoints [0].y = y2 - 1 ;
  250. aPoints [1].x = x1 ;
  251. aPoints [1].y = y1 ;
  252. aPoints [2].x = x2 ;
  253. aPoints [2].y = y1 ;
  254. aPoints [3].x = x1 + 1 ;
  255. aPoints [3].y = y2 - 1 ;
  256. aPoints [4].x = x1 + 1 ;
  257. aPoints [4].y = y1 + 1 ;
  258. aPoints [5].x = x2 - 1 ;
  259. aPoints [5].y = y1 + 1 ;
  260. aCounts [0] = 3 ;
  261. aCounts [1] = 3 ;
  262. PolyPolyline (hDC, aPoints, aCounts, 2) ;
  263. if (hPenShadow)
  264. hPenPrevious = SelectPen (hDC, hPenPrevious) ;
  265. #else
  266. HLine (hDC, hPenShadow, x1, x2, y1) ; // outside top line
  267. HLine (hDC, hPenShadow, x1 + 1, x2 - 1, y1 + 1) ; // inside top line
  268. VLine (hDC, hPenShadow, x1, y1, y2) ; // outside left line
  269. VLine (hDC, hPenShadow, x1 + 1, y1 + 1, y2 - 1) ; // inside left line
  270. #endif
  271. //����������������������������Ŀ
  272. //� Draw Highlight �
  273. //������������������������������
  274. #if 1
  275. if (hPenHighlight)
  276. hPenPrevious = SelectPen (hDC, hPenHighlight) ;
  277. aPoints [0].x = x1 + 1 ;
  278. aPoints [0].y = y2 - 1 ;
  279. aPoints [1].x = x2 ;
  280. aPoints [1].y = y2 - 1 ;
  281. aPoints [2].x = x2 - 1 ;
  282. aPoints [2].y = y2 - 1 ;
  283. aPoints [3].x = x2 - 1 ;
  284. aPoints [3].y = y1 ;
  285. aCounts [0] = 2 ;
  286. aCounts [1] = 2 ;
  287. PolyPolyline (hDC, aPoints, aCounts, 2) ;
  288. if (hPenHighlight)
  289. hPenPrevious = SelectPen (hDC, hPenPrevious) ;
  290. #else
  291. HLine (hDC, hPenHighlight, x1 + 1, x2, y2 - 1) ; // outside bottom line
  292. VLine (hDC, hPenHighlight, x2 - 1, y1 + 1, y2) ; // outside right line
  293. #endif
  294. }
  295. #endif // KEEP_UTIL
  296. void
  297. ThreeDConvex1 (
  298. HDC hDC,
  299. int x1,
  300. int y1,
  301. int x2,
  302. int y2
  303. )
  304. {
  305. HBRUSH hBrushPrevious ;
  306. POINT aPoints [6] ;
  307. DWORD aCounts [2] ;
  308. HPEN hPenPrevious = NULL;
  309. //����������������������������Ŀ
  310. //� Draw Face �
  311. //������������������������������
  312. #if 1
  313. hBrushPrevious = SelectBrush (hDC, hBrushFace) ;
  314. PatBlt (hDC,
  315. x1 + 1, y1 + 1,
  316. x2 - x1 - 1, y2 - y1 - 1,
  317. PATCOPY) ;
  318. SelectBrush (hDC, hBrushPrevious) ;
  319. //����������������������������Ŀ
  320. //� Draw Highlight �
  321. //������������������������������
  322. if (hPenHighlight)
  323. hPenPrevious = SelectPen (hDC, hPenHighlight) ;
  324. aPoints [0].x = x1 ;
  325. aPoints [0].y = y2 - 1 ;
  326. aPoints [1].x = x1 ;
  327. aPoints [1].y = y1 ;
  328. aPoints [2].x = x2 ;
  329. aPoints [2].y = y1 ;
  330. Polyline (hDC, aPoints, 3) ;
  331. if (hPenHighlight)
  332. hPenPrevious = SelectPen (hDC, hPenPrevious) ;
  333. #else
  334. HLine (hDC, hPenHighlight, x1, x2, y1) ; // outside top line
  335. VLine (hDC, hPenHighlight, x1, y1, y2) ; // outside left line
  336. #endif
  337. //����������������������������Ŀ
  338. //� Draw Shadow �
  339. //������������������������������
  340. #if 1
  341. if (hPenShadow)
  342. hPenPrevious = SelectPen (hDC, hPenShadow) ;
  343. aPoints [0].x = x1 + 1 ;
  344. aPoints [0].y = y2 - 1 ;
  345. aPoints [1].x = x2 ;
  346. aPoints [1].y = y2 - 1 ;
  347. aPoints [2].x = x2 - 1 ;
  348. aPoints [2].y = y2 - 1 ;
  349. aPoints [3].x = x2 - 1 ;
  350. aPoints [3].y = y1 ;
  351. aCounts [0] = 2 ;
  352. aCounts [1] = 2 ;
  353. PolyPolyline (hDC, aPoints, aCounts, 2) ;
  354. if (hPenShadow)
  355. hPenPrevious = SelectPen (hDC, hPenPrevious) ;
  356. #else
  357. HLine (hDC, hPenShadow, x1 + 1, x2, y2 - 1) ; // outside bottom line
  358. VLine (hDC, hPenShadow, x2 - 1, y1 + 1, y2) ; // outside right line
  359. #endif
  360. }
  361. void
  362. ThreeDConcave1 (
  363. HDC hDC,
  364. int x1,
  365. int y1,
  366. int x2,
  367. int y2
  368. )
  369. {
  370. HBRUSH hBrushPrevious ;
  371. POINT aPoints [6] ;
  372. DWORD aCounts [2] ;
  373. HPEN hPenPrevious = NULL;
  374. //����������������������������Ŀ
  375. //� Draw Face �
  376. //������������������������������
  377. hBrushPrevious = SelectBrush (hDC, hBrushFace) ;
  378. PatBlt (hDC,
  379. x1 + 1, y1 + 1,
  380. x2 - x1 - 1, y2 - y1 - 1,
  381. PATCOPY) ;
  382. SelectBrush (hDC, hBrushPrevious) ;
  383. //����������������������������Ŀ
  384. //� Draw Shadow �
  385. //������������������������������
  386. #if 1
  387. if (hPenShadow)
  388. hPenPrevious = SelectPen (hDC, hPenShadow) ;
  389. aPoints [0].x = x1 ;
  390. aPoints [0].y = y2 - 1 ;
  391. aPoints [1].x = x1 ;
  392. aPoints [1].y = y1 ;
  393. aPoints [2].x = x2 ;
  394. aPoints [2].y = y1 ;
  395. Polyline (hDC, aPoints, 3) ;
  396. if (hPenShadow)
  397. hPenPrevious = SelectPen (hDC, hPenPrevious) ;
  398. #else
  399. HLine (hDC, hPenShadow, x1, x2, y1) ; // outside top line
  400. VLine (hDC, hPenShadow, x1, y1, y2) ; // outside left line
  401. #endif
  402. //����������������������������Ŀ
  403. //� Draw Highlight �
  404. //������������������������������
  405. #if 1
  406. if (hPenHighlight)
  407. hPenPrevious = SelectPen (hDC, hPenHighlight) ;
  408. aPoints [0].x = x1 + 1 ;
  409. aPoints [0].y = y2 - 1 ;
  410. aPoints [1].x = x2 ;
  411. aPoints [1].y = y2 - 1 ;
  412. aPoints [2].x = x2 - 1 ;
  413. aPoints [2].y = y2 - 2 ;
  414. aPoints [3].x = x2 - 1 ;
  415. aPoints [3].y = y1 ;
  416. aCounts [0] = 2 ;
  417. aCounts [1] = 2 ;
  418. PolyPolyline (hDC, aPoints, aCounts, 2) ;
  419. if (hPenHighlight)
  420. hPenPrevious = SelectPen (hDC, hPenPrevious) ;
  421. #else
  422. HLine (hDC, hPenHighlight, x1 + 1, x2, y2 - 1) ; // outside bottom line
  423. VLine (hDC, hPenHighlight, x2 - 1, y1 + 1, y2) ; // outside right line
  424. #endif
  425. }
  426. int
  427. TextWidth (
  428. HDC hDC,
  429. LPTSTR lpszText
  430. )
  431. {
  432. SIZE size ;
  433. if (!lpszText)
  434. return (0) ;
  435. GetTextExtentPoint (hDC, lpszText, lstrlen (lpszText), &size) ;
  436. return (size.cx) ;
  437. }
  438. int
  439. _cdecl
  440. DlgErrorBox (
  441. HWND hDlg,
  442. UINT id,
  443. ...
  444. )
  445. {
  446. TCHAR szMessageFmt [FilePathLen + 1] ;
  447. TCHAR szBuffer [FilePathLen * 2] ;
  448. va_list vaList ;
  449. int NumOfChar ;
  450. TCHAR szApplication [WindowCaptionLen] ;
  451. NumOfChar = StringLoad (id, szMessageFmt) ;
  452. if (NumOfChar) {
  453. va_start (vaList, id) ;
  454. TSPRINTF (szBuffer, szMessageFmt, va_arg(vaList, LPTSTR)) ;
  455. va_end (vaList) ;
  456. StringLoad (IDS_APPNAME, szApplication) ;
  457. MessageBox (hDlg, szBuffer, szApplication,
  458. MB_OK | MB_ICONSTOP | MB_TASKMODAL) ;
  459. }
  460. return (0) ;
  461. }
  462. int
  463. FontHeight (
  464. HDC hDC,
  465. BOOL bIncludeLeading
  466. )
  467. {
  468. TEXTMETRIC tm ;
  469. GetTextMetrics (hDC, &tm) ;
  470. if (bIncludeLeading)
  471. return (tm.tmHeight + tm.tmExternalLeading) ;
  472. else
  473. return (tm.tmHeight) ;
  474. }
  475. int
  476. TextAvgWidth (
  477. HDC hDC,
  478. int iNumChars
  479. )
  480. {
  481. TEXTMETRIC tm ;
  482. int xAvgWidth ;
  483. GetTextMetrics (hDC, &tm) ;
  484. xAvgWidth = iNumChars * tm.tmAveCharWidth ;
  485. // add 10% slop
  486. return (MulDiv (xAvgWidth, 11, 10)) ;
  487. }
  488. void
  489. WindowCenter (
  490. HWND hWnd
  491. )
  492. /*
  493. Effect: Center the window hWnd in the center of the screen.
  494. Physically update the windows appearance as well.
  495. Globals: xScreenWidth, yScreenHeight.
  496. */
  497. {
  498. RECT rectWindow ;
  499. int xWindowWidth, yWindowHeight ;
  500. GetWindowRect (hWnd, &rectWindow) ;
  501. xWindowWidth = rectWindow.right - rectWindow.left ;
  502. yWindowHeight = rectWindow.bottom - rectWindow.top ;
  503. MoveWindow (hWnd,
  504. (xScreenWidth - xWindowWidth) / 2,
  505. (yScreenHeight - yWindowHeight) / 2,
  506. xWindowWidth,
  507. yWindowHeight,
  508. TRUE) ;
  509. }
  510. BOOL
  511. DialogMove (
  512. HDLG hDlg,
  513. WORD wControlID,
  514. int xPos,
  515. int yPos,
  516. int xWidth,
  517. int yHeight
  518. )
  519. /*
  520. Effect: Move the control identified by wControlID in the dialog
  521. hDlg to the new position (xPos, yPos), and resize to
  522. (xWidth, yHeight). If any of these values are NOCHANGE, retain
  523. the current value.
  524. Examples: DialogMove (hDlg, IDD_FOO, 10, 20, NOCHANGE, NOCHANGE)
  525. moves control but does not resize it
  526. DialogMove (hDlg, IDD_FOO, NOCHANGE, NOCHANGE, 100, NOCHANGE)
  527. sets width of control to 100
  528. */
  529. {
  530. HWND hWndControl ;
  531. RECT rectControl ;
  532. hWndControl = DialogControl (hDlg, wControlID) ;
  533. if (!hWndControl)
  534. return (FALSE) ;
  535. GetWindowRect (hWndControl, &rectControl) ;
  536. ScreenRectToClient (hDlg, &rectControl) ;
  537. MoveWindow (hWndControl,
  538. (xPos == NOCHANGE) ? rectControl.left : xPos,
  539. (yPos == NOCHANGE) ? rectControl.top : yPos,
  540. (xWidth == NOCHANGE) ? rectControl.right - rectControl.left : xWidth,
  541. (yHeight == NOCHANGE) ? rectControl.bottom - rectControl.top : yHeight,
  542. TRUE) ;
  543. return (TRUE) ;
  544. }
  545. int
  546. DialogWidth (
  547. HDLG hDlg,
  548. WORD wControlID
  549. )
  550. {
  551. HWND hWndControl ;
  552. RECT rectControl ;
  553. hWndControl = DialogControl (hDlg, wControlID) ;
  554. if (!hWndControl)
  555. return (0) ;
  556. GetWindowRect (hWndControl, &rectControl) ;
  557. return (rectControl.right - rectControl.left) ;
  558. }
  559. int
  560. DialogHeight (
  561. HDLG hDlg,
  562. WORD wControlID
  563. )
  564. {
  565. HWND hWndControl ;
  566. RECT rectControl ;
  567. hWndControl = DialogControl (hDlg, wControlID) ;
  568. if (!hWndControl)
  569. return (0) ;
  570. GetWindowRect (hWndControl, &rectControl) ;
  571. return (rectControl.bottom - rectControl.top) ;
  572. }
  573. int
  574. DialogXPos (
  575. HDLG hDlg,
  576. WORD wControlID
  577. )
  578. {
  579. HWND hWndControl ;
  580. RECT rectControl ;
  581. hWndControl = DialogControl (hDlg, wControlID) ;
  582. if (!hWndControl)
  583. return (0) ;
  584. GetWindowRect (hWndControl, &rectControl) ;
  585. ScreenRectToClient (hDlg, &rectControl) ;
  586. return (rectControl.left) ;
  587. }
  588. int
  589. DialogYPos (
  590. HDLG hDlg,
  591. WORD wControlID
  592. )
  593. {
  594. HWND hWndControl ;
  595. RECT rectControl ;
  596. hWndControl = DialogControl (hDlg, wControlID) ;
  597. if (!hWndControl)
  598. return (0) ;
  599. GetWindowRect (hWndControl, &rectControl) ;
  600. ScreenRectToClient (hDlg, &rectControl) ;
  601. return (rectControl.top) ;
  602. }
  603. void
  604. DialogEnable (
  605. HDLG hDlg,
  606. WORD wID,
  607. BOOL bEnable
  608. )
  609. /*
  610. Effect: Enable or disable (based on bEnable) the control
  611. identified by wID in dialog hDlg.
  612. See Also: DialogShow.
  613. */
  614. {
  615. HCONTROL hControl ;
  616. hControl = GetDlgItem (hDlg, wID) ;
  617. if (hControl)
  618. EnableWindow (hControl, bEnable) ;
  619. }
  620. void
  621. DialogShow (
  622. HDLG hDlg,
  623. WORD wID,
  624. BOOL bShow
  625. )
  626. {
  627. HCONTROL hControl ;
  628. hControl = GetDlgItem (hDlg, wID) ;
  629. if (hControl)
  630. ShowWindow (hControl, bShow ? SW_SHOW : SW_HIDE) ;
  631. }
  632. BOOL
  633. _cdecl
  634. DialogSetText (
  635. HDLG hDlg,
  636. WORD wControlID,
  637. WORD wStringID,
  638. ...
  639. )
  640. {
  641. TCHAR szFormat [ControlStringLen] ;
  642. TCHAR szText [ControlStringLen] ;
  643. va_list vaList ;
  644. if (LoadString (hInstance, wStringID,
  645. szFormat, ControlStringLen - 1)) {
  646. va_start (vaList, wStringID) ;
  647. TSPRINTF (szText, szFormat, va_arg(vaList, LPTSTR)) ;
  648. va_end (vaList) ;
  649. SetDlgItemText (hDlg, wControlID, szText) ;
  650. return (TRUE) ;
  651. } else {
  652. return (FALSE) ;
  653. }
  654. }
  655. // Bug - lpszFormat could contain % in it
  656. #if 0
  657. BOOL
  658. _cdecl
  659. DialogSetString (
  660. HDLG hDlg,
  661. WORD wControlID,
  662. LPTSTR lpszFormat,
  663. ...
  664. )
  665. {
  666. TCHAR szText [ControlStringLen] ;
  667. va_list vaList ;
  668. va_start (vaList, lpszFormat) ;
  669. TSPRINTF (szText, lpszFormat, va_arg(vaList, LPTSTR)) ;
  670. va_end (vaList) ;
  671. SetDlgItemText (hDlg, wControlID, szText) ;
  672. return (TRUE) ;
  673. }
  674. #endif
  675. LPTSTR
  676. LongToCommaString (
  677. LONG lNumber,
  678. LPTSTR lpszText
  679. )
  680. {
  681. BOOL bNegative ;
  682. TCHAR szTemp1 [40] ;
  683. TCHAR szTemp2 [40] ;
  684. LPTSTR lpsz1 ;
  685. LPTSTR lpsz2 ;
  686. int i ;
  687. int iDigit ;
  688. // 1. Convert the number to a reversed string.
  689. lpsz1 = szTemp1 ;
  690. bNegative = (lNumber < 0) ;
  691. lNumber = labs (lNumber) ;
  692. if (lNumber)
  693. while (lNumber) {
  694. iDigit = (int) (lNumber % 10L) ;
  695. lNumber /= 10L ;
  696. *lpsz1++ = (TCHAR) (TEXT('0') + iDigit) ;
  697. } else
  698. *lpsz1++ = TEXT('0') ;
  699. *lpsz1++ = TEXT('\0') ;
  700. // 2. reverse the string and add commas
  701. lpsz1 = szTemp1 + lstrlen (szTemp1) - 1 ;
  702. lpsz2 = szTemp2 ;
  703. if (bNegative)
  704. *lpsz2++ = TEXT('-') ;
  705. for (i = lstrlen (szTemp1) - 1; i >= 0; i--) {
  706. *lpsz2++ = *lpsz1-- ;
  707. if (i && !(i % 3))
  708. *lpsz2++ = TEXT(',') ;
  709. }
  710. *lpsz2++ = TEXT('\0') ;
  711. return (lstrcpy (lpszText, szTemp2)) ;
  712. }
  713. BOOL
  714. MenuSetPopup (
  715. HWND hWnd,
  716. int iPosition,
  717. WORD wControlID,
  718. LPTSTR lpszResourceID
  719. )
  720. {
  721. HMENU hMenuMain ;
  722. HMENU hMenuPopup ;
  723. TCHAR szTopChoice [MenuStringLen + 1] ;
  724. hMenuMain = GetMenu (hWnd) ;
  725. if (!hMenuMain)
  726. return (FALSE);
  727. hMenuPopup = LoadMenu (hInstance, lpszResourceID) ;
  728. if (!hMenuPopup) {
  729. DestroyMenu(hMenuMain);
  730. return (FALSE) ;
  731. }
  732. StringLoad (wControlID, szTopChoice) ;
  733. return (ModifyMenu (hMenuMain, iPosition, MF_BYPOSITION | MF_POPUP,
  734. (UINT_PTR) hMenuPopup, szTopChoice)) ;
  735. }
  736. LPTSTR
  737. FileCombine (
  738. LPTSTR lpszFileSpec,
  739. LPTSTR lpszFileDirectory,
  740. LPTSTR lpszFileName
  741. )
  742. {
  743. int stringLen ;
  744. TCHAR DIRECTORY_DELIMITER[2] ;
  745. DIRECTORY_DELIMITER[0] = TEXT('\\') ;
  746. DIRECTORY_DELIMITER[1] = TEXT('\0') ;
  747. lstrcpy (lpszFileSpec, lpszFileDirectory) ;
  748. stringLen = lstrlen (lpszFileSpec) ;
  749. if (stringLen > 0 &&
  750. lpszFileSpec [stringLen - 1] != DIRECTORY_DELIMITER [0])
  751. lstrcat (lpszFileSpec, DIRECTORY_DELIMITER) ;
  752. lstrcat (lpszFileSpec, lpszFileName) ;
  753. return (lpszFileSpec) ;
  754. }
  755. // This routine extract the filename portion from a given full-path filename
  756. LPTSTR
  757. ExtractFileName (
  758. LPTSTR pFileSpec
  759. )
  760. {
  761. LPTSTR pFileName = NULL ;
  762. TCHAR DIRECTORY_DELIMITER1 = TEXT('\\') ;
  763. TCHAR DIRECTORY_DELIMITER2 = TEXT(':') ;
  764. if (pFileSpec) {
  765. pFileName = pFileSpec + lstrlen (pFileSpec) ;
  766. while (*pFileName != DIRECTORY_DELIMITER1 &&
  767. *pFileName != DIRECTORY_DELIMITER2) {
  768. if (pFileName == pFileSpec) {
  769. // done when no directory delimiter is found
  770. break ;
  771. }
  772. pFileName-- ;
  773. }
  774. if (*pFileName == DIRECTORY_DELIMITER1 ||
  775. *pFileName == DIRECTORY_DELIMITER2) {
  776. // directory delimiter found, point the
  777. // filename right after it
  778. pFileName++ ;
  779. }
  780. }
  781. return pFileName ;
  782. }
  783. int
  784. CBAddInt (
  785. HWND hWndCB,
  786. int iValue
  787. )
  788. {
  789. TCHAR szValue [ShortTextLen + 1] ;
  790. CHAR szCharValue [ShortTextLen + 1] ;
  791. _itoa (iValue, (LPSTR)szCharValue, 10) ;
  792. #ifdef UNICODE
  793. mbstowcs (szValue, (LPSTR)szCharValue, strlen((LPSTR)szCharValue)+1) ;
  794. return (CBAdd (hWndCB, szValue)) ;
  795. #else
  796. return (CBAdd (hWndCB, szCharValue)) ;
  797. #endif
  798. }
  799. void
  800. DialogSetInterval (
  801. HDLG hDlg,
  802. WORD wControlID,
  803. int IntervalMSec
  804. )
  805. {
  806. TCHAR szValue [MiscTextLen] ;
  807. TSPRINTF (szValue, TEXT("%3.3f"),
  808. (FLOAT)(IntervalMSec / 1000)) ;
  809. ConvertDecimalPoint (szValue) ;
  810. SetDlgItemText (hDlg, wControlID, szValue) ;
  811. }
  812. void
  813. DialogSetFloat (
  814. HDLG hDlg,
  815. WORD wControlID,
  816. FLOAT eValue
  817. )
  818. {
  819. TCHAR szValue [40] ;
  820. FLOAT tempValue = eValue ;
  821. if (tempValue < (FLOAT) 0.0) {
  822. tempValue = - tempValue ;
  823. }
  824. if (tempValue < (FLOAT) 1.0E+8) {
  825. TSPRINTF (szValue, TEXT("%1.4f"), eValue) ;
  826. } else {
  827. TSPRINTF (szValue, TEXT("%14.6e"), eValue) ;
  828. }
  829. ConvertDecimalPoint (szValue) ;
  830. SetDlgItemText (hDlg, wControlID, szValue) ;
  831. }
  832. FLOAT
  833. DialogFloat (
  834. HDLG hDlg,
  835. WORD wControlID,
  836. BOOL *pbOK
  837. )
  838. /*
  839. Effect: Return a floating point representation of the string
  840. value found in the control wControlID of hDlg.
  841. Internals: We use sscanf instead of atof becuase atof returns a
  842. double. This may or may not be the right thing to do.
  843. */
  844. {
  845. TCHAR szValue [ShortTextLen+1] ;
  846. FLOAT eValue ;
  847. int iNumScanned ;
  848. DialogText (hDlg, wControlID, szValue) ;
  849. ReconvertDecimalPoint (szValue) ;
  850. iNumScanned = swscanf (szValue, TEXT("%e"), &eValue) ;
  851. if (pbOK)
  852. *pbOK = (iNumScanned == 1) ;
  853. return (eValue) ;
  854. }
  855. LPTSTR
  856. StringAllocate (
  857. LPTSTR lpszText1
  858. )
  859. {
  860. LPTSTR lpszText2 = NULL;
  861. DWORD dwStrLen;
  862. if (lpszText1 != NULL) {
  863. if (*lpszText1 != 0) {
  864. dwStrLen = (lstrlen (lpszText1)+1) * sizeof (TCHAR);
  865. lpszText2 = MemoryAllocate (dwStrLen) ;
  866. if (lpszText2 != NULL) {
  867. lstrcpy (lpszText2, lpszText1) ;
  868. }
  869. }
  870. }
  871. return (lpszText2) ;
  872. }
  873. int
  874. DivRound (
  875. int iNumerator,
  876. int iDenominator
  877. )
  878. /*
  879. Effect: Return the quotient (iNumerator / iDenominator).
  880. Round the quotient to the nearest integer.
  881. This function is similar to normal integer division (/),
  882. but normal division always rounds down.
  883. Note: Surely there must already be a runtime version of this,
  884. but I couldn't find it.
  885. Note: This function originally used the runtime div function
  886. instead of (/ and %), but the div runtime function is
  887. now broken (build 265).
  888. */
  889. {
  890. int iQuotient ;
  891. int iRemainder ;
  892. iQuotient = iNumerator / iDenominator ;
  893. iRemainder = iNumerator % iDenominator ;
  894. if (iRemainder >= (iDenominator / 2))
  895. iQuotient++ ;
  896. return (iQuotient) ;
  897. }
  898. BOOL
  899. MenuEnableItem (
  900. HMENU hMenu,
  901. WORD wID,
  902. BOOL bEnable
  903. )
  904. /*
  905. Effect: Enable or disable, depending on bEnable, the menu item
  906. associated with id wID in the menu hMenu.
  907. Any disabled menu items are displayed grayed out.
  908. See Also: EnableMenuItem (windows).
  909. */
  910. {
  911. return (EnableMenuItem (hMenu, wID,
  912. bEnable ?
  913. (MF_ENABLED | MF_BYCOMMAND) :
  914. (MF_GRAYED | MF_BYCOMMAND))) ;
  915. }
  916. void
  917. BitmapDimemsion (
  918. HBITMAP hBitmap,
  919. int *pHeight,
  920. int *pWidth
  921. )
  922. {
  923. BITMAP bm ;
  924. GetObject (hBitmap, sizeof (BITMAP), (LPSTR) &bm) ;
  925. if (pHeight)
  926. *pHeight = bm.bmHeight ;
  927. if (pWidth)
  928. *pWidth = bm.bmWidth ;
  929. }
  930. int
  931. WindowHeight (
  932. HWND hWnd
  933. )
  934. {
  935. RECT rectWindow ;
  936. GetWindowRect (hWnd, &rectWindow) ;
  937. return (rectWindow.bottom - rectWindow.top) ;
  938. }
  939. #ifdef KEEP_UTIL
  940. int
  941. WindowWidth (
  942. HWND hWnd
  943. )
  944. {
  945. RECT rectWindow ;
  946. GetWindowRect (hWnd, &rectWindow) ;
  947. return (rectWindow.right - rectWindow.left) ;
  948. }
  949. void
  950. WindowResize (
  951. HWND hWnd,
  952. int xWidth,
  953. int yHeight
  954. )
  955. /*
  956. Effect: Change the size of the window hWnd, leaving the
  957. starting position intact. Redraw the window.
  958. If either xWidth or yHeight is NULL, keep the
  959. corresponding dimension unchanged.
  960. Internals: Since hWnd may be a child of another parent, we need
  961. to scale the MoveWindow arguments to be in the client
  962. coordinates of the parent.
  963. */
  964. {
  965. RECT rectWindow ;
  966. HWND hWndParent ;
  967. GetWindowRect (hWnd, &rectWindow) ;
  968. hWndParent = WindowParent (hWnd) ;
  969. if (hWndParent)
  970. ScreenRectToClient (hWndParent, &rectWindow) ;
  971. MoveWindow (hWnd,
  972. rectWindow.left,
  973. rectWindow.top,
  974. xWidth ? xWidth : rectWindow.right - rectWindow.left,
  975. yHeight ? yHeight : rectWindow.bottom - rectWindow.top,
  976. TRUE) ;
  977. }
  978. #endif
  979. void
  980. WindowSetTopmost (
  981. HWND hWnd,
  982. BOOL bTopmost
  983. )
  984. /*
  985. Effect: Set or clear the "topmost" attribute of hWnd. If a window
  986. is "topmost", it remains ontop of other windows, even ones
  987. that have the focus.
  988. */
  989. {
  990. SetWindowPos (hWnd, bTopmost ? HWND_TOPMOST : HWND_NOTOPMOST,
  991. 0, 0, 0, 0,
  992. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE) ;
  993. }
  994. void
  995. WindowEnableTitle (
  996. HWND hWnd,
  997. BOOL bTitle
  998. )
  999. {
  1000. DWORD dwStyle ;
  1001. dwStyle = WindowStyle (hWnd) ;
  1002. if (bTitle)
  1003. dwStyle = WS_TILEDWINDOW | dwStyle ;
  1004. else
  1005. dwStyle =
  1006. dwStyle &
  1007. ~ (WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX) ;
  1008. if (!bTitle)
  1009. SetMenu (hWnd, NULL) ;
  1010. WindowSetStyle (hWnd, dwStyle) ;
  1011. SetWindowPos (hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE |
  1012. SWP_NOZORDER | SWP_FRAMECHANGED );
  1013. }
  1014. #ifdef PERFMON_DEBUG
  1015. #define MikeBufferSize 256
  1016. int
  1017. _cdecl
  1018. mike (
  1019. TCHAR *szFormat,
  1020. ...
  1021. )
  1022. /*
  1023. Note: This function returns a value so that it can more easily
  1024. be used in conditional expressions.
  1025. */
  1026. {
  1027. TCHAR szBuffer [MikeBufferSize] ;
  1028. va_list vaList ;
  1029. va_start (vaList, szFormat) ;
  1030. TSPRINTF (szBuffer, szFormat, va_arg(vaList, LPTSTR)) ;
  1031. va_end (vaList) ;
  1032. MessageBox (NULL, szBuffer, TEXT("Debug"), MB_OK | MB_TASKMODAL) ;
  1033. return (0) ;
  1034. }
  1035. int
  1036. _cdecl
  1037. mike1 (
  1038. TCHAR *szFormat,
  1039. ...
  1040. )
  1041. /*
  1042. Note: This function returns a value so that it can more easily
  1043. be used in conditional expressions.
  1044. */
  1045. {
  1046. TCHAR szBuffer [MikeBufferSize] ;
  1047. va_list vaList ;
  1048. HDC hDC ;
  1049. RECT rect ;
  1050. va_start (vaList, szFormat) ;
  1051. TSPRINTF (szBuffer, szFormat, va_arg(vaList, LPTSTR)) ;
  1052. va_end (vaList) ;
  1053. rect.left = 0 ;
  1054. rect.right = xScreenWidth ;
  1055. rect.top = 0 ;
  1056. rect.bottom = 20 ;
  1057. hDC = CreateScreenDC () ;
  1058. ExtTextOut (hDC, 0, 0, ETO_OPAQUE, &rect,
  1059. szBuffer, lstrlen (szBuffer), NULL) ;
  1060. DeleteDC (hDC) ;
  1061. return (0) ;
  1062. }
  1063. int
  1064. _cdecl
  1065. mike2 (
  1066. TCHAR *szFormat,
  1067. ...
  1068. )
  1069. /*
  1070. Note: This function returns a value so that it can more easily
  1071. be used in conditional expressions.
  1072. */
  1073. {
  1074. TCHAR szBuffer [MikeBufferSize] ;
  1075. va_list vaList ;
  1076. va_start (vaList, szFormat) ;
  1077. TSPRINTF (szBuffer, szFormat, va_arg(vaList, LPTSTR)) ;
  1078. va_end (vaList) ;
  1079. OutputDebugString (szBuffer) ;
  1080. return (0) ;
  1081. }
  1082. #endif // PERFMON_DEBUG
  1083. #ifdef KEEP_UTIL
  1084. int
  1085. inttok (
  1086. LPSTR lpszText,
  1087. LPSTR lpszDelimiters
  1088. )
  1089. {
  1090. // Inttok only works with LPSTRs because of the atoi & strtok
  1091. LPSTR lpszToken ;
  1092. lpszToken = strtok (lpszText, lpszDelimiters) ;
  1093. if (lpszToken)
  1094. return (atoi (lpszToken)) ;
  1095. else
  1096. return (0) ;
  1097. }
  1098. void
  1099. WindowPlacementToString (
  1100. PWINDOWPLACEMENT pWP,
  1101. LPTSTR lpszText
  1102. )
  1103. {
  1104. TSPRINTF (lpszText, TEXT("%d %d %d %d %d %d %d %d %d"),
  1105. pWP->showCmd,
  1106. pWP->ptMinPosition.x,
  1107. pWP->ptMinPosition.y,
  1108. pWP->ptMaxPosition.x,
  1109. pWP->ptMaxPosition.y,
  1110. pWP->rcNormalPosition.left,
  1111. pWP->rcNormalPosition.top,
  1112. pWP->rcNormalPosition.right,
  1113. pWP->rcNormalPosition.bottom) ;
  1114. }
  1115. void
  1116. StringToWindowPlacement (
  1117. LPTSTR lpszText,
  1118. PWINDOWPLACEMENT pWP
  1119. )
  1120. {
  1121. int iNumScanned ;
  1122. iNumScanned = swscanf (lpszText, TEXT("%d %d %d %d %d %d %d %d %d"),
  1123. &pWP->showCmd,
  1124. &pWP->ptMinPosition.x,
  1125. &pWP->ptMinPosition.y,
  1126. &pWP->ptMaxPosition.x,
  1127. &pWP->ptMaxPosition.y,
  1128. &pWP->rcNormalPosition.left,
  1129. &pWP->rcNormalPosition.top,
  1130. &pWP->rcNormalPosition.right,
  1131. &pWP->rcNormalPosition.bottom) ;
  1132. }
  1133. #endif // KEEP_UTIL
  1134. int
  1135. LogFontHeight (
  1136. HDC hDC,
  1137. int iPointSize
  1138. )
  1139. /*
  1140. Effect: Return the appropriate pixel height for the lfHeight
  1141. field of the LOGFONT structure for the requested point
  1142. size. This size depends on the number of pixels per
  1143. logical inch of the current display context, hDC.
  1144. Called By: Any function which wants to create a particular
  1145. point-height font.
  1146. */
  1147. {
  1148. return (-MulDiv (iPointSize, GetDeviceCaps (hDC, LOGPIXELSY), 72)) ;
  1149. }
  1150. // this routine converts the input menu id into help id.
  1151. DWORD
  1152. MenuIDToHelpID (
  1153. DWORD MenuID
  1154. )
  1155. {
  1156. DWORD HelpID = 0 ;
  1157. if (MenuID >= IDM_FIRSTMENUID && MenuID <= IDM_LASTMENUID) {
  1158. // only special cases...
  1159. if (MenuID >= IDM_OPTIONSREFRESHNOWCHART &&
  1160. MenuID <= IDM_OPTIONSREFRESHNOWREPORT) {
  1161. HelpID = HC_PM_MENU_OPTIONSREFRESHNOW ;
  1162. } else {
  1163. HelpID = MenuID - MENUIDTOHELPID ;
  1164. }
  1165. #ifndef ADVANCED_PERFMON
  1166. // need to convert these 2 IDs for Perf. Meter
  1167. if (HelpID == HC_PM_MENU_HELPABOUT) {
  1168. HelpID = HC_NTPM_MENU_HELPABOUT ;
  1169. } else if (HelpID == HC_PM_MENU_FILEEXIT) {
  1170. HelpID = HC_NTPM_MENU_FILEEXIT ;
  1171. }
  1172. #endif
  1173. }
  1174. return (HelpID) ;
  1175. }