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