home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / cbubble.cpp next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  5.0 KB  |  233 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. CString CBubble::m_ClassName = "";
  18.  
  19. CFont CBubble::m_Font;
  20.  
  21. DWORD CBubble::m_Height = 0;
  22.  
  23. IMPLEMENT_DYNAMIC( CBubble, CWnd );
  24.  
  25. CBubble::CBubble()
  26. {
  27.    m_FontSize = (-8);
  28.    m_Created = FALSE;
  29. }
  30.  
  31. CBubble::CBubble( int font_size )
  32. {
  33.    m_Created = FALSE;
  34.    m_FontSize = font_size;
  35. }
  36.  
  37. CBubble::~CBubble()
  38. {
  39. }
  40.  
  41. BOOL CBubble::Create()
  42. {
  43.    if ( m_ClassName == "" )
  44.    {
  45.       CBrush brush;
  46.  
  47.       TRY
  48.       {
  49.          brush.CreateSolidBrush( LIGHT_YELLOW );
  50.       }
  51.       CATCH( CResourceException, e )
  52.       {
  53.          return( FALSE );
  54.       }
  55.       END_CATCH
  56.  
  57.       m_ClassName = ::AfxRegisterWndClass( CS_BYTEALIGNCLIENT | CS_SAVEBITS | CS_HREDRAW | CS_VREDRAW, 0, (HBRUSH) brush.Detach() );
  58.  
  59.       if ( m_ClassName == "" )
  60.       {
  61.          TRACE( "CBubble, AfxRegisterWndClass() failed\n" );
  62.          return( FALSE );
  63.       }
  64.    }
  65.  
  66.    CRect rectangle;
  67.  
  68.    rectangle.SetRectEmpty();
  69.  
  70.    m_Created = CreateEx( 0,
  71.                          m_ClassName,
  72.                          "",
  73.                          WS_POPUP | WS_BORDER,
  74.                          rectangle.left,
  75.                          rectangle.top,
  76.                          rectangle.right,
  77.                          rectangle.bottom,
  78.                          NULL,
  79.                  (HMENU) NULL );
  80.  
  81.    if ( m_Font.GetSafeHandle() == NULL )
  82.    {
  83.       m_SetFont();
  84.    }
  85.  
  86.    return( m_Created );
  87. }
  88.  
  89. BOOL CBubble::DestroyWindow()
  90. {
  91.    m_Created = FALSE;
  92.    return( CWnd::DestroyWindow() );
  93. }
  94.  
  95. DWORD CBubble::GetHeight( void ) const
  96. {
  97.    return( m_Height );
  98. }
  99.  
  100. void CBubble::Hide( void )
  101. {
  102.    ShowWindow( SW_HIDE );
  103. }
  104.  
  105. BOOL CBubble::IsCreated( void ) const
  106. {
  107.    return( m_Created );
  108. }
  109.  
  110. void CBubble::m_SetFont( void )
  111. {
  112.    CClientDC device_context( this );
  113.  
  114.    TRY
  115.    {
  116.       LOGFONT lf;
  117.  
  118.       ::ZeroMemory( &lf, sizeof( lf ) );
  119.  
  120.       lf.lfHeight         = -::MulDiv( m_FontSize, device_context.GetDeviceCaps( LOGPIXELSY ), 72 );
  121.       lf.lfCharSet        = DEFAULT_CHARSET;
  122.       lf.lfQuality        = DEFAULT_QUALITY;
  123.       lf.lfClipPrecision  = CLIP_LH_ANGLES | CLIP_STROKE_PRECIS;
  124.       lf.lfPitchAndFamily = FF_SWISS;
  125.  
  126.       m_Font.CreateFontIndirect( &lf );
  127.    }
  128.    CATCH( CResourceException, e )
  129.    {
  130.       TRACE( "CBubble, font creation failed\n" );
  131.       return;
  132.    }
  133.    END_CATCH
  134.  
  135.    CFont *old_font = device_context.SelectObject( &m_Font );
  136.  
  137.    TEXTMETRIC tm;
  138.    device_context.GetTextMetrics( &tm );
  139.    device_context.SelectObject( old_font );
  140.    m_Height = tm.tmHeight + tm.tmExternalLeading + ( 6 * GetSystemMetrics( SM_CYBORDER ) );
  141. }
  142.  
  143. void CBubble::SetFontSize( int font_size )
  144. {
  145.    m_FontSize = font_size;
  146. }
  147.  
  148. void CBubble::Show( CPoint point, const CString& string )
  149. {
  150.    Hide();
  151.    UpdateWindow();
  152.  
  153.    SetWindowText( string );
  154.  
  155.    CRect bubble;
  156.    GetWindowRect( &bubble );
  157.    CRect screen;
  158.  
  159.    ::GetWindowRect( ::GetDesktopWindow(), &screen );
  160.  
  161.    /*
  162.    ** Let's make sure the window is always visible (on screen)
  163.    */
  164.  
  165.    if ( point.y < 0 )
  166.    {
  167.       point.y = 0;
  168.    }
  169.  
  170.    if ( ( point.y + bubble.Height() ) > screen.bottom )
  171.    {
  172.       point.y = screen.bottom - ( bubble.Height() + 1 );
  173.    }
  174.  
  175.    if ( point.x < 0 )
  176.    {
  177.       point.x = abs( point.x ) - ( bubble.Width() / 2 );
  178.    }
  179.  
  180.    if ( point.x > screen.right )
  181.    {
  182.       point.x = screen.right - ( bubble.Width() + 1 );
  183.    }
  184.  
  185.    SetWindowPos( &wndTop, point.x, point.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE );
  186.  
  187.    ShowWindow( SW_SHOWNOACTIVATE );
  188. }
  189.  
  190. BEGIN_MESSAGE_MAP( CBubble, CWnd )
  191.    ON_WM_PAINT()
  192.    ON_MESSAGE( WM_SETTEXT, OnSetText )
  193. END_MESSAGE_MAP()
  194.  
  195. void CBubble::OnPaint()
  196. {
  197.    CPaintDC device_context( this );
  198.  
  199.    CRect rectangle;
  200.  
  201.    GetClientRect( rectangle );
  202.  
  203.    CFont *old_font = device_context.SelectObject( &m_Font );
  204.    device_context.SetTextAlign( TA_CENTER );
  205.    device_context.SetBkMode( TRANSPARENT );
  206.    device_context.SetTextColor( BLACK );
  207.  
  208.    CString text;
  209.    GetWindowText( text );
  210.    CSize size = device_context.GetTextExtent( text, text.GetLength() );
  211.    device_context.TextOut( rectangle.right / 2, ( rectangle.bottom - size.cy ) / 2, text );
  212.    device_context.SelectObject( old_font );
  213. }
  214.  
  215. afx_msg LONG CBubble::OnSetText( UINT, LONG lParam )
  216. {
  217.    CRect rectangle;
  218.    GetWindowRect( rectangle );
  219.    
  220.    CClientDC device_context( this );
  221.  
  222.    CFont *old_font = device_context.SelectObject( &m_Font );
  223.    CSize size = device_context.GetTextExtent( (LPCSTR) lParam, lstrlen( (LPCSTR) lParam ) );
  224.  
  225.    rectangle.right = rectangle.left + size.cx + ( 6 * GetSystemMetrics( SM_CXBORDER ) );
  226.  
  227.    rectangle.bottom = rectangle.top + m_Height;
  228.    MoveWindow( &rectangle );
  229.    device_context.SelectObject( old_font );
  230.  
  231.    return( CWnd::Default() );
  232. }
  233.