GraphWnd.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:11k
源码类别:
P2P编程
开发平台:
Visual C++
- // GraphWnd.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "GraphWnd.h"
- #include "download.h"
- #include "FileDB.h"
- #include "MonitorWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define SB_LINEDOWN_WHEEL 10
- #define SB_LINEUP_WHEEL 11
- /////////////////////////////////////////////////////////////////////////////
- // CGraphWnd
- CGraphWnd::CGraphWnd() : m_size(11, 11)
- {
- ASSERT(m_size.cx && m_size.cy);
- m_clrBackground = RGB(212, 208, 200); //GetSysColor(COLOR_3DFACE);
- m_lGridCountV = 0;
- m_lGridCountH = 0;
- m_lLineCount = 0;
- m_lBeginInx = 0;
- m_pFileDBItem = 0;
- m_pMonitorWnd = 0;
- }
- CGraphWnd::~CGraphWnd()
- {
- }
- BEGIN_MESSAGE_MAP(CGraphWnd, CWnd)
- //{{AFX_MSG_MAP(CGraphWnd)
- ON_WM_PAINT()
- ON_WM_SIZE()
- ON_WM_CREATE()
- ON_WM_VSCROLL()
- ON_WM_MOUSEWHEEL()
- ON_WM_LBUTTONDOWN()
- ON_WM_RBUTTONDOWN()
- ON_WM_KEYDOWN()
- ON_WM_CHAR()
- ON_WM_ERASEBKGND()
- ON_WM_CONTEXTMENU()
- //}}AFX_MSG_MAP
- ON_COMMAND_RANGE(ID_MENUITEM_MONITOR_INFORMATION, ID_MENUITEM_MONITOR_LOGS, OnMenuitemMonitorSub)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CGraphWnd message handlers
- void CGraphWnd::Monitor(CFileDBItem *pFileDBItem)
- {
- m_pFileDBItem = pFileDBItem;
- // SetContent(vector<long>());
- }
- void CGraphWnd::ShowGraph(bool bRefreshInfo)
- {
- vector<long> vItems;
- if (m_pFileDBItem )
- {
- if (m_pFileDBItem->m_pDownload && m_pFileDBItem->m_pDownload->IsDownloading() &&
- !m_pFileDBItem->m_pDownload->GetFeedback().IsStorageChecking())
- {
- m_pFileDBItem->m_pDownload->GetFractions(vItems);
- }
- else
- {
- m_pFileDBItem->GetHave(vItems);
- if (!vItems.empty())
- {
- long lCount = vItems.size();
- assert(m_pFileDBItem->m_lPieceCount == vItems.size());
- }
- else
- {
- for (int i=0; i<m_pFileDBItem->m_lPieceCount; i++)
- vItems.push_back(0);
- }
- }
- }
- if (bRefreshInfo || vItems.size() != m_vItems.size())
- {
- SetContent(vItems);
- return;
- }
- //
- // Refresh visible and changed items.
- //
- m_vItems = vItems;
- Refresh();
- }
- void CGraphWnd::SetContent(vector<long> vItems)
- {
- m_vItems = vItems;
- m_lBeginInx = 0;
- if (GetSafeHwnd())
- {
- CRect rc;
- GetClientRect(rc);
- OnSizeChanged(rc.Width(), rc.Height());
- DrawItem();
- }
- }
- BOOL CGraphWnd::CreateEx(CWnd *pParent, CRect rc, long lID, long lParam, CMonitorWnd* pMonitorWnd)
- {
- m_pMonitorWnd = pMonitorWnd;
- return Create(0, 0, WS_CHILD|WS_BORDER|WS_VISIBLE|WS_VSCROLL, rc, pParent, lID);
- }
- int CGraphWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- BOOL bRet = m_bmp.LoadBitmap(IDB_BITMAP_GRAPH);
- ASSERT(bRet);
- SetScrollRange(SB_VERT, 0, 100);
- return 0;
- }
- void CGraphWnd::OnSize(UINT nType, int cx, int cy)
- {
- // strange onsize, cx and cy be lager than 63555.
- if (cy > 10000 || cx > 10000)
- return;
- OnSizeChanged(cx, cy);
- CWnd::OnSize(nType, cx, cy);
- }
- void CGraphWnd::OnSizeChanged(int cx, int cy)
- {
- long lPosOld = m_lBeginInx * m_lGridCountH;
- m_lGridCountH = cx/m_size.cx;
- m_lGridCountV = cy/m_size.cy;
- m_vGrids.clear();
- if (m_lGridCountH && m_lGridCountV)
- {
- // reset grid value.
- m_lBeginInx = lPosOld / m_lGridCountH;
- for (int i=0; i<m_lGridCountV*m_lGridCountH; i++)
- {
- long lVal = -1;
- long lInx = m_lBeginInx * m_lGridCountH + i;
- if (lInx < m_vItems.size())
- lVal = m_vItems[lInx];
- m_vGrids.push_back(lVal);
- }
- // set scroll info.
- m_lLineCount = m_vItems.size() / m_lGridCountH;
- if (m_vItems.size()% m_lGridCountH)
- m_lLineCount ++;
- int nScrollMax = 0;
- if (m_lGridCountV < m_lLineCount)
- nScrollMax = m_lLineCount - m_lGridCountV;
- SCROLLINFO si;
- si.cbSize = sizeof(SCROLLINFO);
- si.fMask = SIF_ALL; // SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS;
- si.nMin = 0;
- si.nMax = m_lLineCount - 1; // nScrollMax;
- si.nPage = m_lGridCountV ;//nScrollMax/10;
- si.nPos = m_lBeginInx;
- si.nTrackPos = 0;
- SetScrollInfo(SB_VERT, &si, FALSE);
- }
- else
- {
- SCROLLINFO si;
- si.cbSize = sizeof(SCROLLINFO);
- si.fMask = SIF_ALL; // SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS;
- si.nMin = 0;
- si.nMax = 0;
- si.nPage = 0;
- si.nPos = 0;
- si.nTrackPos = 0;
- SetScrollInfo(SB_VERT, &si, FALSE);
- }
- }
- void CGraphWnd::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- DrawItem(&dc);
- }
- void CGraphWnd::DrawItem(CDC *pDC)
- {
- CClientDC dc(this);
- if (!pDC) pDC = &dc;
- CRect rc;
- GetWindowRect( rc );
- rc.OffsetRect(-rc.left, -rc.top);
- CMemDC memdc(pDC, &rc);
- memdc.SetBkMode(TRANSPARENT);
- memdc.FillSolidRect( rc, m_clrBackground);
- long lVisibleCount = m_lGridCountV * m_lGridCountH;
- for (int i=0; i<lVisibleCount; i++)
- {
- DrawSubItem(memdc, i);
- }
- }
- void CGraphWnd::Refresh()
- {
- CClientDC dc(this);
- long lInx = m_lBeginInx * m_lGridCountH;
- long lVisibleCount = m_lGridCountV * m_lGridCountH;
- for (int i=0; i<lVisibleCount; i++)
- {
- long lVal = -1;
- if ((lInx + i) < m_vItems.size())
- lVal = m_vItems[lInx +i];
- SetSubItem(i, lVal, &dc);
- }
- }
- void CGraphWnd::SetSubItem(long lSubitemInx, long lVal, CDC *pDC)
- {
- if (m_vGrids[lSubitemInx] == lVal)
- return;
- m_vGrids[lSubitemInx] = lVal;
- DrawSubItem(pDC, lSubitemInx);
- }
- void CGraphWnd::DrawSubItem(CDC *pDC, long lSubitemInx)
- {
- assert(lSubitemInx <m_vGrids.size());
- if (!m_lGridCountV)
- {
- ASSERT(FALSE);
- return;
- }
- long x = m_size.cx * ((lSubitemInx) % m_lGridCountH);
- long y = m_size.cy * ((lSubitemInx) / m_lGridCountH);
- long cx = 9; // m_size.cx - 1;
- long cy = 9; // m_size.cy - 1;
- if (m_vGrids[lSubitemInx] == -1)
- {
- CRect rc(x, y, x + m_size.cx, y +m_size.cy);
- pDC->FillRect(rc, &CBrush(m_clrBackground));
- }
- else
- {
- CDC MemDC;
- MemDC.CreateCompatibleDC(NULL);
- MemDC.SelectObject(&m_bmp);
- COLORREF col = RGB(0, 0, 255);
- long lOffset = 0;
- if (m_vGrids[lSubitemInx] == 2)
- {
- lOffset = cx;
- col = RGB(255, 0, 0);
- }
- else if (m_vGrids[lSubitemInx] == 0)
- {
- lOffset = cx * 2;
- col = RGB(180, 180, 180);
- }
- pDC->BitBlt(x, y, cx, cy, &MemDC, lOffset, 0, SRCCOPY);
- // pDC->Draw3dRect(x, y,cx, cy, col, col);
- /*
- CString str;
- str.Format("%d", lInx);
- pDC->DrawText(str, CRect(x, y, x + m_size.cx, y + m_size.cy), DT_LEFT);
- //*/
- }
- }
- void CGraphWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- if (m_lGridCountV <= 0)
- {
- assert(false);
- return;
- }
- //*
- int nDelta;
- int nMaxPos = m_lLineCount - m_lGridCountV;
- switch (nSBCode)
- {
- case SB_LINEDOWN_WHEEL :
- {
- if (m_lBeginInx >= nMaxPos)
- return;
- if ((nMaxPos - m_lBeginInx) < 0)
- {
- assert(false);
- return;
- }
- nDelta = min(3, nMaxPos - m_lBeginInx); //% m_lGridCountV);
- }
- break;
- case SB_LINEUP_WHEEL:
- if (m_lBeginInx <= 0)
- return;
- nDelta = -min(3, m_lBeginInx);
- break;
- case SB_LINEDOWN:
- if (m_lBeginInx >= nMaxPos)
- return;
- nDelta = min(1, nMaxPos);
- break;
- case SB_LINEUP:
- if (m_lBeginInx <= 0)
- return;
- nDelta = -min(1, m_lBeginInx);
- break;
- case SB_PAGEDOWN:
- {
- if (m_lBeginInx>= nMaxPos) return;
- if ((nMaxPos - m_lBeginInx) < 0)
- {
- assert(false);
- return;
- }
- long lPage = m_lGridCountV;
- if (lPage <= 0) lPage = 1;
- nDelta = min(lPage, nMaxPos - m_lBeginInx);
- // nDelta = min(lPage, m_lGridCountV);
- }
- break;
- case SB_PAGEUP:
- {
- if (m_lBeginInx <= 0) return;
- long lPage = m_lGridCountV;
- if (lPage <= 0) lPage = 1;
- nDelta = -min(lPage, m_lBeginInx);
- }
- break;
- case SB_THUMBPOSITION:
- {
- nDelta = (int)nPos - m_lBeginInx;
- }
- break;
- case SB_THUMBTRACK:
- {
- nDelta = (int)nPos - m_lBeginInx;
- }
- break;
- default:
- return;
- }
- m_lBeginInx += nDelta;
- if (m_lBeginInx < 0 ||
- (m_lBeginInx >= m_lLineCount && m_lLineCount != 0))
- {
- assert(false);
- m_lBeginInx = 0;
- }
- SetScrollPos(SB_VERT, m_lBeginInx, TRUE);
- // DrawItem();
- Refresh();
- CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
- }
- BOOL CGraphWnd::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
- {
- OnVScroll(zDelta > 0 ? SB_LINEUP_WHEEL : SB_LINEDOWN_WHEEL , 0, 0);
- return CWnd::OnMouseWheel(nFlags, zDelta, pt);
- }
- void CGraphWnd::OnLButtonDown(UINT nFlags, CPoint point)
- {
- SetFocus();
- CWnd::OnLButtonDown(nFlags, point);
- }
- void CGraphWnd::OnRButtonDown(UINT nFlags, CPoint point)
- {
- SetFocus();
- CWnd::OnRButtonDown(nFlags, point);
- }
- void CGraphWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- switch (nChar)
- {
- case 0x21:
- {
- OnVScroll(SB_PAGEUP, 0, 0);
- }
- break;
- case 0x22:
- {
- OnVScroll(SB_PAGEDOWN, 0, 0);
- }
- break;
- }
- CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CGraphWnd::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- CWnd::OnChar(nChar, nRepCnt, nFlags);
- }
- BOOL CGraphWnd::PreTranslateMessage(MSG* pMsg)
- {
- if (pMsg->message == WM_KEYDOWN)
- {
- switch (pMsg->wParam)
- {
- case VK_UP:
- {
- OnVScroll(SB_LINEUP, 0, 0);
- return true;
- }
- break;
- case VK_DOWN:
- {
- OnVScroll(SB_LINEDOWN , 0, 0);
- return true;
- }
- case VK_HOME:
- {
- OnVScroll(SB_THUMBPOSITION, 0, 0);
- return true;
- }
- case VK_END:
- {
- if (m_lLineCount <= m_lGridCountV)
- return true;
- OnVScroll(SB_THUMBPOSITION, m_lLineCount - m_lGridCountV, 0);
- return true;
- }
- break;
- }
- }
- return CWnd::PreTranslateMessage(pMsg);
- }
- BOOL CGraphWnd::OnEraseBkgnd(CDC* pDC)
- {
- /*
- CBrush backBrush(RGB(250, 250, 250));
- CBrush* pOldBrush = pDC->SelectObject(&backBrush);
- CRect rect;
- pDC->GetClipBox(rect);
- pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),
- PATCOPY);
- pDC->SelectObject(pOldBrush);
- return TRUE;
- //*/
- return CWnd::OnEraseBkgnd(pDC);
- }
- void CGraphWnd::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- CRect rc;
- GetClientRect(&rc);
- ClientToScreen(rc);
- if (!rc.PtInRect(point))
- {
- CWnd::OnContextMenu(pWnd, point);
- return;
- }
- CMenu menu_bar;
- menu_bar.CreatePopupMenu();
- if (!m_pMonitorWnd)
- {
- assert(false);
- return;
- }
- m_pMonitorWnd->OnContextQuery(menu_bar);
- CPoint posMouse;
- GetCursorPos(&posMouse);
- menu_bar.TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,posMouse.x,posMouse.y,this);
- }
- void CGraphWnd::OnMenuitemMonitorSub(UINT uID)
- {
- if (!m_pMonitorWnd)
- {
- assert(false);
- return;
- }
- if (uID >= ID_MENUITEM_MONITOR_INFORMATION &&
- uID <= ID_MENUITEM_MONITOR_LOGS)
- {
- m_pMonitorWnd->OnShowNode(uID );
- }
- }