Back to index

lightning-sunbird  0.9+nobinonly
BrowserToolTip.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org Code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Adam Lock <adamlock@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "stdafx.h"
00039 
00040 #include "BrowserToolTip.h"
00041 
00042 #ifdef _DEBUG
00043 #define new DEBUG_NEW
00044 #undef THIS_FILE
00045 static char THIS_FILE[] = __FILE__;
00046 #endif
00047 
00049 // CBrowserToolTip
00050 
00051 CBrowserToolTip::CBrowserToolTip()
00052 {
00053 }
00054 
00055 CBrowserToolTip::~CBrowserToolTip()
00056 {
00057 }
00058 
00059 BEGIN_MESSAGE_MAP(CBrowserToolTip, CWnd)
00060        //{{AFX_MSG_MAP(CBrowserToolTip)
00061        ON_WM_PAINT()
00062        //}}AFX_MSG_MAP
00063 END_MESSAGE_MAP()
00064 
00065 
00066 BOOL CBrowserToolTip::Create(CWnd *pParentWnd)
00067 {
00068     return CWnd::CreateEx(WS_EX_TOOLWINDOW,
00069         AfxRegisterWndClass(CS_SAVEBITS, NULL, GetSysColorBrush(COLOR_INFOBK), NULL),
00070         _T("ToolTip"), WS_POPUP | WS_BORDER, 0, 0, 1, 1, pParentWnd->GetSafeHwnd(), NULL);
00071 }
00072 
00074 // CBrowserToolTip message handlers
00075 
00076 void CBrowserToolTip::OnPaint() 
00077 {
00078        CPaintDC dc(this); // device context for painting
00079 
00080     CRect rcClient;
00081     GetClientRect(&rcClient);
00082 
00083     // Draw tip text
00084     int oldBkMode = dc.SetBkMode(TRANSPARENT);
00085     COLORREF oldTextColor = dc.SetTextColor(GetSysColor(COLOR_INFOTEXT));
00086     HGDIOBJ oldFont = dc.SelectObject(GetStockObject(DEFAULT_GUI_FONT));
00087 
00088     dc.DrawText(m_szTipText, -1, rcClient, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
00089 
00090     dc.SetBkMode(oldBkMode);
00091     dc.SetTextColor(oldTextColor);
00092     dc.SelectObject(oldFont);
00093 }
00094 
00095 BOOL CBrowserToolTip::PreCreateWindow(CREATESTRUCT& cs) 
00096 {
00097        return CWnd::PreCreateWindow(cs);
00098 }
00099 
00100 void CBrowserToolTip::SetTipText(const CString &szTipText)
00101 {
00102     m_szTipText = szTipText;
00103 }
00104 
00105 void CBrowserToolTip::Show(CWnd *pOverWnd, long left, long top)
00106 {
00107     // Calculate the client window size
00108     CRect rcNewClient(0,0,0,0);
00109     CDC *pdc = GetDC();
00110     HGDIOBJ oldFont = pdc->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
00111     rcNewClient.bottom = pdc->DrawText(m_szTipText, -1, rcNewClient,
00112         DT_SINGLELINE | DT_VCENTER | DT_CENTER | DT_CALCRECT);
00113     pdc->SelectObject(oldFont);
00114     ReleaseDC(pdc);
00115     rcNewClient.right += 8;
00116     rcNewClient.bottom += 8;
00117     
00118     // Adjust the tooltip to new size
00119     AdjustWindowRectEx(rcNewClient, GetWindowLong(m_hWnd, GWL_STYLE), FALSE, GetWindowLong(m_hWnd, GWL_EXSTYLE));
00120 
00121     // Adjust the left, top position of the tooltip
00122     CPoint ptTip(left, top);
00123     pOverWnd->ClientToScreen(&ptTip);
00124 
00125     // Make sure tip is below cursor
00126     POINT ptCursor;
00127     GetCursorPos(&ptCursor);
00128     long cyCursor = GetSystemMetrics(SM_CYCURSOR);
00129     if (ptTip.y < ptCursor.y + cyCursor)
00130         ptTip.y = ptCursor.y + cyCursor;
00131 
00132     // Make sure tip is fully visible
00133     RECT rcScreen;
00134     GetDesktopWindow()->GetClientRect(&rcScreen);
00135     if (ptTip.x < 0)
00136         ptTip.x = 0;
00137     else if (ptTip.x + rcNewClient.Width() > rcScreen.right)
00138         ptTip.x = rcScreen.right - rcNewClient.Width();
00139     if (ptTip.y < 0)
00140         ptTip.y = 0;
00141     else if (ptTip.y + rcNewClient.Height() > rcScreen.bottom)
00142         ptTip.y = rcScreen.bottom - rcNewClient.Height();
00143 
00144     // Position and show the tip
00145     SetWindowPos(&CWnd::wndTop, ptTip.x, ptTip.y, rcNewClient.Width(), rcNewClient.Height(),
00146         SWP_NOACTIVATE | SWP_SHOWWINDOW);
00147 }
00148 
00149 void CBrowserToolTip::Hide()
00150 {
00151     ShowWindow(SW_HIDE);
00152 }