home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / tip.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.0 KB  |  173 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "stdafx.h"
  20. #include "tip.h"
  21.  
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. #define COL_LEFT_MARGIN    ((m_cxChar+1)/2)
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CTip
  31.  
  32. CTip::CTip()
  33. {
  34.     m_hFont = NULL;
  35.     m_dwStyle = 0;
  36.     m_iCSID = CS_LATIN1;
  37. }
  38.  
  39. CTip::~CTip()
  40. {
  41.     DestroyWindow();
  42. }
  43.  
  44.  
  45. BEGIN_MESSAGE_MAP(CTip, CWnd)
  46.     ON_WM_PAINT()
  47.     ON_WM_NCHITTEST()
  48. END_MESSAGE_MAP()
  49.  
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CTip message handlers
  53.  
  54. void CTip::OnPaint() 
  55. {
  56.     CRect rect;
  57.     HFONT OldFont = NULL;
  58.     CPaintDC dc(this); // device context for painting
  59.     
  60.     if (m_hFont) {
  61.         OldFont = (HFONT) dc.SelectObject(m_hFont);
  62.     }    
  63.     GetClientRect(&rect);
  64.  
  65.     COLORREF crInfoBack, crInfoFore;
  66.  
  67. #ifdef _WIN32
  68.     if ( sysInfo.m_bWin4 ) {
  69.         crInfoFore = GetSysColor( COLOR_INFOTEXT );
  70.         crInfoBack = GetSysColor( COLOR_INFOBK );
  71.     }
  72.     else
  73. #endif
  74.     {
  75.         crInfoBack = RGB(255,255,225);
  76.         crInfoFore = RGB(  0,  0,  0);
  77.     }
  78.  
  79.     // paint background and text
  80.     CBrush brush;
  81.     brush.CreateSolidBrush(crInfoBack);
  82.     dc.FillRect(&rect, &brush);
  83.     dc.SetBkMode(TRANSPARENT);
  84.  
  85.     CSize TextSize = CIntlWin::GetTextExtent(0, dc.m_hDC, csTitle, csTitle.GetLength());
  86.     dc.SetTextColor(crInfoFore);
  87.     rect.left += COL_LEFT_MARGIN;
  88.     CIntlWin::DrawText(m_iCSID, dc.m_hDC, (LPSTR) (LPCSTR) csTitle, -1, &rect, DT_LEFT|DT_NOPREFIX);
  89.     
  90.     if (OldFont) {
  91.         dc.SelectObject(OldFont);
  92.     }
  93. }
  94.  
  95. void CTip::Create()
  96. {
  97.     DWORD dwStyleEx = 0;
  98.     DWORD dwStyle = WS_POPUP|WS_BORDER;
  99.     LPCTSTR lpszClass = AfxRegisterWndClass(CS_SAVEBITS);
  100.  
  101. #ifdef _WIN32
  102.     if ( sysInfo.m_bWin4 ) {
  103.         dwStyleEx |= WS_EX_TOOLWINDOW;
  104.     } 
  105. #endif
  106.     if (!CreateEx( dwStyleEx, lpszClass, _T(""), dwStyle,
  107.                    0, 0, 0, 0, NULL, NULL)) {
  108.         TRACE0("COULDN'T CREATE TIP WINDOW\n");
  109.     }
  110. }
  111.  
  112. void CTip::Show( HWND owner, int x, int y, int cx, int cy, 
  113.                  LPCSTR text, DWORD dwStyle, HFONT font )
  114. {
  115.     m_dwStyle = dwStyle;
  116.     m_hFont = font;
  117.  
  118.     RECT rect;
  119.     CClientDC dc(this);
  120.     HFONT OldFont = NULL;
  121.     
  122.     if (m_hFont) {
  123.         OldFont = (HFONT) dc.SelectObject(m_hFont);
  124.     }    
  125.     TEXTMETRIC tm;
  126.     dc.GetTextMetrics ( &tm );
  127.     m_cxChar = tm.tmAveCharWidth;
  128.  
  129.     ::GetWindowRect(owner, &rect);
  130.     x += rect.left;
  131.     y += rect.top;
  132.     cy += 1;
  133.  
  134.     csTitle = text;
  135.  
  136.     // calc window size
  137.     SIZE TextSize = CIntlWin::GetTextExtent(m_iCSID, dc.m_hDC, csTitle, csTitle.GetLength());
  138.     TextSize.cx += COL_LEFT_MARGIN * 2 + 1;
  139.  
  140.     if ( m_dwStyle & NSTTS_RIGHT ) {
  141.         x = x + cx - TextSize.cx;
  142.     } else {
  143.         x--;
  144.     }
  145.  
  146.     // Bump onto screen, if necessary.
  147.     int xScreenRight = ::GetSystemMetrics(SM_CXSCREEN);
  148.     int yScreenBottom = ::GetSystemMetrics(SM_CYSCREEN);
  149.     if (x + TextSize.cx > xScreenRight)
  150.         x -= x + TextSize.cx - xScreenRight;
  151.     if (y + cy > yScreenBottom)
  152.         y -= y + cy - yScreenBottom;
  153.  
  154.     SetWindowText(csTitle);
  155.     if ( csTitle.GetLength() && ( (TextSize.cx > cx) || (m_dwStyle & NSTTS_ALWAYSSHOW)) ) {
  156.         SetWindowPos(&wndTopMost, x, y, TextSize.cx, cy, SWP_SHOWWINDOW|SWP_NOACTIVATE);
  157.         UpdateWindow();
  158.     }
  159.     if (OldFont) {
  160.         dc.SelectObject(OldFont);
  161.     }
  162. }
  163.  
  164. void CTip::Hide()
  165. {
  166.     ShowWindow( SW_HIDE );
  167. }
  168.  
  169. UINT CTip::OnNcHitTest(CPoint point)
  170. {
  171.     return (UINT) HTTRANSPARENT;
  172. }
  173.