UIResourceManager.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "StdAfx.h"
  2. #include "UIResourceManager.h"
  3. namespace DuiLib {
  4. CResourceManager::CResourceManager(void)
  5. {
  6. m_pQuerypInterface = NULL;
  7. }
  8. CResourceManager::~CResourceManager(void)
  9. {
  10. ResetResourceMap();
  11. }
  12. BOOL CResourceManager::LoadResource(STRINGorID xml, LPCTSTR type)
  13. {
  14. if( HIWORD(xml.m_lpstr) != NULL )
  15. {
  16. if( *(xml.m_lpstr) == _T('<') )
  17. {
  18. if( !m_xml.Load(xml.m_lpstr) ) return NULL;
  19. }
  20. else
  21. {
  22. if( !m_xml.LoadFromFile(xml.m_lpstr) ) return NULL;
  23. }
  24. }
  25. else
  26. {
  27. HRSRC hResource = ::FindResource(CPaintManagerUI::GetResourceDll(), xml.m_lpstr, type);
  28. if( hResource == NULL ) return NULL;
  29. HGLOBAL hGlobal = ::LoadResource(CPaintManagerUI::GetResourceDll(), hResource);
  30. if( hGlobal == NULL )
  31. {
  32. FreeResource(hResource);
  33. return NULL;
  34. }
  35. if( !m_xml.LoadFromMem((BYTE*)::LockResource(hGlobal), ::SizeofResource(CPaintManagerUI::GetResourceDll(), hResource) )) {
  36. return NULL;
  37. }
  38. ::FreeResource(hGlobal);
  39. }
  40. return LoadResource(m_xml.GetRoot());
  41. }
  42. BOOL CResourceManager::LoadResource(CMarkupNode Root)
  43. {
  44. if( !Root.IsValid() ) return FALSE;
  45. LPCTSTR pstrClass = NULL;
  46. int nAttributes = 0;
  47. LPCTSTR pstrName = NULL;
  48. LPCTSTR pstrValue = NULL;
  49. LPTSTR pstr = NULL;
  50. //加载图片资源
  51. LPCTSTR pstrId = NULL;
  52. LPCTSTR pstrPath = NULL;
  53. for( CMarkupNode node = Root.GetChild() ; node.IsValid(); node = node.GetSibling() )
  54. {
  55. pstrClass = node.GetName();
  56. CMarkupNode ChildNode = node.GetChild();
  57. if(ChildNode.IsValid()) LoadResource(node);
  58. else if ((_tcsicmp(pstrClass,_T("Image")) == 0) && node.HasAttributes())
  59. {
  60. //加载图片资源
  61. nAttributes = node.GetAttributeCount();
  62. for( int i = 0; i < nAttributes; i++ )
  63. {
  64. pstrName = node.GetAttributeName(i);
  65. pstrValue = node.GetAttributeValue(i);
  66. if( _tcsicmp(pstrName, _T("id")) == 0 )
  67. {
  68. pstrId = pstrValue;
  69. }
  70. else if( _tcsicmp(pstrName, _T("path")) == 0 )
  71. {
  72. pstrPath = pstrValue;
  73. }
  74. }
  75. if( pstrId == NULL || pstrPath == NULL) continue;
  76. CDuiString * pstrFind = static_cast<CDuiString *>(m_mImageHashMap.Find(pstrId));
  77. if(pstrFind != NULL) continue;
  78. m_mImageHashMap.Insert(pstrId, (LPVOID)new CDuiString(pstrPath));
  79. }
  80. else if( _tcsicmp(pstrClass,_T("Xml")) == 0 && node.HasAttributes()) {
  81. //加载XML配置文件
  82. nAttributes = node.GetAttributeCount();
  83. for( int i = 0; i < nAttributes; i++ )
  84. {
  85. pstrName = node.GetAttributeName(i);
  86. pstrValue = node.GetAttributeValue(i);
  87. if( _tcsicmp(pstrName, _T("id")) == 0 )
  88. {
  89. pstrId = pstrValue;
  90. }
  91. else if( _tcsicmp(pstrName, _T("path")) == 0 )
  92. {
  93. pstrPath = pstrValue;
  94. }
  95. }
  96. if( pstrId == NULL || pstrPath == NULL) continue;
  97. CDuiString * pstrFind = static_cast<CDuiString *>(m_mXmlHashMap.Find(pstrId));
  98. if(pstrFind != NULL) continue;
  99. m_mXmlHashMap.Insert(pstrId, (LPVOID)new CDuiString(pstrPath));
  100. }
  101. else continue;
  102. }
  103. return TRUE;
  104. }
  105. LPCTSTR CResourceManager::GetImagePath(LPCTSTR lpstrId)
  106. {
  107. CDuiString * lpStr = static_cast<CDuiString *>(m_mImageHashMap.Find(lpstrId));
  108. return lpStr == NULL? NULL:lpStr->GetData();
  109. }
  110. LPCTSTR CResourceManager::GetXmlPath(LPCTSTR lpstrId)
  111. {
  112. CDuiString * lpStr = static_cast<CDuiString *>(m_mXmlHashMap.Find(lpstrId));
  113. return lpStr == NULL? NULL:lpStr->GetData();
  114. }
  115. void CResourceManager::ResetResourceMap()
  116. {
  117. CDuiString* lpStr;
  118. for( int i = 0; i< m_mImageHashMap.GetSize(); i++ )
  119. {
  120. if(LPCTSTR key = m_mImageHashMap.GetAt(i))
  121. {
  122. lpStr = static_cast<CDuiString *>(m_mImageHashMap.Find(key));
  123. delete lpStr;
  124. lpStr = NULL;
  125. }
  126. }
  127. for( int i = 0; i< m_mXmlHashMap.GetSize(); i++ )
  128. {
  129. if(LPCTSTR key = m_mXmlHashMap.GetAt(i))
  130. {
  131. lpStr = static_cast<CDuiString *>(m_mXmlHashMap.Find(key));
  132. delete lpStr;
  133. lpStr = NULL;
  134. }
  135. }
  136. for( int i = 0; i< m_mTextResourceHashMap.GetSize(); i++ )
  137. {
  138. if(LPCTSTR key = m_mTextResourceHashMap.GetAt(i))
  139. {
  140. lpStr = static_cast<CDuiString *>(m_mTextResourceHashMap.Find(key));
  141. delete lpStr;
  142. lpStr = NULL;
  143. }
  144. }
  145. }
  146. BOOL CResourceManager::LoadLanguage(LPCTSTR pstrXml)
  147. {
  148. CMarkup xml;
  149. if( *(pstrXml) == _T('<') ) {
  150. if( !xml.Load(pstrXml) ) return FALSE;
  151. }
  152. else {
  153. if( !xml.LoadFromFile(pstrXml) ) return FALSE;
  154. }
  155. CMarkupNode Root = xml.GetRoot();
  156. if( !Root.IsValid() ) return FALSE;
  157. LPCTSTR pstrClass = NULL;
  158. int nAttributes = 0;
  159. LPCTSTR pstrName = NULL;
  160. LPCTSTR pstrValue = NULL;
  161. LPTSTR pstr = NULL;
  162. //加载图片资源
  163. LPCTSTR pstrId = NULL;
  164. LPCTSTR pstrText = NULL;
  165. for( CMarkupNode node = Root.GetChild() ; node.IsValid(); node = node.GetSibling() )
  166. {
  167. pstrClass = node.GetName();
  168. if ((_tcsicmp(pstrClass,_T("Text")) == 0) && node.HasAttributes())
  169. {
  170. //加载图片资源
  171. nAttributes = node.GetAttributeCount();
  172. for( int i = 0; i < nAttributes; i++ )
  173. {
  174. pstrName = node.GetAttributeName(i);
  175. pstrValue = node.GetAttributeValue(i);
  176. if( _tcsicmp(pstrName, _T("id")) == 0 )
  177. {
  178. pstrId = pstrValue;
  179. }
  180. else if( _tcsicmp(pstrName, _T("value")) == 0 )
  181. {
  182. pstrText = pstrValue;
  183. }
  184. }
  185. if( pstrId == NULL || pstrText == NULL) continue;
  186. CDuiString *lpstrFind = static_cast<CDuiString *>(m_mTextResourceHashMap.Find(pstrId));
  187. if(lpstrFind != NULL) {
  188. lpstrFind->Assign(pstrText);
  189. }
  190. else {
  191. m_mTextResourceHashMap.Insert(pstrId, (LPVOID)new CDuiString(pstrText));
  192. }
  193. }
  194. else continue;
  195. }
  196. return TRUE;
  197. }
  198. CDuiString CResourceManager::GetText(LPCTSTR lpstrId, LPCTSTR lpstrType)
  199. {
  200. if(lpstrId == NULL) return _T("");
  201. CDuiString *lpstrFind = static_cast<CDuiString *>(m_mTextResourceHashMap.Find(lpstrId));
  202. if (lpstrFind == NULL && m_pQuerypInterface)
  203. {
  204. LPCTSTR lpText = m_pQuerypInterface->QueryControlText(lpstrId, lpstrType);
  205. if(lpText != NULL) {
  206. lpstrFind = new CDuiString(lpText);
  207. m_mTextResourceHashMap.Insert(lpstrId, (LPVOID)lpstrFind);
  208. }
  209. }
  210. return lpstrFind == NULL ? lpstrId : *lpstrFind;
  211. }
  212. void CResourceManager::ReloadText()
  213. {
  214. if(m_pQuerypInterface == NULL) return;
  215. //重载文字描述
  216. LPCTSTR lpstrId = NULL;
  217. LPCTSTR lpstrText;
  218. for( int i = 0; i < m_mTextResourceHashMap.GetSize(); i++ )
  219. {
  220. lpstrId = m_mTextResourceHashMap.GetAt(i);
  221. if (lpstrId == NULL) continue;
  222. lpstrText = m_pQuerypInterface->QueryControlText(lpstrId, NULL);
  223. if(lpstrText != NULL) {
  224. CDuiString * lpStr = static_cast<CDuiString *>(m_mTextResourceHashMap.Find(lpstrId));
  225. lpStr->Assign(lpstrText);
  226. }
  227. }
  228. }
  229. void CResourceManager::ResetTextMap()
  230. {
  231. CDuiString * lpStr;
  232. for( int i = 0; i< m_mTextResourceHashMap.GetSize(); i++ )
  233. {
  234. if(LPCTSTR key = m_mTextResourceHashMap.GetAt(i))
  235. {
  236. lpStr = static_cast<CDuiString *>(m_mTextResourceHashMap.Find(key));
  237. delete lpStr;
  238. }
  239. }
  240. }
  241. } // namespace DuiLib