#include "PortMonitorView.h" #include "resource.h" #include "WindowsSupport/WindowsSupport.pkg" #include "WindowsControls/WindowsControls.pkg" #include "DataConversion/DataConversion.pkg" //---------------------------------------------------------------------------------------------------------------------- // Constructors //---------------------------------------------------------------------------------------------------------------------- PortMonitorView::PortMonitorView(IUIManager& uiManager, PortMonitorViewPresenter& presenter, IS315_5313& model) :ViewBase(uiManager, presenter), _presenter(presenter), _model(model), _initializedDialog(false), _currentControlFocus(0) { _hwndDataGrid = NULL; _hwndControlPanel = NULL; _hfontHeader = NULL; _hfontData = NULL; _logLastModifiedToken = 0; SetWindowSettings(presenter.GetUnqualifiedViewTitle(), 0, 0, 500, 600); SetDockableViewType(); } //---------------------------------------------------------------------------------------------------------------------- // Member window procedure //---------------------------------------------------------------------------------------------------------------------- LRESULT PortMonitorView::WndProcWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { WndProcDialogImplementGiveFocusToChildWindowOnClick(hwnd, msg, wparam, lparam); switch (msg) { case WM_CREATE: return msgWM_CREATE(hwnd, wparam, lparam); case WM_DESTROY: return msgWM_DESTROY(hwnd, wparam, lparam); case WM_TIMER: return msgWM_TIMER(hwnd, wparam, lparam); case WM_SIZE: return msgWM_SIZE(hwnd, wparam, lparam); case WM_PAINT: return msgWM_PAINT(hwnd, wparam, lparam); } return DefWindowProc(hwnd, msg, wparam, lparam); } //---------------------------------------------------------------------------------------------------------------------- // Event handlers //---------------------------------------------------------------------------------------------------------------------- LRESULT PortMonitorView::msgWM_CREATE(HWND hwnd, WPARAM wparam, LPARAM lparam) { // Register the DataGrid window class WC_DataGrid::RegisterWindowClass(GetAssemblyHandle()); // Create the DataGrid child control _hwndDataGrid = CreateWindowEx(WS_EX_CLIENTEDGE, WC_DataGrid::WindowClassName, L"", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, 0, 0, 0, 0, hwnd, (HMENU)CTL_DATAGRID, GetAssemblyHandle(), NULL); // Insert our columns into the DataGrid control SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::InsertColumn, 0, (LPARAM)&(const WC_DataGrid::Grid_InsertColumn&)WC_DataGrid::Grid_InsertColumn(L"Operation", COLUMN_OPERATION)); SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::InsertColumn, 0, (LPARAM)&(const WC_DataGrid::Grid_InsertColumn&)WC_DataGrid::Grid_InsertColumn(L"Data", COLUMN_DATA)); SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::InsertColumn, 0, (LPARAM)&(const WC_DataGrid::Grid_InsertColumn&)WC_DataGrid::Grid_InsertColumn(L"HCounter", COLUMN_HCOUNTER)); SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::InsertColumn, 0, (LPARAM)&(const WC_DataGrid::Grid_InsertColumn&)WC_DataGrid::Grid_InsertColumn(L"VCounter", COLUMN_VCOUNTER)); SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::InsertColumn, 0, (LPARAM)&(const WC_DataGrid::Grid_InsertColumn&)WC_DataGrid::Grid_InsertColumn(L"Access Time", COLUMN_ACCESSTIME)); SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::InsertColumn, 0, (LPARAM)&(const WC_DataGrid::Grid_InsertColumn&)WC_DataGrid::Grid_InsertColumn(L"Source", COLUMN_SOURCE)); // Create the dialog control panel _hwndControlPanel = CreateDialogParam(GetAssemblyHandle(), MAKEINTRESOURCE(IDD_S315_5313_PORTMONITOR_CONTROL), hwnd, WndProcPanelStatic, (LPARAM)this); ShowWindow(_hwndControlPanel, SW_SHOWNORMAL); UpdateWindow(_hwndControlPanel); // Obtain the correct metrics for our custom font object int fontPointSize = 8; HDC hdc = GetDC(hwnd); int fontnHeight = -MulDiv(fontPointSize, GetDeviceCaps(hdc, LOGPIXELSY), 72); ReleaseDC(hwnd, hdc); // Create the font for the header in the grid control std::wstring headerFontTypefaceName = L"MS Shell Dlg"; _hfontHeader = CreateFont(fontnHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, &headerFontTypefaceName[0]); // Set the header font for the grid control SendMessage(_hwndDataGrid, WM_SETFONT, (WPARAM)_hfontHeader, (LPARAM)TRUE); // Create the font for the data region in the grid control std::wstring dataFontTypefaceName = L"Courier New"; _hfontData = CreateFont(fontnHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, &dataFontTypefaceName[0]); // Set the data region font for the grid control SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::SetDataAreaFont, (WPARAM)_hfontData, (LPARAM)TRUE); // Create a timer to trigger updates to the grid SetTimer(hwnd, 1, 200, NULL); return 0; } //---------------------------------------------------------------------------------------------------------------------- LRESULT PortMonitorView::msgWM_DESTROY(HWND hwnd, WPARAM wparam, LPARAM lparam) { // Delete our custom font objects SendMessage(_hwndDataGrid, WM_SETFONT, (WPARAM)NULL, (LPARAM)FALSE); SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::SetDataAreaFont, (WPARAM)NULL, (LPARAM)FALSE); DeleteObject(_hfontHeader); DeleteObject(_hfontData); KillTimer(hwnd, 1); return DefWindowProc(hwnd, WM_DESTROY, wparam, lparam); } //---------------------------------------------------------------------------------------------------------------------- LRESULT PortMonitorView::msgWM_TIMER(HWND hwnd, WPARAM wparam, LPARAM lparam) { // Update the control panel SendMessage(_hwndControlPanel, WM_TIMER, wparam, lparam); // If the port monitor log hasn't changed since the last refresh, abort any further // processing. unsigned int newLogLastModifiedToken = _model.GetPortMonitorLogLastModifiedToken(); if (newLogLastModifiedToken == _logLastModifiedToken) { return 0; } _logLastModifiedToken = newLogLastModifiedToken; // Retrieve the latest port monitor log std::list portMonitorList = _model.GetPortMonitorLog(); // Delete any extra rows from the data grid that are no longer required unsigned int currentRowCount = (unsigned int)SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::GetRowCount, 0, 0); if ((unsigned int)portMonitorList.size() < currentRowCount) { unsigned int rowCountToRemove = currentRowCount - (unsigned int)portMonitorList.size(); WC_DataGrid::Grid_DeleteRows deleteRowsInfo; deleteRowsInfo.targetRowNo = currentRowCount - rowCountToRemove; deleteRowsInfo.rowCount = rowCountToRemove; SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::DeleteRows, 0, (LPARAM)&deleteRowsInfo); } // Update the data grid with the latest entries std::map> rowText; unsigned int currentRow = 0; for (std::list::const_iterator i = portMonitorList.begin(); i != portMonitorList.end(); ++i) { const IS315_5313::PortMonitorEntry& entry = *i; std::map& columnText = rowText[currentRow++]; std::wstring dataString; std::wstring hcounterString; std::wstring vcounterString; std::wstring accessTimeString; IntToStringBase16(entry.data, dataString, 4); IntToStringBase16(entry.hcounterPos, hcounterString, 3); IntToStringBase16(entry.vcounterPos, vcounterString, 3); DoubleToString(entry.accessTime, accessTimeString); columnText[COLUMN_OPERATION] = entry.target; columnText[COLUMN_DATA] = dataString; columnText[COLUMN_HCOUNTER] = hcounterString; columnText[COLUMN_VCOUNTER] = vcounterString; columnText[COLUMN_ACCESSTIME] = accessTimeString; columnText[COLUMN_SOURCE] = entry.source; } SendMessage(_hwndDataGrid, (UINT)WC_DataGrid::WindowMessages::UpdateMultipleRowText, 0, (LPARAM)&rowText); return 0; } //---------------------------------------------------------------------------------------------------------------------- LRESULT PortMonitorView::msgWM_SIZE(HWND hwnd, WPARAM wparam, LPARAM lparam) { // Read the new client size of the window RECT rect; GetClientRect(hwnd, &rect); int controlWidth = rect.right; int controlHeight = rect.bottom; GetClientRect(_hwndControlPanel, &rect); int controlPanelWidth = rect.right; int controlPanelHeight = rect.bottom; // Global parameters defining how child windows are positioned int borderSize = 4; // Calculate the new position of the control panel int controlPanelPosX = borderSize; int controlPanelPosY = controlHeight - (borderSize + controlPanelHeight); MoveWindow(_hwndControlPanel, controlPanelPosX, controlPanelPosY, controlPanelWidth, controlPanelHeight, TRUE); // Calculate the new size and position of the list int listBoxWidth = controlWidth - (borderSize * 2); int listBoxPosX = borderSize; int listBoxHeight = controlHeight - ((borderSize * 2) + controlPanelHeight); int listBoxPosY = borderSize; MoveWindow(_hwndDataGrid, listBoxPosX, listBoxPosY, listBoxWidth, listBoxHeight, TRUE); return 0; } //---------------------------------------------------------------------------------------------------------------------- LRESULT PortMonitorView::msgWM_PAINT(HWND hwnd, WPARAM wparam, LPARAM lparam) { // Fill the background of the control with the dialog background colour HDC hdc = GetDC(hwnd); HBRUSH hbrush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); HBRUSH hbrushOld = (HBRUSH)SelectObject(hdc, hbrush); RECT rect; GetClientRect(hwnd, &rect); FillRect(hdc, &rect, hbrush); SelectObject(hdc, hbrushOld); DeleteObject(hbrush); ReleaseDC(hwnd, hdc); return DefWindowProc(hwnd, WM_PAINT, wparam, lparam); } //---------------------------------------------------------------------------------------------------------------------- // Panel dialog window procedure //---------------------------------------------------------------------------------------------------------------------- INT_PTR CALLBACK PortMonitorView::WndProcPanelStatic(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { // Obtain the object pointer PortMonitorView* state = (PortMonitorView*)GetWindowLongPtr(hwnd, GWLP_USERDATA); // Process the message switch (msg) { case WM_INITDIALOG: // Set the object pointer state = (PortMonitorView*)lparam; SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)(state)); // Pass this message on to the member window procedure function if (state != 0) { return state->WndProcPanel(hwnd, msg, wparam, lparam); } break; case WM_DESTROY: if (state != 0) { // Pass this message on to the member window procedure function INT_PTR result = state->WndProcPanel(hwnd, msg, wparam, lparam); // Discard the object pointer SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)0); // Return the result from processing the message return result; } break; } // Pass this message on to the member window procedure function INT_PTR result = FALSE; if (state != 0) { result = state->WndProcPanel(hwnd, msg, wparam, lparam); } return result; } //---------------------------------------------------------------------------------------------------------------------- INT_PTR PortMonitorView::WndProcPanel(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { WndProcDialogImplementSaveFieldWhenLostFocus(hwnd, msg, wparam, lparam); switch (msg) { case WM_INITDIALOG: return msgPanelWM_INITDIALOG(hwnd, wparam, lparam); case WM_TIMER: return msgPanelWM_TIMER(hwnd, wparam, lparam); case WM_COMMAND: return msgPanelWM_COMMAND(hwnd, wparam, lparam); } return FALSE; } //---------------------------------------------------------------------------------------------------------------------- // Panel dialog event handlers //---------------------------------------------------------------------------------------------------------------------- INT_PTR PortMonitorView::msgPanelWM_INITDIALOG(HWND hwnd, WPARAM wparam, LPARAM lparam) { return TRUE; } //---------------------------------------------------------------------------------------------------------------------- INT_PTR PortMonitorView::msgPanelWM_TIMER(HWND hwnd, WPARAM wparam, LPARAM lparam) { _initializedDialog = true; if (_currentControlFocus != IDC_S315_5313_PORTMONITOR_LISTSIZE) UpdateDlgItemBin(hwnd, IDC_S315_5313_PORTMONITOR_LISTSIZE, _model.GetPortMonitorLength()); CheckDlgButton(hwnd, IDC_S315_5313_PORTMONITOR_SRREAD, (_model.GetPortMonitorStatusReadEnabled())? BST_CHECKED: BST_UNCHECKED); CheckDlgButton(hwnd, IDC_S315_5313_PORTMONITOR_DPREAD, (_model.GetPortMonitorDataReadEnabled())? BST_CHECKED: BST_UNCHECKED); CheckDlgButton(hwnd, IDC_S315_5313_PORTMONITOR_HVREAD, (_model.GetPortMonitorHVReadEnabled())? BST_CHECKED: BST_UNCHECKED); CheckDlgButton(hwnd, IDC_S315_5313_PORTMONITOR_CPWRITE, (_model.GetPortMonitorControlWriteEnabled())? BST_CHECKED: BST_UNCHECKED); CheckDlgButton(hwnd, IDC_S315_5313_PORTMONITOR_DPWRITE, (_model.GetPortMonitorDataWriteEnabled())? BST_CHECKED: BST_UNCHECKED); return TRUE; } //---------------------------------------------------------------------------------------------------------------------- INT_PTR PortMonitorView::msgPanelWM_COMMAND(HWND hwnd, WPARAM wparam, LPARAM lparam) { if ((HIWORD(wparam) == EN_SETFOCUS) && _initializedDialog) { _previousText = GetDlgItemString(hwnd, LOWORD(wparam)); _currentControlFocus = LOWORD(wparam); } else if ((HIWORD(wparam) == EN_KILLFOCUS) && _initializedDialog) { std::wstring newText = GetDlgItemString(hwnd, LOWORD(wparam)); if (newText != _previousText) { switch (LOWORD(wparam)) { case IDC_S315_5313_PORTMONITOR_LISTSIZE: _model.SetPortMonitorLength(GetDlgItemBin(hwnd, LOWORD(wparam))); break; } } } else if (HIWORD(wparam) == BN_CLICKED) { switch (LOWORD(wparam)) { case IDC_S315_5313_PORTMONITOR_SRREAD: _model.SetPortMonitorStatusReadEnabled(IsDlgButtonChecked(hwnd, LOWORD(wparam)) == BST_CHECKED); break; case IDC_S315_5313_PORTMONITOR_DPREAD: _model.SetPortMonitorDataReadEnabled(IsDlgButtonChecked(hwnd, LOWORD(wparam)) == BST_CHECKED); break; case IDC_S315_5313_PORTMONITOR_HVREAD: _model.SetPortMonitorHVReadEnabled(IsDlgButtonChecked(hwnd, LOWORD(wparam)) == BST_CHECKED); break; case IDC_S315_5313_PORTMONITOR_CPWRITE: _model.SetPortMonitorControlWriteEnabled(IsDlgButtonChecked(hwnd, LOWORD(wparam)) == BST_CHECKED); break; case IDC_S315_5313_PORTMONITOR_DPWRITE: _model.SetPortMonitorDataWriteEnabled(IsDlgButtonChecked(hwnd, LOWORD(wparam)) == BST_CHECKED); break; case IDC_S315_5313_PORTMONITOR_CLEAR: _model.ClearPortMonitorLog(); break; } } return TRUE; }