-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlTreeView.cpp
299 lines (230 loc) · 7.29 KB
/
XmlTreeView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
////////////////////////////////////////////////////////////////////////////////
//! \file XmlTreeView.cpp
//! \brief The XmlTreeView class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "XmlTreeView.hpp"
#include "TheView.hpp"
#include <XML/Document.hpp>
#include <XML/TextNode.hpp>
#include <XML/ElementNode.hpp>
#include <XML/CommentNode.hpp>
#include <XML/ProcessingNode.hpp>
#include "TheApp.hpp"
#include "TheDoc.hpp"
////////////////////////////////////////////////////////////////////////////////
//! Default constructor.
XmlTreeView::XmlTreeView(TheView& oView)
: m_oView(oView)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
XmlTreeView::~XmlTreeView()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Get the current selected node.
XML::NodePtr XmlTreeView::Selection() const
{
HTREEITEM hSelItem = TreeView::Selection();
XML::NodePtr pNode;
if (hSelItem != NULL)
pNode = GetItemNode(hSelItem);
return pNode;
}
////////////////////////////////////////////////////////////////////////////////
//! Set the selected node.
void XmlTreeView::SetSelection(const XML::NodePtr& pNode)
{
HTREEITEM hItem = GetNodeItem(pNode);
Select(hItem);
}
////////////////////////////////////////////////////////////////////////////////
//! Refresh the entire document.
void XmlTreeView::Refresh()
{
// Clear the old DOM.
m_mapItemNode.clear();
m_mapNodeItem.clear();
Clear();
XML::DocumentPtr pDOM = m_oView.Document().DOM();
tstring strItem = TXT("DOM");
ASSERT(pDOM.get() != nullptr);
HTREEITEM hRoot = InsertRootItem(strItem, pDOM->hasChildren(), 0);
AddItemNodeMapping(hRoot, pDOM);
AddNodeTree(hRoot, *pDOM);
}
////////////////////////////////////////////////////////////////////////////////
//! Create a mapping between the item and node.
void XmlTreeView::AddItemNodeMapping(HTREEITEM hItem, const XML::NodePtr& pNode)
{
ASSERT(m_mapItemNode.find(hItem) == m_mapItemNode.end());
ASSERT(m_mapNodeItem.find(pNode.get()) == m_mapNodeItem.end());
m_mapItemNode.insert(std::make_pair(hItem, pNode.get()));
m_mapNodeItem.insert(std::make_pair(pNode.get(), hItem));
}
////////////////////////////////////////////////////////////////////////////////
//! Get the XML node for the tree item.
XML::NodePtr XmlTreeView::GetItemNode(HTREEITEM hItem) const
{
ItemNodeMap::const_iterator it = m_mapItemNode.find(hItem);
ASSERT(it != m_mapItemNode.end());
return XML::NodePtr(it->second, true);
}
////////////////////////////////////////////////////////////////////////////////
//! Get the tree item for the XML node.
HTREEITEM XmlTreeView::GetNodeItem(const XML::NodePtr& pNode) const
{
NodeItemMap::const_iterator it = m_mapNodeItem.find(pNode.get());
ASSERT(it != m_mapNodeItem.end());
return it->second;
}
////////////////////////////////////////////////////////////////////////////////
//! Handle child message reflected back from the parent.
void XmlTreeView::OnReflectedCtrlMsg(NMHDR& oMsgHdr)
{
ASSERT(oMsgHdr.hwndFrom == m_hWnd);
// Reflect back to the underlying view window.
if (oMsgHdr.code == TVN_SELCHANGED)
m_oView.OnNodeSelected(reinterpret_cast<NMTREEVIEW&>(oMsgHdr));
}
////////////////////////////////////////////////////////////////////////////////
//! Add the node container to the tree.
void XmlTreeView::AddNodeTree(HTREEITEM hParent, const XML::NodeContainer& oContainer)
{
typedef XML::NodeContainer::const_iterator CIter;
// Add all children to the parent node.
for (CIter it = oContainer.beginChild(); it != oContainer.endChild(); ++it)
{
const XML::NodePtr& pNode = *it;
HTREEITEM hItem = AddNode(hParent, pNode);
// If a container node, recursively add it's sub-tree.
if (pNode->type() == XML::DOCUMENT_NODE)
AddNodeTree(hItem, *Core::static_ptr_cast<XML::Document>(pNode));
else if (pNode->type() == XML::ELEMENT_NODE)
AddNodeTree(hItem, *Core::static_ptr_cast<XML::ElementNode>(pNode));
}
}
////////////////////////////////////////////////////////////////////////////////
//! Add a node to the tree.
HTREEITEM XmlTreeView::AddNode(HTREEITEM hParent, const XML::NodePtr& pNode)
{
// Add it to the tree view.
HTREEITEM hItem = InsertItem(hParent, TVI_LAST, TXT(""));
AddItemNodeMapping(hItem, pNode);
UpdateNode(hItem, pNode);
return hItem;
}
////////////////////////////////////////////////////////////////////////////////
//! Update a node in the tree.
void XmlTreeView::UpdateNode(HTREEITEM hItem, const XML::NodePtr& pNode)
{
XML::NodeType eType = pNode->type();
tstring strItem = pNode->typeStr();
bool bHasChildren = false;
int nImage = -1;
// Create a summary for the tree item.
if (eType == XML::DOCUMENT_NODE)
{
XML::DocumentPtr pDoc = Core::static_ptr_cast<XML::Document>(pNode);
bHasChildren = pDoc->hasChildren();
nImage = 0;
}
else if (eType == XML::ELEMENT_NODE)
{
XML::ElementNodePtr pElement = Core::static_ptr_cast<XML::ElementNode>(pNode);
strItem = pElement->name();
strItem += TXT(' ');
strItem += MakeAttribSummary(pElement->getAttributes());
bHasChildren = pElement->hasChildren();
nImage = 1;
PostProcessSummary(strItem);
}
else if (eType == XML::TEXT_NODE)
{
XML::TextNodePtr pText = Core::static_ptr_cast<XML::TextNode>(pNode);
strItem = pText->text();
nImage = 6;
PostProcessSummary(strItem);
}
else if (eType == XML::COMMENT_NODE)
{
nImage = 5;
}
else if (eType == XML::PROCESSING_NODE)
{
XML::ProcessingNodePtr pProcInst = Core::static_ptr_cast<XML::ProcessingNode>(pNode);
strItem = pProcInst->target();
strItem += TXT(' ');
strItem += MakeAttribSummary(pProcInst->getAttributes());
nImage = 4;
PostProcessSummary(strItem);
}
else if (eType == XML::DOCTYPE_NODE)
{
strItem = TXT("DOCTYPE");
nImage = 7;
}
else if (eType == XML::CDATA_NODE)
{
strItem = TXT("CDATA");
nImage = 8;
}
else
{
ASSERT_FALSE();
}
// Add it to the tree view.
UpdateItem(hItem, strItem, bHasChildren, nImage);
}
////////////////////////////////////////////////////////////////////////////////
//! Generate a summary of the attributes.
tstring XmlTreeView::MakeAttribSummary(XML::Attributes& vAttribs)
{
// Type aliases.
typedef XML::Attributes::const_iterator ConstIter;
tstring str;
// For all attributes...
for (ConstIter it = vAttribs.begin(); it != vAttribs.end(); ++it)
{
const XML::AttributePtr& pAttrib = *it;
if (!str.empty())
str += TXT(' ');
str += pAttrib->name();
str += TXT("=\"");
str += pAttrib->value();
str += TXT("\"");
}
return str;
}
////////////////////////////////////////////////////////////////////////////////
//! Post-process the summary.
void XmlTreeView::PostProcessSummary(tstring& str)
{
// Replace empty strings.
if (str.empty())
{
str = TXT("(empty)");
return;
}
bool bWhitespaceOnly = true;
// Find if only whitespace characters.
for (tstring::const_iterator it = str.begin(); ((it != str.end()) && bWhitespaceOnly); ++it)
{
if (!tisspace(static_cast<utchar>(*it)))
bWhitespaceOnly = false;
}
// Replace "invisible" strings.
if (bWhitespaceOnly)
{
str = TXT("(whitespace)");
return;
}
// Trim string.
if (str.length() > App.m_nDefMaxItemLen)
{
str.erase(App.m_nDefMaxItemLen, str.length()-App.m_nDefMaxItemLen);
str += TXT("...");
}
}