uLib  User mode C/C++ extended API library for Win32 programmers.
DlgPlate.cpp
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // Project: uLib - User mode library.
3 // Module: Dialog "memory templates" support.
4 // Author: Copyright (c) Love Nystrom
5 // License: NNOSL (BSD descendant, see NNOSL.txt in the base directory).
6 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 
8 #include <uLib/DlgPlate.h>
9 #include <uLib/StrFunc.h>
10 //#include <uLib/Debug.h>
11 
12 // Dialog templates need careful alignment...
14 PVOID AlignPtr( PVOID Ptr, WORD Chunk ) // Chunk = 2,4,8,16... (power of 2)
15 {
16  --Chunk; return PVOID( DWORD_PTR( PBYTE(Ptr)+Chunk ) & ~DWORD_PTR( Chunk ));
17 }
18 PBYTE dwAlign( PBYTE Ptr )
19 {
20  return (PBYTE) (DWORD_PTR(Ptr+3) & ~DWORD_PTR(3));
21 }
22 PBYTE wAlign( PBYTE Ptr )
23 {
24  return (PBYTE)(DWORD_PTR(Ptr+1) & ~DWORD_PTR(1));
25 }
26 DWORD_PTR dwPad( DWORD_PTR X )
27 {
28  return (DWORD_PTR(X+3) & ~DWORD_PTR(3));
29 }
31 
32 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 // Classic dialog templates
34 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35 
36 static const LPCWSTR S_ShellFont = L"MS Shell Dlg"; // Mandatory for DS_SHELLFONT
37 
38 LPDLGTEMPLATE AllocDlgTemplate( WORD Size )
39 {
40  // GlobalAlloc allows Windows to unlock and move the template if need be.
41  HGLOBAL hMem = GlobalAlloc( GHND, Size );
42  return hMem ? (LPDLGTEMPLATE) GlobalLock( hMem ) : NULL;
43 }
44 void FreeDlgTemplate( LPDLGTEMPLATE pDlg )
45 {
46  GlobalFree( (HGLOBAL) pDlg );
47 }
48 
49 typedef union _ditUniPtr { // Local type
50  LPDLGITEMTEMPLATE pItem;
54  PWSTR pwStr;
55  } DitUniPtr;
56 
57 LPDLGITEMTEMPLATE SetDlgStyle(
58  LPDLGTEMPLATE pDlg,
59  DWORD Style,
60  short X, short Y,
61  short W, short H,
62  LPCWSTR Caption,
63  WORD nCtls,
64  LPCWSTR Font,
65  WORD Points
66  )
67 {
68  if (DWORD_PTR( pDlg ) & DWORD_PTR( 3 )) { // Check buffer alignment
69  SetLastError( ERROR_INVALID_PARAMETER ); // Not DW aligned
70  return NULL;
71  }
72  pDlg->style = Style;
73  pDlg->dwExtendedStyle = 0;
74  pDlg->cdit = nCtls;
75  pDlg->x = X; // Dialog units..
76  pDlg->y = Y;
77  pDlg->cx = W;
78  pDlg->cy = H;
79 
80  DitUniPtr u;
81  u.pByte = PBYTE( pDlg+1 ); // Past the header. NOTE: C pointer arithmetic!
82  *u.pWord++ = 0x0000; // No menu
83  *u.pWord++ = 0x0000; // Std system dialog class
84  u.pwStr = (1 + wcsecpy( u.pwStr, Caption ));
85  if (Style & DS_SETFONT)
86  {
87  if HAVE_BITS( Style, DS_SHELLFONT ) Font = S_ShellFont; // Mandatory, so override arg.
88  *u.pWord++ = Points; // points
89  u.pwStr = (1 + wcsecpy( u.pwStr, Font ));
90  }
91  return u.pItem;
92 }
93 
94 LPDLGITEMTEMPLATE AddDlgItem(
95  LPDLGITEMTEMPLATE pBuf,
96  DWORD Type,
97  DWORD Style,
98  DWORD ExStyle,
99  short X, short Y,
100  short W, short H,
101  LPCWSTR Caption,
102  WORD Id
103  )
104 {
105  DitUniPtr u;
106  u.pByte = dwAlign( (PBYTE) pBuf ); // align
107 
108  u.pItem->style = Style;
109  u.pItem->dwExtendedStyle = ExStyle; //WS_EX_STATICEDGE;//WS_EX_WINDOWEDGE
110  u.pItem->x = X;
111  u.pItem->y = Y;
112  u.pItem->cx = W;
113  u.pItem->cy = H;
114  u.pItem->id = Id;
115 
116  u.pItem++; // Past item header
117 
118  *u.pDword++ = Type; // E.g. ORD_STATIC;
119  u.pwStr = (1 + wcsecpy( u.pwStr, Caption ));
120  *u.pWord++ = 0; // No creation data
121  return u.pItem;
122 }
123 
124 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125 // Extended dialog templates
126 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127 
129 {
130  HGLOBAL hMem = GlobalAlloc( GHND, Size );
131  return hMem ? (PDLGTEMPLATEEX) GlobalLock( hMem ) : NULL;
132 }
134 {
135  GlobalFree( (HGLOBAL) pDlg );
136 }
137 
138 typedef union _ditxUniPtr { // Local type
143  PWSTR pwStr;
144  } DitXUniPtr;
145 
147  PDLGTEMPLATEEX pDlg,
148  DWORD Style,
149  short X, short Y,
150  short W, short H,
151  LPCWSTR Caption,
152  DWORD HelpId,
153  WORD nCtls,
154  LPCWSTR Font,
155  short Points,
156  short Weight,
157  BYTE CharSet
158  )
159 {
160  if (DWORD_PTR( pDlg ) & DWORD_PTR( 3 )) { // Check buffer alignment
161  SetLastError( ERROR_INVALID_PARAMETER ); // Not DW aligned
162  return NULL;
163  }
164  pDlg->dlgVer = 1;
165  pDlg->signature = 0xffff;
166  pDlg->helpId = HelpId;
167  pDlg->exStyle = 0; // Currently not used for dialogs
168  pDlg->style = Style;
169  pDlg->cDlgItems = nCtls;
170  pDlg->x = X; // Dialog units..
171  pDlg->y = Y;
172  pDlg->cx = W;
173  pDlg->cy = H;
174 
175  // Everything else in this structure is variable length,
176  // and therefore must be determined dynamically
177 
178  DitXUniPtr u;
179  u.pByte = PBYTE( pDlg+1 ); // Past the header
180 
181  // wsz_Or_Ord menu; // Name or ordinal of a menu resource. 0x0000 = No menu
182  // wsz_Or_Ord windowClass; // Name or ordinal of a window class. 0x0000 = Std dialog class
183  // WCHAR title[titleLen]; // Title string of the dialog box
184  // short pointsize; // Only if DS_SETFONT is set
185  // short weight; // -"-
186  // uchar bItalic; // -"-
187  // uchar charSet; // -"-
188  // WCHAR font[fontLen]; // Typeface name, if DS_SETFONT is set
189 
190  *u.pWord++ = 0x0000; // No menu
191  *u.pWord++ = 0x0000; // Std system dialog class
192  u.pwStr = (1 + wcsecpy( u.pwStr, Caption ));
193 
194  if (Style & DS_SETFONT)
195  {
196  if HAVE_BITS( Style, DS_SHELLFONT ) Font = S_ShellFont; // Mandatory, so override arg.
197  *u.pWord++ = Points; // points
198  *u.pWord++ = Weight; // wight
199  *u.pByte++ = 0; // italic
200  *u.pByte++ = CharSet; // charset
201  u.pwStr = (1 + wcsecpy( u.pwStr, Font ));
202  }
203  return u.pItem;
204 }
205 
207  PDLGITEMTEMPLATEEX pBuf,
208  DWORD Type,
209  DWORD Style,
210  DWORD ExStyle,
211  short X, short Y,
212  short W, short H,
213  LPCWSTR Caption,
214  WORD Id,
215  DWORD HelpId
216  )
217 {
218  DitXUniPtr u;
219  u.pByte = dwAlign( (PBYTE) pBuf ); // align
220 
221  u.pItem->helpId = HelpId;
222  u.pItem->exStyle = ExStyle;
223  u.pItem->style = Style;
224  u.pItem->x = X;
225  u.pItem->y = Y;
226  u.pItem->cx = W;
227  u.pItem->cy = H;
228  u.pItem->id = Id;
229 
230  // Everything else in this structure is variable length,
231  // and therefore must be determined dynamically
232 
233  u.pItem++; // Past item header
234 
235  // wsz_Or_Ord ctrlClass; // Name or ordinal of a control class
236  // wsz_Or_Ord title; // Title string or ordinal of a resource
237  // WORD extraCount; // Bytes following creation data
238 
239  *u.pDword++ = Type; // E.g. ORD_STATIC;
240  u.pwStr = (1 + wcsecpy( u.pwStr, Caption ));
241  *u.pWord++ = 0; // No creation data
242  return u.pItem;
243 }
244 
245 // EOF
unsigned long DWORD
Definition: Common.h:414
#define HAVE_BITS(var, mask)
Definition: Common.h:1018
void FreeDlgTemplate(LPDLGTEMPLATE pDlg)
Definition: DlgPlate.cpp:44
unsigned short WORD
Definition: Common.h:413
WORD signature
Definition: DlgPlate.h:101
PDWORD pDword
Definition: DlgPlate.cpp:142
WORD cDlgItems
Definition: DlgPlate.h:105
DWORD helpId
Definition: DlgPlate.h:102
unsigned char * PBYTE
Definition: Common.h:412
LPDLGITEMTEMPLATE SetDlgStyle(LPDLGTEMPLATE pDlg, DWORD Style, short X, short Y, short W, short H, LPCWSTR Caption, WORD nCtls, LPCWSTR Font, WORD Points)
Definition: DlgPlate.cpp:57
DLGTEMPLATEEX * PDLGTEMPLATEEX
Definition: DlgPlate.h:115
DWORD exStyle
Definition: DlgPlate.h:103
PWORD pWord
Definition: DlgPlate.cpp:52
PBYTE pByte
Definition: DlgPlate.cpp:51
LPDLGTEMPLATE AllocDlgTemplate(WORD Size)
Definition: DlgPlate.cpp:38
LPDLGITEMTEMPLATE AddDlgItem(LPDLGITEMTEMPLATE pBuf, DWORD Type, DWORD Style, DWORD ExStyle, short X, short Y, short W, short H, LPCWSTR Caption, WORD Id)
Definition: DlgPlate.cpp:94
PWSTR pwStr
Definition: DlgPlate.cpp:143
void FreeExDlgTemplate(PDLGTEMPLATEEX pDlg)
Definition: DlgPlate.cpp:133
DWORD_PTR dwPad(DWORD_PTR X)
PDLGITEMTEMPLATEEX AddExDlgItem(PDLGITEMTEMPLATEEX pBuf, DWORD Type, DWORD Style, DWORD ExStyle, short X, short Y, short W, short H, LPCWSTR Caption, WORD Id, DWORD HelpId)
Definition: DlgPlate.cpp:206
PWSTR pwStr
Definition: DlgPlate.cpp:54
PBYTE dwAlign(PBYTE Ptr)
PDLGITEMTEMPLATEEX pItem
Definition: DlgPlate.cpp:139
DWORD style
Definition: DlgPlate.h:104
PWORD pWord
Definition: DlgPlate.cpp:141
wchar_t *__fastcall wcsecpy(register wchar_t *Dst, register const wchar_t *Src)
Definition: StrFunc.cpp:68
PDLGTEMPLATEEX AllocExDlgTemplate(WORD Size)
Definition: DlgPlate.cpp:128
LPDLGITEMTEMPLATE pItem
Definition: DlgPlate.cpp:50
unsigned short * PWORD
Definition: Common.h:413
PDLGITEMTEMPLATEEX SetExDlgStyle(PDLGTEMPLATEEX pDlg, DWORD Style, short X, short Y, short W, short H, LPCWSTR Caption, DWORD HelpId, WORD nCtls, LPCWSTR Font, short Points, short Weight, BYTE CharSet)
Definition: DlgPlate.cpp:146
PDWORD pDword
Definition: DlgPlate.cpp:53
unsigned char BYTE
Definition: Common.h:412
PBYTE pByte
Definition: DlgPlate.cpp:140
unsigned long * PDWORD
Definition: Common.h:414