123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- // MultiLanguage.cpp: implementation of the CMultiLanguage class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "cp_main.h"
- #include "MultiLanguage.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CMultiLanguage::CMultiLanguage()
- {
- m_csAuthor = "";
- m_lFileVersion = 0;
- m_bOnlyGetHeader = false;
- }
- CMultiLanguage::~CMultiLanguage()
- {
- ClearArrays();
- }
- void CMultiLanguage::ClearArrays()
- {
- m_csAuthor = "";
- m_lFileVersion = 0;
- m_bOnlyGetHeader = false;
- ClearArray(m_RightClickMenu);
- ClearArray(m_ClipProperties);
- ClearArray(m_OptionsGeneral);
- ClearArray(m_OptionsSupportedTypes);
- ClearArray(m_OptionsShortcuts);
- ClearArray(m_OptionsQuickPaste);
- ClearArray(m_OptionsFriends);
- ClearArray(m_OptionsFriendsDetail);
- ClearArray(m_OptionsStats);
- ClearArray(m_OptionsSupportedTypesAdd);
- ClearArray(m_MoveToGroups);
- ClearArray(m_TrayIconRightClickMenu);
- ClearArray(m_OptionsSheet);
- ClearMap(m_StringMap);
- }
- void CMultiLanguage::ClearArray(LANGUAGE_ARRAY &Array)
- {
- int nSize = Array.GetSize();
- for(int i = 0; i < nSize; i++)
- {
- CLangItem *plItem = Array[i];
- delete plItem;
- plItem = NULL;
- }
- Array.RemoveAll();
- }
- void CMultiLanguage::ClearMap(LANGUAGE_MAP &Map)
- {
- POSITION pos = Map.GetStartPosition();
- CLangItem *plItem;
- CString csKey;
- while(pos)
- {
- Map.GetNextAssoc(pos, csKey, plItem);
- if(plItem)
- {
- delete plItem;
- plItem = NULL;
- }
- }
- Map.RemoveAll();
- }
- CString CMultiLanguage::GetString(CString csID, CString csDefault)
- {
- CLangItem *pItem;
-
- if(m_StringMap.Lookup(csID, pItem) == FALSE)
- {
- return csDefault;
- }
- if(pItem->m_csForeignLang.GetLength() <= 0)
- return csDefault;
- return pItem->m_csForeignLang;
- }
- bool CMultiLanguage::UpdateRightClickMenu(CMenu *pMenu)
- {
- return UpdateMenuToLanguage(pMenu, m_RightClickMenu);
- }
- bool CMultiLanguage::UpdateTrayIconRightClickMenu(CMenu *pMenu)
- {
- return UpdateMenuToLanguage(pMenu, m_TrayIconRightClickMenu);
- }
- bool CMultiLanguage::UpdateClipProperties(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_ClipProperties);
- }
- bool CMultiLanguage::UpdateOptionGeneral(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsGeneral);
- }
- bool CMultiLanguage::UpdateOptionSupportedTypes(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsSupportedTypes);
- }
- bool CMultiLanguage::UpdateOptionShortcuts(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsShortcuts);
- }
- bool CMultiLanguage::UpdateOptionQuickPaste(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsQuickPaste);
- }
- bool CMultiLanguage::UpdateOptionFriends(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsFriends);
- }
- bool CMultiLanguage::UpdateOptionFriendsDetail(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsFriendsDetail);
- }
- bool CMultiLanguage::UpdateOptionStats(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsStats);
- }
- bool CMultiLanguage::UpdateOptionSupportedTypesAdd(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsSupportedTypesAdd);
- }
- bool CMultiLanguage::UpdateMoveToGroups(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_MoveToGroups);
- }
- bool CMultiLanguage::UpdateOptionsSheet(CWnd *pParent)
- {
- return UpdateWindowToLanguage(pParent, m_OptionsSheet);
- }
- bool CMultiLanguage::UpdateMenuToLanguage(CMenu *pMenu, LANGUAGE_ARRAY &Array)
- {
- int nSize = Array.GetSize();
- for(int i = 0; i < nSize; i++)
- {
- CLangItem *plItem = Array[i];
- if(plItem->m_csForeignLang.GetLength() > 0)
- {
- if(plItem->m_nID > 0)
- {
- pMenu->ModifyMenu(plItem->m_nID, MF_BYCOMMAND, plItem->m_nID, plItem->m_csForeignLang);
- }
- else
- {
- //If an item doesn't have a menu id then its a group menu
- //just search for the text and update the text with the foreign text
- int nMenuPos;
- CMenu *pNewMenu = GetMenuPos(pMenu, plItem->m_csEnglishLang, nMenuPos);
- if(pNewMenu)
- {
- pNewMenu->ModifyMenu(nMenuPos, MF_BYPOSITION, -1, plItem->m_csForeignLang);
- }
- }
- }
- }
- return true;
- }
- bool CMultiLanguage::UpdateWindowToLanguage(CWnd *pParent, LANGUAGE_ARRAY &Array)
- {
- int nSize = Array.GetSize();
- for(int i = 0; i < nSize; i++)
- {
- CLangItem *plItem = Array[i];
- if(plItem->m_csForeignLang.GetLength() > 0)
- {
- if(plItem->m_nID > 0)
- {
- CWnd *pWnd = pParent->GetDlgItem(plItem->m_nID);
- if(pWnd)
- {
- pWnd->SetWindowText(plItem->m_csForeignLang);
- }
- }
- //If item id is -1 then set the title for the dialog
- else if(plItem->m_nID == -1)
- {
- pParent->SetWindowText(plItem->m_csForeignLang);
- }
- }
- }
- return true;
- }
- CMenu * CMultiLanguage::GetMenuPos(CMenu *pMenu, const CString &csLookingForMenuText, int &nMenuPos)
- {
- CMenu *pSubMenu;
- CString csMenuText;
- int nCount = pMenu->GetMenuItemCount();
- for(int i = 0; i < nCount; i++)
- {
- pMenu->GetMenuString(i, csMenuText, MF_BYPOSITION);
- if(csMenuText == csLookingForMenuText)
- {
- nMenuPos = i;
- return pMenu;
- }
- pSubMenu = pMenu->GetSubMenu(i);
- if(pSubMenu)
- {
- CMenu *pMenuReturn = GetMenuPos(pSubMenu, csLookingForMenuText, nMenuPos);
- if(pMenuReturn)
- return pMenuReturn;
- }
- }
- return NULL;
- }
- bool CMultiLanguage::LoadLanguageFile(CString csFile)
- {
- m_csLastError = "";
-
- CString csPath = CGetSetOptions::GetExeFileName();
- csPath = GetFilePath(csPath);
- csPath += "language\\";
- csPath += csFile;
- csPath += ".xml";
- ClearArrays();
- if(csFile.GetLength() <= 0)
- {
- m_csLastError = "Language file is blank";
- return false;
- }
- TiXmlDocument doc(csPath);
- if(!doc.LoadFile())
- {
- m_csLastError.Format("Error loading file %s - reasion = %s", csFile, doc.ErrorDesc());
- Log(m_csLastError);
- return false;
- }
- TiXmlElement *ItemHeader = doc.FirstChildElement("Ditto_Language_File");
- if(!ItemHeader)
- {
- m_csLastError.Format("Error finding the section %s", "Ditto_Language_File");
- ASSERT(!m_csLastError);
- Log(m_csLastError);
- return false;
- }
- CString csVersion = ItemHeader->Attribute("Version");
- m_lFileVersion = atoi(csVersion);
- m_csAuthor = ItemHeader->Attribute("Author");
- m_csNotes = ItemHeader->Attribute("Notes");
- if(m_bOnlyGetHeader)
- return true;
- bool bRet = LoadSection(*ItemHeader, m_RightClickMenu, "Ditto_Right_Click_Menu");
- bRet = LoadSection(*ItemHeader, m_ClipProperties, "Ditto_Clip_Properties");
- bRet = LoadSection(*ItemHeader, m_OptionsGeneral, "Ditto_Options_General");
- bRet = LoadSection(*ItemHeader, m_OptionsSupportedTypes, "Ditto_Options_Supported_Types");
- bRet = LoadSection(*ItemHeader, m_OptionsShortcuts, "Ditto_Options_Shortcuts");
- bRet = LoadSection(*ItemHeader, m_OptionsQuickPaste, "Ditto_Options_Quick_Paste");
- bRet = LoadSection(*ItemHeader, m_OptionsFriends, "Ditto_Options_Friends");
- bRet = LoadSection(*ItemHeader, m_OptionsFriendsDetail, "Ditto_Options_Friends_Detail");
- bRet = LoadSection(*ItemHeader, m_OptionsStats, "Ditto_Options_Stats");
- bRet = LoadSection(*ItemHeader, m_OptionsSupportedTypesAdd, "Ditto_Options_Supported_Types_Add");
- bRet = LoadSection(*ItemHeader, m_MoveToGroups, "Ditto_Move_To_Groups");
- bRet = LoadSection(*ItemHeader, m_OptionsSheet, "Ditto_Options_Sheet");
- bRet = LoadSection(*ItemHeader, m_TrayIconRightClickMenu, "Ditto_Tray_Icon_Menu");
-
- bRet = LoadStringTableSection(*ItemHeader, m_StringMap, "Ditto_String_Table");
- return true;
- }
- bool CMultiLanguage::LoadSection(TiXmlNode &doc, LANGUAGE_ARRAY &Array, CString csSection)
- {
- TiXmlNode *node = doc.FirstChild(csSection);
- if(!node)
- {
- m_csLastError.Format("Error finding the section %s", csSection);
- ASSERT(!m_csLastError);
- Log(m_csLastError);
- return false;
- }
- TiXmlNode* ForeignNode;
- CString csID;
-
- TiXmlElement *ItemElement = node->FirstChildElement();
- //load all items for this section
- //they look like
- //<Item English_Text = "Use Ctrl - Num" ID= "32777"></Item>
- while(ItemElement)
- {
- CLangItem *plItem = new CLangItem;
- plItem->m_csEnglishLang = ItemElement->Attribute("English_Text");
- csID = ItemElement->Attribute("ID");
- plItem->m_nID = atoi(csID);
- ForeignNode = ItemElement->FirstChild();
- if(ForeignNode)
- {
- plItem->m_csForeignLang = ForeignNode->Value();
- }
- Array.Add(plItem);
- ItemElement = ItemElement->NextSiblingElement();
- }
-
- return true;
- }
- bool CMultiLanguage::LoadStringTableSection(TiXmlNode &doc, LANGUAGE_MAP &Map, CString csSection)
- {
- TiXmlNode *node = doc.FirstChild(csSection);
- if(!node)
- {
- CString cs;
- cs.Format("Error finding the section %s", csSection);
- ASSERT(!cs);
- Log(cs);
- return false;
- }
- TiXmlNode* ForeignNode;
-
- TiXmlElement *ItemElement = node->FirstChildElement();
- //load all items for this section
- //they look like
- //<Item English_Text = "Use Ctrl - Num" ID= "32777"></Item>
- while(ItemElement)
- {
- CLangItem *plItem = new CLangItem;
- plItem->m_csEnglishLang = ItemElement->Attribute("English_Text");
- plItem->m_csID = ItemElement->Attribute("ID");
- ForeignNode = ItemElement->FirstChild();
- if(ForeignNode)
- {
- plItem->m_csForeignLang = ForeignNode->Value();
- }
- Map.SetAt(plItem->m_csID, plItem);
- ItemElement = ItemElement->NextSiblingElement();
- }
- return true;
- }
|