-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxCheckSizerFlags.cpp
408 lines (370 loc) · 12 KB
/
wxCheckSizerFlags.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <wx/app.h>
#include <wx/cmdline.h>
#include <wx/xml/xml.h>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <memory>
#include <stdexcept>
#include <sstream>
#include <boost/assign/list_inserter.hpp>
#include <boost/algorithm/string/trim.hpp>
// this becomes available in standard C++20
inline bool ends_with(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
namespace wxFB
{
struct Property
{
Property(const wxXmlNode& node);
std::string name;
std::string value;
};
struct Object;
typedef std::vector<std::shared_ptr<Object> > Objects;
typedef std::vector<std::shared_ptr<Property> > Properties;
struct Object
{
Object(const wxXmlNode& node, const wxXmlNode& nodeRoot, const Object* parent = NULL);
int depth;
int lineNumber;
std::string className;
bool expanded;
Objects children;
Properties properties;
const Object* parent; // non-owning pointer!
std::string getProperty(const std::string& name) const;
int getIntProperty(const std::string& name) const;
void checkSizerFlags();
bool isSizerType() const { return ends_with(className, "Sizer"); }
bool isGridSizerType() const { return ends_with(className, "GridSizer"); }
bool isBoxSizerType() const { return ends_with(className, "BoxSizer"); }
int getFlags() const;
void assertValidSizerFlags() const;
void showInvalidFlags(const std::string& msg) const;
std::string getHierarchy() const;
};
struct Project
{
Objects objects;
Project(const wxXmlNode& nodeRoot);
};
}
class CheckSizerFlagsApp : public wxAppConsole
{
void OnInitCmdLine(wxCmdLineParser& parser) override;
int OnRun() override;
};
wxIMPLEMENT_APP_CONSOLE(CheckSizerFlagsApp);
void CheckSizerFlagsApp::OnInitCmdLine(wxCmdLineParser& parser)
{
wxAppConsole::OnInitCmdLine(parser);
static const wxCmdLineEntryDesc cmdLineDesc[] =
{
{
wxCMD_LINE_PARAM,
"","","a wxFormBuilder XML project file (.fbp)",
wxCMD_LINE_VAL_STRING,
wxCMD_LINE_OPTION_MANDATORY // wxCMD_LINE_PARAM_MULTIPLE
},
wxCMD_LINE_DESC_END
};
parser.SetDesc(cmdLineDesc);
}
wxFB::Object::Object(const wxXmlNode& nodeObject, const wxXmlNode& nodeRoot, const Object* parent_) :
parent(parent_)
{
depth = nodeObject.GetDepth(const_cast<wxXmlNode*>(&nodeRoot));
lineNumber = nodeObject.GetLineNumber();
for (const wxXmlAttribute* attr = nodeObject.GetAttributes();
attr;
attr = attr->GetNext())
{
if ("class" == attr->GetName())
className = attr->GetValue();
else if ("expanded" == attr->GetName())
expanded = "true" == attr->GetValue();
else
std::cout << "\nUnrecognized object node attribute " << attr->GetName() << '\n';
}
for (const wxXmlNode* nodeChild = nodeObject.GetChildren();
nodeChild;
nodeChild = nodeChild->GetNext())
{
if ("object" == nodeChild->GetName())
children.emplace_back(new wxFB::Object(*nodeChild, nodeRoot, this));
else if ("property" == nodeChild->GetName())
properties.emplace_back(new wxFB::Property(*nodeChild));
else if ("event" == nodeChild->GetName())
; // ignored
else
std::cout << "\nUnrecognized node name " << nodeChild->GetName() << '\n';
}
}
wxFB::Property::Property(const wxXmlNode& nodeProperty)
{
//std::cout << 'p'; // progress indicator
for (const wxXmlAttribute* attr = nodeProperty.GetAttributes();
attr;
attr = attr->GetNext())
{
if ("name" == attr->GetName())
name = attr->GetValue();
else
std::cout << "\nUnrecognized property node attribute " << attr->GetName() << '\n';
}
value = nodeProperty.GetNodeContent();
}
wxFB::Project::Project(const wxXmlNode& nodeRoot)
{
for (const wxXmlNode* nodeChild = nodeRoot.GetChildren();
nodeChild;
nodeChild = nodeChild->GetNext())
if ("FileVersion" == nodeChild->GetName())
; // ignore
else if ("object" == nodeChild->GetName())
objects.emplace_back(new wxFB::Object(*nodeChild, nodeRoot));
else
std::cout << "\nUnrecognized node name " << nodeChild->GetName() << '\n';
}
std::ostream& operator <<(std::ostream& os, const wxFB::Object& object)
{
std::string prefix(object.depth, ' ');
os << prefix << object.className << '\n';
prefix += ' ';
for (auto p : object.properties)
os << prefix << p->name << " = " << p->value << '\n';
for (auto p : object.children)
os << prefix << *p;
return os;
}
std::ostream& operator <<(std::ostream& os, const wxFB::Project& project)
{
for (auto p : project.objects)
os << *p;
return os;
}
int CheckSizerFlagsApp::OnRun()
{
const wxString fileName(argv[1]);
wxXmlDocument doc;
doc.Load(fileName);
const wxXmlNode* nodeRoot = doc.GetRoot();
if ("wxFormBuilder_Project" != nodeRoot->GetName())
throw std::runtime_error("argument is not a wxFormBuilder project file");
// build the object tree
wxFB::Project project(*nodeRoot);
for (auto p : project.objects)
p->checkSizerFlags();
return 0;
}
void wxFB::Object::checkSizerFlags()
{
// see the asserts in wxWidgets/src/common/sizer.cpp
if (isSizerType())
assertValidSizerFlags();
// see wxGridSizer::DoInsert
if (isGridSizerType())
{
// check for too many children for fixed column+row count
const int rows = getIntProperty("rows");
const int cols = getIntProperty("cols");
if ((0 != rows) && (0 != cols))
{
const int capacity = rows * cols;
if (children.size() > capacity)
showInvalidFlags("too many children in wxGridSizer");
}
// check for children with EXPAND to be able to expand
for (auto p : children)
{
const int flags = p->getFlags();
if (flags & wxEXPAND)
{
const bool ok = !(flags & (wxALIGN_BOTTOM | wxALIGN_CENTRE_VERTICAL)) ||
!(flags & (wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL));
if (!ok)
p->showInvalidFlags("wxEXPAND flag in child sizer will be overridden by alignment flags, remove "
"either wxEXPAND or the alignment in at least one direction");
}
}
}
if (isBoxSizerType())
{
// see wxBoxSizer::DoInsert
const std::string orient = getProperty("orient");
const bool isVertical = "wxVERTICAL" == orient;
const bool isHorizontal = "wxHORIZONTAL" == orient;
for (auto p : children)
{
const int flags = p->getFlags();
if (isVertical)
{
static const std::string msg("only horizontal alignment flags can be used in child sizers of vertical box sizers");
if (wxALIGN_BOTTOM & flags)
p->showInvalidFlags(msg);
if (!(flags & wxALIGN_CENTRE_HORIZONTAL))
if (flags & wxALIGN_CENTRE_VERTICAL)
p->showInvalidFlags(msg);
}
else if (isHorizontal)
{
static const std::string msg("only vertical alignment flags can be used in child sizers of horizontal box sizers");
if (wxALIGN_RIGHT & flags)
p->showInvalidFlags(msg);
if (!(flags & wxALIGN_CENTRE_VERTICAL))
if (flags & wxALIGN_CENTRE_HORIZONTAL)
p->showInvalidFlags(msg);
}
else
p->showInvalidFlags("missing orient property in wxBoxSizer");
if ( (flags & wxEXPAND) && !(flags & wxSHAPED) )
if (flags & (wxALIGN_RIGHT | wxALIGN_CENTRE_HORIZONTAL |
wxALIGN_BOTTOM | wxALIGN_CENTRE_VERTICAL))
p->showInvalidFlags("wxEXPAND overrides alignment flags in box sizers");
}
}
for (auto p : children)
p->checkSizerFlags();
}
static std::string to_hex_string(int v)
{
std::ostringstream ss;
ss << "0x" << std::hex << v;
return ss.str();
}
void wxFB::Object::assertValidSizerFlags() const
{
static const int SIZER_FLAGS_MASK
= 0
| wxCENTRE
| wxHORIZONTAL
| wxVERTICAL
| wxLEFT
| wxRIGHT
| wxUP
| wxDOWN
| wxTOP
| wxBOTTOM
| wxALIGN_NOT
| wxALIGN_CENTER_HORIZONTAL
| wxALIGN_RIGHT
| wxALIGN_BOTTOM
| wxALIGN_CENTER_VERTICAL
| wxALIGN_LEFT
| wxALIGN_TOP
| wxALIGN_CENTER
| wxALIGN_CENTRE
| wxFIXED_MINSIZE
| wxRESERVE_SPACE_EVEN_IF_HIDDEN
| wxSTRETCH_NOT
| wxSHRINK
| wxGROW
| wxSHAPED;
const int flags = getFlags();
if ((flags & SIZER_FLAGS_MASK) != flags)
showInvalidFlags("invalid flags not within " + to_hex_string(flags));
}
// This returns a vector of strings. With an empty input string, the
// result will include a single empty string.
static auto splitString(const std::string& in, char sep)
{
std::vector<std::string> r;
r.reserve(std::count(in.begin(), in.end(), sep) + 1); // preallocate
for (auto p = in.begin();; ++p) {
auto q = p;
p = std::find(p, in.end(), sep);
r.emplace_back(q, p);
if (p == in.end())
return r;
}
}
static std::map<std::string, int> flagNameMap;
static void initFlagNameMap()
{
if (0 == flagNameMap.size())
boost::assign::insert(flagNameMap)
("wxCENTRE", wxCENTRE)
("wxHORIZONTAL", wxHORIZONTAL)
("wxVERTICAL", wxVERTICAL)
("wxLEFT", wxLEFT)
("wxRIGHT", wxRIGHT)
("wxUP", wxUP)
("wxDOWN", wxDOWN)
("wxTOP", wxTOP)
("wxBOTTOM", wxBOTTOM)
("wxALIGN_NOT", wxALIGN_NOT)
("wxALIGN_CENTER_HORIZONTAL", wxALIGN_CENTER_HORIZONTAL)
("wxALIGN_RIGHT", wxALIGN_RIGHT)
("wxALIGN_BOTTOM", wxALIGN_BOTTOM)
("wxALIGN_CENTER_VERTICAL", wxALIGN_CENTER_VERTICAL)
("wxALIGN_LEFT", wxALIGN_LEFT)
("wxALIGN_TOP", wxALIGN_TOP)
("wxALIGN_CENTER", wxALIGN_CENTER)
("wxALIGN_CENTRE", wxALIGN_CENTRE)
("wxFIXED_MINSIZE", wxFIXED_MINSIZE)
("wxRESERVE_SPACE_EVEN_IF_HIDDEN", wxRESERVE_SPACE_EVEN_IF_HIDDEN)
("wxSTRETCH_NOT", wxSTRETCH_NOT)
("wxSHRINK", wxSHRINK)
("wxGROW", wxGROW)
("wxSHAPED", wxSHAPED)
("wxEXPAND", wxEXPAND)
("wxALL", wxALL)
;
}
int wxFB::Object::getFlags() const
{
initFlagNameMap();
// can't use a temporary because splitString will return pointers
// into this
const std::string flagsProperty = getProperty("flag");
auto flagNames = splitString(flagsProperty, '|');
int flags = 0;
for (auto flagName : flagNames)
{
boost::trim(flagName); // trim whitespace
if ("" == flagName)
continue;
auto p = flagNameMap.find(flagName);
if (flagNameMap.end() == p)
showInvalidFlags("unknown flag " + flagName);
else
flags |= p->second;
}
return flags;
}
std::string wxFB::Object::getHierarchy() const
{
// display hierarchy
std::string ancestors;
if (parent)
ancestors = parent->getHierarchy();
ancestors += '/' + className;
const std::string myName = getProperty("name");
if ("" != myName)
ancestors += '(' + myName + ')';
return ancestors;
}
void wxFB::Object::showInvalidFlags(const std::string& msg) const
{
if (("sizeritem" == className) && (1 == children.size()))
children[0]->showInvalidFlags(msg);
else
std::cout << "Object " << getHierarchy() << " at line " << lineNumber << ": " << msg << '\n';
}
std::string wxFB::Object::getProperty(const std::string& name) const
{
for (auto p : properties)
if (name == p->name)
return p->value;
return "";
}
int wxFB::Object::getIntProperty(const std::string& name) const
{
const std::string value = getProperty(name);
return std::stoi(value);
}