UIDlgBuilder.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include "StdAfx.h"
  2. namespace DuiLib {
  3. CDialogBuilder::CDialogBuilder() : m_pCallback(NULL), m_pstrtype(NULL)
  4. {
  5. }
  6. CControlUI* CDialogBuilder::Create(STRINGorID xml, LPCTSTR type, IDialogBuilderCallback* pCallback,
  7. CPaintManagerUI* pManager, CControlUI* pParent)
  8. {
  9. //资源ID为0-65535,两个字节;字符串指针为4个字节
  10. //字符串以<开头认为是XML字符串,否则认为是XML文件
  11. if( HIWORD(xml.m_lpstr) != NULL ) {
  12. if( *(xml.m_lpstr) == _T('<') ) {
  13. if( !m_xml.Load(xml.m_lpstr) ) return NULL;
  14. }
  15. else {
  16. if( !m_xml.LoadFromFile(xml.m_lpstr) ) return NULL;
  17. }
  18. }
  19. else {
  20. HRSRC hResource = ::FindResource(CPaintManagerUI::GetResourceDll(), xml.m_lpstr, type);
  21. if( hResource == NULL ) return NULL;
  22. HGLOBAL hGlobal = ::LoadResource(CPaintManagerUI::GetResourceDll(), hResource);
  23. if( hGlobal == NULL ) {
  24. FreeResource(hResource);
  25. return NULL;
  26. }
  27. m_pCallback = pCallback;
  28. if( !m_xml.LoadFromMem((BYTE*)::LockResource(hGlobal), ::SizeofResource(CPaintManagerUI::GetResourceDll(), hResource) )) return NULL;
  29. ::FreeResource(hResource);
  30. m_pstrtype = type;
  31. }
  32. return Create(pCallback, pManager, pParent);
  33. }
  34. CControlUI* CDialogBuilder::Create(IDialogBuilderCallback* pCallback, CPaintManagerUI* pManager, CControlUI* pParent)
  35. {
  36. m_pCallback = pCallback;
  37. CMarkupNode root = m_xml.GetRoot();
  38. if( !root.IsValid() ) return NULL;
  39. if( pManager ) {
  40. LPCTSTR pstrClass = NULL;
  41. int nAttributes = 0;
  42. LPCTSTR pstrName = NULL;
  43. LPCTSTR pstrValue = NULL;
  44. LPTSTR pstr = NULL;
  45. for( CMarkupNode node = root.GetChild() ; node.IsValid(); node = node.GetSibling() ) {
  46. pstrClass = node.GetName();
  47. if( _tcsicmp(pstrClass, _T("Image")) == 0 ) {
  48. nAttributes = node.GetAttributeCount();
  49. LPCTSTR pImageName = NULL;
  50. LPCTSTR pImageResType = NULL;
  51. DWORD mask = 0;
  52. bool shared = false;
  53. for( int i = 0; i < nAttributes; i++ ) {
  54. pstrName = node.GetAttributeName(i);
  55. pstrValue = node.GetAttributeValue(i);
  56. if( _tcsicmp(pstrName, _T("name")) == 0 ) {
  57. pImageName = pstrValue;
  58. }
  59. else if( _tcsicmp(pstrName, _T("restype")) == 0 ) {
  60. pImageResType = pstrValue;
  61. }
  62. else if( _tcsicmp(pstrName, _T("mask")) == 0 ) {
  63. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  64. mask = _tcstoul(pstrValue, &pstr, 16);
  65. }
  66. else if( _tcsicmp(pstrName, _T("shared")) == 0 ) {
  67. shared = (_tcsicmp(pstrValue, _T("true")) == 0);
  68. }
  69. }
  70. if( pImageName ) pManager->AddImage(pImageName, pImageResType, mask, shared);
  71. }
  72. else if( _tcsicmp(pstrClass, _T("Font")) == 0 ) {
  73. nAttributes = node.GetAttributeCount();
  74. int id = -1;
  75. LPCTSTR pFontName = NULL;
  76. int size = 12;
  77. bool bold = false;
  78. bool underline = false;
  79. bool italic = false;
  80. bool defaultfont = false;
  81. bool shared = false;
  82. for( int i = 0; i < nAttributes; i++ ) {
  83. pstrName = node.GetAttributeName(i);
  84. pstrValue = node.GetAttributeValue(i);
  85. if( _tcsicmp(pstrName, _T("id")) == 0 ) {
  86. id = _tcstol(pstrValue, &pstr, 10);
  87. }
  88. else if( _tcsicmp(pstrName, _T("name")) == 0 ) {
  89. pFontName = pstrValue;
  90. }
  91. else if( _tcsicmp(pstrName, _T("size")) == 0 ) {
  92. size = _tcstol(pstrValue, &pstr, 10);
  93. }
  94. else if( _tcsicmp(pstrName, _T("bold")) == 0 ) {
  95. bold = (_tcsicmp(pstrValue, _T("true")) == 0);
  96. }
  97. else if( _tcsicmp(pstrName, _T("underline")) == 0 ) {
  98. underline = (_tcsicmp(pstrValue, _T("true")) == 0);
  99. }
  100. else if( _tcsicmp(pstrName, _T("italic")) == 0 ) {
  101. italic = (_tcsicmp(pstrValue, _T("true")) == 0);
  102. }
  103. else if( _tcsicmp(pstrName, _T("default")) == 0 ) {
  104. defaultfont = (_tcsicmp(pstrValue, _T("true")) == 0);
  105. }
  106. else if( _tcsicmp(pstrName, _T("shared")) == 0 ) {
  107. shared = (_tcsicmp(pstrValue, _T("true")) == 0);
  108. }
  109. }
  110. if( id >= 0 && pFontName ) {
  111. pManager->AddFont(id, pFontName, size, bold, underline, italic, shared);
  112. if( defaultfont ) pManager->SetDefaultFont(pFontName, size, bold, underline, italic, shared);
  113. }
  114. }
  115. else if( _tcsicmp(pstrClass, _T("Default")) == 0 ) {
  116. nAttributes = node.GetAttributeCount();
  117. LPCTSTR pControlName = NULL;
  118. LPCTSTR pControlValue = NULL;
  119. bool shared = false;
  120. for( int i = 0; i < nAttributes; i++ ) {
  121. pstrName = node.GetAttributeName(i);
  122. pstrValue = node.GetAttributeValue(i);
  123. if( _tcsicmp(pstrName, _T("name")) == 0 ) {
  124. pControlName = pstrValue;
  125. }
  126. else if( _tcsicmp(pstrName, _T("value")) == 0 ) {
  127. pControlValue = pstrValue;
  128. }
  129. else if( _tcsicmp(pstrName, _T("shared")) == 0 ) {
  130. shared = (_tcsicmp(pstrValue, _T("true")) == 0);
  131. }
  132. }
  133. if( pControlName ) {
  134. pManager->AddDefaultAttributeList(pControlName, pControlValue, shared);
  135. }
  136. }
  137. else if( _tcsicmp(pstrClass, _T("MultiLanguage")) == 0 ) {
  138. nAttributes = node.GetAttributeCount();
  139. int id = -1;
  140. LPCTSTR pMultiLanguage = NULL;
  141. for( int i = 0; i < nAttributes; i++ ) {
  142. pstrName = node.GetAttributeName(i);
  143. pstrValue = node.GetAttributeValue(i);
  144. if( _tcsicmp(pstrName, _T("id")) == 0 ) {
  145. id = _tcstol(pstrValue, &pstr, 10);
  146. }
  147. else if( _tcsicmp(pstrName, _T("value")) == 0 ) {
  148. pMultiLanguage = pstrValue;
  149. }
  150. }
  151. if (id >= 0 && pMultiLanguage ) {
  152. pManager->AddMultiLanguageString(id, pMultiLanguage);
  153. }
  154. }
  155. }
  156. pstrClass = root.GetName();
  157. if( _tcsicmp(pstrClass, _T("Window")) == 0 ) {
  158. if( pManager->GetPaintWindow() ) {
  159. int nAttributes = root.GetAttributeCount();
  160. for( int i = 0; i < nAttributes; i++ ) {
  161. pstrName = root.GetAttributeName(i);
  162. pstrValue = root.GetAttributeValue(i);
  163. pManager->SetWindowAttribute(pstrName, pstrValue);
  164. }
  165. }
  166. }
  167. }
  168. return _Parse(&root, pParent, pManager);
  169. }
  170. CMarkup* CDialogBuilder::GetMarkup()
  171. {
  172. return &m_xml;
  173. }
  174. void CDialogBuilder::GetLastErrorMessage(LPTSTR pstrMessage, SIZE_T cchMax) const
  175. {
  176. return m_xml.GetLastErrorMessage(pstrMessage, cchMax);
  177. }
  178. void CDialogBuilder::GetLastErrorLocation(LPTSTR pstrSource, SIZE_T cchMax) const
  179. {
  180. return m_xml.GetLastErrorLocation(pstrSource, cchMax);
  181. }
  182. CControlUI* CDialogBuilder::_Parse(CMarkupNode* pRoot, CControlUI* pParent, CPaintManagerUI* pManager)
  183. {
  184. IContainerUI* pContainer = NULL;
  185. CControlUI* pReturn = NULL;
  186. for( CMarkupNode node = pRoot->GetChild() ; node.IsValid(); node = node.GetSibling() ) {
  187. LPCTSTR pstrClass = node.GetName();
  188. if( _tcsicmp(pstrClass, _T("Image")) == 0 || _tcsicmp(pstrClass, _T("Font")) == 0 \
  189. || _tcsicmp(pstrClass, _T("Default")) == 0
  190. || _tcsicmp(pstrClass, _T("MultiLanguage")) == 0 ) continue;
  191. CControlUI* pControl = NULL;
  192. if( _tcsicmp(pstrClass, _T("Include")) == 0 ) {
  193. if( !node.HasAttributes() ) continue;
  194. int count = 1;
  195. LPTSTR pstr = NULL;
  196. TCHAR szValue[500] = { 0 };
  197. SIZE_T cchLen = lengthof(szValue) - 1;
  198. if ( node.GetAttributeValue(_T("count"), szValue, cchLen) )
  199. count = _tcstol(szValue, &pstr, 10);
  200. cchLen = lengthof(szValue) - 1;
  201. if ( !node.GetAttributeValue(_T("source"), szValue, cchLen) ) continue;
  202. CDialogBuilder builder;
  203. for ( int i = 0; i < count; i++ ) {
  204. if (!builder.GetMarkup()->IsValid())
  205. {
  206. if( m_pstrtype != NULL ) { // 使用资源dll,从资源中读取
  207. WORD id = (WORD)_tcstol(szValue, &pstr, 10);
  208. pControl = builder.Create((UINT)id, m_pstrtype, m_pCallback, pManager, pParent);
  209. }
  210. else
  211. pControl = builder.Create((LPCTSTR)szValue, (UINT)0, m_pCallback, pManager, pParent);
  212. }
  213. else
  214. pControl = builder.Create(m_pCallback, pManager, pParent);
  215. }
  216. continue;
  217. }
  218. //树控件XML解析
  219. else if( _tcsicmp(pstrClass, _T("TreeNode")) == 0 ) {
  220. CTreeNodeUI* pParentNode = static_cast<CTreeNodeUI*>(pParent->GetInterface(_T("TreeNode")));
  221. CTreeNodeUI* pNode = new CTreeNodeUI();
  222. if(pParentNode){
  223. if(!pParentNode->Add(pNode)){
  224. delete pNode;
  225. continue;
  226. }
  227. }
  228. // 若有控件默认配置先初始化默认属性
  229. if( pManager ) {
  230. pNode->SetManager(pManager, NULL, false);
  231. LPCTSTR pDefaultAttributes = pManager->GetDefaultAttributeList(pstrClass);
  232. if( pDefaultAttributes ) {
  233. pNode->SetAttributeList(pDefaultAttributes);
  234. }
  235. }
  236. // 解析所有属性并覆盖默认属性
  237. if( node.HasAttributes() ) {
  238. TCHAR szValue[500] = { 0 };
  239. SIZE_T cchLen = lengthof(szValue) - 1;
  240. // Set ordinary attributes
  241. int nAttributes = node.GetAttributeCount();
  242. for( int i = 0; i < nAttributes; i++ ) {
  243. pNode->SetAttribute(node.GetAttributeName(i), node.GetAttributeValue(i));
  244. }
  245. }
  246. //检索子节点及附加控件
  247. if(node.HasChildren()){
  248. CControlUI* pSubControl = _Parse(&node,pNode,pManager);
  249. if(pSubControl && _tcsicmp(pSubControl->GetClass(),_T("TreeNodeUI")) != 0)
  250. {
  251. // pSubControl->SetFixedWidth(30);
  252. // CHorizontalLayoutUI* pHorz = pNode->GetTreeNodeHoriznotal();
  253. // pHorz->Add(new CEditUI());
  254. // continue;
  255. }
  256. }
  257. if(!pParentNode){
  258. CTreeViewUI* pTreeView = static_cast<CTreeViewUI*>(pParent->GetInterface(_T("TreeView")));
  259. ASSERT(pTreeView);
  260. if( pTreeView == NULL ) return NULL;
  261. if( !pTreeView->Add(pNode) ) {
  262. delete pNode;
  263. continue;
  264. }
  265. }
  266. continue;
  267. }
  268. else {
  269. #ifdef _DEBUG
  270. DUITRACE(_T("Create Control: %s"), pstrClass);
  271. #endif
  272. SIZE_T cchLen = _tcslen(pstrClass);
  273. switch( cchLen ) {
  274. case 4:
  275. if( _tcsicmp(pstrClass, DUI_CTR_EDIT) == 0 ) pControl = new CEditUI;
  276. else if( _tcsicmp(pstrClass, DUI_CTR_LIST) == 0 ) pControl = new CListUI;
  277. else if( _tcsicmp(pstrClass, DUI_CTR_TEXT) == 0 ) pControl = new CTextUI;
  278. else if( _tcsicmp(pstrClass, DUI_CTR_TREE) == 0 ) pControl = new CTreeViewUI;
  279. else if( _tcsicmp(pstrClass, DUI_CTR_HBOX) == 0 ) pControl = new CHorizontalLayoutUI;
  280. else if( _tcsicmp(pstrClass, DUI_CTR_VBOX) == 0 ) pControl = new CVerticalLayoutUI;
  281. break;
  282. case 5:
  283. if( _tcsicmp(pstrClass, DUI_CTR_COMBO) == 0 ) pControl = new CComboUI;
  284. else if( _tcsicmp(pstrClass, DUI_CTR_LABEL) == 0 ) pControl = new CLabelUI;
  285. //else if( _tcsicmp(pstrClass, DUI_CTR_FLASH) == 0 ) pControl = new CFlashUI;
  286. break;
  287. case 6:
  288. if( _tcsicmp(pstrClass, DUI_CTR_BUTTON) == 0 ) pControl = new CButtonUI;
  289. else if( _tcsicmp(pstrClass, DUI_CTR_OPTION) == 0 ) pControl = new COptionUI;
  290. else if( _tcsicmp(pstrClass, DUI_CTR_SLIDER) == 0 ) pControl = new CSliderUI;
  291. break;
  292. case 7:
  293. if( _tcsicmp(pstrClass, DUI_CTR_CONTROL) == 0 ) pControl = new CControlUI;
  294. else if( _tcsicmp(pstrClass, DUI_CTR_ACTIVEX) == 0 ) pControl = new CActiveXUI;
  295. else if (_tcscmp(pstrClass, DUI_CTR_GIFANIM) == 0) pControl = new CGifAnimUI;
  296. break;
  297. case 8:
  298. if( _tcsicmp(pstrClass, DUI_CTR_PROGRESS) == 0 ) pControl = new CProgressUI;
  299. else if( _tcsicmp(pstrClass, DUI_CTR_RICHEDIT) == 0 ) pControl = new CRichEditUI;
  300. else if( _tcsicmp(pstrClass, DUI_CTR_CHECKBOX) == 0 ) pControl = new CCheckBoxUI;
  301. else if( _tcsicmp(pstrClass, DUI_CTR_COMBOBOX) == 0 ) pControl = new CComboUI;
  302. else if( _tcsicmp(pstrClass, DUI_CTR_DATETIME) == 0 ) pControl = new CDateTimeUI;
  303. else if( _tcsicmp(pstrClass, DUI_CTR_TREEVIEW) == 0 ) pControl = new CTreeViewUI;
  304. else if( _tcsicmp(pstrClass, DUI_CTR_TREENODE) == 0 ) pControl = new CTreeNodeUI;
  305. break;
  306. case 9:
  307. if( _tcsicmp(pstrClass, DUI_CTR_CONTAINER) == 0 ) pControl = new CContainerUI;
  308. else if( _tcsicmp(pstrClass, DUI_CTR_TABLAYOUT) == 0 ) pControl = new CTabLayoutUI;
  309. else if( _tcsicmp(pstrClass, DUI_CTR_SCROLLBAR) == 0 ) pControl = new CScrollBarUI;
  310. break;
  311. case 10:
  312. if( _tcsicmp(pstrClass, DUI_CTR_LISTHEADER) == 0 ) pControl = new CListHeaderUI;
  313. else if( _tcsicmp(pstrClass, DUI_CTR_TILELAYOUT) == 0 ) pControl = new CTileLayoutUI;
  314. else if( _tcsicmp(pstrClass, DUI_CTR_WEBBROWSER) == 0 ) pControl = new CWebBrowserUI;
  315. break;
  316. case 11:
  317. if (_tcsicmp(pstrClass, DUI_CTR_CHILDLAYOUT) == 0) pControl = new CChildLayoutUI;
  318. break;
  319. case 14:
  320. if( _tcsicmp(pstrClass, DUI_CTR_VERTICALLAYOUT) == 0 ) pControl = new CVerticalLayoutUI;
  321. else if( _tcsicmp(pstrClass, DUI_CTR_LISTHEADERITEM) == 0 ) pControl = new CListHeaderItemUI;
  322. break;
  323. case 15:
  324. if( _tcsicmp(pstrClass, DUI_CTR_LISTTEXTELEMENT) == 0 ) pControl = new CListTextElementUI;
  325. else if( _tcsicmp(pstrClass, DUI_CTR_LISTHBOXELEMENT) == 0 ) pControl = new CListHBoxElementUI;
  326. break;
  327. case 16:
  328. if( _tcsicmp(pstrClass, DUI_CTR_HORIZONTALLAYOUT) == 0 ) pControl = new CHorizontalLayoutUI;
  329. else if( _tcsicmp(pstrClass, DUI_CTR_LISTLABELELEMENT) == 0 ) pControl = new CListLabelElementUI;
  330. break;
  331. case 20:
  332. if( _tcsicmp(pstrClass, DUI_CTR_LISTCONTAINERELEMENT) == 0 ) pControl = new CListContainerElementUI;
  333. break;
  334. }
  335. // User-supplied control factory
  336. if( pControl == NULL ) {
  337. CDuiPtrArray* pPlugins = CPaintManagerUI::GetPlugins();
  338. LPCREATECONTROL lpCreateControl = NULL;
  339. for( int i = 0; i < pPlugins->GetSize(); ++i ) {
  340. lpCreateControl = (LPCREATECONTROL)pPlugins->GetAt(i);
  341. if( lpCreateControl != NULL ) {
  342. pControl = lpCreateControl(pstrClass);
  343. if( pControl != NULL ) break;
  344. }
  345. }
  346. }
  347. if( pControl == NULL && m_pCallback != NULL ) {
  348. pControl = m_pCallback->CreateControl(pstrClass);
  349. }
  350. }
  351. #ifndef _DEBUG
  352. ASSERT(pControl);
  353. #endif // _DEBUG
  354. if( pControl == NULL )
  355. {
  356. #ifdef _DEBUG
  357. DUITRACE(_T("Unknow Control:%s"),pstrClass);
  358. #endif
  359. continue;
  360. }
  361. // Add children
  362. if( node.HasChildren() ) {
  363. _Parse(&node, pControl, pManager);
  364. }
  365. TCHAR szValue[256] = { 0 };
  366. int cchLen = lengthof(szValue) - 1;
  367. // Attach to parent
  368. // 因为某些属性和父窗口相关,比如selected,必须先Add到父窗口
  369. if( pParent != NULL ) {
  370. LPCTSTR lpValue = szValue;
  371. if( node.GetAttributeValue(_T("cover"), szValue, cchLen) && _tcscmp(lpValue, _T("true")) == 0 ) {
  372. pParent->SetCover(pControl);
  373. }
  374. else {
  375. CTreeNodeUI* pContainerNode = static_cast<CTreeNodeUI*>(pParent->GetInterface(DUI_CTR_TREENODE));
  376. if(pContainerNode)
  377. pContainerNode->GetTreeNodeHoriznotal()->Add(pControl);
  378. else
  379. {
  380. if( pContainer == NULL ) pContainer = static_cast<IContainerUI*>(pParent->GetInterface(DUI_CTR_ICONTAINER));
  381. ASSERT(pContainer);
  382. if( pContainer == NULL ) return NULL;
  383. if( !pContainer->Add(pControl) ) {
  384. pControl->Delete();
  385. continue;
  386. }
  387. }
  388. }
  389. }
  390. // Init default attributes
  391. if( pManager ) {
  392. pControl->SetManager(pManager, NULL, false);
  393. LPCTSTR pDefaultAttributes = pManager->GetDefaultAttributeList(pstrClass);
  394. if( pDefaultAttributes ) {
  395. pControl->SetAttributeList(pDefaultAttributes);
  396. }
  397. }
  398. // Process attributes
  399. if( node.HasAttributes() ) {
  400. // Set ordinary attributes
  401. int nAttributes = node.GetAttributeCount();
  402. for( int i = 0; i < nAttributes; i++ ) {
  403. pControl->SetAttribute(node.GetAttributeName(i), node.GetAttributeValue(i));
  404. }
  405. }
  406. if( pManager ) {
  407. pControl->SetManager(NULL, NULL, false);
  408. }
  409. // Return first item
  410. if( pReturn == NULL ) pReturn = pControl;
  411. }
  412. return pReturn;
  413. }
  414. } // namespace DuiLib