From 700eaff131179e27cc2e70fbd4b57c4f1d429756 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 10 Mar 2020 23:37:42 +0100 Subject: [PATCH] Add wxAuiToolBar::DestroyTool() and DestroyToolByIndex() These new functions destroy the associated window too, unlike the existing DeleteTool() and DeleteByIndex(). Closes #16552. --- include/wx/aui/auibar.h | 6 ++++++ interface/wx/aui/auibar.h | 28 ++++++++++++++++++++++++++-- src/aui/auibar.cpp | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/include/wx/aui/auibar.h b/include/wx/aui/auibar.h index 05b841983a..6b612b545a 100644 --- a/include/wx/aui/auibar.h +++ b/include/wx/aui/auibar.h @@ -525,6 +525,12 @@ public: void ClearTools() { Clear() ; } void Clear(); + + bool DestroyTool(int toolId); + bool DestroyToolByIndex(int idx); + + // Note that these methods do _not_ delete the associated control, if any. + // Use DestroyTool() or DestroyToolByIndex() if this is wanted. bool DeleteTool(int toolId); bool DeleteByIndex(int toolId); diff --git a/interface/wx/aui/auibar.h b/interface/wx/aui/auibar.h index a3e23752d7..04e261b7a3 100644 --- a/interface/wx/aui/auibar.h +++ b/interface/wx/aui/auibar.h @@ -724,11 +724,33 @@ public: void Clear(); /** - Removes the tool with the given ID from the tool bar. + Destroys the tool with the given ID and its associated window, if any. + + @param tool_id ID of a previously added tool. + @return @true if the tool was destroyed or @false otherwise, e.g. if + the tool with the given ID was not found. + + @since 3.1.4 + */ + bool DestroyTool(int tool_id); + + /** + Destroys the tool at the given position and its associated window, if + any. + + @param idx The index, or position, of a previously added tool. + @return @true if the tool was destroyed or @false otherwise, e.g. if + the provided index is out of range. + */ + bool DestroyToolByIndex(int idx); + + /** + Removes the tool with the given ID from the toolbar. Note that if this tool was added by AddControl(), the associated control is @e not deleted and must either be reused (e.g. by reparenting it under a different window) or destroyed by caller. + If this behaviour is unwanted, prefer using DestroyTool() instead. @param tool_id ID of a previously added tool. @return @true if the tool was removed or @false otherwise, e.g. if the @@ -737,11 +759,13 @@ public: bool DeleteTool(int tool_id); /** - Removes the tool at the given position from the tool bar. + Removes the tool at the given position from the toolbar. Note that if this tool was added by AddControl(), the associated control is @e not deleted and must either be reused (e.g. by reparenting it under a different window) or destroyed by caller. + If this behaviour is unwanted, prefer using DestroyToolByIndex() + instead. @param idx The index, or position, of a previously added tool. @return @true if the tool was removed or @false otherwise, e.g. if the diff --git a/src/aui/auibar.cpp b/src/aui/auibar.cpp index 2d2fd80bb3..22c1b45d3f 100644 --- a/src/aui/auibar.cpp +++ b/src/aui/auibar.cpp @@ -1180,6 +1180,22 @@ bool wxAuiToolBar::DeleteByIndex(int idx) return false; } +bool wxAuiToolBar::DestroyTool(int tool_id) +{ + return DestroyToolByIndex(GetToolIndex(tool_id)); +} + +bool wxAuiToolBar::DestroyToolByIndex(int idx) +{ + if ( idx < 0 || static_cast(idx) >= m_items.GetCount() ) + return false; + + if ( wxWindow* window = m_items[idx].GetWindow() ) + window->Destroy(); + + return DeleteByIndex(idx); +} + wxControl* wxAuiToolBar::FindControl(int id) {