Convert PlayerPlaylistBar to virtual list mode for performance (#3813)

This commit is contained in:
augmero authored and GitHub committed 2026-03-26 20:40:19 +01:00
1 parent 28c70dca59
commit c3f74b67f8
4 files changed
+382 -149

No files matched your search

+179 -42
View File
@@ -71,16 +71,8 @@ void CInPlaceWinHotkey::OnKillFocus(CWnd* pNewWnd)
CString str;
GetWindowText(str);
LV_DISPINFO dispinfo;
dispinfo.hdr.hwndFrom = GetParent()->m_hWnd;
dispinfo.hdr.idFrom = GetDlgCtrlID();
dispinfo.hdr.code = LVN_ENDLABELEDIT;
dispinfo.item.mask = LVIF_TEXT;
dispinfo.item.iItem = m_iItem;
dispinfo.item.iSubItem = m_iSubItem;
dispinfo.item.pszText = m_bESC ? nullptr : LPTSTR((LPCTSTR)str);
dispinfo.item.cchTextMax = str.GetLength();
GetParent()->GetParent()->SendMessage(WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM)&dispinfo);
CPlayerListCtrl::SendLabelEditNotify(GetParent(), LVN_ENDLABELEDIT, m_iItem, m_iSubItem,
m_bESC ? nullptr : (LPCTSTR)str);
DestroyWindow();
}
@@ -170,16 +162,8 @@ void CInPlaceEdit::OnKillFocus(CWnd* pNewWnd)
CString str;
GetWindowText(str);
LV_DISPINFO dispinfo;
dispinfo.hdr.hwndFrom = GetParent()->m_hWnd;
dispinfo.hdr.idFrom = GetDlgCtrlID();
dispinfo.hdr.code = LVN_ENDLABELEDIT;
dispinfo.item.mask = LVIF_TEXT;
dispinfo.item.iItem = m_iItem;
dispinfo.item.iSubItem = m_iSubItem;
dispinfo.item.pszText = m_bESC ? nullptr : LPTSTR((LPCTSTR)str);
dispinfo.item.cchTextMax = str.GetLength();
GetParent()->GetParent()->SendMessage(WM_NOTIFY, GetParent()->GetDlgCtrlID(), (LPARAM)&dispinfo);
CPlayerListCtrl::SendLabelEditNotify(GetParent(), LVN_ENDLABELEDIT, m_iItem, m_iSubItem,
m_bESC ? nullptr : (LPCTSTR)str);
DestroyWindow();
}
@@ -482,6 +466,86 @@ void CInPlaceListBox::OnNcDestroy()
}
// CMPCThemeInPlaceEdit
CMPCThemeInPlaceEdit::CMPCThemeInPlaceEdit(int iItem, int iSubItem, CString sInitText, CMPCThemeInPlaceEdit** ppSelf)
: m_iItem(iItem)
, m_iSubItem(iSubItem)
, m_sInitText(sInitText)
, m_ppSelf(ppSelf)
, m_bESC(FALSE)
{
if (m_ppSelf) *m_ppSelf = this;
}
CMPCThemeInPlaceEdit::~CMPCThemeInPlaceEdit()
{
}
BEGIN_MESSAGE_MAP(CMPCThemeInPlaceEdit, CMPCThemeInlineEdit)
ON_WM_KILLFOCUS()
ON_WM_NCDESTROY()
ON_WM_CHAR()
ON_WM_CREATE()
END_MESSAGE_MAP()
BOOL CMPCThemeInPlaceEdit::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
if (pMsg->wParam == VK_RETURN
|| pMsg->wParam == VK_DELETE
|| pMsg->wParam == VK_ESCAPE
|| GetKeyState(VK_CONTROL)) {
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
return TRUE;
}
}
return CEdit::PreTranslateMessage(pMsg);
}
void CMPCThemeInPlaceEdit::OnKillFocus(CWnd* pNewWnd)
{
CEdit::OnKillFocus(pNewWnd);
CString str;
GetWindowText(str);
CPlayerListCtrl::SendLabelEditNotify(GetParent(), LVN_ENDLABELEDIT, m_iItem, m_iSubItem,
m_bESC ? nullptr : (LPCTSTR)str);
DestroyWindow();
}
void CMPCThemeInPlaceEdit::OnNcDestroy()
{
if (m_ppSelf) *m_ppSelf = nullptr;
CEdit::OnNcDestroy();
delete this;
}
void CMPCThemeInPlaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == VK_ESCAPE || nChar == VK_RETURN) {
if (nChar == VK_ESCAPE) {
m_bESC = TRUE;
}
GetParent()->SetFocus();
return;
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
int CMPCThemeInPlaceEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1) {
return -1;
}
SetFont(GetParent()->GetFont());
SetWindowText(m_sInitText);
return 0;
}
// CPlayerListCtrl
IMPLEMENT_DYNAMIC(CPlayerListCtrl, CMPCThemePlayerListCtrl)
@@ -492,6 +556,7 @@ CPlayerListCtrl::CPlayerListCtrl(int tStartEditingDelay)
, m_nTimerID(0)
, m_fInPlaceDirty(false)
, inPlaceControl(false)
, m_pVirtualEdit(nullptr)
{
}
@@ -752,6 +817,53 @@ CEdit* CPlayerListCtrl::ShowInPlaceEdit(int nItem, int nCol)
return pEdit;
}
void CPlayerListCtrl::AdjustVirtualEditPos(int xOffset, int maxWidth)
{
if (!m_pVirtualEdit) return;
CRect r;
m_pVirtualEdit->GetWindowRect(r);
ScreenToClient(r);
r.left += xOffset;
if (maxWidth > 0 && r.left + maxWidth < r.right) {
r.right = r.left + maxWidth;
}
m_pVirtualEdit->MoveWindow(r);
m_pVirtualEdit->setOverridePos(xOffset, maxWidth > 0 ? maxWidth : -1);
}
void CPlayerListCtrl::StartVirtualEditLabel(int nItem, int nSubItem)
{
// Cancel any lingering edit (should already be gone via SetFocus at top of OnLButtonDown)
if (m_pVirtualEdit) {
SetFocus();
}
CString text = GetItemText(nItem, nSubItem); // triggers LVN_GETDISPINFO for virtual lists
CRect rect;
GetItemRect(nItem, &rect, LVIR_LABEL);
// Create hidden — parent adjusts rect/font in response to LVN_BEGINLABELEDIT
m_pVirtualEdit = DEBUG_NEW CMPCThemeInPlaceEdit(nItem, nSubItem, text, &m_pVirtualEdit);
m_pVirtualEdit->Create(WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, rect, this, 0);
// Notify parent; edit is already created so parent can call GetVirtualEditCtrl()
if (!SendLabelEditNotify(this, LVN_BEGINLABELEDIT, nItem, nSubItem, text)) {
// Parent denied editing
if (m_pVirtualEdit) {
m_pVirtualEdit->DestroyWindow(); // → OnNcDestroy → m_pVirtualEdit = nullptr
}
return;
}
// Parent allowed; show and focus
if (m_pVirtualEdit) {
m_pVirtualEdit->ShowWindow(SW_SHOW);
m_pVirtualEdit->SetFocus();
m_pVirtualEdit->SetSel(0, -1);
}
}
CEdit* CPlayerListCtrl::ShowInPlaceFloatEdit(int nItem, int nCol)
{
CRect rect;
@@ -840,6 +952,7 @@ CListBox* CPlayerListCtrl::ShowInPlaceListBox(int nItem, int nCol, CAtlList<CStr
}
BEGIN_MESSAGE_MAP(CPlayerListCtrl, CMPCThemePlayerListCtrl)
ON_WM_SIZE()
ON_WM_VSCROLL()
ON_WM_HSCROLL()
ON_WM_MOUSEWHEEL()
@@ -862,10 +975,19 @@ BEGIN_MESSAGE_MAP(CPlayerListCtrl, CMPCThemePlayerListCtrl)
ON_WM_XBUTTONDBLCLK()
ON_WM_SETCURSOR()
ON_NOTIFY_REFLECT_EX(LVN_ENDLABELEDIT, &CPlayerListCtrl::OnLvnEndlabeledit)
ON_MESSAGE(LVM_EDITLABEL, OnLvmEditLabel)
END_MESSAGE_MAP()
// CPlayerListCtrl message handlers
void CPlayerListCtrl::OnSize(UINT nType, int cx, int cy)
{
if (m_pVirtualEdit) {
SetFocus();
}
__super::OnSize(nType, cx, cy);
}
void CPlayerListCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if (GetFocus() != this) {
@@ -890,6 +1012,38 @@ BOOL CPlayerListCtrl::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
return CListCtrl::OnMouseWheel(nFlags, zDelta, pt);
}
LRESULT CPlayerListCtrl::SendLabelEditNotify(CWnd* pList, UINT code, int nItem, int nSubItem, LPCTSTR pszText)
{
LV_DISPINFO dispinfo = {};
dispinfo.hdr.hwndFrom = pList->m_hWnd;
dispinfo.hdr.idFrom = pList->GetDlgCtrlID();
dispinfo.hdr.code = code;
dispinfo.item.iItem = nItem;
dispinfo.item.iSubItem = nSubItem;
if (pszText) {
dispinfo.item.mask = LVIF_TEXT;
dispinfo.item.pszText = const_cast<LPTSTR>(pszText);
dispinfo.item.cchTextMax = (int)_tcslen(pszText);
}
return pList->GetParent()->SendMessage(WM_NOTIFY, pList->GetDlgCtrlID(), (LPARAM)&dispinfo);
}
LRESULT CPlayerListCtrl::OnLvmEditLabel(WPARAM wParam, LPARAM)
{
int nItem = (int)wParam;
if (nItem < 0) {
return 0;
}
if (GetStyle() & LVS_OWNERDATA) {
StartVirtualEditLabel(nItem, m_nSubItemClicked);
} else {
if (SendLabelEditNotify(this, LVN_BEGINLABELEDIT, nItem, m_nSubItemClicked)) {
SendLabelEditNotify(this, LVN_DOLABELEDIT, nItem, m_nSubItemClicked);
}
}
return 0;
}
void CPlayerListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
CListCtrl::OnLButtonDown(nFlags, point);
@@ -910,20 +1064,10 @@ void CPlayerListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
} else if (m_nItemClicked == m_nItemClickedNow /*&& m_nSubItemClicked == m_nSubItemClickedNow*/) {
m_nSubItemClicked = m_nSubItemClickedNow;
LV_DISPINFO dispinfo;
dispinfo.hdr.hwndFrom = m_hWnd;
dispinfo.hdr.idFrom = GetDlgCtrlID();
dispinfo.hdr.code = LVN_BEGINLABELEDIT;
dispinfo.item.mask = 0;
dispinfo.item.iItem = m_nItemClicked;
dispinfo.item.iSubItem = m_nSubItemClicked;
if (GetParent()->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&dispinfo)) {
if (m_tStartEditingDelay > 0) {
m_nTimerID = SetTimer(1, m_tStartEditingDelay, nullptr);
} else {
dispinfo.hdr.code = LVN_DOLABELEDIT;
GetParent()->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&dispinfo);
}
if (m_tStartEditingDelay > 0) {
m_nTimerID = SetTimer(1, m_tStartEditingDelay, nullptr);
} else {
SendMessage(LVM_EDITLABEL, m_nItemClicked, 0);
}
} else {
m_nItemClicked = m_nItemClickedNow;
@@ -941,14 +1085,7 @@ void CPlayerListCtrl::OnTimer(UINT_PTR nIDEvent)
UINT flag = LVIS_FOCUSED;
if ((GetItemState(m_nItemClicked, flag) & flag) == flag && m_nSubItemClicked >= 0) {
LV_DISPINFO dispinfo;
dispinfo.hdr.hwndFrom = m_hWnd;
dispinfo.hdr.idFrom = GetDlgCtrlID();
dispinfo.hdr.code = LVN_DOLABELEDIT;
dispinfo.item.mask = 0;
dispinfo.item.iItem = m_nItemClicked;
dispinfo.item.iSubItem = m_nSubItemClicked;
GetParent()->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&dispinfo);
SendMessage(LVM_EDITLABEL, m_nItemClicked, 0);
}
} else if (nIDEvent == 43) {
// CListCtrl does really strange things on this timer.
+46
View File
@@ -24,6 +24,7 @@
#include "WinHotkeyCtrl.h"
#include "CMPCThemeComboBox.h"
#include "CMPCThemePlayerListCtrl.h"
#include "CMPCThemeInlineEdit.h"
#define LVN_DOLABELEDIT (LVN_FIRST+1)
@@ -140,6 +141,35 @@ public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
// CMPCThemeInPlaceEdit — themed inline edit for virtual (LVS_OWNERDATA) list controls.
// CPlayerListCtrl creates and owns instances. The edit notifies the list ctrl via a
// back-reference pointer and self-destructs in OnNcDestroy (same as CInPlaceEdit).
class CMPCThemeInPlaceEdit : public CMPCThemeInlineEdit
{
protected:
int m_iItem;
int m_iSubItem;
CString m_sInitText;
CMPCThemeInPlaceEdit** m_ppSelf; // nulled on destruction so owner's pointer clears
BOOL m_bESC;
public:
CMPCThemeInPlaceEdit(int iItem, int iSubItem, CString sInitText, CMPCThemeInPlaceEdit** ppSelf);
virtual ~CMPCThemeInPlaceEdit();
protected:
virtual BOOL PreTranslateMessage(MSG* pMsg);
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnNcDestroy();
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
// CPlayerListCtrl
class CPlayerListCtrl : public CMPCThemePlayerListCtrl
@@ -153,7 +183,10 @@ private:
CRect inPlaceControlRect;
UINT_PTR m_nTimerID;
CMPCThemeInPlaceEdit* m_pVirtualEdit; // active virtual-list inline edit, or nullptr
bool PrepareInPlaceControl(int nRow, int nCol, CRect& rect);
void StartVirtualEditLabel(int nItem, int nSubItem);
public:
CPlayerListCtrl(int tStartEditingDelay = 500);
@@ -164,6 +197,17 @@ public:
int GetBottomIndex() const;
// Returns the active virtual-list inline edit, or nullptr if none is open.
// Valid during and after LVN_BEGINLABELEDIT; caller may adjust rect/font.
CMPCThemeInPlaceEdit* GetVirtualEditCtrl() const { return m_pVirtualEdit; }
// Positions the active virtual-list inline edit. xOffset shifts the left edge
// (e.g. to clear a sequence number drawn before the label); maxWidth caps the
// right edge. Call from LVN_BEGINLABELEDIT before returning *pResult = 1.
void AdjustVirtualEditPos(int xOffset, int maxWidth = -1);
static LRESULT SendLabelEditNotify(CWnd* pList, UINT code, int nItem, int nSubItem, LPCTSTR pszText = nullptr);
CWinHotkeyCtrl* ShowInPlaceWinHotkey(int nItem, int nCol);
CEdit* ShowInPlaceEdit(int nItem, int nCol);
CEdit* ShowInPlaceFloatEdit(int nItem, int nCol);
@@ -180,6 +224,7 @@ protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
@@ -201,4 +246,5 @@ public:
afx_msg void OnXButtonDblClk(UINT nFlags, UINT nButton, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg BOOL OnLvnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg LRESULT OnLvmEditLabel(WPARAM wParam, LPARAM lParam);
};
+149 -105
View File
@@ -77,8 +77,8 @@ BOOL CPlayerPlaylistBar::Create(CWnd* pParentWnd, UINT defDockBarID)
WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_TABSTOP
| LVS_OWNERDRAWFIXED
| LVS_OWNERDATA
| LVS_NOCOLUMNHEADER
| LVS_EDITLABELS
| LVS_REPORT | LVS_SINGLESEL | LVS_AUTOARRANGE | LVS_NOSORTHEADER, // TODO: remove LVS_SINGLESEL and implement multiple item repositioning (dragging is ready)
CRect(0, 0, 100, 100), this, IDC_PLAYLIST);
@@ -188,7 +188,7 @@ void CPlayerPlaylistBar::LoadDuration(POSITION pos) {
int duration = std::stoi(info);
if (duration > 0) {
pli.m_duration = duration * 10000LL;
m_list.SetItemText(FindItem(pos), COL_TIME, pli.GetLabel(1));
RefreshItem(pos);
}
} catch (...) {
}
@@ -348,13 +348,13 @@ void CPlayerPlaylistBar::ResolveLinkFiles(CAtlList<CString>& fns)
bool CPlayerPlaylistBar::AddItemNoDuplicate(CString fn, bool insertAtCurrent /*= false*/)
{
POSITION pos = m_pl.GetHeadPosition();
CString fnLower = CString(fn).MakeLower();
CString fnLower = CString(fn).MakeLower();
while (pos) {
const CPlaylistItem& pli = m_pl.GetNext(pos);
POSITION subpos = pli.m_fns.GetHeadPosition();
while (subpos) {
CString cur = pli.m_fns.GetNext(subpos);
if (cur.MakeLower() == fnLower) {
if (cur.MakeLower() == fnLower) {
// duplicate
return false;
}
@@ -1065,6 +1065,14 @@ void CPlayerPlaylistBar::Refresh()
ResizeListColumn();
}
void CPlayerPlaylistBar::RefreshItem(POSITION pos)
{
int idx = FindItem(pos);
if (idx >= 0) {
m_list.RedrawItems(idx, idx);
}
}
void CPlayerPlaylistBar::PlayListChanged() {
AfxGetAppSettings().externalPlayListPath = L"";
}
@@ -1074,7 +1082,9 @@ bool CPlayerPlaylistBar::Empty()
CAutoLock pledit(&m_plEditLock);
bool bWasPlaying = m_pl.RemoveAll();
m_list.DeleteAllItems();
m_list.SetItemCountEx(0);
m_posToIndex.clear();
m_indexToPos.clear();
m_SaveDelayed = true;
AfxGetAppSettings().externalPlayListPath = L"";
@@ -1131,7 +1141,8 @@ void CPlayerPlaylistBar::Append(CAtlList<CString>& fns, bool fMulti, CAtlList<CS
EnsureVisible(m_pl.GetTailPosition()); // This ensures that we maximize the number of newly added items shown
EnsureVisible(posFirstAdded);
if (activateListItemIndex) { // Select the first added item only if some were already present
m_list.SetItemState(activateListItemIndex, LVIS_SELECTED, LVIS_SELECTED);
m_list.SetItemState(activateListItemIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
m_list.SetSelectionMark(activateListItemIndex);
}
}
}
@@ -1205,33 +1216,20 @@ void CPlayerPlaylistBar::Append(CStringW vdn, CStringW adn, int vinput, int vcha
Refresh();
EnsureVisible(m_pl.GetTailPosition());
m_list.SetItemState((int)m_pl.GetCount() - 1, LVIS_SELECTED, LVIS_SELECTED);
m_list.SetItemState((int)m_pl.GetCount() - 1, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
SavePlaylist(true);
}
void CPlayerPlaylistBar::SetupList()
{
m_list.DeleteAllItems();
POSITION pos = m_pl.GetHeadPosition();
for (int i = 0; pos; i++) {
CPlaylistItem& pli = m_pl.GetAt(pos);
m_list.SetItemData(m_list.InsertItem(i, pli.GetLabel()), (DWORD_PTR)pos);
m_list.SetItemText(i, COL_TIME, pli.GetLabel(1));
m_pl.GetNext(pos);
}
RebuildPosMap();
m_list.SetItemCountEx((int)m_pl.GetCount(), 0);
}
void CPlayerPlaylistBar::UpdateList()
{
POSITION pos = m_pl.GetHeadPosition();
for (int i = 0, j = m_list.GetItemCount(); pos && i < j; i++) {
CPlaylistItem& pli = m_pl.GetAt(pos);
m_list.SetItemData(i, (DWORD_PTR)pos);
m_list.SetItemText(i, COL_NAME, pli.GetLabel(0));
m_list.SetItemText(i, COL_TIME, pli.GetLabel(1));
m_pl.GetNext(pos);
}
// Virtual list mode: data is always fetched from m_pl on demand
m_list.Invalidate();
}
void CPlayerPlaylistBar::EnsureVisible(POSITION pos)
@@ -1246,20 +1244,44 @@ void CPlayerPlaylistBar::EnsureVisible(POSITION pos)
int CPlayerPlaylistBar::FindItem(const POSITION pos) const
{
for (int i = 0; i < m_list.GetItemCount(); i++) {
if ((POSITION)m_list.GetItemData(i) == pos) {
return (i);
}
auto it = m_posToIndex.find(pos);
if (it != m_posToIndex.end()) {
return it->second;
}
return -1;
}
POSITION CPlayerPlaylistBar::FindPos(int i)
{
if (i < 0) {
if (i < 0 || i >= (int)m_indexToPos.size()) {
return nullptr;
}
return (POSITION)m_list.GetItemData(i);
return m_indexToPos[i];
}
void CPlayerPlaylistBar::RebuildPosMap()
{
m_posToIndex.clear();
m_indexToPos.clear();
m_indexToPos.reserve(m_pl.GetCount());
POSITION pos = m_pl.GetHeadPosition();
for (int i = 0; pos; i++) {
m_posToIndex[pos] = i;
m_indexToPos.push_back(pos);
m_pl.GetNext(pos);
}
}
void CPlayerPlaylistBar::InvalidatePlayingItem(POSITION oldPos, POSITION newPos)
{
// Only repaint the old and new "now playing" items instead of updating all items
if (oldPos) {
RefreshItem(oldPos);
}
if (newPos && newPos != oldPos) {
RefreshItem(newPos);
}
m_list.UpdateWindow();
}
INT_PTR CPlayerPlaylistBar::GetCount() const
@@ -1361,7 +1383,7 @@ bool CPlayerPlaylistBar::SetNext()
while (m_pl.GetNextWrap(pos).m_fInvalid && pos != org) {
;
}
UpdateList();
InvalidatePlayingItem(org, pos);
m_pl.SetPos(pos);
EnsureVisible(pos);
@@ -1374,6 +1396,7 @@ bool CPlayerPlaylistBar::SetPrev()
while (m_pl.GetPrevWrap(pos).m_fInvalid && pos != org) {
;
}
InvalidatePlayingItem(org, pos);
m_pl.SetPos(pos);
EnsureVisible(pos);
@@ -1382,6 +1405,7 @@ bool CPlayerPlaylistBar::SetPrev()
void CPlayerPlaylistBar::SetFirstSelected()
{
POSITION oldPos = m_pl.GetPos();
POSITION pos = m_list.GetFirstSelectedItemPosition();
if (pos) {
pos = FindPos(m_list.GetNextSelectedItem(pos));
@@ -1392,9 +1416,9 @@ void CPlayerPlaylistBar::SetFirstSelected()
;
}
// Select the first item to be played when no item was previously selected
m_list.SetItemState(FindItem(pos), LVIS_SELECTED, LVIS_SELECTED);
m_list.SetItemState(FindItem(pos), LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
}
UpdateList();
InvalidatePlayingItem(oldPos, pos);
m_pl.SetPos(pos);
EnsureVisible(pos);
}
@@ -1408,7 +1432,7 @@ void CPlayerPlaylistBar::SetFirst()
while (m_pl.GetNextWrap(pos).m_fInvalid && pos != org) {
;
}
UpdateList();
InvalidatePlayingItem(org, pos);
m_pl.SetPos(pos);
EnsureVisible(pos);
}
@@ -1419,6 +1443,7 @@ void CPlayerPlaylistBar::SetLast()
while (m_pl.GetPrevWrap(pos).m_fInvalid && pos != org) {
;
}
InvalidatePlayingItem(org, pos);
m_pl.SetPos(pos);
EnsureVisible(pos);
}
@@ -1429,8 +1454,7 @@ void CPlayerPlaylistBar::SetCurValid(bool fValid)
if (pos) {
m_pl.GetAt(pos).m_fInvalid = !fValid;
if (!fValid) {
int i = FindItem(pos);
m_list.RedrawItems(i, i);
RefreshItem(pos);
}
}
}
@@ -1442,7 +1466,7 @@ void CPlayerPlaylistBar::SetCurLabel(CString label)
auto pi = m_pl.GetAt(pos);
pi.m_label = label;
m_pl.SetAt(pos, pi);
Refresh();
RefreshItem(pos);
}
}
@@ -1452,7 +1476,7 @@ void CPlayerPlaylistBar::SetCurTime(REFERENCE_TIME rt)
if (pos) {
CPlaylistItem& pli = m_pl.GetAt(pos);
pli.m_duration = rt;
m_list.SetItemText(FindItem(pos), COL_TIME, pli.GetLabel(1));
RefreshItem(pos);
}
}
@@ -1469,7 +1493,7 @@ void CPlayerPlaylistBar::UpdateLabel(CString in) {
CPlaylistItem& m = m_pl.GetAt(pos);
m.m_label = in;
m_pl.SetAt(m_pl.GetPos(), m);
Refresh();
RefreshItem(pos);
}
}
@@ -1592,8 +1616,9 @@ bool CPlayerPlaylistBar::DeleteFileInPlaylist(POSITION pos, bool recycle)
int listPos = FindItem(pos);
if (listPos >= 0) {
m_pl.RemoveAt(pos);
m_list.DeleteItem(listPos);
m_list.RedrawItems(listPos, m_list.GetItemCount() - 1);
RebuildPosMap();
m_list.SetItemCountEx((int)m_pl.GetCount(), LVSICF_NOINVALIDATEALL);
m_list.Invalidate();
SavePlaylist();
} else {
ASSERT(false);
@@ -1711,6 +1736,7 @@ BEGIN_MESSAGE_MAP(CPlayerPlaylistBar, CMPCThemePlayerBar)
ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)
ON_WM_TIMER()
ON_WM_CONTEXTMENU()
ON_NOTIFY(LVN_GETDISPINFO, IDC_PLAYLIST, OnLvnGetDispInfoList)
ON_NOTIFY(LVN_BEGINLABELEDIT, IDC_PLAYLIST, OnLvnBeginlabeleditList)
ON_NOTIFY(LVN_ENDLABELEDIT, IDC_PLAYLIST, OnLvnEndlabeleditList)
ON_WM_XBUTTONDOWN()
@@ -1759,6 +1785,11 @@ void CPlayerPlaylistBar::EventCallback(MpcEvent ev)
void CPlayerPlaylistBar::ResizeListColumn()
{
if (::IsWindow(m_list.m_hWnd)) {
// Commit any active inline edit before resizing
if (m_list.GetVirtualEditCtrl()) {
m_list.SetFocus();
}
CRect r;
GetClientRect(r);
r.DeflateRect(2, 2);
@@ -1818,14 +1849,14 @@ void CPlayerPlaylistBar::OnLvnKeyDown(NMHDR* pNMHDR, LRESULT* pResult)
m_pMainFrame->SendMessage(WM_COMMAND, ID_FILE_CLOSEMEDIA);
}
m_pl.RemoveAt(remplpos);
m_list.DeleteItem(selected);
RebuildPosMap();
m_list.SetItemCountEx((int)m_pl.GetCount(), LVSICF_NOINVALIDATEALL);
m_list.Invalidate();
if (m_list.GetItemCount() > 0) {
if (selected < m_list.GetItemCount()) {
m_list.SetItemState(selected, LVIS_SELECTED, LVIS_SELECTED);
} else {
m_list.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);
}
int sel = (selected < m_list.GetItemCount()) ? selected : 0;
m_list.SetItemState(sel, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
m_list.SetSelectionMark(sel);
}
ResizeListColumn();
} else {
@@ -2009,7 +2040,7 @@ void CPlayerPlaylistBar::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruc
sequenceColor = textColor;
}
CString time = !pli.m_fInvalid ? m_list.GetItemText(nItem, COL_TIME) : CString(_T("Invalid"));
CString time = !pli.m_fInvalid ? pli.GetLabel(1) : CString(_T("Invalid"));
CPoint timept(rcItem.right, 0);
if (!time.IsEmpty()) {
CSize timesize = pDC->GetTextExtent(time);
@@ -2023,14 +2054,16 @@ void CPlayerPlaylistBar::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruc
}
pli.inlineEditMaxWidth = timept.x - inlineEditXpos;
file = m_list.GetItemText(nItem, COL_NAME).GetString();
file = pli.GetLabel(0);
CSize filesize = pDC->GetTextExtent(file);
while (dpi3 + numWidth.cx + filesize.cx + dpi6 > timept.x && file.GetLength() > 3) {
file = file.Left(file.GetLength() - 4) + _T("...");
filesize = pDC->GetTextExtent(file);
}
if (!::IsWindow(m_edit.m_hWnd) || !itemSelected) { //if inline edit is active, and this is the selected item, don't draw filename (visually distracting while editing)
CWnd* pFocused = GetFocus();
bool inlineEditActive = pFocused && pFocused != &m_list && m_list.IsChild(pFocused);
if (!inlineEditActive || !itemSelected) { //if inline edit is active, and this is the selected item, don't draw filename (visually distracting while editing)
pDC->SetTextColor(textColor);
pDC->SetBkColor(bgColor);
pDC->TextOut(rcItem.left + dpi3 + numWidth.cx, (rcItem.top + rcItem.bottom - filesize.cy) / 2, file);
@@ -2194,44 +2227,44 @@ void CPlayerPlaylistBar::DropItemOnList()
m_ptDropPoint.y += 10;
m_nDropIndex = m_list.HitTest(CPoint(10, m_ptDropPoint.y));
TCHAR szLabel[MAX_PATH];
LV_ITEM lvi;
ZeroMemory(&lvi, sizeof(LV_ITEM));
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE | LVIF_PARAM;
lvi.stateMask = LVIS_DROPHILITED | LVIS_FOCUSED | LVIS_SELECTED;
lvi.pszText = szLabel;
lvi.iItem = m_nDragIndex;
lvi.cchTextMax = MAX_PATH;
m_list.GetItem(&lvi);
if (m_nDropIndex < 0) {
m_nDropIndex = m_list.GetItemCount();
}
lvi.iItem = m_nDropIndex;
m_list.InsertItem(&lvi);
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_list.GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
lvi.mask = LVIF_TEXT;
lvi.iItem = m_nDropIndex;
//INDEX OF DRAGGED ITEM WILL CHANGE IF ITEM IS DROPPED ABOVE ITSELF
if (m_nDropIndex < m_nDragIndex) {
m_nDragIndex++;
}
for (int col = 1; col < nColumnCount; col++) {
_tcscpy_s(lvi.pszText, MAX_PATH, (LPCTSTR)(m_list.GetItemText(m_nDragIndex, col)));
lvi.iSubItem = col;
m_list.SetItem(&lvi);
m_nDropIndex = (int)m_pl.GetCount();
}
m_list.DeleteItem(m_nDragIndex);
ASSERT(m_indexToPos.size() == (size_t)m_pl.GetCount());
for (int i = 0; i < m_list.GetItemCount(); i++) {
POSITION pos = (POSITION)m_list.GetItemData(i);
m_pl.MoveToTail(pos);
POSITION dragPos = FindPos(m_nDragIndex);
if (!dragPos || m_nDragIndex == m_nDropIndex) {
return;
}
// Build the desired order by moving dragPos within m_indexToPos
int dropIdx = m_nDropIndex;
if (m_nDropIndex > m_nDragIndex) {
dropIdx--;
}
m_indexToPos.erase(m_indexToPos.begin() + m_nDragIndex);
if (dropIdx > (int)m_indexToPos.size()) {
dropIdx = (int)m_indexToPos.size();
}
m_indexToPos.insert(m_indexToPos.begin() + dropIdx, dragPos);
// Reorder m_pl to match the new visual order (preserves POSITIONs)
for (size_t i = 0; i < m_indexToPos.size(); i++) {
m_pl.MoveToTail(m_indexToPos[i]);
}
RebuildPosMap();
m_list.Invalidate();
ResizeListColumn();
int newIndex = FindItem(dragPos);
if (newIndex >= 0) {
// In virtual list mode, explicitly clear old states and set new ones
m_list.SetItemState(-1, 0, LVIS_SELECTED | LVIS_FOCUSED);
m_list.SetItemState(newIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
m_list.SetSelectionMark(newIndex);
}
}
BOOL CPlayerPlaylistBar::OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
@@ -2395,7 +2428,9 @@ void CPlayerPlaylistBar::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
if (m_pl.RemoveAt(pos)) {
m_pMainFrame->SendMessage(WM_COMMAND, ID_FILE_CLOSEMEDIA);
}
m_list.DeleteItem(lvhti.iItem);
RebuildPosMap();
m_list.SetItemCountEx((int)m_pl.GetCount(), LVSICF_NOINVALIDATEALL);
m_list.Invalidate();
} else {
if (Empty()) {
m_pMainFrame->SendMessage(WM_COMMAND, ID_FILE_CLOSEMEDIA);
@@ -2580,38 +2615,47 @@ void CPlayerPlaylistBar::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
}
}
void CPlayerPlaylistBar::OnLvnGetDispInfoList(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLVDISPINFO* pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
LVITEM& item = pDispInfo->item;
POSITION pos = FindPos(item.iItem);
if (!pos) {
return;
}
CPlaylistItem& pli = m_pl.GetAt(pos);
if (item.mask & LVIF_TEXT) {
CString text = pli.GetLabel(item.iSubItem);
_tcsncpy_s(item.pszText, item.cchTextMax, text, _TRUNCATE);
}
*pResult = 0;
}
void CPlayerPlaylistBar::OnLvnBeginlabeleditList(NMHDR* pNMHDR, LRESULT* pResult)
{
HWND e_hwnd = (HWND)m_list.SendMessage(LVM_GETEDITCONTROL);
if (::IsWindow(m_edit.m_hWnd)) {
m_edit.UnsubclassWindow();
}
if (e_hwnd) {
m_edit.SubclassWindow(e_hwnd);
int inlineEditMaxWidth = -1;
NMLVDISPINFO* pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
if (pDispInfo->item.iItem >= 0) {
CPlaylistItem& pli = m_pl.GetAt((POSITION)m_list.GetItemData(pDispInfo->item.iItem));
inlineEditMaxWidth = pli.inlineEditMaxWidth;
}
m_edit.setOverridePos(inlineEditXpos, inlineEditMaxWidth);
}
NMLVDISPINFO* pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
POSITION pos = FindPos(pDispInfo->item.iItem);
int maxW = pos ? m_pl.GetAt(pos).inlineEditMaxWidth : -1;
m_list.AdjustVirtualEditPos(inlineEditXpos, maxW);
*pResult = 1; // allow editing
}
void CPlayerPlaylistBar::OnLvnEndlabeleditList(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLVDISPINFO* pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
if (pDispInfo->item.iItem >= 0 && pDispInfo->item.pszText) {
CPlaylistItem& pli = m_pl.GetAt((POSITION)m_list.GetItemData(pDispInfo->item.iItem));
if (wcsnlen_s(pDispInfo->item.pszText, 1024) > 0) {
pli.m_label = pDispInfo->item.pszText;
m_list.SetItemText(pDispInfo->item.iItem, 0, pDispInfo->item.pszText);
// pszText is nullptr when the edit was cancelled (ESC)
if (pDispInfo->item.pszText != nullptr) {
POSITION pos = FindPos(pDispInfo->item.iItem);
if (pos) {
CString text(pDispInfo->item.pszText);
text.Trim();
m_pl.GetAt(pos).m_label = text;
RefreshItem(pos);
}
}
if (::IsWindow(m_edit.m_hWnd)) {
m_edit.UnsubclassWindow();
}
*pResult = 0;
}
+8 -2
View File
@@ -22,12 +22,13 @@
#pragma once
#include <afxcoll.h>
#include <unordered_map>
#include <vector>
#include "CMPCThemePlayerBar.h"
#include "PlayerListCtrl.h"
#include "Playlist.h"
#include "DropTarget.h"
#include "../Subtitles/TextFile.h"
#include "CMPCThemeInlineEdit.h"
#include "YoutubeDL.h"
#include "AppSettings.h"
@@ -52,7 +53,6 @@ private:
enum { COL_NAME, COL_TIME };
CMainFrame* m_pMainFrame;
CMPCThemeInlineEdit m_edit;
int inlineEditXpos;
CFont m_font;
@@ -72,6 +72,7 @@ private:
int m_nTimeColWidth;
void ResizeListColumn();
void RefreshItem(POSITION pos);
CPlaylistItem* GetCur();
@@ -98,6 +99,10 @@ private:
void EnsureVisible(POSITION pos);
int FindItem(const POSITION pos) const;
POSITION FindPos(int i);
void RebuildPosMap();
void InvalidatePlayingItem(POSITION oldPos, POSITION newPos);
std::unordered_map<POSITION, int> m_posToIndex;
std::vector<POSITION> m_indexToPos;
POSITION m_insertingPos;
CImageList* m_pDragImage;
@@ -202,6 +207,7 @@ public:
afx_msg BOOL OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnContextMenu(CWnd* /*pWnd*/, CPoint point);
afx_msg void OnLvnGetDispInfoList(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnLvnBeginlabeleditList(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnLvnEndlabeleditList(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnXButtonDown(UINT nFlags, UINT nButton, CPoint point);