Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

CtrlCoolTip.cpp

Go to the documentation of this file.
00001 //
00002 // CtrlCoolTip.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "CoolInterface.h"
00026 #include "CtrlCoolTip.h"
00027 #include "GraphLine.h"
00028 
00029 #ifdef _DEBUG
00030 #define new DEBUG_NEW
00031 #undef THIS_FILE
00032 static char THIS_FILE[] = __FILE__;
00033 #endif
00034 
00035 IMPLEMENT_DYNAMIC(CCoolTipCtrl, CWnd)
00036 
00037 BEGIN_MESSAGE_MAP(CCoolTipCtrl, CWnd)
00038         //{{AFX_MSG_MAP(CCoolTipCtrl)
00039         ON_WM_CREATE()
00040         ON_WM_DESTROY()
00041         ON_WM_ERASEBKGND()
00042         ON_WM_PAINT()
00043         ON_WM_MOUSEMOVE()
00044         ON_WM_KEYDOWN()
00045         ON_WM_TIMER()
00046         //}}AFX_MSG_MAP
00047 END_MESSAGE_MAP()
00048 
00049 LPCTSTR CCoolTipCtrl::m_hClass = NULL;
00050 
00051 #define TIP_TIMER               100
00052 #define TIP_OFFSET_X    0
00053 #define TIP_OFFSET_Y    24
00054 #define TIP_MARGIN              6
00055 #define TIP_TEXTHEIGHT  14
00056 
00057 
00059 // CCoolTipCtrl construction
00060 
00061 CCoolTipCtrl::CCoolTipCtrl()
00062 {
00063         m_pbEnable      = NULL;
00064         m_pContext      = NULL;
00065         m_hAltWnd       = NULL;
00066         m_bTimer        = FALSE;
00067         m_bVisible      = FALSE;
00068         m_tOpen         = 0;
00069 
00070         if ( m_hClass == NULL ) m_hClass = AfxRegisterWndClass( CS_SAVEBITS );
00071 
00072         if ( m_hClass == NULL ) m_hClass = AfxRegisterWndClass( CS_SAVEBITS );
00073 }
00074 
00075 CCoolTipCtrl::~CCoolTipCtrl()
00076 {
00077         if ( m_hWnd != NULL ) DestroyWindow();
00078 }
00079 
00081 // CCoolTipCtrl operations
00082 
00083 BOOL CCoolTipCtrl::Create(CWnd* pParentWnd, BOOL* pbEnable)
00084 {
00085         CRect rc( 0, 0, 0, 0 );
00086 
00087         DWORD dwStyleEx = WS_EX_TOPMOST | ( theApp.m_bRTL ? WS_EX_LAYOUTRTL : 0 );
00088         if ( ! CWnd::CreateEx( dwStyleEx, m_hClass, NULL, WS_POPUP|WS_DISABLED,
00089                 rc, pParentWnd, 0, NULL ) ) return FALSE;
00090 
00091         SetOwner( pParentWnd );
00092         m_pbEnable = pbEnable;
00093 
00094         return TRUE;
00095 }
00096 
00097 void CCoolTipCtrl::Show(LPVOID pContext, HWND hAltWnd)
00098 {
00099         if ( pContext == NULL ) return;
00100         if ( AfxGetMainWnd() != GetForegroundWindow() ) return;
00101         if ( m_pbEnable != NULL && *m_pbEnable == 0 ) return;
00102 
00103         CPoint point;
00104         GetCursorPos( &point );
00105 
00106         m_hAltWnd = hAltWnd;
00107 
00108         if ( m_bVisible )
00109         {
00110                 if ( pContext == m_pContext ) return;
00111 
00112                 Hide();
00113 
00114                 m_pContext = pContext;
00115 
00116                 ShowImpl();
00117         }
00118         else if ( point != m_pOpen )
00119         {
00120                 m_pContext      = pContext;
00121                 m_pOpen         = point;
00122                 m_tOpen         = GetTickCount() + Settings.Interface.TipDelay;
00123 
00124                 if ( ! m_bTimer )
00125                 {
00126                         SetTimer( 1, TIP_TIMER, NULL );
00127                         m_bTimer = TRUE;
00128                 }
00129         }
00130 }
00131 
00132 void CCoolTipCtrl::Hide()
00133 {
00134         m_pContext      = NULL;
00135         m_tOpen         = 0;
00136 
00137         if ( m_bVisible )
00138         {
00139                 OnHide();
00140 
00141                 ShowWindow( SW_HIDE );
00142                 ModifyStyleEx( WS_EX_LAYERED, 0 );
00143                 m_bVisible = FALSE;
00144                 GetCursorPos( &m_pOpen );
00145         }
00146 
00147         if ( m_bTimer )
00148         {
00149                 KillTimer( 1 );
00150                 m_bTimer = FALSE;
00151         }
00152 }
00153 
00154 void CCoolTipCtrl::ShowImpl()
00155 {
00156         /*
00157         if ( m_bVisible ) return;
00158 
00159         m_sz.cx = m_sz.cy = 0;
00160 
00161         if ( ! OnPrepare() ) return;
00162 
00163         CRect rc( m_pOpen.x + TIP_OFFSET_X, m_pOpen.y + TIP_OFFSET_Y, 0, 0 );
00164         rc.right = rc.left + m_sz.cx + TIP_MARGIN * 2;
00165         rc.bottom = rc.top + m_sz.cy + TIP_MARGIN * 2;
00166 
00167         if ( rc.right >= GetSystemMetrics( SM_CXSCREEN ) )
00168         {
00169                 rc.OffsetRect( GetSystemMetrics( SM_CXSCREEN ) - rc.right - 4, 0 );
00170         }
00171 
00172         if ( rc.bottom >= GetSystemMetrics( SM_CYSCREEN ) )
00173         {
00174                 rc.OffsetRect( 0, - ( m_sz.cy + TIP_MARGIN * 2 + TIP_OFFSET_Y + 4 ) );
00175         }
00176         */
00177         if ( m_bVisible ) return;
00178 
00179         m_sz.cx = m_sz.cy = 0;
00180 
00181         if ( ! OnPrepare() ) return;
00182 
00183         HMONITOR hMonitor = NULL;
00184         MONITORINFO mi = {0};
00185         CRect rcMonitor( 0, 0, 0, 0 );
00186         CRect rc( m_pOpen.x + TIP_OFFSET_X, m_pOpen.y + TIP_OFFSET_Y, 0, 0 );
00187         rc.right = rc.left + m_sz.cx + TIP_MARGIN * 2;
00188         rc.bottom = rc.top + m_sz.cy + TIP_MARGIN * 2;
00189 
00190         if ( ( theApp.m_dwWindowsVersion >= 5 ) && (GetSystemMetrics( SM_CMONITORS ) > 1) && (theApp.m_pfnMonitorFromRect) )
00191         {
00192                 mi.cbSize = sizeof(MONITORINFO);
00193 
00194                 hMonitor = theApp.m_pfnMonitorFromRect( rc, MONITOR_DEFAULTTONEAREST );
00195                 //hMonitor = MonitorFromPoint( m_pOpen, MONITOR_DEFAULTTONEAREST );
00196                 if (NULL != hMonitor)
00197                 {
00198                         if ( theApp.m_pfnGetMonitorInfoA(hMonitor, &mi) )
00199                                 rcMonitor = mi.rcWork;
00200                         else
00201                                 hMonitor = NULL; // Fall back to GetSystemMetrics
00202                 }
00203 
00204         }
00205 
00206         if ( NULL == hMonitor )
00207         {
00208                 // Unimon system or something is wrong with multimon
00209 
00210                 rcMonitor.right = GetSystemMetrics( SM_CXSCREEN );
00211                 rcMonitor.bottom = GetSystemMetrics( SM_CYSCREEN );
00212         }
00213 
00214         if ( rc.right >= rcMonitor.right)
00215         {
00216                 rc.OffsetRect( rcMonitor.right - rc.right - 4, 0 );
00217         }
00218 
00219         if ( rc.bottom >= rcMonitor.bottom )
00220         {
00221                 rc.OffsetRect( 0, - ( m_sz.cy + TIP_MARGIN * 2 + TIP_OFFSET_Y + 4 ) );
00222         }
00223 
00224 
00225         m_bVisible = TRUE;
00226 
00227         OnShow();
00228 
00229         if ( Settings.Interface.TipAlpha == 255 || theApp.m_pfnSetLayeredWindowAttributes == NULL )
00230         {
00231                 ModifyStyleEx( WS_EX_LAYERED, 0 );
00232         }
00233         else
00234         {
00235                 ModifyStyleEx( 0, WS_EX_LAYERED );
00236                 (*theApp.m_pfnSetLayeredWindowAttributes)( GetSafeHwnd(),
00237                         0, (BYTE)Settings.Interface.TipAlpha, LWA_ALPHA );
00238         }
00239 
00240         SetWindowPos( &wndTopMost, rc.left, rc.top, rc.Width(), rc.Height(),
00241                 SWP_SHOWWINDOW|SWP_NOACTIVATE );
00242         UpdateWindow();
00243 
00244         if ( ! m_bTimer )
00245         {
00246                 SetTimer( 1, TIP_TIMER, NULL );
00247                 m_bTimer = TRUE;
00248         }
00249 }
00250 
00251 void CCoolTipCtrl::CalcSizeHelper()
00252 {
00253         CClientDC dc( this );
00254 
00255         m_sz.cx = m_sz.cy = 0;
00256 
00257         CFont* pOldFont = (CFont*)dc.SelectObject( &CoolInterface.m_fntBold );
00258 
00259         OnCalcSize( &dc );
00260 
00261         dc.SelectObject( pOldFont );
00262 }
00263 
00264 void CCoolTipCtrl::AddSize(CDC* pDC, LPCTSTR pszText, int nBase)
00265 {
00266         CSize szText = pDC->GetTextExtent( pszText, _tcslen( pszText ) );
00267         szText.cx += nBase;
00268         m_sz.cx = max( m_sz.cx, szText.cx );
00269 }
00270 
00271 void CCoolTipCtrl::GetPaintRect(RECT* pRect)
00272 {
00273         pRect->left = 0;
00274         pRect->top = 0;
00275         pRect->right = m_sz.cx;
00276         pRect->bottom = m_sz.cy;
00277 }
00278 
00279 void CCoolTipCtrl::DrawText(CDC* pDC, POINT* pPoint, LPCTSTR pszText, int nBase)
00280 {
00281         DWORD dwFlags = ( theApp.m_bRTL ? ETO_RTLREADING : 0 );
00282         CSize sz = pDC->GetTextExtent( pszText, _tcslen( pszText ) );
00283 
00284         if ( nBase ) pPoint->x += nBase;
00285         CRect rc( pPoint->x - 2, pPoint->y - 2, pPoint->x + sz.cx + 2, pPoint->y + sz.cy + 2 );
00286 
00287         pDC->SetBkColor( CoolInterface.m_crTipBack );
00288         pDC->ExtTextOut( pPoint->x, pPoint->y, ETO_CLIPPED|ETO_OPAQUE|dwFlags, &rc, pszText, _tcslen( pszText ), NULL );
00289         pDC->ExcludeClipRect( &rc );
00290 
00291         if ( nBase ) pPoint->x -= nBase;
00292 }
00293 
00294 void CCoolTipCtrl::DrawRule(CDC* pDC, POINT* pPoint, BOOL bPos)
00295 {
00296         pPoint->y += 5;
00297         if ( bPos )
00298         {
00299                 pDC->Draw3dRect( pPoint->x, pPoint->y,
00300                         m_sz.cx + ( TIP_MARGIN - 3 ) - pPoint->x, 1, CoolInterface.m_crTipBorder,
00301                         CoolInterface.m_crTipBorder );
00302                 pDC->ExcludeClipRect( pPoint->x, pPoint->y,
00303                         m_sz.cx + ( TIP_MARGIN - 3 ), pPoint->y + 1 );
00304         }
00305         else
00306         {
00307                 pDC->Draw3dRect( -( TIP_MARGIN - 3 ), pPoint->y,
00308                         m_sz.cx + ( TIP_MARGIN - 3 ) * 2, 1, CoolInterface.m_crTipBorder,
00309                         CoolInterface.m_crTipBorder );
00310                 pDC->ExcludeClipRect( -( TIP_MARGIN - 3 ), pPoint->y,
00311                         m_sz.cx + ( TIP_MARGIN - 3 ), pPoint->y + 1 );
00312         }
00313         pPoint->y += 6;
00314 }
00315 
00316 BOOL CCoolTipCtrl::WindowFromPointBelongsToOwner(const CPoint& point)
00317 {
00318         CRect rc;
00319         GetOwner()->GetWindowRect( &rc );
00320 
00321         if ( ! rc.PtInRect( point ) ) return FALSE;
00322 
00323         CWnd* pWnd = WindowFromPoint( point );
00324 
00325         while ( pWnd )
00326         {
00327                 if ( pWnd == GetOwner() ) return TRUE;
00328                 if ( m_hAltWnd != NULL && pWnd->GetSafeHwnd() == m_hAltWnd ) return TRUE;
00329                 pWnd = pWnd->GetParent();
00330         }
00331 
00332         return FALSE;
00333 }
00334 
00335 CLineGraph* CCoolTipCtrl::CreateLineGraph()
00336 {
00337         CLineGraph* pGraph = new CLineGraph();
00338 
00339         pGraph->m_bShowLegend   = FALSE;
00340         pGraph->m_bShowAxis             = FALSE;
00341         pGraph->m_nMinGridVert  = 16;
00342 
00343         pGraph->m_crBack = CoolInterface.CalculateColour(
00344                 RGB( 255, 255, 255 ), CoolInterface.m_crTipBack, 80 );
00345         pGraph->m_crGrid = CoolInterface.CalculateColour(
00346                 CoolInterface.m_crTipBorder, pGraph->m_crBack, 180 );
00347 
00348         return pGraph;
00349 }
00350 
00352 // CCoolTipCtrl message handlers
00353 
00354 int CCoolTipCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
00355 {
00356         if ( CWnd::OnCreate( lpCreateStruct ) == -1 ) return -1;
00357         m_bTimer = FALSE;
00358         return 0;
00359 }
00360 
00361 void CCoolTipCtrl::OnDestroy()
00362 {
00363         if ( m_bTimer ) KillTimer( 1 );
00364         m_bTimer = FALSE;
00365         if ( m_bVisible ) Hide();
00366         CWnd::OnDestroy();
00367 }
00368 
00369 BOOL CCoolTipCtrl::OnEraseBkgnd(CDC* pDC)
00370 {
00371         return TRUE;
00372 }
00373 
00374 void CCoolTipCtrl::OnPaint()
00375 {
00376         if ( ! IsWindow( GetSafeHwnd() ) || ! IsWindowVisible() ) return;
00377 
00378         CPaintDC dc( this );
00379         CRect rc;
00380 
00381         GetClientRect( &rc );
00382 
00383         CFont* pOldFont = (CFont*)dc.SelectObject( &CoolInterface.m_fntBold );
00384 
00385         dc.Draw3dRect( &rc, CoolInterface.m_crTipBorder, CoolInterface.m_crTipBorder );
00386         dc.SetViewportOrg( TIP_MARGIN, TIP_MARGIN );
00387         rc.DeflateRect( 1, 1 );
00388 
00389         OnPaint( &dc );
00390 
00391         dc.SetViewportOrg( 0, 0 );
00392         dc.FillSolidRect( &rc, CoolInterface.m_crTipBack );
00393         dc.SelectObject( pOldFont );
00394 }
00395 
00396 void CCoolTipCtrl::OnMouseMove(UINT nFlags, CPoint point)
00397 {
00398         Hide();
00399 }
00400 
00401 void CCoolTipCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
00402 {
00403         Hide();
00404         CWnd::OnKeyDown( nChar, nRepCnt, nFlags );
00405 }
00406 
00407 void CCoolTipCtrl::OnTimer(UINT nIDEvent)
00408 {
00409         CPoint point;
00410         GetCursorPos( &point );
00411 
00412         if ( WindowFromPointBelongsToOwner( point ) )
00413         {
00414                 CWnd* pWnd = GetForegroundWindow();
00415 
00416                 if ( pWnd != this && pWnd != AfxGetMainWnd() )
00417                 {
00418                         if ( m_bVisible ) Hide();
00419                         return;
00420                 }
00421         }
00422         else
00423         {
00424                 if ( m_bVisible ) Hide();
00425                 return;
00426         }
00427 
00428         if ( ! m_bVisible && m_tOpen && GetTickCount() >= m_tOpen )
00429         {
00430                 m_tOpen = 0;
00431                 if ( point == m_pOpen || m_hAltWnd != NULL ) ShowImpl();
00432         }
00433 }
00434 
00436 // CCoolTipCtrl events
00437 
00438 BOOL CCoolTipCtrl::OnPrepare()
00439 {
00440         return FALSE;
00441 }
00442 
00443 void CCoolTipCtrl::OnCalcSize(CDC* pDC)
00444 {
00445 }
00446 
00447 void CCoolTipCtrl::OnShow()
00448 {
00449 }
00450 
00451 void CCoolTipCtrl::OnHide()
00452 {
00453 }
00454 
00455 void CCoolTipCtrl::OnPaint(CDC* pDC)
00456 {
00457 }

Generated on Thu Dec 15 10:39:35 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2