2 * USER resource functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/winbase16.h"
27 #include "wine/winuser16.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(resource);
31 WINE_DECLARE_DEBUG_CHANNEL(accel);
33 /* this is the 8 byte accel struct used in Win32 resources (internal only) */
41 } PE_ACCEL, *LPPE_ACCEL;
43 /**********************************************************************
44 * LoadAccelerators [USER.177]
46 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, LPCSTR lpTableName)
50 TRACE_(accel)("%04x %s\n", instance, debugres_a(lpTableName) );
52 if (!(hRsrc = FindResource16( instance, lpTableName, RT_ACCELERATORA ))) {
53 WARN_(accel)("couldn't find accelerator table resource\n");
57 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
58 return LoadResource16(instance,hRsrc);
61 /**********************************************************************
62 * LoadAcceleratorsW (USER32.@)
63 * The image layout seems to look like this (not 100% sure):
64 * 00: BYTE type type of accelerator
65 * 01: BYTE pad (to WORD boundary)
68 * 06: WORD pad (to DWORD boundary)
70 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
73 HACCEL hMem,hRetval=0;
76 if (HIWORD(lpTableName))
77 TRACE_(accel)("%p '%s'\n",
78 (LPVOID)instance, (char *)( lpTableName ) );
80 TRACE_(accel)("%p 0x%04x\n",
81 (LPVOID)instance, LOWORD(lpTableName) );
83 if (!(hRsrc = FindResourceW( instance, lpTableName, RT_ACCELERATORW )))
85 WARN_(accel)("couldn't find accelerator table resource\n");
87 hMem = LoadResource( instance, hRsrc );
88 size = SizeofResource( instance, hRsrc );
89 if(size>=sizeof(PE_ACCEL))
91 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
93 int i,nrofaccells = size/sizeof(PE_ACCEL);
95 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
96 accel16 = (LPACCEL16)GlobalLock16(hRetval);
97 for (i=0;i<nrofaccells;i++) {
98 accel16[i].fVirt = accel_table[i].fVirt;
99 accel16[i].key = accel_table[i].key;
100 accel16[i].cmd = accel_table[i].cmd;
102 accel16[i-1].fVirt |= 0x80;
105 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
109 /***********************************************************************
110 * LoadAcceleratorsA (USER32.@)
112 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
118 if (!HIWORD(lpTableName)) return LoadAcceleratorsW( instance, (LPCWSTR)lpTableName );
120 len = MultiByteToWideChar( CP_ACP, 0, lpTableName, -1, NULL, 0 );
121 if ((uni = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
123 MultiByteToWideChar( CP_ACP, 0, lpTableName, -1, uni, len );
124 result = LoadAcceleratorsW(instance,uni);
125 HeapFree( GetProcessHeap(), 0, uni);
130 /**********************************************************************
131 * CopyAcceleratorTableA (USER32.@)
133 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
135 return CopyAcceleratorTableW(src, dst, entries);
138 /**********************************************************************
139 * CopyAcceleratorTableW (USER32.@)
141 * By mortene@pvv.org 980321
143 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
147 LPACCEL16 accel = (LPACCEL16)GlobalLock16(src);
150 /* Do parameter checking to avoid the explosions and the screaming
151 as far as possible. */
152 if((dst && (entries < 1)) || (src == (HACCEL)NULL) || !accel) {
153 WARN_(accel)("Application sent invalid parameters (%p %p %d).\n",
154 (LPVOID)src, (LPVOID)dst, entries);
157 xsize = GlobalSize16(src)/sizeof(ACCEL16);
158 if (xsize>entries) entries=xsize;
162 /* Spit out some debugging information. */
163 TRACE_(accel)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
164 i, accel[i].fVirt, accel[i].key, accel[i].cmd);
166 /* Copy data to the destination structure array (if dst == NULL,
167 we're just supposed to count the number of entries). */
169 dst[i].fVirt = accel[i].fVirt;
170 dst[i].key = accel[i].key;
171 dst[i].cmd = accel[i].cmd;
173 /* Check if we've reached the end of the application supplied
174 accelerator table. */
176 /* Turn off the high order bit, just in case. */
177 dst[i].fVirt &= 0x7f;
182 /* The highest order bit seems to mark the end of the accelerator
183 resource table, but not always. Use GlobalSize() check too. */
184 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
192 /*********************************************************************
193 * CreateAcceleratorTableA (USER32.@)
195 * By mortene@pvv.org 980321
197 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
203 /* Do parameter checking just in case someone's trying to be
206 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
208 SetLastError(ERROR_INVALID_PARAMETER);
211 FIXME_(accel)("should check that the accelerator descriptions are valid,"
212 " return NULL and SetLastError() if not.\n");
215 /* Allocate memory and copy the table. */
216 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
218 TRACE_(accel)("handle %x\n", hAccel);
220 ERR_(accel)("Out of memory.\n");
221 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
224 accel = GlobalLock16(hAccel);
225 for (i=0;i<cEntries;i++) {
226 accel[i].fVirt = lpaccel[i].fVirt;
227 accel[i].key = lpaccel[i].key;
228 accel[i].cmd = lpaccel[i].cmd;
230 /* Set the end-of-table terminator. */
231 accel[cEntries-1].fVirt |= 0x80;
233 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
237 /*********************************************************************
238 * CreateAcceleratorTableW (USER32.@)
242 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
249 /* Do parameter checking just in case someone's trying to be
252 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
254 SetLastError(ERROR_INVALID_PARAMETER);
257 FIXME_(accel)("should check that the accelerator descriptions are valid,"
258 " return NULL and SetLastError() if not.\n");
261 /* Allocate memory and copy the table. */
262 hAccel = GlobalAlloc16(0,cEntries*sizeof(ACCEL16));
264 TRACE_(accel)("handle %x\n", hAccel);
266 ERR_(accel)("Out of memory.\n");
267 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
270 accel = GlobalLock16(hAccel);
273 for (i=0;i<cEntries;i++) {
274 accel[i].fVirt = lpaccel[i].fVirt;
275 if( !(accel[i].fVirt & FVIRTKEY) ) {
276 ckey = (char) lpaccel[i].key;
277 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
278 WARN_(accel)("Error converting ASCII accelerator table to Unicode");
281 accel[i].key = lpaccel[i].key;
282 accel[i].cmd = lpaccel[i].cmd;
285 /* Set the end-of-table terminator. */
286 accel[cEntries-1].fVirt |= 0x80;
288 TRACE_(accel)("Allocated accelerator handle %x\n", hAccel);
292 /******************************************************************************
293 * DestroyAcceleratorTable [USER32.@]
294 * Destroys an accelerator table
297 * By mortene@pvv.org 980321
300 * handle [I] Handle to accelerator table
304 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
306 return !GlobalFree16(handle);
309 /**********************************************************************
310 * LoadString (USER.176)
312 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
313 LPSTR buffer, INT16 buflen )
321 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
322 instance, resource_id, (int) buffer, buflen);
324 hrsrc = FindResource16( instance, (LPCSTR)((resource_id>>4)+1), RT_STRINGA );
325 if (!hrsrc) return 0;
326 hmem = LoadResource16( instance, hrsrc );
329 p = LockResource16(hmem);
330 string_num = resource_id & 0x000f;
331 for (i = 0; i < string_num; i++)
334 TRACE("strlen = %d\n", (int)*p );
336 if (buffer == NULL) return *p;
337 i = min(buflen - 1, *p);
339 memcpy(buffer, p + 1, i);
346 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
348 FreeResource16( hmem );
350 TRACE("'%s' loaded !\n", buffer);
354 /**********************************************************************
355 * LoadStringW (USER32.@)
357 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
358 LPWSTR buffer, INT buflen )
366 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
367 resource_id = (UINT)(-((INT)resource_id));
368 TRACE("instance = %04x, id = %04x, buffer = %08x, length = %d\n",
369 instance, (int)resource_id, (int) buffer, buflen);
371 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
373 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
375 if (!hrsrc) return 0;
376 hmem = LoadResource( instance, hrsrc );
379 p = LockResource(hmem);
380 string_num = resource_id & 0x000f;
381 for (i = 0; i < string_num; i++)
384 TRACE("strlen = %d\n", (int)*p );
386 if (buffer == NULL) return *p;
387 i = min(buflen - 1, *p);
389 memcpy(buffer, p + 1, i * sizeof (WCHAR));
390 buffer[i] = (WCHAR) 0;
393 buffer[0] = (WCHAR) 0;
397 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
401 TRACE("%s loaded !\n", debugstr_w(buffer));
405 /**********************************************************************
406 * LoadStringA (USER32.@)
408 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
409 LPSTR buffer, INT buflen )
414 TRACE("instance = %04x, id = %04x, buffer = %08x, length = %d\n",
415 instance, (int)resource_id, (int) buffer, buflen);
417 if(buffer == NULL) /* asked size of string */
418 return LoadStringW(instance, resource_id, NULL, 0);
420 wbuf = HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(WCHAR));
424 retval = LoadStringW(instance, resource_id, wbuf, buflen);
427 retval = WideCharToMultiByte(CP_ACP, 0, wbuf, retval, buffer, buflen - 1, NULL, NULL);
429 TRACE("%s loaded !\n", debugstr_a(buffer));
431 else buffer[0] = 0; /* no check of buflen here */
432 HeapFree( GetProcessHeap(), 0, wbuf );