NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
treesearchctrl.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2019 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
19#include "treesearchctrl.hpp"
20#include "treedata.hpp"
21
22
32bool TreeSearchCtrl::selectItem(const wxString& value)
33{
34 // Ensure that a tree as associated
36 {
37 // Get the root node and the first child
38 wxTreeItemIdValue cookie;
39 wxTreeItemId root = m_associatedCtrl->GetRootItem();
40 wxTreeItemId child = m_associatedCtrl->GetFirstChild(root, cookie);
41
42 // If the child exists, try to find the passed string
43 // in the labels of all childs
44 if (child.IsOk())
45 {
46 wxTreeItemId match = findItem(value[0] == ' ' ? value.substr(1).Lower() : value.Lower(), child);
47
48 // If a label was found, ensure it is visible
49 // and select it
50 if (match.IsOk())
51 {
52 m_associatedCtrl->EnsureVisible(match);
53 m_associatedCtrl->SelectItem(match);
54
55 return true;
56 }
57 }
58 }
59
60 return false;
61}
62
63
74wxTreeItemId TreeSearchCtrl::findItem(const wxString& value, wxTreeItemId node)
75{
76 // Go through all siblings
77 do
78 {
79 // Return the current node, if it
80 // corresponds to the passed string
81 if (m_associatedCtrl->GetItemText(node).Lower() == value)
82 return node;
83
84 // Search the first child
85 wxTreeItemIdValue cookie;
86 wxTreeItemId child = m_associatedCtrl->GetFirstChild(node, cookie);
87
88 // If the child exists, try to find the
89 // passed string in its or its siblings
90 // labels
91 if (child.IsOk())
92 {
93 wxTreeItemId match = findItem(value, child);
94
95 // Return the id, if it exists
96 if (match.IsOk())
97 return match;
98 }
99 }
100 while ((node = m_associatedCtrl->GetNextSibling(node)).IsOk());
101
102 // Return an invalid tree item id, if
103 // nothing had been found
104 return wxTreeItemId();
105}
106
107
117wxArrayString TreeSearchCtrl::getCandidates(const wxString& enteredText)
118{
119 // Ensure that a tree control was associated
121 {
122 // Find root node and its child
123 wxTreeItemIdValue cookie;
124 wxTreeItemId root = m_associatedCtrl->GetRootItem();
125 wxTreeItemId child = m_associatedCtrl->GetFirstChild(root, cookie);
126
127 // If the child exists, get all labels of its
128 // siblings and their childs, which are candidates
129 // for the passed string
130 if (child.IsOk())
131 return getChildCandidates(enteredText.Lower(), child);
132 }
133
134 // Return an empty string otherwise
135 return wxArrayString();
136}
137
138
151wxArrayString TreeSearchCtrl::getChildCandidates(const wxString& enteredText, wxTreeItemId node)
152{
153 wxArrayString stringArray;
154
155 // Go through all siblings
156 do
157 {
158 // Append the current label, if it contains the
159 // searched string
160 if (m_searchToolTip)
161 {
162 ToolTipTreeData* data = static_cast<ToolTipTreeData*>(m_associatedCtrl->GetItemData(node));
163
164 if ((data && data->tooltip.Lower().find(enteredText) != std::string::npos)
165 || m_associatedCtrl->GetItemText(node).Lower().find(enteredText) != std::string::npos)
166 stringArray.Add(m_associatedCtrl->GetItemText(node));
167 }
168 else if (m_associatedCtrl->GetItemText(node).Lower().find(enteredText) != std::string::npos)
169 stringArray.Add(m_associatedCtrl->GetItemText(node));
170
171 // Find the first child of the current node
172 wxTreeItemIdValue cookie;
173 wxTreeItemId child = m_associatedCtrl->GetFirstChild(node, cookie);
174
175 // If the child exists, find the candidates in
176 // its siblings and childs
177 if (child.IsOk())
178 {
179 wxArrayString childArray = getChildCandidates(enteredText, child);
180
181 // Insert the candidates into the current
182 // string array
183 for (size_t i = 0; i < childArray.size(); i++)
184 stringArray.Add(childArray[i]);
185 }
186 }
187 while ((node = m_associatedCtrl->GetNextSibling(node)).IsOk());
188
189 return stringArray;
190}
191
192
193
194
This class provides the basic functionality to provide a tooltip for a tree item.
Definition: treedata.hpp:28
wxString tooltip
Definition: treedata.hpp:30
virtual bool selectItem(const wxString &value) override
This method searches and selects the item with the passed label in the associated tree.
virtual wxArrayString getCandidates(const wxString &enteredText) override
This method returns an array of strings containing possible candidates for the passed search string.
wxTreeCtrl * m_associatedCtrl
wxTreeItemId findItem(const wxString &value, wxTreeItemId node)
This method searches for the tree item, whose label corresponds to the passed string.
wxArrayString getChildCandidates(const wxString &enteredText, wxTreeItemId node)
This method returns an array of strings containing possible candiates for the passed search string,...