Remove the win32 debug channel, misc cleanup of debugging code.
[wine/dcerpc.git] / dlls / user / resource.c
blob8e8150a1bf0ec67fe4ab486974fe9052067bd961
1 /*
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
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winnls.h"
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
30 #include "wownt32.h"
31 #include "user.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) */
38 typedef struct
40 BYTE fVirt;
41 BYTE pad0;
42 WORD key;
43 WORD cmd;
44 WORD pad1;
45 } PE_ACCEL, *LPPE_ACCEL;
47 /**********************************************************************
48 * LoadAccelerators [USER.177]
50 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, LPCSTR lpTableName)
52 HRSRC16 hRsrc;
54 TRACE_(accel)("%04x %s\n", instance, debugstr_a(lpTableName) );
56 if (!(hRsrc = FindResource16( instance, lpTableName, (LPSTR)RT_ACCELERATOR ))) {
57 WARN_(accel)("couldn't find accelerator table resource\n");
58 return 0;
61 TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
62 return LoadResource16(instance,hRsrc);
65 /**********************************************************************
66 * LoadAcceleratorsW (USER32.@)
67 * The image layout seems to look like this (not 100% sure):
68 * 00: BYTE type type of accelerator
69 * 01: BYTE pad (to WORD boundary)
70 * 02: WORD event
71 * 04: WORD IDval
72 * 06: WORD pad (to DWORD boundary)
74 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
76 HRSRC hRsrc;
77 HACCEL hMem;
78 HACCEL16 hRetval=0;
79 DWORD size;
81 if (HIWORD(lpTableName))
82 TRACE_(accel)("%p '%s'\n",
83 (LPVOID)instance, (char *)( lpTableName ) );
84 else
85 TRACE_(accel)("%p 0x%04x\n",
86 (LPVOID)instance, LOWORD(lpTableName) );
88 if (!(hRsrc = FindResourceW( instance, lpTableName, (LPWSTR)RT_ACCELERATOR )))
90 WARN_(accel)("couldn't find accelerator table resource\n");
91 } else {
92 hMem = LoadResource( instance, hRsrc );
93 size = SizeofResource( instance, hRsrc );
94 if(size>=sizeof(PE_ACCEL))
96 LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
97 LPACCEL16 accel16;
98 int i,nrofaccells = size/sizeof(PE_ACCEL);
100 hRetval = GlobalAlloc16(0,sizeof(ACCEL16)*nrofaccells);
101 accel16 = (LPACCEL16)GlobalLock16(hRetval);
102 for (i=0;i<nrofaccells;i++) {
103 accel16[i].fVirt = accel_table[i].fVirt;
104 accel16[i].key = accel_table[i].key;
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)
119 INT len;
120 LPWSTR uni;
121 HACCEL result = 0;
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);
132 return result;
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,
149 INT entries)
151 int i,xsize;
152 LPACCEL16 accel = (LPACCEL16)GlobalLock16(HACCEL_16(src));
153 BOOL done = FALSE;
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);
160 return 0;
162 xsize = GlobalSize16(HACCEL_16(src))/sizeof(ACCEL16);
163 if (xsize>entries) entries=xsize;
165 i=0;
166 while(!done) {
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). */
173 if(dst) {
174 dst[i].fVirt = accel[i].fVirt;
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. */
180 if(i+1 == entries) {
181 /* Turn off the high order bit, just in case. */
182 dst[i].fVirt &= 0x7f;
183 done = TRUE;
187 /* The highest order bit seems to mark the end of the accelerator
188 resource table, but not always. Use GlobalSize() check too. */
189 if((accel[i].fVirt & 0x80) != 0) done = TRUE;
191 i++;
194 return i;
197 /*********************************************************************
198 * CreateAcceleratorTableA (USER32.@)
200 * By mortene@pvv.org 980321
202 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
204 HACCEL hAccel;
205 LPACCEL16 accel;
206 int i;
208 /* Do parameter checking just in case someone's trying to be
209 funny. */
210 if(cEntries < 1) {
211 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
212 lpaccel, cEntries);
213 SetLastError(ERROR_INVALID_PARAMETER);
214 return NULL;
216 FIXME_(accel)("should check that the accelerator descriptions are valid,"
217 " return NULL and SetLastError() if not.\n");
220 /* Allocate memory and copy the table. */
221 hAccel = HACCEL_32(GlobalAlloc16(0,cEntries*sizeof(ACCEL16)));
223 TRACE_(accel)("handle %p\n", hAccel);
224 if(!hAccel) {
225 ERR_(accel)("Out of memory.\n");
226 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
227 return NULL;
229 accel = GlobalLock16(HACCEL_16(hAccel));
230 for (i=0;i<cEntries;i++) {
231 accel[i].fVirt = lpaccel[i].fVirt & ~0x80;
232 accel[i].key = lpaccel[i].key;
233 accel[i].cmd = lpaccel[i].cmd;
235 /* Set the end-of-table terminator. */
236 accel[cEntries-1].fVirt |= 0x80;
238 TRACE_(accel)("Allocated accelerator handle %p with %d entries\n", hAccel,cEntries);
239 return hAccel;
242 /*********************************************************************
243 * CreateAcceleratorTableW (USER32.@)
247 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
249 HACCEL hAccel;
250 LPACCEL16 accel;
251 int i;
252 char ckey;
254 /* Do parameter checking just in case someone's trying to be
255 funny. */
256 if(cEntries < 1) {
257 WARN_(accel)("Application sent invalid parameters (%p %d).\n",
258 lpaccel, cEntries);
259 SetLastError(ERROR_INVALID_PARAMETER);
260 return NULL;
262 FIXME_(accel)("should check that the accelerator descriptions are valid,"
263 " return NULL and SetLastError() if not.\n");
266 /* Allocate memory and copy the table. */
267 hAccel = HACCEL_32(GlobalAlloc16(0,cEntries*sizeof(ACCEL16)));
269 TRACE_(accel)("handle %p\n", hAccel);
270 if(!hAccel) {
271 ERR_(accel)("Out of memory.\n");
272 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
273 return NULL;
275 accel = GlobalLock16(HACCEL_16(hAccel));
278 for (i=0;i<cEntries;i++) {
279 accel[i].fVirt = lpaccel[i].fVirt;
280 if( !(accel[i].fVirt & FVIRTKEY) ) {
281 ckey = (char) lpaccel[i].key;
282 if(!MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, &ckey, 1, &accel[i].key, 1))
283 WARN_(accel)("Error converting ASCII accelerator table to Unicode\n");
285 else
286 accel[i].key = lpaccel[i].key;
287 accel[i].cmd = lpaccel[i].cmd;
290 /* Set the end-of-table terminator. */
291 accel[cEntries-1].fVirt |= 0x80;
293 TRACE_(accel)("Allocated accelerator handle %p\n", hAccel);
294 return hAccel;
297 /******************************************************************************
298 * DestroyAcceleratorTable [USER32.@]
299 * Destroys an accelerator table
301 * NOTES
302 * By mortene@pvv.org 980321
304 * PARAMS
305 * handle [I] Handle to accelerator table
307 * RETURNS STD
309 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
311 return !GlobalFree16(HACCEL_16(handle));
314 /**********************************************************************
315 * LoadString (USER.176)
317 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
318 LPSTR buffer, INT16 buflen )
320 HGLOBAL16 hmem;
321 HRSRC16 hrsrc;
322 unsigned char *p;
323 int string_num;
324 int i;
326 TRACE("inst=%04x id=%04x buff=%08x len=%d\n",
327 instance, resource_id, (int) buffer, buflen);
329 hrsrc = FindResource16( instance, (LPCSTR)((resource_id>>4)+1), (LPSTR)RT_STRING );
330 if (!hrsrc) return 0;
331 hmem = LoadResource16( instance, hrsrc );
332 if (!hmem) return 0;
334 p = LockResource16(hmem);
335 string_num = resource_id & 0x000f;
336 for (i = 0; i < string_num; i++)
337 p += *p + 1;
339 TRACE("strlen = %d\n", (int)*p );
341 if (buffer == NULL) return *p;
342 i = min(buflen - 1, *p);
343 if (i > 0) {
344 memcpy(buffer, p + 1, i);
345 buffer[i] = '\0';
346 } else {
347 if (buflen > 1) {
348 buffer[0] = '\0';
349 return 0;
351 WARN("Don't know why caller gave buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
353 FreeResource16( hmem );
355 TRACE("'%s' loaded !\n", buffer);
356 return i;
359 /**********************************************************************
360 * LoadStringW (USER32.@)
362 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
363 LPWSTR buffer, INT buflen )
365 HGLOBAL hmem;
366 HRSRC hrsrc;
367 WCHAR *p;
368 int string_num;
369 int i;
371 if (HIWORD(resource_id)==0xFFFF) /* netscape 3 passes this */
372 resource_id = (UINT)(-((INT)resource_id));
373 TRACE("instance = %p, id = %04x, buffer = %08x, length = %d\n",
374 instance, (int)resource_id, (int) buffer, buflen);
376 /* Use bits 4 - 19 (incremented by 1) as resourceid, mask out
377 * 20 - 31. */
378 hrsrc = FindResourceW( instance, (LPCWSTR)(((resource_id>>4)&0xffff)+1),
379 (LPWSTR)RT_STRING );
380 if (!hrsrc) return 0;
381 hmem = LoadResource( instance, hrsrc );
382 if (!hmem) return 0;
384 p = LockResource(hmem);
385 string_num = resource_id & 0x000f;
386 for (i = 0; i < string_num; i++)
387 p += *p + 1;
389 TRACE("strlen = %d\n", (int)*p );
391 if (buffer == NULL) return *p;
392 i = min(buflen - 1, *p);
393 if (i > 0) {
394 memcpy(buffer, p + 1, i * sizeof (WCHAR));
395 buffer[i] = (WCHAR) 0;
396 } else {
397 if (buflen > 1) {
398 buffer[0] = (WCHAR) 0;
399 return 0;
401 #if 0
402 WARN("Don't know why caller gave buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
403 #endif
406 TRACE("%s loaded !\n", debugstr_w(buffer));
407 return i;
410 /**********************************************************************
411 * LoadStringA (USER32.@)
413 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id,
414 LPSTR buffer, INT buflen )
416 INT retval;
417 LPWSTR wbuf;
419 TRACE("instance = %p, id = %04x, buffer = %08x, length = %d\n",
420 instance, (int)resource_id, (int) buffer, buflen);
422 if(buffer == NULL) /* asked size of string */
423 return LoadStringW(instance, resource_id, NULL, 0);
425 wbuf = HeapAlloc(GetProcessHeap(), 0, buflen * sizeof(WCHAR));
426 if(!wbuf)
427 return 0;
429 retval = LoadStringW(instance, resource_id, wbuf, buflen);
430 if(retval != 0)
432 retval = WideCharToMultiByte(CP_ACP, 0, wbuf, retval, buffer, buflen - 1, NULL, NULL);
433 buffer[retval] = 0;
434 TRACE("%s loaded !\n", debugstr_a(buffer));
436 else buffer[0] = 0; /* no check of buflen here */
437 HeapFree( GetProcessHeap(), 0, wbuf );
439 return retval;