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 /**********************************************************************
23 * LoadAccelerators16 [USER.177]
25 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, SEGPTR lpTableName)
29 if (HIWORD(lpTableName))
30 TRACE_(accel)("%04x '%s'\n",
31 instance, (char *)PTR_SEG_TO_LIN( lpTableName ) );
33 TRACE_(accel)("%04x %04x\n",
34 instance, LOWORD(lpTableName) );
36 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATOR16 ))) {
37 WARN_(accel)("couldn't find accelerator table resource\n");
41 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
42 return LoadResource16(instance,hRsrc);
45 /**********************************************************************
46 * LoadAcceleratorsW (USER32.356)
47 * The image layout seems to look like this (not 100% sure):
48 * 00: BYTE type type of accelerator
49 * 01: BYTE pad (to WORD boundary)
52 * 06: WORD pad (to DWORD boundary)
54 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
57 HACCEL hMem,hRetval=0;
60 if (HIWORD(lpTableName))
61 TRACE_(accel)("%p '%s'\n",
62 (LPVOID)instance, (char *)( lpTableName ) );
64 TRACE_(accel)("%p 0x%04x\n",
65 (LPVOID)instance, LOWORD(lpTableName) );
67 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
69 WARN_(accel)("couldn't find accelerator table resource\n");
71 hMem = LoadResource( instance, hRsrc );
72 size = SizeofResource( instance, hRsrc );
73 if(size>=sizeof(PE_ACCEL))
75 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
77 int i,nrofaccells = size/sizeof(PE_ACCEL);
79 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
80 accel16 = (LPACCEL16)GlobalLock16(hRetval);
81 for (i=0;i<nrofaccells;i++) {
82 accel16[i].fVirt = accel_table[i].fVirt;
83 accel16[i].key = accel_table[i].key;
84 accel16[i].cmd = accel_table[i].cmd;
86 accel16[i-1].fVirt |= 0x80;
89 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
93 /***********************************************************************
94 * LoadAcceleratorsA (USER32.355)
96 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
100 if (HIWORD(lpTableName))
101 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
103 uni = (LPWSTR)lpTableName;
104 result = LoadAcceleratorsW(instance,uni);
105 if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
109 /**********************************************************************
110 * CopyAcceleratorTableA (USER32.58)
112 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
114 return CopyAcceleratorTableW(src, dst, entries);
117 /**********************************************************************
118 * CopyAcceleratorTableW (USER32.59)
120 * By mortene@pvv.org 980321
122 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
126 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
129 /* Do parameter checking to avoid the explosions and the screaming
130 as far as possible. */
131 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
132 WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
133 (LPVOID)src, (LPVOID)dst, entries);
136 xsize = GlobalSize16(src)/sizeof(ACCEL16);
137 if (xsize>entries) entries=xsize;
141 /* Spit out some debugging information. */
142 TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
143 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
145 /* Copy data to the destination structure array (if dst == NULL,
146 we're just supposed to count the number of entries). */
148 dst[i].fVirt = accel[i].fVirt;
149 dst[i].key = accel[i].key;
150 dst[i].cmd = accel[i].cmd;
152 /* Check if we've reached the end of the application supplied
153 accelerator table. */
155 /* Turn off the high order bit, just in case. */
156 dst[i].fVirt &= 0x7f;
161 /* The highest order bit seems to mark the end of the accelerator
162 resource table, but not always. Use GlobalSize() check too. */
163 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
171 /*********************************************************************
172 * CreateAcceleratorTableA (USER32.64)
174 * By mortene@pvv.org 980321
176 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
182 /* Do parameter checking just in case someone's trying to be
185 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
187 SetLastError(ERROR_INVALID_PARAMETER);
190 FIXME_(accel)("should check that the accelerator descriptions are valid,"
191 " return NULL and SetLastError() if not.\n");
194 /* Allocate memory and copy the table. */
195 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
197 TRACE_(accel)("handle %x\n", hAccel);
199 ERR_(accel)("Out of memory.\n");
200 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
203 accel = GlobalLock16(hAccel);
204 for (i=0;i<cEntries;i++) {
205 accel[i].fVirt = lpaccel[i].fVirt;
206 accel[i].key = lpaccel[i].key;
207 accel[i].cmd = lpaccel[i].cmd;
209 /* Set the end-of-table terminator. */
210 accel[cEntries-1].fVirt |= 0x80;
212 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
216 /*********************************************************************
217 * CreateAcceleratorTableW (USER32.64)
221 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
228 /* Do parameter checking just in case someone's trying to be
231 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
233 SetLastError(ERROR_INVALID_PARAMETER);
236 FIXME_(accel)("should check that the accelerator descriptions are valid,"
237 " return NULL and SetLastError() if not.\n");
240 /* Allocate memory and copy the table. */
241 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
243 TRACE_(accel)("handle %x\n", hAccel);
245 ERR_(accel)("Out of memory.\n");
246 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
249 accel = GlobalLock16(hAccel);
252 for (i=0;i<cEntries;i++) {
253 accel[i].fVirt = lpaccel[i].fVirt;
254 if( !(accel[i].fVirt & FVIRTKEY) ) {
255 ckey = (char) lpaccel[i].key;
256 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
257 WARN_(accel)("Error converting ASCII accelerator table to Unicode");
260 accel[i].key = lpaccel[i].key;
261 accel[i].cmd = lpaccel[i].cmd;
264 /* Set the end-of-table terminator. */
265 accel[cEntries-1].fVirt |= 0x80;
267 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
271 /******************************************************************************
272 * DestroyAcceleratorTable [USER32.130]
273 * Destroys an accelerator table
276 * By mortene@pvv.org 980321
279 * handle [I] Handle to accelerator table
283 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
285 return !GlobalFree16(handle);
288 /**********************************************************************
289 * LoadString16 (USER.176)
291 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
292 LPSTR buffer, INT16 buflen )
300 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
301 instance, resource_id, (int) buffer, buflen);
303 hrsrc = FindResource16( instance, (SEGPTR)((resource_id>>4)+1), RT_STRING16 );
304 if (!hrsrc) return 0;
305 hmem = LoadResource16( instance, hrsrc );
308 p = LockResource16(hmem);
309 string_num = resource_id & 0x000f;
310 for (i = 0; i < string_num; i++)
313 TRACE("strlen = %d\n", (int)*p );
315 if (buffer == NULL) return *p;
316 i = min(buflen - 1, *p);
318 memcpy(buffer, p + 1, i);
325 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
327 FreeResource16( hmem );
329 TRACE("'%s' loaded !\n", buffer);
333 /**********************************************************************
334 * LoadStringW (USER32.376)
336 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
337 LPWSTR buffer, INT buflen )
345 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
346 resource_id = (UINT)(-((INT)resource_id));
347 TRACE("instance = %04x, id = %04x, buffer = %08x, "
348 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
350 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
352 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
354 if (!hrsrc) return 0;
355 hmem = LoadResource( instance, hrsrc );
358 p = LockResource(hmem);
359 string_num = resource_id & 0x000f;
360 for (i = 0; i < string_num; i++)
363 TRACE("strlen = %d\n", (int)*p );
365 if (buffer == NULL) return *p;
366 i = min(buflen - 1, *p);
368 memcpy(buffer, p + 1, i * sizeof (WCHAR));
369 buffer[i] = (WCHAR) 0;
372 buffer[0] = (WCHAR) 0;
376 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
380 TRACE("%s loaded !\n", debugstr_w(buffer));
384 /**********************************************************************
385 * LoadStringA (USER32.375)
387 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
388 LPSTR buffer, INT buflen )
393 TRACE("instance = %04x, id = %04x, buffer = %08x, "
394 "length = %d\n", instance, (int)resource_id, (int) buffer, buflen);
396 if(buffer == NULL) /* asked size of string */
397 return LoadStringW(instance, resource_id, NULL, 0);
399 wbuf = HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(WCHAR));
403 retval = LoadStringW(instance, resource_id, wbuf, buflen);
406 retval = WideCharToMultiByte(CP_ACP, 0, wbuf, retval, buffer, buflen - 1, NULL, NULL);
408 TRACE("%s loaded !\n", debugstr_a(buffer));
410 HeapFree( GetProcessHeap(), 0, wbuf );