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.

274 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. Util.c
  5. Abstract:
  6. This module contains miscellaneous utility functions.
  7. Author:
  8. David J. Gilman (davegi) 11-Sep-1992
  9. Environment:
  10. User Mode
  11. --*/
  12. #include <string.h>
  13. #include "wintools.h"
  14. LPCWSTR
  15. BaseNameW(
  16. IN LPCWSTR Name
  17. )
  18. /*++
  19. Routine Description:
  20. Returns the base portion of a file name if it exists.
  21. Arguments:
  22. Name - Supplies the name where the base name should be extracted
  23. from.
  24. Return Value:
  25. LPCWSTR - Returns the base name.
  26. --*/
  27. {
  28. LPWSTR String;
  29. DbgPointerAssert( Name );
  30. //
  31. // If the name exists, look for the last '\' character and return the
  32. // remaining part of name.
  33. //
  34. if( Name ) {
  35. String = wcsrchr( Name, L'\\' );
  36. return( String ) ? String + 1 : Name;
  37. }
  38. //
  39. // No name, return NULL.
  40. //
  41. return NULL;
  42. }
  43. BOOL
  44. GetCharMetricsW(
  45. IN HDC hDC,
  46. IN LPLONG CharWidth,
  47. IN LPLONG CharHeight
  48. )
  49. /*++
  50. Routine Description:
  51. Return the width and height of a character.
  52. Arguments:
  53. hDC - Supplies a handle to the DC where the characters are to be
  54. displayed.
  55. CharWidth - Supplies a pointer where the character width is returned.
  56. CharHeight - Supplies a pointer where the character height is returned.
  57. Return Value:
  58. BOOL - Returns TRUE if the character height and width are returned.
  59. --*/
  60. {
  61. BOOL Success;
  62. TEXTMETRICW TextMetric;
  63. DbgHandleAssert( hDC );
  64. DbgPointerAssert( CharWidth );
  65. DbgPointerAssert( CharHeight );
  66. //
  67. // Attempt to retrieve the text metrics for the supplied DC.
  68. //
  69. Success = GetTextMetricsW( hDC, &TextMetric );
  70. DbgAssert( Success );
  71. if( Success ) {
  72. //
  73. // Compute the character width and height.
  74. //
  75. *CharWidth = TextMetric.tmAveCharWidth;
  76. *CharHeight = TextMetric.tmHeight
  77. + TextMetric.tmExternalLeading;
  78. }
  79. return Success;
  80. }
  81. BOOL
  82. GetClientSize(
  83. IN HWND hWnd,
  84. IN LPLONG ClientWidth,
  85. IN LPLONG ClientHeight
  86. )
  87. /*++
  88. Routine Description:
  89. Return the width and height of the client area of a window.
  90. Arguments:
  91. hWnd - Supplies ahdnle to the window whose cient area size is of
  92. interest
  93. ClientWidth - Supplies a pointer where the window's client area width
  94. is returned.
  95. ClientHeight - Supplies a pointer where the window's client area height
  96. is returned.
  97. Return Value:
  98. BOOL - Returns TRUE if the character height and width are
  99. returned.
  100. --*/
  101. {
  102. RECT Rect;
  103. BOOL Success;
  104. DbgHandleAssert( hWnd );
  105. DbgPointerAssert( ClientWidth );
  106. DbgPointerAssert( ClientHeight );
  107. //
  108. // Attempt to retrieve the clieant area size for the supplied window.
  109. //
  110. Success = GetClientRect( hWnd, &Rect );
  111. DbgAssert( Success == TRUE );
  112. if( Success == TRUE ) {
  113. //
  114. // Return the client area width and height.
  115. //
  116. *ClientWidth = Rect.right;
  117. *ClientHeight = Rect.bottom;
  118. }
  119. return Success;
  120. }
  121. BOOL
  122. SetScrollPosEx(
  123. IN HWND hWnd,
  124. IN INT fnBar,
  125. IN INT nPos,
  126. IN BOOL fRedraw,
  127. OUT PINT pnOldPos
  128. )
  129. /*++
  130. Routine Description:
  131. An extended version of the SetScrollPos API, that tests of the supplied
  132. window has scroll bars before positioning them and accounts for an
  133. ambiguity in the return value.
  134. Arguments:
  135. hWnd - See the SetScrollPos API.
  136. fnBar - See the SetScrollPos API.
  137. nPos - See the SetScrollPos API.
  138. fRedraw - See the SetScrollPos API.
  139. pnOldPos - Supplies an optional pointer which if present will be set to
  140. the old scroll bar position.
  141. Return Value:
  142. BOOL - Returns TRUE if the scroll bar was successfully scrolled.
  143. --*/
  144. {
  145. INT OldPos;
  146. LONG Style;
  147. //
  148. // Get the window style to see if it has scroll bars to position.
  149. //
  150. Style = GetWindowLong( hWnd, GWL_STYLE );
  151. DbgAssert( Style != 0 );
  152. if( Style == 0 ) {
  153. return FALSE;
  154. }
  155. //
  156. // If the scroll bat being position is a control or if the supplied
  157. // windiw has scroll bars...
  158. //
  159. if( ( fnBar == SB_CTL )
  160. || (( fnBar == SB_HORZ ) && ( Style & WS_HSCROLL ))
  161. || (( fnBar == SB_VERT ) && ( Style & WS_VSCROLL ))) {
  162. //
  163. // Position the scroll bar.
  164. //
  165. OldPos = SetScrollPos(
  166. hWnd,
  167. fnBar,
  168. nPos,
  169. fRedraw
  170. );
  171. //
  172. // SetScrollPos has an ambiguity in that it returns 0 for error which
  173. // means it can't be distinguished from an old position of 0. Therefore
  174. // special case the scenario of changing positions from 0->0 or 0->1.
  175. //
  176. if(( OldPos == 0 ) && (( nPos == 0 ) || ( nPos == 1 ))) {
  177. return TRUE;
  178. }
  179. DbgAssert( OldPos != 0 );
  180. if( OldPos == 0 ) {
  181. return FALSE;
  182. }
  183. //
  184. // If requested return the old position.
  185. //
  186. if( ARGUMENT_PRESENT( pnOldPos )) {
  187. *pnOldPos = OldPos;
  188. }
  189. }
  190. return TRUE;
  191. }