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"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(resource);
20 DECLARE_DEBUG_CHANNEL(accel);
22 /* this is the 8 byte accel struct used in Win32 resources (internal only) */
30 } PE_ACCEL, *LPPE_ACCEL;
32 /**********************************************************************
33 * LoadAccelerators16 [USER.177]
35 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, LPCSTR lpTableName)
39 TRACE_(accel)("%04x %s\n", instance, debugres_a(lpTableName) );
41 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATORA ))) {
42 WARN_(accel)("couldn't find accelerator table resource\n");
46 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
47 return LoadResource16(instance,hRsrc);
50 /**********************************************************************
51 * LoadAcceleratorsW (USER32.356)
52 * The image layout seems to look like this (not 100% sure):
53 * 00: BYTE type type of accelerator
54 * 01: BYTE pad (to WORD boundary)
57 * 06: WORD pad (to DWORD boundary)
59 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
62 HACCEL hMem,hRetval=0;
65 if (HIWORD(lpTableName))
66 TRACE_(accel)("%p '%s'\n",
67 (LPVOID)instance, (char *)( lpTableName ) );
69 TRACE_(accel)("%p 0x%04x\n",
70 (LPVOID)instance, LOWORD(lpTableName) );
72 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
74 WARN_(accel)("couldn't find accelerator table resource\n");
76 hMem = LoadResource( instance, hRsrc );
77 size = SizeofResource( instance, hRsrc );
78 if(size>=sizeof(PE_ACCEL))
80 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
82 int i,nrofaccells = size/sizeof(PE_ACCEL);
84 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
85 accel16 = (LPACCEL16)GlobalLock16(hRetval);
86 for (i=0;i<nrofaccells;i++) {
87 accel16[i].fVirt = accel_table[i].fVirt;
88 accel16[i].key = accel_table[i].key;
89 accel16[i].cmd = accel_table[i].cmd;
91 accel16[i-1].fVirt |= 0x80;
94 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
98 /***********************************************************************
99 * LoadAcceleratorsA (USER32.355)
101 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
105 if (HIWORD(lpTableName))
106 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
108 uni = (LPWSTR)lpTableName;
109 result = LoadAcceleratorsW(instance,uni);
110 if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
114 /**********************************************************************
115 * CopyAcceleratorTableA (USER32.58)
117 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
119 return CopyAcceleratorTableW(src, dst, entries);
122 /**********************************************************************
123 * CopyAcceleratorTableW (USER32.59)
125 * By mortene@pvv.org 980321
127 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
131 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
134 /* Do parameter checking to avoid the explosions and the screaming
135 as far as possible. */
136 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
137 WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
138 (LPVOID)src, (LPVOID)dst, entries);
141 xsize = GlobalSize16(src)/sizeof(ACCEL16);
142 if (xsize>entries) entries=xsize;
146 /* Spit out some debugging information. */
147 TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
148 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
150 /* Copy data to the destination structure array (if dst == NULL,
151 we're just supposed to count the number of entries). */
153 dst[i].fVirt = accel[i].fVirt;
154 dst[i].key = accel[i].key;
155 dst[i].cmd = accel[i].cmd;
157 /* Check if we've reached the end of the application supplied
158 accelerator table. */
160 /* Turn off the high order bit, just in case. */
161 dst[i].fVirt &= 0x7f;
166 /* The highest order bit seems to mark the end of the accelerator
167 resource table, but not always. Use GlobalSize() check too. */
168 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
176 /*********************************************************************
177 * CreateAcceleratorTableA (USER32.64)
179 * By mortene@pvv.org 980321
181 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
187 /* Do parameter checking just in case someone's trying to be
190 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
192 SetLastError(ERROR_INVALID_PARAMETER);
195 FIXME_(accel)("should check that the accelerator descriptions are valid,"
196 " return NULL and SetLastError() if not.\n");
199 /* Allocate memory and copy the table. */
200 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
202 TRACE_(accel)("handle %x\n", hAccel);
204 ERR_(accel)("Out of memory.\n");
205 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
208 accel = GlobalLock16(hAccel);
209 for (i=0;i<cEntries;i++) {
210 accel[i].fVirt = lpaccel[i].fVirt;
211 accel[i].key = lpaccel[i].key;
212 accel[i].cmd = lpaccel[i].cmd;
214 /* Set the end-of-table terminator. */
215 accel[cEntries-1].fVirt |= 0x80;
217 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
221 /*********************************************************************
222 * CreateAcceleratorTableW (USER32.64)
226 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
233 /* Do parameter checking just in case someone's trying to be
236 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
238 SetLastError(ERROR_INVALID_PARAMETER);
241 FIXME_(accel)("should check that the accelerator descriptions are valid,"
242 " return NULL and SetLastError() if not.\n");
245 /* Allocate memory and copy the table. */
246 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
248 TRACE_(accel)("handle %x\n", hAccel);
250 ERR_(accel)("Out of memory.\n");
251 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
254 accel = GlobalLock16(hAccel);
257 for (i=0;i<cEntries;i++) {
258 accel[i].fVirt = lpaccel[i].fVirt;
259 if( !(accel[i].fVirt & FVIRTKEY) ) {
260 ckey = (char) lpaccel[i].key;
261 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
262 WARN_(accel)("Error converting ASCII accelerator table to Unicode");
265 accel[i].key = lpaccel[i].key;
266 accel[i].cmd = lpaccel[i].cmd;
269 /* Set the end-of-table terminator. */
270 accel[cEntries-1].fVirt |= 0x80;
272 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
276 /******************************************************************************
277 * DestroyAcceleratorTable [USER32.130]
278 * Destroys an accelerator table
281 * By mortene@pvv.org 980321
284 * handle [I] Handle to accelerator table
288 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
290 return !GlobalFree16(handle);
293 /**********************************************************************
294 * LoadString16 (USER.176)
296 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
297 LPSTR buffer, INT16 buflen )
305 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
306 instance, resource_id, (int) buffer, buflen);
308 hrsrc = FindResource16( instance, (LPCSTR)((resource_id>>4)+1), RT_STRINGA );
309 if (!hrsrc) return 0;
310 hmem = LoadResource16( instance, hrsrc );
313 p = LockResource16(hmem);
314 string_num = resource_id & 0x000f;
315 for (i = 0; i < string_num; i++)
318 TRACE("strlen = %d\n", (int)*p );
320 if (buffer == NULL) return *p;
321 i = min(buflen - 1, *p);
323 memcpy(buffer, p + 1, i);
330 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
332 FreeResource16( hmem );
334 TRACE("'%s' loaded !\n", buffer);
338 /**********************************************************************
339 * LoadStringW (USER32.376)
341 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
342 LPWSTR buffer, INT buflen )
350 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
351 resource_id = (UINT)(-((INT)resource_id));
352 TRACE("instance = %04x, id = %04x, buffer = %08x, "
353 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
355 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
357 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
359 if (!hrsrc) return 0;
360 hmem = LoadResource( instance, hrsrc );
363 p = LockResource(hmem);
364 string_num = resource_id & 0x000f;
365 for (i = 0; i < string_num; i++)
368 TRACE("strlen = %d\n", (int)*p );
370 if (buffer == NULL) return *p;
371 i = min(buflen - 1, *p);
373 memcpy(buffer, p + 1, i * sizeof (WCHAR));
374 buffer[i] = (WCHAR) 0;
377 buffer[0] = (WCHAR) 0;
381 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
385 TRACE("%s loaded !\n", debugstr_w(buffer));
389 /**********************************************************************
390 * LoadStringA (USER32.375)
392 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
393 LPSTR buffer, INT buflen )
398 TRACE("instance = %04x, id = %04x, buffer = %08x, "
399 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
401 if(buffer == NULL) /* asked size of string */
402 return LoadStringW(instance, resource_id, NULL, 0);
404 wbuf = HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(WCHAR));
408 retval = LoadStringW(instance, resource_id, wbuf, buflen);
411 retval = WideCharToMultiByte(CP_ACP, 0, wbuf, retval, buffer, buflen - 1, NULL, NULL);
413 TRACE("%s loaded !\n", debugstr_a(buffer));
415 else buffer[0] = 0; /* no check of buflen here */
416 HeapFree( GetProcessHeap(), 0, wbuf );