🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #2630

Re: SHBrowseForFolder and filtering

#2630
From: Remy Lebeau (TeamB)
Date: Wed, 07 Feb 2007 18:59
171 lines
5567 bytes
"Bob Gonder" <notbg@notmindspring.invalid> wrote in message
news:6f0ks2166gv2qvl1v7oq0kofrmitcu4mio@4ax.com...

> The remarks section here
> http://msdn2.microsoft.com/en-us/library/ms647664.aspx
> mentions  Custom Filtering (with 6 steps)

I stand corrected.  I never noticed that Microsoft added filtering to
the dialog in XP.  In which case, my earlier example then becomes
something like the following instead:

    #include <shobjidl.h>

    class MyFilter : public IFolderFilter
    {
    private:
        ULONG mRef;
    public:
        MyFilter()
            : mRef(0)
        {
        }

        HRESULT __stdcall QueryInterface(const REFIID riid, void**
ppv)
        {
            if( !ppv )
                return E_POINTER;

            *ppv = NULL;

            if( riid == IID_IUnknown )
                *ppv = (IUnknown*) this;
            else if( riid == IID_IFolderFilter )
                *ppv = (IFolderFilter*) this;

            if( *ppv == NULL )
                return E_NOINTERFACE;

            AddRef();
            return S_OK;
        }

        ULONG __stdcall AddRef()
        {
            return ++mRef;
        }

        ULONG __stdcall Release()
        {
            if( --mRef > 0 )
                return mRef;

            delete this;
            return 0;
        }

        HRESULT __stdcall GetEnumFlags(IShellFolder *psf,
PCIDLIST_ABSOLUTE pidlFolder, HWND *phwnd, DWORD *pgrfFlags)
        {
            *pgrfFlags = SHCONTF_FOLDERS;
            return S_OK;
        }

        HRESULT __stdcall ShouldShow(IShellFolder *psf,
PCIDLIST_ABSOLUTE pidlFolder, PCUITEMID_CHILD pidlItem)
        {
            ULONG attrs = 0;
            psf->GetAttributesOf(1, &pidlFolder, &attrs);

            if( (attrs & SFGAO_FILESYSTEM) && (attrs &
SFGAO_REMOVABLE) )
                return S_OK;

            return S_FALSE;
        }
    };

    #ifndef BFFM_IUNKNOWN
    #define BFFM_IUNKNOWN 5
    #endif

    int CALLBACK FilterCallbackProc(HWND hwnd, UINT uMsg, LPARAM
lParam, LPARAM lpData)
    {
        if( uMsg == BFFM_IUNKNOWN )
        {
            IFolderFilterSite *pSite = NULL;
            ((IUnknown*)
lParam)->QueryInterface(IID_IFolderFilterSite, (void**)&pSite);

            MyFilter *filter = new MyFilter;
            if( FAILED(pSite->SetFilter(filter)) )
                delete filter;
            pSite->Release();
        }

        return 0;
    }

    int CALLBACK FolderCallbackProc(HWND hwnd, UINT uMsg, LPARAM
lParam, LPARAM lpData)
    {
        if( uMsg == BFFM_SELCHANGED )
        {
            LPITEMIDLIST pidl = (LPITEMIDLIST) lpData;
            TCHAR path[MAX_PATH+1] = {0};

            if( SHGetPathFromIDList(pidl, path) )
            {
                if( path[1] == TEXT(':') )
                {
                    path[2] = 0;

                    UINT type = GetDriveType(path);
                    if( (type == DRIVE_REMOVABLE) || (type ==
DRIVE_CDROM) )
                    {
                        SendMessage(hwnd, BFFM_ENABLEOK, TRUE, 0);
                        SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0,
(LPARAM) TEXT(""));
                        return 0;
                    }
                }
            }

            SendMessage(hwnd, BFFM_ENABLEOK, FALSE, 0);
            SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)
TEXT("Select a removable drive/folder only!"));
        }

        return 0;
    }

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        TCHAR buffer[MAX_PATH+1];

        LPITEMIDLIST pidlRoot = NULL;
        SHGetSpecialFolderLocation(Handle, CSIDL_DRIVES, &pidlRoot);

        BROWSEINFO bi = {0};
        bi.hwndOwner = Handle;
        bi.pidlRoot = pidlRoot;
        bi.pszDisplayName = buffer;
        bi.lpszTitle = TEXT("Select a removable drive/folder");
        bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS;

        if( (Win32Platform != VER_PLATFORM_WIN32_NT) ||
(Win32MajorVersion < 5) || ((Win32MajorVersion == 5) &&
(Win32MinorVersion < 1)) )
        {
            bi.lpfn = &FolderCallbackProc;
            bi.ulFlags |= BIF_STATUSTEXT;
        }
        else
            bi.lpfn = &FilterCallbackProc;

        LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
        if( pidlRoot ) CoTaskMemFree(pidlRoot);

        if( pidl )
        {
            //...
            CoTaskMemFree(pidl);
        }
    }


Gambit
Technical Details
Message-ID: <45ca218c@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.albasani.net!feeder06.uucp-net.de!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail
References: <45c9eb0a@newsgroups.borland.com> <6f0ks2166gv2qvl1v7oq0kofrmitcu4mio@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,44855005298c82e
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <45c9eb0a@newsgroups.borland.com> <6f0ks2166gv2qvl1v7oq0kofrmitcu4mio@4ax.com>
Subject: Re: SHBrowseForFolder and filtering
Date: Wed, 7 Feb 2007 10:59:08 -0800
Lines: 173
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.83.34.171
Message-ID: <45ca218c@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1170874764 75.83.34.171 (7 Feb 2007 10:59:24 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.albasani.net!feeder06.uucp-net.de!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail