From: Markus R. <rol...@us...> - 2006-01-16 11:36:21
|
Update of /cvsroot/simspark/simspark/contrib/rsgedit In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2030 Added Files: sparktree.cpp sparktree.h Log Message: - added sparktree. A class that manages a tree control to display the zeitgeist object hierarchy --- NEW FILE: sparktree.h --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2003 Koblenz University $Id: sparktree.h,v 1.1 2006/01/16 11:35:55 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef SPARKTREE_H__ #define SPARKTREE_H__ #include <zeitgeist/core.h> #include <wx/treectrl.h> class SparkTree { public: struct ItemData : public wxTreeItemData { public: boost::weak_ptr<zeitgeist::Leaf> leaf; public: ItemData(boost::weak_ptr<zeitgeist::Leaf> l) : wxTreeItemData(), leaf(l) {} }; public: SparkTree(); virtual ~SparkTree(); void Init(wxTreeCtrl* tree); bool CreateChildren(const wxTreeItemId id); bool GetLocation(const wxTreeItemId id, wxString& location); protected: void SetItemData(wxTreeItemId id, boost::weak_ptr<zeitgeist::Leaf> leaf); ItemData* GetItemData(wxTreeItemId id); protected: wxTreeCtrl* mTree; boost::shared_ptr<zeitgeist::Core> mCore; wxTreeItemId mRootId; }; #endif // SPARKTREE_H__ --- NEW FILE: sparktree.cpp --- /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- this file is part of rcssserver3D Fri May 9 2003 Copyright (C) 2003 Koblenz University $Id: sparktree.cpp,v 1.1 2006/01/16 11:35:55 rollmark Exp $ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "sparktree.h" #include "main.h" #include "simspark.h" //! wxWidgets and zeitgeist both use a 'DECLARE_CLASS' macro #undef DECLARE_CLASS #include <zeitgeist/leaf.h> #include <zeitgeist/class.h> using namespace boost; using namespace zeitgeist; SparkTree::SparkTree() : mTree(0) { } SparkTree::~SparkTree() { // remove but don't delete us from the event handler stack of the // tree mTree->PopEventHandler(false); } void SparkTree::SetItemData(wxTreeItemId id, weak_ptr<Leaf> leaf) { ItemData* data = new ItemData(leaf); mTree->SetItemData(id, data); } SparkTree::ItemData* SparkTree::GetItemData(wxTreeItemId id) { ItemData* data = static_cast<ItemData*>(mTree->GetItemData(id)); if ( (data == 0) || (data->leaf.expired()) ) { return 0; } return data; } void SparkTree::Init(wxTreeCtrl* ctrl) { mTree = ctrl; mTree->DeleteAllItems(); shared_ptr<SimSpark> spark = wxGetApp().GetSpark(); if (spark.get() == 0) { return; } mCore = spark->GetCore(); mRootId = mTree->AddRoot("/"); shared_ptr<Leaf> root = mCore->GetRoot(); SetItemData(mRootId, root); if (! root->IsLeaf()) { mTree->SetItemHasChildren(mRootId,true); mTree->Expand(mRootId); } } bool SparkTree::CreateChildren(const wxTreeItemId id) { if (mTree->GetChildrenCount(id) > 0) { // already expanded return true; } ItemData* data = GetItemData(id); if (data == 0) { return false; } const Leaf& leaf = (*data->leaf.lock()); if (leaf.IsLeaf()) { return false; } for ( Leaf::TLeafList::const_iterator iter = leaf.begin(); iter != leaf.end(); ++iter ) { shared_ptr<Leaf> child = (*iter); wxString label = child->GetName(); shared_ptr<Class> childClass = child->GetClass(); if (childClass.get() != 0) { label += " (" + childClass->GetName() + ")"; } wxTreeItemId childId = mTree->AppendItem(id, label); SetItemData(childId, child); if ( (! child->IsLeaf()) && (child->begin() != child->end()) ) { // allow expansion but don't add children yet mTree->SetItemHasChildren(childId,true); } } } bool SparkTree::GetLocation(const wxTreeItemId id, wxString& location) { ItemData* data = GetItemData(id); if (data == 0) { return false; } location = data->leaf.lock()->GetFullPath(); return true; } |