#include "stdafx.h"
|
#include "LogEdit.h"
|
|
|
#define MENU_ITEM_SEL_ALL 0x666
|
#define MENU_ITEM_COPY 0x667
|
#define MENU_ITEM_CLEAR 0x668
|
|
CLogEdit::CLogEdit()
|
{
|
m_nMaxLines = 0xffff;
|
m_nTrimLines = 100;
|
m_bAutoScroll = TRUE;
|
}
|
|
|
CLogEdit::~CLogEdit()
|
{
|
}
|
|
BEGIN_MESSAGE_MAP(CLogEdit, CEdit)
|
ON_WM_CONTEXTMENU()
|
ON_WM_VSCROLL()
|
ON_WM_MOUSEWHEEL()
|
END_MESSAGE_MAP()
|
|
void CLogEdit::SetMaxLineCount(int line)
|
{
|
m_nMaxLines = line;
|
m_nTrimLines = min(m_nMaxLines, 4000);
|
}
|
|
void CLogEdit::OnContextMenu(CWnd* pWnd, CPoint point)
|
{
|
HMENU hMenu = CreatePopupMenu();
|
InsertMenu(hMenu, 0, MF_BYPOSITION, MENU_ITEM_SEL_ALL, "ȫѡ");
|
InsertMenu(hMenu, 1, MF_BYPOSITION, MENU_ITEM_COPY, "¸´ÖÆ");
|
InsertMenu(hMenu, 2, MF_BYPOSITION | MF_SEPARATOR, NULL, NULL);
|
InsertMenu(hMenu, 3, MF_BYPOSITION, MENU_ITEM_CLEAR, "È«²¿Çå³ý");
|
int cmd = ::TrackPopupMenu(hMenu,
|
TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD,
|
point.x, point.y + 2, 0, m_hWnd, NULL);
|
DestroyMenu(hMenu);
|
|
if (cmd == MENU_ITEM_SEL_ALL) {
|
SetFocus();
|
this->SetSel(0, -1);
|
}
|
else if (cmd == MENU_ITEM_COPY) {
|
this->Copy();
|
}
|
else if (cmd == MENU_ITEM_CLEAR) {
|
SetWindowText(_T(""));
|
}
|
}
|
|
void CLogEdit::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
|
{
|
// ÿ´Î¹ö¶¯Ê±¼ì²éÊÇ·ñ»¹Ôڵײ¿
|
m_bAutoScroll = IsScrollBarAtBottom();
|
CEdit::OnVScroll(nSBCode, nPos, pScrollBar);
|
}
|
|
BOOL CLogEdit::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
|
{
|
// ÿ´Î¹ö¶¯Ê±¼ì²éÊÇ·ñ»¹Ôڵײ¿
|
m_bAutoScroll = IsScrollBarAtBottom();
|
return CEdit::OnMouseWheel(nFlags, zDelta, pt);
|
}
|
|
BOOL CLogEdit::IsScrollBarAtBottom()
|
{
|
SCROLLINFO si = { sizeof(si), SIF_ALL };
|
GetScrollInfo(SB_VERT, &si);
|
return (si.nPos + (int)si.nPage >= si.nMax);
|
}
|
|
void CLogEdit::AppendText(const char* pszText)
|
{
|
SetRedraw(FALSE);
|
|
// ¼ôÇйý¶àÐÐ
|
int totalLines = GetLineCount();
|
if (totalLines > m_nMaxLines) {
|
int startChar = LineIndex(0);
|
int endChar = LineIndex(m_nTrimLines);
|
if (startChar >= 0 && endChar > startChar) {
|
SetSel(startChar, endChar);
|
ReplaceSel(_T(""));
|
}
|
}
|
|
// ±£´æµ±Ç°Ñ¡Ôñ
|
int start, end;
|
GetSel(start, end);
|
bool hasSelection = (start != end);
|
|
int endPos = GetWindowTextLength();
|
SetSel(endPos, endPos);
|
ReplaceSel(lpszText);
|
|
if (m_bAutoScroll && !hasSelection) {
|
LineScroll(GetLineCount());
|
}
|
|
// »Ö¸´Ñ¡Ôñ
|
if (hasSelection) {
|
SetSel(start, end);
|
}
|
|
SetRedraw(TRUE);
|
|
if (m_bAutoScroll && !hasSelection) {
|
Invalidate();
|
UpdateWindow();
|
}
|
}
|