winegstreamer: Manage our own streaming thread.
[wine.git] / dlls / shell32 / brsfolder.c
blob188524d6a994674f14c10c3762fe25b0f5798834
1 /*
2 * Copyright 1999 Juergen Schmied
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.
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.
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
18 * FIXME:
19 * - many memory leaks
20 * - many flags unimplemented
21 * - implement editbox
24 #include <stdlib.h>
25 #include <string.h>
27 #define COBJMACROS
28 #define NONAMELESSUNION
30 #include "wine/debug.h"
31 #include "undocshell.h"
32 #include "commoncontrols.h"
33 #include "pidl.h"
34 #include "shell32_main.h"
35 #include "shellapi.h"
36 #include "shresdef.h"
37 #include "shellfolder.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(shell);
41 #define SHV_CHANGE_NOTIFY (WM_USER + 0x1111)
43 /* original margins and control size */
44 typedef struct tagLAYOUT_DATA
46 LONG left, width, right;
47 LONG top, height, bottom;
48 } LAYOUT_DATA;
50 typedef struct tagbrowse_info
52 HWND hWnd;
53 HWND hwndTreeView;
54 LPBROWSEINFOW lpBrowseInfo;
55 LPITEMIDLIST pidlRet;
56 LAYOUT_DATA *layout; /* filled by LayoutInit, used by LayoutUpdate */
57 SIZE szMin;
58 ULONG hNotify; /* change notification handle */
59 } browse_info;
61 typedef struct tagTV_ITEMDATA
63 LPSHELLFOLDER lpsfParent; /* IShellFolder of the parent */
64 LPITEMIDLIST lpi; /* PIDL relative to parent */
65 LPITEMIDLIST lpifq; /* Fully qualified PIDL */
66 IEnumIDList* pEnumIL; /* Children iterator */
67 } TV_ITEMDATA, *LPTV_ITEMDATA;
69 typedef struct tagLAYOUT_INFO
71 int iItemId; /* control id */
72 DWORD dwAnchor; /* BF_* flags specifying which margins should remain constant */
73 } LAYOUT_INFO;
75 static const LAYOUT_INFO g_layout_info[] =
77 {IDD_TITLE, BF_TOP|BF_LEFT|BF_RIGHT},
78 {IDD_STATUS, BF_TOP|BF_LEFT|BF_RIGHT},
79 {IDD_FOLDER, BF_TOP|BF_LEFT|BF_RIGHT},
80 {IDD_TREEVIEW, BF_TOP|BF_BOTTOM|BF_LEFT|BF_RIGHT},
81 {IDD_FOLDER, BF_BOTTOM|BF_LEFT},
82 {IDD_FOLDERTEXT, BF_BOTTOM|BF_LEFT|BF_RIGHT},
83 {IDD_MAKENEWFOLDER, BF_BOTTOM|BF_LEFT},
84 {IDOK, BF_BOTTOM|BF_RIGHT},
85 {IDCANCEL, BF_BOTTOM|BF_RIGHT}
88 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
89 BIF_BROWSEFORCOMPUTER | \
90 BIF_RETURNFSANCESTORS | \
91 BIF_RETURNONLYFSDIRS | \
92 BIF_NONEWFOLDERBUTTON | \
93 BIF_NEWDIALOGSTYLE | \
94 BIF_BROWSEINCLUDEFILES)
96 static void FillTreeView(browse_info*, LPSHELLFOLDER,
97 LPITEMIDLIST, HTREEITEM, IEnumIDList*);
98 static HTREEITEM InsertTreeViewItem( browse_info*, IShellFolder *,
99 LPCITEMIDLIST, LPCITEMIDLIST, IEnumIDList*, HTREEITEM);
101 static const WCHAR szBrowseFolderInfo[] = {
102 '_','_','W','I','N','E','_',
103 'B','R','S','F','O','L','D','E','R','D','L','G','_',
104 'I','N','F','O',0
107 static inline DWORD BrowseFlagsToSHCONTF(UINT ulFlags)
109 return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
112 static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo, HWND hWnd,
113 UINT msg, LPARAM param )
115 if (!lpBrowseInfo->lpfn)
116 return;
117 lpBrowseInfo->lpfn( hWnd, msg, param, lpBrowseInfo->lParam );
120 static LAYOUT_DATA *LayoutInit(HWND hwnd, const LAYOUT_INFO *layout_info, int layout_count)
122 LAYOUT_DATA *data;
123 RECT rcWnd;
124 int i;
126 GetClientRect(hwnd, &rcWnd);
127 data = SHAlloc(sizeof(LAYOUT_DATA)*layout_count);
128 for (i = 0; i < layout_count; i++)
130 RECT r;
131 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
133 if (hItem == NULL)
134 ERR("Item %d not found\n", i);
135 GetWindowRect(hItem, &r);
136 MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);
138 data[i].left = r.left;
139 data[i].right = rcWnd.right - r.right;
140 data[i].width = r.right - r.left;
142 data[i].top = r.top;
143 data[i].bottom = rcWnd.bottom - r.bottom;
144 data[i].height = r.bottom - r.top;
146 return data;
149 static void LayoutUpdate(HWND hwnd, LAYOUT_DATA *data, const LAYOUT_INFO *layout_info, int layout_count)
151 RECT rcWnd;
152 int i;
154 GetClientRect(hwnd, &rcWnd);
155 for (i = 0; i < layout_count; i++)
157 RECT r;
158 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
160 GetWindowRect(hItem, &r);
161 MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);
163 if (layout_info[i].dwAnchor & BF_RIGHT)
165 r.right = rcWnd.right - data[i].right;
166 if (!(layout_info[i].dwAnchor & BF_LEFT))
167 r.left = r.right - data[i].width;
170 if (layout_info[i].dwAnchor & BF_BOTTOM)
172 r.bottom = rcWnd.bottom - data[i].bottom;
173 if (!(layout_info[i].dwAnchor & BF_TOP))
174 r.top = r.bottom - data[i].height;
177 SetWindowPos(hItem, NULL, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOZORDER);
182 /******************************************************************************
183 * InitializeTreeView [Internal]
185 * Called from WM_INITDIALOG handler.
187 * PARAMS
188 * hwndParent [I] The BrowseForFolder dialog
189 * root [I] ITEMIDLIST of the root shell folder
191 static void InitializeTreeView( browse_info *info )
193 LPITEMIDLIST pidlParent, pidlChild;
194 HIMAGELIST hImageList;
195 HRESULT hr;
196 IShellFolder *lpsfParent, *lpsfRoot;
197 IEnumIDList * pEnumChildren = NULL;
198 HTREEITEM item;
199 DWORD flags;
200 LPCITEMIDLIST root = info->lpBrowseInfo->pidlRoot;
202 TRACE("%p\n", info );
204 Shell_GetImageLists(NULL, &hImageList);
206 if (hImageList)
207 SendMessageW( info->hwndTreeView, TVM_SETIMAGELIST, 0, (LPARAM)hImageList );
209 /* We want to call InsertTreeViewItem down the code, in order to insert
210 * the root item of the treeview. Due to InsertTreeViewItem's signature,
211 * we need the following to do this:
213 * + An ITEMIDLIST corresponding to _the parent_ of root.
214 * + An ITEMIDLIST, which is a relative path from root's parent to root
215 * (containing a single SHITEMID).
216 * + An IShellFolder interface pointer of root's parent folder.
218 * If root is 'Desktop', then root's parent is also 'Desktop'.
221 pidlParent = ILClone(root);
222 ILRemoveLastID(pidlParent);
223 pidlChild = ILClone(ILFindLastID(root));
225 if (_ILIsDesktop(pidlParent)) {
226 hr = SHGetDesktopFolder(&lpsfParent);
227 } else {
228 IShellFolder *lpsfDesktop;
229 hr = SHGetDesktopFolder(&lpsfDesktop);
230 if (FAILED(hr)) {
231 WARN("SHGetDesktopFolder failed! hr = %08x\n", hr);
232 ILFree(pidlChild);
233 ILFree(pidlParent);
234 return;
236 hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
237 IShellFolder_Release(lpsfDesktop);
240 if (FAILED(hr)) {
241 WARN("Could not bind to parent shell folder! hr = %08x\n", hr);
242 ILFree(pidlChild);
243 ILFree(pidlParent);
244 return;
247 if (!_ILIsEmpty(pidlChild)) {
248 hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
249 } else {
250 lpsfRoot = lpsfParent;
251 hr = IShellFolder_AddRef(lpsfParent);
254 if (FAILED(hr)) {
255 WARN("Could not bind to root shell folder! hr = %08x\n", hr);
256 IShellFolder_Release(lpsfParent);
257 ILFree(pidlChild);
258 ILFree(pidlParent);
259 return;
262 flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
263 hr = IShellFolder_EnumObjects( lpsfRoot, info->hWnd, flags, &pEnumChildren );
264 if (FAILED(hr)) {
265 WARN("Could not get child iterator! hr = %08x\n", hr);
266 IShellFolder_Release(lpsfParent);
267 IShellFolder_Release(lpsfRoot);
268 ILFree(pidlChild);
269 ILFree(pidlParent);
270 return;
273 SendMessageW( info->hwndTreeView, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT );
274 item = InsertTreeViewItem( info, lpsfParent, pidlChild,
275 pidlParent, pEnumChildren, TVI_ROOT );
276 SendMessageW( info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item );
278 ILFree(pidlChild);
279 ILFree(pidlParent);
280 IShellFolder_Release(lpsfRoot);
281 IShellFolder_Release(lpsfParent);
284 static int GetIcon(LPCITEMIDLIST lpi, UINT uFlags)
286 SHFILEINFOW sfi;
287 IImageList *list;
289 list = (IImageList *)SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
290 if (list) IImageList_Release(list);
291 return sfi.iIcon;
294 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
296 LPITEMIDLIST pidlDesktop = NULL;
297 DWORD flags;
299 TRACE("%p %p\n",lpifq, lpTV_ITEM);
301 if (!lpifq)
303 pidlDesktop = _ILCreateDesktop();
304 lpifq = pidlDesktop;
307 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
308 lpTV_ITEM->iImage = GetIcon( lpifq, flags );
310 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
311 lpTV_ITEM->iSelectedImage = GetIcon( lpifq, flags );
313 if (pidlDesktop)
314 ILFree( pidlDesktop );
317 /******************************************************************************
318 * GetName [Internal]
320 * Query a shell folder for the display name of one of its children
322 * PARAMS
323 * lpsf [I] IShellFolder interface of the folder to be queried.
324 * lpi [I] ITEMIDLIST of the child, relative to parent
325 * dwFlags [I] as in IShellFolder::GetDisplayNameOf
326 * lpFriendlyName [O] The desired display name in unicode
328 * RETURNS
329 * Success: TRUE
330 * Failure: FALSE
332 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
334 BOOL bSuccess=TRUE;
335 STRRET str;
337 TRACE("%p %p %x %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
338 if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
339 bSuccess = StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi);
340 else
341 bSuccess = FALSE;
343 TRACE("-- %s\n", debugstr_w(lpFriendlyName));
344 return bSuccess;
347 /******************************************************************************
348 * InsertTreeViewItem [Internal]
350 * PARAMS
351 * info [I] data for the dialog
352 * lpsf [I] IShellFolder interface of the item's parent shell folder
353 * pidl [I] ITEMIDLIST of the child to insert, relative to parent
354 * pidlParent [I] ITEMIDLIST of the parent shell folder
355 * pEnumIL [I] Iterator for the children of the item to be inserted
356 * hParent [I] The treeview-item that represents the parent shell folder
358 * RETURNS
359 * Success: Handle to the created and inserted treeview-item
360 * Failure: NULL
362 static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
363 LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
364 HTREEITEM hParent)
366 TVITEMW tvi;
367 TVINSERTSTRUCTW tvins;
368 WCHAR szBuff[MAX_PATH];
369 LPTV_ITEMDATA lptvid=0;
371 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
373 tvi.cChildren= pEnumIL ? 1 : 0;
374 tvi.mask |= TVIF_CHILDREN;
376 if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
377 return NULL;
379 lptvid = SHAlloc( sizeof(TV_ITEMDATA) );
380 if (!lptvid)
381 return NULL;
383 tvi.pszText = szBuff;
384 tvi.cchTextMax = MAX_PATH;
385 tvi.lParam = (LPARAM)lptvid;
387 IShellFolder_AddRef(lpsf);
388 lptvid->lpsfParent = lpsf;
389 lptvid->lpi = ILClone(pidl);
390 lptvid->lpifq = pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
391 lptvid->pEnumIL = pEnumIL;
392 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
394 tvins.u.item = tvi;
395 tvins.hInsertAfter = NULL;
396 tvins.hParent = hParent;
398 return TreeView_InsertItemW( info->hwndTreeView, &tvins );
401 /******************************************************************************
402 * FillTreeView [Internal]
404 * For each child (given by lpe) of the parent shell folder, which is given by
405 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
407 * PARAMS
408 * info [I] data for the dialog
409 * lpsf [I] IShellFolder interface of the parent shell folder
410 * pidl [I] ITEMIDLIST of the parent shell folder
411 * hParent [I] The treeview item that represents the parent shell folder
412 * lpe [I] An iterator for the children of the parent shell folder
414 static void FillTreeView( browse_info *info, IShellFolder * lpsf,
415 LPITEMIDLIST pidl, HTREEITEM hParent, IEnumIDList* lpe)
417 LPITEMIDLIST pidlTemp = 0;
418 ULONG ulFetched;
419 HRESULT hr;
420 HWND hwnd = GetParent( info->hwndTreeView );
422 TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
424 /* No IEnumIDList -> No children */
425 if (!lpe) return;
427 SetCapture( hwnd );
428 SetCursor( LoadCursorA( 0, (LPSTR)IDC_WAIT ) );
430 while (S_OK == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
432 ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
433 IEnumIDList* pEnumIL = NULL;
434 IShellFolder* pSFChild = NULL;
435 IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
436 if (ulAttrs & SFGAO_FOLDER)
438 hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
439 if (SUCCEEDED(hr))
441 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
442 hr = IShellFolder_EnumObjects(pSFChild, hwnd, flags, &pEnumIL);
443 if (hr == S_OK)
445 if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) ||
446 FAILED(IEnumIDList_Reset(pEnumIL)))
448 IEnumIDList_Release(pEnumIL);
449 pEnumIL = NULL;
452 IShellFolder_Release(pSFChild);
456 if (!InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent))
457 goto done;
458 SHFree(pidlTemp); /* Finally, free the pidl that the shell gave us... */
459 pidlTemp=NULL;
462 done:
463 ReleaseCapture();
464 SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
465 SHFree(pidlTemp);
468 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
470 LPPIDLDATA data = _ILGetDataPointer(pidl);
471 if (!data)
472 return FALSE;
473 return (data->type == type);
476 static void BrsFolder_CheckValidSelection( browse_info *info, LPTV_ITEMDATA lptvid )
478 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
479 LPCITEMIDLIST pidl = lptvid->lpi;
480 BOOL bEnabled = TRUE;
481 DWORD dwAttributes;
482 HRESULT r;
484 if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
485 !PIDLIsType(pidl, PT_COMP))
486 bEnabled = FALSE;
487 if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
489 dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
490 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
491 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
492 if (FAILED(r) || !(dwAttributes & (SFGAO_FILESYSANCESTOR|SFGAO_FILESYSTEM)))
493 bEnabled = FALSE;
496 dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
497 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
498 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
499 if (FAILED(r) ||
500 ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
502 if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
503 bEnabled = FALSE;
504 EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), FALSE);
506 else
507 EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), TRUE);
509 SendMessageW(info->hWnd, BFFM_ENABLEOK, 0, bEnabled);
512 static LRESULT BrsFolder_Treeview_Delete( browse_info *info, NMTREEVIEWW *pnmtv )
514 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA)pnmtv->itemOld.lParam;
516 TRACE("TVN_DELETEITEMA/W %p\n", lptvid);
518 IShellFolder_Release(lptvid->lpsfParent);
519 if (lptvid->pEnumIL)
520 IEnumIDList_Release(lptvid->pEnumIL);
521 SHFree(lptvid->lpi);
522 SHFree(lptvid->lpifq);
523 SHFree(lptvid);
524 return 0;
527 static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
529 IShellFolder *lpsf2 = NULL;
530 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
531 HRESULT r;
533 TRACE("TVN_ITEMEXPANDINGA/W\n");
535 if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
536 return 0;
538 if (!_ILIsEmpty(lptvid->lpi)) {
539 r = IShellFolder_BindToObject( lptvid->lpsfParent, lptvid->lpi, 0,
540 &IID_IShellFolder, (void**)&lpsf2 );
541 } else {
542 lpsf2 = lptvid->lpsfParent;
543 IShellFolder_AddRef(lpsf2);
544 r = S_OK;
547 if (SUCCEEDED(r))
549 FillTreeView( info, lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
550 IShellFolder_Release( lpsf2 );
553 /* My Computer is already sorted and trying to do a simple text
554 * sort will only mess things up */
555 if (!_ILIsMyComputer(lptvid->lpi))
556 SendMessageW( info->hwndTreeView, TVM_SORTCHILDREN,
557 FALSE, (LPARAM)pnmtv->itemNew.hItem );
559 return 0;
562 static HRESULT BrsFolder_Treeview_Changed( browse_info *info, NMTREEVIEWW *pnmtv )
564 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
565 WCHAR name[MAX_PATH];
567 ILFree(info->pidlRet);
568 info->pidlRet = ILClone(lptvid->lpifq);
570 if (GetName(lptvid->lpsfParent, lptvid->lpi, SHGDN_NORMAL, name))
571 SetWindowTextW( GetDlgItem(info->hWnd, IDD_FOLDERTEXT), name );
573 browsefolder_callback( info->lpBrowseInfo, info->hWnd, BFFM_SELCHANGED,
574 (LPARAM)info->pidlRet );
575 BrsFolder_CheckValidSelection( info, lptvid );
576 return S_OK;
579 static LRESULT BrsFolder_Treeview_Rename(browse_info *info, NMTVDISPINFOW *pnmtv)
581 LPTV_ITEMDATA item_data;
582 WCHAR old_path[MAX_PATH], new_path[MAX_PATH], *p;
583 NMTREEVIEWW nmtv;
584 TVITEMW item;
586 if(!pnmtv->item.pszText)
587 return 0;
589 item.mask = TVIF_HANDLE|TVIF_PARAM;
590 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
591 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
592 item_data = (LPTV_ITEMDATA)item.lParam;
594 SHGetPathFromIDListW(item_data->lpifq, old_path);
595 if(!(p = strrchrW(old_path, '\\')))
596 return 0;
597 p = new_path+(p-old_path+1);
598 memcpy(new_path, old_path, (p-new_path)*sizeof(WCHAR));
599 strcpyW(p, pnmtv->item.pszText);
601 if(!MoveFileW(old_path, new_path))
602 return 0;
604 SHFree(item_data->lpifq);
605 SHFree(item_data->lpi);
606 item_data->lpifq = SHSimpleIDListFromPathW(new_path);
607 IShellFolder_ParseDisplayName(item_data->lpsfParent, NULL, NULL,
608 pnmtv->item.pszText, NULL, &item_data->lpi, NULL);
610 item.mask = TVIF_HANDLE|TVIF_TEXT;
611 item.pszText = pnmtv->item.pszText;
612 SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);
614 nmtv.itemNew.lParam = item.lParam;
615 BrsFolder_Treeview_Changed(info, &nmtv);
616 return 0;
619 static HRESULT BrsFolder_Rename(browse_info *info, HTREEITEM rename)
621 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)rename);
622 SendMessageW(info->hwndTreeView, TVM_EDITLABELW, 0, (LPARAM)rename);
623 return S_OK;
626 static LRESULT BrsFolder_Treeview_Keydown(browse_info *info, LPNMTVKEYDOWN keydown)
628 HTREEITEM selected_item;
630 /* Old dialog doesn't support those advanced features */
631 if (!(info->lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
632 return 0;
634 selected_item = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
636 switch (keydown->wVKey)
638 case VK_F2:
639 BrsFolder_Rename(info, selected_item);
640 break;
641 case VK_DELETE:
643 const ITEMIDLIST *item_id;
644 ISFHelper *psfhlp;
645 HRESULT hr;
646 TVITEMW item;
647 TV_ITEMDATA *item_data;
649 item.mask = TVIF_PARAM;
650 item.hItem = selected_item;
651 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
652 item_data = (TV_ITEMDATA *)item.lParam;
653 item_id = item_data->lpi;
655 hr = IShellFolder_QueryInterface(item_data->lpsfParent, &IID_ISFHelper, (void**)&psfhlp);
656 if(FAILED(hr))
657 return 0;
659 /* perform the item deletion - tree view gets updated over shell notification */
660 ISFHelper_DeleteItems(psfhlp, 1, &item_id);
661 ISFHelper_Release(psfhlp);
663 break;
665 return 0;
668 static LRESULT BrsFolder_OnNotify( browse_info *info, UINT CtlID, LPNMHDR lpnmh )
670 NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
672 TRACE("%p %x %p msg=%x\n", info, CtlID, lpnmh, pnmtv->hdr.code);
674 if (pnmtv->hdr.idFrom != IDD_TREEVIEW)
675 return 0;
677 switch (pnmtv->hdr.code)
679 case TVN_DELETEITEMA:
680 case TVN_DELETEITEMW:
681 return BrsFolder_Treeview_Delete( info, pnmtv );
683 case TVN_ITEMEXPANDINGA:
684 case TVN_ITEMEXPANDINGW:
685 return BrsFolder_Treeview_Expand( info, pnmtv );
687 case TVN_SELCHANGEDA:
688 case TVN_SELCHANGEDW:
689 return BrsFolder_Treeview_Changed( info, pnmtv );
691 case TVN_ENDLABELEDITA:
692 case TVN_ENDLABELEDITW:
693 return BrsFolder_Treeview_Rename( info, (LPNMTVDISPINFOW)pnmtv );
695 case TVN_KEYDOWN:
696 return BrsFolder_Treeview_Keydown( info, (LPNMTVKEYDOWN)pnmtv );
698 default:
699 WARN("unhandled (%d)\n", pnmtv->hdr.code);
700 break;
703 return 0;
707 static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
709 LPITEMIDLIST computer_pidl;
710 SHChangeNotifyEntry ntreg;
711 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
713 info->hWnd = hWnd;
714 SetPropW( hWnd, szBrowseFolderInfo, info );
716 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
717 FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
718 if (lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS)
719 FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS);
721 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
723 RECT rcWnd;
725 info->layout = LayoutInit(hWnd, g_layout_info, ARRAY_SIZE(g_layout_info));
727 /* TODO: Windows allows shrinking the windows a bit */
728 GetWindowRect(hWnd, &rcWnd);
729 info->szMin.cx = rcWnd.right - rcWnd.left;
730 info->szMin.cy = rcWnd.bottom - rcWnd.top;
732 else
734 info->layout = NULL;
737 if (lpBrowseInfo->lpszTitle)
738 SetWindowTextW( GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle );
739 else
740 ShowWindow( GetDlgItem(hWnd, IDD_TITLE), SW_HIDE );
742 if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT)
743 || (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
744 ShowWindow( GetDlgItem(hWnd, IDD_STATUS), SW_HIDE );
746 /* Hide "Make New Folder" Button? */
747 if ((lpBrowseInfo->ulFlags & BIF_NONEWFOLDERBUTTON)
748 || !(lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
749 ShowWindow( GetDlgItem(hWnd, IDD_MAKENEWFOLDER), SW_HIDE );
751 /* Hide the editbox? */
752 if (!(lpBrowseInfo->ulFlags & BIF_EDITBOX))
754 ShowWindow( GetDlgItem(hWnd, IDD_FOLDER), SW_HIDE );
755 ShowWindow( GetDlgItem(hWnd, IDD_FOLDERTEXT), SW_HIDE );
758 info->hwndTreeView = GetDlgItem( hWnd, IDD_TREEVIEW );
759 if (info->hwndTreeView)
761 InitializeTreeView( info );
763 /* Resize the treeview if there's not editbox */
764 if ((lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
765 && !(lpBrowseInfo->ulFlags & BIF_EDITBOX))
767 RECT rc;
768 GetClientRect(info->hwndTreeView, &rc);
769 SetWindowPos(info->hwndTreeView, HWND_TOP, 0, 0,
770 rc.right, rc.bottom + 40, SWP_NOMOVE);
773 else
774 ERR("treeview control missing!\n");
776 /* Register for change notifications */
777 SHGetFolderLocation(NULL, CSIDL_DESKTOP, NULL, 0, &computer_pidl);
779 ntreg.pidl = computer_pidl;
780 ntreg.fRecursive = TRUE;
782 info->hNotify = SHChangeNotifyRegister(hWnd, SHCNRF_InterruptLevel, SHCNE_ALLEVENTS, SHV_CHANGE_NOTIFY, 1, &ntreg);
784 browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
786 ILFree(computer_pidl);
788 return TRUE;
791 static HRESULT BrsFolder_NewFolder(browse_info *info)
793 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
794 IShellFolder *desktop, *cur;
795 ISFHelper *sfhelper;
796 WCHAR name[MAX_PATH];
797 HTREEITEM parent, added;
798 LPTV_ITEMDATA item_data;
799 LPITEMIDLIST new_item;
800 TVITEMW item;
801 HRESULT hr;
802 int len;
804 if(!info->pidlRet) {
805 ERR("Make new folder button should be disabled\n");
806 return E_FAIL;
809 /* Create new directory */
810 hr = SHGetDesktopFolder(&desktop);
811 if(FAILED(hr))
812 return hr;
813 hr = IShellFolder_BindToObject(desktop, info->pidlRet, 0, &IID_IShellFolder, (void**)&cur);
814 IShellFolder_Release(desktop);
815 if(FAILED(hr))
816 return hr;
818 hr = IShellFolder_QueryInterface(cur, &IID_ISFHelper, (void**)&sfhelper);
819 if(FAILED(hr))
820 return hr;
822 if(!SHGetPathFromIDListW(info->pidlRet, name)) {
823 hr = E_FAIL;
824 goto cleanup;
827 len = strlenW(name);
828 if(len<MAX_PATH)
829 name[len++] = '\\';
830 hr = ISFHelper_GetUniqueName(sfhelper, &name[len], MAX_PATH-len);
831 ISFHelper_Release(sfhelper);
832 if(FAILED(hr))
833 goto cleanup;
835 hr = E_FAIL;
836 if(!CreateDirectoryW(name, NULL))
837 goto cleanup;
839 /* Update parent of newly created directory */
840 parent = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
841 if(!parent)
842 goto cleanup;
844 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)parent);
846 memset(&item, 0, sizeof(TVITEMW));
847 item.mask = TVIF_PARAM|TVIF_STATE;
848 item.hItem = parent;
849 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
850 item_data = (LPTV_ITEMDATA)item.lParam;
851 if(!item_data)
852 goto cleanup;
854 if(item_data->pEnumIL)
855 IEnumIDList_Release(item_data->pEnumIL);
856 hr = IShellFolder_EnumObjects(cur, info->hwndTreeView, flags, &item_data->pEnumIL);
857 if(FAILED(hr))
858 goto cleanup;
860 /* Update treeview */
861 if(!(item.state&TVIS_EXPANDEDONCE)) {
862 item.mask = TVIF_STATE;
863 item.state = TVIS_EXPANDEDONCE;
864 item.stateMask = TVIS_EXPANDEDONCE;
865 SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);
868 hr = IShellFolder_ParseDisplayName(cur, NULL, NULL, name+len, NULL, &new_item, NULL);
869 if(FAILED(hr))
870 goto cleanup;
872 added = InsertTreeViewItem(info, cur, new_item, item_data->lpifq, NULL, parent);
873 IShellFolder_Release(cur);
874 SHFree(new_item);
876 SendMessageW(info->hwndTreeView, TVM_SORTCHILDREN, FALSE, (LPARAM)parent);
877 return BrsFolder_Rename(info, added);
879 cleanup:
880 return hr;
883 static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
885 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
887 switch (id)
889 case IDOK:
890 if (info->pidlRet == NULL) /* A null pidl would mean a cancel */
891 info->pidlRet = _ILCreateDesktop();
892 pdump( info->pidlRet );
893 if (lpBrowseInfo->pszDisplayName)
894 SHGetPathFromIDListW( info->pidlRet, lpBrowseInfo->pszDisplayName );
895 EndDialog( info->hWnd, 1 );
896 return TRUE;
898 case IDCANCEL:
899 EndDialog( info->hWnd, 0 );
900 return TRUE;
902 case IDD_MAKENEWFOLDER:
903 BrsFolder_NewFolder(info);
904 return TRUE;
906 return FALSE;
909 static BOOL BrsFolder_OnSetExpanded(browse_info *info, LPVOID selection,
910 BOOL is_str, HTREEITEM *pItem)
912 LPITEMIDLIST pidlSelection = selection;
913 LPCITEMIDLIST pidlCurrent, pidlRoot;
914 TVITEMEXW item;
915 BOOL bResult = FALSE;
917 memset(&item, 0, sizeof(item));
919 /* If 'selection' is a string, convert to a Shell ID List. */
920 if (is_str) {
921 IShellFolder *psfDesktop;
922 HRESULT hr;
924 hr = SHGetDesktopFolder(&psfDesktop);
925 if (FAILED(hr))
926 goto done;
928 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL,
929 selection, NULL, &pidlSelection, NULL);
930 IShellFolder_Release(psfDesktop);
931 if (FAILED(hr))
932 goto done;
935 /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
936 * the sub-tree currently displayed. */
937 pidlRoot = info->lpBrowseInfo->pidlRoot;
938 pidlCurrent = pidlSelection;
939 while (!_ILIsEmpty(pidlRoot) && _ILIsEqualSimple(pidlRoot, pidlCurrent)) {
940 pidlRoot = ILGetNext(pidlRoot);
941 pidlCurrent = ILGetNext(pidlCurrent);
944 /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
945 if (!_ILIsEmpty(pidlRoot))
946 goto done;
948 /* Initialize item to point to the first child of the root folder. */
949 item.mask = TVIF_PARAM;
950 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
952 if (item.hItem)
953 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
954 (LPARAM)item.hItem);
956 /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
957 while (item.hItem && !_ILIsEmpty(pidlCurrent)) {
958 LPTV_ITEMDATA pItemData;
960 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
961 pItemData = (LPTV_ITEMDATA)item.lParam;
963 if (_ILIsEqualSimple(pItemData->lpi, pidlCurrent)) {
964 pidlCurrent = ILGetNext(pidlCurrent);
965 if (!_ILIsEmpty(pidlCurrent)) {
966 /* Only expand current node and move on to its first child,
967 * if we didn't already reach the last SHITEMID */
968 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item.hItem);
969 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
970 (LPARAM)item.hItem);
972 } else {
973 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT,
974 (LPARAM)item.hItem);
978 if (_ILIsEmpty(pidlCurrent) && item.hItem)
979 bResult = TRUE;
981 done:
982 if (pidlSelection && pidlSelection != selection)
983 ILFree(pidlSelection);
985 if (pItem)
986 *pItem = item.hItem;
988 return bResult;
991 static BOOL BrsFolder_OnSetSelectionW(browse_info *info, LPVOID selection, BOOL is_str) {
992 HTREEITEM hItem;
993 BOOL bResult;
995 if (!selection) return FALSE;
997 bResult = BrsFolder_OnSetExpanded(info, selection, is_str, &hItem);
998 if (bResult)
999 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem );
1000 return bResult;
1003 static BOOL BrsFolder_OnSetSelectionA(browse_info *info, LPVOID selection, BOOL is_str) {
1004 LPWSTR selectionW = NULL;
1005 BOOL result = FALSE;
1006 int length;
1008 if (!is_str)
1009 return BrsFolder_OnSetSelectionW(info, selection, is_str);
1011 if ((length = MultiByteToWideChar(CP_ACP, 0, selection, -1, NULL, 0)) &&
1012 (selectionW = heap_alloc(length * sizeof(WCHAR))) &&
1013 MultiByteToWideChar(CP_ACP, 0, selection, -1, selectionW, length))
1015 result = BrsFolder_OnSetSelectionW(info, selectionW, is_str);
1018 heap_free(selectionW);
1019 return result;
1022 static LRESULT BrsFolder_OnWindowPosChanging(browse_info *info, WINDOWPOS *pos)
1024 if ((info->lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE) && !(pos->flags & SWP_NOSIZE))
1026 if (pos->cx < info->szMin.cx)
1027 pos->cx = info->szMin.cx;
1028 if (pos->cy < info->szMin.cy)
1029 pos->cy = info->szMin.cy;
1031 return 0;
1034 static INT BrsFolder_OnDestroy(browse_info *info)
1036 if (info->layout)
1038 SHFree(info->layout);
1039 info->layout = NULL;
1042 SHChangeNotifyDeregister(info->hNotify);
1044 return 0;
1047 /* Find a treeview node by recursively walking the treeview */
1048 static HTREEITEM BrsFolder_FindItemByPidl(browse_info *info, LPCITEMIDLIST pidl, HTREEITEM hItem)
1050 TV_ITEMW item;
1051 TV_ITEMDATA *item_data;
1052 HRESULT hr;
1054 item.mask = TVIF_HANDLE | TVIF_PARAM;
1055 item.hItem = hItem;
1056 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
1057 item_data = (TV_ITEMDATA *)item.lParam;
1059 hr = IShellFolder_CompareIDs(item_data->lpsfParent, 0, item_data->lpifq, pidl);
1060 if(SUCCEEDED(hr) && !HRESULT_CODE(hr))
1061 return hItem;
1063 hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);
1065 while (hItem)
1067 HTREEITEM newItem = BrsFolder_FindItemByPidl(info, pidl, hItem);
1068 if (newItem)
1069 return newItem;
1070 hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
1072 return NULL;
1075 static LRESULT BrsFolder_OnChange(browse_info *info, const LPCITEMIDLIST *pidls, LONG event)
1077 BOOL ret = TRUE;
1079 TRACE("(%p)->(%p, %p, 0x%08x)\n", info, pidls[0], pidls[1], event);
1081 switch (event)
1083 case SHCNE_RMDIR:
1084 case SHCNE_DELETE:
1086 HTREEITEM handle_root = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
1087 HTREEITEM handle_item = BrsFolder_FindItemByPidl(info, pidls[0], handle_root);
1089 if (handle_item)
1090 SendMessageW(info->hwndTreeView, TVM_DELETEITEM, 0, (LPARAM)handle_item);
1092 break;
1095 return ret;
1098 /*************************************************************************
1099 * BrsFolderDlgProc32 (not an exported API function)
1101 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
1102 LPARAM lParam )
1104 browse_info *info;
1106 TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd, msg, wParam, lParam );
1108 if (msg == WM_INITDIALOG)
1109 return BrsFolder_OnCreate( hWnd, (browse_info*) lParam );
1111 info = GetPropW( hWnd, szBrowseFolderInfo );
1112 if (!info)
1113 return FALSE;
1115 switch (msg)
1117 case WM_NOTIFY:
1118 return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
1120 case WM_COMMAND:
1121 return BrsFolder_OnCommand( info, wParam );
1123 case WM_WINDOWPOSCHANGING:
1124 return BrsFolder_OnWindowPosChanging( info, (WINDOWPOS *)lParam);
1126 case WM_SIZE:
1127 if (info->layout) /* new style dialogs */
1128 LayoutUpdate(hWnd, info->layout, g_layout_info, ARRAY_SIZE(g_layout_info));
1129 return 0;
1131 case BFFM_SETSTATUSTEXTA:
1132 TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
1133 SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
1134 break;
1136 case BFFM_SETSTATUSTEXTW:
1137 TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
1138 SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
1139 break;
1141 case BFFM_ENABLEOK:
1142 TRACE("Enable %ld\n", lParam);
1143 EnableWindow(GetDlgItem(hWnd, 1), lParam != 0);
1144 break;
1146 case BFFM_SETOKTEXT: /* unicode only */
1147 TRACE("Set OK text %s\n", debugstr_w((LPWSTR)lParam));
1148 SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)lParam);
1149 break;
1151 case BFFM_SETSELECTIONA:
1152 return BrsFolder_OnSetSelectionA(info, (LPVOID)lParam, (BOOL)wParam);
1154 case BFFM_SETSELECTIONW:
1155 return BrsFolder_OnSetSelectionW(info, (LPVOID)lParam, (BOOL)wParam);
1157 case BFFM_SETEXPANDED: /* unicode only */
1158 return BrsFolder_OnSetExpanded(info, (LPVOID)lParam, (BOOL)wParam, NULL);
1160 case SHV_CHANGE_NOTIFY:
1161 return BrsFolder_OnChange(info, (const LPCITEMIDLIST*)wParam, (LONG)lParam);
1163 case WM_DESTROY:
1164 return BrsFolder_OnDestroy(info);
1166 return FALSE;
1169 static const WCHAR swBrowseTemplateName[] = {
1170 'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
1171 static const WCHAR swNewBrowseTemplateName[] = {
1172 'S','H','N','E','W','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
1174 /*************************************************************************
1175 * SHBrowseForFolderA [SHELL32.@]
1176 * SHBrowseForFolder [SHELL32.@]
1178 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
1180 BROWSEINFOW bi;
1181 LPITEMIDLIST lpid;
1182 INT len;
1183 LPWSTR title;
1185 TRACE("%p\n", lpbi);
1187 bi.hwndOwner = lpbi->hwndOwner;
1188 bi.pidlRoot = lpbi->pidlRoot;
1189 if (lpbi->pszDisplayName)
1190 bi.pszDisplayName = heap_alloc( MAX_PATH * sizeof(WCHAR) );
1191 else
1192 bi.pszDisplayName = NULL;
1194 if (lpbi->lpszTitle)
1196 len = MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0 );
1197 title = heap_alloc( len * sizeof(WCHAR) );
1198 MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, title, len );
1200 else
1201 title = NULL;
1203 bi.lpszTitle = title;
1204 bi.ulFlags = lpbi->ulFlags;
1205 bi.lpfn = lpbi->lpfn;
1206 bi.lParam = lpbi->lParam;
1207 bi.iImage = lpbi->iImage;
1208 lpid = SHBrowseForFolderW( &bi );
1209 if (bi.pszDisplayName)
1211 WideCharToMultiByte( CP_ACP, 0, bi.pszDisplayName, -1,
1212 lpbi->pszDisplayName, MAX_PATH, 0, NULL);
1213 heap_free( bi.pszDisplayName );
1215 heap_free(title);
1216 lpbi->iImage = bi.iImage;
1217 return lpid;
1221 /*************************************************************************
1222 * SHBrowseForFolderW [SHELL32.@]
1224 * NOTES
1225 * crashes when passed a null pointer
1227 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
1229 browse_info info;
1230 DWORD r;
1231 HRESULT hr;
1232 const WCHAR * templateName;
1233 INITCOMMONCONTROLSEX icex;
1235 info.hWnd = 0;
1236 info.pidlRet = NULL;
1237 info.lpBrowseInfo = lpbi;
1238 info.hwndTreeView = NULL;
1240 icex.dwSize = sizeof( icex );
1241 icex.dwICC = ICC_TREEVIEW_CLASSES;
1242 InitCommonControlsEx( &icex );
1244 hr = OleInitialize(NULL);
1246 if (lpbi->ulFlags & BIF_NEWDIALOGSTYLE)
1247 templateName = swNewBrowseTemplateName;
1248 else
1249 templateName = swBrowseTemplateName;
1250 r = DialogBoxParamW( shell32_hInstance, templateName, lpbi->hwndOwner,
1251 BrsFolderDlgProc, (LPARAM)&info );
1252 if (SUCCEEDED(hr))
1253 OleUninitialize();
1254 if (!r)
1256 ILFree(info.pidlRet);
1257 return NULL;
1260 return info.pidlRet;