From 79067e43f0106ea26a1a17408354e1356ce279e6 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 24 Jun 2018 21:51:03 -0400 Subject: [PATCH 01/12] ProcessUIHandleResult is a member function of TrackPanel --- src/TrackPanel.cpp | 158 ++++++++++++++++++++++----------------------- src/TrackPanel.h | 5 +- 2 files changed, 82 insertions(+), 81 deletions(-) diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index 100c2414e..c7b7f31bc 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -628,80 +628,78 @@ void TrackPanel::HandleInterruptedDrag() } } -namespace +void TrackPanel::ProcessUIHandleResult + (Track *pClickedTrack, Track *pLatestTrack, + UIHandle::Result refreshResult) { - void ProcessUIHandleResult - (TrackPanel *panel, AdornedRulerPanel *ruler, - Track *pClickedTrack, Track *pLatestTrack, - UIHandle::Result refreshResult) - { - // TODO: make a finer distinction between refreshing the track control area, - // and the waveform area. As it is, redraw both whenever you must redraw either. + const auto panel = this; - // Copy data from the underlying tracks to the pending tracks that are - // really displayed - panel->GetProject()->GetTracks()->UpdatePendingTracks(); + // TODO: make a finer distinction between refreshing the track control area, + // and the waveform area. As it is, redraw both whenever you must redraw either. - using namespace RefreshCode; + // Copy data from the underlying tracks to the pending tracks that are + // really displayed + panel->GetProject()->GetTracks()->UpdatePendingTracks(); - if (refreshResult & DestroyedCell) { - panel->UpdateViewIfNoTracks(); - // Beware stale pointer! - if (pLatestTrack == pClickedTrack) - pLatestTrack = NULL; - pClickedTrack = NULL; - } + using namespace RefreshCode; - if (pClickedTrack && (refreshResult & UpdateVRuler)) - panel->UpdateVRuler(pClickedTrack); - - if (refreshResult & DrawOverlays) { - panel->DrawOverlays(false); - ruler->DrawOverlays(false); - } - - // Refresh all if told to do so, or if told to refresh a track that - // is not known. - const bool refreshAll = - ( (refreshResult & RefreshAll) - || ((refreshResult & RefreshCell) && !pClickedTrack) - || ((refreshResult & RefreshLatestCell) && !pLatestTrack)); - - if (refreshAll) - panel->Refresh(false); - else { - if (refreshResult & RefreshCell) - panel->RefreshTrack(pClickedTrack); - if (refreshResult & RefreshLatestCell) - panel->RefreshTrack(pLatestTrack); - } - - if (refreshResult & FixScrollbars) - panel->MakeParentRedrawScrollbars(); - - if (refreshResult & Resize) - panel->GetListener()->TP_HandleResize(); - - // This flag is superfluous if you do full refresh, - // because TrackPanel::Refresh() does this too - if (refreshResult & UpdateSelection) { - panel->DisplaySelection(); - - { - // Formerly in TrackPanel::UpdateSelectionDisplay(): - - // Make sure the ruler follows suit. - // mRuler->DrawSelection(); - - // ... but that too is superfluous it does nothing but refresh - // the ruler, while DisplaySelection calls TP_DisplaySelection which - // also always refreshes the ruler. - } - } - - if ((refreshResult & EnsureVisible) && pClickedTrack) - panel->EnsureVisible(pClickedTrack); + if (refreshResult & DestroyedCell) { + panel->UpdateViewIfNoTracks(); + // Beware stale pointer! + if (pLatestTrack == pClickedTrack) + pLatestTrack = NULL; + pClickedTrack = NULL; } + + if (pClickedTrack && (refreshResult & RefreshCode::UpdateVRuler)) + panel->UpdateVRuler(pClickedTrack); + + if (refreshResult & RefreshCode::DrawOverlays) { + panel->DrawOverlays(false); + mRuler->DrawOverlays(false); + } + + // Refresh all if told to do so, or if told to refresh a track that + // is not known. + const bool refreshAll = + ( (refreshResult & RefreshAll) + || ((refreshResult & RefreshCell) && !pClickedTrack) + || ((refreshResult & RefreshLatestCell) && !pLatestTrack)); + + if (refreshAll) + panel->Refresh(false); + else { + if (refreshResult & RefreshCell) + panel->RefreshTrack(pClickedTrack); + if (refreshResult & RefreshLatestCell) + panel->RefreshTrack(pLatestTrack); + } + + if (refreshResult & FixScrollbars) + panel->MakeParentRedrawScrollbars(); + + if (refreshResult & Resize) + panel->GetListener()->TP_HandleResize(); + + // This flag is superfluous if you do full refresh, + // because TrackPanel::Refresh() does this too + if (refreshResult & UpdateSelection) { + panel->DisplaySelection(); + + { + // Formerly in TrackPanel::UpdateSelectionDisplay(): + + // Make sure the ruler follows suit. + // mRuler->DrawSelection(); + + // ... but that too is superfluous it does nothing but refresh + // the ruler, while DisplaySelection calls TP_DisplaySelection which + // also always refreshes the ruler. + } + } + + if ((refreshResult & RefreshCode::EnsureVisible) && pClickedTrack) + panel->EnsureVisible(pClickedTrack); } void TrackPanel::Uncapture(wxMouseState *pState) @@ -728,7 +726,7 @@ bool TrackPanel::CancelDragging() auto pTrack = GetTracks()->Lock(mpClickedTrack); if (pTrack) ProcessUIHandleResult( - this, mRuler, pTrack.get(), NULL, + pTrack.get(), NULL, refreshResult | mMouseOverUpdateFlags ); mpClickedTrack.reset(); mUIHandle.reset(), handle.reset(), ClearTargets(); @@ -883,7 +881,7 @@ void TrackPanel::HandleMotion // Re-draw any highlighting if (oldCell) { ProcessUIHandleResult( - this, GetRuler(), oldTrack.get(), oldTrack.get(), updateFlags); + oldTrack.get(), oldTrack.get(), updateFlags); } } @@ -950,7 +948,7 @@ void TrackPanel::HandleMotion SetCursor( *pCursor ); ProcessUIHandleResult( - this, GetRuler(), newTrack.get(), newTrack.get(), refreshCode); + newTrack.get(), newTrack.get(), refreshCode); } bool TrackPanel::HasRotation() @@ -1371,7 +1369,7 @@ void TrackPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ) pCell->HandleWheelRotation( tpmEvent, GetProject() ); auto pTrack = static_cast(pCell.get())->FindTrack(); ProcessUIHandleResult( - this, mRuler, pTrack.get(), pTrack.get(), result); + pTrack.get(), pTrack.get(), result); } void TrackPanel::OnCaptureKey(wxCommandEvent & event) @@ -1389,7 +1387,7 @@ void TrackPanel::OnCaptureKey(wxCommandEvent & event) if (t) { const unsigned refreshResult = ((TrackPanelCell*)t)->CaptureKey(*kevent, *mViewInfo, this); - ProcessUIHandleResult(this, mRuler, t, t, refreshResult); + ProcessUIHandleResult(t, t, refreshResult); event.Skip(kevent->GetSkipped()); } @@ -1456,7 +1454,7 @@ void TrackPanel::OnKeyDown(wxKeyEvent & event) if (t) { const unsigned refreshResult = ((TrackPanelCell*)t)->KeyDown(event, *mViewInfo, this); - ProcessUIHandleResult(this, mRuler, t, t, refreshResult); + ProcessUIHandleResult(t, t, refreshResult); } else event.Skip(); @@ -1479,7 +1477,7 @@ void TrackPanel::OnChar(wxKeyEvent & event) if (t) { const unsigned refreshResult = ((TrackPanelCell*)t)->Char(event, *mViewInfo, this); - ProcessUIHandleResult(this, mRuler, t, t, refreshResult); + ProcessUIHandleResult(t, t, refreshResult); } else event.Skip(); @@ -1511,7 +1509,7 @@ void TrackPanel::OnKeyUp(wxKeyEvent & event) if (t) { const unsigned refreshResult = ((TrackPanelCell*)t)->KeyUp(event, *mViewInfo, this); - ProcessUIHandleResult(this, mRuler, t, t, refreshResult); + ProcessUIHandleResult(t, t, refreshResult); return; } @@ -1631,7 +1629,7 @@ try const UIHandle::Result refreshResult = handle->Drag( tpmEvent, GetProject() ); ProcessUIHandleResult - (this, mRuler, pClickedTrack.get(), pTrack.get(), refreshResult); + (pClickedTrack.get(), pTrack.get(), refreshResult); mMouseOverUpdateFlags |= refreshResult; if (refreshResult & RefreshCode::Cancelled) { // Drag decided to abort itself @@ -1651,7 +1649,7 @@ try UIHandle::Result refreshResult = mUIHandle->Release( tpmEvent, GetProject(), this ); ProcessUIHandleResult - (this, mRuler, pClickedTrack.get(), pTrack.get(), + (pClickedTrack.get(), pTrack.get(), refreshResult | moreFlags); mUIHandle.reset(), ClearTargets(); mpClickedTrack.reset(); @@ -1736,7 +1734,7 @@ void TrackPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) HandleMotion( tpmState ); } ProcessUIHandleResult( - this, mRuler, pTrack.get(), pTrack.get(), refreshResult); + pTrack.get(), pTrack.get(), refreshResult); mMouseOverUpdateFlags |= refreshResult; } } @@ -2586,7 +2584,7 @@ void TrackPanel::OnTrackMenu(Track *t) const wxRect rect(FindTrackRect(t, true)); const UIHandle::Result refreshResult = pCell->DoContextMenu(rect, this, NULL); - ProcessUIHandleResult(this, mRuler, t, t, refreshResult); + ProcessUIHandleResult(t, t, refreshResult); } Track * TrackPanel::GetFirstSelectedTrack() diff --git a/src/TrackPanel.h b/src/TrackPanel.h index a4273efd0..c6a74c13c 100644 --- a/src/TrackPanel.h +++ b/src/TrackPanel.h @@ -550,7 +550,10 @@ protected: DECLARE_EVENT_TABLE() - // friending GetInfoCommand allow automation to get sizes of the + void ProcessUIHandleResult + (Track *pClickedTrack, Track *pLatestTrack, unsigned refreshResult); + + // friending GetInfoCommand allow automation to get sizes of the // tracks, track control panel and such. friend class GetInfoCommand; }; From 7395e5acbdaab0bd0653305d420eadb9b6ebd6a6 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 24 Jun 2018 22:14:37 -0400 Subject: [PATCH 02/12] Remove pTrack from FoundCell; new function hides some casting --- src/TrackPanel.cpp | 24 +++++++++++++----------- src/TrackPanel.h | 1 - 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index c7b7f31bc..1c0fa4e8e 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -628,6 +628,15 @@ void TrackPanel::HandleInterruptedDrag() } } +namespace { + std::shared_ptr FindTrack(TrackPanelCell *pCell ) + { + if (pCell) + return static_cast( pCell )->FindTrack(); + return {}; + } +} + void TrackPanel::ProcessUIHandleResult (Track *pClickedTrack, Track *pLatestTrack, UIHandle::Result refreshResult) @@ -844,10 +853,7 @@ void TrackPanel::HandleMotion auto newCell = tpmState.pCell; - std::shared_ptr newTrack; - if ( newCell ) - newTrack = static_cast( newCell.get() )-> - FindTrack(); + const auto newTrack = FindTrack( newCell.get() ); wxString status{}, tooltip{}; wxCursor *pCursor{}; @@ -865,10 +871,7 @@ void TrackPanel::HandleMotion // Not yet dragging. auto oldCell = mLastCell.lock(); - std::shared_ptr oldTrack; - if ( oldCell ) - oldTrack = static_cast( oldCell.get() )-> - FindTrack(); + const auto oldTrack = FindTrack( oldCell.get() ); unsigned updateFlags = mMouseOverUpdateFlags; @@ -1540,7 +1543,7 @@ try const auto foundCell = FindCell( event.m_x, event.m_y ); auto &rect = foundCell.rect; auto &pCell = foundCell.pCell; - auto &pTrack = foundCell.pTrack; + const auto pTrack = FindTrack( pCell.get() ); const auto size = GetSize(); TrackPanelMouseEvent tpmEvent{ event, rect, size, pCell }; @@ -1677,7 +1680,7 @@ try wxRect rect; const auto foundCell = FindCell(event.m_x, event.m_y); - auto t = foundCell.pTrack; + const auto t = FindTrack( foundCell.pCell.get() ); if ( t ) EnsureVisible(t.get()); } @@ -2773,7 +2776,6 @@ TrackPanel::FoundCell TrackPanel::FindCell(int mouseX, int mouseY) iter = prev; auto found = *iter; return { - static_cast( found.first.get() )->FindTrack(), found.first, found.second }; diff --git a/src/TrackPanel.h b/src/TrackPanel.h index c6a74c13c..47ddbc336 100644 --- a/src/TrackPanel.h +++ b/src/TrackPanel.h @@ -367,7 +367,6 @@ protected: // Find track info by coordinate struct FoundCell { - std::shared_ptr pTrack; std::shared_ptr pCell; wxRect rect; }; From 98514194a40b9abe0e9cd0bee7905d76a30e3243 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 24 Jun 2018 22:00:58 -0400 Subject: [PATCH 03/12] ProcessUIHandleResult takes pointers to TrackPanelCell --- src/TrackPanel.cpp | 43 ++++++++++++++++++++++--------------------- src/TrackPanel.h | 5 +++-- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index 1c0fa4e8e..d18e473f4 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -638,10 +638,17 @@ namespace { } void TrackPanel::ProcessUIHandleResult - (Track *pClickedTrack, Track *pLatestTrack, + (TrackPanelCell *pClickedCell, TrackPanelCell *pLatestCell, UIHandle::Result refreshResult) { const auto panel = this; + auto pLatestTrack = FindTrack( pLatestCell ).get(); + + // This precaution checks that the track is not only nonnull, but also + // really owned by the track list + auto pClickedTrack = GetTracks()->Lock( + std::weak_ptr{ FindTrack( pClickedCell ) } + ).get(); // TODO: make a finer distinction between refreshing the track control area, // and the waveform area. As it is, redraw both whenever you must redraw either. @@ -732,12 +739,12 @@ bool TrackPanel::CancelDragging() auto handle = mUIHandle; // UIHANDLE CANCEL UIHandle::Result refreshResult = handle->Cancel(GetProject()); - auto pTrack = GetTracks()->Lock(mpClickedTrack); - if (pTrack) + auto pClickedCell = mpClickedCell.lock(); + if (pClickedCell) ProcessUIHandleResult( - pTrack.get(), NULL, + pClickedCell.get(), {}, refreshResult | mMouseOverUpdateFlags ); - mpClickedTrack.reset(); + mpClickedCell.reset(); mUIHandle.reset(), handle.reset(), ClearTargets(); Uncapture(); return true; @@ -853,8 +860,6 @@ void TrackPanel::HandleMotion auto newCell = tpmState.pCell; - const auto newTrack = FindTrack( newCell.get() ); - wxString status{}, tooltip{}; wxCursor *pCursor{}; unsigned refreshCode = 0; @@ -871,7 +876,6 @@ void TrackPanel::HandleMotion // Not yet dragging. auto oldCell = mLastCell.lock(); - const auto oldTrack = FindTrack( oldCell.get() ); unsigned updateFlags = mMouseOverUpdateFlags; @@ -884,7 +888,7 @@ void TrackPanel::HandleMotion // Re-draw any highlighting if (oldCell) { ProcessUIHandleResult( - oldTrack.get(), oldTrack.get(), updateFlags); + oldCell.get(), oldCell.get(), updateFlags); } } @@ -951,7 +955,7 @@ void TrackPanel::HandleMotion SetCursor( *pCursor ); ProcessUIHandleResult( - newTrack.get(), newTrack.get(), refreshCode); + newCell.get(), newCell.get(), refreshCode); } bool TrackPanel::HasRotation() @@ -1370,9 +1374,8 @@ void TrackPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ) unsigned result = pCell->HandleWheelRotation( tpmEvent, GetProject() ); - auto pTrack = static_cast(pCell.get())->FindTrack(); ProcessUIHandleResult( - pTrack.get(), pTrack.get(), result); + pCell.get(), pCell.get(), result); } void TrackPanel::OnCaptureKey(wxCommandEvent & event) @@ -1543,7 +1546,6 @@ try const auto foundCell = FindCell( event.m_x, event.m_y ); auto &rect = foundCell.rect; auto &pCell = foundCell.pCell; - const auto pTrack = FindTrack( pCell.get() ); const auto size = GetSize(); TrackPanelMouseEvent tpmEvent{ event, rect, size, pCell }; @@ -1624,7 +1626,7 @@ try } if (mUIHandle) { - auto pClickedTrack = GetTracks()->Lock(mpClickedTrack); + auto pClickedCell = mpClickedCell.lock(); if (event.Dragging()) { // UIHANDLE DRAG // copy shared_ptr for safety, as in HandleClick @@ -1632,12 +1634,12 @@ try const UIHandle::Result refreshResult = handle->Drag( tpmEvent, GetProject() ); ProcessUIHandleResult - (pClickedTrack.get(), pTrack.get(), refreshResult); + (pClickedCell.get(), pCell.get(), refreshResult); mMouseOverUpdateFlags |= refreshResult; if (refreshResult & RefreshCode::Cancelled) { // Drag decided to abort itself mUIHandle.reset(), handle.reset(), ClearTargets(); - mpClickedTrack.reset(); + mpClickedCell.reset(); Uncapture( &event ); } else { @@ -1652,10 +1654,10 @@ try UIHandle::Result refreshResult = mUIHandle->Release( tpmEvent, GetProject(), this ); ProcessUIHandleResult - (pClickedTrack.get(), pTrack.get(), + (pClickedCell.get(), pCell.get(), refreshResult | moreFlags); mUIHandle.reset(), ClearTargets(); - mpClickedTrack.reset(); + mpClickedCell.reset(); // will also Uncapture() below } } @@ -1700,7 +1702,6 @@ catch( ... ) void TrackPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) { auto pCell = tpmEvent.pCell; - auto pTrack = static_cast( pCell.get() )->FindTrack(); // Do hit test once more, in case the button really pressed was not the // one "anticipated." @@ -1725,7 +1726,7 @@ void TrackPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) if (refreshResult & RefreshCode::Cancelled) mUIHandle.reset(), handle.reset(), ClearTargets(); else { - mpClickedTrack = pTrack; + mpClickedCell = pCell; // Perhaps the clicked handle wants to update cursor and state message // after a click. @@ -1737,7 +1738,7 @@ void TrackPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) HandleMotion( tpmState ); } ProcessUIHandleResult( - pTrack.get(), pTrack.get(), refreshResult); + pCell.get(), pCell.get(), refreshResult); mMouseOverUpdateFlags |= refreshResult; } } diff --git a/src/TrackPanel.h b/src/TrackPanel.h index 47ddbc336..aac34e86f 100644 --- a/src/TrackPanel.h +++ b/src/TrackPanel.h @@ -540,7 +540,7 @@ protected: bool ChangeTarget(bool forward, bool cycle); - std::weak_ptr mpClickedTrack; + std::weak_ptr mpClickedCell; UIHandlePtr mUIHandle; std::shared_ptr mpBackground; @@ -550,7 +550,8 @@ protected: DECLARE_EVENT_TABLE() void ProcessUIHandleResult - (Track *pClickedTrack, Track *pLatestTrack, unsigned refreshResult); + (TrackPanelCell *pClickedCell, TrackPanelCell *pLatestCell, + unsigned refreshResult); // friending GetInfoCommand allow automation to get sizes of the // tracks, track control panel and such. From d76777bef72aa8fe80d8381c9868e7f51c98909d Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 29 Jul 2018 16:53:11 -0400 Subject: [PATCH 04/12] Each cell can define cursor, status, and tooltip in default of hit tests --- src/TrackPanel.cpp | 25 ++++++++++++++++++++++++- src/TrackPanelCell.h | 6 ++++++ src/tracks/ui/CommonTrackPanelCell.cpp | 7 +++++++ src/tracks/ui/CommonTrackPanelCell.h | 4 ++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index d18e473f4..5a5359312 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -937,8 +937,21 @@ void TrackPanel::HandleMotion refreshCode |= code; mMouseOverUpdateFlags |= code; } + if (newCell && + (!pCursor || status.empty() || tooltip.empty())) { + // Defaulting of cursor, tooltip, and status if there is no handle, + // or if the handle does not specify them + const auto preview = newCell->DefaultPreview( tpmState, GetProject() ); + if (!pCursor) + pCursor = preview.cursor; + if (status.empty()) + status = preview.message; + if (tooltip.empty()) + tooltip = preview.tooltip; + } if (!pCursor) { - static wxCursor defaultCursor{ wxCURSOR_ARROW }; + // Ultimate default cursor + static wxCursor defaultCursor{ wxCURSOR_DEFAULT }; pCursor = &defaultCursor; } @@ -946,11 +959,15 @@ void TrackPanel::HandleMotion /* i18n-hint Esc is a key on the keyboard */ status += wxT(" "), status += _("(Esc to cancel)"); mListener->TP_DisplayStatusMessage(status); + +#if wxUSE_TOOLTIPS if (tooltip != GetToolTipText()) { // Unset first, by analogy with AButton UnsetToolTip(); SetToolTip(tooltip); } +#endif + if (pCursor) SetCursor( *pCursor ); @@ -3531,6 +3548,12 @@ TrackPanelCell::~TrackPanelCell() { } +HitTestPreview TrackPanelCell::DefaultPreview +(const TrackPanelMouseState &, const AudacityProject *) +{ + return {}; +} + unsigned TrackPanelCell::HandleWheelRotation (const TrackPanelMouseEvent &, AudacityProject *) { diff --git a/src/TrackPanelCell.h b/src/TrackPanelCell.h index 74b96e78d..c5bf1ddd1 100644 --- a/src/TrackPanelCell.h +++ b/src/TrackPanelCell.h @@ -14,6 +14,7 @@ Paul Licameli #include "MemoryX.h" class AudacityProject; +struct HitTestPreview; struct TrackPanelMouseEvent; struct TrackPanelMouseState; class ViewInfo; @@ -34,6 +35,11 @@ class AUDACITY_DLL_API TrackPanelCell /* not final */ public: virtual ~TrackPanelCell () = 0; + // May supply default cursor, status message, and tooltip, when there is no + // handle to hit at the mouse position, or the handle does not supply them. + virtual HitTestPreview DefaultPreview + (const TrackPanelMouseState &state, const AudacityProject *pProject); + // Return pointers to objects that can be queried for a status // bar message and cursor appropriate to the point, and that dispatch // mouse button events. diff --git a/src/tracks/ui/CommonTrackPanelCell.cpp b/src/tracks/ui/CommonTrackPanelCell.cpp index 0a06188b4..45cf92253 100644 --- a/src/tracks/ui/CommonTrackPanelCell.cpp +++ b/src/tracks/ui/CommonTrackPanelCell.cpp @@ -24,6 +24,13 @@ CommonTrackPanelCell::~CommonTrackPanelCell() { } +HitTestPreview CommonTrackPanelCell::DefaultPreview +(const TrackPanelMouseState &, const AudacityProject *) +{ + static wxCursor defaultCursor{ wxCURSOR_ARROW }; + return { {}, &defaultCursor, {} }; +} + unsigned CommonTrackPanelCell::HandleWheelRotation (const TrackPanelMouseEvent &evt, AudacityProject *pProject) { diff --git a/src/tracks/ui/CommonTrackPanelCell.h b/src/tracks/ui/CommonTrackPanelCell.h index 0f92f2bfc..b90819fbd 100644 --- a/src/tracks/ui/CommonTrackPanelCell.h +++ b/src/tracks/ui/CommonTrackPanelCell.h @@ -27,6 +27,10 @@ public: virtual ~CommonTrackPanelCell() = 0; + // Default to the arrow cursor + HitTestPreview DefaultPreview + (const TrackPanelMouseState &, const AudacityProject *) override; + virtual std::shared_ptr FindTrack() = 0; protected: From 062066eb7965c1498c93a5bce0360ee0eb8ccd4f Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sun, 24 Jun 2018 23:17:36 -0400 Subject: [PATCH 05/12] Split base class CellularPanel out of TrackPanel... ... To be reused with the Ruler too. The new base class does all the handling of hit tests on cells and managing of UIHandles, and keyboard events and focus. It has no knowledge of tracks -- that all resides in overriding functions in TrackPanel. Still to do, of course, is reorganizing drawing with callbacks to the cell objects. --- src/TrackPanel.cpp | 221 ++++++++++++++++++++++++++------------------- src/TrackPanel.h | 199 +++++++++++++++++++++++++--------------- 2 files changed, 254 insertions(+), 166 deletions(-) diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index 5a5359312..837577fd6 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -254,16 +254,22 @@ template < class A, class B, class DIST > bool within(A a, B b, DIST d) return (a > b - d) && (a < b + d); } -BEGIN_EVENT_TABLE(TrackPanel, OverlayPanel) +BEGIN_EVENT_TABLE(CellularPanel, OverlayPanel) + EVT_MOUSE_EVENTS(CellularPanel::OnMouseEvent) + EVT_MOUSE_CAPTURE_LOST(CellularPanel::OnCaptureLost) + EVT_COMMAND(wxID_ANY, EVT_CAPTURE_KEY, CellularPanel::OnCaptureKey) + EVT_KEY_DOWN(CellularPanel::OnKeyDown) + EVT_KEY_UP(CellularPanel::OnKeyUp) + EVT_CHAR(CellularPanel::OnChar) + EVT_SET_FOCUS(CellularPanel::OnSetFocus) + EVT_KILL_FOCUS(CellularPanel::OnKillFocus) +END_EVENT_TABLE() + +BEGIN_EVENT_TABLE(TrackPanel, CellularPanel) EVT_MOUSE_EVENTS(TrackPanel::OnMouseEvent) - EVT_MOUSE_CAPTURE_LOST(TrackPanel::OnCaptureLost) - EVT_COMMAND(wxID_ANY, EVT_CAPTURE_KEY, TrackPanel::OnCaptureKey) EVT_KEY_DOWN(TrackPanel::OnKeyDown) - EVT_KEY_UP(TrackPanel::OnKeyUp) - EVT_CHAR(TrackPanel::OnChar) + EVT_PAINT(TrackPanel::OnPaint) - EVT_SET_FOCUS(TrackPanel::OnSetFocus) - EVT_KILL_FOCUS(TrackPanel::OnKillFocus) EVT_CONTEXT_MENU(TrackPanel::OnContextMenu) EVT_TIMER(wxID_ANY, TrackPanel::OnTimer) @@ -301,11 +307,11 @@ TrackPanel::TrackPanel(wxWindow * parent, wxWindowID id, ViewInfo * viewInfo, TrackPanelListener * listener, AdornedRulerPanel * ruler) - : OverlayPanel(parent, id, pos, size, wxWANTS_CHARS | wxNO_BORDER), + : CellularPanel(parent, id, pos, size, viewInfo, + wxWANTS_CHARS | wxNO_BORDER), mTrackInfo(this), mListener(listener), mTracks(tracks), - mViewInfo(viewInfo), mRuler(ruler), mTrackArtist(nullptr), mRefreshBacking(false), @@ -614,7 +620,7 @@ void TrackPanel::MakeParentRedrawScrollbars() mListener->TP_RedrawScrollbars(); } -void TrackPanel::HandleInterruptedDrag() +void CellularPanel::HandleInterruptedDrag() { if (mUIHandle && mUIHandle->StopsOnKeystroke() ) { // The bogus id isn't used anywhere, but may help with debugging. @@ -718,7 +724,7 @@ void TrackPanel::ProcessUIHandleResult panel->EnsureVisible(pClickedTrack); } -void TrackPanel::Uncapture(wxMouseState *pState) +void CellularPanel::Uncapture(wxMouseState *pState) { auto state = ::wxGetMouseState(); if (!pState) { @@ -732,7 +738,7 @@ void TrackPanel::Uncapture(wxMouseState *pState) HandleMotion( *pState ); } -bool TrackPanel::CancelDragging() +bool CellularPanel::CancelDragging() { if (mUIHandle) { // copy shared_ptr for safety, as in HandleClick @@ -752,7 +758,7 @@ bool TrackPanel::CancelDragging() return false; } -bool TrackPanel::HandleEscapeKey(bool down) +bool CellularPanel::HandleEscapeKey(bool down) { if (!down) return false; @@ -778,7 +784,7 @@ bool TrackPanel::HandleEscapeKey(bool down) return false; } -void TrackPanel::UpdateMouseState(const wxMouseState &state) +void CellularPanel::UpdateMouseState(const wxMouseState &state) { mLastMouseState = state; @@ -798,7 +804,7 @@ void TrackPanel::UpdateMouseState(const wxMouseState &state) } } -void TrackPanel::HandleModifierKey() +void CellularPanel::HandleModifierKey() { HandleCursorForPresentMouseState(); } @@ -813,7 +819,7 @@ void TrackPanel::HandlePageDownKey() mListener->TP_ScrollWindow(GetScreenEndTime()); } -void TrackPanel::HandleCursorForPresentMouseState(bool doHit) +void CellularPanel::HandleCursorForPresentMouseState(bool doHit) { // Come here on modifier key or mouse button transitions, // or on starting or stopping of play or record, @@ -842,7 +848,7 @@ bool TrackPanel::IsAudioActive() /// may cause the appropriate cursor and message to change. /// As this procedure checks which region the mouse is over, it is /// appropriate to establish the message in the status bar. -void TrackPanel::HandleMotion( wxMouseState &inState, bool doHit ) +void CellularPanel::HandleMotion( wxMouseState &inState, bool doHit ) { UpdateMouseState( inState ); @@ -853,7 +859,7 @@ void TrackPanel::HandleMotion( wxMouseState &inState, bool doHit ) HandleMotion( tpmState, doHit ); } -void TrackPanel::HandleMotion +void CellularPanel::HandleMotion ( const TrackPanelMouseState &tpmState, bool doHit ) { auto handle = mUIHandle; @@ -955,11 +961,8 @@ void TrackPanel::HandleMotion pCursor = &defaultCursor; } - if (HasEscape()) - /* i18n-hint Esc is a key on the keyboard */ - status += wxT(" "), status += _("(Esc to cancel)"); - mListener->TP_DisplayStatusMessage(status); - + UpdateStatusMessage(status); + #if wxUSE_TOOLTIPS if (tooltip != GetToolTipText()) { // Unset first, by analogy with AButton @@ -975,7 +978,16 @@ void TrackPanel::HandleMotion newCell.get(), newCell.get(), refreshCode); } -bool TrackPanel::HasRotation() +void TrackPanel::UpdateStatusMessage( const wxString &st ) +{ + auto status = st; + if (HasEscape()) + /* i18n-hint Esc is a key on the keyboard */ + status += wxT(" "), status += _("(Esc to cancel)"); + mListener->TP_DisplayStatusMessage(status); +} + +bool CellularPanel::HasRotation() { // Is there a nontrivial TAB key rotation? if ( mTargets.size() > 1 ) @@ -984,7 +996,7 @@ bool TrackPanel::HasRotation() return target && target->HasRotation(); } -bool TrackPanel::HasEscape() +bool CellularPanel::HasEscape() { if (IsMouseCaptured()) return true; @@ -997,7 +1009,7 @@ bool TrackPanel::HasEscape() return mTargets.size() > 0; } -bool TrackPanel::ChangeTarget(bool forward, bool cycle) +bool CellularPanel::ChangeTarget(bool forward, bool cycle) { auto size = mTargets.size(); @@ -1090,7 +1102,7 @@ void TrackPanel::MessageForScreenReader(const wxString& message) } /// Determines if a modal tool is active -bool TrackPanel::IsMouseCaptured() +bool CellularPanel::IsMouseCaptured() { return mUIHandle != NULL; } @@ -1140,9 +1152,9 @@ void TrackPanel::OnTrackListResizing(wxCommandEvent & e) // Tracks have been removed from the list. void TrackPanel::OnTrackListDeletion(wxCommandEvent & e) { - if (mUIHandle) { - // copy shared_ptr for safety, as in HandleClick - auto handle = mUIHandle; + // copy shared_ptr for safety, as in HandleClick + auto handle = Target(); + if (handle) { handle->OnProjectChange(GetProject()); } @@ -1349,7 +1361,7 @@ bool TrackInfo::HideTopItem( const wxRect &rect, const wxRect &subRect, } /// Handle mouse wheel rotation (for zoom in/out, vertical and horizontal scrolling) -void TrackPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ) +void CellularPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ) { auto pCell = tpmEvent.pCell; if (!pCell) @@ -1395,7 +1407,7 @@ void TrackPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ) pCell.get(), pCell.get(), result); } -void TrackPanel::OnCaptureKey(wxCommandEvent & event) +void CellularPanel::OnCaptureKey(wxCommandEvent & event) { mEnableTab = false; wxKeyEvent *kevent = static_cast(event.GetEventObject()); @@ -1403,19 +1415,17 @@ void TrackPanel::OnCaptureKey(wxCommandEvent & event) if ( WXK_ESCAPE != code ) HandleInterruptedDrag(); - // TODO? Some notion of focused cell, more generally than focused tracks - - // Give focused track precedence - Track * const t = GetFocusedTrack(); + // Give focused cell precedence + const auto t = GetFocusedCell(); if (t) { const unsigned refreshResult = - ((TrackPanelCell*)t)->CaptureKey(*kevent, *mViewInfo, this); + t->CaptureKey(*kevent, *mViewInfo, this); ProcessUIHandleResult(t, t, refreshResult); event.Skip(kevent->GetSkipped()); } #if 0 - // Special TAB key handling, but only if the track didn't capture it + // Special TAB key handling, but only if the cell didn't capture it if ( !(t && !kevent->GetSkipped()) && WXK_TAB == code && HasRotation() ) { // Override TAB navigation in wxWidgets, by not skipping @@ -1430,6 +1440,26 @@ void TrackPanel::OnCaptureKey(wxCommandEvent & event) } void TrackPanel::OnKeyDown(wxKeyEvent & event) +{ + switch (event.GetKeyCode()) + { + // Allow PageUp and PageDown keys to + //scroll the Track Panel left and right + case WXK_PAGEUP: + HandlePageUpKey(); + return; + + case WXK_PAGEDOWN: + HandlePageDownKey(); + return; + + default: + // fall through to base class handler + event.Skip(); + } +} + +void CellularPanel::OnKeyDown(wxKeyEvent & event) { switch (event.GetKeyCode()) { @@ -1450,16 +1480,6 @@ void TrackPanel::OnKeyDown(wxKeyEvent & event) HandleModifierKey(); break; - // Allow PageUp and PageDown keys to - //scroll the Track Panel left and right - case WXK_PAGEUP: - HandlePageUpKey(); - return; - - case WXK_PAGEDOWN: - HandlePageDownKey(); - return; - #if 0 case WXK_TAB: if ( mEnableTab && HasRotation() ) { @@ -1472,18 +1492,18 @@ void TrackPanel::OnKeyDown(wxKeyEvent & event) #endif } - Track *const t = GetFocusedTrack(); + const auto t = GetFocusedCell(); if (t) { const unsigned refreshResult = - ((TrackPanelCell*)t)->KeyDown(event, *mViewInfo, this); + t->KeyDown(event, *mViewInfo, this); ProcessUIHandleResult(t, t, refreshResult); } else event.Skip(); } -void TrackPanel::OnChar(wxKeyEvent & event) +void CellularPanel::OnChar(wxKeyEvent & event) { switch (event.GetKeyCode()) { @@ -1496,17 +1516,17 @@ void TrackPanel::OnChar(wxKeyEvent & event) return; } - Track *const t = GetFocusedTrack(); + const auto t = GetFocusedCell(); if (t) { const unsigned refreshResult = - ((TrackPanelCell*)t)->Char(event, *mViewInfo, this); + t->Char(event, *mViewInfo, this); ProcessUIHandleResult(t, t, refreshResult); } else event.Skip(); } -void TrackPanel::OnKeyUp(wxKeyEvent & event) +void CellularPanel::OnKeyUp(wxKeyEvent & event) { bool didSomething = false; switch (event.GetKeyCode()) @@ -1528,10 +1548,10 @@ void TrackPanel::OnKeyUp(wxKeyEvent & event) if (didSomething) return; - Track * const t = GetFocusedTrack(); + const auto t = GetFocusedCell(); if (t) { const unsigned refreshResult = - ((TrackPanelCell*)t)->KeyUp(event, *mViewInfo, this); + t->KeyUp(event, *mViewInfo, this); ProcessUIHandleResult(t, t, refreshResult); return; } @@ -1540,7 +1560,7 @@ void TrackPanel::OnKeyUp(wxKeyEvent & event) } /// Should handle the case when the mouse capture is lost. -void TrackPanel::OnCaptureLost(wxMouseCaptureLostEvent & WXUNUSED(event)) +void CellularPanel::OnCaptureLost(wxMouseCaptureLostEvent & WXUNUSED(event)) { ClearTargets(); @@ -1554,10 +1574,37 @@ void TrackPanel::OnCaptureLost(wxMouseCaptureLostEvent & WXUNUSED(event)) OnMouseEvent(e); } +void TrackPanel::OnMouseEvent(wxMouseEvent & event) +{ + if (event.LeftDown()) { + // wxTimers seem to be a little unreliable, so this + // "primes" it to make sure it keeps going for a while... + + // When this timer fires, we call TrackPanel::OnTimer and + // possibly update the screen for offscreen scrolling. + mTimer.Stop(); + mTimer.Start(kTimerInterval, FALSE); + } + + + if (event.ButtonUp()) { + //EnsureVisible should be called after processing the up-click. + this->CallAfter( [this, event]{ + const auto foundCell = FindCell(event.m_x, event.m_y); + const auto t = FindTrack( foundCell.pCell.get() ); + if ( t ) + EnsureVisible(t.get()); + } ); + } + + // Must also fall through to base class handler + event.Skip(); +} + /// This handles just generic mouse events. Then, based /// on our current state, we forward the mouse events to /// various interested parties. -void TrackPanel::OnMouseEvent(wxMouseEvent & event) +void CellularPanel::OnMouseEvent(wxMouseEvent & event) try { const auto foundCell = FindCell( event.m_x, event.m_y ); @@ -1578,7 +1625,7 @@ try // If a mouse event originates from a keyboard context menu event then // event.GetPosition() == wxDefaultPosition. wxContextMenu events are handled in - // TrackPanel::OnContextMenu(), and therefore associated mouse events are ignored here. + // CellularPanel::OnContextMenu(), and therefore associated mouse events are ignored here. // Not ignoring them was causing bug 613: the mouse events were interpreted as clicking // outside the tracks. if (event.GetPosition() == wxDefaultPosition && (event.RightDown() || event.RightUp())) { @@ -1604,14 +1651,6 @@ try // parent window 'come alive' if it didn't have focus. wxActivateEvent e; GetParent()->GetEventHandler()->ProcessEvent(e); - - // wxTimers seem to be a little unreliable, so this - // "primes" it to make sure it keeps going for a while... - - // When this timer fires, we call TrackPanel::OnTimer and - // possibly update the screen for offscreen scrolling. - mTimer.Stop(); - mTimer.Start(kTimerInterval, FALSE); } if (event.ButtonDown()) { @@ -1692,17 +1731,8 @@ try CaptureMouse(); } - //EnsureVisible should be called after the up-click. - if (event.ButtonUp()) { + if (event.ButtonUp()) Uncapture(); - - wxRect rect; - - const auto foundCell = FindCell(event.m_x, event.m_y); - const auto t = FindTrack( foundCell.pCell.get() ); - if ( t ) - EnsureVisible(t.get()); - } } catch( ... ) { @@ -1716,7 +1746,7 @@ catch( ... ) throw; } -void TrackPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) +void CellularPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) { auto pCell = tpmEvent.pCell; @@ -1762,7 +1792,7 @@ void TrackPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) double TrackPanel::GetMostRecentXPos() { - return mViewInfo->PositionToTime(mMouseMostRecentX, GetLabelWidth()); + return mViewInfo->PositionToTime(MostRecentXCoord(), GetLabelWidth()); } void TrackPanel::RefreshTrack(Track *trk, bool refreshbacking) @@ -2779,7 +2809,7 @@ void TrackPanel::DrawShadow(Track * /* t */ , wxDC * dc, const wxRect & rect) /// Determines which cell is under the mouse /// @param mouseX - mouse X position. /// @param mouseY - mouse Y position. -TrackPanel::FoundCell TrackPanel::FindCell(int mouseX, int mouseY) +auto TrackPanel::FindCell(int mouseX, int mouseY) -> FoundCell { auto range = Cells(); auto &iter = range.first, &end = range.second; @@ -2861,36 +2891,41 @@ void TrackPanel::DisplaySelection() mListener->TP_DisplaySelection(); } -Track *TrackPanel::GetFocusedTrack() +TrackPanelCell *TrackPanel::GetFocusedCell() { return mAx->GetFocus().get(); } +Track *TrackPanel::GetFocusedTrack() +{ + return static_cast( GetFocusedCell() ); +} + +void TrackPanel::SetFocusedCell() +{ + SetFocusedTrack( GetFocusedTrack() ); +} + void TrackPanel::SetFocusedTrack( Track *t ) { // Make sure we always have the first linked track of a stereo track if (t && !t->GetLinked() && t->GetLink()) t = (WaveTrack*)t->GetLink(); - if ( !mAx->SetFocus( Track::Pointer( t ) ) ) - return; + auto cell = mAx->SetFocus( Track::Pointer( t ) ).get(); - if (t && AudacityProject::GetKeyboardCaptureHandler()) - AudacityProject::ReleaseKeyboard(this); - - if (t) + if (cell) { AudacityProject::CaptureKeyboard(this); - - Refresh( false ); + Refresh( false ); + } } -void TrackPanel::OnSetFocus(wxFocusEvent & WXUNUSED(event)) +void CellularPanel::OnSetFocus(wxFocusEvent & WXUNUSED(event)) { - SetFocusedTrack( GetFocusedTrack() ); - Refresh( false ); + SetFocusedCell(); } -void TrackPanel::OnKillFocus(wxFocusEvent & WXUNUSED(event)) +void CellularPanel::OnKillFocus(wxFocusEvent & WXUNUSED(event)) { if (AudacityProject::HasKeyboardCapture(this)) { diff --git a/src/TrackPanel.h b/src/TrackPanel.h index aac34e86f..0afaab43f 100644 --- a/src/TrackPanel.h +++ b/src/TrackPanel.h @@ -242,9 +242,126 @@ private: const int DragThreshold = 3;// Anything over 3 pixels is a drag, else a click. -class AUDACITY_DLL_API TrackPanel final : public OverlayPanel { - public: +// This class manages a panel divided into a number of sub-rectangles called +// cells, that each implement hit tests returning click-drag-release handler +// objects, and other services. +// It has no dependency on the Track class. +class AUDACITY_DLL_API CellularPanel : public OverlayPanel { +public: + CellularPanel(wxWindow * parent, wxWindowID id, + const wxPoint & pos, + const wxSize & size, + ViewInfo *viewInfo, + // default as for wxPanel: + long style = wxTAB_TRAVERSAL | wxNO_BORDER) + : OverlayPanel(parent, id, pos, size, style) + , mViewInfo( viewInfo ) + {} + // Overridables: + + virtual AudacityProject *GetProject() const = 0; + + // Find track info by coordinate + struct FoundCell { + std::shared_ptr pCell; + wxRect rect; + }; + virtual FoundCell FindCell(int mouseX, int mouseY) = 0; + virtual TrackPanelCell *GetFocusedCell() = 0; + virtual void SetFocusedCell() = 0; + + virtual void ProcessUIHandleResult + (TrackPanelCell *pClickedCell, TrackPanelCell *pLatestCell, + unsigned refreshResult) = 0; + + virtual void UpdateStatusMessage( const wxString & ) = 0; + +public: + UIHandlePtr Target() + { + if (mTargets.size()) + return mTargets[mTarget]; + else + return {}; + } + + bool IsMouseCaptured(); + + wxCoord MostRecentXCoord() const { return mMouseMostRecentX; } + + void HandleCursorForPresentMouseState(bool doHit = true); + +protected: + bool HasEscape(); + bool CancelDragging(); + void ClearTargets() + { + // Forget the rotation of hit test candidates when the mouse moves from + // cell to cell or outside of the panel entirely. + mLastCell.reset(); + mTargets.clear(); + mTarget = 0; + mMouseOverUpdateFlags = 0; + } + +private: + bool HasRotation(); + bool ChangeTarget(bool forward, bool cycle); + + void OnMouseEvent(wxMouseEvent & event); + void OnCaptureLost(wxMouseCaptureLostEvent & event); + void OnCaptureKey(wxCommandEvent & event); + void OnKeyDown(wxKeyEvent & event); + void OnChar(wxKeyEvent & event); + void OnKeyUp(wxKeyEvent & event); + + void OnSetFocus(wxFocusEvent & event); + void OnKillFocus(wxFocusEvent & event); + + void HandleInterruptedDrag(); + void Uncapture( wxMouseState *pState = nullptr ); + bool HandleEscapeKey(bool down); + void UpdateMouseState(const wxMouseState &state); + void HandleModifierKey(); + + void HandleClick( const TrackPanelMouseEvent &tpmEvent ); + void HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ); + + void HandleMotion( wxMouseState &state, bool doHit = true ); + void HandleMotion + ( const TrackPanelMouseState &tpmState, bool doHit = true ); + + +protected: + ViewInfo *mViewInfo; + +private: + UIHandlePtr mUIHandle; + + std::weak_ptr mLastCell; + std::vector mTargets; + size_t mTarget {}; + unsigned mMouseOverUpdateFlags{}; + +protected: + // To do: make a drawing method and make this private + wxMouseState mLastMouseState; + +private: + int mMouseMostRecentX; + int mMouseMostRecentY; + + std::weak_ptr mpClickedCell; + + bool mEnableTab{}; + + DECLARE_EVENT_TABLE() +}; + +class AUDACITY_DLL_API TrackPanel final : public CellularPanel { + + public: TrackPanel(wxWindow * parent, wxWindowID id, const wxPoint & pos, @@ -263,14 +380,7 @@ class AUDACITY_DLL_API TrackPanel final : public OverlayPanel { void OnPaint(wxPaintEvent & event); void OnMouseEvent(wxMouseEvent & event); - void OnCaptureLost(wxMouseCaptureLostEvent & event); - void OnCaptureKey(wxCommandEvent & event); void OnKeyDown(wxKeyEvent & event); - void OnChar(wxKeyEvent & event); - void OnKeyUp(wxKeyEvent & event); - - void OnSetFocus(wxFocusEvent & event); - void OnKillFocus(wxFocusEvent & event); void OnContextMenu(wxContextMenuEvent & event); @@ -302,31 +412,24 @@ class AUDACITY_DLL_API TrackPanel final : public OverlayPanel { // void SetSelectionFormat(int iformat) // void SetSnapTo(int snapto) - void HandleInterruptedDrag(); - void Uncapture( wxMouseState *pState = nullptr ); - bool CancelDragging(); - bool HandleEscapeKey(bool down); - void UpdateMouseState(const wxMouseState &state); - void HandleModifierKey(); void HandlePageUpKey(); void HandlePageDownKey(); - AudacityProject * GetProject() const; + AudacityProject * GetProject() const override; void ScrollIntoView(double pos); void ScrollIntoView(int x); void OnTrackMenu(Track *t = NULL); Track * GetFirstSelectedTrack(); - bool IsMouseCaptured(); void EnsureVisible(Track * t); void VerticalScroll( float fracPosition); + TrackPanelCell *GetFocusedCell() override; + void SetFocusedCell() override; Track *GetFocusedTrack(); void SetFocusedTrack(Track *t); - void HandleCursorForPresentMouseState(bool doHit = true); - void UpdateVRulers(); void UpdateVRuler(Track *t); void UpdateTrackVRuler(const Track *t); @@ -338,7 +441,6 @@ class AUDACITY_DLL_API TrackPanel final : public OverlayPanel { protected: bool IsAudioActive(); - void HandleClick( const TrackPanelMouseEvent &tpmEvent ); public: size_t GetTrackCount() const; @@ -351,9 +453,6 @@ public: void UpdateAccessibility(); void MessageForScreenReader(const wxString& message); - // MM: Handle mouse wheel rotation - void HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ); - void MakeParentRedrawScrollbars(); // If label, rectangle includes track control panel only. @@ -366,15 +465,7 @@ protected: // a crash, as it can take many seconds for large (eg. 10 track-hours) projects // Find track info by coordinate - struct FoundCell { - std::shared_ptr pCell; - wxRect rect; - }; - FoundCell FindCell(int mouseX, int mouseY); - - void HandleMotion( wxMouseState &state, bool doHit = true ); - void HandleMotion - ( const TrackPanelMouseState &tpmState, bool doHit = true ); + FoundCell FindCell(int mouseX, int mouseY) override; int GetVRulerWidth() const; int GetVRulerOffset() const { return mTrackInfo.GetTrackInfoWidth(); } @@ -447,7 +538,6 @@ protected: TrackPanelListener *mListener; std::shared_ptr mTracks; - ViewInfo *mViewInfo; AdornedRulerPanel *mRuler; @@ -481,11 +571,6 @@ protected: bool mRedrawAfterStop; - wxMouseState mLastMouseState; - - int mMouseMostRecentX; - int mMouseMostRecentY; - friend class TrackPanelAx; #if wxUSE_ACCESSIBILITY @@ -510,48 +595,16 @@ protected: wxSize vrulerSize; protected: - std::weak_ptr mLastCell; - std::vector mTargets; - size_t mTarget {}; - unsigned mMouseOverUpdateFlags{}; - - public: - UIHandlePtr Target() - { - if (mTargets.size()) - return mTargets[mTarget]; - else - return {}; - } - - protected: - void ClearTargets() - { - // Forget the rotation of hit test candidates when the mouse moves from - // cell to cell or outside of the TrackPanel entirely. - mLastCell.reset(); - mTargets.clear(); - mTarget = 0; - mMouseOverUpdateFlags = 0; - } - - bool HasRotation(); - bool HasEscape(); - - bool ChangeTarget(bool forward, bool cycle); - - std::weak_ptr mpClickedCell; - UIHandlePtr mUIHandle; std::shared_ptr mpBackground; - bool mEnableTab{}; - DECLARE_EVENT_TABLE() void ProcessUIHandleResult (TrackPanelCell *pClickedCell, TrackPanelCell *pLatestCell, - unsigned refreshResult); + unsigned refreshResult) override; + + void UpdateStatusMessage( const wxString &status ) override; // friending GetInfoCommand allow automation to get sizes of the // tracks, track control panel and such. From c08c17eafacbf68f76488708725bb87623389cf4 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 26 Jun 2018 23:36:34 -0400 Subject: [PATCH 06/12] Add a method to find the rectangle of a given cell... ... Cells are not responsible to remember that; the cells' container determines that. --- src/TrackPanel.cpp | 14 ++++++++++++++ src/TrackPanel.h | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index 837577fd6..0d608344b 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -2829,6 +2829,20 @@ auto TrackPanel::FindCell(int mouseX, int mouseY) -> FoundCell }; } +wxRect TrackPanel::FindRect( const TrackPanelCell &cell ) +{ + auto range = Cells(); + auto end = range.second, + iter = std::find_if( range.first, end, + [&]( const decltype(*end) &pair ) + { return pair.first.get() == &cell; } + ); + if (iter == end) + return {}; + else + return (*iter).second; +} + // This finds the rectangle of a given track (including all channels), // either that of the label 'adornment' or the track itself // The given track is assumed to be the first channel diff --git a/src/TrackPanel.h b/src/TrackPanel.h index 0afaab43f..78b264cae 100644 --- a/src/TrackPanel.h +++ b/src/TrackPanel.h @@ -268,6 +268,7 @@ public: wxRect rect; }; virtual FoundCell FindCell(int mouseX, int mouseY) = 0; + virtual wxRect FindRect(const TrackPanelCell &cell) = 0; virtual TrackPanelCell *GetFocusedCell() = 0; virtual void SetFocusedCell() = 0; @@ -467,6 +468,9 @@ protected: // Find track info by coordinate FoundCell FindCell(int mouseX, int mouseY) override; + // Find rectangle of the given cell + wxRect FindRect(const TrackPanelCell &cell) override; + int GetVRulerWidth() const; int GetVRulerOffset() const { return mTrackInfo.GetTrackInfoWidth(); } From 7f84e713247a7ad287b6160f4d78fc2bed03cbf8 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 26 Jun 2018 22:43:04 -0400 Subject: [PATCH 07/12] Context menu handling is also in CellularPanel --- src/Track.h | 4 ++++ src/TrackPanel.cpp | 30 ++++++++++++++++++++---------- src/TrackPanel.h | 5 +++-- src/TrackPanelCell.h | 4 ++++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Track.h b/src/Track.h index f04220309..b56629b7a 100644 --- a/src/Track.h +++ b/src/Track.h @@ -165,6 +165,10 @@ class AUDACITY_DLL_API Track /* not final */ (const TrackPanelMouseState &, const AudacityProject *pProject) final override; + // Delegates the handling to the related TCP cell + std::shared_ptr ContextMenuDelegate() override + { return GetTrackControl(); } + public: // Rather override this for subclasses: diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index 0d608344b..dc75ae388 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -263,6 +263,7 @@ BEGIN_EVENT_TABLE(CellularPanel, OverlayPanel) EVT_CHAR(CellularPanel::OnChar) EVT_SET_FOCUS(CellularPanel::OnSetFocus) EVT_KILL_FOCUS(CellularPanel::OnKillFocus) + EVT_CONTEXT_MENU(CellularPanel::OnContextMenu) END_EVENT_TABLE() BEGIN_EVENT_TABLE(TrackPanel, CellularPanel) @@ -270,7 +271,6 @@ BEGIN_EVENT_TABLE(TrackPanel, CellularPanel) EVT_KEY_DOWN(TrackPanel::OnKeyDown) EVT_PAINT(TrackPanel::OnPaint) - EVT_CONTEXT_MENU(TrackPanel::OnContextMenu) EVT_TIMER(wxID_ANY, TrackPanel::OnTimer) END_EVENT_TABLE() @@ -1167,9 +1167,9 @@ void TrackPanel::OnTrackListDeletion(wxCommandEvent & e) e.Skip(); } -void TrackPanel::OnContextMenu(wxContextMenuEvent & WXUNUSED(event)) +void CellularPanel::OnContextMenu(wxContextMenuEvent & WXUNUSED(event)) { - OnTrackMenu(); + DoContextMenu(); } struct TrackInfo::TCPLine { @@ -2625,17 +2625,27 @@ void TrackPanel::ScrollIntoView(int x) void TrackPanel::OnTrackMenu(Track *t) { - if(!t) { - t = GetFocusedTrack(); - if(!t) + CellularPanel::DoContextMenu( t ); +} + +void CellularPanel::DoContextMenu( TrackPanelCell *pCell ) +{ + if( !pCell ) { + pCell = GetFocusedCell(); + if( !pCell ) return; } - const auto pCell = t->GetTrackControl(); - const wxRect rect(FindTrackRect(t, true)); + const auto delegate = pCell->ContextMenuDelegate(); + if (!delegate) + return; + + auto rect = FindRect( *delegate ); const UIHandle::Result refreshResult = - pCell->DoContextMenu(rect, this, NULL); - ProcessUIHandleResult(t, t, refreshResult); + delegate->DoContextMenu(rect, this, NULL); + + // To do: use safer shared_ptr to pCell + ProcessUIHandleResult(pCell, pCell, refreshResult); } Track * TrackPanel::GetFirstSelectedTrack() diff --git a/src/TrackPanel.h b/src/TrackPanel.h index 78b264cae..6a3c00cff 100644 --- a/src/TrackPanel.h +++ b/src/TrackPanel.h @@ -305,6 +305,7 @@ protected: mTarget = 0; mMouseOverUpdateFlags = 0; } + void DoContextMenu( TrackPanelCell *pCell = nullptr ); private: bool HasRotation(); @@ -320,6 +321,8 @@ private: void OnSetFocus(wxFocusEvent & event); void OnKillFocus(wxFocusEvent & event); + void OnContextMenu(wxContextMenuEvent & event); + void HandleInterruptedDrag(); void Uncapture( wxMouseState *pState = nullptr ); bool HandleEscapeKey(bool down); @@ -383,8 +386,6 @@ class AUDACITY_DLL_API TrackPanel final : public CellularPanel { void OnMouseEvent(wxMouseEvent & event); void OnKeyDown(wxKeyEvent & event); - void OnContextMenu(wxContextMenuEvent & event); - void OnPlayback(wxCommandEvent &); void OnTrackListResizing(wxCommandEvent & event); void OnTrackListDeletion(wxCommandEvent & event); diff --git a/src/TrackPanelCell.h b/src/TrackPanelCell.h index c5bf1ddd1..23d3af107 100644 --- a/src/TrackPanelCell.h +++ b/src/TrackPanelCell.h @@ -56,6 +56,10 @@ public: (const TrackPanelMouseEvent &event, AudacityProject *pProject); + // A cell may delegate context menu handling to another one + virtual std::shared_ptr ContextMenuDelegate() + { return {}; } + // The pPosition parameter indicates mouse position but may be NULL // Return value is a bitwise OR of RefreshCode values // Default implementation does nothing From f6609e5ffbff1fed28a0aab5d291fadc34760659 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sat, 4 Aug 2018 13:06:55 -0400 Subject: [PATCH 08/12] Move class CellularPanel into its own files --- mac/Audacity.xcodeproj/project.pbxproj | 6 + src/CellularPanel.cpp | 784 ++++++++++++++++++ src/CellularPanel.h | 148 ++++ src/Makefile.am | 2 + src/Makefile.in | 113 ++- src/TrackPanel.cpp | 750 ----------------- src/TrackPanel.h | 133 +-- win/Projects/Audacity/Audacity.vcxproj | 2 + .../Audacity/Audacity.vcxproj.filters | 6 + 9 files changed, 1023 insertions(+), 921 deletions(-) create mode 100644 src/CellularPanel.cpp create mode 100644 src/CellularPanel.h diff --git a/mac/Audacity.xcodeproj/project.pbxproj b/mac/Audacity.xcodeproj/project.pbxproj index 63532e291..baf0e385e 100644 --- a/mac/Audacity.xcodeproj/project.pbxproj +++ b/mac/Audacity.xcodeproj/project.pbxproj @@ -1206,6 +1206,7 @@ 5E07842E1DEE6B8600CA76EA /* FileException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E07842C1DEE6B8600CA76EA /* FileException.cpp */; }; 5E0784311DF1E4F400CA76EA /* UserException.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E07842F1DF1E4F400CA76EA /* UserException.cpp */; }; 5E0A0E311D23019A00CD2567 /* MenusMac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E0A0E301D23019A00CD2567 /* MenusMac.cpp */; }; + 5E0A1CDD20E95FF7001AAF8D /* CellularPanel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E0A1CDB20E95FF7001AAF8D /* CellularPanel.cpp */; }; 5E10D9061EC8F81300B3AC57 /* PlayableTrackButtonHandles.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E10D9041EC8F81300B3AC57 /* PlayableTrackButtonHandles.cpp */; }; 5E15123D1DB000C000702E29 /* UIHandle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E15123B1DB000C000702E29 /* UIHandle.cpp */; }; 5E15125A1DB000DC00702E29 /* LabelTrackControls.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E1512401DB000DC00702E29 /* LabelTrackControls.cpp */; }; @@ -3053,6 +3054,8 @@ 5E07842F1DF1E4F400CA76EA /* UserException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UserException.cpp; sourceTree = ""; }; 5E0784301DF1E4F400CA76EA /* UserException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UserException.h; sourceTree = ""; }; 5E0A0E301D23019A00CD2567 /* MenusMac.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = MenusMac.cpp; sourceTree = ""; }; + 5E0A1CDB20E95FF7001AAF8D /* CellularPanel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CellularPanel.cpp; sourceTree = ""; }; + 5E0A1CDC20E95FF7001AAF8D /* CellularPanel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CellularPanel.h; sourceTree = ""; }; 5E10D9041EC8F81300B3AC57 /* PlayableTrackButtonHandles.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PlayableTrackButtonHandles.cpp; sourceTree = ""; }; 5E10D9051EC8F81300B3AC57 /* PlayableTrackButtonHandles.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlayableTrackButtonHandles.h; sourceTree = ""; }; 5E1512381DB000C000702E29 /* HitTestResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HitTestResult.h; sourceTree = ""; }; @@ -4053,6 +4056,7 @@ 1790AFD809883BFD008A330A /* BatchProcessDialog.cpp */, 1790AFDA09883BFD008A330A /* Benchmark.cpp */, 1790AFE809883BFD008A330A /* BlockFile.cpp */, + 5E0A1CDB20E95FF7001AAF8D /* CellularPanel.cpp */, 1790AFF409883BFD008A330A /* CrossFade.cpp */, 2849B4600A7444BE00ECF12D /* Dependencies.cpp */, 28D000A31A32920C00367B21 /* DeviceChange.cpp */, @@ -4147,6 +4151,7 @@ 1790AFD909883BFD008A330A /* BatchProcessDialog.h */, 1790AFDB09883BFD008A330A /* Benchmark.h */, 1790AFE909883BFD008A330A /* BlockFile.h */, + 5E0A1CDC20E95FF7001AAF8D /* CellularPanel.h */, 1790AFF009883BFD008A330A /* configtemplate.h */, 1790AFF509883BFD008A330A /* CrossFade.h */, 2849B4610A7444BE00ECF12D /* Dependencies.h */, @@ -7729,6 +7734,7 @@ 1790B11E09883BFD008A330A /* BatchCommands.cpp in Sources */, 5ED1D0B11CDE560C00471E3C /* BackedPanel.cpp in Sources */, 1790B11F09883BFD008A330A /* BatchProcessDialog.cpp in Sources */, + 5E0A1CDD20E95FF7001AAF8D /* CellularPanel.cpp in Sources */, 1790B12009883BFD008A330A /* Benchmark.cpp in Sources */, 1790B12109883BFD008A330A /* LegacyAliasBlockFile.cpp in Sources */, 1790B12209883BFD008A330A /* LegacyBlockFile.cpp in Sources */, diff --git a/src/CellularPanel.cpp b/src/CellularPanel.cpp new file mode 100644 index 000000000..16e513044 --- /dev/null +++ b/src/CellularPanel.cpp @@ -0,0 +1,784 @@ +/********************************************************************** + + Audacity: A Digital Audio Editor + + CellularPanel.cpp + + Dominic Mazzoni + and lots of other contributors + + Implements CellularPanel. + +********************************************************************//*! + +\class CellularPanel +\brief + + Formerly part of TrackPanel, this abstract base class has no special knowledge + of Track objects and is intended for reuse with other windows. + + Manages a division of a panel's area into disjoint rectangles, each with + an associated Cell object. Details of that partition and association, and + the choice of the cell with keyboard focus, are subclass responsibilities. + + Handling of keyboard events is delegated to the focused cell. The cell under + the mouse position is queried for hit-test candidate objects, which handle + click-drag-release (and ESC key abort) sequences. + +*//*****************************************************************/ + +#include "Audacity.h" +#include "CellularPanel.h" +#include "Project.h" +#include "UIHandle.h" +#include "TrackPanelMouseEvent.h" +#include "HitTestResult.h" +#include "RefreshCode.h" + + +BEGIN_EVENT_TABLE(CellularPanel, OverlayPanel) + EVT_MOUSE_EVENTS(CellularPanel::OnMouseEvent) + EVT_MOUSE_CAPTURE_LOST(CellularPanel::OnCaptureLost) + EVT_COMMAND(wxID_ANY, EVT_CAPTURE_KEY, CellularPanel::OnCaptureKey) + EVT_KEY_DOWN(CellularPanel::OnKeyDown) + EVT_KEY_UP(CellularPanel::OnKeyUp) + EVT_CHAR(CellularPanel::OnChar) + EVT_SET_FOCUS(CellularPanel::OnSetFocus) + EVT_KILL_FOCUS(CellularPanel::OnKillFocus) + EVT_CONTEXT_MENU(CellularPanel::OnContextMenu) +END_EVENT_TABLE() + +void CellularPanel::HandleInterruptedDrag() +{ + if (mUIHandle && mUIHandle->StopsOnKeystroke() ) { + // The bogus id isn't used anywhere, but may help with debugging. + // as this is sending a bogus mouse up. The mouse button is still actually down + // and may go up again. + const int idBogusUp = 2; + wxMouseEvent evt { wxEVT_LEFT_UP }; + evt.SetId( idBogusUp ); + evt.SetPosition(this->ScreenToClient(::wxGetMousePosition())); + this->ProcessEvent(evt); + } +} + +void CellularPanel::Uncapture(wxMouseState *pState) +{ + auto state = ::wxGetMouseState(); + if (!pState) { + // Remap the position + state.SetPosition(this->ScreenToClient(state.GetPosition())); + pState = &state; + } + + if (HasCapture()) + ReleaseMouse(); + HandleMotion( *pState ); +} + +bool CellularPanel::CancelDragging() +{ + if (mUIHandle) { + // copy shared_ptr for safety, as in HandleClick + auto handle = mUIHandle; + // UIHANDLE CANCEL + UIHandle::Result refreshResult = handle->Cancel(GetProject()); + auto pClickedCell = mpClickedCell.lock(); + if (pClickedCell) + ProcessUIHandleResult( + pClickedCell.get(), {}, + refreshResult | mMouseOverUpdateFlags ); + mpClickedCell.reset(); + mUIHandle.reset(), handle.reset(), ClearTargets(); + Uncapture(); + return true; + } + return false; +} + +bool CellularPanel::HandleEscapeKey(bool down) +{ + if (!down) + return false; + + { + auto target = Target(); + if (target && target->HasEscape() && target->Escape()) { + HandleCursorForPresentMouseState(false); + return true; + } + } + + if (mUIHandle) { + CancelDragging(); + return true; + } + + if (ChangeTarget(true, false)) { + HandleCursorForPresentMouseState(false); + return true; + } + + return false; +} + +void CellularPanel::UpdateMouseState(const wxMouseState &state) +{ + mLastMouseState = state; + + // Simulate a down button if none, so hit test routines can anticipate + // which button will be clicked + if (!state.ButtonIsDown(wxMOUSE_BTN_ANY)) { +#ifdef __WXOSX__ + if (state.RawControlDown()) + // On Mac we can distinctly anticipate "right" click (as Control+click) + mLastMouseState.SetRightDown( true ), + mLastMouseState.SetLeftDown( false ); + else +#endif + // Anticipate a left click by default + mLastMouseState.SetRightDown( false ), + mLastMouseState.SetLeftDown( true ); + } +} + +void CellularPanel::HandleModifierKey() +{ + HandleCursorForPresentMouseState(); +} + +void CellularPanel::HandleCursorForPresentMouseState(bool doHit) +{ + // Come here on modifier key or mouse button transitions, + // or on starting or stopping of play or record, + // or change of toolbar button, + // and change the cursor appropriately. + + // Get the button and key states + auto state = ::wxGetMouseState(); + // Remap the position + state.SetPosition(this->ScreenToClient(state.GetPosition())); + + HandleMotion( state, doHit ); +} + +/// CellularPanel::HandleMotion( ) sets the cursor drawn at the mouse location, +/// and updates the status bar message. +/// We treat certain other changes of mouse button and key state as "motions" +/// too, and also starting and stopping of playback or recording, all of which +/// may cause the appropriate cursor and message to change. +/// As this procedure checks which region the mouse is over, it is +/// appropriate to establish the message in the status bar. +void CellularPanel::HandleMotion( wxMouseState &inState, bool doHit ) +{ + UpdateMouseState( inState ); + + const auto foundCell = FindCell( inState.m_x, inState.m_y ); + auto &rect = foundCell.rect; + auto &pCell = foundCell.pCell; + const TrackPanelMouseState tpmState{ mLastMouseState, rect, pCell }; + HandleMotion( tpmState, doHit ); +} + +void CellularPanel::HandleMotion +( const TrackPanelMouseState &tpmState, bool doHit ) +{ + auto handle = mUIHandle; + + auto newCell = tpmState.pCell; + + wxString status{}, tooltip{}; + wxCursor *pCursor{}; + unsigned refreshCode = 0; + + if ( ! doHit ) { + // Dragging or not + handle = Target(); + + // Assume cell does not change but target does + refreshCode = mMouseOverUpdateFlags; + mMouseOverUpdateFlags = 0; + } + else if ( !mUIHandle ) { + // Not yet dragging. + + auto oldCell = mLastCell.lock(); + + unsigned updateFlags = mMouseOverUpdateFlags; + + // First check whether crossing cell to cell + if ( newCell == oldCell ) + oldCell.reset(); + else { + // Forget old targets + ClearTargets(); + // Re-draw any highlighting + if (oldCell) { + ProcessUIHandleResult( + oldCell.get(), oldCell.get(), updateFlags); + } + } + + auto oldHandle = Target(); + auto oldPosition = mTarget; + + // Now do the + // UIHANDLE HIT TEST ! + mTargets = newCell->HitTest(tpmState, GetProject()); + + mTarget = 0; + + // Find the old target's NEW place if we can + if (oldHandle) { + auto begin = mTargets.begin(), end = mTargets.end(), + iter = std::find(begin, end, oldHandle); + if (iter != end) { + size_t newPosition = iter - begin; + if (newPosition <= oldPosition) + mTarget = newPosition; + // else, some NEW hit and this position takes priority + } + } + + handle = Target(); + + mLastCell = newCell; + + if (!oldCell && oldHandle != handle) + // Did not move cell to cell, but did change the target + refreshCode = updateFlags; + + if (handle && handle != oldHandle) + handle->Enter(true); + } + + // UIHANDLE PREVIEW + // Update status message and cursor, whether dragging or not + if (handle) { + auto preview = handle->Preview( tpmState, GetProject() ); + status = preview.message; + tooltip = preview.tooltip; + pCursor = preview.cursor; + auto code = handle->GetChangeHighlight(); + handle->SetChangeHighlight(RefreshCode::RefreshNone); + refreshCode |= code; + mMouseOverUpdateFlags |= code; + } + if (newCell && + (!pCursor || status.empty() || tooltip.empty())) { + // Defaulting of cursor, tooltip, and status if there is no handle, + // or if the handle does not specify them + const auto preview = newCell->DefaultPreview( tpmState, GetProject() ); + if (!pCursor) + pCursor = preview.cursor; + if (status.empty()) + status = preview.message; + if (tooltip.empty()) + tooltip = preview.tooltip; + } + if (!pCursor) { + // Ultimate default cursor + static wxCursor defaultCursor{ wxCURSOR_DEFAULT }; + pCursor = &defaultCursor; + } + + UpdateStatusMessage(status); + +#if wxUSE_TOOLTIPS + if (tooltip != GetToolTipText()) { + // Unset first, by analogy with AButton + UnsetToolTip(); + SetToolTip(tooltip); + } +#endif + + if (pCursor) + SetCursor( *pCursor ); + + ProcessUIHandleResult( + newCell.get(), newCell.get(), refreshCode); +} + +bool CellularPanel::HasRotation() +{ + // Is there a nontrivial TAB key rotation? + if ( mTargets.size() > 1 ) + return true; + auto target = Target(); + return target && target->HasRotation(); +} + +bool CellularPanel::HasEscape() +{ + if (IsMouseCaptured()) + return true; + + if (mTarget + 1 == mTargets.size() && + Target() && + !Target()->HasEscape()) + return false; + + return mTargets.size() > 0; +} + +bool CellularPanel::ChangeTarget(bool forward, bool cycle) +{ + auto size = mTargets.size(); + + auto target = Target(); + if (target && target->HasRotation()) { + if(target->Rotate(forward)) + return true; + else if (cycle && (size == 1 || IsMouseCaptured())) { + // Rotate through the states of this target only. + target->Enter(forward); + return true; + } + } + + if (!cycle && + ((forward && mTarget + 1 == size) || + (!forward && mTarget == 0))) + return false; + + if (size > 1) { + if (forward) + ++mTarget; + else + mTarget += size - 1; + mTarget %= size; + if (Target()) + Target()->Enter(forward); + return true; + } + + return false; +} + +/// Determines if a modal tool is active +bool CellularPanel::IsMouseCaptured() +{ + return mUIHandle != NULL; +} + +void CellularPanel::OnContextMenu(wxContextMenuEvent & WXUNUSED(event)) +{ + DoContextMenu(); +} + +/// Handle mouse wheel rotation (for zoom in/out, vertical and horizontal scrolling) +void CellularPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ) +{ + auto pCell = tpmEvent.pCell; + if (!pCell) + return; + + auto &event = tpmEvent.event; + double steps {}; +#if defined(__WXMAC__) && defined(EVT_MAGNIFY) + // PRL: + // Pinch and spread implemented in wxWidgets 3.1.0, or cherry-picked from + // the future in custom build of 3.0.2 + if (event.Magnify()) { + event.SetControlDown(true); + steps = 2 * event.GetMagnification(); + } + else +#endif + { + steps = event.m_wheelRotation / + (event.m_wheelDelta > 0 ? (double)event.m_wheelDelta : 120.0); + } + + if(event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL) { + // Two-fingered horizontal swipe on mac is treated like shift-mousewheel + event.SetShiftDown(true); + // This makes the wave move in the same direction as the fingers, and the scrollbar + // thumb moves oppositely + steps *= -1; + } + + tpmEvent.steps = steps; + + if(!event.HasAnyModifiers()) { + // We will later un-skip if we do anything, but if we don't, + // propagate the event up for the sake of the scrubber + event.Skip(); + event.ResumePropagation(wxEVENT_PROPAGATE_MAX); + } + + unsigned result = + pCell->HandleWheelRotation( tpmEvent, GetProject() ); + ProcessUIHandleResult( + pCell.get(), pCell.get(), result); +} + +void CellularPanel::OnCaptureKey(wxCommandEvent & event) +{ + mEnableTab = false; + wxKeyEvent *kevent = static_cast(event.GetEventObject()); + const auto code = kevent->GetKeyCode(); + if ( WXK_ESCAPE != code ) + HandleInterruptedDrag(); + + // Give focused cell precedence + const auto t = GetFocusedCell(); + if (t) { + const unsigned refreshResult = + t->CaptureKey(*kevent, *mViewInfo, this); + ProcessUIHandleResult(t, t, refreshResult); + event.Skip(kevent->GetSkipped()); + } + +#if 0 + // Special TAB key handling, but only if the cell didn't capture it + if ( !(t && !kevent->GetSkipped()) && + WXK_TAB == code && HasRotation() ) { + // Override TAB navigation in wxWidgets, by not skipping + event.Skip(false); + mEnableTab = true; + return; + } + else +#endif + if (!t) + event.Skip(); +} + +void CellularPanel::OnKeyDown(wxKeyEvent & event) +{ + switch (event.GetKeyCode()) + { + case WXK_ESCAPE: + if(HandleEscapeKey(true)) + // Don't skip the event, eat it so that + // AudacityApp does not also stop any playback. + return; + else + break; + + case WXK_ALT: + case WXK_SHIFT: + case WXK_CONTROL: +#ifdef __WXOSX__ + case WXK_RAW_CONTROL: +#endif + HandleModifierKey(); + break; + +#if 0 + case WXK_TAB: + if ( mEnableTab && HasRotation() ) { + ChangeTarget( !event.ShiftDown(), true ); + HandleCursorForPresentMouseState(false); + return; + } + else + break; +#endif + } + + const auto t = GetFocusedCell(); + + if (t) { + const unsigned refreshResult = + t->KeyDown(event, *mViewInfo, this); + ProcessUIHandleResult(t, t, refreshResult); + } + else + event.Skip(); +} + +void CellularPanel::OnChar(wxKeyEvent & event) +{ + switch (event.GetKeyCode()) + { + case WXK_ESCAPE: + case WXK_ALT: + case WXK_SHIFT: + case WXK_CONTROL: + case WXK_PAGEUP: + case WXK_PAGEDOWN: + return; + } + + const auto t = GetFocusedCell(); + if (t) { + const unsigned refreshResult = + t->Char(event, *mViewInfo, this); + ProcessUIHandleResult(t, t, refreshResult); + } + else + event.Skip(); +} + +void CellularPanel::OnKeyUp(wxKeyEvent & event) +{ + bool didSomething = false; + switch (event.GetKeyCode()) + { + case WXK_ESCAPE: + didSomething = HandleEscapeKey(false); + break; + + case WXK_ALT: + case WXK_SHIFT: + case WXK_CONTROL: +#ifdef __WXOSX__ + case WXK_RAW_CONTROL: +#endif + HandleModifierKey(); + break; + } + + if (didSomething) + return; + + const auto t = GetFocusedCell(); + if (t) { + const unsigned refreshResult = + t->KeyUp(event, *mViewInfo, this); + ProcessUIHandleResult(t, t, refreshResult); + return; + } + + event.Skip(); +} + +/// Should handle the case when the mouse capture is lost. +void CellularPanel::OnCaptureLost(wxMouseCaptureLostEvent & WXUNUSED(event)) +{ + ClearTargets(); + + // This is bad. We are lying abou the event by saying it is a mouse up. + wxMouseEvent e(wxEVT_LEFT_UP); + e.SetId( kCaptureLostEventId ); + + e.m_x = mMouseMostRecentX; + e.m_y = mMouseMostRecentY; + + OnMouseEvent(e); +} + +/// This handles just generic mouse events. Then, based +/// on our current state, we forward the mouse events to +/// various interested parties. +void CellularPanel::OnMouseEvent(wxMouseEvent & event) +try +{ + const auto foundCell = FindCell( event.m_x, event.m_y ); + auto &rect = foundCell.rect; + auto &pCell = foundCell.pCell; + + const auto size = GetSize(); + TrackPanelMouseEvent tpmEvent{ event, rect, size, pCell }; + +#if defined(__WXMAC__) && defined(EVT_MAGNIFY) + // PRL: + // Pinch and spread implemented in wxWidgets 3.1.0, or cherry-picked from + // the future in custom build of 3.0.2 + if (event.Magnify()) { + HandleWheelRotation( tpmEvent ); + } +#endif + + // If a mouse event originates from a keyboard context menu event then + // event.GetPosition() == wxDefaultPosition. wxContextMenu events are handled in + // CellularPanel::OnContextMenu(), and therefore associated mouse events are ignored here. + // Not ignoring them was causing bug 613: the mouse events were interpreted as clicking + // outside the tracks. + if (event.GetPosition() == wxDefaultPosition && (event.RightDown() || event.RightUp())) { + event.Skip(); + return; + } + + if (event.m_wheelRotation != 0) + HandleWheelRotation( tpmEvent ); + + if (event.LeftDown() || event.LeftIsDown() || event.Moving()) { + // Skip, even if we do something, so that the left click or drag + // may have an additional effect in the scrubber. + event.Skip(); + event.ResumePropagation(wxEVENT_PROPAGATE_MAX); + } + + mMouseMostRecentX = event.m_x; + mMouseMostRecentY = event.m_y; + + if (event.LeftDown()) { + // The activate event is used to make the + // parent window 'come alive' if it didn't have focus. + wxActivateEvent e; + GetParent()->GetEventHandler()->ProcessEvent(e); + } + + if (event.ButtonDown()) { + SetFocus(); + } + + if (event.Leaving()) + { + if ( !mUIHandle ) + ClearTargets(); + + auto buttons = + // Bug 1325: button state in Leaving events is unreliable on Mac. + // Poll the global state instead. + // event.ButtonIsDown(wxMOUSE_BTN_ANY); + ::wxGetMouseState().ButtonIsDown(wxMOUSE_BTN_ANY); + + if(!buttons) { + CancelDragging(); + +#if defined(__WXMAC__) + + // We must install the cursor ourselves since the window under + // the mouse is no longer this one and wx2.8.12 makes that check. + // Should re-evaluate with wx3. + wxSTANDARD_CURSOR->MacInstall(); +#endif + } + } + + if (mUIHandle) { + auto pClickedCell = mpClickedCell.lock(); + if (event.Dragging()) { + // UIHANDLE DRAG + // copy shared_ptr for safety, as in HandleClick + auto handle = mUIHandle; + const UIHandle::Result refreshResult = + handle->Drag( tpmEvent, GetProject() ); + ProcessUIHandleResult + (pClickedCell.get(), pCell.get(), refreshResult); + mMouseOverUpdateFlags |= refreshResult; + if (refreshResult & RefreshCode::Cancelled) { + // Drag decided to abort itself + mUIHandle.reset(), handle.reset(), ClearTargets(); + mpClickedCell.reset(); + Uncapture( &event ); + } + else { + UpdateMouseState(event); + TrackPanelMouseState tpmState{ mLastMouseState, rect, pCell }; + HandleMotion( tpmState ); + } + } + else if (event.ButtonUp()) { + // UIHANDLE RELEASE + unsigned moreFlags = mMouseOverUpdateFlags; + UIHandle::Result refreshResult = + mUIHandle->Release( tpmEvent, GetProject(), this ); + ProcessUIHandleResult + (pClickedCell.get(), pCell.get(), + refreshResult | moreFlags); + mUIHandle.reset(), ClearTargets(); + mpClickedCell.reset(); + // will also Uncapture() below + } + } + else if ( event.GetEventType() == wxEVT_MOTION ) + // Update status message and cursor, not during drag + // consider it not a drag, even if button is down during motion, if + // mUIHandle is null, as it becomes during interrupted drag + // (e.g. by hitting space to play while dragging an envelope point) + HandleMotion( event ); + else if ( event.ButtonDown() || event.ButtonDClick() ) + HandleClick( tpmEvent ); + + if (event.ButtonDown() && IsMouseCaptured()) { + if (!HasCapture()) + CaptureMouse(); + } + + if (event.ButtonUp()) + Uncapture(); +} +catch( ... ) +{ + // Abort any dragging, as if by hitting Esc + if ( CancelDragging() ) + ; + else { + Uncapture(); + Refresh(false); + } + throw; +} + +void CellularPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) +{ + auto pCell = tpmEvent.pCell; + + // Do hit test once more, in case the button really pressed was not the + // one "anticipated." + { + TrackPanelMouseState tpmState{ + tpmEvent.event, + tpmEvent.rect, + tpmEvent.pCell + }; + HandleMotion( tpmState ); + } + + mUIHandle = Target(); + + if (mUIHandle) { + // UIHANDLE CLICK + // Make another shared pointer to the handle, in case recursive + // event dispatching otherwise tries to delete the handle. + auto handle = mUIHandle; + UIHandle::Result refreshResult = + handle->Click( tpmEvent, GetProject() ); + if (refreshResult & RefreshCode::Cancelled) + mUIHandle.reset(), handle.reset(), ClearTargets(); + else { + mpClickedCell = pCell; + + // Perhaps the clicked handle wants to update cursor and state message + // after a click. + TrackPanelMouseState tpmState{ + tpmEvent.event, + tpmEvent.rect, + tpmEvent.pCell + }; + HandleMotion( tpmState ); + } + ProcessUIHandleResult( + pCell.get(), pCell.get(), refreshResult); + mMouseOverUpdateFlags |= refreshResult; + } +} + +void CellularPanel::DoContextMenu( TrackPanelCell *pCell ) +{ + if( !pCell ) { + pCell = GetFocusedCell(); + if( !pCell ) + return; + } + + const auto delegate = pCell->ContextMenuDelegate(); + if (!delegate) + return; + + auto rect = FindRect( *delegate ); + const UIHandle::Result refreshResult = + delegate->DoContextMenu(rect, this, NULL); + + // To do: use safer shared_ptr to pCell + ProcessUIHandleResult(pCell, pCell, refreshResult); +} + +void CellularPanel::OnSetFocus(wxFocusEvent & WXUNUSED(event)) +{ + SetFocusedCell(); +} + +void CellularPanel::OnKillFocus(wxFocusEvent & WXUNUSED(event)) +{ + if (AudacityProject::HasKeyboardCapture(this)) + { + AudacityProject::ReleaseKeyboard(this); + } + Refresh( false); +} diff --git a/src/CellularPanel.h b/src/CellularPanel.h new file mode 100644 index 000000000..a576b823e --- /dev/null +++ b/src/CellularPanel.h @@ -0,0 +1,148 @@ +/********************************************************************** + + Audacity: A Digital Audio Editor + + TrackPanel.h + + Paul Licameli + + **********************************************************************/ + +#ifndef __AUDACITY_CELLULAR_PANEL__ +#define __AUDACITY_CELLULAR_PANEL__ + +#include +#include "widgets/OverlayPanel.h" + +class ViewInfo; +class AudacityProject; + +class TrackPanelCell; +struct TrackPanelMouseEvent; +struct TrackPanelMouseState; + +class UIHandle; +using UIHandlePtr = std::shared_ptr; + +// This class manages a panel divided into a number of sub-rectangles called +// cells, that each implement hit tests returning click-drag-release handler +// objects, and other services. +// It has no dependency on the Track class. +class AUDACITY_DLL_API CellularPanel : public OverlayPanel { +public: + CellularPanel(wxWindow * parent, wxWindowID id, + const wxPoint & pos, + const wxSize & size, + ViewInfo *viewInfo, + // default as for wxPanel: + long style = wxTAB_TRAVERSAL | wxNO_BORDER) + : OverlayPanel(parent, id, pos, size, style) + , mViewInfo( viewInfo ) + {} + + // Overridables: + + virtual AudacityProject *GetProject() const = 0; + + // Find track info by coordinate + struct FoundCell { + std::shared_ptr pCell; + wxRect rect; + }; + virtual FoundCell FindCell(int mouseX, int mouseY) = 0; + virtual wxRect FindRect(const TrackPanelCell &cell) = 0; + virtual TrackPanelCell *GetFocusedCell() = 0; + virtual void SetFocusedCell() = 0; + + virtual void ProcessUIHandleResult + (TrackPanelCell *pClickedCell, TrackPanelCell *pLatestCell, + unsigned refreshResult) = 0; + + virtual void UpdateStatusMessage( const wxString & ) = 0; + +public: + UIHandlePtr Target() + { + if (mTargets.size()) + return mTargets[mTarget]; + else + return {}; + } + + bool IsMouseCaptured(); + + wxCoord MostRecentXCoord() const { return mMouseMostRecentX; } + + void HandleCursorForPresentMouseState(bool doHit = true); + +protected: + bool HasEscape(); + bool CancelDragging(); + void DoContextMenu( TrackPanelCell *pCell = nullptr ); + void ClearTargets() + { + // Forget the rotation of hit test candidates when the mouse moves from + // cell to cell or outside of the panel entirely. + mLastCell.reset(); + mTargets.clear(); + mTarget = 0; + mMouseOverUpdateFlags = 0; + } + +private: + bool HasRotation(); + bool ChangeTarget(bool forward, bool cycle); + + void OnMouseEvent(wxMouseEvent & event); + void OnCaptureLost(wxMouseCaptureLostEvent & event); + void OnCaptureKey(wxCommandEvent & event); + void OnKeyDown(wxKeyEvent & event); + void OnChar(wxKeyEvent & event); + void OnKeyUp(wxKeyEvent & event); + + void OnSetFocus(wxFocusEvent & event); + void OnKillFocus(wxFocusEvent & event); + + void OnContextMenu(wxContextMenuEvent & event); + + void HandleInterruptedDrag(); + void Uncapture( wxMouseState *pState = nullptr ); + bool HandleEscapeKey(bool down); + void UpdateMouseState(const wxMouseState &state); + void HandleModifierKey(); + + void HandleClick( const TrackPanelMouseEvent &tpmEvent ); + void HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ); + + void HandleMotion( wxMouseState &state, bool doHit = true ); + void HandleMotion + ( const TrackPanelMouseState &tpmState, bool doHit = true ); + + +protected: + ViewInfo *mViewInfo; + +private: + UIHandlePtr mUIHandle; + + std::weak_ptr mLastCell; + std::vector mTargets; + size_t mTarget {}; + unsigned mMouseOverUpdateFlags{}; + +protected: + // To do: make a drawing method and make this private + wxMouseState mLastMouseState; + +private: + int mMouseMostRecentX; + int mMouseMostRecentY; + + std::weak_ptr mpClickedCell; + + bool mEnableTab{}; + + DECLARE_EVENT_TABLE() +}; + +#endif diff --git a/src/Makefile.am b/src/Makefile.am index e61be0e4e..37d567cab 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -121,6 +121,8 @@ audacity_SOURCES = \ BatchProcessDialog.h \ Benchmark.cpp \ Benchmark.h \ + CellularPanel.cpp \ + CellularPanel.h \ Dependencies.cpp \ Dependencies.h \ DeviceChange.cpp \ diff --git a/src/Makefile.in b/src/Makefile.in index d28e7abc1..501bf8237 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -1,7 +1,7 @@ -# Makefile.in generated by automake 1.14.1 from Makefile.am. +# Makefile.in generated by automake 1.15 from Makefile.am. # @configure_input@ -# Copyright (C) 1994-2013 Free Software Foundation, Inc. +# Copyright (C) 1994-2014 Free Software Foundation, Inc. # This Makefile.in is free software; the Free Software Foundation # gives unlimited permission to copy and/or distribute it, @@ -16,7 +16,17 @@ VPATH = @srcdir@ -am__is_gnu_make = test -n '$(MAKEFILE_LIST)' && test -n '$(MAKELEVEL)' +am__is_gnu_make = { \ + if test -z '$(MAKELEVEL)'; then \ + false; \ + elif test -n '$(MAKE_HOST)'; then \ + true; \ + elif test -n '$(MAKE_VERSION)' && test -n '$(CURDIR)'; then \ + true; \ + else \ + false; \ + fi; \ +} am__make_running_with_option = \ case $${target_option-} in \ ?) ;; \ @@ -190,9 +200,6 @@ bin_PROGRAMS = audacity$(EXEEXT) @USE_VST_TRUE@ $(NULL) subdir = src -DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am \ - $(srcdir)/configtemplate.h $(srcdir)/audacity.desktop.in \ - $(top_srcdir)/autotools/depcomp $(dist_mime_DATA) ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/m4/ac_c99_func_lrint.m4 \ $(top_srcdir)/m4/ac_c99_func_lrintf.m4 \ @@ -235,6 +242,8 @@ am__aclocal_m4_deps = $(top_srcdir)/m4/ac_c99_func_lrint.m4 \ $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) +DIST_COMMON = $(srcdir)/Makefile.am $(dist_mime_DATA) \ + $(am__DIST_COMMON) mkinstalldirs = $(install_sh) -d CONFIG_HEADER = configwin.h configunix.h CONFIG_CLEAN_FILES = audacity.desktop @@ -287,22 +296,23 @@ am__audacity_SOURCES_DIST = BlockFile.cpp BlockFile.h DirManager.cpp \ AutoRecovery.h BatchCommandDialog.cpp BatchCommandDialog.h \ BatchCommands.cpp BatchCommands.h BatchProcessDialog.cpp \ BatchProcessDialog.h Benchmark.cpp Benchmark.h \ - Dependencies.cpp Dependencies.h DeviceChange.cpp \ - DeviceChange.h DeviceManager.cpp DeviceManager.h Diags.cpp \ - Diags.h Envelope.cpp Envelope.h Experimental.h FFmpeg.cpp \ - FFmpeg.h FFT.cpp FFT.h FileException.cpp FileException.h \ - FileIO.cpp FileIO.h FileNames.cpp FileNames.h float_cast.h \ - FreqWindow.cpp FreqWindow.h HelpText.cpp HelpText.h \ - HistoryWindow.cpp HistoryWindow.h HitTestResult.h \ - ImageManipulation.cpp ImageManipulation.h \ - InconsistencyException.cpp InconsistencyException.h \ - InterpolateAudio.cpp InterpolateAudio.h LabelDialog.cpp \ - LabelDialog.h LabelTrack.cpp LabelTrack.h LangChoice.cpp \ - LangChoice.h Languages.cpp Languages.h Legacy.cpp Legacy.h \ - Lyrics.cpp Lyrics.h LyricsWindow.cpp LyricsWindow.h \ - MacroMagic.h Matrix.cpp Matrix.h MemoryX.h Menus.cpp Menus.h \ - Mix.cpp Mix.h MixerBoard.cpp MixerBoard.h ModuleManager.cpp \ - ModuleManager.h NumberScale.h PitchName.cpp PitchName.h \ + CellularPanel.cpp CellularPanel.h Dependencies.cpp \ + Dependencies.h DeviceChange.cpp DeviceChange.h \ + DeviceManager.cpp DeviceManager.h Diags.cpp Diags.h \ + Envelope.cpp Envelope.h Experimental.h FFmpeg.cpp FFmpeg.h \ + FFT.cpp FFT.h FileException.cpp FileException.h FileIO.cpp \ + FileIO.h FileNames.cpp FileNames.h float_cast.h FreqWindow.cpp \ + FreqWindow.h HelpText.cpp HelpText.h HistoryWindow.cpp \ + HistoryWindow.h HitTestResult.h ImageManipulation.cpp \ + ImageManipulation.h InconsistencyException.cpp \ + InconsistencyException.h InterpolateAudio.cpp \ + InterpolateAudio.h LabelDialog.cpp LabelDialog.h \ + LabelTrack.cpp LabelTrack.h LangChoice.cpp LangChoice.h \ + Languages.cpp Languages.h Legacy.cpp Legacy.h Lyrics.cpp \ + Lyrics.h LyricsWindow.cpp LyricsWindow.h MacroMagic.h \ + Matrix.cpp Matrix.h MemoryX.h Menus.cpp Menus.h Mix.cpp Mix.h \ + MixerBoard.cpp MixerBoard.h ModuleManager.cpp ModuleManager.h \ + NumberScale.h PitchName.cpp PitchName.h \ PlatformCompatibility.cpp PlatformCompatibility.h \ PluginManager.cpp PluginManager.h Printing.cpp Printing.h \ Profiler.cpp Profiler.h Project.cpp Project.h RealFFTf.cpp \ @@ -612,7 +622,8 @@ am_audacity_OBJECTS = $(am__objects_1) audacity-AboutDialog.$(OBJEXT) \ audacity-BatchCommandDialog.$(OBJEXT) \ audacity-BatchCommands.$(OBJEXT) \ audacity-BatchProcessDialog.$(OBJEXT) \ - audacity-Benchmark.$(OBJEXT) audacity-Dependencies.$(OBJEXT) \ + audacity-Benchmark.$(OBJEXT) audacity-CellularPanel.$(OBJEXT) \ + audacity-Dependencies.$(OBJEXT) \ audacity-DeviceChange.$(OBJEXT) \ audacity-DeviceManager.$(OBJEXT) audacity-Diags.$(OBJEXT) \ audacity-Envelope.$(OBJEXT) audacity-FFmpeg.$(OBJEXT) \ @@ -1008,6 +1019,8 @@ am__define_uniq_tagged_files = \ done | $(am__uniquify_input)` ETAGS = etags CTAGS = ctags +am__DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/audacity.desktop.in \ + $(srcdir)/configtemplate.h $(top_srcdir)/autotools/depcomp DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) ACLOCAL = @ACLOCAL@ AMTAR = @AMTAR@ @@ -1102,6 +1115,7 @@ LN_S = @LN_S@ LTLIBICONV = @LTLIBICONV@ LTLIBINTL = @LTLIBINTL@ LTLIBOBJS = @LTLIBOBJS@ +LT_SYS_LIBRARY_PATH = @LT_SYS_LIBRARY_PATH@ LV2_CFLAGS = @LV2_CFLAGS@ LV2_LIBS = @LV2_LIBS@ MAINT = @MAINT@ @@ -1127,6 +1141,8 @@ PACKAGE_URL = @PACKAGE_URL@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ PKG_CONFIG = @PKG_CONFIG@ +PKG_CONFIG_LIBDIR = @PKG_CONFIG_LIBDIR@ +PKG_CONFIG_PATH = @PKG_CONFIG_PATH@ PORTAUDIO_CFLAGS = @PORTAUDIO_CFLAGS@ PORTAUDIO_LIBS = @PORTAUDIO_LIBS@ PORTMIDI_CFLAGS = @PORTMIDI_CFLAGS@ @@ -1308,22 +1324,23 @@ audacity_SOURCES = $(libaudacity_la_SOURCES) AboutDialog.cpp \ AutoRecovery.h BatchCommandDialog.cpp BatchCommandDialog.h \ BatchCommands.cpp BatchCommands.h BatchProcessDialog.cpp \ BatchProcessDialog.h Benchmark.cpp Benchmark.h \ - Dependencies.cpp Dependencies.h DeviceChange.cpp \ - DeviceChange.h DeviceManager.cpp DeviceManager.h Diags.cpp \ - Diags.h Envelope.cpp Envelope.h Experimental.h FFmpeg.cpp \ - FFmpeg.h FFT.cpp FFT.h FileException.cpp FileException.h \ - FileIO.cpp FileIO.h FileNames.cpp FileNames.h float_cast.h \ - FreqWindow.cpp FreqWindow.h HelpText.cpp HelpText.h \ - HistoryWindow.cpp HistoryWindow.h HitTestResult.h \ - ImageManipulation.cpp ImageManipulation.h \ - InconsistencyException.cpp InconsistencyException.h \ - InterpolateAudio.cpp InterpolateAudio.h LabelDialog.cpp \ - LabelDialog.h LabelTrack.cpp LabelTrack.h LangChoice.cpp \ - LangChoice.h Languages.cpp Languages.h Legacy.cpp Legacy.h \ - Lyrics.cpp Lyrics.h LyricsWindow.cpp LyricsWindow.h \ - MacroMagic.h Matrix.cpp Matrix.h MemoryX.h Menus.cpp Menus.h \ - Mix.cpp Mix.h MixerBoard.cpp MixerBoard.h ModuleManager.cpp \ - ModuleManager.h NumberScale.h PitchName.cpp PitchName.h \ + CellularPanel.cpp CellularPanel.h Dependencies.cpp \ + Dependencies.h DeviceChange.cpp DeviceChange.h \ + DeviceManager.cpp DeviceManager.h Diags.cpp Diags.h \ + Envelope.cpp Envelope.h Experimental.h FFmpeg.cpp FFmpeg.h \ + FFT.cpp FFT.h FileException.cpp FileException.h FileIO.cpp \ + FileIO.h FileNames.cpp FileNames.h float_cast.h FreqWindow.cpp \ + FreqWindow.h HelpText.cpp HelpText.h HistoryWindow.cpp \ + HistoryWindow.h HitTestResult.h ImageManipulation.cpp \ + ImageManipulation.h InconsistencyException.cpp \ + InconsistencyException.h InterpolateAudio.cpp \ + InterpolateAudio.h LabelDialog.cpp LabelDialog.h \ + LabelTrack.cpp LabelTrack.h LangChoice.cpp LangChoice.h \ + Languages.cpp Languages.h Legacy.cpp Legacy.h Lyrics.cpp \ + Lyrics.h LyricsWindow.cpp LyricsWindow.h MacroMagic.h \ + Matrix.cpp Matrix.h MemoryX.h Menus.cpp Menus.h Mix.cpp Mix.h \ + MixerBoard.cpp MixerBoard.h ModuleManager.cpp ModuleManager.h \ + NumberScale.h PitchName.cpp PitchName.h \ PlatformCompatibility.cpp PlatformCompatibility.h \ PluginManager.cpp PluginManager.h Printing.cpp Printing.h \ Profiler.cpp Profiler.h Project.cpp Project.h RealFFTf.cpp \ @@ -1602,7 +1619,6 @@ $(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__confi echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/Makefile'; \ $(am__cd) $(top_srcdir) && \ $(AUTOMAKE) --foreign src/Makefile -.PRECIOUS: Makefile Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status @case '$?' in \ *config.status*) \ @@ -2408,6 +2424,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/audacity-BatchProcessDialog.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/audacity-Benchmark.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/audacity-BlockFile.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/audacity-CellularPanel.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/audacity-Dependencies.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/audacity-DeviceChange.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/audacity-DeviceManager.Po@am__quote@ @@ -3273,6 +3290,20 @@ audacity-Benchmark.obj: Benchmark.cpp @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(audacity_CPPFLAGS) $(CPPFLAGS) $(audacity_CXXFLAGS) $(CXXFLAGS) -c -o audacity-Benchmark.obj `if test -f 'Benchmark.cpp'; then $(CYGPATH_W) 'Benchmark.cpp'; else $(CYGPATH_W) '$(srcdir)/Benchmark.cpp'; fi` +audacity-CellularPanel.o: CellularPanel.cpp +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(audacity_CPPFLAGS) $(CPPFLAGS) $(audacity_CXXFLAGS) $(CXXFLAGS) -MT audacity-CellularPanel.o -MD -MP -MF $(DEPDIR)/audacity-CellularPanel.Tpo -c -o audacity-CellularPanel.o `test -f 'CellularPanel.cpp' || echo '$(srcdir)/'`CellularPanel.cpp +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/audacity-CellularPanel.Tpo $(DEPDIR)/audacity-CellularPanel.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='CellularPanel.cpp' object='audacity-CellularPanel.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(audacity_CPPFLAGS) $(CPPFLAGS) $(audacity_CXXFLAGS) $(CXXFLAGS) -c -o audacity-CellularPanel.o `test -f 'CellularPanel.cpp' || echo '$(srcdir)/'`CellularPanel.cpp + +audacity-CellularPanel.obj: CellularPanel.cpp +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(audacity_CPPFLAGS) $(CPPFLAGS) $(audacity_CXXFLAGS) $(CXXFLAGS) -MT audacity-CellularPanel.obj -MD -MP -MF $(DEPDIR)/audacity-CellularPanel.Tpo -c -o audacity-CellularPanel.obj `if test -f 'CellularPanel.cpp'; then $(CYGPATH_W) 'CellularPanel.cpp'; else $(CYGPATH_W) '$(srcdir)/CellularPanel.cpp'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/audacity-CellularPanel.Tpo $(DEPDIR)/audacity-CellularPanel.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(AM_V_CXX)source='CellularPanel.cpp' object='audacity-CellularPanel.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(audacity_CPPFLAGS) $(CPPFLAGS) $(audacity_CXXFLAGS) $(CXXFLAGS) -c -o audacity-CellularPanel.obj `if test -f 'CellularPanel.cpp'; then $(CYGPATH_W) 'CellularPanel.cpp'; else $(CYGPATH_W) '$(srcdir)/CellularPanel.cpp'; fi` + audacity-Dependencies.o: Dependencies.cpp @am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(audacity_CPPFLAGS) $(CPPFLAGS) $(audacity_CXXFLAGS) $(CXXFLAGS) -MT audacity-Dependencies.o -MD -MP -MF $(DEPDIR)/audacity-Dependencies.Tpo -c -o audacity-Dependencies.o `test -f 'Dependencies.cpp' || echo '$(srcdir)/'`Dependencies.cpp @am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/audacity-Dependencies.Tpo $(DEPDIR)/audacity-Dependencies.Po @@ -7722,6 +7753,8 @@ uninstall-am: uninstall-binPROGRAMS uninstall-desktopDATA \ tags tags-am uninstall uninstall-am uninstall-binPROGRAMS \ uninstall-desktopDATA uninstall-dist_mimeDATA +.PRECIOUS: Makefile + # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index dc75ae388..7118d7f4e 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -169,8 +169,6 @@ is time to refresh some aspect of the screen. #include "RefreshCode.h" #include "TrackArtist.h" #include "TrackPanelAx.h" -#include "UIHandle.h" -#include "HitTestResult.h" #include "WaveTrack.h" #ifdef EXPERIMENTAL_MIDI_OUT #include "NoteTrack.h" @@ -254,18 +252,6 @@ template < class A, class B, class DIST > bool within(A a, B b, DIST d) return (a > b - d) && (a < b + d); } -BEGIN_EVENT_TABLE(CellularPanel, OverlayPanel) - EVT_MOUSE_EVENTS(CellularPanel::OnMouseEvent) - EVT_MOUSE_CAPTURE_LOST(CellularPanel::OnCaptureLost) - EVT_COMMAND(wxID_ANY, EVT_CAPTURE_KEY, CellularPanel::OnCaptureKey) - EVT_KEY_DOWN(CellularPanel::OnKeyDown) - EVT_KEY_UP(CellularPanel::OnKeyUp) - EVT_CHAR(CellularPanel::OnChar) - EVT_SET_FOCUS(CellularPanel::OnSetFocus) - EVT_KILL_FOCUS(CellularPanel::OnKillFocus) - EVT_CONTEXT_MENU(CellularPanel::OnContextMenu) -END_EVENT_TABLE() - BEGIN_EVENT_TABLE(TrackPanel, CellularPanel) EVT_MOUSE_EVENTS(TrackPanel::OnMouseEvent) EVT_KEY_DOWN(TrackPanel::OnKeyDown) @@ -620,20 +606,6 @@ void TrackPanel::MakeParentRedrawScrollbars() mListener->TP_RedrawScrollbars(); } -void CellularPanel::HandleInterruptedDrag() -{ - if (mUIHandle && mUIHandle->StopsOnKeystroke() ) { - // The bogus id isn't used anywhere, but may help with debugging. - // as this is sending a bogus mouse up. The mouse button is still actually down - // and may go up again. - const int idBogusUp = 2; - wxMouseEvent evt { wxEVT_LEFT_UP }; - evt.SetId( idBogusUp ); - evt.SetPosition(this->ScreenToClient(::wxGetMousePosition())); - this->ProcessEvent(evt); - } -} - namespace { std::shared_ptr FindTrack(TrackPanelCell *pCell ) { @@ -724,91 +696,6 @@ void TrackPanel::ProcessUIHandleResult panel->EnsureVisible(pClickedTrack); } -void CellularPanel::Uncapture(wxMouseState *pState) -{ - auto state = ::wxGetMouseState(); - if (!pState) { - // Remap the position - state.SetPosition(this->ScreenToClient(state.GetPosition())); - pState = &state; - } - - if (HasCapture()) - ReleaseMouse(); - HandleMotion( *pState ); -} - -bool CellularPanel::CancelDragging() -{ - if (mUIHandle) { - // copy shared_ptr for safety, as in HandleClick - auto handle = mUIHandle; - // UIHANDLE CANCEL - UIHandle::Result refreshResult = handle->Cancel(GetProject()); - auto pClickedCell = mpClickedCell.lock(); - if (pClickedCell) - ProcessUIHandleResult( - pClickedCell.get(), {}, - refreshResult | mMouseOverUpdateFlags ); - mpClickedCell.reset(); - mUIHandle.reset(), handle.reset(), ClearTargets(); - Uncapture(); - return true; - } - return false; -} - -bool CellularPanel::HandleEscapeKey(bool down) -{ - if (!down) - return false; - - { - auto target = Target(); - if (target && target->HasEscape() && target->Escape()) { - HandleCursorForPresentMouseState(false); - return true; - } - } - - if (mUIHandle) { - CancelDragging(); - return true; - } - - if (ChangeTarget(true, false)) { - HandleCursorForPresentMouseState(false); - return true; - } - - return false; -} - -void CellularPanel::UpdateMouseState(const wxMouseState &state) -{ - mLastMouseState = state; - - // Simulate a down button if none, so hit test routines can anticipate - // which button will be clicked - if (!state.ButtonIsDown(wxMOUSE_BTN_ANY)) { -#ifdef __WXOSX__ - if (state.RawControlDown()) - // On Mac we can distinctly anticipate "right" click (as Control+click) - mLastMouseState.SetRightDown( true ), - mLastMouseState.SetLeftDown( false ); - else -#endif - // Anticipate a left click by default - mLastMouseState.SetRightDown( false ), - mLastMouseState.SetLeftDown( true ); - } -} - -void CellularPanel::HandleModifierKey() -{ - HandleCursorForPresentMouseState(); -} - void TrackPanel::HandlePageUpKey() { mListener->TP_ScrollWindow(2 * mViewInfo->h - GetScreenEndTime()); @@ -819,165 +706,12 @@ void TrackPanel::HandlePageDownKey() mListener->TP_ScrollWindow(GetScreenEndTime()); } -void CellularPanel::HandleCursorForPresentMouseState(bool doHit) -{ - // Come here on modifier key or mouse button transitions, - // or on starting or stopping of play or record, - // or change of toolbar button, - // and change the cursor appropriately. - - // Get the button and key states - auto state = ::wxGetMouseState(); - // Remap the position - state.SetPosition(this->ScreenToClient(state.GetPosition())); - - HandleMotion( state, doHit ); -} - bool TrackPanel::IsAudioActive() { AudacityProject *p = GetProject(); return p->IsAudioActive(); } - -/// TrackPanel::HandleMotion( ) sets the cursor drawn at the mouse location, -/// and updates the status bar message. -/// We treat certain other changes of mouse button and key state as "motions" -/// too, and also starting and stopping of playback or recording, all of which -/// may cause the appropriate cursor and message to change. -/// As this procedure checks which region the mouse is over, it is -/// appropriate to establish the message in the status bar. -void CellularPanel::HandleMotion( wxMouseState &inState, bool doHit ) -{ - UpdateMouseState( inState ); - - const auto foundCell = FindCell( inState.m_x, inState.m_y ); - auto &rect = foundCell.rect; - auto &pCell = foundCell.pCell; - const TrackPanelMouseState tpmState{ mLastMouseState, rect, pCell }; - HandleMotion( tpmState, doHit ); -} - -void CellularPanel::HandleMotion -( const TrackPanelMouseState &tpmState, bool doHit ) -{ - auto handle = mUIHandle; - - auto newCell = tpmState.pCell; - - wxString status{}, tooltip{}; - wxCursor *pCursor{}; - unsigned refreshCode = 0; - - if ( ! doHit ) { - // Dragging or not - handle = Target(); - - // Assume cell does not change but target does - refreshCode = mMouseOverUpdateFlags; - mMouseOverUpdateFlags = 0; - } - else if ( !mUIHandle ) { - // Not yet dragging. - - auto oldCell = mLastCell.lock(); - - unsigned updateFlags = mMouseOverUpdateFlags; - - // First check whether crossing cell to cell - if ( newCell == oldCell ) - oldCell.reset(); - else { - // Forget old targets - ClearTargets(); - // Re-draw any highlighting - if (oldCell) { - ProcessUIHandleResult( - oldCell.get(), oldCell.get(), updateFlags); - } - } - - auto oldHandle = Target(); - auto oldPosition = mTarget; - - // Now do the - // UIHANDLE HIT TEST ! - mTargets = newCell->HitTest(tpmState, GetProject()); - - mTarget = 0; - - // Find the old target's NEW place if we can - if (oldHandle) { - auto begin = mTargets.begin(), end = mTargets.end(), - iter = std::find(begin, end, oldHandle); - if (iter != end) { - size_t newPosition = iter - begin; - if (newPosition <= oldPosition) - mTarget = newPosition; - // else, some NEW hit and this position takes priority - } - } - - handle = Target(); - - mLastCell = newCell; - - if (!oldCell && oldHandle != handle) - // Did not move cell to cell, but did change the target - refreshCode = updateFlags; - - if (handle && handle != oldHandle) - handle->Enter(true); - } - - // UIHANDLE PREVIEW - // Update status message and cursor, whether dragging or not - if (handle) { - auto preview = handle->Preview( tpmState, GetProject() ); - status = preview.message; - tooltip = preview.tooltip; - pCursor = preview.cursor; - auto code = handle->GetChangeHighlight(); - handle->SetChangeHighlight(RefreshCode::RefreshNone); - refreshCode |= code; - mMouseOverUpdateFlags |= code; - } - if (newCell && - (!pCursor || status.empty() || tooltip.empty())) { - // Defaulting of cursor, tooltip, and status if there is no handle, - // or if the handle does not specify them - const auto preview = newCell->DefaultPreview( tpmState, GetProject() ); - if (!pCursor) - pCursor = preview.cursor; - if (status.empty()) - status = preview.message; - if (tooltip.empty()) - tooltip = preview.tooltip; - } - if (!pCursor) { - // Ultimate default cursor - static wxCursor defaultCursor{ wxCURSOR_DEFAULT }; - pCursor = &defaultCursor; - } - - UpdateStatusMessage(status); - -#if wxUSE_TOOLTIPS - if (tooltip != GetToolTipText()) { - // Unset first, by analogy with AButton - UnsetToolTip(); - SetToolTip(tooltip); - } -#endif - - if (pCursor) - SetCursor( *pCursor ); - - ProcessUIHandleResult( - newCell.get(), newCell.get(), refreshCode); -} - void TrackPanel::UpdateStatusMessage( const wxString &st ) { auto status = st; @@ -987,62 +721,6 @@ void TrackPanel::UpdateStatusMessage( const wxString &st ) mListener->TP_DisplayStatusMessage(status); } -bool CellularPanel::HasRotation() -{ - // Is there a nontrivial TAB key rotation? - if ( mTargets.size() > 1 ) - return true; - auto target = Target(); - return target && target->HasRotation(); -} - -bool CellularPanel::HasEscape() -{ - if (IsMouseCaptured()) - return true; - - if (mTarget + 1 == mTargets.size() && - Target() && - !Target()->HasEscape()) - return false; - - return mTargets.size() > 0; -} - -bool CellularPanel::ChangeTarget(bool forward, bool cycle) -{ - auto size = mTargets.size(); - - auto target = Target(); - if (target && target->HasRotation()) { - if(target->Rotate(forward)) - return true; - else if (cycle && (size == 1 || IsMouseCaptured())) { - // Rotate through the states of this target only. - target->Enter(forward); - return true; - } - } - - if (!cycle && - ((forward && mTarget + 1 == size) || - (!forward && mTarget == 0))) - return false; - - if (size > 1) { - if (forward) - ++mTarget; - else - mTarget += size - 1; - mTarget %= size; - if (Target()) - Target()->Enter(forward); - return true; - } - - return false; -} - void TrackPanel::UpdateSelectionDisplay() { // Full refresh since the label area may need to indicate @@ -1101,12 +779,6 @@ void TrackPanel::MessageForScreenReader(const wxString& message) mAx->MessageForScreenReader(message); } -/// Determines if a modal tool is active -bool CellularPanel::IsMouseCaptured() -{ - return mUIHandle != NULL; -} - void TrackPanel::UpdateViewIfNoTracks() { if (mTracks->empty()) @@ -1167,11 +839,6 @@ void TrackPanel::OnTrackListDeletion(wxCommandEvent & e) e.Skip(); } -void CellularPanel::OnContextMenu(wxContextMenuEvent & WXUNUSED(event)) -{ - DoContextMenu(); -} - struct TrackInfo::TCPLine { using DrawFunction = void (*)( TrackPanelDrawingContext &context, @@ -1360,85 +1027,6 @@ bool TrackInfo::HideTopItem( const wxRect &rect, const wxRect &subRect, return subRect.y + subRect.height - allowance >= rect.y + limit; } -/// Handle mouse wheel rotation (for zoom in/out, vertical and horizontal scrolling) -void CellularPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ) -{ - auto pCell = tpmEvent.pCell; - if (!pCell) - return; - - auto &event = tpmEvent.event; - double steps {}; -#if defined(__WXMAC__) && defined(EVT_MAGNIFY) - // PRL: - // Pinch and spread implemented in wxWidgets 3.1.0, or cherry-picked from - // the future in custom build of 3.0.2 - if (event.Magnify()) { - event.SetControlDown(true); - steps = 2 * event.GetMagnification(); - } - else -#endif - { - steps = event.m_wheelRotation / - (event.m_wheelDelta > 0 ? (double)event.m_wheelDelta : 120.0); - } - - if(event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL) { - // Two-fingered horizontal swipe on mac is treated like shift-mousewheel - event.SetShiftDown(true); - // This makes the wave move in the same direction as the fingers, and the scrollbar - // thumb moves oppositely - steps *= -1; - } - - tpmEvent.steps = steps; - - if(!event.HasAnyModifiers()) { - // We will later un-skip if we do anything, but if we don't, - // propagate the event up for the sake of the scrubber - event.Skip(); - event.ResumePropagation(wxEVENT_PROPAGATE_MAX); - } - - unsigned result = - pCell->HandleWheelRotation( tpmEvent, GetProject() ); - ProcessUIHandleResult( - pCell.get(), pCell.get(), result); -} - -void CellularPanel::OnCaptureKey(wxCommandEvent & event) -{ - mEnableTab = false; - wxKeyEvent *kevent = static_cast(event.GetEventObject()); - const auto code = kevent->GetKeyCode(); - if ( WXK_ESCAPE != code ) - HandleInterruptedDrag(); - - // Give focused cell precedence - const auto t = GetFocusedCell(); - if (t) { - const unsigned refreshResult = - t->CaptureKey(*kevent, *mViewInfo, this); - ProcessUIHandleResult(t, t, refreshResult); - event.Skip(kevent->GetSkipped()); - } - -#if 0 - // Special TAB key handling, but only if the cell didn't capture it - if ( !(t && !kevent->GetSkipped()) && - WXK_TAB == code && HasRotation() ) { - // Override TAB navigation in wxWidgets, by not skipping - event.Skip(false); - mEnableTab = true; - return; - } - else -#endif - if (!t) - event.Skip(); -} - void TrackPanel::OnKeyDown(wxKeyEvent & event) { switch (event.GetKeyCode()) @@ -1459,121 +1047,6 @@ void TrackPanel::OnKeyDown(wxKeyEvent & event) } } -void CellularPanel::OnKeyDown(wxKeyEvent & event) -{ - switch (event.GetKeyCode()) - { - case WXK_ESCAPE: - if(HandleEscapeKey(true)) - // Don't skip the event, eat it so that - // AudacityApp does not also stop any playback. - return; - else - break; - - case WXK_ALT: - case WXK_SHIFT: - case WXK_CONTROL: -#ifdef __WXOSX__ - case WXK_RAW_CONTROL: -#endif - HandleModifierKey(); - break; - -#if 0 - case WXK_TAB: - if ( mEnableTab && HasRotation() ) { - ChangeTarget( !event.ShiftDown(), true ); - HandleCursorForPresentMouseState(false); - return; - } - else - break; -#endif - } - - const auto t = GetFocusedCell(); - - if (t) { - const unsigned refreshResult = - t->KeyDown(event, *mViewInfo, this); - ProcessUIHandleResult(t, t, refreshResult); - } - else - event.Skip(); -} - -void CellularPanel::OnChar(wxKeyEvent & event) -{ - switch (event.GetKeyCode()) - { - case WXK_ESCAPE: - case WXK_ALT: - case WXK_SHIFT: - case WXK_CONTROL: - case WXK_PAGEUP: - case WXK_PAGEDOWN: - return; - } - - const auto t = GetFocusedCell(); - if (t) { - const unsigned refreshResult = - t->Char(event, *mViewInfo, this); - ProcessUIHandleResult(t, t, refreshResult); - } - else - event.Skip(); -} - -void CellularPanel::OnKeyUp(wxKeyEvent & event) -{ - bool didSomething = false; - switch (event.GetKeyCode()) - { - case WXK_ESCAPE: - didSomething = HandleEscapeKey(false); - break; - - case WXK_ALT: - case WXK_SHIFT: - case WXK_CONTROL: -#ifdef __WXOSX__ - case WXK_RAW_CONTROL: -#endif - HandleModifierKey(); - break; - } - - if (didSomething) - return; - - const auto t = GetFocusedCell(); - if (t) { - const unsigned refreshResult = - t->KeyUp(event, *mViewInfo, this); - ProcessUIHandleResult(t, t, refreshResult); - return; - } - - event.Skip(); -} - -/// Should handle the case when the mouse capture is lost. -void CellularPanel::OnCaptureLost(wxMouseCaptureLostEvent & WXUNUSED(event)) -{ - ClearTargets(); - - // This is bad. We are lying abou the event by saying it is a mouse up. - wxMouseEvent e(wxEVT_LEFT_UP); - e.SetId( kCaptureLostEventId ); - - e.m_x = mMouseMostRecentX; - e.m_y = mMouseMostRecentY; - - OnMouseEvent(e); -} - void TrackPanel::OnMouseEvent(wxMouseEvent & event) { if (event.LeftDown()) { @@ -1601,195 +1074,6 @@ void TrackPanel::OnMouseEvent(wxMouseEvent & event) event.Skip(); } -/// This handles just generic mouse events. Then, based -/// on our current state, we forward the mouse events to -/// various interested parties. -void CellularPanel::OnMouseEvent(wxMouseEvent & event) -try -{ - const auto foundCell = FindCell( event.m_x, event.m_y ); - auto &rect = foundCell.rect; - auto &pCell = foundCell.pCell; - - const auto size = GetSize(); - TrackPanelMouseEvent tpmEvent{ event, rect, size, pCell }; - -#if defined(__WXMAC__) && defined(EVT_MAGNIFY) - // PRL: - // Pinch and spread implemented in wxWidgets 3.1.0, or cherry-picked from - // the future in custom build of 3.0.2 - if (event.Magnify()) { - HandleWheelRotation( tpmEvent ); - } -#endif - - // If a mouse event originates from a keyboard context menu event then - // event.GetPosition() == wxDefaultPosition. wxContextMenu events are handled in - // CellularPanel::OnContextMenu(), and therefore associated mouse events are ignored here. - // Not ignoring them was causing bug 613: the mouse events were interpreted as clicking - // outside the tracks. - if (event.GetPosition() == wxDefaultPosition && (event.RightDown() || event.RightUp())) { - event.Skip(); - return; - } - - if (event.m_wheelRotation != 0) - HandleWheelRotation( tpmEvent ); - - if (event.LeftDown() || event.LeftIsDown() || event.Moving()) { - // Skip, even if we do something, so that the left click or drag - // may have an additional effect in the scrubber. - event.Skip(); - event.ResumePropagation(wxEVENT_PROPAGATE_MAX); - } - - mMouseMostRecentX = event.m_x; - mMouseMostRecentY = event.m_y; - - if (event.LeftDown()) { - // The activate event is used to make the - // parent window 'come alive' if it didn't have focus. - wxActivateEvent e; - GetParent()->GetEventHandler()->ProcessEvent(e); - } - - if (event.ButtonDown()) { - SetFocus(); - } - - if (event.Leaving()) - { - if ( !mUIHandle ) - ClearTargets(); - - auto buttons = - // Bug 1325: button state in Leaving events is unreliable on Mac. - // Poll the global state instead. - // event.ButtonIsDown(wxMOUSE_BTN_ANY); - ::wxGetMouseState().ButtonIsDown(wxMOUSE_BTN_ANY); - - if(!buttons) { - CancelDragging(); - -#if defined(__WXMAC__) - - // We must install the cursor ourselves since the window under - // the mouse is no longer this one and wx2.8.12 makes that check. - // Should re-evaluate with wx3. - wxSTANDARD_CURSOR->MacInstall(); -#endif - } - } - - if (mUIHandle) { - auto pClickedCell = mpClickedCell.lock(); - if (event.Dragging()) { - // UIHANDLE DRAG - // copy shared_ptr for safety, as in HandleClick - auto handle = mUIHandle; - const UIHandle::Result refreshResult = - handle->Drag( tpmEvent, GetProject() ); - ProcessUIHandleResult - (pClickedCell.get(), pCell.get(), refreshResult); - mMouseOverUpdateFlags |= refreshResult; - if (refreshResult & RefreshCode::Cancelled) { - // Drag decided to abort itself - mUIHandle.reset(), handle.reset(), ClearTargets(); - mpClickedCell.reset(); - Uncapture( &event ); - } - else { - UpdateMouseState(event); - TrackPanelMouseState tpmState{ mLastMouseState, rect, pCell }; - HandleMotion( tpmState ); - } - } - else if (event.ButtonUp()) { - // UIHANDLE RELEASE - unsigned moreFlags = mMouseOverUpdateFlags; - UIHandle::Result refreshResult = - mUIHandle->Release( tpmEvent, GetProject(), this ); - ProcessUIHandleResult - (pClickedCell.get(), pCell.get(), - refreshResult | moreFlags); - mUIHandle.reset(), ClearTargets(); - mpClickedCell.reset(); - // will also Uncapture() below - } - } - else if ( event.GetEventType() == wxEVT_MOTION ) - // Update status message and cursor, not during drag - // consider it not a drag, even if button is down during motion, if - // mUIHandle is null, as it becomes during interrupted drag - // (e.g. by hitting space to play while dragging an envelope point) - HandleMotion( event ); - else if ( event.ButtonDown() || event.ButtonDClick() ) - HandleClick( tpmEvent ); - - if (event.ButtonDown() && IsMouseCaptured()) { - if (!HasCapture()) - CaptureMouse(); - } - - if (event.ButtonUp()) - Uncapture(); -} -catch( ... ) -{ - // Abort any dragging, as if by hitting Esc - if ( CancelDragging() ) - ; - else { - Uncapture(); - Refresh(false); - } - throw; -} - -void CellularPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) -{ - auto pCell = tpmEvent.pCell; - - // Do hit test once more, in case the button really pressed was not the - // one "anticipated." - { - TrackPanelMouseState tpmState{ - tpmEvent.event, - tpmEvent.rect, - tpmEvent.pCell - }; - HandleMotion( tpmState ); - } - - mUIHandle = Target(); - - if (mUIHandle) { - // UIHANDLE CLICK - // Make another shared pointer to the handle, in case recursive - // event dispatching otherwise tries to delete the handle. - auto handle = mUIHandle; - UIHandle::Result refreshResult = - handle->Click( tpmEvent, GetProject() ); - if (refreshResult & RefreshCode::Cancelled) - mUIHandle.reset(), handle.reset(), ClearTargets(); - else { - mpClickedCell = pCell; - - // Perhaps the clicked handle wants to update cursor and state message - // after a click. - TrackPanelMouseState tpmState{ - tpmEvent.event, - tpmEvent.rect, - tpmEvent.pCell - }; - HandleMotion( tpmState ); - } - ProcessUIHandleResult( - pCell.get(), pCell.get(), refreshResult); - mMouseOverUpdateFlags |= refreshResult; - } -} - double TrackPanel::GetMostRecentXPos() { return mViewInfo->PositionToTime(MostRecentXCoord(), GetLabelWidth()); @@ -2628,26 +1912,6 @@ void TrackPanel::OnTrackMenu(Track *t) CellularPanel::DoContextMenu( t ); } -void CellularPanel::DoContextMenu( TrackPanelCell *pCell ) -{ - if( !pCell ) { - pCell = GetFocusedCell(); - if( !pCell ) - return; - } - - const auto delegate = pCell->ContextMenuDelegate(); - if (!delegate) - return; - - auto rect = FindRect( *delegate ); - const UIHandle::Result refreshResult = - delegate->DoContextMenu(rect, this, NULL); - - // To do: use safer shared_ptr to pCell - ProcessUIHandleResult(pCell, pCell, refreshResult); -} - Track * TrackPanel::GetFirstSelectedTrack() { @@ -2944,20 +2208,6 @@ void TrackPanel::SetFocusedTrack( Track *t ) } } -void CellularPanel::OnSetFocus(wxFocusEvent & WXUNUSED(event)) -{ - SetFocusedCell(); -} - -void CellularPanel::OnKillFocus(wxFocusEvent & WXUNUSED(event)) -{ - if (AudacityProject::HasKeyboardCapture(this)) - { - AudacityProject::ReleaseKeyboard(this); - } - Refresh( false); -} - /********************************************************************** TrackInfo code is destined to move out of this file. diff --git a/src/TrackPanel.h b/src/TrackPanel.h index 6a3c00cff..9df28299c 100644 --- a/src/TrackPanel.h +++ b/src/TrackPanel.h @@ -22,7 +22,7 @@ #include "SelectedRegion.h" -#include "widgets/OverlayPanel.h" +#include "CellularPanel.h" #include "SelectionState.h" @@ -34,7 +34,6 @@ class SpectrumAnalyst; class Track; class TrackList; class TrackPanel; -class TrackPanelCell; class TrackArtist; class Ruler; class SnapManager; @@ -43,20 +42,13 @@ class LWSlider; class ControlToolBar; //Needed because state of controls can affect what gets drawn. class ToolsToolBar; //Needed because state of controls can affect what gets drawn. class MixerBoard; -class AudacityProject; class TrackPanelAx; class TrackPanelCellIterator; -struct TrackPanelMouseEvent; -struct TrackPanelMouseState; - -class ViewInfo; class NoteTrack; class WaveTrack; class WaveClip; -class UIHandle; -using UIHandlePtr = std::shared_ptr; // Declared elsewhere, to reduce compilation dependencies class TrackPanelListener; @@ -242,127 +234,6 @@ private: const int DragThreshold = 3;// Anything over 3 pixels is a drag, else a click. -// This class manages a panel divided into a number of sub-rectangles called -// cells, that each implement hit tests returning click-drag-release handler -// objects, and other services. -// It has no dependency on the Track class. -class AUDACITY_DLL_API CellularPanel : public OverlayPanel { -public: - CellularPanel(wxWindow * parent, wxWindowID id, - const wxPoint & pos, - const wxSize & size, - ViewInfo *viewInfo, - // default as for wxPanel: - long style = wxTAB_TRAVERSAL | wxNO_BORDER) - : OverlayPanel(parent, id, pos, size, style) - , mViewInfo( viewInfo ) - {} - - // Overridables: - - virtual AudacityProject *GetProject() const = 0; - - // Find track info by coordinate - struct FoundCell { - std::shared_ptr pCell; - wxRect rect; - }; - virtual FoundCell FindCell(int mouseX, int mouseY) = 0; - virtual wxRect FindRect(const TrackPanelCell &cell) = 0; - virtual TrackPanelCell *GetFocusedCell() = 0; - virtual void SetFocusedCell() = 0; - - virtual void ProcessUIHandleResult - (TrackPanelCell *pClickedCell, TrackPanelCell *pLatestCell, - unsigned refreshResult) = 0; - - virtual void UpdateStatusMessage( const wxString & ) = 0; - -public: - UIHandlePtr Target() - { - if (mTargets.size()) - return mTargets[mTarget]; - else - return {}; - } - - bool IsMouseCaptured(); - - wxCoord MostRecentXCoord() const { return mMouseMostRecentX; } - - void HandleCursorForPresentMouseState(bool doHit = true); - -protected: - bool HasEscape(); - bool CancelDragging(); - void ClearTargets() - { - // Forget the rotation of hit test candidates when the mouse moves from - // cell to cell or outside of the panel entirely. - mLastCell.reset(); - mTargets.clear(); - mTarget = 0; - mMouseOverUpdateFlags = 0; - } - void DoContextMenu( TrackPanelCell *pCell = nullptr ); - -private: - bool HasRotation(); - bool ChangeTarget(bool forward, bool cycle); - - void OnMouseEvent(wxMouseEvent & event); - void OnCaptureLost(wxMouseCaptureLostEvent & event); - void OnCaptureKey(wxCommandEvent & event); - void OnKeyDown(wxKeyEvent & event); - void OnChar(wxKeyEvent & event); - void OnKeyUp(wxKeyEvent & event); - - void OnSetFocus(wxFocusEvent & event); - void OnKillFocus(wxFocusEvent & event); - - void OnContextMenu(wxContextMenuEvent & event); - - void HandleInterruptedDrag(); - void Uncapture( wxMouseState *pState = nullptr ); - bool HandleEscapeKey(bool down); - void UpdateMouseState(const wxMouseState &state); - void HandleModifierKey(); - - void HandleClick( const TrackPanelMouseEvent &tpmEvent ); - void HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ); - - void HandleMotion( wxMouseState &state, bool doHit = true ); - void HandleMotion - ( const TrackPanelMouseState &tpmState, bool doHit = true ); - - -protected: - ViewInfo *mViewInfo; - -private: - UIHandlePtr mUIHandle; - - std::weak_ptr mLastCell; - std::vector mTargets; - size_t mTarget {}; - unsigned mMouseOverUpdateFlags{}; - -protected: - // To do: make a drawing method and make this private - wxMouseState mLastMouseState; - -private: - int mMouseMostRecentX; - int mMouseMostRecentY; - - std::weak_ptr mpClickedCell; - - bool mEnableTab{}; - - DECLARE_EVENT_TABLE() -}; - class AUDACITY_DLL_API TrackPanel final : public CellularPanel { public: @@ -606,7 +477,7 @@ protected: DECLARE_EVENT_TABLE() void ProcessUIHandleResult - (TrackPanelCell *pClickedCell, TrackPanelCell *pLatestCell, + (TrackPanelCell *pClickedTrack, TrackPanelCell *pLatestCell, unsigned refreshResult) override; void UpdateStatusMessage( const wxString &status ) override; diff --git a/win/Projects/Audacity/Audacity.vcxproj b/win/Projects/Audacity/Audacity.vcxproj index 5adb717ab..bc59e1dc5 100755 --- a/win/Projects/Audacity/Audacity.vcxproj +++ b/win/Projects/Audacity/Audacity.vcxproj @@ -138,6 +138,7 @@ + @@ -481,6 +482,7 @@ + diff --git a/win/Projects/Audacity/Audacity.vcxproj.filters b/win/Projects/Audacity/Audacity.vcxproj.filters index c046afba1..64f352cc6 100755 --- a/win/Projects/Audacity/Audacity.vcxproj.filters +++ b/win/Projects/Audacity/Audacity.vcxproj.filters @@ -1082,6 +1082,9 @@ src\widgets + + src + @@ -2158,6 +2161,9 @@ src\widgets + + src + From 4e1a80f5acbbc31fe423389be77ca72de7862a66 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 2 Aug 2018 16:46:18 -0400 Subject: [PATCH 09/12] Move CellularPanel data members into a state structure out of the .h file --- src/CellularPanel.cpp | 176 +++++++++++++++++++++++++++++------------- src/CellularPanel.h | 47 +++-------- 2 files changed, 132 insertions(+), 91 deletions(-) diff --git a/src/CellularPanel.cpp b/src/CellularPanel.cpp index 16e513044..af0831386 100644 --- a/src/CellularPanel.cpp +++ b/src/CellularPanel.cpp @@ -35,6 +35,23 @@ #include "HitTestResult.h" #include "RefreshCode.h" +struct CellularPanel::State +{ + UIHandlePtr mUIHandle; + + std::weak_ptr mLastCell; + std::vector mTargets; + size_t mTarget {}; + unsigned mMouseOverUpdateFlags{}; + + int mMouseMostRecentX; + int mMouseMostRecentY; + + std::weak_ptr mpClickedCell; + + bool mEnableTab{}; +}; + BEGIN_EVENT_TABLE(CellularPanel, OverlayPanel) EVT_MOUSE_EVENTS(CellularPanel::OnMouseEvent) @@ -48,9 +65,24 @@ BEGIN_EVENT_TABLE(CellularPanel, OverlayPanel) EVT_CONTEXT_MENU(CellularPanel::OnContextMenu) END_EVENT_TABLE() +CellularPanel::CellularPanel( + wxWindow * parent, wxWindowID id, + const wxPoint & pos, const wxSize & size, + ViewInfo *viewInfo, + long style) +: OverlayPanel(parent, id, pos, size, style) +, mViewInfo( viewInfo ) +, mState{ std::make_unique() } +{ +} + +CellularPanel::~CellularPanel() = default; + void CellularPanel::HandleInterruptedDrag() { - if (mUIHandle && mUIHandle->StopsOnKeystroke() ) { + auto &state = *mState; + if (state.mUIHandle && + state.mUIHandle->StopsOnKeystroke() ) { // The bogus id isn't used anywhere, but may help with debugging. // as this is sending a bogus mouse up. The mouse button is still actually down // and may go up again. @@ -78,18 +110,19 @@ void CellularPanel::Uncapture(wxMouseState *pState) bool CellularPanel::CancelDragging() { - if (mUIHandle) { + auto &state = *mState; + if (state.mUIHandle) { // copy shared_ptr for safety, as in HandleClick - auto handle = mUIHandle; + auto handle = state.mUIHandle; // UIHANDLE CANCEL UIHandle::Result refreshResult = handle->Cancel(GetProject()); - auto pClickedCell = mpClickedCell.lock(); + auto pClickedCell = state.mpClickedCell.lock(); if (pClickedCell) ProcessUIHandleResult( pClickedCell.get(), {}, - refreshResult | mMouseOverUpdateFlags ); - mpClickedCell.reset(); - mUIHandle.reset(), handle.reset(), ClearTargets(); + refreshResult | state.mMouseOverUpdateFlags ); + state.mpClickedCell.reset(); + state.mUIHandle.reset(), handle.reset(), ClearTargets(); Uncapture(); return true; } @@ -109,7 +142,8 @@ bool CellularPanel::HandleEscapeKey(bool down) } } - if (mUIHandle) { + auto &state = *mState; + if (state.mUIHandle) { CancelDragging(); return true; } @@ -183,7 +217,8 @@ void CellularPanel::HandleMotion( wxMouseState &inState, bool doHit ) void CellularPanel::HandleMotion ( const TrackPanelMouseState &tpmState, bool doHit ) { - auto handle = mUIHandle; + auto &state = *mState; + auto handle = state.mUIHandle; auto newCell = tpmState.pCell; @@ -196,15 +231,15 @@ void CellularPanel::HandleMotion handle = Target(); // Assume cell does not change but target does - refreshCode = mMouseOverUpdateFlags; - mMouseOverUpdateFlags = 0; + refreshCode = state.mMouseOverUpdateFlags; + state.mMouseOverUpdateFlags = 0; } - else if ( !mUIHandle ) { + else if ( !state.mUIHandle ) { // Not yet dragging. - auto oldCell = mLastCell.lock(); + auto oldCell = state.mLastCell.lock(); - unsigned updateFlags = mMouseOverUpdateFlags; + unsigned updateFlags = state.mMouseOverUpdateFlags; // First check whether crossing cell to cell if ( newCell == oldCell ) @@ -220,29 +255,29 @@ void CellularPanel::HandleMotion } auto oldHandle = Target(); - auto oldPosition = mTarget; + auto oldPosition = state.mTarget; // Now do the // UIHANDLE HIT TEST ! - mTargets = newCell->HitTest(tpmState, GetProject()); + state.mTargets = newCell->HitTest(tpmState, GetProject()); - mTarget = 0; + state.mTarget = 0; // Find the old target's NEW place if we can if (oldHandle) { - auto begin = mTargets.begin(), end = mTargets.end(), + auto begin = state.mTargets.begin(), end = state.mTargets.end(), iter = std::find(begin, end, oldHandle); if (iter != end) { size_t newPosition = iter - begin; if (newPosition <= oldPosition) - mTarget = newPosition; + state.mTarget = newPosition; // else, some NEW hit and this position takes priority } } handle = Target(); - mLastCell = newCell; + state.mLastCell = newCell; if (!oldCell && oldHandle != handle) // Did not move cell to cell, but did change the target @@ -262,7 +297,7 @@ void CellularPanel::HandleMotion auto code = handle->GetChangeHighlight(); handle->SetChangeHighlight(RefreshCode::RefreshNone); refreshCode |= code; - mMouseOverUpdateFlags |= code; + state.mMouseOverUpdateFlags |= code; } if (newCell && (!pCursor || status.empty() || tooltip.empty())) { @@ -301,8 +336,9 @@ void CellularPanel::HandleMotion bool CellularPanel::HasRotation() { + auto &state = *mState; // Is there a nontrivial TAB key rotation? - if ( mTargets.size() > 1 ) + if ( state.mTargets.size() > 1 ) return true; auto target = Target(); return target && target->HasRotation(); @@ -313,17 +349,19 @@ bool CellularPanel::HasEscape() if (IsMouseCaptured()) return true; - if (mTarget + 1 == mTargets.size() && + auto &state = *mState; + if (state.mTarget + 1 == state.mTargets.size() && Target() && !Target()->HasEscape()) return false; - return mTargets.size() > 0; + return state.mTargets.size() > 0; } bool CellularPanel::ChangeTarget(bool forward, bool cycle) { - auto size = mTargets.size(); + auto &state = *mState; + auto size = state.mTargets.size(); auto target = Target(); if (target && target->HasRotation()) { @@ -337,16 +375,16 @@ bool CellularPanel::ChangeTarget(bool forward, bool cycle) } if (!cycle && - ((forward && mTarget + 1 == size) || - (!forward && mTarget == 0))) + ((forward && state.mTarget + 1 == size) || + (!forward && state.mTarget == 0))) return false; if (size > 1) { if (forward) - ++mTarget; + ++state.mTarget; else - mTarget += size - 1; - mTarget %= size; + state.mTarget += size - 1; + state.mTarget %= size; if (Target()) Target()->Enter(forward); return true; @@ -358,7 +396,8 @@ bool CellularPanel::ChangeTarget(bool forward, bool cycle) /// Determines if a modal tool is active bool CellularPanel::IsMouseCaptured() { - return mUIHandle != NULL; + auto &state = *mState; + return state.mUIHandle != NULL; } void CellularPanel::OnContextMenu(wxContextMenuEvent & WXUNUSED(event)) @@ -415,7 +454,8 @@ void CellularPanel::HandleWheelRotation( TrackPanelMouseEvent &tpmEvent ) void CellularPanel::OnCaptureKey(wxCommandEvent & event) { - mEnableTab = false; + auto &state = *mState; + state.mEnableTab = false; wxKeyEvent *kevent = static_cast(event.GetEventObject()); const auto code = kevent->GetKeyCode(); if ( WXK_ESCAPE != code ) @@ -554,8 +594,9 @@ void CellularPanel::OnCaptureLost(wxMouseCaptureLostEvent & WXUNUSED(event)) wxMouseEvent e(wxEVT_LEFT_UP); e.SetId( kCaptureLostEventId ); - e.m_x = mMouseMostRecentX; - e.m_y = mMouseMostRecentY; + auto &state = *mState; + e.m_x = state.mMouseMostRecentX; + e.m_y = state.mMouseMostRecentY; OnMouseEvent(e); } @@ -602,8 +643,9 @@ try event.ResumePropagation(wxEVENT_PROPAGATE_MAX); } - mMouseMostRecentX = event.m_x; - mMouseMostRecentY = event.m_y; + auto &state = *mState; + state.mMouseMostRecentX = event.m_x; + state.mMouseMostRecentY = event.m_y; if (event.LeftDown()) { // The activate event is used to make the @@ -618,7 +660,7 @@ try if (event.Leaving()) { - if ( !mUIHandle ) + if ( !state.mUIHandle ) ClearTargets(); auto buttons = @@ -640,21 +682,21 @@ try } } - if (mUIHandle) { - auto pClickedCell = mpClickedCell.lock(); + if (state.mUIHandle) { + auto pClickedCell = state.mpClickedCell.lock(); if (event.Dragging()) { // UIHANDLE DRAG // copy shared_ptr for safety, as in HandleClick - auto handle = mUIHandle; + auto handle = state.mUIHandle; const UIHandle::Result refreshResult = handle->Drag( tpmEvent, GetProject() ); ProcessUIHandleResult (pClickedCell.get(), pCell.get(), refreshResult); - mMouseOverUpdateFlags |= refreshResult; + state.mMouseOverUpdateFlags |= refreshResult; if (refreshResult & RefreshCode::Cancelled) { // Drag decided to abort itself - mUIHandle.reset(), handle.reset(), ClearTargets(); - mpClickedCell.reset(); + state.mUIHandle.reset(), handle.reset(), ClearTargets(); + state.mpClickedCell.reset(); Uncapture( &event ); } else { @@ -665,14 +707,14 @@ try } else if (event.ButtonUp()) { // UIHANDLE RELEASE - unsigned moreFlags = mMouseOverUpdateFlags; + unsigned moreFlags = state.mMouseOverUpdateFlags; UIHandle::Result refreshResult = - mUIHandle->Release( tpmEvent, GetProject(), this ); + state.mUIHandle->Release( tpmEvent, GetProject(), this ); ProcessUIHandleResult (pClickedCell.get(), pCell.get(), refreshResult | moreFlags); - mUIHandle.reset(), ClearTargets(); - mpClickedCell.reset(); + state.mUIHandle.reset(), ClearTargets(); + state.mpClickedCell.reset(); // will also Uncapture() below } } @@ -720,19 +762,20 @@ void CellularPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) HandleMotion( tpmState ); } - mUIHandle = Target(); + auto &state = *mState; + state.mUIHandle = Target(); - if (mUIHandle) { + if (state.mUIHandle) { // UIHANDLE CLICK // Make another shared pointer to the handle, in case recursive // event dispatching otherwise tries to delete the handle. - auto handle = mUIHandle; + auto handle = state.mUIHandle; UIHandle::Result refreshResult = handle->Click( tpmEvent, GetProject() ); if (refreshResult & RefreshCode::Cancelled) - mUIHandle.reset(), handle.reset(), ClearTargets(); + state.mUIHandle.reset(), handle.reset(), ClearTargets(); else { - mpClickedCell = pCell; + state.mpClickedCell = pCell; // Perhaps the clicked handle wants to update cursor and state message // after a click. @@ -745,7 +788,7 @@ void CellularPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) } ProcessUIHandleResult( pCell.get(), pCell.get(), refreshResult); - mMouseOverUpdateFlags |= refreshResult; + state.mMouseOverUpdateFlags |= refreshResult; } } @@ -782,3 +825,30 @@ void CellularPanel::OnKillFocus(wxFocusEvent & WXUNUSED(event)) } Refresh( false); } + +UIHandlePtr CellularPanel::Target() +{ + auto &state = *mState; + if (state.mTargets.size()) + return state.mTargets[state.mTarget]; + else + return {}; +} + +wxCoord CellularPanel::MostRecentXCoord() const +{ + auto &state = *mState; + return state.mMouseMostRecentX; +} + +void CellularPanel::ClearTargets() +{ + auto &state = *mState; + // Forget the rotation of hit test candidates when the mouse moves from + // cell to cell or outside of the panel entirely. + state.mLastCell.reset(); + state.mTargets.clear(); + state.mTarget = 0; + state.mMouseOverUpdateFlags = 0; +} + diff --git a/src/CellularPanel.h b/src/CellularPanel.h index a576b823e..e7a145b9c 100644 --- a/src/CellularPanel.h +++ b/src/CellularPanel.h @@ -35,10 +35,8 @@ public: const wxSize & size, ViewInfo *viewInfo, // default as for wxPanel: - long style = wxTAB_TRAVERSAL | wxNO_BORDER) - : OverlayPanel(parent, id, pos, size, style) - , mViewInfo( viewInfo ) - {} + long style = wxTAB_TRAVERSAL | wxNO_BORDER); + ~CellularPanel() override; // Overridables: @@ -61,17 +59,11 @@ public: virtual void UpdateStatusMessage( const wxString & ) = 0; public: - UIHandlePtr Target() - { - if (mTargets.size()) - return mTargets[mTarget]; - else - return {}; - } + UIHandlePtr Target(); bool IsMouseCaptured(); - wxCoord MostRecentXCoord() const { return mMouseMostRecentX; } + wxCoord MostRecentXCoord() const; void HandleCursorForPresentMouseState(bool doHit = true); @@ -79,15 +71,7 @@ protected: bool HasEscape(); bool CancelDragging(); void DoContextMenu( TrackPanelCell *pCell = nullptr ); - void ClearTargets() - { - // Forget the rotation of hit test candidates when the mouse moves from - // cell to cell or outside of the panel entirely. - mLastCell.reset(); - mTargets.clear(); - mTarget = 0; - mMouseOverUpdateFlags = 0; - } + void ClearTargets(); private: bool HasRotation(); @@ -121,26 +105,13 @@ private: protected: ViewInfo *mViewInfo; - -private: - UIHandlePtr mUIHandle; - - std::weak_ptr mLastCell; - std::vector mTargets; - size_t mTarget {}; - unsigned mMouseOverUpdateFlags{}; - -protected: + // To do: make a drawing method and make this private wxMouseState mLastMouseState; - + private: - int mMouseMostRecentX; - int mMouseMostRecentY; - - std::weak_ptr mpClickedCell; - - bool mEnableTab{}; + struct State; + std::unique_ptr mState; DECLARE_EVENT_TABLE() }; From 7cbe85facf41aa1b662c17a7e7c712cf64c75422 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Fri, 6 Jul 2018 20:12:58 -0400 Subject: [PATCH 10/12] Add CellularPanel::LastCell --- src/CellularPanel.cpp | 5 +++++ src/CellularPanel.h | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/CellularPanel.cpp b/src/CellularPanel.cpp index af0831386..01d033800 100644 --- a/src/CellularPanel.cpp +++ b/src/CellularPanel.cpp @@ -852,3 +852,8 @@ void CellularPanel::ClearTargets() state.mMouseOverUpdateFlags = 0; } +std::shared_ptr CellularPanel::LastCell() const +{ + auto &state = *mState; + return state.mLastCell.lock(); +} diff --git a/src/CellularPanel.h b/src/CellularPanel.h index e7a145b9c..de3d27217 100644 --- a/src/CellularPanel.h +++ b/src/CellularPanel.h @@ -61,6 +61,8 @@ public: public: UIHandlePtr Target(); + std::shared_ptr LastCell() const; + bool IsMouseCaptured(); wxCoord MostRecentXCoord() const; From ff98ca5a32112525fb66af6fc875280a3a757d4f Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 2 Aug 2018 11:45:27 -0400 Subject: [PATCH 11/12] CellularPanel saves and restores focus for each click-drag-release... ... while the project makes note of which window has temporarily given up the focus; and TrackPanel can detect that, so that the yellow border of the focused track is still drawn while the panel "lends" the focus. Why do all this? So that, when the ruler is another CellularPanel, ESC key will work to abort drags in the ruler, but TrackPanel appearance won't change during the drags. --- src/CellularPanel.cpp | 20 +++++++++++++++----- src/Project.cpp | 5 +++++ src/Project.h | 6 ++++++ src/TrackPanel.cpp | 2 +- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/CellularPanel.cpp b/src/CellularPanel.cpp index 01d033800..0d8fe78fd 100644 --- a/src/CellularPanel.cpp +++ b/src/CellularPanel.cpp @@ -106,6 +106,10 @@ void CellularPanel::Uncapture(wxMouseState *pState) if (HasCapture()) ReleaseMouse(); HandleMotion( *pState ); + + auto lender = GetProject()->mFocusLender.get(); + if (lender) + lender->SetFocus(); } bool CellularPanel::CancelDragging() @@ -654,10 +658,6 @@ try GetParent()->GetEventHandler()->ProcessEvent(e); } - if (event.ButtonDown()) { - SetFocus(); - } - if (event.Leaving()) { if ( !state.mUIHandle ) @@ -775,6 +775,9 @@ void CellularPanel::HandleClick( const TrackPanelMouseEvent &tpmEvent ) if (refreshResult & RefreshCode::Cancelled) state.mUIHandle.reset(), handle.reset(), ClearTargets(); else { + if( !HasFocus() ) + SetFocus(); + state.mpClickedCell = pCell; // Perhaps the clicked handle wants to update cursor and state message @@ -812,13 +815,20 @@ void CellularPanel::DoContextMenu( TrackPanelCell *pCell ) ProcessUIHandleResult(pCell, pCell, refreshResult); } -void CellularPanel::OnSetFocus(wxFocusEvent & WXUNUSED(event)) +void CellularPanel::OnSetFocus(wxFocusEvent &event) { + auto &ptr = GetProject()->mFocusLender; + if ( !ptr ) + ptr = event.GetWindow(); + SetFocusedCell(); } void CellularPanel::OnKillFocus(wxFocusEvent & WXUNUSED(event)) { + // Forget any borrowing of focus + GetProject()->mFocusLender = NULL; + if (AudacityProject::HasKeyboardCapture(this)) { AudacityProject::ReleaseKeyboard(this); diff --git a/src/Project.cpp b/src/Project.cpp index 8d7d5a6d4..f53bd893e 100644 --- a/src/Project.cpp +++ b/src/Project.cpp @@ -6258,3 +6258,8 @@ void AudacityProject::PlaybackScroller::OnTimer(wxCommandEvent &event) trackPanel->Refresh(false); } } + +bool AudacityProject::IsFocused( const wxWindow *window ) const +{ + return window == mFocusLender || window == wxWindow::FindFocus(); +} diff --git a/src/Project.h b/src/Project.h index 828883b0f..2a7e8c6f1 100644 --- a/src/Project.h +++ b/src/Project.h @@ -834,6 +834,12 @@ public: std::shared_ptr GetBackgroundCell() const { return mBackgroundCell; } + wxWindowRef mFocusLender; + + // Return true if the window is really focused, or if focus was borrowed + // from it + bool IsFocused( const wxWindow *window ) const; + DECLARE_EVENT_TABLE() }; diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index 7118d7f4e..44ecd112b 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -1270,7 +1270,7 @@ void TrackPanel::DrawEverythingElse(TrackPanelDrawingContext &context, // if (GetFocusedTrack() != NULL) { // the highlight was reportedly drawn even when something else // was the focus and no highlight should be drawn. -RBD - if (GetFocusedTrack() != NULL && wxWindow::FindFocus() == this) { + if (GetFocusedTrack() != NULL && GetProject()->IsFocused( this )) { HighlightFocusedTrack(dc, focusRect); } From 0fc4747742b4ad97bdec007c9ab9822db2a01739 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 26 Jul 2018 08:47:51 -0400 Subject: [PATCH 12/12] CellularPanel handles leave and capture loss like other transitions --- src/CellularPanel.cpp | 28 ++++++++++++++++++++-------- src/CellularPanel.h | 1 + 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/CellularPanel.cpp b/src/CellularPanel.cpp index 0d8fe78fd..0dc127aee 100644 --- a/src/CellularPanel.cpp +++ b/src/CellularPanel.cpp @@ -263,7 +263,9 @@ void CellularPanel::HandleMotion // Now do the // UIHANDLE HIT TEST ! - state.mTargets = newCell->HitTest(tpmState, GetProject()); + state.mTargets.clear(); + if (newCell) + state.mTargets = newCell->HitTest(tpmState, GetProject()); state.mTarget = 0; @@ -334,8 +336,18 @@ void CellularPanel::HandleMotion if (pCursor) SetCursor( *pCursor ); - ProcessUIHandleResult( - newCell.get(), newCell.get(), refreshCode); + if (newCell) + ProcessUIHandleResult(newCell.get(), newCell.get(), refreshCode); +} + +void CellularPanel::Leave() +{ + // Make transition into an empty CellularPanel state + auto state = ::wxGetMouseState(); + const wxRect rect; + std::shared_ptr pCell; + TrackPanelMouseState tpmState{ state, rect, pCell }; + HandleMotion( tpmState ); } bool CellularPanel::HasRotation() @@ -589,16 +601,17 @@ void CellularPanel::OnKeyUp(wxKeyEvent & event) event.Skip(); } -/// Should handle the case when the mouse capture is lost. +/// Should handle the case when the mouse capture is lost. (MSW only) void CellularPanel::OnCaptureLost(wxMouseCaptureLostEvent & WXUNUSED(event)) { - ClearTargets(); + auto &state = *mState; + state.mUIHandle.reset(); + Leave(); // This is bad. We are lying abou the event by saying it is a mouse up. wxMouseEvent e(wxEVT_LEFT_UP); e.SetId( kCaptureLostEventId ); - auto &state = *mState; e.m_x = state.mMouseMostRecentX; e.m_y = state.mMouseMostRecentY; @@ -660,8 +673,7 @@ try if (event.Leaving()) { - if ( !state.mUIHandle ) - ClearTargets(); + Leave(); auto buttons = // Bug 1325: button state in Leaving events is unreliable on Mac. diff --git a/src/CellularPanel.h b/src/CellularPanel.h index de3d27217..c41d3bdb2 100644 --- a/src/CellularPanel.h +++ b/src/CellularPanel.h @@ -103,6 +103,7 @@ private: void HandleMotion( wxMouseState &state, bool doHit = true ); void HandleMotion ( const TrackPanelMouseState &tpmState, bool doHit = true ); + void Leave(); protected: