UIControl.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. // [NIM 触屏模型] 触摸事件归一化。
  692. // CPaintManagerUI 把 WM_TOUCH 解析成真正的 UIEVENT_TOUCH* 事件并派发到本控件,
  693. // 这里把按下/抬起归一化为等价鼠标事件,使所有既有控件(按钮、列表项、Edit、Combo
  694. // 以及 60+ 窗口里的自定义控件)无需逐个改写即可原生响应手指操作。
  695. //
  696. // 这与已废弃的 SynthesizeTap 有本质区别:
  697. // SynthesizeTap = 向窗口句柄 PostMessage 伪造鼠标消息,要绕 Windows 消息队列,
  698. // 在模态弹框里会被路由吞掉导致点击丢失;
  699. // 归一化 = 进程内直接换用事件类型后交给控件自己处理,不经消息队列,
  700. // 主窗口与弹框行为完全一致。
  701. // 注意:UIEVENT_TOUCHMOVE 不在此归一化,它由 CContainerUI 单独处理为跟手滚动,
  702. // 并经本函数末尾的父链冒泡抵达那里。
  703. if (event.Type == UIEVENT_TOUCHDOWN)
  704. event.Type = UIEVENT_BUTTONDOWN;
  705. else if (event.Type == UIEVENT_TOUCHUP)
  706. event.Type = UIEVENT_BUTTONUP;
  707. if (event.Type == UIEVENT_SETCURSOR)
  708. {
  709. if (GetCursor())
  710. {
  711. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(GetCursor())));
  712. }
  713. else
  714. {
  715. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
  716. }
  717. return;
  718. }
  719. if (event.Type == UIEVENT_SETFOCUS)
  720. {
  721. m_bFocused = true;
  722. Invalidate();
  723. return;
  724. }
  725. if (event.Type == UIEVENT_KILLFOCUS)
  726. {
  727. m_bFocused = false;
  728. Invalidate();
  729. return;
  730. }
  731. if (event.Type == UIEVENT_TIMER)
  732. {
  733. m_pManager->SendNotify(this, DUI_MSGTYPE_TIMER, event.wParam, event.lParam);
  734. return;
  735. }
  736. if (event.Type == UIEVENT_CONTEXTMENU)
  737. {
  738. if (IsContextMenuUsed())
  739. {
  740. m_pManager->SendNotify(this, DUI_MSGTYPE_MENU, event.wParam, event.lParam);
  741. return;
  742. }
  743. }
  744. if (m_pParent != NULL) m_pParent->DoEvent(event);
  745. }
  746. void CControlUI::SetVirtualWnd(LPCTSTR pstrValue)
  747. {
  748. m_sVirtualWnd = pstrValue;
  749. m_pManager->UsedVirtualWnd(true);
  750. }
  751. CDuiString CControlUI::GetVirtualWnd() const
  752. {
  753. CDuiString str;
  754. if (!m_sVirtualWnd.IsEmpty())
  755. {
  756. str = m_sVirtualWnd;
  757. }
  758. else
  759. {
  760. CControlUI* pParent = GetParent();
  761. if (pParent != NULL)
  762. {
  763. str = pParent->GetVirtualWnd();
  764. }
  765. else
  766. {
  767. str = _T("");
  768. }
  769. }
  770. return str;
  771. }
  772. void CControlUI::AddCustomAttribute(LPCTSTR pstrName, LPCTSTR pstrAttr)
  773. {
  774. if (pstrName == NULL || pstrName[0] == _T('\0') || pstrAttr == NULL || pstrAttr[0] == _T('\0')) return;
  775. if (m_mCustomAttrHash.Find(pstrName) == NULL)
  776. {
  777. CDuiString* pCostomAttr = new CDuiString(pstrAttr);
  778. if (pCostomAttr != NULL)
  779. {
  780. m_mCustomAttrHash.Set(pstrName, (LPVOID)pCostomAttr);
  781. }
  782. }
  783. }
  784. LPCTSTR CControlUI::GetCustomAttribute(LPCTSTR pstrName) const
  785. {
  786. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  787. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(pstrName));
  788. if (pCostomAttr) return pCostomAttr->GetData();
  789. return NULL;
  790. }
  791. bool CControlUI::RemoveCustomAttribute(LPCTSTR pstrName)
  792. {
  793. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  794. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(pstrName));
  795. if (!pCostomAttr) return false;
  796. delete pCostomAttr;
  797. return m_mCustomAttrHash.Remove(pstrName);
  798. }
  799. void CControlUI::RemoveAllCustomAttribute()
  800. {
  801. CDuiString* pCostomAttr;
  802. for (int i = 0; i < m_mCustomAttrHash.GetSize(); i++)
  803. {
  804. if (LPCTSTR key = m_mCustomAttrHash.GetAt(i))
  805. {
  806. pCostomAttr = static_cast<CDuiString*>(m_mCustomAttrHash.Find(key));
  807. delete pCostomAttr;
  808. }
  809. }
  810. m_mCustomAttrHash.Resize();
  811. }
  812. void CControlUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  813. {
  814. // 样式表
  815. if (m_pManager != NULL && _tcsicmp(pstrName, _T("style")) == 0)
  816. {
  817. LPCTSTR pStyle = m_pManager->GetStyle(pstrValue);
  818. if (pStyle != NULL)
  819. {
  820. ApplyAttributeList(pStyle);
  821. return;
  822. }
  823. }
  824. // 属性
  825. if (_tcsicmp(pstrName, _T("innerstyle")) == 0)
  826. {
  827. ApplyAttributeList(pstrValue);
  828. }
  829. else if (_tcsicmp(pstrName, _T("pos")) == 0)
  830. {
  831. RECT rcPos = { 0 };
  832. LPTSTR pstr = NULL;
  833. rcPos.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  834. rcPos.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  835. rcPos.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  836. rcPos.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  837. SIZE szXY = { rcPos.left >= 0 ? rcPos.left : rcPos.right, rcPos.top >= 0 ? rcPos.top : rcPos.bottom };
  838. SetFixedXY(szXY);
  839. SetFixedWidth(abs(rcPos.right - rcPos.left));
  840. SetFixedHeight(abs(rcPos.bottom - rcPos.top));
  841. }
  842. else if (_tcsicmp(pstrName, _T("float")) == 0)
  843. {
  844. CDuiString nValue = pstrValue;
  845. // 动态计算相对比例
  846. if (nValue.Find(',') < 0)
  847. {
  848. SetFloat(_tcsicmp(pstrValue, _T("true")) == 0);
  849. }
  850. else
  851. {
  852. TPercentInfo piFloatPercent = { 0 };
  853. LPTSTR pstr = NULL;
  854. piFloatPercent.left = _tcstod(pstrValue, &pstr); ASSERT(pstr);
  855. piFloatPercent.top = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  856. piFloatPercent.right = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  857. piFloatPercent.bottom = _tcstod(pstr + 1, &pstr); ASSERT(pstr);
  858. SetFloatPercent(piFloatPercent);
  859. SetFloat(true);
  860. }
  861. }
  862. else if (_tcsicmp(pstrName, _T("floatalign")) == 0)
  863. {
  864. UINT uAlign = GetFloatAlign();
  865. // 解析文字属性
  866. while (*pstrValue != _T('\0'))
  867. {
  868. CDuiString sValue;
  869. while (*pstrValue == _T(',') || *pstrValue == _T(' ')) pstrValue = ::CharNext(pstrValue);
  870. while (*pstrValue != _T('\0') && *pstrValue != _T(',') && *pstrValue != _T(' '))
  871. {
  872. LPTSTR pstrTemp = ::CharNext(pstrValue);
  873. while (pstrValue < pstrTemp)
  874. {
  875. sValue += *pstrValue++;
  876. }
  877. }
  878. if (sValue.CompareNoCase(_T("null")) == 0)
  879. {
  880. uAlign = 0;
  881. }
  882. if (sValue.CompareNoCase(_T("left")) == 0)
  883. {
  884. uAlign &= ~(DT_CENTER | DT_RIGHT);
  885. uAlign |= DT_LEFT;
  886. }
  887. else if (sValue.CompareNoCase(_T("center")) == 0)
  888. {
  889. uAlign &= ~(DT_LEFT | DT_RIGHT);
  890. uAlign |= DT_CENTER;
  891. }
  892. else if (sValue.CompareNoCase(_T("right")) == 0)
  893. {
  894. uAlign &= ~(DT_LEFT | DT_CENTER);
  895. uAlign |= DT_RIGHT;
  896. }
  897. else if (sValue.CompareNoCase(_T("top")) == 0)
  898. {
  899. uAlign &= ~(DT_BOTTOM | DT_VCENTER);
  900. uAlign |= DT_TOP;
  901. }
  902. else if (sValue.CompareNoCase(_T("vcenter")) == 0)
  903. {
  904. uAlign &= ~(DT_TOP | DT_BOTTOM);
  905. uAlign |= DT_VCENTER;
  906. }
  907. else if (sValue.CompareNoCase(_T("bottom")) == 0)
  908. {
  909. uAlign &= ~(DT_TOP | DT_VCENTER);
  910. uAlign |= DT_BOTTOM;
  911. }
  912. }
  913. SetFloatAlign(uAlign);
  914. }
  915. else if (_tcsicmp(pstrName, _T("padding")) == 0)
  916. {
  917. RECT rcPadding = { 0 };
  918. LPTSTR pstr = NULL;
  919. rcPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  920. rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  921. rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  922. rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  923. SetPadding(rcPadding);
  924. }
  925. else if (_tcsicmp(pstrName, _T("gradient")) == 0) SetGradient(pstrValue);
  926. else if (_tcsicmp(pstrName, _T("bkcolor")) == 0 || _tcsicmp(pstrName, _T("bkcolor1")) == 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. SetBkColor(clrColor);
  933. }
  934. else if (_tcsicmp(pstrName, _T("bkcolor2")) == 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. SetBkColor2(clrColor);
  941. }
  942. else if (_tcsicmp(pstrName, _T("bkcolor3")) == 0)
  943. {
  944. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  945. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  946. LPTSTR pstr = NULL;
  947. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  948. SetBkColor3(clrColor);
  949. }
  950. else if (_tcsicmp(pstrName, _T("forecolor")) == 0)
  951. {
  952. while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
  953. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  954. LPTSTR pstr = NULL;
  955. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  956. SetForeColor(clrColor);
  957. }
  958. else if (_tcsicmp(pstrName, _T("bordercolor")) == 0)
  959. {
  960. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  961. LPTSTR pstr = NULL;
  962. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  963. SetBorderColor(clrColor);
  964. }
  965. else if (_tcsicmp(pstrName, _T("focusbordercolor")) == 0)
  966. {
  967. if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  968. LPTSTR pstr = NULL;
  969. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  970. SetFocusBorderColor(clrColor);
  971. }
  972. else if (_tcsicmp(pstrName, _T("colorhsl")) == 0) SetColorHSL(_tcsicmp(pstrValue, _T("true")) == 0);
  973. else if (_tcsicmp(pstrName, _T("bordersize")) == 0)
  974. {
  975. CDuiString nValue = pstrValue;
  976. if (nValue.Find(',') < 0)
  977. {
  978. SetBorderSize(_ttoi(pstrValue));
  979. RECT rcPadding = { 0 };
  980. SetBorderSize(rcPadding);
  981. }
  982. else
  983. {
  984. RECT rcPadding = { 0 };
  985. LPTSTR pstr = NULL;
  986. rcPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  987. rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  988. rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  989. rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  990. SetBorderSize(rcPadding);
  991. }
  992. }
  993. else if (_tcsicmp(pstrName, _T("leftbordersize")) == 0) SetLeftBorderSize(_ttoi(pstrValue));
  994. else if (_tcsicmp(pstrName, _T("topbordersize")) == 0) SetTopBorderSize(_ttoi(pstrValue));
  995. else if (_tcsicmp(pstrName, _T("rightbordersize")) == 0) SetRightBorderSize(_ttoi(pstrValue));
  996. else if (_tcsicmp(pstrName, _T("bottombordersize")) == 0) SetBottomBorderSize(_ttoi(pstrValue));
  997. else if (_tcsicmp(pstrName, _T("borderstyle")) == 0) SetBorderStyle(_ttoi(pstrValue));
  998. else if (_tcsicmp(pstrName, _T("borderround")) == 0)
  999. {
  1000. SIZE cxyRound = { 0 };
  1001. LPTSTR pstr = NULL;
  1002. cxyRound.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  1003. cxyRound.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  1004. SetBorderRound(cxyRound);
  1005. }
  1006. else if (_tcsicmp(pstrName, _T("bkimage")) == 0) SetBkImage(pstrValue);
  1007. else if (_tcsicmp(pstrName, _T("foreimage")) == 0) SetForeImage(pstrValue);
  1008. else if (_tcsicmp(pstrName, _T("width")) == 0) SetFixedWidth(_ttoi(pstrValue));
  1009. else if (_tcsicmp(pstrName, _T("height")) == 0) SetFixedHeight(_ttoi(pstrValue));
  1010. else if (_tcsicmp(pstrName, _T("minwidth")) == 0) SetMinWidth(_ttoi(pstrValue));
  1011. else if (_tcsicmp(pstrName, _T("minheight")) == 0) SetMinHeight(_ttoi(pstrValue));
  1012. else if (_tcsicmp(pstrName, _T("maxwidth")) == 0) SetMaxWidth(_ttoi(pstrValue));
  1013. else if (_tcsicmp(pstrName, _T("maxheight")) == 0) SetMaxHeight(_ttoi(pstrValue));
  1014. else if (_tcsicmp(pstrName, _T("name")) == 0) SetName(pstrValue);
  1015. else if (_tcsicmp(pstrName, _T("drag")) == 0) SetDragEnable(_tcsicmp(pstrValue, _T("true")) == 0);
  1016. else if (_tcsicmp(pstrName, _T("drop")) == 0) SetDropEnable(_tcsicmp(pstrValue, _T("true")) == 0);
  1017. else if (_tcsicmp(pstrName, _T("resourcetext")) == 0) SetResourceText(_tcsicmp(pstrValue, _T("true")) == 0);
  1018. else if (_tcsicmp(pstrName, _T("richevent")) == 0) SetRichEvent(_tcsicmp(pstrValue, _T("true")) == 0);
  1019. else if (_tcsicmp(pstrName, _T("text")) == 0) SetText(pstrValue);
  1020. else if (_tcsicmp(pstrName, _T("tooltip")) == 0) SetToolTip(pstrValue);
  1021. else if (_tcsicmp(pstrName, _T("userdata")) == 0) SetUserData(pstrValue);
  1022. else if (_tcsicmp(pstrName, _T("enabled")) == 0) SetEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1023. else if (_tcsicmp(pstrName, _T("mouse")) == 0) SetMouseEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1024. else if (_tcsicmp(pstrName, _T("keyboard")) == 0) SetKeyboardEnabled(_tcsicmp(pstrValue, _T("true")) == 0);
  1025. else if (_tcsicmp(pstrName, _T("visible")) == 0) SetVisible(_tcsicmp(pstrValue, _T("true")) == 0);
  1026. else if (_tcsicmp(pstrName, _T("float")) == 0) SetFloat(_tcsicmp(pstrValue, _T("true")) == 0);
  1027. else if (_tcsicmp(pstrName, _T("shortcut")) == 0) SetShortcut(pstrValue[0]);
  1028. else if (_tcsicmp(pstrName, _T("menu")) == 0) SetContextMenuUsed(_tcsicmp(pstrValue, _T("true")) == 0);
  1029. else if (_tcsicmp(pstrName, _T("cursor")) == 0 && pstrValue)
  1030. {
  1031. if (_tcsicmp(pstrValue, _T("arrow")) == 0) SetCursor(DUI_ARROW);
  1032. else if (_tcsicmp(pstrValue, _T("ibeam")) == 0) SetCursor(DUI_IBEAM);
  1033. else if (_tcsicmp(pstrValue, _T("wait")) == 0) SetCursor(DUI_WAIT);
  1034. else if (_tcsicmp(pstrValue, _T("cross")) == 0) SetCursor(DUI_CROSS);
  1035. else if (_tcsicmp(pstrValue, _T("uparrow")) == 0) SetCursor(DUI_UPARROW);
  1036. else if (_tcsicmp(pstrValue, _T("size")) == 0) SetCursor(DUI_SIZE);
  1037. else if (_tcsicmp(pstrValue, _T("icon")) == 0) SetCursor(DUI_ICON);
  1038. else if (_tcsicmp(pstrValue, _T("sizenwse")) == 0) SetCursor(DUI_SIZENWSE);
  1039. else if (_tcsicmp(pstrValue, _T("sizenesw")) == 0) SetCursor(DUI_SIZENESW);
  1040. else if (_tcsicmp(pstrValue, _T("sizewe")) == 0) SetCursor(DUI_SIZEWE);
  1041. else if (_tcsicmp(pstrValue, _T("sizens")) == 0) SetCursor(DUI_SIZENS);
  1042. else if (_tcsicmp(pstrValue, _T("sizeall")) == 0) SetCursor(DUI_SIZEALL);
  1043. else if (_tcsicmp(pstrValue, _T("no")) == 0) SetCursor(DUI_NO);
  1044. else if (_tcsicmp(pstrValue, _T("hand")) == 0) SetCursor(DUI_HAND);
  1045. }
  1046. else if (_tcsicmp(pstrName, _T("virtualwnd")) == 0) SetVirtualWnd(pstrValue);
  1047. else
  1048. {
  1049. AddCustomAttribute(pstrName, pstrValue);
  1050. }
  1051. }
  1052. CControlUI* CControlUI::ApplyAttributeList(LPCTSTR pstrValue)
  1053. {
  1054. // 解析样式表
  1055. if (m_pManager != NULL)
  1056. {
  1057. LPCTSTR pStyle = m_pManager->GetStyle(pstrValue);
  1058. if (pStyle != NULL)
  1059. {
  1060. return ApplyAttributeList(pStyle);
  1061. }
  1062. }
  1063. CDuiString sXmlData = pstrValue;
  1064. sXmlData.Replace(_T("&quot;"), _T("\""));
  1065. sXmlData.Replace(_T("\r"), _T(" "));
  1066. sXmlData.Replace(_T("\n"), _T(" "));
  1067. sXmlData.Replace(_T("\t"), _T(" "));
  1068. sXmlData.Trim();
  1069. LPCTSTR pstrList = sXmlData.GetData();
  1070. // 解析样式属性
  1071. CDuiString sItem;
  1072. CDuiString sValue;
  1073. while (*pstrList != _T('\0'))
  1074. {
  1075. sItem.Empty();
  1076. sValue.Empty();
  1077. while (*pstrList != _T('\0') && (*pstrList == _T(' ')))
  1078. {
  1079. pstrList++;
  1080. }
  1081. while (*pstrList != _T('\0') && *pstrList != _T('='))
  1082. {
  1083. LPTSTR pstrTemp = ::CharNext(pstrList);
  1084. while (pstrList < pstrTemp)
  1085. {
  1086. sItem += *pstrList++;
  1087. }
  1088. }
  1089. ASSERT(*pstrList == _T('='));
  1090. if (*pstrList++ != _T('=')) return this;
  1091. ASSERT(*pstrList == _T('\"'));
  1092. if (*pstrList++ != _T('\"')) return this;
  1093. while (*pstrList != _T('\0') && *pstrList != _T('\"'))
  1094. {
  1095. LPTSTR pstrTemp = ::CharNext(pstrList);
  1096. while (pstrList < pstrTemp)
  1097. {
  1098. sValue += *pstrList++;
  1099. }
  1100. }
  1101. ASSERT(*pstrList == _T('\"'));
  1102. if (*pstrList++ != _T('\"')) return this;
  1103. SetAttribute(sItem, sValue);
  1104. if (*pstrList != _T(' ') && *pstrList != _T(','))
  1105. {
  1106. return this;
  1107. }
  1108. else
  1109. {
  1110. ++pstrList;
  1111. }
  1112. }
  1113. return this;
  1114. }
  1115. SIZE CControlUI::EstimateSize(SIZE szAvailable)
  1116. {
  1117. if (m_pManager != NULL)
  1118. return m_pManager->GetDPIObj()->Scale(m_cxyFixed);
  1119. return m_cxyFixed;
  1120. }
  1121. bool CControlUI::Paint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  1122. {
  1123. if (pStopControl == this) return false;
  1124. if (!::IntersectRect(&m_rcPaint, &rcPaint, &m_rcItem)) return true;
  1125. if (!DoPaint(hDC, m_rcPaint, pStopControl)) return false;
  1126. return true;
  1127. }
  1128. bool CControlUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  1129. {
  1130. // 绘制循序:背景颜色->背景图->状态图->文本->边框
  1131. SIZE cxyBorderRound = GetBorderRound();
  1132. RECT rcBorderSize = GetBorderRectSize();
  1133. if (cxyBorderRound.cx > 0 || cxyBorderRound.cy > 0)
  1134. {
  1135. CRenderClip roundClip;
  1136. CRenderClip::GenerateRoundClip(hDC, m_rcPaint, m_rcItem, cxyBorderRound.cx, cxyBorderRound.cy, roundClip);
  1137. PaintBkColor(hDC);
  1138. PaintBkImage(hDC);
  1139. PaintStatusImage(hDC);
  1140. PaintForeColor(hDC);
  1141. PaintForeImage(hDC);
  1142. PaintText(hDC);
  1143. PaintBorder(hDC);
  1144. }
  1145. else
  1146. {
  1147. PaintBkColor(hDC);
  1148. PaintBkImage(hDC);
  1149. PaintStatusImage(hDC);
  1150. PaintForeColor(hDC);
  1151. PaintForeImage(hDC);
  1152. PaintText(hDC);
  1153. PaintBorder(hDC);
  1154. }
  1155. return true;
  1156. }
  1157. void CControlUI::PaintBkColor(HDC hDC)
  1158. {
  1159. if (m_dwBackColor != 0)
  1160. {
  1161. bool bVer = (m_sGradient.CompareNoCase(_T("hor")) != 0);
  1162. if (m_dwBackColor2 != 0)
  1163. {
  1164. if (m_dwBackColor3 != 0)
  1165. {
  1166. RECT rc = m_rcItem;
  1167. rc.bottom = (rc.bottom + rc.top) / 2;
  1168. CRenderEngine::DrawGradient(hDC, rc, GetAdjustColor(m_dwBackColor), GetAdjustColor(m_dwBackColor2), bVer, 8);
  1169. rc.top = rc.bottom;
  1170. rc.bottom = m_rcItem.bottom;
  1171. CRenderEngine::DrawGradient(hDC, rc, GetAdjustColor(m_dwBackColor2), GetAdjustColor(m_dwBackColor3), bVer, 8);
  1172. }
  1173. else
  1174. {
  1175. CRenderEngine::DrawGradient(hDC, m_rcItem, GetAdjustColor(m_dwBackColor), GetAdjustColor(m_dwBackColor2), bVer, 16);
  1176. }
  1177. }
  1178. else if (m_dwBackColor >= 0xFF000000) CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwBackColor));
  1179. else CRenderEngine::DrawColor(hDC, m_rcItem, GetAdjustColor(m_dwBackColor));
  1180. }
  1181. }
  1182. void CControlUI::PaintBkImage(HDC hDC)
  1183. {
  1184. if (m_sBkImage.IsEmpty()) return;
  1185. if (!DrawImage(hDC, (LPCTSTR)m_sBkImage))
  1186. {
  1187. }
  1188. }
  1189. void CControlUI::PaintStatusImage(HDC hDC)
  1190. {
  1191. return;
  1192. }
  1193. void CControlUI::PaintForeColor(HDC hDC)
  1194. {
  1195. CRenderEngine::DrawColor(hDC, m_rcItem, GetAdjustColor(m_dwForeColor));
  1196. }
  1197. void CControlUI::PaintForeImage(HDC hDC)
  1198. {
  1199. if (m_sForeImage.IsEmpty()) return;
  1200. DrawImage(hDC, (LPCTSTR)m_sForeImage);
  1201. }
  1202. void CControlUI::PaintText(HDC hDC)
  1203. {
  1204. return;
  1205. }
  1206. void CControlUI::PaintBorder(HDC hDC)
  1207. {
  1208. int nBorderSize = GetBorderSize();
  1209. SIZE cxyBorderRound = GetBorderRound();
  1210. RECT rcBorderSize = GetBorderRectSize();
  1211. if (m_dwBorderColor != 0 || m_dwFocusBorderColor != 0)
  1212. {
  1213. //画圆角边框
  1214. if (nBorderSize > 0 && (cxyBorderRound.cx > 0 || cxyBorderRound.cy > 0))
  1215. {
  1216. if (IsFocused() && m_dwFocusBorderColor != 0)
  1217. CRenderEngine::DrawRoundRect(hDC, m_rcItem, nBorderSize, cxyBorderRound.cx, cxyBorderRound.cy, GetAdjustColor(m_dwFocusBorderColor), m_nBorderStyle);
  1218. else
  1219. CRenderEngine::DrawRoundRect(hDC, m_rcItem, nBorderSize, cxyBorderRound.cx, cxyBorderRound.cy, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1220. }
  1221. else
  1222. {
  1223. if (IsFocused() && m_dwFocusBorderColor != 0 && nBorderSize > 0)
  1224. {
  1225. CRenderEngine::DrawRect(hDC, m_rcItem, nBorderSize, GetAdjustColor(m_dwFocusBorderColor), m_nBorderStyle);
  1226. }
  1227. else if (rcBorderSize.left > 0 || rcBorderSize.top > 0 || rcBorderSize.right > 0 || rcBorderSize.bottom > 0)
  1228. {
  1229. RECT rcBorder;
  1230. if (rcBorderSize.left > 0)
  1231. {
  1232. rcBorder = m_rcItem;
  1233. rcBorder.right = rcBorder.left;
  1234. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.left, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1235. }
  1236. if (rcBorderSize.top > 0)
  1237. {
  1238. rcBorder = m_rcItem;
  1239. rcBorder.bottom = rcBorder.top;
  1240. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.top, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1241. }
  1242. if (rcBorderSize.right > 0)
  1243. {
  1244. rcBorder = m_rcItem;
  1245. rcBorder.right -= 1;
  1246. rcBorder.left = rcBorder.right;
  1247. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.right, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1248. }
  1249. if (rcBorderSize.bottom > 0)
  1250. {
  1251. rcBorder = m_rcItem;
  1252. rcBorder.bottom -= 1;
  1253. rcBorder.top = rcBorder.bottom;
  1254. CRenderEngine::DrawLine(hDC, rcBorder, rcBorderSize.bottom, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1255. }
  1256. }
  1257. else if (nBorderSize > 0)
  1258. {
  1259. CRenderEngine::DrawRect(hDC, m_rcItem, nBorderSize, GetAdjustColor(m_dwBorderColor), m_nBorderStyle);
  1260. }
  1261. }
  1262. }
  1263. }
  1264. void CControlUI::DoPostPaint(HDC hDC, const RECT& rcPaint)
  1265. {
  1266. return;
  1267. }
  1268. int CControlUI::GetLeftBorderSize() const
  1269. {
  1270. RECT rcBorderSize = m_rcBorderSize;
  1271. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1272. return rcBorderSize.left;
  1273. }
  1274. void CControlUI::SetLeftBorderSize(int nSize)
  1275. {
  1276. m_rcBorderSize.left = nSize;
  1277. Invalidate();
  1278. }
  1279. int CControlUI::GetTopBorderSize() const
  1280. {
  1281. RECT rcBorderSize = m_rcBorderSize;
  1282. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1283. return rcBorderSize.top;
  1284. }
  1285. void CControlUI::SetTopBorderSize(int nSize)
  1286. {
  1287. m_rcBorderSize.top = nSize;
  1288. Invalidate();
  1289. }
  1290. int CControlUI::GetRightBorderSize() const
  1291. {
  1292. RECT rcBorderSize = m_rcBorderSize;
  1293. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1294. return rcBorderSize.right;
  1295. }
  1296. void CControlUI::SetRightBorderSize(int nSize)
  1297. {
  1298. m_rcBorderSize.right = nSize;
  1299. Invalidate();
  1300. }
  1301. int CControlUI::GetBottomBorderSize() const
  1302. {
  1303. RECT rcBorderSize = m_rcBorderSize;
  1304. if (m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcBorderSize);
  1305. return rcBorderSize.bottom;
  1306. }
  1307. void CControlUI::SetBottomBorderSize(int nSize)
  1308. {
  1309. m_rcBorderSize.bottom = nSize;
  1310. Invalidate();
  1311. }
  1312. int CControlUI::GetBorderStyle() const
  1313. {
  1314. return m_nBorderStyle;
  1315. }
  1316. void CControlUI::SetBorderStyle(int nStyle)
  1317. {
  1318. m_nBorderStyle = nStyle;
  1319. Invalidate();
  1320. }
  1321. } // namespace DuiLib