]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/treelay.cpp
don't crash trying to dereference NULL m_selection (happens when showing a grid witho...
[wxWidgets.git] / src / generic / treelay.cpp
index 35496a9d7dd2bd48582591f9a61282d9613ff58d..701a0f14b7c977ee153f3bd5be407d4ef80a582c 100644 (file)
@@ -2,7 +2,7 @@
 // Name:        treelay.h
 // Purpose:     wxTreeLayout class
 // Author:      Julian Smart
-// Modified by: 
+// Modified by:
 // Created:     7/4/98
 // RCS-ID:      $Id$
 // Copyright:   (c) 1998 Julian Smart
 #endif
 
 // For compilers that support precompilation, includes "wx.h".
-#include <wx/wxprec.h>
+#include "wx/wxprec.h"
 
 #ifdef __BORLANDC__
 #pragma hdrstop
 #endif
 
 #ifndef WX_PRECOMP
-#include <wx/wx.h>
+#include "wx/dc.h"
+#include "wx/event.h"
 #endif
 
 #if wxUSE_TREELAYOUT
 
-#include <wx/treelay.h>
+#include "wx/treelay.h"
 
 /*
  * Abstract tree
@@ -49,7 +50,7 @@ void wxTreeLayout::DoLayout(wxDC& dc, long topId)
 {
     if (topId != -1)
         SetTopNode(topId);
-    
+
     long actualTopId = GetTopNode();
     long id = actualTopId;
     while (id != -1)
@@ -72,7 +73,7 @@ void wxTreeLayout::Draw(wxDC& dc)
 }
 
 void wxTreeLayout::DrawNodes(wxDC& dc)
-{  
+{
     long id = GetTopNode();
     while (id != -1)
     {
@@ -83,7 +84,7 @@ void wxTreeLayout::DrawNodes(wxDC& dc)
 }
 
 void wxTreeLayout::DrawBranches(wxDC& dc)
-{  
+{
     long id = GetTopNode();
     while (id != -1)
     {
@@ -99,13 +100,13 @@ void wxTreeLayout::DrawBranches(wxDC& dc)
 
 void wxTreeLayout::DrawNode(long id, wxDC& dc)
 {
-    char buf[80];
+    wxChar buf[80];
     wxString name(GetNodeName(id));
-    if (name != "")
-        sprintf(buf, "%s", (const char*) name);
+    if (name != wxT(""))
+        wxSprintf(buf, wxT("%s"), (const wxChar*) name);
     else
-        sprintf(buf, "<unnamed>");
-    
+        wxSprintf(buf, wxT("<unnamed>"));
+
     long x = 80;
     long y = 20;
     dc.GetTextExtent(buf, &x, &y);
@@ -127,7 +128,7 @@ void wxTreeLayout::Initialize(void)
 void wxTreeLayout::GetNodeSize(long id, long *x, long *y, wxDC& dc)
 {
     wxString name(GetNodeName(id));
-    if (name != "")
+    if (name != wxT(""))
         dc.GetTextExtent(name, x, y);
     else
     {
@@ -140,7 +141,7 @@ void wxTreeLayout::CalcLayout(long nodeId, int level, wxDC& dc)
     wxList children;
     GetChildren(nodeId, children);
     int n = children.Number();
-    
+
     if (m_orientation == FALSE)
     {
         // Left to right
@@ -156,18 +157,18 @@ void wxTreeLayout::CalcLayout(long nodeId, int level, wxDC& dc)
                 GetNodeSize(parentId, &x, &y, dc);
             SetNodeX(nodeId, (long)(GetNodeX(parentId) + m_xSpacing + x));
         }
-        
+
         wxNode *node = children.First();
         while (node)
         {
             CalcLayout((long)node->Data(), level+1, dc);
             node = node->Next();
         }
-        
+
         // Y Calculations
         long averageY;
         ActivateNode(nodeId, TRUE);
-        
+
         if (n > 0)
         {
             averageY = 0;
@@ -185,14 +186,14 @@ void wxTreeLayout::CalcLayout(long nodeId, int level, wxDC& dc)
             SetNodeY(nodeId, m_lastY);
             long x, y;
             GetNodeSize(nodeId, &x, &y, dc);
-            
+
             m_lastY = m_lastY + y + m_ySpacing;
         }
     }
     else
     {
         // Top to bottom
-        
+
         // Y Calculations
         if (level == 0)
             SetNodeY(nodeId, m_topMargin);
@@ -205,18 +206,18 @@ void wxTreeLayout::CalcLayout(long nodeId, int level, wxDC& dc)
                 GetNodeSize(parentId, &x, &y, dc);
             SetNodeY(nodeId, (long)(GetNodeY(parentId) + m_ySpacing + y));
         }
-        
+
         wxNode *node = children.First();
         while (node)
         {
             CalcLayout((long)node->Data(), level+1, dc);
             node = node->Next();
         }
-        
+
         // X Calculations
         long averageX;
         ActivateNode(nodeId, TRUE);
-        
+
         if (n > 0)
         {
             averageX = 0;
@@ -234,7 +235,7 @@ void wxTreeLayout::CalcLayout(long nodeId, int level, wxDC& dc)
             SetNodeX(nodeId, m_lastX);
             long x, y;
             GetNodeSize(nodeId, &x, &y, dc);
-            
+
             m_lastX = m_lastX + x + m_xSpacing;
         }
     }
@@ -245,22 +246,22 @@ void wxTreeLayout::CalcLayout(long nodeId, int level, wxDC& dc)
  *
  */
 
