// CPageLogcat.cpp: 实现文件 // #include "stdafx.h" #include "BondEq.h" #include "Common.h" #include "CPageLogcat.h" #include "afxdialogex.h" #include // CPageLogcat 对话框 IMPLEMENT_DYNAMIC(CPageLogcat, CDialogEx) CPageLogcat::CPageLogcat(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_PAGE_LOGCAT, pParent) { m_crBkgnd = PAGE_BACKGROUND_COLOR; m_hbrBkgnd = nullptr; m_pObserver = nullptr; m_nLevel = 0; m_strIncludeText = _T(""); m_bIncludeRegex = FALSE; } CPageLogcat::~CPageLogcat() { } void CPageLogcat::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(CPageLogcat, CDialogEx) ON_WM_CTLCOLOR() ON_WM_DESTROY() ON_WM_SIZE() ON_NOTIFY(BLBUTTON_MENU_ITEM_CLICKED, IDC_BUTTON_LEVEL, &CPageLogcat::OnButtonLevelMenuClicked) ON_NOTIFY(BLBUTTON_MENU_ITEM_CLICKED, IDC_BUTTON_INCLUDE, &CPageLogcat::OnButtonIncludeMenuClicked) ON_EN_CHANGE(IDC_EDIT_INCLUDE, &CPageLogcat::OnEnChangeEditInclude) ON_BN_CLICKED(IDC_CHECK_REGEX, &CPageLogcat::OnBnClickedCheckRegex) END_MESSAGE_MAP() // CPageLogcat 消息处理程序 void CPageLogcat::InitRxWindow() { /* 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 bInclude = TRUE; if (!m_strIncludeText.IsEmpty()) { if (!m_bIncludeRegex) { bInclude = (strText.Find(m_strIncludeText) >= 0); } else { bInclude = std::regex_search((LPTSTR)(LPCTSTR)strText, std::regex((LPTSTR)(LPCTSTR)m_strIncludeText)); } } if (bInclude) { 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 CPageLogcat::OnInitDialog() { CDialogEx::OnInitDialog(); // 缓存 m_nLevel = theApp.m_model.m_configuration.getLogcatLevel(); theApp.m_model.m_configuration.getLogcatIncludeText(m_strIncludeText); m_bIncludeRegex = theApp.m_model.m_configuration.isLogcatIncludeRegex(); theApp.m_model.m_configuration.getCustomLogcatIncludeTexts(m_customIncludeTexts); // 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); { 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, 0, MF_BYPOSITION, 0x1998+i, item.c_str()); m_btnInclude.SetMenu(hMenu); } } SetDlgItemText(IDC_EDIT_INCLUDE, m_strIncludeText); CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_REGEX); pCheckBox->SetCheck(m_bIncludeRegex ? BST_CHECKED : BST_UNCHECKED); // 内容 m_logEdit.SetMaxLineCount(20); m_logEdit.SetLimitText(-1); InitRxWindow(); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } HBRUSH CPageLogcat::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 CPageLogcat::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); } ASSERT(m_pObserver != NULL); m_pObserver->unsubscribe(); m_pObserver = NULL; } void CPageLogcat::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); if (GetDlgItem(IDC_EDIT_LOG) == nullptr) return; int x, y, temp; CRect rcClient, rcItem; CWnd* pItem; GetClientRect(&rcClient); x = 12; pItem = GetDlgItem(IDC_BUTTON_LEVEL); pItem->GetWindowRect(&rcItem); ScreenToClient(&rcItem); pItem->MoveWindow(x, 12, rcItem.Width(), rcItem.Height()); x += rcItem.Width(); x += 18; y = rcItem.bottom; y += 12; pItem = GetDlgItem(IDC_BUTTON_INCLUDE); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x, 12, rcItem.Width(), rcItem.Height()); x += rcItem.Width(); x += 2; pItem = GetDlgItem(IDC_EDIT_INCLUDE); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x, 12, rcItem.Width(), rcItem.Height()); x += rcItem.Width(); x += 12; temp = rcItem.Height(); pItem = GetDlgItem(IDC_CHECK_REGEX); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x, 12 + (temp - rcItem.Height()) / 2, rcItem.Width(), rcItem.Height()); pItem = GetDlgItem(IDC_EDIT_LOG); pItem->MoveWindow(12, y, rcClient.Width() - 24, rcClient.Height() - 12 - y); } void CPageLogcat::AppendLog(int level, const char* pszText) { if (!::IsWindow(m_logEdit.m_hWnd)) { return; } m_logEdit.AppendText(pszText); } void CPageLogcat::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 CPageLogcat::OnButtonIncludeMenuClicked(NMHDR* pNMHDR, LRESULT* pResult) { BLBUTTON_NMHDR* pblbNmhdr = reinterpret_cast(pNMHDR); int position = (int)pblbNmhdr->dwData; std::string& strInclude = m_customIncludeTexts.at(position); SetDlgItemText(IDC_EDIT_INCLUDE, strInclude.c_str()); CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_REGEX); m_bIncludeRegex = FALSE; pCheckBox->SetCheck(BST_UNCHECKED); theApp.m_model.m_configuration.setLogcatIncludeRegex(m_bIncludeRegex); theApp.m_model.m_configuration.setLogcatIncludeText(m_strIncludeText); *pResult = 0; } void CPageLogcat::OnEnChangeEditInclude() { GetDlgItemText(IDC_EDIT_INCLUDE, m_strIncludeText); theApp.m_model.m_configuration.setLogcatIncludeText(m_strIncludeText); } void CPageLogcat::OnBnClickedCheckRegex() { CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_REGEX); m_bIncludeRegex = pCheckBox->GetCheck(); theApp.m_model.m_configuration.setLogcatIncludeRegex(m_bIncludeRegex); }