[SHELL32]
[reactos.git] / dll / win32 / shell32 / brsfolder.cpp
1 /*
2 * Copyright 1999 Juergen Schmied
3 *
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.
8 *
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.
13 *
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
17 *
18 * FIXME:
19 * - many memory leaks
20 * - many flags unimplemented
21 * - implement new dialog style "make new folder" button
22 * - implement editbox
23 * - implement new dialog style resizing
24 */
25
26 #include "precomp.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(shell);
29
30 /* original margins and control size */
31 typedef struct tagLAYOUT_DATA
32 {
33 LONG left, width, right;
34 LONG top, height, bottom;
35 } LAYOUT_DATA;
36
37 typedef struct tagbrowse_info
38 {
39 HWND hWnd;
40 HWND hwndTreeView;
41 LPBROWSEINFOW lpBrowseInfo;
42 LPITEMIDLIST pidlRet;
43 LAYOUT_DATA *layout; /* filled by LayoutInit, used by LayoutUpdate */
44 SIZE szMin;
45 } browse_info;
46
47 typedef struct tagTV_ITEMDATA
48 {
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;
54
55 typedef struct tagLAYOUT_INFO
56 {
57 int iItemId; /* control id */
58 DWORD dwAnchor; /* BF_* flags specifying which margins should remain constant */
59 } LAYOUT_INFO;
60
61 static const LAYOUT_INFO g_layout_info[] =
62 {
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}
72 };
73
74 #define LAYOUT_INFO_COUNT (sizeof(g_layout_info)/sizeof(g_layout_info[0]))
75
76 #define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
77 BIF_BROWSEFORCOMPUTER | \
78 BIF_RETURNFSANCESTORS | \
79 BIF_RETURNONLYFSDIRS | \
80 BIF_NONEWFOLDERBUTTON | \
81 BIF_NEWDIALOGSTYLE | \
82 BIF_BROWSEINCLUDEFILES)
83
84 static void FillTreeView(browse_info*, LPSHELLFOLDER,
85 LPITEMIDLIST, HTREEITEM, IEnumIDList*);
86 static HTREEITEM InsertTreeViewItem( browse_info*, IShellFolder *,
87 LPCITEMIDLIST, LPCITEMIDLIST, IEnumIDList*, HTREEITEM);
88
89 static const WCHAR szBrowseFolderInfo[] = L"__WINE_BRSFOLDERDLG_INFO";
90
91 static DWORD __inline BrowseFlagsToSHCONTF(UINT ulFlags)
92 {
93 return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
94 }
95
96 static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo, HWND hWnd,
97 UINT msg, LPARAM param )
98 {
99 if (!lpBrowseInfo->lpfn)
100 return;
101 lpBrowseInfo->lpfn( hWnd, msg, param, lpBrowseInfo->lParam );
102 }
103
104 static LAYOUT_DATA *LayoutInit(HWND hwnd, const LAYOUT_INFO *layout_info, int layout_count)
105 {
106 LAYOUT_DATA *data;
107 RECT rcWnd;
108 int i;
109
110 GetClientRect(hwnd, &rcWnd);
111 data = (LAYOUT_DATA *)SHAlloc(sizeof(LAYOUT_DATA)*layout_count);
112 for (i = 0; i < layout_count; i++)
113 {
114 RECT r;
115 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
116
117 if (hItem == NULL)
118 ERR("Item %d not found\n", i);
119 GetWindowRect(hItem, &r);
120 MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);
121
122 data[i].left = r.left;
123 data[i].right = rcWnd.right - r.right;
124 data[i].width = r.right - r.left;
125
126 data[i].top = r.top;
127 data[i].bottom = rcWnd.bottom - r.bottom;
128 data[i].height = r.bottom - r.top;
129 }
130 return data;
131 }
132
133 static void LayoutUpdate(HWND hwnd, LAYOUT_DATA *data, const LAYOUT_INFO *layout_info, int layout_count)
134 {
135 RECT rcWnd;
136 int i;
137
138 GetClientRect(hwnd, &rcWnd);
139 for (i = 0; i < layout_count; i++)
140 {
141 RECT r;
142 HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);
143
144 GetWindowRect(hItem, &r);
145 MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);
146
147 if (layout_info[i].dwAnchor & BF_RIGHT)
148 {
149 r.right = rcWnd.right - data[i].right;
150 if (!(layout_info[i].dwAnchor & BF_LEFT))
151 r.left = r.right - data[i].width;
152 }
153
154 if (layout_info[i].dwAnchor & BF_BOTTOM)
155 {
156 r.bottom = rcWnd.bottom - data[i].bottom;
157 if (!(layout_info[i].dwAnchor & BF_TOP))
158 r.top = r.bottom - data[i].height;
159 }
160
161 SetWindowPos(hItem, NULL, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOZORDER);
162 }
163 }
164
165
166 /******************************************************************************
167 * InitializeTreeView [Internal]
168 *
169 * Called from WM_INITDIALOG handler.
170 *
171 * PARAMS
172 * hwndParent [I] The BrowseForFolder dialog
173 * root [I] ITEMIDLIST of the root shell folder
174 */
175 static void InitializeTreeView( browse_info *info )
176 {
177 LPITEMIDLIST pidlParent, pidlChild;
178 HIMAGELIST hImageList;
179 HRESULT hr;
180 IShellFolder *lpsfParent, *lpsfRoot;
181 IEnumIDList * pEnumChildren = NULL;
182 HTREEITEM item;
183 DWORD flags;
184 LPCITEMIDLIST root = info->lpBrowseInfo->pidlRoot;
185
186 TRACE("%p\n", info );
187
188 Shell_GetImageLists(NULL, &hImageList);
189
190 if (hImageList)
191 SendMessageW( info->hwndTreeView, TVM_SETIMAGELIST, 0, (LPARAM)hImageList );
192
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:
196 *
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.
201 *
202 * If root is 'Desktop', then root's parent is also 'Desktop'.
203 */
204
205 pidlParent = ILClone(root);
206 ILRemoveLastID(pidlParent);
207 pidlChild = ILClone(ILFindLastID(root));
208
209 if (_ILIsDesktop(pidlParent)) {
210 hr = SHGetDesktopFolder(&lpsfParent);
211 } else {
212 IShellFolder *lpsfDesktop;
213 hr = SHGetDesktopFolder(&lpsfDesktop);
214 if (FAILED(hr)) {
215 WARN("SHGetDesktopFolder failed! hr = %08x\n", hr);
216 ILFree(pidlChild);
217 ILFree(pidlParent);
218 return;
219 }
220 hr = lpsfDesktop->BindToObject(pidlParent, 0, IID_PPV_ARG(IShellFolder, &lpsfParent));
221 lpsfDesktop->Release();
222 }
223
224 if (FAILED(hr)) {
225 WARN("Could not bind to parent shell folder! hr = %08x\n", hr);
226 ILFree(pidlChild);
227 ILFree(pidlParent);
228 return;
229 }
230
231 if (pidlChild && pidlChild->mkid.cb) {
232 hr = lpsfParent->BindToObject(pidlChild, 0, IID_PPV_ARG(IShellFolder, &lpsfRoot));
233 } else {
234 lpsfRoot = lpsfParent;
235 hr = lpsfParent->AddRef();
236 }
237
238 if (FAILED(hr)) {
239 WARN("Could not bind to root shell folder! hr = %08x\n", hr);
240 lpsfParent->Release();
241 ILFree(pidlChild);
242 ILFree(pidlParent);
243 return;
244 }
245
246 flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
247 hr = lpsfRoot->EnumObjects(info->hWnd, flags, &pEnumChildren );
248 if (FAILED(hr)) {
249 WARN("Could not get child iterator! hr = %08x\n", hr);
250 lpsfParent->Release();
251 lpsfRoot->Release();
252 ILFree(pidlChild);
253 ILFree(pidlParent);
254 return;
255 }
256
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 );
261
262 ILFree(pidlChild);
263 ILFree(pidlParent);
264 lpsfRoot->Release();
265 lpsfParent->Release();
266 }
267
268 static int GetIcon(LPCITEMIDLIST lpi, UINT uFlags)
269 {
270 SHFILEINFOW sfi;
271 SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
272 return sfi.iIcon;
273 }
274
275 static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
276 {
277 LPITEMIDLIST pidlDesktop = NULL;
278 DWORD flags;
279
280 TRACE("%p %p\n",lpifq, lpTV_ITEM);
281
282 if (!lpifq)
283 {
284 pidlDesktop = _ILCreateDesktop();
285 lpifq = pidlDesktop;
286 }
287
288 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
289 lpTV_ITEM->iImage = GetIcon( lpifq, flags );
290
291 flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
292 lpTV_ITEM->iSelectedImage = GetIcon( lpifq, flags );
293
294 if (pidlDesktop)
295 ILFree( pidlDesktop );
296 }
297
298 /******************************************************************************
299 * GetName [Internal]
300 *
301 * Query a shell folder for the display name of one of it's children
302 *
303 * PARAMS
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
308 *
309 * RETURNS
310 * Success: TRUE
311 * Failure: FALSE
312 */
313 static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
314 {
315 BOOL bSuccess=TRUE;
316 STRRET str;
317
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);
321 else
322 bSuccess = FALSE;
323
324 TRACE("-- %s\n", debugstr_w(lpFriendlyName));
325 return bSuccess;
326 }
327
328 /******************************************************************************
329 * InsertTreeViewItem [Internal]
330 *
331 * PARAMS
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
338 *
339 * RETURNS
340 * Success: Handle to the created and inserted treeview-item
341 * Failure: NULL
342 */
343 static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
344 LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
345 HTREEITEM hParent)
346 {
347 TVITEMW tvi;
348 TVINSERTSTRUCTW tvins;
349 WCHAR szBuff[MAX_PATH];
350 LPTV_ITEMDATA lptvid=0;
351
352 tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
353
354 tvi.cChildren= pEnumIL ? 1 : 0;
355 tvi.mask |= TVIF_CHILDREN;
356
357 lptvid = (TV_ITEMDATA *)SHAlloc( sizeof(TV_ITEMDATA) );
358 if (!lptvid)
359 return NULL;
360
361 if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
362 return NULL;
363
364 tvi.pszText = szBuff;
365 tvi.cchTextMax = MAX_PATH;
366 tvi.lParam = (LPARAM)lptvid;
367
368 lpsf->AddRef();
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);
374
375 tvins.item = tvi;
376 tvins.hInsertAfter = NULL;
377 tvins.hParent = hParent;
378
379 return TreeView_InsertItem( info->hwndTreeView, &tvins );
380 }
381
382 /******************************************************************************
383 * FillTreeView [Internal]
384 *
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
387 *
388 * PARAMS
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
394 */
395 static void FillTreeView( browse_info *info, IShellFolder * lpsf,
396 LPITEMIDLIST pidl, HTREEITEM hParent, IEnumIDList* lpe)
397 {
398 LPITEMIDLIST pidlTemp = 0;
399 ULONG ulFetched;
400 HRESULT hr;
401 HWND hwnd = GetParent( info->hwndTreeView );
402
403 TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
404
405 /* No IEnumIDList -> No children */
406 if (!lpe) return;
407
408 SetCapture( hwnd );
409 SetCursor( LoadCursorA( 0, (LPSTR)IDC_WAIT ) );
410
411 while (S_OK == lpe->Next(1,&pidlTemp,&ulFetched))
412 {
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)
418 {
419 hr = lpsf->BindToObject(pidlTemp, NULL, IID_PPV_ARG(IShellFolder, &pSFChild));
420 if (SUCCEEDED(hr))
421 {
422 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
423 hr = pSFChild->EnumObjects(hwnd, flags, &pEnumIL);
424 if (hr == S_OK)
425 {
426 if ((pEnumIL->Skip(1) != S_OK) ||
427 FAILED(pEnumIL->Reset()))
428 {
429 pEnumIL->Release();
430 pEnumIL = NULL;
431 }
432 }
433 pSFChild->Release();
434 }
435 }
436
437 if (!InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent))
438 goto done;
439 SHFree(pidlTemp); /* Finally, free the pidl that the shell gave us... */
440 pidlTemp=NULL;
441 }
442
443 done:
444 ReleaseCapture();
445 SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
446 SHFree(pidlTemp);
447 }
448
449 static BOOL __inline PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
450 {
451 LPPIDLDATA data = _ILGetDataPointer(pidl);
452 if (!data)
453 return FALSE;
454 return (data->type == type);
455 }
456
457 static void BrsFolder_CheckValidSelection( browse_info *info, LPTV_ITEMDATA lptvid )
458 {
459 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
460 LPCITEMIDLIST pidl = lptvid->lpi;
461 BOOL bEnabled = TRUE;
462 DWORD dwAttributes;
463 HRESULT r;
464
465 if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
466 !PIDLIsType(pidl, PT_COMP))
467 bEnabled = FALSE;
468 if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
469 {
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)))
474 bEnabled = FALSE;
475 }
476
477 dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
478 r = lptvid->lpsfParent->GetAttributesOf(1,
479 (LPCITEMIDLIST*)&lptvid->lpi,
480 &dwAttributes);
481 if (FAILED(r) ||
482 ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
483 {
484 if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
485 bEnabled = FALSE;
486 EnableWindow(GetDlgItem(info->hWnd, IDC_BROWSE_FOR_FOLDER_NEW_FOLDER), FALSE);
487 }
488 else
489 EnableWindow(GetDlgItem(info->hWnd, IDC_BROWSE_FOR_FOLDER_NEW_FOLDER), TRUE);
490
491 SendMessageW(info->hWnd, BFFM_ENABLEOK, 0, bEnabled);
492 }
493
494 static LRESULT BrsFolder_Treeview_Delete( browse_info *info, NMTREEVIEWW *pnmtv )
495 {
496 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA)pnmtv->itemOld.lParam;
497
498 TRACE("TVN_DELETEITEMA/W %p\n", lptvid);
499
500 lptvid->lpsfParent->Release();
501 if (lptvid->pEnumIL)
502 lptvid->pEnumIL->Release();
503 SHFree(lptvid->lpi);
504 SHFree(lptvid->lpifq);
505 SHFree(lptvid);
506 return 0;
507 }
508
509 static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
510 {
511 IShellFolder *lpsf2 = NULL;
512 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
513 HRESULT r;
514
515 TRACE("TVN_ITEMEXPANDINGA/W\n");
516
517 if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
518 return 0;
519
520 if (lptvid->lpi && lptvid->lpi->mkid.cb) {
521 r = lptvid->lpsfParent->BindToObject(lptvid->lpi, 0, IID_PPV_ARG(IShellFolder, &lpsf2));
522 } else {
523 lpsf2 = lptvid->lpsfParent;
524 r = lpsf2->AddRef();
525 }
526
527 if (SUCCEEDED(r))
528 {
529 FillTreeView( info, lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
530 lpsf2->Release();
531 }
532
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 );
538
539 return 0;
540 }
541
542 static HRESULT BrsFolder_Treeview_Changed( browse_info *info, NMTREEVIEWW *pnmtv )
543 {
544 LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
545 WCHAR name[MAX_PATH];
546
547 ILFree(info->pidlRet);
548 info->pidlRet = ILClone(lptvid->lpifq);
549
550 if (GetName(lptvid->lpsfParent, lptvid->lpi, SHGDN_NORMAL, name))
551 SetWindowTextW( GetDlgItem(info->hWnd, IDC_BROWSE_FOR_FOLDER_FOLDER_TEXT), name );
552
553 browsefolder_callback( info->lpBrowseInfo, info->hWnd, BFFM_SELCHANGED,
554 (LPARAM)info->pidlRet );
555 BrsFolder_CheckValidSelection( info, lptvid );
556 return 0;
557 }
558
559 static LRESULT BrsFolder_Treeview_Rename(browse_info *info, NMTVDISPINFOW *pnmtv)
560 {
561 LPTV_ITEMDATA item_data;
562 WCHAR old_path[MAX_PATH], new_path[MAX_PATH], *p;
563 NMTREEVIEWW nmtv;
564 TVITEMW item;
565
566 if(!pnmtv->item.pszText)
567 return 0;
568
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;
573
574 SHGetPathFromIDListW(item_data->lpifq, old_path);
575 if(!(p = strrchrW(old_path, '\\')))
576 return 0;
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);
580
581 if(!MoveFileW(old_path, new_path))
582 return 0;
583
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);
589
590 item.mask = TVIF_HANDLE|TVIF_TEXT;
591 item.pszText = pnmtv->item.pszText;
592 SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);
593
594 nmtv.itemNew.lParam = item.lParam;
595 BrsFolder_Treeview_Changed(info, &nmtv);
596 return 0;
597 }
598
599 static LRESULT BrsFolder_OnNotify( browse_info *info, UINT CtlID, LPNMHDR lpnmh )
600 {
601 NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
602
603 TRACE("%p %x %p msg=%x\n", info, CtlID, lpnmh, pnmtv->hdr.code);
604
605 if (pnmtv->hdr.idFrom != IDC_BROWSE_FOR_FOLDER_TREEVIEW)
606 return 0;
607
608 switch (pnmtv->hdr.code)
609 {
610 case TVN_DELETEITEMA:
611 case TVN_DELETEITEMW:
612 return BrsFolder_Treeview_Delete( info, pnmtv );
613
614 case TVN_ITEMEXPANDINGA:
615 case TVN_ITEMEXPANDINGW:
616 return BrsFolder_Treeview_Expand( info, pnmtv );
617
618 case TVN_SELCHANGEDA:
619 case TVN_SELCHANGEDW:
620 return BrsFolder_Treeview_Changed( info, pnmtv );
621
622 case TVN_ENDLABELEDITA:
623 case TVN_ENDLABELEDITW:
624 return BrsFolder_Treeview_Rename( info, (LPNMTVDISPINFOW)pnmtv );
625
626 default:
627 WARN("unhandled (%d)\n", pnmtv->hdr.code);
628 break;
629 }
630
631 return 0;
632 }
633
634
635 static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
636 {
637 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
638
639 info->hWnd = hWnd;
640 SetPropW( hWnd, szBrowseFolderInfo, info );
641
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);
646
647 if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
648 {
649 RECT rcWnd;
650
651 info->layout = LayoutInit(hWnd, g_layout_info, LAYOUT_INFO_COUNT);
652
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;
657 }
658 else
659 {
660 info->layout = NULL;
661 }
662
663 if (lpBrowseInfo->lpszTitle)
664 SetWindowTextW( GetDlgItem(hWnd, IDC_BROWSE_FOR_FOLDER_TITLE), lpBrowseInfo->lpszTitle );
665 else
666 ShowWindow( GetDlgItem(hWnd, IDC_BROWSE_FOR_FOLDER_TITLE), SW_HIDE );
667
668 if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT)
669 || (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
670 ShowWindow( GetDlgItem(hWnd, IDC_BROWSE_FOR_FOLDER_STATUS), SW_HIDE );
671
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 );
676
677 /* Hide the editbox? */
678 if (!(lpBrowseInfo->ulFlags & BIF_EDITBOX))
679 {
680 ShowWindow( GetDlgItem(hWnd, IDC_BROWSE_FOR_FOLDER_FOLDER), SW_HIDE );
681 ShowWindow( GetDlgItem(hWnd, IDC_BROWSE_FOR_FOLDER_FOLDER_TEXT), SW_HIDE );
682 }
683
684 info->hwndTreeView = GetDlgItem( hWnd, IDC_BROWSE_FOR_FOLDER_TREEVIEW );
685 if (info->hwndTreeView)
686 {
687 InitializeTreeView( info );
688
689 /* Resize the treeview if there's not editbox */
690 if ((lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
691 && !(lpBrowseInfo->ulFlags & BIF_EDITBOX))
692 {
693 RECT rc;
694 GetClientRect(info->hwndTreeView, &rc);
695 SetWindowPos(info->hwndTreeView, HWND_TOP, 0, 0,
696 rc.right, rc.bottom + 40, SWP_NOMOVE);
697 }
698 }
699 else
700 ERR("treeview control missing!\n");
701
702 browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
703
704 return TRUE;
705 }
706
707 static HRESULT BrsFolder_Rename(browse_info *info, HTREEITEM rename)
708 {
709 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)rename);
710 SendMessageW(info->hwndTreeView, TVM_EDITLABELW, 0, (LPARAM)rename);
711 return S_OK;
712 }
713
714 static HRESULT BrsFolder_NewFolder(browse_info *info)
715 {
716 DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
717 IShellFolder *desktop, *cur;
718 ISFHelper *sfhelper;
719 WCHAR name[MAX_PATH];
720 HTREEITEM parent, added;
721 LPTV_ITEMDATA item_data;
722 LPITEMIDLIST new_item;
723 TVITEMW item;
724 HRESULT hr;
725 int len;
726
727 if(!info->pidlRet) {
728 ERR("Make new folder button should be disabled\n");
729 return E_FAIL;
730 }
731
732 /* Create new directory */
733 hr = SHGetDesktopFolder(&desktop);
734 if(FAILED(hr))
735 return hr;
736 hr = desktop->BindToObject(info->pidlRet, 0, IID_PPV_ARG(IShellFolder, &cur));
737 desktop->Release();
738 if(FAILED(hr))
739 return hr;
740
741 hr = cur->QueryInterface(IID_PPV_ARG(ISFHelper, &sfhelper));
742 if(FAILED(hr))
743 return hr;
744
745 hr = SHGetPathFromIDListW(info->pidlRet, name);
746 if(FAILED(hr))
747 goto cleanup;
748
749 len = strlenW(name);
750 if(len<MAX_PATH)
751 name[len++] = '\\';
752 hr = sfhelper->GetUniqueName(&name[len], MAX_PATH-len);
753 sfhelper->Release();
754 if(FAILED(hr))
755 goto cleanup;
756
757 hr = E_FAIL;
758 if(!CreateDirectoryW(name, NULL))
759 goto cleanup;
760
761 /* Update parent of newly created directory */
762 parent = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
763 if(!parent)
764 goto cleanup;
765
766 SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)parent);
767
768 memset(&item, 0, sizeof(TVITEMW));
769 item.mask = TVIF_PARAM|TVIF_STATE;
770 item.hItem = parent;
771 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
772 item_data = (LPTV_ITEMDATA)item.lParam;
773 if(!item_data)
774 goto cleanup;
775
776 if(item_data->pEnumIL)
777 item_data->pEnumIL->Release();
778 hr = cur->EnumObjects(info->hwndTreeView, flags, &item_data->pEnumIL);
779 if(FAILED(hr))
780 goto cleanup;
781
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);
788 }
789
790 hr = cur->ParseDisplayName(NULL, NULL, name+len, NULL, &new_item, NULL);
791 if(FAILED(hr))
792 goto cleanup;
793
794 added = InsertTreeViewItem(info, cur, new_item, item_data->lpifq, NULL, parent);
795 cur->Release();
796 SHFree(new_item);
797
798 SendMessageW(info->hwndTreeView, TVM_SORTCHILDREN, FALSE, (LPARAM)parent);
799 return BrsFolder_Rename(info, added);
800
801 cleanup:
802 return hr;
803 }
804
805 static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
806 {
807 LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
808
809 switch (id)
810 {
811 case IDOK:
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 );
820 return TRUE;
821
822 case IDCANCEL:
823 EndDialog( info->hWnd, 0 );
824 return TRUE;
825
826 case IDC_BROWSE_FOR_FOLDER_NEW_FOLDER:
827 BrsFolder_NewFolder(info);
828 return TRUE;
829 }
830 return FALSE;
831 }
832
833 static BOOL BrsFolder_OnSetExpanded(browse_info *info, LPVOID selection,
834 BOOL is_str, HTREEITEM *pItem)
835 {
836 LPITEMIDLIST pidlSelection = (LPITEMIDLIST)selection;
837 LPCITEMIDLIST pidlCurrent, pidlRoot;
838 TVITEMEXW item;
839 BOOL bResult = FALSE;
840
841 memset(&item, 0, sizeof(item));
842
843 /* If 'selection' is a string, convert to a Shell ID List. */
844 if (is_str) {
845 IShellFolder *psfDesktop;
846 HRESULT hr;
847
848 hr = SHGetDesktopFolder(&psfDesktop);
849 if (FAILED(hr))
850 goto done;
851
852 hr = psfDesktop->ParseDisplayName(NULL, NULL, (LPOLESTR)selection,
853 NULL, &pidlSelection, NULL);
854 psfDesktop->Release();
855 if (FAILED(hr))
856 goto done;
857 }
858
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);
866 }
867
868 /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
869 if (!_ILIsEmpty(pidlRoot))
870 goto done;
871
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);
875
876 if (item.hItem)
877 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
878 (LPARAM)item.hItem);
879
880 /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
881 while (item.hItem && !_ILIsEmpty(pidlCurrent)) {
882 LPTV_ITEMDATA pItemData;
883
884 SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
885 pItemData = (LPTV_ITEMDATA)item.lParam;
886
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,
894 (LPARAM)item.hItem);
895 }
896 } else {
897 item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT,
898 (LPARAM)item.hItem);
899 }
900 }
901
902 if (_ILIsEmpty(pidlCurrent) && item.hItem)
903 bResult = TRUE;
904
905 done:
906 if (pidlSelection && pidlSelection != (LPITEMIDLIST)selection)
907 ILFree(pidlSelection);
908
909 if (pItem)
910 *pItem = item.hItem;
911
912 return bResult;
913 }
914
915 static BOOL BrsFolder_OnSetSelectionW(browse_info *info, LPVOID selection, BOOL is_str) {
916 HTREEITEM hItem;
917 BOOL bResult;
918
919 if (!selection) return FALSE;
920
921 bResult = BrsFolder_OnSetExpanded(info, selection, is_str, &hItem);
922 if (bResult)
923 SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem );
924 return bResult;
925 }
926
927 static BOOL BrsFolder_OnSetSelectionA(browse_info *info, LPVOID selection, BOOL is_str) {
928 LPWSTR selectionW = NULL;
929 BOOL result = FALSE;
930 int length;
931
932 if (!is_str)
933 return BrsFolder_OnSetSelectionW(info, selection, is_str);
934
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))
938 {
939 result = BrsFolder_OnSetSelectionW(info, selectionW, is_str);
940 }
941
942 HeapFree(GetProcessHeap(), 0, selectionW);
943 return result;
944 }
945
946 static BOOL BrsFolder_OnWindowPosChanging(browse_info *info, WINDOWPOS *pos)
947 {
948 if ((info->lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE) && !(pos->flags & SWP_NOSIZE))
949 {
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;
954 }
955 return 0;
956 }
957
958 static INT BrsFolder_OnDestroy(browse_info *info)
959 {
960 if (info->layout)
961 {
962 SHFree(info->layout);
963 info->layout = NULL;
964 }
965
966 return 0;
967 }
968
969 /*************************************************************************
970 * BrsFolderDlgProc32 (not an exported API function)
971 */
972 static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
973 LPARAM lParam )
974 {
975 browse_info *info;
976
977 TRACE("hwnd=%p msg=%04x 0x%08lx 0x%08lx\n", hWnd, msg, wParam, lParam );
978
979 if (msg == WM_INITDIALOG)
980 return BrsFolder_OnCreate( hWnd, (browse_info*) lParam );
981
982 info = (browse_info*) GetPropW( hWnd, szBrowseFolderInfo );
983
984 switch (msg)
985 {
986 case WM_NOTIFY:
987 return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
988
989 case WM_COMMAND:
990 return BrsFolder_OnCommand( info, wParam );
991
992 case WM_WINDOWPOSCHANGING:
993 return BrsFolder_OnWindowPosChanging( info, (WINDOWPOS *)lParam);
994
995 case WM_SIZE:
996 if (info->layout) /* new style dialogs */
997 LayoutUpdate(hWnd, info->layout, g_layout_info, LAYOUT_INFO_COUNT);
998 return 0;
999
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);
1003 break;
1004
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);
1008 break;
1009
1010 case BFFM_ENABLEOK:
1011 TRACE("Enable %ld\n", lParam);
1012 EnableWindow(GetDlgItem(hWnd, 1), (lParam)?TRUE:FALSE);
1013 break;
1014
1015 case BFFM_SETOKTEXT: /* unicode only */
1016 TRACE("Set OK text %s\n", debugstr_w((LPWSTR)wParam));
1017 SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)wParam);
1018 break;
1019
1020 case BFFM_SETSELECTIONA:
1021 return BrsFolder_OnSetSelectionA(info, (LPVOID)lParam, (BOOL)wParam);
1022
1023 case BFFM_SETSELECTIONW:
1024 return BrsFolder_OnSetSelectionW(info, (LPVOID)lParam, (BOOL)wParam);
1025
1026 case BFFM_SETEXPANDED: /* unicode only */
1027 return BrsFolder_OnSetExpanded(info, (LPVOID)lParam, (BOOL)wParam, NULL);
1028
1029 case WM_DESTROY:
1030 return BrsFolder_OnDestroy(info);
1031 }
1032 return FALSE;
1033 }
1034
1035
1036
1037
1038 /*************************************************************************
1039 * SHBrowseForFolderA [SHELL32.@]
1040 * SHBrowseForFolder [SHELL32.@]
1041 */
1042 LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
1043 {
1044 BROWSEINFOW bi;
1045 LPITEMIDLIST lpid;
1046 INT len;
1047 LPWSTR title;
1048
1049 TRACE("%p\n", lpbi);
1050
1051 bi.hwndOwner = lpbi->hwndOwner;
1052 bi.pidlRoot = lpbi->pidlRoot;
1053 if (lpbi->pszDisplayName)
1054 {
1055 bi.pszDisplayName = (WCHAR *)HeapAlloc( GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR) );
1056 MultiByteToWideChar( CP_ACP, 0, lpbi->pszDisplayName, -1, bi.pszDisplayName, MAX_PATH );
1057 }
1058 else
1059 bi.pszDisplayName = NULL;
1060
1061 if (lpbi->lpszTitle)
1062 {
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 );
1066 }
1067 else
1068 title = NULL;
1069
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)
1077 {
1078 WideCharToMultiByte( CP_ACP, 0, bi.pszDisplayName, -1,
1079 lpbi->pszDisplayName, MAX_PATH, 0, NULL);
1080 HeapFree( GetProcessHeap(), 0, bi.pszDisplayName );
1081 }
1082 HeapFree(GetProcessHeap(), 0, title);
1083 lpbi->iImage = bi.iImage;
1084 return lpid;
1085 }
1086
1087
1088 /*************************************************************************
1089 * SHBrowseForFolderW [SHELL32.@]
1090 *
1091 * NOTES
1092 * crashes when passed a null pointer
1093 */
1094 LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
1095 {
1096 browse_info info;
1097 DWORD r;
1098 HRESULT hr;
1099 WORD wDlgId;
1100
1101 info.hWnd = 0;
1102 info.pidlRet = NULL;
1103 info.lpBrowseInfo = lpbi;
1104 info.hwndTreeView = NULL;
1105
1106 hr = OleInitialize(NULL);
1107
1108 if (lpbi->ulFlags & BIF_NEWDIALOGSTYLE)
1109 wDlgId = IDD_BROWSE_FOR_FOLDER_NEW;
1110 else
1111 wDlgId = IDD_BROWSE_FOR_FOLDER;
1112 r = DialogBoxParamW( shell32_hInstance, MAKEINTRESOURCEW(wDlgId), lpbi->hwndOwner,
1113 BrsFolderDlgProc, (LPARAM)&info );
1114 if (SUCCEEDED(hr))
1115 OleUninitialize();
1116 if (!r)
1117 {
1118 ILFree(info.pidlRet);
1119 return NULL;
1120 }
1121
1122 return info.pidlRet;
1123 }