Here's the code which is basically the same as in the link in my initial post, 
slightly modified.



Code:

// Indices of file types
#define INDEX_COLLADA 1
#define INDEX_OPENSCENEGRAPH 2
#define INDEX_KEYHOLEMARKUPLANGUAGE 3
#define INDEX_ALLFILES 4

#define FILE_TYPE_MODELS 1
#define FILE_TYPE_IMAGES 2
#define FILE_TYPE_TERRAFORM 3

class CDialogEventHandler : public IFileDialogEvents,
                                                        public 
IFileDialogControlEvents
{
public:
        // IUnknown methods
        IFACEMETHODIMP QueryInterface(REFIID riid, void** ppv)
        {
                static const QITAB qit[] = {
                        QITABENT(CDialogEventHandler, IFileDialogEvents),
                        QITABENT(CDialogEventHandler, IFileDialogControlEvents),
                        { 0 },
                };
                return QISearch(this, qit, riid, ppv);
        }

        IFACEMETHODIMP_(ULONG) AddRef()
        {
                return InterlockedIncrement(&_cRef);
        }

        IFACEMETHODIMP_(ULONG) Release()
        {
                long cRef = InterlockedDecrement(&_cRef);
                if (!cRef)
                        delete this;
                return cRef;
        }

        // IFileDialogEvents methods
        IFACEMETHODIMP OnFileOk(IFileDialog *) { return S_OK; };
        IFACEMETHODIMP OnFolderChange(IFileDialog *) { return S_OK; };
        IFACEMETHODIMP OnFolderChanging(IFileDialog *, IShellItem *) { return 
S_OK; };
        IFACEMETHODIMP OnHelp(IFileDialog *) { return S_OK; };
        IFACEMETHODIMP OnSelectionChange(IFileDialog *) { return S_OK; };
        IFACEMETHODIMP OnShareViolation(IFileDialog *, IShellItem *, 
FDE_SHAREVIOLATION_RESPONSE *) { return S_OK; };
        IFACEMETHODIMP OnTypeChange(IFileDialog *pfd) { return S_OK; };
        IFACEMETHODIMP OnOverwrite(IFileDialog *, IShellItem *, 
FDE_OVERWRITE_RESPONSE *) { return S_OK; };

        // IFileDialogControlEvents methods
        IFACEMETHODIMP OnItemSelected(IFileDialogCustomize *pfdc, DWORD 
dwIDCtl, DWORD dwIDItem) { return S_OK; };
        IFACEMETHODIMP OnButtonClicked(IFileDialogCustomize *, DWORD) { return 
S_OK; };
        IFACEMETHODIMP OnCheckButtonToggled(IFileDialogCustomize *, DWORD, 
BOOL) { return S_OK; };
        IFACEMETHODIMP OnControlActivating(IFileDialogCustomize *, DWORD) { 
return S_OK; };

        CDialogEventHandler() : _cRef(1) { };
private:
        ~CDialogEventHandler() { };
        long _cRef;
};

