OSDN Git Service

Change Dir Structure to be same as TortoiseSVN'
[tortoisegit/TortoiseGitJp.git] / src / Utils / MiscUI / DlgTemplate.h
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2006,2008 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 class CDlgTemplate\r
20 {\r
21 \r
22 public:\r
23 \r
24         CDlgTemplate()\r
25         {\r
26                 usedBufferLength = 0;\r
27                 totalBufferLength = 0;\r
28                 dialogTemplate = 0;\r
29         };\r
30     CDlgTemplate(LPCTSTR caption, DWORD style, short x, short y, short w, short h,\r
31         LPCTSTR font = NULL, LONG fontSize = 8)\r
32     {\r
33 \r
34         usedBufferLength = sizeof(DLGTEMPLATE );\r
35         totalBufferLength = usedBufferLength;\r
36 \r
37         dialogTemplate = (DLGTEMPLATE*)malloc(totalBufferLength);\r
38 \r
39         dialogTemplate->style = style;\r
40 \r
41         dialogTemplate->style |= DS_SETFONT;\r
42 \r
43         dialogTemplate->x     = x;\r
44         dialogTemplate->y     = y;\r
45         dialogTemplate->cx    = w;\r
46         dialogTemplate->cy    = h;\r
47         dialogTemplate->cdit  = 0;\r
48 \r
49         dialogTemplate->dwExtendedStyle = 0;\r
50 \r
51         //the dialog box doesn't have a menu or a special class\r
52 \r
53         AppendData(_T("\0"), 2);\r
54         AppendData(_T("\0"), 2);\r
55 \r
56         //add the dialog's caption to the template\r
57 \r
58         AppendString(caption);\r
59 \r
60         AppendData(&fontSize, sizeof(WORD));\r
61         AppendString(font);\r
62 \r
63     }\r
64 \r
65     void AddComponent(LPCTSTR type, LPCTSTR caption, DWORD style, DWORD exStyle,\r
66         short x, short y, short w, short h, WORD id)\r
67     {\r
68 \r
69         DLGITEMTEMPLATE item;\r
70 \r
71         item.style = style;\r
72         item.x     = x;\r
73         item.y     = y;\r
74         item.cx    = w;\r
75         item.cy    = h;\r
76         item.id    = id;\r
77 \r
78         item.dwExtendedStyle = exStyle;\r
79 \r
80         AppendData(&item, sizeof(DLGITEMTEMPLATE));\r
81 \r
82         AppendString(type);\r
83         AppendString(caption);\r
84 \r
85         WORD creationDataLength = 0;\r
86         AppendData(&creationDataLength, sizeof(WORD));\r
87 \r
88         //increment the component count\r
89 \r
90         dialogTemplate->cdit++;\r
91 \r
92     }\r
93 \r
94     void AddButton(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,\r
95         short w, short h, WORD id)\r
96     {\r
97 \r
98         AddStandardComponent(0x0080, caption, style, exStyle, x, y, w, h, id);\r
99 \r
100         WORD creationDataLength = 0;\r
101         AppendData(&creationDataLength, sizeof(WORD));\r
102 \r
103     }\r
104 \r
105     void AddEditBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,\r
106         short w, short h, WORD id)\r
107     {\r
108 \r
109         AddStandardComponent(0x0081, caption, style, exStyle, x, y, w, h, id);\r
110 \r
111         WORD creationDataLength = 0;\r
112         AppendData(&creationDataLength, sizeof(WORD));\r
113 \r
114     }\r
115 \r
116     void AddStatic(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,\r
117         short w, short h, WORD id)\r
118     {\r
119 \r
120         AddStandardComponent(0x0082, caption, style, exStyle, x, y, w, h, id);\r
121 \r
122         WORD creationDataLength = 0;\r
123         AppendData(&creationDataLength, sizeof(WORD));\r
124 \r
125     }\r
126 \r
127     void AddListBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,\r
128         short w, short h, WORD id)\r
129     {\r
130 \r
131         AddStandardComponent(0x0083, caption, style, exStyle, x, y, w, h, id);\r
132 \r
133         WORD creationDataLength = 0;\r
134         AppendData(&creationDataLength, sizeof(WORD));\r
135 \r
136     }\r
137 \r
138     void AddScrollBar(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,\r
139         short w, short h, WORD id)\r
140     {\r
141 \r
142         AddStandardComponent(0x0084, caption, style, exStyle, x, y, w, h, id);\r
143 \r
144         WORD creationDataLength = 0;\r
145         AppendData(&creationDataLength, sizeof(WORD));\r
146 \r
147     }\r
148 \r
149     void AddComboBox(LPCTSTR caption, DWORD style, DWORD exStyle, short x, short y,\r
150         short w, short h, WORD id)\r
151     {\r
152 \r
153         AddStandardComponent(0x0085, caption, style, exStyle, x, y, w, h, id);\r
154 \r
155         WORD creationDataLength = 0;\r
156         AppendData(&creationDataLength, sizeof(WORD));\r
157 \r
158     }\r
159 \r
160     /**\r
161      * Returns a pointer to the Win32 dialog template which the object\r
162      * represents. This pointer may become invalid if additional\r
163      * components are added to the template.\r
164      */\r
165 \r
166     operator const DLGTEMPLATE*() const\r
167     {\r
168         return dialogTemplate;\r
169     }\r
170 \r
171     virtual ~CDlgTemplate()\r
172     {\r
173         free(dialogTemplate);\r
174     }\r
175 \r
176 protected:\r
177 \r
178     void AddStandardComponent(WORD type, LPCTSTR caption, DWORD style,\r
179         DWORD exStyle, short x, short y, short w, short h, WORD id)\r
180     {\r
181 \r
182         DLGITEMTEMPLATE item;\r
183 \r
184         // DWORD align the beginning of the component data\r
185 \r
186         AlignData(sizeof(DWORD));\r
187 \r
188         item.style = style;\r
189         item.x     = x;\r
190         item.y     = y;\r
191         item.cx    = w;\r
192         item.cy    = h;\r
193         item.id    = id;\r
194 \r
195         item.dwExtendedStyle = exStyle;\r
196 \r
197         AppendData(&item, sizeof(DLGITEMTEMPLATE));\r
198 \r
199         WORD preType = 0xFFFF;\r
200 \r
201         AppendData(&preType, sizeof(WORD));\r
202         AppendData(&type, sizeof(WORD));\r
203 \r
204         AppendString(caption);\r
205 \r
206         // Increment the component count\r
207 \r
208         dialogTemplate->cdit++;\r
209 \r
210     }\r
211 \r
212     void AlignData(int size)\r
213     {\r
214 \r
215         int paddingSize = usedBufferLength % size;\r
216 \r
217         if (paddingSize != 0)\r
218         {\r
219             EnsureSpace(paddingSize);\r
220             usedBufferLength += paddingSize;\r
221         }\r
222 \r
223     }\r
224 \r
225     void AppendString(LPCTSTR string)\r
226     {\r
227 #ifndef _UNICODE\r
228         int length = MultiByteToWideChar(CP_ACP, 0, string, -1, NULL, 0);\r
229 #else\r
230                 int length = (int)wcslen(string)+1;\r
231 #endif\r
232         WCHAR* wideString = (WCHAR*)malloc(sizeof(WCHAR) * length);\r
233 #ifndef _UNICODE\r
234         MultiByteToWideChar(CP_ACP, 0, string, -1, wideString, length);\r
235 #else\r
236                 wcscpy_s(wideString, length, string);\r
237 #endif\r
238         AppendData(wideString, length * sizeof(WCHAR));\r
239         free(wideString);\r
240     }\r
241 \r
242     void AppendData(void* data, int dataLength)\r
243     {\r
244 \r
245         EnsureSpace(dataLength);\r
246 \r
247         memcpy((char*)dialogTemplate + usedBufferLength, data, dataLength);\r
248         usedBufferLength += dataLength;\r
249 \r
250     }\r
251 \r
252     void EnsureSpace(int length)\r
253     {\r
254 \r
255         if (length + usedBufferLength > totalBufferLength)\r
256         {\r
257 \r
258             totalBufferLength += length * 2;\r
259 \r
260             void* newBuffer = malloc(totalBufferLength);\r
261             memcpy(newBuffer, dialogTemplate, usedBufferLength);\r
262 \r
263             free(dialogTemplate);\r
264             dialogTemplate = (DLGTEMPLATE*)newBuffer;\r
265 \r
266         }\r
267 \r
268     }\r
269 \r
270 private:\r
271 \r
272     DLGTEMPLATE* dialogTemplate;\r
273 \r
274     int totalBufferLength;\r
275     int usedBufferLength;\r
276 \r
277 };\r