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
20 * - many flags unimplemented
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
31 #include "wine/debug.h"
32 #include "undocshell.h"
34 #include "shell32_main.h"
37 #include "shellfolder.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(shell);
41 /* original margins and control size */
42 typedef struct tagLAYOUT_DATA
44 LONG left, width, right;
45 LONG top, height, bottom;
48 typedef struct tagbrowse_info
52 LPBROWSEINFOW lpBrowseInfo;
54 LAYOUT_DATA *layout; /* filled by LayoutInit, used by LayoutUpdate */
58 typedef struct tagTV_ITEMDATA
60 LPSHELLFOLDER lpsfParent; /* IShellFolder of the parent */
61 LPITEMIDLIST lpi; /* PIDL relative to parent */
62 LPITEMIDLIST lpifq; /* Fully qualified PIDL */
63 IEnumIDList* pEnumIL; /* Children iterator */
64 } TV_ITEMDATA, *LPTV_ITEMDATA;
66 typedef struct tagLAYOUT_INFO
68 int iItemId; /* control id */
69 DWORD dwAnchor; /* BF_* flags specifying which margins should remain constant */
72 static const LAYOUT_INFO g_layout_info[] =
74 {IDD_TITLE, BF_TOP|BF_LEFT|BF_RIGHT},
75 {IDD_STATUS, BF_TOP|BF_LEFT|BF_RIGHT},
76 {IDD_FOLDER, BF_TOP|BF_LEFT|BF_RIGHT},
77 {IDD_TREEVIEW, BF_TOP|BF_BOTTOM|BF_LEFT|BF_RIGHT},
78 {IDD_FOLDER, BF_BOTTOM|BF_LEFT},
79 {IDD_FOLDERTEXT, BF_BOTTOM|BF_LEFT|BF_RIGHT},
80 {IDD_MAKENEWFOLDER, BF_BOTTOM|BF_LEFT},
81 {IDOK, BF_BOTTOM|BF_RIGHT},
82 {IDCANCEL, BF_BOTTOM|BF_RIGHT}
85 #define LAYOUT_INFO_COUNT (sizeof(g_layout_info)/sizeof(g_layout_info[0]))
87 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
88 BIF_BROWSEFORCOMPUTER | \
89 BIF_RETURNFSANCESTORS | \
90 BIF_RETURNONLYFSDIRS | \
91 BIF_NONEWFOLDERBUTTON | \
92 BIF_NEWDIALOGSTYLE | \
93 BIF_BROWSEINCLUDEFILES)
95 static void FillTreeView(browse_info*, LPSHELLFOLDER,
96 LPITEMIDLIST, HTREEITEM, IEnumIDList*);
97 static HTREEITEM InsertTreeViewItem( browse_info*, IShellFolder *,
98 LPCITEMIDLIST, LPCITEMIDLIST, IEnumIDList*, HTREEITEM);
100 static const WCHAR szBrowseFolderInfo[] = {
101 '_','_','W','I','N','E','_',
102 'B','R','S','F','O','L','D','E','R','D','L','G','_',
106 static inline DWORD BrowseFlagsToSHCONTF(UINT ulFlags)
108 return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
111 static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo, HWND hWnd,
112 UINT msg, LPARAM param )
114 if (!lpBrowseInfo->lpfn)
116 lpBrowseInfo->lpfn( hWnd, msg, param, lpBrowseInfo->lParam );
119 static LAYOUT_DATA *LayoutInit(HWND hwnd, const LAYOUT_INFO *layout_info, int layout_count)
125 GetClientRect(hwnd, &rcWnd);
126 data = SHAlloc(sizeof(LAYOUT_DATA)*layout_count);
127 for (i = 0; i < layout_count; i++)
130 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
133 ERR("Item %d not found\n", i);
134 GetWindowRect(hItem, &r);
135 MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);
137 data[i].left = r.left;
138 data[i].right = rcWnd.right - r.right;
139 data[i].width = r.right - r.left;
142 data[i].bottom = rcWnd.bottom - r.bottom;
143 data[i].height = r.bottom - r.top;
148 static void LayoutUpdate(HWND hwnd, LAYOUT_DATA *data, const LAYOUT_INFO *layout_info, int layout_count)
153 GetClientRect(hwnd, &rcWnd);
154 for (i = 0; i < layout_count; i++)
157 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
159 GetWindowRect(hItem, &r);
160 MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);
162 if (layout_info[i].dwAnchor & BF_RIGHT)
164 r.right = rcWnd.right - data[i].right;
165 if (!(layout_info[i].dwAnchor & BF_LEFT))
166 r.left = r.right - data[i].width;
169 if (layout_info[i].dwAnchor & BF_BOTTOM)
171 r.bottom = rcWnd.bottom - data[i].bottom;
172 if (!(layout_info[i].dwAnchor & BF_TOP))
173 r.top = r.bottom - data[i].height;
176 SetWindowPos(hItem, NULL, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOZORDER);
181 /******************************************************************************
182 * InitializeTreeView [Internal]
184 * Called from WM_INITDIALOG handler.
187 * hwndParent [I] The BrowseForFolder dialog
188 * root [I] ITEMIDLIST of the root shell folder
190 static void InitializeTreeView( browse_info *info )
192 LPITEMIDLIST pidlParent, pidlChild;
193 HIMAGELIST hImageList;
195 IShellFolder *lpsfParent, *lpsfRoot;
196 IEnumIDList * pEnumChildren = NULL;
199 LPCITEMIDLIST root = info->lpBrowseInfo->pidlRoot;
201 TRACE("%p\n", info );
203 Shell_GetImageLists(NULL, &hImageList);
206 SendMessageW( info->hwndTreeView, TVM_SETIMAGELIST, 0, (LPARAM)hImageList );
208 /* We want to call InsertTreeViewItem down the code, in order to insert
209 * the root item of the treeview. Due to InsertTreeViewItem's signature,
210 * we need the following to do this:
212 * + An ITEMIDLIST corresponding to _the parent_ of root.
213 * + An ITEMIDLIST, which is a relative path from root's parent to root
214 * (containing a single SHITEMID).
215 * + An IShellFolder interface pointer of root's parent folder.
217 * If root is 'Desktop', then root's parent is also 'Desktop'.
220 pidlParent = ILClone(root);
221 ILRemoveLastID(pidlParent);
222 pidlChild = ILClone(ILFindLastID(root));
224 if (_ILIsDesktop(pidlParent)) {
225 hr = SHGetDesktopFolder(&lpsfParent);
227 IShellFolder *lpsfDesktop;
228 hr = SHGetDesktopFolder(&lpsfDesktop);
230 WARN("SHGetDesktopFolder failed! hr = %08x\n", hr);
235 hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
236 IShellFolder_Release(lpsfDesktop);
240 WARN("Could not bind to parent shell folder! hr = %08x\n", hr);
246 if (!_ILIsEmpty(pidlChild)) {
247 hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
249 lpsfRoot = lpsfParent;
250 hr = IShellFolder_AddRef(lpsfParent);
254 WARN("Could not bind to root shell folder! hr = %08x\n", hr);
255 IShellFolder_Release(lpsfParent);
261 flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
262 hr = IShellFolder_EnumObjects( lpsfRoot, info->hWnd, flags, &pEnumChildren );
264 WARN("Could not get child iterator! hr = %08x\n", hr);
265 IShellFolder_Release(lpsfParent);
266 IShellFolder_Release(lpsfRoot);
272 SendMessageW( info->hwndTreeView, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT );
273 item = InsertTreeViewItem( info, lpsfParent, pidlChild,
274 pidlParent, pEnumChildren, TVI_ROOT );
275 SendMessageW( info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item );
279 IShellFolder_Release(lpsfRoot);
280 IShellFolder_Release(lpsfParent);
283 static int GetIcon(LPCITEMIDLIST lpi, UINT uFlags)
286 SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
290 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
292 LPITEMIDLIST pidlDesktop = NULL;
295 TRACE("%p %p\n",lpifq, lpTV_ITEM);
299 pidlDesktop = _ILCreateDesktop();
303 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
304 lpTV_ITEM->iImage = GetIcon( lpifq, flags );
306 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
307 lpTV_ITEM->iSelectedImage = GetIcon( lpifq, flags );
310 ILFree( pidlDesktop );
313 /******************************************************************************
316 * Query a shell folder for the display name of one of it's children
319 * lpsf [I] IShellFolder interface of the folder to be queried.
320 * lpi [I] ITEMIDLIST of the child, relative to parent
321 * dwFlags [I] as in IShellFolder::GetDisplayNameOf
322 * lpFriendlyName [O] The desired display name in unicode
328 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
333 TRACE("%p %p %x %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
334 if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
335 bSuccess = StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi);
339 TRACE("-- %s\n", debugstr_w(lpFriendlyName));
343 /******************************************************************************
344 * InsertTreeViewItem [Internal]
347 * info [I] data for the dialog
348 * lpsf [I] IShellFolder interface of the item's parent shell folder
349 * pidl [I] ITEMIDLIST of the child to insert, relative to parent
350 * pidlParent [I] ITEMIDLIST of the parent shell folder
351 * pEnumIL [I] Iterator for the children of the item to be inserted
352 * hParent [I] The treeview-item that represents the parent shell folder
355 * Success: Handle to the created and inserted treeview-item
358 static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
359 LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
363 TVINSERTSTRUCTW tvins;
364 WCHAR szBuff[MAX_PATH];
365 LPTV_ITEMDATA lptvid=0;
367 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
369 tvi.cChildren= pEnumIL ? 1 : 0;
370 tvi.mask |= TVIF_CHILDREN;
372 lptvid = SHAlloc( sizeof(TV_ITEMDATA) );
376 if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
379 tvi.pszText = szBuff;
380 tvi.cchTextMax = MAX_PATH;
381 tvi.lParam = (LPARAM)lptvid;
383 IShellFolder_AddRef(lpsf);
384 lptvid->lpsfParent = lpsf;
385 lptvid->lpi = ILClone(pidl);
386 lptvid->lpifq = pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
387 lptvid->pEnumIL = pEnumIL;
388 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
391 tvins.hInsertAfter = NULL;
392 tvins.hParent = hParent;
394 return TreeView_InsertItemW( info->hwndTreeView, &tvins );
397 /******************************************************************************
398 * FillTreeView [Internal]
400 * For each child (given by lpe) of the parent shell folder, which is given by
401 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
404 * info [I] data for the dialog
405 * lpsf [I] IShellFolder interface of the parent shell folder
406 * pidl [I] ITEMIDLIST of the parent shell folder
407 * hParent [I] The treeview item that represents the parent shell folder
408 * lpe [I] An iterator for the children of the parent shell folder
410 static void FillTreeView( browse_info *info, IShellFolder * lpsf,
411 LPITEMIDLIST pidl, HTREEITEM hParent, IEnumIDList* lpe)
414 LPITEMIDLIST pidlTemp = 0;
417 HWND hwnd = GetParent( info->hwndTreeView );
419 TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
421 /* No IEnumIDList -> No children */
425 SetCursor( LoadCursorA( 0, (LPSTR)IDC_WAIT ) );
427 while (S_OK == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
429 ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
430 IEnumIDList* pEnumIL = NULL;
431 IShellFolder* pSFChild = NULL;
432 IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
433 if (ulAttrs & SFGAO_FOLDER)
435 hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
438 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
439 hr = IShellFolder_EnumObjects(pSFChild, hwnd, flags, &pEnumIL);
442 if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) ||
443 FAILED(IEnumIDList_Reset(pEnumIL)))
445 IEnumIDList_Release(pEnumIL);
449 IShellFolder_Release(pSFChild);
453 if (!(hPrev = InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent)))
455 SHFree(pidlTemp); /* Finally, free the pidl that the shell gave us... */
461 SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
465 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
467 LPPIDLDATA data = _ILGetDataPointer(pidl);
470 return (data->type == type);
473 static void BrsFolder_CheckValidSelection( browse_info *info, LPTV_ITEMDATA lptvid )
475 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
476 LPCITEMIDLIST pidl = lptvid->lpi;
477 BOOL bEnabled = TRUE;
481 if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
482 !PIDLIsType(pidl, PT_COMP))
484 if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
486 dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
487 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
488 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
489 if (FAILED(r) || !(dwAttributes & (SFGAO_FILESYSANCESTOR|SFGAO_FILESYSTEM)))
493 dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
494 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
495 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
497 ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
499 if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
501 EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), FALSE);
504 EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), TRUE);
506 SendMessageW(info->hWnd, BFFM_ENABLEOK, 0, bEnabled);
509 static LRESULT BrsFolder_Treeview_Delete( browse_info *info, NMTREEVIEWW *pnmtv )
511 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA)pnmtv->itemOld.lParam;
513 TRACE("TVN_DELETEITEMA/W %p\n", lptvid);
515 IShellFolder_Release(lptvid->lpsfParent);
517 IEnumIDList_Release(lptvid->pEnumIL);
519 SHFree(lptvid->lpifq);
524 static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
526 IShellFolder *lpsf2 = NULL;
527 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
530 TRACE("TVN_ITEMEXPANDINGA/W\n");
532 if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
535 if (!_ILIsEmpty(lptvid->lpi)) {
536 r = IShellFolder_BindToObject( lptvid->lpsfParent, lptvid->lpi, 0,
537 &IID_IShellFolder, (void**)&lpsf2 );
539 lpsf2 = lptvid->lpsfParent;
540 IShellFolder_AddRef(lpsf2);
546 FillTreeView( info, lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
547 IShellFolder_Release( lpsf2 );
550 /* My Computer is already sorted and trying to do a simple text
551 * sort will only mess things up */
552 if (!_ILIsMyComputer(lptvid->lpi))
553 SendMessageW( info->hwndTreeView, TVM_SORTCHILDREN,
554 FALSE, (LPARAM)pnmtv->itemNew.hItem );
559 static HRESULT BrsFolder_Treeview_Changed( browse_info *info, NMTREEVIEWW *pnmtv )
561 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
562 WCHAR name[MAX_PATH];
564 ILFree(info->pidlRet);
565 info->pidlRet = ILClone(lptvid->lpifq);
567 if (GetName(lptvid->lpsfParent, lptvid->lpi, SHGDN_NORMAL, name))
568 SetWindowTextW( GetDlgItem(info->hWnd, IDD_FOLDERTEXT), name );
570 browsefolder_callback( info->lpBrowseInfo, info->hWnd, BFFM_SELCHANGED,
571 (LPARAM)info->pidlRet );
572 BrsFolder_CheckValidSelection( info, lptvid );
576 static LRESULT BrsFolder_Treeview_Rename(browse_info *info, NMTVDISPINFOW *pnmtv)
578 LPTV_ITEMDATA item_data;
579 WCHAR old_path[MAX_PATH], new_path[MAX_PATH], *p;
583 if(!pnmtv->item.pszText)
586 item.mask = TVIF_HANDLE|TVIF_PARAM;
587 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
588 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
589 item_data = (LPTV_ITEMDATA)item.lParam;
591 SHGetPathFromIDListW(item_data->lpifq, old_path);
592 if(!(p = strrchrW(old_path, '\\')))
594 p = new_path+(p-old_path+1);
595 memcpy(new_path, old_path, (p-new_path)*sizeof(WCHAR));
596 strcpyW(p, pnmtv->item.pszText);
598 if(!MoveFileW(old_path, new_path))
601 SHFree(item_data->lpifq);
602 SHFree(item_data->lpi);
603 item_data->lpifq = SHSimpleIDListFromPathW(new_path);
604 IShellFolder_ParseDisplayName(item_data->lpsfParent, NULL, NULL,
605 pnmtv->item.pszText, NULL, &item_data->lpi, NULL);
607 item.mask = TVIF_HANDLE|TVIF_TEXT;
608 item.pszText = pnmtv->item.pszText;
609 SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);
611 nmtv.itemNew.lParam = item.lParam;
612 BrsFolder_Treeview_Changed(info, &nmtv);
616 static LRESULT BrsFolder_OnNotify( browse_info *info, UINT CtlID, LPNMHDR lpnmh )
618 NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
620 TRACE("%p %x %p msg=%x\n", info, CtlID, lpnmh, pnmtv->hdr.code);
622 if (pnmtv->hdr.idFrom != IDD_TREEVIEW)
625 switch (pnmtv->hdr.code)
627 case TVN_DELETEITEMA:
628 case TVN_DELETEITEMW:
629 return BrsFolder_Treeview_Delete( info, pnmtv );
631 case TVN_ITEMEXPANDINGA:
632 case TVN_ITEMEXPANDINGW:
633 return BrsFolder_Treeview_Expand( info, pnmtv );
635 case TVN_SELCHANGEDA:
636 case TVN_SELCHANGEDW:
637 return BrsFolder_Treeview_Changed( info, pnmtv );
639 case TVN_ENDLABELEDITA:
640 case TVN_ENDLABELEDITW:
641 return BrsFolder_Treeview_Rename( info, (LPNMTVDISPINFOW)pnmtv );
644 WARN("unhandled (%d)\n", pnmtv->hdr.code);
652 static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
654 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
657 SetPropW( hWnd, szBrowseFolderInfo, info );
659 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
660 FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
661 if (lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS)
662 FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS);
664 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
668 info->layout = LayoutInit(hWnd, g_layout_info, LAYOUT_INFO_COUNT);
670 /* TODO: Windows allows shrinking the windows a bit */
671 GetWindowRect(hWnd, &rcWnd);
672 info->szMin.cx = rcWnd.right - rcWnd.left;
673 info->szMin.cy = rcWnd.bottom - rcWnd.top;
680 if (lpBrowseInfo->lpszTitle)
681 SetWindowTextW( GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle );
683 ShowWindow( GetDlgItem(hWnd, IDD_TITLE), SW_HIDE );
685 if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT)
686 || (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
687 ShowWindow( GetDlgItem(hWnd, IDD_STATUS), SW_HIDE );
689 /* Hide "Make New Folder" Button? */
690 if ((lpBrowseInfo->ulFlags & BIF_NONEWFOLDERBUTTON)
691 || !(lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
692 ShowWindow( GetDlgItem(hWnd, IDD_MAKENEWFOLDER), SW_HIDE );
694 /* Hide the editbox? */
695 if (!(lpBrowseInfo->ulFlags & BIF_EDITBOX))
697 ShowWindow( GetDlgItem(hWnd, IDD_FOLDER), SW_HIDE );
698 ShowWindow( GetDlgItem(hWnd, IDD_FOLDERTEXT), SW_HIDE );
701 info->hwndTreeView = GetDlgItem( hWnd, IDD_TREEVIEW );
702 if (info->hwndTreeView)
704 InitializeTreeView( info );
706 /* Resize the treeview if there's not editbox */
707 if ((lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
708 && !(lpBrowseInfo->ulFlags & BIF_EDITBOX))
711 GetClientRect(info->hwndTreeView, &rc);
712 SetWindowPos(info->hwndTreeView, HWND_TOP, 0, 0,
713 rc.right, rc.bottom + 40, SWP_NOMOVE);
717 ERR("treeview control missing!\n");
719 browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
724 static HRESULT BrsFolder_Rename(browse_info *info, HTREEITEM rename)
726 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)rename);
727 SendMessageW(info->hwndTreeView, TVM_EDITLABELW, 0, (LPARAM)rename);
731 static HRESULT BrsFolder_NewFolder(browse_info *info)
733 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
734 IShellFolder *desktop, *cur;
736 WCHAR name[MAX_PATH];
737 HTREEITEM parent, added;
738 LPTV_ITEMDATA item_data;
739 LPITEMIDLIST new_item;
745 ERR("Make new folder button should be disabled\n");
749 /* Create new directory */
750 hr = SHGetDesktopFolder(&desktop);
753 hr = IShellFolder_BindToObject(desktop, info->pidlRet, 0, &IID_IShellFolder, (void**)&cur);
754 IShellFolder_Release(desktop);
758 hr = IShellFolder_QueryInterface(cur, &IID_ISFHelper, (void**)&sfhelper);
762 hr = SHGetPathFromIDListW(info->pidlRet, name);
769 hr = ISFHelper_GetUniqueName(sfhelper, &name[len], MAX_PATH-len);
770 ISFHelper_Release(sfhelper);
775 if(!CreateDirectoryW(name, NULL))
778 /* Update parent of newly created directory */
779 parent = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
783 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)parent);
785 memset(&item, 0, sizeof(TVITEMW));
786 item.mask = TVIF_PARAM|TVIF_STATE;
788 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
789 item_data = (LPTV_ITEMDATA)item.lParam;
793 if(item_data->pEnumIL)
794 IEnumIDList_Release(item_data->pEnumIL);
795 hr = IShellFolder_EnumObjects(cur, info->hwndTreeView, flags, &item_data->pEnumIL);
799 /* Update treeview */
800 if(!(item.state&TVIS_EXPANDEDONCE)) {
801 item.mask = TVIF_STATE;
802 item.state = TVIS_EXPANDEDONCE;
803 item.stateMask = TVIS_EXPANDEDONCE;
804 SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);
807 hr = IShellFolder_ParseDisplayName(cur, NULL, NULL, name+len, NULL, &new_item, NULL);
811 added = InsertTreeViewItem(info, cur, new_item, item_data->lpifq, NULL, parent);
812 IShellFolder_Release(cur);
815 SendMessageW(info->hwndTreeView, TVM_SORTCHILDREN, FALSE, (LPARAM)parent);
816 return BrsFolder_Rename(info, added);
822 static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
824 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
829 if (info->pidlRet == NULL) /* A null pidl would mean a cancel */
830 info->pidlRet = _ILCreateDesktop();
831 pdump( info->pidlRet );
832 if (lpBrowseInfo->pszDisplayName)
833 SHGetPathFromIDListW( info->pidlRet, lpBrowseInfo->pszDisplayName );
834 EndDialog( info->hWnd, 1 );
838 EndDialog( info->hWnd, 0 );
841 case IDD_MAKENEWFOLDER:
842 BrsFolder_NewFolder(info);
848 static BOOL BrsFolder_OnSetExpanded(browse_info *info, LPVOID selection,
849 BOOL is_str, HTREEITEM *pItem)
851 LPITEMIDLIST pidlSelection = selection;
852 LPCITEMIDLIST pidlCurrent, pidlRoot;
854 BOOL bResult = FALSE;
856 memset(&item, 0, sizeof(item));
858 /* If 'selection' is a string, convert to a Shell ID List. */
860 IShellFolder *psfDesktop;
863 hr = SHGetDesktopFolder(&psfDesktop);
867 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL,
868 selection, NULL, &pidlSelection, NULL);
869 IShellFolder_Release(psfDesktop);
874 /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
875 * the sub-tree currently displayed. */
876 pidlRoot = info->lpBrowseInfo->pidlRoot;
877 pidlCurrent = pidlSelection;
878 while (!_ILIsEmpty(pidlRoot) && _ILIsEqualSimple(pidlRoot, pidlCurrent)) {
879 pidlRoot = ILGetNext(pidlRoot);
880 pidlCurrent = ILGetNext(pidlCurrent);
883 /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
884 if (!_ILIsEmpty(pidlRoot))
887 /* Initialize item to point to the first child of the root folder. */
888 item.mask = TVIF_PARAM;
889 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
892 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
895 /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
896 while (item.hItem && !_ILIsEmpty(pidlCurrent)) {
897 LPTV_ITEMDATA pItemData;
899 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
900 pItemData = (LPTV_ITEMDATA)item.lParam;
902 if (_ILIsEqualSimple(pItemData->lpi, pidlCurrent)) {
903 pidlCurrent = ILGetNext(pidlCurrent);
904 if (!_ILIsEmpty(pidlCurrent)) {
905 /* Only expand current node and move on to it's first child,
906 * if we didn't already reach the last SHITEMID */
907 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item.hItem);
908 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
912 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT,
917 if (_ILIsEmpty(pidlCurrent) && item.hItem)
921 if (pidlSelection && pidlSelection != selection)
922 ILFree(pidlSelection);
930 static BOOL BrsFolder_OnSetSelectionW(browse_info *info, LPVOID selection, BOOL is_str) {
934 if (!selection) return FALSE;
936 bResult = BrsFolder_OnSetExpanded(info, selection, is_str, &hItem);
938 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem );
942 static BOOL BrsFolder_OnSetSelectionA(browse_info *info, LPVOID selection, BOOL is_str) {
943 LPWSTR selectionW = NULL;
948 return BrsFolder_OnSetSelectionW(info, selection, is_str);
950 if ((length = MultiByteToWideChar(CP_ACP, 0, selection, -1, NULL, 0)) &&
951 (selectionW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR))) &&
952 MultiByteToWideChar(CP_ACP, 0, selection, -1, selectionW, length))
954 result = BrsFolder_OnSetSelectionW(info, selectionW, is_str);
957 HeapFree(GetProcessHeap(), 0, selectionW);
961 static BOOL BrsFolder_OnWindowPosChanging(browse_info *info, WINDOWPOS *pos)
963 if ((info->lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE) && !(pos->flags & SWP_NOSIZE))
965 if (pos->cx < info->szMin.cx)
966 pos->cx = info->szMin.cx;
967 if (pos->cy < info->szMin.cy)
968 pos->cy = info->szMin.cy;
973 static INT BrsFolder_OnDestroy(browse_info *info)
977 SHFree(info->layout);
984 /*************************************************************************
985 * BrsFolderDlgProc32 (not an exported API function)
987 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
992 TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd, msg, wParam, lParam );
994 if (msg == WM_INITDIALOG)
995 return BrsFolder_OnCreate( hWnd, (browse_info*) lParam );
997 info = GetPropW( hWnd, szBrowseFolderInfo );
1002 return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
1005 return BrsFolder_OnCommand( info, wParam );
1007 case WM_WINDOWPOSCHANGING:
1008 return BrsFolder_OnWindowPosChanging( info, (WINDOWPOS *)lParam);
1011 if (info->layout) /* new style dialogs */
1012 LayoutUpdate(hWnd, info->layout, g_layout_info, LAYOUT_INFO_COUNT);
1015 case BFFM_SETSTATUSTEXTA:
1016 TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
1017 SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
1020 case BFFM_SETSTATUSTEXTW:
1021 TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
1022 SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
1026 TRACE("Enable %ld\n", lParam);
1027 EnableWindow(GetDlgItem(hWnd, 1), lParam != 0);
1030 case BFFM_SETOKTEXT: /* unicode only */
1031 TRACE("Set OK text %s\n", debugstr_w((LPWSTR)wParam));
1032 SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)wParam);
1035 case BFFM_SETSELECTIONA:
1036 return BrsFolder_OnSetSelectionA(info, (LPVOID)lParam, (BOOL)wParam);
1038 case BFFM_SETSELECTIONW:
1039 return BrsFolder_OnSetSelectionW(info, (LPVOID)lParam, (BOOL)wParam);
1041 case BFFM_SETEXPANDED: /* unicode only */
1042 return BrsFolder_OnSetExpanded(info, (LPVOID)lParam, (BOOL)wParam, NULL);
1045 return BrsFolder_OnDestroy(info);
1050 static const WCHAR swBrowseTemplateName[] = {
1051 'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
1052 static const WCHAR swNewBrowseTemplateName[] = {
1053 'S','H','N','E','W','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
1055 /*************************************************************************
1056 * SHBrowseForFolderA [SHELL32.@]
1057 * SHBrowseForFolder [SHELL32.@]
1059 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
1066 TRACE("%p\n", lpbi);
1068 bi.hwndOwner = lpbi->hwndOwner;
1069 bi.pidlRoot = lpbi->pidlRoot;
1070 if (lpbi->pszDisplayName)
1071 bi.pszDisplayName = HeapAlloc( GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR) );
1073 bi.pszDisplayName = NULL;
1075 if (lpbi->lpszTitle)
1077 len = MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0 );
1078 title = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1079 MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, title, len );
1084 bi.lpszTitle = title;
1085 bi.ulFlags = lpbi->ulFlags;
1086 bi.lpfn = lpbi->lpfn;
1087 bi.lParam = lpbi->lParam;
1088 bi.iImage = lpbi->iImage;
1089 lpid = SHBrowseForFolderW( &bi );
1090 if (bi.pszDisplayName)
1092 WideCharToMultiByte( CP_ACP, 0, bi.pszDisplayName, -1,
1093 lpbi->pszDisplayName, MAX_PATH, 0, NULL);
1094 HeapFree( GetProcessHeap(), 0, bi.pszDisplayName );
1096 HeapFree(GetProcessHeap(), 0, title);
1097 lpbi->iImage = bi.iImage;
1102 /*************************************************************************
1103 * SHBrowseForFolderW [SHELL32.@]
1106 * crashes when passed a null pointer
1108 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
1113 const WCHAR * templateName;
1116 info.pidlRet = NULL;
1117 info.lpBrowseInfo = lpbi;
1118 info.hwndTreeView = NULL;
1120 hr = OleInitialize(NULL);
1122 if (lpbi->ulFlags & BIF_NEWDIALOGSTYLE)
1123 templateName = swNewBrowseTemplateName;
1125 templateName = swBrowseTemplateName;
1126 r = DialogBoxParamW( shell32_hInstance, templateName, lpbi->hwndOwner,
1127 BrsFolderDlgProc, (LPARAM)&info );
1132 ILFree(info.pidlRet);
1136 return info.pidlRet;