[KMTESTS]
[reactos.git] / rosapps / applications / imagesoft / misc.c
1 #include <precomp.h>
2
3 static INT
4 LengthOfStrResource(IN HINSTANCE hInst,
5 IN UINT uID)
6 {
7 HRSRC hrSrc;
8 HGLOBAL hRes;
9 LPWSTR lpName, lpStr;
10
11 if (hInst == NULL)
12 {
13 return -1;
14 }
15
16 /* There are always blocks of 16 strings */
17 lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
18
19 /* Find the string table block */
20 if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
21 (hRes = LoadResource(hInst, hrSrc)) &&
22 (lpStr = LockResource(hRes)))
23 {
24 UINT x;
25
26 /* Find the string we're looking for */
27 uID &= 0xF; /* position in the block, same as % 16 */
28 for (x = 0; x < uID; x++)
29 {
30 lpStr += (*lpStr) + 1;
31 }
32
33 /* Found the string */
34 return (int)(*lpStr);
35 }
36 return -1;
37 }
38
39 INT
40 AllocAndLoadString(OUT LPTSTR *lpTarget,
41 IN HINSTANCE hInst,
42 IN UINT uID)
43 {
44 INT ln;
45
46 ln = LengthOfStrResource(hInst,
47 uID);
48 if (ln++ > 0)
49 {
50 (*lpTarget) = (LPTSTR)LocalAlloc(LMEM_FIXED,
51 ln * sizeof(TCHAR));
52 if ((*lpTarget) != NULL)
53 {
54 INT Ret;
55 if (!(Ret = LoadString(hInst, uID, *lpTarget, ln)))
56 {
57 LocalFree((HLOCAL)(*lpTarget));
58 }
59 return Ret;
60 }
61 }
62 return 0;
63 }
64
65 DWORD
66 LoadAndFormatString(IN HINSTANCE hInstance,
67 IN UINT uID,
68 OUT LPTSTR *lpTarget,
69 ...)
70 {
71 DWORD Ret = 0;
72 LPTSTR lpFormat;
73 va_list lArgs;
74
75 if (AllocAndLoadString(&lpFormat,
76 hInstance,
77 uID) > 0)
78 {
79 va_start(lArgs, lpTarget);
80 /* let's use FormatMessage to format it because it has the ability to allocate
81 memory automatically */
82 Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
83 lpFormat,
84 0,
85 0,
86 (LPTSTR)lpTarget,
87 0,
88 &lArgs);
89 va_end(lArgs);
90
91 LocalFree((HLOCAL)lpFormat);
92 }
93
94 return Ret;
95 }
96
97 BOOL
98 StatusBarLoadAndFormatString(IN HWND hStatusBar,
99 IN INT PartId,
100 IN HINSTANCE hInstance,
101 IN UINT uID,
102 ...)
103 {
104 BOOL Ret = FALSE;
105 LPTSTR lpFormat, lpStr;
106 va_list lArgs;
107
108 if (AllocAndLoadString(&lpFormat,
109 hInstance,
110 uID) > 0)
111 {
112 va_start(lArgs, uID);
113 /* let's use FormatMessage to format it because it has the ability to allocate
114 memory automatically */
115 Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
116 lpFormat,
117 0,
118 0,
119 (LPTSTR)&lpStr,
120 0,
121 &lArgs);
122 va_end(lArgs);
123
124 if (lpStr != NULL)
125 {
126 Ret = (BOOL)SendMessage(hStatusBar,
127 SB_SETTEXT,
128 (WPARAM)PartId,
129 (LPARAM)lpStr);
130 LocalFree((HLOCAL)lpStr);
131 }
132
133 LocalFree((HLOCAL)lpFormat);
134 }
135
136 return Ret;
137 }
138
139 BOOL
140 StatusBarLoadString(IN HWND hStatusBar,
141 IN INT PartId,
142 IN HINSTANCE hInstance,
143 IN UINT uID)
144 {
145 BOOL Ret = FALSE;
146 LPTSTR lpStr;
147
148 if (AllocAndLoadString(&lpStr,
149 hInstance,
150 uID) > 0)
151 {
152 Ret = (BOOL)SendMessage(hStatusBar,
153 SB_SETTEXT,
154 (WPARAM)PartId,
155 (LPARAM)lpStr);
156 LocalFree((HLOCAL)lpStr);
157 }
158
159 return Ret;
160 }
161
162
163 INT
164 GetTextFromEdit(OUT LPTSTR lpString,
165 IN HWND hDlg,
166 IN UINT Res)
167 {
168 INT len = GetWindowTextLength(GetDlgItem(hDlg, Res));
169 if(len > 0)
170 {
171 GetDlgItemText(hDlg,
172 Res,
173 lpString,
174 len + 1);
175 }
176 else
177 lpString = NULL;
178
179 return len;
180 }
181
182
183 VOID GetError(DWORD err)
184 {
185 LPVOID lpMsgBuf;
186
187 if (err == 0)
188 err = GetLastError();
189
190 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
191 FORMAT_MESSAGE_FROM_SYSTEM |
192 FORMAT_MESSAGE_IGNORE_INSERTS,
193 NULL,
194 err,
195 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
196 (LPTSTR) &lpMsgBuf,
197 0,
198 NULL );
199
200 MessageBox(NULL, lpMsgBuf, _T("Error!"), MB_OK | MB_ICONERROR);
201
202 LocalFree(lpMsgBuf);
203 }
204
205
206
207 /*
208 * Toolbar custom control routines
209 */
210
211 typedef struct _TBCUSTCTL
212 {
213 HWND hWndControl;
214 INT iCommand;
215 BOOL HideVertical : 1;
216 BOOL IsVertical : 1;
217 } TBCUSTCTL, *PTBCUSTCTL;
218
219 BOOL
220 ToolbarDeleteControlSpace(HWND hWndToolbar,
221 const TBBUTTON *ptbButton)
222 {
223 if ((ptbButton->fsStyle & TBSTYLE_SEP) &&
224 ptbButton->dwData != 0)
225 {
226 PTBCUSTCTL cctl = (PTBCUSTCTL)ptbButton->dwData;
227
228 DestroyWindow(cctl->hWndControl);
229
230 HeapFree(ProcessHeap,
231 0,
232 cctl);
233 return TRUE;
234 }
235
236 return FALSE;
237 }
238
239 VOID
240 ToolbarUpdateControlSpaces(HWND hWndToolbar,
241 ToolbarChangeControlCallback ChangeCallback)
242 {
243 BOOL Vert;
244 DWORD nButtons, i;
245 TBBUTTON tbtn;
246
247 Vert = ((SendMessage(hWndToolbar,
248 TB_GETSTYLE,
249 0,
250 0) & CCS_VERT) != 0);
251
252 nButtons = (DWORD)SendMessage(hWndToolbar,
253 TB_BUTTONCOUNT,
254 0,
255 0);
256
257 for (i = 0;
258 i != nButtons;
259 i++)
260 {
261 if (SendMessage(hWndToolbar,
262 TB_GETBUTTON,
263 (WPARAM)i,
264 (LPARAM)&tbtn))
265 {
266 if ((tbtn.fsStyle & TBSTYLE_SEP) && tbtn.dwData != 0)
267 {
268 PTBCUSTCTL cctl = (PTBCUSTCTL)tbtn.dwData;
269
270 cctl->IsVertical = Vert;
271
272 if (cctl->HideVertical)
273 {
274 ShowWindow(cctl->hWndControl,
275 (Vert ? SW_HIDE : SW_SHOW));
276 goto ShowHideSep;
277 }
278 else if (cctl->IsVertical != Vert)
279 {
280 ChangeCallback(hWndToolbar,
281 cctl->hWndControl,
282 Vert);
283
284 ShowHideSep:
285 /* show/hide the separator */
286 SendMessage(hWndToolbar,
287 TB_HIDEBUTTON,
288 (WPARAM)cctl->iCommand,
289 (LPARAM)Vert && cctl->HideVertical);
290 }
291 }
292 }
293 }
294 }
295
296 BOOL
297 ToolbarInsertSpaceForControl(HWND hWndToolbar,
298 HWND hWndControl,
299 INT Index,
300 INT iCmd,
301 BOOL HideVertical)
302 {
303 PTBCUSTCTL cctl;
304 RECT rcControl, rcItem;
305
306 cctl = HeapAlloc(ProcessHeap,
307 0,
308 sizeof(TBCUSTCTL));
309 if (cctl == NULL)
310 return FALSE;
311
312 cctl->HideVertical = HideVertical;
313 cctl->hWndControl = hWndControl;
314 cctl->iCommand = iCmd;
315
316 if (GetWindowRect(hWndControl,
317 &rcControl))
318 {
319 TBBUTTON tbtn = {0};
320
321 tbtn.iBitmap = rcControl.right - rcControl.left;
322 tbtn.idCommand = iCmd;
323 tbtn.fsStyle = TBSTYLE_SEP;
324 tbtn.dwData = (DWORD_PTR)cctl;
325
326 if (SendMessage(hWndToolbar,
327 TB_GETSTYLE,
328 0,
329 0) & CCS_VERT)
330 {
331 if (HideVertical)
332 tbtn.fsState |= TBSTATE_HIDDEN;
333
334 cctl->IsVertical = TRUE;
335 }
336 else
337 cctl->IsVertical = FALSE;
338
339 if (SendMessage(hWndToolbar,
340 TB_INSERTBUTTON,
341 (WPARAM)Index,
342 (LPARAM)&tbtn))
343 {
344 if (SendMessage(hWndToolbar,
345 TB_GETITEMRECT,
346 (WPARAM)Index,
347 (LPARAM)&rcItem))
348 {
349 SetWindowPos(hWndControl,
350 NULL,
351 rcItem.left,
352 rcItem.top,
353 rcItem.right - rcItem.left,
354 rcItem.bottom - rcItem.top,
355 SWP_NOZORDER);
356
357 ShowWindow(hWndControl,
358 SW_SHOW);
359
360 return TRUE;
361 }
362 else if (tbtn.fsState & TBSTATE_HIDDEN)
363 {
364 ShowWindow(hWndControl,
365 SW_HIDE);
366 }
367 }
368 }
369
370 return FALSE;
371 }
372
373
374 HIMAGELIST
375 InitImageList(UINT NumImages, UINT StartResource)
376 {
377 HBITMAP hBitmap;
378 HIMAGELIST hImageList;
379 UINT i, k;
380 INT Ret;
381
382
383 /* Create the toolbar icon image list */
384 hImageList = ImageList_Create(TB_BMP_WIDTH,
385 TB_BMP_HEIGHT,
386 ILC_MASK | ILC_COLOR24,
387 NumImages,
388 0);
389 if (! hImageList)
390 return NULL;
391
392 /* Add all icons to the image list */
393 for (i = StartResource, k = 0; k < NumImages; i++, k++)
394 {
395 hBitmap = LoadImage(hInstance,
396 MAKEINTRESOURCE(i),
397 IMAGE_BITMAP,
398 TB_BMP_WIDTH,
399 TB_BMP_HEIGHT,
400 LR_LOADTRANSPARENT);
401
402 Ret = ImageList_AddMasked(hImageList,
403 hBitmap,
404 RGB(255, 255, 254));
405
406 DeleteObject(hBitmap);
407 }
408
409 return hImageList;
410
411 }
412
413 /*
414 static BOOL
415 DestroyImageList(HIMAGELIST hImageList)
416 {
417 if (! ImageList_Destroy(hImageList))
418 return FALSE;
419 else
420 return TRUE;
421 }
422 */