brsfolder.c 37.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 1999 Juergen Schmied
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 19
 *
 * FIXME:
 *  - many memory leaks
20
 *  - many flags unimplemented
21
 *    - implement editbox
22 23
 */

24 25 26
#include <stdlib.h>
#include <string.h>

27
#define COBJMACROS
28
#define NONAMELESSUNION
29

30
#include "wine/debug.h"
31 32
#include "pidl.h"
#include "shell32_main.h"
33
#include "commoncontrols.h"
34
#include "shellapi.h"
35
#include "shresdef.h"
36
#include "shellfolder.h"
37

38
WINE_DEFAULT_DEBUG_CHANNEL(shell);
39

40 41
#define SHV_CHANGE_NOTIFY (WM_USER + 0x1111)

42 43 44 45 46 47 48
/* original margins and control size */
typedef struct tagLAYOUT_DATA
{
    LONG left, width, right;
    LONG top, height, bottom;
} LAYOUT_DATA;

49 50 51 52 53 54
typedef struct tagbrowse_info
{
    HWND          hWnd;
    HWND          hwndTreeView;
    LPBROWSEINFOW lpBrowseInfo;
    LPITEMIDLIST  pidlRet;
55 56
    LAYOUT_DATA  *layout;  /* filled by LayoutInit, used by LayoutUpdate */
    SIZE          szMin;
57
    ULONG         hNotify; /* change notification handle */
58
} browse_info;
59

60
typedef struct tagTV_ITEMDATA
61 62
{
   LPSHELLFOLDER lpsfParent; /* IShellFolder of the parent */
63
   LPITEMIDLIST  lpi;        /* PIDL relative to parent */
64 65 66 67
   LPITEMIDLIST  lpifq;      /* Fully qualified PIDL */
   IEnumIDList*  pEnumIL;    /* Children iterator */ 
} TV_ITEMDATA, *LPTV_ITEMDATA;

68 69 70 71 72 73 74 75 76 77 78 79
typedef struct tagLAYOUT_INFO
{
    int iItemId;          /* control id */
    DWORD dwAnchor;       /* BF_* flags specifying which margins should remain constant */
} LAYOUT_INFO;

static const LAYOUT_INFO g_layout_info[] =
{
    {IDD_TITLE,         BF_TOP|BF_LEFT|BF_RIGHT},
    {IDD_STATUS,        BF_TOP|BF_LEFT|BF_RIGHT},
    {IDD_FOLDER,        BF_TOP|BF_LEFT|BF_RIGHT},
    {IDD_TREEVIEW,      BF_TOP|BF_BOTTOM|BF_LEFT|BF_RIGHT},
80
    {IDD_FOLDER,        BF_BOTTOM|BF_LEFT},
81 82 83 84 85 86
    {IDD_FOLDERTEXT,    BF_BOTTOM|BF_LEFT|BF_RIGHT},
    {IDD_MAKENEWFOLDER, BF_BOTTOM|BF_LEFT},
    {IDOK,              BF_BOTTOM|BF_RIGHT},
    {IDCANCEL,          BF_BOTTOM|BF_RIGHT}
};

87 88 89 90
#define SUPPORTEDFLAGS (BIF_STATUSTEXT | \
                        BIF_BROWSEFORCOMPUTER | \
                        BIF_RETURNFSANCESTORS | \
                        BIF_RETURNONLYFSDIRS | \
91 92
                        BIF_NONEWFOLDERBUTTON | \
                        BIF_NEWDIALOGSTYLE | \
93 94
                        BIF_BROWSEINCLUDEFILES)

95 96 97 98 99
static void FillTreeView(browse_info*, LPSHELLFOLDER,
               LPITEMIDLIST, HTREEITEM, IEnumIDList*);
static HTREEITEM InsertTreeViewItem( browse_info*, IShellFolder *,
               LPCITEMIDLIST, LPCITEMIDLIST, IEnumIDList*, HTREEITEM);

100 101 102 103
static inline DWORD BrowseFlagsToSHCONTF(UINT ulFlags)
{
    return SHCONTF_FOLDERS | (ulFlags & BIF_BROWSEINCLUDEFILES ? SHCONTF_NONFOLDERS : 0);
}
104

105 106 107 108 109
static void browsefolder_callback( LPBROWSEINFOW lpBrowseInfo, HWND hWnd,
                                   UINT msg, LPARAM param )
{
    if (!lpBrowseInfo->lpfn)
        return;
110
    lpBrowseInfo->lpfn( hWnd, msg, param, lpBrowseInfo->lParam );
111 112
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
static LAYOUT_DATA *LayoutInit(HWND hwnd, const LAYOUT_INFO *layout_info, int layout_count)
{
    LAYOUT_DATA *data;
    RECT rcWnd;
    int i;

    GetClientRect(hwnd, &rcWnd);
    data = SHAlloc(sizeof(LAYOUT_DATA)*layout_count);
    for (i = 0; i < layout_count; i++)
    {
        RECT r;
        HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);

        if (hItem == NULL)
            ERR("Item %d not found\n", i);
        GetWindowRect(hItem, &r);
        MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);

        data[i].left = r.left;
        data[i].right = rcWnd.right - r.right;
        data[i].width = r.right - r.left;

        data[i].top = r.top;
        data[i].bottom = rcWnd.bottom - r.bottom;
        data[i].height = r.bottom - r.top;
    }
    return data;
}

static void LayoutUpdate(HWND hwnd, LAYOUT_DATA *data, const LAYOUT_INFO *layout_info, int layout_count)
{
    RECT rcWnd;
    int i;

    GetClientRect(hwnd, &rcWnd);
    for (i = 0; i < layout_count; i++)
    {
        RECT r;
        HWND hItem = GetDlgItem(hwnd, layout_info[i].iItemId);

        GetWindowRect(hItem, &r);
        MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&r, 2);

        if (layout_info[i].dwAnchor & BF_RIGHT)
        {
            r.right = rcWnd.right - data[i].right;
            if (!(layout_info[i].dwAnchor & BF_LEFT))
                r.left = r.right - data[i].width;
        }

        if (layout_info[i].dwAnchor & BF_BOTTOM)
        {
            r.bottom = rcWnd.bottom - data[i].bottom;
            if (!(layout_info[i].dwAnchor & BF_TOP))
                r.top = r.bottom - data[i].height;
        }

        SetWindowPos(hItem, NULL, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOZORDER);
    }
}


175 176 177 178 179 180 181 182 183
/******************************************************************************
 * InitializeTreeView [Internal]
 *
 * Called from WM_INITDIALOG handler.
 * 
 * PARAMS
 *  hwndParent [I] The BrowseForFolder dialog
 *  root       [I] ITEMIDLIST of the root shell folder
 */
184
static void InitializeTreeView( browse_info *info )
185
{
186 187 188 189 190
    LPITEMIDLIST pidlParent, pidlChild;
    HIMAGELIST hImageList;
    HRESULT hr;
    IShellFolder *lpsfParent, *lpsfRoot;
    IEnumIDList * pEnumChildren = NULL;
191 192 193
    HTREEITEM item;
    DWORD flags;
    LPCITEMIDLIST root = info->lpBrowseInfo->pidlRoot;
194

195
    TRACE("%p\n", info );
196
    
197
    Shell_GetImageLists(NULL, &hImageList);
198 199

    if (hImageList)
200
        SendMessageW( info->hwndTreeView, TVM_SETIMAGELIST, 0, (LPARAM)hImageList );
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

    /* We want to call InsertTreeViewItem down the code, in order to insert
     * the root item of the treeview. Due to InsertTreeViewItem's signature, 
     * we need the following to do this:
     *
     * + An ITEMIDLIST corresponding to _the parent_ of root. 
     * + An ITEMIDLIST, which is a relative path from root's parent to root 
     *   (containing a single SHITEMID).
     * + An IShellFolder interface pointer of root's parent folder.
     *
     * If root is 'Desktop', then root's parent is also 'Desktop'.
     */

    pidlParent = ILClone(root);
    ILRemoveLastID(pidlParent);
    pidlChild = ILClone(ILFindLastID(root));
    
    if (_ILIsDesktop(pidlParent)) {
        hr = SHGetDesktopFolder(&lpsfParent);
    } else {
        IShellFolder *lpsfDesktop;
        hr = SHGetDesktopFolder(&lpsfDesktop);
223
        if (FAILED(hr)) {
224
            WARN("SHGetDesktopFolder failed! hr = %08lx\n", hr);
225
            ILFree(pidlChild);
226
            ILFree(pidlParent);
227 228 229 230 231
            return;
        }
        hr = IShellFolder_BindToObject(lpsfDesktop, pidlParent, 0, &IID_IShellFolder, (LPVOID*)&lpsfParent);
        IShellFolder_Release(lpsfDesktop);
    }
232 233

    if (FAILED(hr)) {
234
        WARN("Could not bind to parent shell folder! hr = %08lx\n", hr);
235
        ILFree(pidlChild);
236
        ILFree(pidlParent);
237 238 239
        return;
    }

240
    if (!_ILIsEmpty(pidlChild)) {
241 242 243 244 245
        hr = IShellFolder_BindToObject(lpsfParent, pidlChild, 0, &IID_IShellFolder, (LPVOID*)&lpsfRoot);
    } else {
        lpsfRoot = lpsfParent;
        hr = IShellFolder_AddRef(lpsfParent);
    }
246 247

    if (FAILED(hr)) {
248
        WARN("Could not bind to root shell folder! hr = %08lx\n", hr);
249
        IShellFolder_Release(lpsfParent);
250
        ILFree(pidlChild);
251
        ILFree(pidlParent);
252 253 254
        return;
    }

255 256
    flags = BrowseFlagsToSHCONTF( info->lpBrowseInfo->ulFlags );
    hr = IShellFolder_EnumObjects( lpsfRoot, info->hWnd, flags, &pEnumChildren );
257
    if (FAILED(hr)) {
258
        WARN("Could not get child iterator! hr = %08lx\n", hr);
259 260
        IShellFolder_Release(lpsfParent);
        IShellFolder_Release(lpsfRoot);
261
        ILFree(pidlChild);
262
        ILFree(pidlParent);
263 264 265
        return;
    }

266
    SendMessageW( info->hwndTreeView, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT );
267 268
    item = InsertTreeViewItem( info, lpsfParent, pidlChild,
                               pidlParent, pEnumChildren, TVI_ROOT );
269
    SendMessageW( info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item );
270

271
    ILFree(pidlChild);
272
    ILFree(pidlParent);
273 274
    IShellFolder_Release(lpsfRoot);
    IShellFolder_Release(lpsfParent);
275 276
}

277
static int GetIcon(LPCITEMIDLIST lpi, UINT uFlags)
278
{
279
    SHFILEINFOW sfi;
280 281 282 283
    IImageList *list;

    list = (IImageList *)SHGetFileInfoW((LPCWSTR)lpi, 0 ,&sfi, sizeof(SHFILEINFOW), uFlags);
    if (list) IImageList_Release(list);
284
    return sfi.iIcon;
285 286
}

287
static void GetNormalAndSelectedIcons(LPITEMIDLIST lpifq, LPTVITEMW lpTV_ITEM)
288
{
289 290
    LPITEMIDLIST pidlDesktop = NULL;
    DWORD flags;
291

292
    TRACE("%p %p\n",lpifq, lpTV_ITEM);
293

294 295 296 297 298
    if (!lpifq)
    {
        pidlDesktop = _ILCreateDesktop();
        lpifq = pidlDesktop;
    }
299

300 301
    flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON;
    lpTV_ITEM->iImage = GetIcon( lpifq, flags );
302

303 304
    flags = SHGFI_PIDL | SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_OPENICON;
    lpTV_ITEM->iSelectedImage = GetIcon( lpifq, flags );
305

306 307
    if (pidlDesktop)
        ILFree( pidlDesktop );
308 309
}

310 311 312
/******************************************************************************
 * GetName [Internal]
 *
313
 * Query a shell folder for the display name of one of its children
314 315 316 317 318 319 320 321 322 323 324
 *
 * PARAMS
 *  lpsf           [I] IShellFolder interface of the folder to be queried.
 *  lpi            [I] ITEMIDLIST of the child, relative to parent
 *  dwFlags        [I] as in IShellFolder::GetDisplayNameOf
 *  lpFriendlyName [O] The desired display name in unicode
 *
 * RETURNS
 *  Success: TRUE
 *  Failure: FALSE
 */
