msvfw32: Convert the German resources to UTF-8.
[wine] / dlls / comctl32 / comctl32undoc.c
1 /*
2  * Undocumented functions from COMCTL32.DLL
3  *
4  * Copyright 1998 Eric Kohl
5  *           1998 Juergen Schmied <j.schmied@metronet.de>
6  *           2000 Eric Kohl for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  *
22  * NOTES
23  *     All of these functions are UNDOCUMENTED!! And I mean UNDOCUMENTED!!!!
24  *     Do NOT rely on names or contents of undocumented structures and types!!!
25  *     These functions are used by EXPLORER.EXE, IEXPLORE.EXE and
26  *     COMCTL32.DLL (internally).
27  *
28  */
29 #include "config.h"
30 #include "wine/port.h"
31
32 #include <stdarg.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <limits.h>
36
37 #define COBJMACROS
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
40
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wingdi.h"
44 #include "winuser.h"
45 #include "winnls.h"
46 #include "winreg.h"
47 #include "commctrl.h"
48 #include "objbase.h"
49 #include "winerror.h"
50
51 #include "wine/unicode.h"
52 #include "comctl32.h"
53
54 #include "wine/debug.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(commctrl);
57
58 static const WCHAR strMRUList[] = { 'M','R','U','L','i','s','t',0 };
59
60 /**************************************************************************
61  * Alloc [COMCTL32.71]
62  *
63  * Allocates memory block from the dll's private heap
64  *
65  * PARAMS
66  *     dwSize [I] size of the allocated memory block
67  *
68  * RETURNS
69  *     Success: pointer to allocated memory block
70  *     Failure: NULL
71  */
72 LPVOID WINAPI Alloc (DWORD dwSize)
73 {
74     return LocalAlloc( LMEM_ZEROINIT, dwSize );
75 }
76
77
78 /**************************************************************************
79  * ReAlloc [COMCTL32.72]
80  *
81  * Changes the size of an allocated memory block or allocates a memory
82  * block using the dll's private heap.
83  *
84  * PARAMS
85  *     lpSrc  [I] pointer to memory block which will be resized
86  *     dwSize [I] new size of the memory block.
87  *
88  * RETURNS
89  *     Success: pointer to the resized memory block
90  *     Failure: NULL
91  *
92  * NOTES
93  *     If lpSrc is a NULL-pointer, then ReAlloc allocates a memory
94  *     block like Alloc.
95  */
96 LPVOID WINAPI ReAlloc (LPVOID lpSrc, DWORD dwSize)
97 {
98     if (lpSrc)
99         return LocalReAlloc( lpSrc, dwSize, LMEM_ZEROINIT | LMEM_MOVEABLE );
100     else
101         return LocalAlloc( LMEM_ZEROINIT, dwSize);
102 }
103
104
105 /**************************************************************************
106  * Free [COMCTL32.73]
107  *
108  * Frees an allocated memory block from the dll's private heap.
109  *
110  * PARAMS
111  *     lpMem [I] pointer to memory block which will be freed
112  *
113  * RETURNS
114  *     Success: TRUE
115  *     Failure: FALSE
116  */
117 BOOL WINAPI Free (LPVOID lpMem)
118 {
119     return !LocalFree( lpMem );
120 }
121
122
123 /**************************************************************************
124  * GetSize [COMCTL32.74]
125  *
126  * Retrieves the size of the specified memory block from the dll's
127  * private heap.
128  *
129  * PARAMS
130  *     lpMem [I] pointer to an allocated memory block
131  *
132  * RETURNS
133  *     Success: size of the specified memory block
134  *     Failure: 0
135  */
136 DWORD WINAPI GetSize (LPVOID lpMem)
137 {
138     return LocalSize( lpMem );
139 }
140
141
142 /**************************************************************************
143  * MRU-Functions  {COMCTL32}
144  *
145  * NOTES
146  * The MRU-Api is a set of functions to manipulate lists of M.R.U. (Most Recently
147  * Used) items. It is an undocumented Api that is used (at least) by the shell
148  * and explorer to implement their recent documents feature.
149  *
150  * Since these functions are undocumented, they are unsupported by MS and
151  * may change at any time.
152  *
153  * Internally, the list is implemented as a last in, last out list of items
154  * persisted into the system registry under a caller chosen key. Each list
155  * item is given a one character identifier in the Ascii range from 'a' to
156  * '}'. A list of the identifiers in order from newest to oldest is stored
157  * under the same key in a value named "MRUList".
158  *
159  * Items are re-ordered by changing the order of the values in the MRUList
160  * value. When a new item is added, it becomes the new value of the oldest
161  * identifier, and that identifier is moved to the front of the MRUList value.
162  * 
163  * Wine stores MRU-lists in the same registry format as Windows, so when
164  * switching between the builtin and native comctl32.dll no problems or
165  * incompatibilities should occur.
166  *
167  * The following undocumented structure is used to create an MRU-list:
168  *|typedef INT (CALLBACK *MRUStringCmpFn)(LPCTSTR lhs, LPCTSTR rhs);
169  *|typedef INT (CALLBACK *MRUBinaryCmpFn)(LPCVOID lhs, LPCVOID rhs, DWORD length);
170  *|
171  *|typedef struct tagCREATEMRULIST
172  *|{
173  *|    DWORD   cbSize;
174  *|    DWORD   nMaxItems;
175  *|    DWORD   dwFlags;
176  *|    HKEY    hKey;
177  *|    LPTSTR  lpszSubKey;
178  *|    PROC    lpfnCompare;
179  *|} CREATEMRULIST, *LPCREATEMRULIST;
180  *
181  * MEMBERS
182  *  cbSize      [I] The size of the CREATEMRULIST structure. This must be set
183  *                  to sizeof(CREATEMRULIST) by the caller.
184  *  nMaxItems   [I] The maximum number of items allowed in the list. Because
185  *                  of the limited number of identifiers, this should be set to
186  *                  a value from 1 to 30 by the caller.
187  *  dwFlags     [I] If bit 0 is set, the list will be used to store binary
188  *                  data, otherwise it is assumed to store strings. If bit 1
189  *                  is set, every change made to the list will be reflected in
190  *                  the registry immediately, otherwise changes will only be
191  *                  written when the list is closed.
192  *  hKey        [I] The registry key that the list should be written under.
193  *                  This must be supplied by the caller.
194  *  lpszSubKey  [I] A caller supplied name of a subkey under hKey to write
195  *                  the list to. This may not be blank.
196  *  lpfnCompare [I] A caller supplied comparison function, which may be either
197  *                  an MRUStringCmpFn if dwFlags does not have bit 0 set, or a
198  *                  MRUBinaryCmpFn otherwise.
199  *
200  * FUNCTIONS
201  *  - Create an MRU-list with CreateMRUList() or CreateMRUListLazy().
202  *  - Add items to an MRU-list with AddMRUString() or AddMRUData().
203  *  - Remove items from an MRU-list with DelMRUString().
204  *  - Find data in an MRU-list with FindMRUString() or FindMRUData().
205  *  - Iterate through an MRU-list with EnumMRUList().
206  *  - Free an MRU-list with FreeMRUList().
207  */
208
209 typedef struct tagCREATEMRULISTA
210 {
211     DWORD  cbSize;
212     DWORD  nMaxItems;
213     DWORD  dwFlags;
214     HKEY   hKey;
215     LPSTR  lpszSubKey;
216     PROC   lpfnCompare;
217 } CREATEMRULISTA, *LPCREATEMRULISTA;
218
219 typedef struct tagCREATEMRULISTW
220 {
221     DWORD   cbSize;
222     DWORD   nMaxItems;
223     DWORD   dwFlags;
224     HKEY    hKey;
225     LPWSTR  lpszSubKey;
226     PROC    lpfnCompare;
227 } CREATEMRULISTW, *LPCREATEMRULISTW;
228
229 /* dwFlags */
230 #define MRUF_STRING_LIST  0 /* list will contain strings */
231 #define MRUF_BINARY_LIST  1 /* list will contain binary data */
232 #define MRUF_DELAYED_SAVE 2 /* only save list order to reg. is FreeMRUList */
233
234 /* If list is a string list lpfnCompare has the following prototype
235  * int CALLBACK MRUCompareString(LPCSTR s1, LPCSTR s2)
236  * for binary lists the prototype is
237  * int CALLBACK MRUCompareBinary(LPCVOID data1, LPCVOID data2, DWORD cbData)
238  * where cbData is the no. of bytes to compare.
239  * Need to check what return value means identical - 0?
240  */
241
242 typedef struct tagWINEMRUITEM
243 {
244     DWORD          size;        /* size of data stored               */
245     DWORD          itemFlag;    /* flags                             */
246     BYTE           datastart;
247 } WINEMRUITEM, *LPWINEMRUITEM;
248
249 /* itemFlag */
250 #define WMRUIF_CHANGED   0x0001 /* this dataitem changed             */
251
252 typedef struct tagWINEMRULIST
253 {
254     CREATEMRULISTW extview;     /* original create information       */
255     BOOL           isUnicode;   /* is compare fn Unicode */
256     DWORD          wineFlags;   /* internal flags                    */
257     DWORD          cursize;     /* current size of realMRU           */
258     LPWSTR         realMRU;     /* pointer to string of index names  */
259     LPWINEMRUITEM  *array;      /* array of pointers to data         */
260                                 /* in 'a' to 'z' order               */
261 } WINEMRULIST, *LPWINEMRULIST;
262
263 /* wineFlags */
264 #define WMRUF_CHANGED  0x0001   /* MRU list has changed              */
265
266 /**************************************************************************
267  *              MRU_SaveChanged (internal)
268  *
269  * Local MRU saving code
270  */
271 static void MRU_SaveChanged ( LPWINEMRULIST mp )
272 {
273     UINT i, err;
274     HKEY newkey;
275     WCHAR realname[2];
276     LPWINEMRUITEM witem;
277
278     /* or should we do the following instead of RegOpenKeyEx:
279      */
280
281     /* open the sub key */
282     if ((err = RegOpenKeyExW( mp->extview.hKey, mp->extview.lpszSubKey,
283                               0, KEY_WRITE, &newkey))) {
284         /* not present - what to do ??? */
285         ERR("Could not open key, error=%d, attempting to create\n",
286             err);
287         if ((err = RegCreateKeyExW( mp->extview.hKey, mp->extview.lpszSubKey,
288                                     0,
289                                     NULL,
290                                     REG_OPTION_NON_VOLATILE,
291                                     KEY_READ | KEY_WRITE,
292                                     0,
293                                     &newkey,
294                                     0))) {
295             ERR("failed to create key /%s/, err=%d\n",
296                 debugstr_w(mp->extview.lpszSubKey), err);
297             return;
298         }
299     }
300     if (mp->wineFlags & WMRUF_CHANGED) {
301         mp->wineFlags &= ~WMRUF_CHANGED;
302         err = RegSetValueExW(newkey, strMRUList, 0, REG_SZ, (LPBYTE)mp->realMRU,
303                              (strlenW(mp->realMRU) + 1)*sizeof(WCHAR));
304         if (err) {
305             ERR("error saving MRUList, err=%d\n", err);
306         }
307         TRACE("saving MRUList=/%s/\n", debugstr_w(mp->realMRU));
308     }
309     realname[1] = 0;
310     for(i=0; i<mp->cursize; i++) {
311         witem = mp->array[i];
312         if (witem->itemFlag & WMRUIF_CHANGED) {
313             witem->itemFlag &= ~WMRUIF_CHANGED;
314             realname[0] = 'a' + i;
315             err = RegSetValueExW(newkey, realname, 0,
316                                  (mp->extview.dwFlags & MRUF_BINARY_LIST) ?
317                                  REG_BINARY : REG_SZ,
318                                  &witem->datastart, witem->size);
319             if (err) {
320                 ERR("error saving /%s/, err=%d\n", debugstr_w(realname), err);
321             }
322             TRACE("saving value for name /%s/ size=%d\n",
323                   debugstr_w(realname), witem->size);
324         }
325     }
326     RegCloseKey( newkey );
327 }
328
329 /**************************************************************************
330  *              FreeMRUList [COMCTL32.152]
331  *
332  * Frees a most-recently-used items list.
333  *
334  * PARAMS
335  *     hMRUList [I] Handle to list.
336  *
337  * RETURNS
338  *     Nothing.
339  */
340 void WINAPI FreeMRUList (HANDLE hMRUList)
341 {
342     LPWINEMRULIST mp = hMRUList;
343     UINT i;
344
345     TRACE("(%p)\n", hMRUList);
346     if (!hMRUList)
347         return;
348
349     if (mp->wineFlags & WMRUF_CHANGED) {
350         /* need to open key and then save the info */
351         MRU_SaveChanged( mp );
352     }
353
354     for(i=0; i<mp->extview.nMaxItems; i++)
355         Free(mp->array[i]);
356
357     Free(mp->realMRU);
358     Free(mp->array);
359     Free(mp->extview.lpszSubKey);
360     Free(mp);
361 }
362
363
364 /**************************************************************************
365  *                  FindMRUData [COMCTL32.169]
366  *
367  * Searches binary list for item that matches lpData of length cbData.
368  * Returns position in list order 0 -> MRU and if lpRegNum != NULL then value
369  * corresponding to item's reg. name will be stored in it ('a' -> 0).
370  *
371  * PARAMS
372  *    hList [I] list handle
373  *    lpData [I] data to find
374  *    cbData [I] length of data
375  *    lpRegNum [O] position in registry (maybe NULL)
376  *
377  * RETURNS
378  *    Position in list 0 -> MRU.  -1 if item not found.
379  */
380 INT WINAPI FindMRUData (HANDLE hList, LPCVOID lpData, DWORD cbData,
381                         LPINT lpRegNum)
382 {
383     const WINEMRULIST *mp = hList;
384     INT ret;
385     UINT i;
386     LPSTR dataA = NULL;
387
388     if (!mp || !mp->extview.lpfnCompare)
389         return -1;
390
391     if(!(mp->extview.dwFlags & MRUF_BINARY_LIST) && !mp->isUnicode) {
392         DWORD len = WideCharToMultiByte(CP_ACP, 0, lpData, -1,
393                                         NULL, 0, NULL, NULL);
394         dataA = Alloc(len);
395         WideCharToMultiByte(CP_ACP, 0, lpData, -1, dataA, len, NULL, NULL);
396     }
397
398     for(i=0; i<mp->cursize; i++) {
399         if (mp->extview.dwFlags & MRUF_BINARY_LIST) {
400             if (!mp->extview.lpfnCompare(lpData, &mp->array[i]->datastart,
401                                          cbData))
402                 break;
403         }
404         else {
405             if(mp->isUnicode) {
406                 if (!mp->extview.lpfnCompare(lpData, &mp->array[i]->datastart))
407                     break;
408             } else {
409                 DWORD len = WideCharToMultiByte(CP_ACP, 0,
410                                                 (LPWSTR)&mp->array[i]->datastart, -1,
411                                                 NULL, 0, NULL, NULL);
412                 LPSTR itemA = Alloc(len);
413                 INT cmp;
414                 WideCharToMultiByte(CP_ACP, 0, (LPWSTR)&mp->array[i]->datastart, -1,
415                                     itemA, len, NULL, NULL);
416
417                 cmp = mp->extview.lpfnCompare(dataA, itemA);
418                 Free(itemA);
419                 if(!cmp)
420                     break;
421             }
422         }
423     }
424     Free(dataA);
425     if (i < mp->cursize)
426         ret = i;
427     else
428         ret = -1;
429     if (lpRegNum && (ret != -1))
430         *lpRegNum = 'a' + i;
431
432     TRACE("(%p, %p, %d, %p) returning %d\n",
433            hList, lpData, cbData, lpRegNum, ret);
434
435     return ret;
436 }
437
438
439 /**************************************************************************
440  *              AddMRUData [COMCTL32.167]
441  *
442  * Add item to MRU binary list.  If item already exists in list then it is
443  * simply moved up to the top of the list and not added again.  If list is
444  * full then the least recently used item is removed to make room.
445  *
446  * PARAMS
447  *     hList [I] Handle to list.
448  *     lpData [I] ptr to data to add.
449  *     cbData [I] no. of bytes of data.
450  *
451  * RETURNS
452  *     No. corresponding to registry name where value is stored 'a' -> 0 etc.
453  *     -1 on error.
454  */
455 INT WINAPI AddMRUData (HANDLE hList, LPCVOID lpData, DWORD cbData)
456 {
457     LPWINEMRULIST mp = hList;
458     LPWINEMRUITEM witem;
459     INT i, replace;
460
461     if ((replace = FindMRUData (hList, lpData, cbData, NULL)) >= 0) {
462         /* Item exists, just move it to the front */
463         LPWSTR pos = strchrW(mp->realMRU, replace + 'a');
464         while (pos > mp->realMRU)
465         {
466             pos[0] = pos[-1];
467             pos--;
468         }
469     }
470     else {
471         /* either add a new entry or replace oldest */
472         if (mp->cursize < mp->extview.nMaxItems) {
473             /* Add in a new item */
474             replace = mp->cursize;
475             mp->cursize++;
476         }
477         else {
478             /* get the oldest entry and replace data */
479             replace = mp->realMRU[mp->cursize - 1] - 'a';
480             Free(mp->array[replace]);
481         }
482
483         /* Allocate space for new item and move in the data */
484         mp->array[replace] = witem = Alloc(cbData + sizeof(WINEMRUITEM));
485         witem->itemFlag |= WMRUIF_CHANGED;
486         witem->size = cbData;
487         memcpy( &witem->datastart, lpData, cbData);
488
489         /* now rotate MRU list */
490         for(i=mp->cursize-1; i>=1; i--)
491             mp->realMRU[i] = mp->realMRU[i-1];
492     }
493
494     /* The new item gets the front spot */
495     mp->wineFlags |= WMRUF_CHANGED;
496     mp->realMRU[0] = replace + 'a';
497
498     TRACE("(%p, %p, %d) adding data, /%c/ now most current\n",
499           hList, lpData, cbData, replace+'a');
500
501     if (!(mp->extview.dwFlags & MRUF_DELAYED_SAVE)) {
502         /* save changed stuff right now */
503         MRU_SaveChanged( mp );
504     }
505
506     return replace;
507 }
508
509 /**************************************************************************
510  *              AddMRUStringW [COMCTL32.401]
511  *
512  * Add an item to an MRU string list.
513  *
514  * PARAMS
515  *     hList      [I] Handle to list.
516  *     lpszString [I] The string to add.
517  *
518  * RETURNS
519  *   Success: The number corresponding to the registry name where the string
520  *            has been stored (0 maps to 'a', 1 to 'b' and so on).
521  *   Failure: -1, if hList is NULL or memory allocation fails. If lpszString
522  *            is invalid, the function returns 0, and GetLastError() returns
523  *            ERROR_INVALID_PARAMETER. The last error value is set only in
524  *            this case.
525  *
526  * NOTES
527  *  -If lpszString exists in the list already, it is moved to the top of the
528  *   MRU list (it is not duplicated).
529  *  -If the list is full the least recently used list entry is replaced with
530  *   lpszString.
531  *  -If this function returns 0 you should check the last error value to
532  *   ensure the call really succeeded.
533  */
534 INT WINAPI AddMRUStringW(HANDLE hList, LPCWSTR lpszString)
535 {
536     TRACE("(%p,%s)\n", hList, debugstr_w(lpszString));
537
538     if (!hList)
539         return -1;
540
541     if (!lpszString || IsBadStringPtrW(lpszString, -1))
542     {
543         SetLastError(ERROR_INVALID_PARAMETER);
544         return 0;
545     }
546
547     return AddMRUData(hList, lpszString,
548                       (strlenW(lpszString) + 1) * sizeof(WCHAR));
549 }
550
551 /**************************************************************************
552  *              AddMRUStringA [COMCTL32.153]
553  *
554  * See AddMRUStringW.
555  */
556 INT WINAPI AddMRUStringA(HANDLE hList, LPCSTR lpszString)
557 {
558     DWORD len;
559     LPWSTR stringW;
560     INT ret;
561
562     TRACE("(%p,%s)\n", hList, debugstr_a(lpszString));
563
564     if (!hList)
565         return -1;
566
567     if (IsBadStringPtrA(lpszString, -1))
568     {
569         SetLastError(ERROR_INVALID_PARAMETER);
570         return 0;
571     }
572
573     len = MultiByteToWideChar(CP_ACP, 0, lpszString, -1, NULL, 0) * sizeof(WCHAR);
574     stringW = Alloc(len);
575     if (!stringW)
576         return -1;
577
578     MultiByteToWideChar(CP_ACP, 0, lpszString, -1, stringW, len/sizeof(WCHAR));
579     ret = AddMRUData(hList, stringW, len);
580     Free(stringW);
581     return ret;
582 }
583
584 /**************************************************************************
585  *              DelMRUString [COMCTL32.156]
586  *
587  * Removes item from either string or binary list (despite its name)
588  *
589  * PARAMS
590  *    hList [I] list handle
591  *    nItemPos [I] item position to remove 0 -> MRU
592  *
593  * RETURNS
594  *    TRUE if successful, FALSE if nItemPos is out of range.
595  */
596 BOOL WINAPI DelMRUString(HANDLE hList, INT nItemPos)
597 {
598     FIXME("(%p, %d): stub\n", hList, nItemPos);
599     return TRUE;
600 }
601
602 /**************************************************************************
603  *                  FindMRUStringW [COMCTL32.402]
604  *
605  * See FindMRUStringA.
606  */
607 INT WINAPI FindMRUStringW (HANDLE hList, LPCWSTR lpszString, LPINT lpRegNum)
608 {
609   return FindMRUData(hList, lpszString,
610                      (lstrlenW(lpszString) + 1) * sizeof(WCHAR), lpRegNum);
611 }
612
613 /**************************************************************************
614  *                  FindMRUStringA [COMCTL32.155]
615  *
616  * Searches string list for item that matches lpszString.
617  * Returns position in list order 0 -> MRU and if lpRegNum != NULL then value
618  * corresponding to item's reg. name will be stored in it ('a' -> 0).
619  *
620  * PARAMS
621  *    hList [I] list handle
622  *    lpszString [I] string to find
623  *    lpRegNum [O] position in registry (maybe NULL)
624  *
625  * RETURNS
626  *    Position in list 0 -> MRU.  -1 if item not found.
627  */
628 INT WINAPI FindMRUStringA (HANDLE hList, LPCSTR lpszString, LPINT lpRegNum)
629 {
630     DWORD len = MultiByteToWideChar(CP_ACP, 0, lpszString, -1, NULL, 0);
631     LPWSTR stringW = Alloc(len * sizeof(WCHAR));
632     INT ret;
633
634     MultiByteToWideChar(CP_ACP, 0, lpszString, -1, stringW, len);
635     ret = FindMRUData(hList, stringW, len * sizeof(WCHAR), lpRegNum);
636     Free(stringW);
637     return ret;
638 }
639
640 /*************************************************************************
641  *                 CreateMRUListLazy_common (internal)
642  */
643 static HANDLE CreateMRUListLazy_common(LPWINEMRULIST mp)
644 {
645     UINT i, err;
646     HKEY newkey;
647     DWORD datasize, dwdisp;
648     WCHAR realname[2];
649     LPWINEMRUITEM witem;
650     DWORD type;
651
652     /* get space to save indices that will turn into names
653      * but in order of most to least recently used
654      */
655     mp->realMRU = Alloc((mp->extview.nMaxItems + 2) * sizeof(WCHAR));
656
657     /* get space to save pointers to actual data in order of
658      * 'a' to 'z' (0 to n).
659      */
660     mp->array = Alloc(mp->extview.nMaxItems * sizeof(LPVOID));
661
662     /* open the sub key */
663     if ((err = RegCreateKeyExW( mp->extview.hKey, mp->extview.lpszSubKey,
664                                 0,
665                                 NULL,
666                                 REG_OPTION_NON_VOLATILE,
667                                 KEY_READ | KEY_WRITE,
668                                 0,
669                                 &newkey,
670                                 &dwdisp))) {
671         /* error - what to do ??? */
672         ERR("(%u %u %x %p %s %p): Could not open key, error=%d\n",
673             mp->extview.cbSize, mp->extview.nMaxItems, mp->extview.dwFlags,
674             mp->extview.hKey, debugstr_w(mp->extview.lpszSubKey),
675                                  mp->extview.lpfnCompare, err);
676         return 0;
677     }
678
679     /* get values from key 'MRUList' */
680     if (newkey) {
681         datasize = (mp->extview.nMaxItems + 1) * sizeof(WCHAR);
682         if((err=RegQueryValueExW( newkey, strMRUList, 0, &type,
683                                   (LPBYTE)mp->realMRU, &datasize))) {
684             /* not present - set size to 1 (will become 0 later) */
685             datasize = 1;
686             *mp->realMRU = 0;
687         }
688         else
689             datasize /= sizeof(WCHAR);
690
691         TRACE("MRU list = %s, datasize = %d\n", debugstr_w(mp->realMRU), datasize);
692
693         mp->cursize = datasize - 1;
694         /* datasize now has number of items in the MRUList */
695
696         /* get actual values for each entry */
697         realname[1] = 0;
698         for(i=0; i<mp->cursize; i++) {
699             realname[0] = 'a' + i;
700             if(RegQueryValueExW( newkey, realname, 0, &type, 0, &datasize)) {
701                 /* not present - what to do ??? */
702                 ERR("Key %s not found 1\n", debugstr_w(realname));
703             }
704             mp->array[i] = witem = Alloc(datasize + sizeof(WINEMRUITEM));
705             witem->size = datasize;
706             if(RegQueryValueExW( newkey, realname, 0, &type,
707                                  &witem->datastart, &datasize)) {
708                 /* not present - what to do ??? */
709                 ERR("Key %s not found 2\n", debugstr_w(realname));
710             }
711         }
712         RegCloseKey( newkey );
713     }
714     else
715         mp->cursize = 0;
716
717     TRACE("(%u %u %x %p %s %p): Current Size = %d\n",
718           mp->extview.cbSize, mp->extview.nMaxItems, mp->extview.dwFlags,
719           mp->extview.hKey, debugstr_w(mp->extview.lpszSubKey),
720           mp->extview.lpfnCompare, mp->cursize);
721     return mp;
722 }
723
724 /**************************************************************************
725  *                  CreateMRUListLazyW [COMCTL32.404]
726  *
727  * See CreateMRUListLazyA.
728  */
729 HANDLE WINAPI CreateMRUListLazyW (const CREATEMRULISTW *lpcml, DWORD dwParam2,
730                                   DWORD dwParam3, DWORD dwParam4)
731 {
732     LPWINEMRULIST mp;
733
734     /* Native does not check for a NULL lpcml */
735
736     if (lpcml->cbSize != sizeof(CREATEMRULISTW) || !lpcml->hKey ||
737         IsBadStringPtrW(lpcml->lpszSubKey, -1))
738         return NULL;
739
740     mp = Alloc(sizeof(WINEMRULIST));
741     memcpy(&mp->extview, lpcml, sizeof(CREATEMRULISTW));
742     mp->extview.lpszSubKey = Alloc((strlenW(lpcml->lpszSubKey) + 1) * sizeof(WCHAR));
743     strcpyW(mp->extview.lpszSubKey, lpcml->lpszSubKey);
744     mp->isUnicode = TRUE;
745
746     return CreateMRUListLazy_common(mp);
747 }
748
749 /**************************************************************************
750  *                  CreateMRUListLazyA [COMCTL32.157]
751  *
752  * Creates a most-recently-used list.
753  *
754  * PARAMS
755  *     lpcml    [I] ptr to CREATEMRULIST structure.
756  *     dwParam2 [I] Unknown
757  *     dwParam3 [I] Unknown
758  *     dwParam4 [I] Unknown
759  *
760  * RETURNS
761  *     Handle to MRU list.
762  */
763 HANDLE WINAPI CreateMRUListLazyA (const CREATEMRULISTA *lpcml, DWORD dwParam2,
764                                   DWORD dwParam3, DWORD dwParam4)
765 {
766     LPWINEMRULIST mp;
767     DWORD len;
768
769     /* Native does not check for a NULL lpcml */
770
771     if (lpcml->cbSize != sizeof(CREATEMRULISTA) || !lpcml->hKey ||
772         IsBadStringPtrA(lpcml->lpszSubKey, -1))
773         return 0;
774
775     mp = Alloc(sizeof(WINEMRULIST));
776     memcpy(&mp->extview, lpcml, sizeof(CREATEMRULISTW));
777     len = MultiByteToWideChar(CP_ACP, 0, lpcml->lpszSubKey, -1, NULL, 0);
778     mp->extview.lpszSubKey = Alloc(len * sizeof(WCHAR));
779     MultiByteToWideChar(CP_ACP, 0, lpcml->lpszSubKey, -1,
780                         mp->extview.lpszSubKey, len);
781     mp->isUnicode = FALSE;
782     return CreateMRUListLazy_common(mp);
783 }
784
785 /**************************************************************************
786  *              CreateMRUListW [COMCTL32.400]
787  *
788  * See CreateMRUListA.
789  */
790 HANDLE WINAPI CreateMRUListW (const CREATEMRULISTW *lpcml)
791 {
792     return CreateMRUListLazyW(lpcml, 0, 0, 0);
793 }
794
795 /**************************************************************************
796  *              CreateMRUListA [COMCTL32.151]
797  *
798  * Creates a most-recently-used list.
799  *
800  * PARAMS
801  *     lpcml [I] ptr to CREATEMRULIST structure.
802  *
803  * RETURNS
804  *     Handle to MRU list.
805  */
806 HANDLE WINAPI CreateMRUListA (const CREATEMRULISTA *lpcml)
807 {
808      return CreateMRUListLazyA (lpcml, 0, 0, 0);
809 }
810
811
812 /**************************************************************************
813  *                EnumMRUListW [COMCTL32.403]
814  *
815  * Enumerate item in a most-recently-used list
816  *
817  * PARAMS
818  *    hList [I] list handle
819  *    nItemPos [I] item position to enumerate
820  *    lpBuffer [O] buffer to receive item
821  *    nBufferSize [I] size of buffer
822  *
823  * RETURNS
824  *    For binary lists specifies how many bytes were copied to buffer, for
825  *    string lists specifies full length of string.  Enumerating past the end
826  *    of list returns -1.
827  *    If lpBuffer == NULL or nItemPos is -ve return value is no. of items in
828  *    the list.
829  */
830 INT WINAPI EnumMRUListW (HANDLE hList, INT nItemPos, LPVOID lpBuffer,
831                          DWORD nBufferSize)
832 {
833     const WINEMRULIST *mp = hList;
834     const WINEMRUITEM *witem;
835     INT desired, datasize;
836
837     if (!mp) return -1;
838     if ((nItemPos < 0) || !lpBuffer) return mp->cursize;
839     if (nItemPos >= mp->cursize) return -1;
840     desired = mp->realMRU[nItemPos];
841     desired -= 'a';
842     TRACE("nItemPos=%d, desired=%d\n", nItemPos, desired);
843     witem = mp->array[desired];
844     datasize = min( witem->size, nBufferSize );
845     memcpy( lpBuffer, &witem->datastart, datasize);
846     TRACE("(%p, %d, %p, %d): returning len=%d\n",
847           hList, nItemPos, lpBuffer, nBufferSize, datasize);
848     return datasize;
849 }
850
851 /**************************************************************************
852  *                EnumMRUListA [COMCTL32.154]
853  *
854  * See EnumMRUListW.
855  */
856 INT WINAPI EnumMRUListA (HANDLE hList, INT nItemPos, LPVOID lpBuffer,
857                          DWORD nBufferSize)
858 {
859     const WINEMRULIST *mp = hList;
860     LPWINEMRUITEM witem;
861     INT desired, datasize;
862     DWORD lenA;
863
864     if (!mp) return -1;
865     if ((nItemPos < 0) || !lpBuffer) return mp->cursize;
866     if (nItemPos >= mp->cursize) return -1;
867     desired = mp->realMRU[nItemPos];
868     desired -= 'a';
869     TRACE("nItemPos=%d, desired=%d\n", nItemPos, desired);
870     witem = mp->array[desired];
871     if(mp->extview.dwFlags & MRUF_BINARY_LIST) {
872         datasize = min( witem->size, nBufferSize );
873         memcpy( lpBuffer, &witem->datastart, datasize);
874     } else {
875         lenA = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)&witem->datastart, -1,
876                                    NULL, 0, NULL, NULL);
877         datasize = min( witem->size, nBufferSize );
878         WideCharToMultiByte(CP_ACP, 0, (LPWSTR)&witem->datastart, -1,
879                             lpBuffer, datasize, NULL, NULL);
880     }
881     TRACE("(%p, %d, %p, %d): returning len=%d\n",
882           hList, nItemPos, lpBuffer, nBufferSize, datasize);
883     return datasize;
884 }
885
886 /**************************************************************************
887  * Str_GetPtrWtoA [internal]
888  *
889  * Converts a unicode string into a multi byte string
890  *
891  * PARAMS
892  *     lpSrc   [I] Pointer to the unicode source string
893  *     lpDest  [O] Pointer to caller supplied storage for the multi byte string
894  *     nMaxLen [I] Size, in bytes, of the destination buffer
895  *
896  * RETURNS
897  *     Length, in bytes, of the converted string.
898  */
899
900 INT Str_GetPtrWtoA (LPCWSTR lpSrc, LPSTR lpDest, INT nMaxLen)
901 {
902     INT len;
903
904     TRACE("(%s %p %d)\n", debugstr_w(lpSrc), lpDest, nMaxLen);
905
906     if (!lpDest && lpSrc)
907         return WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, 0, 0, NULL, NULL);
908
909     if (nMaxLen == 0)
910         return 0;
911
912     if (lpSrc == NULL) {
913         lpDest[0] = '\0';
914         return 0;
915     }
916
917     len = WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, 0, 0, NULL, NULL);
918     if (len >= nMaxLen)
919         len = nMaxLen - 1;
920
921     WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, lpDest, len, NULL, NULL);
922     lpDest[len] = '\0';
923
924     return len;
925 }
926
927 /**************************************************************************
928  * Str_GetPtrAtoW [internal]
929  *
930  * Converts a multibyte string into a unicode string
931  *
932  * PARAMS
933  *     lpSrc   [I] Pointer to the multibyte source string
934  *     lpDest  [O] Pointer to caller supplied storage for the unicode string
935  *     nMaxLen [I] Size, in characters, of the destination buffer
936  *
937  * RETURNS
938  *     Length, in characters, of the converted string.
939  */
940
941 INT Str_GetPtrAtoW (LPCSTR lpSrc, LPWSTR lpDest, INT nMaxLen)
942 {
943     INT len;
944
945     TRACE("(%s %p %d)\n", debugstr_a(lpSrc), lpDest, nMaxLen);
946
947     if (!lpDest && lpSrc)
948         return MultiByteToWideChar(CP_ACP, 0, lpSrc, -1, 0, 0);
949
950     if (nMaxLen == 0)
951         return 0;
952
953     if (lpSrc == NULL) {
954         lpDest[0] = '\0';
955         return 0;
956     }
957
958     len = MultiByteToWideChar(CP_ACP, 0, lpSrc, -1, 0, 0);
959     if (len >= nMaxLen)
960         len = nMaxLen - 1;
961
962     MultiByteToWideChar(CP_ACP, 0, lpSrc, -1, lpDest, len);
963     lpDest[len] = '\0';
964
965     return len;
966 }
967
968
969 /**************************************************************************
970  * Str_SetPtrAtoW [internal]
971  *
972  * Converts a multi byte string to a unicode string.
973  * If the pointer to the destination buffer is NULL a buffer is allocated.
974  * If the destination buffer is too small to keep the converted multi byte
975  * string the destination buffer is reallocated. If the source pointer is
976  * NULL, the destination buffer is freed.
977  *
978  * PARAMS
979  *     lppDest [I/O] pointer to a pointer to the destination buffer
980  *     lpSrc   [I] pointer to a multi byte string
981  *
982  * RETURNS
983  *     TRUE: conversion successful
984  *     FALSE: error
985  */
986 BOOL Str_SetPtrAtoW (LPWSTR *lppDest, LPCSTR lpSrc)
987 {
988     TRACE("(%p %s)\n", lppDest, lpSrc);
989
990     if (lpSrc) {
991         INT len = MultiByteToWideChar(CP_ACP,0,lpSrc,-1,NULL,0);
992         LPWSTR ptr = ReAlloc (*lppDest, len*sizeof(WCHAR));
993
994         if (!ptr)
995             return FALSE;
996         MultiByteToWideChar(CP_ACP,0,lpSrc,-1,ptr,len);
997         *lppDest = ptr;
998     }
999     else {
1000         Free (*lppDest);
1001         *lppDest = NULL;
1002     }
1003
1004     return TRUE;
1005 }
1006
1007 /**************************************************************************
1008  * Str_SetPtrWtoA [internal]
1009  *
1010  * Converts a unicode string to a multi byte string.
1011  * If the pointer to the destination buffer is NULL a buffer is allocated.
1012  * If the destination buffer is too small to keep the converted wide
1013  * string the destination buffer is reallocated. If the source pointer is
1014  * NULL, the destination buffer is freed.
1015  *
1016  * PARAMS
1017  *     lppDest [I/O] pointer to a pointer to the destination buffer
1018  *     lpSrc   [I] pointer to a wide string
1019  *
1020  * RETURNS
1021  *     TRUE: conversion successful
1022  *     FALSE: error
1023  */
1024 BOOL Str_SetPtrWtoA (LPSTR *lppDest, LPCWSTR lpSrc)
1025 {
1026     TRACE("(%p %s)\n", lppDest, debugstr_w(lpSrc));
1027
1028     if (lpSrc) {
1029         INT len = WideCharToMultiByte(CP_ACP,0,lpSrc,-1,NULL,0,NULL,FALSE);
1030         LPSTR ptr = ReAlloc (*lppDest, len*sizeof(CHAR));
1031
1032         if (!ptr)
1033             return FALSE;
1034         WideCharToMultiByte(CP_ACP,0,lpSrc,-1,ptr,len,NULL,FALSE);
1035         *lppDest = ptr;
1036     }
1037     else {
1038         Free (*lppDest);
1039         *lppDest = NULL;
1040     }
1041
1042     return TRUE;
1043 }
1044
1045
1046 /**************************************************************************
1047  * Notification functions
1048  */
1049
1050 typedef struct tagNOTIFYDATA
1051 {
1052     HWND hwndFrom;
1053     HWND hwndTo;
1054     DWORD  dwParam3;
1055     DWORD  dwParam4;
1056     DWORD  dwParam5;
1057     DWORD  dwParam6;
1058 } NOTIFYDATA, *LPNOTIFYDATA;
1059
1060
1061 /**************************************************************************
1062  * DoNotify [Internal]
1063  */
1064
1065 static LRESULT DoNotify (const NOTIFYDATA *lpNotify, UINT uCode, LPNMHDR lpHdr)
1066 {
1067     NMHDR nmhdr;
1068     LPNMHDR lpNmh = NULL;
1069     UINT idFrom = 0;
1070
1071     TRACE("(%p %p %d %p 0x%08x)\n",
1072            lpNotify->hwndFrom, lpNotify->hwndTo, uCode, lpHdr,
1073            lpNotify->dwParam5);
1074
1075     if (!lpNotify->hwndTo)
1076         return 0;
1077
1078     if (lpNotify->hwndFrom == (HWND)-1) {
1079         lpNmh = lpHdr;
1080         idFrom = lpHdr->idFrom;
1081     }
1082     else {
1083         if (lpNotify->hwndFrom)
1084             idFrom = GetDlgCtrlID (lpNotify->hwndFrom);
1085
1086         lpNmh = (lpHdr) ? lpHdr : &nmhdr;
1087
1088         lpNmh->hwndFrom = lpNotify->hwndFrom;
1089         lpNmh->idFrom = idFrom;
1090         lpNmh->code = uCode;
1091     }
1092
1093     return SendMessageW (lpNotify->hwndTo, WM_NOTIFY, idFrom, (LPARAM)lpNmh);
1094 }
1095
1096
1097 /**************************************************************************
1098  * SendNotify [COMCTL32.341]
1099  *
1100  * Sends a WM_NOTIFY message to the specified window.
1101  *
1102  * PARAMS
1103  *     hwndTo   [I] Window to receive the message
1104  *     hwndFrom [I] Window that the message is from (see notes)
1105  *     uCode    [I] Notification code
1106  *     lpHdr    [I] The NMHDR and any additional information to send or NULL
1107  *
1108  * RETURNS
1109  *     Success: return value from notification
1110  *     Failure: 0
1111  *
1112  * NOTES
1113  *     If hwndFrom is -1 then the identifier of the control sending the
1114  *     message is taken from the NMHDR structure.
1115  *     If hwndFrom is not -1 then lpHdr can be NULL.
1116  */
1117 LRESULT WINAPI SendNotify (HWND hwndTo, HWND hwndFrom, UINT uCode, LPNMHDR lpHdr)
1118 {
1119     NOTIFYDATA notify;
1120
1121     TRACE("(%p %p %d %p)\n",
1122            hwndTo, hwndFrom, uCode, lpHdr);
1123
1124     notify.hwndFrom = hwndFrom;
1125     notify.hwndTo   = hwndTo;
1126     notify.dwParam5 = 0;
1127     notify.dwParam6 = 0;
1128
1129     return DoNotify (&notify, uCode, lpHdr);
1130 }
1131
1132
1133 /**************************************************************************
1134  * SendNotifyEx [COMCTL32.342]
1135  *
1136  * Sends a WM_NOTIFY message to the specified window.
1137  *
1138  * PARAMS
1139  *     hwndFrom [I] Window to receive the message
1140  *     hwndTo   [I] Window that the message is from
1141  *     uCode    [I] Notification code
1142  *     lpHdr    [I] The NMHDR and any additional information to send or NULL
1143  *     dwParam5 [I] Unknown
1144  *
1145  * RETURNS
1146  *     Success: return value from notification
1147  *     Failure: 0
1148  *
1149  * NOTES
1150  *     If hwndFrom is -1 then the identifier of the control sending the
1151  *     message is taken from the NMHDR structure.
1152  *     If hwndFrom is not -1 then lpHdr can be NULL.
1153  */
1154 LRESULT WINAPI SendNotifyEx (HWND hwndTo, HWND hwndFrom, UINT uCode,
1155                              LPNMHDR lpHdr, DWORD dwParam5)
1156 {
1157     NOTIFYDATA notify;
1158     HWND hwndNotify;
1159
1160     TRACE("(%p %p %d %p 0x%08x)\n",
1161            hwndFrom, hwndTo, uCode, lpHdr, dwParam5);
1162
1163     hwndNotify = hwndTo;
1164     if (!hwndTo) {
1165         if (IsWindow (hwndFrom)) {
1166             hwndNotify = GetParent (hwndFrom);
1167             if (!hwndNotify)
1168                 return 0;
1169         }
1170     }
1171
1172     notify.hwndFrom = hwndFrom;
1173     notify.hwndTo   = hwndNotify;
1174     notify.dwParam5 = dwParam5;
1175     notify.dwParam6 = 0;
1176
1177     return DoNotify (&notify, uCode, lpHdr);
1178 }