shell32: Fix interface leak while adding tree items.
[wine.git] / dlls / shell32 / brsfolder.c
blobe990c61e96f9971b7622b6f27daa58dc8c759a04
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 new dialog style "make new folder" button
22 * - implement editbox
25 #include <stdlib.h>
26 #include <string.h>
28 #define COBJMACROS
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
32 #include "wine/debug.h"
33 #include "undocshell.h"
34 #include "pidl.h"
35 #include "shell32_main.h"
36 #include "shellapi.h"
37 #include "shresdef.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;
46 } LAYOUT_DATA;
48 typedef struct tagbrowse_info
50 HWND hWnd;
51 HWND hwndTreeView;
52 LPBROWSEINFOW lpBrowseInfo;
53 LPITEMIDLIST pidlRet;
54 LAYOUT_DATA *layout; /* filled by LayoutInit, used by LayoutUpdate */
55 SIZE szMin;
56 } browse_info;
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 */
70 } LAYOUT_INFO;
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','_',
103 'I','N','F','O',0
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)
115 return;
116 lpBrowseInfo->lpfn( hWnd, msg, param, lpBrowseInfo->lParam );
119 static LAYOUT_DATA *LayoutInit(HWND hwnd, const LAYOUT_INFO *layout_info, int layout_count)
121 LAYOUT_DATA *data;
122 RECT rcWnd;
123 int i;
125 GetClientRect(hwnd, &rcWnd);
126 data = SHAlloc(sizeof(LAYOUT_DATA)*layout_count);
127 for (i = 0; i < layout_count; i++)
129 RECT r;
130 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
132 if (hItem == NULL)
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;
141 data[i].top = r.top;
142 data[i].bottom = rcWnd.bottom - r.bottom;
143 data[i].height = r.bottom - r.top;
145 return data;
148 static void LayoutUpdate(HWND hwnd, LAYOUT_DATA *data, const LAYOUT_INFO *layout_info, int layout_count)
150 RECT rcWnd;
151 int i;
153 GetClientRect(hwnd, &rcWnd);
154 for (i = 0; i < layout_count; i++)
156 RECT r;
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.
186 * PARAMS
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;
194 HRESULT hr;
195 IShellFolder *lpsfParent, *lpsfRoot;
196 IEnumIDList * pEnumChildren = NULL;
197 HTREEITEM item;
198 DWORD flags;
199 LPCITEMIDLIST root = info->lpBrowseInfo->pidlRoot;
201 TRACE("%p\n", info );
203 Shell_GetImageLists(NULL, &hImageList);
205 if (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);
226 } else {
227 IShellFolder *lpsfDesktop;
228 hr = SHGetDesktopFolder(&lpsfDesktop);
229 if (FAILED(hr)) {
230 WARN("SHGetDesktopFolder failed! hr = %08x\n", hr);
231 return;
233 hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
234 IShellFolder_Release(lpsfDesktop);
237 if (FAILED(hr)) {
238 WARN("Could not bind to parent shell folder! hr = %08x\n", hr);
239 return;
242 if (!_ILIsEmpty(pidlChild)) {
243 hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
244 } else {
245 lpsfRoot = lpsfParent;
246 hr = IShellFolder_AddRef(lpsfParent);
249 if (FAILED(hr)) {
250 WARN("Could not bind to root shell folder! hr = %08x\n", hr);
251 IShellFolder_Release(lpsfParent);
252 return;
255 flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
256 hr = IShellFolder_EnumObjects( lpsfRoot, info->hWnd, flags, &pEnumChildren );
257 if (FAILED(hr)) {
258 WARN("Could not get child iterator! hr = %08x\n", hr);
259 IShellFolder_Release(lpsfParent);
260 IShellFolder_Release(lpsfRoot);
261 return;
264 SendMessageW( info->hwndTreeView, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT );
265 item = InsertTreeViewItem( info, lpsfParent, pidlChild,
266 pidlParent, pEnumChildren, TVI_ROOT );
267 SendMessageW( info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item );
269 IShellFolder_Release(lpsfRoot);
270 IShellFolder_Release(lpsfParent);
273 static int GetIcon(LPCITEMIDLIST lpi, UINT uFlags)
275 SHFILEINFOW sfi;
276 SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
277 return sfi.iIcon;
280 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
282 LPITEMIDLIST pidlDesktop = NULL;
283 DWORD flags;
285 TRACE("%p %p\n",lpifq, lpTV_ITEM);
287 if (!lpifq)
289 pidlDesktop = _ILCreateDesktop();
290 lpifq = pidlDesktop;
293 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
294 lpTV_ITEM->iImage = GetIcon( lpifq, flags );
296 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
297 lpTV_ITEM->iSelectedImage = GetIcon( lpifq, flags );
299 if (pidlDesktop)
300 ILFree( pidlDesktop );
303 /******************************************************************************
304 * GetName [Internal]
306 * Query a shell folder for the display name of one of it's children
308 * PARAMS
309 * lpsf [I] IShellFolder interface of the folder to be queried.
310 * lpi [I] ITEMIDLIST of the child, relative to parent
311 * dwFlags [I] as in IShellFolder::GetDisplayNameOf
312 * lpFriendlyName [O] The desired display name in unicode
314 * RETURNS
315 * Success: TRUE
316 * Failure: FALSE
318 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
320 BOOL bSuccess=TRUE;
321 STRRET str;
323 TRACE("%p %p %x %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
324 if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
325 bSuccess = StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi);
326 else
327 bSuccess = FALSE;
329 TRACE("-- %s\n", debugstr_w(lpFriendlyName));
330 return bSuccess;
333 /******************************************************************************
334 * InsertTreeViewItem [Internal]
336 * PARAMS
337 * info [I] data for the dialog
338 * lpsf [I] IShellFolder interface of the item's parent shell folder
339 * pidl [I] ITEMIDLIST of the child to insert, relative to parent
340 * pidlParent [I] ITEMIDLIST of the parent shell folder
341 * pEnumIL [I] Iterator for the children of the item to be inserted
342 * hParent [I] The treeview-item that represents the parent shell folder
344 * RETURNS
345 * Success: Handle to the created and inserted treeview-item
346 * Failure: NULL
348 static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
349 LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
350 HTREEITEM hParent)
352 TVITEMW tvi;
353 TVINSERTSTRUCTW tvins;
354 WCHAR szBuff[MAX_PATH];
355 LPTV_ITEMDATA lptvid=0;
357 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
359 tvi.cChildren= pEnumIL ? 1 : 0;
360 tvi.mask |= TVIF_CHILDREN;
362 lptvid = SHAlloc( sizeof(TV_ITEMDATA) );
363 if (!lptvid)
364 return NULL;
366 if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
367 return NULL;
369 tvi.pszText = szBuff;
370 tvi.cchTextMax = MAX_PATH;
371 tvi.lParam = (LPARAM)lptvid;
373 IShellFolder_AddRef(lpsf);
374 lptvid->lpsfParent = lpsf;
375 lptvid->lpi = ILClone(pidl);
376 lptvid->lpifq = pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
377 lptvid->pEnumIL = pEnumIL;
378 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
380 tvins.u.item = tvi;
381 tvins.hInsertAfter = NULL;
382 tvins.hParent = hParent;
384 return TreeView_InsertItemW( info->hwndTreeView, &tvins );
387 /******************************************************************************
388 * FillTreeView [Internal]
390 * For each child (given by lpe) of the parent shell folder, which is given by
391 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
393 * PARAMS
394 * info [I] data for the dialog
395 * lpsf [I] IShellFolder interface of the parent shell folder
396 * pidl [I] ITEMIDLIST of the parent shell folder
397 * hParent [I] The treeview item that represents the parent shell folder
398 * lpe [I] An iterator for the children of the parent shell folder
400 static void FillTreeView( browse_info *info, IShellFolder * lpsf,
401 LPITEMIDLIST pidl, HTREEITEM hParent, IEnumIDList* lpe)
403 HTREEITEM hPrev = 0;
404 LPITEMIDLIST pidlTemp = 0;
405 ULONG ulFetched;
406 HRESULT hr;
407 HWND hwnd = GetParent( info->hwndTreeView );
409 TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
411 /* No IEnumIDList -> No children */
412 if (!lpe) return;
414 SetCapture( hwnd );
415 SetCursor( LoadCursorA( 0, (LPSTR)IDC_WAIT ) );
417 while (S_OK == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
419 ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
420 IEnumIDList* pEnumIL = NULL;
421 IShellFolder* pSFChild = NULL;
422 IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
423 if (ulAttrs & SFGAO_FOLDER)
425 hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
426 if (SUCCEEDED(hr))
428 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
429 hr = IShellFolder_EnumObjects(pSFChild, hwnd, flags, &pEnumIL);
430 if (hr == S_OK)
432 if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) ||
433 FAILED(IEnumIDList_Reset(pEnumIL)))
435 IEnumIDList_Release(pEnumIL);
436 pEnumIL = NULL;
439 IShellFolder_Release(pSFChild);
443 if (!(hPrev = InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent)))
444 goto done;
445 SHFree(pidlTemp); /* Finally, free the pidl that the shell gave us... */
446 pidlTemp=NULL;
449 done:
450 ReleaseCapture();
451 SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
452 SHFree(pidlTemp);
455 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
457 LPPIDLDATA data = _ILGetDataPointer(pidl);
458 if (!data)
459 return FALSE;
460 return (data->type == type);
463 static void BrsFolder_CheckValidSelection( browse_info *info, LPTV_ITEMDATA lptvid )
465 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
466 LPCITEMIDLIST pidl = lptvid->lpi;
467 BOOL bEnabled = TRUE;
468 DWORD dwAttributes;
469 HRESULT r;
471 if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
472 !PIDLIsType(pidl, PT_COMP))
473 bEnabled = FALSE;
474 if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
476 dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
477 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
478 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
479 if (FAILED(r) || !(dwAttributes & (SFGAO_FILESYSANCESTOR|SFGAO_FILESYSTEM)))
480 bEnabled = FALSE;
482 if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
484 dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
485 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
486 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
487 if (FAILED(r) ||
488 ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
490 bEnabled = FALSE;
493 SendMessageW(info->hWnd, BFFM_ENABLEOK, 0, bEnabled);
496 static LRESULT BrsFolder_Treeview_Delete( browse_info *info, NMTREEVIEWW *pnmtv )
498 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA)pnmtv->itemOld.lParam;
500 TRACE("TVN_DELETEITEMA/W %p\n", lptvid);
502 IShellFolder_Release(lptvid->lpsfParent);
503 if (lptvid->pEnumIL)
504 IEnumIDList_Release(lptvid->pEnumIL);
505 SHFree(lptvid->lpi);
506 SHFree(lptvid->lpifq);
507 SHFree(lptvid);
508 return 0;
511 static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
513 IShellFolder *lpsf2 = NULL;
514 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
515 HRESULT r;
517 TRACE("TVN_ITEMEXPANDINGA/W\n");
519 if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
520 return 0;
522 if (!_ILIsEmpty(lptvid->lpi)) {
523 r = IShellFolder_BindToObject( lptvid->lpsfParent, lptvid->lpi, 0,
524 &IID_IShellFolder, (void**)&lpsf2 );
525 } else {
526 lpsf2 = lptvid->lpsfParent;
527 IShellFolder_AddRef(lpsf2);
528 r = S_OK;
531 if (SUCCEEDED(r))
533 FillTreeView( info, lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
534 IShellFolder_Release( lpsf2 );
537 /* My Computer is already sorted and trying to do a simple text
538 * sort will only mess things up */
539 if (!_ILIsMyComputer(lptvid->lpi))
540 SendMessageW( info->hwndTreeView, TVM_SORTCHILDREN,
541 FALSE, (LPARAM)pnmtv->itemNew.hItem );
543 return 0;
546 static HRESULT BrsFolder_Treeview_Changed( browse_info *info, NMTREEVIEWW *pnmtv )
548 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
550 lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
551 info->pidlRet = lptvid->lpifq;
552 browsefolder_callback( info->lpBrowseInfo, info->hWnd, BFFM_SELCHANGED,
553 (LPARAM)info->pidlRet );
554 BrsFolder_CheckValidSelection( info, lptvid );
555 return 0;
558 static LRESULT BrsFolder_OnNotify( browse_info *info, UINT CtlID, LPNMHDR lpnmh )
560 NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
562 TRACE("%p %x %p msg=%x\n", info, CtlID, lpnmh, pnmtv->hdr.code);
564 if (pnmtv->hdr.idFrom != IDD_TREEVIEW)
565 return 0;
567 switch (pnmtv->hdr.code)
569 case TVN_DELETEITEMA:
570 case TVN_DELETEITEMW:
571 return BrsFolder_Treeview_Delete( info, pnmtv );
573 case TVN_ITEMEXPANDINGA:
574 case TVN_ITEMEXPANDINGW:
575 return BrsFolder_Treeview_Expand( info, pnmtv );
577 case TVN_SELCHANGEDA:
578 case TVN_SELCHANGEDW:
579 return BrsFolder_Treeview_Changed( info, pnmtv );
581 default:
582 WARN("unhandled (%d)\n", pnmtv->hdr.code);
583 break;
586 return 0;
590 static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
592 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
594 info->hWnd = hWnd;
595 SetPropW( hWnd, szBrowseFolderInfo, info );
597 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
598 FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
599 if (lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS)
600 FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS);
602 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
604 RECT rcWnd;
606 info->layout = LayoutInit(hWnd, g_layout_info, LAYOUT_INFO_COUNT);
608 /* TODO: Windows allows shrinking the windows a bit */
609 GetWindowRect(hWnd, &rcWnd);
610 info->szMin.cx = rcWnd.right - rcWnd.left;
611 info->szMin.cy = rcWnd.bottom - rcWnd.top;
613 else
615 info->layout = NULL;
618 if (lpBrowseInfo->lpszTitle)
619 SetWindowTextW( GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle );
620 else
621 ShowWindow( GetDlgItem(hWnd, IDD_TITLE), SW_HIDE );
623 if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT)
624 || (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
625 ShowWindow( GetDlgItem(hWnd, IDD_STATUS), SW_HIDE );
627 /* Hide "Make New Folder" Button? */
628 if ((lpBrowseInfo->ulFlags & BIF_NONEWFOLDERBUTTON)
629 || !(lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
630 ShowWindow( GetDlgItem(hWnd, IDD_MAKENEWFOLDER), SW_HIDE );
632 /* Hide the editbox? */
633 if (!(lpBrowseInfo->ulFlags & BIF_EDITBOX))
635 ShowWindow( GetDlgItem(hWnd, IDD_FOLDER), SW_HIDE );
636 ShowWindow( GetDlgItem(hWnd, IDD_FOLDERTEXT), SW_HIDE );
639 info->hwndTreeView = GetDlgItem( hWnd, IDD_TREEVIEW );
640 if (info->hwndTreeView)
642 InitializeTreeView( info );
644 /* Resize the treeview if there's not editbox */
645 if ((lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
646 && !(lpBrowseInfo->ulFlags & BIF_EDITBOX))
648 RECT rc;
649 GetClientRect(info->hwndTreeView, &rc);
650 SetWindowPos(info->hwndTreeView, HWND_TOP, 0, 0,
651 rc.right, rc.bottom + 40, SWP_NOMOVE);
654 else
655 ERR("treeview control missing!\n");
657 browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
659 return TRUE;
662 static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
664 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
666 switch (id)
668 case IDOK:
669 /* The original pidl is owned by the treeview and will be free'd. */
670 info->pidlRet = ILClone(info->pidlRet);
671 if (info->pidlRet == NULL) /* A null pidl would mean a cancel */
672 info->pidlRet = _ILCreateDesktop();
673 pdump( info->pidlRet );
674 if (lpBrowseInfo->pszDisplayName)
675 SHGetPathFromIDListW( info->pidlRet, lpBrowseInfo->pszDisplayName );
676 EndDialog( info->hWnd, 1 );
677 return TRUE;
679 case IDCANCEL:
680 EndDialog( info->hWnd, 0 );
681 return TRUE;
683 case IDD_MAKENEWFOLDER:
684 FIXME("make new folder not implemented\n");
685 return TRUE;
687 return FALSE;
690 static BOOL BrsFolder_OnSetExpanded(browse_info *info, LPVOID selection,
691 BOOL is_str, HTREEITEM *pItem)
693 LPITEMIDLIST pidlSelection = selection;
694 LPCITEMIDLIST pidlCurrent, pidlRoot;
695 TVITEMEXW item;
696 BOOL bResult = FALSE;
698 /* If 'selection' is a string, convert to a Shell ID List. */
699 if (is_str) {
700 IShellFolder *psfDesktop;
701 HRESULT hr;
703 hr = SHGetDesktopFolder(&psfDesktop);
704 if (FAILED(hr))
705 goto done;
707 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL,
708 selection, NULL, &pidlSelection, NULL);
709 IShellFolder_Release(psfDesktop);
710 if (FAILED(hr))
711 goto done;
714 /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
715 * the sub-tree currently displayed. */
716 pidlRoot = info->lpBrowseInfo->pidlRoot;
717 pidlCurrent = pidlSelection;
718 while (!_ILIsEmpty(pidlRoot) && _ILIsEqualSimple(pidlRoot, pidlCurrent)) {
719 pidlRoot = ILGetNext(pidlRoot);
720 pidlCurrent = ILGetNext(pidlCurrent);
723 /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
724 if (!_ILIsEmpty(pidlRoot))
725 goto done;
727 /* Initialize item to point to the first child of the root folder. */
728 memset(&item, 0, sizeof(item));
729 item.mask = TVIF_PARAM;
730 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
732 if (item.hItem)
733 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
734 (LPARAM)item.hItem);
736 /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
737 while (item.hItem && !_ILIsEmpty(pidlCurrent)) {
738 LPTV_ITEMDATA pItemData;
740 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
741 pItemData = (LPTV_ITEMDATA)item.lParam;
743 if (_ILIsEqualSimple(pItemData->lpi, pidlCurrent)) {
744 pidlCurrent = ILGetNext(pidlCurrent);
745 if (!_ILIsEmpty(pidlCurrent)) {
746 /* Only expand current node and move on to it's first child,
747 * if we didn't already reach the last SHITEMID */
748 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item.hItem);
749 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
750 (LPARAM)item.hItem);
752 } else {
753 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT,
754 (LPARAM)item.hItem);
758 if (_ILIsEmpty(pidlCurrent) && item.hItem)
759 bResult = TRUE;
761 done:
762 if (pidlSelection && pidlSelection != selection)
763 ILFree(pidlSelection);
765 if (pItem)
766 *pItem = item.hItem;
768 return bResult;
771 static BOOL BrsFolder_OnSetSelectionW(browse_info *info, LPVOID selection, BOOL is_str) {
772 HTREEITEM hItem;
773 BOOL bResult;
775 bResult = BrsFolder_OnSetExpanded(info, selection, is_str, &hItem);
776 if (bResult)
777 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem );
778 return bResult;
781 static BOOL BrsFolder_OnSetSelectionA(browse_info *info, LPVOID selection, BOOL is_str) {
782 LPWSTR selectionW = NULL;
783 BOOL result = FALSE;
784 int length;
786 if (!is_str)
787 return BrsFolder_OnSetSelectionW(info, selection, is_str);
789 if ((length = MultiByteToWideChar(CP_ACP, 0, selection, -1, NULL, 0)) &&
790 (selectionW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR))) &&
791 MultiByteToWideChar(CP_ACP, 0, selection, -1, selectionW, length))
793 result = BrsFolder_OnSetSelectionW(info, selectionW, is_str);
796 HeapFree(GetProcessHeap(), 0, selectionW);
797 return result;
800 static BOOL BrsFolder_OnWindowPosChanging(browse_info *info, WINDOWPOS *pos)
802 if ((info->lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE) && !(pos->flags & SWP_NOSIZE))
804 if (pos->cx < info->szMin.cx)
805 pos->cx = info->szMin.cx;
806 if (pos->cy < info->szMin.cy)
807 pos->cy = info->szMin.cy;
809 return 0;
812 static INT BrsFolder_OnDestroy(browse_info *info)
814 if (info->layout)
816 SHFree(info->layout);
817 info->layout = NULL;
820 return 0;
823 /*************************************************************************
824 * BrsFolderDlgProc32 (not an exported API function)
826 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
827 LPARAM lParam )
829 browse_info *info;
831 TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd, msg, wParam, lParam );
833 if (msg == WM_INITDIALOG)
834 return BrsFolder_OnCreate( hWnd, (browse_info*) lParam );
836 info = GetPropW( hWnd, szBrowseFolderInfo );
838 switch (msg)
840 case WM_NOTIFY:
841 return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
843 case WM_COMMAND:
844 return BrsFolder_OnCommand( info, wParam );
846 case WM_WINDOWPOSCHANGING:
847 return BrsFolder_OnWindowPosChanging( info, (WINDOWPOS *)lParam);
849 case WM_SIZE:
850 if (info->layout) /* new style dialogs */
851 LayoutUpdate(hWnd, info->layout, g_layout_info, LAYOUT_INFO_COUNT);
852 return 0;
854 case BFFM_SETSTATUSTEXTA:
855 TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
856 SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
857 break;
859 case BFFM_SETSTATUSTEXTW:
860 TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
861 SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
862 break;
864 case BFFM_ENABLEOK:
865 TRACE("Enable %ld\n", lParam);
866 EnableWindow(GetDlgItem(hWnd, 1), (lParam)?TRUE:FALSE);
867 break;
869 case BFFM_SETOKTEXT: /* unicode only */
870 TRACE("Set OK text %s\n", debugstr_w((LPWSTR)wParam));
871 SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)wParam);
872 break;
874 case BFFM_SETSELECTIONA:
875 return BrsFolder_OnSetSelectionA(info, (LPVOID)lParam, (BOOL)wParam);
877 case BFFM_SETSELECTIONW:
878 return BrsFolder_OnSetSelectionW(info, (LPVOID)lParam, (BOOL)wParam);
880 case BFFM_SETEXPANDED: /* unicode only */
881 return BrsFolder_OnSetExpanded(info, (LPVOID)lParam, (BOOL)wParam, NULL);
883 case WM_DESTROY:
884 return BrsFolder_OnDestroy(info);
886 return FALSE;
889 static const WCHAR swBrowseTemplateName[] = {
890 'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
891 static const WCHAR swNewBrowseTemplateName[] = {
892 'S','H','N','E','W','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
894 /*************************************************************************
895 * SHBrowseForFolderA [SHELL32.@]
896 * SHBrowseForFolder [SHELL32.@]
898 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
900 BROWSEINFOW bi;
901 LPITEMIDLIST lpid;
902 INT len;
903 LPWSTR title;
905 TRACE("%p\n", lpbi);
907 bi.hwndOwner = lpbi->hwndOwner;
908 bi.pidlRoot = lpbi->pidlRoot;
909 if (lpbi->pszDisplayName)
911 bi.pszDisplayName = HeapAlloc( GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR) );
912 MultiByteToWideChar( CP_ACP, 0, lpbi->pszDisplayName, -1, bi.pszDisplayName, MAX_PATH );
914 else
915 bi.pszDisplayName = NULL;
917 if (lpbi->lpszTitle)
919 len = MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0 );
920 title = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
921 MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, title, len );
923 else
924 title = NULL;
926 bi.lpszTitle = title;
927 bi.ulFlags = lpbi->ulFlags;
928 bi.lpfn = lpbi->lpfn;
929 bi.lParam = lpbi->lParam;
930 bi.iImage = lpbi->iImage;
931 lpid = SHBrowseForFolderW( &bi );
932 if (bi.pszDisplayName)
934 WideCharToMultiByte( CP_ACP, 0, bi.pszDisplayName, -1,
935 lpbi->pszDisplayName, MAX_PATH, 0, NULL);
936 HeapFree( GetProcessHeap(), 0, bi.pszDisplayName );
938 HeapFree(GetProcessHeap(), 0, title);
939 lpbi->iImage = bi.iImage;
940 return lpid;
944 /*************************************************************************
945 * SHBrowseForFolderW [SHELL32.@]
947 * NOTES
948 * crashes when passed a null pointer
950 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
952 browse_info info;
953 DWORD r;
954 HRESULT hr;
955 const WCHAR * templateName;
957 info.hWnd = 0;
958 info.pidlRet = NULL;
959 info.lpBrowseInfo = lpbi;
960 info.hwndTreeView = NULL;
962 hr = OleInitialize(NULL);
964 if (lpbi->ulFlags & BIF_NEWDIALOGSTYLE)
965 templateName = swNewBrowseTemplateName;
966 else
967 templateName = swBrowseTemplateName;
968 r = DialogBoxParamW( shell32_hInstance, templateName, lpbi->hwndOwner,
969 BrsFolderDlgProc, (LPARAM)&info );
970 if (SUCCEEDED(hr))
971 OleUninitialize();
972 if (!r)
973 return NULL;
975 return info.pidlRet;