d3dx9: Introduce a function for filling a texture.
[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 tagMRUINFO
172  *|{
173  *|    DWORD   cbSize;
174  *|    UINT    uMax;
175  *|    UINT    fFlags;
176  *|    HKEY    hKey;
177  *|    LPTSTR  lpszSubKey;
178  *|    PROC    lpfnCompare;
179  *|} MRUINFO, *LPMRUINFO;
180  *
181  * MEMBERS
182  *  cbSize      [I] The size of the MRUINFO structure. This must be set
183  *                  to sizeof(MRUINFO) by the caller.
184  *  uMax        [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  *  fFlags      [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 INT (CALLBACK *MRUStringCmpFnA)(LPCSTR lhs, LPCSTR rhs);
210 typedef INT (CALLBACK *MRUStringCmpFnW)(LPCWSTR lhs, LPCWSTR rhs);
211 typedef INT (CALLBACK *MRUBinaryCmpFn)(LPCVOID lhs, LPCVOID rhs, DWORD length);
212
213 typedef struct tagMRUINFOA
214 {
215     DWORD  cbSize;
216     UINT   uMax;
217     UINT   fFlags;
218     HKEY   hKey;
219     LPSTR  lpszSubKey;
220     union
221     {
222         MRUStringCmpFnA string_cmpfn;
223         MRUBinaryCmpFn  binary_cmpfn;
224     } u;
225 } MRUINFOA, *LPMRUINFOA;
226
227 typedef struct tagMRUINFOW
228 {
229     DWORD   cbSize;
230     UINT    uMax;
231     UINT    fFlags;
232     HKEY    hKey;
233     LPWSTR  lpszSubKey;
234     union
235     {
236         MRUStringCmpFnW string_cmpfn;
237         MRUBinaryCmpFn  binary_cmpfn;
238     } u;
239 } MRUINFOW, *LPMRUINFOW;
240
241 /* MRUINFO.fFlags */
242 #define MRU_STRING     0 /* list will contain strings */
243 #define MRU_BINARY     1 /* list will contain binary data */
244 #define MRU_CACHEWRITE 2 /* only save list order to reg. is FreeMRUList */
245
246 /* If list is a string list lpfnCompare has the following prototype
247  * int CALLBACK MRUCompareString(LPCSTR s1, LPCSTR s2)
248  * for binary lists the prototype is
249  * int CALLBACK MRUCompareBinary(LPCVOID data1, LPCVOID data2, DWORD cbData)
250  * where cbData is the no. of bytes to compare.
251  * Need to check what return value means identical - 0?
252  */
253
254 typedef struct tagWINEMRUITEM
255 {
256     DWORD          size;        /* size of data stored               */
257     DWORD          itemFlag;    /* flags                             */
258     BYTE           datastart;
259 } WINEMRUITEM, *LPWINEMRUITEM;
260
261 /* itemFlag */
262 #define WMRUIF_CHANGED   0x0001 /* this dataitem changed             */
263
264 typedef struct tagWINEMRULIST
265 {
266     MRUINFOW       extview;     /* original create information       */
267     BOOL           isUnicode;   /* is compare fn Unicode */
268     DWORD          wineFlags;   /* internal flags                    */
269     DWORD          cursize;     /* current size of realMRU           */
270     LPWSTR         realMRU;     /* pointer to string of index names  */
271     LPWINEMRUITEM  *array;      /* array of pointers to data         */
272                                 /* in 'a' to 'z' order               */
273 } WINEMRULIST, *LPWINEMRULIST;
274
275 /* wineFlags */
276 #define WMRUF_CHANGED  0x0001   /* MRU list has changed              */
277
278 /**************************************************************************
279  *              MRU_SaveChanged (internal)
280  *
281  * Local MRU saving code
282  */
283 static void MRU_SaveChanged ( LPWINEMRULIST mp )
284 {
285     UINT i, err;
286     HKEY newkey;
287     WCHAR realname[2];
288     LPWINEMRUITEM witem;
289
290     /* or should we do the following instead of RegOpenKeyEx:
291      */
292
293     /* open the sub key */
294     if ((err = RegOpenKeyExW( mp->extview.hKey, mp->extview.lpszSubKey,
295                               0, KEY_WRITE, &newkey))) {
296         /* not present - what to do ??? */
297         ERR("Could not open key, error=%d, attempting to create\n",
298             err);
299         if ((err = RegCreateKeyExW( mp->extview.hKey, mp->extview.lpszSubKey,
300                                     0,
301                                     NULL,
302                                     REG_OPTION_NON_VOLATILE,
303                                     KEY_READ | KEY_WRITE,
304                                     0,
305                                     &newkey,
306                                     0))) {
307             ERR("failed to create key /%s/, err=%d\n",
308                 debugstr_w(mp->extview.lpszSubKey), err);
309             return;
310         }
311     }
312     if (mp->wineFlags & WMRUF_CHANGED) {
313         mp->wineFlags &= ~WMRUF_CHANGED;
314         err = RegSetValueExW(newkey, strMRUList, 0, REG_SZ, (LPBYTE)mp->realMRU,
315                              (strlenW(mp->realMRU) + 1)*sizeof(WCHAR));
316         if (err) {
317             ERR("error saving MRUList, err=%d\n", err);
318         }
319         TRACE("saving MRUList=/%s/\n", debugstr_w(mp->realMRU));
320     }
321     realname[1] = 0;
322     for(i=0; i<mp->cursize; i++) {
323         witem = mp->array[i];
324         if (witem->itemFlag & WMRUIF_CHANGED) {
325             witem->itemFlag &= ~WMRUIF_CHANGED;
326             realname[0] = 'a' + i;
327             err = RegSetValueExW(newkey, realname, 0,
328                                  (mp->extview.fFlags & MRU_BINARY) ?
329                                  REG_BINARY : REG_SZ,
330                                  &witem->datastart, witem->size);
331             if (err) {
332                 ERR("error saving /%s/, err=%d\n", debugstr_w(realname), err);
333             }
334             TRACE("saving value for name /%s/ size=%d\n",
335                   debugstr_w(realname), witem->size);
336         }
337     }
338     RegCloseKey( newkey );
339 }
340
341 /**************************************************************************
342  *              FreeMRUList [COMCTL32.152]
343  *
344  * Frees a most-recently-used items list.
345  *
346  * PARAMS
347  *     hMRUList [I] Handle to list.
348  *
349  * RETURNS
350  *     Nothing.
351  */
352 void WINAPI FreeMRUList (HANDLE hMRUList)
353 {
354     LPWINEMRULIST mp = hMRUList;
355     UINT i;
356
357     TRACE("(%p)\n", hMRUList);
358     if (!hMRUList)
359         return;
360
361     if (mp->wineFlags & WMRUF_CHANGED) {
362         /* need to open key and then save the info */
363         MRU_SaveChanged( mp );
364     }
365
366     for(i=0; i<mp->extview.uMax; i++)
367         Free(mp->array[i]);
368
369     Free(mp->realMRU);
370     Free(mp->array);
371     Free(mp->extview.lpszSubKey);
372     Free(mp);
373 }
374
375
376 /**************************************************************************
377  *                  FindMRUData [COMCTL32.169]
378  *
379  * Searches binary list for item that matches lpData of length cbData.
380  * Returns position in list order 0 -> MRU and if lpRegNum != NULL then value
381  * corresponding to item's reg. name will be stored in it ('a' -> 0).
382  *
383  * PARAMS
384  *    hList [I] list handle
385  *    lpData [I] data to find
386  *    cbData [I] length of data
387  *    lpRegNum [O] position in registry (maybe NULL)
388  *
389  * RETURNS
390  *    Position in list 0 -> MRU.  -1 if item not found.
391  */
392 INT WINAPI FindMRUData (HANDLE hList, LPCVOID lpData, DWORD cbData,
393                         LPINT lpRegNum)
394 {
395     const WINEMRULIST *mp = hList;
396     INT ret;
397     UINT i;
398     LPSTR dataA = NULL;
399
400     if (!mp || !mp->extview.u.string_cmpfn)
401         return -1;
402
403     if(!(mp->extview.fFlags & MRU_BINARY) && !mp->isUnicode) {
404         DWORD len = WideCharToMultiByte(CP_ACP, 0, lpData, -1,
405                                         NULL, 0, NULL, NULL);
406         dataA = Alloc(len);
407         WideCharToMultiByte(CP_ACP, 0, lpData, -1, dataA, len, NULL, NULL);
408     }
409
410     for(i=0; i<mp->cursize; i++) {
411         if (mp->extview.fFlags & MRU_BINARY) {
412             if (!mp->extview.u.binary_cmpfn(lpData, &mp->array[i]->datastart, cbData))
413                 break;
414         }
415         else {
416             if(mp->isUnicode) {
417                 if (!mp->extview.u.string_cmpfn(lpData, (LPWSTR)&mp->array[i]->datastart))
418                     break;
419             } else {
420                 DWORD len = WideCharToMultiByte(CP_ACP, 0,
421                                                 (LPWSTR)&mp->array[i]->datastart, -1,
422                                                 NULL, 0, NULL, NULL);
423                 LPSTR itemA = Alloc(len);
424                 INT cmp;
425                 WideCharToMultiByte(CP_ACP, 0, (LPWSTR)&mp->array[i]->datastart, -1,
426                                     itemA, len, NULL, NULL);
427
428                 cmp = mp->extview.u.string_cmpfn((LPWSTR)dataA, (LPWSTR)itemA);
429                 Free(itemA);
430                 if(!cmp)
431                     break;
432             }
433         }
434     }
435     Free(dataA);
436     if (i < mp->cursize)
437         ret = i;
438     else
439         ret = -1;
440     if (lpRegNum && (ret != -1))
441         *lpRegNum = 'a' + i;
442
443     TRACE("(%p, %p, %d, %p) returning %d\n",
444            hList, lpData, cbData, lpRegNum, ret);
445
446     return ret;
447 }
448
449
450 /**************************************************************************
451  *              AddMRUData [COMCTL32.167]
452  *
453  * Add item to MRU binary list.  If item already exists in list then it is
454  * simply moved up to the top of the list and not added again.  If list is
455  * full then the least recently used item is removed to make room.
456  *
457  * PARAMS
458  *     hList [I] Handle to list.
459  *     lpData [I] ptr to data to add.
460  *     cbData [I] no. of bytes of data.
461  *
462  * RETURNS
463  *     No. corresponding to registry name where value is stored 'a' -> 0 etc.
464  *     -1 on error.
465  */
466 INT WINAPI AddMRUData (HANDLE hList, LPCVOID lpData, DWORD cbData)
467 {
468     LPWINEMRULIST mp = hList;
469     LPWINEMRUITEM witem;
470     INT i, replace;
471
472     if ((replace = FindMRUData (hList, lpData, cbData, NULL)) >= 0) {
473         /* Item exists, just move it to the front */
474         LPWSTR pos = strchrW(mp->realMRU, replace + 'a');
475         while (pos > mp->realMRU)
476         {
477             pos[0] = pos[-1];
478             pos--;
479         }
480     }
481     else {
482         /* either add a new entry or replace oldest */
483         if (mp->cursize < mp->extview.uMax) {
484             /* Add in a new item */
485             replace = mp->cursize;
486             mp->cursize++;
487         }
488         else {
489             /* get the oldest entry and replace data */
490             replace = mp->realMRU[mp->cursize - 1] - 'a';
491             Free(mp->array[replace]);
492         }
493
494         /* Allocate space for new item and move in the data */
495         mp->array[replace] = witem = Alloc(cbData + sizeof(WINEMRUITEM));
496         witem->itemFlag |= WMRUIF_CHANGED;
497         witem->size = cbData;
498         memcpy( &witem->datastart, lpData, cbData);
499
500         /* now rotate MRU list */
501         for(i=mp->cursize-1; i>=1; i--)
502             mp->realMRU[i] = mp->realMRU[i-1];
503     }
504
505     /* The new item gets the front spot */
506     mp->wineFlags |= WMRUF_CHANGED;
507     mp->realMRU[0] = replace + 'a';
508
509     TRACE("(%p, %p, %d) adding data, /%c/ now most current\n",
510           hList, lpData, cbData, replace+'a');
511
512     if (!(mp->extview.fFlags & MRU_CACHEWRITE)) {
513         /* save changed stuff right now */
514         MRU_SaveChanged( mp );
515     }
516
517     return replace;
518 }
519
520 /**************************************************************************
521  *              AddMRUStringW [COMCTL32.401]
522  *
523  * Add an item to an MRU string list.
524  *
525  * PARAMS
526  *     hList      [I] Handle to list.
527  *     lpszString [I] The string to add.
528  *
529  * RETURNS
530  *   Success: The number corresponding to the registry name where the string
531  *            has been stored (0 maps to 'a', 1 to 'b' and so on).
532  *   Failure: -1, if hList is NULL or memory allocation fails. If lpszString
533  *            is invalid, the function returns 0, and GetLastError() returns
534  *            ERROR_INVALID_PARAMETER. The last error value is set only in
535  *            this case.
536  *
537  * NOTES
538  *  -If lpszString exists in the list already, it is moved to the top of the
539  *   MRU list (it is not duplicated).
540  *  -If the list is full the least recently used list entry is replaced with
541  *   lpszString.
542  *  -If this function returns 0 you should check the last error value to
543  *   ensure the call really succeeded.
544  */
545 INT WINAPI AddMRUStringW(HANDLE hList, LPCWSTR lpszString)
546 {
547     TRACE("(%p,%s)\n", hList, debugstr_w(lpszString));
548
549     if (!hList)
550         return -1;
551
552     if (!lpszString || IsBadStringPtrW(lpszString, -1))
553     {
554         SetLastError(ERROR_INVALID_PARAMETER);
555         return 0;
556     }
557
558     return AddMRUData(hList, lpszString,
559                       (strlenW(lpszString) + 1) * sizeof(WCHAR));
560 }
561
562 /**************************************************************************
563  *              AddMRUStringA [COMCTL32.153]
564  *
565  * See AddMRUStringW.
566  */
567 INT WINAPI AddMRUStringA(HANDLE hList, LPCSTR lpszString)
568 {
569     DWORD len;
570     LPWSTR stringW;
571     INT ret;
572
573     TRACE("(%p,%s)\n", hList, debugstr_a(lpszString));
574
575     if (!hList)
576         return -1;
577
578     if (IsBadStringPtrA(lpszString, -1))
579     {
580         SetLastError(ERROR_INVALID_PARAMETER);
581         return 0;
582     }
583
584     len = MultiByteToWideChar(CP_ACP, 0, lpszString, -1, NULL, 0) * sizeof(WCHAR);
585     stringW = Alloc(len);
586     if (!stringW)
587         return -1;
588
589     MultiByteToWideChar(CP_ACP, 0, lpszString, -1, stringW, len/sizeof(WCHAR));
590     ret = AddMRUData(hList, stringW, len);
591     Free(stringW);
592     return ret;
593 }
594
595 /**************************************************************************
596  *              DelMRUString [COMCTL32.156]
597  *
598  * Removes item from either string or binary list (despite its name)
599  *
600  * PARAMS
601  *    hList [I] list handle
602  *    nItemPos [I] item position to remove 0 -> MRU
603  *
604  * RETURNS
605  *    TRUE if successful, FALSE if nItemPos is out of range.
606  */
607 BOOL WINAPI DelMRUString(HANDLE hList, INT nItemPos)
608 {
609     FIXME("(%p, %d): stub\n", hList, nItemPos);
610     return TRUE;
611 }
612
613 /**************************************************************************
614  *                  FindMRUStringW [COMCTL32.402]
615  *
616  * See FindMRUStringA.
617  */
618 INT WINAPI FindMRUStringW (HANDLE hList, LPCWSTR lpszString, LPINT lpRegNum)
619 {
620   return FindMRUData(hList, lpszString,
621                      (lstrlenW(lpszString) + 1) * sizeof(WCHAR), lpRegNum);
622 }
623
624 /**************************************************************************
625  *                  FindMRUStringA [COMCTL32.155]
626  *
627  * Searches string list for item that matches lpszString.
628  * Returns position in list order 0 -> MRU and if lpRegNum != NULL then value
629  * corresponding to item's reg. name will be stored in it ('a' -> 0).
630  *
631  * PARAMS
632  *    hList [I] list handle
633  *    lpszString [I] string to find
634  *    lpRegNum [O] position in registry (maybe NULL)
635  *
636  * RETURNS
637  *    Position in list 0 -> MRU.  -1 if item not found.
638  */
639 INT WINAPI FindMRUStringA (HANDLE hList, LPCSTR lpszString, LPINT lpRegNum)
640 {
641     DWORD len = MultiByteToWideChar(CP_ACP, 0, lpszString, -1, NULL, 0);
642     LPWSTR stringW = Alloc(len * sizeof(WCHAR));
643     INT ret;
644
645     MultiByteToWideChar(CP_ACP, 0, lpszString, -1, stringW, len);
646     ret = FindMRUData(hList, stringW, len * sizeof(WCHAR), lpRegNum);
647     Free(stringW);
648     return ret;
649 }
650
651 /*************************************************************************
652  *                 create_mru_list (internal)
653  */
654 static HANDLE create_mru_list(LPWINEMRULIST mp)
655 {
656     UINT i, err;
657     HKEY newkey;
658     DWORD datasize, dwdisp;
659     WCHAR realname[2];
660     LPWINEMRUITEM witem;
661     DWORD type;
662
663     /* get space to save indices that will turn into names
664      * but in order of most to least recently used
665      */
666     mp->realMRU = Alloc((mp->extview.uMax + 2) * sizeof(WCHAR));
667
668     /* get space to save pointers to actual data in order of
669      * 'a' to 'z' (0 to n).
670      */
671     mp->array = Alloc(mp->extview.uMax * sizeof(LPVOID));
672
673     /* open the sub key */
674     if ((err = RegCreateKeyExW( mp->extview.hKey, mp->extview.lpszSubKey,
675                                 0,
676                                 NULL,
677                                 REG_OPTION_NON_VOLATILE,
678                                 KEY_READ | KEY_WRITE,
679                                 0,
680                                 &newkey,
681                                 &dwdisp))) {
682         /* error - what to do ??? */
683         ERR("(%u %u %x %p %s %p): Could not open key, error=%d\n",
684             mp->extview.cbSize, mp->extview.uMax, mp->extview.fFlags,
685             mp->extview.hKey, debugstr_w(mp->extview.lpszSubKey),
686             mp->extview.u.string_cmpfn, err);
687         return 0;
688     }
689
690     /* get values from key 'MRUList' */
691     if (newkey) {
692         datasize = (mp->extview.uMax + 1) * sizeof(WCHAR);
693         if((err=RegQueryValueExW( newkey, strMRUList, 0, &type,
694                                   (LPBYTE)mp->realMRU, &datasize))) {
695             /* not present - set size to 1 (will become 0 later) */
696             datasize = 1;
697             *mp->realMRU = 0;
698         }
699         else
700             datasize /= sizeof(WCHAR);
701
702         TRACE("MRU list = %s, datasize = %d\n", debugstr_w(mp->realMRU), datasize);
703
704         mp->cursize = datasize - 1;
705         /* datasize now has number of items in the MRUList */
706
707         /* get actual values for each entry */
708         realname[1] = 0;
709         for(i=0; i<mp->cursize; i++) {
710             realname[0] = 'a' + i;
711             if(RegQueryValueExW( newkey, realname, 0, &type, 0, &datasize)) {
712                 /* not present - what to do ??? */
713                 ERR("Key %s not found 1\n", debugstr_w(realname));
714             }
715             mp->array[i] = witem = Alloc(datasize + sizeof(WINEMRUITEM));
716             witem->size = datasize;
717             if(RegQueryValueExW( newkey, realname, 0, &type,
718                                  &witem->datastart, &datasize)) {
719                 /* not present - what to do ??? */
720                 ERR("Key %s not found 2\n", debugstr_w(realname));
721             }
722         }
723         RegCloseKey( newkey );
724     }
725     else
726         mp->cursize = 0;
727
728     TRACE("(%u %u %x %p %s %p): Current Size = %d\n",
729           mp->extview.cbSize, mp->extview.uMax, mp->extview.fFlags,
730           mp->extview.hKey, debugstr_w(mp->extview.lpszSubKey),
731           mp->extview.u.string_cmpfn, mp->cursize);
732     return mp;
733 }
734
735 /**************************************************************************
736  *                  CreateMRUListLazyW [COMCTL32.404]
737  *
738  * See CreateMRUListLazyA.
739  */
740 HANDLE WINAPI CreateMRUListLazyW (const MRUINFOW *infoW, DWORD dwParam2,
741                                   DWORD dwParam3, DWORD dwParam4)
742 {
743     LPWINEMRULIST mp;
744
745     /* Native does not check for a NULL lpcml */
746     if (!infoW->hKey || IsBadStringPtrW(infoW->lpszSubKey, -1))
747         return NULL;
748
749     mp = Alloc(sizeof(WINEMRULIST));
750     memcpy(&mp->extview, infoW, sizeof(MRUINFOW));
751     mp->extview.lpszSubKey = Alloc((strlenW(infoW->lpszSubKey) + 1) * sizeof(WCHAR));
752     strcpyW(mp->extview.lpszSubKey, infoW->lpszSubKey);
753     mp->isUnicode = TRUE;
754
755     return create_mru_list(mp);
756 }
757
758 /**************************************************************************
759  *                  CreateMRUListLazyA [COMCTL32.157]
760  *
761  * Creates a most-recently-used list.
762  *
763  * PARAMS
764  *     lpcml    [I] ptr to CREATEMRULIST structure.
765  *     dwParam2 [I] Unknown
766  *     dwParam3 [I] Unknown
767  *     dwParam4 [I] Unknown
768  *
769  * RETURNS
770  *     Handle to MRU list.
771  */
772 HANDLE WINAPI CreateMRUListLazyA (const MRUINFOA *lpcml, DWORD dwParam2,
773                                   DWORD dwParam3, DWORD dwParam4)
774 {
775     LPWINEMRULIST mp;
776     DWORD len;
777
778     /* Native does not check for a NULL lpcml */
779
780     if (!lpcml->hKey || IsBadStringPtrA(lpcml->lpszSubKey, -1))
781         return 0;
782
783     mp = Alloc(sizeof(WINEMRULIST));
784     memcpy(&mp->extview, lpcml, sizeof(MRUINFOA));
785     len = MultiByteToWideChar(CP_ACP, 0, lpcml->lpszSubKey, -1, NULL, 0);
786     mp->extview.lpszSubKey = Alloc(len * sizeof(WCHAR));
787     MultiByteToWideChar(CP_ACP, 0, lpcml->lpszSubKey, -1,
788                         mp->extview.lpszSubKey, len);
789     mp->isUnicode = FALSE;
790     return create_mru_list(mp);
791 }
792
793 /**************************************************************************
794  *              CreateMRUListW [COMCTL32.400]
795  *
796  * See CreateMRUListA.
797  */
798 HANDLE WINAPI CreateMRUListW (const MRUINFOW *infoW)
799 {
800     return CreateMRUListLazyW(infoW, 0, 0, 0);
801 }
802
803 /**************************************************************************
804  *              CreateMRUListA [COMCTL32.151]
805  *
806  * Creates a most-recently-used list.
807  *
808  * PARAMS
809  *     lpcml [I] ptr to CREATEMRULIST structure.
810  *
811  * RETURNS
812  *     Handle to MRU list.
813  */
814 HANDLE WINAPI CreateMRUListA (const MRUINFOA *lpcml)
815 {
816      return CreateMRUListLazyA (lpcml, 0, 0, 0);
817 }
818
819
820 /**************************************************************************
821  *                EnumMRUListW [COMCTL32.403]
822  *
823  * Enumerate item in a most-recently-used list
824  *
825  * PARAMS
826  *    hList [I] list handle
827  *    nItemPos [I] item position to enumerate
828  *    lpBuffer [O] buffer to receive item
829  *    nBufferSize [I] size of buffer
830  *
831  * RETURNS
832  *    For binary lists specifies how many bytes were copied to buffer, for
833  *    string lists specifies full length of string.  Enumerating past the end
834  *    of list returns -1.
835  *    If lpBuffer == NULL or nItemPos is -ve return value is no. of items in
836  *    the list.
837  */
838 INT WINAPI EnumMRUListW (HANDLE hList, INT nItemPos, LPVOID lpBuffer,
839                          DWORD nBufferSize)
840 {
841     const WINEMRULIST *mp = hList;
842     const WINEMRUITEM *witem;
843     INT desired, datasize;
844
845     if (!mp) return -1;
846     if ((nItemPos < 0) || !lpBuffer) return mp->cursize;
847     if (nItemPos >= mp->cursize) return -1;
848     desired = mp->realMRU[nItemPos];
849     desired -= 'a';
850     TRACE("nItemPos=%d, desired=%d\n", nItemPos, desired);
851     witem = mp->array[desired];
852     datasize = min( witem->size, nBufferSize );
853     memcpy( lpBuffer, &witem->datastart, datasize);
854     TRACE("(%p, %d, %p, %d): returning len=%d\n",
855           hList, nItemPos, lpBuffer, nBufferSize, datasize);
856     return datasize;
857 }
858
859 /**************************************************************************
860  *                EnumMRUListA [COMCTL32.154]
861  *
862  * See EnumMRUListW.
863  */
864 INT WINAPI EnumMRUListA (HANDLE hList, INT nItemPos, LPVOID lpBuffer,
865                          DWORD nBufferSize)
866 {
867     const WINEMRULIST *mp = hList;
868     LPWINEMRUITEM witem;
869     INT desired, datasize;
870     DWORD lenA;
871
872     if (!mp) return -1;
873     if ((nItemPos < 0) || !lpBuffer) return mp->cursize;
874     if (nItemPos >= mp->cursize) return -1;
875     desired = mp->realMRU[nItemPos];
876     desired -= 'a';
877     TRACE("nItemPos=%d, desired=%d\n", nItemPos, desired);
878     witem = mp->array[desired];
879     if(mp->extview.fFlags & MRU_BINARY) {
880         datasize = min( witem->size, nBufferSize );
881         memcpy( lpBuffer, &witem->datastart, datasize);
882     } else {
883         lenA = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)&witem->datastart, -1,
884                                    NULL, 0, NULL, NULL);
885         datasize = min( lenA, nBufferSize );
886         WideCharToMultiByte(CP_ACP, 0, (LPWSTR)&witem->datastart, -1,
887                             lpBuffer, datasize, NULL, NULL);
888         ((char *)lpBuffer)[ datasize - 1 ] = '\0';
889         datasize = lenA - 1;
890     }
891     TRACE("(%p, %d, %p, %d): returning len=%d\n",
892           hList, nItemPos, lpBuffer, nBufferSize, datasize);
893     return datasize;
894 }
895
896 /**************************************************************************
897  * Str_GetPtrWtoA [internal]
898  *
899  * Converts a unicode string into a multi byte string
900  *
901  * PARAMS
902  *     lpSrc   [I] Pointer to the unicode source string
903  *     lpDest  [O] Pointer to caller supplied storage for the multi byte string
904  *     nMaxLen [I] Size, in bytes, of the destination buffer
905  *
906  * RETURNS
907  *     Length, in bytes, of the converted string.
908  */
909
910 INT Str_GetPtrWtoA (LPCWSTR lpSrc, LPSTR lpDest, INT nMaxLen)
911 {
912     INT len;
913
914     TRACE("(%s %p %d)\n", debugstr_w(lpSrc), lpDest, nMaxLen);
915
916     if (!lpDest && lpSrc)
917         return WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, 0, 0, NULL, NULL);
918
919     if (nMaxLen == 0)
920         return 0;
921
922     if (lpSrc == NULL) {
923         lpDest[0] = '\0';
924         return 0;
925     }
926
927     len = WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, 0, 0, NULL, NULL);
928     if (len >= nMaxLen)
929         len = nMaxLen - 1;
930
931     WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, lpDest, len, NULL, NULL);
932     lpDest[len] = '\0';
933
934     return len;
935 }
936
937 /**************************************************************************
938  * Str_GetPtrAtoW [internal]
939  *
940  * Converts a multibyte string into a unicode string
941  *
942  * PARAMS
943  *     lpSrc   [I] Pointer to the multibyte source string
944  *     lpDest  [O] Pointer to caller supplied storage for the unicode string
945  *     nMaxLen [I] Size, in characters, of the destination buffer
946  *
947  * RETURNS
948  *     Length, in characters, of the converted string.
949  */
950
951 INT Str_GetPtrAtoW (LPCSTR lpSrc, LPWSTR lpDest, INT nMaxLen)
952 {
953     INT len;
954
955     TRACE("(%s %p %d)\n", debugstr_a(lpSrc), lpDest, nMaxLen);
956
957     if (!lpDest && lpSrc)
958         return MultiByteToWideChar(CP_ACP, 0, lpSrc, -1, 0, 0);
959
960     if (nMaxLen == 0)
961         return 0;
962
963     if (lpSrc == NULL) {
964         lpDest[0] = '\0';
965         return 0;
966     }
967
968     len = MultiByteToWideChar(CP_ACP, 0, lpSrc, -1, 0, 0);
969     if (len >= nMaxLen)
970         len = nMaxLen - 1;
971
972     MultiByteToWideChar(CP_ACP, 0, lpSrc, -1, lpDest, len);
973     lpDest[len] = '\0';
974
975     return len;
976 }
977
978
979 /**************************************************************************
980  * Str_SetPtrAtoW [internal]
981  *
982  * Converts a multi byte string to a unicode string.
983  * If the pointer to the destination buffer is NULL a buffer is allocated.
984  * If the destination buffer is too small to keep the converted multi byte
985  * string the destination buffer is reallocated. If the source pointer is
986  * NULL, the destination buffer is freed.
987  *
988  * PARAMS
989  *     lppDest [I/O] pointer to a pointer to the destination buffer
990  *     lpSrc   [I] pointer to a multi byte string
991  *
992  * RETURNS
993  *     TRUE: conversion successful
994  *     FALSE: error
995  */
996 BOOL Str_SetPtrAtoW (LPWSTR *lppDest, LPCSTR lpSrc)
997 {
998     TRACE("(%p %s)\n", lppDest, lpSrc);
999
1000     if (lpSrc) {
1001         INT len = MultiByteToWideChar(CP_ACP,0,lpSrc,-1,NULL,0);
1002         LPWSTR ptr = ReAlloc (*lppDest, len*sizeof(WCHAR));
1003
1004         if (!ptr)
1005             return FALSE;
1006         MultiByteToWideChar(CP_ACP,0,lpSrc,-1,ptr,len);
1007         *lppDest = ptr;
1008     }
1009     else {
1010         Free (*lppDest);
1011         *lppDest = NULL;
1012     }
1013
1014     return TRUE;
1015 }
1016
1017 /**************************************************************************
1018  * Str_SetPtrWtoA [internal]
1019  *
1020  * Converts a unicode string to a multi byte string.
1021  * If the pointer to the destination buffer is NULL a buffer is allocated.
1022  * If the destination buffer is too small to keep the converted wide
1023  * string the destination buffer is reallocated. If the source pointer is
1024  * NULL, the destination buffer is freed.
1025  *
1026  * PARAMS
1027  *     lppDest [I/O] pointer to a pointer to the destination buffer
1028  *     lpSrc   [I] pointer to a wide string
1029  *
1030  * RETURNS
1031  *     TRUE: conversion successful
1032  *     FALSE: error
1033  */
1034 BOOL Str_SetPtrWtoA (LPSTR *lppDest, LPCWSTR lpSrc)
1035 {
1036     TRACE("(%p %s)\n", lppDest, debugstr_w(lpSrc));
1037
1038     if (lpSrc) {
1039         INT len = WideCharToMultiByte(CP_ACP,0,lpSrc,-1,NULL,0,NULL,FALSE);
1040         LPSTR ptr = ReAlloc (*lppDest, len*sizeof(CHAR));
1041
1042         if (!ptr)
1043             return FALSE;
1044         WideCharToMultiByte(CP_ACP,0,lpSrc,-1,ptr,len,NULL,FALSE);
1045         *lppDest = ptr;
1046     }
1047     else {
1048         Free (*lppDest);
1049         *lppDest = NULL;
1050     }
1051
1052     return TRUE;
1053 }
1054
1055
1056 /**************************************************************************
1057  * Notification functions
1058  */
1059
1060 typedef struct tagNOTIFYDATA
1061 {
1062     HWND hwndFrom;
1063     HWND hwndTo;
1064     DWORD  dwParam3;
1065     DWORD  dwParam4;
1066     DWORD  dwParam5;
1067     DWORD  dwParam6;
1068 } NOTIFYDATA, *LPNOTIFYDATA;
1069
1070
1071 /**************************************************************************
1072  * DoNotify [Internal]
1073  */
1074
1075 static LRESULT DoNotify (const NOTIFYDATA *lpNotify, UINT uCode, LPNMHDR lpHdr)
1076 {
1077     NMHDR nmhdr;
1078     LPNMHDR lpNmh = NULL;
1079     UINT idFrom = 0;
1080
1081     TRACE("(%p %p %d %p 0x%08x)\n",
1082            lpNotify->hwndFrom, lpNotify->hwndTo, uCode, lpHdr,
1083            lpNotify->dwParam5);
1084
1085     if (!lpNotify->hwndTo)
1086         return 0;
1087
1088     if (lpNotify->hwndFrom == (HWND)-1) {
1089         lpNmh = lpHdr;
1090         idFrom = lpHdr->idFrom;
1091     }
1092     else {
1093         if (lpNotify->hwndFrom)
1094             idFrom = GetDlgCtrlID (lpNotify->hwndFrom);
1095
1096         lpNmh = (lpHdr) ? lpHdr : &nmhdr;
1097
1098         lpNmh->hwndFrom = lpNotify->hwndFrom;
1099         lpNmh->idFrom = idFrom;
1100         lpNmh->code = uCode;
1101     }
1102
1103     return SendMessageW (lpNotify->hwndTo, WM_NOTIFY, idFrom, (LPARAM)lpNmh);
1104 }
1105
1106
1107 /**************************************************************************
1108  * SendNotify [COMCTL32.341]
1109  *
1110  * Sends a WM_NOTIFY message to the specified window.
1111  *
1112  * PARAMS
1113  *     hwndTo   [I] Window to receive the message
1114  *     hwndFrom [I] Window that the message is from (see notes)
1115  *     uCode    [I] Notification code
1116  *     lpHdr    [I] The NMHDR and any additional information to send or NULL
1117  *
1118  * RETURNS
1119  *     Success: return value from notification
1120  *     Failure: 0
1121  *
1122  * NOTES
1123  *     If hwndFrom is -1 then the identifier of the control sending the
1124  *     message is taken from the NMHDR structure.
1125  *     If hwndFrom is not -1 then lpHdr can be NULL.
1126  */
1127 LRESULT WINAPI SendNotify (HWND hwndTo, HWND hwndFrom, UINT uCode, LPNMHDR lpHdr)
1128 {
1129     NOTIFYDATA notify;
1130
1131     TRACE("(%p %p %d %p)\n",
1132            hwndTo, hwndFrom, uCode, lpHdr);
1133
1134     notify.hwndFrom = hwndFrom;
1135     notify.hwndTo   = hwndTo;
1136     notify.dwParam5 = 0;
1137     notify.dwParam6 = 0;
1138
1139     return DoNotify (&notify, uCode, lpHdr);
1140 }
1141
1142
1143 /**************************************************************************
1144  * SendNotifyEx [COMCTL32.342]
1145  *
1146  * Sends a WM_NOTIFY message to the specified window.
1147  *
1148  * PARAMS
1149  *     hwndFrom [I] Window to receive the message
1150  *     hwndTo   [I] Window that the message is from
1151  *     uCode    [I] Notification code
1152  *     lpHdr    [I] The NMHDR and any additional information to send or NULL
1153  *     dwParam5 [I] Unknown
1154  *
1155  * RETURNS
1156  *     Success: return value from notification
1157  *     Failure: 0
1158  *
1159  * NOTES
1160  *     If hwndFrom is -1 then the identifier of the control sending the
1161  *     message is taken from the NMHDR structure.
1162  *     If hwndFrom is not -1 then lpHdr can be NULL.
1163  */
1164 LRESULT WINAPI SendNotifyEx (HWND hwndTo, HWND hwndFrom, UINT uCode,
1165                              LPNMHDR lpHdr, DWORD dwParam5)
1166 {
1167     NOTIFYDATA notify;
1168     HWND hwndNotify;
1169
1170     TRACE("(%p %p %d %p 0x%08x)\n",
1171            hwndFrom, hwndTo, uCode, lpHdr, dwParam5);
1172
1173     hwndNotify = hwndTo;
1174     if (!hwndTo) {
1175         if (IsWindow (hwndFrom)) {
1176             hwndNotify = GetParent (hwndFrom);
1177             if (!hwndNotify)
1178                 return 0;
1179         }
1180     }
1181
1182     notify.hwndFrom = hwndFrom;
1183     notify.hwndTo   = hwndNotify;
1184     notify.dwParam5 = dwParam5;
1185     notify.dwParam6 = 0;
1186
1187     return DoNotify (&notify, uCode, lpHdr);
1188 }