Merge branch 'aui-delete-tool'

Provide work around for surprising behaviour of
wxAuiToolBar::DeleteTool().

Closes https://github.com/wxWidgets/wxWidgets/pull/1758
This commit is contained in:
Vadim Zeitlin
2020-03-15 17:10:41 +01:00
3 changed files with 111 additions and 50 deletions

View File

@@ -525,6 +525,12 @@ public:
void ClearTools() { Clear() ; } void ClearTools() { Clear() ; }
void 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 DeleteTool(int toolId);
bool DeleteByIndex(int toolId); bool DeleteByIndex(int toolId);

View File

@@ -681,13 +681,13 @@ public:
bool SetFont(const wxFont& font); bool SetFont(const wxFont& font);
wxAuiToolBarItem* AddTool(int tool_id, wxAuiToolBarItem* AddTool(int toolId,
const wxString& label, const wxString& label,
const wxBitmap& bitmap, const wxBitmap& bitmap,
const wxString& short_help_string = wxEmptyString, const wxString& short_help_string = wxEmptyString,
wxItemKind kind = wxITEM_NORMAL); wxItemKind kind = wxITEM_NORMAL);
wxAuiToolBarItem* AddTool(int tool_id, wxAuiToolBarItem* AddTool(int toolId,
const wxString& label, const wxString& label,
const wxBitmap& bitmap, const wxBitmap& bitmap,
const wxBitmap& disabled_bitmap, const wxBitmap& disabled_bitmap,
@@ -696,7 +696,7 @@ public:
const wxString& long_help_string, const wxString& long_help_string,
wxObject* client_data); wxObject* client_data);
wxAuiToolBarItem* AddTool(int tool_id, wxAuiToolBarItem* AddTool(int toolId,
const wxBitmap& bitmap, const wxBitmap& bitmap,
const wxBitmap& disabled_bitmap, const wxBitmap& disabled_bitmap,
bool toggle = false, bool toggle = false,
@@ -704,7 +704,7 @@ public:
const wxString& short_help_string = wxEmptyString, const wxString& short_help_string = wxEmptyString,
const wxString& long_help_string = wxEmptyString); const wxString& long_help_string = wxEmptyString);
wxAuiToolBarItem* AddLabel(int tool_id, wxAuiToolBarItem* AddLabel(int toolId,
const wxString& label = wxEmptyString, const wxString& label = wxEmptyString,
const int width = -1); const int width = -1);
wxAuiToolBarItem* AddControl(wxControl* control, wxAuiToolBarItem* AddControl(wxControl* control,
@@ -718,19 +718,67 @@ public:
wxControl* FindControl(int window_id); wxControl* FindControl(int window_id);
wxAuiToolBarItem* FindToolByPosition(wxCoord x, wxCoord y) const; wxAuiToolBarItem* FindToolByPosition(wxCoord x, wxCoord y) const;
wxAuiToolBarItem* FindToolByIndex(int idx) const; wxAuiToolBarItem* FindToolByIndex(int idx) const;
wxAuiToolBarItem* FindTool(int tool_id) const; wxAuiToolBarItem* FindTool(int toolId) const;
void ClearTools(); void ClearTools();
void Clear(); void Clear();
bool DeleteTool(int tool_id);
bool DeleteByIndex(int tool_id); /**
Destroys the tool with the given ID and its associated window, if any.
@param toolId 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 toolId);
/**
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 toolId ID of a previously added tool.
@return @true if the tool was removed or @false otherwise, e.g. if the
tool with the given ID was not found.
*/
bool DeleteTool(int toolId);
/**
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
provided index is out of range.
*/
bool DeleteByIndex(int idx);
size_t GetToolCount() const; size_t GetToolCount() const;
int GetToolPos(int tool_id) const; int GetToolPos(int toolId) const;
int GetToolIndex(int tool_id) const; int GetToolIndex(int toolId) const;
bool GetToolFits(int tool_id) const; bool GetToolFits(int toolId) const;
wxRect GetToolRect(int tool_id) const; wxRect GetToolRect(int toolId) const;
bool GetToolFitsByIndex(int tool_id) const; bool GetToolFitsByIndex(int toolId) const;
bool GetToolBarFits() const; bool GetToolBarFits() const;
void SetMargins(const wxSize& size); void SetMargins(const wxSize& size);
@@ -746,11 +794,11 @@ public:
bool GetGripperVisible() const; bool GetGripperVisible() const;
void SetGripperVisible(bool visible); void SetGripperVisible(bool visible);
void ToggleTool(int tool_id, bool state); void ToggleTool(int toolId, bool state);
bool GetToolToggled(int tool_id) const; bool GetToolToggled(int toolId) const;
void EnableTool(int tool_id, bool state); void EnableTool(int toolId, bool state);
bool GetToolEnabled(int tool_id) const; bool GetToolEnabled(int toolId) const;
/** /**
Set whether the specified toolbar item has a drop down button. Set whether the specified toolbar item has a drop down button.
@@ -759,7 +807,7 @@ public:
@see wxAuiToolBarItem::SetHasDropDown() @see wxAuiToolBarItem::SetHasDropDown()
*/ */
void SetToolDropDown(int tool_id, bool dropdown); void SetToolDropDown(int toolId, bool dropdown);
/** /**
Returns whether the specified toolbar item has an associated drop down Returns whether the specified toolbar item has an associated drop down
@@ -767,7 +815,7 @@ public:
@see wxAuiToolBarItem::HasDropDown() @see wxAuiToolBarItem::HasDropDown()
*/ */
bool GetToolDropDown(int tool_id) const; bool GetToolDropDown(int toolId) const;
void SetToolBorderPadding(int padding); void SetToolBorderPadding(int padding);
int GetToolBorderPadding() const; int GetToolBorderPadding() const;
@@ -778,26 +826,26 @@ public:
void SetToolPacking(int packing); void SetToolPacking(int packing);
int GetToolPacking() const; int GetToolPacking() const;
void SetToolProportion(int tool_id, int proportion); void SetToolProportion(int toolId, int proportion);
int GetToolProportion(int tool_id) const; int GetToolProportion(int toolId) const;
void SetToolSeparation(int separation); void SetToolSeparation(int separation);
int GetToolSeparation() const; int GetToolSeparation() const;
void SetToolSticky(int tool_id, bool sticky); void SetToolSticky(int toolId, bool sticky);
bool GetToolSticky(int tool_id) const; bool GetToolSticky(int toolId) const;
wxString GetToolLabel(int tool_id) const; wxString GetToolLabel(int toolId) const;
void SetToolLabel(int tool_id, const wxString& label); void SetToolLabel(int toolId, const wxString& label);
wxBitmap GetToolBitmap(int tool_id) const; wxBitmap GetToolBitmap(int toolId) const;
void SetToolBitmap(int tool_id, const wxBitmap& bitmap); void SetToolBitmap(int toolId, const wxBitmap& bitmap);
wxString GetToolShortHelp(int tool_id) const; wxString GetToolShortHelp(int toolId) const;
void SetToolShortHelp(int tool_id, const wxString& help_string); void SetToolShortHelp(int toolId, const wxString& help_string);
wxString GetToolLongHelp(int tool_id) const; wxString GetToolLongHelp(int toolId) const;
void SetToolLongHelp(int tool_id, const wxString& help_string); void SetToolLongHelp(int toolId, const wxString& help_string);
void SetCustomOverflowItems(const wxAuiToolBarItemArray& prepend, void SetCustomOverflowItems(const wxAuiToolBarItemArray& prepend,
const wxAuiToolBarItemArray& append); const wxAuiToolBarItemArray& append);

View File

@@ -1165,15 +1165,7 @@ void wxAuiToolBar::Clear()
bool wxAuiToolBar::DeleteTool(int tool_id) bool wxAuiToolBar::DeleteTool(int tool_id)
{ {
int idx = GetToolIndex(tool_id); return DeleteByIndex(GetToolIndex(tool_id));
if (idx >= 0 && idx < (int)m_items.GetCount())
{
m_items.RemoveAt(idx);
Realize();
return true;
}
return false;
} }
bool wxAuiToolBar::DeleteByIndex(int idx) bool wxAuiToolBar::DeleteByIndex(int idx)
@@ -1188,6 +1180,22 @@ bool wxAuiToolBar::DeleteByIndex(int idx)
return false; return false;
} }
bool wxAuiToolBar::DestroyTool(int tool_id)
{
return DestroyToolByIndex(GetToolIndex(tool_id));
}
bool wxAuiToolBar::DestroyToolByIndex(int idx)
{
if ( idx < 0 || static_cast<unsigned>(idx) >= m_items.GetCount() )
return false;
if ( wxWindow* window = m_items[idx].GetWindow() )
window->Destroy();
return DeleteByIndex(idx);
}
wxControl* wxAuiToolBar::FindControl(int id) wxControl* wxAuiToolBar::FindControl(int id)
{ {
@@ -1946,14 +1954,14 @@ bool wxAuiToolBar::RealizeHelper(wxClientDC& dc, bool horizontal)
for (i = 0, count = m_items.GetCount(); i < count; ++i) for (i = 0, count = m_items.GetCount(); i < count; ++i)
{ {
wxAuiToolBarItem& item = m_items.Item(i); wxAuiToolBarItem& item = m_items.Item(i);
wxSizerItem* m_sizerItem = NULL; wxSizerItem* sizerItem = NULL;
switch (item.m_kind) switch (item.m_kind)
{ {
case wxITEM_LABEL: case wxITEM_LABEL:
{ {
wxSize size = m_art->GetLabelSize(dc, this, item); wxSize size = m_art->GetLabelSize(dc, this, item);
m_sizerItem = sizer->Add(size.x + (m_toolBorderPadding*2), sizerItem = sizer->Add(size.x + (m_toolBorderPadding*2),
size.y + (m_toolBorderPadding*2), size.y + (m_toolBorderPadding*2),
item.m_proportion, item.m_proportion,
item.m_alignment); item.m_alignment);
@@ -1970,7 +1978,7 @@ bool wxAuiToolBar::RealizeHelper(wxClientDC& dc, bool horizontal)
case wxITEM_RADIO: case wxITEM_RADIO:
{ {
wxSize size = m_art->GetToolSize(dc, this, item); wxSize size = m_art->GetToolSize(dc, this, item);
m_sizerItem = sizer->Add(size.x + (m_toolBorderPadding*2), sizerItem = sizer->Add(size.x + (m_toolBorderPadding*2),
size.y + (m_toolBorderPadding*2), size.y + (m_toolBorderPadding*2),
0, 0,
item.m_alignment); item.m_alignment);
@@ -1986,9 +1994,9 @@ bool wxAuiToolBar::RealizeHelper(wxClientDC& dc, bool horizontal)
case wxITEM_SEPARATOR: case wxITEM_SEPARATOR:
{ {
if (horizontal) if (horizontal)
m_sizerItem = sizer->Add(separatorSize, 1, 0, wxEXPAND); sizerItem = sizer->Add(separatorSize, 1, 0, wxEXPAND);
else else
m_sizerItem = sizer->Add(1, separatorSize, 0, wxEXPAND); sizerItem = sizer->Add(1, separatorSize, 0, wxEXPAND);
// add tool packing // add tool packing
if (i+1 < count) if (i+1 < count)
@@ -2001,14 +2009,13 @@ bool wxAuiToolBar::RealizeHelper(wxClientDC& dc, bool horizontal)
case wxITEM_SPACER: case wxITEM_SPACER:
if (item.m_proportion > 0) if (item.m_proportion > 0)
m_sizerItem = sizer->AddStretchSpacer(item.m_proportion); sizerItem = sizer->AddStretchSpacer(item.m_proportion);
else else
m_sizerItem = sizer->Add(item.m_spacerPixels, 1); sizerItem = sizer->Add(item.m_spacerPixels, 1);
break; break;
case wxITEM_CONTROL: case wxITEM_CONTROL:
{ {
//m_sizerItem = sizer->Add(item.m_window, item.m_proportion, wxEXPAND);
wxSizerItem* ctrl_m_sizerItem; wxSizerItem* ctrl_m_sizerItem;
wxBoxSizer* vert_sizer = new wxBoxSizer(wxVERTICAL); wxBoxSizer* vert_sizer = new wxBoxSizer(wxVERTICAL);
@@ -2024,7 +2031,7 @@ bool wxAuiToolBar::RealizeHelper(wxClientDC& dc, bool horizontal)
} }
m_sizerItem = sizer->Add(vert_sizer, item.m_proportion, wxEXPAND); sizerItem = sizer->Add(vert_sizer, item.m_proportion, wxEXPAND);
wxSize min_size = item.m_minSize; wxSize min_size = item.m_minSize;
@@ -2038,7 +2045,7 @@ bool wxAuiToolBar::RealizeHelper(wxClientDC& dc, bool horizontal)
if (min_size.IsFullySpecified()) if (min_size.IsFullySpecified())
{ {
m_sizerItem->SetMinSize(min_size); sizerItem->SetMinSize(min_size);
ctrl_m_sizerItem->SetMinSize(min_size); ctrl_m_sizerItem->SetMinSize(min_size);
} }
@@ -2050,7 +2057,7 @@ bool wxAuiToolBar::RealizeHelper(wxClientDC& dc, bool horizontal)
} }
} }
item.m_sizerItem = m_sizerItem; item.m_sizerItem = sizerItem;
} }
// add "right" padding // add "right" padding