// VirtualListView.cpp // // (c)1998-2025 Codejock Software, All Rights Reserved. // // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN // CONSENT OF CODEJOCK SOFTWARE. // // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A // SINGLE COMPUTER. // // CONTACT INFORMATION: // support@codejock.com // http://www.codejock.com // ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "VirtualList.h" #include "VirtualListDoc.h" #include "VirtualListView.h" #include "DialogRowsCount.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CVirtualRecord::CVirtualRecord(CDBVListSet* pSet) { AddItem(new CXTPGridRecordItemText()); AddItem(new CXTPGridRecordItemText()); AddItem(new CXTPGridRecordItemText()); AddItem(new CXTPGridRecordItemText()); AddItem(new CXTPGridRecordItemText()); AddItem(new CXTPGridRecordItemText()); m_pSet = pSet; } void CVirtualRecord::GetItemMetrics(XTP_GRIDRECORDITEM_DRAWARGS* pDrawArgs, XTP_GRIDRECORDITEM_METRICS* pItemMetrics) { CXTPGridColumnOrder* pSortOrder = pDrawArgs->pControl->GetColumns()->GetSortOrder(); BOOL bDecreasing = pSortOrder->GetCount() > 0 && pSortOrder->GetAt(0)->IsSortedDecreasing(); CString strColumn = pDrawArgs->pColumn->GetCaption(); int nIndexCol = pDrawArgs->pColumn->GetItemIndex(); int nIndexRow = pDrawArgs->pRow->GetIndex(); int nCount = pDrawArgs->pControl->GetRows()->GetCount(); // MD if (m_pSet) { int iCnt = m_pSet->GetFieldCount(); if (nIndexCol < iCnt) { m_pSet->MoveFirst(); m_pSet->Move(nIndexRow); if (!m_pSet->IsEOF()) { switch (nIndexCol) { case 0: pItemMetrics->strText = m_pSet->m_Email; break; case 1: pItemMetrics->strText = m_pSet->m_Name; break; case 2: pItemMetrics->strText = m_pSet->m_Phone; break; case 3: pItemMetrics->strText = m_pSet->m_Location; break; case 4: pItemMetrics->strText = m_pSet->m_Title; break; case 5: pItemMetrics->strText = m_pSet->m_Department; break; } } } } else { pItemMetrics->strText.Format(_T("%s - Row %i"), strColumn, bDecreasing ? nCount - nIndexRow : nIndexRow + 1); } // MD - this is just example of auto-generated strings for cell caption CVirtualListView* pView = DYNAMIC_DOWNCAST(CVirtualListView, pDrawArgs->pControl->GetParent()); if (pView && pView->m_bColorize) { // Execute this code to apply predefined formatting. // To turn on automatic formatting, select it from the menu if (nIndexRow % 2) { pItemMetrics->clrBackground = RGB(245, 245, 245); } } if (pView && pView->m_bSimpleTree) { // Demonstrate possibility to organize one-level tree if (nIndexRow % 10 == 0) { POSITION pos = pView->m_lstCollapsed.Find(nIndexRow); pDrawArgs->pRow->SetExpanded(pos == NULL); if (pDrawArgs->pColumn->GetIndex() == 0) { pItemMetrics->nVirtRowLevel = 0; pItemMetrics->nVirtRowFlags = xtpVirtRowHasChildren; // row has children } pItemMetrics->nVirtRowFlags |= xtpVirtRowLastChild; // row is the last child } else { pDrawArgs->pRow->SetExpanded(FALSE); // pDrawArgs->pRow->SetVisible(FALSE); if (pDrawArgs->pColumn->GetIndex() == 0) { pItemMetrics->nVirtRowLevel = 1; pItemMetrics->nVirtRowFlags = 0; } if (nIndexRow % 10 == 9) { pItemMetrics->nVirtRowFlags |= xtpVirtRowLastChild; } } } } // MD - custom derived function (base class function does nothing) // to simulate SetDispInfo action (persist updates) // User can put here extra validation, promt to confirm or fill audit trail record about // modifications BOOL CVirtualRecord::UpdateRecordField(int nIndexRow, int nIndexCol, CString strText) { if (m_pSet) { int iCnt = m_pSet->GetFieldCount(); if (nIndexCol < iCnt) { m_pSet->MoveFirst(); m_pSet->Move(nIndexRow); m_pSet->Edit(); if (!m_pSet->IsEOF()) { BOOL bUpd(FALSE); switch (nIndexCol) { case 0: if (m_pSet->m_Email != strText) { m_pSet->m_Email = strText; bUpd = TRUE; } break; case 1: if (m_pSet->m_Name != strText) { m_pSet->m_Name = strText; bUpd = TRUE; } break; case 2: if (m_pSet->m_Phone != strText) { m_pSet->m_Phone = strText; bUpd = TRUE; } break; case 3: if (m_pSet->m_Location != strText) { m_pSet->m_Location = strText; bUpd = TRUE; } break; case 4: if (m_pSet->m_Title != strText) { m_pSet->m_Title = strText; bUpd = TRUE; } break; case 5: if (m_pSet->m_Department != strText) { m_pSet->m_Department = strText; bUpd = TRUE; } break; } if (bUpd) m_pSet->Update(); } } } return TRUE; } // MD ///////////////////////////////////////////////////////////////////////////// // CVirtualListView IMPLEMENT_DYNCREATE(CVirtualListView, CXTPGridView) BEGIN_MESSAGE_MAP(CVirtualListView, CXTPGridView) //{{AFX_MSG_MAP(CVirtualListView) ON_WM_CREATE() ON_COMMAND(ID_EDIT_COPY, OnEditCopy) ON_COMMAND(ID_GRID_SETROWSCOUNT, OnGridSetrowscount) ON_COMMAND(ID_GRID_COLORIZE, OnGridColorize) ON_UPDATE_COMMAND_UI(ID_GRID_COLORIZE, OnUpdateGridColorize) ON_COMMAND(ID_GRID_TREE, OnGridSimpleTree) ON_UPDATE_COMMAND_UI(ID_GRID_TREE, OnUpdateGridSimpleTree) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CXTPGridView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CXTPGridView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CXTPGridView::OnFilePrintPreview) ON_NOTIFY(NM_CLICK, XTP_ID_GRID_CONTROL, OnGridItemClick) ON_NOTIFY(XTP_NM_GRID_ROWEXPANDED, XTP_ID_GRID_CONTROL, OnGridRowExpandChanged) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CVirtualListView construction/destruction CVirtualListView::CVirtualListView() { m_bColorize = FALSE; m_bSimpleTree = FALSE; } CVirtualListView::~CVirtualListView() { } BOOL CVirtualListView::PreCreateWindow(CREATESTRUCT& cs) { return CXTPGridView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CVirtualListView diagnostics ///////////////////////////////////////////////////////////////////////////// #ifdef _DEBUG void CVirtualListView::AssertValid() const { CView::AssertValid(); } void CVirtualListView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CVirtualListDoc* CVirtualListView::GetDocument() // non-debug version is inline { _ASSERTE(m_pDocument->IsKindOf(RUNTIME_CLASS(CVirtualListDoc))); return (CVirtualListDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CVirtualListView message handlers ///////////////////////////////////////////////////////////////////////////// int CVirtualListView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CXTPGridView::OnCreate(lpCreateStruct) == -1) return -1; CXTPGridControl& wndGrid = GetGridCtrl(); wndGrid.GetGridHeader()->AllowColumnRemove(FALSE); CDBVListSet* pSet = &GetDocument()->m_dBVListSet; TRY { if (pSet && pSet->Open()) { CXTPGridColumn* pCol = wndGrid.AddColumn(new CXTPGridColumn(0, _T("Email"), 50)); CXTPGridRecordItemEditOptions* pOpt = pCol->GetEditOptions(); pCol = wndGrid.AddColumn(new CXTPGridColumn(1, _T("Name (First, Last)"), 100)); pCol = wndGrid.AddColumn(new CXTPGridColumn(2, _T("Phone"), 50)); pCol = wndGrid.AddColumn(new CXTPGridColumn(3, _T("Location"), 50)); pCol = wndGrid.AddColumn(new CXTPGridColumn(4, _T("Title"), 50)); pCol = wndGrid.AddColumn(new CXTPGridColumn(5, _T("Dept"), 50)); /* wndGrid.GetHeaderRecords()->Add(new CVirtualRecord(pSet)); wndGrid.GetHeaderRecords()->Add(new CVirtualRecord(pSet)); wndGrid.GetHeaderRecords()->Add(new CVirtualRecord(pSet)); wndGrid.ShowHeaderRows(TRUE); wndGrid.HeaderRowsAllowAccess(TRUE); wndGrid.HeaderRowsEnableSelection(TRUE); */ /* wndGrid.GetFooterRecords()->Add(new CVirtualRecord(pSet)); wndGrid.GetFooterRecords()->Add(new CVirtualRecord(pSet)); wndGrid.GetFooterRecords()->Add(new CVirtualRecord(pSet)); wndGrid.ShowFooterRows(TRUE); wndGrid.FooterRowsAllowAccess(TRUE); wndGrid.FooterRowsEnableSelection(TRUE); */ // pSet->SetFilter("Business Systems"); // pSet->SetFilter("Consumer"); pSet->SetFilter(_T("Desktop")); // pSet->SetFilter("Developer"); // pSet->SetFilter("F & A"); // pSet->SetFilter("International Operat"); // pSet->SetFilter("International Subs"); // pSet->SetFilter("Law & Corporate Affa"); // pSet->SetFilter("Operations Service C"); // pSet->SetFilter("Personal Systems Div"); // wndGrid.SetFilterText(); // set the item count to the new record count int nRecordCount = 0; if (pSet->IsOpen()) nRecordCount = pSet->GetRecordCount(); wndGrid.SetVirtualMode(new CVirtualRecord(pSet), nRecordCount); wndGrid.GetGridHeader()->AllowColumnSort(FALSE); } } CATCH_ALL(e) { pSet = NULL; } END_CATCH_ALL if (!pSet) { wndGrid.AddColumn(new CXTPGridColumn(0, _T("Column 1"), 50)); wndGrid.AddColumn(new CXTPGridColumn(1, _T("Column 2"), 50)); wndGrid.AddColumn(new CXTPGridColumn(2, _T("Column 3"), 50)); wndGrid.SetVirtualMode(new CVirtualRecord(), 1000000); } wndGrid.Populate(); wndGrid.AllowEdit(TRUE); wndGrid.FocusSubItems(TRUE); return 0; } void CVirtualListView::OnGridItemClick(NMHDR* pNotifyStruct, LRESULT* /*result*/) { XTP_NM_GRIDRECORDITEM* pItemNotify = (XTP_NM_GRIDRECORDITEM*)pNotifyStruct; if (!pItemNotify->pRow || !pItemNotify->pColumn) return; TRACE(_T("Click on row %d, col %d\n"), pItemNotify->pRow->GetIndex(), pItemNotify->pColumn->GetItemIndex()); } void CVirtualListView::OnGridRowExpandChanged(NMHDR* pNotifyStruct, LRESULT* result) { UNREFERENCED_PARAMETER(result); XTP_NM_GRIDRECORDITEM* pItemNotify = (XTP_NM_GRIDRECORDITEM*)pNotifyStruct; if ((NULL != pItemNotify) && (NULL != pItemNotify->pRow)) { CXTPGridRow* pRow = pItemNotify->pRow; int nIndex = pRow->GetIndex(); POSITION pos = m_lstCollapsed.Find(nIndex); if (NULL != pos) { m_lstCollapsed.RemoveAt(pos); pRow->SetExpanded(FALSE); } else { m_lstCollapsed.AddTail(nIndex); pRow->SetExpanded(TRUE); } } } void CVirtualListView::OnEditCopy() { CXTPGridView::OnEditCopy(); CXTPGridSelectedRows* pSelectedRows = GetGridCtrl().GetSelectedRows(); POSITION pos = pSelectedRows->GetFirstSelectedRowPosition(); while (pos) { int nIndex = pSelectedRows->GetNextSelectedRow(pos)->GetIndex(); TRACE(_T("%i, "), nIndex + 1); } } void CVirtualListView::OnGridSetrowscount() { CDialogRowsCount dr; if (dr.DoModal() == IDOK) { GetGridCtrl().SetVirtualMode(new CVirtualRecord(), dr.m_nRowsCount); GetGridCtrl().Populate(); } } void CVirtualListView::OnGridColorize() { m_bColorize = !m_bColorize; GetGridCtrl().RedrawControl(); } void CVirtualListView::OnUpdateGridColorize(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_bColorize); } void CVirtualListView::OnGridSimpleTree() { m_bSimpleTree = !m_bSimpleTree; GetGridCtrl().GetColumns()->GetAt(0)->SetTreeColumn(m_bSimpleTree); if (!m_bSimpleTree) { m_lstCollapsed.RemoveAll(); } GetGridCtrl().RedrawControl(); } void CVirtualListView::OnUpdateGridSimpleTree(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_bSimpleTree); }