HoverButton.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // HoverButton.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ipos.h"
  5. #include "HoverButton.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CHoverButton
  13. BEGIN_MESSAGE_MAP(CHoverButton, CBitmapButton)
  14. //{{AFX_MSG_MAP(CHoverButton)
  15. ON_WM_MOUSEMOVE()
  16. ON_WM_LBUTTONDOWN()
  17. ON_WM_LBUTTONUP()
  18. //}}AFX_MSG_MAP
  19. END_MESSAGE_MAP()
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CHoverButton construction / destruction
  22. CHoverButton::CHoverButton()
  23. {
  24. // To start with, the button is switched off and we are NOT tracking the mouse
  25. m_ButtonState = BUTTON_OFF;
  26. m_bMouseTracking = FALSE;
  27. m_bTabButton = false;
  28. m_bTabState = false;
  29. }
  30. CHoverButton::~CHoverButton()
  31. {
  32. }
  33. void CHoverButton::SetTabState( bool bTabState )
  34. {
  35. m_bTabState = bTabState;
  36. }
  37. bool CHoverButton::GetTabState()
  38. {
  39. return m_bTabState;
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CHoverButton message handlers
  43. void CHoverButton::OnMouseMove(UINT nFlags, CPoint point)
  44. {
  45. CBitmapButton::OnMouseMove(nFlags, point);
  46. // 1. Mouse has moved and we are not tracking this button, or
  47. // 2. mouse has moved and the cursor was not above this window
  48. // == Is equivalent to WM_MOUSEENTER (for which there is no message)
  49. if( !m_bTabButton )
  50. {
  51. // OnMouseEnter();
  52. //*
  53. if((!m_bMouseTracking || GetCapture()!=this) && (m_ButtonState != BUTTON_GREYED))
  54. {
  55. OnMouseEnter();
  56. }
  57. else
  58. {
  59. if( m_ButtonState != BUTTON_GREYED )
  60. {
  61. CRect rc;
  62. GetClientRect(&rc);
  63. if(!rc.PtInRect(point)) // The mouse cursor is no longer above this button
  64. OnMouseLeave();
  65. }
  66. }
  67. //*/
  68. }
  69. else
  70. {
  71. if((!m_bMouseTracking || GetCapture()!=this)
  72. && (m_ButtonState != BUTTON_GREYED) )
  73. {
  74. OnMouseEnter();
  75. }
  76. else
  77. {
  78. if( m_ButtonState != BUTTON_GREYED )
  79. {
  80. CRect rc;
  81. GetClientRect(&rc);
  82. if(!rc.PtInRect(point)) // The mouse cursor is no longer above this button
  83. OnMouseLeave();
  84. }
  85. }
  86. }
  87. }
  88. void CHoverButton::OnMouseEnter(void)
  89. {
  90. // We are now tracking the mouse, OVER this button
  91. m_bMouseTracking = TRUE;
  92. if( !m_bTabButton )
  93. m_ButtonState = BUTTON_OVER;
  94. else if( m_ButtonState != BUTTON_ON && m_bTabState != true )
  95. m_ButtonState = BUTTON_OVER;
  96. // Ensure that mouse input is sent to the button
  97. SetCapture();
  98. Invalidate( false );
  99. // UpdateWindow();
  100. }
  101. void CHoverButton::OnMouseLeave(void)
  102. {
  103. // We are not tracking the mouse, this button is OFF.
  104. if( !m_bTabButton )
  105. m_ButtonState = BUTTON_OFF;
  106. else if( m_bTabState != true )
  107. m_ButtonState = BUTTON_OFF;
  108. m_bMouseTracking = FALSE;
  109. // Release mouse capture from the button and restore normal mouse input
  110. Invalidate( false );
  111. // UpdateWindow();
  112. ReleaseCapture();
  113. }
  114. void CHoverButton::OnLButtonDown(UINT nFlags, CPoint point)
  115. {
  116. if( !m_bTabButton )
  117. SetButtonState(BUTTON_ON);
  118. else if( m_ButtonState != BUTTON_GREYED )
  119. {
  120. // if( m_ButtonState == BUTTON_ON )
  121. // SetButtonState(BUTTON_OFF);
  122. // else
  123. SetButtonState(BUTTON_ON);
  124. }
  125. CBitmapButton::OnLButtonDown(nFlags, point);
  126. }
  127. void CHoverButton::OnLButtonUp(UINT nFlags, CPoint point)
  128. {
  129. if( !m_bTabButton )
  130. SetButtonState(BUTTON_OVER); // Highlight button
  131. else
  132. {
  133. if( m_ButtonState == BUTTON_OFF )
  134. SetButtonState(BUTTON_OVER);
  135. }
  136. CBitmapButton::OnLButtonUp(nFlags, point);
  137. }
  138. // Purpose: Set the new state of the button
  139. // Return: Returns the old state of the button
  140. // Parameters: nState = Either ON or OFF. The default is OFF. This is NOT tri-state button!
  141. BUTTON_STATE CHoverButton::SetButtonState(BUTTON_STATE nState)
  142. {
  143. BUTTON_STATE nOldState = (BUTTON_STATE)GetCheck();
  144. BUTTON_STATE m_OldButtonState = m_ButtonState;
  145. m_ButtonState = nState;
  146. switch(m_ButtonState)
  147. {
  148. case BUTTON_ON:
  149. EnableWindow(TRUE);
  150. SetState(BUTTON_ON);
  151. break;
  152. case BUTTON_GREYED:
  153. EnableWindow(FALSE);
  154. break;
  155. case BUTTON_OVER:
  156. EnableWindow(TRUE);
  157. SetState(BUTTON_OVER);
  158. break;
  159. default:
  160. EnableWindow(TRUE);
  161. SetState(BUTTON_OFF);
  162. m_ButtonState = BUTTON_OFF;
  163. break;
  164. }
  165. if( m_OldButtonState != m_ButtonState )
  166. Invalidate(TRUE);
  167. return(nOldState);
  168. }
  169. // Draws the buttons in their relevant state, and text labels
  170. void CHoverButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  171. {
  172. CDC memDC;
  173. CBitmap* pOld=NULL;
  174. CBitmap* pBitmap=NULL;
  175. CDC* pDC;
  176. CRect rc;
  177. int iSaveDC;
  178. pDC= CDC::FromHandle(lpDrawItemStruct->hDC);
  179. memDC.CreateCompatibleDC(pDC);
  180. VERIFY(pDC);
  181. iSaveDC=pDC->SaveDC();
  182. rc.CopyRect(&lpDrawItemStruct->rcItem);
  183. pDC->SetBkMode(TRANSPARENT);
  184. pDC->SetTextColor(GetSysColor(COLOR_WINDOWFRAME));// Black text color
  185. switch(m_ButtonState)
  186. {
  187. case BUTTON_ON:
  188. pBitmap=&m_bmpButtonDown;
  189. break;
  190. case BUTTON_OVER:
  191. pBitmap=&m_bmpButtonFocussed;
  192. break;
  193. case BUTTON_GREYED:
  194. pBitmap=&m_bmpButtonDisabled;
  195. //AfxMessageBox("BUTTON_GREYED");
  196. break;
  197. default:
  198. pBitmap=&m_bmpButtonUp;
  199. break;
  200. }
  201. CString strTitle;
  202. GetWindowText(strTitle);
  203. if (pBitmap->m_hObject)
  204. {
  205. CRect rcBitmap(rc);
  206. BITMAP bmpInfo;
  207. CSize size;
  208. // Text
  209. size = pDC->GetTextExtent(strTitle);
  210. rcBitmap.OffsetRect(size.cx+5,0);
  211. // Draw bitmap
  212. if(!pBitmap->GetBitmap(&bmpInfo))
  213. return;
  214. pOld=memDC.SelectObject((CBitmap*) pBitmap);
  215. if (pOld==NULL)
  216. return; //Destructors will clean up
  217. if(!pDC->BitBlt(0, 0, rc.Width(), rc.Height(), &memDC, 0, 0, SRCCOPY))
  218. return;
  219. memDC.SelectObject(pOld);
  220. if(memDC==NULL)
  221. return;
  222. }
  223. /*
  224. CRect rcText(rc);
  225. UINT nFormat = DT_CENTER;
  226. if(m_ButtonState == BUTTON_GREYED)
  227. {
  228. rcText.OffsetRect(1,1);
  229. pDC->SetTextColor(GetSysColor(COLOR_BTNHIGHLIGHT));
  230. pDC->DrawText(strTitle,rcText,nFormat);
  231. rcText.OffsetRect(-1,-1);
  232. pDC->SetTextColor(GetSysColor(COLOR_BTNSHADOW));
  233. pDC->DrawText(strTitle,rcText,nFormat);
  234. }
  235. else
  236. pDC->DrawText(strTitle,rcText,nFormat);
  237. //*/
  238. pDC->RestoreDC(iSaveDC);
  239. }
  240. BOOL CHoverButton::LoadBitmaps(UINT nBitmapUp, UINT nBitmapDown,
  241. UINT nBitmapFocus, UINT nBitmapDisabled)
  242. {
  243. return LoadBitmaps(MAKEINTRESOURCE(nBitmapUp),
  244. MAKEINTRESOURCE(nBitmapDown),
  245. MAKEINTRESOURCE(nBitmapFocus),
  246. MAKEINTRESOURCE(nBitmapDisabled));
  247. }
  248. BOOL CHoverButton::LoadBitmaps(LPCSTR lpszBitmapUp, LPCSTR lpszBitmapDown,
  249. LPCSTR lpszBitmapFocus, LPCSTR lpszBitmapDisabled)
  250. {
  251. BOOL bAllLoaded=TRUE;
  252. //Delete old ones
  253. m_bmpButtonDown.DeleteObject();
  254. m_bmpButtonFocussed.DeleteObject();
  255. m_bmpButtonUp.DeleteObject();
  256. m_bmpButtonDisabled.DeleteObject();
  257. if (!m_bmpButtonUp.LoadBitmap(lpszBitmapUp))
  258. {
  259. TRACE0("Failed to load up bitmap of bitmap button\n");
  260. return FALSE;
  261. }
  262. if (lpszBitmapDown!=NULL)
  263. {
  264. if (!m_bmpButtonDown.LoadBitmap(lpszBitmapDown))
  265. {
  266. TRACE0("Failed to load down bitmap of bitmap button\n");
  267. return bAllLoaded=FALSE;
  268. }
  269. }
  270. if (lpszBitmapFocus!=NULL)
  271. {
  272. if (!m_bmpButtonFocussed.LoadBitmap(lpszBitmapFocus))
  273. {
  274. TRACE0("Failed to load focussed bitmap of bitmap button\n");
  275. return bAllLoaded=FALSE;
  276. }
  277. }
  278. if (lpszBitmapDisabled!=NULL)
  279. {
  280. if (!m_bmpButtonDisabled.LoadBitmap(lpszBitmapDisabled))
  281. {
  282. TRACE0("Failed to load disabled bitmap of bitmap button\n");
  283. return bAllLoaded=FALSE;
  284. }
  285. }
  286. return bAllLoaded;
  287. }