Browse code

Various changes:

* Use enum class instead of enum (except for the enums for the resource IDs, not really necessary there).
* For NCSF specifically, included a function to convert an enum class to its underlying integral type (as this is needed for use with the std::bitset class).
* Cleanup headers so all the ones needed in a file are explicitly included even if they may possibly be included in another header.
* Used forward declarations in a few spots.
* Explicitly namespaced all (u)int*_t uses (this might seem like overkill, but it helps me see when the standard types are being used with a simple search for std::).
* Made sure it all builds with MinGW-w64 as well (both gcc and clang).
* Removed some std::move from DialogBuilder.cpp based on clang's warnings for that.
* Replaced use of std::copy_n on strings in DialogBuilder.cpp with my CopyToString functions that use wcscpy.
* Replaced CHAR_MIN/CHAR_MAX in eqstr.h and ltstr.h with std::numeric_limits<char>::min/max().

Naram Qashat authored on 2021/03/21 03:16:45
Showing 1 changed files
... ...
@@ -3,13 +3,20 @@
3 3
  * By Naram Qashat (CyberBotX) [cyberbotx@cyberbotx.com]
4 4
  */
5 5
 
6
+#include <algorithm>
7
+#include <memory>
8
+#include <utility>
9
+#include <vector>
10
+#include <cstdint>
11
+#include "windowsh_wrapper.h"
6 12
 #include "DialogBuilder.h"
13
+#include "XSFCommon.h"
7 14
 
8 15
 // Code modified from the following answer on Stack Overflow:
9 16
 // http://stackoverflow.com/a/3407254
