MultiLanguage.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // MultiLanguage.cpp: implementation of the CMultiLanguage class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "cp_main.h"
  6. #include "MultiLanguage.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CMultiLanguage::CMultiLanguage()
  16. {
  17. m_csAuthor = "";
  18. m_lFileVersion = 0;
  19. m_bOnlyGetHeader = false;
  20. }
  21. CMultiLanguage::~CMultiLanguage()
  22. {
  23. ClearArrays();
  24. }
  25. void CMultiLanguage::ClearArrays()
  26. {
  27. m_csAuthor = "";
  28. m_lFileVersion = 0;
  29. m_bOnlyGetHeader = false;
  30. ClearArray(m_RightClickMenu);
  31. ClearArray(m_ClipProperties);
  32. ClearArray(m_OptionsGeneral);
  33. ClearArray(m_OptionsSupportedTypes);
  34. ClearArray(m_OptionsShortcuts);
  35. ClearArray(m_OptionsQuickPaste);
  36. ClearArray(m_OptionsFriends);
  37. ClearArray(m_OptionsFriendsDetail);
  38. ClearArray(m_OptionsStats);
  39. ClearArray(m_OptionsSupportedTypesAdd);
  40. ClearArray(m_MoveToGroups);
  41. ClearArray(m_TrayIconRightClickMenu);
  42. ClearArray(m_OptionsSheet);
  43. ClearMap(m_StringMap);
  44. }
  45. void CMultiLanguage::ClearArray(LANGUAGE_ARRAY &Array)
  46. {
  47. int nSize = Array.GetSize();
  48. for(int i = 0; i < nSize; i++)
  49. {
  50. CLangItem *plItem = Array[i];
  51. delete plItem;
  52. plItem = NULL;
  53. }
  54. Array.RemoveAll();
  55. }
  56. void CMultiLanguage::ClearMap(LANGUAGE_MAP &Map)
  57. {
  58. POSITION pos = Map.GetStartPosition();
  59. CLangItem *plItem;
  60. CString csKey;
  61. while(pos)
  62. {
  63. Map.GetNextAssoc(pos, csKey, plItem);
  64. if(plItem)
  65. {
  66. delete plItem;
  67. plItem = NULL;
  68. }
  69. }
  70. Map.RemoveAll();
  71. }
  72. CString CMultiLanguage::GetString(CString csID, CString csDefault)
  73. {
  74. CLangItem *pItem;
  75. if(m_StringMap.Lookup(csID, pItem) == FALSE)
  76. {
  77. return csDefault;
  78. }
  79. if(pItem->m_csForeignLang.GetLength() <= 0)
  80. return csDefault;
  81. return pItem->m_csForeignLang;
  82. }
  83. bool CMultiLanguage::UpdateRightClickMenu(CMenu *pMenu)
  84. {
  85. return UpdateMenuToLanguage(pMenu, m_RightClickMenu);
  86. }
  87. bool CMultiLanguage::UpdateTrayIconRightClickMenu(CMenu *pMenu)
  88. {
  89. return UpdateMenuToLanguage(pMenu, m_TrayIconRightClickMenu);
  90. }
  91. bool CMultiLanguage::UpdateClipProperties(CWnd *pParent)
  92. {
  93. return UpdateWindowToLanguage(pParent, m_ClipProperties);
  94. }
  95. bool CMultiLanguage::UpdateOptionGeneral(CWnd *pParent)
  96. {
  97. return UpdateWindowToLanguage(pParent, m_OptionsGeneral);
  98. }
  99. bool CMultiLanguage::UpdateOptionSupportedTypes(CWnd *pParent)
  100. {
  101. return UpdateWindowToLanguage(pParent, m_OptionsSupportedTypes);
  102. }
  103. bool CMultiLanguage::UpdateOptionShortcuts(CWnd *pParent)
  104. {
  105. return UpdateWindowToLanguage(pParent, m_OptionsShortcuts);
  106. }
  107. bool CMultiLanguage::UpdateOptionQuickPaste(CWnd *pParent)
  108. {
  109. return UpdateWindowToLanguage(pParent, m_OptionsQuickPaste);
  110. }
  111. bool CMultiLanguage::UpdateOptionFriends(CWnd *pParent)
  112. {
  113. return UpdateWindowToLanguage(pParent, m_OptionsFriends);
  114. }
  115. bool CMultiLanguage::UpdateOptionFriendsDetail(CWnd *pParent)
  116. {
  117. return UpdateWindowToLanguage(pParent, m_OptionsFriendsDetail);
  118. }
  119. bool CMultiLanguage::UpdateOptionStats(CWnd *pParent)
  120. {
  121. return UpdateWindowToLanguage(pParent, m_OptionsStats);
  122. }
  123. bool CMultiLanguage::UpdateOptionSupportedTypesAdd(CWnd *pParent)
  124. {
  125. return UpdateWindowToLanguage(pParent, m_OptionsSupportedTypesAdd);
  126. }
  127. bool CMultiLanguage::UpdateMoveToGroups(CWnd *pParent)
  128. {
  129. return UpdateWindowToLanguage(pParent, m_MoveToGroups);
  130. }
  131. bool CMultiLanguage::UpdateOptionsSheet(CWnd *pParent)
  132. {
  133. return UpdateWindowToLanguage(pParent, m_OptionsSheet);
  134. }
  135. bool CMultiLanguage::UpdateMenuToLanguage(CMenu *pMenu, LANGUAGE_ARRAY &Array)
  136. {
  137. int nSize = Array.GetSize();
  138. for(int i = 0; i < nSize; i++)
  139. {
  140. CLangItem *plItem = Array[i];
  141. if(plItem->m_csForeignLang.GetLength() > 0)
  142. {
  143. if(plItem->m_nID > 0)
  144. {
  145. pMenu->ModifyMenu(plItem->m_nID, MF_BYCOMMAND, plItem->m_nID, plItem->m_csForeignLang);
  146. }
  147. else
  148. {
  149. //If an item doesn't have a menu id then its a group menu
  150. //just search for the text and update the text with the foreign text
  151. int nMenuPos;
  152. CMenu *pNewMenu = GetMenuPos(pMenu, plItem->m_csEnglishLang, nMenuPos);
  153. if(pNewMenu)
  154. {
  155. pNewMenu->ModifyMenu(nMenuPos, MF_BYPOSITION, -1, plItem->m_csForeignLang);
  156. }
  157. }
  158. }
  159. }
  160. return true;
  161. }
  162. bool CMultiLanguage::UpdateWindowToLanguage(CWnd *pParent, LANGUAGE_ARRAY &Array)
  163. {
  164. int nSize = Array.GetSize();
  165. for(int i = 0; i < nSize; i++)
  166. {
  167. CLangItem *plItem = Array[i];
  168. if(plItem->m_csForeignLang.GetLength() > 0)
  169. {
  170. if(plItem->m_nID > 0)
  171. {
  172. CWnd *pWnd = pParent->GetDlgItem(plItem->m_nID);
  173. if(pWnd)
  174. {
  175. pWnd->SetWindowText(plItem->m_csForeignLang);
  176. }
  177. }
  178. //If item id is -1 then set the title for the dialog
  179. else if(plItem->m_nID == -1)
  180. {
  181. pParent->SetWindowText(plItem->m_csForeignLang);
  182. }
  183. }
  184. }
  185. return true;
  186. }
  187. CMenu * CMultiLanguage::GetMenuPos(CMenu *pMenu, const CString &csLookingForMenuText, int &nMenuPos)
  188. {
  189. CMenu *pSubMenu;
  190. CString csMenuText;
  191. int nCount = pMenu->GetMenuItemCount();
  192. for(int i = 0; i < nCount; i++)
  193. {
  194. pMenu->GetMenuString(i, csMenuText, MF_BYPOSITION);
  195. if(csMenuText == csLookingForMenuText)
  196. {
  197. nMenuPos = i;
  198. return pMenu;
  199. }
  200. pSubMenu = pMenu->GetSubMenu(i);
  201. if(pSubMenu)
  202. {
  203. CMenu *pMenuReturn = GetMenuPos(pSubMenu, csLookingForMenuText, nMenuPos);
  204. if(pMenuReturn)
  205. return pMenuReturn;
  206. }
  207. }
  208. return NULL;
  209. }
  210. bool CMultiLanguage::LoadLanguageFile(CString csFile)
  211. {
  212. m_csLastError = "";
  213. CString csPath = CGetSetOptions::GetExeFileName();
  214. csPath = GetFilePath(csPath);
  215. csPath += "language\\";
  216. csPath += csFile;
  217. csPath += ".xml";
  218. ClearArrays();
  219. if(csFile.GetLength() <= 0)
  220. {
  221. m_csLastError = "Language file is blank";
  222. return false;
  223. }
  224. TiXmlDocument doc(csPath);
  225. if(!doc.LoadFile())
  226. {
  227. m_csLastError.Format("Error loading file %s - reasion = %s", csFile, doc.ErrorDesc());
  228. Log(m_csLastError);
  229. return false;
  230. }
  231. TiXmlElement *ItemHeader = doc.FirstChildElement("Ditto_Language_File");
  232. if(!ItemHeader)
  233. {
  234. m_csLastError.Format("Error finding the section %s", "Ditto_Language_File");
  235. ASSERT(!m_csLastError);
  236. Log(m_csLastError);
  237. return false;
  238. }
  239. CString csVersion = ItemHeader->Attribute("Version");
  240. m_lFileVersion = atoi(csVersion);
  241. m_csAuthor = ItemHeader->Attribute("Author");
  242. m_csNotes = ItemHeader->Attribute("Notes");
  243. if(m_bOnlyGetHeader)
  244. return true;
  245. bool bRet = LoadSection(*ItemHeader, m_RightClickMenu, "Ditto_Right_Click_Menu");
  246. bRet = LoadSection(*ItemHeader, m_ClipProperties, "Ditto_Clip_Properties");
  247. bRet = LoadSection(*ItemHeader, m_OptionsGeneral, "Ditto_Options_General");
  248. bRet = LoadSection(*ItemHeader, m_OptionsSupportedTypes, "Ditto_Options_Supported_Types");
  249. bRet = LoadSection(*ItemHeader, m_OptionsShortcuts, "Ditto_Options_Shortcuts");
  250. bRet = LoadSection(*ItemHeader, m_OptionsQuickPaste, "Ditto_Options_Quick_Paste");
  251. bRet = LoadSection(*ItemHeader, m_OptionsFriends, "Ditto_Options_Friends");
  252. bRet = LoadSection(*ItemHeader, m_OptionsFriendsDetail, "Ditto_Options_Friends_Detail");
  253. bRet = LoadSection(*ItemHeader, m_OptionsStats, "Ditto_Options_Stats");
  254. bRet = LoadSection(*ItemHeader, m_OptionsSupportedTypesAdd, "Ditto_Options_Supported_Types_Add");
  255. bRet = LoadSection(*ItemHeader, m_MoveToGroups, "Ditto_Move_To_Groups");
  256. bRet = LoadSection(*ItemHeader, m_OptionsSheet, "Ditto_Options_Sheet");
  257. bRet = LoadSection(*ItemHeader, m_TrayIconRightClickMenu, "Ditto_Tray_Icon_Menu");
  258. bRet = LoadStringTableSection(*ItemHeader, m_StringMap, "Ditto_String_Table");
  259. return true;
  260. }
  261. bool CMultiLanguage::LoadSection(TiXmlNode &doc, LANGUAGE_ARRAY &Array, CString csSection)
  262. {
  263. TiXmlNode *node = doc.FirstChild(csSection);
  264. if(!node)
  265. {
  266. m_csLastError.Format("Error finding the section %s", csSection);
  267. ASSERT(!m_csLastError);
  268. Log(m_csLastError);
  269. return false;
  270. }
  271. TiXmlNode* ForeignNode;
  272. CString csID;
  273. TiXmlElement *ItemElement = node->FirstChildElement();
  274. //load all items for this section
  275. //they look like
  276. //<Item English_Text = "Use Ctrl - Num" ID= "32777"></Item>
  277. while(ItemElement)
  278. {
  279. CLangItem *plItem = new CLangItem;
  280. plItem->m_csEnglishLang = ItemElement->Attribute("English_Text");
  281. csID = ItemElement->Attribute("ID");
  282. plItem->m_nID = atoi(csID);
  283. ForeignNode = ItemElement->FirstChild();
  284. if(ForeignNode)
  285. {
  286. plItem->m_csForeignLang = ForeignNode->Value();
  287. }
  288. Array.Add(plItem);
  289. ItemElement = ItemElement->NextSiblingElement();
  290. }
  291. return true;
  292. }
  293. bool CMultiLanguage::LoadStringTableSection(TiXmlNode &doc, LANGUAGE_MAP &Map, CString csSection)
  294. {
  295. TiXmlNode *node = doc.FirstChild(csSection);
  296. if(!node)
  297. {
  298. CString cs;
  299. cs.Format("Error finding the section %s", csSection);
  300. ASSERT(!cs);
  301. Log(cs);
  302. return false;
  303. }
  304. TiXmlNode* ForeignNode;
  305. TiXmlElement *ItemElement = node->FirstChildElement();
  306. //load all items for this section
  307. //they look like
  308. //<Item English_Text = "Use Ctrl - Num" ID= "32777"></Item>
  309. while(ItemElement)
  310. {
  311. CLangItem *plItem = new CLangItem;
  312. plItem->m_csEnglishLang = ItemElement->Attribute("English_Text");
  313. plItem->m_csID = ItemElement->Attribute("ID");
  314. ForeignNode = ItemElement->FirstChild();
  315. if(ForeignNode)
  316. {
  317. plItem->m_csForeignLang = ForeignNode->Value();
  318. }
  319. Map.SetAt(plItem->m_csID, plItem);
  320. ItemElement = ItemElement->NextSiblingElement();
  321. }
  322. return true;
  323. }