Fix history combobox show twice item
[TortoiseGit.git] / src / Utils / MiscUI / DlgTemplate.h
blobf074f85fdb930f3ff968c835da49b599706c6a52
1 // TortoiseSVN - 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 dialogTemplate->style = style;
41 dialogTemplate->style |= DS_SETFONT;
43 dialogTemplate->x = x;
44 dialogTemplate->y = y;
45 dialogTemplate->cx = w;
46 dialogTemplate->cy = h;
47 dialogTemplate->cdit = 0;
49 dialogTemplate->dwExtendedStyle = 0;
51 //the dialog box doesn't have a menu or a special class
53 AppendData(_T("\0"), 2);
54 AppendData(_T("\0"), 2);
56 //add the dialog's caption to the template
58 AppendString(caption);
60 AppendData(&fontSize, sizeof(WORD));
61 AppendString(font);
65 void AddComponent(LPCTSTR type, LPCTSTR caption, DWORD style, DWORD exStyle,
66 short x, short y, short w, short h, WORD id)
69 DLGITEMTEMPLATE item;
71 item.style = style;
72 item.x = x;
73 item.y = y;
74 item.cx = w;
75 item.cy = h;
76 item.id = id;
78 item.dwExtendedStyle = exStyle;
80 AppendData(&item, sizeof(DLGITEMTEMPLATE));
82 AppendString(type);
83 AppendString(caption);
85 WORD creationDataLength = 0;
86 AppendData(&creationDataLength, sizeof(WORD));
88 //increment the component count
90 dialogTemplate->cdit++;
94 void AddButton(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
95 short w, short h, WORD id)
98 AddStandardComponent(0x0080, caption, style, exStyle, x, y, w, h, id);
100 WORD creationDataLength = 0;
101 AppendData(&creationDataLength, sizeof(WORD));
105 void AddEditBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
106 short w, short h, WORD id)
109 AddStandardComponent(0x0081, caption, style, exStyle, x, y, w, h, id);
111 WORD creationDataLength = 0;
112 AppendData(&creationDataLength, sizeof(WORD));
116 void AddStatic(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
117 short w, short h, WORD id)
120 AddStandardComponent(0x0082, caption, style, exStyle, x, y, w, h, id);
122 WORD creationDataLength = 0;
123 AppendData(&creationDataLength, sizeof(WORD));
127 void AddListBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
128 short w, short h, WORD id)
131 AddStandardComponent(0x0083, caption, style, exStyle, x, y, w, h, id);
133 WORD creationDataLength = 0;
134 AppendData(&creationDataLength, sizeof(WORD));
138 void AddScrollBar(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
139 short w, short h, WORD id)
142 AddStandardComponent(0x0084, caption, style, exStyle, x, y, w, h, id);
144 WORD creationDataLength = 0;
145 AppendData(&creationDataLength, sizeof(WORD));
149 void AddComboBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,
150 short w, short h, WORD id)
153 AddStandardComponent(0x0085, caption, style, exStyle, x, y, w, h, id);
155 WORD creationDataLength = 0;
156 AppendData(&creationDataLength, sizeof(WORD));
161 * Returns a pointer to the Win32 dialog template which the object
162 * represents. This pointer may become invalid if additional
163 * components are added to the template.
166 operator const DLGTEMPLATE*() const
168 return dialogTemplate;
171 virtual ~CDlgTemplate()
173 free(dialogTemplate);
176 protected:
178 void AddStandardComponent(WORD type, LPCTSTR caption, DWORD style,
179 DWORD exStyle, short x, short y, short w, short h, WORD id)
182 DLGITEMTEMPLATE item;
184 // DWORD align the beginning of the component data
186 AlignData(sizeof(DWORD));
188 item.style = style;
189 item.x = x;
190 item.y = y;
191 item.cx = w;
192 item.cy = h;
193 item.id = id;
195 item.dwExtendedStyle = exStyle;
197 AppendData(&item, sizeof(DLGITEMTEMPLATE));
199 WORD preType = 0xFFFF;
201 AppendData(&preType, sizeof(WORD));
202 AppendData(&type, sizeof(WORD));
204 AppendString(caption);
206 // Increment the component count
208 dialogTemplate->cdit++;
212 void AlignData(int size)
215 int paddingSize = usedBufferLength % size;
217 if (paddingSize != 0)
219 EnsureSpace(paddingSize);
220 usedBufferLength += paddingSize;
225 void AppendString(LPCTSTR string)
227 #ifndef _UNICODE
228 int length = MultiByteToWideChar(CP_ACP, 0, string, -1, NULL, 0);
229 #else
230 int length = (int)wcslen(string)+1;
231 #endif
232 WCHAR* wideString = (WCHAR*)malloc(sizeof(WCHAR) * length);
233 #ifndef _UNICODE
234 MultiByteToWideChar(CP_ACP, 0, string, -1, wideString, length);
235 #else
236 wcscpy_s(wideString, length, string);
237 #endif
238 AppendData(wideString, length * sizeof(WCHAR));
239 free(wideString);
242 void AppendData(void* data, int dataLength)
245 EnsureSpace(dataLength);
247 memcpy((char*)dialogTemplate + usedBufferLength, data, dataLength);
248 usedBufferLength += dataLength;
252 void EnsureSpace(int length)
255 if (length + usedBufferLength > totalBufferLength)
258 totalBufferLength += length * 2;
260 void* newBuffer = malloc(totalBufferLength);
261 memcpy(newBuffer, dialogTemplate, usedBufferLength);
263 free(dialogTemplate);
264 dialogTemplate = (DLGTEMPLATE*)newBuffer;
270 private:
272 DLGTEMPLATE* dialogTemplate;
274 int totalBufferLength;
275 int usedBufferLength;