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
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(resource
);
35 WINE_DECLARE_DEBUG_CHANNEL(accel
);
37 /* this is the 8 byte accel struct used in Win32 resources (internal only) */
44 } PE_ACCEL
, *LPPE_ACCEL
;
46 /**********************************************************************
47 * LoadAccelerators [USER.177]
49 HACCEL16 WINAPI
LoadAccelerators16(HINSTANCE16 instance
, LPCSTR lpTableName
)
53 TRACE_(accel
)("%04x %s\n", instance
, debugstr_a(lpTableName
) );
55 if (!(hRsrc
= FindResource16( instance
, lpTableName
, (LPSTR
)RT_ACCELERATOR
))) {
56 WARN_(accel
)("couldn't find accelerator table resource\n");
60 TRACE_(accel
)("returning HACCEL 0x%x\n", hRsrc
);
61 return LoadResource16(instance
,hRsrc
);
64 /**********************************************************************
65 * LoadAcceleratorsW (USER32.@)
66 * The image layout seems to look like this (not 100% sure):
67 * 00: WORD type type of accelerator
70 * 06: WORD pad (to DWORD boundary)
72 HACCEL WINAPI
LoadAcceleratorsW(HINSTANCE instance
,LPCWSTR lpTableName
)
79 if (HIWORD(lpTableName
))
80 TRACE_(accel
)("%p '%s'\n",
81 (LPVOID
)instance
, (char *)( lpTableName
) );
83 TRACE_(accel
)("%p 0x%04x\n",
84 (LPVOID
)instance
, LOWORD(lpTableName
) );
86 if (!(hRsrc
= FindResourceW( instance
, lpTableName
, (LPWSTR
)RT_ACCELERATOR
)))
88 WARN_(accel
)("couldn't find accelerator table resource\n");
90 hMem
= LoadResource( instance
, hRsrc
);
91 size
= SizeofResource( instance
, hRsrc
);
92 if(size
>=sizeof(PE_ACCEL
))
94 LPPE_ACCEL accel_table
= (LPPE_ACCEL
) hMem
;
96 int i
,nrofaccells
= size
/sizeof(PE_ACCEL
);
98 hRetval
= GlobalAlloc16(0,sizeof(ACCEL16
)*nrofaccells
);
99 accel16
= (LPACCEL16
)GlobalLock16(hRetval
);
100 for (i
=0;i
<nrofaccells
;i
++) {
101 accel16
[i
].fVirt
= accel_table
[i
].fVirt
& 0x7f;
102 accel16
[i
].key
= accel_table
[i
].key
;
103 if( !(accel16
[i
].fVirt
& FVIRTKEY
) )
104 accel16
[i
].key
&= 0x00ff;
105 accel16
[i
].cmd
= accel_table
[i
].cmd
;
107 accel16
[i
-1].fVirt
|= 0x80;
110 TRACE_(accel
)("returning HACCEL %p\n", hRsrc
);
111 return HACCEL_32(hRetval
);
114 /***********************************************************************
115 * LoadAcceleratorsA (USER32.@)
117 HACCEL WINAPI
LoadAcceleratorsA(HINSTANCE instance
,LPCSTR lpTableName
)
123 if (!HIWORD(lpTableName
)) return LoadAcceleratorsW( instance
, (LPCWSTR
)lpTableName
);
125 len
= MultiByteToWideChar( CP_ACP
, 0, lpTableName
, -1, NULL
, 0 );
126 if ((uni
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
128 MultiByteToWideChar( CP_ACP
, 0, lpTableName
, -1, uni
, len
);
129 result
= LoadAcceleratorsW(instance
,uni
);
130 HeapFree( GetProcessHeap(), 0, uni
);
135 /**********************************************************************
136 * CopyAcceleratorTableA (USER32.@)
138 INT WINAPI
CopyAcceleratorTableA(HACCEL src
, LPACCEL dst
, INT entries
)
140 return CopyAcceleratorTableW(src
, dst
, entries
);
143 /**********************************************************************
144 * CopyAcceleratorTableW (USER32.@)
146 * By mortene@pvv.org 980321
148 INT WINAPI
CopyAcceleratorTableW(HACCEL src
, LPACCEL dst
,
152 LPACCEL16 accel
= (LPACCEL16
)GlobalLock16(HACCEL_16(src
));
155 /* Do parameter checking to avoid the explosions and the screaming
156 as far as possible. */
157 if((dst
&& (entries
< 1)) || (src
== NULL
) || !accel
) {
158 WARN_(accel
)("Application sent invalid parameters (%p %p %d).\n",
159 (LPVOID
)src
, (LPVOID
)dst
, entries
);
162 xsize
= GlobalSize16(HACCEL_16(src
))/sizeof(ACCEL16
);
163 if (xsize
<entries
) entries
=xsize
;
167 /* Spit out some debugging information. */
168 TRACE_(accel
)("accel %d: type 0x%02x, event '%c', IDval 0x%04x.\n",
169 i
, accel
[i
].fVirt
, accel
[i
].key
, accel
[i
].cmd
);
171 /* Copy data to the destination structure array (if dst == NULL,
172 we're just supposed to count the number of entries). */
174 dst
[i
].fVirt
= accel
[i
].fVirt
&0x7f;
175 dst
[i
].key
= accel
[i
].key
;
176 dst
[i
].cmd
= accel
[i
].cmd
;
178 /* Check if we've reached the end of the application supplied
179 accelerator table. */
184 /* The highest order bit seems to mark the end of the accelerator
185 resource table, but not always. Use GlobalSize() check too. */
186 if((accel
[i
].fVirt
& 0x80) != 0) done
= TRUE
;
194 /*********************************************************************
195 * CreateAcceleratorTableA (USER32.@)
197 * By mortene@pvv.org 980321
199 HACCEL WINAPI
CreateAcceleratorTableA(LPACCEL lpaccel
, INT cEntries
)
205 /* Do parameter checking just in case someone's trying to be
208 WARN_(accel
)("Application sent invalid parameters (%p %d).\n",
210 SetLastError(ERROR_INVALID_PARAMETER
);
214 /* Allocate memory and copy the table. */
215 hAccel
= HACCEL_32(GlobalAlloc16(0,cEntries
*sizeof(ACCEL16
)));
217 TRACE_(accel
)("handle %p\n", hAccel
);
219 ERR_(accel
)("Out of memory.\n");
220 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
223 accel
= GlobalLock16(HACCEL_16(hAccel
));
224 for (i
=0;i
<cEntries
;i
++) {
225 accel
[i
].fVirt
= lpaccel
[i
].fVirt
&0x7f;
226 accel
[i
].key
= lpaccel
[i
].key
;
227 if( !(accel
[i
].fVirt
& FVIRTKEY
) )
228 accel
[i
].key
&= 0x00ff;
229 accel
[i
].cmd
= lpaccel
[i
].cmd
;
231 /* Set the end-of-table terminator. */
232 accel
[cEntries
-1].fVirt
|= 0x80;
234 TRACE_(accel
)("Allocated accelerator handle %p with %d entries\n", hAccel
,cEntries
);
238 /*********************************************************************
239 * CreateAcceleratorTableW (USER32.@)
243 HACCEL WINAPI
CreateAcceleratorTableW(LPACCEL lpaccel
, INT cEntries
)
250 /* Do parameter checking just in case someone's trying to be
253 WARN_(accel
)("Application sent invalid parameters (%p %d).\n",
255 SetLastError(ERROR_INVALID_PARAMETER
);
259 /* Allocate memory and copy the table. */
260 hAccel
= HACCEL_32(GlobalAlloc16(0,cEntries
*sizeof(ACCEL16
)));
262 TRACE_(accel
)("handle %p\n", hAccel
);
264 ERR_(accel
)("Out of memory.\n");
265 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
268 accel
= GlobalLock16(HACCEL_16(hAccel
));
271 for (i
=0;i
<cEntries
;i
++) {
272 accel
[i
].fVirt
= lpaccel
[i
].fVirt
&0x7f;
273 if( !(accel
[i
].fVirt
& FVIRTKEY
) ) {
274 ckey
= (char) lpaccel
[i
].key
;
275 if(!MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, &ckey
, 1, &accel
[i
].key
, 1))
276 WARN_(accel
)("Error converting ASCII accelerator table to Unicode\n");
279 accel
[i
].key
= lpaccel
[i
].key
;
280 accel
[i
].cmd
= lpaccel
[i
].cmd
;
283 /* Set the end-of-table terminator. */
284 accel
[cEntries
-1].fVirt
|= 0x80;
286 TRACE_(accel
)("Allocated accelerator handle %p\n", hAccel
);
290 /******************************************************************************
291 * DestroyAcceleratorTable [USER32.@]
292 * Destroys an accelerator table
295 * By mortene@pvv.org 980321
298 * handle [I] Handle to accelerator table
302 BOOL WINAPI
DestroyAcceleratorTable( HACCEL handle
)
306 return !GlobalFree16(HACCEL_16(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), (LPSTR
)RT_STRING
);
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("Don't know why caller gave 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 = %p, 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("Don't know why caller gave 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 = %p, 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
);
437 /**********************************************************************
438 * GetGuiResources (USER32.@)
440 DWORD WINAPI
GetGuiResources( HANDLE hProcess
, DWORD uiFlags
)
442 FIXME("(%p,%lx): stub\n",hProcess
,uiFlags
);
443 SetLastError( ERROR_CALL_NOT_IMPLEMENTED
);