NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
procedureviewer.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 "procedureviewer.hpp"
20#include "../../kernel/core/ui/language.hpp"
21#include "../../kernel/core/utils/stringtools.hpp"
22#include "../editor/editor.h"
23
24BEGIN_EVENT_TABLE(ProcedureViewer, wxListView)
25 EVT_LIST_COL_CLICK(-1, ProcedureViewer::OnColumnClick)
26 EVT_LIST_ITEM_ACTIVATED(-1, ProcedureViewer::OnItemClick)
28
29// Data structure for the procedure viewer to enable
30// effective sorting
32{
33 int ID;
37
38 ProcedureViewerData(int nID, const wxString& procDef, const wxString& flags, const wxString& retVal = "") : ID(nID), procedureDefinition(procDef), procedureFlags(flags), procedureReturnVal(retVal) {}
39};
40
41// Sorting callback function. Will use the selected column and the data
42// stored in the items to return the order of two elements by returning
43// numbers.
44// wxCALLBACK aliases some weird calling convention on WIN32.
45int wxCALLBACK ProcedureViewerCompare(wxIntPtr item1, wxIntPtr item2, wxIntPtr parent)
46{
47 ProcedureViewer* viewer = reinterpret_cast<ProcedureViewer*>(parent);
48
49 // Switch to determine the correct data field
50 // of the internal data structure
51 switch (viewer->nSortColumn)
52 {
53 case 0:
54 if (viewer->vData[item1].ID < viewer->vData[item2].ID)
55 return -1;
56 else if (viewer->vData[item1].ID == viewer->vData[item2].ID)
57 return 0;
58 return 1;
59 case 1:
60 if (viewer->vData[item1].procedureFlags < viewer->vData[item2].procedureFlags)
61 return -1;
62 else if (viewer->vData[item1].procedureFlags == viewer->vData[item2].procedureFlags)
63 return 0;
64 return 1;
65 case 2:
66 if (viewer->vData[item1].procedureReturnVal < viewer->vData[item2].procedureReturnVal)
67 return -1;
68 else if (viewer->vData[item1].procedureReturnVal == viewer->vData[item2].procedureReturnVal)
69 return 0;
70 return 1;
71 case 3:
72 if (toLowerCase(viewer->vData[item1].procedureDefinition.ToStdString()) < toLowerCase(viewer->vData[item2].procedureDefinition.ToStdString()))
73 return -1;
74 else if (toLowerCase(viewer->vData[item1].procedureDefinition.ToStdString()) == toLowerCase(viewer->vData[item2].procedureDefinition.ToStdString()))
75 return 0;
76 return 1;
77 }
78
79 return 0;
80}
81
82// This private member function redirects the control
83// to the editor to obtain the procedure list of the
84// currently watched file
86{
87 if (m_currentEd)
88 {
89 // Get the list
91 }
92 else
93 {
94 // Create an empty control
96 }
97}
98
99// This private member function creates an empty control,
100// which will display "no procedures in this file" message
102{
103 if (GetItemCount())
104 {
105 DeleteAllItems();
106 vData.clear();
107 }
108
109 InsertItem(0, "--");
110 SetItem(0, 3, _guilang.get("GUI_PROCEDUREVIEWER_EMPTY"));
111 SetItemData(0, 0);
112
113 // Make the message grey and printed in italics
114 SetItemFont(0, GetFont().MakeItalic());
115 SetItemTextColour(0, wxColour(128, 128, 128));
116
117 // Add a fallback data object
118 vData.push_back(ProcedureViewerData(0, "", ""));
119}
120
121// Constructor
122ProcedureViewer::ProcedureViewer(wxWindow* parent) : wxListView(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_VRULES)
123{
124 m_currentEd = nullptr;
125 nSortColumn = 0;
126 vData = std::vector<ProcedureViewerData>();
127 AppendColumn("ID");
128 AppendColumn("Flags");
129 AppendColumn("Returns");
130 AppendColumn(_guilang.get("GUI_PROCEDUREVIEWER_SIGNATURE"));
131
132 // Create an empty control
133 emptyControl();
134}
135
136// This member function removes obsolete whitespaces from
137// the passed string
138void ProcedureViewer::stripSpaces(wxString& sString)
139{
140 if (sString.find_first_not_of(' ') != std::string::npos)
141 {
142 sString.erase(0, sString.find_first_not_of(' '));
143
144 if (sString.find_last_not_of(' ') != std::string::npos)
145 sString.erase(sString.find_last_not_of(' ')+1);
146 }
147 else
148 sString.clear();
149}
150
151// This member function is used to create and manage
152// the Editor <-> ProcedureViewer binding, so that the
153// editor may update the procedure list after saving
155{
156 // If the procedure viewer was already registered
157 // in an editor, unregister it
158 if (m_currentEd)
160
161 m_currentEd = editor;
162
163 // If the new editor is not a null pointer,
164 // register the procedure viewer
165 if (m_currentEd)
167
168 // Get the current procedure list
170}
171
172// This member function decodes the obtained procedure
173// list and updates the contents of this widget
174void ProcedureViewer::updateProcedureList(const std::vector<wxString>& vProcedures)
175{
176 // Clear the contents of the list
177 if (GetItemCount())
178 {
179 DeleteAllItems();
180 vData.clear();
181 }
182
183 wxString procdef;
184 wxString flags;
185 wxString returns;
186
187 // Go through the list
188 for (size_t i = 0; i < vProcedures.size(); i++)
189 {
190 // Get the current definition
191 procdef = vProcedures[i];
192 returns.clear();
193
194 // Split the definition at the flags (if available)
195 // and at the definition string (if available)
196 if (procdef.find("::") != std::string::npos)
197 {
198 flags = procdef.substr(procdef.find("::")+2);
199 procdef.erase(procdef.find("::"));
200
201 if (flags.find("\n") != std::string::npos)
202 flags.erase(flags.find("\n"));
203
204 if (flags.find("->") != std::string::npos)
205 {
206 returns = flags.substr(flags.find("->")+2);
207 flags.erase(flags.find("->"));
208 }
209 }
210 else
211 {
212 flags.clear();
213
214 if (procdef.find("\n") != std::string::npos)
215 procdef.erase(procdef.find("\n"));
216
217 if (procdef.find("->") != std::string::npos)
218 {
219 returns = procdef.substr(procdef.find("->")+2);
220 procdef.erase(procdef.find("->"));
221 }
222 }
223
224 // Remove obsolete whitespaces
225 this->stripSpaces(flags);
226 this->stripSpaces(procdef);
227 this->stripSpaces(returns);
228
229 // Create a new data object in the buffer
230 vData.push_back(ProcedureViewerData(i, procdef, flags, returns));
231
232 // Create the new list item and store
233 // a pointer to the current data object
234 InsertItem(i, toString(i+1));
235 SetItem(i, 1, flags);
236 SetItem(i, 2, returns);
237 SetItem(i, 3, procdef);
238 //SetItemPtrData(i, (wxUIntPtr)&vData[vData.size()-1]);
239 SetItemData(i, i);
240
241 // If the current procedure is not flagged as
242 // "local" (which is done by the editor automatically)
243 // then make its text bold
244 if (flags.find("local") == std::string::npos)
245 SetItemFont(i, GetFont().MakeBold());
246 }
247
248 // Sort the items according the last selected
249 // sorting column or create an empty control,
250 // if no data is available in the control
251 if (GetItemCount())
252 SortItems(ProcedureViewerCompare, (wxIntPtr)this);
253 else
254 emptyControl();
255
256 // Resize the columns
257 if (vProcedures.size())
258 {
259 SetColumnWidth(0, wxLIST_AUTOSIZE);
260 SetColumnWidth(1, wxLIST_AUTOSIZE);
261
262 if (GetColumnWidth(1) < 50)
263 SetColumnWidth(1, 50);
264
265 SetColumnWidth(2, wxLIST_AUTOSIZE);
266
267 if (GetColumnWidth(2) < 55)
268 SetColumnWidth(2, 55);
269
270 SetColumnWidth(3, wxLIST_AUTOSIZE);
271
272 if (GetColumnWidth(3) < 250)
273 SetColumnWidth(3, 250);
274 }
275 else
276 {
277 SetColumnWidth(0, wxLIST_AUTOSIZE_USEHEADER);
278 SetColumnWidth(1, wxLIST_AUTOSIZE_USEHEADER);
279 SetColumnWidth(2, wxLIST_AUTOSIZE_USEHEADER);
280 SetColumnWidth(3, wxLIST_AUTOSIZE);
281 }
282}
283
284// This member function is the event handler for
285// clicking on the columns header to trigger
286// the sorting process
287void ProcedureViewer::OnColumnClick(wxListEvent& event)
288{
289 nSortColumn = event.GetColumn();
290
291 SortItems(ProcedureViewerCompare, (wxIntPtr)this);
292}
293
294// This member function is the event handler called
295// after the user activated an item. It will focus the
296// selected procedure in the editor
297void ProcedureViewer::OnItemClick(wxListEvent& event)
298{
299 if (!vData[event.GetData()].procedureDefinition.length())
300 return;
301
302 wxString sProcDef = vData[event.GetData()].procedureDefinition.substr(0, vData[event.GetData()].procedureDefinition.find('('));
303
304 // We can use the "thisfile" namespace always. The
305 // editor will find the correct procedure although
306 // it might be the naming procedure of the current
307 // file
308 m_currentEd->FindAndOpenProcedure("$thisfile~" + sProcDef.substr(1));
309}
310
std::string toLowerCase(const std::string &)
Converts uppercase to lowercase letters.
std::string get(const std::string &sMessage, const std::vector< std::string > &vTokens) const
This member function returns the language string for the passed language identifier and replaces all ...
Definition: language.cpp:292
The class of the editor window.
Definition: editor.h:53
void registerProcedureViewer(ProcedureViewer *viewer)
Registers the passed procedure viewer.
Definition: editor.cpp:5326
void FindAndOpenProcedure(const wxString &procedurename)
Finds the procedure definition and displays it in the editor.
Definition: editor.cpp:5461
std::vector< wxString > getProceduresInFile()
Wrapper for the search controller.
Definition: editor.cpp:5631
void updateProcedureList(const std::vector< wxString > &vProcedures)
void stripSpaces(wxString &sString)
void OnItemClick(wxListEvent &event)
void OnColumnClick(wxListEvent &event)
void setCurrentEditor(NumeReEditor *editor)
std::vector< ProcedureViewerData > vData
ProcedureViewer(wxWindow *parent)
NumeReEditor * m_currentEd
void getProcedureListFromEditor()
Language _guilang
int wxCALLBACK ProcedureViewerCompare(wxIntPtr item1, wxIntPtr item2, wxIntPtr parent)
ProcedureViewerData(int nID, const wxString &procDef, const wxString &flags, const wxString &retVal="")
std::string toString(int)
Converts an integer to a string without the Settings bloat.
END_EVENT_TABLE()