shell32: Start implementing BIF_USENEWUI.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  * FIXME:
19  *  - many memory leaks
20  *  - many flags unimplemented
21  *    - implement new dialog style "make new folder" button
22  *    - implement editbox
23  *    - implement new dialog style resizing
24  */
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #define COBJMACROS
30 #define NONAMELESSUNION
31 #define NONAMELESSSTRUCT
32
33 #include "wine/debug.h"
34 #include "undocshell.h"
35 #include "pidl.h"
36 #include "shell32_main.h"
37 #include "shellapi.h"
38 #include "shresdef.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(shell);
41
42 typedef struct tagbrowse_info
43 {
44     HWND          hWnd;
45     HWND          hwndTreeView;
46     LPBROWSEINFOW lpBrowseInfo;
47     LPITEMIDLIST  pidlRet;
48 } browse_info;
49
50 typedef struct tagTV_ITEMDATA
51 {
52    LPSHELLFOLDER lpsfParent; /* IShellFolder of the parent */
53    LPITEMIDLIST  lpi;        /* PIDL relativ to parent */
54    LPITEMIDLIST  lpifq;      /* Fully qualified PIDL */
55    IEnumIDList*  pEnumIL;    /* Children iterator */ 
56 } TV_ITEMDATA, *LPTV_ITEMDATA;
57
58 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
59                         BIF_BROWSEFORCOMPUTER | \
60                         BIF_RETURNFSANCESTORS | \
61                         BIF_RETURNONLYFSDIRS | \
62                         BIF_NONEWFOLDERBUTTON | \
63                         BIF_NEWDIALOGSTYLE | \
64                         BIF_BROWSEINCLUDEFILES)
65
66 static void FillTreeView(browse_info*, LPSHELLFOLDER,
67                LPITEMIDLIST, HTREEITEM, IEnumIDList*);
68 static HTREEITEM InsertTreeViewItem( browse_info*, IShellFolder *,
69                LPCITEMIDLIST, LPCITEMIDLIST, IEnumIDList*, HTREEITEM);
70
71 static const WCHAR szBrowseFolderInfo[] = {
72     '_','_','W','I','N','E','_',
73     'B','R','S','F','O','L','D','E','R','D','L','G','_',
74     'I','N','F','O',0
75 };
76
77 static inline DWORD BrowseFlagsToSHCONTF(UINT ulFlags)
78 {
79     return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
80 }
81
82 static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo, HWND hWnd,
83                                    UINT msg, LPARAM param )
84 {
85     if (!lpBrowseInfo->lpfn)
86         return;
87     lpBrowseInfo->lpfn( hWnd, msg, param, lpBrowseInfo->lParam );
88 }
89
90 /******************************************************************************
91  * InitializeTreeView [Internal]
92  *
93  * Called from WM_INITDIALOG handler.
94  * 
95  * PARAMS
96  *  hwndParent [I] The BrowseForFolder dialog
97  *  root       [I] ITEMIDLIST of the root shell folder
98  */
99 static void InitializeTreeView( browse_info *info )
100 {
101     LPITEMIDLIST pidlParent, pidlChild;
102     HIMAGELIST hImageList;
103     HRESULT hr;
104     IShellFolder *lpsfParent, *lpsfRoot;
105     IEnumIDList * pEnumChildren = NULL;
106     HTREEITEM item;
107     DWORD flags;
108     LPCITEMIDLIST root = info->lpBrowseInfo->pidlRoot;
109
110     TRACE("%p\n", info );
111     
112     Shell_GetImageList(NULL, &hImageList);
113
114     if (hImageList)
115         SendMessageW( info->hwndTreeView, TVM_SETIMAGELIST, 0, (LPARAM)hImageList );
116
117     /* We want to call InsertTreeViewItem down the code, in order to insert
118      * the root item of the treeview. Due to InsertTreeViewItem's signature, 
119      * we need the following to do this:
120      *
121      * + An ITEMIDLIST corresponding to _the parent_ of root. 
122      * + An ITEMIDLIST, which is a relative path from root's parent to root 
123      *   (containing a single SHITEMID).
124      * + An IShellFolder interface pointer of root's parent folder.
125      *
126      * If root is 'Desktop', then root's parent is also 'Desktop'.
127      */
128
129     pidlParent = ILClone(root);
130     ILRemoveLastID(pidlParent);
131     pidlChild = ILClone(ILFindLastID(root));
132     
133     if (_ILIsDesktop(pidlParent)) {
134         hr = SHGetDesktopFolder(&lpsfParent);
135     } else {
136         IShellFolder *lpsfDesktop;
137         hr = SHGetDesktopFolder(&lpsfDesktop);
138         if (!SUCCEEDED(hr)) {
139             WARN("SHGetDesktopFolder failed! hr = %08x\n", hr);
140             return;
141         }
142         hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
143         IShellFolder_Release(lpsfDesktop);
144     }
145     
146     if (!SUCCEEDED(hr)) {
147         WARN("Could not bind to parent shell folder! hr = %08x\n", hr);
148         return;
149     }
150
151     if (pidlChild && pidlChild->mkid.cb) {
152         hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
153     } else {
154         lpsfRoot = lpsfParent;
155         hr = IShellFolder_AddRef(lpsfParent);
156     }
157     
158     if (!SUCCEEDED(hr)) {
159         WARN("Could not bind to root shell folder! hr = %08x\n", hr);
160         IShellFolder_Release(lpsfParent);
161         return;
162     }
163
164     flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
165     hr = IShellFolder_EnumObjects( lpsfRoot, info->hWnd, flags, &pEnumChildren );
166     if (!SUCCEEDED(hr)) {
167         WARN("Could not get child iterator! hr = %08x\n", hr);
168         IShellFolder_Release(lpsfParent);
169         IShellFolder_Release(lpsfRoot);
170         return;
171     }
172
173     SendMessageW( info->hwndTreeView, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT );
174     item = InsertTreeViewItem( info, lpsfParent, pidlChild,
175                                pidlParent, pEnumChildren, TVI_ROOT );
176     SendMessageW( info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item );
177
178     IShellFolder_Release(lpsfRoot);
179     IShellFolder_Release(lpsfParent);
180 }
181
182 static int GetIcon(LPITEMIDLIST lpi, UINT uFlags)
183 {
184     SHFILEINFOW sfi;
185     SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
186     return sfi.iIcon;
187 }
188
189 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
190 {
191     LPITEMIDLIST pidlDesktop = NULL;
192     DWORD flags;
193
194     TRACE("%p %p\n",lpifq, lpTV_ITEM);
195
196     if (!lpifq)
197     {
198         pidlDesktop = _ILCreateDesktop();
199         lpifq = pidlDesktop;
200     }
201
202     flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
203     lpTV_ITEM->iImage = GetIcon( lpifq, flags );
204
205     flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
206     lpTV_ITEM->iSelectedImage = GetIcon( lpifq, flags );
207
208     if (pidlDesktop)
209         ILFree( pidlDesktop );
210 }
211
212 /******************************************************************************
213  * GetName [Internal]
214  *
215  * Query a shell folder for the display name of one of it's children
216  *
217  * PARAMS
218  *  lpsf           [I] IShellFolder interface of the folder to be queried.
219  *  lpi            [I] ITEMIDLIST of the child, relative to parent
220  *  dwFlags        [I] as in IShellFolder::GetDisplayNameOf
221  *  lpFriendlyName [O] The desired display name in unicode
222  *
223  * RETURNS
224  *  Success: TRUE
225  *  Failure: FALSE
226  */
227 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
228 {
229         BOOL   bSuccess=TRUE;
230         STRRET str;
231
232         TRACE("%p %p %x %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
233         if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
234           bSuccess = StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi);
235         else
236           bSuccess = FALSE;
237
238         TRACE("-- %s\n", debugstr_w(lpFriendlyName));
239         return bSuccess;
240 }
241
242 /******************************************************************************
243  * InsertTreeViewItem [Internal]
244  *
245  * PARAMS
246  *  info       [I] data for the dialog
247  *  lpsf       [I] IShellFolder interface of the item's parent shell folder 
248  *  pidl       [I] ITEMIDLIST of the child to insert, relativ to parent 
249  *  pidlParent [I] ITEMIDLIST of the parent shell folder
250  *  pEnumIL    [I] Iterator for the children of the item to be inserted
251  *  hParent    [I] The treeview-item that represents the parent shell folder
252  *
253  * RETURNS
254  *  Success: Handle to the created and inserted treeview-item
255  *  Failure: NULL
256  */
257 static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
258     LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
259     HTREEITEM hParent)
260 {
261         TVITEMW         tvi;
262         TVINSERTSTRUCTW tvins;
263         WCHAR           szBuff[MAX_PATH];
264         LPTV_ITEMDATA   lptvid=0;
265
266         tvi.mask  = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
267
268         tvi.cChildren= pEnumIL ? 1 : 0;
269         tvi.mask |= TVIF_CHILDREN;
270
271         lptvid = SHAlloc( sizeof(TV_ITEMDATA) );
272         if (!lptvid)
273             return NULL;
274
275         if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
276             return NULL;
277
278         tvi.pszText    = szBuff;
279         tvi.cchTextMax = MAX_PATH;
280         tvi.lParam = (LPARAM)lptvid;
281
282         IShellFolder_AddRef(lpsf);
283         lptvid->lpsfParent = lpsf;
284         lptvid->lpi     = ILClone(pidl);
285         lptvid->lpifq   = pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
286         lptvid->pEnumIL = pEnumIL;
287         GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
288
289         tvins.u.item       = tvi;
290         tvins.hInsertAfter = NULL;
291         tvins.hParent      = hParent;
292
293         return (HTREEITEM)TreeView_InsertItemW( info->hwndTreeView, &tvins );
294 }
295
296 /******************************************************************************
297  * FillTreeView [Internal]
298  *
299  * For each child (given by lpe) of the parent shell folder, which is given by 
300  * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
301  *
302  * PARAMS
303  *  info    [I] data for the dialog
304  *  lpsf    [I] IShellFolder interface of the parent shell folder
305  *  pidl    [I] ITEMIDLIST of the parent shell folder
306  *  hParent [I] The treeview item that represents the parent shell folder
307  *  lpe     [I] An iterator for the children of the parent shell folder
308  */
309 static void FillTreeView( browse_info *info, IShellFolder * lpsf,
310                  LPITEMIDLIST  pidl, HTREEITEM hParent, IEnumIDList* lpe)
311 {
312         HTREEITEM       hPrev = 0;
313         LPITEMIDLIST    pidlTemp = 0;
314         ULONG           ulFetched;
315         HRESULT         hr;
316         HWND            hwnd = GetParent( info->hwndTreeView );
317
318         TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
319
320         /* No IEnumIDList -> No children */
321         if (!lpe) return;
322         
323         SetCapture( hwnd );
324         SetCursor( LoadCursorA( 0, (LPSTR)IDC_WAIT ) );
325
326         while (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
327         {
328             ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
329             IEnumIDList* pEnumIL = NULL;
330             IShellFolder* pSFChild = NULL;
331             IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
332             if (ulAttrs & SFGAO_FOLDER)
333             {
334                 hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
335                 if (SUCCEEDED(hr))
336                 {
337                     DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
338                     hr = IShellFolder_EnumObjects(pSFChild, hwnd, flags, &pEnumIL);
339                     if (hr == S_OK)
340                     {
341                         if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) ||
342                              FAILED(IEnumIDList_Reset(pEnumIL)))
343                         {
344                             IEnumIDList_Release(pEnumIL);
345                             pEnumIL = NULL;
346                         }
347                     }
348                     IShellFolder_Release(pSFChild);
349                 }
350             }
351
352             if (!(hPrev = InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent)))
353                 goto done;
354             SHFree(pidlTemp);  /* Finally, free the pidl that the shell gave us... */
355             pidlTemp=NULL;
356         }
357
358 done:
359         ReleaseCapture();
360         SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
361     SHFree(pidlTemp);
362 }
363
364 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
365 {
366     LPPIDLDATA data = _ILGetDataPointer(pidl);
367     if (!data)
368         return FALSE;
369     return (data->type == type);
370 }
371
372 static void BrsFolder_CheckValidSelection( browse_info *info, LPTV_ITEMDATA lptvid )
373 {
374     LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
375     LPCITEMIDLIST pidl = lptvid->lpi;
376     BOOL bEnabled = TRUE;
377     DWORD dwAttributes;
378     HRESULT r;
379
380     if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
381         !PIDLIsType(pidl, PT_COMP))
382         bEnabled = FALSE;
383     if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
384     {
385         dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
386         r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
387                                 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
388         if (FAILED(r) || !(dwAttributes & (SFGAO_FILESYSANCESTOR|SFGAO_FILESYSTEM)))
389             bEnabled = FALSE;
390     }
391     if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
392     {
393         dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
394         r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
395                                 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
396         if (FAILED(r) || 
397             ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
398         {
399             bEnabled = FALSE;
400         }
401     }
402     SendMessageW(info->hWnd, BFFM_ENABLEOK, 0, (LPARAM)bEnabled);
403 }
404
405 static LRESULT BrsFolder_Treeview_Delete( browse_info *info, NMTREEVIEWW *pnmtv )
406 {
407     LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA)pnmtv->itemOld.lParam;
408
409     TRACE("TVN_DELETEITEMA/W %p\n", lptvid);
410
411     IShellFolder_Release(lptvid->lpsfParent);
412     if (lptvid->pEnumIL)
413         IEnumIDList_Release(lptvid->pEnumIL);
414     SHFree(lptvid->lpi);
415     SHFree(lptvid->lpifq);
416     SHFree(lptvid);
417     return 0;
418 }
419
420 static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
421 {
422     IShellFolder *lpsf2 = NULL;
423     LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
424     HRESULT r;
425
426     TRACE("TVN_ITEMEXPANDINGA/W\n");
427
428     if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
429         return 0;
430
431     if (lptvid->lpi && lptvid->lpi->mkid.cb) {
432         r = IShellFolder_BindToObject( lptvid->lpsfParent, lptvid->lpi, 0,
433                                       (REFIID)&IID_IShellFolder, (LPVOID *)&lpsf2 );
434     } else {
435         lpsf2 = lptvid->lpsfParent;
436         r = IShellFolder_AddRef(lpsf2);
437     }
438
439     if (SUCCEEDED(r))
440         FillTreeView( info, lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
441
442     /* My Computer is already sorted and trying to do a simple text
443      * sort will only mess things up */
444     if (!_ILIsMyComputer(lptvid->lpi))
445         SendMessageW( info->hwndTreeView, TVM_SORTCHILDREN,
446                       FALSE, (LPARAM)pnmtv->itemNew.hItem );
447
448     return 0;
449 }
450
451 static HRESULT BrsFolder_Treeview_Changed( browse_info *info, NMTREEVIEWW *pnmtv )
452 {
453     LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
454
455     lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
456     info->pidlRet = lptvid->lpifq;
457     browsefolder_callback( info->lpBrowseInfo, info->hWnd, BFFM_SELCHANGED,
458                            (LPARAM)info->pidlRet );
459     BrsFolder_CheckValidSelection( info, lptvid );
460     return 0;
461 }
462
463 static LRESULT BrsFolder_OnNotify( browse_info *info, UINT CtlID, LPNMHDR lpnmh )
464 {
465     NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
466
467     TRACE("%p %x %p msg=%x\n", info, CtlID, lpnmh, pnmtv->hdr.code);
468
469     if (pnmtv->hdr.idFrom != IDD_TREEVIEW)
470         return 0;
471
472     switch (pnmtv->hdr.code)
473     {
474     case TVN_DELETEITEMA:
475     case TVN_DELETEITEMW:
476         return BrsFolder_Treeview_Delete( info, pnmtv );
477
478     case TVN_ITEMEXPANDINGA:
479     case TVN_ITEMEXPANDINGW:
480         return BrsFolder_Treeview_Expand( info, pnmtv );
481
482     case TVN_SELCHANGEDA:
483     case TVN_SELCHANGEDW:
484         return BrsFolder_Treeview_Changed( info, pnmtv );
485
486     default:
487         WARN("unhandled (%d)\n", pnmtv->hdr.code);
488         break;
489     }
490
491     return 0;
492 }
493
494
495 static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
496 {
497     LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
498
499     info->hWnd = hWnd;
500     SetPropW( hWnd, szBrowseFolderInfo, info );
501
502     if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
503         FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
504     if (lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS)
505         FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS);
506
507     if (lpBrowseInfo->lpszTitle)
508         SetWindowTextW( GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle );
509     else
510         ShowWindow( GetDlgItem(hWnd, IDD_TITLE), SW_HIDE );
511
512     if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT)
513         || (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
514         ShowWindow( GetDlgItem(hWnd, IDD_STATUS), SW_HIDE );
515
516     /* Hide "Make New Folder" Button? */
517     if ((lpBrowseInfo->ulFlags & BIF_NONEWFOLDERBUTTON)
518         || !(lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
519         ShowWindow( GetDlgItem(hWnd, IDD_MAKENEWFOLDER), SW_HIDE );
520
521     /* Hide the editbox? */
522     if (!(lpBrowseInfo->ulFlags & BIF_EDITBOX))
523     {
524         ShowWindow( GetDlgItem(hWnd, IDD_FOLDER), SW_HIDE );
525         ShowWindow( GetDlgItem(hWnd, IDD_FOLDERTEXT), SW_HIDE );
526     }
527
528     info->hwndTreeView = GetDlgItem( hWnd, IDD_TREEVIEW );
529     if (info->hwndTreeView)
530     {
531         InitializeTreeView( info );
532
533         /* Resize the treeview if there's not editbox */
534         if ((lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
535             && !(lpBrowseInfo->ulFlags & BIF_EDITBOX))
536         {
537             RECT rc;
538             GetClientRect(info->hwndTreeView, &rc);
539             SetWindowPos(info->hwndTreeView, HWND_TOP, 0, 0,
540                          rc.right, rc.bottom + 40, SWP_NOMOVE);
541         }
542     }
543     else
544         ERR("treeview control missing!\n");
545
546     browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
547
548     return TRUE;
549 }
550
551 static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
552 {
553     LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
554
555     switch (id)
556     {
557     case IDOK:
558         /* The original pidl is owned by the treeview and will be free'd. */
559         info->pidlRet = ILClone(info->pidlRet);
560         if (info->pidlRet == NULL) /* A null pidl would mean a cancel */
561             info->pidlRet = _ILCreateDesktop();
562         pdump( info->pidlRet );
563         if (lpBrowseInfo->pszDisplayName)
564             SHGetPathFromIDListW( info->pidlRet, lpBrowseInfo->pszDisplayName );
565         EndDialog( info->hWnd, 1 );
566         return TRUE;
567
568     case IDCANCEL:
569         EndDialog( info->hWnd, 0 );
570         return TRUE;
571
572     case IDD_MAKENEWFOLDER:
573         FIXME("make new folder not implemented\n");
574         return TRUE;
575     }
576     return FALSE;
577 }
578
579 static BOOL BrsFolder_OnSetExpanded(browse_info *info, LPVOID selection, 
580     BOOL is_str, HTREEITEM *pItem)
581 {
582     LPITEMIDLIST pidlSelection = (LPITEMIDLIST)selection;
583     LPCITEMIDLIST pidlCurrent, pidlRoot;
584     TVITEMEXW item;
585     BOOL bResult = FALSE;
586     
587     /* If 'selection' is a string, convert to a Shell ID List. */ 
588     if (is_str) {
589         IShellFolder *psfDesktop;
590         HRESULT hr;
591
592         hr = SHGetDesktopFolder(&psfDesktop);
593         if (FAILED(hr))
594             goto done;
595
596         hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, 
597                      (LPOLESTR)selection, NULL, &pidlSelection, NULL);
598         IShellFolder_Release(psfDesktop);
599         if (FAILED(hr)) 
600             goto done;
601     }
602
603     /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
604      * the sub-tree currently displayed. */
605     pidlRoot = info->lpBrowseInfo->pidlRoot;
606     pidlCurrent = pidlSelection;
607     while (!_ILIsEmpty(pidlRoot) && _ILIsEqualSimple(pidlRoot, pidlCurrent)) {
608         pidlRoot = ILGetNext(pidlRoot);
609         pidlCurrent = ILGetNext(pidlCurrent);
610     }
611
612     /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
613     if (!_ILIsEmpty(pidlRoot))
614         goto done;
615
616     /* Initialize item to point to the first child of the root folder. */
617     memset(&item, 0, sizeof(item));
618     item.mask = TVIF_PARAM;
619     item.hItem = TreeView_GetRoot(info->hwndTreeView);
620     if (item.hItem) 
621         item.hItem = TreeView_GetChild(info->hwndTreeView, item.hItem);
622
623     /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
624     while (item.hItem && !_ILIsEmpty(pidlCurrent)) {
625         LPTV_ITEMDATA pItemData;
626
627         SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
628         pItemData = (LPTV_ITEMDATA)item.lParam;
629
630         if (_ILIsEqualSimple(pItemData->lpi, pidlCurrent)) {
631             pidlCurrent = ILGetNext(pidlCurrent);
632             if (!_ILIsEmpty(pidlCurrent)) {
633                 /* Only expand current node and move on to it's first child,
634                  * if we didn't already reach the last SHITEMID */
635                 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item.hItem);
636                 item.hItem = TreeView_GetChild(info->hwndTreeView, item.hItem);
637             }
638         } else {
639             item.hItem = TreeView_GetNextSibling(info->hwndTreeView, item.hItem);
640         }
641     }
642
643     if (_ILIsEmpty(pidlCurrent) && item.hItem) 
644         bResult = TRUE;
645
646 done:
647     if (pidlSelection && pidlSelection != (LPITEMIDLIST)selection)
648         ILFree(pidlSelection);
649
650     if (pItem) 
651         *pItem = item.hItem;
652     
653     return bResult;
654 }
655
656 static BOOL BrsFolder_OnSetSelectionW(browse_info *info, LPVOID selection, BOOL is_str) {
657     HTREEITEM hItem;
658     BOOL bResult;
659
660     bResult = BrsFolder_OnSetExpanded(info, selection, is_str, &hItem);
661     if (bResult)
662         SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem );
663     return bResult;
664 }
665
666 static BOOL BrsFolder_OnSetSelectionA(browse_info *info, LPVOID selection, BOOL is_str) {
667     LPWSTR selectionW = NULL;
668     BOOL result = FALSE;
669     int length;
670     
671     if (!is_str)
672         return BrsFolder_OnSetSelectionW(info, selection, is_str);
673     
674     if ((length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)selection, -1, NULL, 0)) &&
675         (selectionW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR))) &&
676         MultiByteToWideChar(CP_ACP, 0, (LPCSTR)selection, -1, selectionW, length))
677     {
678         result = BrsFolder_OnSetSelectionW(info, selectionW, is_str);
679     }
680
681     HeapFree(GetProcessHeap(), 0, selectionW);
682     return result;
683 }
684
685 /*************************************************************************
686  *             BrsFolderDlgProc32  (not an exported API function)
687  */
688 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
689                                           LPARAM lParam )
690 {
691     browse_info *info;
692
693     TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd, msg, wParam, lParam );
694
695     if (msg == WM_INITDIALOG)
696         return BrsFolder_OnCreate( hWnd, (browse_info*) lParam );
697
698     info = (browse_info*) GetPropW( hWnd, szBrowseFolderInfo );
699
700     switch (msg)
701     {
702     case WM_NOTIFY:
703         return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
704
705     case WM_COMMAND:
706         return BrsFolder_OnCommand( info, wParam );
707
708     case BFFM_SETSTATUSTEXTA:
709         TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
710         SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
711         break;
712
713     case BFFM_SETSTATUSTEXTW:
714         TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
715         SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
716         break;
717
718     case BFFM_ENABLEOK:
719         TRACE("Enable %ld\n", lParam);
720         EnableWindow(GetDlgItem(hWnd, 1), (lParam)?TRUE:FALSE);
721         break;
722
723     case BFFM_SETOKTEXT: /* unicode only */
724         TRACE("Set OK text %s\n", debugstr_w((LPWSTR)wParam));
725         SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)wParam);
726         break;
727
728     case BFFM_SETSELECTIONA:
729         return BrsFolder_OnSetSelectionA(info, (LPVOID)lParam, (BOOL)wParam);
730
731     case BFFM_SETSELECTIONW:
732         return BrsFolder_OnSetSelectionW(info, (LPVOID)lParam, (BOOL)wParam);
733
734     case BFFM_SETEXPANDED: /* unicode only */
735         return BrsFolder_OnSetExpanded(info, (LPVOID)lParam, (BOOL)wParam, NULL);
736     }
737     return FALSE;
738 }
739
740 static const WCHAR swBrowseTemplateName[] = {
741     'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
742 static const WCHAR swNewBrowseTemplateName[] = {
743     'S','H','N','E','W','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
744
745 /*************************************************************************
746  * SHBrowseForFolderA [SHELL32.@]
747  * SHBrowseForFolder  [SHELL32.@]
748  */
749 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
750 {
751     BROWSEINFOW bi;
752     LPITEMIDLIST lpid;
753     INT len;
754     LPWSTR title;
755
756     TRACE("%p\n", lpbi);
757
758     bi.hwndOwner = lpbi->hwndOwner;
759     bi.pidlRoot = lpbi->pidlRoot;
760     if (lpbi->pszDisplayName)
761     {
762         bi.pszDisplayName = HeapAlloc( GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR) );
763         MultiByteToWideChar( CP_ACP, 0, lpbi->pszDisplayName, -1, bi.pszDisplayName, MAX_PATH );
764     }
765     else
766         bi.pszDisplayName = NULL;
767
768     if (lpbi->lpszTitle)
769     {
770         len = MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0 );
771         title = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
772         MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, title, len );
773     }
774     else
775         title = NULL;
776
777     bi.lpszTitle = title;
778     bi.ulFlags = lpbi->ulFlags;
779     bi.lpfn = lpbi->lpfn;
780     bi.lParam = lpbi->lParam;
781     bi.iImage = lpbi->iImage;
782     lpid = SHBrowseForFolderW( &bi );
783     if (bi.pszDisplayName)
784     {
785         WideCharToMultiByte( CP_ACP, 0, bi.pszDisplayName, -1,
786                              lpbi->pszDisplayName, MAX_PATH, 0, NULL);
787         HeapFree( GetProcessHeap(), 0, bi.pszDisplayName );
788     }
789     HeapFree(GetProcessHeap(), 0, title);
790     lpbi->iImage = bi.iImage;
791     return lpid;
792 }
793
794
795 /*************************************************************************
796  * SHBrowseForFolderW [SHELL32.@]
797  *
798  * NOTES
799  *  crashes when passed a null pointer
800  */
801 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
802 {
803     browse_info info;
804     DWORD r;
805     HRESULT hr;
806     const WCHAR * templateName;
807
808     info.hWnd = 0;
809     info.pidlRet = NULL;
810     info.lpBrowseInfo = lpbi;
811     info.hwndTreeView = NULL;
812
813     hr = OleInitialize(NULL);
814
815     if (lpbi->ulFlags & BIF_NEWDIALOGSTYLE)
816         templateName = swNewBrowseTemplateName;
817     else
818         templateName = swBrowseTemplateName;
819     r = DialogBoxParamW( shell32_hInstance, templateName, lpbi->hwndOwner,
820                          BrsFolderDlgProc, (LPARAM)&info );
821     if (SUCCEEDED(hr)) 
822         OleUninitialize();
823     if (!r)
824         return NULL;
825
826     return info.pidlRet;
827 }