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.

192 lines
5.0 KiB

  1. /****************************************************************************\
  2. *
  3. * hint.cpp
  4. *
  5. * Created: William Taylor (wtaylor) 01/22/01
  6. *
  7. * MS Ratings Hint Handling Class
  8. *
  9. \****************************************************************************/
  10. #include "msrating.h"
  11. #include "mslubase.h"
  12. #include "hint.h" // CHint
  13. #include "basedlg.h" // CCommonDialogRoutines
  14. #include "debug.h" // TraceMsg()
  15. const int cchHintLength_c = 127;
  16. const TCHAR tchAmpersand_c[] = "&";
  17. const TCHAR tchAmpersandReplacement_c[] = "&&";
  18. // Default CHint Constructor
  19. CHint::CHint()
  20. {
  21. CHint( NULL, 0 );
  22. }
  23. // CHint Constructor with Dialog Window Handle and Hint Control Id.
  24. CHint::CHint( HWND p_hWnd, int p_iId )
  25. {
  26. m_hWnd = p_hWnd;
  27. m_iId = p_iId;
  28. }
  29. // Display the Hint Text on the Dialog Control.
  30. void CHint::DisplayHint( void )
  31. {
  32. HWND hwndControl = ::GetDlgItem( m_hWnd, m_iId );
  33. if ( hwndControl != NULL )
  34. {
  35. CString strHint;
  36. RetrieveHint( strHint );
  37. // Avoid display of "_" (accelerator) by replacing single "&" with "&&".
  38. strHint.Replace( tchAmpersand_c, tchAmpersandReplacement_c );
  39. ::SetWindowText( hwndControl, strHint );
  40. }
  41. }
  42. // Initialize the Hint Dialog Control by limiting the Number of Hint Characters.
  43. void CHint::InitHint( void )
  44. {
  45. HWND hwndControl = ::GetDlgItem( m_hWnd, m_iId );
  46. if ( hwndControl != NULL )
  47. {
  48. ::SendMessage( hwndControl, EM_SETLIMITTEXT, (WPARAM) cchHintLength_c, (LPARAM) 0);
  49. }
  50. }
  51. // Check for a Blank Hint entered on the Dialog Control.
  52. // Also, give the user the option to enter a non-blank hint.
  53. bool CHint::VerifyHint( void )
  54. {
  55. bool fVerified = true; // Default to true so we don't halt user if hint save fails.
  56. CString strHint;
  57. GetHint( strHint );
  58. if ( strHint.IsEmpty() )
  59. {
  60. CString strHintRecommended;
  61. CString strCaption;
  62. strHintRecommended.LoadString( IDS_HINT_RECOMMENDED );
  63. strCaption.LoadString( IDS_GENERIC );
  64. if ( ::MessageBox( m_hWnd, strHintRecommended, strCaption, MB_YESNO | MB_DEFBUTTON1 ) == IDYES )
  65. {
  66. CCommonDialogRoutines cdr;
  67. cdr.SetErrorFocus( m_hWnd, m_iId );
  68. fVerified = false;
  69. }
  70. }
  71. return fVerified;
  72. }
  73. // Save the Dialog Hint Text to the Registry (or Remove the Hint from the Registry if blank).
  74. void CHint::SaveHint( void )
  75. {
  76. CString strHint;
  77. GetHint( strHint );
  78. if ( strHint.IsEmpty() )
  79. {
  80. RemoveHint();
  81. }
  82. else
  83. {
  84. StoreHint( strHint );
  85. }
  86. }
  87. // Remove the Hint from the Registry.
  88. void CHint::RemoveHint( void )
  89. {
  90. CRegKey regKey;
  91. if ( regKey.Open( HKEY_LOCAL_MACHINE, ::szRATINGS ) == ERROR_SUCCESS )
  92. {
  93. if ( regKey.DeleteValue( szHINTVALUENAME ) != ERROR_SUCCESS )
  94. {
  95. TraceMsg( TF_WARNING, "CHint::RemoveHint() - Failed to delete the hint registry value." );
  96. }
  97. }
  98. else
  99. {
  100. TraceMsg( TF_WARNING, "CHint::RemoveHint() - Failed to open the ratings registry key." );
  101. }
  102. }
  103. // Get the Hint Text from the Dialog's Control (remove leading and trailing blanks).
  104. void CHint::GetHint( CString & p_rstrHint )
  105. {
  106. p_rstrHint.Empty();
  107. HWND hwndControl = ::GetDlgItem( m_hWnd, m_iId );
  108. // We shouldn't be attempting to save a hint if the edit control does not exist.
  109. ASSERT( hwndControl );
  110. if ( hwndControl != NULL )
  111. {
  112. ::GetWindowText( hwndControl, (LPTSTR) (LPCTSTR) p_rstrHint.GetBufferSetLength( cchHintLength_c + 1 ), cchHintLength_c );
  113. p_rstrHint.ReleaseBuffer();
  114. }
  115. }
  116. // Retrieve a previous Hint from the Registry.
  117. void CHint::RetrieveHint( CString & p_rstrHint )
  118. {
  119. CRegKey regKey;
  120. DWORD dwCount;
  121. p_rstrHint.Empty();
  122. if ( regKey.Open( HKEY_LOCAL_MACHINE, ::szRATINGS, KEY_READ ) == ERROR_SUCCESS )
  123. {
  124. dwCount = cchHintLength_c;
  125. if ( regKey.QueryValue( (LPTSTR) (LPCTSTR) p_rstrHint.GetBufferSetLength( cchHintLength_c + 1 ),
  126. szHINTVALUENAME, &dwCount ) != ERROR_SUCCESS )
  127. {
  128. TraceMsg( TF_WARNING, "CHint::RetrieveHint() - Failed to query the hint registry value." );
  129. }
  130. p_rstrHint.ReleaseBuffer();
  131. }
  132. else
  133. {
  134. TraceMsg( TF_WARNING, "CHint::RetrieveHint() - Failed to open the ratings registry key." );
  135. }
  136. if ( p_rstrHint.IsEmpty() )
  137. {
  138. p_rstrHint.LoadString( IDS_NO_HINT );
  139. }
  140. }
  141. // Store Hint Text into the Registry.
  142. void CHint::StoreHint( CString & p_rstrHint )
  143. {
  144. CRegKey regKey;
  145. if ( regKey.Open( HKEY_LOCAL_MACHINE, ::szRATINGS ) == ERROR_SUCCESS )
  146. {
  147. if ( regKey.SetValue( (LPTSTR) (LPCTSTR) p_rstrHint, szHINTVALUENAME ) != ERROR_SUCCESS )
  148. {
  149. TraceMsg( TF_WARNING, "CHint::StoreHint() - Failed to save the hint registry value." );
  150. }
  151. }
  152. else
  153. {
  154. TraceMsg( TF_WARNING, "CHint::StoreHint() - Failed to create the ratings registry key." );
  155. }
  156. }