325
static BOOL GetName(LPSHELLFOLDER lpsf, LPCITEMIDLIST lpi, DWORD dwFlags, LPWSTR lpFriendlyName)
326
{
327
	BOOL   bSuccess=TRUE;
328 329
	STRRET str;

330
	TRACE("%p %p %lx %p\n", lpsf, lpi, dwFlags, lpFriendlyName);
331
	if (SUCCEEDED(IShellFolder_GetDisplayNameOf(lpsf, lpi, dwFlags, &str)))
332
          bSuccess = StrRetToStrNW(lpFriendlyName, MAX_PATH, &str, lpi);
333 334 335
	else
	  bSuccess = FALSE;

336
	TRACE("-- %s\n", debugstr_w(lpFriendlyName));
337 338 339
	return bSuccess;
}

340 341 342 343
/******************************************************************************
 * InsertTreeViewItem [Internal]
 *
 * PARAMS
344
 *  info       [I] data for the dialog
345
 *  lpsf       [I] IShellFolder interface of the item's parent shell folder 
346
 *  pidl       [I] ITEMIDLIST of the child to insert, relative to parent
347 348 349 350 351 352 353 354
 *  pidlParent [I] ITEMIDLIST of the parent shell folder
 *  pEnumIL    [I] Iterator for the children of the item to be inserted
 *  hParent    [I] The treeview-item that represents the parent shell folder
 *
 * RETURNS
 *  Success: Handle to the created and inserted treeview-item
 *  Failure: NULL
 */
355 356 357
static HTREEITEM InsertTreeViewItem( browse_info *info, IShellFolder * lpsf,
    LPCITEMIDLIST pidl, LPCITEMIDLIST pidlParent, IEnumIDList* pEnumIL,
    HTREEITEM hParent)
358
{
359 360 361
	TVITEMW 	tvi;
	TVINSERTSTRUCTW	tvins;
	WCHAR		szBuff[MAX_PATH];
362
	LPTV_ITEMDATA	lptvid=0;
363

364
	tvi.mask  = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
365

366 367
	tvi.cChildren= pEnumIL ? 1 : 0;
	tvi.mask |= TVIF_CHILDREN;
368

369
	if (!GetName(lpsf, pidl, SHGDN_NORMAL, szBuff))
370
	    return NULL;
371

372 373
	lptvid = SHAlloc( sizeof(TV_ITEMDATA) );
	if (!lptvid)
374
	    return NULL;
375

376 377 378
	tvi.pszText    = szBuff;
	tvi.cchTextMax = MAX_PATH;
	tvi.lParam = (LPARAM)lptvid;
379

380 381 382 383 384 385
	IShellFolder_AddRef(lpsf);
	lptvid->lpsfParent = lpsf;
	lptvid->lpi	= ILClone(pidl);
	lptvid->lpifq	= pidlParent ? ILCombine(pidlParent, pidl) : ILClone(pidl);
	lptvid->pEnumIL = pEnumIL;
	GetNormalAndSelectedIcons(lptvid->lpifq, &tvi);
386

387
	tvins.u.item       = tvi;
388 389
	tvins.hInsertAfter = NULL;
	tvins.hParent      = hParent;
390

391
	return TreeView_InsertItemW( info->hwndTreeView, &tvins );
392
}
393

394 395 396 397 398 399 400
/******************************************************************************
 * FillTreeView [Internal]
 *
 * For each child (given by lpe) of the parent shell folder, which is given by 
 * lpsf and whose PIDL is pidl, insert a treeview-item right under hParent
 *
 * PARAMS
401
 *  info    [I] data for the dialog
402 403 404 405 406
 *  lpsf    [I] IShellFolder interface of the parent shell folder
 *  pidl    [I] ITEMIDLIST of the parent shell folder
 *  hParent [I] The treeview item that represents the parent shell folder
 *  lpe     [I] An iterator for the children of the parent shell folder
 */
407 408
static void FillTreeView( browse_info *info, IShellFolder * lpsf,
                 LPITEMIDLIST  pidl, HTREEITEM hParent, IEnumIDList* lpe)
409
{
410
	LPITEMIDLIST	pidlTemp = 0;
411 412
	ULONG		ulFetched;
	HRESULT		hr;
413
	HWND		hwnd = GetParent( info->hwndTreeView );
414

Kevin Koltzau's avatar
Kevin Koltzau committed
415
	TRACE("%p %p %p %p\n",lpsf, pidl, hParent, lpe);
416 417 418 419

	/* No IEnumIDList -> No children */
	if (!lpe) return;
	
420 421
	SetCapture( hwnd );
	SetCursor( LoadCursorA( 0, (LPSTR)IDC_WAIT ) );
422

423
	while (S_OK == IEnumIDList_Next(lpe,1,&pidlTemp,&ulFetched))
424 425 426 427
	{
	    ULONG ulAttrs = SFGAO_HASSUBFOLDER | SFGAO_FOLDER;
	    IEnumIDList* pEnumIL = NULL;
	    IShellFolder* pSFChild = NULL;
428
	    IShellFolder_GetAttributesOf(lpsf, 1, (LPCITEMIDLIST*)&pidlTemp, &ulAttrs);
429 430 431 432
	    if (ulAttrs & SFGAO_FOLDER)
	    {
	        hr = IShellFolder_BindToObject(lpsf,pidlTemp,NULL,&IID_IShellFolder,(LPVOID*)&pSFChild);
	        if (SUCCEEDED(hr))
433
                {
434 435
	            DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
	            hr = IShellFolder_EnumObjects(pSFChild, hwnd, flags, &pEnumIL);
436
                    if (hr == S_OK)
437
                    {
438 439
                        if ((IEnumIDList_Skip(pEnumIL, 1) != S_OK) ||
                             FAILED(IEnumIDList_Reset(pEnumIL)))
440 441 442 443 444 445 446
                        {
                            IEnumIDList_Release(pEnumIL);
                            pEnumIL = NULL;
                        }
                    }
                    IShellFolder_Release(pSFChild);
                }
447
	    }
448

449
	    if (!InsertTreeViewItem(info, lpsf, pidlTemp, pidl, pEnumIL, hParent))
450
	        goto done;
451
	    SHFree(pidlTemp);  /* Finally, free the pidl that the shell gave us... */
452
	    pidlTemp=NULL;
453 454
	}

455
done:
456
	ReleaseCapture();
457
	SetCursor(LoadCursorW(0, (LPWSTR)IDC_ARROW));
458
    SHFree(pidlTemp);
459 460
}

