1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-20 14:20:06 +02:00

Smuggle AudacityProject into LabelTrackView::GetSelectedIndex() ...

... and eliminate some more calls to GetActiveProject
This commit is contained in:
Paul Licameli 2019-06-28 13:31:12 -04:00
parent 30715e4fe2
commit f77ddc43d9
9 changed files with 105 additions and 81 deletions

View File

@ -112,7 +112,7 @@ bool SetLabelCommand::Apply(const CommandContext & context)
double t1 = pLabel->selectedRegion.t1();
selectedRegion.setTimes( t0, t1);
}
else if( view.GetSelectedIndex() == ii )
else if( view.GetSelectedIndex( context.project ) == ii )
view.SetSelectedIndex( -1 );
}

View File

@ -48,19 +48,19 @@ bool DoPasteText(AudacityProject &project)
for (auto pLabelTrack : tracks.Any<LabelTrack>())
{
// Does this track have an active label?
if (LabelTrackView::Get( *pLabelTrack ).HasSelection()) {
if (LabelTrackView::Get( *pLabelTrack ).HasSelection( project )) {
// Yes, so try pasting into it
auto &view = LabelTrackView::Get( *pLabelTrack );
if (view.PasteSelectedText(selectedRegion.t0(),
selectedRegion.t1()))
if (view.PasteSelectedText( project, selectedRegion.t0(),
selectedRegion.t1() ))
{
ProjectHistory::Get( project )
.PushState(_("Pasted text from the clipboard"), _("Paste"));
// Make sure caret is in view
int x;
if (view.CalcCursorX(&x)) {
if (view.CalcCursorX( project, &x )) {
trackPanel.ScrollIntoView(x);
}
@ -246,7 +246,7 @@ void OnCut(const CommandContext &context)
for (auto lt : tracks.Selected< LabelTrack >()) {
auto &view = LabelTrackView::Get( *lt );
if (view.CutSelectedText()) {
if (view.CutSelectedText( context.project )) {
trackPanel.Refresh(false);
return;
}
@ -354,7 +354,7 @@ void OnCopy(const CommandContext &context)
for (auto lt : tracks.Selected< LabelTrack >()) {
auto &view = LabelTrackView::Get( *lt );
if (view.CopySelectedText()) {
if (view.CopySelectedText( context.project )) {
//trackPanel.Refresh(false);
return;
}
@ -1034,8 +1034,11 @@ const ReservedCommandFlag
CutCopyAvailableFlag{
[](const AudacityProject &project){
auto range = TrackList::Get( project ).Any<const LabelTrack>()
+ [](const LabelTrack *pTrack){
return LabelTrackView::Get( *pTrack ).IsTextSelected();
+ [&](const LabelTrack *pTrack){
return LabelTrackView::Get( *pTrack ).IsTextSelected(
// unhappy const_cast because track focus might be set
const_cast<AudacityProject&>(project)
);
};
if ( !range.empty() )
return true;

View File

@ -325,8 +325,8 @@ void OnPasteNewLabel(const CommandContext &context)
// Paul L: copy whatever defines the selected region, not just times
auto &view = LabelTrackView::Get( *lt );
view.AddLabel(selectedRegion);
if (view.PasteSelectedText(selectedRegion.t0(),
selectedRegion.t1()))
if (view.PasteSelectedText( context.project, selectedRegion.t0(),
selectedRegion.t1() ))
bPastedSomething = true;
// Set previous track

View File

@ -275,7 +275,8 @@ static int Constrain( int value, int min, int max )
}
bool LabelGlyphHandle::HandleGlyphDragRelease
(LabelTrackHit &hit, const wxMouseEvent & evt,
(AudacityProject &project,
LabelTrackHit &hit, const wxMouseEvent & evt,
wxRect & r, const ZoomInfo &zoomInfo,
SelectedRegion *newSel)
{
@ -334,9 +335,9 @@ bool LabelGlyphHandle::HandleGlyphDragRelease
}
const auto &view = LabelTrackView::Get( *pTrack );
if( view.HasSelection() )
if( view.HasSelection( project ) )
{
auto selIndex = view.GetSelectedIndex();
auto selIndex = view.GetSelectedIndex( project );
//Set the selection region to be equal to
//the NEW size of the label.
*newSel = mLabels[ selIndex ].selectedRegion;
@ -355,7 +356,7 @@ UIHandle::Result LabelGlyphHandle::Drag
const wxMouseEvent &event = evt.event;
auto &viewInfo = ViewInfo::Get( *pProject );
HandleGlyphDragRelease(
*mpHit, event, mRect, viewInfo, &viewInfo.selectedRegion);
*pProject, *mpHit, event, mRect, viewInfo, &viewInfo.selectedRegion);
// Refresh all so that the change of selection is redrawn in all tracks
return result | RefreshCode::RefreshAll | RefreshCode::DrawOverlays;
@ -376,7 +377,7 @@ UIHandle::Result LabelGlyphHandle::Release
const wxMouseEvent &event = evt.event;
auto &viewInfo = ViewInfo::Get( *pProject );
if (HandleGlyphDragRelease(
*mpHit, event, mRect, viewInfo, &viewInfo.selectedRegion)) {
*pProject, *mpHit, event, mRect, viewInfo, &viewInfo.selectedRegion)) {
ProjectHistory::Get( *pProject ).PushState(_("Modified Label"),
_("Label Edit"),
UndoPush::CONSOLIDATE);

View File

@ -94,7 +94,8 @@ private:
const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo,
SelectedRegion *newSel);
bool HandleGlyphDragRelease
(LabelTrackHit &hit,
(AudacityProject &project,
LabelTrackHit &hit,
const wxMouseEvent & evt, wxRect & r, const ZoomInfo &zoomInfo,
SelectedRegion *newSel);

View File

@ -71,7 +71,12 @@ LabelTextHandle::~LabelTextHandle()
{
}
void LabelTextHandle::HandleTextClick(const wxMouseEvent & evt,
void LabelTextHandle::HandleTextClick(AudacityProject &
#if defined(__WXGTK__) && (HAVE_GTK)
project
#endif
,
const wxMouseEvent & evt,
const wxRect & r, const ZoomInfo &zoomInfo,
SelectedRegion *newSel)
{
@ -138,7 +143,7 @@ void LabelTextHandle::HandleTextClick(const wxMouseEvent & evt,
if (evt.MiddleDown()) {
// Paste text, making a NEW label if none is selected.
wxTheClipboard->UsePrimarySelection(true);
view.PasteSelectedText(newSel->t0(), newSel->t1());
view.PasteSelectedText(project, newSel->t0(), newSel->t1());
wxTheClipboard->UsePrimarySelection(false);
}
#endif
@ -163,7 +168,8 @@ UIHandle::Result LabelTextHandle::Click
auto &viewInfo = ViewInfo::Get( *pProject );
mSelectedRegion = viewInfo.selectedRegion;
HandleTextClick( event, evt.rect, viewInfo, &viewInfo.selectedRegion );
HandleTextClick( *pProject,
event, evt.rect, viewInfo, &viewInfo.selectedRegion );
{
// IF the user clicked a label, THEN select all other tracks by Label
@ -192,7 +198,8 @@ UIHandle::Result LabelTextHandle::Click
return result | RefreshCode::RefreshCell | RefreshCode::UpdateSelection;
}
void LabelTextHandle::HandleTextDragRelease(const wxMouseEvent & evt)
void LabelTextHandle::HandleTextDragRelease(
AudacityProject &project, const wxMouseEvent & evt)
{
auto pTrack = mpLT.lock();
if (!pTrack)
@ -231,13 +238,13 @@ void LabelTextHandle::HandleTextDragRelease(const wxMouseEvent & evt)
}
if (evt.RightUp()) {
const auto selIndex = view.GetSelectedIndex();
const auto selIndex = view.GetSelectedIndex( project );
if ( selIndex != -1 &&
LabelTrackView::OverTextBox(
pTrack->GetLabel( selIndex ), evt.m_x, evt.m_y ) ) {
// popup menu for editing
// TODO: handle context menus via CellularPanel?
view.ShowContextMenu();
view.ShowContextMenu( project );
}
}
@ -247,13 +254,14 @@ void LabelTextHandle::HandleTextDragRelease(const wxMouseEvent & evt)
UIHandle::Result LabelTextHandle::Drag
(const TrackPanelMouseEvent &evt, AudacityProject *pProject)
{
auto &project = *pProject;
using namespace RefreshCode;
auto result = LabelDefaultClickHandle::Drag( evt, pProject );
const wxMouseEvent &event = evt.event;
auto pLT = TrackList::Get( *pProject ).Lock(mpLT);
if(pLT)
HandleTextDragRelease(event);
HandleTextDragRelease( project, event );
// locate the initial mouse position
if (event.LeftIsDown()) {
@ -263,9 +271,9 @@ UIHandle::Result LabelTextHandle::Drag
auto pView = pLT ? &LabelTrackView::Get( *pLT ) : nullptr;
if (pLT &&
(pView->GetSelectedIndex() != -1) &&
(pView->GetSelectedIndex( project ) != -1) &&
LabelTrackView::OverTextBox(
pLT->GetLabel(pView->GetSelectedIndex()),
pLT->GetLabel(pView->GetSelectedIndex( project )),
mLabelTrackStartXPos,
mLabelTrackStartYPos))
mLabelTrackStartYPos = -1;
@ -302,7 +310,7 @@ UIHandle::Result LabelTextHandle::Release
const wxMouseEvent &event = evt.event;
auto pLT = TrackList::Get( *pProject ).Lock(mpLT);
if (pLT)
HandleTextDragRelease(event);
HandleTextDragRelease( *pProject, event );
// handle mouse left button up
if (event.LeftUp())

View File

@ -57,9 +57,11 @@ public:
private:
void HandleTextClick
(const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo,
(AudacityProject &project,
const wxMouseEvent & evt, const wxRect & r, const ZoomInfo &zoomInfo,
SelectedRegion *newSel);
void HandleTextDragRelease(const wxMouseEvent & evt);
void HandleTextDragRelease(
AudacityProject &project, const wxMouseEvent & evt);
std::weak_ptr<LabelTrack> mpLT {};
int mLabelNum{ -1 };

View File

@ -636,9 +636,9 @@ void getXPos( const LabelStruct &ls, wxDC & dc, int * xPos1, int cursorPos)
}
}
bool LabelTrackView::CalcCursorX(int * x) const
bool LabelTrackView::CalcCursorX( AudacityProject &project, int * x) const
{
if ( HasSelection() ) {
if ( HasSelection( project ) ) {
wxMemoryDC dc;
if (msFont.Ok()) {
@ -767,6 +767,8 @@ void LabelTrackView::Draw
DrawGlyphs( dc, labelStruct, r, GlyphLeft, GlyphRight );
}}
auto &project = *artist->parent->GetProject();
// Draw the label boxes.
{
#ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
@ -779,7 +781,7 @@ void LabelTrackView::Draw
#ifdef EXPERIMENTAL_TRACK_PANEL_HIGHLIGHTING
highlight = highlightTrack && target->GetLabelNum() == i;
#endif
bool selected = GetSelectedIndex() == i;
bool selected = GetSelectedIndex( project ) == i;
if( selected )
dc.SetBrush( AColor::labelTextEditBrush );
@ -793,7 +795,7 @@ void LabelTrackView::Draw
}
// Draw highlights
if ( (mInitialCursorPos != mCurrentCursorPos) && HasSelection() )
if ( (mInitialCursorPos != mCurrentCursorPos) && HasSelection( project ) )
{
int xpos1, xpos2;
CalcHighlightXs(&xpos1, &xpos2);
@ -803,15 +805,15 @@ void LabelTrackView::Draw
// Draw the text and the label boxes.
{ int i = -1; for (const auto &labelStruct : mLabels) { ++i;
if( GetSelectedIndex() == i )
if( GetSelectedIndex( project ) == i )
dc.SetBrush(AColor::labelTextEditBrush);
DrawText( dc, labelStruct, r );
if( GetSelectedIndex() == i )
if( GetSelectedIndex( project ) == i )
dc.SetBrush(AColor::labelTextNormalBrush);
}}
// Draw the cursor, if there is one.
if( mDrawCursor && HasSelection() )
if( mDrawCursor && HasSelection( project ) )
{
const auto &labelStruct = mLabels[mSelIndex];
int xPos = labelStruct.xText;
@ -933,9 +935,9 @@ void LabelTrackView::calculateFontHeight(wxDC & dc)
mFontHeight += CursorExtraHeight - (charLeading+charDescent);
}
bool LabelTrackView::IsTextSelected() const
bool LabelTrackView::IsTextSelected( AudacityProject &project ) const
{
if ( !HasSelection() )
if ( !HasSelection( project ) )
return false;
if (mCurrentCursorPos == mInitialCursorPos)
return false;
@ -944,9 +946,9 @@ bool LabelTrackView::IsTextSelected() const
/// Cut the selected text in the text box
/// @return true if text is selected in text box, false otherwise
bool LabelTrackView::CutSelectedText()
bool LabelTrackView::CutSelectedText( AudacityProject &project )
{
if (!IsTextSelected())
if (!IsTextSelected( project ))
return false;
const auto pTrack = FindLabelTrack();
@ -991,9 +993,9 @@ bool LabelTrackView::CutSelectedText()
/// Copy the selected text in the text box
/// @return true if text is selected in text box, false otherwise
bool LabelTrackView::CopySelectedText()
bool LabelTrackView::CopySelectedText( AudacityProject &project )
{
if ( !HasSelection() )
if ( !HasSelection( project ) )
return false;
const auto pTrack = FindLabelTrack();
@ -1025,11 +1027,12 @@ bool LabelTrackView::CopySelectedText()
// PRL: should this set other fields of the label selection?
/// Paste the text on the clipboard to text box
/// @return true if mouse is clicked in text box, false otherwise
bool LabelTrackView::PasteSelectedText(double sel0, double sel1)
bool LabelTrackView::PasteSelectedText(
AudacityProject &project, double sel0, double sel1 )
{
const auto pTrack = FindLabelTrack();
if ( !HasSelection() )
if ( !HasSelection( project ) )
AddLabel(SelectedRegion(sel0, sel1));
wxString text, left, right;
@ -1076,7 +1079,7 @@ bool LabelTrackView::IsTextClipSupported()
}
int LabelTrackView::GetSelectedIndex() const
int LabelTrackView::GetSelectedIndex( AudacityProject & ) const
{
// may make delayed update of mutable mSelIndex after track selection change
auto track = FindLabelTrack();
@ -1209,7 +1212,7 @@ static bool IsGoodLabelEditKey(const wxKeyEvent & evt)
// Check for keys that we will process
bool LabelTrackView::DoCaptureKey(
const AudacityProject &project, wxKeyEvent & event )
AudacityProject &project, wxKeyEvent & event )
{
// Check for modifiers and only allow shift
int mods = event.GetModifiers();
@ -1225,7 +1228,7 @@ bool LabelTrackView::DoCaptureKey(
!mLabels.empty())
return true;
if ( HasSelection() ) {
if ( HasSelection( project ) ) {
if (IsGoodLabelEditKey(event)) {
return true;
}
@ -1294,7 +1297,7 @@ unsigned LabelTrackView::KeyDown(
// Make sure caret is in view
int x;
if (CalcCursorX(&x))
if (CalcCursorX( *project, &x ))
TrackPanel::Get( *project ).ScrollIntoView(x);
// If selection modified, refresh
@ -1352,7 +1355,7 @@ bool LabelTrackView::DoKeyDown(
// All editing keys are only active if we're currently editing a label
const auto pTrack = FindLabelTrack();
const auto &mLabels = pTrack->GetLabels();
if ( HasSelection() ) {
if ( HasSelection( project ) ) {
auto labelStruct = mLabels[mSelIndex];
auto &title = labelStruct.title;
switch (keyCode) {
@ -1496,7 +1499,7 @@ bool LabelTrackView::DoKeyDown(
case '\x10': // OSX
case WXK_MENU:
case WXK_WINDOWS_MENU:
ShowContextMenu();
ShowContextMenu( project );
break;
default:
@ -1562,7 +1565,8 @@ bool LabelTrackView::DoKeyDown(
/// OnChar is called for incoming characters -- that's any keypress not handled
/// by OnKeyDown.
bool LabelTrackView::DoChar(
AudacityProject &project, SelectedRegion &WXUNUSED(newSel), wxKeyEvent & event)
AudacityProject &project, SelectedRegion &WXUNUSED(newSel),
wxKeyEvent & event)
{
// Check for modifiers and only allow shift.
//
@ -1588,7 +1592,7 @@ bool LabelTrackView::DoChar(
// If we've reached this point and aren't currently editing, add NEW label
const auto pTrack = FindLabelTrack();
if ( !HasSelection() ) {
if ( !HasSelection( project ) ) {
// Don't create a NEW label for a space
if (wxIsspace(charCode)) {
event.Skip();
@ -1663,18 +1667,21 @@ enum
OnEditSelectedLabelID,
};
void LabelTrackView::ShowContextMenu()
void LabelTrackView::ShowContextMenu( AudacityProject &project )
{
wxWindow *parent = wxWindow::FindFocus();
// Bug 2044. parent can be nullptr after a context switch.
if( !parent )
parent = FindProjectFrame( GetActiveProject() );
parent = &GetProjectFrame( project );
if( parent )
{
wxMenu menu;
menu.Bind(wxEVT_MENU, &LabelTrackView::OnContextMenu, this);
menu.Bind(wxEVT_MENU,
[this, &project]( wxCommandEvent &event ){
OnContextMenu( project, event ); }
);
menu.Append(OnCutSelectedTextID, _("Cu&t"));
menu.Append(OnCopySelectedTextID, _("&Copy"));
@ -1682,13 +1689,13 @@ void LabelTrackView::ShowContextMenu()
menu.Append(OnDeleteSelectedLabelID, _("&Delete Label"));
menu.Append(OnEditSelectedLabelID, _("&Edit..."));
menu.Enable(OnCutSelectedTextID, IsTextSelected());
menu.Enable(OnCopySelectedTextID, IsTextSelected());
menu.Enable(OnCutSelectedTextID, IsTextSelected( project ));
menu.Enable(OnCopySelectedTextID, IsTextSelected( project ));
menu.Enable(OnPasteSelectedTextID, IsTextClipSupported());
menu.Enable(OnDeleteSelectedLabelID, true);
menu.Enable(OnEditSelectedLabelID, true);
if( !HasSelection() ) {
if( !HasSelection( project ) ) {
wxASSERT( false );
return;
}
@ -1704,7 +1711,7 @@ void LabelTrackView::ShowContextMenu()
}
int x = 0;
bool success = CalcCursorX(&x);
bool success = CalcCursorX( project, &x );
wxASSERT(success);
static_cast<void>(success); // Suppress unused variable warning if debug mode is disabled
@ -1712,18 +1719,18 @@ void LabelTrackView::ShowContextMenu()
}
}
void LabelTrackView::OnContextMenu(wxCommandEvent & evt)
void LabelTrackView::OnContextMenu(
AudacityProject &project, wxCommandEvent & evt )
{
AudacityProject *p = GetActiveProject();
auto &selectedRegion = ViewInfo::Get( *p ).selectedRegion;
auto &selectedRegion = ViewInfo::Get( project ).selectedRegion;
switch (evt.GetId())
{
/// Cut selected text if cut menu item is selected
case OnCutSelectedTextID:
if (CutSelectedText())
if (CutSelectedText( project ))
{
ProjectHistory::Get( *p ).PushState(_("Modified Label"),
ProjectHistory::Get( project ).PushState(_("Modified Label"),
_("Label Edit"),
UndoPush::CONSOLIDATE);
}
@ -1731,14 +1738,15 @@ void LabelTrackView::OnContextMenu(wxCommandEvent & evt)
/// Copy selected text if copy menu item is selected
case OnCopySelectedTextID:
CopySelectedText();
CopySelectedText( project );
break;
/// paste selected text if paste menu item is selected
case OnPasteSelectedTextID:
if (PasteSelectedText(selectedRegion.t0(), selectedRegion.t1()))
if (PasteSelectedText(
project, selectedRegion.t0(), selectedRegion.t1() ))
{
ProjectHistory::Get( *p ).PushState(_("Modified Label"),
ProjectHistory::Get( project ).PushState(_("Modified Label"),
_("Label Edit"),
UndoPush::CONSOLIDATE);
}
@ -1751,7 +1759,7 @@ void LabelTrackView::OnContextMenu(wxCommandEvent & evt)
{
const auto pTrack = FindLabelTrack();
pTrack->DeleteLabel(ndx);
ProjectHistory::Get( *p ).PushState(_("Deleted Label"),
ProjectHistory::Get( project ).PushState(_("Deleted Label"),
_("Label Edit"),
UndoPush::CONSOLIDATE);
}
@ -1761,7 +1769,7 @@ void LabelTrackView::OnContextMenu(wxCommandEvent & evt)
case OnEditSelectedLabelID: {
int ndx = GetLabelIndex(selectedRegion.t0(), selectedRegion.t1());
if (ndx != -1)
DoEditLabels(*p, FindLabelTrack().get(), ndx);
DoEditLabels( project, FindLabelTrack().get(), ndx );
}
break;
}
@ -1792,9 +1800,9 @@ void LabelTrackView::RemoveSelectedText()
mInitialCursorPos = mCurrentCursorPos = left.length();
}
bool LabelTrackView::HasSelection() const
bool LabelTrackView::HasSelection( AudacityProject &project ) const
{
const auto selIndex = GetSelectedIndex();
const auto selIndex = GetSelectedIndex( project );
return (selIndex >= 0 &&
selIndex < (int)FindLabelTrack()->GetLabels().size());
}

View File

@ -52,7 +52,7 @@ public:
static LabelTrackView &Get( LabelTrack& );
static const LabelTrackView &Get( const LabelTrack& );
bool DoCaptureKey(const AudacityProject &project, wxKeyEvent &event);
bool DoCaptureKey( AudacityProject &project, wxKeyEvent &event );
bool DoKeyDown(
AudacityProject &project, SelectedRegion &sel, wxKeyEvent & event);
bool DoChar(
@ -97,7 +97,7 @@ public:
AudacityProject &project, const SelectedRegion& region,
const wxString& initialValue, wxString& value);
bool IsTextSelected() const;
bool IsTextSelected( AudacityProject &project ) const;
private:
void CreateCustomGlyphs();
@ -108,12 +108,13 @@ public:
void Draw( TrackPanelDrawingContext &context, const wxRect & r ) const;
int GetSelectedIndex() const;
int GetSelectedIndex( AudacityProject &project ) const;
void SetSelectedIndex( int index );
bool CutSelectedText();
bool CopySelectedText();
bool PasteSelectedText(double sel0, double sel1);
bool CutSelectedText( AudacityProject &project );
bool CopySelectedText( AudacityProject &project );
bool PasteSelectedText(
AudacityProject &project, double sel0, double sel1 );
static void OverGlyph(
const LabelTrack &track, LabelTrackHit &hit, int x, int y );
@ -155,16 +156,16 @@ private:
public:
//get current cursor position,
// relative to the left edge of the track panel
bool CalcCursorX(int * x) const;
bool CalcCursorX( AudacityProject &project, int * x ) const;
private:
void CalcHighlightXs(int *x1, int *x2) const;
public:
void ShowContextMenu();
void ShowContextMenu( AudacityProject &project );
private:
void OnContextMenu(wxCommandEvent & evt);
void OnContextMenu( AudacityProject &project, wxCommandEvent & evt);
mutable int mSelIndex{-1}; /// Keeps track of the currently selected label
@ -212,7 +213,7 @@ private:
static void calculateFontHeight(wxDC & dc);
public:
bool HasSelection() const;
bool HasSelection( AudacityProject &project ) const;
private:
void RemoveSelectedText();