-IMPLEMENT_DYNAMIC_CLASS(wxLayoutTreeStored, wxTreeLayout)
+IMPLEMENT_DYNAMIC_CLASS(wxTreeLayoutStored, wxTreeLayout)
 
-wxLayoutTreeStored::wxLayoutTreeStored(int n):wxTreeLayout()
+wxTreeLayoutStored::wxTreeLayoutStored(int n):wxTreeLayout()
 {
     m_nodes = NULL;
     m_maxNodes = 0;
     Initialize(n);
 }
 
-wxLayoutTreeStored::~wxLayoutTreeStored(void)
+wxTreeLayoutStored::~wxTreeLayoutStored(void)
 {
     if (m_nodes)
         delete[] m_nodes;
 }
 
-void wxLayoutTreeStored::Initialize(int n)
+void wxTreeLayoutStored::Initialize(int n)
 {
     m_maxNodes = n;
     wxTreeLayout::Initialize();
@@ -278,14 +279,40 @@ void wxLayoutTreeStored::Initialize(int n)
     m_num = 0;
 }
 
-long wxLayoutTreeStored::AddChild(const wxString& name, const wxString& parent)
+long wxTreeLayoutStored::AddChild(const wxString& name, const wxString& parent)
 {
     if (m_num < (m_maxNodes -1 ))
     {
         long i = -1;
-        if (parent != "")
+        if (parent != wxT(""))
             i = NameToId(parent);
         else m_parentNode = m_num;
+
+        m_nodes[m_num].m_parentId = i;
+        m_nodes[m_num].m_name = name;
+        m_nodes[m_num].m_x = m_nodes[m_num].m_y = 0;
+        m_nodes[m_num].m_clientData = 0;
+        m_num ++;
+
+        return (m_num - 1);
+    }
+    else
+        return -1;
+}
+
+long wxTreeLayoutStored::AddChild(const wxString& name, long parent)
+{
+    if (m_num < (m_maxNodes -1 ) && parent < m_num)
+    {
+        long i = -1;
+        if (parent != -1)
+        {
+          i = parent;
+        }
+        else 
+        {
+          m_parentNode = m_num;
+        }
         
         m_nodes[m_num].m_parentId = i;
         m_nodes[m_num].m_name = name;
@@ -299,7 +326,7 @@ long wxLayoutTreeStored::AddChild(const wxString& name, const wxString& parent)
         return -1;
 }
 
-long wxLayoutTreeStored::NameToId(const wxString& name)
+long wxTreeLayoutStored::NameToId(const wxString& name)
 {
     long i;
     for (i = 0; i < m_num; i++)
@@ -308,7 +335,7 @@ long wxLayoutTreeStored::NameToId(const wxString& name)
         return -1;
 }
 
-void wxLayoutTreeStored::GetChildren(long id, wxList& list)
+void wxTreeLayoutStored::GetChildren(long id, wxList& list)
 {
     long currentId = GetTopNode();
     while (currentId != -1)
@@ -319,126 +346,124 @@ void wxLayoutTreeStored::GetChildren(long id, wxList& list)
     }
 }
 
