Use GetUIObjectOf instead of BindToObject to get a IDropTarget object.
[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 typedef struct tagbrowse_info
41 {
42     HWND          hWnd;
43     HWND          hwndTreeView;
44     LPBROWSEINFOW lpBrowseInfo;
45     LPITEMIDLIST  pidlRet;
46 } browse_info;
47
48 typedef struct tagTV_ITEMDATA
49 {
50    LPSHELLFOLDER lpsfParent; /* IShellFolder of the parent */
51    LPITEMIDLIST  lpi;        /* PIDL relativ to parent */
52    LPITEMIDLIST  lpifq;      /* Fully qualified PIDL */
53    IEnumIDList*  pEnumIL;    /* Children iterator */ 
54 } TV_ITEMDATA, *LPTV_ITEMDATA;
55
56 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
57                         BIF_BROWSEFORCOMPUTER | \
58                         BIF_RETURNFSANCESTORS | \
59                         BIF_RETURNONLYFSDIRS | \
60                         BIF_BROWSEINCLUDEFILES)
61
62 static void FillTreeView(browse_info*, LPSHELLFOLDER,
63                LPITEMIDLIST, HTREEITEM, IEnumIDList*);
64 static HTREEITEM InsertTreeViewItem( browse_info*, IShellFolder *,
65                LPCITEMIDLIST, LPCITEMIDLIST, IEnumIDList*, HTREEITEM);
66
67 const WCHAR szBrowseFolderInfo[] = {
68     '_','_','W','I','N','E','_',
69     'B','R','S','F','O','L','D','E','R','D','L','G','_',
70     'I','N','F','O',0
71 };
72
73 static inline DWORD BrowseFlagsToSHCONTF(UINT ulFlags)
74 {
75     return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
76 }
77
78 static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo, HWND hWnd,
79                                    UINT msg, LPARAM param )
80 {
81     if (!lpBrowseInfo->lpfn)
82         return;
83     lpBrowseInfo->lpfn( hWnd, msg, param, lpBrowseInfo->lParam );
84 }
85
86 /******************************************************************************
87  * InitializeTreeView [Internal]
88  *
89  * Called from WM_INITDIALOG handler.
90  * 
91  * PARAMS
92  *  hwndParent [I] The BrowseForFolder dialog
93  *  root       [I] ITEMIDLIST of the root shell folder
94  */
95 static void InitializeTreeView( browse_info *info )
96 {
97     LPITEMIDLIST pidlParent, pidlChild;
98     HIMAGELIST hImageList;
99     HRESULT hr;
100     IShellFolder *lpsfParent, *lpsfRoot;
101     IEnumIDList * pEnumChildren = NULL;
102     HTREEITEM item;
103     DWORD flags;
104     LPCITEMIDLIST root = info->lpBrowseInfo->pidlRoot;
105
106     TRACE("%p\n", info );
107     
108     Shell_GetImageList(NULL, &hImageList);
109
110     if (hImageList)
111         TreeView_SetImageList( info->hwndTreeView, hImageList, 0 );
112
113     /* We want to call InsertTreeViewItem down the code, in order to insert
114      * the root item of the treeview. Due to InsertTreeViewItem's signature, 
115      * we need the following to do this:
116      *
117      * + An ITEMIDLIST corresponding to _the parent_ of root. 
118      * + An ITEMIDLIST, which is a relative path from root's parent to root 
119      *   (containing a single SHITEMID).
120      * + An IShellFolder interface pointer of root's parent folder.
121      *
122      * If root is 'Desktop', then root's parent is also 'Desktop'.
123      */
124
125     pidlParent = ILClone(root);
126     ILRemoveLastID(pidlParent);
127     pidlChild = ILClone(ILFindLastID(root));
128     
129     if (_ILIsDesktop(pidlParent)) {
130         hr = SHGetDesktopFolder(&lpsfParent);
131     } else {
132         IShellFolder *lpsfDesktop;
133         hr = SHGetDesktopFolder(&lpsfDesktop);
134         if (!SUCCEEDED(hr)) {
135             WARN("SHGetDesktopFolder failed! hr = %08lx\n", hr);
136             return;
137         }
138         hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
139         IShellFolder_Release(lpsfDesktop);
140     }
141     
142     if (!SUCCEEDED(hr)) {
143         WARN("Could not bind to parent shell folder! hr = %08lx\n", hr);
144         return;
145     }
146
147     if (pidlChild && pidlChild->mkid.cb) {
148         hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
149     } else {
150         lpsfRoot = lpsfParent;
151         hr = IShellFolder_AddRef(lpsfParent);
152     }
153     
154     if (!SUCCEEDED(hr)) {
155         WARN("Could not bind to root shell folder! hr = %08lx\n", hr);
156         IShellFolder_Release(lpsfParent);
157         return;
158     }
159
160     flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
161     hr = IShellFolder_EnumObjects( lpsfRoot, info->hWnd, flags, &pEnumChildren );
162     if (!SUCCEEDED(hr)) {
163         WARN("Could not get child iterator! hr = %08lx\n", hr);
164         IShellFolder_Release(lpsfParent);
165         IShellFolder_Release(lpsfRoot);
166         return;
167     }
168
169     TreeView_DeleteAllItems( info->hwndTreeView );
170     item = InsertTreeViewItem( info, lpsfParent, pidlChild,
171                                pidlParent, pEnumChildren, TVI_ROOT );
172     TreeView_Expand( info->hwndTreeView, item, TVE_EXPAND );
173
174     IShellFolder_Release(lpsfRoot);
175     IShellFolder_Release(lpsfParent);
176 }
177
178 static int GetIcon(LPITEMIDLIST lpi, UINT uFlags)
179 {
180     SHFILEINFOW sfi;
181     SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
182     return sfi.iIcon;
183 }
184
185 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
186 {
187     LPITEMIDLIST pidlDesktop = NULL;
188     DWORD flags;
189
190     TRACE("%p %p\n",lpifq, lpTV_ITEM);
191
192     if (!lpifq)
193     {
194         pidlDesktop = _ILCreateDesktop();
195         lpifq = pidlDesktop;
196     }
197
198     flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
199     lpTV_ITEM->iImage = GetIcon( lpifq, flags );
200
201     flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
202     lpTV_ITEM->iSelectedImage = GetIcon( lpifq, flags );
203
204     if (pidlDesktop)
205         ILFree( pidlDesktop );
206 }
207
208 /******************************************************************************
209  * GetName [Internal]
210  *
211  * Query a shell folder for the display name of one of it's children
212  *
213  * PARAMS
214  *  lpsf           [I] IShellFolder interface of the folder to be queried.
215  *  lpi            [I] ITEMIDLIST of the child, relative to parent
216  *  dwFlags        [I] as in IShellFolder::GetDisplayNameOf
217  *  lpFriendlyName [O] The desired display name in unicode
218  *
219  * RETURNS
220  *  Success: TRUE
221  *  Failure: FALSE
222  */
223 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
224 {
225         BOOL   bSuccess=TRUE;
226         STRRET str;
227
228         TRACE("%p %p %lx %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
229         if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
230         {
231           if (FAILED(StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi)))
232           {
233               bSuccess = FALSE;
234           }
235         }
236         else
237           bSuccess = FALSE;
238
239         TRACE("-- %s\n", debugstr_w(lpFriendlyName));
240         return bSuccess;
241 }
242
243 /******************************************************************************
244  * InsertTreeViewItem [Internal]
245  *
246  * PARAMS
247  *  info       [I] data for the dialog
248  *  lpsf       [I] IShellFolder interface of the item's parent shell folder 
249  *  pidl       [I] ITEMIDLIST of the child to insert, relativ to parent 
250  *  pidlParent [I] ITEMIDLIST of the parent shell folder
251  *  pEnumIL    [I] Iterator for the children of the item to be inserted
252  *  hParent    [I] The treeview-item that represents the parent shell folder
253  *
254  * RETURNS
255  *  Success: Handle to the created and inserted treeview-item
256  *  Failure: NULL
257  */
258 static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
259     LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
260     HTREEITEM hParent)
261 {
262         TVITEMW         tvi;
263         TVINSERTSTRUCTW tvins;
264         WCHAR           szBuff[MAX_PATH];
265         LPTV_ITEMDATA   lptvid=0;
266
267         tvi.mask  = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
268
269         tvi.cChildren= pEnumIL ? 1 : 0;
270         tvi.mask |= TVIF_CHILDREN;
271
272         lptvid = SHAlloc( sizeof(TV_ITEMDATA) );
273         if (!lptvid)
274             return NULL;
275
276         if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
277             return NULL;
278
279         tvi.pszText    = szBuff;
280         tvi.cchTextMax = MAX_PATH;
281         tvi.lParam = (LPARAM)lptvid;
282
283         IShellFolder_AddRef(lpsf);
284         lptvid->lpsfParent = lpsf;
285         lptvid->lpi     = ILClone(pidl);
286         lptvid->lpifq   = pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
287         lptvid->pEnumIL = pEnumIL;
288         GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
289
290         tvins.u.item       = tvi;
291         tvins.hInsertAfter = NULL;
292         tvins.hParent      = hParent;
293
294         return (HTREEITEM)TreeView_InsertItemW( info->hwndTreeView, &tvins );
295 }
296
297 /******************************************************************************
298  * FillTreeView [Internal]
299  *
300  * For each child (given by lpe) of the parent shell folder, which is given by 
301  * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
302  *
303  * PARAMS
304  *  info    [I] data for the dialog
305  *  lpsf    [I] IShellFolder interface of the parent shell folder
306  *  pidl    [I] ITEMIDLIST of the parent shell folder
307  *  hParent [I] The treeview item that represents the parent shell folder
308  *  lpe     [I] An iterator for the children of the parent shell folder
309  */
310 static void FillTreeView( browse_info *info, IShellFolder * lpsf,
311                  LPITEMIDLIST  pidl, HTREEITEM hParent, IEnumIDList* lpe)
312 {
313         HTREEITEM       hPrev = 0;
314         LPITEMIDLIST    pidlTemp = 0;
315         ULONG           ulFetched;
316         HRESULT         hr;
317         HWND            hwnd = GetParent( info->hwndTreeView );
318
319         TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
320
321         /* No IEnumIDList -> No children */
322         if (!lpe) return;
323         
324         SetCapture( hwnd );
325         SetCursor( LoadCursorA( 0, (LPSTR)IDC_WAIT ) );
326
327         while (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
328         {
329             ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
330             IEnumIDList* pEnumIL = NULL;
331             IShellFolder* pSFChild = NULL;
332             IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
333             if (ulAttrs & SFGAO_FOLDER)
334             {
335                 hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
336                 if (SUCCEEDED(hr))
337                 {
338                     DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
339                     hr = IShellFolder_EnumObjects(pSFChild, hwnd, flags, &pEnumIL);
340                     if (hr == S_OK)
341                     {
342                         if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) ||
343                              FAILED(IEnumIDList_Reset(pEnumIL)))
344                         {
345                             IEnumIDList_Release(pEnumIL);
346                             pEnumIL = NULL;
347                         }
348                     }
349                     IShellFolder_Release(pSFChild);
350                 }
351             }
352
353             if (!(hPrev = InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent)))
354                 goto done;
355             SHFree(pidlTemp);  /* Finally, free the pidl that the shell gave us... */
356             pidlTemp=NULL;
357         }
358
359 done:
360         ReleaseCapture();
361         SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
362
363         if (pidlTemp)
364           SHFree(pidlTemp);
365 }
366
367 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
368 {
369     LPPIDLDATA data = _ILGetDataPointer(pidl);
370     if (!data)
371         return FALSE;
372     return (data->type == type);
373 }
374
375 static void BrsFolder_CheckValidSelection( browse_info *info, LPTV_ITEMDATA lptvid )
376 {
377     LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
378     LPCITEMIDLIST pidl = lptvid->lpi;
379     BOOL bEnabled = TRUE;
380     DWORD dwAttributes;
381     HRESULT r;
382
383     if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
384         !PIDLIsType(pidl, PT_COMP))
385         bEnabled = FALSE;
386     if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
387     {
388         dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
389         r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
390                                 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
391         if (FAILED(r) || !(dwAttributes & (SFGAO_FILESYSANCESTOR|SFGAO_FILESYSTEM)))
392             bEnabled = FALSE;
393     }
394     if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
395     {
396         dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
397         r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
398                                 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
399         if (FAILED(r) || 
400             ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
401         {
402             bEnabled = FALSE;
403         }
404     }
405     SendMessageW(info->hWnd, BFFM_ENABLEOK, 0, (LPARAM)bEnabled);
406 }
407
408 static LRESULT BrsFolder_Treeview_Delete( browse_info *info, NMTREEVIEWW *pnmtv )
409 {
410     LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA)pnmtv->itemOld.lParam;
411
412     TRACE("TVN_DELETEITEMA/W %p\n", lptvid);
413
414     IShellFolder_Release(lptvid->lpsfParent);
415     if (lptvid->pEnumIL)
416         IEnumIDList_Release(lptvid->pEnumIL);
417     SHFree(lptvid->lpi);
418     SHFree(lptvid->lpifq);
419     SHFree(lptvid);
420     return 0;
421 }
422
423 static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
424 {
425     IShellFolder *lpsf2 = NULL;
426     LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
427     HRESULT r;
428
429     TRACE("TVN_ITEMEXPANDINGA/W\n");
430
431     if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
432         return 0;
433
434     if (lptvid->lpi && lptvid->lpi->mkid.cb) {
435         r = IShellFolder_BindToObject( lptvid->lpsfParent, lptvid->lpi, 0,
436                                       (REFIID)&IID_IShellFolder, (LPVOID *)&lpsf2 );
437     } else {
438         lpsf2 = lptvid->lpsfParent;
439         r = IShellFolder_AddRef(lpsf2);
440     }
441
442     if (SUCCEEDED(r))
443         FillTreeView( info, lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
444
445     /* My Computer is already sorted and trying to do a simple text
446      * sort will only mess things up */
447     if (!_ILIsMyComputer(lptvid->lpi))
448         TreeView_SortChildren(info->hwndTreeView, pnmtv->itemNew.hItem, FALSE);
449
450     return 0;
451 }
452
453 static HRESULT BrsFolder_Treeview_Changed( browse_info *info, NMTREEVIEWW *pnmtv )
454 {
455     LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
456
457     lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
458     info->pidlRet = lptvid->lpifq;
459     browsefolder_callback( info->lpBrowseInfo, info->hWnd, BFFM_SELCHANGED,
460                            (LPARAM)info->pidlRet );
461     BrsFolder_CheckValidSelection( info, lptvid );
462     return 0;
463 }
464
465 static LRESULT BrsFolder_OnNotify( browse_info *info, UINT CtlID, LPNMHDR lpnmh )
466 {
467     NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
468
469     TRACE("%p %x %p msg=%x\n", info, CtlID, lpnmh, pnmtv->hdr.code);
470
471     if (pnmtv->hdr.idFrom != IDD_TREEVIEW)
472         return 0;
473
474     switch (pnmtv->hdr.code)
475     {
476     case TVN_DELETEITEMA:
477     case TVN_DELETEITEMW:
478         return BrsFolder_Treeview_Delete( info, pnmtv );
479
480     case TVN_ITEMEXPANDINGA:
481     case TVN_ITEMEXPANDINGW:
482         return BrsFolder_Treeview_Expand( info, pnmtv );
483
484     case TVN_SELCHANGEDA:
485     case TVN_SELCHANGEDW:
486         return BrsFolder_Treeview_Changed( info, pnmtv );
487
488     default:
489         WARN("unhandled (%d)\n", pnmtv->hdr.code);
490         break;
491     }
492
493     return 0;
494 }
495
496
497 static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
498 {
499     LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
500
501     info->hWnd = hWnd;
502     SetPropW( hWnd, szBrowseFolderInfo, info );
503
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         ShowWindow( GetDlgItem(hWnd, IDD_STATUS), SW_HIDE );
514
515     info->hwndTreeView = GetDlgItem( hWnd, IDD_TREEVIEW );
516     if (info->hwndTreeView)
517         InitializeTreeView( info );
518     else
519         ERR("treeview control missing!\n");
520
521     browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
522
523     return TRUE;
524 }
525
526 static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
527 {
528     LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
529
530     switch (id)
531     {
532     case IDOK:
533         info->pidlRet = ILClone(info->pidlRet); /* The original pidl will be free'd. */
534         pdump( info->pidlRet );
535         if (lpBrowseInfo->pszDisplayName)
536             SHGetPathFromIDListW( info->pidlRet, lpBrowseInfo->pszDisplayName );
537         EndDialog( info->hWnd, 1 );
538         return TRUE;
539
540     case IDCANCEL:
541         EndDialog( info->hWnd, 0 );
542         return TRUE;
543     }
544     return FALSE;
545 }
546
547 /*************************************************************************
548  *             BrsFolderDlgProc32  (not an exported API function)
549  */
550 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
551                                           LPARAM lParam )
552 {
553     browse_info *info;
554
555     TRACE("hwnd=%p msg=%04x 0x%08x 0x%08lx\n", hWnd,  msg, wParam, lParam );
556
557     if (msg == WM_INITDIALOG)
558         return BrsFolder_OnCreate( hWnd, (browse_info*) lParam );
559
560     info = (browse_info*) GetPropW( hWnd, szBrowseFolderInfo );
561
562     switch (msg)
563     {
564     case WM_NOTIFY:
565         return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
566
567     case WM_COMMAND:
568         return BrsFolder_OnCommand( info, wParam );
569
570     case BFFM_SETSTATUSTEXTA:
571         TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
572         SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
573         break;
574
575     case BFFM_SETSTATUSTEXTW:
576         TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
577         SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
578         break;
579
580     case BFFM_ENABLEOK:
581         TRACE("Enable %ld\n", lParam);
582         EnableWindow(GetDlgItem(hWnd, 1), (lParam)?TRUE:FALSE);
583         break;
584
585     case BFFM_SETOKTEXT: /* unicode only */
586         TRACE("Set OK text %s\n", debugstr_w((LPWSTR)wParam));
587         SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)wParam);
588         break;
589
590     case BFFM_SETSELECTIONA:
591         if (wParam)
592             FIXME("Set selection %s\n", debugstr_a((LPSTR)lParam));
593         else
594             FIXME("Set selection %p\n", (void*)lParam);
595         break;
596
597     case BFFM_SETSELECTIONW:
598         if (wParam)
599             FIXME("Set selection %s\n", debugstr_w((LPWSTR)lParam));
600         else
601             FIXME("Set selection %p\n", (void*)lParam);
602         break;
603
604     case BFFM_SETEXPANDED: /* unicode only */
605         if (wParam)
606             FIXME("Set expanded %s\n", debugstr_w((LPWSTR)lParam));
607         else
608             FIXME("Set expanded %p\n", (void*)lParam);
609         break;
610     }
611     return FALSE;
612 }
613
614 static const WCHAR swBrowseTemplateName[] = {
615     'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
616
617 /*************************************************************************
618  * SHBrowseForFolderA [SHELL32.@]
619  * SHBrowseForFolder  [SHELL32.@]
620  */
621 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
622 {
623     BROWSEINFOW bi;
624     LPITEMIDLIST lpid;
625     INT len;
626     
627     TRACE("%p\n", lpbi);
628
629     bi.hwndOwner = lpbi->hwndOwner;
630     bi.pidlRoot = lpbi->pidlRoot;
631     if (lpbi->pszDisplayName)
632     {
633         bi.pszDisplayName = HeapAlloc( GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR) );
634         MultiByteToWideChar( CP_ACP, 0, lpbi->pszDisplayName, -1, bi.pszDisplayName, MAX_PATH );
635     }
636     else
637         bi.pszDisplayName = NULL;
638
639     if (lpbi->lpszTitle)
640     {
641         len = MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0 );
642         bi.lpszTitle = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
643         MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, (LPWSTR)bi.lpszTitle, len );
644     }
645     else
646         bi.lpszTitle = NULL;
647
648     bi.ulFlags = lpbi->ulFlags;
649     bi.lpfn = lpbi->lpfn;
650     bi.lParam = lpbi->lParam;
651     bi.iImage = lpbi->iImage;
652     lpid = SHBrowseForFolderW( &bi );
653     if (bi.pszDisplayName)
654     {
655         WideCharToMultiByte( CP_ACP, 0, bi.pszDisplayName, -1,
656                              lpbi->pszDisplayName, MAX_PATH, 0, NULL);
657         HeapFree( GetProcessHeap(), 0, bi.pszDisplayName );
658     }
659     HeapFree(GetProcessHeap(), 0, (LPVOID)bi.lpszTitle);
660     lpbi->iImage = bi.iImage;
661     return lpid;
662 }
663
664
665 /*************************************************************************
666  * SHBrowseForFolderW [SHELL32.@]
667  *
668  * NOTES
669  *  crashes when passed a null pointer
670  */
671 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
672 {
673     browse_info info;
674     DWORD r;
675     HRESULT hr;
676
677     info.hWnd = 0;
678     info.pidlRet = NULL;
679     info.lpBrowseInfo = lpbi;
680     info.hwndTreeView = NULL;
681
682     hr = OleInitialize(NULL);
683     r = DialogBoxParamW( shell32_hInstance, swBrowseTemplateName, lpbi->hwndOwner,
684                          BrsFolderDlgProc, (LPARAM)&info );
685     if (SUCCEEDED(hr)) 
686         OleUninitialize();
687     if (!r)
688         return NULL;
689
690     return info.pidlRet;
691 }