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