home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / ttips2 / popuptip.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-13  |  6.3 KB  |  244 lines

  1. // popuptip.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "popuptip.h"
  6. #include "tooltip.h"
  7.  
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char BASED_CODE THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. #define new DEBUG_NEW 
  14.  
  15. #if defined(STYLE3D)
  16. #define DeflateRect(rect,nCount) \
  17.             rect.left += nCount;   \
  18.             rect.top += nCount;    \
  19.             rect.bottom -= nCount; \
  20.             rect.right -= nCount;
  21. #endif
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CPopupTip
  24.  
  25. CPopupTip::CPopupTip(CString &strClass)
  26. {                                     
  27.     m_strClass = strClass;
  28.     m_pFont = NULL;
  29. }
  30.  
  31. CPopupTip::~CPopupTip()
  32. {          
  33. }
  34.  
  35. void CPopupTip::SetDisplay(const CString &strDisplay)
  36. {
  37.     m_strDisplay = strDisplay;
  38. }
  39.  
  40. void CPopupTip::PostNcDestroy( )
  41. {
  42.     delete this;
  43. }
  44.  
  45. BOOL CPopupTip::PreCreateWindow( CREATESTRUCT& cs )
  46. {
  47.     return TRUE;
  48. }
  49.           
  50. BOOL CPopupTip::PopItUp(POINT pt, UINT nNewStyle)
  51. {
  52. #if defined(STYLE3D)
  53.     if (nNewStyle < TTIPS_SQUARESTYLE || nNewStyle > TTIPS_3DSTYLE)
  54.         nNewStyle = TTIPS_SQUARESTYLE;
  55. #else        
  56.     if (nNewStyle < TTIPS_SQUARESTYLE || nNewStyle > TTIPS_ROUNDSTYLE)
  57.         nNewStyle = TTIPS_SQUARESTYLE;
  58. #endif
  59.  
  60.     int  nBorder = 2;
  61.     
  62. #if defined(STYLE3D)
  63.     if (nNewStyle == TTIPS_3DSTYLE)
  64.         nBorder += 6;
  65. #endif
  66.  
  67. //
  68. // Get the width and height of the window based on length of text
  69. //
  70.     CWindowDC dc(NULL);
  71.     CFont *pfontOld = dc.SelectObject(m_pFont);
  72.     CString strExt = m_strDisplay + "1";
  73.     CSize sizeText = dc.GetTextExtent(strExt, strExt.GetLength());
  74.     dc.SelectObject(pfontOld);
  75.     
  76.     sizeText.cx += nBorder, 
  77.     sizeText.cy += (sizeText.cy / 4) + nBorder;
  78. //
  79. // See of the box is off the screen to the right.  If it is, move it back
  80. // onto the screen (just barely)
  81. //      
  82.     int ScreenRight = GetSystemMetrics(SM_CXSCREEN);
  83.     if (pt.x + sizeText.cx > ScreenRight - 6)
  84.         pt.x = ScreenRight - sizeText.cx - 6;
  85.  
  86.     if (CreateEx( 0,
  87.         m_strClass,
  88.         NULL,
  89.         WS_POPUP, 
  90.         pt.x, 
  91.         pt.y, 
  92.         sizeText.cx,
  93.         sizeText.cy,
  94.         NULL,
  95.         0) ) {                  
  96.             ShowWindow(SW_SHOWNOACTIVATE);
  97.             m_nStyleFlag = nNewStyle;
  98.             return TRUE;
  99.     }
  100.     else
  101.       return FALSE;   
  102. }
  103.  
  104. BEGIN_MESSAGE_MAP(CPopupTip, CWnd)
  105.     //{{AFX_MSG_MAP(CPopupTip)
  106.     ON_WM_PAINT()
  107.     ON_WM_ERASEBKGND()
  108.     //}}AFX_MSG_MAP
  109. END_MESSAGE_MAP()
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CPopupTip message handlers
  113.  
  114. void CPopupTip::OnPaint()
  115. {
  116.     CPaintDC dc(this); // device context for painting
  117.     CRect rectClient;
  118.     GetClientRect(&rectClient);
  119.    
  120. #if defined(STYLE3D)
  121.     if (m_nStyleFlag == TTIPS_3DSTYLE)
  122.         Draw3DFrame(&dc, rectClient);
  123.     else {
  124. #endif    
  125. //
  126. // Draw the rectangle box for the tip
  127. //
  128. #if defined(USE_APPWORKSPACE_COLORS)
  129.         CBrush brYellow(::GetSysColor(COLOR_APPWORKSPACE));
  130. #else
  131.         CBrush brYellow(RGB(255,255,128));  // Standard tool tip yellow
  132.                                             // take out the 128 for darker
  133.                                             // yellow.
  134. #endif        
  135.         CBrush *pBrushOld = dc.SelectObject(&brYellow);
  136.         
  137.         if( m_nStyleFlag == TTIPS_ROUNDSTYLE ) {
  138.             CPoint ellipsePts(5,10);
  139.             dc.RoundRect( &rectClient, ellipsePts );
  140.         }
  141.         else
  142.             dc.Rectangle( &rectClient );
  143.         dc.SelectObject(pBrushOld);
  144. #if defined(STYLE3D)
  145.     }       
  146. #endif    
  147.     CFont *pFontOld = NULL;
  148.     if (m_pFont)
  149.         pFontOld = dc.SelectObject(m_pFont);
  150.                
  151.     //force colors
  152.     int nBkMode = dc.SetBkMode(TRANSPARENT);
  153.  
  154. #if defined(USE_APPWORKSPACE_COLORS)
  155.     COLORREF crText = dc.SetTextColor(::GetSysColor(COLOR_BTNTEXT));
  156. #else    
  157.     COLORREF crText = dc.SetTextColor(RGB(0,0,0));  // use black for text
  158. #endif
  159.  
  160.     dc.DrawText( m_strDisplay, 
  161.         -1,
  162.         &rectClient,
  163.         DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  164.  
  165.     dc.SetTextColor(crText);
  166.     dc.SetBkMode(nBkMode);
  167.                      
  168.     if (pFontOld)
  169.         dc.SelectObject(pFontOld);                   
  170. }
  171.  
  172.  
  173. BOOL CPopupTip::OnEraseBkgnd(CDC* pDC)
  174. {
  175.    return TRUE;   
  176. }
  177.  
  178. #if defined(STYLE3D)
  179.  
  180. void CPopupTip::Draw3DFrame(CDC* pDC, CRect& rcFrame)
  181. {   
  182. //
  183. // Draw a 3D frame for the tip box
  184. //
  185.     CPen    penBlack(PS_SOLID,1,RGB(0, 0, 0));
  186.     CPen    penWhite(PS_SOLID,1,::GetSysColor(COLOR_BTNHIGHLIGHT));
  187.     CPen    penGray(PS_SOLID,1,::GetSysColor(COLOR_BTNFACE));
  188.     CPen    penDark(PS_SOLID,1,::GetSysColor(COLOR_BTNSHADOW));
  189.  
  190.     CPen*   pOldPen     = (CPen*)   pDC->SelectObject( &penBlack );
  191.     CBrush* pOldBrush   = (CBrush*) pDC->SelectStockObject(LTGRAY_BRUSH);
  192.  
  193.     DeflateRect(rcFrame,1);            // Frame the window with a black line    
  194.     pDC->Rectangle(&rcFrame);
  195.     pDC->MoveTo( rcFrame.left, rcFrame.bottom );
  196.     pDC->LineTo( rcFrame.TopLeft() );
  197.     pDC->LineTo( rcFrame.right, rcFrame.top );
  198.     pDC->LineTo( rcFrame.BottomRight() );             
  199.     pDC->LineTo( rcFrame.left, rcFrame.bottom );
  200.  
  201.     DeflateRect(rcFrame,1);
  202.     pDC->SelectObject( &penWhite );    // Draw white highlight
  203.     pDC->MoveTo( rcFrame.left, rcFrame.bottom );
  204.     pDC->LineTo( rcFrame.TopLeft() );
  205.     pDC->LineTo( rcFrame.right+1, rcFrame.top );             
  206.         
  207.     pDC->SelectObject( &penDark );    // Draw the dark shadow
  208.     pDC->MoveTo( rcFrame.right, rcFrame.top+1 );
  209.     pDC->LineTo( rcFrame.BottomRight() );             
  210.     pDC->LineTo( rcFrame.left, rcFrame.bottom );
  211.  
  212.     DeflateRect(rcFrame,1);
  213.     pDC->SelectObject( &penGray );    // Draw the gray frame
  214.     pDC->MoveTo( rcFrame.left, rcFrame.bottom );
  215.     pDC->LineTo( rcFrame.TopLeft() );
  216.     pDC->LineTo( rcFrame.right, rcFrame.top );
  217.     pDC->LineTo( rcFrame.BottomRight() );             
  218.     pDC->LineTo( rcFrame.left, rcFrame.bottom );
  219.     
  220.     DeflateRect(rcFrame,1);
  221.     pDC->SelectObject( &penDark );  // Draw the inside dark shadow
  222.     pDC->MoveTo( rcFrame.left, rcFrame.bottom );
  223.     pDC->LineTo( rcFrame.TopLeft() );
  224.     pDC->LineTo( rcFrame.right, rcFrame.top );
  225.     
  226.     pDC->SelectObject( &penWhite ); // Draw the inside white hightlight
  227.     pDC->MoveTo( rcFrame.right, rcFrame.top+1 );
  228.     pDC->LineTo( rcFrame.BottomRight() );             
  229.     pDC->LineTo( rcFrame.left, rcFrame.bottom );
  230.  
  231.     ////////////////////////////////////////////////////////
  232.     // Be good and restore the DC to as it was            //
  233.     ////////////////////////////////////////////////////////
  234.     pDC->SelectObject( (CPen*) pOldPen );
  235.     pDC->SelectObject( (CBrush*) pOldBrush);
  236.     // Now clean up the temp GDI objects
  237.     penWhite.DeleteObject();
  238.     penGray.DeleteObject();
  239.     penDark.DeleteObject();
  240.     penBlack.DeleteObject();
  241. }
  242.  
  243. #endif
  244.