461 462 463 464 465 466 467 468
static inline BOOL PIDLIsType(LPCITEMIDLIST pidl, PIDLTYPE type)
{
    LPPIDLDATA data = _ILGetDataPointer(pidl);
    if (!data)
        return FALSE;
    return (data->type == type);
}

469
static void BrsFolder_CheckValidSelection( browse_info *info, LPTV_ITEMDATA lptvid )
470
{
471
    LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;
472 473 474
    LPCITEMIDLIST pidl = lptvid->lpi;
    BOOL bEnabled = TRUE;
    DWORD dwAttributes;
475 476
    HRESULT r;

477 478 479 480 481 482
    if ((lpBrowseInfo->ulFlags & BIF_BROWSEFORCOMPUTER) &&
        !PIDLIsType(pidl, PT_COMP))
        bEnabled = FALSE;
    if (lpBrowseInfo->ulFlags & BIF_RETURNFSANCESTORS)
    {
        dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
483 484
        r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
                                (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
485
        if (FAILED(r) || !(dwAttributes & (SFGAO_FILESYSANCESTOR|SFGAO_FILESYSTEM)))
486 487
            bEnabled = FALSE;
    }
488 489 490 491 492

    dwAttributes = SFGAO_FOLDER | SFGAO_FILESYSTEM;
    r = IShellFolder_GetAttributesOf(lptvid->lpsfParent, 1,
            (LPCITEMIDLIST*)&lptvid->lpi, &dwAttributes);
    if (FAILED(r) ||
493
            ((dwAttributes & (SFGAO_FOLDER|SFGAO_FILESYSTEM)) != (SFGAO_FOLDER|SFGAO_FILESYSTEM)))
494 495
    {
        if (lpBrowseInfo->ulFlags & BIF_RETURNONLYFSDIRS)
496
            bEnabled = FALSE;
497
        EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), FALSE);
498
    }
499 500 501
    else
        EnableWindow(GetDlgItem(info->hWnd, IDD_MAKENEWFOLDER), TRUE);

502
    SendMessageW(info->hWnd, BFFM_ENABLEOK, 0, bEnabled);
503 504
}

505
static LRESULT BrsFolder_Treeview_Delete( browse_info *info, NMTREEVIEWW *pnmtv )
506
{
507
    LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA)pnmtv->itemOld.lParam;
508

509
    TRACE("TVN_DELETEITEMA/W %p\n", lptvid);
510

511 512 513 514 515 516 517 518
    IShellFolder_Release(lptvid->lpsfParent);
    if (lptvid->pEnumIL)
        IEnumIDList_Release(lptvid->pEnumIL);
    SHFree(lptvid->lpi);
    SHFree(lptvid->lpifq);
    SHFree(lptvid);
    return 0;
}
519

520
static LRESULT BrsFolder_Treeview_Expand( browse_info *info, NMTREEVIEWW *pnmtv )
521 522 523 524
{
    IShellFolder *lpsf2 = NULL;
    LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
    HRESULT r;
525

526 527 528 529 530
    TRACE("TVN_ITEMEXPANDINGA/W\n");

    if ((pnmtv->itemNew.state & TVIS_EXPANDEDONCE))
        return 0;

531
    if (!_ILIsEmpty(lptvid->lpi)) {
532
        r = IShellFolder_BindToObject( lptvid->lpsfParent, lptvid->lpi, 0,
533
                                       &IID_IShellFolder, (void**)&lpsf2 );
534 535
    } else {
        lpsf2 = lptvid->lpsfParent;
536 537
        IShellFolder_AddRef(lpsf2);
        r = S_OK;
538 539
    }

540
    if (SUCCEEDED(r))
541
    {
542
        FillTreeView( info, lpsf2, lptvid->lpifq, pnmtv->itemNew.hItem, lptvid->pEnumIL);
543 544
        IShellFolder_Release( lpsf2 );
    }
545

546 547 548
    /* My Computer is already sorted and trying to do a simple text
     * sort will only mess things up */
    if (!_ILIsMyComputer(lptvid->lpi))
549 550
        SendMessageW( info->hwndTreeView, TVM_SORTCHILDREN,
                      FALSE, (LPARAM)pnmtv->itemNew.hItem );
551 552

    return 0;
553 554
}

555
static HRESULT BrsFolder_Treeview_Changed( browse_info *info, NMTREEVIEWW *pnmtv )
556 557
{
    LPTV_ITEMDATA lptvid = (LPTV_ITEMDATA) pnmtv->itemNew.lParam;
558
    WCHAR name[MAX_PATH];
559

560 561
    ILFree(info->pidlRet);
    info->pidlRet = ILClone(lptvid->lpifq);
562 563 564 565

    if (GetName(lptvid->lpsfParent, lptvid->lpi, SHGDN_NORMAL, name))
            SetWindowTextW( GetDlgItem(info->hWnd, IDD_FOLDERTEXT), name );

566 567 568
    browsefolder_callback( info->lpBrowseInfo, info->hWnd, BFFM_SELCHANGED,
                           (LPARAM)info->pidlRet );
    BrsFolder_CheckValidSelection( info, lptvid );
569
    return S_OK;
570
}
571

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
static LRESULT BrsFolder_Treeview_Rename(browse_info *info, NMTVDISPINFOW *pnmtv)
{
    LPTV_ITEMDATA item_data;
    WCHAR old_path[MAX_PATH], new_path[MAX_PATH], *p;
    NMTREEVIEWW nmtv;
    TVITEMW item;

    if(!pnmtv->item.pszText)
        return 0;

    item.mask = TVIF_HANDLE|TVIF_PARAM;
    item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
    SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
    item_data = (LPTV_ITEMDATA)item.lParam;

    SHGetPathFromIDListW(item_data->lpifq, old_path);
588
    if(!(p = wcsrchr(old_path, '\\')))
589 590 591
        return 0;
    p = new_path+(p-old_path+1);
    memcpy(new_path, old_path, (p-new_path)*sizeof(WCHAR));
592
    lstrcpyW(p, pnmtv->item.pszText);
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

    if(!MoveFileW(old_path, new_path))
        return 0;

    SHFree(item_data->lpifq);
    SHFree(item_data->lpi);
    item_data->lpifq = SHSimpleIDListFromPathW(new_path);
    IShellFolder_ParseDisplayName(item_data->lpsfParent, NULL, NULL,
            pnmtv->item.pszText, NULL, &item_data->lpi, NULL);

    item.mask = TVIF_HANDLE|TVIF_TEXT;
    item.pszText = pnmtv->item.pszText;
    SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);

    nmtv.itemNew.lParam = item.lParam;
    BrsFolder_Treeview_Changed(info, &nmtv);
    return 0;
}

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
static HRESULT BrsFolder_Rename(browse_info *info, HTREEITEM rename)
{
    SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)rename);
    SendMessageW(info->hwndTreeView, TVM_EDITLABELW, 0, (LPARAM)rename);
    return S_OK;
}

