Back to index

lightning-sunbird  0.9+nobinonly
BrowserToolTip.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: Mozilla-sample-code 1.0
00004  *
00005  * Copyright (c) 2002 Netscape Communications Corporation and
00006  * other contributors
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a
00009  * copy of this Mozilla sample software and associated documentation files
00010  * (the "Software"), to deal in the Software without restriction, including
00011  * without limitation the rights to use, copy, modify, merge, publish,
00012  * distribute, sublicense, and/or sell copies of the Software, and to permit
00013  * persons to whom the Software is furnished to do so, subject to the
00014  * following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included
00017  * in all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00020  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00022  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00025  * DEALINGS IN THE SOFTWARE.
00026  *
00027  * Contributor(s):
00028  *   Adam Lock <adamlock@netscape.com>
00029  *
00030  * ***** END LICENSE BLOCK ***** */
00031 
00032 #include "stdafx.h"
00033 
00034 #include "BrowserToolTip.h"
00035 
00036 #ifdef _DEBUG
00037 #define new DEBUG_NEW
00038 #undef THIS_FILE
00039 static char THIS_FILE[] = __FILE__;
00040 #endif
00041 
00043 // CBrowserToolTip
00044 
00045 CBrowserToolTip::CBrowserToolTip()
00046 {
00047 }
00048 
00049 CBrowserToolTip::~CBrowserToolTip()
00050 {
00051 }
00052 
00053 
00054 BEGIN_MESSAGE_MAP(CBrowserToolTip, CWnd)
00055        //{{AFX_MSG_MAP(CBrowserToolTip)
00056        ON_WM_PAINT()
00057        //}}AFX_MSG_MAP
00058 END_MESSAGE_MAP()
00059 
00060 
00061 BOOL CBrowserToolTip::Create(CWnd *pParentWnd)
00062 {
00063     return CWnd::CreateEx(WS_EX_TOOLWINDOW,
00064         AfxRegisterWndClass(CS_SAVEBITS, NULL, GetSysColorBrush(COLOR_INFOBK), NULL),
00065         _T("ToolTip"), WS_POPUP | WS_BORDER, 0, 0, 1, 1, pParentWnd->GetSafeHwnd(), NULL);
00066 }
00067 
00069 // CBrowserToolTip message handlers
00070 
00071 void CBrowserToolTip::OnPaint() 
00072 {
00073        CPaintDC dc(this); // device context for painting
00074 
00075     CRect rcClient;
00076     GetClientRect(&rcClient);
00077 
00078     // Draw tip text
00079     int oldBkMode = dc.SetBkMode(TRANSPARENT);
00080     COLORREF oldTextColor = dc.SetTextColor(GetSysColor(COLOR_INFOTEXT));
00081     HGDIOBJ oldFont = dc.SelectObject(GetStockObject(DEFAULT_GUI_FONT));
00082 
00083     dc.DrawText(m_szTipText, -1, rcClient, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
00084 
00085     dc.SetBkMode(oldBkMode);
00086     dc.SetTextColor(oldTextColor);
00087     dc.SelectObject(oldFont);
00088 }
00089 
00090 BOOL CBrowserToolTip::PreCreateWindow(CREATESTRUCT& cs) 
00091 {
00092        return CWnd::PreCreateWindow(cs);
00093 }
00094 
00095 void CBrowserToolTip::SetTipText(const CString &szTipText)
00096 {
00097     m_szTipText = szTipText;
00098 }
00099 
00100 void CBrowserToolTip::Show(CWnd *pOverWnd, long left, long top)
00101 {
00102     // Calculate the client window size
00103     CRect rcNewClient(0,0,0,0);
00104     CDC *pdc = GetDC();
00105     HGDIOBJ oldFont = pdc->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
00106     rcNewClient.bottom = pdc->DrawText(m_szTipText, -1, rcNewClient,
00107         DT_SINGLELINE | DT_VCENTER | DT_CENTER | DT_CALCRECT);
00108     pdc->SelectObject(oldFont);
00109     ReleaseDC(pdc);
00110     rcNewClient.right += 8;
00111     rcNewClient.bottom += 8;
00112     
00113     // Adjust the tooltip to new size
00114     AdjustWindowRectEx(rcNewClient, GetWindowLong(m_hWnd, GWL_STYLE), FALSE, GetWindowLong(m_hWnd, GWL_EXSTYLE));
00115 
00116     // Adjust the left, top position of the tooltip
00117     CPoint ptTip(left, top);
00118     pOverWnd->ClientToScreen(&ptTip);
00119 
00120     // Make sure tip is below cursor
00121     POINT ptCursor;
00122     GetCursorPos(&ptCursor);
00123     long cyCursor = GetSystemMetrics(SM_CYCURSOR);
00124     if (ptTip.y < ptCursor.y + cyCursor)
00125         ptTip.y = ptCursor.y + cyCursor;
00126 
00127     // Make sure tip is fully visible
00128     RECT rcScreen;
00129     GetDesktopWindow()->GetClientRect(&rcScreen);
00130     if (ptTip.x < 0)
00131         ptTip.x = 0;
00132     else if (ptTip.x + rcNewClient.Width() > rcScreen.right)
00133         ptTip.x = rcScreen.right - rcNewClient.Width();
00134     if (ptTip.y < 0)
00135         ptTip.y = 0;
00136     else if (ptTip.y + rcNewClient.Height() > rcScreen.bottom)
00137         ptTip.y = rcScreen.bottom - rcNewClient.Height();
00138 
00139     // Position and show the tip
00140     SetWindowPos(&CWnd::wndTop, ptTip.x, ptTip.y, rcNewClient.Width(), rcNewClient.Height(),
00141         SWP_NOACTIVATE | SWP_SHOWWINDOW);
00142 }
00143 
00144 void CBrowserToolTip::Hide()
00145 {
00146     ShowWindow(SW_HIDE);
00147 }