BuildDialog.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. #include "StdAfx.h"
  2. #include "BuildDialog.h"
  3. #include "XmlManager.h"
  4. #include "SkinButton.h"
  5. #include "SkinComboBox.h"
  6. #include "SkinDialog.h"
  7. #include "SkinEdit.h"
  8. #include "SkinListBox.h"
  9. #include "SkinSliderCtrl.h"
  10. #include "SkinProgressCtrl.h"
  11. #include "SkinListCtrl.h"
  12. #include "SkinTreeCtrl.h"
  13. #include "SkinTabCtrl.h"
  14. #include "SkinRichEdit.h"
  15. #include "SkinMenu.h"
  16. #include "FlashControl.h"
  17. #include "SkinToolBar.h"
  18. CBuildDialog::CBuildDialog(void)
  19. {
  20. m_hOwnWnd = NULL;
  21. m_szWindowSize.SetSize(0,0);
  22. m_szRcSize.SetSize(0,0);
  23. m_szMinSize.SetSize(0,0);
  24. m_szMaxSize.SetSize(0,0);
  25. m_bExtrude = false;
  26. m_bClip = false;
  27. m_colBK = 0;
  28. m_bDefaultSkin = false;
  29. m_pXml = new CXmlManager;
  30. }
  31. CBuildDialog::~CBuildDialog(void)
  32. {
  33. SafeDelete(m_pXml);
  34. for (int i=0;i<(int)m_ControlArray.size();i++)
  35. {
  36. SafeDelete(m_ControlArray[i]);
  37. }
  38. m_ControlArray.clear();
  39. //////////////////////////////////////////////////////////////////////////
  40. for (int i = 0; i < (int)m_StringArray.size(); i++)
  41. {
  42. tagString * pStatic = m_StringArray[i];
  43. SafeDelete(pStatic->pPositionData);
  44. SafeDelete(pStatic);
  45. }
  46. m_StringArray.clear();
  47. //////////////////////////////////////////////////////////////////////////
  48. for (int i = 0; i < (int)m_ImageArray.size(); i++)
  49. {
  50. tagImage * pImageData = m_ImageArray[i];
  51. SafeDelete(pImageData->pPositionData);
  52. RenderEngine->RemoveImage(pImageData->pImage);
  53. SafeDelete(pImageData);
  54. }
  55. m_ImageArray.clear();
  56. }
  57. bool CBuildDialog::ParseWindow( LPCTSTR lpszFileName )
  58. {
  59. LPCTSTR lpszFilePath = lpszFileName;
  60. CFileFind fd;
  61. if( fd.FindFile(lpszFileName) == FALSE )
  62. {
  63. TCHAR szWorkDirectory[MAX_PATH]={0};
  64. RenderEngine->GetWorkDirectory(szWorkDirectory,MAX_PATH);
  65. StrCat(szWorkDirectory,TEXT("\\"));
  66. StrCat(szWorkDirectory,lpszFileName);
  67. if( fd.FindFile(szWorkDirectory) == FALSE )
  68. {
  69. return false;
  70. }
  71. lpszFilePath = szWorkDirectory;
  72. }
  73. if( !m_pXml->LoadFromFile(lpszFilePath) )
  74. return false;
  75. CXmlNode root = m_pXml->GetRoot();
  76. if( !root.IsValid() ) return false;
  77. LPCTSTR pstrClass = root.GetName();;
  78. int nAttributes = 0;
  79. LPCTSTR pstrName = NULL;
  80. LPCTSTR pstrValue = NULL;
  81. LPTSTR pstr = NULL;
  82. if( _tcscmp(pstrClass, _T("Window")) == 0 )
  83. {
  84. int nAttributes = root.GetAttributeCount();
  85. for( int i = 0; i < nAttributes; i++ )
  86. {
  87. pstrName = root.GetAttributeName(i);
  88. pstrValue = root.GetAttributeValue(i);
  89. if( _tcscmp(pstrName, _T("size")) == 0 )
  90. {
  91. LPTSTR pstr = NULL;
  92. m_szWindowSize.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  93. m_szWindowSize.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  94. }
  95. else if( _tcscmp(pstrName, _T("roundcorner")) == 0 )
  96. {
  97. LPTSTR pstr = NULL;
  98. m_szRcSize.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  99. m_szRcSize.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  100. }
  101. else if( _tcscmp(pstrName, _T("mininfo")) == 0 )
  102. {
  103. LPTSTR pstr = NULL;
  104. m_szMinSize.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  105. m_szMinSize.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  106. }
  107. else if( _tcscmp(pstrName, _T("maxinfo")) == 0 )
  108. {
  109. LPTSTR pstr = NULL;
  110. m_szMaxSize.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  111. m_szMaxSize.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  112. }
  113. else if( _tcscmp(pstrName, _T("Resizing")) == 0 )
  114. {
  115. if ( _tcscmp(pstrValue, _T("true")) == 0 )
  116. {
  117. m_bExtrude = true;
  118. }
  119. }
  120. else if( _tcscmp(pstrName, _T("clip")) == 0 )
  121. {
  122. if ( _tcscmp(pstrValue, _T("true")) == 0 )
  123. {
  124. m_bClip = true;
  125. }
  126. }
  127. else if( _tcscmp(pstrName, _T("bkcolor")) == 0 )
  128. {
  129. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  130. m_colBK = _tcstoul(pstrValue, &pstr, 16);
  131. }
  132. else if( _tcscmp(pstrName, _T("default")) == 0 )
  133. {
  134. m_bDefaultSkin = _tcscmp(pstrValue, _T("true")) == 0;
  135. }
  136. }
  137. }
  138. return ParseGraphics();
  139. }
  140. bool CBuildDialog::ParseGraphics()
  141. {
  142. CXmlNode root = m_pXml->GetRoot();
  143. if( !root.IsValid() ) return false;
  144. LPCTSTR pstrName = NULL;
  145. LPCTSTR pstrValue = NULL;
  146. LPCTSTR pstrClass = NULL;
  147. int nAttributes = 0;
  148. LPTSTR pstr = NULL;
  149. for( CXmlNode node = root.GetChild() ; node.IsValid(); node = node.GetSibling() )
  150. {
  151. pstrClass = node.GetName();
  152. if( _tcscmp(pstrClass, _T("Font")) == 0 )
  153. {
  154. nAttributes = node.GetAttributeCount();
  155. LPCTSTR pFontName = NULL;
  156. int size = 12;
  157. bool bold = false;
  158. bool underline = false;
  159. bool italic = false;
  160. bool defaultfont = false;
  161. for( int i = 0; i < nAttributes; i++ )
  162. {
  163. pstrName = node.GetAttributeName(i);
  164. pstrValue = node.GetAttributeValue(i);
  165. if( _tcscmp(pstrName, _T("name")) == 0 )
  166. {
  167. pFontName = pstrValue;
  168. }
  169. else if( _tcscmp(pstrName, _T("size")) == 0 )
  170. {
  171. size = _tcstol(pstrValue, &pstr, 10);
  172. }
  173. else if( _tcscmp(pstrName, _T("bold")) == 0 )
  174. {
  175. bold = (_tcscmp(pstrValue, _T("true")) == 0);
  176. }
  177. else if( _tcscmp(pstrName, _T("underline")) == 0 )
  178. {
  179. underline = (_tcscmp(pstrValue, _T("true")) == 0);
  180. }
  181. else if( _tcscmp(pstrName, _T("italic")) == 0 )
  182. {
  183. italic = (_tcscmp(pstrValue, _T("true")) == 0);
  184. }
  185. else if( _tcscmp(pstrName, _T("default")) == 0 )
  186. {
  187. defaultfont = (_tcscmp(pstrValue, _T("true")) == 0);
  188. }
  189. }
  190. if ( defaultfont ) RenderEngine->SetDefaultFont(pFontName,size,bold,underline,italic);
  191. else RenderEngine->AddFont(pFontName,size,bold,underline,italic);
  192. }
  193. else if( _tcscmp(pstrClass, _T("String")) == 0 )
  194. {
  195. nAttributes = node.GetAttributeCount();
  196. LPCTSTR pText = NULL;
  197. UINT nFontIndex = NULL;
  198. COLORREF color = 0;
  199. tagPositionData *pPositionData = new tagPositionData;
  200. UINT uID = 0;
  201. UINT uFormat = DT_LEFT | DT_TOP | DT_SINGLELINE;
  202. bool bVisible = true;
  203. for( int i = 0; i < nAttributes; i++ )
  204. {
  205. pstrName = node.GetAttributeName(i);
  206. pstrValue = node.GetAttributeValue(i);
  207. if( _tcscmp(pstrName, _T("text")) == 0 )
  208. {
  209. pText = pstrValue;
  210. }
  211. else if( _tcscmp(pstrName, _T("font")) == 0 )
  212. {
  213. nFontIndex = _ttoi(pstrValue);
  214. }
  215. else if( _tcscmp(pstrName, _T("visible")) == 0 )
  216. {
  217. bVisible = _tcscmp(pstrName, _T("false")) == 0;
  218. }
  219. else if( _tcscmp(pstrName, _T("format")) == 0 )
  220. {
  221. uFormat = _ttoi(pstrValue);
  222. }
  223. else if( _tcscmp(pstrName, _T("color")) == 0 )
  224. {
  225. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  226. color = _tcstoul(pstrValue, &pstr, 16);
  227. }
  228. else if( _tcscmp(pstrName, _T("pos")) == 0 )
  229. {
  230. pPositionData->nFixedPostion[0] = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  231. pPositionData->nFixedPostion[1] = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  232. pPositionData->ptPosition.x = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  233. pPositionData->ptPosition.y = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  234. pPositionData->szSize.cx = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  235. pPositionData->szSize.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  236. }
  237. else if( _tcscmp(pstrName, _T("id")) == 0 )
  238. {
  239. uID = _ttoi(pstrValue);
  240. }
  241. }
  242. AddString(pText, nFontIndex, uFormat,color,bVisible,pPositionData,uID);
  243. }
  244. else if( _tcscmp(pstrClass, _T("Image")) == 0 )
  245. {
  246. nAttributes = node.GetAttributeCount();
  247. LPCTSTR pPath = NULL;
  248. BYTE cbAlpha = 255;
  249. DWORD dwMask = 0;
  250. int nType = 0;
  251. int nRotation = 0;
  252. bool bGray = false;
  253. bool bVisible = true;
  254. tagPositionData *pPositionData = new tagPositionData;
  255. UINT uID = 0;
  256. for( int i = 0; i < nAttributes; i++ )
  257. {
  258. pstrName = node.GetAttributeName(i);
  259. pstrValue = node.GetAttributeValue(i);
  260. if( _tcscmp(pstrName, _T("path")) == 0 )
  261. {
  262. pPath = pstrValue;
  263. }
  264. else if( _tcscmp(pstrName, _T("alpha")) == 0 )
  265. {
  266. cbAlpha = _ttoi(pstrValue);
  267. }
  268. else if( _tcscmp(pstrName, _T("gray")) == 0 )
  269. {
  270. bGray = _tcscmp(pstrValue, _T("true")) == 0;
  271. }
  272. else if( _tcscmp(pstrName, _T("mask")) == 0 )
  273. {
  274. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  275. dwMask = _tcstoul(pstrValue, &pstr, 16);
  276. }
  277. else if( _tcscmp(pstrName, _T("pos")) == 0 )
  278. {
  279. pPositionData->nFixedPostion[0] = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  280. pPositionData->nFixedPostion[1] = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  281. pPositionData->ptPosition.x = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  282. pPositionData->ptPosition.y = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  283. pPositionData->szSize.cx = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  284. pPositionData->szSize.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  285. }
  286. else if( _tcscmp(pstrName, _T("id")) == 0 )
  287. {
  288. uID = _ttoi(pstrValue);
  289. }
  290. else if( _tcscmp(pstrName, _T("type")) == 0 )
  291. {
  292. nType = _ttoi(pstrValue);
  293. }
  294. else if( _tcscmp(pstrName, _T("rotation")) == 0 )
  295. {
  296. nRotation = _ttoi(pstrValue);
  297. }
  298. else if( _tcscmp(pstrName, _T("visible")) == 0 )
  299. {
  300. bVisible = _tcscmp(pstrName, _T("false")) == 0;
  301. }
  302. }
  303. AddImage(pPath,cbAlpha,dwMask,nType,nRotation,bGray,bVisible,pPositionData,uID);
  304. }
  305. }
  306. return true;
  307. }
  308. bool CBuildDialog::ParseControl(CWnd* pParentWnd)
  309. {
  310. CXmlNode root = m_pXml->GetRoot();
  311. if( !root.IsValid() ) return false;
  312. LPCTSTR pstrClass = NULL;
  313. ISkinControl*pControl = NULL;
  314. for( CXmlNode node = root.GetChild() ; node.IsValid(); node = node.GetSibling() )
  315. {
  316. pstrClass = node.GetName();
  317. if( _tcscmp(pstrClass, _T("Button")) == 0 ) pControl = new CSkinButton;
  318. else if( _tcscmp(pstrClass, _T("ComBox")) == 0 ) pControl = new CSkinComboBox;
  319. else if( _tcscmp(pstrClass, _T("Edit")) == 0 ) pControl = new CSkinEdit;
  320. else if( _tcscmp(pstrClass, _T("MulitiEdit")) == 0 ) pControl = new CMultiSkinEdit;
  321. else if( _tcscmp(pstrClass, _T("ListBox")) == 0 ) pControl = new CSkinListBox;
  322. else if( _tcscmp(pstrClass, _T("ListCtrl")) == 0 ) pControl = new CSkinListCtrl;
  323. else if( _tcscmp(pstrClass, _T("Progress")) == 0 ) pControl = new CSkinProgressCtrl;
  324. else if( _tcscmp(pstrClass, _T("RichEdit")) == 0 ) pControl = new CSkinRichEdit;
  325. else if( _tcscmp(pstrClass, _T("Slider")) == 0 ) pControl = new CSkinSliderCtrl;
  326. else if( _tcscmp(pstrClass, _T("TabCtrl")) == 0 ) pControl = new CSkinTabCtrl;
  327. else if( _tcscmp(pstrClass, _T("ToolBar")) == 0 ) pControl = new CSkinToolBar;
  328. else if( _tcscmp(pstrClass, _T("TreeCtrl")) == 0 ) pControl = new CSkinTreeCtrl;
  329. else if( _tcscmp(pstrClass, _T("Flash")) == 0 ) pControl = new CFlashControl;
  330. //用户自己重绘其他控件
  331. else if( !CreateUserControl() ) continue;
  332. //控件创建失败,跳过
  333. if( !pControl->CreateControl(pParentWnd) ) continue;
  334. //入栈
  335. m_ControlArray.push_back(pControl);
  336. //解析属性
  337. if( node.HasAttributes() )
  338. {
  339. int nAttributes = node.GetAttributeCount();
  340. for( int i = 0; i < nAttributes; i++ )
  341. {
  342. pControl->SetAttribute(node.GetAttributeName(i), node.GetAttributeValue(i));
  343. }
  344. }
  345. //解析子节点
  346. if( node.HasChildren() )
  347. {
  348. for( CXmlNode child = node.GetChild() ; child.IsValid(); child = child.GetSibling() )
  349. {
  350. pstrClass = child.GetName();
  351. if( child.HasAttributes() )
  352. {
  353. pControl->ParseItem(&child);
  354. }
  355. }
  356. }
  357. }
  358. return true;
  359. }
  360. void CBuildDialog::DrawString(CDC*pDC)
  361. {
  362. tagString *pStatic = NULL;
  363. for (int i=0;i<(int)m_StringArray.size();i++)
  364. {
  365. pStatic = m_StringArray[i];
  366. if ( NULL != pStatic )
  367. {
  368. if( !pStatic->bVisible ) continue;
  369. int nMode = pDC->SetBkMode(TRANSPARENT);
  370. pDC->SelectObject(RenderEngine->GetFont(pStatic->uFontIndex-1));
  371. pDC->SetTextColor(pStatic->colStatic);
  372. CRect rcBounder;
  373. RenderEngine->GetRect(m_hOwnWnd,pStatic->pPositionData,rcBounder);
  374. pDC->DrawText(pStatic->strText, rcBounder, pStatic->uFormat);
  375. pDC->SetBkMode(nMode);
  376. }
  377. }
  378. }
  379. int CBuildDialog::AddString(LPCTSTR lpszText, UINT uFontIndex,UINT uFormat,COLORREF col,bool bVisible,tagPositionData *pPositionData,UINT uID )
  380. {
  381. if ( GetString(uID) != NULL ) return -1;
  382. tagString*pStatic = new tagString;
  383. if (NULL == pStatic) return -1;
  384. pStatic->uFontIndex = uFontIndex;
  385. pStatic->colStatic = col;
  386. pStatic->pPositionData = pPositionData;
  387. pStatic->uID = uID;
  388. pStatic->strText = lpszText;
  389. pStatic->uFormat = uFormat;
  390. pStatic->bVisible = bVisible;
  391. m_StringArray.push_back(pStatic);
  392. return m_StringArray.size() - 1;
  393. }
  394. tagPositionData * CBuildDialog::GetStringRect( UINT uID )
  395. {
  396. tagString*pStatic = NULL;
  397. static tagPositionData _PositionData={ 0 };
  398. for (int i=0;i<(int)m_StringArray.size();i++)
  399. {
  400. pStatic = m_StringArray[i];
  401. if ( NULL != pStatic )
  402. {
  403. if ( pStatic->uID == uID )
  404. {
  405. return pStatic->pPositionData;
  406. }
  407. }
  408. }
  409. return &_PositionData;
  410. }
  411. tagString * CBuildDialog::GetString( UINT uID )
  412. {
  413. for (int i=0;i<(int)m_StringArray.size();i++)
  414. {
  415. if( m_StringArray[i]->uID == uID )
  416. return m_StringArray[i];
  417. }
  418. return NULL;
  419. }
  420. int CBuildDialog::AddImage( LPCTSTR lpszFileName,BYTE dwAlpha,DWORD dwMask,int nType,int nRotation,bool bGray,bool bVisible,tagPositionData *pPositionData,UINT uID )
  421. {
  422. if ( GetImage(uID) != NULL ) return -1;
  423. tagImage*pImageData = new tagImage;
  424. if (NULL == pImageData) return -1;
  425. pImageData->pImage = RenderEngine->GetImage(lpszFileName);
  426. pImageData->cbAlpha = dwAlpha;
  427. pImageData->dwMask = dwMask;
  428. pImageData->nType = nType;
  429. pImageData->nRotation = nRotation;
  430. pImageData->pPositionData = pPositionData;
  431. pImageData->uID = uID;
  432. pImageData->bGray = bGray;
  433. pImageData->bVisible = bVisible;
  434. pImageData->pImage->SetRotation(nRotation);
  435. pImageData->pImage->SetAlpha(dwAlpha);
  436. if(bGray) pImageData->pImage->SetGray();
  437. m_ImageArray.push_back(pImageData);
  438. return m_ImageArray.size() - 1;
  439. }
  440. tagPositionData * CBuildDialog::GetImageRect( UINT uID )
  441. {
  442. tagImage*pImageData = NULL;
  443. static tagPositionData _PositionData={ 0 };
  444. for (int i=0;i<(int)m_ImageArray.size();i++)
  445. {
  446. pImageData = m_ImageArray[i];
  447. if ( NULL != pImageData )
  448. {
  449. if ( pImageData->uID == uID )
  450. {
  451. return pImageData->pPositionData;
  452. }
  453. }
  454. }
  455. return &_PositionData;
  456. }
  457. tagImage * CBuildDialog::GetImage( UINT uID )
  458. {
  459. for (int i=0;i<(int)m_ImageArray.size();i++)
  460. {
  461. if( m_ImageArray[i]->uID == uID )
  462. return m_ImageArray[i];
  463. }
  464. return NULL;
  465. }
  466. void CBuildDialog::DrawImage( CDC*pDC )
  467. {
  468. tagImage *pImageData = NULL;
  469. for (int i=0;i<(int)m_ImageArray.size();i++)
  470. {
  471. pImageData = m_ImageArray[i];
  472. if ( NULL != pImageData )
  473. {
  474. if( !pImageData->bVisible ) continue;
  475. CRect rcBounder;
  476. RenderEngine->GetRect(m_hOwnWnd,pImageData->pPositionData,rcBounder);
  477. pImageData->pImage->DrawImage(pDC,rcBounder);
  478. }
  479. }
  480. }