1 /* 2 * PROJECT: ReactOS API Test GUI 3 * LICENSE: GPL - See COPYING in the top level directory 4 * FILE: 5 * PURPOSE: miscallanous functions 6 * COPYRIGHT: Copyright 2005 Thomas Weidenmueller <w3seek@reactos.org> 7 * Copyright 2006 - 2008 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)MAKEINTRESOURCEW((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 LPWSTR *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 = LoadStringW(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 LPWSTR *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 = FormatMessageW(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 LPWSTR 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 = FormatMessageW(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)SendMessageW(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 LPWSTR lpStr; 157 158 if (AllocAndLoadString(&lpStr, 159 hInstance, 160 uID) > 0) 161 { 162 Ret = (BOOL)SendMessageW(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 LPWSTR lpString, 175 IN HWND hDlg, 176 IN UINT Res) 177 { 178 INT len = GetWindowTextLengthW(GetDlgItem(hDlg, Res)); 179 if(len > 0) 180 { 181 GetDlgItemTextW(hDlg, 182 Res, 183 lpString, 184 len + 1); 185 } 186 else 187 lpString = NULL; 188 189 return len; 190 } 191 192 VOID DisplayError(INT err) 193 { 194 LPWSTR lpMsgBuf = NULL; 195 196 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | 197 FORMAT_MESSAGE_FROM_SYSTEM | 198 FORMAT_MESSAGE_IGNORE_INSERTS, 199 NULL, 200 err, 201 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 202 (VOID*)&lpMsgBuf, 203 0, 204 NULL ); 205 206 MessageBoxW(NULL, lpMsgBuf, L"Error!", MB_OK | MB_ICONERROR); 207 208 LocalFree(lpMsgBuf); 209 } 210 211 VOID DisplayMessage(LPWSTR lpMsg) 212 { 213 MessageBoxW(NULL, lpMsg, L"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 { 224 HICON hIcon; 225 HIMAGELIST hImageList; 226 UINT i; 227 INT Ret; 228 229 /* Create the toolbar icon image list */ 230 hImageList = ImageList_Create(Width, 231 Height, 232 ILC_MASK | ILC_COLOR32, 233 EndResource - StartResource, 234 0); 235 if (hImageList == NULL) 236 return NULL; 237 238 /* Add all icons to the image list */ 239 for (i = StartResource; i <= EndResource; i++) 240 { 241 hIcon = (HICON)LoadImageW(hInstance, 242 MAKEINTRESOURCEW(i), 243 IMAGE_ICON, 244 Width, 245 Height, 246 LR_DEFAULTCOLOR); 247 if (hIcon == NULL) 248 goto fail; 249 250 Ret = ImageList_AddIcon(hImageList, 251 hIcon); 252 253 DestroyIcon(hIcon); 254 255 if (Ret == -1) 256 goto fail; 257 } 258 259 return hImageList; 260 261 fail: 262 ImageList_Destroy(hImageList); 263 return NULL; 264 } 265 266 DWORD 267 AnsiToUnicode(LPCSTR lpSrcStr, 268 LPWSTR *lpDstStr) 269 { 270 INT length; 271 INT ret = 0; 272 273 length = strlen(lpSrcStr) + 1; 274 275 *lpDstStr = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR)); 276 if (*lpDstStr) 277 { 278 ret = MultiByteToWideChar(CP_ACP, 279 0, 280 lpSrcStr, 281 -1, 282 *lpDstStr, 283 length); 284 } 285 286 return ret; 287 } 288 289 DWORD 290 UnicodeToAnsi(LPCWSTR lpSrcStr, 291 LPSTR *lpDstStr) 292 { 293 INT length; 294 INT ret = 0; 295 296 length = wcslen(lpSrcStr) + 1; 297 298 *lpDstStr = (LPSTR)HeapAlloc(GetProcessHeap(), 0, length); 299 if (*lpDstStr) 300 { 301 ret = WideCharToMultiByte(CP_ACP, 302 0, 303 lpSrcStr, 304 -1, 305 *lpDstStr, 306 length, 307 NULL, 308 NULL); 309 } 310 311 return ret; 312 } 313