Print retval in decimal to avoid confusion.
[wine] / dlls / shell32 / brsfolder.c
1 /*
2  * Copyright 1999 Juergen Schmied
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * FIXME:
19  *  - many memory leaks
20  *  - many flags unimplemented
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #define COBJMACROS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29
30 #include "wine/debug.h"
31 #include "undocshell.h"
32 #include "shlguid.h"
33 #include "pidl.h"
34 #include "shell32_main.h"
35 #include "shellapi.h"
36 #include "shresdef.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
39
40 static HWND             hwndTreeView;
41 static LPBROWSEINFOW    lpBrowseInfo;
42 static LPITEMIDLIST     pidlRet;
43
44 static void FillTreeView(LPSHELLFOLDER lpsf, LPITEMIDLIST  lpifq, HTREEITEM hParent, IEnumIDList* lpe);
45 static HTREEITEM InsertTreeViewItem(IShellFolder * lpsf, LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL, HTREEITEM hParent);
46
47 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
48                         BIF_BROWSEFORCOMPUTER | \
49                         BIF_RETURNFSANCESTORS | \
50                         BIF_RETURNONLYFSDIRS | \
51                         BIF_BROWSEINCLUDEFILES)
52
53 static inline DWORD BrowseFlagsToSHCONTF(UINT ulFlags)
54 {
55     return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
56 }
57
58 /******************************************************************************
59  * InitializeTreeView [Internal]
60  *
61  * Called from WM_INITDIALOG handler.
62  * 
63  * PARAMS
64  *  hwndParent [I] The BrowseForFolder dialog
65  *  root       [I] ITEMIDLIST of the root shell folder
66  */
67 static void InitializeTreeView(HWND hwndParent, LPCITEMIDLIST root)
68 {
69     LPITEMIDLIST pidlParent, pidlChild;
70     HIMAGELIST hImageList;
71     HRESULT hr;
72     IShellFolder *lpsfParent, *lpsfRoot;
73     IEnumIDList * pEnumChildren = NULL;
74
75     TRACE("dlg=%p tree=%p\n", hwndParent, hwndTreeView );
76     
77     hwndTreeView = GetDlgItem (hwndParent, IDD_TREEVIEW);
78     if (!hwndTreeView) {
79         FIXME("Could not get handle to treeview control! Error: %08lx\n", GetLastError());
80         return;
81     }
82     Shell_GetImageList(NULL, &hImageList);
83
84     if (hImageList)
85         TreeView_SetImageList(hwndTreeView, hImageList, 0);
86
87     /* We want to call InsertTreeViewItem down the code, in order to insert
88      * the root item of the treeview. Due to InsertTreeViewItem's signature, 
89      * we need the following to do this:
90      *
91      * + An ITEMIDLIST corresponding to _the parent_ of root. 
92      * + An ITEMIDLIST, which is a relative path from root's parent to root 
93      *   (containing a single SHITEMID).
94      * + An IShellFolder interface pointer of root's parent folder.
95      *
96      * If root is 'Desktop', then root's parent is also 'Desktop'.
97      */
98
99     pidlParent = ILClone(root);
100     ILRemoveLastID(pidlParent);
101     pidlChild = ILClone(ILFindLastID(root));
102     
103     if (_ILIsDesktop(pidlParent)) {
104         hr = SHGetDesktopFolder(&lpsfParent);
105     } else {
106         IShellFolder *lpsfDesktop;
107         hr = SHGetDesktopFolder(&lpsfDesktop);
108         if (!SUCCEEDED(hr)) {
109             WARN("SHGetDesktopFolder failed! hr = %08lx\n", hr);
110             return;
111         }
112         hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
113         IShellFolder_Release(lpsfDesktop);
114     }
115     
116     if (!SUCCEEDED(hr)) {
117         WARN("Could not bind to parent shell folder! hr = %08lx\n", hr);
118         return;
119     }
120
121     if (pidlChild && pidlChild->mkid.cb) {
122         hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
123     } else {
124         lpsfRoot = lpsfParent;
125         hr = IShellFolder_AddRef(lpsfParent);
126     }
127     
128     if (!SUCCEEDED(hr)) {
129         WARN("Could not bind to root shell folder! hr = %08lx\n", hr);
130         IShellFolder_Release(lpsfParent);
131         return;
132     }
133
134     hr = IShellFolder_EnumObjects(lpsfRoot, hwndParent, BrowseFlagsToSHCONTF(lpBrowseInfo->ulFlags), &pEnumChildren);
135     if (!SUCCEEDED(hr)) {
136         WARN("Could not get child iterator! hr = %08lx\n", hr);
137         IShellFolder_Release(lpsfParent);
138         IShellFolder_Release(lpsfRoot);
139         return;
140     }
141
142     TreeView_DeleteAllItems(hwndTreeView);
143     TreeView_Expand(hwndTreeView, InsertTreeViewItem(lpsfParent, pidlChild, pidlParent, pEnumChildren,  TVI_ROOT), TVE_EXPAND);
144
145     IShellFolder_Release(lpsfRoot);
146     IShellFolder_Release(lpsfParent);
147 }
148
149 static int GetIcon(LPITEMIDLIST lpi, UINT uFlags)
150 {
151         SHFILEINFOW    sfi;
152         SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
153         return sfi.iIcon;
154 }
155
156 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
157 {
158         LPITEMIDLIST pidlDesktop = NULL;
159
160         TRACE("%p %p\n",lpifq, lpTV_ITEM);
161
162         if (!lpifq)
163         {
164             pidlDesktop = _ILCreateDesktop();
165             lpifq = pidlDesktop;
166         }
167
168         lpTV_ITEM->iImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
169         lpTV_ITEM->iSelectedImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON);
170
171         if (pidlDesktop)
172             ILFree(pidlDesktop);
173
174         return;
175 }
176
177 typedef struct tagID
178 {
179    LPSHELLFOLDER lpsfParent; /* IShellFolder of the parent */
180    LPITEMIDLIST  lpi;        /* PIDL relativ to parent */
181    LPITEMIDLIST  lpifq;      /* Fully qualified PIDL */
182    IEnumIDList*  pEnumIL;    /* Children iterator */ 
183 } TV_ITEMDATA, *LPTV_ITEMDATA;
184
185 /******************************************************************************
186  * GetName [Internal]
187  *
188  * Query a shell folder for the display name of one of it's children
189  *
190  * PARAMS
191  *  lpsf           [I] IShellFolder interface of the folder to be queried.
192  *  lpi            [I] ITEMIDLIST of the child, relative to parent
193  *  dwFlags        [I] as in IShellFolder::GetDisplayNameOf
194  *  lpFriendlyName [O] The desired display name in unicode
195  *
196  * RETURNS
197  *  Success: TRUE
198  *  Failure: FALSE
199  */
200 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
201 {
202         BOOL   bSuccess=TRUE;
203         STRRET str;
204
205         TRACE("%p %p %lx %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
206         if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
207         {
208           if (FAILED(StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi)))
209           {
210               bSuccess = FALSE;
211           }
212         }
213         else
214           bSuccess = FALSE;
215
216         TRACE("-- %s\n", debugstr_w(lpFriendlyName));
217         return bSuccess;
218 }
219
220 /******************************************************************************
221  * InsertTreeViewItem [Internal]
222  *
223  * PARAMS
224  *  lpsf       [I] IShellFolder interface of the item's parent shell folder 
225  *  pidl       [I] ITEMIDLIST of the child to insert, relativ to parent 
226  *  pidlParent [I] ITEMIDLIST of the parent shell folder
227  *  pEnumIL    [I] Iterator for the children of the item to be inserted
228  *  hParent    [I] The treeview-item that represents the parent shell folder
229  *
230  * RETURNS
231  *  Success: Handle to the created and inserted treeview-item
232  *  Failure: NULL
233  */
234 static HTREEITEM InsertTreeViewItem(IShellFolder * lpsf, LPCITEMIDLIST pidl, 
235     LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL, HTREEITEM hParent)
236 {
237         TVITEMW         tvi;
238         TVINSERTSTRUCTW tvins;
239         WCHAR           szBuff[MAX_PATH];
240         LPTV_ITEMDATA   lptvid=0;
241
242         tvi.mask  = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
243
244         tvi.cChildren= pEnumIL ? 1 : 0;
245         tvi.mask |= TVIF_CHILDREN;
246
247         if (!(lptvid = (LPTV_ITEMDATA)SHAlloc(sizeof(TV_ITEMDATA))))
248             return NULL;
249
250         if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
251             return NULL;
252
253         tvi.pszText    = szBuff;
254         tvi.cchTextMax = MAX_PATH;
255         tvi.lParam = (LPARAM)lptvid;
256
257         IShellFolder_AddRef(lpsf);
258         lptvid->lpsfParent = lpsf;
259         lptvid->lpi     = ILClone(pidl);
260         lptvid->lpifq   = pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
261         lptvid->pEnumIL = pEnumIL;
262         GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
263
264         tvins.u.item       = tvi;
265         tvins.hInsertAfter = NULL;
266         tvins.hParent      = hParent;
267
268         return (HTREEITEM)TreeView_InsertItemW(hwndTreeView, &tvins);
269 }
270
271 /******************************************************************************
272  * FillTreeView [Internal]
273  *
274  * For each child (given by lpe) of the parent shell folder, which is given by 
275  * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
276  *
277  * PARAMS
278  *  lpsf    [I] IShellFolder interface of the parent shell folder
279  *  pidl    [I] ITEMIDLIST of the parent shell folder
280  *  hParent [I] The treeview item that represents the parent shell folder
281  *  lpe     [I] An iterator for the children of the parent shell folder
282  */
283 static void FillTreeView(IShellFolder * lpsf, LPITEMIDLIST  pidl, HTREEITEM hParent, IEnumIDList* lpe)
284 {
285         HTREEITEM       hPrev = 0;
286         LPITEMIDLIST    pidlTemp = 0;
287         ULONG           ulFetched;
288         HRESULT         hr;
289         HWND            hwnd=GetParent(hwndTreeView);
290
291         TRACE("%p %p %x %p\n",lpsf, pidl, (INT)hParent, lpe);
292
293         /* No IEnumIDList -> No children */
294         if (!lpe) return;
295         
296         SetCapture(GetParent(hwndTreeView));
297         SetCursor(LoadCursorA(0, (LPSTR)IDC_WAIT));
298
299         while (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
300         {
301             ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
302             IEnumIDList* pEnumIL = NULL;
303             IShellFolder* pSFChild = NULL;
304             IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
305             if (ulAttrs & SFGAO_FOLDER)
306             {
307                 hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
308                 if (SUCCEEDED(hr))
309                 {
310                     hr = IShellFolder_EnumObjects(pSFChild, hwnd, BrowseFlagsToSHCONTF(lpBrowseInfo->ulFlags), &pEnumIL);
311                     if (SUCCEEDED(hr))
312                     {
313                         if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) || FAILED(IEnumIDList_Reset(pEnumIL)))
314                         {
315                             IEnumIDList_Release(pEnumIL);
316                             pEnumIL = NULL;
317                         }
318                     }
319                     IShellFolder_Release(pSFChild);
320                 }
321             }
322
323             if (!(hPrev = InsertTreeViewItem(lpsf, pidlTemp, pidl, pEnumIL, hParent)))
324                 goto Done;
325             SHFree(pidlTemp);  /* Finally, free the pidl that the shell gave us... */
326             pidlTemp=NULL;
327         }
328
329 Done:
330         ReleaseCapture();
331         SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
332
333         if (pidlTemp)
334           SHFree(pidlTemp);
335 }
336
337 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
338 {
339     LPPIDLDATA data = _ILGetDataPointer(pidl);
340     if (!data)
341         return FALSE;
342     return (data->type == type);
343 }
344
345 static void BrsFolder_CheckValidSelection(HWND hWndTree, LPTV_ITEMDATA lptvid)
346 {
347     LPCITEMIDLIST pidl = lptvid->lpi;
348     BOOL bEnabled = TRUE;
349     DWORD dwAttributes;
350     if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
351         !PIDLIsType(pidl, PT_COMP))
352         bEnabled = FALSE;
353     if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
354     {
355         dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
356         if (FAILED(IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1, (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes)) ||
357             !dwAttributes)
358             bEnabled = FALSE;
359     }
360     if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
361     {
362         dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
363         if (FAILED(IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1, (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes)) ||
364             (dwAttributes != (SFGAO_FOLDER | SFGAO_FILESYSTEM)))
365             bEnabled = FALSE;
366     }
367     SendMessageW(hWndTree, BFFM_ENABLEOK, 0, (LPARAM)bEnabled);
368 }
369
370 static LRESULT MsgNotify(HWND hWnd,  UINT CtlID, LPNMHDR lpnmh)
371 {
372         NMTREEVIEWW     *pnmtv   = (NMTREEVIEWW *)lpnmh;
373         LPTV_ITEMDATA   lptvid;  /* Long pointer to TreeView item data */
374         IShellFolder *  lpsf2=0;
375
376
377         TRACE("%p %x %p msg=%x\n", hWnd,  CtlID, lpnmh, pnmtv->hdr.code);
378
379         switch (pnmtv->hdr.idFrom)
380         { case IDD_TREEVIEW:
381             switch (pnmtv->hdr.code)
382             {
383               case TVN_DELETEITEMA:
384               case TVN_DELETEITEMW:
385                 TRACE("TVN_DELETEITEMA/W\n");
386                 lptvid=(LPTV_ITEMDATA)pnmtv->itemOld.lParam;
387                 IShellFolder_Release(lptvid->lpsfParent);
388                 if (lptvid->pEnumIL)
389                   IEnumIDList_Release(lptvid->pEnumIL);
390                 SHFree(lptvid->lpi);
391                 SHFree(lptvid->lpifq);
392                 SHFree(lptvid);
393                 break;
394
395               case TVN_ITEMEXPANDINGA:
396               case TVN_ITEMEXPANDINGW:
397                 {
398                   TRACE("TVN_ITEMEXPANDINGA/W\n");
399                   if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
400                     break;
401
402                   lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
403                   if (SUCCEEDED(IShellFolder_BindToObject(lptvid->lpsfParent, lptvid->lpi,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
404                   { FillTreeView( lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
405                   }
406                   /* My Computer is already sorted and trying to do a simple text
407                    * sort will only mess things up */
408                   if (!_ILIsMyComputer(lptvid->lpi))
409                     TreeView_SortChildren(hwndTreeView, pnmtv->itemNew.hItem, FALSE);
410                 }
411                 break;
412               case TVN_SELCHANGEDA:
413               case TVN_SELCHANGEDW:
414                 lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
415                 pidlRet = lptvid->lpifq;
416                 if (lpBrowseInfo->lpfn)
417                    (lpBrowseInfo->lpfn)(hWnd, BFFM_SELCHANGED, (LPARAM)pidlRet, lpBrowseInfo->lParam);
418                 BrsFolder_CheckValidSelection(hWnd, lptvid);
419                 break;
420
421               default:
422                 WARN("unhandled (%d)\n", pnmtv->hdr.code);
423                 break;
424             }
425             break;
426
427           default:
428             break;
429         }
430
431         return 0;
432 }
433
434
435 /*************************************************************************
436  *             BrsFolderDlgProc32  (not an exported API function)
437  */
438 static INT_PTR CALLBACK BrsFolderDlgProc(HWND hWnd, UINT msg, WPARAM wParam,
439                                      LPARAM lParam )
440 {
441         TRACE("hwnd=%p msg=%04x 0x%08x 0x%08lx\n", hWnd,  msg, wParam, lParam );
442
443         switch(msg)
444         { case WM_INITDIALOG:
445             pidlRet = NULL;
446             lpBrowseInfo = (LPBROWSEINFOW) lParam;
447             if (lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS)
448               FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS);
449             if (lpBrowseInfo->lpszTitle) {
450                SetWindowTextW(GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle);
451             } else {
452                ShowWindow(GetDlgItem(hWnd, IDD_TITLE), SW_HIDE);
453             }
454             if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT))
455                ShowWindow(GetDlgItem(hWnd, IDD_STATUS), SW_HIDE);
456
457             InitializeTreeView(hWnd, lpBrowseInfo->pidlRoot);
458
459             if (lpBrowseInfo->lpfn)
460                (lpBrowseInfo->lpfn)(hWnd, BFFM_INITIALIZED, 0, lpBrowseInfo->lParam);
461
462             return TRUE;
463
464           case WM_NOTIFY:
465             MsgNotify( hWnd, (UINT)wParam, (LPNMHDR)lParam);
466             break;
467
468           case WM_COMMAND:
469             switch (wParam)
470             { case IDOK:
471                 pdump ( pidlRet );
472                 if (lpBrowseInfo->pszDisplayName)
473                     SHGetPathFromIDListW(pidlRet, lpBrowseInfo->pszDisplayName);
474                 EndDialog(hWnd, (DWORD) ILClone(pidlRet));
475                 return TRUE;
476
477               case IDCANCEL:
478                 EndDialog(hWnd, 0);
479                 return TRUE;
480             }
481             break;
482         case BFFM_SETSTATUSTEXTA:
483            TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
484            SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
485            break;
486         case BFFM_SETSTATUSTEXTW:
487            TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
488            SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
489            break;
490         case BFFM_ENABLEOK:
491            TRACE("Enable %ld\n", lParam);
492            EnableWindow(GetDlgItem(hWnd, 1), (lParam)?TRUE:FALSE);
493            break;
494         case BFFM_SETOKTEXT: /* unicode only */
495            TRACE("Set OK text %s\n", debugstr_w((LPWSTR)wParam));
496            SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)wParam);
497            break;
498         case BFFM_SETSELECTIONA:
499            if (wParam)
500               FIXME("Set selection %s\n", debugstr_a((LPSTR)lParam));
501            else
502               FIXME("Set selection %p\n", (void*)lParam);
503            break;
504         case BFFM_SETSELECTIONW:
505            if (wParam)
506               FIXME("Set selection %s\n", debugstr_w((LPWSTR)lParam));
507            else
508               FIXME("Set selection %p\n", (void*)lParam);
509            break;
510         case BFFM_SETEXPANDED: /* unicode only */
511            if (wParam)
512               FIXME("Set expanded %s\n", debugstr_w((LPWSTR)lParam));
513            else
514               FIXME("Set expanded %p\n", (void*)lParam);
515            break;
516         }
517         return FALSE;
518 }
519
520 static const WCHAR swBrowseTempName[] = {'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
521
522 /*************************************************************************
523  * SHBrowseForFolderA [SHELL32.@]
524  * SHBrowseForFolder  [SHELL32.@]
525  */
526 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
527 {
528         BROWSEINFOW bi;
529         LPITEMIDLIST lpid;
530         INT len;
531         
532         TRACE("(%p{lpszTitle=%s,owner=%p})\n", lpbi,
533             lpbi ? debugstr_a(lpbi->lpszTitle) : NULL, lpbi ? lpbi->hwndOwner : NULL);
534
535         if (!lpbi)
536           return NULL;
537
538         bi.hwndOwner = lpbi->hwndOwner;
539         bi.pidlRoot = lpbi->pidlRoot;
540         if (lpbi->pszDisplayName)
541         {
542           /*lpbi->pszDisplayName is assumed to be MAX_PATH (MSDN) */
543           bi.pszDisplayName = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
544           MultiByteToWideChar(CP_ACP, 0, lpbi->pszDisplayName, -1, bi.pszDisplayName, MAX_PATH);
545         }
546         else
547           bi.pszDisplayName = NULL;
548
549         if (lpbi->lpszTitle)
550         {
551           len = MultiByteToWideChar(CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0);
552           bi.lpszTitle = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
553           MultiByteToWideChar(CP_ACP, 0, lpbi->lpszTitle, -1, (LPWSTR)bi.lpszTitle, len);
554         }
555         else
556           bi.lpszTitle = NULL;
557
558         bi.ulFlags = lpbi->ulFlags;
559         bi.lpfn = lpbi->lpfn;
560         bi.lParam = lpbi->lParam;
561         bi.iImage = lpbi->iImage;
562         lpid = (LPITEMIDLIST) DialogBoxParamW(shell32_hInstance,
563                                               swBrowseTempName, lpbi->hwndOwner,
564                                               BrsFolderDlgProc, (INT)&bi);
565         if (bi.pszDisplayName)
566         {
567           WideCharToMultiByte(CP_ACP, 0, bi.pszDisplayName, -1, lpbi->pszDisplayName, MAX_PATH, 0, NULL);
568           HeapFree(GetProcessHeap(), 0, bi.pszDisplayName);
569         }
570         HeapFree(GetProcessHeap(), 0, (LPVOID)bi.lpszTitle);
571         lpbi->iImage = bi.iImage;
572         return lpid;
573 }
574
575
576 /*************************************************************************
577  * SHBrowseForFolderW [SHELL32.@]
578  */
579 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
580 {
581         TRACE("((%p->{lpszTitle=%s,owner=%p})\n", lpbi,
582             lpbi ? debugstr_w(lpbi->lpszTitle) : NULL, lpbi ? lpbi->hwndOwner : 0);
583
584         if (!lpbi)
585           return NULL;
586
587         return (LPITEMIDLIST) DialogBoxParamW(shell32_hInstance,
588                                               swBrowseTempName, lpbi->hwndOwner,
589                                               BrsFolderDlgProc, (INT)lpbi);
590 }