Debian Codegraph @

Source/DialogTemplate.cpp

raw
1 /*
2 * DialogTemplate.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 2002-2026 Amir Szekely <[email protected]> and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 */
16
17 #include "DialogTemplate.h"
18 #include "winchar.h"
19 #include <cassert> // for assert(3)
20 #ifndef _WIN32
21 # include "util.h" // for Unicode conversion functions
22 # include <stdio.h>
23 # include <stdlib.h>
24 # include <iconv.h>
25 # include <errno.h>
26 #endif
27
28 using namespace std;
29
30 //////////////////////////////////////////////////////////////////////
31 // Utilities
32 //////////////////////////////////////////////////////////////////////
33
34 static inline DWORD ConvertEndianness(DWORD d) {
35 return FIX_ENDIAN_INT32(d);
36 }
37
38 static inline WORD ConvertEndianness(WORD w) {
39 return FIX_ENDIAN_INT16(w);
40 }
41
42 static inline short ConvertEndianness(short s) {
43 return ConvertEndianness(WORD(s));
44 }
45
46 #define ALIGN(dwToAlign, dwAlignOn) dwToAlign = (dwToAlign%dwAlignOn == 0) ? dwToAlign : dwToAlign - (dwToAlign%dwAlignOn) + dwAlignOn
47
48 // Reads a variant length array from seeker into readInto and advances seeker
49 void ReadVarLenArr(LPBYTE &seeker, WINWCHAR* &readInto, unsigned int uCodePage) {
50 WORD* arr = (WORD*)seeker;
51 switch (ConvertEndianness(arr[0])) {
52 case 0x0000:
53 readInto = 0;
54 seeker += sizeof(WORD);
55 break;
56 case 0xFFFF:
57 readInto = MAKEINTRESOURCEWINW(ConvertEndianness(arr[1]));
58 seeker += 2*sizeof(WORD);
59 break;
60 default:
61 {
62 readInto = WinWStrDupFromWinWStr((WINWCHAR*) arr);
63 WINWCHAR *wseeker = (WINWCHAR*) seeker;
64 while (*wseeker++);
65 seeker = LPBYTE(wseeker);
66 }
67 break;
68 }
69 }
70
71 // A macro that writes a given string (that can be a number too) into the buffer
72 #define WriteStringOrId(x) \
73 if (x) \
74 if (IS_INTRESOURCE(x)) { \
75 *(WORD*)seeker = 0xFFFF; \
76 seeker += sizeof(WORD); \
77 *(WORD*)seeker = ConvertEndianness(WORD((ULONG_PTR)(x))); \
78 seeker += sizeof(WORD); \
79 } \
80 else { \
81 WinWStrCpy((WINWCHAR *) seeker, x); \
82 seeker += WinWStrLen((WINWCHAR *) seeker) * sizeof(WINWCHAR) + sizeof(WINWCHAR); \
83 } \
84 else \
85 seeker += sizeof(WORD);
86
87 // A macro that adds the size of x (which can be a string a number, or nothing) to dwSize
88 #define AddStringOrIdSize(x) dwSize += x ? (IS_INTRESOURCE(x) ? sizeof(DWORD) : (DWORD)((WinWStrLen(x) + 1) * sizeof(WINWCHAR))) : sizeof(WORD)
89
90 //////////////////////////////////////////////////////////////////////
91 // Construction/Destruction
92 //////////////////////////////////////////////////////////////////////
93
94 CDialogTemplate::CDialogTemplate(BYTE* pbData, bool build_unicode, unsigned int uCodePage) {
95 m_uCodePage = uCodePage;
96 m_build_unicode = build_unicode;
97
98 m_dwHelpId = 0;
99 m_szClass = 0;
100 m_szFont = 0;
101 m_sFontSize = 0;
102 m_sFontWeight = 0;
103 m_bItalic = 0;
104 m_bCharset = 0;
105 m_szMenu = 0;
106 m_szTitle = 0;
107
108 WORD wItems = 0;
109
110 if (*(DWORD*)pbData == EXTENDED_DIALOG) { // Extended dialog template signature
111 m_bExtended = true;
112
113 DLGTEMPLATEEX* dTemplateEx = (DLGTEMPLATEEX*)pbData;
114
115 m_dwHelpId = ConvertEndianness(dTemplateEx->helpID);
116 m_dwStyle = ConvertEndianness(dTemplateEx->style);
117 m_dwExtStyle = ConvertEndianness(dTemplateEx->exStyle);
118 m_sX = ConvertEndianness(dTemplateEx->x);
119 m_sY = ConvertEndianness(dTemplateEx->y);
120 m_sWidth = ConvertEndianness(dTemplateEx->cx);
121 m_sHeight = ConvertEndianness(dTemplateEx->cy);
122
123 wItems = ConvertEndianness(dTemplateEx->cDlgItems);
124 }
125 else {
126 m_bExtended = false;
127
128 DLGTEMPLATE* dTemplate = (DLGTEMPLATE*)pbData;
129
130 m_dwStyle = ConvertEndianness(dTemplate->style);
131 m_dwExtStyle = ConvertEndianness(dTemplate->dwExtendedStyle);
132 m_sX = ConvertEndianness(dTemplate->x);
133 m_sY = ConvertEndianness(dTemplate->y);
134 m_sWidth = ConvertEndianness(dTemplate->cx);
135 m_sHeight = ConvertEndianness(dTemplate->cy);
136
137 wItems = ConvertEndianness(dTemplate->cdit);
138 }
139
140 BYTE* seeker = pbData + (m_bExtended ? sizeof(DLGTEMPLATEEX) : sizeof(DLGTEMPLATE));
141
142 // Read menu variant length array
143 ReadVarLenArr(seeker, m_szMenu, m_uCodePage);
144 // Read class variant length array
145 ReadVarLenArr(seeker, m_szClass, m_uCodePage);
146 // Read title variant length array
147 ReadVarLenArr(seeker, m_szTitle, m_uCodePage);
148 // Read font size and variant length array (only if style DS_SETFONT is used!)
149 if (m_dwStyle & DS_SETFONT) {
150 m_sFontSize = ConvertEndianness(*(short*)seeker);
151 seeker += sizeof(short);
152 if (m_bExtended) {
153 m_sFontWeight = ConvertEndianness(*(short*)seeker);
154 seeker += sizeof(short);
155 m_bItalic = *(BYTE*)seeker;
156 seeker += sizeof(BYTE);
157 m_bCharset = *(BYTE*)seeker;
158 seeker += sizeof(BYTE);
159 }
160 ReadVarLenArr(seeker, m_szFont, m_uCodePage);
161 }
162
163 // Read items
164 for (int i = 0; i < wItems; i++) {
165 // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundary
166 if (DWORD(seeker - pbData) % sizeof(DWORD))
167 seeker += sizeof(WORD);
168
169 DialogItemTemplate* item = new DialogItemTemplate;
170 ZeroMemory(item, sizeof(DialogItemTemplate));
171
172 if (m_bExtended) {
173 DLGITEMTEMPLATEEX* rawItem = (DLGITEMTEMPLATEEX*)seeker;
174
175 item->dwHelpId = ConvertEndianness(rawItem->helpID);
176 item->dwStyle = ConvertEndianness(rawItem->style);
177 item->dwExtStyle = ConvertEndianness(rawItem->exStyle);
178 item->sX = ConvertEndianness(rawItem->x);
179 item->sY = ConvertEndianness(rawItem->y);
180 item->sWidth = ConvertEndianness(rawItem->cx);
181 item->sHeight = ConvertEndianness(rawItem->cy);
182 item->wId = ConvertEndianness(rawItem->id);
183
184 seeker += sizeof(DLGITEMTEMPLATEEX);
185 }
186 else {
187 DLGITEMTEMPLATE* rawItem = (DLGITEMTEMPLATE*)seeker;
188
189 item->dwHelpId = 0;
190 item->dwStyle = ConvertEndianness(rawItem->style);
191 item->dwExtStyle = ConvertEndianness(rawItem->dwExtendedStyle);
192 item->sX = ConvertEndianness(rawItem->x);
193 item->sY = ConvertEndianness(rawItem->y);
194 item->sWidth = ConvertEndianness(rawItem->cx);
195 item->sHeight = ConvertEndianness(rawItem->cy);
196 item->wId = ConvertEndianness(rawItem->id);
197
198 seeker += sizeof(DLGITEMTEMPLATE);
199 }
200
201 // Read class variant length array
202 ReadVarLenArr(seeker, item->szClass, m_uCodePage);
203 // Read title variant length array
204 ReadVarLenArr(seeker, item->szTitle, m_uCodePage);
205
206 // Read creation data variant length array
207 // First read the size of the array (no null termination)
208 item->wCreateDataSize = ConvertEndianness(*(WORD*)seeker);
209 seeker += sizeof(WORD);
210 // Then read the array it self (if size is not 0)
211 if (item->wCreateDataSize) {
212 item->wCreateDataSize -= sizeof(WORD); // Size includes size field itself...
213 item->szCreationData = new char[item->wCreateDataSize];
214 CopyMemory(item->szCreationData, seeker, item->wCreateDataSize);
215 seeker += item->wCreateDataSize;
216 }
217
218 // Add the item to the vector
219 m_vItems.push_back(item);
220 }
221 }
222
223 static void free_template_string(WINWCHAR*s)
224 {
225 if (!IS_INTRESOURCE(s)) free(s);
226 }
227
228 CDialogTemplate::~CDialogTemplate() {
229 free_template_string(m_szMenu);
230 free_template_string(m_szClass);
231 free(m_szTitle);
232 free(m_szFont);
233
234 for (unsigned int i = 0; i < m_vItems.size(); i++) {
235 free_template_string(m_vItems[i]->szClass);
236 free_template_string(m_vItems[i]->szTitle);
237 if (m_vItems[i]->szCreationData) delete [] m_vItems[i]->szCreationData;
238 delete m_vItems[i];
239 }
240 }
241
242 //////////////////////////////////////////////////////////////////////
243 // Methods
244 //////////////////////////////////////////////////////////////////////
245
246 // Returns the width of the dialog
247 short CDialogTemplate::GetWidth() {
248 return m_sWidth;
249 }
250
251 // Returns the height of the dialog
252 short CDialogTemplate::GetHeight() {
253 return m_sHeight;
254 }
255
256 // Returns info about the item with the id wId
257 DialogItemTemplate* CDialogTemplate::GetItem(WORD wId) {
258 for (unsigned int i = 0; i < m_vItems.size(); i++)
259 if (m_vItems[i]->wId == wId)
260 return m_vItems[i];
261 return 0;
262 }
263
264 // Returns info about the item with the indexed i
265 DialogItemTemplate* CDialogTemplate::GetItemByIdx(DWORD i) {
266 if (i >= m_vItems.size()) return 0;
267 return m_vItems[i];
268 }
269
270 // Removes an item
271 // Returns 1 if removed, 0 otherwise
272 int CDialogTemplate::RemoveItem(WORD wId) {
273 for (unsigned int i = 0; i < m_vItems.size(); i++) {
274 if (m_vItems[i]->wId == wId) {
275 m_vItems.erase(m_vItems.begin() + i);
276 return 1;
277 }
278 }
279 return 0;
280 }
281
282 // Sets the font of the dialog
283 void CDialogTemplate::SetFont(TCHAR* szFaceName, WORD wFontSize) {
284 m_dwStyle &= ~DS_SHELLFONT;
285 if (!_tcscmp(szFaceName, _T("MS Shell Dlg"))) // TODO: "MS Shell Dlg 2"?
286 m_dwStyle |= DS_SHELLFONT;
287 m_dwStyle |= DS_SETFONT;
288 m_bCharset = DEFAULT_CHARSET;
289 free(m_szFont);
290 m_szFont = WinWStrDupFromTChar(szFaceName, m_uCodePage);
291 m_sFontSize = wFontSize;
292 }
293
294 // Adds an item to the dialog
295 void CDialogTemplate::AddItem(DialogItemTemplate item) {
296 DialogItemTemplate* newItem = new DialogItemTemplate;
297 CopyMemory(newItem, &item, sizeof(DialogItemTemplate));
298
299 if (item.szClass && !IS_INTRESOURCE(item.szClass)) {
300 newItem->szClass = WinWStrDupFromWinWStr(item.szClass);
301 }
302 if (item.szTitle && !IS_INTRESOURCE(item.szTitle)) {
303 newItem->szTitle = WinWStrDupFromWinWStr(item.szTitle);
304 }
305 if (item.wCreateDataSize) {
306 newItem->szCreationData = new char[item.wCreateDataSize];
307 memcpy(newItem->szCreationData, item.szCreationData, item.wCreateDataSize);
308 }
309 m_vItems.push_back(newItem);
310 }
311
312 // Moves all of the items in the dialog by (x,y)
313 void CDialogTemplate::MoveAll(short x, short y) {
314 for (unsigned int i = 0; i < m_vItems.size(); i++) {
315 m_vItems[i]->sX += x;
316 m_vItems[i]->sY += y;
317 }
318 }
319
320 // Resizes the dialog by (x,y)
321 void CDialogTemplate::Resize(short x, short y) {
322 m_sWidth += x;
323 m_sHeight += y;
324 }
325
326 #ifdef _WIN32
327 // Creates a dummy dialog that is used for converting units
328 HWND CDialogTemplate::CreateDummyDialog() {
329 DWORD dwTemp;
330 BYTE* pbDlg = Save(dwTemp);
331 HWND hDlg = CreateDialogIndirect(GetModuleHandle(0), (DLGTEMPLATE*)pbDlg, 0, 0);
332 FreeSavedTemplate(pbDlg);
333 if (!hDlg)
334 throw runtime_error("Can't create dialog from template!");
335
336 return hDlg;
337 }
338
339 // Converts pixels to this dialog's units
340 void CDialogTemplate::PixelsToDlgUnits(short& x, short& y) {
341 HWND hDlg = CreateDummyDialog();
342 RECT r = {0, 0, 10000, 10000};
343 MapDialogRect(hDlg, &r);
344 DestroyWindow(hDlg);
345
346 x = short(float(x) / (float(r.right)/10000));
347 y = short(float(y) / (float(r.bottom)/10000));
348 }
349
350 void CDialogTemplate::PixelsToDlgUnits(SIZE& siz) {
351 short x = (short)siz.cx, y = (short)siz.cy;
352 PixelsToDlgUnits(x, y);
353 siz.cx = x, siz.cy = y;
354 }
355
356 // Converts pixels to this dialog's units
357 void CDialogTemplate::DlgUnitsToPixels(short& x, short& y) {
358 HWND hDlg = CreateDummyDialog();
359 RECT r = {0, 0, 10000, 10000};
360 MapDialogRect(hDlg, &r);
361 DestroyWindow(hDlg);
362
363 x = short(float(x) * (float(r.right)/10000));
364 y = short(float(y) * (float(r.bottom)/10000));
365 }
366
367 // Returns the size of a string in the dialog (in dialog units)
368 SIZE CDialogTemplate::GetStringSize(WORD id, TCHAR *str) {
369 HWND hDlg = CreateDummyDialog();
370
371 LOGFONT f;
372 GetObject((HFONT)SendMessage(hDlg, WM_GETFONT, 0, 0), sizeof(LOGFONT), &f);
373
374 HDC memDC = CreateCompatibleDC(GetDC(hDlg));
375 HFONT font = CreateFontIndirect(&f);
376 SelectObject(memDC, font);
377
378 SIZE size;
379 GetTextExtentPoint32(memDC, str, (int) _tcslen(str), &size);
380
381 DestroyWindow(hDlg);
382 DeleteObject(font);
383 DeleteDC(memDC);
384
385 PixelsToDlgUnits(size);
386
387 return size;
388 }
389
390 // Trims the right margins of a control to fit a given text string size.
391 void CDialogTemplate::RTrimToString(WORD id, TCHAR *str, int margins) {
392 DialogItemTemplate* item = GetItem(id);
393 if (!item) return;
394
395 SIZE size = GetStringSize(id, str);
396
397 size.cx += margins;
398 size.cy += 2;
399
400 item->sWidth = short(size.cx);
401 item->sHeight = short(size.cy);
402 }
403
404 // Trims the left margins of a control to fit a given text string size.
405 void CDialogTemplate::LTrimToString(WORD id, TCHAR *str, int margins) {
406 DialogItemTemplate* item = GetItem(id);
407 if (!item) return;
408
409 SIZE size = GetStringSize(id, str);
410
411 size.cx += margins;
412 size.cy += 2;
413
414 item->sX += item->sWidth - short(size.cx);
415 item->sWidth = short(size.cx);
416 item->sHeight = short(size.cy);
417 }
418
419 // Trims the left and right margins of a control to fit a given text string size.
420 void CDialogTemplate::CTrimToString(WORD id, TCHAR *str, int margins) {
421 DialogItemTemplate* item = GetItem(id);
422 if (!item) return;
423
424 SIZE size = GetStringSize(id, str);
425
426 size.cx += margins;
427 size.cy += 2;
428
429 item->sX += item->sWidth/2 - short(size.cx/2);
430 item->sWidth = short(size.cx);
431 item->sHeight = short(size.cy);
432 }
433 #else //! WIN32
434 void CDialogTemplate::PixelsToDlgUnits(short& x, short& y) { assert(0); }
435 void CDialogTemplate::DlgUnitsToPixels(short& x, short& y) { assert(0); }
436 #endif //~ WIN32
437
438 // Moves every item right and gives it the WS_EX_RIGHT extended style
439 void CDialogTemplate::ConvertToRTL() {
440 for (unsigned int i = 0; i < m_vItems.size(); i++) {
441 bool addExStyle = false;
442 bool addExLeftScrollbar = true;
443
444 // Button
445 if ((ULONG_PTR)(m_vItems[i]->szClass) == 0x80) {
446 m_vItems[i]->dwStyle ^= BS_LEFTTEXT;
447 m_vItems[i]->dwStyle ^= BS_RIGHT;
448 m_vItems[i]->dwStyle ^= BS_LEFT;
449
450 if ((m_vItems[i]->dwStyle & (BS_LEFT|BS_RIGHT)) == (BS_LEFT|BS_RIGHT)) {
451 m_vItems[i]->dwStyle ^= BS_LEFT;
452 m_vItems[i]->dwStyle ^= BS_RIGHT;
453 if (m_vItems[i]->dwStyle & (BS_RADIOBUTTON|BS_CHECKBOX|BS_USERBUTTON)) {
454 m_vItems[i]->dwStyle |= BS_RIGHT;
455 }
456 }
457 }
458 // Edit
459 else if ((ULONG_PTR)(m_vItems[i]->szClass) == 0x81) {
460 if ((m_vItems[i]->dwStyle & ES_CENTER) == 0) {
461 m_vItems[i]->dwStyle ^= ES_RIGHT;
462 }
463 }
464 // Static
465 else if ((ULONG_PTR)(m_vItems[i]->szClass) == 0x82) {
466 if ((m_vItems[i]->dwStyle & SS_TYPEMASK) == SS_LEFT || (m_vItems[i]->dwStyle & SS_TYPEMASK) == SS_LEFTNOWORDWRAP)
467 {
468 m_vItems[i]->dwStyle &= ~SS_TYPEMASK;
469 m_vItems[i]->dwStyle |= SS_RIGHT;
470 }
471 else if ((m_vItems[i]->dwStyle & SS_TYPEMASK) == SS_ICON) {
472 m_vItems[i]->dwStyle |= SS_CENTERIMAGE;
473 }
474 }
475 else if (!IS_INTRESOURCE(m_vItems[i]->szClass) && !WinWStrNICmpASCII(m_vItems[i]->szClass, "RichEdit20", 10)) {
476 if ((m_vItems[i]->dwStyle & ES_CENTER) == 0) {
477 m_vItems[i]->dwStyle ^= ES_RIGHT;
478 }
479 }
480 else if (!IS_INTRESOURCE(m_vItems[i]->szClass) && !WinWStrICmpASCII(m_vItems[i]->szClass, "SysTreeView32")) {
481 // we can't have both TVS_RTLREADING and WS_EX_LAYOUTRTL because the text becomes non-LTR
482 // we chose just WS_EX_LAYOUTRTL because just TVS_RTLREADING changes the text only and not the tree itself
483 // https://github.com/qbittorrent/qBittorrent/pull/19929
484 //m_vItems[i]->dwStyle |= TVS_RTLREADING;
485 m_vItems[i]->dwExtStyle |= WS_EX_LAYOUTRTL;
486 addExStyle = true;
487 addExLeftScrollbar = false;
488 }
489 else if (!IS_INTRESOURCE(m_vItems[i]->szClass) && !WinWStrICmpASCII(m_vItems[i]->szClass, "SysListView32")) {
490 m_vItems[i]->dwExtStyle |= WS_EX_LAYOUTRTL;
491 addExLeftScrollbar = false;
492 }
493 else addExStyle = true;
494
495 if (addExStyle)
496 m_vItems[i]->dwExtStyle |= WS_EX_RIGHT;
497 if (addExLeftScrollbar)
498 m_vItems[i]->dwExtStyle |= WS_EX_LEFTSCROLLBAR;
499
500 m_vItems[i]->dwExtStyle |= WS_EX_RTLREADING;
501
502 m_vItems[i]->sX = m_sWidth - m_vItems[i]->sWidth - m_vItems[i]->sX;
503 }
504 m_dwExtStyle |= WS_EX_RIGHT | WS_EX_RTLREADING | WS_EX_LEFTSCROLLBAR;
505 }
506
507 // Saves the dialog in the form of DLGTEMPLATE[EX]
508 BYTE* CDialogTemplate::Save(DWORD& dwSize) {
509 // We need the size first to know how much memory to allocate
510 dwSize = GetSize();
511 BYTE* pbDlg = new BYTE[dwSize];
512 ZeroMemory(pbDlg, dwSize);
513 BYTE* seeker = pbDlg;
514
515 if (m_bExtended) {
516 DLGTEMPLATEEX dh = {
517 ConvertEndianness(WORD(0x0001)),
518 ConvertEndianness(WORD(0xFFFF)),
519 ConvertEndianness(m_dwHelpId),
520 ConvertEndianness(m_dwExtStyle),
521 ConvertEndianness(m_dwStyle),
522 ConvertEndianness(WORD(m_vItems.size())),
523 ConvertEndianness(m_sX),
524 ConvertEndianness(m_sY),
525 ConvertEndianness(m_sWidth),
526 ConvertEndianness(m_sHeight)
527 };
528
529 CopyMemory(seeker, &dh, sizeof(DLGTEMPLATEEX));
530 seeker += sizeof(DLGTEMPLATEEX);
531 }
532 else {
533 DLGTEMPLATE dh = {
534 ConvertEndianness(m_dwStyle),
535 ConvertEndianness(m_dwExtStyle),
536 ConvertEndianness(WORD(m_vItems.size())),
537 ConvertEndianness(m_sX),
538 ConvertEndianness(m_sY),
539 ConvertEndianness(m_sWidth),
540 ConvertEndianness(m_sHeight)
541 };
542
543 CopyMemory(seeker, &dh, sizeof(DLGTEMPLATE));
544 seeker += sizeof(DLGTEMPLATE);
545 }
546
547 // Write menu variant length array
548 WriteStringOrId(m_szMenu);
549 // Write class variant length array
550 WriteStringOrId(m_szClass);
551 // Write title variant length array
552 WriteStringOrId(m_szTitle);
553
554 // Write font variant length array, size, and extended info (if needed)
555 if (m_dwStyle & DS_SETFONT) {
556 *(short*)seeker = ConvertEndianness(m_sFontSize);
557 seeker += sizeof(short);
558 if (m_bExtended) {
559 *(short*)seeker = ConvertEndianness(m_sFontWeight);
560 seeker += sizeof(short);
561 *(BYTE*)seeker = m_bItalic;
562 seeker += sizeof(BYTE);
563 *(BYTE*)seeker = m_bCharset;
564 seeker += sizeof(BYTE);
565 }
566 WriteStringOrId(m_szFont);
567 }
568
569 // Write all of the items
570 for (unsigned int i = 0; i < m_vItems.size(); i++) {
571 // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundary
572 if ((seeker - pbDlg) % sizeof(DWORD))
573 seeker += sizeof(WORD);
574
575 if (m_bExtended) {
576 DLGITEMTEMPLATEEX dih = {
577 ConvertEndianness(m_vItems[i]->dwHelpId),
578 ConvertEndianness(m_vItems[i]->dwExtStyle),
579 ConvertEndianness(m_vItems[i]->dwStyle),
580 ConvertEndianness(m_vItems[i]->sX),
581 ConvertEndianness(m_vItems[i]->sY),
582 ConvertEndianness(m_vItems[i]->sWidth),
583 ConvertEndianness(m_vItems[i]->sHeight),
584 ConvertEndianness(m_vItems[i]->wId)
585 };
586
587 CopyMemory(seeker, &dih, sizeof(DLGITEMTEMPLATEEX));
588 seeker += sizeof(DLGITEMTEMPLATEEX);
589 }
590 else {
591 DLGITEMTEMPLATE dih = {
592 ConvertEndianness(m_vItems[i]->dwStyle),
593 ConvertEndianness(m_vItems[i]->dwExtStyle),
594 ConvertEndianness(m_vItems[i]->sX),
595 ConvertEndianness(m_vItems[i]->sY),
596 ConvertEndianness(m_vItems[i]->sWidth),
597 ConvertEndianness(m_vItems[i]->sHeight),
598 ConvertEndianness(m_vItems[i]->wId)
599 };
600
601 CopyMemory(seeker, &dih, sizeof(DLGITEMTEMPLATE));
602 seeker += sizeof(DLGITEMTEMPLATE);
603 }
604
605 // Write class variant length array
606 const WINWCHAR *szClass = m_vItems[i]->szClass;
607 #ifdef _UNICODE
608 static const WINWCHAR clsRE20W[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
609 static const WINWCHAR clsRE20A[] = {'R','i','c','h','E','d','i','t','2','0','A',0};
610 if (!IS_INTRESOURCE(szClass)) {
611 // transmute RichEdit20A/W control into RichEdit20T that matches the target
612 if (m_build_unicode && !WinWStrICmpASCII(szClass, "RichEdit20A")) szClass = clsRE20W;
613 if (!m_build_unicode && !WinWStrICmpASCII(szClass, "RichEdit20W")) szClass = clsRE20A;
614 }
615 #endif
616 WriteStringOrId(szClass);
617 // Write title variant length array
618 WriteStringOrId(m_vItems[i]->szTitle);
619
620 // Write creation data variant length array
621 // First write its size
622 WORD wCreateDataSize = m_vItems[i]->wCreateDataSize;
623 if (m_vItems[i]->wCreateDataSize) wCreateDataSize += sizeof(WORD);
624 *(WORD*)seeker = ConvertEndianness(wCreateDataSize);
625 seeker += sizeof(WORD);
626 // If size is nonzero write the data too
627 if (m_vItems[i]->wCreateDataSize) {
628 CopyMemory(seeker, m_vItems[i]->szCreationData, m_vItems[i]->wCreateDataSize);
629 seeker += m_vItems[i]->wCreateDataSize;
630 }
631 }
632
633 assert((DWORD)(seeker - pbDlg) == dwSize);
634
635 // DONE!
636 return pbDlg;
637 }
638
639 // Returns the size that the DLGTEMPLATE[EX] will take when saved
640 DWORD CDialogTemplate::GetSize() {
641 DWORD dwSize = m_bExtended ? sizeof(DLGTEMPLATEEX) : sizeof(DLGTEMPLATE);
642
643 // Menu
644 AddStringOrIdSize(m_szMenu);
645 // Class
646 AddStringOrIdSize(m_szClass);
647 // Title
648 AddStringOrIdSize(m_szTitle);
649
650 // Font
651 if (m_dwStyle & DS_SETFONT) {
652 dwSize += sizeof(WORD) + (m_bExtended ? sizeof(short) + 2*sizeof(BYTE) : 0);
653 AddStringOrIdSize(m_szFont);
654 }
655
656 for (unsigned int i = 0; i < m_vItems.size(); i++) {
657 // DLGITEMTEMPLATE[EX]s must be aligned on DWORD boundary
658 ALIGN(dwSize, sizeof(DWORD));
659
660 dwSize += m_bExtended ? sizeof(DLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE);
661
662 // Class
663 AddStringOrIdSize(m_vItems[i]->szClass);
664 // Title
665 AddStringOrIdSize(m_vItems[i]->szTitle);
666
667 dwSize += sizeof(WORD) + m_vItems[i]->wCreateDataSize;
668 }
669
670 return dwSize;
671 }