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
28 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
30 /* original margins and control size */
31 typedef struct tagLAYOUT_DATA
33 LONG left
, width
, right
;
34 LONG top
, height
, bottom
;
37 typedef struct tagbrowse_info
41 LPBROWSEINFOW lpBrowseInfo
;
43 LAYOUT_DATA
*layout
; /* filled by LayoutInit, used by LayoutUpdate */
47 typedef struct tagTV_ITEMDATA
49 LPSHELLFOLDER lpsfParent
; /* IShellFolder of the parent */
50 LPITEMIDLIST lpi
; /* PIDL relative to parent */
51 LPITEMIDLIST lpifq
; /* Fully qualified PIDL */
52 IEnumIDList
* pEnumIL
; /* Children iterator */
53 } TV_ITEMDATA
, *LPTV_ITEMDATA
;
55 typedef struct tagLAYOUT_INFO
57 int iItemId
; /* control id */
58 DWORD dwAnchor
; /* BF_* flags specifying which margins should remain constant */
61 static const LAYOUT_INFO g_layout_info
[] =
63 {IDC_BROWSE_FOR_FOLDER_TITLE
, BF_TOP
|BF_LEFT
|BF_RIGHT
},
64 {IDC_BROWSE_FOR_FOLDER_STATUS
, BF_TOP
|BF_LEFT
|BF_RIGHT
},
65 {IDC_BROWSE_FOR_FOLDER_FOLDER
, BF_TOP
|BF_LEFT
|BF_RIGHT
},
66 {IDC_BROWSE_FOR_FOLDER_TREEVIEW
, BF_TOP
|BF_BOTTOM
|BF_LEFT
|BF_RIGHT
},
67 {IDC_BROWSE_FOR_FOLDER_FOLDER
, BF_BOTTOM
|BF_LEFT
},
68 {IDC_BROWSE_FOR_FOLDER_FOLDER_TEXT
, BF_BOTTOM
|BF_LEFT
|BF_RIGHT
},
69 {IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
, BF_BOTTOM
|BF_LEFT
},
70 {IDOK
, BF_BOTTOM
|BF_RIGHT
},
71 {IDCANCEL
, BF_BOTTOM
|BF_RIGHT
}
74 #define LAYOUT_INFO_COUNT (sizeof(g_layout_info)/sizeof(g_layout_info[0]))
76 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
77 BIF_BROWSEFORCOMPUTER | \
78 BIF_RETURNFSANCESTORS | \
79 BIF_RETURNONLYFSDIRS | \
80 BIF_NONEWFOLDERBUTTON | \
81 BIF_NEWDIALOGSTYLE | \
82 BIF_BROWSEINCLUDEFILES)
84 static void FillTreeView(browse_info
*, LPSHELLFOLDER
,
85 LPITEMIDLIST
, HTREEITEM
, IEnumIDList
*);
86 static HTREEITEM
InsertTreeViewItem( browse_info
*, IShellFolder
*,
87 LPCITEMIDLIST
, LPCITEMIDLIST
, IEnumIDList
*, HTREEITEM
);
89 static const WCHAR szBrowseFolderInfo
[] = L
"__WINE_BRSFOLDERDLG_INFO";
91 static DWORD __inline
BrowseFlagsToSHCONTF(UINT ulFlags
)
93 return SHCONTF_FOLDERS
| (ulFlags
& BIF_BROWSEINCLUDEFILES
? SHCONTF_NONFOLDERS
: 0);
96 static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo
, HWND hWnd
,
97 UINT msg
, LPARAM param
)
99 if (!lpBrowseInfo
->lpfn
)
101 lpBrowseInfo
->lpfn( hWnd
, msg
, param
, lpBrowseInfo
->lParam
);
104 static LAYOUT_DATA
*LayoutInit(HWND hwnd
, const LAYOUT_INFO
*layout_info
, int layout_count
)
110 GetClientRect(hwnd
, &rcWnd
);
111 data
= (LAYOUT_DATA
*)SHAlloc(sizeof(LAYOUT_DATA
)*layout_count
);
112 for (i
= 0; i
< layout_count
; i
++)
115 HWND hItem
= GetDlgItem(hwnd
, layout_info
[i
].iItemId
);
118 ERR("Item %d not found\n", i
);
119 GetWindowRect(hItem
, &r
);
120 MapWindowPoints(HWND_DESKTOP
, hwnd
, (LPPOINT
)&r
, 2);
122 data
[i
].left
= r
.left
;
123 data
[i
].right
= rcWnd
.right
- r
.right
;
124 data
[i
].width
= r
.right
- r
.left
;
127 data
[i
].bottom
= rcWnd
.bottom
- r
.bottom
;
128 data
[i
].height
= r
.bottom
- r
.top
;
133 static void LayoutUpdate(HWND hwnd
, LAYOUT_DATA
*data
, const LAYOUT_INFO
*layout_info
, int layout_count
)
138 GetClientRect(hwnd
, &rcWnd
);
139 for (i
= 0; i
< layout_count
; i
++)
142 HWND hItem
= GetDlgItem(hwnd
, layout_info
[i
].iItemId
);
144 GetWindowRect(hItem
, &r
);
145 MapWindowPoints(HWND_DESKTOP
, hwnd
, (LPPOINT
)&r
, 2);
147 if (layout_info
[i
].dwAnchor
& BF_RIGHT
)
149 r
.right
= rcWnd
.right
- data
[i
].right
;
150 if (!(layout_info
[i
].dwAnchor
& BF_LEFT
))
151 r
.left
= r
.right
- data
[i
].width
;
154 if (layout_info
[i
].dwAnchor
& BF_BOTTOM
)
156 r
.bottom
= rcWnd
.bottom
- data
[i
].bottom
;
157 if (!(layout_info
[i
].dwAnchor
& BF_TOP
))
158 r
.top
= r
.bottom
- data
[i
].height
;
161 SetWindowPos(hItem
, NULL
, r
.left
, r
.top
, r
.right
- r
.left
, r
.bottom
- r
.top
, SWP_NOZORDER
);
166 /******************************************************************************
167 * InitializeTreeView [Internal]
169 * Called from WM_INITDIALOG handler.
172 * hwndParent [I] The BrowseForFolder dialog
173 * root [I] ITEMIDLIST of the root shell folder
175 static void InitializeTreeView( browse_info
*info
)
177 LPITEMIDLIST pidlParent
, pidlChild
;
178 HIMAGELIST hImageList
;
180 IShellFolder
*lpsfParent
, *lpsfRoot
;
181 IEnumIDList
* pEnumChildren
= NULL
;
184 LPCITEMIDLIST root
= info
->lpBrowseInfo
->pidlRoot
;
186 TRACE("%p\n", info
);
188 Shell_GetImageLists(NULL
, &hImageList
);
191 SendMessageW( info
->hwndTreeView
, TVM_SETIMAGELIST
, 0, (LPARAM
)hImageList
);
193 /* We want to call InsertTreeViewItem down the code, in order to insert
194 * the root item of the treeview. Due to InsertTreeViewItem's signature,
195 * we need the following to do this:
197 * + An ITEMIDLIST corresponding to _the parent_ of root.
198 * + An ITEMIDLIST, which is a relative path from root's parent to root
199 * (containing a single SHITEMID).
200 * + An IShellFolder interface pointer of root's parent folder.
202 * If root is 'Desktop', then root's parent is also 'Desktop'.
205 pidlParent
= ILClone(root
);
206 ILRemoveLastID(pidlParent
);
207 pidlChild
= ILClone(ILFindLastID(root
));
209 if (_ILIsDesktop(pidlParent
)) {
210 hr
= SHGetDesktopFolder(&lpsfParent
);
212 IShellFolder
*lpsfDesktop
;
213 hr
= SHGetDesktopFolder(&lpsfDesktop
);
215 WARN("SHGetDesktopFolder failed! hr = %08x\n", hr
);
220 hr
= lpsfDesktop
->BindToObject(pidlParent
, 0, IID_PPV_ARG(IShellFolder
, &lpsfParent
));
221 lpsfDesktop
->Release();
225 WARN("Could not bind to parent shell folder! hr = %08x\n", hr
);
231 if (pidlChild
&& pidlChild
->mkid
.cb
) {
232 hr
= lpsfParent
->BindToObject(pidlChild
, 0, IID_PPV_ARG(IShellFolder
, &lpsfRoot
));
234 lpsfRoot
= lpsfParent
;
235 hr
= lpsfParent
->AddRef();
239 WARN("Could not bind to root shell folder! hr = %08x\n", hr
);
240 lpsfParent
->Release();
246 flags
= BrowseFlagsToSHCONTF( info
->lpBrowseInfo
->ulFlags
);
247 hr
= lpsfRoot
->EnumObjects(info
->hWnd
, flags
, &pEnumChildren
);
249 WARN("Could not get child iterator! hr = %08x\n", hr
);
250 lpsfParent
->Release();
257 SendMessageW( info
->hwndTreeView
, TVM_DELETEITEM
, 0, (LPARAM
)TVI_ROOT
);
258 item
= InsertTreeViewItem( info
, lpsfParent
, pidlChild
,
259 pidlParent
, pEnumChildren
, TVI_ROOT
);
260 SendMessageW( info
->hwndTreeView
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)item
);
265 lpsfParent
->Release();
268 static int GetIcon(LPCITEMIDLIST lpi
, UINT uFlags
)
271 SHGetFileInfoW((LPCWSTR
)lpi
, 0 ,&sfi
, sizeof(SHFILEINFOW
), uFlags
);
275 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq
, LPTVITEMW lpTV_ITEM
)
277 LPITEMIDLIST pidlDesktop
= NULL
;
280 TRACE("%p %p\n",lpifq
, lpTV_ITEM
);
284 pidlDesktop
= _ILCreateDesktop();
288 flags
= SHGFI_PIDL
| SHGFI_SYSICONINDEX
| SHGFI_SMALLICON
;
289 lpTV_ITEM
->iImage
= GetIcon( lpifq
, flags
);
291 flags
= SHGFI_PIDL
| SHGFI_SYSICONINDEX
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
292 lpTV_ITEM
->iSelectedImage
= GetIcon( lpifq
, flags
);
295 ILFree( pidlDesktop
);
298 /******************************************************************************
301 * Query a shell folder for the display name of one of it's children
304 * lpsf [I] IShellFolder interface of the folder to be queried.
305 * lpi [I] ITEMIDLIST of the child, relative to parent
306 * dwFlags [I] as in IShellFolder::GetDisplayNameOf
307 * lpFriendlyName [O] The desired display name in unicode
313 static BOOL
GetName(LPSHELLFOLDER lpsf
, LPCITEMIDLIST lpi
, DWORD dwFlags
, LPWSTR lpFriendlyName
)
318 TRACE("%p %p %x %p\n", lpsf
, lpi
, dwFlags
, lpFriendlyName
);
319 if (SUCCEEDED(lpsf
->GetDisplayNameOf(lpi
, dwFlags
, &str
)))
320 bSuccess
= StrRetToStrNW(lpFriendlyName
, MAX_PATH
, &str
, lpi
);
324 TRACE("-- %s\n", debugstr_w(lpFriendlyName
));
328 /******************************************************************************
329 * InsertTreeViewItem [Internal]
332 * info [I] data for the dialog
333 * lpsf [I] IShellFolder interface of the item's parent shell folder
334 * pidl [I] ITEMIDLIST of the child to insert, relative to parent
335 * pidlParent [I] ITEMIDLIST of the parent shell folder
336 * pEnumIL [I] Iterator for the children of the item to be inserted
337 * hParent [I] The treeview-item that represents the parent shell folder
340 * Success: Handle to the created and inserted treeview-item
343 static HTREEITEM
InsertTreeViewItem( browse_info
*info
, IShellFolder
* lpsf
,
344 LPCITEMIDLIST pidl
, LPCITEMIDLIST pidlParent
, IEnumIDList
* pEnumIL
,
348 TVINSERTSTRUCTW tvins
;
349 WCHAR szBuff
[MAX_PATH
];
350 LPTV_ITEMDATA lptvid
=0;
352 tvi
.mask
= TVIF_TEXT
| TVIF_IMAGE
| TVIF_SELECTEDIMAGE
| TVIF_PARAM
;
354 tvi
.cChildren
= pEnumIL
? 1 : 0;
355 tvi
.mask
|= TVIF_CHILDREN
;
357 lptvid
= (TV_ITEMDATA
*)SHAlloc( sizeof(TV_ITEMDATA
) );
361 if (!GetName(lpsf
, pidl
, SHGDN_NORMAL
, szBuff
))
364 tvi
.pszText
= szBuff
;
365 tvi
.cchTextMax
= MAX_PATH
;
366 tvi
.lParam
= (LPARAM
)lptvid
;
369 lptvid
->lpsfParent
= lpsf
;
370 lptvid
->lpi
= ILClone(pidl
);
371 lptvid
->lpifq
= pidlParent
? ILCombine(pidlParent
, pidl
) : ILClone(pidl
);
372 lptvid
->pEnumIL
= pEnumIL
;
373 GetNormalAndSelectedIcons(lptvid
->lpifq
, &tvi
);
376 tvins
.hInsertAfter
= NULL
;
377 tvins
.hParent
= hParent
;
379 return TreeView_InsertItem( info
->hwndTreeView
, &tvins
);
382 /******************************************************************************
383 * FillTreeView [Internal]
385 * For each child (given by lpe) of the parent shell folder, which is given by
386 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
389 * info [I] data for the dialog
390 * lpsf [I] IShellFolder interface of the parent shell folder
391 * pidl [I] ITEMIDLIST of the parent shell folder
392 * hParent [I] The treeview item that represents the parent shell folder
393 * lpe [I] An iterator for the children of the parent shell folder
395 static void FillTreeView( browse_info
*info
, IShellFolder
* lpsf
,
396 LPITEMIDLIST pidl
, HTREEITEM hParent
, IEnumIDList
* lpe
)
398 LPITEMIDLIST pidlTemp
= 0;
401 HWND hwnd
= GetParent( info
->hwndTreeView
);
403 TRACE("%p %p %p %p\n",lpsf
, pidl
, hParent
, lpe
);
405 /* No IEnumIDList -> No children */
409 SetCursor( LoadCursorA( 0, (LPSTR
)IDC_WAIT
) );
411 while (S_OK
== lpe
->Next(1,&pidlTemp
,&ulFetched
))
413 ULONG ulAttrs
= SFGAO_HASSUBFOLDER
| SFGAO_FOLDER
;
414 IEnumIDList
* pEnumIL
= NULL
;
415 IShellFolder
* pSFChild
= NULL
;
416 lpsf
->GetAttributesOf(1, (LPCITEMIDLIST
*)&pidlTemp
, &ulAttrs
);
417 if (ulAttrs
& SFGAO_FOLDER
)
419 hr
= lpsf
->BindToObject(pidlTemp
, NULL
, IID_PPV_ARG(IShellFolder
, &pSFChild
));
422 DWORD flags
= BrowseFlagsToSHCONTF(info
->lpBrowseInfo
->ulFlags
);
423 hr
= pSFChild
->EnumObjects(hwnd
, flags
, &pEnumIL
);
426 if ((pEnumIL
->Skip(1) != S_OK
) ||
427 FAILED(pEnumIL
->Reset()))
437 if (!InsertTreeViewItem(info
, lpsf
, pidlTemp
, pidl
, pEnumIL
, hParent
))
439 SHFree(pidlTemp
); /* Finally, free the pidl that the shell gave us... */
445 SetCursor(LoadCursorW(0, (LPWSTR
)IDC_ARROW
));
449 static BOOL __inline
PIDLIsType(LPCITEMIDLIST pidl
, PIDLTYPE type
)
451 LPPIDLDATA data
= _ILGetDataPointer(pidl
);
454 return (data
->type
== type
);
457 static void BrsFolder_CheckValidSelection( browse_info
*info
, LPTV_ITEMDATA lptvid
)
459 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
460 LPCITEMIDLIST pidl
= lptvid
->lpi
;
461 BOOL bEnabled
= TRUE
;
465 if ((lpBrowseInfo
->ulFlags
& BIF_BROWSEFORCOMPUTER
) &&
466 !PIDLIsType(pidl
, PT_COMP
))
468 if (lpBrowseInfo
->ulFlags
& BIF_RETURNFSANCESTORS
)
470 dwAttributes
= SFGAO_FILESYSANCESTOR
| SFGAO_FILESYSTEM
;
471 r
= lptvid
->lpsfParent
->GetAttributesOf(1,
472 (LPCITEMIDLIST
*)&lptvid
->lpi
, &dwAttributes
);
473 if (FAILED(r
) || !(dwAttributes
& (SFGAO_FILESYSANCESTOR
|SFGAO_FILESYSTEM
)))
477 dwAttributes
= SFGAO_FOLDER
| SFGAO_FILESYSTEM
;
478 r
= lptvid
->lpsfParent
->GetAttributesOf(1,
479 (LPCITEMIDLIST
*)&lptvid
->lpi
,
482 ((dwAttributes
& (SFGAO_FOLDER
|SFGAO_FILESYSTEM
)) != (SFGAO_FOLDER
|SFGAO_FILESYSTEM
)))
484 if (lpBrowseInfo
->ulFlags
& BIF_RETURNONLYFSDIRS
)
486 EnableWindow(GetDlgItem(info
->hWnd
, IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
), FALSE
);
489 EnableWindow(GetDlgItem(info
->hWnd
, IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
), TRUE
);
491 SendMessageW(info
->hWnd
, BFFM_ENABLEOK
, 0, bEnabled
);
494 static LRESULT
BrsFolder_Treeview_Delete( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
496 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
)pnmtv
->itemOld
.lParam
;
498 TRACE("TVN_DELETEITEMA/W %p\n", lptvid
);
500 lptvid
->lpsfParent
->Release();
502 lptvid
->pEnumIL
->Release();
504 SHFree(lptvid
->lpifq
);
509 static LRESULT
BrsFolder_Treeview_Expand( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
511 IShellFolder
*lpsf2
= NULL
;
512 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
) pnmtv
->itemNew
.lParam
;
515 TRACE("TVN_ITEMEXPANDINGA/W\n");
517 if ((pnmtv
->itemNew
.state
& TVIS_EXPANDEDONCE
))
520 if (lptvid
->lpi
&& lptvid
->lpi
->mkid
.cb
) {
521 r
= lptvid
->lpsfParent
->BindToObject(lptvid
->lpi
, 0, IID_PPV_ARG(IShellFolder
, &lpsf2
));
523 lpsf2
= lptvid
->lpsfParent
;
529 FillTreeView( info
, lpsf2
, lptvid
->lpifq
, pnmtv
->itemNew
.hItem
, lptvid
->pEnumIL
);
533 /* My Computer is already sorted and trying to do a simple text
534 * sort will only mess things up */
535 if (!_ILIsMyComputer(lptvid
->lpi
))
536 SendMessageW( info
->hwndTreeView
, TVM_SORTCHILDREN
,
537 FALSE
, (LPARAM
)pnmtv
->itemNew
.hItem
);
542 static HRESULT
BrsFolder_Treeview_Changed( browse_info
*info
, NMTREEVIEWW
*pnmtv
)
544 LPTV_ITEMDATA lptvid
= (LPTV_ITEMDATA
) pnmtv
->itemNew
.lParam
;
545 WCHAR name
[MAX_PATH
];
547 ILFree(info
->pidlRet
);
548 info
->pidlRet
= ILClone(lptvid
->lpifq
);
550 if (GetName(lptvid
->lpsfParent
, lptvid
->lpi
, SHGDN_NORMAL
, name
))
551 SetWindowTextW( GetDlgItem(info
->hWnd
, IDC_BROWSE_FOR_FOLDER_FOLDER_TEXT
), name
);
553 browsefolder_callback( info
->lpBrowseInfo
, info
->hWnd
, BFFM_SELCHANGED
,
554 (LPARAM
)info
->pidlRet
);
555 BrsFolder_CheckValidSelection( info
, lptvid
);
559 static LRESULT
BrsFolder_Treeview_Rename(browse_info
*info
, NMTVDISPINFOW
*pnmtv
)
561 LPTV_ITEMDATA item_data
;
562 WCHAR old_path
[MAX_PATH
], new_path
[MAX_PATH
], *p
;
566 if(!pnmtv
->item
.pszText
)
569 item
.mask
= TVIF_HANDLE
|TVIF_PARAM
;
570 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
571 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
572 item_data
= (LPTV_ITEMDATA
)item
.lParam
;
574 SHGetPathFromIDListW(item_data
->lpifq
, old_path
);
575 if(!(p
= strrchrW(old_path
, '\\')))
577 p
= new_path
+(p
-old_path
+1);
578 memcpy(new_path
, old_path
, (p
-new_path
)*sizeof(WCHAR
));
579 strcpyW(p
, pnmtv
->item
.pszText
);
581 if(!MoveFileW(old_path
, new_path
))
584 SHFree(item_data
->lpifq
);
585 SHFree(item_data
->lpi
);
586 item_data
->lpifq
= SHSimpleIDListFromPathW(new_path
);
587 item_data
->lpsfParent
->ParseDisplayName(NULL
, NULL
, pnmtv
->item
.pszText
,
588 NULL
, &item_data
->lpi
, NULL
);
590 item
.mask
= TVIF_HANDLE
|TVIF_TEXT
;
591 item
.pszText
= pnmtv
->item
.pszText
;
592 SendMessageW(info
->hwndTreeView
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
594 nmtv
.itemNew
.lParam
= item
.lParam
;
595 BrsFolder_Treeview_Changed(info
, &nmtv
);
599 static LRESULT
BrsFolder_OnNotify( browse_info
*info
, UINT CtlID
, LPNMHDR lpnmh
)
601 NMTREEVIEWW
*pnmtv
= (NMTREEVIEWW
*)lpnmh
;
603 TRACE("%p %x %p msg=%x\n", info
, CtlID
, lpnmh
, pnmtv
->hdr
.code
);
605 if (pnmtv
->hdr
.idFrom
!= IDC_BROWSE_FOR_FOLDER_TREEVIEW
)
608 switch (pnmtv
->hdr
.code
)
610 case TVN_DELETEITEMA
:
611 case TVN_DELETEITEMW
:
612 return BrsFolder_Treeview_Delete( info
, pnmtv
);
614 case TVN_ITEMEXPANDINGA
:
615 case TVN_ITEMEXPANDINGW
:
616 return BrsFolder_Treeview_Expand( info
, pnmtv
);
618 case TVN_SELCHANGEDA
:
619 case TVN_SELCHANGEDW
:
620 return BrsFolder_Treeview_Changed( info
, pnmtv
);
622 case TVN_ENDLABELEDITA
:
623 case TVN_ENDLABELEDITW
:
624 return BrsFolder_Treeview_Rename( info
, (LPNMTVDISPINFOW
)pnmtv
);
627 WARN("unhandled (%d)\n", pnmtv
->hdr
.code
);
635 static BOOL
BrsFolder_OnCreate( HWND hWnd
, browse_info
*info
)
637 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
640 SetPropW( hWnd
, szBrowseFolderInfo
, info
);
642 if (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
643 FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
644 if (lpBrowseInfo
->ulFlags
& ~SUPPORTEDFLAGS
)
645 FIXME("flags %x not implemented\n", lpBrowseInfo
->ulFlags
& ~SUPPORTEDFLAGS
);
647 if (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
651 info
->layout
= LayoutInit(hWnd
, g_layout_info
, LAYOUT_INFO_COUNT
);
653 /* TODO: Windows allows shrinking the windows a bit */
654 GetWindowRect(hWnd
, &rcWnd
);
655 info
->szMin
.cx
= rcWnd
.right
- rcWnd
.left
;
656 info
->szMin
.cy
= rcWnd
.bottom
- rcWnd
.top
;
663 if (lpBrowseInfo
->lpszTitle
)
664 SetWindowTextW( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_TITLE
), lpBrowseInfo
->lpszTitle
);
666 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_TITLE
), SW_HIDE
);
668 if (!(lpBrowseInfo
->ulFlags
& BIF_STATUSTEXT
)
669 || (lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
))
670 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_STATUS
), SW_HIDE
);
672 /* Hide "Make New Folder" Button? */
673 if ((lpBrowseInfo
->ulFlags
& BIF_NONEWFOLDERBUTTON
)
674 || !(lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
))
675 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
), SW_HIDE
);
677 /* Hide the editbox? */
678 if (!(lpBrowseInfo
->ulFlags
& BIF_EDITBOX
))
680 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_FOLDER
), SW_HIDE
);
681 ShowWindow( GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_FOLDER_TEXT
), SW_HIDE
);
684 info
->hwndTreeView
= GetDlgItem( hWnd
, IDC_BROWSE_FOR_FOLDER_TREEVIEW
);
685 if (info
->hwndTreeView
)
687 InitializeTreeView( info
);
689 /* Resize the treeview if there's not editbox */
690 if ((lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
)
691 && !(lpBrowseInfo
->ulFlags
& BIF_EDITBOX
))
694 GetClientRect(info
->hwndTreeView
, &rc
);
695 SetWindowPos(info
->hwndTreeView
, HWND_TOP
, 0, 0,
696 rc
.right
, rc
.bottom
+ 40, SWP_NOMOVE
);
700 ERR("treeview control missing!\n");
702 browsefolder_callback( info
->lpBrowseInfo
, hWnd
, BFFM_INITIALIZED
, 0 );
707 static HRESULT
BrsFolder_Rename(browse_info
*info
, HTREEITEM rename
)
709 SendMessageW(info
->hwndTreeView
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)rename
);
710 SendMessageW(info
->hwndTreeView
, TVM_EDITLABELW
, 0, (LPARAM
)rename
);
714 static HRESULT
BrsFolder_NewFolder(browse_info
*info
)
716 DWORD flags
= BrowseFlagsToSHCONTF(info
->lpBrowseInfo
->ulFlags
);
717 IShellFolder
*desktop
, *cur
;
719 WCHAR name
[MAX_PATH
];
720 HTREEITEM parent
, added
;
721 LPTV_ITEMDATA item_data
;
722 LPITEMIDLIST new_item
;
728 ERR("Make new folder button should be disabled\n");
732 /* Create new directory */
733 hr
= SHGetDesktopFolder(&desktop
);
736 hr
= desktop
->BindToObject(info
->pidlRet
, 0, IID_PPV_ARG(IShellFolder
, &cur
));
741 hr
= cur
->QueryInterface(IID_PPV_ARG(ISFHelper
, &sfhelper
));
745 hr
= SHGetPathFromIDListW(info
->pidlRet
, name
);
752 hr
= sfhelper
->GetUniqueName(&name
[len
], MAX_PATH
-len
);
758 if(!CreateDirectoryW(name
, NULL
))
761 /* Update parent of newly created directory */
762 parent
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
766 SendMessageW(info
->hwndTreeView
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)parent
);
768 memset(&item
, 0, sizeof(TVITEMW
));
769 item
.mask
= TVIF_PARAM
|TVIF_STATE
;
771 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
772 item_data
= (LPTV_ITEMDATA
)item
.lParam
;
776 if(item_data
->pEnumIL
)
777 item_data
->pEnumIL
->Release();
778 hr
= cur
->EnumObjects(info
->hwndTreeView
, flags
, &item_data
->pEnumIL
);
782 /* Update treeview */
783 if(!(item
.state
&TVIS_EXPANDEDONCE
)) {
784 item
.mask
= TVIF_STATE
;
785 item
.state
= TVIS_EXPANDEDONCE
;
786 item
.stateMask
= TVIS_EXPANDEDONCE
;
787 SendMessageW(info
->hwndTreeView
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
790 hr
= cur
->ParseDisplayName(NULL
, NULL
, name
+len
, NULL
, &new_item
, NULL
);
794 added
= InsertTreeViewItem(info
, cur
, new_item
, item_data
->lpifq
, NULL
, parent
);
798 SendMessageW(info
->hwndTreeView
, TVM_SORTCHILDREN
, FALSE
, (LPARAM
)parent
);
799 return BrsFolder_Rename(info
, added
);
805 static BOOL
BrsFolder_OnCommand( browse_info
*info
, UINT id
)
807 LPBROWSEINFOW lpBrowseInfo
= info
->lpBrowseInfo
;
812 /* The original pidl is owned by the treeview and will be free'd. */
813 info
->pidlRet
= ILClone(info
->pidlRet
);
814 if (info
->pidlRet
== NULL
) /* A null pidl would mean a cancel */
815 info
->pidlRet
= _ILCreateDesktop();
816 pdump( info
->pidlRet
);
817 if (lpBrowseInfo
->pszDisplayName
)
818 SHGetPathFromIDListW( info
->pidlRet
, lpBrowseInfo
->pszDisplayName
);
819 EndDialog( info
->hWnd
, 1 );
823 EndDialog( info
->hWnd
, 0 );
826 case IDC_BROWSE_FOR_FOLDER_NEW_FOLDER
:
827 BrsFolder_NewFolder(info
);
833 static BOOL
BrsFolder_OnSetExpanded(browse_info
*info
, LPVOID selection
,
834 BOOL is_str
, HTREEITEM
*pItem
)
836 LPITEMIDLIST pidlSelection
= (LPITEMIDLIST
)selection
;
837 LPCITEMIDLIST pidlCurrent
, pidlRoot
;
839 BOOL bResult
= FALSE
;
841 memset(&item
, 0, sizeof(item
));
843 /* If 'selection' is a string, convert to a Shell ID List. */
845 IShellFolder
*psfDesktop
;
848 hr
= SHGetDesktopFolder(&psfDesktop
);
852 hr
= psfDesktop
->ParseDisplayName(NULL
, NULL
, (LPOLESTR
)selection
,
853 NULL
, &pidlSelection
, NULL
);
854 psfDesktop
->Release();
859 /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
860 * the sub-tree currently displayed. */
861 pidlRoot
= info
->lpBrowseInfo
->pidlRoot
;
862 pidlCurrent
= pidlSelection
;
863 while (!_ILIsEmpty(pidlRoot
) && _ILIsEqualSimple(pidlRoot
, pidlCurrent
)) {
864 pidlRoot
= ILGetNext(pidlRoot
);
865 pidlCurrent
= ILGetNext(pidlCurrent
);
868 /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
869 if (!_ILIsEmpty(pidlRoot
))
872 /* Initialize item to point to the first child of the root folder. */
873 item
.mask
= TVIF_PARAM
;
874 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
877 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CHILD
,
880 /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
881 while (item
.hItem
&& !_ILIsEmpty(pidlCurrent
)) {
882 LPTV_ITEMDATA pItemData
;
884 SendMessageW(info
->hwndTreeView
, TVM_GETITEMW
, 0, (LPARAM
)&item
);
885 pItemData
= (LPTV_ITEMDATA
)item
.lParam
;
887 if (_ILIsEqualSimple(pItemData
->lpi
, pidlCurrent
)) {
888 pidlCurrent
= ILGetNext(pidlCurrent
);
889 if (!_ILIsEmpty(pidlCurrent
)) {
890 /* Only expand current node and move on to it's first child,
891 * if we didn't already reach the last SHITEMID */
892 SendMessageW(info
->hwndTreeView
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)item
.hItem
);
893 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_CHILD
,
897 item
.hItem
= (HTREEITEM
)SendMessageW(info
->hwndTreeView
, TVM_GETNEXTITEM
, TVGN_NEXT
,
902 if (_ILIsEmpty(pidlCurrent
) && item
.hItem
)
906 if (pidlSelection
&& pidlSelection
!= (LPITEMIDLIST
)selection
)
907 ILFree(pidlSelection
);
915 static BOOL
BrsFolder_OnSetSelectionW(browse_info
*info
, LPVOID selection
, BOOL is_str
) {
919 if (!selection
) return FALSE
;
921 bResult
= BrsFolder_OnSetExpanded(info
, selection
, is_str
, &hItem
);
923 SendMessageW(info
->hwndTreeView
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)hItem
);
927 static BOOL
BrsFolder_OnSetSelectionA(browse_info
*info
, LPVOID selection
, BOOL is_str
) {
928 LPWSTR selectionW
= NULL
;
933 return BrsFolder_OnSetSelectionW(info
, selection
, is_str
);
935 if ((length
= MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)selection
, -1, NULL
, 0)) &&
936 (selectionW
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, length
* sizeof(WCHAR
))) &&
937 MultiByteToWideChar(CP_ACP
, 0, (LPCSTR
)selection
, -1, selectionW
, length
))
939 result
= BrsFolder_OnSetSelectionW(info
, selectionW
, is_str
);
942 HeapFree(GetProcessHeap(), 0, selectionW
);
946 static BOOL
BrsFolder_OnWindowPosChanging(browse_info
*info
, WINDOWPOS
*pos
)
948 if ((info
->lpBrowseInfo
->ulFlags
& BIF_NEWDIALOGSTYLE
) && !(pos
->flags
& SWP_NOSIZE
))
950 if (pos
->cx
< info
->szMin
.cx
)
951 pos
->cx
= info
->szMin
.cx
;
952 if (pos
->cy
< info
->szMin
.cy
)
953 pos
->cy
= info
->szMin
.cy
;
958 static INT
BrsFolder_OnDestroy(browse_info
*info
)
962 SHFree(info
->layout
);
969 /*************************************************************************
970 * BrsFolderDlgProc32 (not an exported API function)
972 static INT_PTR CALLBACK
BrsFolderDlgProc( HWND hWnd
, UINT msg
, WPARAM wParam
,
977 TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd
, msg
, wParam
, lParam
);
979 if (msg
== WM_INITDIALOG
)
980 return BrsFolder_OnCreate( hWnd
, (browse_info
*) lParam
);
982 info
= (browse_info
*) GetPropW( hWnd
, szBrowseFolderInfo
);
987 return BrsFolder_OnNotify( info
, (UINT
)wParam
, (LPNMHDR
)lParam
);
990 return BrsFolder_OnCommand( info
, wParam
);
992 case WM_WINDOWPOSCHANGING
:
993 return BrsFolder_OnWindowPosChanging( info
, (WINDOWPOS
*)lParam
);
996 if (info
->layout
) /* new style dialogs */
997 LayoutUpdate(hWnd
, info
->layout
, g_layout_info
, LAYOUT_INFO_COUNT
);
1000 case BFFM_SETSTATUSTEXTA
:
1001 TRACE("Set status %s\n", debugstr_a((LPSTR
)lParam
));
1002 SetWindowTextA(GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_STATUS
), (LPSTR
)lParam
);
1005 case BFFM_SETSTATUSTEXTW
:
1006 TRACE("Set status %s\n", debugstr_w((LPWSTR
)lParam
));
1007 SetWindowTextW(GetDlgItem(hWnd
, IDC_BROWSE_FOR_FOLDER_STATUS
), (LPWSTR
)lParam
);
1011 TRACE("Enable %ld\n", lParam
);
1012 EnableWindow(GetDlgItem(hWnd
, 1), (lParam
)?TRUE
:FALSE
);
1015 case BFFM_SETOKTEXT
: /* unicode only */
1016 TRACE("Set OK text %s\n", debugstr_w((LPWSTR
)wParam
));
1017 SetWindowTextW(GetDlgItem(hWnd
, 1), (LPWSTR
)wParam
);
1020 case BFFM_SETSELECTIONA
:
1021 return BrsFolder_OnSetSelectionA(info
, (LPVOID
)lParam
, (BOOL
)wParam
);
1023 case BFFM_SETSELECTIONW
:
1024 return BrsFolder_OnSetSelectionW(info
, (LPVOID
)lParam
, (BOOL
)wParam
);
1026 case BFFM_SETEXPANDED
: /* unicode only */
1027 return BrsFolder_OnSetExpanded(info
, (LPVOID
)lParam
, (BOOL
)wParam
, NULL
);
1030 return BrsFolder_OnDestroy(info
);
1038 /*************************************************************************
1039 * SHBrowseForFolderA [SHELL32.@]
1040 * SHBrowseForFolder [SHELL32.@]
1042 LPITEMIDLIST WINAPI
SHBrowseForFolderA (LPBROWSEINFOA lpbi
)
1049 TRACE("%p\n", lpbi
);
1051 bi
.hwndOwner
= lpbi
->hwndOwner
;
1052 bi
.pidlRoot
= lpbi
->pidlRoot
;
1053 if (lpbi
->pszDisplayName
)
1055 bi
.pszDisplayName
= (WCHAR
*)HeapAlloc( GetProcessHeap(), 0, MAX_PATH
* sizeof(WCHAR
) );
1056 MultiByteToWideChar( CP_ACP
, 0, lpbi
->pszDisplayName
, -1, bi
.pszDisplayName
, MAX_PATH
);
1059 bi
.pszDisplayName
= NULL
;
1061 if (lpbi
->lpszTitle
)
1063 len
= MultiByteToWideChar( CP_ACP
, 0, lpbi
->lpszTitle
, -1, NULL
, 0 );
1064 title
= (WCHAR
*)HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1065 MultiByteToWideChar( CP_ACP
, 0, lpbi
->lpszTitle
, -1, title
, len
);
1070 bi
.lpszTitle
= title
;
1071 bi
.ulFlags
= lpbi
->ulFlags
;
1072 bi
.lpfn
= lpbi
->lpfn
;
1073 bi
.lParam
= lpbi
->lParam
;
1074 bi
.iImage
= lpbi
->iImage
;
1075 lpid
= SHBrowseForFolderW( &bi
);
1076 if (bi
.pszDisplayName
)
1078 WideCharToMultiByte( CP_ACP
, 0, bi
.pszDisplayName
, -1,
1079 lpbi
->pszDisplayName
, MAX_PATH
, 0, NULL
);
1080 HeapFree( GetProcessHeap(), 0, bi
.pszDisplayName
);
1082 HeapFree(GetProcessHeap(), 0, title
);
1083 lpbi
->iImage
= bi
.iImage
;
1088 /*************************************************************************
1089 * SHBrowseForFolderW [SHELL32.@]
1092 * crashes when passed a null pointer
1094 LPITEMIDLIST WINAPI
SHBrowseForFolderW (LPBROWSEINFOW lpbi
)
1102 info
.pidlRet
= NULL
;
1103 info
.lpBrowseInfo
= lpbi
;
1104 info
.hwndTreeView
= NULL
;
1106 hr
= OleInitialize(NULL
);
1108 if (lpbi
->ulFlags
& BIF_NEWDIALOGSTYLE
)
1109 wDlgId
= IDD_BROWSE_FOR_FOLDER_NEW
;
1111 wDlgId
= IDD_BROWSE_FOR_FOLDER
;
1112 r
= DialogBoxParamW( shell32_hInstance
, MAKEINTRESOURCEW(wDlgId
), lpbi
->hwndOwner
,
1113 BrsFolderDlgProc
, (LPARAM
)&info
);
1118 ILFree(info
.pidlRet
);
1122 return info
.pidlRet
;