BOOL CReportCtrl::_ItemCheckMonitor(int nIndex, BOOL bBefore, BOOL bAfter, UINT nMsg) { if (!_IsValidIndex(nIndex) || m_nChkStyle == RC_CHKBOX_NONE || (bBefore && bAfter) || (!bBefore && !bAfter)) { return FALSE; } if (m_nChkStyle == RC_CHKBOX_SINGLE) { // Only one item can be checked if (!bBefore && bAfter) { SetAllItemStates(RC_ITEM_CHECKED, RC_ITEM_UNCHECKED); // uncheck all SetItemStates(nIndex, RC_ITEM_CHECKED); } } else if (m_nChkStyle == RC_CHKBOX_DISABLED) { // Cannot change the item checked/unchecked states by mouse clicks SetItemStates(nIndex, bBefore ? RC_ITEM_CHECKED : RC_ITEM_UNCHECKED); return TRUE; } SetAllItemStates(RC_ITEM_SELECTED, RC_ITEM_UNSELECTED); // unselect all SetItemStates(nIndex, RC_ITEM_SELECTED | RC_ITEM_FOCUSED); GetParent()->SendMessage(WM_ON_CHKBOX, (WPARAM)nIndex, (LPARAM)nMsg); return TRUE; }
void CReportCtrl::InvertItems(int nType) { const int ITEMS = CListCtrl::GetItemCount(); if (nType == RC_INVERT_SELECTION) { for (int i = 0; i < ITEMS; i++) SetItemStates(i, ExamItemStates(i, RC_ITEM_SELECTED) ? RC_ITEM_UNSELECTED : RC_ITEM_SELECTED); } else if (nType == RC_INVERT_CHECKMARK) { for (int i = 0; i < ITEMS; i++) SetItemStates(i, ExamItemStates(i, RC_ITEM_CHECKED) ? RC_ITEM_UNCHECKED : RC_ITEM_CHECKED); } }
int CReportCtrl::SetAllItemStates(DWORD dwOldStates, DWORD dwNewStates) { if(dwNewStates == RC_ITEM_NONE || dwOldStates == RC_ITEM_NONE) { return(0); } int nCount = 0; const int ITEMS = CListCtrl :: GetItemCount(); for(int i = 0; i < ITEMS; i++) { if(dwOldStates == RC_ITEM_ALL || ExamItemStates(i, dwOldStates)) { SetItemStates(i, dwNewStates); nCount++; } } if(dwNewStates & RC_ITEM_CHECKED) { _EnsureSingleCheck(-1); } return(nCount); }
BOOL CReportCtrl::StartEdit(int nItem, int nSubItem) { // Get the grid width and height if (!m_bAllowEdit || !_IsValidIndex(nItem) || nSubItem < 0 || nSubItem >= GetColumnCount()) return FALSE; if (m_ptEditting.x == nItem && m_ptEditting.y == nSubItem) return TRUE; EndEdit(TRUE); m_ptEditting.x = nItem; m_ptEditting.y = nSubItem; SetAllItemStates(RC_ITEM_SELECTED, RC_ITEM_UNSELECTED); // unselect all SetItemStates(m_ptEditting.x, RC_ITEM_SELECTED | RC_ITEM_FOCUSED); // determine editbox font and alignment const DWORD FMT = _GetHeaderTextFormat(nSubItem); if (FMT != m_dwPrevEditFmt) { m_dwPrevEditFmt = FMT; // Funny thing: // Changing CEdit style among ES_LEFT, ES_CENTER, ES_RIGHT at runtime works // sometimes and fails other times. It just cannot guarantee to be succeed. // So I decided to destroy and recreate the CEdit every time when the text // format changes. if (m_pWndEdit->GetSafeHwnd() != NULL) m_pWndEdit->DestroyWindow(); if (!m_pWndEdit->Create(ES_AUTOHSCROLL | ES_NOHIDESEL | WS_CHILD | WS_BORDER | FMT, CRect(0, 0, 1, 1), this, 0)) return FALSE; } else { if (m_pWndEdit->GetSafeHwnd() == NULL && !m_pWndEdit->Create(ES_AUTOHSCROLL | ES_NOHIDESEL | WS_CHILD | WS_BORDER | FMT, CRect(0, 0, 1, 1), this, 0)) { return FALSE; } } m_pWndEdit->SetFont(GetFont()); CRect rcEdit; ListView_GetSubItemRect(GetSafeHwnd(), m_ptEditting.x, m_ptEditting.y, LVIR_LABEL, &rcEdit); if (m_ptEditting.y > 0 && GetImageList() != NULL && GetItemImage(m_ptEditting.x, m_ptEditting.y) >= 0) rcEdit.DeflateRect(16, 0, 0, 0); // Move the editbox to that grid, obtain text from the grid, display the // editbox, and, finally, highlights all text in the editbox and set the // windows focus to the editbox. m_pWndEdit->MoveWindow(&rcEdit); m_pWndEdit->SetWindowText(GetItemText(m_ptEditting.x, m_ptEditting.y)); m_pWndEdit->ShowWindow(SW_SHOW); m_pWndEdit->SetSel(0, -1); m_pWndEdit->SetFocus(); return TRUE; }
BOOL CReportCtrl::DeleteItem(int nItem, BOOL bSelectNextItem) { EndEdit(m_ptEditting.x != nItem); if (bSelectNextItem) SetItemStates(nItem + 1, RC_ITEM_SELECTED); _FreeItemMemory(nItem); return CListCtrl::DeleteItem(nItem); }
BOOL CReportCtrl::SwapItems(int nItem1, int nItem2) { EndEdit(TRUE); if(!_IsValidIndex(nItem1) || !_IsValidIndex(nItem2)) { return(FALSE); } if(nItem1 == nItem2) { return(TRUE); } _UnsetSortedColumn(); // record previous states first const DWORD STATES1 = GetItemStates(nItem1); const DWORD STATES2 = GetItemStates(nItem2); const DWORD DATA1 = CListCtrl :: GetItemData(nItem1); const DWORD DATA2 = CListCtrl :: GetItemData(nItem2); // swap item texts and images for(int i = 0; i < GetColumnCount(); i++) { CString str = GetItemText(nItem1, i); CListCtrl :: SetItemText(nItem1, i, GetItemText(nItem2, i)); CListCtrl :: SetItemText(nItem2, i, str); UINT nImg = GetItemImage(nItem1, i); SetItemImage(nItem1, i, GetItemImage(nItem2, i)); SetItemImage(nItem2, i, nImg); } // swap item data CListCtrl :: SetItemData(nItem1, DATA2); CListCtrl :: SetItemData(nItem2, DATA1); // restore states SetItemStates(nItem1, STATES2); SetItemStates(nItem2, STATES1); return(TRUE); }
int CReportCtrl::MoveTo(int nItem, int nNewPosition) { if(!_IsValidIndex(nItem)) { return(-1); } EndEdit(TRUE); const int ITEMS = CListCtrl :: GetItemCount(); nNewPosition = max(0, nNewPosition); nNewPosition = min(ITEMS - 1, nNewPosition); if(nItem == nNewPosition) { return(nNewPosition); } _UnsetSortedColumn(); // Backup all states and attributes const int COLS = GetColumnCount(); const DWORD STATES = GetItemStates(nItem); const DWORD DATA = CListCtrl :: GetItemData(nItem); CArray<int, int> aImages; CStringArray aTexts; aImages.SetSize(COLS); aTexts.SetSize(COLS); for(int i = 0; i < COLS; i++) { aImages[i] = GetItemImage(nItem, i); aTexts[i] = GetItemText(nItem, i); } // Delete the item CListCtrl :: DeleteItem(nItem); // Insert a new item to the new position const int IDX = CListCtrl :: InsertItem(nNewPosition, _T("")); // Restore all states & attributes to the newly inserted item for(int j = 0; j < COLS; j++) { CListCtrl :: SetItemText(IDX, j, aTexts[j]); SetItemImage(IDX, j, aImages[j]); } CListCtrl :: SetItemData(IDX, DATA); SetItemStates(IDX, STATES); return(IDX); }
BOOL CReportCtrl::_PartialSort(int nStart, int nEnd) { if(nStart >= nEnd || !_IsValidIndex(nStart) || !_IsValidIndex(nEnd)) { return(FALSE); } const int COUNT = nEnd - nStart + 1; int i = 0; int *aIndices = new int[COUNT]; for(i = 0; i < COUNT; i++) { aIndices[i] = nStart + i; } _QuickSortRecursive(aIndices, 0, COUNT - 1); // rearrange items const int COLS = GetColumnCount(); ROWINFO *aRows = new ROWINFO[COUNT]; for(i = 0; i < COUNT; i++) { //int n = aIndices[i]; aRows[i].dwStates = GetItemStates(aIndices[i]); aRows[i].dwData = CListCtrl :: GetItemData(aIndices[i]); aRows[i].aImages.SetSize(COLS); aRows[i].aTexts.SetSize(COLS); for(int j = 0; j < COLS; j++) { aRows[i].aImages[j] = GetItemImage(aIndices[i], j); aRows[i].aTexts[j] = GetItemText(aIndices[i], j); } } for(i = 0; i < COUNT; i++) { SetItemStates(nStart + i, aRows[i].dwStates); CListCtrl :: SetItemData(nStart + i, aRows[i].dwData); for(int j = 0; j < COLS; j++) { SetItemImage(nStart + i, j, aRows[i].aImages[j]); CListCtrl :: SetItemText(nStart + i, j, aRows[i].aTexts[j]); } } delete[] aRows; delete[] aIndices; return(TRUE); }