4a4d6fbd9215fa720c56bd3b5404cbf45f35cb9c
[reactos.git] / reactos / dll / win32 / 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 *| LPCTSTR 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 LPCSTR 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 LPCWSTR 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=%ld\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 = (LPWINEMRULIST)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 if (mp->array[i])
356 Free(mp->array[i]);
357 }
358 Free(mp->realMRU);
359 Free(mp->array);
360 Free((LPWSTR)mp->extview.lpszSubKey);
361 Free(mp);
362 }
363
364
365 /**************************************************************************
366 * FindMRUData [COMCTL32.169]
367 *
368 * Searches binary list for item that matches lpData of length cbData.
369 * Returns position in list order 0 -> MRU and if lpRegNum != NULL then value
370 * corresponding to item's reg. name will be stored in it ('a' -> 0).
371 *
372 * PARAMS
373 * hList [I] list handle
374 * lpData [I] data to find
375 * cbData [I] length of data
376 * lpRegNum [O] position in registry (maybe NULL)
377 *
378 * RETURNS
379 * Position in list 0 -> MRU. -1 if item not found.
380 */
381 INT WINAPI FindMRUData (HANDLE hList, LPCVOID lpData, DWORD cbData,
382 LPINT lpRegNum)
383 {
384 LPWINEMRULIST mp = (LPWINEMRULIST)hList;
385 INT ret;
386 UINT i;
387 LPSTR dataA = NULL;
388
389 if (!mp->extview.lpfnCompare)
390 return -1;
391
392 if(!(mp->extview.dwFlags & MRUF_BINARY_LIST) && !mp->isUnicode) {
393 DWORD len = WideCharToMultiByte(CP_ACP, 0, lpData, -1,
394 NULL, 0, NULL, NULL);
395 dataA = Alloc(len);
396 WideCharToMultiByte(CP_ACP, 0, lpData, -1, dataA, len, NULL, NULL);
397 }
398
399 for(i=0; i<mp->cursize; i++) {
400 if (mp->extview.dwFlags & MRUF_BINARY_LIST) {
401 if (!mp->extview.lpfnCompare(lpData, &mp->array[i]->datastart,
402 cbData))
403 break;
404 }
405 else {
406 if(mp->isUnicode) {
407 if (!mp->extview.lpfnCompare(lpData, &mp->array[i]->datastart))
408 break;
409 } else {
410 DWORD len = WideCharToMultiByte(CP_ACP, 0,
411 (LPWSTR)&mp->array[i]->datastart, -1,
412 NULL, 0, NULL, NULL);
413 LPSTR itemA = Alloc(len);
414 INT cmp;
415 WideCharToMultiByte(CP_ACP, 0, (LPWSTR)&mp->array[i]->datastart, -1,
416 itemA, len, NULL, NULL);
417
418 cmp = mp->extview.lpfnCompare(dataA, itemA);
419 Free(itemA);
420 if(!cmp)
421 break;
422 }
423 }
424 }
425 if(dataA)
426 Free(dataA);
427 if (i < mp->cursize)
428 ret = i;
429 else
430 ret = -1;
431 if (lpRegNum && (ret != -1))
432 *lpRegNum = 'a' + i;
433
434 TRACE("(%p, %p, %ld, %p) returning %d\n",
435 hList, lpData, cbData, lpRegNum, ret);
436
437 return ret;
438 }
439
440
441 /**************************************************************************
442 * AddMRUData [COMCTL32.167]
443 *
444 * Add item to MRU binary list. If item already exists in list then it is
445 * simply moved up to the top of the list and not added again. If list is
446 * full then the least recently used item is removed to make room.
447 *
448 * PARAMS
449 * hList [I] Handle to list.
450 * lpData [I] ptr to data to add.
451 * cbData [I] no. of bytes of data.
452 *
453 * RETURNS
454 * No. corresponding to registry name where value is stored 'a' -> 0 etc.
455 * -1 on error.
456 */
457 INT WINAPI AddMRUData (HANDLE hList, LPCVOID lpData, DWORD cbData)
458 {
459 LPWINEMRULIST mp = (LPWINEMRULIST)hList;
460 LPWINEMRUITEM witem;
461 INT i, replace;
462
463 if ((replace = FindMRUData (hList, lpData, cbData, NULL)) >= 0) {
464 /* Item exists, just move it to the front */
465 LPWSTR pos = strchrW(mp->realMRU, replace + 'a');
466 while (pos > mp->realMRU)
467 {
468 pos[0] = pos[-1];
469 pos--;
470 }
471 }
472 else {
473 /* either add a new entry or replace oldest */
474 if (mp->cursize < mp->extview.nMaxItems) {
475 /* Add in a new item */
476 replace = mp->cursize;
477 mp->cursize++;
478 }
479 else {
480 /* get the oldest entry and replace data */
481 replace = mp->realMRU[mp->cursize - 1] - 'a';
482 Free(mp->array[replace]);
483 }
484
485 /* Allocate space for new item and move in the data */
486 mp->array[replace] = witem = Alloc(cbData + sizeof(WINEMRUITEM));
487 witem->itemFlag |= WMRUIF_CHANGED;
488 witem->size = cbData;
489 memcpy( &witem->datastart, lpData, cbData);
490
491 /* now rotate MRU list */
492 for(i=mp->cursize-1; i>=1; i--)
493 mp->realMRU[i] = mp->realMRU[i-1];
494 }
495
496 /* The new item gets the front spot */
497 mp->wineFlags |= WMRUF_CHANGED;
498 mp->realMRU[0] = replace + 'a';
499
500 TRACE("(%p, %p, %ld) adding data, /%c/ now most current\n",
501 hList, lpData, cbData, replace+'a');
502
503 if (!(mp->extview.dwFlags & MRUF_DELAYED_SAVE)) {
504 /* save changed stuff right now */
505 MRU_SaveChanged( mp );
506 }
507
508 return replace;
509 }
510
511 /**************************************************************************
512 * AddMRUStringW [COMCTL32.401]
513 *
514 * Add an item to an MRU string list.
515 *
516 * PARAMS
517 * hList [I] Handle to list.
518 * lpszString [I] The string to add.
519 *
520 * RETURNS
521 * Success: The number corresponding to the registry name where the string
522 * has been stored (0 maps to 'a', 1 to 'b' and so on).
523 * Failure: -1, if hList is NULL or memory allocation fails. If lpszString
524 * is invalid, the function returns 0, and GetLastError() returns
525 * ERROR_INVALID_PARAMETER. The last error value is set only in
526 * this case.
527 *
528 * NOTES
529 * -If lpszString exists in the list already, it is moved to the top of the
530 * MRU list (it is not duplicated).
531 * -If the list is full the least recently used list entry is replaced with
532 * lpszString.
533 * -If this function returns 0 you should check the last error value to
534 * ensure the call really succeeded.
535 */
536 INT WINAPI AddMRUStringW(HANDLE hList, LPCWSTR lpszString)
537 {
538 TRACE("(%p,%s)\n", hList, debugstr_w(lpszString));
539
540 if (!hList)
541 return -1;
542
543 if (!lpszString || IsBadStringPtrW(lpszString, -1))
544 {
545 SetLastError(ERROR_INVALID_PARAMETER);
546 return 0;
547 }
548
549 return AddMRUData(hList, lpszString,
550 (strlenW(lpszString) + 1) * sizeof(WCHAR));
551 }
552
553 /**************************************************************************
554 * AddMRUStringA [COMCTL32.153]
555 *
556 * See AddMRUStringW.
557 */
558 INT WINAPI AddMRUStringA(HANDLE hList, LPCSTR lpszString)
559 {
560 DWORD len;
561 LPWSTR stringW;
562 INT ret;
563
564 TRACE("(%p,%s)\n", hList, debugstr_a(lpszString));
565
566 if (!hList)
567 return -1;
568
569 if (IsBadStringPtrA(lpszString, -1))
570 {
571 SetLastError(ERROR_INVALID_PARAMETER);
572 return 0;
573 }
574
575 len = MultiByteToWideChar(CP_ACP, 0, lpszString, -1, NULL, 0) * sizeof(WCHAR);
576 stringW = Alloc(len);
577 if (!stringW)
578 return -1;
579
580 MultiByteToWideChar(CP_ACP, 0, lpszString, -1, stringW, len/sizeof(WCHAR));
581 ret = AddMRUData(hList, stringW, len);
582 Free(stringW);
583 return ret;
584 }
585
586 /**************************************************************************
587 * DelMRUString [COMCTL32.156]
588 *
589 * Removes item from either string or binary list (despite its name)
590 *
591 * PARAMS
592 * hList [I] list handle
593 * nItemPos [I] item position to remove 0 -> MRU
594 *
595 * RETURNS
596 * TRUE if successful, FALSE if nItemPos is out of range.
597 */
598 BOOL WINAPI DelMRUString(HANDLE hList, INT nItemPos)
599 {
600 FIXME("(%p, %d): stub\n", hList, nItemPos);
601 return TRUE;
602 }
603
604 /**************************************************************************
605 * FindMRUStringW [COMCTL32.402]
606 *
607 * See FindMRUStringA.
608 */
609 INT WINAPI FindMRUStringW (HANDLE hList, LPCWSTR lpszString, LPINT lpRegNum)
610 {
611 return FindMRUData(hList, lpszString,
612 (lstrlenW(lpszString) + 1) * sizeof(WCHAR), lpRegNum);
613 }
614
615 /**************************************************************************
616 * FindMRUStringA [COMCTL32.155]
617 *
618 * Searches string list for item that matches lpszString.
619 * Returns position in list order 0 -> MRU and if lpRegNum != NULL then value
620 * corresponding to item's reg. name will be stored in it ('a' -> 0).
621 *
622 * PARAMS
623 * hList [I] list handle
624 * lpszString [I] string to find
625 * lpRegNum [O] position in registry (maybe NULL)
626 *
627 * RETURNS
628 * Position in list 0 -> MRU. -1 if item not found.
629 */
630 INT WINAPI FindMRUStringA (HANDLE hList, LPCSTR lpszString, LPINT lpRegNum)
631 {
632 DWORD len = MultiByteToWideChar(CP_ACP, 0, lpszString, -1, NULL, 0);
633 LPWSTR stringW = Alloc(len * sizeof(WCHAR));
634 INT ret;
635
636 MultiByteToWideChar(CP_ACP, 0, lpszString, -1, stringW, len);
637 ret = FindMRUData(hList, stringW, len * sizeof(WCHAR), lpRegNum);
638 Free(stringW);
639 return ret;
640 }
641
642 /*************************************************************************
643 * CreateMRUListLazy_common (internal)
644 */
645 static HANDLE CreateMRUListLazy_common(LPWINEMRULIST mp)
646 {
647 UINT i, err;
648 HKEY newkey;
649 DWORD datasize, dwdisp;
650 WCHAR realname[2];
651 LPWINEMRUITEM witem;
652 DWORD type;
653
654 /* get space to save indices that will turn into names
655 * but in order of most to least recently used
656 */
657 mp->realMRU = Alloc((mp->extview.nMaxItems + 2) * sizeof(WCHAR));
658
659 /* get space to save pointers to actual data in order of
660 * 'a' to 'z' (0 to n).
661 */
662 mp->array = Alloc(mp->extview.nMaxItems * sizeof(LPVOID));
663
664 /* open the sub key */
665 if ((err = RegCreateKeyExW( mp->extview.hKey, mp->extview.lpszSubKey,
666 0,
667 NULL,
668 REG_OPTION_NON_VOLATILE,
669 KEY_READ | KEY_WRITE,
670 0,
671 &newkey,
672 &dwdisp))) {
673 /* error - what to do ??? */
674 ERR("(%lu %lu %lx %p %s %p): Could not open key, error=%d\n",
675 mp->extview.cbSize, mp->extview.nMaxItems, mp->extview.dwFlags,
676 mp->extview.hKey, debugstr_w(mp->extview.lpszSubKey),
677 mp->extview.lpfnCompare, err);
678 return 0;
679 }
680
681 /* get values from key 'MRUList' */
682 if (newkey) {
683 datasize = (mp->extview.nMaxItems + 1) * sizeof(WCHAR);
684 if((err=RegQueryValueExW( newkey, strMRUList, 0, &type,
685 (LPBYTE)mp->realMRU, &datasize))) {
686 /* not present - set size to 1 (will become 0 later) */
687 datasize = 1;
688 *mp->realMRU = 0;
689 }
690 else
691 datasize /= sizeof(WCHAR);
692
693 TRACE("MRU list = %s, datasize = %ld\n", debugstr_w(mp->realMRU), datasize);
694
695 mp->cursize = datasize - 1;
696 /* datasize now has number of items in the MRUList */
697
698 /* get actual values for each entry */
699 realname[1] = 0;
700 for(i=0; i<mp->cursize; i++) {
701 realname[0] = 'a' + i;
702 if(RegQueryValueExW( newkey, realname, 0, &type, 0, &datasize)) {
703 /* not present - what to do ??? */
704 ERR("Key %s not found 1\n", debugstr_w(realname));
705 }
706 mp->array[i] = witem = Alloc(datasize + sizeof(WINEMRUITEM));
707 witem->size = datasize;
708 if(RegQueryValueExW( newkey, realname, 0, &type,
709 &witem->datastart, &datasize)) {
710 /* not present - what to do ??? */
711 ERR("Key %s not found 2\n", debugstr_w(realname));
712 }
713 }
714 RegCloseKey( newkey );
715 }
716 else
717 mp->cursize = 0;
718
719 TRACE("(%lu %lu %lx %p %s %p): Current Size = %ld\n",
720 mp->extview.cbSize, mp->extview.nMaxItems, mp->extview.dwFlags,
721 mp->extview.hKey, debugstr_w(mp->extview.lpszSubKey),
722 mp->extview.lpfnCompare, mp->cursize);
723 return (HANDLE)mp;
724 }
725
726 /**************************************************************************
727 * CreateMRUListLazyW [COMCTL32.404]
728 *
729 * See CreateMRUListLazyA.
730 */
731 HANDLE WINAPI CreateMRUListLazyW (LPCREATEMRULISTW lpcml, DWORD dwParam2,
732 DWORD dwParam3, DWORD dwParam4)
733 {
734 LPWINEMRULIST mp;
735
736 /* Native does not check for a NULL lpcml */
737
738 if (lpcml->cbSize != sizeof(CREATEMRULISTW) || !lpcml->hKey ||
739 IsBadStringPtrW(lpcml->lpszSubKey, -1))
740 return NULL;
741
742 mp = Alloc(sizeof(WINEMRULIST));
743 memcpy(&mp->extview, lpcml, sizeof(CREATEMRULISTW));
744 mp->extview.lpszSubKey = Alloc((strlenW(lpcml->lpszSubKey) + 1) * sizeof(WCHAR));
745 strcpyW((LPWSTR)mp->extview.lpszSubKey, lpcml->lpszSubKey);
746 mp->isUnicode = TRUE;
747
748 return CreateMRUListLazy_common(mp);
749 }
750
751 /**************************************************************************
752 * CreateMRUListLazyA [COMCTL32.157]
753 *
754 * Creates a most-recently-used list.
755 *
756 * PARAMS
757 * lpcml [I] ptr to CREATEMRULIST structure.
758 * dwParam2 [I] Unknown
759 * dwParam3 [I] Unknown
760 * dwParam4 [I] Unknown
761 *
762 * RETURNS
763 * Handle to MRU list.
764 */
765 HANDLE WINAPI CreateMRUListLazyA (LPCREATEMRULISTA lpcml, DWORD dwParam2,
766 DWORD dwParam3, DWORD dwParam4)
767 {
768 LPWINEMRULIST mp;
769 DWORD len;
770
771 /* Native does not check for a NULL lpcml */
772
773 if (lpcml->cbSize != sizeof(CREATEMRULISTA) || !lpcml->hKey ||
774 IsBadStringPtrA(lpcml->lpszSubKey, -1))
775 return 0;
776
777 mp = Alloc(sizeof(WINEMRULIST));
778 memcpy(&mp->extview, lpcml, sizeof(CREATEMRULISTW));
779 len = MultiByteToWideChar(CP_ACP, 0, lpcml->lpszSubKey, -1, NULL, 0);
780 mp->extview.lpszSubKey = Alloc(len * sizeof(WCHAR));
781 MultiByteToWideChar(CP_ACP, 0, lpcml->lpszSubKey, -1,
782 (LPWSTR)mp->extview.lpszSubKey, len);
783 mp->isUnicode = FALSE;
784 return CreateMRUListLazy_common(mp);
785 }
786
787 /**************************************************************************
788 * CreateMRUListW [COMCTL32.400]
789 *
790 * See CreateMRUListA.
791 */
792 HANDLE WINAPI CreateMRUListW (LPCREATEMRULISTW lpcml)
793 {
794 return CreateMRUListLazyW(lpcml, 0, 0, 0);
795 }
796
797 /**************************************************************************
798 * CreateMRUListA [COMCTL32.151]
799 *
800 * Creates a most-recently-used list.
801 *
802 * PARAMS
803 * lpcml [I] ptr to CREATEMRULIST structure.
804 *
805 * RETURNS
806 * Handle to MRU list.
807 */
808 HANDLE WINAPI CreateMRUListA (LPCREATEMRULISTA lpcml)
809 {
810 return CreateMRUListLazyA (lpcml, 0, 0, 0);
811 }
812
813
814 /**************************************************************************
815 * EnumMRUListW [COMCTL32.403]
816 *
817 * Enumerate item in a most-recenty-used list
818 *
819 * PARAMS
820 * hList [I] list handle
821 * nItemPos [I] item position to enumerate
822 * lpBuffer [O] buffer to receive item
823 * nBufferSize [I] size of buffer
824 *
825 * RETURNS
826 * For binary lists specifies how many bytes were copied to buffer, for
827 * string lists specifies full length of string. Enumerating past the end
828 * of list returns -1.
829 * If lpBuffer == NULL or nItemPos is -ve return value is no. of items in
830 * the list.
831 */
832 INT WINAPI EnumMRUListW (HANDLE hList, INT nItemPos, LPVOID lpBuffer,
833 DWORD nBufferSize)
834 {
835 LPWINEMRULIST mp = (LPWINEMRULIST) hList;
836 LPWINEMRUITEM witem;
837 INT desired, datasize;
838
839 if (nItemPos >= mp->cursize) return -1;
840 if ((nItemPos < 0) || !lpBuffer) return mp->cursize;
841 desired = mp->realMRU[nItemPos];
842 desired -= 'a';
843 TRACE("nItemPos=%d, desired=%d\n", nItemPos, desired);
844 witem = mp->array[desired];
845 datasize = min( witem->size, nBufferSize );
846 memcpy( lpBuffer, &witem->datastart, datasize);
847 TRACE("(%p, %d, %p, %ld): returning len=%d\n",
848 hList, nItemPos, lpBuffer, nBufferSize, datasize);
849 return datasize;
850 }
851
852 /**************************************************************************
853 * EnumMRUListA [COMCTL32.154]
854 *
855 * See EnumMRUListW.
856 */
857 INT WINAPI EnumMRUListA (HANDLE hList, INT nItemPos, LPVOID lpBuffer,
858 DWORD nBufferSize)
859 {
860 LPWINEMRULIST mp = (LPWINEMRULIST) hList;
861 LPWINEMRUITEM witem;
862 INT desired, datasize;
863 DWORD lenA;
864
865 if (nItemPos >= mp->cursize) return -1;
866 if ((nItemPos < 0) || !lpBuffer) return mp->cursize;
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, %ld): returning len=%d\n",
882 hList, nItemPos, lpBuffer, nBufferSize, datasize);
883 return datasize;
884 }
885
886
887 /**************************************************************************
888 * Str_GetPtrA [COMCTL32.233]
889 *
890 * Copies a string into a destination buffer.
891 *
892 * PARAMS
893 * lpSrc [I] Source string
894 * lpDest [O] Destination buffer
895 * nMaxLen [I] Size of buffer in characters
896 *
897 * RETURNS
898 * The number of characters copied.
899 */
900 INT WINAPI Str_GetPtrA (LPCSTR lpSrc, LPSTR lpDest, INT nMaxLen)
901 {
902 INT len;
903
904 TRACE("(%p %p %d)\n", lpSrc, lpDest, nMaxLen);
905
906 if (!lpDest && lpSrc)
907 return strlen (lpSrc);
908
909 if (nMaxLen == 0)
910 return 0;
911
912 if (lpSrc == NULL) {
913 lpDest[0] = '\0';
914 return 0;
915 }
916
917 len = strlen (lpSrc);
918 if (len >= nMaxLen)
919 len = nMaxLen - 1;
920
921 RtlMoveMemory (lpDest, lpSrc, len);
922 lpDest[len] = '\0';
923
924 return len;
925 }
926
927
928 /**************************************************************************
929 * Str_SetPtrA [COMCTL32.234]
930 *
931 * Makes a copy of a string, allocating memory if necessary.
932 *
933 * PARAMS
934 * lppDest [O] Pointer to destination string
935 * lpSrc [I] Source string
936 *
937 * RETURNS
938 * Success: TRUE
939 * Failure: FALSE
940 *
941 * NOTES
942 * Set lpSrc to NULL to free the memory allocated by a previous call
943 * to this function.
944 */
945 BOOL WINAPI Str_SetPtrA (LPSTR *lppDest, LPCSTR lpSrc)
946 {
947 TRACE("(%p %p)\n", lppDest, lpSrc);
948
949 if (lpSrc) {
950 LPSTR ptr = ReAlloc (*lppDest, strlen (lpSrc) + 1);
951 if (!ptr)
952 return FALSE;
953 strcpy (ptr, lpSrc);
954 *lppDest = ptr;
955 }
956 else {
957 if (*lppDest) {
958 Free (*lppDest);
959 *lppDest = NULL;
960 }
961 }
962
963 return TRUE;
964 }
965
966
967 /**************************************************************************
968 * Str_GetPtrW [COMCTL32.235]
969 *
970 * See Str_GetPtrA.
971 */
972 INT WINAPI Str_GetPtrW (LPCWSTR lpSrc, LPWSTR lpDest, INT nMaxLen)
973 {
974 INT len;
975
976 TRACE("(%p %p %d)\n", lpSrc, lpDest, nMaxLen);
977
978 if (!lpDest && lpSrc)
979 return strlenW (lpSrc);
980
981 if (nMaxLen == 0)
982 return 0;
983
984 if (lpSrc == NULL) {
985 lpDest[0] = L'\0';
986 return 0;
987 }
988
989 len = strlenW (lpSrc);
990 if (len >= nMaxLen)
991 len = nMaxLen - 1;
992
993 RtlMoveMemory (lpDest, lpSrc, len*sizeof(WCHAR));
994 lpDest[len] = L'\0';
995
996 return len;
997 }
998
999
1000 /**************************************************************************
1001 * Str_SetPtrW [COMCTL32.236]
1002 *
1003 * See Str_SetPtrA.
1004 */
1005 BOOL WINAPI Str_SetPtrW (LPWSTR *lppDest, LPCWSTR lpSrc)
1006 {
1007 TRACE("(%p %p)\n", lppDest, lpSrc);
1008
1009 if (lpSrc) {
1010 INT len = strlenW (lpSrc) + 1;
1011 LPWSTR ptr = ReAlloc (*lppDest, len * sizeof(WCHAR));
1012 if (!ptr)
1013 return FALSE;
1014 strcpyW (ptr, lpSrc);
1015 *lppDest = ptr;
1016 }
1017 else {
1018 if (*lppDest) {
1019 Free (*lppDest);
1020 *lppDest = NULL;
1021 }
1022 }
1023
1024 return TRUE;
1025 }
1026
1027
1028 /**************************************************************************
1029 * Str_GetPtrWtoA [internal]
1030 *
1031 * Converts a unicode string into a multi byte string
1032 *
1033 * PARAMS
1034 * lpSrc [I] Pointer to the unicode source string
1035 * lpDest [O] Pointer to caller supplied storage for the multi byte string
1036 * nMaxLen [I] Size, in bytes, of the destination buffer
1037 *
1038 * RETURNS
1039 * Length, in bytes, of the converted string.
1040 */
1041
1042 INT Str_GetPtrWtoA (LPCWSTR lpSrc, LPSTR lpDest, INT nMaxLen)
1043 {
1044 INT len;
1045
1046 TRACE("(%s %p %d)\n", debugstr_w(lpSrc), lpDest, nMaxLen);
1047
1048 if (!lpDest && lpSrc)
1049 return WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, 0, 0, NULL, NULL);
1050
1051 if (nMaxLen == 0)
1052 return 0;
1053
1054 if (lpSrc == NULL) {
1055 lpDest[0] = '\0';
1056 return 0;
1057 }
1058
1059 len = WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, 0, 0, NULL, NULL);
1060 if (len >= nMaxLen)
1061 len = nMaxLen - 1;
1062
1063 WideCharToMultiByte(CP_ACP, 0, lpSrc, -1, lpDest, len, NULL, NULL);
1064 lpDest[len] = '\0';
1065
1066 return len;
1067 }
1068
1069
1070 /**************************************************************************
1071 * Str_SetPtrAtoW [internal]
1072 *
1073 * Converts a multi byte string to a unicode string.
1074 * If the pointer to the destination buffer is NULL a buffer is allocated.
1075 * If the destination buffer is too small to keep the converted multi byte
1076 * string the destination buffer is reallocated. If the source pointer is
1077 * NULL, the destination buffer is freed.
1078 *
1079 * PARAMS
1080 * lppDest [I/O] pointer to a pointer to the destination buffer
1081 * lpSrc [I] pointer to a multi byte string
1082 *
1083 * RETURNS
1084 * TRUE: conversion successful
1085 * FALSE: error
1086 */
1087 BOOL Str_SetPtrAtoW (LPWSTR *lppDest, LPCSTR lpSrc)
1088 {
1089 TRACE("(%p %s)\n", lppDest, lpSrc);
1090
1091 if (lpSrc) {
1092 INT len = MultiByteToWideChar(CP_ACP,0,lpSrc,-1,NULL,0);
1093 LPWSTR ptr = ReAlloc (*lppDest, len*sizeof(WCHAR));
1094
1095 if (!ptr)
1096 return FALSE;
1097 MultiByteToWideChar(CP_ACP,0,lpSrc,-1,ptr,len);
1098 *lppDest = ptr;
1099 }
1100 else {
1101 if (*lppDest) {
1102 Free (*lppDest);
1103 *lppDest = NULL;
1104 }
1105 }
1106
1107 return TRUE;
1108 }
1109
1110 /**************************************************************************
1111 * Str_SetPtrWtoA [internal]
1112 *
1113 * Converts a unicode string to a multi byte string.
1114 * If the pointer to the destination buffer is NULL a buffer is allocated.
1115 * If the destination buffer is too small to keep the converted wide
1116 * string the destination buffer is reallocated. If the source pointer is
1117 * NULL, the destination buffer is freed.
1118 *
1119 * PARAMS
1120 * lppDest [I/O] pointer to a pointer to the destination buffer
1121 * lpSrc [I] pointer to a wide string
1122 *
1123 * RETURNS
1124 * TRUE: conversion successful
1125 * FALSE: error
1126 */
1127 BOOL Str_SetPtrWtoA (LPSTR *lppDest, LPCWSTR lpSrc)
1128 {
1129 TRACE("(%p %s)\n", lppDest, debugstr_w(lpSrc));
1130
1131 if (lpSrc) {
1132 INT len = WideCharToMultiByte(CP_ACP,0,lpSrc,-1,NULL,0,NULL,FALSE);
1133 LPSTR ptr = ReAlloc (*lppDest, len*sizeof(CHAR));
1134
1135 if (!ptr)
1136 return FALSE;
1137 WideCharToMultiByte(CP_ACP,0,lpSrc,-1,ptr,len,NULL,FALSE);
1138 *lppDest = ptr;
1139 }
1140 else {
1141 if (*lppDest) {
1142 Free (*lppDest);
1143 *lppDest = NULL;
1144 }
1145 }
1146
1147 return TRUE;
1148 }
1149
1150
1151 /**************************************************************************
1152 * Notification functions
1153 */
1154
1155 typedef struct tagNOTIFYDATA
1156 {
1157 HWND hwndFrom;
1158 HWND hwndTo;
1159 DWORD dwParam3;
1160 DWORD dwParam4;
1161 DWORD dwParam5;
1162 DWORD dwParam6;
1163 } NOTIFYDATA, *LPNOTIFYDATA;
1164
1165
1166 /**************************************************************************
1167 * DoNotify [Internal]
1168 */
1169
1170 static LRESULT DoNotify (LPNOTIFYDATA lpNotify, UINT uCode, LPNMHDR lpHdr)
1171 {
1172 NMHDR nmhdr;
1173 LPNMHDR lpNmh = NULL;
1174 UINT idFrom = 0;
1175
1176 TRACE("(%p %p %d %p 0x%08lx)\n",
1177 lpNotify->hwndFrom, lpNotify->hwndTo, uCode, lpHdr,
1178 lpNotify->dwParam5);
1179
1180 if (!lpNotify->hwndTo)
1181 return 0;
1182
1183 if (lpNotify->hwndFrom == (HWND)-1) {
1184 lpNmh = lpHdr;
1185 idFrom = lpHdr->idFrom;
1186 }
1187 else {
1188 if (lpNotify->hwndFrom)
1189 idFrom = GetDlgCtrlID (lpNotify->hwndFrom);
1190
1191 lpNmh = (lpHdr) ? lpHdr : &nmhdr;
1192
1193 lpNmh->hwndFrom = lpNotify->hwndFrom;
1194 lpNmh->idFrom = idFrom;
1195 lpNmh->code = uCode;
1196 }
1197
1198 return SendMessageW (lpNotify->hwndTo, WM_NOTIFY, idFrom, (LPARAM)lpNmh);
1199 }
1200
1201
1202 /**************************************************************************
1203 * SendNotify [COMCTL32.341]
1204 *
1205 * Sends a WM_NOTIFY message to the specified window.
1206 *
1207 * PARAMS
1208 * hwndTo [I] Window to receive the message
1209 * hwndFrom [I] Window that the message is from (see notes)
1210 * uCode [I] Notification code
1211 * lpHdr [I] The NMHDR and any additional information to send or NULL
1212 *
1213 * RETURNS
1214 * Success: return value from notification
1215 * Failure: 0
1216 *
1217 * NOTES
1218 * If hwndFrom is -1 then the identifier of the control sending the
1219 * message is taken from the NMHDR structure.
1220 * If hwndFrom is not -1 then lpHdr can be NULL.
1221 */
1222 LRESULT WINAPI SendNotify (HWND hwndTo, HWND hwndFrom, UINT uCode, LPNMHDR lpHdr)
1223 {
1224 NOTIFYDATA notify;
1225
1226 TRACE("(%p %p %d %p)\n",
1227 hwndTo, hwndFrom, uCode, lpHdr);
1228
1229 notify.hwndFrom = hwndFrom;
1230 notify.hwndTo = hwndTo;
1231 notify.dwParam5 = 0;
1232 notify.dwParam6 = 0;
1233
1234 return DoNotify (&notify, uCode, lpHdr);
1235 }
1236
1237
1238 /**************************************************************************
1239 * SendNotifyEx [COMCTL32.342]
1240 *
1241 * Sends a WM_NOTIFY message to the specified window.
1242 *
1243 * PARAMS
1244 * hwndFrom [I] Window to receive the message
1245 * hwndTo [I] Window that the message is from
1246 * uCode [I] Notification code
1247 * lpHdr [I] The NMHDR and any additional information to send or NULL
1248 * dwParam5 [I] Unknown
1249 *
1250 * RETURNS
1251 * Success: return value from notification
1252 * Failure: 0
1253 *
1254 * NOTES
1255 * If hwndFrom is -1 then the identifier of the control sending the
1256 * message is taken from the NMHDR structure.
1257 * If hwndFrom is not -1 then lpHdr can be NULL.
1258 */
1259 LRESULT WINAPI SendNotifyEx (HWND hwndTo, HWND hwndFrom, UINT uCode,
1260 LPNMHDR lpHdr, DWORD dwParam5)
1261 {
1262 NOTIFYDATA notify;
1263 HWND hwndNotify;
1264
1265 TRACE("(%p %p %d %p 0x%08lx)\n",
1266 hwndFrom, hwndTo, uCode, lpHdr, dwParam5);
1267
1268 hwndNotify = hwndTo;
1269 if (!hwndTo) {
1270 if (IsWindow (hwndFrom)) {
1271 hwndNotify = GetParent (hwndFrom);
1272 if (!hwndNotify)
1273 return 0;
1274 }
1275 }
1276
1277 notify.hwndFrom = hwndFrom;
1278 notify.hwndTo = hwndNotify;
1279 notify.dwParam5 = dwParam5;
1280 notify.dwParam6 = 0;
1281
1282 return DoNotify (&notify, uCode, lpHdr);
1283 }