home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n04 / multilin.exe / LANGLIST.CPP < prev    next >
C/C++ Source or Header  |  1995-04-01  |  8KB  |  285 lines

  1. // langlist.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "global.h"
  6. #include "langlist.h"
  7.  
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char BASED_CODE THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13.  
  14.  
  15. BOOL GetLocaleName(LCID lcid, CString& str, PLANGVALS pLangVals)
  16. {
  17.     // Obtain required buffer size
  18.     int    cchData  = GetLocaleInfo( lcid, pLangVals->lctLanguageForm, NULL, 0) + 2;
  19.         cchData += GetLocaleInfo( lcid, pLangVals->lctCountryForm,  NULL, 0);
  20.  
  21.     if( cchData < 3)     // LocaleInfo failed both times
  22.     {
  23.         str += "<No entry>";
  24.         return FALSE;
  25.     }
  26.     else
  27.     {
  28.         LPTSTR lpzLang = str.GetBufferSetLength(cchData);
  29.         int cch = GetLocaleInfo( lcid, 
  30.                 pLangVals->lctLanguageForm, 
  31.                 lpzLang, 
  32.                 cchData-3);
  33.         str.ReleaseBuffer(-1);
  34.         str+= TEXT(" - ");
  35.         lpzLang = str.GetBufferSetLength(cchData);
  36.         cch += 3;
  37.         GetLocaleInfo( lcid, 
  38.                 pLangVals->lctCountryForm,
  39.                 lpzLang + (cch-1),
  40.                 cchData - (cch-1));
  41.         str.ReleaseBuffer(-1);
  42.         return TRUE;
  43.     }
  44. }
  45.  
  46. //.............................................................................
  47. // Callback function for enumerating the system's locals
  48. PLANGVALS pLangVals = NULL;
  49.  
  50. BOOL CALLBACK EnumLocalesProc( LPTSTR lpzLocale)
  51. {
  52.     BOOL fRC  = FALSE;  // Assume failure
  53.     LCID lcid = 0;      // For LCID value.
  54.         
  55.                                 // translate locale string to lcid value.
  56.     int  cchData = _stscanf( lpzLocale, TEXT("%x"), &lcid);    
  57.     
  58.     if ( cchData > 0 )
  59.     {                           // Build the string from lang and ctry names.
  60.         CString str;
  61.         GetLocaleName(lcid, str, pLangVals);
  62.                                 // fill the listbox
  63.                        
  64.         int lid = SendDlgItemMessage( pLangVals->hwndDlg,
  65.                                       IDC_LANGLIST, 
  66.                                       LB_ADDSTRING, 
  67.                                       0, 
  68.                                       (LPARAM)(LPCTSTR)str);
  69.         SendDlgItemMessage( pLangVals->hwndDlg,
  70.                                       IDC_LANGLIST, 
  71.                                       LB_SETITEMDATA, 
  72.                                       lid, 
  73.                                       lcid);
  74.         fRC = TRUE;
  75.     }
  76.     return fRC;
  77. }
  78.  
  79.  
  80. //.............................................................................
  81.  
  82. VOID CLanglist::LoadLanguageListBox( )
  83. {
  84.     pLangVals = &m_LangVals;
  85.     m_LangVals.hwndDlg = m_hWnd;
  86.  
  87.     SendDlgItemMessage( IDC_LANGLIST, LB_RESETCONTENT, 0, 0);
  88.     EnumSystemLocales( (LOCALE_ENUMPROC)EnumLocalesProc, 
  89.                         m_LangVals.dwInstalledForm);
  90.                             // rotate current locale into view
  91.  
  92.     CString str;
  93.     GetLocaleName(GetUserDefaultLCID(), str, pLangVals);
  94.  
  95.     int lid = SendDlgItemMessage(
  96.                         IDC_LANGLIST,
  97.                         LB_FINDSTRING,
  98.                         0,
  99.                         (LPARAM)(LPCTSTR)str);
  100.  
  101.     SendDlgItemMessage( IDC_LANGLIST, 
  102.                         LB_SETCURSEL, 
  103.                         lid, 
  104.                         0);            
  105. }
  106.  
  107. ///////////////////////////////
  108. // LoadLanguage
  109. //
  110. // Load a language module.  This function assumes that the language modules
  111. // to load are .DLLs and further that those .DLLs use a naming convention
  112. // in which the base name of the file is the three characters used in
  113. // Windows NT as the abbreviation for a language name (e.g., deu==German).
  114. //
  115. // Returns TRUE if module loaded, else FALSE
  116.  
  117. BOOL LoadLanguage( 
  118.  
  119. LCID lcid)      // Locale ID
  120. {
  121.     SetThreadLocale( lcid);     // Change thread locale
  122.  
  123.     // Get the handle to the current resource DLL and free it
  124.     HINSTANCE hInstRes = AfxGetResourceHandle();
  125.     HINSTANCE hInst = AfxGetInstanceHandle();
  126.     if( hInstRes != hInst)
  127.     {
  128.         FreeLibrary(hInstRes);
  129.         // Set resource handle to current Exe
  130.         AfxSetResourceHandle(hInst); 
  131.     }
  132.  
  133.     // Get the file name and try to load resource DLL
  134.     TCHAR lpszLang[4];
  135.     GetLocaleInfo( lcid, LOCALE_SABBREVLANGNAME, lpszLang, 4);
  136.  
  137.     if ( (int)(hInstRes = LoadLibrary( lpszLang)) < HINSTANCE_ERROR )
  138.     {
  139.         // We didn't get an exact match. Try the default sublanguage
  140.         // first    
  141.         lcid = MAKELANGID( PRIMARYLANGID( lcid), SUBLANG_DEFAULT);
  142.          GetLocaleInfo( lcid, LOCALE_SABBREVLANGNAME, lpszLang, 4);
  143.  
  144.         if( (int)(hInstRes=LoadLibrary(lpszLang)) < HINSTANCE_ERROR )
  145.         {
  146.              // wildcard search for any sublanguage
  147.              lpszLang[3] = _T('?');
  148.              WIN32_FIND_DATA  ffd;         
  149.  
  150.               HANDLE hFind = FindFirstFile(lpszLang, &ffd);
  151.  
  152.             if ( hFind != INVALID_HANDLE_VALUE )
  153.             {
  154.                 do
  155.                 {
  156.                     hInstRes = LoadLibrary( ffd.cFileName) ;                         
  157.                 } while( (int) hInstRes < HINSTANCE_ERROR
  158.                       && FindNextFile(hFind, &ffd) );
  159.  
  160.                 FindClose( hFind );
  161.             }
  162.           }
  163.  
  164.         // Execise: look for English before giving up
  165.  
  166.  
  167.         if( (int)(hInstRes) < HINSTANCE_ERROR )
  168.         {
  169.             // nothing found, use EXE file for resources
  170.             return( FALSE);
  171.         }
  172.     }
  173.     // If we get here we got a resource DLL matching the requested 
  174.     // language    and tell MFC to use it
  175.     AfxSetResourceHandle(hInstRes);
  176.     
  177.     return( TRUE);      // announce success          
  178. }
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CLanglist dialog
  182.  
  183. CLanglist::CLanglist(CWnd* pParent /*=NULL*/)
  184.     : CDialog(CLanglist::IDD, pParent)
  185. {
  186.     //{{AFX_DATA_INIT(CLanglist)
  187.         // NOTE: the ClassWizard will add member initialization here
  188.     //}}AFX_DATA_INIT
  189. }
  190.  
  191.  
  192. void CLanglist::DoDataExchange(CDataExchange* pDX)
  193. {
  194.     CDialog::DoDataExchange(pDX);
  195.     //{{AFX_DATA_MAP(CLanglist)
  196.         // NOTE: the ClassWizard will add DDX and DDV calls here
  197.     //}}AFX_DATA_MAP
  198. }
  199.  
  200.  
  201. BEGIN_MESSAGE_MAP(CLanglist, CDialog)
  202.     //{{AFX_MSG_MAP(CLanglist)
  203.     ON_BN_CLICKED(IDC_RADIOENGLISH, OnRadioEnglish)
  204.     ON_BN_CLICKED(IDC_RADIOLOCALIZED, OnRadioLocalized)
  205.     ON_BN_CLICKED(IDC_RADIONATIVE, OnRadioNative)
  206.     ON_BN_CLICKED(IDC_CHECKINSTALLED, OnCheckInstalled)
  207.     //}}AFX_MSG_MAP
  208. END_MESSAGE_MAP()
  209.  
  210.  
  211. /////////////////////////////////////////////////////////////////////////////
  212. // CLanglist message handlers
  213.  
  214. BOOL CLanglist::OnInitDialog() 
  215. {
  216.     CDialog::OnInitDialog();
  217.     
  218.     // TODO: Add extra initialization here
  219.  
  220.     m_LangVals.dwInstalledForm = LCID_INSTALLED;
  221.     m_LangVals.lctLanguageForm = LOCALE_SENGLANGUAGE;
  222.     m_LangVals.lctCountryForm = LOCALE_SENGCOUNTRY;
  223. //    m_LangVals.lctLanguageForm = LOCALE_SNATIVELANGNAME;
  224. //    m_LangVals.lctCountryForm =    LOCALE_SNATIVECTRYNAME;
  225.  
  226.     CheckDlgButton( IDC_CHECKINSTALLED, 
  227.                     (m_LangVals.dwInstalledForm == LCID_INSTALLED
  228.                         ? TRUE : FALSE));
  229.     CheckRadioButton( IDC_RADIOENGLISH, 
  230.                         IDC_RADIONATIVE, 
  231.                         IDC_RADIOENGLISH   
  232.                     );
  233.     LoadLanguageListBox();
  234.  
  235.     return TRUE;  // return TRUE unless you set the focus to a control
  236.                   // EXCEPTION: OCX Property Pages should return FALSE
  237. }
  238.  
  239. void CLanglist::OnRadioEnglish() 
  240. {
  241.     // TODO: Add your control notification handler code here
  242.     
  243.     m_LangVals.lctLanguageForm = LOCALE_SENGLANGUAGE;
  244.     m_LangVals.lctCountryForm  = LOCALE_SENGCOUNTRY;
  245.     LoadLanguageListBox();
  246. }
  247.  
  248. void CLanglist::OnRadioLocalized() 
  249. {
  250.     // TODO: Add your control notification handler code here
  251.     
  252.     m_LangVals.lctLanguageForm = LOCALE_SLANGUAGE;
  253.     m_LangVals.lctCountryForm  = LOCALE_SCOUNTRY;
  254.     LoadLanguageListBox();
  255. }
  256.  
  257. void CLanglist::OnRadioNative() 
  258. {
  259.     // TODO: Add your control notification handler code here
  260.     m_LangVals.lctLanguageForm = LOCALE_SNATIVELANGNAME;
  261.     m_LangVals.lctCountryForm  = LOCALE_SNATIVECTRYNAME;
  262.     LoadLanguageListBox();
  263. }
  264.  
  265. void CLanglist::OnCheckInstalled() 
  266. {
  267.     // TODO: Add your control notification handler code here
  268.     m_LangVals.dwInstalledForm = 
  269.                     (IsDlgButtonChecked(IDC_CHECKINSTALLED))
  270.                              ? LCID_INSTALLED : LCID_SUPPORTED;
  271.     LoadLanguageListBox();
  272. }
  273.  
  274. void CLanglist::OnOK() 
  275. {
  276.     int i = SendDlgItemMessage( IDC_LANGLIST, 
  277.                                             LB_GETCARETINDEX, 0, 0);
  278.     LCID lcid = SendDlgItemMessage( IDC_LANGLIST, 
  279.                                             LB_GETITEMDATA, i, 0);
  280.     LoadLanguage( lcid);
  281.       // TODO: Add extra validation here
  282.  
  283.     CDialog::OnOK();
  284. }
  285.