static LRESULT BrsFolder_Treeview_Keydown(browse_info *info, LPNMTVKEYDOWN keydown)
{
    HTREEITEM selected_item;

    /* Old dialog doesn't support those advanced features */
    if (!(info->lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
        return 0;

    selected_item = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);

    switch (keydown->wVKey)
    {
    case VK_F2:
        BrsFolder_Rename(info, selected_item);
        break;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
    case VK_DELETE:
        {
            const ITEMIDLIST *item_id;
            ISFHelper *psfhlp;
            HRESULT hr;
            TVITEMW item;
            TV_ITEMDATA *item_data;

            item.mask  = TVIF_PARAM;
            item.hItem = selected_item;
            SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
            item_data = (TV_ITEMDATA *)item.lParam;
            item_id = item_data->lpi;

            hr = IShellFolder_QueryInterface(item_data->lpsfParent, &IID_ISFHelper, (void**)&psfhlp);
            if(FAILED(hr))
                return 0;

            /* perform the item deletion - tree view gets updated over shell notification */
            ISFHelper_DeleteItems(psfhlp, 1, &item_id);
            ISFHelper_Release(psfhlp);
        }
        break;
657 658 659 660
    }
    return 0;
}

661
static LRESULT BrsFolder_OnNotify( browse_info *info, UINT CtlID, LPNMHDR lpnmh )
662
{
663
    NMTREEVIEWW *pnmtv = (NMTREEVIEWW *)lpnmh;
664

665
    TRACE("%p %x %p msg=%x\n", info, CtlID, lpnmh, pnmtv->hdr.code);
666

667 668
    if (pnmtv->hdr.idFrom != IDD_TREEVIEW)
        return 0;
669

670 671 672 673
    switch (pnmtv->hdr.code)
    {
    case TVN_DELETEITEMA:
    case TVN_DELETEITEMW:
674
        return BrsFolder_Treeview_Delete( info, pnmtv );
675

676 677
    case TVN_ITEMEXPANDINGA:
    case TVN_ITEMEXPANDINGW:
678
        return BrsFolder_Treeview_Expand( info, pnmtv );
679

680 681
    case TVN_SELCHANGEDA:
    case TVN_SELCHANGEDW:
682
        return BrsFolder_Treeview_Changed( info, pnmtv );
683

684 685 686 687
    case TVN_ENDLABELEDITA:
    case TVN_ENDLABELEDITW:
        return BrsFolder_Treeview_Rename( info, (LPNMTVDISPINFOW)pnmtv );

688 689 690
    case TVN_KEYDOWN:
        return BrsFolder_Treeview_Keydown( info, (LPNMTVKEYDOWN)pnmtv );

691 692 693 694 695 696 697 698 699
    default:
        WARN("unhandled (%d)\n", pnmtv->hdr.code);
        break;
    }

    return 0;
}


700
static BOOL BrsFolder_OnCreate( HWND hWnd, browse_info *info )
701
{
702 703
    LPITEMIDLIST computer_pidl;
    SHChangeNotifyEntry ntreg;
704 705 706
    LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;

    info->hWnd = hWnd;
707
    SetPropW( hWnd, L"__WINE_BRSFOLDERDLG_INFO", info );
708

709 710
    if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
        FIXME("flags BIF_NEWDIALOGSTYLE partially implemented\n");
711 712 713
    if (lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS)
	FIXME("flags %x not implemented\n", lpBrowseInfo->ulFlags & ~SUPPORTEDFLAGS);

714 715 716 717
    if (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
    {
        RECT rcWnd;

718
        info->layout = LayoutInit(hWnd, g_layout_info, ARRAY_SIZE(g_layout_info));
719 720 721 722 723 724

        /* TODO: Windows allows shrinking the windows a bit */
        GetWindowRect(hWnd, &rcWnd);
        info->szMin.cx = rcWnd.right - rcWnd.left;
        info->szMin.cy = rcWnd.bottom - rcWnd.top;
    }
725 726 727 728
    else
    {
        info->layout = NULL;
    }
729

730 731 732 733 734
    if (lpBrowseInfo->lpszTitle)
	SetWindowTextW( GetDlgItem(hWnd, IDD_TITLE), lpBrowseInfo->lpszTitle );
    else
	ShowWindow( GetDlgItem(hWnd, IDD_TITLE), SW_HIDE );

735 736
    if (!(lpBrowseInfo->ulFlags & BIF_STATUSTEXT)
        || (lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
737 738
	ShowWindow( GetDlgItem(hWnd, IDD_STATUS), SW_HIDE );

739 740 741 742 743 744 745 746 747 748 749 750
    /* Hide "Make New Folder" Button? */
    if ((lpBrowseInfo->ulFlags & BIF_NONEWFOLDERBUTTON)
        || !(lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE))
        ShowWindow( GetDlgItem(hWnd, IDD_MAKENEWFOLDER), SW_HIDE );

    /* Hide the editbox? */
    if (!(lpBrowseInfo->ulFlags & BIF_EDITBOX))
    {
        ShowWindow( GetDlgItem(hWnd, IDD_FOLDER), SW_HIDE );
        ShowWindow( GetDlgItem(hWnd, IDD_FOLDERTEXT), SW_HIDE );
    }

751 752
    info->hwndTreeView = GetDlgItem( hWnd, IDD_TREEVIEW );
    if (info->hwndTreeView)
753
    {
754
        InitializeTreeView( info );
755 756 757 758 759 760 761 762 763 764 765

        /* Resize the treeview if there's not editbox */
        if ((lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE)
            && !(lpBrowseInfo->ulFlags & BIF_EDITBOX))
        {
            RECT rc;
            GetClientRect(info->hwndTreeView, &rc);
            SetWindowPos(info->hwndTreeView, HWND_TOP, 0, 0,
                         rc.right, rc.bottom + 40, SWP_NOMOVE);
        }
    }
766 767
    else
        ERR("treeview control missing!\n");
768

769 770 771 772 773 774 775 776
    /* Register for change notifications */
    SHGetFolderLocation(NULL, CSIDL_DESKTOP, NULL, 0, &computer_pidl);

    ntreg.pidl = computer_pidl;
    ntreg.fRecursive = TRUE;

    info->hNotify = SHChangeNotifyRegister(hWnd, SHCNRF_InterruptLevel, SHCNE_ALLEVENTS, SHV_CHANGE_NOTIFY, 1, &ntreg);

777
    browsefolder_callback( info->lpBrowseInfo, hWnd, BFFM_INITIALIZED, 0 );
778

779 780
    ILFree(computer_pidl);

781 782 783
    return TRUE;
}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
static HRESULT BrsFolder_NewFolder(browse_info *info)
{
    DWORD flags = BrowseFlagsToSHCONTF(info->lpBrowseInfo->ulFlags);
    IShellFolder *desktop, *cur;
    ISFHelper *sfhelper;
    WCHAR name[MAX_PATH];
    HTREEITEM parent, added;
    LPTV_ITEMDATA item_data;
    LPITEMIDLIST new_item;
    TVITEMW item;
    HRESULT hr;
    int len;

    if(!info->pidlRet) {
        ERR("Make new folder button should be disabled\n");
        return E_FAIL;
    }

    /* Create new directory */
    hr = SHGetDesktopFolder(&desktop);
    if(FAILED(hr))
        return hr;
    hr = IShellFolder_BindToObject(desktop, info->pidlRet, 0, &IID_IShellFolder, (void**)&cur);
    IShellFolder_Release(desktop);
    if(FAILED(hr))
        return hr;

    hr = IShellFolder_QueryInterface(cur, &IID_ISFHelper, (void**)&sfhelper);
    if(FAILED(hr))
        return hr;

815 816
    if(!SHGetPathFromIDListW(info->pidlRet, name)) {
        hr = E_FAIL;
817
        goto cleanup;
818
    }
819

820
    len = lstrlenW(name);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
    if(len<MAX_PATH)
        name[len++] = '\\';
    hr = ISFHelper_GetUniqueName(sfhelper, &name[len], MAX_PATH-len);
    ISFHelper_Release(sfhelper);
    if(FAILED(hr))
        goto cleanup;

    hr = E_FAIL;
    if(!CreateDirectoryW(name, NULL))
        goto cleanup;

    /* Update parent of newly created directory */
    parent = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CARET, 0);
    if(!parent)
        goto cleanup;

    SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)parent);

    memset(&item, 0, sizeof(TVITEMW));
    item.mask = TVIF_PARAM|TVIF_STATE;
    item.hItem = parent;
    SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
    item_data = (LPTV_ITEMDATA)item.lParam;
    if(!item_data)
        goto cleanup;

    if(item_data->pEnumIL)
        IEnumIDList_Release(item_data->pEnumIL);
    hr = IShellFolder_EnumObjects(cur, info->hwndTreeView, flags, &item_data->pEnumIL);
    if(FAILED(hr))
        goto cleanup;

    /* Update treeview */
    if(!(item.state&TVIS_EXPANDEDONCE)) {
        item.mask = TVIF_STATE;
        item.state = TVIS_EXPANDEDONCE;
        item.stateMask = TVIS_EXPANDEDONCE;
        SendMessageW(info->hwndTreeView, TVM_SETITEMW, 0, (LPARAM)&item);
    }

    hr = IShellFolder_ParseDisplayName(cur, NULL, NULL, name+len, NULL, &new_item, NULL);
    if(FAILED(hr))
        goto cleanup;

    added = InsertTreeViewItem(info, cur, new_item, item_data->lpifq, NULL, parent);
    IShellFolder_Release(cur);
    SHFree(new_item);

    SendMessageW(info->hwndTreeView, TVM_SORTCHILDREN, FALSE, (LPARAM)parent);
    return BrsFolder_Rename(info, added);

cleanup:
    return hr;
}

876
static BOOL BrsFolder_OnCommand( browse_info *info, UINT id )
877
{
878 879
    LPBROWSEINFOW lpBrowseInfo = info->lpBrowseInfo;

880 881 882
    switch (id)
    {
    case IDOK:
883 884
        if (info->pidlRet == NULL) /* A null pidl would mean a cancel */
            info->pidlRet = _ILCreateDesktop();
885
        pdump( info->pidlRet );
886
        if (lpBrowseInfo->pszDisplayName)
887 888
            SHGetPathFromIDListW( info->pidlRet, lpBrowseInfo->pszDisplayName );
        EndDialog( info->hWnd, 1 );
889 890 891
        return TRUE;

    case IDCANCEL:
892
        EndDialog( info->hWnd, 0 );
893
        return TRUE;
894 895

    case IDD_MAKENEWFOLDER:
896
        BrsFolder_NewFolder(info);
897
        return TRUE;
898 899 900 901
    }
    return FALSE;
}

902 903 904
static BOOL BrsFolder_OnSetExpanded(browse_info *info, LPVOID selection, 
    BOOL is_str, HTREEITEM *pItem)
{
905
    LPITEMIDLIST pidlSelection = selection;
906 907 908
    LPCITEMIDLIST pidlCurrent, pidlRoot;
    TVITEMEXW item;
    BOOL bResult = FALSE;
909 910 911

    memset(&item, 0, sizeof(item));

912 913 914 915 916 917 918 919 920 921
    /* If 'selection' is a string, convert to a Shell ID List. */ 
    if (is_str) {
        IShellFolder *psfDesktop;
        HRESULT hr;

        hr = SHGetDesktopFolder(&psfDesktop);
        if (FAILED(hr))
            goto done;

        hr = IShellFolder_ParseDisplayName(psfDesktop, NULL, NULL, 
922
                     selection, NULL, &pidlSelection, NULL);
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
        IShellFolder_Release(psfDesktop);
        if (FAILED(hr)) 
            goto done;
    }

    /* Move pidlCurrent behind the SHITEMIDs in pidlSelection, which are the root of
     * the sub-tree currently displayed. */
    pidlRoot = info->lpBrowseInfo->pidlRoot;
    pidlCurrent = pidlSelection;
    while (!_ILIsEmpty(pidlRoot) && _ILIsEqualSimple(pidlRoot, pidlCurrent)) {
        pidlRoot = ILGetNext(pidlRoot);
        pidlCurrent = ILGetNext(pidlCurrent);
    }

    /* The given ID List is not part of the SHBrowseForFolder's current sub-tree. */
    if (!_ILIsEmpty(pidlRoot))
        goto done;

    /* Initialize item to point to the first child of the root folder. */
    item.mask = TVIF_PARAM;
943 944 945 946 947
    item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);

    if (item.hItem)
        item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
                                             (LPARAM)item.hItem);
