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
21 * - implement new dialog style "make new folder" button
23 * - implement new dialog style resizing
29 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
31 /* original margins and control size */
32 typedef struct tagLAYOUT_DATA
34 LONG left
, width
, right
;
35 LONG top
, height
, bottom
;
38 typedef struct tagbrowse_info
42 LPBROWSEINFOW lpBrowseInfo
;
44 LAYOUT_DATA
*layout
; /* filled by LayoutInit, used by LayoutUpdate */
48 typedef struct tagTV_ITEMDATA
50 LPSHELLFOLDER lpsfParent
; /* IShellFolder of the parent */
51 LPITEMIDLIST lpi
; /* PIDL relative to parent */
52 LPITEMIDLIST lpifq
; /* Fully qualified PIDL */
53 IEnumIDList
* pEnumIL
; /* Children iterator */
54 } TV_ITEMDATA
, *LPTV_ITEMDATA
;
56 typedef struct tagLAYOUT_INFO
58 int iItemId
; /* control id */
59 DWORD dwAnchor
; /* BF_* flags specifying which margins should remain constant */
62 static const LAYOUT_INFO g_layout_info
[] =
64 {IDC_BROWSE_FOR_FOLDER_TITLE
, BF_TOP
|BF_LEFT
|BF_RIGHT
},
65 {IDC_BROWSE_FOR_FOLDER_STATUS
, BF_TOP
|BF_LEFT
|BF_RIGHT
},
66 {IDC_BROWSE_FOR_FOLDER_FOLDER
, BF_TOP
|BF_LEFT
|BF_RIGHT
},
67 {IDC_BROWSE_FOR_FOLDER_TREEVIEW
, BF_TOP
|BF_BOTTOM
|BF_LEFT
|BF_RIGHT
},
68 {IDC_BROWSE_FOR_FOLDER_FOLDER
, BF_BOTTOM
|BF_LEFT
},
69 {IDC_BROWSE_FOR_FOLDER_FOLDER_TEXT
, BF_BOTTOM
|BF_LEFT
|BF_RIGHT
},
70 {IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
, BF_BOTTOM
|BF_LEFT
},
71 {IDOK
, BF_BOTTOM
|BF_RIGHT
},
72 {IDCANCEL
, BF_BOTTOM
|BF_RIGHT
}
75 #define LAYOUT_INFO_COUNT (sizeof(g_layout_info)/sizeof(g_layout_info[0]))
77 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
78 BIF_BROWSEFORCOMPUTER | \
79 BIF_RETURNFSANCESTORS | \
80 BIF_RETURNONLYFSDIRS | \
81 BIF_NONEWFOLDERBUTTON | \
82 BIF_NEWDIALOGSTYLE | \
83 BIF_BROWSEINCLUDEFILES)
85 static void FillTreeView(browse_info
*, LPSHELLFOLDER
,
86 LPITEMIDLIST
, HTREEITEM
, IEnumIDList
*);
87 static HTREEITEM
InsertTreeViewItem( browse_info
*, IShellFolder
*,
88 LPCITEMIDLIST
, LPCITEMIDLIST
, IEnumIDList
*, HTREEITEM
);
90 static const WCHAR szBrowseFolderInfo
[] = L
"__WINE_BRSFOLDERDLG_INFO";
92 static DWORD __inline
BrowseFlagsToSHCONTF(UINT ulFlags
)
94 return SHCONTF_FOLDERS
| (ulFlags
& BIF_BROWSEINCLUDEFILES
? SHCONTF_NONFOLDERS
: 0);
97 static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo
, HWND hWnd
,
98 UINT msg
, LPARAM param
)
100 if (!lpBrowseInfo
->lpfn
)
102 lpBrowseInfo
->lpfn( hWnd
, msg
, param
, lpBrowseInfo
->lParam
);
105 static LAYOUT_DATA
*LayoutInit(HWND hwnd
, const LAYOUT_INFO
*layout_info
, int layout_count
)
111 GetClientRect(hwnd
, &rcWnd
);
112 data
= (LAYOUT_DATA
*)SHAlloc(sizeof(LAYOUT_DATA
)*layout_count
);
113 for (i
= 0; i
< layout_count
; i
++)
116 HWND hItem
= GetDlgItem(hwnd
, layout_info
[i
].iItemId
);
119 ERR("Item %d not found\n", i
);
120 GetWindowRect(hItem
, &r
);
121 MapWindowPoints(HWND_DESKTOP
, hwnd
, (LPPOINT
)&r
, 2);
123 data
[i
].left
= r
.left
;
124 data
[i
].right
= rcWnd
.right
- r
.right
;
125 data
[i
].width
= r
.right
- r
.left
;
128 data
[i
].bottom
= rcWnd
.bottom
- r
.bottom
;
129 data
[i
].height
= r
.bottom
- r
.top
;
134 static void LayoutUpdate(HWND hwnd
, LAYOUT_DATA
*data
, const LAYOUT_INFO
*layout_info
, int layout_count
)
139 GetClientRect(hwnd
, &rcWnd
);
140 for (i
= 0; i
< layout_count
; i
++)
143 HWND hItem
= GetDlgItem(hwnd
, layout_info
[i
].iItemId
);
145 GetWindowRect(hItem
, &r
);
146 MapWindowPoints(HWND_DESKTOP
, hwnd
, (LPPOINT
)&r
, 2);
148 if (layout_info
[i
].dwAnchor
& BF_RIGHT
)
150 r
.right
= rcWnd
.right
- data
[i
].right
;
151 if (!(layout_info
[i
].dwAnchor
& BF_LEFT
))
152 r
.left
= r
.right
- data
[i
].width
;
155 if (layout_info
[i
].dwAnchor
& BF_BOTTOM
)
157 r
.bottom
= rcWnd
.bottom
- data
[i
].bottom
;
158 if (!(layout_info
[i
].dwAnchor
& BF_TOP
))
159 r
.top
= r
.bottom
- data
[i
].height
;
162 SetWindowPos(hItem
, NULL
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_NOZORDER
);
167 /******************************************************************************
168 * InitializeTreeView [Internal]
170 * Called from WM_INITDIALOG handler.
173 * hwndParent [I] The BrowseForFolder dialog
174 * root [I] ITEMIDLIST of the root shell folder
176 static void InitializeTreeView( browse_info
*info
)
178 LPITEMIDLIST pidlParent
, pidlChild
;
179 HIMAGELIST hImageList
;
181 IShellFolder
*lpsfParent
, *lpsfRoot
;
182 IEnumIDList
* pEnumChildren
= NULL
;
185 LPCITEMIDLIST root
= info
->lpBrowseInfo
->pidlRoot
;
187 TRACE("%p\n", info
);
189 Shell_GetImageLists(NULL
, &hImageList
);
192 SendMessageW( info
->hwndTreeView
, TVM_SETIMAGELIST
, 0, (LPARAM
)hImageList
);
194 /* We want to call InsertTreeViewItem down the code, in order to insert
195 * the root item of the treeview. Due to InsertTreeViewItem's signature,
196 * we need the following to do this:
198 * + An ITEMIDLIST corresponding to _the parent_ of root.
199 * + An ITEMIDLIST, which is a relative path from root's parent to root
200 * (containing a single SHITEMID).
201 * + An IShellFolder interface pointer of root's parent folder.
203 * If root is 'Desktop', then root's parent is also 'Desktop'.
206 pidlParent
= ILClone(root
);
207 ILRemoveLastID(pidlParent
);
208 pidlChild
= ILClone(ILFindLastID(root
));
210 if (_ILIsDesktop(pidlParent
)) {
211 hr
= SHGetDesktopFolder(&lpsfParent
);
213 IShellFolder
*lpsfDesktop
;
214 hr
= SHGetDesktopFolder(&lpsfDesktop
);
216 WARN("SHGetDesktopFolder failed! hr = %08x\n", hr
);
221 hr
= lpsfDesktop
->BindToObject(pidlParent
, 0, IID_IShellFolder
, (LPVOID
*)&lpsfParent
);
222 lpsfDesktop
->Release();
226 WARN("Could not bind to parent shell folder! hr = %08x\n", hr
);
232 if (pidlChild
&& pidlChild
->mkid
.cb
) {
233 hr
= lpsfParent
->BindToObject(pidlChild
, 0, IID_IShellFolder
, (LPVOID
*)&lpsfRoot
);
235 lpsfRoot
= lpsfParent
;
236 hr
= lpsfParent
->AddRef();
240 WARN("Could not bind to root shell folder! hr = %08x\n", hr
);
241 lpsfParent
->Release();
247 flags
= BrowseFlagsToSHCONTF( info
->lpBrowseInfo
->ulFlags
);
248 hr
= lpsfRoot
->EnumObjects(info
->hWnd
, flags
, &pEnumChildren
);
250 WARN("Could not get child iterator! hr = %08x\n", hr
);
251 lpsfParent
->Release();
258 SendMessageW( info
->hwndTreeView
, TVM_DELETEITEM
, 0, (LPARAM
)TVI_ROOT
);
259 item
= InsertTreeViewItem( info
, lpsfParent
, pidlChild
,
260 pidlParent
, pEnumChildren
, TVI_ROOT
);
261 SendMessageW( info
->hwndTreeView
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)item
);
266 lpsfParent
->Release();
269 static int GetIcon(LPCITEMIDLIST lpi
, UINT uFlags
)
272 SHGetFileInfoW((LPCWSTR
)lpi
, 0 ,&sfi
, sizeof(SHFILEINFOW
), uFlags
);
276 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq
, LPTVITEMW lpTV_ITEM
)
278 LPITEMIDLIST pidlDesktop
= NULL
;
281 TRACE("%p %p\n",lpifq
, lpTV_ITEM
);
285 pidlDesktop
= _ILCreateDesktop();
289 flags
= SHGFI_PIDL
| SHGFI_SYSICONINDEX
| SHGFI_SMALLICON
;
290 lpTV_ITEM
->iImage
= GetIcon( lpifq
, flags
);
292 flags
= SHGFI_PIDL
| SHGFI_SYSICONINDEX
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
293 lpTV_ITEM
->iSelectedImage
= GetIcon( lpifq
, flags
);
296 ILFree( pidlDesktop
);
299 /******************************************************************************
302 * Query a shell folder for the display name of one of it's children
305 * lpsf [I] IShellFolder interface of the folder to be queried.
306 * lpi [I] ITEMIDLIST of the child, relative to parent
307 * dwFlags [I] as in IShellFolder::GetDisplayNameOf
308 * lpFriendlyName [O] The desired display name in unicode
314 static BOOL
GetName(LPSHELLFOLDER lpsf
, LPCITEMIDLIST lpi
, DWORD dwFlags
, LPWSTR lpFriendlyName
)
319 TRACE("%p %p %x %p\n", lpsf
, lpi
, dwFlags
, lpFriendlyName
);
320 if (SUCCEEDED(lpsf
->GetDisplayNameOf(lpi
, dwFlags
, &str
)))
321 bSuccess
= StrRetToStrNW(lpFriendlyName
, MAX_PATH
, &str
, lpi
);
325 TRACE("-- %s\n", debugstr_w(lpFriendlyName
));
329 /******************************************************************************
330 * InsertTreeViewItem [Internal]
333 * info [I] data for the dialog
334 * lpsf [I] IShellFolder interface of the item's parent shell folder
335 * pidl [I] ITEMIDLIST of the child to insert, relative to parent
336 * pidlParent [I] ITEMIDLIST of the parent shell folder
337 * pEnumIL [I] Iterator for the children of the item to be inserted
338 * hParent [I] The treeview-item that represents the parent shell folder
341 * Success: Handle to the created and inserted treeview-item
344 static HTREEITEM
InsertTreeViewItem( browse_info
*info
, IShellFolder
* lpsf
,
345 LPCITEMIDLIST pidl
, LPCITEMIDLIST pidlParent
, IEnumIDList
* pEnumIL
,
349 TVINSERTSTRUCTW tvins
;
350 WCHAR szBuff
[MAX_PATH
];
351 LPTV_ITEMDATA lptvid
=0;
353 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_PARAM
;
355 tvi
.cChildren
= pEnumIL
? 1 : 0;
356 tvi
.mask
|= TVIF_CHILDREN
;
358 lptvid
= (TV_ITEMDATA
*)SHAlloc( sizeof(TV_ITEMDATA
) );
362 if (!GetName(lpsf
, pidl
, SHGDN_NORMAL
, szBuff
))
365 tvi
.pszText
= szBuff
;
366 tvi
.cchTextMax
= MAX_PATH
;
367 tvi
.lParam
= (LPARAM
)lptvid
;
370 lptvid
->lpsfParent
= lpsf
;
371 lptvid
->lpi
= ILClone(pidl
);
372 lptvid
->lpifq
= pidlParent
? ILCombine(pidlParent
, pidl
) : ILClone(pidl
);
373 lptvid
->pEnumIL
= pEnumIL
;
374 GetNormalAndSelectedIcons(lptvid
->lpifq
, &tvi
);
377 tvins
.hInsertAfter
= NULL
;
378 tvins
.hParent
= hParent
;
380 return TreeView_InsertItem( info
->hwndTreeView
, &tvins
);
383 /******************************************************************************
384 * FillTreeView [Internal]
386 * For each child (given by lpe) of the parent shell folder, which is given by
387 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
390 * info [I] data for the dialog
391 * lpsf [I] IShellFolder interface of the parent shell folder
392 * pidl [I] ITEMIDLIST of the parent shell folder
393 * hParent [I] The treeview item that represents the parent shell folder
394 * lpe [I] An iterator for the children of the parent shell folder
396 static void FillTreeView( browse_info
*info
, IShellFolder
* lpsf
,
397 LPITEMIDLIST pidl
, HTREEITEM hParent
, IEnumIDList
* lpe
)
399 LPITEMIDLIST pidlTemp
= 0;
402 HWND hwnd
= GetParent( info
->hwndTreeView
);
404 TRACE("%p %p %p %p\n",lpsf
, pidl
, hParent
, lpe
);
406 /* No IEnumIDList -> No children */
410 SetCursor( LoadCursorA( 0, (LPSTR
)IDC_WAIT
) );
412 while (S_OK
== lpe
->Next(1,&pidlTemp
,&ulFetched
))
414 ULONG ulAttrs
= SFGAO_HASSUBFOLDER
| SFGAO_FOLDER
;
415 IEnumIDList
* pEnumIL
= NULL
;
416 IShellFolder
* pSFChild
= NULL
;
417 lpsf
->GetAttributesOf(1, (LPCITEMIDLIST
*)&pidlTemp
, &ulAttrs
);
418 if (ulAttrs
& SFGAO_FOLDER
)
420 hr
= lpsf
->BindToObject(pidlTemp
, NULL
, IID_IShellFolder
, (LPVOID
*)&pSFChild
);
423 DWORD flags
= BrowseFlagsToSHCONTF(info
->lpBrowseInfo
->ulFlags
);
424 hr
= pSFChild
->EnumObjects(hwnd
, flags
, &pEnumIL
);
427 if ((pEnumIL
->Skip(1) != S_OK
) ||
428 FAILED(pEnumIL
->Reset()))
438 if (!InsertTreeViewItem(info
, lpsf
, pidlTemp
, pidl
, pEnumIL
, hParent
))
440 SHFree(pidlTemp
); /* Finally, free the pidl that the shell gave us... */
446 SetCursor(LoadCursorW(0, (LPWSTR
)IDC_ARROW
));
450 static BOOL __inline
PIDLIsType(LPCITEMIDLIST pidl
, PIDLTYPE type
)
452 LPPIDLDATA data
= _ILGetDataPointer(pidl
);
455 return (data
->type
== type
);
458 static void BrsFolder_CheckValidSelection( browse_info
*info
, LPTV_ITEMDATA lptvid
)
460 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
461 LPCITEMIDLIST pidl
= lptvid
->lpi
;
462 BOOL bEnabled
= TRUE
;
466 if ((lpBrowseInfo
->ulFlags
& BIF_BROWSEFORCOMPUTER
) &&
467 !PIDLIsType(pidl
, PT_COMP
))
469 if (lpBrowseInfo
->ulFlags
& BIF_RETURNFSANCESTORS
)
471 dwAttributes
= SFGAO_FILESYSANCESTOR
| SFGAO_FILESYSTEM
;
472 r
= lptvid
->lpsfParent
->GetAttributesOf(1,
473 (LPCITEMIDLIST
*)&lptvid
->lpi
, &dwAttributes
);
474 if (FAILED(r
) || !(dwAttributes
& (SFGAO_FILESYSANCESTOR
|SFGAO_FILESYSTEM
)))
478 dwAttributes
= SFGAO_FOLDER
| SFGAO_FILESYSTEM
;
479 r
= lptvid
->lpsfParent
->GetAttributesOf(1,
480 (LPCITEMIDLIST
*)&lptvid
->lpi
,
483 ((dwAttributes
& (SFGAO_FOLDER
|SFGAO_FILESYSTEM
)) != (SFGAO_FOLDER
|SFGAO_FILESYSTEM
)))
485 if (lpBrowseInfo
->ulFlags
& BIF_RETURNONLYFSDIRS
)
487 EnableWindow(GetDlgItem(info
->hWnd
, IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
), FALSE
);
490 EnableWindow(GetDlgItem(info
->hWnd
, IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
), TRUE
);
492 SendMessageW(info
->hWnd
, BFFM_ENABLEOK
, 0, bEnabled
);
495 static LRESULT
BrsFolder_Treeview_Delete( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
497 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
)pnmtv
->itemOld
.lParam
;
499 TRACE("TVN_DELETEITEMA/W %p\n", lptvid
);
501 lptvid
->lpsfParent
->Release();
503 lptvid
->pEnumIL
->Release();
505 SHFree(lptvid
->lpifq
);
510 static LRESULT
BrsFolder_Treeview_Expand( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
512 IShellFolder
*lpsf2
= NULL
;
513 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
) pnmtv
->itemNew
.lParam
;
516 TRACE("TVN_ITEMEXPANDINGA/W\n");
518 if ((pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
))
521 if (lptvid
->lpi
&& lptvid
->lpi
->mkid
.cb
) {
522 r
= lptvid
->lpsfParent
->BindToObject(lptvid
->lpi
, 0,
523 IID_IShellFolder
, (LPVOID
*)&lpsf2
);
525 lpsf2
= lptvid
->lpsfParent
;
531 FillTreeView( info
, lpsf2
, lptvid
->lpifq
, pnmtv
->itemNew
.hItem
, lptvid
->pEnumIL
);
535 /* My Computer is already sorted and trying to do a simple text
536 * sort will only mess things up */
537 if (!_ILIsMyComputer(lptvid
->lpi
))
538 SendMessageW( info
->hwndTreeView
, TVM_SORTCHILDREN
,
539 FALSE
, (LPARAM
)pnmtv
->itemNew
.hItem
);
544 static HRESULT
BrsFolder_Treeview_Changed( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
546 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
) pnmtv
->itemNew
.lParam
;
547 WCHAR name
[MAX_PATH
];
549 ILFree(info
->pidlRet
);
550 info
->pidlRet
= ILClone(lptvid
->lpifq
);
552 if (GetName(lptvid
->lpsfParent
, lptvid
->lpi
, SHGDN_NORMAL
, name
))
553 SetWindowTextW( GetDlgItem(info
->hWnd
, IDC_BROWSE_FOR_FOLDER_FOLDER_TEXT
), name
);
555 browsefolder_callback( info
->lpBrowseInfo
, info
->hWnd
, BFFM_SELCHANGED
,
556 (LPARAM
)info
->pidlRet
);
557 BrsFolder_CheckValidSelection( info
, lptvid
);
561 static LRESULT
BrsFolder_Treeview_Rename(browse_info
*info
, NMTVDISPINFOW
*pnmtv
)
563 LPTV_ITEMDATA item_data
;
564 WCHAR old_path
[MAX_PATH
], new_path
[MAX_PATH
], *p
;
568 if(!pnmtv
->item
.pszText
)
571 item
.mask
= TVIF_HANDLE
|TVIF_PARAM
;
572 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
573 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
574 item_data
= (LPTV_ITEMDATA
)item
.lParam
;
576 SHGetPathFromIDListW(item_data
->lpifq
, old_path
);
577 if(!(p
= strrchrW(old_path
, '\\')))
579 p
= new_path
+(p
-old_path
+1);
580 memcpy(new_path
, old_path
, (p
-new_path
)*sizeof(WCHAR
));
581 strcpyW(p
, pnmtv
->item
.pszText
);
583 if(!MoveFileW(old_path
, new_path
))
586 SHFree(item_data
->lpifq
);
587 SHFree(item_data
->lpi
);
588 item_data
->lpifq
= SHSimpleIDListFromPathW(new_path
);
589 item_data
->lpsfParent
->ParseDisplayName(NULL
, NULL
, pnmtv
->item
.pszText
,
590 NULL
, &item_data
->lpi
, NULL
);
592 item
.mask
= TVIF_HANDLE
|TVIF_TEXT
;
593 item
.pszText
= pnmtv
->item
.pszText
;
594 SendMessageW(info
->hwndTreeView
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
596 nmtv
.itemNew
.lParam
= item
.lParam
;
597 BrsFolder_Treeview_Changed(info
, &nmtv
);
601 static LRESULT
BrsFolder_OnNotify( browse_info
*info
, UINT CtlID
, LPNMHDR lpnmh
)
603 NMTREEVIEWW
*pnmtv
= (NMTREEVIEWW
*)lpnmh
;
605 TRACE("%p %x %p msg=%x\n", info
, CtlID
, lpnmh
, pnmtv
->hdr
.code
);
607 if (pnmtv
->hdr
.idFrom
!= IDC_BROWSE_FOR_FOLDER_TREEVIEW
)
610 switch (pnmtv
->hdr
.code
)
612 case TVN_DELETEITEMA
:
613 case TVN_DELETEITEMW
:
614 return BrsFolder_Treeview_Delete( info
, pnmtv
);
616 case TVN_ITEMEXPANDINGA
:
617 case TVN_ITEMEXPANDINGW
:
618 return BrsFolder_Treeview_Expand( info
, pnmtv
);
620 case TVN_SELCHANGEDA
:
621 case TVN_SELCHANGEDW
:
622 return BrsFolder_Treeview_Changed( info
, pnmtv
);
624 case TVN_ENDLABELEDITA
:
625 case TVN_ENDLABELEDITW
:
626 return BrsFolder_Treeview_Rename( info
, (LPNMTVDISPINFOW
)pnmtv
);
629 WARN("unhandled (%d)\n", pnmtv
->hdr
.code
);
637 static BOOL
BrsFolder_OnCreate( HWND hWnd
, browse_info
*info
)
639 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
642 SetPropW( hWnd
, szBrowseFolderInfo
, info
);
644 if (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
645 FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
646 if (lpBrowseInfo
->ulFlags
& ~SUPPORTEDFLAGS
)
647 FIXME("flags %x not implemented\n", lpBrowseInfo
->ulFlags
& ~SUPPORTEDFLAGS
);
649 if (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
653 info
->layout
= LayoutInit(hWnd
, g_layout_info
, LAYOUT_INFO_COUNT
);
655 /* TODO: Windows allows shrinking the windows a bit */
656 GetWindowRect(hWnd
, &rcWnd
);
657 info
->szMin
.cx
= rcWnd
.right
- rcWnd
.left
;
658 info
->szMin
.cy
= rcWnd
.bottom
- rcWnd
.top
;
665 if (lpBrowseInfo
->lpszTitle
)
666 SetWindowTextW( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_TITLE
), lpBrowseInfo
->lpszTitle
);
668 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_TITLE
), SW_HIDE
);
670 if (!(lpBrowseInfo
->ulFlags
& BIF_STATUSTEXT
)
671 || (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
))
672 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_STATUS
), SW_HIDE
);
674 /* Hide "Make New Folder" Button? */
675 if ((lpBrowseInfo
->ulFlags
& BIF_NONEWFOLDERBUTTON
)
676 || !(lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
))
677 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
), SW_HIDE
);
679 /* Hide the editbox? */
680 if (!(lpBrowseInfo
->ulFlags
& BIF_EDITBOX
))
682 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_FOLDER
), SW_HIDE
);
683 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_FOLDER_TEXT
), SW_HIDE
);
686 info
->hwndTreeView
= GetDlgItem( hWnd
, IDC_BROWSE_FOR_FOLDER_TREEVIEW
);
687 if (info
->hwndTreeView
)
689 InitializeTreeView( info
);
691 /* Resize the treeview if there's not editbox */
692 if ((lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
693 && !(lpBrowseInfo
->ulFlags
& BIF_EDITBOX
))
696 GetClientRect(info
->hwndTreeView
, &rc
);
697 SetWindowPos(info
->hwndTreeView
, HWND_TOP
, 0, 0,
698 rc
.right
, rc
.bottom
+ 40, SWP_NOMOVE
);
702 ERR("treeview control missing!\n");
704 browsefolder_callback( info
->lpBrowseInfo
, hWnd
, BFFM_INITIALIZED
, 0 );
709 static HRESULT
BrsFolder_Rename(browse_info
*info
, HTREEITEM rename
)
711 SendMessageW(info
->hwndTreeView
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)rename
);
712 SendMessageW(info
->hwndTreeView
, TVM_EDITLABELW
, 0, (LPARAM
)rename
);
716 static HRESULT
BrsFolder_NewFolder(browse_info
*info
)
718 DWORD flags
= BrowseFlagsToSHCONTF(info
->lpBrowseInfo
->ulFlags
);
719 IShellFolder
*desktop
, *cur
;
721 WCHAR name
[MAX_PATH
];
722 HTREEITEM parent
, added
;
723 LPTV_ITEMDATA item_data
;
724 LPITEMIDLIST new_item
;
730 ERR("Make new folder button should be disabled\n");
734 /* Create new directory */
735 hr
= SHGetDesktopFolder(&desktop
);
738 hr
= desktop
->BindToObject(info
->pidlRet
, 0, IID_IShellFolder
, (LPVOID
*)&cur
);
743 hr
= cur
->QueryInterface(IID_ISFHelper
, (LPVOID
*)&sfhelper
);
747 hr
= SHGetPathFromIDListW(info
->pidlRet
, name
);
754 hr
= sfhelper
->GetUniqueName(&name
[len
], MAX_PATH
-len
);
760 if(!CreateDirectoryW(name
, NULL
))
763 /* Update parent of newly created directory */
764 parent
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
768 SendMessageW(info
->hwndTreeView
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)parent
);
770 memset(&item
, 0, sizeof(TVITEMW
));
771 item
.mask
= TVIF_PARAM
|TVIF_STATE
;
773 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
774 item_data
= (LPTV_ITEMDATA
)item
.lParam
;
778 if(item_data
->pEnumIL
)
779 item_data
->pEnumIL
->Release();
780 hr
= cur
->EnumObjects(info
->hwndTreeView
, flags
, &item_data
->pEnumIL
);
784 /* Update treeview */
785 if(!(item
.state
&TVIS_EXPANDEDONCE
)) {
786 item
.mask
= TVIF_STATE
;
787 item
.state
= TVIS_EXPANDEDONCE
;
788 item
.stateMask
= TVIS_EXPANDEDONCE
;
789 SendMessageW(info
->hwndTreeView
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
792 hr
= cur
->ParseDisplayName(NULL
, NULL
, name
+len
, NULL
, &new_item
, NULL
);
796 added
= InsertTreeViewItem(info
, cur
, new_item
, item_data
->lpifq
, NULL
, parent
);
800 SendMessageW(info
->hwndTreeView
, TVM_SORTCHILDREN
, FALSE
, (LPARAM
)parent
);
801 return BrsFolder_Rename(info
, added
);
807 static BOOL
BrsFolder_OnCommand( browse_info
*info
, UINT id
)
809 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
814 /* The original pidl is owned by the treeview and will be free'd. */
815 info
->pidlRet
= ILClone(info
->pidlRet
);
816 if (info
->pidlRet
== NULL
) /* A null pidl would mean a cancel */
817 info
->pidlRet
= _ILCreateDesktop();
818 pdump( info
->pidlRet
);
819 if (lpBrowseInfo
->pszDisplayName
)
820 SHGetPathFromIDListW( info
->pidlRet
, lpBrowseInfo
->pszDisplayName
);
821 EndDialog( info
->hWnd
, 1 );
825 EndDialog( info
->hWnd
, 0 );
828 case IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
:
829 BrsFolder_NewFolder(info
);
835 static BOOL
BrsFolder_OnSetExpanded(browse_info
*info
, LPVOID selection
,
836 BOOL is_str
, HTREEITEM
*pItem
)
838 LPITEMIDLIST pidlSelection
= (LPITEMIDLIST
)selection
;
839 LPCITEMIDLIST pidlCurrent
, pidlRoot
;
841 BOOL bResult
= FALSE
;
843 memset(&item
, 0, sizeof(item
));
845 /* If 'selection' is a string, convert to a Shell ID List. */
847 IShellFolder
*psfDesktop
;
850 hr
= SHGetDesktopFolder(&psfDesktop
);
854 hr
= psfDesktop
->ParseDisplayName(NULL
, NULL
, (LPOLESTR
)selection
,
855 NULL
, &pidlSelection
, NULL
);
856 psfDesktop
->Release();
861 /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
862 * the sub-tree currently displayed. */
863 pidlRoot
= info
->lpBrowseInfo
->pidlRoot
;
864 pidlCurrent
= pidlSelection
;
865 while (!_ILIsEmpty(pidlRoot
) && _ILIsEqualSimple(pidlRoot
, pidlCurrent
)) {
866 pidlRoot
= ILGetNext(pidlRoot
);
867 pidlCurrent
= ILGetNext(pidlCurrent
);
870 /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
871 if (!_ILIsEmpty(pidlRoot
))
874 /* Initialize item to point to the first child of the root folder. */
875 item
.mask
= TVIF_PARAM
;
876 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
879 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CHILD
,
882 /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
883 while (item
.hItem
&& !_ILIsEmpty(pidlCurrent
)) {
884 LPTV_ITEMDATA pItemData
;
886 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
887 pItemData
= (LPTV_ITEMDATA
)item
.lParam
;
889 if (_ILIsEqualSimple(pItemData
->lpi
, pidlCurrent
)) {
890 pidlCurrent
= ILGetNext(pidlCurrent
);
891 if (!_ILIsEmpty(pidlCurrent
)) {
892 /* Only expand current node and move on to it's first child,
893 * if we didn't already reach the last SHITEMID */
894 SendMessageW(info
->hwndTreeView
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)item
.hItem
);
895 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CHILD
,
899 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_NEXT
,
904 if (_ILIsEmpty(pidlCurrent
) && item
.hItem
)
908 if (pidlSelection
&& pidlSelection
!= (LPITEMIDLIST
)selection
)
909 ILFree(pidlSelection
);
917 static BOOL
BrsFolder_OnSetSelectionW(browse_info
*info
, LPVOID selection
, BOOL is_str
) {
921 if (!selection
) return FALSE
;
923 bResult
= BrsFolder_OnSetExpanded(info
, selection
, is_str
, &hItem
);
925 SendMessageW(info
->hwndTreeView
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hItem
);
929 static BOOL
BrsFolder_OnSetSelectionA(browse_info
*info
, LPVOID selection
, BOOL is_str
) {
930 LPWSTR selectionW
= NULL
;
935 return BrsFolder_OnSetSelectionW(info
, selection
, is_str
);
937 if ((length
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)selection
, -1, NULL
, 0)) &&
938 (selectionW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, length
* sizeof(WCHAR
))) &&
939 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)selection
, -1, selectionW
, length
))
941 result
= BrsFolder_OnSetSelectionW(info
, selectionW
, is_str
);
944 HeapFree(GetProcessHeap(), 0, selectionW
);
948 static BOOL
BrsFolder_OnWindowPosChanging(browse_info
*info
, WINDOWPOS
*pos
)
950 if ((info
->lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
) && !(pos
->flags
& SWP_NOSIZE
))
952 if (pos
->cx
< info
->szMin
.cx
)
953 pos
->cx
= info
->szMin
.cx
;
954 if (pos
->cy
< info
->szMin
.cy
)
955 pos
->cy
= info
->szMin
.cy
;
960 static INT
BrsFolder_OnDestroy(browse_info
*info
)
964 SHFree(info
->layout
);
971 /*************************************************************************
972 * BrsFolderDlgProc32 (not an exported API function)
974 static INT_PTR CALLBACK
BrsFolderDlgProc( HWND hWnd
, UINT msg
, WPARAM wParam
,
979 TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd
, msg
, wParam
, lParam
);
981 if (msg
== WM_INITDIALOG
)
982 return BrsFolder_OnCreate( hWnd
, (browse_info
*) lParam
);
984 info
= (browse_info
*) GetPropW( hWnd
, szBrowseFolderInfo
);
989 return BrsFolder_OnNotify( info
, (UINT
)wParam
, (LPNMHDR
)lParam
);
992 return BrsFolder_OnCommand( info
, wParam
);
994 case WM_WINDOWPOSCHANGING
:
995 return BrsFolder_OnWindowPosChanging( info
, (WINDOWPOS
*)lParam
);
998 if (info
->layout
) /* new style dialogs */
999 LayoutUpdate(hWnd
, info
->layout
, g_layout_info
, LAYOUT_INFO_COUNT
);
1002 case BFFM_SETSTATUSTEXTA
:
1003 TRACE("Set status %s\n", debugstr_a((LPSTR
)lParam
));
1004 SetWindowTextA(GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_STATUS
), (LPSTR
)lParam
);
1007 case BFFM_SETSTATUSTEXTW
:
1008 TRACE("Set status %s\n", debugstr_w((LPWSTR
)lParam
));
1009 SetWindowTextW(GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_STATUS
), (LPWSTR
)lParam
);
1013 TRACE("Enable %ld\n", lParam
);
1014 EnableWindow(GetDlgItem(hWnd
, 1), (lParam
)?TRUE
:FALSE
);
1017 case BFFM_SETOKTEXT
: /* unicode only */
1018 TRACE("Set OK text %s\n", debugstr_w((LPWSTR
)wParam
));
1019 SetWindowTextW(GetDlgItem(hWnd
, 1), (LPWSTR
)wParam
);
1022 case BFFM_SETSELECTIONA
:
1023 return BrsFolder_OnSetSelectionA(info
, (LPVOID
)lParam
, (BOOL
)wParam
);
1025 case BFFM_SETSELECTIONW
:
1026 return BrsFolder_OnSetSelectionW(info
, (LPVOID
)lParam
, (BOOL
)wParam
);
1028 case BFFM_SETEXPANDED
: /* unicode only */
1029 return BrsFolder_OnSetExpanded(info
, (LPVOID
)lParam
, (BOOL
)wParam
, NULL
);
1032 return BrsFolder_OnDestroy(info
);
1040 /*************************************************************************
1041 * SHBrowseForFolderA [SHELL32.@]
1042 * SHBrowseForFolder [SHELL32.@]
1044 LPITEMIDLIST WINAPI
SHBrowseForFolderA (LPBROWSEINFOA lpbi
)
1051 TRACE("%p\n", lpbi
);
1053 bi
.hwndOwner
= lpbi
->hwndOwner
;
1054 bi
.pidlRoot
= lpbi
->pidlRoot
;
1055 if (lpbi
->pszDisplayName
)
1057 bi
.pszDisplayName
= (WCHAR
*)HeapAlloc( GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
) );
1058 MultiByteToWideChar( CP_ACP
, 0, lpbi
->pszDisplayName
, -1, bi
.pszDisplayName
, MAX_PATH
);
1061 bi
.pszDisplayName
= NULL
;
1063 if (lpbi
->lpszTitle
)
1065 len
= MultiByteToWideChar( CP_ACP
, 0, lpbi
->lpszTitle
, -1, NULL
, 0 );
1066 title
= (WCHAR
*)HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1067 MultiByteToWideChar( CP_ACP
, 0, lpbi
->lpszTitle
, -1, title
, len
);
1072 bi
.lpszTitle
= title
;
1073 bi
.ulFlags
= lpbi
->ulFlags
;
1074 bi
.lpfn
= lpbi
->lpfn
;
1075 bi
.lParam
= lpbi
->lParam
;
1076 bi
.iImage
= lpbi
->iImage
;
1077 lpid
= SHBrowseForFolderW( &bi
);
1078 if (bi
.pszDisplayName
)
1080 WideCharToMultiByte( CP_ACP
, 0, bi
.pszDisplayName
, -1,
1081 lpbi
->pszDisplayName
, MAX_PATH
, 0, NULL
);
1082 HeapFree( GetProcessHeap(), 0, bi
.pszDisplayName
);
1084 HeapFree(GetProcessHeap(), 0, title
);
1085 lpbi
->iImage
= bi
.iImage
;
1090 /*************************************************************************
1091 * SHBrowseForFolderW [SHELL32.@]
1094 * crashes when passed a null pointer
1096 LPITEMIDLIST WINAPI
SHBrowseForFolderW (LPBROWSEINFOW lpbi
)
1104 info
.pidlRet
= NULL
;
1105 info
.lpBrowseInfo
= lpbi
;
1106 info
.hwndTreeView
= NULL
;
1108 hr
= OleInitialize(NULL
);
1110 if (lpbi
->ulFlags
& BIF_NEWDIALOGSTYLE
)
1111 wDlgId
= IDD_BROWSE_FOR_FOLDER_NEW
;
1113 wDlgId
= IDD_BROWSE_FOR_FOLDER
;
1114 r
= DialogBoxParamW( shell32_hInstance
, MAKEINTRESOURCEW(wDlgId
), lpbi
->hwndOwner
,
1115 BrsFolderDlgProc
, (LPARAM
)&info
);
1120 ILFree(info
.pidlRet
);
1124 return info
.pidlRet
;