diff --git a/src/LabelTrack.cpp b/src/LabelTrack.cpp index 4e5b6da47..f8f23f440 100644 --- a/src/LabelTrack.cpp +++ b/src/LabelTrack.cpp @@ -279,7 +279,6 @@ void LabelTrackView::ResetFlags() { mInitialCursorPos = 1; mCurrentCursorPos = 1; - mRightDragging = false; mDrawCursor = false; } @@ -288,7 +287,6 @@ void LabelTrackView::RestoreFlags( const Flags& flags ) mInitialCursorPos = flags.mInitialCursorPos; mCurrentCursorPos = flags.mCurrentCursorPos; mSelIndex = flags.mSelIndex; - mRightDragging = flags.mRightDragging; mDrawCursor = flags.mDrawCursor; } @@ -1663,8 +1661,13 @@ bool LabelGlyphHandle::HandleGlyphDragRelease return false; } -void LabelTrackView::HandleTextDragRelease(const wxMouseEvent & evt) +void LabelTextHandle::HandleTextDragRelease(const wxMouseEvent & evt) { + auto pTrack = mpLT.lock(); + if (!pTrack) + return; + auto &view = LabelTrackView::Get( *pTrack ); + if(evt.LeftUp()) { #if 0 @@ -1690,19 +1693,20 @@ void LabelTrackView::HandleTextDragRelease(const wxMouseEvent & evt) { if (!mRightDragging) // Update drag end - SetCurrentCursorPosition(FindCursorPosition(evt.m_x)); + view.SetCurrentCursorPosition( + view.FindCursorPosition( evt.m_x ) ); return; } if (evt.RightUp()) { - const auto pTrack = FindLabelTrack(); - if (HasSelection() && - OverTextBox( - pTrack->GetLabel(mSelIndex), evt.m_x, evt.m_y)) { + const auto selIndex = view.GetSelectedIndex(); + if ( selIndex != -1 && + LabelTrackView::OverTextBox( + pTrack->GetLabel( selIndex ), evt.m_x, evt.m_y ) ) { // popup menu for editing // TODO: handle context menus via CellularPanel? - ShowContextMenu(); + view.ShowContextMenu(); } } @@ -1767,47 +1771,50 @@ void LabelGlyphHandle::HandleGlyphClick } } -void LabelTrackView::HandleTextClick(const wxMouseEvent & evt, +void LabelTextHandle::HandleTextClick(const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo, SelectedRegion *newSel) { + auto pTrack = mpLT.lock(); + if (!pTrack) + return; + + auto &view = LabelTrackView::Get( *pTrack ); static_cast(r);//compiler food. static_cast(zoomInfo);//compiler food. if (evt.ButtonDown()) { - - const auto pTrack = FindLabelTrack(); - mSelIndex = OverATextBox( *pTrack, evt.m_x, evt.m_y ); - if (mSelIndex != -1) { + const auto selIndex = LabelTrackView::OverATextBox( *pTrack, evt.m_x, evt.m_y ); + view.SetSelectedIndex( selIndex ); + if ( selIndex != -1 ) { const auto &mLabels = pTrack->GetLabels(); - const auto &labelStruct = mLabels[mSelIndex]; + const auto &labelStruct = mLabels[ selIndex ]; *newSel = labelStruct.selectedRegion; if (evt.LeftDown()) { // Find the NEW drag end - auto position = FindCursorPosition(evt.m_x); + auto position = view.FindCursorPosition( evt.m_x ); // Anchor shift-drag at the farther end of the previous highlight // that is farther from the click, on Mac, for consistency with // its text editors, but on the others, re-use the previous // anchor. + auto initial = view.GetInitialCursorPosition(); if (evt.ShiftDown()) { #ifdef __WXMAC__ // Set the drag anchor at the end of the previous selection // that is farther from the NEW drag end - if (abs(position - mCurrentCursorPos) > - abs(position - mInitialCursorPos)) - mInitialCursorPos = mCurrentCursorPos; + const auto current = view.GetCurrentCursorPosition(); + if ( abs( position - current ) > abs( position - initial ) ) + initial = current; #else - // mInitialCursorPos remains as before + // initial position remains as before #endif } else - mInitialCursorPos = position; + initial = position; - mCurrentCursorPos = position; - - mDrawCursor = true; + view.SetTextHighlight( initial, position ); mRightDragging = false; } else @@ -1820,8 +1827,8 @@ void LabelTrackView::HandleTextClick(const wxMouseEvent & evt, // Check for a click outside of the selected label's text box; in this // case PasteSelectedText() will start a NEW label at the click // location - if (!OverTextBox(&labelStruct, evt.m_x, evt.m_y)) - mSelIndex = -1; + if (!LabelTrackView::OverTextBox(&labelStruct, evt.m_x, evt.m_y)) + view.SetSelectedIndex( -1 ); double t = zoomInfo.PositionToTime(evt.m_x, r.x); *newSel = SelectedRegion(t, t); } @@ -1831,7 +1838,7 @@ void LabelTrackView::HandleTextClick(const wxMouseEvent & evt, if (evt.MiddleDown()) { // Paste text, making a NEW label if none is selected. wxTheClipboard->UsePrimarySelection(true); - PasteSelectedText(newSel->t0(), newSel->t1()); + view.PasteSelectedText(newSel->t0(), newSel->t1()); wxTheClipboard->UsePrimarySelection(false); } #endif diff --git a/src/tracks/labeltrack/ui/LabelTextHandle.cpp b/src/tracks/labeltrack/ui/LabelTextHandle.cpp index 7c3948e58..0c5e14f2f 100644 --- a/src/tracks/labeltrack/ui/LabelTextHandle.cpp +++ b/src/tracks/labeltrack/ui/LabelTextHandle.cpp @@ -87,8 +87,7 @@ UIHandle::Result LabelTextHandle::Click auto &viewInfo = ViewInfo::Get( *pProject ); mSelectedRegion = viewInfo.selectedRegion; - LabelTrackView::Get( *pLT ) - .HandleTextClick( event, evt.rect, viewInfo, &viewInfo.selectedRegion ); + HandleTextClick( event, evt.rect, viewInfo, &viewInfo.selectedRegion ); { // IF the user clicked a label, THEN select all other tracks by Label @@ -125,9 +124,8 @@ UIHandle::Result LabelTextHandle::Drag const wxMouseEvent &event = evt.event; auto pLT = TrackList::Get( *pProject ).Lock(mpLT); - auto pView = pLT ? &LabelTrackView::Get( *pLT ) : nullptr; if(pLT) - pView->HandleTextDragRelease(event); + HandleTextDragRelease(event); // locate the initial mouse position if (event.LeftIsDown()) { @@ -135,6 +133,7 @@ UIHandle::Result LabelTextHandle::Drag mLabelTrackStartXPos = event.m_x; mLabelTrackStartYPos = event.m_y; + auto pView = pLT ? &LabelTrackView::Get( *pLT ) : nullptr; if (pLT && (pView->GetSelectedIndex() != -1) && LabelTrackView::OverTextBox( @@ -175,7 +174,7 @@ UIHandle::Result LabelTextHandle::Release const wxMouseEvent &event = evt.event; auto pLT = TrackList::Get( *pProject ).Lock(mpLT); if (pLT) - LabelTrackView::Get( *pLT ).HandleTextDragRelease(event); + HandleTextDragRelease(event); // handle mouse left button up if (event.LeftUp()) diff --git a/src/tracks/labeltrack/ui/LabelTextHandle.h b/src/tracks/labeltrack/ui/LabelTextHandle.h index db568ce0c..9477ced37 100644 --- a/src/tracks/labeltrack/ui/LabelTextHandle.h +++ b/src/tracks/labeltrack/ui/LabelTextHandle.h @@ -17,6 +17,7 @@ Paul Licameli split from TrackPanel.cpp class wxMouseState; class LabelTrack; class SelectionStateChanger; +class ZoomInfo; class LabelTextHandle final : public LabelDefaultClickHandle { @@ -55,12 +56,20 @@ public: Result Cancel(AudacityProject *pProject) override; private: + void HandleTextClick + (const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo, + SelectedRegion *newSel); + void HandleTextDragRelease(const wxMouseEvent & evt); + std::weak_ptr mpLT {}; int mLabelNum{ -1 }; int mLabelTrackStartXPos { -1 }; int mLabelTrackStartYPos { -1 }; SelectedRegion mSelectedRegion{}; std::shared_ptr mChanger; + + /// flag to tell if it's a valid dragging + bool mRightDragging{ false }; }; #endif diff --git a/src/tracks/labeltrack/ui/LabelTrackView.h b/src/tracks/labeltrack/ui/LabelTrackView.h index 3b9cb06fe..eb71349ec 100644 --- a/src/tracks/labeltrack/ui/LabelTrackView.h +++ b/src/tracks/labeltrack/ui/LabelTrackView.h @@ -118,7 +118,7 @@ private: struct Flags { int mInitialCursorPos, mCurrentCursorPos, mSelIndex; - bool mRightDragging, mDrawCursor; + bool mDrawCursor; }; void ResetFlags(); @@ -126,7 +126,7 @@ private: { return { mInitialCursorPos, mCurrentCursorPos, mSelIndex, - mRightDragging, mDrawCursor + mDrawCursor }; } void RestoreFlags( const Flags& flags ); @@ -138,11 +138,6 @@ public: private: static bool IsTextClipSupported(); - - void HandleTextClick - (const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo, - SelectedRegion *newSel); - void HandleTextDragRelease(const wxMouseEvent & evt); public: void AddedLabel( const wxString &title, int pos ); @@ -160,7 +155,10 @@ public: private: void CalcHighlightXs(int *x1, int *x2) const; +public: void ShowContextMenu(); + +private: void OnContextMenu(wxCommandEvent & evt); mutable int mSelIndex{-1}; /// Keeps track of the currently selected label @@ -176,7 +174,6 @@ private: int mCurrentCursorPos; /// current cursor position int mInitialCursorPos; /// initial cursor position - bool mRightDragging; /// flag to tell if it's a valid dragging bool mDrawCursor; /// flag to tell if drawing the /// cursor or not int mRestoreFocus{-2}; /// Restore focus to this track @@ -217,7 +214,6 @@ public: static wxFont msFont; friend LabelDefaultClickHandle; - friend LabelTextHandle; }; #endif