kernel32: Add message resource for TRUST_E_NOSIGNATURE.
[wine.git] / dlls / shell32 / brsfolder.c
blobfc7b5b6b741b49ae2e545fda9c0593895aa92cbf
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 editbox
24 #include <stdlib.h>
25 #include <string.h>
27 #define COBJMACROS
28 #define NONAMELESSUNION
30 #include "wine/debug.h"
31 #include "undocshell.h"
32 #include "commoncontrols.h"
33 #include "pidl.h"
34 #include "shell32_main.h"
35 #include "shellapi.h"
36 #include "shresdef.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;
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 ILFree(pidlChild);
232 ILFree(pidlParent);
233 return;
235 hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
236 IShellFolder_Release(lpsfDesktop);
239 if (FAILED(hr)) {
240 WARN("Could not bind to parent shell folder! hr = %08x\n", hr);
241 ILFree(pidlChild);
242 ILFree(pidlParent);
243 return;
246 if (!_ILIsEmpty(pidlChild)) {
247 hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
248 } else {
249 lpsfRoot = lpsfParent;
250 hr = IShellFolder_AddRef(lpsfParent);
253 if (FAILED(hr)) {
254 WARN("Could not bind to root shell folder! hr = %08x\n", hr);
255 IShellFolder_Release(lpsfParent);
256 ILFree(pidlChild);
257 ILFree(pidlParent);
258 return;
261 flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
262 hr = IShellFolder_EnumObjects( lpsfRoot, info->hWnd, flags, &pEnumChildren );
263 if (FAILED(hr)) {
264 WARN("Could not get child iterator! hr = %08x\n", hr);
265 IShellFolder_Release(lpsfParent);
266 IShellFolder_Release(lpsfRoot);
267 ILFree(pidlChild);
268 ILFree(pidlParent);
269 return;
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 );
277 ILFree(pidlChild);
278 ILFree(pidlParent);
279 IShellFolder_Release(lpsfRoot);
280 IShellFolder_Release(lpsfParent);
283 static int GetIcon(LPCITEMIDLIST lpi, UINT uFlags)
285 SHFILEINFOW sfi;
286 IImageList *list;
288 list = (IImageList *)SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
289 if (list) IImageList_Release(list);
290 return sfi.iIcon;
293 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
295 LPITEMIDLIST pidlDesktop = NULL;
296 DWORD flags;
298 TRACE("%p %p\n",lpifq, lpTV_ITEM);
300 if (!lpifq)
302 pidlDesktop = _ILCreateDesktop();
303 lpifq = pidlDesktop;
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 );
312 if (pidlDesktop)
313 ILFree( pidlDesktop );
316 /******************************************************************************
317 * GetName [Internal]
319 * Query a shell folder for the display name of one of its children
321 * PARAMS
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
327 * RETURNS
328 * Success: TRUE
329 * Failure: FALSE
331 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
333 BOOL bSuccess=TRUE;
334 STRRET str;
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);
339 else
340 bSuccess = FALSE;
342 TRACE("-- %s\n", debugstr_w(lpFriendlyName));
343 return bSuccess;
346 /******************************************************************************
347 * InsertTreeViewItem [Internal]
349 * PARAMS
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
357 * RETURNS
358 * Success: Handle to the created and inserted treeview-item
359 * Failure: NULL
361 static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
362 LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
363 HTREEITEM hParent)
365 TVITEMW tvi;
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))
376 return NULL;
378 lptvid = SHAlloc( sizeof(TV_ITEMDATA) );
379 if (!lptvid)
380 return NULL;
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);
393 tvins.u.item = 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
406 * PARAMS
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;
417 ULONG ulFetched;
418 HRESULT hr;
419 HWND hwnd = GetParent( info->hwndTreeView );
421 TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
423 /* No IEnumIDList -> No children */
424 if (!lpe) return;
426 SetCapture( hwnd );
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);
438 if (SUCCEEDED(hr))
440 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
441 hr = IShellFolder_EnumObjects(pSFChild, hwnd, flags, &pEnumIL);
442 if (hr == S_OK)
444 if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) ||
445 FAILED(IEnumIDList_Reset(pEnumIL)))
447 IEnumIDList_Release(pEnumIL);
448 pEnumIL = NULL;
451 IShellFolder_Release(pSFChild);
455 if (!InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent))
456 goto done;
457 SHFree(pidlTemp); /* Finally, free the pidl that the shell gave us... */
458 pidlTemp=NULL;
461 done:
462 ReleaseCapture();
463 SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
464 SHFree(pidlTemp);
467 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
469 LPPIDLDATA data = _ILGetDataPointer(pidl);
470 if (!data)
471 return FALSE;
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;
480 DWORD dwAttributes;
481 HRESULT r;
483 if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
484 !PIDLIsType(pidl, PT_COMP))
485 bEnabled = FALSE;
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)))
492 bEnabled = FALSE;
495 dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
496 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
497 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
498 if (FAILED(r) ||
499 ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
501 if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
502 bEnabled = FALSE;
503 EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), FALSE);
505 else
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);
518 if (lptvid->pEnumIL)
519 IEnumIDList_Release(lptvid->pEnumIL);
520 SHFree(lptvid->lpi);
521 SHFree(lptvid->lpifq);
522 SHFree(lptvid);
523 return 0;
526 static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
528 IShellFolder *lpsf2 = NULL;
529 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
530 HRESULT r;
532 TRACE("TVN_ITEMEXPANDINGA/W\n");
534 if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
535 return 0;
537 if (!_ILIsEmpty(lptvid->lpi)) {
538 r = IShellFolder_BindToObject( lptvid->lpsfParent, lptvid->lpi, 0,
539 &IID_IShellFolder, (void**)&lpsf2 );
540 } else {
541 lpsf2 = lptvid->lpsfParent;
542 IShellFolder_AddRef(lpsf2);
543 r = S_OK;
546 if (SUCCEEDED(r))
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 );
558 return 0;
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 );
575 return S_OK;
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;
582 NMTREEVIEWW nmtv;
583 TVITEMW item;
585 if(!pnmtv->item.pszText)
586 return 0;
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, '\\')))
595 return 0;
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))
601 return 0;
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);
615 return 0;
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)
625 return 0;
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 );
645 default:
646 WARN("unhandled (%d)\n", pnmtv->hdr.code);
647 break;
650 return 0;
654 static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
656 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
658 info->hWnd = hWnd;
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)
668 RECT rcWnd;
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;
677 else
679 info->layout = NULL;
682 if (lpBrowseInfo->lpszTitle)
683 SetWindowTextW( GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle );
684 else
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))
712 RECT rc;
713 GetClientRect(info->hwndTreeView, &rc);
714 SetWindowPos(info->hwndTreeView, HWND_TOP, 0, 0,
715 rc.right, rc.bottom + 40, SWP_NOMOVE);
718 else
719 ERR("treeview control missing!\n");
721 browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
723 return TRUE;
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);
730 return S_OK;
733 static HRESULT BrsFolder_NewFolder(browse_info *info)
735 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
736 IShellFolder *desktop, *cur;
737 ISFHelper *sfhelper;
738 WCHAR name[MAX_PATH];
739 HTREEITEM parent, added;
740 LPTV_ITEMDATA item_data;
741 LPITEMIDLIST new_item;
742 TVITEMW item;
743 HRESULT hr;
744 int len;
746 if(!info->pidlRet) {
747 ERR("Make new folder button should be disabled\n");
748 return E_FAIL;
751 /* Create new directory */
752 hr = SHGetDesktopFolder(&desktop);
753 if(FAILED(hr))
754 return hr;
755 hr = IShellFolder_BindToObject(desktop, info->pidlRet, 0, &IID_IShellFolder, (void**)&cur);
756 IShellFolder_Release(desktop);
757 if(FAILED(hr))
758 return hr;
760 hr = IShellFolder_QueryInterface(cur, &IID_ISFHelper, (void**)&sfhelper);
761 if(FAILED(hr))
762 return hr;
764 if(!SHGetPathFromIDListW(info->pidlRet, name)) {
765 hr = E_FAIL;
766 goto cleanup;
769 len = strlenW(name);
770 if(len<MAX_PATH)
771 name[len++] = '\\';
772 hr = ISFHelper_GetUniqueName(sfhelper, &name[len], MAX_PATH-len);
773 ISFHelper_Release(sfhelper);
774 if(FAILED(hr))
775 goto cleanup;
777 hr = E_FAIL;
778 if(!CreateDirectoryW(name, NULL))
779 goto cleanup;
781 /* Update parent of newly created directory */
782 parent = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
783 if(!parent)
784 goto cleanup;
786 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)parent);
788 memset(&item, 0, sizeof(TVITEMW));
789 item.mask = TVIF_PARAM|TVIF_STATE;
790 item.hItem = parent;
791 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
792 item_data = (LPTV_ITEMDATA)item.lParam;
793 if(!item_data)
794 goto cleanup;
796 if(item_data->pEnumIL)
797 IEnumIDList_Release(item_data->pEnumIL);
798 hr = IShellFolder_EnumObjects(cur, info->hwndTreeView, flags, &item_data->pEnumIL);
799 if(FAILED(hr))
800 goto cleanup;
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);
811 if(FAILED(hr))
812 goto cleanup;
814 added = InsertTreeViewItem(info, cur, new_item, item_data->lpifq, NULL, parent);
815 IShellFolder_Release(cur);
816 SHFree(new_item);
818 SendMessageW(info->hwndTreeView, TVM_SORTCHILDREN, FALSE, (LPARAM)parent);
819 return BrsFolder_Rename(info, added);
821 cleanup:
822 return hr;
825 static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
827 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
829 switch (id)
831 case IDOK:
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 );
838 return TRUE;
840 case IDCANCEL:
841 EndDialog( info->hWnd, 0 );
842 return TRUE;
844 case IDD_MAKENEWFOLDER:
845 BrsFolder_NewFolder(info);
846 return TRUE;
848 return FALSE;
851 static BOOL BrsFolder_OnSetExpanded(browse_info *info, LPVOID selection,
852 BOOL is_str, HTREEITEM *pItem)
854 LPITEMIDLIST pidlSelection = selection;
855 LPCITEMIDLIST pidlCurrent, pidlRoot;
856 TVITEMEXW item;
857 BOOL bResult = FALSE;
859 memset(&item, 0, sizeof(item));
861 /* If 'selection' is a string, convert to a Shell ID List. */
862 if (is_str) {
863 IShellFolder *psfDesktop;
864 HRESULT hr;
866 hr = SHGetDesktopFolder(&psfDesktop);
867 if (FAILED(hr))
868 goto done;
870 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL,
871 selection, NULL, &pidlSelection, NULL);
872 IShellFolder_Release(psfDesktop);
873 if (FAILED(hr))
874 goto done;
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))
888 goto done;
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);
894 if (item.hItem)
895 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
896 (LPARAM)item.hItem);
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,
912 (LPARAM)item.hItem);
914 } else {
915 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT,
916 (LPARAM)item.hItem);
920 if (_ILIsEmpty(pidlCurrent) && item.hItem)
921 bResult = TRUE;
923 done:
924 if (pidlSelection && pidlSelection != selection)
925 ILFree(pidlSelection);
927 if (pItem)
928 *pItem = item.hItem;
930 return bResult;
933 static BOOL BrsFolder_OnSetSelectionW(browse_info *info, LPVOID selection, BOOL is_str) {
934 HTREEITEM hItem;
935 BOOL bResult;
937 if (!selection) return FALSE;
939 bResult = BrsFolder_OnSetExpanded(info, selection, is_str, &hItem);
940 if (bResult)
941 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem );
942 return bResult;
945 static BOOL BrsFolder_OnSetSelectionA(browse_info *info, LPVOID selection, BOOL is_str) {
946 LPWSTR selectionW = NULL;
947 BOOL result = FALSE;
948 int length;
950 if (!is_str)
951 return BrsFolder_OnSetSelectionW(info, selection, is_str);
953 if ((length = MultiByteToWideChar(CP_ACP, 0, selection, -1, NULL, 0)) &&
954 (selectionW = heap_alloc(length * sizeof(WCHAR))) &&
955 MultiByteToWideChar(CP_ACP, 0, selection, -1, selectionW, length))
957 result = BrsFolder_OnSetSelectionW(info, selectionW, is_str);
960 heap_free(selectionW);
961 return result;
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;
973 return 0;
976 static INT BrsFolder_OnDestroy(browse_info *info)
978 if (info->layout)
980 SHFree(info->layout);
981 info->layout = NULL;
984 return 0;
987 /*************************************************************************
988 * BrsFolderDlgProc32 (not an exported API function)
990 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
991 LPARAM lParam )
993 browse_info *info;
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 );
1002 switch (msg)
1004 case WM_NOTIFY:
1005 return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
1007 case WM_COMMAND:
1008 return BrsFolder_OnCommand( info, wParam );
1010 case WM_WINDOWPOSCHANGING:
1011 return BrsFolder_OnWindowPosChanging( info, (WINDOWPOS *)lParam);
1013 case WM_SIZE:
1014 if (info->layout) /* new style dialogs */
1015 LayoutUpdate(hWnd, info->layout, g_layout_info, LAYOUT_INFO_COUNT);
1016 return 0;
1018 case BFFM_SETSTATUSTEXTA:
1019 TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
1020 SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
1021 break;
1023 case BFFM_SETSTATUSTEXTW:
1024 TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
1025 SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
1026 break;
1028 case BFFM_ENABLEOK:
1029 TRACE("Enable %ld\n", lParam);
1030 EnableWindow(GetDlgItem(hWnd, 1), lParam != 0);
1031 break;
1033 case BFFM_SETOKTEXT: /* unicode only */
1034 TRACE("Set OK text %s\n", debugstr_w((LPWSTR)lParam));
1035 SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)lParam);
1036 break;
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);
1047 case WM_DESTROY:
1048 return BrsFolder_OnDestroy(info);
1050 return FALSE;
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)
1064 BROWSEINFOW bi;
1065 LPITEMIDLIST lpid;
1066 INT len;
1067 LPWSTR title;
1069 TRACE("%p\n", lpbi);
1071 bi.hwndOwner = lpbi->hwndOwner;
1072 bi.pidlRoot = lpbi->pidlRoot;
1073 if (lpbi->pszDisplayName)
1074 bi.pszDisplayName = heap_alloc( MAX_PATH * sizeof(WCHAR) );
1075 else
1076 bi.pszDisplayName = NULL;
1078 if (lpbi->lpszTitle)
1080 len = MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0 );
1081 title = heap_alloc( len * sizeof(WCHAR) );
1082 MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, title, len );
1084 else
1085 title = NULL;
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 heap_free( bi.pszDisplayName );
1099 heap_free(title);
1100 lpbi->iImage = bi.iImage;
1101 return lpid;
1105 /*************************************************************************
1106 * SHBrowseForFolderW [SHELL32.@]
1108 * NOTES
1109 * crashes when passed a null pointer
1111 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
1113 browse_info info;
1114 DWORD r;
1115 HRESULT hr;
1116 const WCHAR * templateName;
1117 INITCOMMONCONTROLSEX icex;
1119 info.hWnd = 0;
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;
1132 else
1133 templateName = swBrowseTemplateName;
1134 r = DialogBoxParamW( shell32_hInstance, templateName, lpbi->hwndOwner,
1135 BrsFolderDlgProc, (LPARAM)&info );
1136 if (SUCCEEDED(hr))
1137 OleUninitialize();
1138 if (!r)
1140 ILFree(info.pidlRet);
1141 return NULL;
1144 return info.pidlRet;