UIControl.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  1. #include "StdAfx.h"
  2. namespace DuiLib
  3. {
  4. IMPLEMENT_DUICONTROL(CControlUI)
  5. CControlUI::CControlUI()
  6. :m_pManager(NULL),
  7. m_pParent(NULL),
  8. m_bUpdateNeeded(true),
  9. m_bMenuUsed(false),
  10. m_bVisible(true),
  11. m_bInternVisible(true),
  12. m_bFocused(false),
  13. m_bEnabled(true),
  14. m_bMouseEnabled(true),
  15. m_bKeyboardEnabled(true),
  16. m_bFloat(false),
  17. m_uFloatAlign(0),
  18. m_bSetPos(false),
  19. m_bRichEvent(false),
  20. m_bDragEnabled(false),
  21. m_bDropEnabled(false),
  22. m_bResourceText(false),
  23. m_chShortcut('\0'),
  24. m_pTag(NULL),
  25. m_dwBackColor(0),
  26. m_dwBackColor2(0),
  27. m_dwBackColor3(0),
  28. m_dwForeColor(0),
  29. m_dwBorderColor(0),
  30. m_dwFocusBorderColor(0),
  31. m_bColorHSL(false),
  32. m_nBorderSize(0),
  33. m_nBorderStyle(PS_SOLID),
  34. m_nTooltipWidth(300),
  35. m_wCursor(0),
  36. m_instance(NULL)
  37. {
  38. m_cXY.cx = m_cXY.cy = 0;
  39. m_cxyFixed.cx = m_cxyFixed.cy = 0;
  40. m_cxyMin.cx = m_cxyMin.cy = 0;
  41. m_cxyMax.cx = m_cxyMax.cy = 9999;
  42. m_cxyBorderRound.cx = m_cxyBorderRound.cy = 0;
  43. ::ZeroMemory(&m_rcPadding, sizeof(RECT));
  44. ::ZeroMemory(&m_rcItem, sizeof(RECT));
  45. ::ZeroMemory(&m_rcPaint, sizeof(RECT));
  46. ::ZeroMemory(&m_rcBorderSize, sizeof(RECT));
  47. m_piFloatPercent.left = m_piFloatPercent.top = m_piFloatPercent.right = m_piFloatPercent.bottom = 0.0f;
  48. }
  49. CControlUI::~CControlUI()
  50. {
  51. if (OnDestroy) OnDestroy(this);
  52. RemoveAllCustomAttribute();
  53. if (m_pManager != NULL) m_pManager->ReapObjects(this);
  54. }
  55. CDuiString CControlUI::GetName() const
  56. {
  57. return m_sName;
  58. }
  59. void CControlUI::SetName(LPCTSTR pstrName)
  60. {
  61. m_sName = pstrName;
  62. }
  63. LPVOID CControlUI::GetInterface(LPCTSTR pstrName)
  64. {
  65. if (_tcsicmp(pstrName, DUI_CTR_CONTROL) == 0) return this;
  66. return NULL;
  67. }
  68. LPCTSTR CControlUI::GetClass() const
  69. {
  70. return _T("ControlUI");
  71. }
  72. UINT CControlUI::GetControlFlags() const
  73. {
  74. return 0;
  75. }
  76. bool CControlUI::Activate()
  77. {
  78. if (!IsVisible()) return false;
  79. if (!IsEnabled()) return false;
  80. return true;
  81. }
  82. CPaintManagerUI* CControlUI::GetManager() const
  83. {
  84. return m_pManager;
  85. }
  86. void CControlUI::SetManager(CPaintManagerUI* pManager, CControlUI* pParent, bool bInit)
  87. {
  88. m_pManager = pManager;
  89. m_pParent = pParent;
  90. if (bInit && m_pParent) Init();
  91. }
  92. CControlUI* CControlUI::GetParent() const
  93. {
  94. return m_pParent;
  95. }
  96. bool CControlUI::SetTimer(UINT nTimerID, UINT nElapse)
  97. {
  98. if (m_pManager == NULL) return false;
  99. return m_pManager->SetTimer(this, nTimerID, nElapse);
  100. }
  101. void CControlUI::KillTimer(UINT nTimerID)
  102. {
  103. if (m_pManager == NULL) return;
  104. m_pManager->KillTimer(this, nTimerID);
  105. }
  106. CDuiString CControlUI::GetText() const
  107. {
  108. if (!IsResourceText()) return m_sText;
  109. return CResourceManager::GetInstance()->GetText(m_sText);
  110. }
  111. void CControlUI::SetText(LPCTSTR pstrText)
  112. {
  113. if (m_sText == pstrText) return;
  114. m_sText = pstrText;
  115. // 解析xml换行符
  116. m_sText.Replace(_T("{\\n}"), _T("\n"));
  117. Invalidate();
  118. }
  119. bool CControlUI::IsResourceText() const
  120. {
  121. return m_bResourceText;
  122. }
  123. void CControlUI::SetResourceText(bool bResource)
  124. {
  125. if (m_bResourceText == bResource) return;
  126. m_bResourceText = bResource;
  127. Invalidate();
  128. }
  129. bool CControlUI::IsDragEnabled() const
  130. {
  131. return m_bDragEnabled;
  132. }
  133. void CControlUI::SetDragEnable(bool bDrag)
  134. {
  135. m_bDragEnabled = bDrag;
  136. }
  137. bool CControlUI::IsDropEnabled() const
  138. {
  139. return m_bDropEnabled;
  140. }
  141. void CControlUI::SetDropEnable(bool bDrop)
  142. {
  143. m_bDropEnabled = bDrop;
  144. }
  145. bool CControlUI::IsRichEvent() const
  146. {
  147. return m_bRichEvent;
  148. }
  149. void CControlUI::SetRichEvent(bool bEnable)
  150. {
  151. m_bRichEvent = bEnable;
  152. }
  153. LPCTSTR CControlUI::GetGradient()
  154. {
  155. return m_sGradient;
  156. }
  157. void CControlUI::SetGradient(LPCTSTR pStrImage)
  158. {
  159. if (m_sGradient == pStrImage) return;
  160. m_sGradient = pStrImage;
  161. Invalidate();
  162. }
  163. DWORD CControlUI::GetBkColor() const
  164. {
  165. return m_dwBackColor;
  166. }
  167. void CControlUI::SetBkColor(DWORD dwBackColor)
  168. {
  169. if (m_dwBackColor == dwBackColor) return;
  170. m_dwBackColor = dwBackColor;
  171. Invalidate();
  172. }
  173. DWORD CControlUI::GetBkColor2() const
  174. {
  175. return m_dwBackColor2;
  176. }
  177. void CControlUI::SetBkColor2(DWORD dwBackColor)
  178. {
  179. if (m_dwBackColor2 == dwBackColor) return;
  180. m_dwBackColor2 = dwBackColor;
  181. Invalidate();
  182. }
  183. DWORD CControlUI::GetBkColor3() const
  184. {
  185. return m_dwBackColor3;
  186. }
  187. void CControlUI::SetBkColor3(DWORD dwBackColor)
  188. {
  189. if (m_dwBackColor3 == dwBackColor) return;
  190. m_dwBackColor3 = dwBackColor;
  191. Invalidate();
  192. }
  193. DWORD CControlUI::GetForeColor() const
  194. {
  195. return m_dwForeColor;
  196. }
  197. void CControlUI::SetForeColor(DWORD dwForeColor)
  198. {
  199. if (m_dwForeColor == dwForeColor) return;
  200. m_dwForeColor = dwForeColor;
  201. Invalidate();
  202. }
  203. LPCTSTR CControlUI::GetBkImage()
  204. {
  205. return m_sBkImage;
  206. }
  207. void CControlUI::SetBkImage(LPCTSTR pStrImage)
  208. {
  209. if (m_sBkImage == pStrImage) return;
  210. m_sBkImage = pStrImage;
  211. Invalidate();
  212. }
  213. LPCTSTR CControlUI::GetForeImage() const
  214. {
  215. return m_sForeImage;
  216. }
  217. void CControlUI::SetForeImage(LPCTSTR pStrImage)
  218. {
  219. if (m_sForeImage == pStrImage) return;
  220. m_sForeImage = pStrImage;
  221. Invalidate();
  222. }
  223. DWORD CControlUI::GetBorderColor() const
  224. {
  225. return m_dwBorderColor;
  226. }
  227. void CControlUI::SetBorderColor(DWORD dwBorderColor)
  228. {
  229. if (m_dwBorderColor == dwBorderColor) return;
  230. m_dwBorderColor = dwBorderColor;
  231. Invalidate();
  232. }
  233. DWORD CControlUI::GetFocusBorderColor() const
  234. {
  235. return m_dwFocusBorderColor;
  236. }
  237. void CControlUI::SetFocusBorderColor(DWORD dwBorderColor)
  238. {
  239. if (m_dwFocusBorderColor == dwBorderColor) return;
  240. m_dwFocusBorderColor = dwBorderColor;
  241. Invalidate();
  242. }
  243. bool CControlUI::IsColorHSL() const
  244. {
  245. return m_bColorHSL;
  246. }
  247. void CControlUI::SetColorHSL(bool bColorHSL)
  248. {
  249. if (m_bColorHSL == bColorHSL) return;
  250. m_bColorHSL = bColorHSL;
  251. Invalidate();
  252. }
  253. int CControlUI::GetBorderSize() const
  254. {
  255. if (m_pManager != NULL) return m_pManager->GetDPIObj()->Scale(m_nBorderSize);
  256. return m_nBorderSize;
  257. }
  258. void CControlUI::SetBorderSize(int nSize)
  259. {
  260. if (m_nBorderSize == nSize) return;
  261. m_nBorderSize = nSize;
  262. Invalidate();
  263. }
  264. RECT CControlUI::GetBorderRectSize() const
  265. {
  266. RECT rcBorderSize = m_rcBorderSize;
  267. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  268. return rcBorderSize;
  269. }
  270. void CControlUI::SetBorderSize(RECT rc)
  271. {
  272. m_rcBorderSize = rc;
  273. Invalidate();
  274. }
  275. SIZE CControlUI::GetBorderRound() const
  276. {
  277. SIZE cxyBorderRound = m_cxyBorderRound;
  278. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyBorderRound);
  279. return cxyBorderRound;
  280. }
  281. void CControlUI::SetBorderRound(SIZE cxyRound)
  282. {
  283. m_cxyBorderRound = cxyRound;
  284. Invalidate();
  285. }
  286. bool CControlUI::DrawImage(HDC hDC, LPCTSTR pStrImage, LPCTSTR pStrModify)
  287. {
  288. return CRenderEngine::DrawImageString(hDC, m_pManager, m_rcItem, m_rcPaint, pStrImage, pStrModify, m_instance);
  289. }
  290. const RECT& CControlUI::GetPos() const
  291. {
  292. return m_rcItem;
  293. }
  294. RECT CControlUI::GetRelativePos() const
  295. {
  296. CControlUI* pParent = GetParent();
  297. if (pParent != NULL)
  298. {
  299. RECT rcParentPos = pParent->GetPos();
  300. CDuiRect rcRelativePos(m_rcItem);
  301. rcRelativePos.Offset(-rcParentPos.left, -rcParentPos.top);
  302. return rcRelativePos;
  303. }
  304. else
  305. {
  306. return CDuiRect(0, 0, 0, 0);
  307. }
  308. }
  309. RECT CControlUI::GetClientPos() const
  310. {
  311. return m_rcItem;
  312. }
  313. void CControlUI::SetPos(RECT rc, bool bNeedInvalidate)
  314. {
  315. if (rc.right < rc.left) rc.right = rc.left;
  316. if (rc.bottom < rc.top) rc.bottom = rc.top;
  317. CDuiRect invalidateRc = m_rcItem;
  318. if (::IsRectEmpty(&invalidateRc)) invalidateRc = rc;
  319. m_rcItem = rc;
  320. if (m_pManager == NULL) return;
  321. if (!m_bSetPos)
  322. {
  323. m_bSetPos = true;
  324. if (OnSize) OnSize(this);
  325. m_bSetPos = false;
  326. }
  327. m_bUpdateNeeded = false;
  328. if (bNeedInvalidate && IsVisible())
  329. {
  330. invalidateRc.Join(m_rcItem);
  331. CControlUI* pParent = this;
  332. RECT rcTemp;
  333. RECT rcParent;
  334. while (pParent = pParent->GetParent())
  335. {
  336. if (!pParent->IsVisible()) return;
  337. rcTemp = invalidateRc;
  338. rcParent = pParent->GetPos();
  339. if (!::IntersectRect(&invalidateRc, &rcTemp, &rcParent)) return;
  340. }
  341. m_pManager->Invalidate(invalidateRc);
  342. }
  343. }
  344. void CControlUI::Move(SIZE szOffset, bool bNeedInvalidate)
  345. {
  346. m_cXY.cx += szOffset.cx;
  347. m_cXY.cy += szOffset.cy;
  348. NeedParentUpdate();
  349. }
  350. int CControlUI::GetWidth() const
  351. {
  352. return m_rcItem.right - m_rcItem.left;
  353. }
  354. int CControlUI::GetHeight() const
  355. {
  356. return m_rcItem.bottom - m_rcItem.top;
  357. }
  358. int CControlUI::GetX() const
  359. {
  360. return m_rcItem.left;
  361. }
  362. int CControlUI::GetY() const
  363. {
  364. return m_rcItem.top;
  365. }
  366. RECT CControlUI::GetPadding() const
  367. {
  368. RECT rcPadding = m_rcPadding;
  369. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcPadding);
  370. return rcPadding;
  371. }
  372. void CControlUI::SetPadding(RECT rcPadding)
  373. {
  374. m_rcPadding = rcPadding;
  375. NeedParentUpdate();
  376. }
  377. SIZE CControlUI::GetFixedXY() const
  378. {
  379. SIZE cXY = m_cXY;
  380. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cXY);
  381. return cXY;
  382. }
  383. void CControlUI::SetFixedXY(SIZE szXY)
  384. {
  385. m_cXY.cx = szXY.cx;
  386. m_cXY.cy = szXY.cy;
  387. NeedParentUpdate();
  388. }
  389. SIZE CControlUI::GetFixedSize() const
  390. {
  391. SIZE cxyFixed = m_cxyFixed;
  392. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyFixed);
  393. return cxyFixed;
  394. }
  395. int CControlUI::GetFixedWidth() const
  396. {
  397. SIZE cxyFixed = m_cxyFixed;
  398. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyFixed);
  399. return cxyFixed.cx;
  400. }
  401. void CControlUI::SetFixedWidth(int cx)
  402. {
  403. if (cx < 0) return;
  404. m_cxyFixed.cx = cx;
  405. NeedParentUpdate();
  406. }
  407. int CControlUI::GetFixedHeight() const
  408. {
  409. SIZE cxyFixed = m_cxyFixed;
  410. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyFixed);
  411. return cxyFixed.cy;
  412. }
  413. void CControlUI::SetFixedHeight(int cy)
  414. {
  415. if (cy < 0) return;
  416. m_cxyFixed.cy = cy;
  417. NeedParentUpdate();
  418. }
  419. int CControlUI::GetMinWidth() const
  420. {
  421. SIZE cxyMin = m_cxyMin;
  422. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyMin);
  423. return cxyMin.cx;
  424. }
  425. void CControlUI::SetMinWidth(int cx)
  426. {
  427. if (m_cxyMin.cx == cx) return;
  428. if (cx < 0) return;
  429. m_cxyMin.cx = cx;
  430. NeedParentUpdate();
  431. }
  432. int CControlUI::GetMaxWidth() const
  433. {
  434. SIZE cxyMax = m_cxyMax;
  435. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyMax);
  436. return cxyMax.cx;
  437. }
  438. void CControlUI::SetMaxWidth(int cx)
  439. {
  440. if (m_cxyMax.cx == cx) return;
  441. if (cx < 0) return;
  442. m_cxyMax.cx = cx;
  443. NeedParentUpdate();
  444. }
  445. int CControlUI::GetMinHeight() const
  446. {
  447. SIZE cxyMin = m_cxyMin;
  448. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyMin);
  449. return cxyMin.cy;
  450. }
  451. void CControlUI::SetMinHeight(int cy)
  452. {
  453. if (m_cxyMin.cy == cy) return;
  454. if (cy < 0) return;
  455. m_cxyMin.cy = cy;
  456. NeedParentUpdate();
  457. }
  458. int CControlUI::GetMaxHeight() const
  459. {
  460. SIZE cxyMax = m_cxyMax;
  461. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&cxyMax);
  462. return cxyMax.cy;
  463. }
  464. void CControlUI::SetMaxHeight(int cy)
  465. {
  466. if (m_cxyMax.cy == cy) return;
  467. if (cy < 0) return;
  468. m_cxyMax.cy = cy;
  469. NeedParentUpdate();
  470. }
  471. TPercentInfo CControlUI::GetFloatPercent() const
  472. {
  473. return m_piFloatPercent;
  474. }
  475. void CControlUI::SetFloatPercent(TPercentInfo piFloatPercent)
  476. {
  477. m_piFloatPercent = piFloatPercent;
  478. NeedParentUpdate();
  479. }
  480. void CControlUI::SetFloatAlign(UINT uAlign)
  481. {
  482. m_uFloatAlign = uAlign;
  483. NeedParentUpdate();
  484. }
  485. UINT CControlUI::GetFloatAlign() const
  486. {
  487. return m_uFloatAlign;
  488. }
  489. CDuiString CControlUI::GetToolTip() const
  490. {
  491. if (!IsResourceText()) return m_sToolTip;
  492. return CResourceManager::GetInstance()->GetText(m_sToolTip);
  493. }
  494. void CControlUI::SetToolTip(LPCTSTR pstrText)
  495. {
  496. CDuiString strTemp(pstrText);
  497. strTemp.Replace(_T("<n>"), _T("\r\n"));
  498. m_sToolTip = strTemp;
  499. }
  500. void CControlUI::SetToolTipWidth(int nWidth)
  501. {
  502. m_nTooltipWidth = nWidth;
  503. }
  504. int CControlUI::GetToolTipWidth(void)
  505. {
  506. if (m_pManager != NULL) return m_pManager->GetDPIObj()->Scale(m_nTooltipWidth);
  507. return m_nTooltipWidth;
  508. }
  509. WORD CControlUI::GetCursor()
  510. {
  511. return m_wCursor;
  512. }
  513. void CControlUI::SetCursor(WORD wCursor)
  514. {
  515. m_wCursor = wCursor;
  516. Invalidate();
  517. }
  518. TCHAR CControlUI::GetShortcut() const
  519. {
  520. return m_chShortcut;
  521. }
  522. void CControlUI::SetShortcut(TCHAR ch)
  523. {
  524. m_chShortcut = ch;
  525. }
  526. bool CControlUI::IsContextMenuUsed() const
  527. {
  528. return m_bMenuUsed;
  529. }
  530. void CControlUI::SetContextMenuUsed(bool bMenuUsed)
  531. {
  532. m_bMenuUsed = bMenuUsed;
  533. }
  534. const CDuiString& CControlUI::GetUserData()
  535. {
  536. return m_sUserData;
  537. }
  538. void CControlUI::SetUserData(LPCTSTR pstrText)
  539. {
  540. m_sUserData = pstrText;
  541. }
  542. UINT_PTR CControlUI::GetTag() const
  543. {
  544. return m_pTag;
  545. }
  546. void CControlUI::SetTag(UINT_PTR pTag)
  547. {
  548. m_pTag = pTag;
  549. }
  550. bool CControlUI::IsVisible() const
  551. {
  552. return m_bVisible && m_bInternVisible;
  553. }
  554. void CControlUI::SetVisible(bool bVisible)
  555. {
  556. if (m_bVisible == bVisible) return;
  557. bool v = IsVisible();
  558. m_bVisible = bVisible;
  559. if (m_bFocused) m_bFocused = false;
  560. if (!bVisible && m_pManager && m_pManager->GetFocus() == this)
  561. {
  562. m_pManager->SetFocus(NULL);
  563. }
  564. if (IsVisible() != v)
  565. {
  566. NeedParentUpdate();
  567. }
  568. }
  569. void CControlUI::SetInternVisible(bool bVisible)
  570. {
  571. m_bInternVisible = bVisible;
  572. if (!bVisible && m_pManager && m_pManager->GetFocus() == this)
  573. {
  574. m_pManager->SetFocus(NULL);
  575. }
  576. }
  577. bool CControlUI::IsEnabled() const
  578. {
  579. return m_bEnabled;
  580. }
  581. void CControlUI::SetEnabled(bool bEnabled)
  582. {
  583. if (m_bEnabled == bEnabled) return;
  584. m_bEnabled = bEnabled;
  585. Invalidate();
  586. }
  587. bool CControlUI::IsMouseEnabled() const
  588. {
  589. return m_bMouseEnabled;
  590. }
  591. void CControlUI::SetMouseEnabled(bool bEnabled)
  592. {
  593. m_bMouseEnabled = bEnabled;
  594. }
  595. bool CControlUI::IsKeyboardEnabled() const
  596. {
  597. return m_bKeyboardEnabled;
  598. }
  599. void CControlUI::SetKeyboardEnabled(bool bEnabled)
  600. {
  601. m_bKeyboardEnabled = bEnabled;
  602. }
  603. bool CControlUI::IsFocused() const
  604. {
  605. return m_bFocused;
  606. }
  607. void CControlUI::SetFocus()
  608. {
  609. if (m_pManager != NULL) m_pManager->SetFocus(this);
  610. }
  611. bool CControlUI::IsFloat() const
  612. {
  613. return m_bFloat;
  614. }
  615. void CControlUI::SetFloat(bool bFloat)
  616. {
  617. if (m_bFloat == bFloat) return;
  618. m_bFloat = bFloat;
  619. NeedParentUpdate();
  620. }
  621. CControlUI* CControlUI::FindControl(FINDCONTROLPROC Proc, LPVOID pData, UINT uFlags)
  622. {
  623. if ((uFlags & UIFIND_VISIBLE) != 0 && !IsVisible()) return NULL;
  624. if ((uFlags & UIFIND_ENABLED) != 0 && !IsEnabled()) return NULL;
  625. if ((uFlags & UIFIND_HITTEST) != 0 && (!m_bMouseEnabled || !::PtInRect(&m_rcItem, *static_cast<LPPOINT>(pData)))) return NULL;
  626. return Proc(this, pData);
  627. }
  628. void CControlUI::Invalidate()
  629. {
  630. if (!IsVisible()) return;
  631. RECT invalidateRc = m_rcItem;
  632. CControlUI* pParent = this;
  633. RECT rcTemp;
  634. RECT rcParent;
  635. while (pParent = pParent->GetParent())
  636. {
  637. rcTemp = invalidateRc;
  638. rcParent = pParent->GetPos();
  639. if (!::IntersectRect(&invalidateRc, &rcTemp, &rcParent))
  640. {
  641. return;
  642. }
  643. }
  644. if (m_pManager != NULL) m_pManager->Invalidate(invalidateRc);
  645. }
  646. bool CControlUI::IsUpdateNeeded() const
  647. {
  648. return m_bUpdateNeeded;
  649. }
  650. void CControlUI::NeedUpdate()
  651. {
  652. if (!IsVisible()) return;
  653. m_bUpdateNeeded = true;
  654. Invalidate();
  655. if (m_pManager != NULL) m_pManager->NeedUpdate();
  656. }
  657. void CControlUI::NeedParentUpdate()
  658. {
  659. if (GetParent())
  660. {
  661. GetParent()->NeedUpdate();
  662. GetParent()->Invalidate();
  663. }
  664. else
  665. {
  666. NeedUpdate();
  667. }
  668. if (m_pManager != NULL) m_pManager->NeedUpdate();
  669. }
  670. DWORD CControlUI::GetAdjustColor(DWORD dwColor)
  671. {
  672. if (!m_bColorHSL) return dwColor;
  673. short H, S, L;
  674. CPaintManagerUI::GetHSL(&H, &S, &L);
  675. return CRenderEngine::AdjustColor(dwColor, H, S, L);
  676. }
  677. void CControlUI::Init()
  678. {
  679. DoInit();
  680. if (OnInit) OnInit(this);
  681. }
  682. void CControlUI::DoInit()
  683. {
  684. }
  685. void CControlUI::Event(TEventUI& event)
  686. {
  687. if (OnEvent(&event)) DoEvent(event);
  688. }
  689. void CControlUI::DoEvent(TEventUI& event)
  690. {
  691. if (event.Type == UIEVENT_SETCURSOR)
  692. {
  693. if (GetCursor())
  694. {
  695. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(GetCursor())));
  696. }
  697. else
  698. {
  699. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
  700. }
  701. return;
  702. }
  703. if (event.Type == UIEVENT_SETFOCUS)
  704. {
  705. m_bFocused = true;
  706. Invalidate();
  707. return;
  708. }
  709. if (event.Type == UIEVENT_KILLFOCUS)
  710. {
  711. m_bFocused = false;
  712. Invalidate();
  713. return;
  714. }
  715. if (event.Type == UIEVENT_TIMER)
  716. {
  717. m_pManager->SendNotify(this, DUI_MSGTYPE_TIMER, event.wParam, event.lParam);
  718. return;
  719. }
  720. if (event.Type == UIEVENT_CONTEXTMENU)
  721. {
  722. if (IsContextMenuUsed())
  723. {
  724. m_pManager->SendNotify(this, DUI_MSGTYPE_MENU, event.wParam, event.lParam);
  725. return;
  726. }
  727. }
  728. if (m_pParent != NULL) m_pParent->DoEvent(event);
  729. }
  730. void CControlUI::SetVirtualWnd(LPCTSTR pstrValue)
  731. {
  732. m_sVirtualWnd = pstrValue;
  733. m_pManager->UsedVirtualWnd(true);
  734. }
  735. CDuiString CControlUI::GetVirtualWnd() const
  736. {
  737. CDuiString str;
  738. if (!m_sVirtualWnd.IsEmpty())
  739. {
  740. str = m_sVirtualWnd;
  741. }
  742. else
  743. {
  744. CControlUI* pParent = GetParent();
  745. if (pParent != NULL)
  746. {
  747. str = pParent->GetVirtualWnd();
  748. }
  749. else
  750. {
  751. str = _T("");
  752. }
  753. }
  754. return str;
  755. }
  756. void CControlUI::AddCustomAttribute(LPCTSTR pstrName, LPCTSTR pstrAttr)
  757. {
  758. if (pstrName == NULL || pstrName[0] == _T('\0') || pstrAttr == NULL || pstrAttr[0] == _T('\0')) return;
  759. if (m_mCustomAttrHash.Find(pstrName) == NULL)
  760. {
  761. CDuiString* pCostomAttr = new CDuiString(pstrAttr);
  762. if (pCostomAttr != NULL)
  763. {
  764. m_mCustomAttrHash.Set(pstrName, (LPVOID)pCostomAttr);
  765. }
  766. }
  767. }
  768. LPCTSTR CControlUI::GetCustomAttribute(LPCTSTR pstrName) const
  769. {
  770. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  771. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(pstrName));
  772. if (pCostomAttr) return pCostomAttr->GetData();
  773. return NULL;
  774. }
  775. bool CControlUI::RemoveCustomAttribute(LPCTSTR pstrName)
  776. {
  777. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  778. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(pstrName));
  779. if (!pCostomAttr) return false;
  780. delete pCostomAttr;
  781. return m_mCustomAttrHash.Remove(pstrName);
  782. }
  783. void CControlUI::RemoveAllCustomAttribute()
  784. {
  785. CDuiString* pCostomAttr;
  786. for (int i = 0; i < m_mCustomAttrHash.GetSize(); i++)
  787. {
  788. if (LPCTSTR key = m_mCustomAttrHash.GetAt(i))
  789. {
  790. pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(key));
  791. delete pCostomAttr;
  792. }
  793. }
  794. m_mCustomAttrHash.Resize();
  795. }
  796. void CControlUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  797. {
  798. // 样式表
  799. if (m_pManager != NULL && _tcsicmp(pstrName, _T("style")) == 0)
  800. {
  801. LPCTSTR pStyle = m_pManager->GetStyle(pstrValue);
  802. if (pStyle != NULL)
  803. {
  804. ApplyAttributeList(pStyle);
  805. return;
  806. }
  807. }
  808. // 属性
  809. if (_tcsicmp(pstrName, _T("innerstyle")) == 0)
  810. {
  811. ApplyAttributeList(pstrValue);
  812. }
  813. else if (_tcsicmp(pstrName, _T("pos")) == 0)
  814. {
  815. RECT rcPos = { 0 };
  816. LPTSTR pstr = NULL;
  817. rcPos.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  818. rcPos.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  819. rcPos.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  820. rcPos.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  821. SIZE szXY = { rcPos.left >= 0 ? rcPos.left : rcPos.right, rcPos.top >= 0 ? rcPos.top : rcPos.bottom };
  822. SetFixedXY(szXY);
  823. SetFixedWidth(abs(rcPos.right - rcPos.left));
  824. SetFixedHeight(abs(rcPos.bottom - rcPos.top));
  825. }
  826. else if (_tcsicmp(pstrName, _T("float")) == 0)
  827. {
  828. CDuiString nValue = pstrValue;
  829. // 动态计算相对比例
  830. if (nValue.Find(',') < 0)
  831. {
  832. SetFloat(_tcsicmp(pstrValue, _T("true")) == 0);
  833. }
  834. else
  835. {
  836. TPercentInfo piFloatPercent = { 0 };
  837. LPTSTR pstr = NULL;
  838. piFloatPercent.left = _tcstod(pstrValue, &pstr); ASSERT(pstr);
  839. piFloatPercent.top = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  840. piFloatPercent.right = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  841. piFloatPercent.bottom = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  842. SetFloatPercent(piFloatPercent);
  843. SetFloat(true);
  844. }
  845. }
  846. else if (_tcsicmp(pstrName, _T("floatalign")) == 0)
  847. {
  848. UINT uAlign = GetFloatAlign();
  849. // 解析文字属性
  850. while (*pstrValue != _T('\0'))
  851. {
  852. CDuiString sValue;
  853. while (*pstrValue == _T(',') || *pstrValue == _T(' ')) pstrValue = ::CharNext(pstrValue);
  854. while (*pstrValue != _T('\0') && *pstrValue != _T(',') && *pstrValue != _T(' '))
  855. {
  856. LPTSTR pstrTemp = ::CharNext(pstrValue);
  857. while (pstrValue < pstrTemp)
  858. {
  859. sValue += *pstrValue++;
  860. }
  861. }
  862. if (sValue.CompareNoCase(_T("null")) == 0)
  863. {
  864. uAlign = 0;
  865. }
  866. if (sValue.CompareNoCase(_T("left")) == 0)
  867. {
  868. uAlign &= ~(DT_CENTER | DT_RIGHT);
  869. uAlign |= DT_LEFT;
  870. }
  871. else if (sValue.CompareNoCase(_T("center")) == 0)
  872. {
  873. uAlign &= ~(DT_LEFT | DT_RIGHT);
  874. uAlign |= DT_CENTER;
  875. }
  876. else if (sValue.CompareNoCase(_T("right")) == 0)
  877. {
  878. uAlign &= ~(DT_LEFT | DT_CENTER);
  879. uAlign |= DT_RIGHT;
  880. }
  881. else if (sValue.CompareNoCase(_T("top")) == 0)
  882. {
  883. uAlign &= ~(DT_BOTTOM | DT_VCENTER);
  884. uAlign |= DT_TOP;
  885. }
  886. else if (sValue.CompareNoCase(_T("vcenter")) == 0)
  887. {
  888. uAlign &= ~(DT_TOP | DT_BOTTOM);
  889. uAlign |= DT_VCENTER;
  890. }
  891. else if (sValue.CompareNoCase(_T("bottom")) == 0)
  892. {
  893. uAlign &= ~(DT_TOP | DT_VCENTER);
  894. uAlign |= DT_BOTTOM;
  895. }
  896. }
  897. SetFloatAlign(uAlign);
  898. }
  899. else if (_tcsicmp(pstrName, _T("padding")) == 0)
  900. {
  901. RECT rcPadding = { 0 };
  902. LPTSTR pstr = NULL;
  903. rcPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  904. rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  905. rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  906. rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  907. SetPadding(rcPadding);
  908. }
  909. else if (_tcsicmp(pstrName, _T("gradient")) == 0) SetGradient(pstrValue);
  910. else if (_tcsicmp(pstrName, _T("bkcolor")) == 0 || _tcsicmp(pstrName, _T("bkcolor1")) == 0)
  911. {
  912. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  913. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  914. LPTSTR pstr = NULL;
  915. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  916. SetBkColor(clrColor);
  917. }
  918. else if (_tcsicmp(pstrName, _T("bkcolor2")) == 0)
  919. {
  920. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  921. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  922. LPTSTR pstr = NULL;
  923. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  924. SetBkColor2(clrColor);
  925. }
  926. else if (_tcsicmp(pstrName, _T("bkcolor3")) == 0)
  927. {
  928. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  929. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  930. LPTSTR pstr = NULL;
  931. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  932. SetBkColor3(clrColor);
  933. }
  934. else if (_tcsicmp(pstrName, _T("forecolor")) == 0)
  935. {
  936. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  937. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  938. LPTSTR pstr = NULL;
  939. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  940. SetForeColor(clrColor);
  941. }
  942. else if (_tcsicmp(pstrName, _T("bordercolor")) == 0)
  943. {
  944. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  945. LPTSTR pstr = NULL;
  946. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  947. SetBorderColor(clrColor);
  948. }
  949. else if (_tcsicmp(pstrName, _T("focusbordercolor")) == 0)
  950. {
  951. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  952. LPTSTR pstr = NULL;
  953. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  954. SetFocusBorderColor(clrColor);
  955. }
  956. else if (_tcsicmp(pstrName, _T("colorhsl")) == 0) SetColorHSL(_tcsicmp(pstrValue, _T("true")) == 0);
  957. else if (_tcsicmp(pstrName, _T("bordersize")) == 0)
  958. {
  959. CDuiString nValue = pstrValue;
  960. if (nValue.Find(',') < 0)
  961. {
  962. SetBorderSize(_ttoi(pstrValue));
  963. RECT rcPadding = { 0 };
  964. SetBorderSize(rcPadding);
  965. }
  966. else
  967. {
  968. RECT rcPadding = { 0 };
  969. LPTSTR pstr = NULL;
  970. rcPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  971. rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  972. rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  973. rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  974. SetBorderSize(rcPadding);
  975. }
  976. }
  977. else if (_tcsicmp(pstrName, _T("leftbordersize")) == 0) SetLeftBorderSize(_ttoi(pstrValue));
  978. else if (_tcsicmp(pstrName, _T("topbordersize")) == 0) SetTopBorderSize(_ttoi(pstrValue));
  979. else if (_tcsicmp(pstrName, _T("rightbordersize")) == 0) SetRightBorderSize(_ttoi(pstrValue));
  980. else if (_tcsicmp(pstrName, _T("bottombordersize")) == 0) SetBottomBorderSize(_ttoi(pstrValue));
  981. else if (_tcsicmp(pstrName, _T("borderstyle")) == 0) SetBorderStyle(_ttoi(pstrValue));
  982. else if (_tcsicmp(pstrName, _T("borderround")) == 0)
  983. {
  984. SIZE cxyRound = { 0 };
  985. LPTSTR pstr = NULL;
  986. cxyRound.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  987. cxyRound.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  988. SetBorderRound(cxyRound);
  989. }
  990. else if (_tcsicmp(pstrName, _T("bkimage")) == 0) SetBkImage(pstrValue);
  991. else if (_tcsicmp(pstrName, _T("foreimage")) == 0) SetForeImage(pstrValue);
  992. else if (_tcsicmp(pstrName, _T("width")) == 0) SetFixedWidth(_ttoi(pstrValue));
  993. else if (_tcsicmp(pstrName, _T("height")) == 0) SetFixedHeight(_ttoi(pstrValue));
  994. else if (_tcsicmp(pstrName, _T("minwidth")) == 0) SetMinWidth(_ttoi(pstrValue));
  995. else if (_tcsicmp(pstrName, _T("minheight")) == 0) SetMinHeight(_ttoi(pstrValue));
  996. else if (_tcsicmp(pstrName, _T("maxwidth")) == 0) SetMaxWidth(_ttoi(pstrValue));
  997. else if (_tcsicmp(pstrName, _T("maxheight")) == 0) SetMaxHeight(_ttoi(pstrValue));
  998. else if (_tcsicmp(pstrName, _T("name")) == 0) SetName(pstrValue);
  999. else if (_tcsicmp(pstrName, _T("drag")) == 0) SetDragEnable(_tcsicmp(pstrValue, _T("true")) == 0);
  1000. else if (_tcsicmp(pstrName, _T("drop")) == 0) SetDropEnable(_tcsicmp(pstrValue, _T("true")) == 0);
  1001. else if (_tcsicmp(pstrName, _T("resourcetext")) == 0) SetResourceText(_tcsicmp(pstrValue, _T("true")) == 0);
  1002. else if (_tcsicmp(pstrName, _T("richevent")) == 0) SetRichEvent(_tcsicmp(pstrValue, _T("true")) == 0);
  1003. else if (_tcsicmp(pstrName, _T("text")) == 0) SetText(pstrValue);
  1004. else if (_tcsicmp(pstrName, _T("tooltip")) == 0) SetToolTip(pstrValue);
  1005. else if (_tcsicmp(pstrName, _T("userdata")) == 0) SetUserData(pstrValue);
  1006. else if (_tcsicmp(pstrName, _T("enabled")) == 0) SetEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1007. else if (_tcsicmp(pstrName, _T("mouse")) == 0) SetMouseEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1008. else if (_tcsicmp(pstrName, _T("keyboard")) == 0) SetKeyboardEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1009. else if (_tcsicmp(pstrName, _T("visible")) == 0) SetVisible(_tcsicmp(pstrValue, _T("true")) == 0);
  1010. else if (_tcsicmp(pstrName, _T("float")) == 0) SetFloat(_tcsicmp(pstrValue, _T("true")) == 0);
  1011. else if (_tcsicmp(pstrName, _T("shortcut")) == 0) SetShortcut(pstrValue[0]);
  1012. else if (_tcsicmp(pstrName, _T("menu")) == 0) SetContextMenuUsed(_tcsicmp(pstrValue, _T("true")) == 0);
  1013. else if (_tcsicmp(pstrName, _T("cursor")) == 0 && pstrValue)
  1014. {
  1015. if (_tcsicmp(pstrValue, _T("arrow")) == 0) SetCursor(DUI_ARROW);
  1016. else if (_tcsicmp(pstrValue, _T("ibeam")) == 0) SetCursor(DUI_IBEAM);
  1017. else if (_tcsicmp(pstrValue, _T("wait")) == 0) SetCursor(DUI_WAIT);
  1018. else if (_tcsicmp(pstrValue, _T("cross")) == 0) SetCursor(DUI_CROSS);
  1019. else if (_tcsicmp(pstrValue, _T("uparrow")) == 0) SetCursor(DUI_UPARROW);
  1020. else if (_tcsicmp(pstrValue, _T("size")) == 0) SetCursor(DUI_SIZE);
  1021. else if (_tcsicmp(pstrValue, _T("icon")) == 0) SetCursor(DUI_ICON);
  1022. else if (_tcsicmp(pstrValue, _T("sizenwse")) == 0) SetCursor(DUI_SIZENWSE);
  1023. else if (_tcsicmp(pstrValue, _T("sizenesw")) == 0) SetCursor(DUI_SIZENESW);
  1024. else if (_tcsicmp(pstrValue, _T("sizewe")) == 0) SetCursor(DUI_SIZEWE);
  1025. else if (_tcsicmp(pstrValue, _T("sizens")) == 0) SetCursor(DUI_SIZENS);
  1026. else if (_tcsicmp(pstrValue, _T("sizeall")) == 0) SetCursor(DUI_SIZEALL);
  1027. else if (_tcsicmp(pstrValue, _T("no")) == 0) SetCursor(DUI_NO);
  1028. else if (_tcsicmp(pstrValue, _T("hand")) == 0) SetCursor(DUI_HAND);
  1029. }
  1030. else if (_tcsicmp(pstrName, _T("virtualwnd")) == 0) SetVirtualWnd(pstrValue);
  1031. else
  1032. {
  1033. AddCustomAttribute(pstrName, pstrValue);
  1034. }
  1035. }
  1036. CControlUI* CControlUI::ApplyAttributeList(LPCTSTR pstrValue)
  1037. {
  1038. // 解析样式表
  1039. if (m_pManager != NULL)
  1040. {
  1041. LPCTSTR pStyle = m_pManager->GetStyle(pstrValue);
  1042. if (pStyle != NULL)
  1043. {
  1044. return ApplyAttributeList(pStyle);
  1045. }
  1046. }
  1047. CDuiString sXmlData = pstrValue;
  1048. sXmlData.Replace(_T("&quot;"), _T("\""));
  1049. sXmlData.Replace(_T("\r"), _T(" "));
  1050. sXmlData.Replace(_T("\n"), _T(" "));
  1051. sXmlData.Replace(_T("\t"), _T(" "));
  1052. sXmlData.Trim();
  1053. LPCTSTR pstrList = sXmlData.GetData();
  1054. // 解析样式属性
  1055. CDuiString sItem;
  1056. CDuiString sValue;
  1057. while (*pstrList != _T('\0'))
  1058. {
  1059. sItem.Empty();
  1060. sValue.Empty();
  1061. while (*pstrList != _T('\0') && (*pstrList == _T(' ')))
  1062. {
  1063. pstrList++;
  1064. }
  1065. while (*pstrList != _T('\0') && *pstrList != _T('='))
  1066. {
  1067. LPTSTR pstrTemp = ::CharNext(pstrList);
  1068. while (pstrList < pstrTemp)
  1069. {
  1070. sItem += *pstrList++;
  1071. }
  1072. }
  1073. ASSERT(*pstrList == _T('='));
  1074. if (*pstrList++ != _T('=')) return this;
  1075. ASSERT(*pstrList == _T('\"'));
  1076. if (*pstrList++ != _T('\"')) return this;
  1077. while (*pstrList != _T('\0') && *pstrList != _T('\"'))
  1078. {
  1079. LPTSTR pstrTemp = ::CharNext(pstrList);
  1080. while (pstrList < pstrTemp)
  1081. {
  1082. sValue += *pstrList++;
  1083. }
  1084. }
  1085. ASSERT(*pstrList == _T('\"'));
  1086. if (*pstrList++ != _T('\"')) return this;
  1087. SetAttribute(sItem, sValue);
  1088. if (*pstrList != _T(' ') && *pstrList != _T(','))
  1089. {
  1090. return this;
  1091. }
  1092. else
  1093. {
  1094. ++pstrList;
  1095. }
  1096. }
  1097. return this;
  1098. }
  1099. SIZE CControlUI::EstimateSize(SIZE szAvailable)
  1100. {
  1101. if (m_pManager != NULL)
  1102. return m_pManager->GetDPIObj()->Scale(m_cxyFixed);
  1103. return m_cxyFixed;
  1104. }
  1105. bool CControlUI::Paint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  1106. {
  1107. if (pStopControl == this) return false;
  1108. if (!::IntersectRect(&m_rcPaint, &rcPaint, &m_rcItem)) return true;
  1109. if (!DoPaint(hDC, m_rcPaint, pStopControl)) return false;
  1110. return true;
  1111. }
  1112. bool CControlUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  1113. {
  1114. // 绘制循序:背景颜色->背景图->状态图->文本->边框
  1115. SIZE cxyBorderRound = GetBorderRound();
  1116. RECT rcBorderSize = GetBorderRectSize();
  1117. if (cxyBorderRound.cx > 0 || cxyBorderRound.cy > 0)
  1118. {
  1119. CRenderClip roundClip;
  1120. CRenderClip::GenerateRoundClip(hDC, m_rcPaint, m_rcItem, cxyBorderRound.cx, cxyBorderRound.cy, roundClip);
  1121. PaintBkColor(hDC);
  1122. PaintBkImage(hDC);
  1123. PaintStatusImage(hDC);
  1124. PaintForeColor(hDC);
  1125. PaintForeImage(hDC);
  1126. PaintText(hDC);
  1127. PaintBorder(hDC);
  1128. }
  1129. else
  1130. {
  1131. PaintBkColor(hDC);
  1132. PaintBkImage(hDC);
  1133. PaintStatusImage(hDC);
  1134. PaintForeColor(hDC);
  1135. PaintForeImage(hDC);
  1136. PaintText(hDC);
  1137. PaintBorder(hDC);
  1138. }
  1139. return true;
  1140. }
  1141. void CControlUI::PaintBkColor(HDC hDC)
  1142. {
  1143. if (m_dwBackColor != 0)
  1144. {
  1145. bool bVer = (m_sGradient.CompareNoCase(_T("hor")) != 0);
  1146. if (m_dwBackColor2 != 0)
  1147. {
  1148. if (m_dwBackColor3 != 0)
  1149. {
  1150. RECT rc = m_rcItem;
  1151. rc.bottom = (rc.bottom + rc.top) / 2;
  1152. CRenderEngine::DrawGradient(hDC, rc, GetAdjustColor(m_dwBackColor), GetAdjustColor(m_dwBackColor2), bVer, 8);
  1153. rc.top = rc.bottom;
  1154. rc.bottom = m_rcItem.bottom;
  1155. CRenderEngine::DrawGradient(hDC, rc, GetAdjustColor(m_dwBackColor2), GetAdjustColor(m_dwBackColor3), bVer, 8);
  1156. }
  1157. else
  1158. {
  1159. CRenderEngine::DrawGradient(hDC, m_rcItem, GetAdjustColor(m_dwBackColor), GetAdjustColor(m_dwBackColor2), bVer, 16);
  1160. }
  1161. }
  1162. else if (m_dwBackColor >= 0xFF000000) CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwBackColor));
  1163. else CRenderEngine::DrawColor(hDC, m_rcItem, GetAdjustColor(m_dwBackColor));
  1164. }
  1165. }
  1166. void CControlUI::PaintBkImage(HDC hDC)
  1167. {
  1168. if (m_sBkImage.IsEmpty()) return;
  1169. if (!DrawImage(hDC, (LPCTSTR)m_sBkImage))
  1170. {
  1171. }
  1172. }
  1173. void CControlUI::PaintStatusImage(HDC hDC)
  1174. {
  1175. return;
  1176. }
  1177. void CControlUI::PaintForeColor(HDC hDC)
  1178. {
  1179. CRenderEngine::DrawColor(hDC, m_rcItem, GetAdjustColor(m_dwForeColor));
  1180. }
  1181. void CControlUI::PaintForeImage(HDC hDC)
  1182. {
  1183. if (m_sForeImage.IsEmpty()) return;
  1184. DrawImage(hDC, (LPCTSTR)m_sForeImage);
  1185. }
  1186. void CControlUI::PaintText(HDC hDC)
  1187. {
  1188. return;
  1189. }
  1190. void CControlUI::PaintBorder(HDC hDC)
  1191. {
  1192. int nBorderSize = GetBorderSize();
  1193. SIZE cxyBorderRound = GetBorderRound();
  1194. RECT rcBorderSize = GetBorderRectSize();
  1195. if (m_dwBorderColor != 0 || m_dwFocusBorderColor != 0)
  1196. {
  1197. //画圆角边框
  1198. if (nBorderSize > 0 && (cxyBorderRound.cx > 0 || cxyBorderRound.cy > 0))
  1199. {
  1200. if (IsFocused() && m_dwFocusBorderColor != 0)
  1201. CRenderEngine::DrawRoundRect(hDC, m_rcItem, nBorderSize, cxyBorderRound.cx, cxyBorderRound.cy, GetAdjustColor(m_dwFocusBorderColor), m_nBorderStyle);
  1202. else
  1203. CRenderEngine::DrawRoundRect(hDC, m_rcItem, nBorderSize, cxyBorderRound.cx, cxyBorderRound.cy, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1204. }
  1205. else
  1206. {
  1207. if (IsFocused() && m_dwFocusBorderColor != 0 && nBorderSize > 0)
  1208. {
  1209. CRenderEngine::DrawRect(hDC, m_rcItem, nBorderSize, GetAdjustColor(m_dwFocusBorderColor), m_nBorderStyle);
  1210. }
  1211. else if (rcBorderSize.left > 0 || rcBorderSize.top > 0 || rcBorderSize.right > 0 || rcBorderSize.bottom > 0)
  1212. {
  1213. RECT rcBorder;
  1214. if (rcBorderSize.left > 0)
  1215. {
  1216. rcBorder = m_rcItem;
  1217. rcBorder.right = rcBorder.left;
  1218. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.left, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1219. }
  1220. if (rcBorderSize.top > 0)
  1221. {
  1222. rcBorder = m_rcItem;
  1223. rcBorder.bottom = rcBorder.top;
  1224. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.top, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1225. }
  1226. if (rcBorderSize.right > 0)
  1227. {
  1228. rcBorder = m_rcItem;
  1229. rcBorder.right -= 1;
  1230. rcBorder.left = rcBorder.right;
  1231. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.right, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1232. }
  1233. if (rcBorderSize.bottom > 0)
  1234. {
  1235. rcBorder = m_rcItem;
  1236. rcBorder.bottom -= 1;
  1237. rcBorder.top = rcBorder.bottom;
  1238. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.bottom, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1239. }
  1240. }
  1241. else if (nBorderSize > 0)
  1242. {
  1243. CRenderEngine::DrawRect(hDC, m_rcItem, nBorderSize, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1244. }
  1245. }
  1246. }
  1247. }
  1248. void CControlUI::DoPostPaint(HDC hDC, const RECT& rcPaint)
  1249. {
  1250. return;
  1251. }
  1252. int CControlUI::GetLeftBorderSize() const
  1253. {
  1254. RECT rcBorderSize = m_rcBorderSize;
  1255. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1256. return rcBorderSize.left;
  1257. }
  1258. void CControlUI::SetLeftBorderSize(int nSize)
  1259. {
  1260. m_rcBorderSize.left = nSize;
  1261. Invalidate();
  1262. }
  1263. int CControlUI::GetTopBorderSize() const
  1264. {
  1265. RECT rcBorderSize = m_rcBorderSize;
  1266. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1267. return rcBorderSize.top;
  1268. }
  1269. void CControlUI::SetTopBorderSize(int nSize)
  1270. {
  1271. m_rcBorderSize.top = nSize;
  1272. Invalidate();
  1273. }
  1274. int CControlUI::GetRightBorderSize() const
  1275. {
  1276. RECT rcBorderSize = m_rcBorderSize;
  1277. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1278. return rcBorderSize.right;
  1279. }
  1280. void CControlUI::SetRightBorderSize(int nSize)
  1281. {
  1282. m_rcBorderSize.right = nSize;
  1283. Invalidate();
  1284. }
  1285. int CControlUI::GetBottomBorderSize() const
  1286. {
  1287. RECT rcBorderSize = m_rcBorderSize;
  1288. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1289. return rcBorderSize.bottom;
  1290. }
  1291. void CControlUI::SetBottomBorderSize(int nSize)
  1292. {
  1293. m_rcBorderSize.bottom = nSize;
  1294. Invalidate();
  1295. }
  1296. int CControlUI::GetBorderStyle() const
  1297. {
  1298. return m_nBorderStyle;
  1299. }
  1300. void CControlUI::SetBorderStyle(int nStyle)
  1301. {
  1302. m_nBorderStyle = nStyle;
  1303. Invalidate();
  1304. }
  1305. } // namespace DuiLib