Updated German translation
[dasher.git] / Src / DasherCore / DasherNode.cpp
blob899641f3c4c633460f3435c5a76e53f4b36e1828
1 // DasherNode.cpp
2 //
3 // Copyright (c) 2007 David Ward
4 //
5 // This file is part of Dasher.
6 //
7 // Dasher is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // Dasher is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with Dasher; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "../Common/Common.h"
23 // #include "AlphabetManager.h" - doesnt seem to be required - pconlon
25 #include "DasherInterfaceBase.h"
27 using namespace Dasher;
28 using namespace Opts;
29 using namespace std;
31 // Track memory leaks on Windows to the line that new'd the memory
32 #ifdef _WIN32
33 #ifdef _DEBUG
34 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
35 #define new DEBUG_NEW
36 #undef THIS_FILE
37 static char THIS_FILE[] = __FILE__;
38 #endif
39 #endif
40 static int iNumNodes = 0;
42 int Dasher::currentNumNodeObjects() {return iNumNodes;}
44 //TODO this used to be inline - should we make it so again?
45 CDasherNode::CDasherNode(int iOffset, int iColour, CDasherScreen::Label *pLabel)
46 : onlyChildRendered(NULL), m_iLbnd(0), m_iHbnd(CDasherModel::NORMALIZATION), m_pParent(NULL), m_iFlags(DEFAULT_FLAGS), m_iOffset(iOffset), m_iColour(iColour), m_pLabel(pLabel) {
47 iNumNodes++;
50 // TODO: put this back to being inlined
51 CDasherNode::~CDasherNode() {
52 // std::cout << "Deleting node: " << this << std::endl;
53 // Release any storage that the node manager has allocated,
54 // unreference ref counted stuff etc.
55 Delete_children();
57 // std::cout << "done." << std::endl;
59 iNumNodes--;
62 void CDasherNode::Trace() const {
63 /* TODO sort out
64 dchar out[256];
65 if (m_Symbol)
66 wsprintf(out,TEXT("%7x %3c %7x %5d %7x %5d %8x %8x \n"),this,m_Symbol,m_iGroup,m_context,m_Children,m_Cscheme,m_iLbnd,m_iHbnd);
67 else
68 wsprintf(out,TEXT("%7x %7x %5d %7x %5d %8x %8x \n"),this,m_iGroup,m_context,m_Children,m_Cscheme,m_iLbnd,m_iHbnd);
70 OutputDebugString(out);
72 if (m_Children) {
73 unsigned int i;
74 for (i=1;i<m_iChars;i++)
75 m_Children[i]->Dump_node();
80 void CDasherNode::GetContext(CDasherInterfaceBase *pInterface, const CAlphabetMap *pAlphabet, vector<symbol> &vContextSymbols, int iOffset, int iLength) {
81 if (!GetFlag(NF_SEEN)) {
82 DASHER_ASSERT(m_pParent);
83 if (m_pParent) m_pParent->GetContext(pInterface, pAlphabet, vContextSymbols, iOffset,iLength);
84 } else {
85 std::string strContext = pInterface->GetContext(iOffset, iLength);
86 pAlphabet->GetSymbols(vContextSymbols, strContext);
90 // kill ourselves and all other children except for the specified
91 // child
92 // FIXME this probably shouldn't be called after history stuff is working
93 void CDasherNode::OrphanChild(CDasherNode *pChild) {
94 DASHER_ASSERT(ChildCount() > 0);
96 ChildMap::const_iterator i;
97 for(i = GetChildren().begin(); i != GetChildren().end(); i++) {
98 if((*i) != pChild) {
99 (*i)->Delete_children();
100 delete (*i);
104 pChild->m_pParent=NULL;
106 Children().clear();
107 SetFlag(NF_ALLCHILDREN, false);
110 // Delete nephews of the child which has the specified symbol
111 // TODO: Need to allow for subnode
112 void CDasherNode::DeleteNephews(CDasherNode *pChild) {
113 DASHER_ASSERT(Children().size() > 0);
115 ChildMap::iterator i;
116 for(i = Children().begin(); i != Children().end(); i++) {
117 if(*i != pChild) {
118 (*i)->Delete_children();
123 // TODO: Need to allow for subnodes
124 // TODO: Incorporate into above routine
125 void CDasherNode::Delete_children() {
126 // std::cout << "Start: " << this << std::endl;
128 ChildMap::iterator i;
129 for(i = Children().begin(); i != Children().end(); i++) {
130 // std::cout << "CNM: " << (*i)->MgrID() << (*i) << " " << (*i)->Parent() << std::endl;
131 delete (*i);
133 Children().clear();
134 // std::cout << "NM: " << MgrID() << std::endl;
135 SetFlag(NF_ALLCHILDREN, false);
136 onlyChildRendered = NULL;
139 void CDasherNode::SetFlag(int iFlag, bool bValue) {
141 if(bValue)
142 m_iFlags = m_iFlags | iFlag;
143 else
144 m_iFlags = m_iFlags & (~iFlag);
147 void CDasherNode::Reparent(CDasherNode *pNewParent, unsigned int iLbnd, unsigned int iHbnd) {
148 DASHER_ASSERT(!m_pParent);
149 DASHER_ASSERT(pNewParent);
150 DASHER_ASSERT(!pNewParent->GetFlag(NF_ALLCHILDREN));
151 DASHER_ASSERT(iLbnd == (pNewParent->GetChildren().empty() ? 0 : pNewParent->GetChildren().back()->m_iHbnd));
152 m_pParent = pNewParent;
153 pNewParent->Children().push_back(this);
154 m_iLbnd = iLbnd;
155 m_iHbnd = iHbnd;
158 int CDasherNode::MostProbableChild() {
159 int iMax(0);
160 int iCurrent;
162 for(ChildMap::iterator it(m_mChildren.begin()); it != m_mChildren.end(); ++it) {
163 iCurrent = (*it)->Range();
165 if(iCurrent > iMax)
166 iMax = iCurrent;
169 return iMax;
172 bool CDasherNode::GameSearchChildren(symbol sym) {
173 for (ChildMap::iterator i = Children().begin(); i != Children().end(); i++) {
174 if ((*i)->GameSearchNode(sym)) return true;
176 return false;