10
-static inline uint32_t getNextMultipleOf4(uint32_t origNum)
17
+static inline std::uint32_t getNextMultipleOf4(std::uint32_t origNum)
11 18
 {
12
-	uint32_t remainder = origNum % 4;
19
+	std::uint32_t remainder = origNum % 4;
13 20
 	if (!remainder)
14 21
 		return origNum;
15 22
 	return origNum + 4 - remainder;
... ...
@@ -20,16 +27,16 @@ Point<short> RelativePosition::CalculatePosition(const Rect<short> &child, const
20 27
 	Point<short> newPosition = child.position;
21 28
 	if (this->relativePosition.y != -1)
22 29
 	{
23
-		if (this->positionType == FROM_TOP || this->positionType == FROM_TOPLEFT || this->positionType == FROM_TOPRIGHT)
30
+		if (this->positionType == PositionType::FromTop || this->positionType == PositionType::FromTopLeft || this->positionType == PositionType::FromTopRight)
24 31
 			newPosition.y = other.position.y + this->relativePosition.y;
25
-		if (this->positionType == FROM_BOTTOM || this->positionType == FROM_BOTTOMLEFT || this->positionType == FROM_BOTTOMRIGHT)
32
+		if (this->positionType == PositionType::FromBottom || this->positionType == PositionType::FromBottomLeft || this->positionType == PositionType::FromBottomRight)
26 33
 			newPosition.y = other.position.y + other.size.height + this->relativePosition.y;
27 34
 	}
28 35
 	if (this->relativePosition.x != -1)
29 36
 	{
30
-		if (this->positionType == FROM_LEFT || this->positionType == FROM_TOPLEFT || this->positionType == FROM_BOTTOMLEFT)
37
+		if (this->positionType == PositionType::FromLeft || this->positionType == PositionType::FromTopLeft || this->positionType == PositionType::FromBottomLeft)
31 38
 			newPosition.x = other.position.x + this->relativePosition.x;
32
-		if (this->positionType == FROM_RIGHT || this->positionType == FROM_TOPRIGHT || this->positionType == FROM_BOTTOMRIGHT)
39
+		if (this->positionType == PositionType::FromRight || this->positionType == PositionType::FromTopRight || this->positionType == PositionType::FromBottomRight)
33 40
 			newPosition.x = other.position.x + other.size.width + this->relativePosition.x;
34 41
 	}
35 42
 	return newPosition;
... ...
@@ -44,15 +51,15 @@ void DialogTemplate::DialogGroup::CalculatePositions(bool doRightAndBottom)
44 51
 		if (control->relativePosition)
45 52
 		{
46 53
 			bool valid = true;
47
-			if (!doRightAndBottom && control->relativePosition->type == RelativePosition::TO_PARENT)
54
+			if (!doRightAndBottom && control->relativePosition->type == RelativePosition::BaseType::ToParent)
48 55
 			{
49 56
 				switch (control->relativePosition->positionType)
50 57
 				{
51
-					case RelativePosition::FROM_BOTTOM:
52
-					case RelativePosition::FROM_BOTTOMLEFT:
53
-					case RelativePosition::FROM_BOTTOMRIGHT:
54
-					case RelativePosition::FROM_RIGHT:
55
-					case RelativePosition::FROM_TOPRIGHT:
58
+					case RelativePosition::PositionType::FromBottom:
59
+					case RelativePosition::PositionType::FromBottomLeft:
60
+					case RelativePosition::PositionType::FromBottomRight:
61
+					case RelativePosition::PositionType::FromRight:
62
+					case RelativePosition::PositionType::FromTopRight:
56 63
 						valid = false;
57 64
 						break;
58 65
 					default:
... ...
@@ -62,7 +69,7 @@ void DialogTemplate::DialogGroup::CalculatePositions(bool doRightAndBottom)
62 69
 			if (valid)
63 70
 			{
64 71
 				Rect<short> other = this->rect;
65
-				if (control->relativePosition->type == RelativePosition::TO_SIBLING)
72
+				if (control->relativePosition->type == RelativePosition::BaseType::ToSibling)
66 73
 				{
67 74
 					short siblingsBack = dynamic_cast<const RelativePositionToSibling *>(control->relativePosition.get())->siblingsBack;
68 75
 					if (x - siblingsBack >= 0)
... ...
@@ -82,15 +89,15 @@ void DialogTemplate::DialogGroup::CalculateSize()
82 89
 		bool usePosition = true;
83 90
 		if (control->relativePosition)
84 91
 		{
85
-			if (control->relativePosition->type == RelativePosition::TO_PARENT)
92
+			if (control->relativePosition->type == RelativePosition::BaseType::ToParent)
86 93
 			{
87 94
 				switch (control->relativePosition->positionType)
88 95
 				{
89
-					case RelativePosition::FROM_BOTTOM:
90
-					case RelativePosition::FROM_BOTTOMLEFT:
91
-					case RelativePosition::FROM_BOTTOMRIGHT:
92
-					case RelativePosition::FROM_RIGHT:
93
-					case RelativePosition::FROM_TOPRIGHT:
96
+					case RelativePosition::PositionType::FromBottom:
97
+					case RelativePosition::PositionType::FromBottomLeft:
98
+					case RelativePosition::PositionType::FromBottomRight:
99
+					case RelativePosition::PositionType::FromRight:
100
+					case RelativePosition::PositionType::FromTopRight:
94 101
 						usePosition = false;
95 102
 						/*if (control->relativePosition->relativePosition.x != -1 && control->rect.size.width + control->relativePosition->relativePosition.x > maxOtherWidth)
96 103
 							maxOtherWidth = control->rect.size.width + control->relativePosition->relativePosition.x;
... ...
@@ -114,27 +121,27 @@ void DialogTemplate::DialogGroup::CalculateSize()
114 121
 	this->rect.size.height = (maxY - this->rect.position.y) + maxOtherHeight + 7;
115 122
 }
116 123
 
117
-uint16_t DialogTemplate::DialogGroup::GetControlCount() const
124
+std::uint16_t DialogTemplate::DialogGroup::GetControlCount() const
118 125
 {
119
-	uint16_t count = 1;
126
+	std::uint16_t count = 1;
120 127
 	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control) { count += control->GetControlCount(); });
121 128
 	return count;
122 129
 }
123 130
 
124
-std::vector<uint8_t> DialogTemplate::DialogGroup::GenerateControlTemplate() const
131
+std::vector<std::uint8_t> DialogTemplate::DialogGroup::GenerateControlTemplate() const
125 132
 {
126
-	auto data = std::vector<uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->groupName.length() + 1)), 0);
133
+	auto data = std::vector<std::uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->groupName.length() + 1)), 0);
127 134
 
128
-	*reinterpret_cast<uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | BS_GROUPBOX | this->style;
129
-	*reinterpret_cast<uint32_t *>(&data[4]) = this->exstyle;
130
-	*reinterpret_cast<uint16_t *>(&data[8]) = this->rect.position.x;
131
-	*reinterpret_cast<uint16_t *>(&data[10]) = this->rect.position.y;
132
-	*reinterpret_cast<uint16_t *>(&data[12]) = this->rect.size.width;
133
-	*reinterpret_cast<uint16_t *>(&data[14]) = this->rect.size.height;
134
-	*reinterpret_cast<uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
135
-	*reinterpret_cast<uint16_t *>(&data[18]) = 0xFFFF;
136
-	*reinterpret_cast<uint16_t *>(&data[20]) = 0x0080;
137
-	std::copy_n(this->groupName.c_str(), this->groupName.length(), reinterpret_cast<wchar_t *>(&data[22]));
135
+	*reinterpret_cast<std::uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | BS_GROUPBOX | this->style;
136
+	*reinterpret_cast<std::uint32_t *>(&data[4]) = this->exstyle;
137
+	*reinterpret_cast<std::uint16_t *>(&data[8]) = this->rect.position.x;
138
+	*reinterpret_cast<std::uint16_t *>(&data[10]) = this->rect.position.y;
139
+	*reinterpret_cast<std::uint16_t *>(&data[12]) = this->rect.size.width;
140
+	*reinterpret_cast<std::uint16_t *>(&data[14]) = this->rect.size.height;
141
+	*reinterpret_cast<std::uint16_t *>(&data[16]) = static_cast<std::uint16_t>(this->id);
142
+	*reinterpret_cast<std::uint16_t *>(&data[18]) = 0xFFFF;
143
+	*reinterpret_cast<std::uint16_t *>(&data[20]) = 0x0080;
144
+	CopyToString(this->groupName, reinterpret_cast<wchar_t *>(&data[22]));
138 145
 
139 146
 	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
140 147
 	{
... ...
@@ -145,44 +152,44 @@ std::vector<uint8_t> DialogTemplate::DialogGroup::GenerateControlTemplate() cons
145 152
 	return data;
146 153
 }
147 154
 
148
-std::vector<uint8_t> DialogTemplate::DialogControlWithoutLabel::GenerateControlTemplate() const
155
+std::vector<std::uint8_t> DialogTemplate::DialogControlWithoutLabel::GenerateControlTemplate() const
149 156
 {
150
-	auto data = std::vector<uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t)), 0);
157
+	auto data = std::vector<std::uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t)), 0);
151 158
 
152
-	*reinterpret_cast<uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | this->style;
153
-	*reinterpret_cast<uint32_t *>(&data[4]) = this->exstyle;
154
-	*reinterpret_cast<uint16_t *>(&data[8]) = this->rect.position.x;
155
-	*reinterpret_cast<uint16_t *>(&data[10]) = this->rect.position.y;
156
-	*reinterpret_cast<uint16_t *>(&data[12]) = this->rect.size.width;
157
-	*reinterpret_cast<uint16_t *>(&data[14]) = this->rect.size.height;
158
-	*reinterpret_cast<uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
159
-	*reinterpret_cast<uint16_t *>(&data[18]) = 0xFFFF;
160
-	*reinterpret_cast<uint16_t *>(&data[20]) = this->type;
159
+	*reinterpret_cast<std::uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | this->style;
160
+	*reinterpret_cast<std::uint32_t *>(&data[4]) = this->exstyle;
161
+	*reinterpret_cast<std::uint16_t *>(&data[8]) = this->rect.position.x;
162
+	*reinterpret_cast<std::uint16_t *>(&data[10]) = this->rect.position.y;
163
+	*reinterpret_cast<std::uint16_t *>(&data[12]) = this->rect.size.width;
164
+	*reinterpret_cast<std::uint16_t *>(&data[14]) = this->rect.size.height;
165
+	*reinterpret_cast<std::uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
166
+	*reinterpret_cast<std::uint16_t *>(&data[18]) = 0xFFFF;
167
+	*reinterpret_cast<std::uint16_t *>(&data[20]) = this->type;
161 168
 
162 169
 	return data;
163 170
 }
164 171
 
165
-std::vector<uint8_t> DialogTemplate::DialogControlWithLabel::GenerateControlTemplate() const
172
+std::vector<std::uint8_t> DialogTemplate::DialogControlWithLabel::GenerateControlTemplate() const
166 173
 {
167
-	auto data = std::vector<uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->label.length() + 1)), 0);
174
+	auto data = std::vector<std::uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->label.length() + 1)), 0);
168 175
 
169
-	*reinterpret_cast<uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | this->style;
170
-	*reinterpret_cast<uint32_t *>(&data[4]) = this->exstyle;
171
-	*reinterpret_cast<uint16_t *>(&data[8]) = this->rect.position.x;
172
-	*reinterpret_cast<uint16_t *>(&data[10]) = this->rect.position.y;
173
-	*reinterpret_cast<uint16_t *>(&data[12]) = this->rect.size.width;
174
-	*reinterpret_cast<uint16_t *>(&data[14]) = this->rect.size.height;
175
-	*reinterpret_cast<uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
176
-	*reinterpret_cast<uint16_t *>(&data[18]) = 0xFFFF;
177
-	*reinterpret_cast<uint16_t *>(&data[20]) = this->type;
178
-	std::copy_n(this->label.c_str(), this->label.length(), reinterpret_cast<wchar_t *>(&data[22]));
176
+	*reinterpret_cast<std::uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | this->style;
177
+	*reinterpret_cast<std::uint32_t *>(&data[4]) = this->exstyle;
178
+	*reinterpret_cast<std::uint16_t *>(&data[8]) = this->rect.position.x;
179
+	*reinterpret_cast<std::uint16_t *>(&data[10]) = this->rect.position.y;
180
+	*reinterpret_cast<std::uint16_t *>(&data[12]) = this->rect.size.width;
181
+	*reinterpret_cast<std::uint16_t *>(&data[14]) = this->rect.size.height;
182
+	*reinterpret_cast<std::uint16_t *>(&data[16]) = static_cast<std::uint16_t>(this->id);
183
+	*reinterpret_cast<std::uint16_t *>(&data[18]) = 0xFFFF;
184
+	*reinterpret_cast<std::uint16_t *>(&data[20]) = this->type;
185
+	CopyToString(this->label, reinterpret_cast<wchar_t *>(&data[22]));
179 186
 
180 187
 	return data;
181 188
 }
182 189
 
183
-uint16_t DialogTemplate::GetTotalControlCount() const
190
+std::uint16_t DialogTemplate::GetTotalControlCount() const
184 191
 {
185
-	uint16_t count = 0;
192
+	std::uint16_t count = 0;
186 193
 	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control) { count += control->GetControlCount(); });
187 194
 	return count;
188 195
 }
... ...
@@ -198,32 +205,32 @@ void DialogTemplate::AddGroupControl(const DialogControlBuilder<DialogGroupBuild
198 205
 
199 206
 void DialogTemplate::AddEditBoxControl(const DialogControlBuilder<DialogEditBoxBuilder> &builder)
200 207
 {
201
-	this->AddControlToGroup(std::move(DialogEditBox::CreateControl(builder)), builder);
208
+	this->AddControlToGroup(DialogEditBox::CreateControl(builder), builder);
202 209
 }
203 210
 
204 211
 void DialogTemplate::AddLabelControl(const DialogControlBuilder<DialogLabelBuilder> &builder)
205 212
 {
206
-	this->AddControlToGroup(std::move(DialogLabel::CreateControl(builder)), builder);
213
+	this->AddControlToGroup(DialogLabel::CreateControl(builder), builder);
207 214
 }
208 215
 
209 216
 void DialogTemplate::AddCheckBoxControl(const DialogControlBuilder<DialogCheckBoxBuilder> &builder)
210 217
 {
211
-	this->AddControlToGroup(std::move(DialogButton::CreateControl(builder)), builder);
218
+	this->AddControlToGroup(DialogButton::CreateControl(builder), builder);
212 219
 }
213 220
 
214 221
 void DialogTemplate::AddButtonControl(const DialogControlBuilder<DialogButtonBuilder> &builder)
215 222
 {
216
-	this->AddControlToGroup(std::move(DialogButton::CreateControl(builder)), builder);
223
+	this->AddControlToGroup(DialogButton::CreateControl(builder), builder);
217 224
 }
218 225
 
219 226
 void DialogTemplate::AddListBoxControl(const DialogControlBuilder<DialogListBoxBuilder> &builder)
220 227
 {
221
-	this->AddControlToGroup(std::move(DialogListBox::CreateControl(builder)), builder);
228
+	this->AddControlToGroup(DialogListBox::CreateControl(builder), builder);
222 229
 }
223 230
 
224 231
 void DialogTemplate::AddComboBoxControl(const DialogControlBuilder<DialogComboBoxBuilder> &builder)
225 232
 {
226
-	this->AddControlToGroup(std::move(DialogComboBox::CreateControl(builder)), builder);
233
+	this->AddControlToGroup(DialogComboBox::CreateControl(builder), builder);
227 234
 }
228 235
 
229 236
 bool DialogTemplate::CalculateControlPosition(short index, bool doRightAndBottom)
... ...
@@ -232,15 +239,15 @@ bool DialogTemplate::CalculateControlPosition(short index, bool doRightAndBottom
232 239
 	bool valid = true;
233 240
 	if (control->relativePosition)
234 241
 	{
235
-		if (!doRightAndBottom && control->relativePosition->type == RelativePosition::TO_PARENT)
242
+		if (!doRightAndBottom && control->relativePosition->type == RelativePosition::BaseType::ToParent)
236 243
 		{
237 244
 			switch (control->relativePosition->positionType)
238 245
 			{
239
-				case RelativePosition::FROM_BOTTOM:
240
-				case RelativePosition::FROM_BOTTOMLEFT:
241
-				case RelativePosition::FROM_BOTTOMRIGHT:
242
-				case RelativePosition::FROM_RIGHT:
243
-				case RelativePosition::FROM_TOPRIGHT:
246
+				case RelativePosition::PositionType::FromBottom:
247
+				case RelativePosition::PositionType::FromBottomLeft:
248
+				case RelativePosition::PositionType::FromBottomRight:
249
+				case RelativePosition::PositionType::FromRight:
250
+				case RelativePosition::PositionType::FromTopRight:
244 251
 					valid = false;
245 252
 					break;
246 253
 				default:
... ...
@@ -250,7 +257,7 @@ bool DialogTemplate::CalculateControlPosition(short index, bool doRightAndBottom
250 257
 		if (valid)
251 258
 		{
252 259
 			Rect<short> other = Rect<short>(Point<short>(), this->size);
253
-			if (control->relativePosition->type == RelativePosition::TO_SIBLING)
260
+			if (control->relativePosition->type == RelativePosition::BaseType::ToSibling)
254 261
 			{
255 262
 				short siblingsBack = dynamic_cast<const RelativePositionToSibling *>(control->relativePosition.get())->siblingsBack;
256 263
 				if (index - siblingsBack >= 0)
... ...
@@ -270,15 +277,15 @@ void DialogTemplate::CalculateSize()
270 277
 		bool usePosition = true;
271 278
 		if (control->relativePosition)
272 279
 		{
273
-			if (control->relativePosition->type == RelativePosition::TO_PARENT)
280
+			if (control->relativePosition->type == RelativePosition::BaseType::ToParent)
274 281
 			{
275 282
 				switch (control->relativePosition->positionType)
276 283
 				{
277
-					case RelativePosition::FROM_BOTTOM:
278
-					case RelativePosition::FROM_BOTTOMLEFT:
279
-					case RelativePosition::FROM_BOTTOMRIGHT:
280
-					case RelativePosition::FROM_RIGHT:
281
-					case RelativePosition::FROM_TOPRIGHT:
284
+					case RelativePosition::PositionType::FromBottom:
285
+					case RelativePosition::PositionType::FromBottomLeft:
286
+					case RelativePosition::PositionType::FromBottomRight:
287
+					case RelativePosition::PositionType::FromRight:
288
+					case RelativePosition::PositionType::FromTopRight:
282 289
 						usePosition = false;
283 290
 						/*if (control->relativePosition->relativePosition.x != -1 && control->rect.size.width + control->relativePosition->relativePosition.x > maxOtherWidth)
284 291
 							maxOtherWidth = control->rect.size.width + control->relativePosition->relativePosition.x;
... ...
@@ -310,7 +317,7 @@ void DialogTemplate::AutoSize()
310 317
 	{
311 318
 		auto &control = this->controls[x];
312 319
 		bool valid = this->CalculateControlPosition(x, false);
313
-		if (valid && control->controlType == GROUP_CONTROL)
320
+		if (valid && control->controlType == DialogControlType::Group)
314 321
 		{
315 322
 			dynamic_cast<DialogGroup *>(control.get())->CalculatePositions(false);
316 323
 			// Technically step 2, but calculate the size of the group
... ...
@@ -323,7 +330,7 @@ void DialogTemplate::AutoSize()
323 330
 	for (x = 0; x < num; ++x)
324 331
 	{
325 332
 		auto &control = this->controls[x];
326
-		if (control->controlType != GROUP_CONTROL)
333
+		if (control->controlType != DialogControlType::Group)
327 334
 			continue;
328 335
 		control->rect.size.width = maxGroupWidth;
329 336
 		dynamic_cast<DialogGroup *>(control.get())->CalculatePositions(true);
... ...
@@ -338,19 +345,19 @@ void DialogTemplate::AutoSize()
338 345
 const DLGTEMPLATE *DialogTemplate::GenerateTemplate()
339 346
 {
340 347
 	this->templateData.clear();
341
-	uint16_t controlCount = this->GetTotalControlCount();
348
+	std::uint16_t controlCount = this->GetTotalControlCount();
342 349
 	this->templateData.resize(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->title.length() + 1 + (this->fontName.empty() ? 0 : this->fontName.length() + 1))), 0);
343 350
 
344
-	*reinterpret_cast<uint32_t *>(&this->templateData[0]) = this->style | (this->fontName.empty() ? 0 : DS_SETFONT);
345
-	*reinterpret_cast<uint32_t *>(&this->templateData[4]) = this->exstyle;
346
-	*reinterpret_cast<uint16_t *>(&this->templateData[8]) = controlCount;
347
-	*reinterpret_cast<uint16_t *>(&this->templateData[14]) = this->size.width;
348
-	*reinterpret_cast<uint16_t *>(&this->templateData[16]) = this->size.height;
349
-	std::copy_n(this->title.c_str(), this->title.length(), reinterpret_cast<wchar_t *>(&this->templateData[22]));
351
+	*reinterpret_cast<std::uint32_t *>(&this->templateData[0]) = this->style | (this->fontName.empty() ? 0 : DS_SETFONT);
352
+	*reinterpret_cast<std::uint32_t *>(&this->templateData[4]) = this->exstyle;
353
+	*reinterpret_cast<std::uint16_t *>(&this->templateData[8]) = controlCount;
354
+	*reinterpret_cast<std::uint16_t *>(&this->templateData[14]) = this->size.width;
355
+	*reinterpret_cast<std::uint16_t *>(&this->templateData[16]) = this->size.height;
356
+	CopyToString(this->title, reinterpret_cast<wchar_t *>(&this->templateData[22]));
350 357
 	if (!this->fontName.empty())
351 358
 	{
352
-		*reinterpret_cast<uint16_t *>(&this->templateData[22 + sizeof(wchar_t) * (this->title.length() + 1)]) = this->fontSizeInPts;
353
-		std::copy_n(this->fontName.c_str(), this->fontName.length(), reinterpret_cast<wchar_t *>(&this->templateData[24 + sizeof(wchar_t) * (this->title.length() + 1)]));
359
+		*reinterpret_cast<std::uint16_t *>(&this->templateData[22 + sizeof(wchar_t) * (this->title.length() + 1)]) = this->fontSizeInPts;
360
+		CopyToString(this->fontName, reinterpret_cast<wchar_t *>(&this->templateData[24 + sizeof(wchar_t) * (this->title.length() + 1)]));
354 361
 	}
355 362
 
356 363
 	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
Browse code

Cleanup a few comments and strings.

Naram Qashat authored on 2021/03/20 20:25:12
Showing 1 changed files
... ...
@@ -313,7 +313,7 @@ void DialogTemplate::AutoSize()
313 313
 		if (valid && control->controlType == GROUP_CONTROL)
314 314
 		{
315 315
 			dynamic_cast<DialogGroup *>(control.get())->CalculatePositions(false);
316
-			// Techically step 2, but calculate the size of the group
316
+			// Technically step 2, but calculate the size of the group
317 317
 			dynamic_cast<DialogGroup *>(control.get())->CalculateSize();
318 318
 			if (control->rect.size.width > maxGroupWidth)
319 319
 				maxGroupWidth = control->rect.size.width;
Browse code

Replace memcpy/memset with std::copy_n/std::fill_n.

Naram Qashat authored on 2021/03/19 16:25:21
Showing 1 changed files
... ...
@@ -134,7 +134,7 @@ std::vector<uint8_t> DialogTemplate::DialogGroup::GenerateControlTemplate() cons
134 134
 	*reinterpret_cast<uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
135 135
 	*reinterpret_cast<uint16_t *>(&data[18]) = 0xFFFF;
136 136
 	*reinterpret_cast<uint16_t *>(&data[20]) = 0x0080;
137
-	memcpy(reinterpret_cast<wchar_t *>(&data[22]), this->groupName.c_str(), this->groupName.length() * sizeof(wchar_t));
137
+	std::copy_n(this->groupName.c_str(), this->groupName.length(), reinterpret_cast<wchar_t *>(&data[22]));
138 138
 
139 139
 	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
140 140
 	{
... ...
@@ -175,7 +175,7 @@ std::vector<uint8_t> DialogTemplate::DialogControlWithLabel::GenerateControlTemp
175 175
 	*reinterpret_cast<uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
176 176
 	*reinterpret_cast<uint16_t *>(&data[18]) = 0xFFFF;
177 177
 	*reinterpret_cast<uint16_t *>(&data[20]) = this->type;
178
-	memcpy(reinterpret_cast<wchar_t *>(&data[22]), this->label.c_str(), this->label.length() * sizeof(wchar_t));
178
+	std::copy_n(this->label.c_str(), this->label.length(), reinterpret_cast<wchar_t *>(&data[22]));
179 179
 
180 180
 	return data;
181 181
 }
... ...
@@ -346,11 +346,11 @@ const DLGTEMPLATE *DialogTemplate::GenerateTemplate()
346 346
 	*reinterpret_cast<uint16_t *>(&this->templateData[8]) = controlCount;
347 347
 	*reinterpret_cast<uint16_t *>(&this->templateData[14]) = this->size.width;
348 348
 	*reinterpret_cast<uint16_t *>(&this->templateData[16]) = this->size.height;
349
-	memcpy(reinterpret_cast<wchar_t *>(&this->templateData[22]), this->title.c_str(), this->title.length() * sizeof(wchar_t));
349
+	std::copy_n(this->title.c_str(), this->title.length(), reinterpret_cast<wchar_t *>(&this->templateData[22]));
350 350
 	if (!this->fontName.empty())
351 351
 	{
352 352
 		*reinterpret_cast<uint16_t *>(&this->templateData[22 + sizeof(wchar_t) * (this->title.length() + 1)]) = this->fontSizeInPts;
353
-		memcpy(reinterpret_cast<wchar_t *>(&this->templateData[24 + sizeof(wchar_t) * (this->title.length() + 1)]), this->fontName.c_str(), this->fontName.length() * sizeof(wchar_t));
353
+		std::copy_n(this->fontName.c_str(), this->fontName.length(), reinterpret_cast<wchar_t *>(&this->templateData[24 + sizeof(wchar_t) * (this->title.length() + 1)]));
354 354
 	}
355 355
 
356 356
 	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
Browse code

Remove last modification date from files.

(I never remember to update these and besides, GitHub history can show when they were last modified.)

Naram Qashat authored on 2021/03/19 10:58:12
Showing 1 changed files
... ...
@@ -1,7 +1,6 @@
1 1
 /*
2 2
  * Windows Dynamic Dialog Builder framework
3 3
  * By Naram Qashat (CyberBotX) [cyberbotx@cyberbotx.com]
4
- * Last modification on 2013-04-23
5 4
  */
6 5
 
7 6
 #include "DialogBuilder.h"
Browse code

[Framework] Super minor cleanups.

Naram Qashat authored on 2020/08/07 23:27:08
Showing 1 changed files
... ...
@@ -38,7 +38,7 @@ Point<short> RelativePosition::CalculatePosition(const Rect<short> &child, const
38 38
 
39 39
 void DialogTemplate::DialogGroup::CalculatePositions(bool doRightAndBottom)
40 40
 {
41
-	short x = 0, num = this->controls.empty() ? 0 : this->controls.size();
41
+	short x = 0, num = this->controls.empty() ? 0 : static_cast<short>(this->controls.size());
42 42
 	for (; x < num; ++x)
43 43
 	{
44 44
 		auto &control = this->controls[x];
... ...
@@ -306,7 +306,7 @@ void DialogTemplate::CalculateSize()
306 306
 void DialogTemplate::AutoSize()
307 307
 {
308 308
 	// Step 1: Calculate positions of dialog controls (only if the controls's position is relative to a sibling or if it's relativeness to the parent is from the top, left, or top left)
309
-	short x = 0, num = this->controls.empty() ? 0 : this->controls.size(), maxGroupWidth = 0;
309
+	short x = 0, num = this->controls.empty() ? 0 : static_cast<short>(this->controls.size()), maxGroupWidth = 0;
310 310
 	for (; x < num; ++x)
311 311
 	{
312 312
 		auto &control = this->controls[x];
Browse code

Added Lanczos interpolation to the NCSF plugin, and cleaned up a bit of the other code, as well as removing pstdint.h since it's no longer needed.

Naram Qashat authored on 2013/04/23 20:29:21
Showing 1 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * Windows Dynamic Dialog Builder framework
3 3
  * By Naram Qashat (CyberBotX) [cyberbotx@cyberbotx.com]
4
- * Last modification on 2013-03-30
4
+ * Last modification on 2013-04-23
5 5
  */
6 6
 
7 7
 #include "DialogBuilder.h"
... ...
@@ -16,13 +16,33 @@ static inline uint32_t getNextMultipleOf4(uint32_t origNum)
16 16
 	return origNum + 4 - remainder;
17 17
 }
18 18
 
19
+Point<short> RelativePosition::CalculatePosition(const Rect<short> &child, const Rect<short> &other)
20
+{
21
+	Point<short> newPosition = child.position;
22
+	if (this->relativePosition.y != -1)
23
+	{
24
+		if (this->positionType == FROM_TOP || this->positionType == FROM_TOPLEFT || this->positionType == FROM_TOPRIGHT)
25
+			newPosition.y = other.position.y + this->relativePosition.y;
26
+		if (this->positionType == FROM_BOTTOM || this->positionType == FROM_BOTTOMLEFT || this->positionType == FROM_BOTTOMRIGHT)
27
+			newPosition.y = other.position.y + other.size.height + this->relativePosition.y;
28
+	}
29
+	if (this->relativePosition.x != -1)
30
+	{
31
+		if (this->positionType == FROM_LEFT || this->positionType == FROM_TOPLEFT || this->positionType == FROM_BOTTOMLEFT)
32
+			newPosition.x = other.position.x + this->relativePosition.x;
33
+		if (this->positionType == FROM_RIGHT || this->positionType == FROM_TOPRIGHT || this->positionType == FROM_BOTTOMRIGHT)
34
+			newPosition.x = other.position.x + other.size.width + this->relativePosition.x;
35
+	}
36
+	return newPosition;
37
+}
38
+
19 39
 void DialogTemplate::DialogGroup::CalculatePositions(bool doRightAndBottom)
20 40
 {
21 41
 	short x = 0, num = this->controls.empty() ? 0 : this->controls.size();
22 42
 	for (; x < num; ++x)
23 43
 	{
24 44
 		auto &control = this->controls[x];
25
-		if (control->relativePosition.get())
45
+		if (control->relativePosition)
26 46
 		{
27 47
 			bool valid = true;
28 48
 			if (!doRightAndBottom && control->relativePosition->type == RelativePosition::TO_PARENT)
... ...
@@ -61,7 +81,7 @@ void DialogTemplate::DialogGroup::CalculateSize()
61 81
 	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
62 82
 	{
63 83
 		bool usePosition = true;
64
-		if (control->relativePosition.get())
84
+		if (control->relativePosition)
65 85
 		{
66 86
 			if (control->relativePosition->type == RelativePosition::TO_PARENT)
67 87
 			{
... ...
@@ -129,6 +149,7 @@ std::vector<uint8_t> DialogTemplate::DialogGroup::GenerateControlTemplate() cons
129 149
 std::vector<uint8_t> DialogTemplate::DialogControlWithoutLabel::GenerateControlTemplate() const
130 150
 {
131 151
 	auto data = std::vector<uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t)), 0);
152
+
132 153
 	*reinterpret_cast<uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | this->style;
133 154
 	*reinterpret_cast<uint32_t *>(&data[4]) = this->exstyle;
134 155
 	*reinterpret_cast<uint16_t *>(&data[8]) = this->rect.position.x;
... ...
@@ -145,6 +166,7 @@ std::vector<uint8_t> DialogTemplate::DialogControlWithoutLabel::GenerateControlT
145 166
 std::vector<uint8_t> DialogTemplate::DialogControlWithLabel::GenerateControlTemplate() const
146 167
 {
147 168
 	auto data = std::vector<uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->label.length() + 1)), 0);
169
+
148 170
 	*reinterpret_cast<uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | this->style;
149 171
 	*reinterpret_cast<uint32_t *>(&data[4]) = this->exstyle;
150 172
 	*reinterpret_cast<uint16_t *>(&data[8]) = this->rect.position.x;
... ...
@@ -168,7 +190,7 @@ uint16_t DialogTemplate::GetTotalControlCount() const
168 190
 
169 191
 void DialogTemplate::AddGroupControl(const DialogControlBuilder<DialogGroupBuilder> &builder)
170 192
 {
171
-	std::unique_ptr<DialogControl> newGroup = DialogGroup::CreateControl(builder);
193
+	auto newGroup = DialogGroup::CreateControl(builder);
172 194
 	if (builder.index == -1)
173 195
 		this->controls.push_back(std::move(newGroup));
174 196
 	else
... ...
@@ -209,7 +231,7 @@ bool DialogTemplate::CalculateControlPosition(short index, bool doRightAndBottom
209 231
 {
210 232
 	auto &control = this->controls[index];
211 233
 	bool valid = true;
212
-	if (control->relativePosition.get())
234
+	if (control->relativePosition)
213 235
 	{
214 236
 		if (!doRightAndBottom && control->relativePosition->type == RelativePosition::TO_PARENT)
215 237
 		{
... ...
@@ -247,7 +269,7 @@ void DialogTemplate::CalculateSize()
247 269
 	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
248 270
 	{
249 271
 		bool usePosition = true;
250
-		if (control->relativePosition.get())
272
+		if (control->relativePosition)
251 273
 		{
252 274
 			if (control->relativePosition->type == RelativePosition::TO_PARENT)
253 275
 			{
... ...
@@ -319,6 +341,7 @@ const DLGTEMPLATE *DialogTemplate::GenerateTemplate()
319 341
 	this->templateData.clear();
320 342
 	uint16_t controlCount = this->GetTotalControlCount();
321 343
 	this->templateData.resize(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->title.length() + 1 + (this->fontName.empty() ? 0 : this->fontName.length() + 1))), 0);
344
+
322 345
 	*reinterpret_cast<uint32_t *>(&this->templateData[0]) = this->style | (this->fontName.empty() ? 0 : DS_SETFONT);
323 346
 	*reinterpret_cast<uint32_t *>(&this->templateData[4]) = this->exstyle;
324 347
 	*reinterpret_cast<uint16_t *>(&this->templateData[8]) = controlCount;
Browse code

Cleanup of some warnings, updating modification dates, using nullptr instead of NULL in some cases.

Naram Qashat authored on 2013/03/30 16:17:42
Showing 1 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * Windows Dynamic Dialog Builder framework
3 3
  * By Naram Qashat (CyberBotX) [cyberbotx@cyberbotx.com]
4
- * Last modification on 2013-03-25
4
+ * Last modification on 2013-03-30
5 5
  */
6 6
 
7 7
 #include "DialogBuilder.h"
Browse code

Utilized more C++11 constructs. Also attempted to fix issue with the Info Box not wanting to come up anymore if the file doesn't exist.

Naram Qashat authored on 2013/03/30 07:36:38
Showing 1 changed files
... ...
@@ -21,7 +21,7 @@ void DialogTemplate::DialogGroup::CalculatePositions(bool doRightAndBottom)
21 21
 	short x = 0, num = this->controls.empty() ? 0 : this->controls.size();
22 22
 	for (; x < num; ++x)
23 23
 	{
24
-		DialogControl *control = this->controls[x];
24
+		auto &control = this->controls[x];
25 25
 		if (control->relativePosition.get())
26 26
 		{
27 27
 			bool valid = true;
... ...
@@ -58,10 +58,8 @@ void DialogTemplate::DialogGroup::CalculatePositions(bool doRightAndBottom)
58 58
 void DialogTemplate::DialogGroup::CalculateSize()
59 59
 {
60 60
 	short maxX = 0, maxY = 0, maxOtherWidth = 0, maxOtherHeight = 0;
61
-	for (DialogTemplate::Controls::const_iterator curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
61
+	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
62 62
 	{
63
-		DialogControl *control = *curr;
64
-
65 63
 		bool usePosition = true;
66 64
 		if (control->relativePosition.get())
67 65
 		{
... ...
@@ -92,7 +90,7 @@ void DialogTemplate::DialogGroup::CalculateSize()
92 90
 			if (control->rect.position.y + control->GetControlHeight() > maxY)
93 91
 				maxY = control->rect.position.y + control->GetControlHeight();
94 92
 		}
95
-	}
93
+	});
96 94
 	this->rect.size.width = (maxX - this->rect.position.x) + maxOtherWidth + 4;
97 95
 	this->rect.size.height = (maxY - this->rect.position.y) + maxOtherHeight + 7;
98 96
 }
... ...
@@ -100,8 +98,7 @@ void DialogTemplate::DialogGroup::CalculateSize()
100 98
 uint16_t DialogTemplate::DialogGroup::GetControlCount() const
101 99
 {
102 100
 	uint16_t count = 1;
103
-	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
104
-		count += (*curr)->GetControlCount();
101
+	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control) { count += control->GetControlCount(); });
105 102
 	return count;
106 103
 }
107 104
 
... ...
@@ -120,11 +117,11 @@ std::vector<uint8_t> DialogTemplate::DialogGroup::GenerateControlTemplate() cons
120 117
 	*reinterpret_cast<uint16_t *>(&data[20]) = 0x0080;
121 118
 	memcpy(reinterpret_cast<wchar_t *>(&data[22]), this->groupName.c_str(), this->groupName.length() * sizeof(wchar_t));
122 119
 
123
-	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
120
+	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
124 121
 	{
125
-		auto controlData = (*curr)->GenerateControlTemplate();
122
+		auto controlData = control->GenerateControlTemplate();
126 123
 		data.insert(data.end(), controlData.begin(), controlData.end());
127
-	}
124
+	});
128 125
 
129 126
 	return data;
130 127
 }
... ...
@@ -165,53 +162,52 @@ std::vector<uint8_t> DialogTemplate::DialogControlWithLabel::GenerateControlTemp
165 162
 uint16_t DialogTemplate::GetTotalControlCount() const
166 163
 {
167 164
 	uint16_t count = 0;
168
-	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
169
-		count += (*curr)->GetControlCount();
165
+	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control) { count += control->GetControlCount(); });
170 166
 	return count;
171 167
 }
172 168
 
173 169
 void DialogTemplate::AddGroupControl(const DialogControlBuilder<DialogGroupBuilder> &builder)
174 170
 {
175
-	DialogGroup *newGroup = DialogGroup::CreateControl(builder);
171
+	std::unique_ptr<DialogControl> newGroup = DialogGroup::CreateControl(builder);
176 172
 	if (builder.index == -1)
177
-		this->controls.push_back(newGroup);
173
+		this->controls.push_back(std::move(newGroup));
178 174
 	else
179
-		this->controls.insert(this->controls.begin() + builder.index, newGroup);
175
+		this->controls.insert(this->controls.begin() + builder.index, std::move(newGroup));
180 176
 }
181 177
 
182 178
 void DialogTemplate::AddEditBoxControl(const DialogControlBuilder<DialogEditBoxBuilder> &builder)
183 179
 {
184
-	this->AddControlToGroup(DialogEditBox::CreateControl(builder), builder);
180
+	this->AddControlToGroup(std::move(DialogEditBox::CreateControl(builder)), builder);
185 181
 }
186 182
 
187 183
 void DialogTemplate::AddLabelControl(const DialogControlBuilder<DialogLabelBuilder> &builder)
188 184
 {
189
-	this->AddControlToGroup(DialogLabel::CreateControl(builder), builder);
185
+	this->AddControlToGroup(std::move(DialogLabel::CreateControl(builder)), builder);
190 186
 }
191 187
 
192 188
 void DialogTemplate::AddCheckBoxControl(const DialogControlBuilder<DialogCheckBoxBuilder> &builder)
193 189
 {
194
-	this->AddControlToGroup(DialogButton::CreateControl(builder), builder);
190
+	this->AddControlToGroup(std::move(DialogButton::CreateControl(builder)), builder);
195 191
 }
196 192
 
197 193
 void DialogTemplate::AddButtonControl(const DialogControlBuilder<DialogButtonBuilder> &builder)
198 194
 {
199
-	this->AddControlToGroup(DialogButton::CreateControl(builder), builder);
195
+	this->AddControlToGroup(std::move(DialogButton::CreateControl(builder)), builder);
200 196
 }
201 197
 
202 198
 void DialogTemplate::AddListBoxControl(const DialogControlBuilder<DialogListBoxBuilder> &builder)
203 199
 {
204
-	this->AddControlToGroup(DialogListBox::CreateControl(builder), builder);
200
+	this->AddControlToGroup(std::move(DialogListBox::CreateControl(builder)), builder);
205 201
 }
206 202
 
207 203
 void DialogTemplate::AddComboBoxControl(const DialogControlBuilder<DialogComboBoxBuilder> &builder)
208 204
 {
209
-	this->AddControlToGroup(DialogComboBox::CreateControl(builder), builder);
205
+	this->AddControlToGroup(std::move(DialogComboBox::CreateControl(builder)), builder);
210 206
 }
211 207
 
212 208
 bool DialogTemplate::CalculateControlPosition(short index, bool doRightAndBottom)
213 209
 {
214
-	DialogControl *control = this->controls[index];
210
+	auto &control = this->controls[index];
215 211
 	bool valid = true;
216 212
 	if (control->relativePosition.get())
217 213
 	{
... ...
@@ -248,10 +244,8 @@ bool DialogTemplate::CalculateControlPosition(short index, bool doRightAndBottom
248 244
 void DialogTemplate::CalculateSize()
249 245
 {
250 246
 	short maxX = 0, maxY = 0, maxOtherWidth = 0, maxOtherHeight = 0;
251
-	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
247
+	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
252 248
 	{
253
-		DialogControl *control = *curr;
254
-
255 249
 		bool usePosition = true;
256 250
 		if (control->relativePosition.get())
257 251
 		{
... ...
@@ -282,7 +276,7 @@ void DialogTemplate::CalculateSize()
282 276
 			if (control->rect.position.y + control->GetControlHeight() > maxY)
283 277
 				maxY = control->rect.position.y + control->GetControlHeight();
284 278
 		}
285
-	}
279
+	});
286 280
 	this->size.width = maxX + maxOtherWidth + 7;
287 281
 	this->size.height = maxY + maxOtherHeight + 7;
288 282
 }
... ...
@@ -293,13 +287,13 @@ void DialogTemplate::AutoSize()
293 287
 	short x = 0, num = this->controls.empty() ? 0 : this->controls.size(), maxGroupWidth = 0;
294 288
 	for (; x < num; ++x)
295 289
 	{
296
-		DialogControl *control = this->controls[x];
290
+		auto &control = this->controls[x];
297 291
 		bool valid = this->CalculateControlPosition(x, false);
298 292
 		if (valid && control->controlType == GROUP_CONTROL)
299 293
 		{
300
-			dynamic_cast<DialogGroup *>(control)->CalculatePositions(false);
294
+			dynamic_cast<DialogGroup *>(control.get())->CalculatePositions(false);
301 295
 			// Techically step 2, but calculate the size of the group
302
-			dynamic_cast<DialogGroup *>(control)->CalculateSize();
296
+			dynamic_cast<DialogGroup *>(control.get())->CalculateSize();
303 297
 			if (control->rect.size.width > maxGroupWidth)
304 298
 				maxGroupWidth = control->rect.size.width;
305 299
 		}
... ...
@@ -307,11 +301,11 @@ void DialogTemplate::AutoSize()
307 301
 	// Step 2: Resize all group controls to be the same width, and then within the group, recalculate positions for all controls
308 302
 	for (x = 0; x < num; ++x)
309 303
 	{
310
-		DialogControl *control = this->controls[x];
304
+		auto &control = this->controls[x];
311 305
 		if (control->controlType != GROUP_CONTROL)
312 306
 			continue;
313 307
 		control->rect.size.width = maxGroupWidth;
314
-		dynamic_cast<DialogGroup *>(control)->CalculatePositions(true);
308
+		dynamic_cast<DialogGroup *>(control.get())->CalculatePositions(true);
315 309
 	}
316 310
 	// Step 3: Resize the dialog box itself
317 311
 	this->CalculateSize();
... ...
@@ -337,11 +331,11 @@ const DLGTEMPLATE *DialogTemplate::GenerateTemplate()
337 331
 		memcpy(reinterpret_cast<wchar_t *>(&this->templateData[24 + sizeof(wchar_t) * (this->title.length() + 1)]), this->fontName.c_str(), this->fontName.length() * sizeof(wchar_t));
338 332
 	}
339 333
 
340
-	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
334
+	std::for_each(this->controls.begin(), this->controls.end(), [&](const std::unique_ptr<DialogControl> &control)
341 335
 	{
342
-		auto controlData = (*curr)->GenerateControlTemplate();
336
+		auto controlData = control->GenerateControlTemplate();
343 337
 		this->templateData.insert(this->templateData.end(), controlData.begin(), controlData.end());
344
-	}
338
+	});
345 339
 
346 340
 	return reinterpret_cast<const DLGTEMPLATE *>(&this->templateData[0]);
347 341
 }
Browse code

Import actual code.

Naram Qashat authored on 2013/03/26 02:41:19
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,347 @@
1
+/*
2
+ * Windows Dynamic Dialog Builder framework
3
+ * By Naram Qashat (CyberBotX) [cyberbotx@cyberbotx.com]
4
+ * Last modification on 2013-03-25
5
+ */
6
+
7
+#include "DialogBuilder.h"
8
+
9
+// Code modified from the following answer on Stack Overflow:
10
+// http://stackoverflow.com/a/3407254
11
+static inline uint32_t getNextMultipleOf4(uint32_t origNum)
12
+{
13
+	uint32_t remainder = origNum % 4;
14
+	if (!remainder)
15
+		return origNum;
16
+	return origNum + 4 - remainder;
17
+}
18
+
19
+void DialogTemplate::DialogGroup::CalculatePositions(bool doRightAndBottom)
20
+{
21
+	short x = 0, num = this->controls.empty() ? 0 : this->controls.size();
22
+	for (; x < num; ++x)
23
+	{
24
+		DialogControl *control = this->controls[x];
25
+		if (control->relativePosition.get())
26
+		{
27
+			bool valid = true;
28
+			if (!doRightAndBottom && control->relativePosition->type == RelativePosition::TO_PARENT)
29
+			{
30
+				switch (control->relativePosition->positionType)
31
+				{
32
+					case RelativePosition::FROM_BOTTOM:
33
+					case RelativePosition::FROM_BOTTOMLEFT:
34
+					case RelativePosition::FROM_BOTTOMRIGHT:
35
+					case RelativePosition::FROM_RIGHT:
36
+					case RelativePosition::FROM_TOPRIGHT:
37
+						valid = false;
38
+						break;
39
+					default:
40
+						break;
41
+				}
42
+			}
43
+			if (valid)
44
+			{
45
+				Rect<short> other = this->rect;
46
+				if (control->relativePosition->type == RelativePosition::TO_SIBLING)
47
+				{
48
+					short siblingsBack = dynamic_cast<const RelativePositionToSibling *>(control->relativePosition.get())->siblingsBack;
49
+					if (x - siblingsBack >= 0)
50
+						other = this->controls[x - siblingsBack]->rect;
51
+				}
52
+				control->rect.position = control->relativePosition->CalculatePosition(control->rect, other);
53
+			}
54
+		}
55
+	}
56
+}
57
+
58
+void DialogTemplate::DialogGroup::CalculateSize()
59
+{
60
+	short maxX = 0, maxY = 0, maxOtherWidth = 0, maxOtherHeight = 0;
61
+	for (DialogTemplate::Controls::const_iterator curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
62
+	{
63
+		DialogControl *control = *curr;
64
+
65
+		bool usePosition = true;
66
+		if (control->relativePosition.get())
67
+		{
68
+			if (control->relativePosition->type == RelativePosition::TO_PARENT)
69
+			{
70
+				switch (control->relativePosition->positionType)
71
+				{
72
+					case RelativePosition::FROM_BOTTOM:
73
+					case RelativePosition::FROM_BOTTOMLEFT:
74
+					case RelativePosition::FROM_BOTTOMRIGHT:
75
+					case RelativePosition::FROM_RIGHT:
76
+					case RelativePosition::FROM_TOPRIGHT:
77
+						usePosition = false;
78
+						/*if (control->relativePosition->relativePosition.x != -1 && control->rect.size.width + control->relativePosition->relativePosition.x > maxOtherWidth)
79
+							maxOtherWidth = control->rect.size.width + control->relativePosition->relativePosition.x;
80
+						if (control->relativePosition->relativePosition.y != -1 && control->GetControlHeight() + control->relativePosition->relativePosition.y > maxOtherHeight)
81
+							maxOtherHeight = control->GetControlHeight() + control->relativePosition->relativePosition.y;*/
82
+						break;
83
+					default:
84
+						break;
85
+				}
86
+			}
87
+		}
88
+		if (usePosition)
89
+		{
90
+			if (control->rect.position.x + control->rect.size.width > maxX)
91
+				maxX = control->rect.position.x + control->rect.size.width;
92
+			if (control->rect.position.y + control->GetControlHeight() > maxY)
93
+				maxY = control->rect.position.y + control->GetControlHeight();
94
+		}
95
+	}
96
+	this->rect.size.width = (maxX - this->rect.position.x) + maxOtherWidth + 4;
97
+	this->rect.size.height = (maxY - this->rect.position.y) + maxOtherHeight + 7;
98
+}
99
+
100
+uint16_t DialogTemplate::DialogGroup::GetControlCount() const
101
+{
102
+	uint16_t count = 1;
103
+	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
104
+		count += (*curr)->GetControlCount();
105
+	return count;
106
+}
107
+
108
+std::vector<uint8_t> DialogTemplate::DialogGroup::GenerateControlTemplate() const
109
+{
110
+	auto data = std::vector<uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->groupName.length() + 1)), 0);
111
+
112
+	*reinterpret_cast<uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | BS_GROUPBOX | this->style;
113
+	*reinterpret_cast<uint32_t *>(&data[4]) = this->exstyle;
114
+	*reinterpret_cast<uint16_t *>(&data[8]) = this->rect.position.x;
115
+	*reinterpret_cast<uint16_t *>(&data[10]) = this->rect.position.y;
116
+	*reinterpret_cast<uint16_t *>(&data[12]) = this->rect.size.width;
117
+	*reinterpret_cast<uint16_t *>(&data[14]) = this->rect.size.height;
118
+	*reinterpret_cast<uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
119
+	*reinterpret_cast<uint16_t *>(&data[18]) = 0xFFFF;
120
+	*reinterpret_cast<uint16_t *>(&data[20]) = 0x0080;
121
+	memcpy(reinterpret_cast<wchar_t *>(&data[22]), this->groupName.c_str(), this->groupName.length() * sizeof(wchar_t));
122
+
123
+	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
124
+	{
125
+		auto controlData = (*curr)->GenerateControlTemplate();
126
+		data.insert(data.end(), controlData.begin(), controlData.end());
127
+	}
128
+
129
+	return data;
130
+}
131
+
132
+std::vector<uint8_t> DialogTemplate::DialogControlWithoutLabel::GenerateControlTemplate() const
133
+{
134
+	auto data = std::vector<uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t)), 0);
135
+	*reinterpret_cast<uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | this->style;
136
+	*reinterpret_cast<uint32_t *>(&data[4]) = this->exstyle;
137
+	*reinterpret_cast<uint16_t *>(&data[8]) = this->rect.position.x;
138
+	*reinterpret_cast<uint16_t *>(&data[10]) = this->rect.position.y;
139
+	*reinterpret_cast<uint16_t *>(&data[12]) = this->rect.size.width;
140
+	*reinterpret_cast<uint16_t *>(&data[14]) = this->rect.size.height;
141
+	*reinterpret_cast<uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
142
+	*reinterpret_cast<uint16_t *>(&data[18]) = 0xFFFF;
143
+	*reinterpret_cast<uint16_t *>(&data[20]) = this->type;
144
+
145
+	return data;
146
+}
147
+
148
+std::vector<uint8_t> DialogTemplate::DialogControlWithLabel::GenerateControlTemplate() const
149
+{
150
+	auto data = std::vector<uint8_t>(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->label.length() + 1)), 0);
151
+	*reinterpret_cast<uint32_t *>(&data[0]) = WS_CHILD | WS_VISIBLE | this->style;
152
+	*reinterpret_cast<uint32_t *>(&data[4]) = this->exstyle;
153
+	*reinterpret_cast<uint16_t *>(&data[8]) = this->rect.position.x;
154
+	*reinterpret_cast<uint16_t *>(&data[10]) = this->rect.position.y;
155
+	*reinterpret_cast<uint16_t *>(&data[12]) = this->rect.size.width;
156
+	*reinterpret_cast<uint16_t *>(&data[14]) = this->rect.size.height;
157
+	*reinterpret_cast<uint16_t *>(&data[16]) = static_cast<uint16_t>(this->id);
158
+	*reinterpret_cast<uint16_t *>(&data[18]) = 0xFFFF;
159
+	*reinterpret_cast<uint16_t *>(&data[20]) = this->type;
160
+	memcpy(reinterpret_cast<wchar_t *>(&data[22]), this->label.c_str(), this->label.length() * sizeof(wchar_t));
161
+
162
+	return data;
163
+}
164
+
165
+uint16_t DialogTemplate::GetTotalControlCount() const
166
+{
167
+	uint16_t count = 0;
168
+	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
169
+		count += (*curr)->GetControlCount();
170
+	return count;
171
+}
172
+
173
+void DialogTemplate::AddGroupControl(const DialogControlBuilder<DialogGroupBuilder> &builder)
174
+{
175
+	DialogGroup *newGroup = DialogGroup::CreateControl(builder);
176
+	if (builder.index == -1)
177
+		this->controls.push_back(newGroup);
178
+	else
179
+		this->controls.insert(this->controls.begin() + builder.index, newGroup);
180
+}
181
+
182
+void DialogTemplate::AddEditBoxControl(const DialogControlBuilder<DialogEditBoxBuilder> &builder)
183
+{
184
+	this->AddControlToGroup(DialogEditBox::CreateControl(builder), builder);
185
+}
186
+
187
+void DialogTemplate::AddLabelControl(const DialogControlBuilder<DialogLabelBuilder> &builder)
188
+{
189
+	this->AddControlToGroup(DialogLabel::CreateControl(builder), builder);
190
+}
191
+
192
+void DialogTemplate::AddCheckBoxControl(const DialogControlBuilder<DialogCheckBoxBuilder> &builder)
193
+{
194
+	this->AddControlToGroup(DialogButton::CreateControl(builder), builder);
195
+}
196
+
197
+void DialogTemplate::AddButtonControl(const DialogControlBuilder<DialogButtonBuilder> &builder)
198
+{
199
+	this->AddControlToGroup(DialogButton::CreateControl(builder), builder);
200
+}
201
+
202
+void DialogTemplate::AddListBoxControl(const DialogControlBuilder<DialogListBoxBuilder> &builder)
203
+{
204
+	this->AddControlToGroup(DialogListBox::CreateControl(builder), builder);
205
+}
206
+
207
+void DialogTemplate::AddComboBoxControl(const DialogControlBuilder<DialogComboBoxBuilder> &builder)
208
+{
209
+	this->AddControlToGroup(DialogComboBox::CreateControl(builder), builder);
210
+}
211
+
212
+bool DialogTemplate::CalculateControlPosition(short index, bool doRightAndBottom)
213
+{
214
+	DialogControl *control = this->controls[index];
215
+	bool valid = true;
216
+	if (control->relativePosition.get())
217
+	{
218
+		if (!doRightAndBottom && control->relativePosition->type == RelativePosition::TO_PARENT)
219
+		{
220
+			switch (control->relativePosition->positionType)
221
+			{
222
+				case RelativePosition::FROM_BOTTOM:
223
+				case RelativePosition::FROM_BOTTOMLEFT:
224
+				case RelativePosition::FROM_BOTTOMRIGHT:
225
+				case RelativePosition::FROM_RIGHT:
226
+				case RelativePosition::FROM_TOPRIGHT:
227
+					valid = false;
228
+					break;
229
+				default:
230
+					break;
231
+			}
232
+		}
233
+		if (valid)
234
+		{
235
+			Rect<short> other = Rect<short>(Point<short>(), this->size);
236
+			if (control->relativePosition->type == RelativePosition::TO_SIBLING)
237
+			{
238
+				short siblingsBack = dynamic_cast<const RelativePositionToSibling *>(control->relativePosition.get())->siblingsBack;
239
+				if (index - siblingsBack >= 0)
240
+					other = this->controls[index - siblingsBack]->rect;
241
+			}
242
+			control->rect.position = control->relativePosition->CalculatePosition(control->rect, other);
243
+		}
244
+	}
245
+	return valid;
246
+}
247
+
248
+void DialogTemplate::CalculateSize()
249
+{
250
+	short maxX = 0, maxY = 0, maxOtherWidth = 0, maxOtherHeight = 0;
251
+	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
252
+	{
253
+		DialogControl *control = *curr;
254
+
255
+		bool usePosition = true;
256
+		if (control->relativePosition.get())
257
+		{
258
+			if (control->relativePosition->type == RelativePosition::TO_PARENT)
259
+			{
260
+				switch (control->relativePosition->positionType)
261
+				{
262
+					case RelativePosition::FROM_BOTTOM:
263
+					case RelativePosition::FROM_BOTTOMLEFT:
264
+					case RelativePosition::FROM_BOTTOMRIGHT:
265
+					case RelativePosition::FROM_RIGHT:
266
+					case RelativePosition::FROM_TOPRIGHT:
267
+						usePosition = false;
268
+						/*if (control->relativePosition->relativePosition.x != -1 && control->rect.size.width + control->relativePosition->relativePosition.x > maxOtherWidth)
269
+							maxOtherWidth = control->rect.size.width + control->relativePosition->relativePosition.x;
270
+						if (control->relativePosition->relativePosition.y != -1 && control->GetControlHeight() + control->relativePosition->relativePosition.y > maxOtherHeight)
271
+							maxOtherHeight = control->GetControlHeight() + control->relativePosition->relativePosition.y;*/
272
+						break;
273
+					default:
274
+						break;
275
+				}
276
+			}
277
+		}
278
+		if (usePosition)
279
+		{
280
+			if (control->rect.position.x + control->rect.size.width > maxX)
281
+				maxX = control->rect.position.x + control->rect.size.width;
282
+			if (control->rect.position.y + control->GetControlHeight() > maxY)
283
+				maxY = control->rect.position.y + control->GetControlHeight();
284
+		}
285
+	}
286
+	this->size.width = maxX + maxOtherWidth + 7;
287
+	this->size.height = maxY + maxOtherHeight + 7;
288
+}
289
+
290
+void DialogTemplate::AutoSize()
291
+{
292
+	// Step 1: Calculate positions of dialog controls (only if the controls's position is relative to a sibling or if it's relativeness to the parent is from the top, left, or top left)
293
+	short x = 0, num = this->controls.empty() ? 0 : this->controls.size(), maxGroupWidth = 0;
294
+	for (; x < num; ++x)
295
+	{
296
+		DialogControl *control = this->controls[x];
297
+		bool valid = this->CalculateControlPosition(x, false);
298
+		if (valid && control->controlType == GROUP_CONTROL)
299
+		{
300
+			dynamic_cast<DialogGroup *>(control)->CalculatePositions(false);
301
+			// Techically step 2, but calculate the size of the group
302
+			dynamic_cast<DialogGroup *>(control)->CalculateSize();
303
+			if (control->rect.size.width > maxGroupWidth)
304
+				maxGroupWidth = control->rect.size.width;
305
+		}
306
+	}
307
+	// Step 2: Resize all group controls to be the same width, and then within the group, recalculate positions for all controls
308
+	for (x = 0; x < num; ++x)
309
+	{
310
+		DialogControl *control = this->controls[x];
311
+		if (control->controlType != GROUP_CONTROL)
312
+			continue;
313
+		control->rect.size.width = maxGroupWidth;
314
+		dynamic_cast<DialogGroup *>(control)->CalculatePositions(true);
315
+	}
316
+	// Step 3: Resize the dialog box itself
317
+	this->CalculateSize();
318
+	// Step 4: Calculate position of all dialog controls
319
+	for (x = 0; x < num; ++x)
320
+		this->CalculateControlPosition(x, true);
321
+}
322
+
323
+const DLGTEMPLATE *DialogTemplate::GenerateTemplate()
324
+{
325
+	this->templateData.clear();
326
+	uint16_t controlCount = this->GetTotalControlCount();
327
+	this->templateData.resize(getNextMultipleOf4(24 + sizeof(wchar_t) * (this->title.length() + 1 + (this->fontName.empty() ? 0 : this->fontName.length() + 1))), 0);
328
+	*reinterpret_cast<uint32_t *>(&this->templateData[0]) = this->style | (this->fontName.empty() ? 0 : DS_SETFONT);
329
+	*reinterpret_cast<uint32_t *>(&this->templateData[4]) = this->exstyle;
330
+	*reinterpret_cast<uint16_t *>(&this->templateData[8]) = controlCount;
331
+	*reinterpret_cast<uint16_t *>(&this->templateData[14]) = this->size.width;
332
+	*reinterpret_cast<uint16_t *>(&this->templateData[16]) = this->size.height;
333
+	memcpy(reinterpret_cast<wchar_t *>(&this->templateData[22]), this->title.c_str(), this->title.length() * sizeof(wchar_t));
334
+	if (!this->fontName.empty())
335
+	{
336
+		*reinterpret_cast<uint16_t *>(&this->templateData[22 + sizeof(wchar_t) * (this->title.length() + 1)]) = this->fontSizeInPts;
337
+		memcpy(reinterpret_cast<wchar_t *>(&this->templateData[24 + sizeof(wchar_t) * (this->title.length() + 1)]), this->fontName.c_str(), this->fontName.length() * sizeof(wchar_t));
338
+	}
339
+
340
+	for (auto curr = this->controls.begin(), end = this->controls.end(); curr != end; ++curr)
341
+	{
342
+		auto controlData = (*curr)->GenerateControlTemplate();
343
+		this->templateData.insert(this->templateData.end(), controlData.begin(), controlData.end());
344
+	}
345
+
346
+	return reinterpret_cast<const DLGTEMPLATE *>(&this->templateData[0]);
347
+}