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
30 #include "wine/debug.h"
31 #include "undocshell.h"
32 #include "commoncontrols.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
)
288 list
= (IImageList
*)SHGetFileInfoW((LPCWSTR
)lpi
, 0 ,&sfi
, sizeof(SHFILEINFOW
), uFlags
);
289 if (list
) IImageList_Release(list
);
293 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq
, LPTVITEMW lpTV_ITEM
)
295 LPITEMIDLIST pidlDesktop
= NULL
;
298 TRACE("%p %p\n",lpifq
, lpTV_ITEM
);
302 pidlDesktop
= _ILCreateDesktop();
306 flags
= SHGFI_PIDL
| SHGFI_SYSICONINDEX
| SHGFI_SMALLICON
;
307 lpTV_ITEM
->iImage
= GetIcon( lpifq
, flags
);
309 flags
= SHGFI_PIDL
| SHGFI_SYSICONINDEX
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
310 lpTV_ITEM
->iSelectedImage
= GetIcon( lpifq
, flags
);
313 ILFree( pidlDesktop
);
316 /******************************************************************************
319 * Query a shell folder for the display name of one of its children
322 * lpsf [I] IShellFolder interface of the folder to be queried.
323 * lpi [I] ITEMIDLIST of the child, relative to parent
324 * dwFlags [I] as in IShellFolder::GetDisplayNameOf
325 * lpFriendlyName [O] The desired display name in unicode
331 static BOOL
GetName(LPSHELLFOLDER lpsf
, LPCITEMIDLIST lpi
, DWORD dwFlags
, LPWSTR lpFriendlyName
)
336 TRACE("%p %p %x %p\n", lpsf
, lpi
, dwFlags
, lpFriendlyName
);
337 if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf
, lpi
, dwFlags
, &str
)))
338 bSuccess
= StrRetToStrNW(lpFriendlyName
, MAX_PATH
, &str
, lpi
);
342 TRACE("-- %s\n", debugstr_w(lpFriendlyName
));
346 /******************************************************************************
347 * InsertTreeViewItem [Internal]
350 * info [I] data for the dialog
351 * lpsf [I] IShellFolder interface of the item's parent shell folder
352 * pidl [I] ITEMIDLIST of the child to insert, relative to parent
353 * pidlParent [I] ITEMIDLIST of the parent shell folder
354 * pEnumIL [I] Iterator for the children of the item to be inserted
355 * hParent [I] The treeview-item that represents the parent shell folder
358 * Success: Handle to the created and inserted treeview-item
361 static HTREEITEM
InsertTreeViewItem( browse_info
*info
, IShellFolder
* lpsf
,
362 LPCITEMIDLIST pidl
, LPCITEMIDLIST pidlParent
, IEnumIDList
* pEnumIL
,
366 TVINSERTSTRUCTW tvins
;
367 WCHAR szBuff
[MAX_PATH
];
368 LPTV_ITEMDATA lptvid
=0;
370 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_PARAM
;
372 tvi
.cChildren
= pEnumIL
? 1 : 0;
373 tvi
.mask
|= TVIF_CHILDREN
;
375 if (!GetName(lpsf
, pidl
, SHGDN_NORMAL
, szBuff
))
378 lptvid
= SHAlloc( sizeof(TV_ITEMDATA
) );
382 tvi
.pszText
= szBuff
;
383 tvi
.cchTextMax
= MAX_PATH
;
384 tvi
.lParam
= (LPARAM
)lptvid
;
386 IShellFolder_AddRef(lpsf
);
387 lptvid
->lpsfParent
= lpsf
;
388 lptvid
->lpi
= ILClone(pidl
);
389 lptvid
->lpifq
= pidlParent
? ILCombine(pidlParent
, pidl
) : ILClone(pidl
);
390 lptvid
->pEnumIL
= pEnumIL
;
391 GetNormalAndSelectedIcons(lptvid
->lpifq
, &tvi
);
394 tvins
.hInsertAfter
= NULL
;
395 tvins
.hParent
= hParent
;
397 return TreeView_InsertItemW( info
->hwndTreeView
, &tvins
);
400 /******************************************************************************
401 * FillTreeView [Internal]
403 * For each child (given by lpe) of the parent shell folder, which is given by
404 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
407 * info [I] data for the dialog
408 * lpsf [I] IShellFolder interface of the parent shell folder
409 * pidl [I] ITEMIDLIST of the parent shell folder
410 * hParent [I] The treeview item that represents the parent shell folder
411 * lpe [I] An iterator for the children of the parent shell folder
413 static void FillTreeView( browse_info
*info
, IShellFolder
* lpsf
,
414 LPITEMIDLIST pidl
, HTREEITEM hParent
, IEnumIDList
* lpe
)
416 LPITEMIDLIST pidlTemp
= 0;
419 HWND hwnd
= GetParent( info
->hwndTreeView
);
421 TRACE("%p %p %p %p\n",lpsf
, pidl
, hParent
, lpe
);
423 /* No IEnumIDList -> No children */
427 SetCursor( LoadCursorA( 0, (LPSTR
)IDC_WAIT
) );
429 while (S_OK
== IEnumIDList_Next(lpe
,1,&pidlTemp
,&ulFetched
))
431 ULONG ulAttrs
= SFGAO_HASSUBFOLDER
| SFGAO_FOLDER
;
432 IEnumIDList
* pEnumIL
= NULL
;
433 IShellFolder
* pSFChild
= NULL
;
434 IShellFolder_GetAttributesOf(lpsf
, 1, (LPCITEMIDLIST
*)&pidlTemp
, &ulAttrs
);
435 if (ulAttrs
& SFGAO_FOLDER
)
437 hr
= IShellFolder_BindToObject(lpsf
,pidlTemp
,NULL
,&IID_IShellFolder
,(LPVOID
*)&pSFChild
);
440 DWORD flags
= BrowseFlagsToSHCONTF(info
->lpBrowseInfo
->ulFlags
);
441 hr
= IShellFolder_EnumObjects(pSFChild
, hwnd
, flags
, &pEnumIL
);
444 if ((IEnumIDList_Skip(pEnumIL
, 1) != S_OK
) ||
445 FAILED(IEnumIDList_Reset(pEnumIL
)))
447 IEnumIDList_Release(pEnumIL
);
451 IShellFolder_Release(pSFChild
);
455 if (!InsertTreeViewItem(info
, lpsf
, pidlTemp
, pidl
, pEnumIL
, hParent
))
457 SHFree(pidlTemp
); /* Finally, free the pidl that the shell gave us... */
463 SetCursor(LoadCursorW(0, (LPWSTR
)IDC_ARROW
));
467 static inline BOOL
PIDLIsType(LPCITEMIDLIST pidl
, PIDLTYPE type
)
469 LPPIDLDATA data
= _ILGetDataPointer(pidl
);
472 return (data
->type
== type
);
475 static void BrsFolder_CheckValidSelection( browse_info
*info
, LPTV_ITEMDATA lptvid
)
477 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
478 LPCITEMIDLIST pidl
= lptvid
->lpi
;
479 BOOL bEnabled
= TRUE
;
483 if ((lpBrowseInfo
->ulFlags
& BIF_BROWSEFORCOMPUTER
) &&
484 !PIDLIsType(pidl
, PT_COMP
))
486 if (lpBrowseInfo
->ulFlags
& BIF_RETURNFSANCESTORS
)
488 dwAttributes
= SFGAO_FILESYSANCESTOR
| SFGAO_FILESYSTEM
;
489 r
= IShellFolder_GetAttributesOf(lptvid
->lpsfParent
, 1,
490 (LPCITEMIDLIST
*)&lptvid
->lpi
, &dwAttributes
);
491 if (FAILED(r
) || !(dwAttributes
& (SFGAO_FILESYSANCESTOR
|SFGAO_FILESYSTEM
)))
495 dwAttributes
= SFGAO_FOLDER
| SFGAO_FILESYSTEM
;
496 r
= IShellFolder_GetAttributesOf(lptvid
->lpsfParent
, 1,
497 (LPCITEMIDLIST
*)&lptvid
->lpi
, &dwAttributes
);
499 ((dwAttributes
& (SFGAO_FOLDER
|SFGAO_FILESYSTEM
)) != (SFGAO_FOLDER
|SFGAO_FILESYSTEM
)))
501 if (lpBrowseInfo
->ulFlags
& BIF_RETURNONLYFSDIRS
)
503 EnableWindow(GetDlgItem(info
->hWnd
, IDD_MAKENEWFOLDER
), FALSE
);
506 EnableWindow(GetDlgItem(info
->hWnd
, IDD_MAKENEWFOLDER
), TRUE
);
508 SendMessageW(info
->hWnd
, BFFM_ENABLEOK
, 0, bEnabled
);
511 static LRESULT
BrsFolder_Treeview_Delete( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
513 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
)pnmtv
->itemOld
.lParam
;
515 TRACE("TVN_DELETEITEMA/W %p\n", lptvid
);
517 IShellFolder_Release(lptvid
->lpsfParent
);
519 IEnumIDList_Release(lptvid
->pEnumIL
);
521 SHFree(lptvid
->lpifq
);
526 static LRESULT
BrsFolder_Treeview_Expand( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
528 IShellFolder
*lpsf2
= NULL
;
529 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
) pnmtv
->itemNew
.lParam
;
532 TRACE("TVN_ITEMEXPANDINGA/W\n");
534 if ((pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
))
537 if (!_ILIsEmpty(lptvid
->lpi
)) {
538 r
= IShellFolder_BindToObject( lptvid
->lpsfParent
, lptvid
->lpi
, 0,
539 &IID_IShellFolder
, (void**)&lpsf2
);
541 lpsf2
= lptvid
->lpsfParent
;
542 IShellFolder_AddRef(lpsf2
);
548 FillTreeView( info
, lpsf2
, lptvid
->lpifq
, pnmtv
->itemNew
.hItem
, lptvid
->pEnumIL
);
549 IShellFolder_Release( lpsf2
);
552 /* My Computer is already sorted and trying to do a simple text
553 * sort will only mess things up */
554 if (!_ILIsMyComputer(lptvid
->lpi
))
555 SendMessageW( info
->hwndTreeView
, TVM_SORTCHILDREN
,
556 FALSE
, (LPARAM
)pnmtv
->itemNew
.hItem
);
561 static HRESULT
BrsFolder_Treeview_Changed( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
563 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
) pnmtv
->itemNew
.lParam
;
564 WCHAR name
[MAX_PATH
];
566 ILFree(info
->pidlRet
);
567 info
->pidlRet
= ILClone(lptvid
->lpifq
);
569 if (GetName(lptvid
->lpsfParent
, lptvid
->lpi
, SHGDN_NORMAL
, name
))
570 SetWindowTextW( GetDlgItem(info
->hWnd
, IDD_FOLDERTEXT
), name
);
572 browsefolder_callback( info
->lpBrowseInfo
, info
->hWnd
, BFFM_SELCHANGED
,
573 (LPARAM
)info
->pidlRet
);
574 BrsFolder_CheckValidSelection( info
, lptvid
);
578 static LRESULT
BrsFolder_Treeview_Rename(browse_info
*info
, NMTVDISPINFOW
*pnmtv
)
580 LPTV_ITEMDATA item_data
;
581 WCHAR old_path
[MAX_PATH
], new_path
[MAX_PATH
], *p
;
585 if(!pnmtv
->item
.pszText
)
588 item
.mask
= TVIF_HANDLE
|TVIF_PARAM
;
589 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
590 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
591 item_data
= (LPTV_ITEMDATA
)item
.lParam
;
593 SHGetPathFromIDListW(item_data
->lpifq
, old_path
);
594 if(!(p
= strrchrW(old_path
, '\\')))
596 p
= new_path
+(p
-old_path
+1);
597 memcpy(new_path
, old_path
, (p
-new_path
)*sizeof(WCHAR
));
598 strcpyW(p
, pnmtv
->item
.pszText
);
600 if(!MoveFileW(old_path
, new_path
))
603 SHFree(item_data
->lpifq
);
604 SHFree(item_data
->lpi
);
605 item_data
->lpifq
= SHSimpleIDListFromPathW(new_path
);
606 IShellFolder_ParseDisplayName(item_data
->lpsfParent
, NULL
, NULL
,
607 pnmtv
->item
.pszText
, NULL
, &item_data
->lpi
, NULL
);
609 item
.mask
= TVIF_HANDLE
|TVIF_TEXT
;
610 item
.pszText
= pnmtv
->item
.pszText
;
611 SendMessageW(info
->hwndTreeView
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
613 nmtv
.itemNew
.lParam
= item
.lParam
;
614 BrsFolder_Treeview_Changed(info
, &nmtv
);
618 static LRESULT
BrsFolder_OnNotify( browse_info
*info
, UINT CtlID
, LPNMHDR lpnmh
)
620 NMTREEVIEWW
*pnmtv
= (NMTREEVIEWW
*)lpnmh
;
622 TRACE("%p %x %p msg=%x\n", info
, CtlID
, lpnmh
, pnmtv
->hdr
.code
);
624 if (pnmtv
->hdr
.idFrom
!= IDD_TREEVIEW
)
627 switch (pnmtv
->hdr
.code
)
629 case TVN_DELETEITEMA
:
630 case TVN_DELETEITEMW
:
631 return BrsFolder_Treeview_Delete( info
, pnmtv
);
633 case TVN_ITEMEXPANDINGA
:
634 case TVN_ITEMEXPANDINGW
:
635 return BrsFolder_Treeview_Expand( info
, pnmtv
);
637 case TVN_SELCHANGEDA
:
638 case TVN_SELCHANGEDW
:
639 return BrsFolder_Treeview_Changed( info
, pnmtv
);
641 case TVN_ENDLABELEDITA
:
642 case TVN_ENDLABELEDITW
:
643 return BrsFolder_Treeview_Rename( info
, (LPNMTVDISPINFOW
)pnmtv
);
646 WARN("unhandled (%d)\n", pnmtv
->hdr
.code
);
654 static BOOL
BrsFolder_OnCreate( HWND hWnd
, browse_info
*info
)
656 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
659 SetPropW( hWnd
, szBrowseFolderInfo
, info
);
661 if (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
662 FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
663 if (lpBrowseInfo
->ulFlags
& ~SUPPORTEDFLAGS
)
664 FIXME("flags %x not implemented\n", lpBrowseInfo
->ulFlags
& ~SUPPORTEDFLAGS
);
666 if (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
670 info
->layout
= LayoutInit(hWnd
, g_layout_info
, LAYOUT_INFO_COUNT
);
672 /* TODO: Windows allows shrinking the windows a bit */
673 GetWindowRect(hWnd
, &rcWnd
);
674 info
->szMin
.cx
= rcWnd
.right
- rcWnd
.left
;
675 info
->szMin
.cy
= rcWnd
.bottom
- rcWnd
.top
;
682 if (lpBrowseInfo
->lpszTitle
)
683 SetWindowTextW( GetDlgItem(hWnd
, IDD_TITLE
), lpBrowseInfo
->lpszTitle
);
685 ShowWindow( GetDlgItem(hWnd
, IDD_TITLE
), SW_HIDE
);
687 if (!(lpBrowseInfo
->ulFlags
& BIF_STATUSTEXT
)
688 || (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
))
689 ShowWindow( GetDlgItem(hWnd
, IDD_STATUS
), SW_HIDE
);
691 /* Hide "Make New Folder" Button? */
692 if ((lpBrowseInfo
->ulFlags
& BIF_NONEWFOLDERBUTTON
)
693 || !(lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
))
694 ShowWindow( GetDlgItem(hWnd
, IDD_MAKENEWFOLDER
), SW_HIDE
);
696 /* Hide the editbox? */
697 if (!(lpBrowseInfo
->ulFlags
& BIF_EDITBOX
))
699 ShowWindow( GetDlgItem(hWnd
, IDD_FOLDER
), SW_HIDE
);
700 ShowWindow( GetDlgItem(hWnd
, IDD_FOLDERTEXT
), SW_HIDE
);
703 info
->hwndTreeView
= GetDlgItem( hWnd
, IDD_TREEVIEW
);
704 if (info
->hwndTreeView
)
706 InitializeTreeView( info
);
708 /* Resize the treeview if there's not editbox */
709 if ((lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
710 && !(lpBrowseInfo
->ulFlags
& BIF_EDITBOX
))
713 GetClientRect(info
->hwndTreeView
, &rc
);
714 SetWindowPos(info
->hwndTreeView
, HWND_TOP
, 0, 0,
715 rc
.right
, rc
.bottom
+ 40, SWP_NOMOVE
);
719 ERR("treeview control missing!\n");
721 browsefolder_callback( info
->lpBrowseInfo
, hWnd
, BFFM_INITIALIZED
, 0 );
726 static HRESULT
BrsFolder_Rename(browse_info
*info
, HTREEITEM rename
)
728 SendMessageW(info
->hwndTreeView
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)rename
);
729 SendMessageW(info
->hwndTreeView
, TVM_EDITLABELW
, 0, (LPARAM
)rename
);
733 static HRESULT
BrsFolder_NewFolder(browse_info
*info
)
735 DWORD flags
= BrowseFlagsToSHCONTF(info
->lpBrowseInfo
->ulFlags
);
736 IShellFolder
*desktop
, *cur
;
738 WCHAR name
[MAX_PATH
];
739 HTREEITEM parent
, added
;
740 LPTV_ITEMDATA item_data
;
741 LPITEMIDLIST new_item
;
747 ERR("Make new folder button should be disabled\n");
751 /* Create new directory */
752 hr
= SHGetDesktopFolder(&desktop
);
755 hr
= IShellFolder_BindToObject(desktop
, info
->pidlRet
, 0, &IID_IShellFolder
, (void**)&cur
);
756 IShellFolder_Release(desktop
);
760 hr
= IShellFolder_QueryInterface(cur
, &IID_ISFHelper
, (void**)&sfhelper
);
764 if(!SHGetPathFromIDListW(info
->pidlRet
, name
)) {
772 hr
= ISFHelper_GetUniqueName(sfhelper
, &name
[len
], MAX_PATH
-len
);
773 ISFHelper_Release(sfhelper
);
778 if(!CreateDirectoryW(name
, NULL
))
781 /* Update parent of newly created directory */
782 parent
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
786 SendMessageW(info
->hwndTreeView
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)parent
);
788 memset(&item
, 0, sizeof(TVITEMW
));
789 item
.mask
= TVIF_PARAM
|TVIF_STATE
;
791 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
792 item_data
= (LPTV_ITEMDATA
)item
.lParam
;
796 if(item_data
->pEnumIL
)
797 IEnumIDList_Release(item_data
->pEnumIL
);
798 hr
= IShellFolder_EnumObjects(cur
, info
->hwndTreeView
, flags
, &item_data
->pEnumIL
);
802 /* Update treeview */
803 if(!(item
.state
&TVIS_EXPANDEDONCE
)) {
804 item
.mask
= TVIF_STATE
;
805 item
.state
= TVIS_EXPANDEDONCE
;
806 item
.stateMask
= TVIS_EXPANDEDONCE
;
807 SendMessageW(info
->hwndTreeView
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
810 hr
= IShellFolder_ParseDisplayName(cur
, NULL
, NULL
, name
+len
, NULL
, &new_item
, NULL
);
814 added
= InsertTreeViewItem(info
, cur
, new_item
, item_data
->lpifq
, NULL
, parent
);
815 IShellFolder_Release(cur
);
818 SendMessageW(info
->hwndTreeView
, TVM_SORTCHILDREN
, FALSE
, (LPARAM
)parent
);
819 return BrsFolder_Rename(info
, added
);
825 static BOOL
BrsFolder_OnCommand( browse_info
*info
, UINT id
)
827 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
832 if (info
->pidlRet
== NULL
) /* A null pidl would mean a cancel */
833 info
->pidlRet
= _ILCreateDesktop();
834 pdump( info
->pidlRet
);
835 if (lpBrowseInfo
->pszDisplayName
)
836 SHGetPathFromIDListW( info
->pidlRet
, lpBrowseInfo
->pszDisplayName
);
837 EndDialog( info
->hWnd
, 1 );
841 EndDialog( info
->hWnd
, 0 );
844 case IDD_MAKENEWFOLDER
:
845 BrsFolder_NewFolder(info
);
851 static BOOL
BrsFolder_OnSetExpanded(browse_info
*info
, LPVOID selection
,
852 BOOL is_str
, HTREEITEM
*pItem
)
854 LPITEMIDLIST pidlSelection
= selection
;
855 LPCITEMIDLIST pidlCurrent
, pidlRoot
;
857 BOOL bResult
= FALSE
;
859 memset(&item
, 0, sizeof(item
));
861 /* If 'selection' is a string, convert to a Shell ID List. */
863 IShellFolder
*psfDesktop
;
866 hr
= SHGetDesktopFolder(&psfDesktop
);
870 hr
= IShellFolder_ParseDisplayName(psfDesktop
, NULL
, NULL
,
871 selection
, NULL
, &pidlSelection
, NULL
);
872 IShellFolder_Release(psfDesktop
);
877 /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
878 * the sub-tree currently displayed. */
879 pidlRoot
= info
->lpBrowseInfo
->pidlRoot
;
880 pidlCurrent
= pidlSelection
;
881 while (!_ILIsEmpty(pidlRoot
) && _ILIsEqualSimple(pidlRoot
, pidlCurrent
)) {
882 pidlRoot
= ILGetNext(pidlRoot
);
883 pidlCurrent
= ILGetNext(pidlCurrent
);
886 /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
887 if (!_ILIsEmpty(pidlRoot
))
890 /* Initialize item to point to the first child of the root folder. */
891 item
.mask
= TVIF_PARAM
;
892 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
895 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CHILD
,
898 /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
899 while (item
.hItem
&& !_ILIsEmpty(pidlCurrent
)) {
900 LPTV_ITEMDATA pItemData
;
902 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
903 pItemData
= (LPTV_ITEMDATA
)item
.lParam
;
905 if (_ILIsEqualSimple(pItemData
->lpi
, pidlCurrent
)) {
906 pidlCurrent
= ILGetNext(pidlCurrent
);
907 if (!_ILIsEmpty(pidlCurrent
)) {
908 /* Only expand current node and move on to its first child,
909 * if we didn't already reach the last SHITEMID */
910 SendMessageW(info
->hwndTreeView
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)item
.hItem
);
911 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CHILD
,
915 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_NEXT
,
920 if (_ILIsEmpty(pidlCurrent
) && item
.hItem
)
924 if (pidlSelection
&& pidlSelection
!= selection
)
925 ILFree(pidlSelection
);
933 static BOOL
BrsFolder_OnSetSelectionW(browse_info
*info
, LPVOID selection
, BOOL is_str
) {
937 if (!selection
) return FALSE
;
939 bResult
= BrsFolder_OnSetExpanded(info
, selection
, is_str
, &hItem
);
941 SendMessageW(info
->hwndTreeView
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hItem
);
945 static BOOL
BrsFolder_OnSetSelectionA(browse_info
*info
, LPVOID selection
, BOOL is_str
) {
946 LPWSTR selectionW
= NULL
;
951 return BrsFolder_OnSetSelectionW(info
, selection
, is_str
);
953 if ((length
= MultiByteToWideChar(CP_ACP
, 0, selection
, -1, NULL
, 0)) &&
954 (selectionW
= HeapAlloc(GetProcessHeap(), 0, length
* sizeof(WCHAR
))) &&
955 MultiByteToWideChar(CP_ACP
, 0, selection
, -1, selectionW
, length
))
957 result
= BrsFolder_OnSetSelectionW(info
, selectionW
, is_str
);
960 HeapFree(GetProcessHeap(), 0, selectionW
);
964 static LRESULT
BrsFolder_OnWindowPosChanging(browse_info
*info
, WINDOWPOS
*pos
)
966 if ((info
->lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
) && !(pos
->flags
& SWP_NOSIZE
))
968 if (pos
->cx
< info
->szMin
.cx
)
969 pos
->cx
= info
->szMin
.cx
;
970 if (pos
->cy
< info
->szMin
.cy
)
971 pos
->cy
= info
->szMin
.cy
;
976 static INT
BrsFolder_OnDestroy(browse_info
*info
)
980 SHFree(info
->layout
);
987 /*************************************************************************
988 * BrsFolderDlgProc32 (not an exported API function)
990 static INT_PTR CALLBACK
BrsFolderDlgProc( HWND hWnd
, UINT msg
, WPARAM wParam
,
995 TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd
, msg
, wParam
, lParam
);
997 if (msg
== WM_INITDIALOG
)
998 return BrsFolder_OnCreate( hWnd
, (browse_info
*) lParam
);
1000 info
= GetPropW( hWnd
, szBrowseFolderInfo
);
1005 return BrsFolder_OnNotify( info
, (UINT
)wParam
, (LPNMHDR
)lParam
);
1008 return BrsFolder_OnCommand( info
, wParam
);
1010 case WM_WINDOWPOSCHANGING
:
1011 return BrsFolder_OnWindowPosChanging( info
, (WINDOWPOS
*)lParam
);
1014 if (info
->layout
) /* new style dialogs */
1015 LayoutUpdate(hWnd
, info
->layout
, g_layout_info
, LAYOUT_INFO_COUNT
);
1018 case BFFM_SETSTATUSTEXTA
:
1019 TRACE("Set status %s\n", debugstr_a((LPSTR
)lParam
));
1020 SetWindowTextA(GetDlgItem(hWnd
, IDD_STATUS
), (LPSTR
)lParam
);
1023 case BFFM_SETSTATUSTEXTW
:
1024 TRACE("Set status %s\n", debugstr_w((LPWSTR
)lParam
));
1025 SetWindowTextW(GetDlgItem(hWnd
, IDD_STATUS
), (LPWSTR
)lParam
);
1029 TRACE("Enable %ld\n", lParam
);
1030 EnableWindow(GetDlgItem(hWnd
, 1), lParam
!= 0);
1033 case BFFM_SETOKTEXT
: /* unicode only */
1034 TRACE("Set OK text %s\n", debugstr_w((LPWSTR
)lParam
));
1035 SetWindowTextW(GetDlgItem(hWnd
, 1), (LPWSTR
)lParam
);
1038 case BFFM_SETSELECTIONA
:
1039 return BrsFolder_OnSetSelectionA(info
, (LPVOID
)lParam
, (BOOL
)wParam
);
1041 case BFFM_SETSELECTIONW
:
1042 return BrsFolder_OnSetSelectionW(info
, (LPVOID
)lParam
, (BOOL
)wParam
);
1044 case BFFM_SETEXPANDED
: /* unicode only */
1045 return BrsFolder_OnSetExpanded(info
, (LPVOID
)lParam
, (BOOL
)wParam
, NULL
);
1048 return BrsFolder_OnDestroy(info
);
1053 static const WCHAR swBrowseTemplateName
[] = {
1054 'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
1055 static const WCHAR swNewBrowseTemplateName
[] = {
1056 'S','H','N','E','W','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
1058 /*************************************************************************
1059 * SHBrowseForFolderA [SHELL32.@]
1060 * SHBrowseForFolder [SHELL32.@]
1062 LPITEMIDLIST WINAPI
SHBrowseForFolderA (LPBROWSEINFOA lpbi
)
1069 TRACE("%p\n", lpbi
);
1071 bi
.hwndOwner
= lpbi
->hwndOwner
;
1072 bi
.pidlRoot
= lpbi
->pidlRoot
;
1073 if (lpbi
->pszDisplayName
)
1074 bi
.pszDisplayName
= HeapAlloc( GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
) );
1076 bi
.pszDisplayName
= NULL
;
1078 if (lpbi
->lpszTitle
)
1080 len
= MultiByteToWideChar( CP_ACP
, 0, lpbi
->lpszTitle
, -1, NULL
, 0 );
1081 title
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1082 MultiByteToWideChar( CP_ACP
, 0, lpbi
->lpszTitle
, -1, title
, len
);
1087 bi
.lpszTitle
= title
;
1088 bi
.ulFlags
= lpbi
->ulFlags
;
1089 bi
.lpfn
= lpbi
->lpfn
;
1090 bi
.lParam
= lpbi
->lParam
;
1091 bi
.iImage
= lpbi
->iImage
;
1092 lpid
= SHBrowseForFolderW( &bi
);
1093 if (bi
.pszDisplayName
)
1095 WideCharToMultiByte( CP_ACP
, 0, bi
.pszDisplayName
, -1,
1096 lpbi
->pszDisplayName
, MAX_PATH
, 0, NULL
);
1097 HeapFree( GetProcessHeap(), 0, bi
.pszDisplayName
);
1099 HeapFree(GetProcessHeap(), 0, title
);
1100 lpbi
->iImage
= bi
.iImage
;
1105 /*************************************************************************
1106 * SHBrowseForFolderW [SHELL32.@]
1109 * crashes when passed a null pointer
1111 LPITEMIDLIST WINAPI
SHBrowseForFolderW (LPBROWSEINFOW lpbi
)
1116 const WCHAR
* templateName
;
1117 INITCOMMONCONTROLSEX icex
;
1120 info
.pidlRet
= NULL
;
1121 info
.lpBrowseInfo
= lpbi
;
1122 info
.hwndTreeView
= NULL
;
1124 icex
.dwSize
= sizeof( icex
);
1125 icex
.dwICC
= ICC_TREEVIEW_CLASSES
;
1126 InitCommonControlsEx( &icex
);
1128 hr
= OleInitialize(NULL
);
1130 if (lpbi
->ulFlags
& BIF_NEWDIALOGSTYLE
)
1131 templateName
= swNewBrowseTemplateName
;
1133 templateName
= swBrowseTemplateName
;
1134 r
= DialogBoxParamW( shell32_hInstance
, templateName
, lpbi
->hwndOwner
,
1135 BrsFolderDlgProc
, (LPARAM
)&info
);
1140 ILFree(info
.pidlRet
);
1144 return info
.pidlRet
;