948 949 950 951 952

    /* Walk the tree along the nodes corresponding to the remaining ITEMIDLIST */
    while (item.hItem && !_ILIsEmpty(pidlCurrent)) {
        LPTV_ITEMDATA pItemData;

953
        SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
954 955 956 957 958
        pItemData = (LPTV_ITEMDATA)item.lParam;

        if (_ILIsEqualSimple(pItemData->lpi, pidlCurrent)) {
            pidlCurrent = ILGetNext(pidlCurrent);
            if (!_ILIsEmpty(pidlCurrent)) {
959
                /* Only expand current node and move on to its first child,
960
                 * if we didn't already reach the last SHITEMID */
961
                SendMessageW(info->hwndTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)item.hItem);
962 963
                item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD,
                                             (LPARAM)item.hItem);
964 965
            }
        } else {
966 967
            item.hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT,
                                             (LPARAM)item.hItem);
968 969 970 971 972 973 974
        }
    }

    if (_ILIsEmpty(pidlCurrent) && item.hItem) 
        bResult = TRUE;

done:
975
    if (pidlSelection && pidlSelection != selection)
976 977 978 979 980 981 982 983 984 985 986 987
        ILFree(pidlSelection);

    if (pItem) 
        *pItem = item.hItem;
    
    return bResult;
}

