From fa6a837300efb9d295b889a8308b74537c9d8677 Mon Sep 17 00:00:00 2001
From: Robert Roebling <robert@roebling.de>
Date: Sat, 9 Sep 2006 11:32:10 +0000
Subject: [PATCH]   Apply patch [ 1554746 ] wxXmlNode::InsertChild fix

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@41084 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/xmlnode.tex |  5 ++++-
 include/wx/xml/xml.h      |  2 +-
 src/xml/xml.cpp           | 25 ++++++++++++++++++++++---
 3 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/docs/latex/wx/xmlnode.tex b/docs/latex/wx/xmlnode.tex
index 12583cf956..97d60ffa55 100644
--- a/docs/latex/wx/xmlnode.tex
+++ b/docs/latex/wx/xmlnode.tex
@@ -213,9 +213,12 @@ Returns \true if this node has a property named {\it propName}.
 
 \membersection{wxXmlNode::InsertChild}\label{wxxmlnodeinsertchild}
 
-\func{void}{InsertChild}{\param{wxXmlNode* }{child}, \param{wxXmlNode* }{before\_node}}
+\func{bool}{InsertChild}{\param{wxXmlNode* }{child}, \param{wxXmlNode* }{before\_node}}
 
 Inserts the {\it child} node after {\it before\_node} in the children list.
+If {\it before\_node} is \NULL, then {\it child} is prepended to the list of children and
+becomes the first child of this node.
+Returns \true if {\it before\_node} has been found and the {\it child} node has been inserted.
 
 \membersection{wxXmlNode::RemoveChild}\label{wxxmlnoderemovechild}
 
diff --git a/include/wx/xml/xml.h b/include/wx/xml/xml.h
index 83bedabb5f..1d7aa31dcd 100644
--- a/include/wx/xml/xml.h
+++ b/include/wx/xml/xml.h
@@ -115,7 +115,7 @@ public:
     wxXmlNode(wxXmlNodeType type, const wxString& name,
               const wxString& content = wxEmptyString);
     virtual void AddChild(wxXmlNode *child);
-    virtual void InsertChild(wxXmlNode *child, wxXmlNode *before_node);
+    virtual bool InsertChild(wxXmlNode *child, wxXmlNode *before_node);
     virtual bool RemoveChild(wxXmlNode *child);
     virtual void AddProperty(const wxString& name, const wxString& value);
     virtual bool DeleteProperty(const wxString& name);
diff --git a/src/xml/xml.cpp b/src/xml/xml.cpp
index d53297f0ae..067cd02319 100644
--- a/src/xml/xml.cpp
+++ b/src/xml/xml.cpp
@@ -178,21 +178,40 @@ void wxXmlNode::AddChild(wxXmlNode *child)
     child->m_parent = this;
 }
 
-void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
+bool wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
 {
-    wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
+    wxCHECK_MSG(before_node == NULL || before_node->GetParent() == this, false,
+                 wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
+    wxCHECK_MSG(child, false, wxT("Cannot insert a NULL pointer!"));
 
     if (m_children == before_node)
        m_children = child;
+    else if (m_children == NULL)
+    {
+        if (before_node != NULL)
+            return false;       // we have no children so we don't need to search
+        m_children = child;
+    }
+    else if (before_node == NULL)
+    {
+        // prepend child
+        child->m_parent = this;
+        child->m_next = m_children;
+        m_children = child;
+        return true;
+    }
     else
     {
         wxXmlNode *ch = m_children;
-        while (ch->m_next != before_node) ch = ch->m_next;
+        while (ch && ch->m_next != before_node) ch = ch->m_next;
+        if (!ch)
+            return false;       // before_node not found
         ch->m_next = child;
     }
 
     child->m_parent = this;
     child->m_next = before_node;
+    return true;
 }
 
 bool wxXmlNode::RemoveChild(wxXmlNode *child)
-- 
2.50.0

