diff --git a/src/LabelTrack.cpp b/src/LabelTrack.cpp index d273aaad2..b0a5de393 100644 --- a/src/LabelTrack.cpp +++ b/src/LabelTrack.cpp @@ -99,7 +99,7 @@ LabelTrack::LabelTrack(DirManager * projDirManager): mSelIndex(-1), mMouseOverLabelLeft(-1), mMouseOverLabelRight(-1), - mpRestoreFocus(0), + mRestoreFocus(-1), mClipLen(0.0), mIsAdjustingLabel(false) { @@ -1841,9 +1841,14 @@ bool LabelTrack::OnKeyDown(SelectedRegion &newSel, wxKeyEvent & event) case WXK_NUMPAD_ENTER: case WXK_ESCAPE: - if (mpRestoreFocus) { - GetActiveProject()->GetTrackPanel()->SetFocusedTrack(mpRestoreFocus); - mpRestoreFocus = 0; + if (mRestoreFocus >= 0) { + TrackListIterator iter(GetActiveProject()->GetTracks()); + Track *track = iter.First(); + while (track && mRestoreFocus--) + track = iter.Next(); + if (track) + GetActiveProject()->GetTrackPanel()->SetFocusedTrack(track); + mRestoreFocus = -1; } mSelIndex = -1; break; @@ -2647,7 +2652,7 @@ int LabelTrack::GetLabelIndex(double t, double t1) } int LabelTrack::AddLabel(const SelectedRegion &selectedRegion, - const wxString &title, Track *pRestoreFocus) + const wxString &title, int restoreFocus) { LabelStruct *l = new LabelStruct(selectedRegion, title); mCurrentCursorPos = title.length(); @@ -2674,7 +2679,7 @@ int LabelTrack::AddLabel(const SelectedRegion &selectedRegion, // mDrawCursor flag will be reset once the action is complete. mDrawCursor = true; - mpRestoreFocus = pRestoreFocus; + mRestoreFocus = restoreFocus; return pos; } diff --git a/src/LabelTrack.h b/src/LabelTrack.h index 4e3d92184..e505fab08 100644 --- a/src/LabelTrack.h +++ b/src/LabelTrack.h @@ -205,7 +205,7 @@ class AUDACITY_DLL_API LabelTrack : public Track //This returns the index of the label we just added. int AddLabel(const SelectedRegion ®ion, const wxString &title = wxT(""), - Track *pRestoreFocus = 0); + int restoreFocus = -1); //And this tells us the index, if there is a label already there. int GetLabelIndex(double t, double t1); @@ -268,7 +268,7 @@ class AUDACITY_DLL_API LabelTrack : public Track bool mResetCursorPos; /// flag to reset cursor position(used in the dragging the glygh) bool mRightDragging; /// flag to tell if it's a valid dragging bool mDrawCursor; /// flag to tell if drawing the cursor or not - Track *mpRestoreFocus; /// Restore focus to this track when done editing + int mRestoreFocus; /// Restore focus to this track when done editing // Set in copied label tracks double mClipLen; diff --git a/src/Menus.cpp b/src/Menus.cpp index f24e3b99d..d38e526f8 100644 --- a/src/Menus.cpp +++ b/src/Menus.cpp @@ -6347,7 +6347,23 @@ int AudacityProject::DoAddLabel(const SelectedRegion ®ion, bool preserveFocus // SelectNone(); lt->SetSelected(true); - int index = lt->AddLabel(region, wxString(), (preserveFocus ? pFocusedTrack : 0)); + int focusTrackNumber = -1; + if (pFocusedTrack && preserveFocus) { + // Must remember the track to re-focus after finishing a label edit. + // do NOT identify it by a pointer, which might dangle! Identify + // by position. + TrackListIterator iter(GetTracks()); + Track *track = iter.First(); + do + ++focusTrackNumber; + while (track != pFocusedTrack && + NULL != (track = iter.Next())); + if (!track) + // How could we not find it? + focusTrackNumber = -1; + } + + int index = lt->AddLabel(region, wxString(), focusTrackNumber); PushState(_("Added label"), _("Label"));