home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / WTJ9403.ZIP / WILDASS / SOURCE / ICONBTN.CPP < prev    next >
C/C++ Source or Header  |  1993-08-12  |  5KB  |  181 lines

  1. // iconbtn.cpp
  2. //
  3. // class behaviour of the icon button class
  4. //
  5. // update: 01-Aug-93 tw
  6. //         10-Aug-93 tw added drag n drop
  7.  
  8. #include <afxwin.h>
  9. #include <afxext.h> 
  10. #include <ctl3d.h>
  11.                     
  12. #include "iconbtn.h" 
  13. #include "mainwnd.h"
  14.  
  15. BEGIN_MESSAGE_MAP( CIconButton, CButton )
  16.    ON_WM_DROPFILES()
  17. END_MESSAGE_MAP()
  18.     
  19. CIconButton::CIconButton()
  20. { /* does nothing */ }
  21.     
  22. void
  23. CIconButton::Create( CWnd * pParent, const CPoint& rPos, UINT nID, const char * pszIconFile )
  24. {
  25.    m_cxIcon = ::GetSystemMetrics( SM_CXICON );
  26.    m_cyIcon = ::GetSystemMetrics( SM_CYICON );
  27.       
  28.    m_cxButton = m_cxIcon+CIconButton::cxFrame;
  29.    m_cyButton = m_cyIcon+CIconButton::cyFrame;
  30.  
  31.    VERIFY( CButton::Create( 0, BS_OWNERDRAW | WS_CHILD | WS_VISIBLE, 
  32.                CRect(rPos.x, rPos.y, rPos.x+m_cxButton, rPos.y+m_cyButton ), 
  33.                pParent, nID ));
  34.                   
  35.    // do we already have an file to attach ?                  
  36.    if ( pszIconFile )
  37.    {
  38.       m_hIcon = ExtractIcon( AfxGetInstanceHandle(), pszIconFile, 0 );     
  39.    }
  40.    else              
  41.    { 
  42.       m_hIcon = 0;   
  43.    }
  44. }
  45.  
  46. void
  47. CIconButton::SetIcon( HICON hIcon )
  48. {
  49.    ASSERT( hIcon );
  50.    m_hIcon = hIcon;  
  51.    
  52.    // repaint the button
  53.    Invalidate();
  54. }
  55.  
  56. void
  57. CIconButton::DrawItem( LPDRAWITEMSTRUCT lp )
  58. {    
  59.    // get a valid device context       
  60.    CDC *pDC = CDC::FromHandle( lp->hDC );
  61.    ASSERT( pDC );               
  62.               
  63.    if ( lp->itemAction & ODA_DRAWENTIRE )
  64.    {
  65.       DrawEntire( pDC );
  66.    }
  67.    else if ( lp->itemAction & ODA_SELECT )
  68.    {              
  69.       if ( lp->itemState & ODS_SELECTED )
  70.       {
  71.          DrawDownState( pDC );
  72.       }
  73.       else                      
  74.       {
  75.          DrawUpState( pDC );
  76.       }
  77.    }         
  78. }
  79.  
  80. void 
  81. CIconButton::DrawDownState( CDC * pDC )
  82. {     
  83.    // blit the image left/down one pixel
  84.    pDC->BitBlt( 4, 4, m_cxButton-5, m_cyButton-5, pDC, 3, 3, SRCCOPY );
  85.    
  86.    // paint the shadow
  87.    CPen penShadow( PS_SOLID, 0, GetSysColor( COLOR_BTNSHADOW ));
  88.    CPen * penOld = pDC->SelectObject( &penShadow );
  89.    
  90.    pDC->MoveTo( 1, m_cyButton-2 );
  91.    pDC->LineTo( 1, 1 );
  92.    pDC->LineTo( m_cxButton-1, 1);
  93.    
  94.    CPen penFace( PS_SOLID, 0, GetSysColor( COLOR_BTNFACE ));
  95.    pDC->SelectObject( &penFace );
  96.    
  97.    pDC->MoveTo( m_cxButton-2, 2);
  98.    pDC->LineTo( m_cxButton-2, 4);
  99.    pDC->MoveTo( 1, m_cyButton-2);
  100.    pDC->LineTo( 3, m_cyButton-2);   
  101.    
  102.    pDC->SelectObject( penOld );
  103. }   
  104.    
  105.    
  106. void 
  107. CIconButton::DrawUpState( CDC * pDC )
  108. {  
  109.    // we could go for some optimizations here, ....
  110.    DrawEntire( pDC );
  111. }  
  112.  
  113. void 
  114. CIconButton::DrawEntire( CDC * pDC )
  115. {   
  116.    CBitmap bm;
  117.    VERIFY( bm.CreateCompatibleBitmap( pDC, m_cxButton, m_cyButton));
  118.    CDC dcMem;
  119.    VERIFY( dcMem.CreateCompatibleDC( pDC ));
  120.    CBitmap* pBitmapOld = dcMem.SelectObject( &bm );
  121.    ASSERT( pBitmapOld );
  122.    
  123.    // draw the buttons shape      
  124.    CBrush faceBrush( GetSysColor( COLOR_BTNFACE ) );
  125.    CRect rectFace;
  126.    GetClientRect( &rectFace );
  127.    pDC->FillRect( rectFace, &faceBrush );
  128.          
  129.    CPen penBlack( PS_SOLID, 0, RGB(0,0,0) );      
  130.    CPen * penOld = pDC->SelectObject( &penBlack );
  131.    pDC->MoveTo( 1, 0 );
  132.    pDC->LineTo( m_cxButton-1, 0);
  133.    
  134.    pDC->MoveTo( m_cxButton-1, 1 );
  135.    pDC->LineTo( m_cxButton-1, m_cyButton-1 );
  136.    
  137.    pDC->MoveTo( m_cxButton-2, m_cyButton-1 );
  138.    pDC->LineTo( 0, m_cyButton-1 );
  139.    
  140.    pDC->MoveTo( 0, m_cyButton-2 );
  141.    pDC->LineTo( 0, 0 );
  142.           
  143.    CPen penHighlight( PS_SOLID, 0, GetSysColor( COLOR_BTNHIGHLIGHT ));          
  144.    pDC->SelectObject( &penHighlight );
  145.    
  146.    pDC->MoveTo( 1, m_cyButton-2 );
  147.    pDC->LineTo( 1, 1 );
  148.    pDC->LineTo( m_cxButton-1, 1);
  149.    
  150.    CPen penShadow( PS_SOLID, 0, GetSysColor( COLOR_BTNSHADOW ));
  151.    pDC->SelectObject( &penShadow );
  152.  
  153.    pDC->MoveTo( m_cxButton-2, 2 );
  154.    pDC->LineTo( m_cxButton-2, m_cyButton-2 );
  155.    pDC->LineTo( 1, m_cyButton-2 );
  156.  
  157.    // only draw icon if we have one   
  158.    if ( m_hIcon )
  159.    {
  160.       VERIFY( pDC->DrawIcon( 4, 4, m_hIcon ));
  161.    }
  162.    
  163.    dcMem.SelectObject( pBitmapOld );
  164.    pDC->SelectObject( penOld );
  165. }
  166.  
  167. /*static */ UINT
  168. CIconButton::Width() 
  169. {
  170.    return (::GetSystemMetrics( SM_CXICON ) + CIconButton::cxFrame );
  171. }
  172.  
  173.  
  174. // just route the dropinfo to the parent window
  175. void
  176. CIconButton::OnDropFiles( HDROP hDrop )
  177. {             
  178.    TRACE("incobtn drop\n");
  179.    ((CMainWnd*)GetParent())->DroppedFiles( hDrop, ::GetWindowWord( m_hWnd, GWW_ID ) );
  180. }
  181.