3 // Copyright (c) 2007 David Ward
5 // This file is part of Dasher.
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
;
31 // Track memory leaks on Windows to the line that new'd the memory
34 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
37 static char THIS_FILE
[] = __FILE__
;
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
) {
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.
57 // std::cout << "done." << std::endl;
62 void CDasherNode::Trace() const {
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);
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);
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
);
85 std::string strContext
= pInterface
->GetContext(iOffset
, iLength
);
86 pAlphabet
->GetSymbols(vContextSymbols
, strContext
);
90 // kill ourselves and all other children except for the specified
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
++) {
99 (*i
)->Delete_children();
104 pChild
->m_pParent
=NULL
;
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
++) {
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;
134 // std::cout << "NM: " << MgrID() << std::endl;
135 SetFlag(NF_ALLCHILDREN
, false);
136 onlyChildRendered
= NULL
;
139 void CDasherNode::SetFlag(int iFlag
, bool bValue
) {
142 m_iFlags
= m_iFlags
| iFlag
;
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);
158 int CDasherNode::MostProbableChild() {
162 for(ChildMap::iterator
it(m_mChildren
.begin()); it
!= m_mChildren
.end(); ++it
) {
163 iCurrent
= (*it
)->Range();
172 bool CDasherNode::GameSearchChildren(symbol sym
) {
173 for (ChildMap::iterator i
= Children().begin(); i
!= Children().end(); i
++) {
174 if ((*i
)->GameSearchNode(sym
)) return true;