// Instance creation helper
        HRESULT CDialogEventHandler_CreateInstance(REFIID riid, void **ppv)
        {
                *ppv = NULL;
                CDialogEventHandler *pDialogEventHandler = new (std::nothrow) 
CDialogEventHandler();
                HRESULT hr = pDialogEventHandler ? S_OK : E_OUTOFMEMORY;
                if (SUCCEEDED(hr))
                {
                        hr = pDialogEventHandler->QueryInterface(riid, ppv);
                        pDialogEventHandler->Release();
                }
                return hr;
        }

        HRESULT basicFileOpen(std::wstring &foundPath, int fileTypes )
        {
                // CoCreate the File Open Dialog object.
                IFileDialog *pfd = NULL;
                HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, 
                                                  NULL, 
                                                  CLSCTX_INPROC_SERVER, 
                                                  IID_PPV_ARGS(&pfd));

                
                pfd->SetTitle( L"Urban Explorer - Open" );
                

                const COMDLG_FILTERSPEC modelSaveTypes[] =
                {
                        {L"COLLADA (*.dae)", L"*.dae"},
                        {L"OpenSceneGraph (*.osg; *.osgb)", L"*.osg;*.osgb"},
                        {L"Keyhole Markup Language (*.kmz)", L"*.kmz"},
                        {L"All Files (*.*)", L"*.*"}
                };

                const COMDLG_FILTERSPEC imageSaveTypes[] =
                {
                        {L"PNG (*.png)", L"*.png"},
                        {L"JPG (*.jpg)", L"*.jpg"},
                        {L"All Files (*.*)", L"*.*"}
                };

                const COMDLG_FILTERSPEC terraformSaveTypes[] =
                {
                        {L"TFI (*.terraformImage)", L"*.terraformImage"},
                        {L"All Files (*.*)", L"*.*"}
                };

                if (SUCCEEDED(hr))
                {
                        // Create an event handling object, and hook it up to 
the dialog.
                        IFileDialogEvents *pfde = NULL;
                        hr = 
CDialogEventHandler_CreateInstance(IID_PPV_ARGS(&pfde));
                        if (SUCCEEDED(hr))
                        {
                                // Hook up the event handler.
                                DWORD dwCookie;
                                hr = pfd->Advise(pfde, &dwCookie);
                                if (SUCCEEDED(hr))
                                {
                                        // Set the options on the dialog.
                                        DWORD dwFlags;
                                        
                                        // Before setting, always get the 
options first in order 
                                        // not to override existing options.
                                        hr = pfd->GetOptions(&dwFlags);
                                        if (SUCCEEDED(hr))
                                        {
                                                // In this case, get shell 
items only for file system items.
                                                hr = pfd->SetOptions(dwFlags | 
FOS_FORCEFILESYSTEM);
                                                if (SUCCEEDED(hr))
                                                {
                                                        // Set the file types 
to display only. 
                                                        // Notice that this is 
a 1-based array.
                                                        
                                                        int allFilesIndex = 0;
                                                        int arraySize = 0;
                                                        if( fileTypes == 
FILE_TYPE_MODELS ) {
                                                                hr = 
pfd->SetFileTypes(ARRAYSIZE(modelSaveTypes), modelSaveTypes);
                                                                arraySize = 
sizeof(modelSaveTypes) / sizeof(COMDLG_FILTERSPEC);
                                                        }
                                                        else if( fileTypes == 
FILE_TYPE_IMAGES ) {
                                                                hr = 
pfd->SetFileTypes(ARRAYSIZE(imageSaveTypes), imageSaveTypes);
                                                                arraySize = 
sizeof(imageSaveTypes) / sizeof(COMDLG_FILTERSPEC);
                                                        }
                                                        else if( fileTypes == 
FILE_TYPE_TERRAFORM ) {
                                                                hr = 
pfd->SetFileTypes(ARRAYSIZE(terraformSaveTypes), terraformSaveTypes);
                                                                arraySize = 
sizeof(terraformSaveTypes) / sizeof(COMDLG_FILTERSPEC);
                                                        }
                                                        else {
                                                                hr = 
E_INVALIDARG;
                                                        }
                                                        
                                                        if (SUCCEEDED(hr))
                                                        {
                                                                
                                                                // Set the 
selected file type index to All files for this example.
                                                                hr = 
pfd->SetFileTypeIndex(arraySize);
                                                                
                                                                if 
(SUCCEEDED(hr))
                                                                {
                                                                        
                                                                        // Set 
the default extension to be ".*" file.
                                                                        hr = 
pfd->SetDefaultExtension(L"*");
                                                                        
                                                                        if 
(SUCCEEDED(hr))
                                                                        {
                                                                                
                                                                                
// Show the dialog
                                                                                
hr = pfd->Show(NULL);
                                                                                
                                                                                
if (SUCCEEDED(hr))
                                                                                
{
                                                                                
                                                                                
        // Obtain the result once the user clicks 
                                                                                
        // the 'Open' button.
                                                                                
        // The result is an IShellItem object.
                                                                                
        IShellItem *psiResult;
                                                                                
        hr = pfd->GetResult(&psiResult);
                                                                                
        if (SUCCEEDED(hr))
                                                                                
        {               
                                                                                
                // We are just going to print out the 
                                                                                
                // name of the file for sample sake.
                                                                                
                PWSTR pszFilePath = NULL;
                                                                                
                
                                                                                
                hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, 
                                                                                
                                                   &pszFilePath);
                                                                                
                if (SUCCEEDED(hr))
                                                                                
                {
                                                                                
                        foundPath = pszFilePath;
                                                                                
                        CoTaskMemFree(pszFilePath);
                                                                                
                }
                                                                                
                psiResult->Release();
                                                                                
        }
                                                                                
}
                                                                        }
                                                                }
                                                        }
                                                }
                                        }
                                        // Unhook the event handler.
                                        pfd->Unadvise(dwCookie);
                                }
                                pfde->Release();
                        }
                        pfd->Release();
                }
                return hr;
        }



Function call:

Code:
if (SUCCEEDED(basicFileOpen(foundPath, FILE_TYPE_MODELS))) {

                                std::string path;
                                for each(char x in foundPath)
                                {
                                        path += x;
                                }

                                std::cout << "File path: " << path << std::endl;
                        }

[/code]

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=53084#53084





_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to