static BOOL BrsFolder_OnSetSelectionW(browse_info *info, LPVOID selection, BOOL is_str) {
    HTREEITEM hItem;
    BOOL bResult;

988 989
    if (!selection) return FALSE;

990 991
    bResult = BrsFolder_OnSetExpanded(info, selection, is_str, &hItem);
    if (bResult)
992
        SendMessageW(info->hwndTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem );
993 994 995 996 997 998 999 1000 1001 1002
    return bResult;
}

static BOOL BrsFolder_OnSetSelectionA(browse_info *info, LPVOID selection, BOOL is_str) {
    LPWSTR selectionW = NULL;
    BOOL result = FALSE;
    int length;
    
    if (!is_str)
        return BrsFolder_OnSetSelectionW(info, selection, is_str);
1003 1004

    if ((length = MultiByteToWideChar(CP_ACP, 0, selection, -1, NULL, 0)) &&
1005
        (selectionW = heap_alloc(length * sizeof(WCHAR))) &&
1006
        MultiByteToWideChar(CP_ACP, 0, selection, -1, selectionW, length))
1007 1008 1009 1010
    {
        result = BrsFolder_OnSetSelectionW(info, selectionW, is_str);
    }

1011
    heap_free(selectionW);
1012 1013 1014
    return result;
}

1015
static LRESULT BrsFolder_OnWindowPosChanging(browse_info *info, WINDOWPOS *pos)
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
{
    if ((info->lpBrowseInfo->ulFlags & BIF_NEWDIALOGSTYLE) && !(pos->flags & SWP_NOSIZE))
    {
        if (pos->cx < info->szMin.cx)
            pos->cx = info->szMin.cx;
        if (pos->cy < info->szMin.cy)
            pos->cy = info->szMin.cy;
    }
    return 0;
}

1027 1028 1029 1030 1031 1032 1033 1034
static INT BrsFolder_OnDestroy(browse_info *info)
{
    if (info->layout)
    {
        SHFree(info->layout);
        info->layout = NULL;
    }

1035 1036
    SHChangeNotifyDeregister(info->hNotify);

1037 1038 1039
    return 0;
}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
/* Find a treeview node by recursively walking the treeview */
static HTREEITEM BrsFolder_FindItemByPidl(browse_info *info, LPCITEMIDLIST pidl, HTREEITEM hItem)
{
    TV_ITEMW item;
    TV_ITEMDATA *item_data;
    HRESULT hr;

    item.mask = TVIF_HANDLE | TVIF_PARAM;
    item.hItem = hItem;
    SendMessageW(info->hwndTreeView, TVM_GETITEMW, 0, (LPARAM)&item);
    item_data = (TV_ITEMDATA *)item.lParam;

    hr = IShellFolder_CompareIDs(item_data->lpsfParent, 0, item_data->lpifq, pidl);
    if(SUCCEEDED(hr) && !HRESULT_CODE(hr))
        return hItem;

    hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_CHILD, (LPARAM)hItem);

    while (hItem)
    {
        HTREEITEM newItem = BrsFolder_FindItemByPidl(info, pidl, hItem);
        if (newItem)
            return newItem;
        hItem = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_NEXT, (LPARAM)hItem);
    }
    return NULL;
}

