[NTDLL]
[reactos.git] / base / applications / mscutils / servman / misc.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/servman/misc.c
5 * PURPOSE: miscallanous functions
6 * COPYRIGHT: Copyright 2005 Thomas Weidenmueller <w3seek@reactos.org>
7 * Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
8 *
9 */
10
11 #include "precomp.h"
12
13 static INT
14 LengthOfStrResource(IN HINSTANCE hInst,
15 IN UINT uID)
16 {
17 HRSRC hrSrc;
18 HGLOBAL hRes;
19 LPWSTR lpName, lpStr;
20
21 if (hInst == NULL)
22 {
23 return -1;
24 }
25
26 /* There are always blocks of 16 strings */
27 lpName = (LPWSTR)MAKEINTRESOURCE((uID >> 4) + 1);
28
29 /* Find the string table block */
30 if ((hrSrc = FindResourceW(hInst, lpName, (LPWSTR)RT_STRING)) &&
31 (hRes = LoadResource(hInst, hrSrc)) &&
32 (lpStr = (WCHAR*) LockResource(hRes)))
33 {
34 UINT x;
35
36 /* Find the string we're looking for */
37 uID &= 0xF; /* position in the block, same as % 16 */
38 for (x = 0; x < uID; x++)
39 {
40 lpStr += (*lpStr) + 1;
41 }
42
43 /* Found the string */
44 return (int)(*lpStr);
45 }
46 return -1;
47 }
48
49 INT
50 AllocAndLoadString(OUT LPTSTR *lpTarget,
51 IN HINSTANCE hInst,
52 IN UINT uID)
53 {
54 INT ln;
55
56 ln = LengthOfStrResource(hInst,
57 uID);
58 if (ln++ > 0)
59 {
60 (*lpTarget) = (LPTSTR)LocalAlloc(LMEM_FIXED,
61 ln * sizeof(TCHAR));
62 if ((*lpTarget) != NULL)
63 {
64 INT Ret;
65 if (!(Ret = LoadString(hInst, uID, *lpTarget, ln)))
66 {
67 LocalFree((HLOCAL)(*lpTarget));
68 }
69 return Ret;
70 }
71 }
72 return 0;
73 }
74
75 DWORD
76 LoadAndFormatString(IN HINSTANCE hInstance,
77 IN UINT uID,
78 OUT LPTSTR *lpTarget,
79 ...)
80 {
81 DWORD Ret = 0;
82 LPTSTR lpFormat;
83 va_list lArgs;
84
85 if (AllocAndLoadString(&lpFormat,
86 hInstance,
87 uID) > 0)
88 {
89 va_start(lArgs, lpTarget);
90 /* let's use Format to format it because it has the ability to allocate
91 memory automatically */
92 Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
93 lpFormat,
94 0,
95 0,
96 (LPTSTR)lpTarget,
97 0,
98 &lArgs);
99 va_end(lArgs);
100
101 LocalFree((HLOCAL)lpFormat);
102 }
103
104 return Ret;
105 }
106
107 BOOL
108 StatusBarLoadAndFormatString(IN HWND hStatusBar,
109 IN INT PartId,
110 IN HINSTANCE hInstance,
111 IN UINT uID,
112 ...)
113 {
114 BOOL Ret = FALSE;
115 LPTSTR lpFormat, lpStr;
116 va_list lArgs;
117
118 if (AllocAndLoadString(&lpFormat,
119 hInstance,
120 uID) > 0)
121 {
122 va_start(lArgs, uID);
123 /* let's use FormatMessage to format it because it has the ability to allocate
124 memory automatically */
125 Ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
126 lpFormat,
127 0,
128 0,
129 (VOID*)&lpStr,
130 0,
131 &lArgs);
132 va_end(lArgs);
133
134 if (lpStr != NULL)
135 {
136 Ret = (BOOL)SendMessage(hStatusBar,
137 SB_SETTEXT,
138 (WPARAM)PartId,
139 (LPARAM)lpStr);
140 LocalFree((HLOCAL)lpStr);
141 }
142
143 LocalFree((HLOCAL)lpFormat);
144 }
145
146 return Ret;
147 }
148
149 BOOL
150 StatusBarLoadString(IN HWND hStatusBar,
151 IN INT PartId,
152 IN HINSTANCE hInstance,
153 IN UINT uID)
154 {
155 BOOL Ret = FALSE;
156 LPTSTR lpStr;
157
158 if (AllocAndLoadString(&lpStr,
159 hInstance,
160 uID) > 0)
161 {
162 Ret = (BOOL)SendMessage(hStatusBar,
163 SB_SETTEXT,
164 (WPARAM)PartId,
165 (LPARAM)lpStr);
166 LocalFree((HLOCAL)lpStr);
167 }
168
169 return Ret;
170 }
171
172
173 INT
174 GetTextFromEdit(OUT LPTSTR lpString,
175 IN HWND hDlg,
176 IN UINT Res)
177 {
178 INT len = GetWindowTextLength(GetDlgItem(hDlg, Res));
179 if(len > 0)
180 {
181 GetDlgItemText(hDlg,
182 Res,
183 lpString,
184 len + 1);
185 }
186 else
187 lpString = NULL;
188
189 return len;
190 }
191
192 VOID GetError(VOID)
193 {
194 LPTSTR lpMsgBuf = NULL;
195
196 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
197 FORMAT_MESSAGE_FROM_SYSTEM |
198 FORMAT_MESSAGE_IGNORE_INSERTS,
199 NULL,
200 GetLastError(),
201 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
202 (VOID*)&lpMsgBuf,
203 0,
204 NULL );
205
206 MessageBox(NULL, lpMsgBuf, _T("Error!"), MB_OK | MB_ICONERROR);
207
208 LocalFree(lpMsgBuf);
209 }
210
211 VOID DisplayString(PTCHAR Msg)
212 {
213 MessageBox(NULL, Msg, _T("Note!"), MB_ICONEXCLAMATION|MB_OK);
214 }
215
216
217
218 HIMAGELIST
219 InitImageList(UINT StartResource,
220 UINT EndResource,
221 UINT Width,
222 UINT Height,
223 ULONG type)
224 {
225 HANDLE hImage;
226 HIMAGELIST himl;
227 UINT i;
228 INT ret;
229
230 /* Create the toolbar icon image list */
231 himl = ImageList_Create(Width,
232 Height,
233 ILC_MASK | ILC_COLOR32,
234 EndResource - StartResource,
235 0);
236 if (himl == NULL)
237 return NULL;
238
239 ret = 0;
240 for (i = StartResource; i <= EndResource && ret != -1; i++)
241 {
242 hImage = LoadImage(hInstance,
243 MAKEINTRESOURCE(i),
244 type,
245 Width,
246 Height,
247 LR_LOADTRANSPARENT);
248 if (hImage == NULL)
249 {
250 ImageList_Destroy(himl);
251 himl = NULL;
252 break;
253 }
254
255 if (type == IMAGE_BITMAP)
256 {
257 ret = ImageList_AddMasked(himl,
258 hImage,
259 RGB(255, 0, 128));
260 }
261 else if (type == IMAGE_ICON)
262 {
263 ret = ImageList_AddIcon(himl,
264 hImage);
265 }
266
267 DeleteObject(hImage);
268 }
269
270 if (ret == -1)
271 {
272 ImageList_Destroy(himl);
273 himl = NULL;
274 }
275
276 return himl;
277 }