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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * - many flags unimplemented
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #include "wine/debug.h"
31 #include "undocshell.h"
34 #include "shell32_main.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
40 static HWND hwndTreeView;
41 static LPBROWSEINFOW lpBrowseInfo;
42 static LPITEMIDLIST pidlRet;
44 static void FillTreeView(LPSHELLFOLDER lpsf, LPITEMIDLIST lpifq, HTREEITEM hParent, IEnumIDList* lpe);
45 static HTREEITEM InsertTreeViewItem(IShellFolder * lpsf, LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL, HTREEITEM hParent);
47 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
48 BIF_BROWSEFORCOMPUTER | \
49 BIF_RETURNFSANCESTORS | \
50 BIF_RETURNONLYFSDIRS | \
51 BIF_BROWSEINCLUDEFILES)
53 static inline DWORD BrowseFlagsToSHCONTF(UINT ulFlags)
55 return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
58 /******************************************************************************
59 * InitializeTreeView [Internal]
61 * Called from WM_INITDIALOG handler.
64 * hwndParent [I] The BrowseForFolder dialog
65 * root [I] ITEMIDLIST of the root shell folder
67 static void InitializeTreeView(HWND hwndParent, LPCITEMIDLIST root)
69 LPITEMIDLIST pidlParent, pidlChild;
70 HIMAGELIST hImageList;
72 IShellFolder *lpsfParent, *lpsfRoot;
73 IEnumIDList * pEnumChildren = NULL;
75 TRACE("dlg=%p tree=%p\n", hwndParent, hwndTreeView );
77 hwndTreeView = GetDlgItem (hwndParent, IDD_TREEVIEW);
79 FIXME("Could not get handle to treeview control! Error: %08lx\n", GetLastError());
82 Shell_GetImageList(NULL, &hImageList);
85 TreeView_SetImageList(hwndTreeView, hImageList, 0);
87 /* We want to call InsertTreeViewItem down the code, in order to insert
88 * the root item of the treeview. Due to InsertTreeViewItem's signature,
89 * we need the following to do this:
91 * + An ITEMIDLIST corresponding to _the parent_ of root.
92 * + An ITEMIDLIST, which is a relative path from root's parent to root
93 * (containing a single SHITEMID).
94 * + An IShellFolder interface pointer of root's parent folder.
96 * If root is 'Desktop', then root's parent is also 'Desktop'.
99 pidlParent = ILClone(root);
100 ILRemoveLastID(pidlParent);
101 pidlChild = ILClone(ILFindLastID(root));
103 if (_ILIsDesktop(pidlParent)) {
104 hr = SHGetDesktopFolder(&lpsfParent);
106 IShellFolder *lpsfDesktop;
107 hr = SHGetDesktopFolder(&lpsfDesktop);
108 if (!SUCCEEDED(hr)) {
109 WARN("SHGetDesktopFolder failed! hr = %08lx\n", hr);
112 hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
113 IShellFolder_Release(lpsfDesktop);
116 if (!SUCCEEDED(hr)) {
117 WARN("Could not bind to parent shell folder! hr = %08lx\n", hr);
121 if (pidlChild && pidlChild->mkid.cb) {
122 hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
124 lpsfRoot = lpsfParent;
125 hr = IShellFolder_AddRef(lpsfParent);
128 if (!SUCCEEDED(hr)) {
129 WARN("Could not bind to root shell folder! hr = %08lx\n", hr);
130 IShellFolder_Release(lpsfParent);
134 hr = IShellFolder_EnumObjects(lpsfRoot, hwndParent, BrowseFlagsToSHCONTF(lpBrowseInfo->ulFlags), &pEnumChildren);
135 if (!SUCCEEDED(hr)) {
136 WARN("Could not get child iterator! hr = %08lx\n", hr);
137 IShellFolder_Release(lpsfParent);
138 IShellFolder_Release(lpsfRoot);
142 TreeView_DeleteAllItems(hwndTreeView);
143 TreeView_Expand(hwndTreeView, InsertTreeViewItem(lpsfParent, pidlChild, pidlParent, pEnumChildren, TVI_ROOT), TVE_EXPAND);
145 IShellFolder_Release(lpsfRoot);
146 IShellFolder_Release(lpsfParent);
149 static int GetIcon(LPITEMIDLIST lpi, UINT uFlags)
152 SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
156 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
158 LPITEMIDLIST pidlDesktop = NULL;
160 TRACE("%p %p\n",lpifq, lpTV_ITEM);
164 pidlDesktop = _ILCreateDesktop();
168 lpTV_ITEM->iImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
169 lpTV_ITEM->iSelectedImage = GetIcon(lpifq, SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON);
179 LPSHELLFOLDER lpsfParent; /* IShellFolder of the parent */
180 LPITEMIDLIST lpi; /* PIDL relativ to parent */
181 LPITEMIDLIST lpifq; /* Fully qualified PIDL */
182 IEnumIDList* pEnumIL; /* Children iterator */
183 } TV_ITEMDATA, *LPTV_ITEMDATA;
185 /******************************************************************************
188 * Query a shell folder for the display name of one of it's children
191 * lpsf [I] IShellFolder interface of the folder to be queried.
192 * lpi [I] ITEMIDLIST of the child, relative to parent
193 * dwFlags [I] as in IShellFolder::GetDisplayNameOf
194 * lpFriendlyName [O] The desired display name in unicode
200 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
205 TRACE("%p %p %lx %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
206 if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
208 if (FAILED(StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi)))
216 TRACE("-- %s\n", debugstr_w(lpFriendlyName));
220 /******************************************************************************
221 * InsertTreeViewItem [Internal]
224 * lpsf [I] IShellFolder interface of the item's parent shell folder
225 * pidl [I] ITEMIDLIST of the child to insert, relativ to parent
226 * pidlParent [I] ITEMIDLIST of the parent shell folder
227 * pEnumIL [I] Iterator for the children of the item to be inserted
228 * hParent [I] The treeview-item that represents the parent shell folder
231 * Success: Handle to the created and inserted treeview-item
234 static HTREEITEM InsertTreeViewItem(IShellFolder * lpsf, LPCITEMIDLIST pidl,
235 LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL, HTREEITEM hParent)
238 TVINSERTSTRUCTW tvins;
239 WCHAR szBuff[MAX_PATH];
240 LPTV_ITEMDATA lptvid=0;
242 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
244 tvi.cChildren= pEnumIL ? 1 : 0;
245 tvi.mask |= TVIF_CHILDREN;
247 if (!(lptvid = (LPTV_ITEMDATA)SHAlloc(sizeof(TV_ITEMDATA))))
250 if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
253 tvi.pszText = szBuff;
254 tvi.cchTextMax = MAX_PATH;
255 tvi.lParam = (LPARAM)lptvid;
257 IShellFolder_AddRef(lpsf);
258 lptvid->lpsfParent = lpsf;
259 lptvid->lpi = ILClone(pidl);
260 lptvid->lpifq = pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
261 lptvid->pEnumIL = pEnumIL;
262 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
265 tvins.hInsertAfter = NULL;
266 tvins.hParent = hParent;
268 return (HTREEITEM)TreeView_InsertItemW(hwndTreeView, &tvins);
271 /******************************************************************************
272 * FillTreeView [Internal]
274 * For each child (given by lpe) of the parent shell folder, which is given by
275 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
278 * lpsf [I] IShellFolder interface of the parent shell folder
279 * pidl [I] ITEMIDLIST of the parent shell folder
280 * hParent [I] The treeview item that represents the parent shell folder
281 * lpe [I] An iterator for the children of the parent shell folder
283 static void FillTreeView(IShellFolder * lpsf, LPITEMIDLIST pidl, HTREEITEM hParent, IEnumIDList* lpe)
286 LPITEMIDLIST pidlTemp = 0;
289 HWND hwnd=GetParent(hwndTreeView);
291 TRACE("%p %p %x %p\n",lpsf, pidl, (INT)hParent, lpe);
293 /* No IEnumIDList -> No children */
296 SetCapture(GetParent(hwndTreeView));
297 SetCursor(LoadCursorA(0, (LPSTR)IDC_WAIT));
299 while (NOERROR == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
301 ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
302 IEnumIDList* pEnumIL = NULL;
303 IShellFolder* pSFChild = NULL;
304 IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
305 if (ulAttrs & SFGAO_FOLDER)
307 hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
310 hr = IShellFolder_EnumObjects(pSFChild, hwnd, BrowseFlagsToSHCONTF(lpBrowseInfo->ulFlags), &pEnumIL);
313 if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) || FAILED(IEnumIDList_Reset(pEnumIL)))
315 IEnumIDList_Release(pEnumIL);
319 IShellFolder_Release(pSFChild);
323 if (!(hPrev = InsertTreeViewItem(lpsf, pidlTemp, pidl, pEnumIL, hParent)))
325 SHFree(pidlTemp); /* Finally, free the pidl that the shell gave us... */
331 SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
337 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
339 LPPIDLDATA data = _ILGetDataPointer(pidl);
342 return (data->type == type);
345 static void BrsFolder_CheckValidSelection(HWND hWndTree, LPTV_ITEMDATA lptvid)
347 LPCITEMIDLIST pidl = lptvid->lpi;
348 BOOL bEnabled = TRUE;
350 if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
351 !PIDLIsType(pidl, PT_COMP))
353 if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
355 dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
356 if (FAILED(IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1, (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes)) ||
360 if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
362 dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
363 if (FAILED(IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1, (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes)) ||
364 (dwAttributes != (SFGAO_FOLDER | SFGAO_FILESYSTEM)))
367 SendMessageW(hWndTree, BFFM_ENABLEOK, 0, (LPARAM)bEnabled);
370 static LRESULT MsgNotify(HWND hWnd, UINT CtlID, LPNMHDR lpnmh)
372 NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
373 LPTV_ITEMDATA lptvid; /* Long pointer to TreeView item data */
374 IShellFolder * lpsf2=0;
377 TRACE("%p %x %p msg=%x\n", hWnd, CtlID, lpnmh, pnmtv->hdr.code);
379 switch (pnmtv->hdr.idFrom)
381 switch (pnmtv->hdr.code)
383 case TVN_DELETEITEMA:
384 case TVN_DELETEITEMW:
385 TRACE("TVN_DELETEITEMA/W\n");
386 lptvid=(LPTV_ITEMDATA)pnmtv->itemOld.lParam;
387 IShellFolder_Release(lptvid->lpsfParent);
389 IEnumIDList_Release(lptvid->pEnumIL);
391 SHFree(lptvid->lpifq);
395 case TVN_ITEMEXPANDINGA:
396 case TVN_ITEMEXPANDINGW:
398 TRACE("TVN_ITEMEXPANDINGA/W\n");
399 if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
402 lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
403 if (SUCCEEDED(IShellFolder_BindToObject(lptvid->lpsfParent, lptvid->lpi,0,(REFIID)&IID_IShellFolder,(LPVOID *)&lpsf2)))
404 { FillTreeView( lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
406 /* My Computer is already sorted and trying to do a simple text
407 * sort will only mess things up */
408 if (!_ILIsMyComputer(lptvid->lpi))
409 TreeView_SortChildren(hwndTreeView, pnmtv->itemNew.hItem, FALSE);
412 case TVN_SELCHANGEDA:
413 case TVN_SELCHANGEDW:
414 lptvid=(LPTV_ITEMDATA)pnmtv->itemNew.lParam;
415 pidlRet = lptvid->lpifq;
416 if (lpBrowseInfo->lpfn)
417 (lpBrowseInfo->lpfn)(hWnd, BFFM_SELCHANGED, (LPARAM)pidlRet, lpBrowseInfo->lParam);
418 BrsFolder_CheckValidSelection(hWnd, lptvid);
422 WARN("unhandled (%d)\n", pnmtv->hdr.code);
435 /*************************************************************************
436 * BrsFolderDlgProc32 (not an exported API function)
438 static INT_PTR CALLBACK BrsFolderDlgProc(HWND hWnd, UINT msg, WPARAM wParam,
441 TRACE("hwnd=%p msg=%04x 0x%08x 0x%08lx\n", hWnd, msg, wParam, lParam );
444 { case WM_INITDIALOG:
446 lpBrowseInfo = (LPBROWSEINFOW) lParam;
447 if (lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS)
448 FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS);
449 if (lpBrowseInfo->lpszTitle) {
450 SetWindowTextW(GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle);
452 ShowWindow(GetDlgItem(hWnd, IDD_TITLE), SW_HIDE);
454 if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT))
455 ShowWindow(GetDlgItem(hWnd, IDD_STATUS), SW_HIDE);
457 InitializeTreeView(hWnd, lpBrowseInfo->pidlRoot);
459 if (lpBrowseInfo->lpfn)
460 (lpBrowseInfo->lpfn)(hWnd, BFFM_INITIALIZED, 0, lpBrowseInfo->lParam);
465 MsgNotify( hWnd, (UINT)wParam, (LPNMHDR)lParam);
472 if (lpBrowseInfo->pszDisplayName)
473 SHGetPathFromIDListW(pidlRet, lpBrowseInfo->pszDisplayName);
474 EndDialog(hWnd, (DWORD) ILClone(pidlRet));
482 case BFFM_SETSTATUSTEXTA:
483 TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
484 SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
486 case BFFM_SETSTATUSTEXTW:
487 TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
488 SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
491 TRACE("Enable %ld\n", lParam);
492 EnableWindow(GetDlgItem(hWnd, 1), (lParam)?TRUE:FALSE);
494 case BFFM_SETOKTEXT: /* unicode only */
495 TRACE("Set OK text %s\n", debugstr_w((LPWSTR)wParam));
496 SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)wParam);
498 case BFFM_SETSELECTIONA:
500 FIXME("Set selection %s\n", debugstr_a((LPSTR)lParam));
502 FIXME("Set selection %p\n", (void*)lParam);
504 case BFFM_SETSELECTIONW:
506 FIXME("Set selection %s\n", debugstr_w((LPWSTR)lParam));
508 FIXME("Set selection %p\n", (void*)lParam);
510 case BFFM_SETEXPANDED: /* unicode only */
512 FIXME("Set expanded %s\n", debugstr_w((LPWSTR)lParam));
514 FIXME("Set expanded %p\n", (void*)lParam);
520 static const WCHAR swBrowseTempName[] = {'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
522 /*************************************************************************
523 * SHBrowseForFolderA [SHELL32.@]
524 * SHBrowseForFolder [SHELL32.@]
526 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
532 TRACE("(%p{lpszTitle=%s,owner=%p})\n", lpbi,
533 lpbi ? debugstr_a(lpbi->lpszTitle) : NULL, lpbi ? lpbi->hwndOwner : NULL);
538 bi.hwndOwner = lpbi->hwndOwner;
539 bi.pidlRoot = lpbi->pidlRoot;
540 if (lpbi->pszDisplayName)
542 /*lpbi->pszDisplayName is assumed to be MAX_PATH (MSDN) */
543 bi.pszDisplayName = HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
544 MultiByteToWideChar(CP_ACP, 0, lpbi->pszDisplayName, -1, bi.pszDisplayName, MAX_PATH);
547 bi.pszDisplayName = NULL;
551 len = MultiByteToWideChar(CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0);
552 bi.lpszTitle = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
553 MultiByteToWideChar(CP_ACP, 0, lpbi->lpszTitle, -1, (LPWSTR)bi.lpszTitle, len);
558 bi.ulFlags = lpbi->ulFlags;
559 bi.lpfn = lpbi->lpfn;
560 bi.lParam = lpbi->lParam;
561 bi.iImage = lpbi->iImage;
562 lpid = (LPITEMIDLIST) DialogBoxParamW(shell32_hInstance,
563 swBrowseTempName, lpbi->hwndOwner,
564 BrsFolderDlgProc, (INT)&bi);
565 if (bi.pszDisplayName)
567 WideCharToMultiByte(CP_ACP, 0, bi.pszDisplayName, -1, lpbi->pszDisplayName, MAX_PATH, 0, NULL);
568 HeapFree(GetProcessHeap(), 0, bi.pszDisplayName);
570 HeapFree(GetProcessHeap(), 0, (LPVOID)bi.lpszTitle);
571 lpbi->iImage = bi.iImage;
576 /*************************************************************************
577 * SHBrowseForFolderW [SHELL32.@]
579 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
581 TRACE("((%p->{lpszTitle=%s,owner=%p})\n", lpbi,
582 lpbi ? debugstr_w(lpbi->lpszTitle) : NULL, lpbi ? lpbi->hwndOwner : 0);
587 return (LPITEMIDLIST) DialogBoxParamW(shell32_hInstance,
588 swBrowseTempName, lpbi->hwndOwner,
589 BrsFolderDlgProc, (INT)lpbi);