Some uses of wxWindowRef, a weak pointer that won't become dangling

This commit is contained in:
Paul Licameli 2018-08-01 22:41:13 -04:00
parent 703bf2b647
commit 0dddcf44cb
5 changed files with 6 additions and 14 deletions

View File

@ -90,7 +90,7 @@ private:
ToolBar *mBar;
wxPoint mResizeOffset;
wxSize mOrigSize;
wxWindow *mOrigFocus{};
wxWindowRef mOrigFocus{};
DECLARE_EVENT_TABLE()
};

View File

@ -634,15 +634,9 @@ int ToolManager::FilterEvent(wxEvent &event)
!dynamic_cast<Grabber*>( window ) &&
!dynamic_cast<ToolFrame*>( window ) &&
top == mParent )
// Note this is a dangle-proof wxWindowRef:
mLastFocus = window;
}
else if (event.GetEventType() == wxEVT_DESTROY) {
auto &closeEvent = static_cast<wxWindowDestroyEvent&>(event);
auto window = closeEvent.GetEventObject();
if (window == mLastFocus)
// Avoid a dangling pointer!
mLastFocus = nullptr;
}
return Event_Skip;
}

View File

@ -93,7 +93,7 @@ class ToolManager final : public wxEvtHandler, public wxEventFilter
void Updated();
AudacityProject *mParent;
wxWindow *mLastFocus{};
wxWindowRef mLastFocus{};
ToolFrame *mDragWindow;
ToolDock *mDragDock;

View File

@ -1043,10 +1043,8 @@ ProgressDialog::~ProgressDialog()
// Restore saved focus, but only if the window still exists.
//
// It is possible that it was a deferred deletion and it was deleted since
// we captured the focused window. So, we need to verify that the window
// still exists by searching all of the wxWidgets windows. It's the only
// sure way.
// PRL: I'm conservatively preserving the old existence test, but I think
// it's redundant now that we use wxWindowRef to avoid a dangling pointer
if (mHadFocus && SearchForWindow(wxTopLevelWindows, mHadFocus)) {
mHadFocus->SetFocus();
}

View File

@ -105,7 +105,7 @@ public:
void SetMessage(const wxString & message);
protected:
wxWeakRef<wxWindow> mHadFocus;
wxWindowRef mHadFocus;
wxStaticText *mElapsed;
wxStaticText *mRemaining;