00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "CoolInterface.h"
00026 #include "CtrlMainTabBar.h"
00027 #include "Skin.h"
00028 #include "SkinWindow.h"
00029
00030 IMPLEMENT_DYNAMIC(CMainTabBarCtrl, CControlBar)
00031
00032 BEGIN_MESSAGE_MAP(CMainTabBarCtrl, CControlBar)
00033 ON_WM_CREATE()
00034 ON_WM_SETCURSOR()
00035 ON_WM_MOUSEMOVE()
00036 ON_WM_LBUTTONDBLCLK()
00037 ON_WM_LBUTTONDOWN()
00038 ON_WM_LBUTTONUP()
00039 ON_WM_RBUTTONDOWN()
00040 ON_WM_TIMER()
00041 END_MESSAGE_MAP()
00042
00043
00045
00046
00047 CMainTabBarCtrl::CMainTabBarCtrl()
00048 {
00049 m_pSkin = NULL;
00050 m_pHover = NULL;
00051 m_pDown = NULL;
00052 }
00053
00054 CMainTabBarCtrl::~CMainTabBarCtrl()
00055 {
00056 for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
00057 {
00058 delete (TabItem*)m_pItems.GetNext( pos );
00059 }
00060
00061 if ( m_dcSkin.m_hDC != NULL )
00062 {
00063 if ( m_hOldSkin != NULL ) m_dcSkin.SelectObject( CBitmap::FromHandle( m_hOldSkin ) );
00064 m_bmSkin.DeleteObject();
00065 m_dcSkin.DeleteDC();
00066 }
00067 }
00068
00070
00071
00072 BOOL CMainTabBarCtrl::Create(CWnd* pParentWnd, DWORD dwStyle, UINT nID)
00073 {
00074 CRect rc;
00075 dwStyle |= WS_CHILD;
00076 return CWnd::Create( NULL, NULL, dwStyle, rc, pParentWnd, nID, NULL );
00077 }
00078
00079 BOOL CMainTabBarCtrl::HasLocalVersion()
00080 {
00081 return ( m_pSkin != NULL ) && ( m_pSkin->m_sLanguage == Settings.General.Language );
00082 }
00083
00084 void CMainTabBarCtrl::OnSkinChange()
00085 {
00086 if ( m_pItems.GetCount() == 0 )
00087 {
00088 m_pItems.AddTail( new TabItem( this, _T("_ID_TAB_HOME") ) );
00089 m_pItems.AddTail( new TabItem( this, _T("_ID_TAB_LIBRARY") ) );
00090 m_pItems.AddTail( new TabItem( this, _T("_ID_TAB_MEDIA") ) );
00091 m_pItems.AddTail( new TabItem( this, _T("_ID_TAB_SEARCH") ) );
00092 m_pItems.AddTail( new TabItem( this, _T("_ID_TAB_TRANSFERS") ) );
00093 m_pItems.AddTail( new TabItem( this, _T("_ID_TAB_NETWORK") ) );
00094 }
00095
00096 if ( m_dcSkin.m_hDC != NULL )
00097 {
00098 if ( m_hOldSkin != NULL ) m_dcSkin.SelectObject( CBitmap::FromHandle( m_hOldSkin ) );
00099 m_bmSkin.DeleteObject();
00100 m_dcSkin.DeleteDC();
00101 }
00102
00103 m_pSkin = Skin.GetWindowSkin( this );
00104
00105 if ( m_pSkin != NULL )
00106 {
00107 BITMAP pInfo;
00108 CDC dcScreen;
00109
00110 dcScreen.Attach( ::GetDC( 0 ) );
00111 m_pSkin->Prepare( &dcScreen );
00112 m_pSkin->m_bmSkin.GetBitmap( &pInfo );
00113 m_dcSkin.CreateCompatibleDC( &dcScreen );
00114 m_bmSkin.CreateCompatibleBitmap( &dcScreen, pInfo.bmWidth, pInfo.bmHeight );
00115 ::ReleaseDC( 0, dcScreen.Detach() );
00116
00117 m_hOldSkin = (HBITMAP)m_dcSkin.SelectObject( &m_bmSkin )->GetSafeHandle();
00118 m_dcSkin.BitBlt( 0, 0, pInfo.bmWidth, pInfo.bmHeight, &m_pSkin->m_dcSkin, 0, 0, SRCCOPY );
00119 m_dcSkin.SelectObject( CBitmap::FromHandle( m_hOldSkin ) );
00120
00121 m_hOldSkin = NULL;
00122
00123 #ifndef _DEBUG
00124 for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
00125 {
00126 TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );
00127 pItem->Skin( m_pSkin, &m_dcSkin, &m_bmSkin );
00128 }
00129
00130 m_hOldSkin = (HBITMAP)m_dcSkin.SelectObject( &m_bmSkin )->GetSafeHandle();
00131 #endif
00132 }
00133
00134 CMDIFrameWnd* pOwner = (CMDIFrameWnd*)GetOwner();
00135
00136 if ( pOwner != NULL && pOwner->IsKindOf( RUNTIME_CLASS(CMDIFrameWnd) ) )
00137 {
00138 if ( ! pOwner->IsIconic() ) pOwner->RecalcLayout();
00139 }
00140 }
00141
00143
00144
00145 int CMainTabBarCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
00146 {
00147 if ( CControlBar::OnCreate( lpCreateStruct ) == -1 ) return -1;
00148 m_dwStyle |= CBRS_BORDER_3D;
00149 SetTimer( 1, 250, NULL );
00150 return 0;
00151 }
00152
00153 void CMainTabBarCtrl::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
00154 {
00155 BOOL bChanged = FALSE;
00156
00157 for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
00158 {
00159 TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );
00160 bChanged |= pItem->Update( pTarget );
00161 }
00162
00163 if ( bChanged ) Invalidate();
00164 }
00165
00166 CSize CMainTabBarCtrl::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
00167 {
00168 CRect rcBackground;
00169 CSize size( 0, 28 );
00170
00171 if ( m_pSkin != NULL && m_pSkin->GetAnchor( _T("Background"), rcBackground ) )
00172 {
00173 size = rcBackground.Size();
00174 }
00175
00176 if ( bStretch )
00177 {
00178 size.cx = 32000;
00179
00180 if ( CWnd* pParent = GetOwner() )
00181 {
00182 CRect rc;
00183 pParent->GetWindowRect( &rc );
00184 if ( rc.Width() > 32 ) size.cx = rc.Width() + 2;
00185 }
00186 }
00187
00188 return size;
00189 }
00190
00191 CMainTabBarCtrl::TabItem* CMainTabBarCtrl::HitTest(const CPoint& point) const
00192 {
00193 CPoint ptLocal( point );
00194 CRect rcClient;
00195
00196 GetClientRect( &rcClient );
00197 CalcInsideRect( rcClient, FALSE );
00198
00199 rcClient.left -= m_cyTopBorder;
00200 rcClient.top -= m_cxLeftBorder;
00201 rcClient.right += m_cyBottomBorder;
00202 rcClient.bottom += m_cxRightBorder;
00203 ptLocal.Offset( -rcClient.left, -rcClient.top );
00204
00205 for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
00206 {
00207 TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );
00208 if ( pItem->HitTest( ptLocal ) ) return pItem;
00209 }
00210
00211 return NULL;
00212 }
00213
00214 int CMainTabBarCtrl::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
00215 {
00216 TabItem* pItem = HitTest( point );
00217
00218 if ( pItem == NULL ) return -1;
00219 if ( pTI == NULL ) return 1;
00220
00221 pTI->uFlags = 0;
00222 pTI->hwnd = GetSafeHwnd();
00223 pTI->uId = (UINT)pItem->m_nID;
00224 pTI->rect = pItem->m_rc;
00225 pTI->lpszText = LPSTR_TEXTCALLBACK;
00226
00227 CString strTip;
00228
00229 if ( LoadString( strTip, pTI->uId ) )
00230 {
00231 if ( LPCTSTR pszBreak = _tcschr( strTip, '\n' ) )
00232 {
00233 pTI->lpszText = _tcsdup( pszBreak + 1 );
00234 }
00235 else
00236 {
00237 strTip = strTip.SpanExcluding( _T(".") );
00238 pTI->lpszText = _tcsdup( strTip );
00239 }
00240 }
00241
00242 return pTI->uId;
00243 }
00244
00245 void CMainTabBarCtrl::DoPaint(CDC* pDC)
00246 {
00247 ASSERT_VALID( this );
00248 ASSERT_VALID( pDC );
00249
00250 CRect rc;
00251 GetClientRect( &rc );
00252
00253 if ( m_pSkin == NULL )
00254 {
00255 DrawBorders( pDC, rc );
00256 pDC->FillSolidRect( &rc, CoolInterface.m_crMidtone );
00257 return;
00258 }
00259 else if ( m_hOldSkin == NULL )
00260 {
00261 for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
00262 {
00263 TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );
00264 pItem->Skin( m_pSkin, &m_dcSkin, &m_bmSkin );
00265 }
00266
00267 m_hOldSkin = (HBITMAP)m_dcSkin.SelectObject( &m_bmSkin )->GetSafeHandle();
00268 }
00269
00270 CDC* pBuffer = CoolInterface.GetBuffer( *pDC, rc.Size() );
00271
00272 DrawBorders( pBuffer, rc );
00273
00274 if ( ! CoolInterface.DrawWatermark( pBuffer, &rc, &m_pSkin->m_bmWatermark ) )
00275 {
00276 pBuffer->FillSolidRect( &rc, CoolInterface.m_crMidtone );
00277 }
00278
00279 CPoint ptOffset = rc.TopLeft();
00280
00281 for ( POSITION pos = m_pItems.GetHeadPosition() ; pos ; )
00282 {
00283 TabItem* pItem = (TabItem*)m_pItems.GetNext( pos );
00284 pItem->Paint( pBuffer, &m_dcSkin, ptOffset,
00285 ( m_pHover == pItem && m_pDown == NULL ) || ( m_pDown == pItem ),
00286 ( m_pDown == pItem ) && ( m_pHover == pItem ) );
00287 }
00288
00289 GetClientRect( &rc );
00290
00291 pDC->BitBlt( 0, 0, rc.Width(), rc.Height(), pBuffer, 0, 0, SRCCOPY );
00292 }
00293
00294 BOOL CMainTabBarCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
00295 {
00296 if ( nHitTest == HTCLIENT )
00297 {
00298 CPoint point;
00299 GetCursorPos( &point );
00300 ScreenToClient( &point );
00301
00302 if ( TabItem* pItem = HitTest( point ) )
00303 {
00304 if ( pItem->m_bEnabled )
00305 {
00306 SetCursor( theApp.LoadCursor( IDC_HAND ) );
00307 return TRUE;
00308 }
00309 }
00310 }
00311
00312 return CControlBar::OnSetCursor( pWnd, nHitTest, message );
00313 }
00314
00315 void CMainTabBarCtrl::OnMouseMove(UINT nFlags, CPoint point)
00316 {
00317 TabItem* pItem = HitTest( point );
00318
00319 if ( pItem != m_pHover )
00320 {
00321 m_pHover = pItem;
00322 Invalidate();
00323 }
00324 }
00325
00326 void CMainTabBarCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
00327 {
00328 OnLButtonDown( nFlags, point );
00329 }
00330
00331 void CMainTabBarCtrl::OnLButtonDown(UINT nFlags, CPoint point)
00332 {
00333 m_pHover = HitTest( point );
00334
00335 if ( m_pHover != NULL && m_pHover->m_bEnabled )
00336 {
00337 m_pDown = m_pHover;
00338 }
00339
00340 if ( m_pHover == NULL )
00341 {
00342 CControlBar::OnLButtonDown( nFlags, point );
00343 }
00344 else
00345 {
00346 SetCapture();
00347 }
00348
00349 Invalidate();
00350 }
00351
00352 void CMainTabBarCtrl::OnLButtonUp(UINT nFlags, CPoint point)
00353 {
00354 if ( m_pDown != NULL && m_pHover == m_pDown )
00355 {
00356 GetOwner()->PostMessage( WM_COMMAND, m_pDown->m_nID );
00357 }
00358
00359 m_pDown = NULL;
00360 ReleaseCapture();
00361 Invalidate();
00362
00363 if ( m_pHover == NULL ) CControlBar::OnLButtonUp( nFlags, point );
00364 }
00365
00366 void CMainTabBarCtrl::OnRButtonDown(UINT nFlags, CPoint point)
00367 {
00368 if ( m_pDown != NULL || m_pHover != NULL )
00369 {
00370 m_pDown = m_pHover = NULL;
00371 ReleaseCapture();
00372 Invalidate();
00373 }
00374
00375 CControlBar::OnRButtonDown( nFlags, point );
00376 }
00377
00378 void CMainTabBarCtrl::OnTimer(UINT nIDEvent)
00379 {
00380 if ( m_pHover != NULL && m_pDown == NULL )
00381 {
00382 CPoint point;
00383 CRect rect;
00384
00385 GetCursorPos( &point );
00386 GetWindowRect( &rect );
00387
00388 if ( rect.PtInRect( point ) == FALSE )
00389 {
00390 m_pHover = NULL;
00391 Invalidate();
00392 }
00393 }
00394 }
00395
00397
00398
00399 CMainTabBarCtrl::TabItem::TabItem(CMainTabBarCtrl* pCtrl, LPCTSTR pszName)
00400 {
00401 m_pCtrl = pCtrl;
00402 m_nID = CoolInterface.NameToID( pszName + 1 );
00403 m_sName = pszName;
00404 m_bEnabled = TRUE;
00405 m_bSelected = FALSE;
00406 }
00407
00409
00410
00411 void CMainTabBarCtrl::TabItem::Skin(CSkinWindow* pSkin, CDC* pdcCache, CBitmap* pbmCache)
00412 {
00413 static LPCTSTR pszState[] = { _T(".Checked"), _T(".Down"), _T(".Hover"), _T(".Up"), _T(".Disabled"), NULL };
00414
00415 m_rc.SetRectEmpty();
00416 pSkin->GetAnchor( m_sName, m_rc );
00417
00418 for ( int nState = 0 ; pszState[ nState ] != NULL ; nState++ )
00419 {
00420 CRect* pRect = &m_rcSrc[ nState ];
00421 pRect->SetRectEmpty();
00422
00423 if ( pSkin->GetPart( m_sName + pszState[ nState ], *pRect ) )
00424 {
00425 CBitmap* pOld = (CBitmap*)pdcCache->SelectObject( pbmCache );
00426
00427 if ( pSkin->m_bmWatermark.m_hObject != NULL )
00428 {
00429 pdcCache->IntersectClipRect( pRect );
00430 CoolInterface.DrawWatermark( pdcCache, pRect, &pSkin->m_bmWatermark,
00431 m_rc.left, m_rc.top );
00432 pdcCache->SelectClipRgn( NULL );
00433 }
00434 else
00435 {
00436 pdcCache->FillSolidRect( pRect, CoolInterface.m_crMidtone );
00437 }
00438
00439 pdcCache->SelectObject( pOld );
00440
00441 pSkin->PreBlend( pbmCache, *pRect, *pRect );
00442 }
00443 }
00444 }
00445
00447
00448
00449 BOOL CMainTabBarCtrl::TabItem::Update(CFrameWnd* pWnd)
00450 {
00451 BOOL bEnabled = m_bEnabled;
00452 BOOL bSelected = m_bSelected;
00453
00454 DoUpdate( pWnd, TRUE );
00455
00456 return ( bEnabled != m_bEnabled || bSelected != m_bSelected );
00457 }
00458
00460
00461
00462 BOOL CMainTabBarCtrl::TabItem::HitTest(const CPoint& point) const
00463 {
00464 return m_rc.PtInRect( point );
00465 }
00466
00468
00469
00470 void CMainTabBarCtrl::TabItem::Paint(CDC* pDstDC, CDC* pSrcDC, const CPoint& ptOffset, BOOL bHover, BOOL bDown)
00471 {
00472 CRect* pPart = NULL;
00473
00474 if ( bDown )
00475 pPart = &m_rcSrc[1];
00476 else if ( m_bSelected )
00477 pPart = &m_rcSrc[0];
00478 else if ( ! m_bEnabled )
00479 pPart = &m_rcSrc[4];
00480 else if ( bHover )
00481 pPart = &m_rcSrc[2];
00482 else
00483 pPart = &m_rcSrc[3];
00484
00485 CRect rcTarget( m_rc );
00486 rcTarget += ptOffset;
00487
00488 pDstDC->BitBlt( rcTarget.left, rcTarget.top, rcTarget.Width(), rcTarget.Height(),
00489 pSrcDC, pPart->left, pPart->top, SRCCOPY );
00490 }
00491
00492 void CMainTabBarCtrl::TabItem::Enable(BOOL bEnable)
00493 {
00494 m_bEnabled = bEnable;
00495 }
00496
00497 void CMainTabBarCtrl::TabItem::SetCheck(BOOL bCheck)
00498 {
00499 m_bSelected = bCheck;
00500 }