user32/tests: Run tests again on win95.
[wine] / dlls / user32 / resource.c
1 /*
2  * USER resource functions
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Alexandre Julliard
6  *
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.
11  *
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.
16  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winternl.h"
28 #include "winnls.h"
29 #include "wine/winbase16.h"
30 #include "wine/winuser16.h"
31 #include "wownt32.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(resource);
35 WINE_DECLARE_DEBUG_CHANNEL(accel);
36
37 /* this is the 8 byte accel struct used in Win32 resources (internal only) */
38 typedef struct
39 {
40     WORD   fVirt;
41     WORD   key;
42     WORD   cmd;
43     WORD   pad;
44 } PE_ACCEL, *LPPE_ACCEL;
45
46 /**********************************************************************
47  *                      LoadAccelerators        [USER.177]
48  */
49 HACCEL16 WINAPI LoadAccelerators16(HINSTANCE16 instance, LPCSTR lpTableName)
50 {
51     HRSRC16     hRsrc;
52
53     TRACE_(accel)("%04x %s\n", instance, debugstr_a(lpTableName) );
54
55     if (!(hRsrc = FindResource16( instance, lpTableName, (LPSTR)RT_ACCELERATOR ))) {
56       WARN_(accel)("couldn't find accelerator table resource\n");
57       return 0;
58     }
59
60     TRACE_(accel)("returning HACCEL 0x%x\n", hRsrc);
61     return LoadResource16(instance,hRsrc);
62 }
63
64 /**********************************************************************
65  *                      LoadAcceleratorsW       (USER32.@)
66  * The image layout seems to look like this (not 100% sure):
67  * 00:  WORD    type            type of accelerator
68  * 02:  WORD    event
69  * 04:  WORD    IDval
70  * 06:  WORD    pad             (to DWORD boundary)
71  */
72 HACCEL WINAPI LoadAcceleratorsW(HINSTANCE instance,LPCWSTR lpTableName)
73 {
74     HRSRC hRsrc;
75     HACCEL hMem;
76     HACCEL16 hRetval=0;
77     DWORD size;
78
79     if (HIWORD(lpTableName))
80         TRACE_(accel)("%p '%s'\n",
81                       (LPVOID)instance, (const char *)( lpTableName ) );
82     else
83         TRACE_(accel)("%p 0x%04x\n",
84                        (LPVOID)instance, LOWORD(lpTableName) );
85
86     if (!(hRsrc = FindResourceW( instance, lpTableName, (LPWSTR)RT_ACCELERATOR )))
87     {
88       WARN_(accel)("couldn't find accelerator table resource\n");
89     } else {
90       hMem = LoadResource( instance, hRsrc );
91       size = SizeofResource( instance, hRsrc );
92       if(size>=sizeof(PE_ACCEL))
93       {
94         LPPE_ACCEL accel_table = (LPPE_ACCEL) hMem;
95         LPACCEL16 accel16;
96         int i,nrofaccells = size/sizeof(PE_ACCEL);
97
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;
106         }
107         accel16[i-1].fVirt |= 0x80;
108       }
109     }
110     TRACE_(accel)("returning HACCEL %p\n", hRsrc);
111     return HACCEL_32(hRetval);
112 }
113
114 /***********************************************************************
115  *              LoadAcceleratorsA   (USER32.@)
116  */
117 HACCEL WINAPI LoadAcceleratorsA(HINSTANCE instance,LPCSTR lpTableName)
118 {
119     INT len;
120     LPWSTR uni;
121     HACCEL result = 0;
122
123     if (!HIWORD(lpTableName)) return LoadAcceleratorsW( instance, (LPCWSTR)lpTableName );
124
125     len = MultiByteToWideChar( CP_ACP, 0, lpTableName, -1, NULL, 0 );
126     if ((uni = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
127     {
128         MultiByteToWideChar( CP_ACP, 0, lpTableName, -1, uni, len );
129         result = LoadAcceleratorsW(instance,uni);
130         HeapFree( GetProcessHeap(), 0, uni);
131     }
132     return result;
133 }
134
135 /**********************************************************************
136  *             CopyAcceleratorTableA   (USER32.@)
137  */
138 INT WINAPI CopyAcceleratorTableA(HACCEL src, LPACCEL dst, INT entries)
139 {
140   return CopyAcceleratorTableW(src, dst, entries);
141 }
142
143 /**********************************************************************
144  *             CopyAcceleratorTableW   (USER32.@)
145  *
146  * By mortene@pvv.org 980321
147  */
148 INT WINAPI CopyAcceleratorTableW(HACCEL src, LPACCEL dst,
149                                      INT entries)
150 {
151   int i,xsize;
152   LPACCEL16 accel = (LPACCEL16)GlobalLock16(HACCEL_16(src));
153   BOOL done = FALSE;
154
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;
161   }
162   xsize = GlobalSize16(HACCEL_16(src))/sizeof(ACCEL16);
163   if (xsize<entries) entries=xsize;
164
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);
170
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&0x7f;
175       dst[i].key = accel[i].key;
176       dst[i].cmd = accel[i].cmd;
177
178       /* Check if we've reached the end of the application supplied
179          accelerator table. */
180       if(i+1 == entries)
181         done = TRUE;
182     }
183
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;
187
188     i++;
189   }
190
191   return i;
192 }
193
194 /*********************************************************************
195  *                    CreateAcceleratorTableA   (USER32.@)
196  *
197  * By mortene@pvv.org 980321
198  */
199 HACCEL WINAPI CreateAcceleratorTableA(LPACCEL lpaccel, INT cEntries)
200 {
201   HACCEL        hAccel;
202   LPACCEL16     accel;
203   int           i;
204
205   /* Do parameter checking just in case someone's trying to be
206      funny. */
207   if(cEntries < 1) {
208     WARN_(accel)("Application sent invalid parameters (%p %d).\n",
209          lpaccel, cEntries);
210     SetLastError(ERROR_INVALID_PARAMETER);
211     return NULL;
212   }
213
214   /* Allocate memory and copy the table. */
215   hAccel = HACCEL_32(GlobalAlloc16(0,cEntries*sizeof(ACCEL16)));
216
217   TRACE_(accel)("handle %p\n", hAccel);
218   if(!hAccel) {
219     ERR_(accel)("Out of memory.\n");
220     SetLastError(ERROR_NOT_ENOUGH_MEMORY);
221     return NULL;
222   }
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;
230   }
231   /* Set the end-of-table terminator. */
232   accel[cEntries-1].fVirt |= 0x80;
233
234   TRACE_(accel)("Allocated accelerator handle %p with %d entries\n", hAccel,cEntries);
235   return hAccel;
236 }
237
238 /*********************************************************************
239  *                    CreateAcceleratorTableW   (USER32.@)
240  *
241  *
242  */
243 HACCEL WINAPI CreateAcceleratorTableW(LPACCEL lpaccel, INT cEntries)
244 {
245   HACCEL        hAccel;
246   LPACCEL16     accel;
247   int           i;
248   char          ckey;
249
250   /* Do parameter checking just in case someone's trying to be
251      funny. */
252   if(cEntries < 1) {
253     WARN_(accel)("Application sent invalid parameters (%p %d).\n",
254          lpaccel, cEntries);
255     SetLastError(ERROR_INVALID_PARAMETER);
256     return NULL;
257   }
258
259   /* Allocate memory and copy the table. */
260   hAccel = HACCEL_32(GlobalAlloc16(0,cEntries*sizeof(ACCEL16)));
261
262   TRACE_(accel)("handle %p\n", hAccel);
263   if(!hAccel) {
264     ERR_(accel)("Out of memory.\n");
265     SetLastError(ERROR_NOT_ENOUGH_MEMORY);
266     return NULL;
267   }
268   accel = GlobalLock16(HACCEL_16(hAccel));
269
270
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");
277        }
278        else
279          accel[i].key = lpaccel[i].key;
280        accel[i].cmd = lpaccel[i].cmd;
281   }
282
283   /* Set the end-of-table terminator. */
284   accel[cEntries-1].fVirt |= 0x80;
285
286   TRACE_(accel)("Allocated accelerator handle %p\n", hAccel);
287   return hAccel;
288 }
289
290 /******************************************************************************
291  * DestroyAcceleratorTable [USER32.@]
292  * Destroys an accelerator table
293  *
294  * NOTES
295  *    By mortene@pvv.org 980321
296  *
297  * PARAMS
298  *    handle [I] Handle to accelerator table
299  *
300  * RETURNS
301  *    Success: TRUE
302  *    Failure: FALSE
303  */
304 BOOL WINAPI DestroyAcceleratorTable( HACCEL handle )
305 {
306     if( !handle )
307         return FALSE;
308     return !GlobalFree16(HACCEL_16(handle));
309 }
310
311 /**********************************************************************
312  *     LoadString   (USER.176)
313  */
314 INT16 WINAPI LoadString16( HINSTANCE16 instance, UINT16 resource_id,
315                            LPSTR buffer, INT16 buflen )
316 {
317     HGLOBAL16 hmem;
318     HRSRC16 hrsrc;
319     unsigned char *p;
320     int string_num;
321     int i;
322
323     TRACE("inst=%04x id=%04x buff=%p len=%d\n",
324           instance, resource_id, buffer, buflen);
325
326     hrsrc = FindResource16( instance, MAKEINTRESOURCEA((resource_id>>4)+1), (LPSTR)RT_STRING );
327     if (!hrsrc) return 0;
328     hmem = LoadResource16( instance, hrsrc );
329     if (!hmem) return 0;
330
331     p = LockResource16(hmem);
332     string_num = resource_id & 0x000f;
333     for (i = 0; i < string_num; i++)
334         p += *p + 1;
335
336     TRACE("strlen = %d\n", (int)*p );
337
338     if (buffer == NULL) return *p;
339     i = min(buflen - 1, *p);
340     if (i > 0) {
341         memcpy(buffer, p + 1, i);
342         buffer[i] = '\0';
343     } else {
344         if (buflen > 1) {
345             buffer[0] = '\0';
346             return 0;
347         }
348         WARN("Don't know why caller gave buflen=%d *p=%d trying to obtain string '%s'\n", buflen, *p, p + 1);
349     }
350     FreeResource16( hmem );
351
352     TRACE("'%s' loaded !\n", buffer);
353     return i;
354 }
355
356 /**********************************************************************
357  *      LoadStringW             (USER32.@)
358  */
359 INT WINAPI LoadStringW( HINSTANCE instance, UINT resource_id,
360                             LPWSTR buffer, INT buflen )
361 {
362     HGLOBAL hmem;
363     HRSRC hrsrc;
364     WCHAR *p;
365     int string_num;
366     int i;
367
368     TRACE("instance = %p, id = %04x, buffer = %p, length = %d\n",
369           instance, resource_id, buffer, buflen);
370
371     if(buffer == NULL)
372         return 0;
373
374     /* Use loword (incremented by 1) as resourceid */
375     hrsrc = FindResourceW( instance, MAKEINTRESOURCEW((LOWORD(resource_id) >> 4) + 1),
376                            (LPWSTR)RT_STRING );
377     if (!hrsrc) return 0;
378     hmem = LoadResource( instance, hrsrc );
379     if (!hmem) return 0;
380
381     p = LockResource(hmem);
382     string_num = resource_id & 0x000f;
383     for (i = 0; i < string_num; i++)
384         p += *p + 1;
385
386     TRACE("strlen = %d\n", (int)*p );
387
388     /*if buflen == 0, then return a read-only pointer to the resource itself in buffer
389     it is assumed that buffer is actually a (LPWSTR *) */
390     if(buflen == 0)
391     {
392         *((LPWSTR *)buffer) = p + 1;
393         return *p;
394     }
395
396     i = min(buflen - 1, *p);
397     if (i > 0) {
398         memcpy(buffer, p + 1, i * sizeof (WCHAR));
399         buffer[i] = (WCHAR) 0;
400     } else {
401         if (buflen > 1) {
402             buffer[0] = (WCHAR) 0;
403             return 0;
404         }
405     }
406
407     TRACE("%s loaded !\n", debugstr_w(buffer));
408     return i;
409 }
410
411 /**********************************************************************
412  *      LoadStringA     (USER32.@)
413  */
414 INT WINAPI LoadStringA( HINSTANCE instance, UINT resource_id, LPSTR buffer, INT buflen )
415 {
416     HGLOBAL hmem;
417     HRSRC hrsrc;
418     DWORD retval = 0;
419
420     TRACE("instance = %p, id = %04x, buffer = %p, length = %d\n",
421           instance, resource_id, buffer, buflen);
422
423     if (!buflen) return -1;
424
425     /* Use loword (incremented by 1) as resourceid */
426     if ((hrsrc = FindResourceW( instance, MAKEINTRESOURCEW((LOWORD(resource_id) >> 4) + 1),
427                                 (LPWSTR)RT_STRING )) &&
428         (hmem = LoadResource( instance, hrsrc )))
429     {
430         const WCHAR *p = LockResource(hmem);
431         unsigned int id = resource_id & 0x000f;
432
433         while (id--) p += *p + 1;
434
435         RtlUnicodeToMultiByteN( buffer, buflen - 1, &retval, p + 1, *p * sizeof(WCHAR) );
436     }
437     buffer[retval] = 0;
438     TRACE("returning %s\n", debugstr_a(buffer));
439     return retval;
440 }
441
442 /**********************************************************************
443  *      GetGuiResources (USER32.@)
444  */
445 DWORD WINAPI GetGuiResources( HANDLE hProcess, DWORD uiFlags )
446 {
447     FIXME("(%p,%x): stub\n",hProcess,uiFlags);
448     SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
449     return 0;
450 }