2 * USER resource functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/winuser16.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(resource);
17 DECLARE_DEBUG_CHANNEL(accel);
19 /* this is the 8 byte accel struct used in Win32 resources (internal only) */
27 } PE_ACCEL, *LPPE_ACCEL;
29 /**********************************************************************
30 * LoadAccelerators [USER.177]
32 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, LPCSTR lpTableName)
36 TRACE_(accel)("%04x %s\n", instance, debugres_a(lpTableName) );
38 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATORA ))) {
39 WARN_(accel)("couldn't find accelerator table resource\n");
43 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
44 return LoadResource16(instance,hRsrc);
47 /**********************************************************************
48 * LoadAcceleratorsW (USER32.@)
49 * The image layout seems to look like this (not 100% sure):
50 * 00: BYTE type type of accelerator
51 * 01: BYTE pad (to WORD boundary)
54 * 06: WORD pad (to DWORD boundary)
56 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
59 HACCEL hMem,hRetval=0;
62 if (HIWORD(lpTableName))
63 TRACE_(accel)("%p '%s'\n",
64 (LPVOID)instance, (char *)( lpTableName ) );
66 TRACE_(accel)("%p 0x%04x\n",
67 (LPVOID)instance, LOWORD(lpTableName) );
69 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
71 WARN_(accel)("couldn't find accelerator table resource\n");
73 hMem = LoadResource( instance, hRsrc );
74 size = SizeofResource( instance, hRsrc );
75 if(size>=sizeof(PE_ACCEL))
77 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
79 int i,nrofaccells = size/sizeof(PE_ACCEL);
81 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
82 accel16 = (LPACCEL16)GlobalLock16(hRetval);
83 for (i=0;i<nrofaccells;i++) {
84 accel16[i].fVirt = accel_table[i].fVirt;
85 accel16[i].key = accel_table[i].key;
86 accel16[i].cmd = accel_table[i].cmd;
88 accel16[i-1].fVirt |= 0x80;
91 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
95 /***********************************************************************
96 * LoadAcceleratorsA (USER32.@)
98 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
104 if (!HIWORD(lpTableName)) return LoadAcceleratorsW( instance, (LPCWSTR)lpTableName );
106 len = MultiByteToWideChar( CP_ACP, 0, lpTableName, -1, NULL, 0 );
107 if ((uni = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
109 MultiByteToWideChar( CP_ACP, 0, lpTableName, -1, uni, len );
110 result = LoadAcceleratorsW(instance,uni);
111 HeapFree( GetProcessHeap(), 0, uni);
116 /**********************************************************************
117 * CopyAcceleratorTableA (USER32.@)
119 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
121 return CopyAcceleratorTableW(src, dst, entries);
124 /**********************************************************************
125 * CopyAcceleratorTableW (USER32.@)
127 * By mortene@pvv.org 980321
129 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
133 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
136 /* Do parameter checking to avoid the explosions and the screaming
137 as far as possible. */
138 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
139 WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
140 (LPVOID)src, (LPVOID)dst, entries);
143 xsize = GlobalSize16(src)/sizeof(ACCEL16);
144 if (xsize>entries) entries=xsize;
148 /* Spit out some debugging information. */
149 TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
150 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
152 /* Copy data to the destination structure array (if dst == NULL,
153 we're just supposed to count the number of entries). */
155 dst[i].fVirt = accel[i].fVirt;
156 dst[i].key = accel[i].key;
157 dst[i].cmd = accel[i].cmd;
159 /* Check if we've reached the end of the application supplied
160 accelerator table. */
162 /* Turn off the high order bit, just in case. */
163 dst[i].fVirt &= 0x7f;
168 /* The highest order bit seems to mark the end of the accelerator
169 resource table, but not always. Use GlobalSize() check too. */
170 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
178 /*********************************************************************
179 * CreateAcceleratorTableA (USER32.@)
181 * By mortene@pvv.org 980321
183 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
189 /* Do parameter checking just in case someone's trying to be
192 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
194 SetLastError(ERROR_INVALID_PARAMETER);
197 FIXME_(accel)("should check that the accelerator descriptions are valid,"
198 " return NULL and SetLastError() if not.\n");
201 /* Allocate memory and copy the table. */
202 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
204 TRACE_(accel)("handle %x\n", hAccel);
206 ERR_(accel)("Out of memory.\n");
207 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
210 accel = GlobalLock16(hAccel);
211 for (i=0;i<cEntries;i++) {
212 accel[i].fVirt = lpaccel[i].fVirt;
213 accel[i].key = lpaccel[i].key;
214 accel[i].cmd = lpaccel[i].cmd;
216 /* Set the end-of-table terminator. */
217 accel[cEntries-1].fVirt |= 0x80;
219 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
223 /*********************************************************************
224 * CreateAcceleratorTableW (USER32.@)
228 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
235 /* Do parameter checking just in case someone's trying to be
238 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
240 SetLastError(ERROR_INVALID_PARAMETER);
243 FIXME_(accel)("should check that the accelerator descriptions are valid,"
244 " return NULL and SetLastError() if not.\n");
247 /* Allocate memory and copy the table. */
248 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
250 TRACE_(accel)("handle %x\n", hAccel);
252 ERR_(accel)("Out of memory.\n");
253 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
256 accel = GlobalLock16(hAccel);
259 for (i=0;i<cEntries;i++) {
260 accel[i].fVirt = lpaccel[i].fVirt;
261 if( !(accel[i].fVirt & FVIRTKEY) ) {
262 ckey = (char) lpaccel[i].key;
263 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
264 WARN_(accel)("Error converting ASCII accelerator table to Unicode");
267 accel[i].key = lpaccel[i].key;
268 accel[i].cmd = lpaccel[i].cmd;
271 /* Set the end-of-table terminator. */
272 accel[cEntries-1].fVirt |= 0x80;
274 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
278 /******************************************************************************
279 * DestroyAcceleratorTable [USER32.@]
280 * Destroys an accelerator table
283 * By mortene@pvv.org 980321
286 * handle [I] Handle to accelerator table
290 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
292 return !GlobalFree16(handle);
295 /**********************************************************************
296 * LoadString (USER.176)
298 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
299 LPSTR buffer, INT16 buflen )
307 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
308 instance, resource_id, (int) buffer, buflen);
310 hrsrc = FindResource16( instance, (LPCSTR)((resource_id>>4)+1), RT_STRINGA );
311 if (!hrsrc) return 0;
312 hmem = LoadResource16( instance, hrsrc );
315 p = LockResource16(hmem);
316 string_num = resource_id & 0x000f;
317 for (i = 0; i < string_num; i++)
320 TRACE("strlen = %d\n", (int)*p );
322 if (buffer == NULL) return *p;
323 i = min(buflen - 1, *p);
325 memcpy(buffer, p + 1, i);
332 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
334 FreeResource16( hmem );
336 TRACE("'%s' loaded !\n", buffer);
340 /**********************************************************************
341 * LoadStringW (USER32.@)
343 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
344 LPWSTR buffer, INT buflen )
352 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
353 resource_id = (UINT)(-((INT)resource_id));
354 TRACE("instance = %04x, id = %04x, buffer = %08x, length = %d\n",
355 instance, (int)resource_id, (int) buffer, buflen);
357 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
359 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
361 if (!hrsrc) return 0;
362 hmem = LoadResource( instance, hrsrc );
365 p = LockResource(hmem);
366 string_num = resource_id & 0x000f;
367 for (i = 0; i < string_num; i++)
370 TRACE("strlen = %d\n", (int)*p );
372 if (buffer == NULL) return *p;
373 i = min(buflen - 1, *p);
375 memcpy(buffer, p + 1, i * sizeof (WCHAR));
376 buffer[i] = (WCHAR) 0;
379 buffer[0] = (WCHAR) 0;
383 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
387 TRACE("%s loaded !\n", debugstr_w(buffer));
391 /**********************************************************************
392 * LoadStringA (USER32.@)
394 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
395 LPSTR buffer, INT buflen )
400 TRACE("instance = %04x, id = %04x, buffer = %08x, length = %d\n",
401 instance, (int)resource_id, (int) buffer, buflen);
403 if(buffer == NULL) /* asked size of string */
404 return LoadStringW(instance, resource_id, NULL, 0);
406 wbuf = HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(WCHAR));
410 retval = LoadStringW(instance, resource_id, wbuf, buflen);
413 retval = WideCharToMultiByte(CP_ACP, 0, wbuf, retval, buffer, buflen - 1, NULL, NULL);
415 TRACE("%s loaded !\n", debugstr_a(buffer));
417 else buffer[0] = 0; /* no check of buflen here */
418 HeapFree( GetProcessHeap(), 0, wbuf );