// LogDlg.cpp : ʵÏÖÎļþ // #include "stdafx.h" #include "Servo.h" #include "PageLog.h" #include "afxdialogex.h" #include "Common.h" #include // CPageLog ¶Ô»°¿ò IMPLEMENT_DYNAMIC(CPageLog, CDialogEx) CPageLog::CPageLog(CWnd* pParent /*=NULL*/) : CDialogEx(IDD_DIALOG_LOG, pParent) { m_crBkgnd = LOGDLG_BACKGROUND_COLOR; m_hbrBkgnd = nullptr; m_pObserver = nullptr; m_nLevel = 0; m_strFilterText = _T(""); m_bRegex = FALSE; m_filterMode = FilterMode::Include; } CPageLog::~CPageLog() { } void CPageLog::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_BUTTON_LEVEL, m_btnLevel); DDX_Control(pDX, IDC_BUTTON_INCLUDE, m_btnInclude); DDX_Control(pDX, IDC_EDIT_LOG, m_logEdit); } BEGIN_MESSAGE_MAP(CPageLog, CDialogEx) ON_WM_CTLCOLOR() ON_WM_SIZE() ON_WM_DESTROY() ON_WM_CLOSE() ON_NOTIFY(BLBUTTON_MENU_ITEM_CLICKED, IDC_BUTTON_LEVEL, &CPageLog::OnButtonLevelMenuClicked) ON_NOTIFY(BLBUTTON_MENU_ITEM_CLICKED, IDC_BUTTON_INCLUDE, &CPageLog::OnButtonIncludeMenuClicked) ON_EN_CHANGE(IDC_EDIT_INCLUDE, &CPageLog::OnEnChangeEditInclude) ON_BN_CLICKED(IDC_CHECK_REGEX, &CPageLog::OnBnClickedCheckRegex) END_MESSAGE_MAP() // CLogDlg ÏûÏ¢´¦Àí³ÌÐò void CPageLog::InitRxWindows() { /* code */ // ¶©ÔÄÊý¾Ý IRxWindows* pRxWindows = RX_GetRxWindows(); pRxWindows->enableLog(5); if (m_pObserver == NULL) { m_pObserver = pRxWindows->allocObserver([&](IAny* pAny) -> void { // onNext pAny->addRef(); int code = pAny->getCode(); if (RX_CODE_LOG == code && ::IsWindow(m_hWnd)) { const char* pszLogMsg; int level; if (pAny->getStringValue("text", pszLogMsg) && pAny->getIntValue("exCode", level)) { if (level >= m_nLevel) { CString strText = pszLogMsg; BOOL bMatch = TRUE; if (!m_strFilterText.IsEmpty()) { if (!m_bRegex) { bMatch = (strText.Find(m_strFilterText) >= 0); } else { CString strTemp = strText; strTemp.TrimRight(); try { bMatch = std::regex_match((LPTSTR)(LPCTSTR)strTemp, std::regex((LPTSTR)(LPCTSTR)m_strFilterText)); } catch (const std::regex_error& e) { } } if (m_filterMode == FilterMode::Exclude) { bMatch = !bMatch; } } if (bMatch) { strText.Replace("\n", "\r\n"); AppendLog(level, (LPTSTR)(LPCTSTR)strText); } } } } pAny->release(); }, [&]() -> void { // onComplete }, [&](IThrowable* pThrowable) -> void { // onErrorm pThrowable->printf(); }); theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread()) ->subscribe(m_pObserver); } } BOOL CPageLog::OnInitDialog() { CDialogEx::OnInitDialog(); InitRxWindows(); // »º´æ m_nLevel = theApp.m_model.m_configuration.getLogcatLevel(); theApp.m_model.m_configuration.getLogcatIncludeText(m_strFilterText); m_bRegex = theApp.m_model.m_configuration.isLogcatIncludeRegex(); theApp.m_model.m_configuration.getCustomLogcatIncludeTexts(m_customIncludeTexts); m_customIncludeTexts.clear(); m_customIncludeTexts.push_back("°üº¬"); m_customIncludeTexts.push_back("Åųý"); // Level HMENU hMenu = LoadMenu(AfxGetInstanceHandle(), MAKEINTRESOURCEA(IDR_MENU_LOGCAT_LEVEL)); m_btnLevel.SetMenu(hMenu); m_btnLevel.SetCurrentMenuItem(m_nLevel); m_btnLevel.SetBkgndColor(BS_NORMAL, RGB(241, 242, 244)); m_btnLevel.SetBkgndColor(BS_HOVER, RGB(220, 223, 228)); m_btnLevel.SetBkgndColor(BS_PRESS, RGB(179, 185, 196)); m_btnLevel.SetFrameColor(BS_NORMAL, RGB(133, 144, 162)); m_btnLevel.SetFrameColor(BS_HOVER, RGB(56, 139, 255)); m_btnLevel.SetFrameColor(BS_PRESS, RGB(56, 139, 255)); m_btnLevel.SetFrameColor(BS_FOCUS, RGB(56, 139, 255)); // °üº¬×Ö·û´® CString strIcon1; strIcon1.Format(_T("%s\\Res\\logcat_include.ico"), theApp.m_strAppDir); HICON hIcon = (HICON)::LoadImage(AfxGetInstanceHandle(), strIcon1, IMAGE_ICON, 24, 24, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); m_btnInclude.SetIcon(hIcon, hIcon, 24); m_filterMode = (FilterMode)theApp.m_model.m_configuration.getFilterMode(); m_btnInclude.SetTextRight(); m_btnInclude.SetWindowText(m_filterMode == FilterMode::Include ? "°üº¬" : "Åųý"); { HMENU hMenu = LoadMenu(AfxGetInstanceHandle(), MAKEINTRESOURCEA(IDR_MENU_INCLUDE)); HMENU hSubMenu = GetSubMenu(hMenu, 0); DeleteMenu(hSubMenu, 0, MF_BYPOSITION); int i = 0; for (auto& item : m_customIncludeTexts) { i++; InsertMenu(hSubMenu, i, MF_BYPOSITION, 0x1998 + i, item.c_str()); m_btnInclude.SetMenu(hMenu); } } SetDlgItemText(IDC_EDIT_INCLUDE, m_strFilterText); CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_REGEX); pCheckBox->SetCheck(m_bRegex ? BST_CHECKED : BST_UNCHECKED); // ÄÚÈÝ m_logEdit.SetMaxLineCount(6000); m_logEdit.SetLimitText(-1); Resize(); return TRUE; // return TRUE unless you set the focus to a control // Òì³£: OCX ÊôÐÔÒ³Ó¦·µ»Ø FALSE } void CPageLog::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); if (GetDlgItem(IDC_EDIT_LOG) == nullptr) return; Resize(); } void CPageLog::Resize() { int x, y, y2, temp; CRect rcClient, rcItem; CWnd* pItem; GetClientRect(&rcClient); y = 12; x = 8; pItem = GetDlgItem(IDC_BUTTON_LEVEL); pItem->GetWindowRect(&rcItem); ScreenToClient(&rcItem); pItem->MoveWindow(x, y, rcItem.Width(), rcItem.Height()); x += rcItem.Width(); x += 18; y2 = rcItem.bottom; y2 += 8; pItem = GetDlgItem(IDC_BUTTON_INCLUDE); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x, y, rcItem.Width(), rcItem.Height()); x += rcItem.Width(); x += 2; pItem = GetDlgItem(IDC_EDIT_INCLUDE); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x, y, rcItem.Width(), rcItem.Height()); x += rcItem.Width(); x += 12; temp = rcItem.Height(); pItem = GetDlgItem(IDC_CHECK_REGEX); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x, y + (temp - rcItem.Height()) / 2, rcItem.Width(), rcItem.Height()); x = 8; pItem = GetDlgItem(IDC_EDIT_LOG); pItem->MoveWindow(x, y2, rcClient.Width() - 16, rcClient.Height() - 5 - y2); } void CPageLog::OnDestroy() { CDialogEx::OnDestroy(); { HMENU hMenu = m_btnLevel.GetMenu(); if (hMenu != NULL) { ::DestroyMenu(hMenu); } } { HMENU hMenu = m_btnInclude.GetMenu(); if (hMenu != NULL) { ::DestroyMenu(hMenu); } } if (m_hbrBkgnd != nullptr) { ::DeleteObject(m_hbrBkgnd); } if (m_pObserver != NULL) { m_pObserver->unsubscribe(); m_pObserver = NULL; } } HBRUSH CPageLog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor); if (nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkColor(m_crBkgnd); } if (m_hbrBkgnd == nullptr) { m_hbrBkgnd = CreateSolidBrush(m_crBkgnd); } return m_hbrBkgnd; } void CPageLog::OnClose() { ShowWindow(SW_HIDE); GetParent()->PostMessage(ID_MSG_LOGDLG_HIDE, 0, 0); } BOOL CPageLog::PreTranslateMessage(MSG* pMsg) { if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE) { return TRUE; } return CDialogEx::PreTranslateMessage(pMsg); } void CPageLog::AppendLog(int level, const char* pszText) { if (!::IsWindow(m_logEdit.m_hWnd)) { return; } m_logEdit.AppendText(pszText); } void CPageLog::OnButtonLevelMenuClicked(NMHDR* pNMHDR, LRESULT* pResult) { BLBUTTON_NMHDR* pblbNmhdr = reinterpret_cast(pNMHDR); m_nLevel = (int)pblbNmhdr->dwData; theApp.m_model.m_configuration.setLogcatLevel(m_nLevel); m_btnLevel.SetCurrentMenuItem(m_nLevel); *pResult = 0; } void CPageLog::OnButtonIncludeMenuClicked(NMHDR* pNMHDR, LRESULT* pResult) { BLBUTTON_NMHDR* pblbNmhdr = reinterpret_cast(pNMHDR); m_filterMode = (FilterMode)pblbNmhdr->dwData; theApp.m_model.m_configuration.setFilterMode((int)m_filterMode); m_btnInclude.SetWindowText(m_filterMode == FilterMode::Include ? "°üº¬" : "Åųý"); *pResult = 0; } void CPageLog::OnEnChangeEditInclude() { GetDlgItemText(IDC_EDIT_INCLUDE, m_strFilterText); theApp.m_model.m_configuration.setLogcatIncludeText(m_strFilterText); } void CPageLog::OnBnClickedCheckRegex() { CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_REGEX); m_bRegex = pCheckBox->GetCheck(); theApp.m_model.m_configuration.setLogcatIncludeRegex(m_bRegex); }