diff --git a/include/wx/tbarbase.h b/include/wx/tbarbase.h index 14d03e032b..ba6a73e489 100644 --- a/include/wx/tbarbase.h +++ b/include/wx/tbarbase.h @@ -608,6 +608,17 @@ protected: // make the size of the buttons big enough to fit the largest bitmap size void AdjustToolBitmapSize(); + // calls InsertTool() and deletes the tool if inserting it failed + wxToolBarToolBase *DoInsertNewTool(size_t pos, wxToolBarToolBase *tool) + { + if ( !InsertTool(pos, tool) ) + { + delete tool; + return NULL; + } + + return tool; + } // the list of all our tools wxToolBarToolsList m_tools; diff --git a/interface/wx/toolbar.h b/interface/wx/toolbar.h index 93b844b26f..36cbd2eb60 100644 --- a/interface/wx/toolbar.h +++ b/interface/wx/toolbar.h @@ -533,6 +533,10 @@ public: You must call Realize() for the change to take place. @see AddTool(), InsertControl(), InsertSeparator() + + @return The newly inserted tool or @NULL on failure. Notice that with + the overload taking @a tool parameter the caller is responsible for + deleting the tool in the latter case. */ wxToolBarToolBase* InsertTool(size_t pos, int toolId, const wxBitmap& bitmap1, diff --git a/src/common/tbarbase.cpp b/src/common/tbarbase.cpp index abc7670961..37f38fa169 100644 --- a/src/common/tbarbase.cpp +++ b/src/common/tbarbase.cpp @@ -180,17 +180,8 @@ wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos, wxCHECK_MSG( pos <= GetToolsCount(), NULL, wxT("invalid position in wxToolBar::InsertTool()") ); - wxToolBarToolBase *tool = CreateTool(id, label, bitmap, bmpDisabled, kind, - clientData, shortHelp, longHelp); - - if ( !InsertTool(pos, tool) ) - { - delete tool; - - return NULL; - } - - return tool; + return DoInsertNewTool(pos, CreateTool(id, label, bitmap, bmpDisabled, kind, + clientData, shortHelp, longHelp)); } wxToolBarToolBase *wxToolBarBase::AddTool(wxToolBarToolBase *tool) @@ -232,19 +223,7 @@ wxToolBarBase::InsertControl(size_t pos, wxCHECK_MSG( control->GetParent() == this, NULL, wxT("control must have toolbar as parent") ); - wxCHECK_MSG( pos <= GetToolsCount(), NULL, - wxT("invalid position in wxToolBar::InsertControl()") ); - - wxToolBarToolBase *tool = CreateTool(control, label); - - if ( !InsertTool(pos, tool) ) - { - delete tool; - - return NULL; - } - - return tool; + return DoInsertNewTool(pos, CreateTool(control, label)); } wxControl *wxToolBarBase::FindControl( int id ) @@ -280,25 +259,11 @@ wxToolBarToolBase *wxToolBarBase::AddSeparator() wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos) { - wxCHECK_MSG( pos <= GetToolsCount(), NULL, - wxT("invalid position in wxToolBar::InsertSeparator()") ); - - wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR, - wxEmptyString, - wxNullBitmap, wxNullBitmap, - wxITEM_SEPARATOR, NULL, - wxEmptyString, wxEmptyString); - - if ( !tool || !DoInsertTool(pos, tool) ) - { - delete tool; - - return NULL; - } - - m_tools.Insert(pos, tool); - - return tool; + return DoInsertNewTool(pos, CreateTool(wxID_SEPARATOR, + wxEmptyString, + wxNullBitmap, wxNullBitmap, + wxITEM_SEPARATOR, NULL, + wxEmptyString, wxEmptyString)); } wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)