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 "GraphItem.h"
00026 #include "CoolInterface.h"
00027 #include "CtrlMonitorBar.h"
00028 #include "Skin.h"
00029
00030 BEGIN_MESSAGE_MAP(CMonitorBarCtrl, CControlBar)
00031 ON_WM_CREATE()
00032 ON_WM_DESTROY()
00033 ON_WM_SIZE()
00034 ON_WM_TIMER()
00035 ON_WM_SETCURSOR()
00036 ON_WM_LBUTTONDOWN()
00037 END_MESSAGE_MAP()
00038
00039
00041
00042
00043 CMonitorBarCtrl::CMonitorBarCtrl()
00044 {
00045 m_pSnapBar[0] = NULL;
00046 m_pSnapBar[1] = NULL;
00047 m_pTxItem = new CGraphItem( GRC_TOTAL_BANDWIDTH_OUT, 0, RGB( 0xFF, 0xFF, 0 ) );
00048 m_pRxItem = new CGraphItem( GRC_TOTAL_BANDWIDTH_IN, 0, RGB( 0, 0xFF, 0 ) );
00049 m_nMaximum = 0;
00050 m_nCount = 0;
00051 m_bTab = FALSE;
00052 }
00053
00054 CMonitorBarCtrl::~CMonitorBarCtrl()
00055 {
00056 delete m_pRxItem;
00057 delete m_pTxItem;
00058 }
00059
00061
00062
00063 BOOL CMonitorBarCtrl::Create(CWnd* pParentWnd, DWORD dwStyle, UINT nID)
00064 {
00065 CRect rc( 0, 0, 0, 0 );
00066 dwStyle |= WS_CHILD|WS_CLIPCHILDREN;
00067 return CWnd::Create( NULL, NULL, dwStyle, rc, pParentWnd, nID, NULL );
00068 }
00069
00070 int CMonitorBarCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
00071 {
00072 if ( CControlBar::OnCreate( lpCreateStruct ) == -1 ) return -1;
00073 m_dwStyle |= CBRS_BORDER_3D;
00074
00075 if ( lpCreateStruct->dwExStyle & WS_EX_LAYOUTRTL )
00076 {
00077 lpCreateStruct->dwExStyle ^= WS_EX_LAYOUTRTL;
00078 SetWindowLong( this->m_hWnd, GWL_EXSTYLE, lpCreateStruct->dwExStyle );
00079 }
00080
00081 m_hTab = (HICON)LoadImage( AfxGetResourceHandle(),
00082 MAKEINTRESOURCE(IDI_POINTER_ARROW), IMAGE_ICON, 16, 16, 0 );
00083
00084 OnSkinChange();
00085
00086 SetTimer( 1, 50, NULL );
00087
00088 return 0;
00089 }
00090
00091 void CMonitorBarCtrl::OnDestroy()
00092 {
00093 KillTimer( 1 );
00094 CControlBar::OnDestroy();
00095 }
00096
00098
00099
00100 CSize CMonitorBarCtrl::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
00101 {
00102 CSize size( 128, 30 );
00103
00104 for ( int nSnap = 1 ; nSnap >= 0 ; nSnap-- )
00105 {
00106 if ( m_pSnapBar[ nSnap ] != NULL && m_pSnapBar[ nSnap ]->IsVisible() )
00107 {
00108 size.cy = m_pSnapBar[ nSnap ]->CalcFixedLayout( FALSE, TRUE ).cy;
00109 break;
00110 }
00111 }
00112
00113 return size;
00114 }
00115
00116 void CMonitorBarCtrl::OnSize(UINT nType, int cx, int cy)
00117 {
00118 CControlBar::OnSize( nType, cx, cy );
00119 Invalidate();
00120 }
00121
00123
00124
00125 void CMonitorBarCtrl::OnTimer(UINT nIDEvent)
00126 {
00127 if ( m_nCount++ & 1 )
00128 {
00129 m_pTxItem->Update();
00130 m_pRxItem->Update();
00131
00132 m_nMaximum = m_pTxItem->GetMaximum();
00133 DWORD nSecond = m_pRxItem->GetMaximum();
00134 m_nMaximum = max( m_nMaximum, nSecond );
00135 }
00136
00137 m_nMaximum = max( m_nMaximum, Settings.Connection.InSpeed * 1024 );
00138 m_nMaximum = max( m_nMaximum, Settings.Connection.OutSpeed * 1024 );
00139
00140 if ( IsWindowVisible() ) Invalidate();
00141 }
00142
00144
00145
00146 void CMonitorBarCtrl::OnSkinChange()
00147 {
00148 HBITMAP hWatermark = Skin.GetWatermark( _T("CMonitorBar") );
00149 if ( m_bmWatermark.m_hObject != NULL ) m_bmWatermark.DeleteObject();
00150 if ( hWatermark != NULL ) m_bmWatermark.Attach( hWatermark );
00151
00152 if ( m_hWnd != NULL && IsWindowVisible() ) Invalidate();
00153 }
00154
00155 void CMonitorBarCtrl::DoPaint(CDC* pDC)
00156 {
00157 CRect rcClient;
00158 GetClientRect( &rcClient );
00159
00160 CDC* pMemDC = CoolInterface.GetBuffer( *pDC, rcClient.Size() );
00161 if ( theApp.m_bRTL ) theApp.m_pfnSetLayout( pMemDC->m_hDC, 0 );
00162
00163 if ( ! CoolInterface.DrawWatermark( pMemDC, &rcClient, &m_bmWatermark ) )
00164 pMemDC->FillSolidRect( &rcClient, CoolInterface.m_crMidtone );
00165 DrawBorders( pMemDC, rcClient );
00166
00167 for ( int nY = rcClient.top + 4 ; nY < rcClient.bottom - 4 ; nY += 2 )
00168 {
00169 pMemDC->Draw3dRect( rcClient.left + 3, nY, 4, 1,
00170 CoolInterface.m_crDisabled, CoolInterface.m_crDisabled );
00171 }
00172
00173 m_pTxItem->SetHistory( rcClient.Width(), TRUE );
00174 m_pRxItem->SetHistory( rcClient.Width(), TRUE );
00175
00176 CRect rcHistory( rcClient.left + 10, rcClient.top + 2, rcClient.right - 15, rcClient.bottom - 6 );
00177 PaintHistory( pMemDC, &rcHistory );
00178
00179 CRect rcCurrent( rcClient.right - 7, rcClient.top + 2, rcClient.right - 2, rcClient.bottom - 6 );
00180 PaintCurrent( pMemDC, &rcCurrent, m_pRxItem );
00181 rcCurrent.OffsetRect( -6, 0 );
00182 PaintCurrent( pMemDC, &rcCurrent, m_pTxItem );
00183
00184 m_rcTrack.SetRect( rcClient.left + 6, rcClient.bottom - 8, rcClient.right, rcClient.bottom - 2 );
00185 PaintTab( pMemDC );
00186
00187 GetClientRect( &rcClient );
00188 pDC->BitBlt( rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(),
00189 pMemDC, 0, 0, SRCCOPY );
00190 if ( theApp.m_bRTL ) theApp.m_pfnSetLayout( pMemDC->m_hDC, LAYOUT_RTL );
00191 }
00192
00194
00195
00196 void CMonitorBarCtrl::PaintHistory(CDC* pDC, CRect* prc)
00197 {
00198 CRect rc( prc );
00199
00200 pDC->Draw3dRect( &rc, GetSysColor( COLOR_3DSHADOW ), GetSysColor( COLOR_3DHIGHLIGHT ) );
00201 rc.DeflateRect( 1, 1 );
00202 pDC->FillSolidRect( &rc, Settings.Live.BandwidthScale > 100 ? RGB( 80, 0, 0 ) : 0 );
00203
00204 if ( m_bTab )
00205 {
00206 CString str;
00207
00208 if ( Settings.Live.BandwidthScale > 100 )
00209 {
00210 str = _T("MAX");
00211 }
00212 else
00213 {
00214 DWORD nRate = max( Settings.Connection.InSpeed, Settings.Connection.OutSpeed );
00215 nRate = nRate * Settings.Live.BandwidthScale / 100;
00216 str.Format( _T("%s (%i%%)"),
00217 (LPCTSTR)Settings.SmartVolume( nRate, TRUE, TRUE ),
00218 Settings.Live.BandwidthScale );
00219 }
00220
00221 CFont* pfOld = (CFont*)pDC->SelectObject( &CoolInterface.m_fntNormal );
00222 pDC->SetBkMode( TRANSPARENT );
00223 pDC->SetTextColor( RGB( 255, 0, 0 ) );
00224 pDC->DrawText( str, &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE );
00225 pDC->SelectObject( pfOld );
00226 return;
00227 }
00228
00229 if ( m_nMaximum == 0 ) return;
00230
00231 DWORD nMax = min( m_pTxItem->m_nLength, (DWORD)rc.Width() );
00232 int nX = rc.right - 1;
00233
00234 for ( DWORD nPos = 0 ; nPos < nMax ; nPos++, nX-- )
00235 {
00236 DWORD nTxValue = m_pTxItem->GetValueAt( nPos );
00237 DWORD nRxValue = m_pRxItem->GetValueAt( nPos );
00238
00239 nTxValue = rc.bottom - nTxValue * rc.Height() / m_nMaximum;
00240 nRxValue = rc.bottom - nRxValue * rc.Height() / m_nMaximum;
00241
00242 if ( nTxValue < nRxValue )
00243 {
00244 if ( nTxValue < (DWORD)rc.bottom )
00245 {
00246 pDC->FillSolidRect( nX, nTxValue + 1, 1, rc.bottom - nTxValue - 1, RGB( 0xBB, 0xBB, 0 ) );
00247 pDC->SetPixel( nX, nTxValue, RGB( 0xFF, 0xFF, 0 ) );
00248 }
00249 if ( nRxValue < (DWORD)rc.bottom )
00250 {
00251 pDC->FillSolidRect( nX, nRxValue + 1, 1, rc.bottom - nRxValue - 1, RGB( 0, 0xBB, 0 ) );
00252 pDC->SetPixel( nX, nRxValue, RGB( 0, 0xFF, 0 ) );
00253 }
00254 }
00255 else
00256 {
00257 if ( nRxValue < (DWORD)rc.bottom )
00258 {
00259 pDC->FillSolidRect( nX, nRxValue + 1, 1, rc.bottom - nRxValue - 1, RGB( 0, 0xBB, 0 ) );
00260 pDC->SetPixel( nX, nRxValue, RGB( 0, 0xFF, 0 ) );
00261 }
00262 if ( nTxValue < (DWORD)rc.bottom )
00263 {
00264 pDC->FillSolidRect( nX, nTxValue + 1, 1, rc.bottom - nTxValue - 1, RGB( 0xBB, 0xBB, 0 ) );
00265 pDC->SetPixel( nX, nTxValue, RGB( 0xFF, 0xFF, 0 ) );
00266 }
00267 }
00268 }
00269 }
00270
00271 void CMonitorBarCtrl::PaintCurrent(CDC* pDC, CRect* prc, CGraphItem* pItem)
00272 {
00273 CRect rc( prc );
00274
00275 pDC->Draw3dRect( &rc, GetSysColor( COLOR_3DSHADOW ), GetSysColor( COLOR_3DHIGHLIGHT ) );
00276 rc.DeflateRect( 1, 1 );
00277 pDC->FillSolidRect( &rc, Settings.Live.BandwidthScale > 100 ? RGB( 80, 0, 0 ) : 0 );
00278
00279 if ( m_nMaximum == 0 || pItem->m_nLength < 1 ) return;
00280
00281 DWORD nValue = (DWORD)pItem->GetValue( pItem->m_nCode );
00282 nValue = nValue * rc.Height() / m_nMaximum;
00283 pDC->FillSolidRect( rc.left, rc.bottom - nValue, rc.Width(), nValue, pItem->m_nColour );
00284 }
00285
00286 void CMonitorBarCtrl::PaintTab(CDC* pDC)
00287 {
00288 float nPosition = 0;
00289
00290 if ( Settings.Live.BandwidthScale > 100 )
00291 nPosition = 1.0f;
00292 else
00293 nPosition = (float)Settings.Live.BandwidthScale / 110.0f;
00294
00295 m_rcTab.left = m_rcTrack.left + (int)( nPosition * ( m_rcTrack.Width() - 16 ) );
00296 m_rcTab.right = m_rcTab.left + 16;
00297 m_rcTab.top = m_rcTrack.top;
00298 m_rcTab.bottom = m_rcTrack.bottom;
00299
00300 DrawIconEx( pDC->GetSafeHdc(), m_rcTab.left, m_rcTab.top, m_hTab, 16, 16, 0, NULL, DI_NORMAL );
00301 }
00302
00304
00305
00306 BOOL CMonitorBarCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
00307 {
00308 CPoint point;
00309 GetCursorPos( &point );
00310 ScreenToClient( &point );
00311
00312 if ( m_rcTrack.PtInRect( point ) )
00313 {
00314 SetCursor( AfxGetApp()->LoadCursor( IDC_HAND ) );
00315 return TRUE;
00316 }
00317 else
00318 {
00319 return CControlBar::OnSetCursor( pWnd, nHitTest, message );
00320 }
00321 }
00322
00323 void CMonitorBarCtrl::OnLButtonDown(UINT nFlags, CPoint point)
00324 {
00325 if ( m_rcTrack.PtInRect( point ) )
00326 {
00327 MSG* pMsg = &AfxGetThreadState()->m_msgCur;
00328 CRect rcTrack( &m_rcTrack );
00329 CPoint point;
00330
00331 ClientToScreen( &rcTrack );
00332 ClipCursor( &rcTrack );
00333 ScreenToClient( &rcTrack );
00334
00335 rcTrack.DeflateRect( m_rcTab.Width() / 2, 0 );
00336
00337 m_bTab = TRUE;
00338 Invalidate();
00339
00340 while ( GetAsyncKeyState( VK_LBUTTON ) & 0x8000 )
00341 {
00342 while ( ::PeekMessage( pMsg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE ) );
00343
00344 if ( ! AfxGetThread()->PumpMessage() )
00345 {
00346 AfxPostQuitMessage( 0 );
00347 break;
00348 }
00349
00350 GetCursorPos( &point );
00351 ScreenToClient( &point );
00352
00353 int nPosition = (DWORD)( 110.0f * (float)( point.x - rcTrack.left ) / (float)rcTrack.Width() );
00354 if ( nPosition < 0 ) nPosition = 0;
00355 else if ( nPosition >= 105 ) nPosition = 101;
00356 else if ( nPosition >= 100 ) nPosition = 100;
00357
00358 if ( nPosition != (int)Settings.Live.BandwidthScale )
00359 {
00360 Settings.Live.BandwidthScale = (DWORD)nPosition;
00361 Invalidate();
00362 }
00363 }
00364
00365 m_bTab = FALSE;
00366 ReleaseCapture();
00367 ClipCursor( NULL );
00368 Invalidate();
00369 }
00370 else
00371 {
00372 CControlBar::OnLButtonDown( nFlags, point );
00373 }
00374 }