Fixed issue #2507: Support keyboard shortcuts in yes/no prompts
[TortoiseGit.git] / src / Utils / MiscUI / DlgTemplate.h
blob0179b1088ea9b4b0be289f447255469f4a89a11c
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2006,2008 - TortoiseSVN
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 class CDlgTemplate
22 public:
24 CDlgTemplate()
26 usedBufferLength = 0;
27 totalBufferLength = 0;
28 dialogTemplate = 0;
30 CDlgTemplate(LPCTSTR caption, DWORD style, short x, short y, short w, short h,
31 LPCTSTR font = NULL, LONG fontSize = 8)
34 usedBufferLength = sizeof(DLGTEMPLATE );
35 totalBufferLength = usedBufferLength;
37 dialogTemplate = (DLGTEMPLATE*)malloc(totalBufferLength);
39 if (dialogTemplate)
41 dialogTemplate->style = style;
43 dialogTemplate->style |= DS_SETFONT;
45 dialogTemplate->x = x;
46 dialogTemplate->y = y;
47 dialogTemplate->cx = w;
48 dialogTemplate->cy = h;
49 dialogTemplate->cdit = 0;
51 dialogTemplate->dwExtendedStyle = 0;
53 //the dialog box doesn't have a menu or a special class
55 AppendData(_T("\0"), 2);
56 AppendData(_T("\0"), 2);
58 //add the dialog's caption to the template
60 AppendString(caption);
62 AppendData(&fontSize, sizeof(WORD));
63 AppendString(font);
67 void AddComponent(LPCTSTR type, LPCTSTR caption, DWORD style, DWORD exStyle,
68 short x, short y, short w, short h, WORD id)
71 DLGITEMTEMPLATE item;
73 item.style = style;
74 item.x = x;
75 item.y = y;
76 item.cx = w;
77 item.cy = h;
78 item.id = id;
80 item.dwExtendedStyle = exStyle;
82 AppendData(&item, sizeof(DLGITEMTEMPLATE));
84 AppendString(type);
85 AppendString(caption);
87 WORD creationDataLength = 0;
88 AppendData(&creationDataLength, sizeof(WORD));
90 //increment the component count
92 dialogTemplate->cdit++;
96 void AddButton(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
97 short w, short h, WORD id)
100 AddStandardComponent(0x0080, caption, style, exStyle, x, y, w, h, id);
102 WORD creationDataLength = 0;
103 AppendData(&creationDataLength, sizeof(WORD));
107 void AddEditBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
108 short w, short h, WORD id)
111 AddStandardComponent(0x0081, caption, style, exStyle, x, y, w, h, id);
113 WORD creationDataLength = 0;
114 AppendData(&creationDataLength, sizeof(WORD));
118 void AddStatic(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
119 short w, short h, WORD id)
122 AddStandardComponent(0x0082, caption, style, exStyle, x, y, w, h, id);
124 WORD creationDataLength = 0;
125 AppendData(&creationDataLength, sizeof(WORD));
129 void AddListBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
130 short w, short h, WORD id)
133 AddStandardComponent(0x0083, caption, style, exStyle, x, y, w, h, id);
135 WORD creationDataLength = 0;
136 AppendData(&creationDataLength, sizeof(WORD));
140 void AddScrollBar(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
141 short w, short h, WORD id)
144 AddStandardComponent(0x0084, caption, style, exStyle, x, y, w, h, id);
146 WORD creationDataLength = 0;
147 AppendData(&creationDataLength, sizeof(WORD));
151 void AddComboBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
152 short w, short h, WORD id)
155 AddStandardComponent(0x0085, caption, style, exStyle, x, y, w, h, id);
157 WORD creationDataLength = 0;
158 AppendData(&creationDataLength, sizeof(WORD));
163 * Returns a pointer to the Win32 dialog template which the object
164 * represents. This pointer may become invalid if additional
165 * components are added to the template.
168 operator const DLGTEMPLATE*() const
170 return dialogTemplate;
173 virtual ~CDlgTemplate()
175 free(dialogTemplate);
178 protected:
180 void AddStandardComponent(WORD type, LPCTSTR caption, DWORD style,
181 DWORD exStyle, short x, short y, short w, short h, WORD id)
184 DLGITEMTEMPLATE item;
186 // DWORD align the beginning of the component data
188 AlignData(sizeof(DWORD));
190 item.style = style;
191 item.x = x;
192 item.y = y;
193 item.cx = w;
194 item.cy = h;
195 item.id = id;
197 item.dwExtendedStyle = exStyle;
199 AppendData(&item, sizeof(DLGITEMTEMPLATE));
201 WORD preType = 0xFFFF;
203 AppendData(&preType, sizeof(WORD));
204 AppendData(&type, sizeof(WORD));
206 AppendString(caption);
208 // Increment the component count
210 dialogTemplate->cdit++;
214 void AlignData(int size)
217 int paddingSize = usedBufferLength % size;
219 if (paddingSize != 0)
221 EnsureSpace(paddingSize);
222 usedBufferLength += paddingSize;
227 void AppendString(LPCTSTR string)
229 #ifndef _UNICODE
230 int length = MultiByteToWideChar(CP_ACP, 0, string, -1, NULL, 0);
231 #else
232 int length = (int)wcslen(string)+1;
233 #endif
234 WCHAR* wideString = (WCHAR*)malloc(sizeof(WCHAR) * length);
235 if (wideString)
237 #ifndef _UNICODE
238 MultiByteToWideChar(CP_ACP, 0, string, -1, wideString, length);
239 #else
240 wcscpy_s(wideString, length, string);
241 #endif
242 AppendData(wideString, length * sizeof(WCHAR));
243 free(wideString);
247 void AppendData(void* data, int dataLength)
250 EnsureSpace(dataLength);
252 memcpy((char*)dialogTemplate + usedBufferLength, data, dataLength);
253 usedBufferLength += dataLength;
257 void EnsureSpace(int length)
260 if (length + usedBufferLength > totalBufferLength)
263 totalBufferLength += length * 2;
265 void* newBuffer = malloc(totalBufferLength);
266 memcpy(newBuffer, dialogTemplate, usedBufferLength);
268 free(dialogTemplate);
269 dialogTemplate = (DLGTEMPLATE*)newBuffer;
275 private:
277 DLGTEMPLATE* dialogTemplate;
279 int totalBufferLength;
280 int usedBufferLength;