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
, SEGPTR lpTableName
)
39 if (HIWORD(lpTableName
))
40 TRACE_(accel
)("%04x '%s'\n",
41 instance
, (char *)PTR_SEG_TO_LIN( lpTableName
) );
43 TRACE_(accel
)("%04x %04x\n",
44 instance
, LOWORD(lpTableName
) );
46 if (!(hRsrc
= FindResource16( instance
, lpTableName
, RT_ACCELERATOR16
))) {
47 WARN_(accel
)("couldn't find accelerator table resource\n");
51 TRACE_(accel
)("returning HACCEL 0x%x\n", hRsrc
);
52 return LoadResource16(instance
,hRsrc
);
55 /**********************************************************************
56 * LoadAcceleratorsW (USER32.356)
57 * The image layout seems to look like this (not 100% sure):
58 * 00: BYTE type type of accelerator
59 * 01: BYTE pad (to WORD boundary)
62 * 06: WORD pad (to DWORD boundary)
64 HACCEL WINAPI
LoadAcceleratorsW(HINSTANCE instance
,LPCWSTR lpTableName
)
67 HACCEL hMem
,hRetval
=0;
70 if (HIWORD(lpTableName
))
71 TRACE_(accel
)("%p '%s'\n",
72 (LPVOID
)instance
, (char *)( lpTableName
) );
74 TRACE_(accel
)("%p 0x%04x\n",
75 (LPVOID
)instance
, LOWORD(lpTableName
) );
77 if (!(hRsrc
= FindResourceW( instance
, lpTableName
, RT_ACCELERATORW
)))
79 WARN_(accel
)("couldn't find accelerator table resource\n");
81 hMem
= LoadResource( instance
, hRsrc
);
82 size
= SizeofResource( instance
, hRsrc
);
83 if(size
>=sizeof(PE_ACCEL
))
85 LPPE_ACCEL accel_table
= (LPPE_ACCEL
) hMem
;
87 int i
,nrofaccells
= size
/sizeof(PE_ACCEL
);
89 hRetval
= GlobalAlloc16(0,sizeof(ACCEL16
)*nrofaccells
);
90 accel16
= (LPACCEL16
)GlobalLock16(hRetval
);
91 for (i
=0;i
<nrofaccells
;i
++) {
92 accel16
[i
].fVirt
= accel_table
[i
].fVirt
;
93 accel16
[i
].key
= accel_table
[i
].key
;
94 accel16
[i
].cmd
= accel_table
[i
].cmd
;
96 accel16
[i
-1].fVirt
|= 0x80;
99 TRACE_(accel
)("returning HACCEL 0x%x\n", hRsrc
);
103 /***********************************************************************
104 * LoadAcceleratorsA (USER32.355)
106 HACCEL WINAPI
LoadAcceleratorsA(HINSTANCE instance
,LPCSTR lpTableName
)
110 if (HIWORD(lpTableName
))
111 uni
= HEAP_strdupAtoW( GetProcessHeap(), 0, lpTableName
);
113 uni
= (LPWSTR
)lpTableName
;
114 result
= LoadAcceleratorsW(instance
,uni
);
115 if (HIWORD(uni
)) HeapFree( GetProcessHeap(), 0, uni
);
119 /**********************************************************************
120 * CopyAcceleratorTableA (USER32.58)
122 INT WINAPI
CopyAcceleratorTableA(HACCEL src
, LPACCEL dst
, INT entries
)
124 return CopyAcceleratorTableW(src
, dst
, entries
);
127 /**********************************************************************
128 * CopyAcceleratorTableW (USER32.59)
130 * By mortene@pvv.org 980321
132 INT WINAPI
CopyAcceleratorTableW(HACCEL src
, LPACCEL dst
,
136 LPACCEL16 accel
= (LPACCEL16
)GlobalLock16(src
);
139 /* Do parameter checking to avoid the explosions and the screaming
140 as far as possible. */
141 if((dst
&& (entries
< 1)) || (src
== (HACCEL
)NULL
) || !accel
) {
142 WARN_(accel
)("Application sent invalid parameters (%p %p %d).\n",
143 (LPVOID
)src
, (LPVOID
)dst
, entries
);
146 xsize
= GlobalSize16(src
)/sizeof(ACCEL16
);
147 if (xsize
>entries
) entries
=xsize
;
151 /* Spit out some debugging information. */
152 TRACE_(accel
)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
153 i
, accel
[i
].fVirt
, accel
[i
].key
, accel
[i
].cmd
);
155 /* Copy data to the destination structure array (if dst == NULL,
156 we're just supposed to count the number of entries). */
158 dst
[i
].fVirt
= accel
[i
].fVirt
;
159 dst
[i
].key
= accel
[i
].key
;
160 dst
[i
].cmd
= accel
[i
].cmd
;
162 /* Check if we've reached the end of the application supplied
163 accelerator table. */
165 /* Turn off the high order bit, just in case. */
166 dst
[i
].fVirt
&= 0x7f;
171 /* The highest order bit seems to mark the end of the accelerator
172 resource table, but not always. Use GlobalSize() check too. */
173 if((accel
[i
].fVirt
& 0x80) != 0) done
= TRUE
;
181 /*********************************************************************
182 * CreateAcceleratorTableA (USER32.64)
184 * By mortene@pvv.org 980321
186 HACCEL WINAPI
CreateAcceleratorTableA(LPACCEL lpaccel
, INT cEntries
)
192 /* Do parameter checking just in case someone's trying to be
195 WARN_(accel
)("Application sent invalid parameters (%p %d).\n",
197 SetLastError(ERROR_INVALID_PARAMETER
);
200 FIXME_(accel
)("should check that the accelerator descriptions are valid,"
201 " return NULL and SetLastError() if not.\n");
204 /* Allocate memory and copy the table. */
205 hAccel
= GlobalAlloc16(0,cEntries
*sizeof(ACCEL16
));
207 TRACE_(accel
)("handle %x\n", hAccel
);
209 ERR_(accel
)("Out of memory.\n");
210 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
213 accel
= GlobalLock16(hAccel
);
214 for (i
=0;i
<cEntries
;i
++) {
215 accel
[i
].fVirt
= lpaccel
[i
].fVirt
;
216 accel
[i
].key
= lpaccel
[i
].key
;
217 accel
[i
].cmd
= lpaccel
[i
].cmd
;
219 /* Set the end-of-table terminator. */
220 accel
[cEntries
-1].fVirt
|= 0x80;
222 TRACE_(accel
)("Allocated accelerator handle %x\n", hAccel
);
226 /*********************************************************************
227 * CreateAcceleratorTableW (USER32.64)
231 HACCEL WINAPI
CreateAcceleratorTableW(LPACCEL lpaccel
, INT cEntries
)
238 /* Do parameter checking just in case someone's trying to be
241 WARN_(accel
)("Application sent invalid parameters (%p %d).\n",
243 SetLastError(ERROR_INVALID_PARAMETER
);
246 FIXME_(accel
)("should check that the accelerator descriptions are valid,"
247 " return NULL and SetLastError() if not.\n");
250 /* Allocate memory and copy the table. */
251 hAccel
= GlobalAlloc16(0,cEntries
*sizeof(ACCEL16
));
253 TRACE_(accel
)("handle %x\n", hAccel
);
255 ERR_(accel
)("Out of memory.\n");
256 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
259 accel
= GlobalLock16(hAccel
);
262 for (i
=0;i
<cEntries
;i
++) {
263 accel
[i
].fVirt
= lpaccel
[i
].fVirt
;
264 if( !(accel
[i
].fVirt
& FVIRTKEY
) ) {
265 ckey
= (char) lpaccel
[i
].key
;
266 if(!MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, &ckey
, 1, &accel
[i
].key
, 1))
267 WARN_(accel
)("Error converting ASCII accelerator table to Unicode");
270 accel
[i
].key
= lpaccel
[i
].key
;
271 accel
[i
].cmd
= lpaccel
[i
].cmd
;
274 /* Set the end-of-table terminator. */
275 accel
[cEntries
-1].fVirt
|= 0x80;
277 TRACE_(accel
)("Allocated accelerator handle %x\n", hAccel
);
281 /******************************************************************************
282 * DestroyAcceleratorTable [USER32.130]
283 * Destroys an accelerator table
286 * By mortene@pvv.org 980321
289 * handle [I] Handle to accelerator table
293 BOOL WINAPI
DestroyAcceleratorTable( HACCEL handle
)
295 return !GlobalFree16(handle
);
298 /**********************************************************************
299 * LoadString16 (USER.176)
301 INT16 WINAPI
LoadString16( HINSTANCE16 instance
, UINT16 resource_id
,
302 LPSTR buffer
, INT16 buflen
)
310 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
311 instance
, resource_id
, (int) buffer
, buflen
);
313 hrsrc
= FindResource16( instance
, (SEGPTR
)((resource_id
>>4)+1), RT_STRING16
);
314 if (!hrsrc
) return 0;
315 hmem
= LoadResource16( instance
, hrsrc
);
318 p
= LockResource16(hmem
);
319 string_num
= resource_id
& 0x000f;
320 for (i
= 0; i
< string_num
; i
++)
323 TRACE("strlen = %d\n", (int)*p
);
325 if (buffer
== NULL
) return *p
;
326 i
= min(buflen
- 1, *p
);
328 memcpy(buffer
, p
+ 1, i
);
335 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen
, *p
, p
+ 1);
337 FreeResource16( hmem
);
339 TRACE("'%s' loaded !\n", buffer
);
343 /**********************************************************************
344 * LoadStringW (USER32.376)
346 INT WINAPI
LoadStringW( HINSTANCE instance
, UINT resource_id
,
347 LPWSTR buffer
, INT buflen
)
355 if (HIWORD(resource_id
)==0xFFFF) /* netscape 3 passes this */
356 resource_id
= (UINT
)(-((INT
)resource_id
));
357 TRACE("instance = %04x, id = %04x, buffer = %08x, "
358 "length = %d\n", instance
, (int)resource_id
, (int) buffer
, buflen
);
360 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
362 hrsrc
= FindResourceW( instance
, (LPCWSTR
)(((resource_id
>>4)&0xffff)+1),
364 if (!hrsrc
) return 0;
365 hmem
= LoadResource( instance
, hrsrc
);
368 p
= LockResource(hmem
);
369 string_num
= resource_id
& 0x000f;
370 for (i
= 0; i
< string_num
; i
++)
373 TRACE("strlen = %d\n", (int)*p
);
375 if (buffer
== NULL
) return *p
;
376 i
= min(buflen
- 1, *p
);
378 memcpy(buffer
, p
+ 1, i
* sizeof (WCHAR
));
379 buffer
[i
] = (WCHAR
) 0;
382 buffer
[0] = (WCHAR
) 0;
386 WARN("Dont know why caller give buflen=%d *p=%d trying to obtain string '%s'\n", buflen
, *p
, p
+ 1);
390 TRACE("%s loaded !\n", debugstr_w(buffer
));
394 /**********************************************************************
395 * LoadStringA (USER32.375)
397 INT WINAPI
LoadStringA( HINSTANCE instance
, UINT resource_id
,
398 LPSTR buffer
, INT buflen
)
403 TRACE("instance = %04x, id = %04x, buffer = %08x, "
404 "length = %d\n", instance
, (int)resource_id
, (int) buffer
, buflen
);
406 if(buffer
== NULL
) /* asked size of string */
407 return LoadStringW(instance
, resource_id
, NULL
, 0);
409 wbuf
= HeapAlloc(GetProcessHeap(), 0, buflen
* sizeof(WCHAR
));
413 retval
= LoadStringW(instance
, resource_id
, wbuf
, buflen
);
416 retval
= WideCharToMultiByte(CP_ACP
, 0, wbuf
, retval
, buffer
, buflen
- 1, NULL
, NULL
);
418 TRACE("%s loaded !\n", debugstr_a(buffer
));
420 else buffer
[0] = 0; /* no check of buflen here */
421 HeapFree( GetProcessHeap(), 0, wbuf
);