UIControl.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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