-wxStoredNode* wxLayoutTreeStored::GetNode(long idx) const
+wxStoredNode* wxTreeLayoutStored::GetNode(long idx) const
 {
     wxASSERT(idx < m_num);
-    
+
     return &m_nodes[idx];
 };
 
-long wxLayoutTreeStored::GetNodeX(long id)
+long wxTreeLayoutStored::GetNodeX(long id)
 {
     wxASSERT(id < m_num);
-    
+
     return (long)m_nodes[id].m_x;
 }
 
-long wxLayoutTreeStored::GetNodeY(long id)
+long wxTreeLayoutStored::GetNodeY(long id)
 {
     wxASSERT(id < m_num);
-    
+
     return (long)m_nodes[id].m_y;
 }
 
-void wxLayoutTreeStored::SetNodeX(long id, long x)
+void wxTreeLayoutStored::SetNodeX(long id, long x)
 {
     wxASSERT(id < m_num);
-    
+
     m_nodes[id].m_x = (int)x;
 }
 
-void wxLayoutTreeStored::SetNodeY(long id, long y)
+void wxTreeLayoutStored::SetNodeY(long id, long y)
 {
     wxASSERT(id < m_num);
-    
+
     m_nodes[id].m_y = (int)y;
 }
 
-void wxLayoutTreeStored::SetNodeName(long id, const wxString& name)
+void wxTreeLayoutStored::SetNodeName(long id, const wxString& name)
 {
     wxASSERT(id < m_num);
-    
+
     m_nodes[id].m_name = name;
 }
 
-wxString wxLayoutTreeStored::GetNodeName(long id)
+wxString wxTreeLayoutStored::GetNodeName(long id)
 {
     wxASSERT(id < m_num);
-    
+
     return m_nodes[id].m_name;
 }
 
-long wxLayoutTreeStored::GetNodeParent(long id)
+long wxTreeLayoutStored::GetNodeParent(long id)
 {
     if (id != -1)
     {
         wxASSERT(id < m_num);
-        
+
         return m_nodes[id].m_parentId;
     }
     else
         return -1;
 }
 
-long wxLayoutTreeStored::GetNextNode(long id)
+long wxTreeLayoutStored::GetNextNode(long id)
 {
     wxASSERT(id < m_num);
-    
+
     if ((id != -1) && (id < (m_num - 1)))
         return id + 1;
     else
         return -1;
 }
 
-void wxLayoutTreeStored::SetClientData(long id, long clientData)
+void wxTreeLayoutStored::SetClientData(long id, long clientData)
 {
     wxASSERT(id < m_num);
-    
+
     m_nodes[id].m_clientData = clientData;
 }
 
-long wxLayoutTreeStored::GetClientData(long id) const
+long wxTreeLayoutStored::GetClientData(long id) const
 {
     wxASSERT(id < m_num);
-    
+
     return m_nodes[id].m_clientData;
 }
 
-void wxLayoutTreeStored::ActivateNode(long id, bool active)
+void wxTreeLayoutStored::ActivateNode(long id, bool active)
 {
     wxASSERT(id < m_num);
-    
+
     m_nodes[id].m_active = active;
 }
 
-bool wxLayoutTreeStored::NodeActive(long id)
+bool wxTreeLayoutStored::NodeActive(long id)
 {
     wxASSERT(id < m_num);
-    
+
     return m_nodes[id].m_active;
 }
 
-wxString wxLayoutTreeStored::HitTest(wxMouseEvent& event, wxDC& dc)
+wxString wxTreeLayoutStored::HitTest(wxMouseEvent& event, wxDC& dc)
 {
     wxPoint pt = event.GetPosition();
     wxCoord x = pt.x;
     wxCoord y = pt.y;
-    
+
     int i;
     for (i = 0; i < m_maxNodes; i++)
     {
-        wxStoredNode* item = &m_nodes[i];
-        
         long width, height;
         dc.GetTextExtent(m_nodes[i].m_name, &width, &height);
-        
+
         if ( (x >= (m_nodes[i].m_x-10)) && (x < (m_nodes[i].m_x + width+10)) &&
             (y >= m_nodes[i].m_y-10) && (y < (m_nodes[i].m_y + height+10)) )
         {
             return m_nodes[i].m_name;
         }
     }
-    
+
     return wxString("");
 }