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.

196 lines
5.0 KiB

  1. #include "StdAfx.h"
  2. #include "uiutils.h"
  3. // UIUtils implementation
  4. /////////////////////////////////////////////////////////////////////////////////////////
  5. /*
  6. Displays a message box just like the default one but get's the text from the resources
  7. */
  8. int UIUtils::MessageBox( HWND hwndParen, UINT nTextID, UINT nTitleID, UINT nType )
  9. {
  10. CString strText;
  11. CString strTitle;
  12. VERIFY( strText.LoadString( nTextID ) );
  13. VERIFY( strTitle.LoadString( nTitleID ) );
  14. return ::MessageBox( hwndParen, strText, strTitle, nType );
  15. }
  16. /*
  17. Loads filter string for GetOpen[Save]FileName API from the resources
  18. The string in the resources contains '|' instead of zero chars. This function will
  19. replace the '|' chars with zero
  20. */
  21. bool UIUtils::LoadOFNFilterFromRes( UINT nResID, CString& rstrFilter )
  22. {
  23. _ASSERT( nResID != 0 );
  24. if ( rstrFilter.LoadString( nResID ) )
  25. {
  26. int nLength = rstrFilter.GetLength();
  27. LPWSTR wszBuffer = rstrFilter.GetBuffer( nLength );
  28. while( *wszBuffer != L'\0' )
  29. {
  30. if ( *wszBuffer == L'|' )
  31. {
  32. *wszBuffer = L'\0';
  33. }
  34. ++wszBuffer;
  35. }
  36. rstrFilter.ReleaseBuffer( nLength );
  37. return true;
  38. }
  39. return false;
  40. }
  41. /*
  42. Compacts a path to fit a control's width. Cimilar to PathSetDlgItemPath but can be used with list boxes
  43. as well. Use nCorrection to change the default width ( for example pass the width of the vert scrollbar )
  44. */
  45. void UIUtils::PathCompatCtrlWidth( HWND hwndCtrl, LPWSTR wszPath, int nCorrection /*=0*/ )
  46. {
  47. _ASSERT( hwndCtrl != NULL );
  48. _ASSERT( wszPath != NULL );
  49. HDC hDC = ::GetDC( hwndCtrl );
  50. RECT rect = { 0 };
  51. HFONT fontOld = NULL;
  52. ::GetClientRect( hwndCtrl, &rect );
  53. // We must select the control font in the DC for the API to properly calc the text width
  54. fontOld = SelectFont( hDC, GetWindowFont( hwndCtrl ) );
  55. // Substract some pixels, as the API formats the text slightly wider then it should be
  56. VERIFY( ::PathCompactPathW( hDC, wszPath, rect.right - rect.left - 6 - nCorrection ) );
  57. SelectFont( hDC, fontOld );
  58. ::ReleaseDC( hwndCtrl, hDC );
  59. }
  60. /*
  61. Similar to the PathCompactCtrlWidth, but for general strings
  62. The string is truncated to fit the control width and "..." as added to the end of it
  63. Use nCorrection to correct the control width for which the calculations will be made
  64. */
  65. void UIUtils::TrimTextToCtrl( HWND hwndCtrl, LPWSTR wszText, int nCorrection /*= 0*/ )
  66. {
  67. _ASSERT( hwndCtrl != NULL );
  68. _ASSERT( wszText != NULL );
  69. HDC hDC = ::GetDC( hwndCtrl );
  70. RECT rect = { 0 };
  71. HFONT fontOld = NULL;
  72. SIZE sizeText = { 0 };
  73. ::GetClientRect( hwndCtrl, &rect );
  74. int nWidth = ( rect.right - rect.left ) - nCorrection;
  75. int nStrLen = ::wcslen( wszText );
  76. fontOld = SelectFont( hDC, GetWindowFont( hwndCtrl ) );
  77. VERIFY( ::GetTextExtentPoint32( hDC, wszText, nStrLen, &sizeText ) );
  78. if ( sizeText.cx > nWidth )
  79. {
  80. // Calc the average width of a symbol and terminate the string
  81. int nPixPerSymb = sizeText.cx / nStrLen;
  82. nStrLen = min( nStrLen, ( nWidth ) / nPixPerSymb );
  83. wszText[ nStrLen - 1 ] = L'\0';
  84. ::wcscat( wszText, L"..." );
  85. nStrLen += 3;
  86. // Adjust the string removing one symbol at a time
  87. do
  88. {
  89. // Make the string one symbol shorter
  90. // Make the last non '.' symbol a '.' symbol and make the whole string one char less long
  91. wszText[ nStrLen - 4 ] = L'.';
  92. wszText[ nStrLen - 1 ] = L'\0';
  93. --nStrLen;
  94. VERIFY( ::GetTextExtentPoint32( hDC, wszText, nStrLen, &sizeText ) );
  95. }while( sizeText.cx > nWidth );
  96. }
  97. SelectFont( hDC, fontOld );
  98. ::ReleaseDC( hwndCtrl, hDC );
  99. }
  100. void UIUtils::ShowCOMError( HWND hwndParent, UINT nTextID, UINT nTitleID, HRESULT hr )
  101. {
  102. _ASSERT( FAILED( hr ) );
  103. CString strText;
  104. CString strTitle;
  105. VERIFY( strTitle.LoadString( nTitleID ) );
  106. // Try to get the string from the system
  107. if ( E_FAIL != E_FAIL )
  108. {
  109. const int MaxErrorBuff = 512;
  110. WCHAR wszText[ MaxErrorBuff ] = L"";
  111. VERIFY( ::FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM,
  112. NULL,
  113. hr,
  114. 0,
  115. wszText,
  116. MaxErrorBuff,
  117. NULL ) != 0 );
  118. strText.Format( nTextID, wszText );
  119. }
  120. else
  121. {
  122. IErrorInfoPtr spErrorInfo;
  123. CComBSTR bstrError;
  124. VERIFY( SUCCEEDED( ::GetErrorInfo( 0, &spErrorInfo ) ) );
  125. VERIFY( SUCCEEDED( spErrorInfo->GetDescription( &bstrError ) ) );
  126. strText.Format( nTextID, bstrError.m_str );
  127. }
  128. ::MessageBox( hwndParent, strText, strTitle, MB_OK | MB_ICONWARNING );
  129. }