/DuiLib/Control/UIButton.cpp

https://github.com/wangchyz/duilib · C++ · 444 lines · 347 code · 44 blank · 53 comment · 158 complexity · 8d723886d28ac90fb7fe9be8798168fd MD5 · raw file

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