LogList.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:8k
源码类别:
P2P编程
开发平台:
Visual C++
- // LogList.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "LogList.h"
- #include "FileDB.h"
- #include "FileBase.h"
- #include "MonitorWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define ID_MENUITEM_CLEAR_ALL 52797
- /////////////////////////////////////////////////////////////////////////////
- // CLogList
- CLogList::CLogList():
- CListCtrlBaseEx("LogList", false)
- {
- //
- // column infos.
- //
- CString strFieldNames[eColumnCount];
- bool bColumnNumeric[eColumnCount];
- strFieldNames[eTime] = "时间";
- strFieldNames[eInfo] = "信息";
- bColumnNumeric[eTime] = false;
- bColumnNumeric[eInfo] = false;
- AddColumn(strFieldNames[0], bColumnNumeric[0]);
- AddColumn(strFieldNames[1], bColumnNumeric[1], 1000);
- m_pFileDBItem = 0;
- m_pMonitorWnd = 0;
- }
- CLogList::~CLogList()
- {
- }
- BEGIN_MESSAGE_MAP(CLogList, CListCtrlBaseEx)
- //{{AFX_MSG_MAP(CLogList)
- ON_WM_CREATE()
- ON_WM_CONTEXTMENU()
- //}}AFX_MSG_MAP
- ON_NOTIFY_REFLECT(LVN_DELETEITEM, OnDeleteitem)
- ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
- ON_COMMAND(ID_EDIT_COPY, OnMenuitemCopy)
- ON_COMMAND(ID_MENUITEM_SELECT_ALL, OnMenuitemSelectAll)
- ON_COMMAND(ID_MENUITEM_CLEAR_ALL, OnMenuitemClearAll)
- ON_COMMAND_RANGE(ID_MENUITEM_MONITOR_INFORMATION, ID_MENUITEM_MONITOR_LOGS, OnMenuitemMonitorSub)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CLogList message handlers
- BOOL CLogList::CreateEx(CWnd *pParent, CRect rc, long lID, long lParam, CMonitorWnd* pMonitorWnd)
- {
- m_pMonitorWnd = pMonitorWnd;
- return Create(WS_CHILD|WS_BORDER|WS_VISIBLE|WS_VSCROLL|LVS_REPORT, rc, pParent, lID);
- }
- int CLogList::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CListCtrlBaseEx::OnCreate(lpCreateStruct) == -1)
- return -1;
- //
- // ImageList.
- //
- HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDB_BITMAP_SYSTEM_MSG), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
- CBitmap bm;
- bm.Attach(hbm);
- m_ctlImage.Create(14, 17, ILC_COLOR8|ILC_MASK, 11, 4);
- m_ctlImage.SetBkColor(::GetSysColor(COLOR_WINDOW));
- m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
- SetImageList(&m_ctlImage, LVSIL_SMALL);
- SetExtendedStyle(LVS_EX_FULLROWSELECT); //|LVS_EX_GRIDLINES);
- return 0;
- }
- void CLogList::OnContextMenu(CWnd* pWnd, CPoint point)
- {
- // if (CListCtrlBaseEx::OnContextMenu(pWnd, point))
- // return;
- CRect rc;
- GetClientRect(&rc);
- ClientToScreen(rc);
- if (!rc.PtInRect(point))
- {
- CWnd::OnContextMenu(pWnd, point);
- return;
- }
- bool bcopy = false;
- if (GetSelectedCount())
- bcopy = true;
- CMenu menu_bar;
- menu_bar.CreatePopupMenu();
- menu_bar.AppendMenu(MF_STRING, ID_EDIT_COPY, "复制(&C)t");
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_CLEAR_ALL, "清除(&R)t");
- menu_bar.AppendMenu(MF_STRING, ID_MENUITEM_SELECT_ALL, "全选(&A)t");
- menu_bar.EnableMenuItem(ID_EDIT_COPY, MF_BYCOMMAND |
- (bcopy ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
- menu_bar.AppendMenu(MF_SEPARATOR);
- 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 CLogList::OnMenuitemMonitorSub(UINT uID)
- {
- if (!m_pMonitorWnd)
- {
- assert(false);
- return;
- }
- if (uID >= ID_MENUITEM_MONITOR_INFORMATION &&
- uID <= ID_MENUITEM_MONITOR_LOGS)
- {
- m_pMonitorWnd->OnShowNode(uID );
- }
- }
- void CLogList::OnMenuitemCopy()
- {
- CString strSource;
- int nColumnCount = GetHeaderCtrl()->GetItemCount();
- POSITION pos = GetFirstSelectedItemPosition();
- while (pos)
- {
- int index = GetNextSelectedItem(pos);
- for (int i=0; i<nColumnCount; i++)
- strSource += GetItemText(index, i) + " ";
- strSource += "rn";
- }
- if (!CopyTextToClipboard(strSource))
- AfxMessageBox("复制失败");
- }
- void CLogList::OnMenuitemClearAll()
- {
- if (m_pFileDBItem)
- {
- if (m_pFileDBItem->m_pDownload)
- {
- m_pFileDBItem->m_pDownload->GetFeedback().ClearSystemMessage();
- }
- else
- {
- m_pFileDBItem->m_vMsg.clear();
- }
- }
- DeleteAllItems();
- }
- void CLogList::OnMenuitemSelectAll()
- {
- for (int i=0; i<GetItemCount(); i++)
- SetItemState(i, LVIS_SELECTED, LVIS_SELECTED);
- SetFocus();
- }
- void CLogList::OnShowColumn(int iItem)
- {
- ShowMsgText(iItem);
- }
- void CLogList::Monitor(CFileDBItem* pFileDBItem)
- {
- m_pFileDBItem = pFileDBItem;
- DeleteAllItems();
- }
- void CLogList::ShowLogs(bool bRefreshInfo)
- {
- if (bRefreshInfo)
- {
- DeleteAllItems();
- }
- if (m_pFileDBItem)
- {
- vector<CSystemMsg> vSystemMsg;
- if (m_pFileDBItem->m_pDownload)
- {
- m_pFileDBItem->m_pDownload->GetSystemMessage(vSystemMsg);
- }
- else
- {
- vSystemMsg = m_pFileDBItem->m_vMsg;
- }
- if (!vSystemMsg.empty())
- {
- time_t tBeg = vSystemMsg[0].m_time;
- while (GetItemCount())
- {
- CSystemMsg* pData = (CSystemMsg*)GetItemData(0);
- if (!pData)
- {
- assert(false);
- break;
- }
- if (pData->m_time < tBeg)
- DeleteItem(0);
- else
- break;
- }
- }
- int iStart = 0;
- if (!bRefreshInfo)
- {
- iStart = GetItemCount();
- if (vSystemMsg.size() < iStart)
- {
- iStart = 0;
- DeleteAllItems();
- }
- }
- for (int i=iStart; i<vSystemMsg.size(); i++)
- {
- InsertItem(i, "");//vSystemMsg[i].m_strMsg.data());
- CSystemMsg* pData = new CSystemMsg(vSystemMsg[i]);
- SetItemData(i, (long)pData);
- ShowMsgText(i);
- }
- }
- }
- void CLogList::OnDeleteitem(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- CSystemMsg* pData = (CSystemMsg*)pNMListView->lParam;;
- if (!pData)
- {
- assert(false);
- return;
- }
- delete pData;
- }
- void CLogList::ShowMsgText(int iItem)
- {
- CSystemMsg* pData = (CSystemMsg*)GetItemData(iItem);
- if (!pData)
- {
- assert(false);
- return;
- }
- CSystemMsg& msg = *pData;
- UINT iImageID = 0;
- switch (msg.m_eMsgType)
- {
- case CSystemMsg::eMsgOut:
- iImageID = eMsgOutID;
- break;
- case CSystemMsg::eMsgIn:
- iImageID = eMsgInID;
- break;
- case CSystemMsg::eCmd:
- iImageID = eCmdID;
- break;
- case CSystemMsg::eBad:
- iImageID = eBadID;
- break;
- default:
- assert(false);
- return;
- }
- SetItem(iItem, 0, LVIF_IMAGE, 0, iImageID, 0, 0, 0);
- SetItemTextEx(iItem, eTime, CTime(msg.m_time).Format("%Y-%m-%d %H:%M:%S"));
- SetItemTextEx(iItem, eInfo, msg.m_strMsg.data());
- }
- void CLogList::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
- switch(lplvcd->nmcd.dwDrawStage)
- {
- case CDDS_PREPAINT:
- *pResult = CDRF_NOTIFYSUBITEMDRAW; // ask for subitem notifications.
- break;
- case CDDS_ITEMPREPAINT:
- {
- *pResult = CDRF_DODEFAULT;
- if (!(lplvcd->nmcd.uItemState & CDIS_SELECTED ))
- {
- CSystemMsg* pData = (CSystemMsg*)lplvcd->nmcd.lItemlParam;
- CSystemMsg& msg = *pData;
- switch (msg.m_eMsgType)
- {
- case CSystemMsg::eMsgOut:
- lplvcd->clrTextBk = RGB(235, 242, 254);
- break;
- case CSystemMsg::eMsgIn:
- // lplvcd->clrTextBk = RGB(235, 242, 254);
- lplvcd->clrTextBk = RGB(255, 250, 237);
- break;
- case CSystemMsg::eCmd:
- lplvcd->clrTextBk = RGB(235, 242, 254); // //RGB(230, 230, 250); // 175, 238, 238 //
- break;
- case CSystemMsg::eBad:
- lplvcd->clrTextBk = RGB(254, 238, 231);
- break;
- default:
- assert(false);
- return;
- }
- *pResult = CDRF_NEWFONT;
- }
- //*pResult = CDRF_NOTIFYSUBITEMDRAW; // ask for subitem notifications.
- }
- break;
- case CDDS_ITEMPREPAINT|CDDS_SUBITEM: // recd when CDRF_NOTIFYSUBITEMDRAW is returned in
- { // response to CDDS_ITEMPREPAINT.
- *pResult = CDRF_DODEFAULT;
- int iCol = lplvcd->iSubItem;
- int iRow = lplvcd->nmcd.dwItemSpec;
- break;
- }
- default:// it wasn't a notification that was interesting to us.
- *pResult = CDRF_DODEFAULT;
- }
- }