UICombo.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. #include "StdAfx.h"
  2. namespace DuiLib {
  3. /////////////////////////////////////////////////////////////////////////////////////
  4. //
  5. //
  6. class CComboWnd : public CWindowWnd, public INotifyUI
  7. {
  8. public:
  9. void Init(CComboUI* pOwner);
  10. LPCTSTR GetWindowClassName() const;
  11. void OnFinalMessage(HWND hWnd);
  12. LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
  13. void Notify(TNotifyUI& msg) override;
  14. void EnsureVisible(int iIndex);
  15. void Scroll(int dx, int dy);
  16. #if(_WIN32_WINNT >= 0x0501)
  17. virtual UINT GetClassStyle() const;
  18. #endif
  19. bool IsHitItem(POINT ptMouse);
  20. public:
  21. CPaintManagerUI m_pm;
  22. CComboUI* m_pOwner;
  23. CVerticalLayoutUI* m_pLayout;
  24. int m_iOldSel;
  25. bool m_bHitItem;
  26. };
  27. void CComboWnd::Notify(TNotifyUI& msg)
  28. {
  29. if (msg.sType == _T("windowinit"))
  30. {
  31. EnsureVisible(m_iOldSel);
  32. }
  33. else if(msg.sType == _T("click")) {
  34. // 测试代码
  35. CDuiString sName = msg.pSender->GetName();
  36. CControlUI* pCtrl = msg.pSender;
  37. while(pCtrl != NULL) {
  38. IListItemUI* pListItem = (IListItemUI*)pCtrl->GetInterface(DUI_CTR_LISTITEM);
  39. if(pListItem != NULL ) {
  40. break;
  41. }
  42. pCtrl = pCtrl->GetParent();
  43. }
  44. if( m_pOwner->GetManager() != NULL ) m_pOwner->GetManager()->SendNotify(msg.pSender, DUI_MSGTYPE_CLICK, 0, 0);
  45. }
  46. }
  47. void CComboWnd::Init(CComboUI* pOwner)
  48. {
  49. m_bHitItem = false;
  50. m_pOwner = pOwner;
  51. m_pLayout = NULL;
  52. m_iOldSel = m_pOwner->GetCurSel();
  53. // Position the popup window in absolute space
  54. SIZE szDrop = m_pOwner->GetDropBoxSize();
  55. RECT rcInset = m_pOwner->GetDropBoxInset();
  56. RECT rcOwner = pOwner->GetPos();
  57. RECT rc = rcOwner;
  58. rc.top = rc.bottom; // 父窗口left、bottom位置作为弹出窗口起点
  59. rc.bottom = rc.top + szDrop.cy; // 计算弹出窗口高度
  60. if( szDrop.cx > 0 ) rc.right = rc.left + szDrop.cx; // 计算弹出窗口宽度
  61. SIZE szAvailable = { rc.right - rc.left, rc.bottom - rc.top };
  62. int cyFixed = rcInset.top;
  63. for( int it = 0; it < pOwner->GetCount(); it++ ) {
  64. CControlUI* pControl = static_cast<CControlUI*>(pOwner->GetItemAt(it));
  65. if( !pControl->IsVisible() ) continue;
  66. SIZE sz = pControl->EstimateSize(szAvailable);
  67. cyFixed += sz.cy;
  68. }
  69. cyFixed += 4;
  70. rc.bottom = rc.top + MIN(cyFixed, szDrop.cy);
  71. ::MapWindowRect(pOwner->GetManager()->GetPaintWindow(), HWND_DESKTOP, &rc);
  72. MONITORINFO oMonitor = {};
  73. oMonitor.cbSize = sizeof(oMonitor);
  74. ::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTOPRIMARY), &oMonitor);
  75. CDuiRect rcWork = oMonitor.rcWork;
  76. if( rc.bottom > rcWork.bottom ) {
  77. rc.left = rcOwner.left;
  78. rc.right = rcOwner.right;
  79. if( szDrop.cx > 0 ) rc.right = rc.left + szDrop.cx;
  80. rc.top = rcOwner.top - MIN(cyFixed, szDrop.cy);
  81. rc.bottom = rcOwner.top;
  82. ::MapWindowRect(pOwner->GetManager()->GetPaintWindow(), HWND_DESKTOP, &rc);
  83. }
  84. Create(pOwner->GetManager()->GetPaintWindow(), NULL, WS_POPUP, WS_EX_TOOLWINDOW, rc);
  85. // HACK: Don't deselect the parent's caption
  86. HWND hWndParent = m_hWnd;
  87. while( ::GetParent(hWndParent) != NULL ) hWndParent = ::GetParent(hWndParent);
  88. ::ShowWindow(m_hWnd, SW_SHOW);
  89. ::SendMessage(hWndParent, WM_NCACTIVATE, TRUE, 0L);
  90. }
  91. LPCTSTR CComboWnd::GetWindowClassName() const
  92. {
  93. return _T("ComboWnd");
  94. }
  95. void CComboWnd::OnFinalMessage(HWND hWnd)
  96. {
  97. m_pOwner->m_pWindow = NULL;
  98. m_pOwner->m_uButtonState &= ~ UISTATE_PUSHED;
  99. m_pOwner->Invalidate();
  100. delete this;
  101. }
  102. bool CComboWnd::IsHitItem(POINT ptMouse)
  103. {
  104. CControlUI* pControl = m_pm.FindControl(ptMouse);
  105. if(pControl != NULL) {
  106. LPVOID pInterface = pControl->GetInterface(DUI_CTR_SCROLLBAR);
  107. if(pInterface) return false;
  108. while(pControl != NULL) {
  109. IListItemUI* pListItem = (IListItemUI*)pControl->GetInterface(DUI_CTR_LISTITEM);
  110. if(pListItem != NULL ) {
  111. return true;
  112. }
  113. pControl = pControl->GetParent();
  114. }
  115. }
  116. return false;
  117. }
  118. LRESULT CComboWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  119. {
  120. if( uMsg == WM_CREATE ) {
  121. m_pm.SetForceUseSharedRes(true);
  122. m_pm.Init(m_hWnd);
  123. m_pm.SetLayered(true);
  124. // The trick is to add the items to the new container. Their owner gets
  125. // reassigned by this operation - which is why it is important to reassign
  126. // the items back to the righfull owner/manager when the window closes.
  127. m_pLayout = new CVerticalLayoutUI;
  128. m_pLayout->SetManager(&m_pm, NULL, true);
  129. LPCTSTR pDefaultAttributes = m_pOwner->GetManager()->GetDefaultAttributeList(_T("VerticalLayout"));
  130. if( pDefaultAttributes ) {
  131. m_pLayout->ApplyAttributeList(pDefaultAttributes);
  132. }
  133. m_pLayout->SetInset(CDuiRect(1, 1, 1, 1));
  134. m_pLayout->SetBkColor(0xFFFFFFFF);
  135. m_pLayout->SetBorderColor(0xFFC6C7D2);
  136. m_pLayout->SetBorderSize(1);
  137. m_pLayout->SetAutoDestroy(false);
  138. m_pLayout->EnableScrollBar();
  139. m_pLayout->ApplyAttributeList(m_pOwner->GetDropBoxAttributeList());
  140. for( int i = 0; i < m_pOwner->GetCount(); i++ ) {
  141. m_pLayout->Add(static_cast<CControlUI*>(m_pOwner->GetItemAt(i)));
  142. }
  143. CShadowUI *pShadow = m_pOwner->GetManager()->GetShadow();
  144. pShadow->CopyShadow(m_pm.GetShadow());
  145. m_pm.GetShadow()->ShowShadow(m_pOwner->IsShowShadow());
  146. m_pm.AttachDialog(m_pLayout);
  147. m_pm.AddNotifier(this);
  148. return 0;
  149. }
  150. else if( uMsg == WM_CLOSE ) {
  151. m_pOwner->SetManager(m_pOwner->GetManager(), m_pOwner->GetParent(), false);
  152. RECT rcNull = { 0 };
  153. for( int i = 0; i < m_pOwner->GetCount(); i++ ) static_cast<CControlUI*>(m_pOwner->GetItemAt(i))->SetPos(rcNull);
  154. m_pOwner->SetFocus();
  155. }
  156. else if( uMsg == WM_LBUTTONDOWN ) {
  157. POINT pt = { 0 };
  158. ::GetCursorPos(&pt);
  159. ::ScreenToClient(m_pm.GetPaintWindow(), &pt);
  160. m_bHitItem = IsHitItem(pt);
  161. }
  162. else if( uMsg == WM_LBUTTONUP ) {
  163. POINT pt = { 0 };
  164. ::GetCursorPos(&pt);
  165. ::ScreenToClient(m_pm.GetPaintWindow(), &pt);
  166. if(m_bHitItem && IsHitItem(pt)) {
  167. PostMessage(WM_KILLFOCUS);
  168. }
  169. m_bHitItem = false;
  170. }
  171. else if( uMsg == WM_KEYDOWN ) {
  172. switch( wParam ) {
  173. case VK_ESCAPE:
  174. m_pOwner->SelectItem(m_iOldSel, true);
  175. EnsureVisible(m_iOldSel);
  176. case VK_RETURN:
  177. PostMessage(WM_KILLFOCUS);
  178. break;
  179. default:
  180. TEventUI event;
  181. event.Type = UIEVENT_KEYDOWN;
  182. event.chKey = (TCHAR)wParam;
  183. m_pOwner->DoEvent(event);
  184. EnsureVisible(m_pOwner->GetCurSel());
  185. return 0;
  186. }
  187. }
  188. else if( uMsg == WM_MOUSEWHEEL ) {
  189. int zDelta = (int) (short) HIWORD(wParam);
  190. TEventUI event = { 0 };
  191. event.Type = UIEVENT_SCROLLWHEEL;
  192. event.wParam = MAKELPARAM(zDelta < 0 ? SB_LINEDOWN : SB_LINEUP, 0);
  193. event.lParam = lParam;
  194. event.dwTimestamp = ::GetTickCount();
  195. if(m_pOwner->GetScrollSelect()) {
  196. m_pOwner->DoEvent(event);
  197. EnsureVisible(m_pOwner->GetCurSel());
  198. return 0;
  199. }
  200. else {
  201. m_pLayout->DoEvent(event);
  202. return 0;
  203. }
  204. }
  205. else if( uMsg == WM_KILLFOCUS ) {
  206. if( m_hWnd != (HWND) wParam ) PostMessage(WM_CLOSE);
  207. }
  208. LRESULT lRes = 0;
  209. if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
  210. return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
  211. }
  212. void CComboWnd::EnsureVisible(int iIndex)
  213. {
  214. if( m_pOwner->GetCurSel() < 0 ) return;
  215. m_pLayout->FindSelectable(m_pOwner->GetCurSel(), false);
  216. RECT rcItem = m_pLayout->GetItemAt(iIndex)->GetPos();
  217. RECT rcList = m_pLayout->GetPos();
  218. CScrollBarUI* pHorizontalScrollBar = m_pLayout->GetHorizontalScrollBar();
  219. if( pHorizontalScrollBar && pHorizontalScrollBar->IsVisible() ) rcList.bottom -= pHorizontalScrollBar->GetFixedHeight();
  220. int iPos = m_pLayout->GetScrollPos().cy;
  221. if( rcItem.top >= rcList.top && rcItem.bottom < rcList.bottom ) return;
  222. int dx = 0;
  223. if( rcItem.top < rcList.top ) dx = rcItem.top - rcList.top;
  224. if( rcItem.bottom > rcList.bottom ) dx = rcItem.bottom - rcList.bottom;
  225. Scroll(0, dx);
  226. }
  227. void CComboWnd::Scroll(int dx, int dy)
  228. {
  229. if( dx == 0 && dy == 0 ) return;
  230. SIZE sz = m_pLayout->GetScrollPos();
  231. m_pLayout->SetScrollPos(CDuiSize(sz.cx + dx, sz.cy + dy));
  232. }
  233. #if(_WIN32_WINNT >= 0x0501)
  234. UINT CComboWnd::GetClassStyle() const
  235. {
  236. return __super::GetClassStyle();
  237. if(m_pOwner->IsShowShadow()) {
  238. return __super::GetClassStyle();
  239. }
  240. else {
  241. return __super::GetClassStyle() | CS_DROPSHADOW;
  242. }
  243. }
  244. #endif
  245. ////////////////////////////////////////////////////////
  246. IMPLEMENT_DUICONTROL(CComboUI)
  247. CComboUI::CComboUI() : m_uTextStyle(DT_VCENTER | DT_SINGLELINE)
  248. , m_dwTextColor(0)
  249. , m_dwDisabledTextColor(0)
  250. , m_iFont(-1)
  251. , m_bShowHtml(false)
  252. , m_pWindow(NULL)
  253. , m_iCurSel(-1)
  254. , m_uButtonState(0)
  255. , m_bScrollSelect(true)
  256. , m_bShowShadow(false)
  257. {
  258. m_szDropBox = CDuiSize(0, 150);
  259. ::ZeroMemory(&m_rcTextPadding, sizeof(m_rcTextPadding));
  260. ::ZeroMemory(&m_rcDropBox, sizeof(m_rcDropBox));
  261. m_ListInfo.nColumns = 0;
  262. m_ListInfo.nFont = -1;
  263. m_ListInfo.uTextStyle = DT_VCENTER;
  264. m_ListInfo.dwTextColor = 0xFF000000;
  265. m_ListInfo.dwBkColor = 0;
  266. m_ListInfo.bAlternateBk = false;
  267. m_ListInfo.dwSelectedTextColor = 0xFF000000;
  268. m_ListInfo.dwSelectedBkColor = 0xFFC1E3FF;
  269. m_ListInfo.dwHotTextColor = 0xFF000000;
  270. m_ListInfo.dwHotBkColor = 0xFFE9F5FF;
  271. m_ListInfo.dwDisabledTextColor = 0xFFCCCCCC;
  272. m_ListInfo.dwDisabledBkColor = 0xFFFFFFFF;
  273. m_ListInfo.dwLineColor = 0;
  274. m_ListInfo.bShowHtml = false;
  275. m_ListInfo.bMultiExpandable = false;
  276. ::ZeroMemory(&m_ListInfo.rcTextPadding, sizeof(m_ListInfo.rcTextPadding));
  277. ::ZeroMemory(&m_ListInfo.rcColumn, sizeof(m_ListInfo.rcColumn));
  278. m_pCompareFunc = NULL;
  279. m_compareData = NULL;
  280. }
  281. LPCTSTR CComboUI::GetClass() const
  282. {
  283. return _T("ComboUI");
  284. }
  285. LPVOID CComboUI::GetInterface(LPCTSTR pstrName)
  286. {
  287. if( _tcsicmp(pstrName, DUI_CTR_COMBO) == 0 ) return static_cast<CComboUI*>(this);
  288. if( _tcsicmp(pstrName, _T("IListOwner")) == 0 ) return static_cast<IListOwnerUI*>(this);
  289. return CContainerUI::GetInterface(pstrName);
  290. }
  291. UINT CComboUI::GetControlFlags() const
  292. {
  293. return UIFLAG_TABSTOP | UIFLAG_SETCURSOR;
  294. }
  295. void CComboUI::DoInit()
  296. {
  297. }
  298. UINT CComboUI::GetListType()
  299. {
  300. return LT_COMBO;
  301. }
  302. TListInfoUI* CComboUI::GetListInfo()
  303. {
  304. return &m_ListInfo;
  305. }
  306. int CComboUI::GetCurSel() const
  307. {
  308. return m_iCurSel;
  309. }
  310. bool CComboUI::SelectItem(int iIndex, bool bTakeFocus)
  311. {
  312. if( iIndex == m_iCurSel ) return true;
  313. int iOldSel = m_iCurSel;
  314. if( m_iCurSel >= 0 ) {
  315. CControlUI* pControl = static_cast<CControlUI*>(m_items[m_iCurSel]);
  316. if( !pControl ) return false;
  317. IListItemUI* pListItem = static_cast<IListItemUI*>(pControl->GetInterface(_T("ListItem")));
  318. if( pListItem != NULL ) pListItem->Select(false);
  319. m_iCurSel = -1;
  320. }
  321. if( iIndex < 0 ) return false;
  322. if( m_items.GetSize() == 0 ) return false;
  323. if( iIndex >= m_items.GetSize() ) iIndex = m_items.GetSize() - 1;
  324. CControlUI* pControl = static_cast<CControlUI*>(m_items[iIndex]);
  325. if( !pControl || !pControl->IsEnabled() ) return false;
  326. IListItemUI* pListItem = static_cast<IListItemUI*>(pControl->GetInterface(_T("ListItem")));
  327. if( pListItem == NULL ) return false;
  328. m_iCurSel = iIndex;
  329. if( m_pWindow != NULL || bTakeFocus ) pControl->SetFocus();
  330. pListItem->Select(true);
  331. if( m_pManager != NULL ) m_pManager->SendNotify(this, DUI_MSGTYPE_ITEMSELECT, m_iCurSel, iOldSel);
  332. Invalidate();
  333. return true;
  334. }
  335. bool CComboUI::SelectMultiItem(int iIndex, bool bTakeFocus)
  336. {
  337. return SelectItem(iIndex, bTakeFocus);
  338. }
  339. bool CComboUI::UnSelectItem(int iIndex, bool bOthers)
  340. {
  341. return false;
  342. }
  343. bool CComboUI::SetItemIndex(CControlUI* pControl, int iIndex)
  344. {
  345. int iOrginIndex = GetItemIndex(pControl);
  346. if( iOrginIndex == -1 ) return false;
  347. if( iOrginIndex == iIndex ) return true;
  348. IListItemUI* pSelectedListItem = NULL;
  349. if( m_iCurSel >= 0 ) pSelectedListItem =
  350. static_cast<IListItemUI*>(GetItemAt(m_iCurSel)->GetInterface(_T("ListItem")));
  351. if( !CContainerUI::SetItemIndex(pControl, iIndex) ) return false;
  352. int iMinIndex = min(iOrginIndex, iIndex);
  353. int iMaxIndex = max(iOrginIndex, iIndex);
  354. for(int i = iMinIndex; i < iMaxIndex + 1; ++i) {
  355. CControlUI* p = GetItemAt(i);
  356. IListItemUI* pListItem = static_cast<IListItemUI*>(p->GetInterface(_T("ListItem")));
  357. if( pListItem != NULL ) {
  358. pListItem->SetIndex(i);
  359. }
  360. }
  361. if( m_iCurSel >= 0 && pSelectedListItem != NULL ) m_iCurSel = pSelectedListItem->GetIndex();
  362. return true;
  363. }
  364. bool CComboUI::Add(CControlUI* pControl)
  365. {
  366. IListItemUI* pListItem = static_cast<IListItemUI*>(pControl->GetInterface(_T("ListItem")));
  367. if( pListItem != NULL )
  368. {
  369. pListItem->SetOwner(this);
  370. pListItem->SetIndex(m_items.GetSize());
  371. }
  372. return CContainerUI::Add(pControl);
  373. }
  374. bool CComboUI::AddAt(CControlUI* pControl, int iIndex)
  375. {
  376. if (!CContainerUI::AddAt(pControl, iIndex)) return false;
  377. // The list items should know about us
  378. IListItemUI* pListItem = static_cast<IListItemUI*>(pControl->GetInterface(_T("ListItem")));
  379. if( pListItem != NULL ) {
  380. pListItem->SetOwner(this);
  381. pListItem->SetIndex(iIndex);
  382. }
  383. for(int i = iIndex + 1; i < GetCount(); ++i) {
  384. CControlUI* p = GetItemAt(i);
  385. pListItem = static_cast<IListItemUI*>(p->GetInterface(_T("ListItem")));
  386. if( pListItem != NULL ) {
  387. pListItem->SetIndex(i);
  388. }
  389. }
  390. if( m_iCurSel >= iIndex ) m_iCurSel += 1;
  391. return true;
  392. }
  393. bool CComboUI::Remove(CControlUI* pControl)
  394. {
  395. int iIndex = GetItemIndex(pControl);
  396. if (iIndex == -1) return false;
  397. if (!CContainerUI::RemoveAt(iIndex)) return false;
  398. for(int i = iIndex; i < GetCount(); ++i) {
  399. CControlUI* p = GetItemAt(i);
  400. IListItemUI* pListItem = static_cast<IListItemUI*>(p->GetInterface(_T("ListItem")));
  401. if( pListItem != NULL ) {
  402. pListItem->SetIndex(i);
  403. }
  404. }
  405. if( iIndex == m_iCurSel && m_iCurSel >= 0 ) {
  406. int iSel = m_iCurSel;
  407. m_iCurSel = -1;
  408. SelectItem(FindSelectable(iSel, false));
  409. }
  410. else if( iIndex < m_iCurSel ) m_iCurSel -= 1;
  411. return true;
  412. }
  413. bool CComboUI::RemoveAt(int iIndex)
  414. {
  415. if (!CContainerUI::RemoveAt(iIndex)) return false;
  416. for(int i = iIndex; i < GetCount(); ++i) {
  417. CControlUI* p = GetItemAt(i);
  418. IListItemUI* pListItem = static_cast<IListItemUI*>(p->GetInterface(_T("ListItem")));
  419. if( pListItem != NULL ) pListItem->SetIndex(i);
  420. }
  421. if( iIndex == m_iCurSel && m_iCurSel >= 0 ) {
  422. int iSel = m_iCurSel;
  423. m_iCurSel = -1;
  424. SelectItem(FindSelectable(iSel, false));
  425. }
  426. else if( iIndex < m_iCurSel ) m_iCurSel -= 1;
  427. return true;
  428. }
  429. void CComboUI::RemoveAll()
  430. {
  431. m_iCurSel = -1;
  432. CContainerUI::RemoveAll();
  433. }
  434. void CComboUI::DoEvent(TEventUI& event)
  435. {
  436. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  437. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  438. else CContainerUI::DoEvent(event);
  439. return;
  440. }
  441. if( event.Type == UIEVENT_SETFOCUS )
  442. {
  443. Invalidate();
  444. }
  445. if( event.Type == UIEVENT_KILLFOCUS )
  446. {
  447. Invalidate();
  448. }
  449. if( event.Type == UIEVENT_BUTTONDOWN )
  450. {
  451. if( IsEnabled() ) {
  452. Activate();
  453. m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
  454. }
  455. return;
  456. }
  457. if( event.Type == UIEVENT_BUTTONUP )
  458. {
  459. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  460. m_uButtonState &= ~ UISTATE_CAPTURED;
  461. Invalidate();
  462. }
  463. return;
  464. }
  465. if( event.Type == UIEVENT_MOUSEMOVE )
  466. {
  467. return;
  468. }
  469. if( event.Type == UIEVENT_KEYDOWN )
  470. {
  471. if (!IsKeyboardEnabled())
  472. {
  473. return;
  474. }
  475. switch( event.chKey ) {
  476. case VK_F4:
  477. Activate();
  478. return;
  479. case VK_UP:
  480. SelectItem(FindSelectable(m_iCurSel - 1, false));
  481. return;
  482. case VK_DOWN:
  483. SelectItem(FindSelectable(m_iCurSel + 1, true));
  484. return;
  485. case VK_PRIOR:
  486. SelectItem(FindSelectable(m_iCurSel - 1, false));
  487. return;
  488. case VK_NEXT:
  489. SelectItem(FindSelectable(m_iCurSel + 1, true));
  490. return;
  491. case VK_HOME:
  492. SelectItem(FindSelectable(0, false));
  493. return;
  494. case VK_END:
  495. SelectItem(FindSelectable(GetCount() - 1, true));
  496. return;
  497. }
  498. }
  499. if( event.Type == UIEVENT_SCROLLWHEEL )
  500. {
  501. if(GetScrollSelect()) {
  502. bool bDownward = LOWORD(event.wParam) == SB_LINEDOWN;
  503. SelectItem(FindSelectable(m_iCurSel + (bDownward ? 1 : -1), bDownward));
  504. }
  505. return;
  506. }
  507. if( event.Type == UIEVENT_CONTEXTMENU )
  508. {
  509. return;
  510. }
  511. if( event.Type == UIEVENT_MOUSEENTER )
  512. {
  513. if( ::PtInRect(&m_rcItem, event.ptMouse ) ) {
  514. if( (m_uButtonState & UISTATE_HOT) == 0 )
  515. m_uButtonState |= UISTATE_HOT;
  516. Invalidate();
  517. }
  518. return;
  519. }
  520. if( event.Type == UIEVENT_MOUSELEAVE )
  521. {
  522. if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  523. m_uButtonState &= ~UISTATE_HOT;
  524. Invalidate();
  525. }
  526. return;
  527. }
  528. CControlUI::DoEvent(event);
  529. }
  530. SIZE CComboUI::EstimateSize(SIZE szAvailable)
  531. {
  532. if( m_cxyFixed.cy == 0 ) return CDuiSize(m_cxyFixed.cx, m_pManager->GetDefaultFontInfo()->tm.tmHeight + 12);
  533. return CControlUI::EstimateSize(szAvailable);
  534. }
  535. bool CComboUI::Activate()
  536. {
  537. if( !CControlUI::Activate() ) return false;
  538. if( m_pManager != NULL ) m_pManager->SendNotify(this, DUI_MSGTYPE_PREDROPDOWN);
  539. if( m_pWindow ) return true;
  540. m_pWindow = new CComboWnd();
  541. ASSERT(m_pWindow);
  542. m_pWindow->Init(this);
  543. if( m_pManager != NULL ) m_pManager->SendNotify(this, DUI_MSGTYPE_DROPDOWN);
  544. Invalidate();
  545. return true;
  546. }
  547. CDuiString CComboUI::GetText() const
  548. {
  549. if( m_iCurSel < 0 ) return CControlUI::GetText();
  550. CControlUI* pControl = static_cast<CControlUI*>(m_items[m_iCurSel]);
  551. return pControl->GetText();
  552. }
  553. void CComboUI::SetEnabled(bool bEnable)
  554. {
  555. CContainerUI::SetEnabled(bEnable);
  556. if( !IsEnabled() ) m_uButtonState = 0;
  557. }
  558. CDuiString CComboUI::GetDropBoxAttributeList()
  559. {
  560. return m_sDropBoxAttributes;
  561. }
  562. void CComboUI::SetDropBoxAttributeList(LPCTSTR pstrList)
  563. {
  564. m_sDropBoxAttributes = pstrList;
  565. }
  566. SIZE CComboUI::GetDropBoxSize() const
  567. {
  568. return m_szDropBox;
  569. }
  570. void CComboUI::SetDropBoxSize(SIZE szDropBox)
  571. {
  572. m_szDropBox = szDropBox;
  573. }
  574. RECT CComboUI::GetDropBoxInset() const
  575. {
  576. return m_rcDropBox;
  577. }
  578. void CComboUI::SetDropBoxInset(RECT rcDropBox)
  579. {
  580. m_rcDropBox = rcDropBox;
  581. }
  582. void CComboUI::SetTextStyle(UINT uStyle)
  583. {
  584. m_uTextStyle = uStyle;
  585. Invalidate();
  586. }
  587. UINT CComboUI::GetTextStyle() const
  588. {
  589. return m_uTextStyle;
  590. }
  591. void CComboUI::SetTextColor(DWORD dwTextColor)
  592. {
  593. m_dwTextColor = dwTextColor;
  594. Invalidate();
  595. }
  596. DWORD CComboUI::GetTextColor() const
  597. {
  598. return m_dwTextColor;
  599. }
  600. void CComboUI::SetDisabledTextColor(DWORD dwTextColor)
  601. {
  602. m_dwDisabledTextColor = dwTextColor;
  603. Invalidate();
  604. }
  605. DWORD CComboUI::GetDisabledTextColor() const
  606. {
  607. return m_dwDisabledTextColor;
  608. }
  609. void CComboUI::SetFont(int index)
  610. {
  611. m_iFont = index;
  612. Invalidate();
  613. }
  614. int CComboUI::GetFont() const
  615. {
  616. return m_iFont;
  617. }
  618. RECT CComboUI::GetTextPadding() const
  619. {
  620. RECT rcTextPadding = m_rcTextPadding;
  621. if(m_pManager != NULL) m_pManager->GetDPIObj()->Scale(&rcTextPadding);
  622. return rcTextPadding;
  623. }
  624. void CComboUI::SetTextPadding(RECT rc)
  625. {
  626. m_rcTextPadding = rc;
  627. Invalidate();
  628. }
  629. bool CComboUI::IsShowHtml()
  630. {
  631. return m_bShowHtml;
  632. }
  633. void CComboUI::SetShowHtml(bool bShowHtml)
  634. {
  635. if( m_bShowHtml == bShowHtml ) return;
  636. m_bShowHtml = bShowHtml;
  637. Invalidate();
  638. }
  639. bool CComboUI::IsShowShadow()
  640. {
  641. return m_bShowShadow;
  642. }
  643. void CComboUI::SetShowShadow(bool bShow)
  644. {
  645. if( m_bShowShadow == bShow ) return;
  646. m_bShowShadow = bShow;
  647. Invalidate();
  648. }
  649. LPCTSTR CComboUI::GetNormalImage() const
  650. {
  651. return m_sNormalImage;
  652. }
  653. void CComboUI::SetNormalImage(LPCTSTR pStrImage)
  654. {
  655. m_sNormalImage = pStrImage;
  656. Invalidate();
  657. }
  658. LPCTSTR CComboUI::GetHotImage() const
  659. {
  660. return m_sHotImage;
  661. }
  662. void CComboUI::SetHotImage(LPCTSTR pStrImage)
  663. {
  664. m_sHotImage = pStrImage;
  665. Invalidate();
  666. }
  667. LPCTSTR CComboUI::GetPushedImage() const
  668. {
  669. return m_sPushedImage;
  670. }
  671. void CComboUI::SetPushedImage(LPCTSTR pStrImage)
  672. {
  673. m_sPushedImage = pStrImage;
  674. Invalidate();
  675. }
  676. LPCTSTR CComboUI::GetFocusedImage() const
  677. {
  678. return m_sFocusedImage;
  679. }
  680. void CComboUI::SetFocusedImage(LPCTSTR pStrImage)
  681. {
  682. m_sFocusedImage = pStrImage;
  683. Invalidate();
  684. }
  685. LPCTSTR CComboUI::GetDisabledImage() const
  686. {
  687. return m_sDisabledImage;
  688. }
  689. void CComboUI::SetDisabledImage(LPCTSTR pStrImage)
  690. {
  691. m_sDisabledImage = pStrImage;
  692. Invalidate();
  693. }
  694. bool CComboUI::GetScrollSelect()
  695. {
  696. return m_bScrollSelect;
  697. }
  698. void CComboUI::SetScrollSelect(bool bScrollSelect)
  699. {
  700. m_bScrollSelect = bScrollSelect;
  701. }
  702. void CComboUI::SetItemFont(int index)
  703. {
  704. m_ListInfo.nFont = index;
  705. Invalidate();
  706. }
  707. void CComboUI::SetItemTextStyle(UINT uStyle)
  708. {
  709. m_ListInfo.uTextStyle = uStyle;
  710. Invalidate();
  711. }
  712. RECT CComboUI::GetItemTextPadding() const
  713. {
  714. return m_ListInfo.rcTextPadding;
  715. }
  716. void CComboUI::SetItemTextPadding(RECT rc)
  717. {
  718. m_ListInfo.rcTextPadding = rc;
  719. Invalidate();
  720. }
  721. void CComboUI::SetItemTextColor(DWORD dwTextColor)
  722. {
  723. m_ListInfo.dwTextColor = dwTextColor;
  724. Invalidate();
  725. }
  726. void CComboUI::SetItemBkColor(DWORD dwBkColor)
  727. {
  728. m_ListInfo.dwBkColor = dwBkColor;
  729. }
  730. void CComboUI::SetItemBkImage(LPCTSTR pStrImage)
  731. {
  732. m_ListInfo.sBkImage = pStrImage;
  733. }
  734. DWORD CComboUI::GetItemTextColor() const
  735. {
  736. return m_ListInfo.dwTextColor;
  737. }
  738. DWORD CComboUI::GetItemBkColor() const
  739. {
  740. return m_ListInfo.dwBkColor;
  741. }
  742. LPCTSTR CComboUI::GetItemBkImage() const
  743. {
  744. return m_ListInfo.sBkImage;
  745. }
  746. bool CComboUI::IsAlternateBk() const
  747. {
  748. return m_ListInfo.bAlternateBk;
  749. }
  750. void CComboUI::SetAlternateBk(bool bAlternateBk)
  751. {
  752. m_ListInfo.bAlternateBk = bAlternateBk;
  753. }
  754. void CComboUI::SetSelectedItemTextColor(DWORD dwTextColor)
  755. {
  756. m_ListInfo.dwSelectedTextColor = dwTextColor;
  757. }
  758. void CComboUI::SetSelectedItemBkColor(DWORD dwBkColor)
  759. {
  760. m_ListInfo.dwSelectedBkColor = dwBkColor;
  761. }
  762. void CComboUI::SetSelectedItemImage(LPCTSTR pStrImage)
  763. {
  764. m_ListInfo.sSelectedImage = pStrImage;
  765. }
  766. DWORD CComboUI::GetSelectedItemTextColor() const
  767. {
  768. return m_ListInfo.dwSelectedTextColor;
  769. }
  770. DWORD CComboUI::GetSelectedItemBkColor() const
  771. {
  772. return m_ListInfo.dwSelectedBkColor;
  773. }
  774. LPCTSTR CComboUI::GetSelectedItemImage() const
  775. {
  776. return m_ListInfo.sSelectedImage;
  777. }
  778. void CComboUI::SetHotItemTextColor(DWORD dwTextColor)
  779. {
  780. m_ListInfo.dwHotTextColor = dwTextColor;
  781. }
  782. void CComboUI::SetHotItemBkColor(DWORD dwBkColor)
  783. {
  784. m_ListInfo.dwHotBkColor = dwBkColor;
  785. }
  786. void CComboUI::SetHotItemImage(LPCTSTR pStrImage)
  787. {
  788. m_ListInfo.sHotImage = pStrImage;
  789. }
  790. DWORD CComboUI::GetHotItemTextColor() const
  791. {
  792. return m_ListInfo.dwHotTextColor;
  793. }
  794. DWORD CComboUI::GetHotItemBkColor() const
  795. {
  796. return m_ListInfo.dwHotBkColor;
  797. }
  798. LPCTSTR CComboUI::GetHotItemImage() const
  799. {
  800. return m_ListInfo.sHotImage;
  801. }
  802. void CComboUI::SetDisabledItemTextColor(DWORD dwTextColor)
  803. {
  804. m_ListInfo.dwDisabledTextColor = dwTextColor;
  805. }
  806. void CComboUI::SetDisabledItemBkColor(DWORD dwBkColor)
  807. {
  808. m_ListInfo.dwDisabledBkColor = dwBkColor;
  809. }
  810. void CComboUI::SetDisabledItemImage(LPCTSTR pStrImage)
  811. {
  812. m_ListInfo.sDisabledImage = pStrImage;
  813. }
  814. DWORD CComboUI::GetDisabledItemTextColor() const
  815. {
  816. return m_ListInfo.dwDisabledTextColor;
  817. }
  818. DWORD CComboUI::GetDisabledItemBkColor() const
  819. {
  820. return m_ListInfo.dwDisabledBkColor;
  821. }
  822. LPCTSTR CComboUI::GetDisabledItemImage() const
  823. {
  824. return m_ListInfo.sDisabledImage;
  825. }
  826. DWORD CComboUI::GetItemLineColor() const
  827. {
  828. return m_ListInfo.dwLineColor;
  829. }
  830. void CComboUI::SetItemLineColor(DWORD dwLineColor)
  831. {
  832. m_ListInfo.dwLineColor = dwLineColor;
  833. }
  834. bool CComboUI::IsItemShowHtml()
  835. {
  836. return m_ListInfo.bShowHtml;
  837. }
  838. void CComboUI::SetItemShowHtml(bool bShowHtml)
  839. {
  840. if( m_ListInfo.bShowHtml == bShowHtml ) return;
  841. m_ListInfo.bShowHtml = bShowHtml;
  842. Invalidate();
  843. }
  844. void CComboUI::SetPos(RECT rc, bool bNeedInvalidate)
  845. {
  846. if(!::EqualRect(&rc, &m_rcItem)) {
  847. // 隐藏下拉窗口
  848. if(m_pWindow && ::IsWindow(m_pWindow->GetHWND())) m_pWindow->Close();
  849. // 所有元素大小置为0
  850. RECT rcNull = { 0 };
  851. for( int i = 0; i < m_items.GetSize(); i++ ) static_cast<CControlUI*>(m_items[i])->SetPos(rcNull);
  852. // 调整位置
  853. CControlUI::SetPos(rc, bNeedInvalidate);
  854. }
  855. }
  856. void CComboUI::Move(SIZE szOffset, bool bNeedInvalidate)
  857. {
  858. CControlUI::Move(szOffset, bNeedInvalidate);
  859. }
  860. void CComboUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  861. {
  862. if( _tcsicmp(pstrName, _T("align")) == 0 ) {
  863. if( _tcsstr(pstrValue, _T("left")) != NULL ) {
  864. m_uTextStyle &= ~(DT_CENTER | DT_RIGHT | DT_SINGLELINE);
  865. m_uTextStyle |= DT_LEFT;
  866. }
  867. if( _tcsstr(pstrValue, _T("center")) != NULL ) {
  868. m_uTextStyle &= ~(DT_LEFT | DT_RIGHT );
  869. m_uTextStyle |= DT_CENTER;
  870. }
  871. if( _tcsstr(pstrValue, _T("right")) != NULL ) {
  872. m_uTextStyle &= ~(DT_LEFT | DT_CENTER | DT_SINGLELINE);
  873. m_uTextStyle |= DT_RIGHT;
  874. }
  875. }
  876. else if( _tcsicmp(pstrName, _T("valign")) == 0 ) {
  877. if( _tcsstr(pstrValue, _T("top")) != NULL ) {
  878. m_uTextStyle &= ~(DT_BOTTOM | DT_VCENTER);
  879. m_uTextStyle |= (DT_TOP | DT_SINGLELINE);
  880. }
  881. if( _tcsstr(pstrValue, _T("vcenter")) != NULL ) {
  882. m_uTextStyle &= ~(DT_TOP | DT_BOTTOM );
  883. m_uTextStyle |= (DT_VCENTER | DT_SINGLELINE);
  884. }
  885. if( _tcsstr(pstrValue, _T("bottom")) != NULL ) {
  886. m_uTextStyle &= ~(DT_TOP | DT_VCENTER);
  887. m_uTextStyle |= (DT_BOTTOM | DT_SINGLELINE);
  888. }
  889. }
  890. else if( _tcsicmp(pstrName, _T("endellipsis")) == 0 ) {
  891. if( _tcsicmp(pstrValue, _T("true")) == 0 ) m_uTextStyle |= DT_END_ELLIPSIS;
  892. else m_uTextStyle &= ~DT_END_ELLIPSIS;
  893. }
  894. else if( _tcsicmp(pstrName, _T("wordbreak")) == 0 ) {
  895. if( _tcsicmp(pstrValue, _T("true")) == 0 ) {
  896. m_uTextStyle &= ~DT_SINGLELINE;
  897. m_uTextStyle |= DT_WORDBREAK | DT_EDITCONTROL;
  898. }
  899. else {
  900. m_uTextStyle &= ~DT_WORDBREAK & ~DT_EDITCONTROL;
  901. m_uTextStyle |= DT_SINGLELINE;
  902. }
  903. }
  904. else if( _tcsicmp(pstrName, _T("font")) == 0 ) SetFont(_ttoi(pstrValue));
  905. else if( _tcsicmp(pstrName, _T("textcolor")) == 0 ) {
  906. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  907. LPTSTR pstr = NULL;
  908. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  909. SetTextColor(clrColor);
  910. }
  911. else if( _tcsicmp(pstrName, _T("disabledtextcolor")) == 0 ) {
  912. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  913. LPTSTR pstr = NULL;
  914. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  915. SetDisabledTextColor(clrColor);
  916. }
  917. else if( _tcsicmp(pstrName, _T("textpadding")) == 0 ) {
  918. RECT rcTextPadding = { 0 };
  919. LPTSTR pstr = NULL;
  920. rcTextPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  921. rcTextPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  922. rcTextPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  923. rcTextPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  924. SetTextPadding(rcTextPadding);
  925. }
  926. else if( _tcsicmp(pstrName, _T("showhtml")) == 0 ) SetShowHtml(_tcsicmp(pstrValue, _T("true")) == 0);
  927. else if( _tcsicmp(pstrName, _T("showshadow")) == 0 ) SetShowShadow(_tcsicmp(pstrValue, _T("true")) == 0);
  928. else if( _tcsicmp(pstrName, _T("normalimage")) == 0 ) SetNormalImage(pstrValue);
  929. else if( _tcsicmp(pstrName, _T("hotimage")) == 0 ) SetHotImage(pstrValue);
  930. else if( _tcsicmp(pstrName, _T("pushedimage")) == 0 ) SetPushedImage(pstrValue);
  931. else if( _tcsicmp(pstrName, _T("focusedimage")) == 0 ) SetFocusedImage(pstrValue);
  932. else if( _tcsicmp(pstrName, _T("disabledimage")) == 0 ) SetDisabledImage(pstrValue);
  933. else if( _tcsicmp(pstrName, _T("scrollselect")) == 0 ) SetScrollSelect(_tcsicmp(pstrValue, _T("true")) == 0);
  934. else if( _tcsicmp(pstrName, _T("dropbox")) == 0 ) SetDropBoxAttributeList(pstrValue);
  935. else if( _tcsicmp(pstrName, _T("dropboxsize")) == 0)
  936. {
  937. SIZE szDropBoxSize = { 0 };
  938. LPTSTR pstr = NULL;
  939. szDropBoxSize.cx = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  940. szDropBoxSize.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  941. SetDropBoxSize(szDropBoxSize);
  942. }
  943. else if( _tcsicmp(pstrName, _T("dropboxinset")) == 0 ) {
  944. RECT rcTextPadding = { 0 };
  945. LPTSTR pstr = NULL;
  946. rcTextPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  947. rcTextPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  948. rcTextPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  949. rcTextPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  950. SetDropBoxInset(rcTextPadding);
  951. }
  952. else if( _tcsicmp(pstrName, _T("itemfont")) == 0 ) SetItemFont(_ttoi(pstrValue));
  953. else if( _tcsicmp(pstrName, _T("itemalign")) == 0 ) {
  954. if( _tcsstr(pstrValue, _T("left")) != NULL ) {
  955. m_ListInfo.uTextStyle &= ~(DT_CENTER | DT_RIGHT);
  956. m_ListInfo.uTextStyle |= DT_LEFT;
  957. }
  958. if( _tcsstr(pstrValue, _T("center")) != NULL ) {
  959. m_ListInfo.uTextStyle &= ~(DT_LEFT | DT_RIGHT);
  960. m_ListInfo.uTextStyle |= DT_CENTER;
  961. }
  962. if( _tcsstr(pstrValue, _T("right")) != NULL ) {
  963. m_ListInfo.uTextStyle &= ~(DT_LEFT | DT_CENTER);
  964. m_ListInfo.uTextStyle |= DT_RIGHT;
  965. }
  966. }
  967. else if( _tcsicmp(pstrName, _T("itemvalign")) == 0 ) {
  968. if( _tcsstr(pstrValue, _T("top")) != NULL ) {
  969. m_ListInfo.uTextStyle &= ~(DT_VCENTER | DT_BOTTOM);
  970. m_ListInfo.uTextStyle |= DT_TOP;
  971. }
  972. if( _tcsstr(pstrValue, _T("vcenter")) != NULL ) {
  973. m_ListInfo.uTextStyle &= ~(DT_TOP | DT_BOTTOM | DT_WORDBREAK);
  974. m_ListInfo.uTextStyle |= DT_VCENTER | DT_SINGLELINE;
  975. }
  976. if( _tcsstr(pstrValue, _T("bottom")) != NULL ) {
  977. m_ListInfo.uTextStyle &= ~(DT_TOP | DT_VCENTER);
  978. m_ListInfo.uTextStyle |= DT_BOTTOM;
  979. }
  980. }
  981. else if( _tcsicmp(pstrName, _T("itemendellipsis")) == 0 ) {
  982. if( _tcsicmp(pstrValue, _T("true")) == 0 ) m_ListInfo.uTextStyle |= DT_END_ELLIPSIS;
  983. else m_ListInfo.uTextStyle &= ~DT_END_ELLIPSIS;
  984. }
  985. else if( _tcsicmp(pstrName, _T("itemtextpadding")) == 0 ) {
  986. RECT rcTextPadding = { 0 };
  987. LPTSTR pstr = NULL;
  988. rcTextPadding.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  989. rcTextPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  990. rcTextPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  991. rcTextPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  992. SetItemTextPadding(rcTextPadding);
  993. }
  994. else if( _tcsicmp(pstrName, _T("itemtextcolor")) == 0 ) {
  995. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  996. LPTSTR pstr = NULL;
  997. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  998. SetItemTextColor(clrColor);
  999. }
  1000. else if( _tcsicmp(pstrName, _T("itembkcolor")) == 0 ) {
  1001. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1002. LPTSTR pstr = NULL;
  1003. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1004. SetItemBkColor(clrColor);
  1005. }
  1006. else if( _tcsicmp(pstrName, _T("itembkimage")) == 0 ) SetItemBkImage(pstrValue);
  1007. else if( _tcsicmp(pstrName, _T("itemaltbk")) == 0 ) SetAlternateBk(_tcsicmp(pstrValue, _T("true")) == 0);
  1008. else if( _tcsicmp(pstrName, _T("itemselectedtextcolor")) == 0 ) {
  1009. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1010. LPTSTR pstr = NULL;
  1011. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1012. SetSelectedItemTextColor(clrColor);
  1013. }
  1014. else if( _tcsicmp(pstrName, _T("itemselectedbkcolor")) == 0 ) {
  1015. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1016. LPTSTR pstr = NULL;
  1017. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1018. SetSelectedItemBkColor(clrColor);
  1019. }
  1020. else if( _tcsicmp(pstrName, _T("itemselectedimage")) == 0 ) SetSelectedItemImage(pstrValue);
  1021. else if( _tcsicmp(pstrName, _T("itemhottextcolor")) == 0 ) {
  1022. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1023. LPTSTR pstr = NULL;
  1024. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1025. SetHotItemTextColor(clrColor);
  1026. }
  1027. else if( _tcsicmp(pstrName, _T("itemhotbkcolor")) == 0 ) {
  1028. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1029. LPTSTR pstr = NULL;
  1030. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1031. SetHotItemBkColor(clrColor);
  1032. }
  1033. else if( _tcsicmp(pstrName, _T("itemhotimage")) == 0 ) SetHotItemImage(pstrValue);
  1034. else if( _tcsicmp(pstrName, _T("itemdisabledtextcolor")) == 0 ) {
  1035. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1036. LPTSTR pstr = NULL;
  1037. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1038. SetDisabledItemTextColor(clrColor);
  1039. }
  1040. else if( _tcsicmp(pstrName, _T("itemdisabledbkcolor")) == 0 ) {
  1041. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1042. LPTSTR pstr = NULL;
  1043. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1044. SetDisabledItemBkColor(clrColor);
  1045. }
  1046. else if( _tcsicmp(pstrName, _T("itemdisabledimage")) == 0 ) SetDisabledItemImage(pstrValue);
  1047. else if( _tcsicmp(pstrName, _T("itemlinecolor")) == 0 ) {
  1048. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  1049. LPTSTR pstr = NULL;
  1050. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  1051. SetItemLineColor(clrColor);
  1052. }
  1053. else if( _tcsicmp(pstrName, _T("itemshowhtml")) == 0 ) SetItemShowHtml(_tcsicmp(pstrValue, _T("true")) == 0);
  1054. else CContainerUI::SetAttribute(pstrName, pstrValue);
  1055. }
  1056. bool CComboUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  1057. {
  1058. return CControlUI::DoPaint(hDC, rcPaint, pStopControl);
  1059. }
  1060. void CComboUI::PaintStatusImage(HDC hDC)
  1061. {
  1062. if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
  1063. else m_uButtonState &= ~ UISTATE_FOCUSED;
  1064. if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
  1065. else m_uButtonState &= ~ UISTATE_DISABLED;
  1066. if( (m_uButtonState & UISTATE_DISABLED) != 0 ) {
  1067. if( !m_sDisabledImage.IsEmpty() ) {
  1068. if( !DrawImage(hDC, (LPCTSTR)m_sDisabledImage) ) {}
  1069. else return;
  1070. }
  1071. }
  1072. else if( (m_uButtonState & UISTATE_PUSHED) != 0 ) {
  1073. if( !m_sPushedImage.IsEmpty() ) {
  1074. if( !DrawImage(hDC, (LPCTSTR)m_sPushedImage) ) {}
  1075. else return;
  1076. }
  1077. }
  1078. else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  1079. if( !m_sHotImage.IsEmpty() ) {
  1080. if( !DrawImage(hDC, (LPCTSTR)m_sHotImage) ) {}
  1081. else return;
  1082. }
  1083. }
  1084. else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) {
  1085. if( !m_sFocusedImage.IsEmpty() ) {
  1086. if( !DrawImage(hDC, (LPCTSTR)m_sFocusedImage) ) {}
  1087. else return;
  1088. }
  1089. }
  1090. if( !m_sNormalImage.IsEmpty() ) {
  1091. if( !DrawImage(hDC, (LPCTSTR)m_sNormalImage) ) {}
  1092. else return;
  1093. }
  1094. }
  1095. void CComboUI::PaintText(HDC hDC)
  1096. {
  1097. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  1098. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  1099. RECT rc = m_rcItem;
  1100. rc.left += m_rcTextPadding.left;
  1101. rc.right -= m_rcTextPadding.right;
  1102. rc.top += m_rcTextPadding.top;
  1103. rc.bottom -= m_rcTextPadding.bottom;
  1104. CDuiString sText = GetText();
  1105. if( sText.IsEmpty() ) return;
  1106. int nLinks = 0;
  1107. if( IsEnabled() ) {
  1108. if( m_bShowHtml )
  1109. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, sText, m_dwTextColor, NULL, NULL, nLinks, m_iFont, m_uTextStyle);
  1110. else
  1111. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwTextColor, m_iFont, m_uTextStyle);
  1112. }
  1113. else {
  1114. if( m_bShowHtml )
  1115. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, NULL, NULL, nLinks, m_iFont, m_uTextStyle);
  1116. else
  1117. CRenderEngine::DrawText(hDC, m_pManager, rc, sText, m_dwDisabledTextColor, m_iFont, m_uTextStyle);
  1118. }
  1119. }
  1120. BOOL CComboUI::SortItems(PULVCompareFunc pfnCompare, UINT_PTR dwData)
  1121. {
  1122. if (!pfnCompare)
  1123. return FALSE;
  1124. m_pCompareFunc = pfnCompare;
  1125. m_compareData = dwData;
  1126. qsort_s(m_items.GetData(), m_items.GetSize(), sizeof(CControlUI*), CComboUI::ItemComareFunc, this);
  1127. IListItemUI* pItem = NULL;
  1128. for (int i = 0; i < m_items.GetSize(); ++i)
  1129. {
  1130. pItem = (IListItemUI*)(static_cast<CControlUI*>(m_items[i])->GetInterface(TEXT("ListItem")));
  1131. if (pItem)
  1132. {
  1133. pItem->SetIndex(i);
  1134. pItem->Select(false);
  1135. }
  1136. }
  1137. if (m_pManager)
  1138. {
  1139. SetPos(GetPos());
  1140. Invalidate();
  1141. }
  1142. return TRUE;
  1143. }
  1144. static int __cdecl ComareFunc(void* pvlocale, const void* item1, const void* item2)
  1145. {
  1146. CComboUI* pThis = (CComboUI*)pvlocale;
  1147. if (!pThis || !item1 || !item2)
  1148. return 0;
  1149. CControlUI* pControl1 = *(CControlUI**)item1;
  1150. CControlUI* pControl2 = *(CControlUI**)item2;
  1151. if (!pControl1 || !pControl2)
  1152. {
  1153. return 0;
  1154. }
  1155. return pControl1->GetText().Compare(pControl2->GetText());
  1156. }
  1157. void CComboUI::SortItems()
  1158. {
  1159. qsort_s(m_items.GetData(), m_items.GetSize(), sizeof(CControlUI*), ComareFunc, this);
  1160. IListItemUI* pItem = NULL;
  1161. for (int i = 0; i < m_items.GetSize(); ++i)
  1162. {
  1163. pItem = (IListItemUI*)(static_cast<CControlUI*>(m_items[i])->GetInterface(TEXT("ListItem")));
  1164. if (pItem)
  1165. {
  1166. pItem->SetIndex(i);
  1167. pItem->Select(false);
  1168. }
  1169. }
  1170. if (m_pManager)
  1171. {
  1172. SetPos(GetPos());
  1173. Invalidate();
  1174. }
  1175. return ;
  1176. }
  1177. int __cdecl CComboUI::ItemComareFunc(void* pvlocale, const void* item1, const void* item2)
  1178. {
  1179. CComboUI* pThis = (CComboUI*)pvlocale;
  1180. if (!pThis || !item1 || !item2)
  1181. return 0;
  1182. return pThis->ItemComareFunc(item1, item2);
  1183. }
  1184. int __cdecl CComboUI::ItemComareFunc(const void* item1, const void* item2)
  1185. {
  1186. if (!m_pCompareFunc)
  1187. {
  1188. return 0;
  1189. }
  1190. CControlUI* pControl1 = *(CControlUI**)item1;
  1191. CControlUI* pControl2 = *(CControlUI**)item2;
  1192. return m_pCompareFunc((UINT_PTR)pControl1, (UINT_PTR)pControl2, m_compareData);
  1193. }
  1194. } // namespace DuiLib