webservices: Add a stub implementation of WS_TYPE_ATTRIBUTE_FIELD_MAPPING in the...
[wine.git] / dlls / shell32 / brsfolder.c
blob404040765094f735918b6da73fec0dc019ccf4ee
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 "pidl.h"
33 #include "shell32_main.h"
34 #include "shellapi.h"
35 #include "shresdef.h"
36 #include "shellfolder.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(shell);
40 /* original margins and control size */
41 typedef struct tagLAYOUT_DATA
43 LONG left, width, right;
44 LONG top, height, bottom;
45 } LAYOUT_DATA;
47 typedef struct tagbrowse_info
49 HWND hWnd;
50 HWND hwndTreeView;
51 LPBROWSEINFOW lpBrowseInfo;
52 LPITEMIDLIST pidlRet;
53 LAYOUT_DATA *layout; /* filled by LayoutInit, used by LayoutUpdate */
54 SIZE szMin;
55 } browse_info;
57 typedef struct tagTV_ITEMDATA
59 LPSHELLFOLDER lpsfParent; /* IShellFolder of the parent */
60 LPITEMIDLIST lpi; /* PIDL relative to parent */
61 LPITEMIDLIST lpifq; /* Fully qualified PIDL */
62 IEnumIDList* pEnumIL; /* Children iterator */
63 } TV_ITEMDATA, *LPTV_ITEMDATA;
65 typedef struct tagLAYOUT_INFO
67 int iItemId; /* control id */
68 DWORD dwAnchor; /* BF_* flags specifying which margins should remain constant */
69 } LAYOUT_INFO;
71 static const LAYOUT_INFO g_layout_info[] =
73 {IDD_TITLE, BF_TOP|BF_LEFT|BF_RIGHT},
74 {IDD_STATUS, BF_TOP|BF_LEFT|BF_RIGHT},
75 {IDD_FOLDER, BF_TOP|BF_LEFT|BF_RIGHT},
76 {IDD_TREEVIEW, BF_TOP|BF_BOTTOM|BF_LEFT|BF_RIGHT},
77 {IDD_FOLDER, BF_BOTTOM|BF_LEFT},
78 {IDD_FOLDERTEXT, BF_BOTTOM|BF_LEFT|BF_RIGHT},
79 {IDD_MAKENEWFOLDER, BF_BOTTOM|BF_LEFT},
80 {IDOK, BF_BOTTOM|BF_RIGHT},
81 {IDCANCEL, BF_BOTTOM|BF_RIGHT}
84 #define LAYOUT_INFO_COUNT (sizeof(g_layout_info)/sizeof(g_layout_info[0]))
86 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
87 BIF_BROWSEFORCOMPUTER | \
88 BIF_RETURNFSANCESTORS | \
89 BIF_RETURNONLYFSDIRS | \
90 BIF_NONEWFOLDERBUTTON | \
91 BIF_NEWDIALOGSTYLE | \
92 BIF_BROWSEINCLUDEFILES)
94 static void FillTreeView(browse_info*, LPSHELLFOLDER,
95 LPITEMIDLIST, HTREEITEM, IEnumIDList*);
96 static HTREEITEM InsertTreeViewItem( browse_info*, IShellFolder *,
97 LPCITEMIDLIST, LPCITEMIDLIST, IEnumIDList*, HTREEITEM);
99 static const WCHAR szBrowseFolderInfo[] = {
100 '_','_','W','I','N','E','_',
101 'B','R','S','F','O','L','D','E','R','D','L','G','_',
102 'I','N','F','O',0
105 static inline DWORD BrowseFlagsToSHCONTF(UINT ulFlags)
107 return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
110 static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo, HWND hWnd,
111 UINT msg, LPARAM param )
113 if (!lpBrowseInfo->lpfn)
114 return;
115 lpBrowseInfo->lpfn( hWnd, msg, param, lpBrowseInfo->lParam );
118 static LAYOUT_DATA *LayoutInit(HWND hwnd, const LAYOUT_INFO *layout_info, int layout_count)
120 LAYOUT_DATA *data;
121 RECT rcWnd;
122 int i;
124 GetClientRect(hwnd, &rcWnd);
125 data = SHAlloc(sizeof(LAYOUT_DATA)*layout_count);
126 for (i = 0; i < layout_count; i++)
128 RECT r;
129 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
131 if (hItem == NULL)
132 ERR("Item %d not found\n", i);
133 GetWindowRect(hItem, &r);
134 MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);
136 data[i].left = r.left;
137 data[i].right = rcWnd.right - r.right;
138 data[i].width = r.right - r.left;
140 data[i].top = r.top;
141 data[i].bottom = rcWnd.bottom - r.bottom;
142 data[i].height = r.bottom - r.top;
144 return data;
147 static void LayoutUpdate(HWND hwnd, LAYOUT_DATA *data, const LAYOUT_INFO *layout_info, int layout_count)
149 RECT rcWnd;
150 int i;
152 GetClientRect(hwnd, &rcWnd);
153 for (i = 0; i < layout_count; i++)
155 RECT r;
156 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
158 GetWindowRect(hItem, &r);
159 MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);
161 if (layout_info[i].dwAnchor & BF_RIGHT)
163 r.right = rcWnd.right - data[i].right;
164 if (!(layout_info[i].dwAnchor & BF_LEFT))
165 r.left = r.right - data[i].width;
168 if (layout_info[i].dwAnchor & BF_BOTTOM)
170 r.bottom = rcWnd.bottom - data[i].bottom;
171 if (!(layout_info[i].dwAnchor & BF_TOP))
172 r.top = r.bottom - data[i].height;
175 SetWindowPos(hItem, NULL, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOZORDER);
180 /******************************************************************************
181 * InitializeTreeView [Internal]
183 * Called from WM_INITDIALOG handler.
185 * PARAMS
186 * hwndParent [I] The BrowseForFolder dialog
187 * root [I] ITEMIDLIST of the root shell folder
189 static void InitializeTreeView( browse_info *info )
191 LPITEMIDLIST pidlParent, pidlChild;
192 HIMAGELIST hImageList;
193 HRESULT hr;
194 IShellFolder *lpsfParent, *lpsfRoot;
195 IEnumIDList * pEnumChildren = NULL;
196 HTREEITEM item;
197 DWORD flags;
198 LPCITEMIDLIST root = info->lpBrowseInfo->pidlRoot;
200 TRACE("%p\n", info );
202 Shell_GetImageLists(NULL, &hImageList);
204 if (hImageList)
205 SendMessageW( info->hwndTreeView, TVM_SETIMAGELIST, 0, (LPARAM)hImageList );
207 /* We want to call InsertTreeViewItem down the code, in order to insert
208 * the root item of the treeview. Due to InsertTreeViewItem's signature,
209 * we need the following to do this:
211 * + An ITEMIDLIST corresponding to _the parent_ of root.
212 * + An ITEMIDLIST, which is a relative path from root's parent to root
213 * (containing a single SHITEMID).
214 * + An IShellFolder interface pointer of root's parent folder.
216 * If root is 'Desktop', then root's parent is also 'Desktop'.
219 pidlParent = ILClone(root);
220 ILRemoveLastID(pidlParent);
221 pidlChild = ILClone(ILFindLastID(root));
223 if (_ILIsDesktop(pidlParent)) {
224 hr = SHGetDesktopFolder(&lpsfParent);
225 } else {
226 IShellFolder *lpsfDesktop;
227 hr = SHGetDesktopFolder(&lpsfDesktop);
228 if (FAILED(hr)) {
229 WARN("SHGetDesktopFolder failed! hr = %08x\n", hr);
230 ILFree(pidlChild);
231 ILFree(pidlParent);
232 return;
234 hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
235 IShellFolder_Release(lpsfDesktop);
238 if (FAILED(hr)) {
239 WARN("Could not bind to parent shell folder! hr = %08x\n", hr);
240 ILFree(pidlChild);
241 ILFree(pidlParent);
242 return;
245 if (!_ILIsEmpty(pidlChild)) {
246 hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
247 } else {
248 lpsfRoot = lpsfParent;
249 hr = IShellFolder_AddRef(lpsfParent);
252 if (FAILED(hr)) {
253 WARN("Could not bind to root shell folder! hr = %08x\n", hr);
254 IShellFolder_Release(lpsfParent);
255 ILFree(pidlChild);
256 ILFree(pidlParent);
257 return;
260 flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
261 hr = IShellFolder_EnumObjects( lpsfRoot, info->hWnd, flags, &pEnumChildren );
262 if (FAILED(hr)) {
263 WARN("Could not get child iterator! hr = %08x\n", hr);
264 IShellFolder_Release(lpsfParent);
265 IShellFolder_Release(lpsfRoot);
266 ILFree(pidlChild);
267 ILFree(pidlParent);
268 return;
271 SendMessageW( info->hwndTreeView, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT );
272 item = InsertTreeViewItem( info, lpsfParent, pidlChild,
273 pidlParent, pEnumChildren, TVI_ROOT );
274 SendMessageW( info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item );
276 ILFree(pidlChild);
277 ILFree(pidlParent);
278 IShellFolder_Release(lpsfRoot);
279 IShellFolder_Release(lpsfParent);
282 static int GetIcon(LPCITEMIDLIST lpi, UINT uFlags)
284 SHFILEINFOW sfi;
285 SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
286 return sfi.iIcon;
289 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
291 LPITEMIDLIST pidlDesktop = NULL;
292 DWORD flags;
294 TRACE("%p %p\n",lpifq, lpTV_ITEM);
296 if (!lpifq)
298 pidlDesktop = _ILCreateDesktop();
299 lpifq = pidlDesktop;
302 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
303 lpTV_ITEM->iImage = GetIcon( lpifq, flags );
305 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
306 lpTV_ITEM->iSelectedImage = GetIcon( lpifq, flags );
308 if (pidlDesktop)
309 ILFree( pidlDesktop );
312 /******************************************************************************
313 * GetName [Internal]
315 * Query a shell folder for the display name of one of its children
317 * PARAMS
318 * lpsf [I] IShellFolder interface of the folder to be queried.
319 * lpi [I] ITEMIDLIST of the child, relative to parent
320 * dwFlags [I] as in IShellFolder::GetDisplayNameOf
321 * lpFriendlyName [O] The desired display name in unicode
323 * RETURNS
324 * Success: TRUE
325 * Failure: FALSE
327 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
329 BOOL bSuccess=TRUE;
330 STRRET str;
332 TRACE("%p %p %x %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
333 if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
334 bSuccess = StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi);
335 else
336 bSuccess = FALSE;
338 TRACE("-- %s\n", debugstr_w(lpFriendlyName));
339 return bSuccess;
342 /******************************************************************************
343 * InsertTreeViewItem [Internal]
345 * PARAMS
346 * info [I] data for the dialog
347 * lpsf [I] IShellFolder interface of the item's parent shell folder
348 * pidl [I] ITEMIDLIST of the child to insert, relative to parent
349 * pidlParent [I] ITEMIDLIST of the parent shell folder
350 * pEnumIL [I] Iterator for the children of the item to be inserted
351 * hParent [I] The treeview-item that represents the parent shell folder
353 * RETURNS
354 * Success: Handle to the created and inserted treeview-item
355 * Failure: NULL
357 static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
358 LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
359 HTREEITEM hParent)
361 TVITEMW tvi;
362 TVINSERTSTRUCTW tvins;
363 WCHAR szBuff[MAX_PATH];
364 LPTV_ITEMDATA lptvid=0;
366 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
368 tvi.cChildren= pEnumIL ? 1 : 0;
369 tvi.mask |= TVIF_CHILDREN;
371 if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
372 return NULL;
374 lptvid = SHAlloc( sizeof(TV_ITEMDATA) );
375 if (!lptvid)
376 return NULL;
378 tvi.pszText = szBuff;
379 tvi.cchTextMax = MAX_PATH;
380 tvi.lParam = (LPARAM)lptvid;
382 IShellFolder_AddRef(lpsf);
383 lptvid->lpsfParent = lpsf;
384 lptvid->lpi = ILClone(pidl);
385 lptvid->lpifq = pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
386 lptvid->pEnumIL = pEnumIL;
387 GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
389 tvins.u.item = tvi;
390 tvins.hInsertAfter = NULL;
391 tvins.hParent = hParent;
393 return TreeView_InsertItemW( info->hwndTreeView, &tvins );
396 /******************************************************************************
397 * FillTreeView [Internal]
399 * For each child (given by lpe) of the parent shell folder, which is given by
400 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
402 * PARAMS
403 * info [I] data for the dialog
404 * lpsf [I] IShellFolder interface of the parent shell folder
405 * pidl [I] ITEMIDLIST of the parent shell folder
406 * hParent [I] The treeview item that represents the parent shell folder
407 * lpe [I] An iterator for the children of the parent shell folder
409 static void FillTreeView( browse_info *info, IShellFolder * lpsf,
410 LPITEMIDLIST pidl, HTREEITEM hParent, IEnumIDList* lpe)
412 LPITEMIDLIST pidlTemp = 0;
413 ULONG ulFetched;
414 HRESULT hr;
415 HWND hwnd = GetParent( info->hwndTreeView );
417 TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
419 /* No IEnumIDList -> No children */
420 if (!lpe) return;
422 SetCapture( hwnd );
423 SetCursor( LoadCursorA( 0, (LPSTR)IDC_WAIT ) );
425 while (S_OK == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
427 ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
428 IEnumIDList* pEnumIL = NULL;
429 IShellFolder* pSFChild = NULL;
430 IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
431 if (ulAttrs & SFGAO_FOLDER)
433 hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
434 if (SUCCEEDED(hr))
436 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
437 hr = IShellFolder_EnumObjects(pSFChild, hwnd, flags, &pEnumIL);
438 if (hr == S_OK)
440 if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) ||
441 FAILED(IEnumIDList_Reset(pEnumIL)))
443 IEnumIDList_Release(pEnumIL);
444 pEnumIL = NULL;
447 IShellFolder_Release(pSFChild);
451 if (!InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent))
452 goto done;
453 SHFree(pidlTemp); /* Finally, free the pidl that the shell gave us... */
454 pidlTemp=NULL;
457 done:
458 ReleaseCapture();
459 SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
460 SHFree(pidlTemp);
463 static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
465 LPPIDLDATA data = _ILGetDataPointer(pidl);
466 if (!data)
467 return FALSE;
468 return (data->type == type);
471 static void BrsFolder_CheckValidSelection( browse_info *info, LPTV_ITEMDATA lptvid )
473 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
474 LPCITEMIDLIST pidl = lptvid->lpi;
475 BOOL bEnabled = TRUE;
476 DWORD dwAttributes;
477 HRESULT r;
479 if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
480 !PIDLIsType(pidl, PT_COMP))
481 bEnabled = FALSE;
482 if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
484 dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
485 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
486 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
487 if (FAILED(r) || !(dwAttributes & (SFGAO_FILESYSANCESTOR|SFGAO_FILESYSTEM)))
488 bEnabled = FALSE;
491 dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
492 r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
493 (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
494 if (FAILED(r) ||
495 ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
497 if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
498 bEnabled = FALSE;
499 EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), FALSE);
501 else
502 EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), TRUE);
504 SendMessageW(info->hWnd, BFFM_ENABLEOK, 0, bEnabled);
507 static LRESULT BrsFolder_Treeview_Delete( browse_info *info, NMTREEVIEWW *pnmtv )
509 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA)pnmtv->itemOld.lParam;
511 TRACE("TVN_DELETEITEMA/W %p\n", lptvid);
513 IShellFolder_Release(lptvid->lpsfParent);
514 if (lptvid->pEnumIL)
515 IEnumIDList_Release(lptvid->pEnumIL);
516 SHFree(lptvid->lpi);
517 SHFree(lptvid->lpifq);
518 SHFree(lptvid);
519 return 0;
522 static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
524 IShellFolder *lpsf2 = NULL;
525 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
526 HRESULT r;
528 TRACE("TVN_ITEMEXPANDINGA/W\n");
530 if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
531 return 0;
533 if (!_ILIsEmpty(lptvid->lpi)) {
534 r = IShellFolder_BindToObject( lptvid->lpsfParent, lptvid->lpi, 0,
535 &IID_IShellFolder, (void**)&lpsf2 );
536 } else {
537 lpsf2 = lptvid->lpsfParent;
538 IShellFolder_AddRef(lpsf2);
539 r = S_OK;
542 if (SUCCEEDED(r))
544 FillTreeView( info, lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
545 IShellFolder_Release( lpsf2 );
548 /* My Computer is already sorted and trying to do a simple text
549 * sort will only mess things up */
550 if (!_ILIsMyComputer(lptvid->lpi))
551 SendMessageW( info->hwndTreeView, TVM_SORTCHILDREN,
552 FALSE, (LPARAM)pnmtv->itemNew.hItem );
554 return 0;
557 static HRESULT BrsFolder_Treeview_Changed( browse_info *info, NMTREEVIEWW *pnmtv )
559 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
560 WCHAR name[MAX_PATH];
562 ILFree(info->pidlRet);
563 info->pidlRet = ILClone(lptvid->lpifq);
565 if (GetName(lptvid->lpsfParent, lptvid->lpi, SHGDN_NORMAL, name))
566 SetWindowTextW( GetDlgItem(info->hWnd, IDD_FOLDERTEXT), name );
568 browsefolder_callback( info->lpBrowseInfo, info->hWnd, BFFM_SELCHANGED,
569 (LPARAM)info->pidlRet );
570 BrsFolder_CheckValidSelection( info, lptvid );
571 return S_OK;
574 static LRESULT BrsFolder_Treeview_Rename(browse_info *info, NMTVDISPINFOW *pnmtv)
576 LPTV_ITEMDATA item_data;
577 WCHAR old_path[MAX_PATH], new_path[MAX_PATH], *p;
578 NMTREEVIEWW nmtv;
579 TVITEMW item;
581 if(!pnmtv->item.pszText)
582 return 0;
584 item.mask = TVIF_HANDLE|TVIF_PARAM;
585 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
586 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
587 item_data = (LPTV_ITEMDATA)item.lParam;
589 SHGetPathFromIDListW(item_data->lpifq, old_path);
590 if(!(p = strrchrW(old_path, '\\')))
591 return 0;
592 p = new_path+(p-old_path+1);
593 memcpy(new_path, old_path, (p-new_path)*sizeof(WCHAR));
594 strcpyW(p, pnmtv->item.pszText);
596 if(!MoveFileW(old_path, new_path))
597 return 0;
599 SHFree(item_data->lpifq);
600 SHFree(item_data->lpi);
601 item_data->lpifq = SHSimpleIDListFromPathW(new_path);
602 IShellFolder_ParseDisplayName(item_data->lpsfParent, NULL, NULL,
603 pnmtv->item.pszText, NULL, &item_data->lpi, NULL);
605 item.mask = TVIF_HANDLE|TVIF_TEXT;
606 item.pszText = pnmtv->item.pszText;
607 SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);
609 nmtv.itemNew.lParam = item.lParam;
610 BrsFolder_Treeview_Changed(info, &nmtv);
611 return 0;
614 static LRESULT BrsFolder_OnNotify( browse_info *info, UINT CtlID, LPNMHDR lpnmh )
616 NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
618 TRACE("%p %x %p msg=%x\n", info, CtlID, lpnmh, pnmtv->hdr.code);
620 if (pnmtv->hdr.idFrom != IDD_TREEVIEW)
621 return 0;
623 switch (pnmtv->hdr.code)
625 case TVN_DELETEITEMA:
626 case TVN_DELETEITEMW:
627 return BrsFolder_Treeview_Delete( info, pnmtv );
629 case TVN_ITEMEXPANDINGA:
630 case TVN_ITEMEXPANDINGW:
631 return BrsFolder_Treeview_Expand( info, pnmtv );
633 case TVN_SELCHANGEDA:
634 case TVN_SELCHANGEDW:
635 return BrsFolder_Treeview_Changed( info, pnmtv );
637 case TVN_ENDLABELEDITA:
638 case TVN_ENDLABELEDITW:
639 return BrsFolder_Treeview_Rename( info, (LPNMTVDISPINFOW)pnmtv );
641 default:
642 WARN("unhandled (%d)\n", pnmtv->hdr.code);
643 break;
646 return 0;
650 static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
652 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
654 info->hWnd = hWnd;
655 SetPropW( hWnd, szBrowseFolderInfo, info );
657 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
658 FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
659 if (lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS)
660 FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS);
662 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
664 RECT rcWnd;
666 info->layout = LayoutInit(hWnd, g_layout_info, LAYOUT_INFO_COUNT);
668 /* TODO: Windows allows shrinking the windows a bit */
669 GetWindowRect(hWnd, &rcWnd);
670 info->szMin.cx = rcWnd.right - rcWnd.left;
671 info->szMin.cy = rcWnd.bottom - rcWnd.top;
673 else
675 info->layout = NULL;
678 if (lpBrowseInfo->lpszTitle)
679 SetWindowTextW( GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle );
680 else
681 ShowWindow( GetDlgItem(hWnd, IDD_TITLE), SW_HIDE );
683 if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT)
684 || (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
685 ShowWindow( GetDlgItem(hWnd, IDD_STATUS), SW_HIDE );
687 /* Hide "Make New Folder" Button? */
688 if ((lpBrowseInfo->ulFlags & BIF_NONEWFOLDERBUTTON)
689 || !(lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
690 ShowWindow( GetDlgItem(hWnd, IDD_MAKENEWFOLDER), SW_HIDE );
692 /* Hide the editbox? */
693 if (!(lpBrowseInfo->ulFlags & BIF_EDITBOX))
695 ShowWindow( GetDlgItem(hWnd, IDD_FOLDER), SW_HIDE );
696 ShowWindow( GetDlgItem(hWnd, IDD_FOLDERTEXT), SW_HIDE );
699 info->hwndTreeView = GetDlgItem( hWnd, IDD_TREEVIEW );
700 if (info->hwndTreeView)
702 InitializeTreeView( info );
704 /* Resize the treeview if there's not editbox */
705 if ((lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
706 && !(lpBrowseInfo->ulFlags & BIF_EDITBOX))
708 RECT rc;
709 GetClientRect(info->hwndTreeView, &rc);
710 SetWindowPos(info->hwndTreeView, HWND_TOP, 0, 0,
711 rc.right, rc.bottom + 40, SWP_NOMOVE);
714 else
715 ERR("treeview control missing!\n");
717 browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
719 return TRUE;
722 static HRESULT BrsFolder_Rename(browse_info *info, HTREEITEM rename)
724 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)rename);
725 SendMessageW(info->hwndTreeView, TVM_EDITLABELW, 0, (LPARAM)rename);
726 return S_OK;
729 static HRESULT BrsFolder_NewFolder(browse_info *info)
731 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
732 IShellFolder *desktop, *cur;
733 ISFHelper *sfhelper;
734 WCHAR name[MAX_PATH];
735 HTREEITEM parent, added;
736 LPTV_ITEMDATA item_data;
737 LPITEMIDLIST new_item;
738 TVITEMW item;
739 HRESULT hr;
740 int len;
742 if(!info->pidlRet) {
743 ERR("Make new folder button should be disabled\n");
744 return E_FAIL;
747 /* Create new directory */
748 hr = SHGetDesktopFolder(&desktop);
749 if(FAILED(hr))
750 return hr;
751 hr = IShellFolder_BindToObject(desktop, info->pidlRet, 0, &IID_IShellFolder, (void**)&cur);
752 IShellFolder_Release(desktop);
753 if(FAILED(hr))
754 return hr;
756 hr = IShellFolder_QueryInterface(cur, &IID_ISFHelper, (void**)&sfhelper);
757 if(FAILED(hr))
758 return hr;
760 if(!SHGetPathFromIDListW(info->pidlRet, name)) {
761 hr = E_FAIL;
762 goto cleanup;
765 len = strlenW(name);
766 if(len<MAX_PATH)
767 name[len++] = '\\';
768 hr = ISFHelper_GetUniqueName(sfhelper, &name[len], MAX_PATH-len);
769 ISFHelper_Release(sfhelper);
770 if(FAILED(hr))
771 goto cleanup;
773 hr = E_FAIL;
774 if(!CreateDirectoryW(name, NULL))
775 goto cleanup;
777 /* Update parent of newly created directory */
778 parent = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
779 if(!parent)
780 goto cleanup;
782 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)parent);
784 memset(&item, 0, sizeof(TVITEMW));
785 item.mask = TVIF_PARAM|TVIF_STATE;
786 item.hItem = parent;
787 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
788 item_data = (LPTV_ITEMDATA)item.lParam;
789 if(!item_data)
790 goto cleanup;
792 if(item_data->pEnumIL)
793 IEnumIDList_Release(item_data->pEnumIL);
794 hr = IShellFolder_EnumObjects(cur, info->hwndTreeView, flags, &item_data->pEnumIL);
795 if(FAILED(hr))
796 goto cleanup;
798 /* Update treeview */
799 if(!(item.state&TVIS_EXPANDEDONCE)) {
800 item.mask = TVIF_STATE;
801 item.state = TVIS_EXPANDEDONCE;
802 item.stateMask = TVIS_EXPANDEDONCE;
803 SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);
806 hr = IShellFolder_ParseDisplayName(cur, NULL, NULL, name+len, NULL, &new_item, NULL);
807 if(FAILED(hr))
808 goto cleanup;
810 added = InsertTreeViewItem(info, cur, new_item, item_data->lpifq, NULL, parent);
811 IShellFolder_Release(cur);
812 SHFree(new_item);
814 SendMessageW(info->hwndTreeView, TVM_SORTCHILDREN, FALSE, (LPARAM)parent);
815 return BrsFolder_Rename(info, added);
817 cleanup:
818 return hr;
821 static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
823 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
825 switch (id)
827 case IDOK:
828 if (info->pidlRet == NULL) /* A null pidl would mean a cancel */
829 info->pidlRet = _ILCreateDesktop();
830 pdump( info->pidlRet );
831 if (lpBrowseInfo->pszDisplayName)
832 SHGetPathFromIDListW( info->pidlRet, lpBrowseInfo->pszDisplayName );
833 EndDialog( info->hWnd, 1 );
834 return TRUE;
836 case IDCANCEL:
837 EndDialog( info->hWnd, 0 );
838 return TRUE;
840 case IDD_MAKENEWFOLDER:
841 BrsFolder_NewFolder(info);
842 return TRUE;
844 return FALSE;
847 static BOOL BrsFolder_OnSetExpanded(browse_info *info, LPVOID selection,
848 BOOL is_str, HTREEITEM *pItem)
850 LPITEMIDLIST pidlSelection = selection;
851 LPCITEMIDLIST pidlCurrent, pidlRoot;
852 TVITEMEXW item;
853 BOOL bResult = FALSE;
855 memset(&item, 0, sizeof(item));
857 /* If 'selection' is a string, convert to a Shell ID List. */
858 if (is_str) {
859 IShellFolder *psfDesktop;
860 HRESULT hr;
862 hr = SHGetDesktopFolder(&psfDesktop);
863 if (FAILED(hr))
864 goto done;
866 hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL,
867 selection, NULL, &pidlSelection, NULL);
868 IShellFolder_Release(psfDesktop);
869 if (FAILED(hr))
870 goto done;
873 /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
874 * the sub-tree currently displayed. */
875 pidlRoot = info->lpBrowseInfo->pidlRoot;
876 pidlCurrent = pidlSelection;
877 while (!_ILIsEmpty(pidlRoot) && _ILIsEqualSimple(pidlRoot, pidlCurrent)) {
878 pidlRoot = ILGetNext(pidlRoot);
879 pidlCurrent = ILGetNext(pidlCurrent);
882 /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
883 if (!_ILIsEmpty(pidlRoot))
884 goto done;
886 /* Initialize item to point to the first child of the root folder. */
887 item.mask = TVIF_PARAM;
888 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
890 if (item.hItem)
891 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
892 (LPARAM)item.hItem);
894 /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
895 while (item.hItem && !_ILIsEmpty(pidlCurrent)) {
896 LPTV_ITEMDATA pItemData;
898 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
899 pItemData = (LPTV_ITEMDATA)item.lParam;
901 if (_ILIsEqualSimple(pItemData->lpi, pidlCurrent)) {
902 pidlCurrent = ILGetNext(pidlCurrent);
903 if (!_ILIsEmpty(pidlCurrent)) {
904 /* Only expand current node and move on to its first child,
905 * if we didn't already reach the last SHITEMID */
906 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item.hItem);
907 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
908 (LPARAM)item.hItem);
910 } else {
911 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT,
912 (LPARAM)item.hItem);
916 if (_ILIsEmpty(pidlCurrent) && item.hItem)
917 bResult = TRUE;
919 done:
920 if (pidlSelection && pidlSelection != selection)
921 ILFree(pidlSelection);
923 if (pItem)
924 *pItem = item.hItem;
926 return bResult;
929 static BOOL BrsFolder_OnSetSelectionW(browse_info *info, LPVOID selection, BOOL is_str) {
930 HTREEITEM hItem;
931 BOOL bResult;
933 if (!selection) return FALSE;
935 bResult = BrsFolder_OnSetExpanded(info, selection, is_str, &hItem);
936 if (bResult)
937 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem );
938 return bResult;
941 static BOOL BrsFolder_OnSetSelectionA(browse_info *info, LPVOID selection, BOOL is_str) {
942 LPWSTR selectionW = NULL;
943 BOOL result = FALSE;
944 int length;
946 if (!is_str)
947 return BrsFolder_OnSetSelectionW(info, selection, is_str);
949 if ((length = MultiByteToWideChar(CP_ACP, 0, selection, -1, NULL, 0)) &&
950 (selectionW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(WCHAR))) &&
951 MultiByteToWideChar(CP_ACP, 0, selection, -1, selectionW, length))
953 result = BrsFolder_OnSetSelectionW(info, selectionW, is_str);
956 HeapFree(GetProcessHeap(), 0, selectionW);
957 return result;
960 static LRESULT BrsFolder_OnWindowPosChanging(browse_info *info, WINDOWPOS *pos)
962 if ((info->lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE) && !(pos->flags & SWP_NOSIZE))
964 if (pos->cx < info->szMin.cx)
965 pos->cx = info->szMin.cx;
966 if (pos->cy < info->szMin.cy)
967 pos->cy = info->szMin.cy;
969 return 0;
972 static INT BrsFolder_OnDestroy(browse_info *info)
974 if (info->layout)
976 SHFree(info->layout);
977 info->layout = NULL;
980 return 0;
983 /*************************************************************************
984 * BrsFolderDlgProc32 (not an exported API function)
986 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
987 LPARAM lParam )
989 browse_info *info;
991 TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd, msg, wParam, lParam );
993 if (msg == WM_INITDIALOG)
994 return BrsFolder_OnCreate( hWnd, (browse_info*) lParam );
996 info = GetPropW( hWnd, szBrowseFolderInfo );
998 switch (msg)
1000 case WM_NOTIFY:
1001 return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
1003 case WM_COMMAND:
1004 return BrsFolder_OnCommand( info, wParam );
1006 case WM_WINDOWPOSCHANGING:
1007 return BrsFolder_OnWindowPosChanging( info, (WINDOWPOS *)lParam);
1009 case WM_SIZE:
1010 if (info->layout) /* new style dialogs */
1011 LayoutUpdate(hWnd, info->layout, g_layout_info, LAYOUT_INFO_COUNT);
1012 return 0;
1014 case BFFM_SETSTATUSTEXTA:
1015 TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
1016 SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
1017 break;
1019 case BFFM_SETSTATUSTEXTW:
1020 TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
1021 SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
1022 break;
1024 case BFFM_ENABLEOK:
1025 TRACE("Enable %ld\n", lParam);
1026 EnableWindow(GetDlgItem(hWnd, 1), lParam != 0);
1027 break;
1029 case BFFM_SETOKTEXT: /* unicode only */
1030 TRACE("Set OK text %s\n", debugstr_w((LPWSTR)lParam));
1031 SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)lParam);
1032 break;
1034 case BFFM_SETSELECTIONA:
1035 return BrsFolder_OnSetSelectionA(info, (LPVOID)lParam, (BOOL)wParam);
1037 case BFFM_SETSELECTIONW:
1038 return BrsFolder_OnSetSelectionW(info, (LPVOID)lParam, (BOOL)wParam);
1040 case BFFM_SETEXPANDED: /* unicode only */
1041 return BrsFolder_OnSetExpanded(info, (LPVOID)lParam, (BOOL)wParam, NULL);
1043 case WM_DESTROY:
1044 return BrsFolder_OnDestroy(info);
1046 return FALSE;
1049 static const WCHAR swBrowseTemplateName[] = {
1050 'S','H','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
1051 static const WCHAR swNewBrowseTemplateName[] = {
1052 'S','H','N','E','W','B','R','S','F','O','R','F','O','L','D','E','R','_','M','S','G','B','O','X',0};
1054 /*************************************************************************
1055 * SHBrowseForFolderA [SHELL32.@]
1056 * SHBrowseForFolder [SHELL32.@]
1058 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
1060 BROWSEINFOW bi;
1061 LPITEMIDLIST lpid;
1062 INT len;
1063 LPWSTR title;
1065 TRACE("%p\n", lpbi);
1067 bi.hwndOwner = lpbi->hwndOwner;
1068 bi.pidlRoot = lpbi->pidlRoot;
1069 if (lpbi->pszDisplayName)
1070 bi.pszDisplayName = HeapAlloc( GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR) );
1071 else
1072 bi.pszDisplayName = NULL;
1074 if (lpbi->lpszTitle)
1076 len = MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0 );
1077 title = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1078 MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, title, len );
1080 else
1081 title = NULL;
1083 bi.lpszTitle = title;
1084 bi.ulFlags = lpbi->ulFlags;
1085 bi.lpfn = lpbi->lpfn;
1086 bi.lParam = lpbi->lParam;
1087 bi.iImage = lpbi->iImage;
1088 lpid = SHBrowseForFolderW( &bi );
1089 if (bi.pszDisplayName)
1091 WideCharToMultiByte( CP_ACP, 0, bi.pszDisplayName, -1,
1092 lpbi->pszDisplayName, MAX_PATH, 0, NULL);
1093 HeapFree( GetProcessHeap(), 0, bi.pszDisplayName );
1095 HeapFree(GetProcessHeap(), 0, title);
1096 lpbi->iImage = bi.iImage;
1097 return lpid;
1101 /*************************************************************************
1102 * SHBrowseForFolderW [SHELL32.@]
1104 * NOTES
1105 * crashes when passed a null pointer
1107 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
1109 browse_info info;
1110 DWORD r;
1111 HRESULT hr;
1112 const WCHAR * templateName;
1113 INITCOMMONCONTROLSEX icex;
1115 info.hWnd = 0;
1116 info.pidlRet = NULL;
1117 info.lpBrowseInfo = lpbi;
1118 info.hwndTreeView = NULL;
1120 icex.dwSize = sizeof( icex );
1121 icex.dwICC = ICC_TREEVIEW_CLASSES;
1122 InitCommonControlsEx( &icex );
1124 hr = OleInitialize(NULL);
1126 if (lpbi->ulFlags & BIF_NEWDIALOGSTYLE)
1127 templateName = swNewBrowseTemplateName;
1128 else
1129 templateName = swBrowseTemplateName;
1130 r = DialogBoxParamW( shell32_hInstance, templateName, lpbi->hwndOwner,
1131 BrsFolderDlgProc, (LPARAM)&info );
1132 if (SUCCEEDED(hr))
1133 OleUninitialize();
1134 if (!r)
1136 ILFree(info.pidlRet);
1137 return NULL;
1140 return info.pidlRet;