home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual Thematic 25: Programming / pc_actual_25.iso / C_C++ / RapidTranslation / data1.cab / Alles / Include / RapTraMFC.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-30  |  6.7 KB  |  246 lines

  1. /*****************************************************************
  2. ------------------------------------------------------------------
  3.  
  4.     RapTraMFC.h
  5.   
  6.     This header adapt's the MFC so that it automatically
  7.     uses the RapidTranslation DLL to translate strings
  8.  
  9.     To use RapidTranslation with MFC, please include this file
  10.  
  11. ------------------------------------------------------------------
  12. *****************************************************************/
  13.  
  14. /*****************************************************************
  15.  
  16.     Don't allow multiple inclusion of this file
  17.  
  18. *****************************************************************/
  19.  
  20. #ifndef INC_RAPTRA_MFC_HEADER
  21. #define INC_RAPTRA_MFC_HEADER
  22.  
  23.  
  24. /*****************************************************************
  25.  
  26.     This uses 
  27.     - the RapidTranslation DLL 
  28.     - the win32 SDK #define's
  29.     - the Macros against virtual functions
  30.  
  31. *****************************************************************/
  32.  
  33. #include "RapTra.h"
  34. #include "RapTraSDK.h"
  35. #include "RapTraMFCFix.h"
  36.  
  37. /*****************************************************************
  38. ------------------------------------------------------------------
  39.  
  40.     Adapt the MFC class CString
  41.  
  42. ------------------------------------------------------------------
  43. *****************************************************************/
  44.  
  45. /* Be sure CString is not #define'd */
  46. #undef CString
  47.  
  48. /*****************************************************************
  49.  
  50.     The maximum length of stringtable entry's
  51.     Adapt this #define if you need larger strings
  52.  
  53. *****************************************************************/
  54.  
  55. #define _RAPTRA_MFC_MAX_STRINGTABLE_ENTRY 2048
  56.  
  57.  
  58. /*****************************************************************
  59.  
  60.     This function translates a CString
  61.  
  62. *****************************************************************/
  63.  
  64. class __X_CString;
  65.  
  66. /* Translate the string */
  67. static BOOL __X_TranslateString(CString& szText)
  68. {
  69.     /* Use this buffer */
  70.     TCHAR szBuffer[_RAPTRA_MFC_MAX_STRINGTABLE_ENTRY];
  71.  
  72.     /* Translate the string */
  73.     int nLen = RTTranslateString(szBuffer, szText,
  74.         _RAPTRA_MFC_MAX_STRINGTABLE_ENTRY);
  75.  
  76.     /* Copy back if something has been translated */
  77.     if(nLen > 0)
  78.         szText = CString(szBuffer);
  79.  
  80.     /* TRUE if something was translated */
  81.     return (nLen > 0);
  82. }
  83.  
  84. /*****************************************************************
  85.  
  86.     This class does adapt the MFC class CString so that it uses
  87.     RapidTranslation
  88.  
  89. *****************************************************************/
  90.  
  91. /* Declare the string load function */
  92. BOOL __X_TranslateLoadString(CString& szText, UINT nID);
  93.  
  94. /* The adapted class */
  95. class __X_CString : public CString
  96. {
  97. public:
  98.     /* The constructors have to be overloaded to make castings work */
  99.     __X_CString() : CString() {};
  100.     __X_CString(const CString& stringSrc) : CString(stringSrc) {};
  101.     __X_CString(TCHAR ch, int nRepeat = 1) : CString(ch, nRepeat) {};
  102.     __X_CString(LPCTSTR lpch, int nLength) : CString(lpch, nLength) {};
  103.     __X_CString(const unsigned char* psz) : CString(psz) {};
  104.  
  105.     /* These are conversion constructors */
  106.     #ifdef _UNICODE
  107.         __X_CString(LPCSTR lpsz) : CString(lpsz) {}; /* conversion a => w */
  108.     #else
  109.         __X_CString(LPCWSTR lpsz) : CString(lpsz) {}; /* conversion w => a */
  110.     #endif
  111.  
  112.     /* And this is a constructor for stringtable entrys */
  113.     __X_CString(LPCTSTR lpsz) : CString(lpsz) 
  114.     {
  115.         /* It is a stringtable constructor */
  116.         if (lpsz != NULL && HIWORD(lpsz) == NULL)
  117.         {
  118.             /* Now translate this string */
  119.             __X_TranslateString(*this);
  120.         }
  121.     }
  122.  
  123.     /* This overrides the LoadString function in normal chars ... */
  124.     BOOL __X_LoadStringA(UINT nID)
  125.     {
  126.         return __X_TranslateLoadString(*this, nID);
  127.     };
  128.  
  129.     /* ... and in UNICODE */
  130.     BOOL __X_LoadStringW(UINT nID)
  131.     {
  132.         return __X_TranslateLoadString(*this, nID);
  133.     };
  134.  
  135.     /* to access original stringtable */
  136.     BOOL __X_GetTheString(UINT nID)
  137.     {
  138.         /* we have to switch off and on the SDK stuff */
  139.         #undef LoadStringA
  140.         #undef LoadStringW
  141.             BOOL bRes = ((CString*)this)->LoadString(nID);
  142.             return bRes;
  143.         #ifdef _UNICODE
  144.             #define LoadStringW __X_LoadStringW            
  145.         #else
  146.             #define LoadStringA __X_LoadStringA
  147.         #endif
  148.     };
  149.  
  150.     /* Format sring member functions */
  151.     void AFX_CDECL Format(LPCTSTR lpszFormat, ...)
  152.     {
  153.         /* do it like mfc does it */
  154.         ASSERT(AfxIsValidString(lpszFormat, FALSE));
  155.         va_list argList;
  156.         va_start(argList, lpszFormat);
  157.         FormatV(lpszFormat, argList);
  158.         va_end(argList);
  159.     }
  160. ;
  161.     /* This formats from stringtable string, so translate it */
  162.     void AFX_CDECL Format(UINT nFormatID, ...)
  163.     {
  164.         /* translate ist */
  165.         __X_CString strFormat;
  166.         #ifdef _UNICODE
  167.             VERIFY(strFormat.__X_LoadStringW(nFormatID) != 0);
  168.         #else
  169.             VERIFY(strFormat.__X_LoadStringA(nFormatID) != 0);
  170.         #endif
  171.  
  172.         /* mfc formats */
  173.         va_list argList;
  174.         va_start(argList, nFormatID);
  175.         FormatV(strFormat, argList);
  176.         va_end(argList);
  177.     }
  178. };
  179.  
  180. /* Load and translate the string from stringtable */
  181. static BOOL __X_TranslateLoadString(CString& szText, UINT nID)
  182. {
  183.     /* Load the original stringtable string */
  184.     BOOL bRes = ((__X_CString*)&szText)->__X_GetTheString(nID);
  185.  
  186.     /* And translate it */
  187.     BOOL bTrans = __X_TranslateString(szText);
  188.  
  189.     /* return TRUE even if not translated to prevent assert's */
  190.     return bRes;
  191. }
  192.  
  193. /* Now activate the new class */
  194. #define CString __X_CString
  195.  
  196.  
  197. /*****************************************************************
  198. ------------------------------------------------------------------
  199.  
  200.     Adapt the Message boxes
  201.  
  202. ------------------------------------------------------------------
  203. *****************************************************************/
  204.  
  205. /* Be sure AfxMessageBox is not #define'd */
  206. #undef AfxMessageBox
  207.  
  208. /*****************************************************************
  209.  
  210.     The new AfxMessageBox - version
  211.  
  212. *****************************************************************/
  213.  
  214. /* AfxMessageBox from stringtable: translate ! */
  215. static int __X_AfxMessageBox(UINT nIDPrompt, UINT nType = MB_OK,
  216.     UINT nIDHelp = (UINT)-1)
  217. {
  218.     /* Get the translated text */
  219.     __X_CString szText;
  220.     __X_TranslateLoadString(szText, nIDPrompt);
  221.  
  222.     /* The rest is MFC*/
  223.     return AfxMessageBox(szText, nType, nIDHelp);
  224. }
  225.  
  226. /* Normal string - AfxMessageBox */
  227. static int __X_AfxMessageBox(LPCTSTR lpszText, UINT nType = MB_OK,
  228.     UINT nIDHelp = 0)
  229. {
  230.     return AfxMessageBox(lpszText, nType, nIDHelp);
  231. }
  232.  
  233. /* Now activate the new message box */
  234.  
  235. #define AfxMessageBox __X_AfxMessageBox
  236.  
  237. /*****************************************************************
  238.  
  239.     End of multiple inclusion prevention
  240.  
  241. *****************************************************************/
  242.  
  243. #endif
  244.  
  245.  
  246.