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"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(resource);
19 DECLARE_DEBUG_CHANNEL(accel);
21 /* this is the 8 byte accel struct used in Win32 resources (internal only) */
29 } PE_ACCEL, *LPPE_ACCEL;
31 /**********************************************************************
32 * LoadAccelerators [USER.177]
34 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, LPCSTR lpTableName)
38 TRACE_(accel)("%04x %s\n", instance, debugres_a(lpTableName) );
40 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATORA ))) {
41 WARN_(accel)("couldn't find accelerator table resource\n");
45 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
46 return LoadResource16(instance,hRsrc);
49 /**********************************************************************
50 * LoadAcceleratorsW (USER32.@)
51 * The image layout seems to look like this (not 100% sure):
52 * 00: BYTE type type of accelerator
53 * 01: BYTE pad (to WORD boundary)
56 * 06: WORD pad (to DWORD boundary)
58 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
61 HACCEL hMem,hRetval=0;
64 if (HIWORD(lpTableName))
65 TRACE_(accel)("%p '%s'\n",
66 (LPVOID)instance, (char *)( lpTableName ) );
68 TRACE_(accel)("%p 0x%04x\n",
69 (LPVOID)instance, LOWORD(lpTableName) );
71 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
73 WARN_(accel)("couldn't find accelerator table resource\n");
75 hMem = LoadResource( instance, hRsrc );
76 size = SizeofResource( instance, hRsrc );
77 if(size>=sizeof(PE_ACCEL))
79 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
81 int i,nrofaccells = size/sizeof(PE_ACCEL);
83 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
84 accel16 = (LPACCEL16)GlobalLock16(hRetval);
85 for (i=0;i<nrofaccells;i++) {
86 accel16[i].fVirt = accel_table[i].fVirt;
87 accel16[i].key = accel_table[i].key;
88 accel16[i].cmd = accel_table[i].cmd;
90 accel16[i-1].fVirt |= 0x80;
93 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
97 /***********************************************************************
98 * LoadAcceleratorsA (USER32.@)
100 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
104 if (HIWORD(lpTableName))
105 uni = HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName );
107 uni = (LPWSTR)lpTableName;
108 result = LoadAcceleratorsW(instance,uni);
109 if (HIWORD(uni)) HeapFree( GetProcessHeap(), 0, uni);
113 /**********************************************************************
114 * CopyAcceleratorTableA (USER32.@)
116 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
118 return CopyAcceleratorTableW(src, dst, entries);
121 /**********************************************************************
122 * CopyAcceleratorTableW (USER32.@)
124 * By mortene@pvv.org 980321
126 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
130 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
133 /* Do parameter checking to avoid the explosions and the screaming
134 as far as possible. */
135 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
136 WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
137 (LPVOID)src, (LPVOID)dst, entries);
140 xsize = GlobalSize16(src)/sizeof(ACCEL16);
141 if (xsize>entries) entries=xsize;
145 /* Spit out some debugging information. */
146 TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
147 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
149 /* Copy data to the destination structure array (if dst == NULL,
150 we're just supposed to count the number of entries). */
152 dst[i].fVirt = accel[i].fVirt;
153 dst[i].key = accel[i].key;
154 dst[i].cmd = accel[i].cmd;
156 /* Check if we've reached the end of the application supplied
157 accelerator table. */
159 /* Turn off the high order bit, just in case. */
160 dst[i].fVirt &= 0x7f;
165 /* The highest order bit seems to mark the end of the accelerator
166 resource table, but not always. Use GlobalSize() check too. */
167 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
175 /*********************************************************************
176 * CreateAcceleratorTableA (USER32.@)
178 * By mortene@pvv.org 980321
180 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
186 /* Do parameter checking just in case someone's trying to be
189 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
191 SetLastError(ERROR_INVALID_PARAMETER);
194 FIXME_(accel)("should check that the accelerator descriptions are valid,"
195 " return NULL and SetLastError() if not.\n");
198 /* Allocate memory and copy the table. */
199 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
201 TRACE_(accel)("handle %x\n", hAccel);
203 ERR_(accel)("Out of memory.\n");
204 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
207 accel = GlobalLock16(hAccel);
208 for (i=0;i<cEntries;i++) {
209 accel[i].fVirt = lpaccel[i].fVirt;
210 accel[i].key = lpaccel[i].key;
211 accel[i].cmd = lpaccel[i].cmd;
213 /* Set the end-of-table terminator. */
214 accel[cEntries-1].fVirt |= 0x80;
216 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
220 /*********************************************************************
221 * CreateAcceleratorTableW (USER32.@)
225 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
232 /* Do parameter checking just in case someone's trying to be
235 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
237 SetLastError(ERROR_INVALID_PARAMETER);
240 FIXME_(accel)("should check that the accelerator descriptions are valid,"
241 " return NULL and SetLastError() if not.\n");
244 /* Allocate memory and copy the table. */
245 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
247 TRACE_(accel)("handle %x\n", hAccel);
249 ERR_(accel)("Out of memory.\n");
250 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
253 accel = GlobalLock16(hAccel);
256 for (i=0;i<cEntries;i++) {
257 accel[i].fVirt = lpaccel[i].fVirt;
258 if( !(accel[i].fVirt & FVIRTKEY) ) {
259 ckey = (char) lpaccel[i].key;
260 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
261 WARN_(accel)("Error converting ASCII accelerator table to Unicode");
264 accel[i].key = lpaccel[i].key;
265 accel[i].cmd = lpaccel[i].cmd;
268 /* Set the end-of-table terminator. */
269 accel[cEntries-1].fVirt |= 0x80;
271 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
275 /******************************************************************************
276 * DestroyAcceleratorTable [USER32.@]
277 * Destroys an accelerator table
280 * By mortene@pvv.org 980321
283 * handle [I] Handle to accelerator table
287 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
289 return !GlobalFree16(handle);
292 /**********************************************************************
293 * LoadString (USER.176)
295 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
296 LPSTR buffer, INT16 buflen )
304 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
305 instance, resource_id, (int) buffer, buflen);
307 hrsrc = FindResource16( instance, (LPCSTR)((resource_id>>4)+1), RT_STRINGA );
308 if (!hrsrc) return 0;
309 hmem = LoadResource16( instance, hrsrc );
312 p = LockResource16(hmem);
313 string_num = resource_id & 0x000f;
314 for (i = 0; i < string_num; i++)
317 TRACE("strlen = %d\n", (int)*p );
319 if (buffer == NULL) return *p;
320 i = min(buflen - 1, *p);
322 memcpy(buffer, p + 1, i);
329 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
331 FreeResource16( hmem );
333 TRACE("'%s' loaded !\n", buffer);
337 /**********************************************************************
338 * LoadStringW (USER32.@)
340 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
341 LPWSTR buffer, INT buflen )
349 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
350 resource_id = (UINT)(-((INT)resource_id));
351 TRACE("instance = %04x, id = %04x, buffer = %08x, length = %d\n",
352 instance, (int)resource_id, (int) buffer, buflen);
354 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
356 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
358 if (!hrsrc) return 0;
359 hmem = LoadResource( instance, hrsrc );
362 p = LockResource(hmem);
363 string_num = resource_id & 0x000f;
364 for (i = 0; i < string_num; i++)
367 TRACE("strlen = %d\n", (int)*p );
369 if (buffer == NULL) return *p;
370 i = min(buflen - 1, *p);
372 memcpy(buffer, p + 1, i * sizeof (WCHAR));
373 buffer[i] = (WCHAR) 0;
376 buffer[0] = (WCHAR) 0;
380 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
384 TRACE("%s loaded !\n", debugstr_w(buffer));
388 /**********************************************************************
389 * LoadStringA (USER32.@)
391 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
392 LPSTR buffer, INT buflen )
397 TRACE("instance = %04x, id = %04x, buffer = %08x, length = %d\n",
398 instance, (int)resource_id, (int) buffer, buflen);
400 if(buffer == NULL) /* asked size of string */
401 return LoadStringW(instance, resource_id, NULL, 0);
403 wbuf = HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(WCHAR));
407 retval = LoadStringW(instance, resource_id, wbuf, buflen);
410 retval = WideCharToMultiByte(CP_ACP, 0, wbuf, retval, buffer, buflen - 1, NULL, NULL);
412 TRACE("%s loaded !\n", debugstr_a(buffer));
414 else buffer[0] = 0; /* no check of buflen here */
415 HeapFree( GetProcessHeap(), 0, wbuf );