UIButton.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. #include "stdafx.h"
  2. #include "UIButton.h"
  3. namespace DuiLib
  4. {
  5. CButtonUI::CButtonUI()
  6. : m_uButtonState(0)
  7. , m_dwHotTextColor(0)
  8. , m_dwPushedTextColor(0)
  9. , m_dwFocusedTextColor(0)
  10. , m_dwHotBkColor(0)
  11. , m_uFadeAlphaDelta(0)
  12. , m_uFadeAlpha(255)
  13. {
  14. m_uTextStyle = DT_SINGLELINE | DT_VCENTER | DT_CENTER;
  15. }
  16. LPCTSTR CButtonUI::GetClass() const
  17. {
  18. return DUI_CTR_BUTTON;
  19. }
  20. LPVOID CButtonUI::GetInterface(LPCTSTR pstrName)
  21. {
  22. if( _tcscmp(pstrName, DUI_CTR_BUTTON) == 0 ) return static_cast<CButtonUI*>(this);
  23. return CLabelUI::GetInterface(pstrName);
  24. }
  25. UINT CButtonUI::GetControlFlags() const
  26. {
  27. return (IsKeyboardEnabled() ? UIFLAG_TABSTOP : 0) | (IsEnabled() ? UIFLAG_SETCURSOR : 0);
  28. }
  29. void CButtonUI::DoEvent(TEventUI& event)
  30. {
  31. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  32. if( m_pParent != NULL ) m_pParent->DoEvent(event);
  33. else CLabelUI::DoEvent(event);
  34. return;
  35. }
  36. if( event.Type == UIEVENT_SETFOCUS )
  37. {
  38. Invalidate();
  39. }
  40. if( event.Type == UIEVENT_KILLFOCUS )
  41. {
  42. Invalidate();
  43. }
  44. if( event.Type == UIEVENT_KEYDOWN )
  45. {
  46. if (IsKeyboardEnabled() && IsEnabled()) {
  47. if( event.chKey == VK_SPACE || event.chKey == VK_RETURN ) {
  48. Activate();
  49. return;
  50. }
  51. }
  52. }
  53. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
  54. {
  55. if( ::PtInRect(&m_rcItem, event.ptMouse) && IsEnabled() ) {
  56. m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
  57. Invalidate();
  58. }
  59. return;
  60. }
  61. if( event.Type == UIEVENT_MOUSEMOVE )
  62. {
  63. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  64. if( ::PtInRect(&m_rcItem, event.ptMouse) ) m_uButtonState |= UISTATE_PUSHED;
  65. else m_uButtonState &= ~UISTATE_PUSHED;
  66. Invalidate();
  67. }
  68. return;
  69. }
  70. if( event.Type == UIEVENT_BUTTONUP )
  71. {
  72. if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
  73. if( ::PtInRect(&m_rcItem, event.ptMouse) && IsEnabled()) Activate();
  74. m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
  75. Invalidate();
  76. }
  77. return;
  78. }
  79. if( event.Type == UIEVENT_CONTEXTMENU )
  80. {
  81. if( IsContextMenuUsed() && IsEnabled()) {
  82. m_pManager->SendNotify(this, DUI_MSGTYPE_MENU, event.wParam, event.lParam);
  83. }
  84. return;
  85. }
  86. if( event.Type == UIEVENT_MOUSEENTER )
  87. {
  88. if( ::PtInRect(&m_rcItem, event.ptMouse ) ) {
  89. if( IsEnabled() ) {
  90. if( (m_uButtonState & UISTATE_HOT) == 0 ) {
  91. m_uButtonState |= UISTATE_HOT;
  92. Invalidate();
  93. }
  94. }
  95. }
  96. if ( GetFadeAlphaDelta() > 0 ) {
  97. m_pManager->SetTimer(this, FADE_TIMERID, FADE_ELLAPSE);
  98. }
  99. }
  100. if( event.Type == UIEVENT_MOUSELEAVE )
  101. {
  102. if( !::PtInRect(&m_rcItem, event.ptMouse ) ) {
  103. if( IsEnabled() ) {
  104. if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  105. m_uButtonState &= ~UISTATE_HOT;
  106. Invalidate();
  107. }
  108. }
  109. if (m_pManager) m_pManager->RemoveMouseLeaveNeeded(this);
  110. if ( GetFadeAlphaDelta() > 0 ) {
  111. m_pManager->SetTimer(this, FADE_TIMERID, FADE_ELLAPSE);
  112. }
  113. }
  114. else {
  115. if (m_pManager) m_pManager->AddMouseLeaveNeeded(this);
  116. return;
  117. }
  118. }
  119. if( event.Type == UIEVENT_SETCURSOR )
  120. {
  121. ::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
  122. return;
  123. }
  124. if( event.Type == UIEVENT_TIMER && event.wParam == FADE_TIMERID )
  125. {
  126. if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  127. if( m_uFadeAlpha > m_uFadeAlphaDelta ) m_uFadeAlpha -= m_uFadeAlphaDelta;
  128. else {
  129. m_uFadeAlpha = 0;
  130. m_pManager->KillTimer(this, FADE_TIMERID);
  131. }
  132. }
  133. else {
  134. if( m_uFadeAlpha < 255-m_uFadeAlphaDelta ) m_uFadeAlpha += m_uFadeAlphaDelta;
  135. else {
  136. m_uFadeAlpha = 255;
  137. m_pManager->KillTimer(this, FADE_TIMERID);
  138. }
  139. }
  140. Invalidate();
  141. return;
  142. }
  143. CLabelUI::DoEvent(event);
  144. }
  145. bool CButtonUI::Activate()
  146. {
  147. if( !CControlUI::Activate() ) return false;
  148. if( m_pManager != NULL ) m_pManager->SendNotify(this, DUI_MSGTYPE_CLICK);
  149. return true;
  150. }
  151. void CButtonUI::SetEnabled(bool bEnable)
  152. {
  153. CControlUI::SetEnabled(bEnable);
  154. if( !IsEnabled() ) {
  155. m_uButtonState = 0;
  156. }
  157. }
  158. void CButtonUI::SetHotBkColor( DWORD dwColor )
  159. {
  160. m_dwHotBkColor = dwColor;
  161. }
  162. DWORD CButtonUI::GetHotBkColor() const
  163. {
  164. return m_dwHotBkColor;
  165. }
  166. void CButtonUI::SetHotTextColor(DWORD dwColor)
  167. {
  168. m_dwHotTextColor = dwColor;
  169. }
  170. DWORD CButtonUI::GetHotTextColor() const
  171. {
  172. return m_dwHotTextColor;
  173. }
  174. void CButtonUI::SetPushedTextColor(DWORD dwColor)
  175. {
  176. m_dwPushedTextColor = dwColor;
  177. }
  178. DWORD CButtonUI::GetPushedTextColor() const
  179. {
  180. return m_dwPushedTextColor;
  181. }
  182. void CButtonUI::SetFocusedTextColor(DWORD dwColor)
  183. {
  184. m_dwFocusedTextColor = dwColor;
  185. }
  186. DWORD CButtonUI::GetFocusedTextColor() const
  187. {
  188. return m_dwFocusedTextColor;
  189. }
  190. LPCTSTR CButtonUI::GetNormalImage()
  191. {
  192. return m_diNormal.sDrawString;
  193. }
  194. void CButtonUI::SetNormalImage(LPCTSTR pStrImage)
  195. {
  196. if( m_diNormal.sDrawString == pStrImage && m_diNormal.pImageInfo != NULL ) return;
  197. m_diNormal.Clear();
  198. m_diNormal.sDrawString = pStrImage;
  199. Invalidate();
  200. }
  201. LPCTSTR CButtonUI::GetHotImage()
  202. {
  203. return m_diHot.sDrawString;
  204. }
  205. void CButtonUI::SetHotImage(LPCTSTR pStrImage)
  206. {
  207. if( m_diHot.sDrawString == pStrImage && m_diHot.pImageInfo != NULL ) return;
  208. m_diHot.Clear();
  209. m_diHot.sDrawString = pStrImage;
  210. Invalidate();
  211. }
  212. LPCTSTR CButtonUI::GetPushedImage()
  213. {
  214. return m_diPushed.sDrawString;
  215. }
  216. void CButtonUI::SetPushedImage(LPCTSTR pStrImage)
  217. {
  218. if( m_diPushed.sDrawString == pStrImage && m_diPushed.pImageInfo != NULL ) return;
  219. m_diPushed.Clear();
  220. m_diPushed.sDrawString = pStrImage;
  221. Invalidate();
  222. }
  223. LPCTSTR CButtonUI::GetFocusedImage()
  224. {
  225. return m_diFocused.sDrawString;
  226. }
  227. void CButtonUI::SetFocusedImage(LPCTSTR pStrImage)
  228. {
  229. if( m_diFocused.sDrawString == pStrImage && m_diFocused.pImageInfo != NULL ) return;
  230. m_diFocused.Clear();
  231. m_diFocused.sDrawString = pStrImage;
  232. Invalidate();
  233. }
  234. LPCTSTR CButtonUI::GetDisabledImage()
  235. {
  236. return m_diDisabled.sDrawString;
  237. }
  238. void CButtonUI::SetDisabledImage(LPCTSTR pStrImage)
  239. {
  240. if( m_diDisabled.sDrawString == pStrImage && m_diDisabled.pImageInfo != NULL ) return;
  241. m_diDisabled.Clear();
  242. m_diDisabled.sDrawString = pStrImage;
  243. Invalidate();
  244. }
  245. LPCTSTR CButtonUI::GetForeImage()
  246. {
  247. return m_diFore.sDrawString;
  248. }
  249. void CButtonUI::SetForeImage( LPCTSTR pStrImage )
  250. {
  251. if( m_diFore.sDrawString == pStrImage && m_diFore.pImageInfo != NULL ) return;
  252. m_diFore.Clear();
  253. m_diFore.sDrawString = pStrImage;
  254. Invalidate();
  255. }
  256. LPCTSTR CButtonUI::GetHotForeImage()
  257. {
  258. return m_diHotFore.sDrawString;
  259. }
  260. void CButtonUI::SetHotForeImage( LPCTSTR pStrImage )
  261. {
  262. if( m_diHotFore.sDrawString == pStrImage && m_diHotFore.pImageInfo != NULL ) return;
  263. m_diHotFore.Clear();
  264. m_diHotFore.sDrawString = pStrImage;
  265. Invalidate();
  266. }
  267. void CButtonUI::SetFiveStatusImage(LPCTSTR pStrImage)
  268. {
  269. m_diNormal.Clear();
  270. m_diNormal.sDrawString = pStrImage;
  271. DrawImage(NULL, m_diNormal);
  272. if (m_diNormal.pImageInfo) {
  273. LONG width = m_diNormal.pImageInfo->nX / 5;
  274. LONG height = m_diNormal.pImageInfo->nY;
  275. m_diNormal.rcBmpPart = CDuiRect(0, 0, width, height);
  276. if( m_bFloat && m_cxyFixed.cx == 0 && m_cxyFixed.cy == 0 ) {
  277. m_cxyFixed.cx = width;
  278. m_cxyFixed.cy = height;
  279. }
  280. }
  281. m_diPushed.Clear();
  282. m_diPushed.sDrawString = pStrImage;
  283. DrawImage(NULL, m_diPushed);
  284. if (m_diPushed.pImageInfo) {
  285. LONG width = m_diPushed.pImageInfo->nX / 5;
  286. LONG height = m_diPushed.pImageInfo->nY;
  287. m_diPushed.rcBmpPart = CDuiRect(width, 0, width*2, height);
  288. }
  289. m_diHot.Clear();
  290. m_diHot.sDrawString = pStrImage;
  291. DrawImage(NULL, m_diHot);
  292. if (m_diHot.pImageInfo) {
  293. LONG width = m_diHot.pImageInfo->nX / 5;
  294. LONG height = m_diHot.pImageInfo->nY;
  295. m_diHot.rcBmpPart = CDuiRect(width*2, 0, width*3, height);
  296. }
  297. m_diFocused.Clear();
  298. m_diFocused.sDrawString = pStrImage;
  299. DrawImage(NULL, m_diFocused);
  300. if (m_diFocused.pImageInfo) {
  301. LONG width = m_diFocused.pImageInfo->nX / 5;
  302. LONG height = m_diFocused.pImageInfo->nY;
  303. m_diFocused.rcBmpPart = CDuiRect(width*3, 0, width*4, height);
  304. }
  305. m_diDisabled.Clear();
  306. m_diDisabled.sDrawString = pStrImage;
  307. DrawImage(NULL, m_diDisabled);
  308. if (m_diDisabled.pImageInfo) {
  309. LONG width = m_diDisabled.pImageInfo->nX / 5;
  310. LONG height = m_diDisabled.pImageInfo->nY;
  311. m_diDisabled.rcBmpPart = CDuiRect(width*4, 0, width*5, height);
  312. }
  313. Invalidate();
  314. }
  315. void CButtonUI::SetFadeAlphaDelta(BYTE uDelta)
  316. {
  317. m_uFadeAlphaDelta = uDelta;
  318. }
  319. BYTE CButtonUI::GetFadeAlphaDelta()
  320. {
  321. return m_uFadeAlphaDelta;
  322. }
  323. SIZE CButtonUI::EstimateSize(SIZE szAvailable)
  324. {
  325. if( m_cxyFixed.cy == 0 ) return CDuiSize(m_cxyFixed.cx, m_pManager->GetFontInfo(GetFont())->tm.tmHeight + 8);
  326. return CControlUI::EstimateSize(szAvailable);
  327. }
  328. void CButtonUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  329. {
  330. if( _tcscmp(pstrName, _T("normalimage")) == 0 ) SetNormalImage(pstrValue);
  331. else if( _tcscmp(pstrName, _T("hotimage")) == 0 ) SetHotImage(pstrValue);
  332. else if( _tcscmp(pstrName, _T("pushedimage")) == 0 ) SetPushedImage(pstrValue);
  333. else if( _tcscmp(pstrName, _T("focusedimage")) == 0 ) SetFocusedImage(pstrValue);
  334. else if( _tcscmp(pstrName, _T("disabledimage")) == 0 ) SetDisabledImage(pstrValue);
  335. else if( _tcscmp(pstrName, _T("foreimage")) == 0 ) SetForeImage(pstrValue);
  336. else if( _tcscmp(pstrName, _T("hotforeimage")) == 0 ) SetHotForeImage(pstrValue);
  337. else if( _tcscmp(pstrName, _T("fivestatusimage")) == 0 ) SetFiveStatusImage(pstrValue);
  338. else if( _tcscmp(pstrName, _T("fadedelta")) == 0 ) SetFadeAlphaDelta((BYTE)_ttoi(pstrValue));
  339. else if( _tcscmp(pstrName, _T("hotbkcolor")) == 0 )
  340. {
  341. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  342. LPTSTR pstr = NULL;
  343. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  344. SetHotBkColor(clrColor);
  345. }
  346. else if( _tcscmp(pstrName, _T("hottextcolor")) == 0 )
  347. {
  348. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  349. LPTSTR pstr = NULL;
  350. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  351. SetHotTextColor(clrColor);
  352. }
  353. else if( _tcscmp(pstrName, _T("pushedtextcolor")) == 0 )
  354. {
  355. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  356. LPTSTR pstr = NULL;
  357. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  358. SetPushedTextColor(clrColor);
  359. }
  360. else if( _tcscmp(pstrName, _T("focusedtextcolor")) == 0 )
  361. {
  362. if( *pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
  363. LPTSTR pstr = NULL;
  364. DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
  365. SetFocusedTextColor(clrColor);
  366. }
  367. else CLabelUI::SetAttribute(pstrName, pstrValue);
  368. }
  369. void CButtonUI::PaintText(HDC hDC)
  370. {
  371. if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
  372. else m_uButtonState &= ~ UISTATE_FOCUSED;
  373. if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
  374. else m_uButtonState &= ~ UISTATE_DISABLED;
  375. if( m_dwTextColor == 0 ) m_dwTextColor = m_pManager->GetDefaultFontColor();
  376. if( m_dwDisabledTextColor == 0 ) m_dwDisabledTextColor = m_pManager->GetDefaultDisabledColor();
  377. if( m_sText.IsEmpty() ) return;
  378. int nLinks = 0;
  379. RECT rc = m_rcItem;
  380. rc.left += m_rcTextPadding.left;
  381. rc.right -= m_rcTextPadding.right;
  382. rc.top += m_rcTextPadding.top;
  383. rc.bottom -= m_rcTextPadding.bottom;
  384. DWORD clrColor = IsEnabled()?m_dwTextColor:m_dwDisabledTextColor;
  385. if( ((m_uButtonState & UISTATE_PUSHED) != 0) && (GetPushedTextColor() != 0) )
  386. clrColor = GetPushedTextColor();
  387. else if( ((m_uButtonState & UISTATE_HOT) != 0) && (GetHotTextColor() != 0) )
  388. clrColor = GetHotTextColor();
  389. else if( ((m_uButtonState & UISTATE_FOCUSED) != 0) && (GetFocusedTextColor() != 0) )
  390. clrColor = GetFocusedTextColor();
  391. if( m_bShowHtml )
  392. CRenderEngine::DrawHtmlText(hDC, m_pManager, rc, m_sText, clrColor, \
  393. NULL, NULL, nLinks, m_iFont, m_uTextStyle);
  394. else
  395. CRenderEngine::DrawText(hDC, m_pManager, rc, m_sText, clrColor, \
  396. m_iFont, m_uTextStyle);
  397. }
  398. void CButtonUI::PaintStatusImage(HDC hDC)
  399. {
  400. if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
  401. else m_uButtonState &= ~ UISTATE_FOCUSED;
  402. if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
  403. else m_uButtonState &= ~ UISTATE_DISABLED;
  404. if( (m_uButtonState & UISTATE_DISABLED) != 0 ) {
  405. if (DrawImage(hDC, m_diDisabled)) goto Label_ForeImage;
  406. }
  407. else if( (m_uButtonState & UISTATE_PUSHED) != 0 ) {
  408. if (!DrawImage(hDC, m_diPushed))
  409. DrawImage(hDC, m_diNormal);
  410. if (DrawImage(hDC, m_diPushedFore)) return;
  411. else goto Label_ForeImage;
  412. }
  413. else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
  414. if( GetFadeAlphaDelta() > 0 ) {
  415. if( m_uFadeAlpha == 0 ) {
  416. m_diHot.uFade = 255;
  417. DrawImage(hDC, m_diHot);
  418. }
  419. else {
  420. m_diNormal.uFade = m_uFadeAlpha;
  421. DrawImage(hDC, m_diNormal);
  422. m_diHot.uFade = 255 - m_uFadeAlpha;
  423. DrawImage(hDC, m_diHot);
  424. }
  425. }
  426. else {
  427. if (!DrawImage(hDC, m_diHot))
  428. DrawImage(hDC, m_diNormal);
  429. }
  430. if (DrawImage(hDC, m_diHotFore)) return;
  431. else if(m_dwHotBkColor != 0) {
  432. CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwHotBkColor));
  433. return;
  434. }
  435. else goto Label_ForeImage;
  436. }
  437. else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) {
  438. if (DrawImage(hDC, m_diFocused)) goto Label_ForeImage;;
  439. }
  440. if ( GetFadeAlphaDelta() > 0 ) {
  441. if( m_uFadeAlpha == 255 ) {
  442. m_diNormal.uFade = 255;
  443. DrawImage(hDC, m_diNormal);
  444. }
  445. else {
  446. m_diHot.uFade = 255 - m_uFadeAlpha;
  447. DrawImage(hDC, m_diHot);
  448. m_diNormal.uFade = m_uFadeAlpha;
  449. DrawImage(hDC, m_diNormal);
  450. }
  451. }
  452. else {
  453. DrawImage(hDC, m_diNormal);
  454. }
  455. Label_ForeImage:
  456. DrawImage(hDC, m_diFore);
  457. }
  458. }