123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- // HoverButton.cpp : implementation file
- //
- #include "stdafx.h"
- #include "ipos.h"
- #include "HoverButton.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CHoverButton
- BEGIN_MESSAGE_MAP(CHoverButton, CBitmapButton)
- //{{AFX_MSG_MAP(CHoverButton)
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CHoverButton construction / destruction
- CHoverButton::CHoverButton()
- {
- // To start with, the button is switched off and we are NOT tracking the mouse
- m_ButtonState = BUTTON_OFF;
- m_bMouseTracking = FALSE;
- m_bTabButton = false;
- m_bTabState = false;
- }
- CHoverButton::~CHoverButton()
- {
- }
- void CHoverButton::SetTabState( bool bTabState )
- {
- m_bTabState = bTabState;
- }
- bool CHoverButton::GetTabState()
- {
- return m_bTabState;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CHoverButton message handlers
- void CHoverButton::OnMouseMove(UINT nFlags, CPoint point)
- {
- CBitmapButton::OnMouseMove(nFlags, point);
- // 1. Mouse has moved and we are not tracking this button, or
- // 2. mouse has moved and the cursor was not above this window
- // == Is equivalent to WM_MOUSEENTER (for which there is no message)
- if( !m_bTabButton )
- {
- // OnMouseEnter();
- //*
- if((!m_bMouseTracking || GetCapture()!=this) && (m_ButtonState != BUTTON_GREYED))
- {
- OnMouseEnter();
- }
- else
- {
- if( m_ButtonState != BUTTON_GREYED )
- {
- CRect rc;
- GetClientRect(&rc);
- if(!rc.PtInRect(point)) // The mouse cursor is no longer above this button
- OnMouseLeave();
- }
- }
- //*/
- }
- else
- {
- if((!m_bMouseTracking || GetCapture()!=this)
- && (m_ButtonState != BUTTON_GREYED) )
- {
- OnMouseEnter();
- }
- else
- {
- if( m_ButtonState != BUTTON_GREYED )
- {
- CRect rc;
- GetClientRect(&rc);
- if(!rc.PtInRect(point)) // The mouse cursor is no longer above this button
- OnMouseLeave();
- }
- }
- }
- }
- void CHoverButton::OnMouseEnter(void)
- {
- // We are now tracking the mouse, OVER this button
- m_bMouseTracking = TRUE;
- if( !m_bTabButton )
- m_ButtonState = BUTTON_OVER;
- else if( m_ButtonState != BUTTON_ON && m_bTabState != true )
- m_ButtonState = BUTTON_OVER;
- // Ensure that mouse input is sent to the button
- SetCapture();
- Invalidate( false );
- // UpdateWindow();
- }
- void CHoverButton::OnMouseLeave(void)
- {
- // We are not tracking the mouse, this button is OFF.
- if( !m_bTabButton )
- m_ButtonState = BUTTON_OFF;
- else if( m_bTabState != true )
- m_ButtonState = BUTTON_OFF;
- m_bMouseTracking = FALSE;
- // Release mouse capture from the button and restore normal mouse input
- Invalidate( false );
- // UpdateWindow();
- ReleaseCapture();
- }
- void CHoverButton::OnLButtonDown(UINT nFlags, CPoint point)
- {
- if( !m_bTabButton )
- SetButtonState(BUTTON_ON);
- else if( m_ButtonState != BUTTON_GREYED )
- {
- // if( m_ButtonState == BUTTON_ON )
- // SetButtonState(BUTTON_OFF);
- // else
- SetButtonState(BUTTON_ON);
- }
- CBitmapButton::OnLButtonDown(nFlags, point);
- }
- void CHoverButton::OnLButtonUp(UINT nFlags, CPoint point)
- {
- if( !m_bTabButton )
- SetButtonState(BUTTON_OVER); // Highlight button
- else
- {
- if( m_ButtonState == BUTTON_OFF )
- SetButtonState(BUTTON_OVER);
- }
- CBitmapButton::OnLButtonUp(nFlags, point);
- }
- // Purpose: Set the new state of the button
- // Return: Returns the old state of the button
- // Parameters: nState = Either ON or OFF. The default is OFF. This is NOT tri-state button!
- BUTTON_STATE CHoverButton::SetButtonState(BUTTON_STATE nState)
- {
- BUTTON_STATE nOldState = (BUTTON_STATE)GetCheck();
-
- BUTTON_STATE m_OldButtonState = m_ButtonState;
- m_ButtonState = nState;
- switch(m_ButtonState)
- {
- case BUTTON_ON:
- EnableWindow(TRUE);
- SetState(BUTTON_ON);
- break;
- case BUTTON_GREYED:
- EnableWindow(FALSE);
- break;
- case BUTTON_OVER:
- EnableWindow(TRUE);
- SetState(BUTTON_OVER);
- break;
- default:
- EnableWindow(TRUE);
- SetState(BUTTON_OFF);
- m_ButtonState = BUTTON_OFF;
- break;
- }
- if( m_OldButtonState != m_ButtonState )
- Invalidate(TRUE);
- return(nOldState);
- }
- // Draws the buttons in their relevant state, and text labels
- void CHoverButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- CDC memDC;
- CBitmap* pOld=NULL;
- CBitmap* pBitmap=NULL;
- CDC* pDC;
- CRect rc;
- int iSaveDC;
- pDC= CDC::FromHandle(lpDrawItemStruct->hDC);
- memDC.CreateCompatibleDC(pDC);
- VERIFY(pDC);
- iSaveDC=pDC->SaveDC();
- rc.CopyRect(&lpDrawItemStruct->rcItem);
- pDC->SetBkMode(TRANSPARENT);
- pDC->SetTextColor(GetSysColor(COLOR_WINDOWFRAME));// Black text color
- switch(m_ButtonState)
- {
- case BUTTON_ON:
- pBitmap=&m_bmpButtonDown;
- break;
- case BUTTON_OVER:
- pBitmap=&m_bmpButtonFocussed;
- break;
- case BUTTON_GREYED:
- pBitmap=&m_bmpButtonDisabled;
- //AfxMessageBox("BUTTON_GREYED");
- break;
- default:
- pBitmap=&m_bmpButtonUp;
- break;
- }
- CString strTitle;
- GetWindowText(strTitle);
- if (pBitmap->m_hObject)
- {
- CRect rcBitmap(rc);
- BITMAP bmpInfo;
- CSize size;
- // Text
- size = pDC->GetTextExtent(strTitle);
- rcBitmap.OffsetRect(size.cx+5,0);
- // Draw bitmap
- if(!pBitmap->GetBitmap(&bmpInfo))
- return;
- pOld=memDC.SelectObject((CBitmap*) pBitmap);
- if (pOld==NULL)
- return; //Destructors will clean up
- if(!pDC->BitBlt(0, 0, rc.Width(), rc.Height(), &memDC, 0, 0, SRCCOPY))
- return;
- memDC.SelectObject(pOld);
- if(memDC==NULL)
- return;
- }
- /*
- CRect rcText(rc);
- UINT nFormat = DT_CENTER;
- if(m_ButtonState == BUTTON_GREYED)
- {
- rcText.OffsetRect(1,1);
- pDC->SetTextColor(GetSysColor(COLOR_BTNHIGHLIGHT));
- pDC->DrawText(strTitle,rcText,nFormat);
- rcText.OffsetRect(-1,-1);
- pDC->SetTextColor(GetSysColor(COLOR_BTNSHADOW));
- pDC->DrawText(strTitle,rcText,nFormat);
- }
- else
- pDC->DrawText(strTitle,rcText,nFormat);
- //*/
- pDC->RestoreDC(iSaveDC);
- }
- BOOL CHoverButton::LoadBitmaps(UINT nBitmapUp, UINT nBitmapDown,
- UINT nBitmapFocus, UINT nBitmapDisabled)
- {
- return LoadBitmaps(MAKEINTRESOURCE(nBitmapUp),
- MAKEINTRESOURCE(nBitmapDown),
- MAKEINTRESOURCE(nBitmapFocus),
- MAKEINTRESOURCE(nBitmapDisabled));
- }
- BOOL CHoverButton::LoadBitmaps(LPCSTR lpszBitmapUp, LPCSTR lpszBitmapDown,
- LPCSTR lpszBitmapFocus, LPCSTR lpszBitmapDisabled)
- {
- BOOL bAllLoaded=TRUE;
- //Delete old ones
- m_bmpButtonDown.DeleteObject();
- m_bmpButtonFocussed.DeleteObject();
- m_bmpButtonUp.DeleteObject();
- m_bmpButtonDisabled.DeleteObject();
-
- if (!m_bmpButtonUp.LoadBitmap(lpszBitmapUp))
- {
- TRACE0("Failed to load up bitmap of bitmap button\n");
- return FALSE;
- }
- if (lpszBitmapDown!=NULL)
- {
- if (!m_bmpButtonDown.LoadBitmap(lpszBitmapDown))
- {
- TRACE0("Failed to load down bitmap of bitmap button\n");
- return bAllLoaded=FALSE;
- }
- }
-
- if (lpszBitmapFocus!=NULL)
- {
- if (!m_bmpButtonFocussed.LoadBitmap(lpszBitmapFocus))
- {
- TRACE0("Failed to load focussed bitmap of bitmap button\n");
- return bAllLoaded=FALSE;
- }
- }
-
- if (lpszBitmapDisabled!=NULL)
- {
- if (!m_bmpButtonDisabled.LoadBitmap(lpszBitmapDisabled))
- {
- TRACE0("Failed to load disabled bitmap of bitmap button\n");
- return bAllLoaded=FALSE;
- }
- }
- return bAllLoaded;
- }
|