static LRESULT BrsFolder_OnChange(browse_info *info, const LPCITEMIDLIST *pidls, LONG event)
{
    BOOL ret = TRUE;

1072
    TRACE("(%p)->(%p, %p, 0x%08lx)\n", info, pidls[0], pidls[1], event);
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090

    switch (event)
    {
        case SHCNE_RMDIR:
        case SHCNE_DELETE:
        {
            HTREEITEM handle_root = (HTREEITEM)SendMessageW(info->hwndTreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
            HTREEITEM handle_item = BrsFolder_FindItemByPidl(info, pidls[0], handle_root);

            if (handle_item)
                SendMessageW(info->hwndTreeView, TVM_DELETEITEM, 0, (LPARAM)handle_item);

            break;
        }
    }
    return ret;
}

1091 1092 1093 1094 1095 1096
/*************************************************************************
 *             BrsFolderDlgProc32  (not an exported API function)
 */
static INT_PTR CALLBACK BrsFolderDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
				          LPARAM lParam )
{
1097 1098
    browse_info *info;

1099
    TRACE("hwnd=%p msg=%04x 0x%08Ix 0x%08Ix\n", hWnd, msg, wParam, lParam );
1100 1101

    if (msg == WM_INITDIALOG)
1102 1103
        return BrsFolder_OnCreate( hWnd, (browse_info*) lParam );

1104
    info = GetPropW( hWnd, L"__WINE_BRSFOLDERDLG_INFO" );
1105 1106
    if (!info)
        return FALSE;
1107 1108 1109 1110

    switch (msg)
    {
    case WM_NOTIFY:
1111
        return BrsFolder_OnNotify( info, (UINT)wParam, (LPNMHDR)lParam);
1112 1113

    case WM_COMMAND:
1114
        return BrsFolder_OnCommand( info, wParam );
1115

1116 1117 1118 1119 1120
    case WM_WINDOWPOSCHANGING:
        return BrsFolder_OnWindowPosChanging( info, (WINDOWPOS *)lParam);

    case WM_SIZE:
        if (info->layout)  /* new style dialogs */
1121
            LayoutUpdate(hWnd, info->layout, g_layout_info, ARRAY_SIZE(g_layout_info));
1122 1123
        return 0;

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    case BFFM_SETSTATUSTEXTA:
        TRACE("Set status %s\n", debugstr_a((LPSTR)lParam));
        SetWindowTextA(GetDlgItem(hWnd, IDD_STATUS), (LPSTR)lParam);
        break;

    case BFFM_SETSTATUSTEXTW:
        TRACE("Set status %s\n", debugstr_w((LPWSTR)lParam));
        SetWindowTextW(GetDlgItem(hWnd, IDD_STATUS), (LPWSTR)lParam);
        break;

    case BFFM_ENABLEOK:
1135
        TRACE("Enable %Id\n", lParam);
1136
        EnableWindow(GetDlgItem(hWnd, 1), lParam != 0);
1137 1138 1139
        break;

    case BFFM_SETOKTEXT: /* unicode only */
1140 1141
        TRACE("Set OK text %s\n", debugstr_w((LPWSTR)lParam));
        SetWindowTextW(GetDlgItem(hWnd, 1), (LPWSTR)lParam);
1142 1143 1144
        break;

    case BFFM_SETSELECTIONA:
1145
        return BrsFolder_OnSetSelectionA(info, (LPVOID)lParam, (BOOL)wParam);
1146 1147

    case BFFM_SETSELECTIONW:
1148
        return BrsFolder_OnSetSelectionW(info, (LPVOID)lParam, (BOOL)wParam);
1149 1150

    case BFFM_SETEXPANDED: /* unicode only */
1151
        return BrsFolder_OnSetExpanded(info, (LPVOID)lParam, (BOOL)wParam, NULL);
1152

1153 1154 1155
    case SHV_CHANGE_NOTIFY:
        return BrsFolder_OnChange(info, (const LPCITEMIDLIST*)wParam, (LONG)lParam);

1156 1157
    case WM_DESTROY:
        return BrsFolder_OnDestroy(info);
1158 1159
    }
    return FALSE;
1160 1161 1162
}

/*************************************************************************
1163
 * SHBrowseForFolderA [SHELL32.@]
Patrik Stridvall's avatar
Patrik Stridvall committed
1164
 * SHBrowseForFolder  [SHELL32.@]
1165
 */
1166
LPITEMIDLIST WINAPI SHBrowseForFolderA (LPBROWSEINFOA lpbi)
1167
{
1168 1169 1170
    BROWSEINFOW bi;
    LPITEMIDLIST lpid;
    INT len;
1171 1172
    LPWSTR title;

1173
    TRACE("%p\n", lpbi);
1174

1175 1176 1177
    bi.hwndOwner = lpbi->hwndOwner;
    bi.pidlRoot = lpbi->pidlRoot;
    if (lpbi->pszDisplayName)
1178
        bi.pszDisplayName = heap_alloc( MAX_PATH * sizeof(WCHAR) );
1179 1180
    else
        bi.pszDisplayName = NULL;
1181

1182 1183 1184
    if (lpbi->lpszTitle)
    {
        len = MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, NULL, 0 );
1185
        title = heap_alloc( len * sizeof(WCHAR) );
1186
        MultiByteToWideChar( CP_ACP, 0, lpbi->lpszTitle, -1, title, len );
1187 1188
    }
    else
1189
        title = NULL;
1190

1191
    bi.lpszTitle = title;
1192 1193 1194 1195 1196 1197 1198 1199 1200
    bi.ulFlags = lpbi->ulFlags;
    bi.lpfn = lpbi->lpfn;
    bi.lParam = lpbi->lParam;
    bi.iImage = lpbi->iImage;
    lpid = SHBrowseForFolderW( &bi );
    if (bi.pszDisplayName)
    {
        WideCharToMultiByte( CP_ACP, 0, bi.pszDisplayName, -1,
                             lpbi->pszDisplayName, MAX_PATH, 0, NULL);
1201
        heap_free( bi.pszDisplayName );
1202
    }
1203
    heap_free(title);
1204 1205
    lpbi->iImage = bi.iImage;
    return lpid;
1206
}
1207

1208

1209 1210
/*************************************************************************
 * SHBrowseForFolderW [SHELL32.@]
1211
 *
1212
 * NOTES
1213
 *  crashes when passed a null pointer
1214 1215 1216
 */
LPITEMIDLIST WINAPI SHBrowseForFolderW (LPBROWSEINFOW lpbi)
{
1217 1218
    browse_info info;
    DWORD r;
1219
    HRESULT hr;
1220
    const WCHAR * templateName;
1221
    INITCOMMONCONTROLSEX icex;
1222 1223 1224 1225 1226

    info.hWnd = 0;
    info.pidlRet = NULL;
    info.lpBrowseInfo = lpbi;
    info.hwndTreeView = NULL;
1227

1228 1229 1230 1231
    icex.dwSize = sizeof( icex );
    icex.dwICC = ICC_TREEVIEW_CLASSES;
    InitCommonControlsEx( &icex );

1232
    hr = OleInitialize(NULL);
1233 1234

    if (lpbi->ulFlags & BIF_NEWDIALOGSTYLE)
1235
        templateName = L"SHNEWBRSFORFOLDER_MSGBOX";
1236
    else
1237
        templateName = L"SHBRSFORFOLDER_MSGBOX";
1238
    r = DialogBoxParamW( shell32_hInstance, templateName, lpbi->hwndOwner,
1239
	                 BrsFolderDlgProc, (LPARAM)&info );
1240
    if (SUCCEEDED(hr)) 
1241
        OleUninitialize();
1242
    if (!r)
1243 1244
    {
        ILFree(info.pidlRet);
1245
        return NULL;
1246
    }
1247

1248
    return info.pidlRet;
1249
}