Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Src/DirView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,32 @@ void CDirView::OnColumnClick(NMHDR *pNMHDR, LRESULT *pResult)
GetOptionsMgr()->SaveOption(OPT_DIRVIEW_SORT_ASCENDING, bSortAscending);
}

// The list view is owner-data, so it tracks the selection, the focus and
// the selection mark by index. Move them along with their items.
const int count = static_cast<int>(m_listViewItems.size());
const auto keyAt = [&](int i) { return (i >= 0 && i < count) ? m_listViewItems[i].lParam : 0; };
const LPARAM focusedKey = keyAt(GetFocusedItem());
const LPARAM markKey = keyAt(m_pList->GetSelectionMark());
std::unordered_set<LPARAM> selectedKeys;
for (int i = -1; (i = m_pList->GetNextItem(i, LVNI_SELECTED)) != -1;)
selectedKeys.insert(keyAt(i));

SortColumnsAppropriately();

// Start from the state most items have and flip the others, so that
// sorting with nearly nothing or nearly everything selected stays cheap.
const bool most = selectedKeys.size() > m_listViewItems.size() / 2;
m_pList->SetItemState(-1, most ? LVIS_SELECTED : 0, LVIS_SELECTED);
for (int i = 0; i < count; i++)
{
const LPARAM key = keyAt(i);
if ((selectedKeys.count(key) != 0) != most)
m_pList->SetItemState(i, most ? 0 : LVIS_SELECTED, LVIS_SELECTED);
if (key == focusedKey)
m_pList->SetItemState(i, LVIS_FOCUSED, LVIS_FOCUSED);
if (key == markKey)
m_pList->SetSelectionMark(i);
}
*pResult = 0;
}

Expand Down