amuse/VST/FileOpenDialog.cpp

234 lines
7.5 KiB
C++
Raw Normal View History

#include "FileOpenDialog.hpp"
#define WIN32_LEAN_AND_MEAN
2016-07-14 04:54:46 +00:00
#include <windows.h> // For common windows data types and function headers
#define STRICT_TYPED_ITEMIDS
2016-07-14 04:54:46 +00:00
#include <objbase.h> // For COM headers
#include <shobjidl.h> // for IFileDialogEvents and IFileDialogControlEvents
#include <shlwapi.h>
#include <knownfolders.h> // for KnownFolder APIs/datatypes/function headers
#include <propvarutil.h> // for PROPVAR-related functions
#include <propkey.h> // for the Property key APIs/datatypes
#include <propidl.h> // for the Property System APIs
#include <strsafe.h> // for StringCchPrintfW
#include <shtypes.h> // for COMDLG_FILTERSPEC
#include <new>
// Controls
2016-07-14 04:54:46 +00:00
#define CONTROL_GROUP 2000
#define CONTROL_RADIOBUTTONLIST 2
2016-07-14 04:54:46 +00:00
#define CONTROL_RADIOBUTTON1 1
#define CONTROL_RADIOBUTTON2 2 // It is OK for this to have the same IDas CONTROL_RADIOBUTTONLIST,
// because it is a child control under CONTROL_RADIOBUTTONLIST
// IDs for the Task Dialog Buttons
2016-07-14 04:54:46 +00:00
#define IDC_BASICFILEOPEN 100
#define IDC_ADDITEMSTOCUSTOMPLACES 101
#define IDC_ADDCUSTOMCONTROLS 102
#define IDC_SETDEFAULTVALUESFORPROPERTIES 103
#define IDC_WRITEPROPERTIESUSINGHANDLERS 104
#define IDC_WRITEPROPERTIESWITHOUTUSINGHANDLERS 105
HWND ghMainWnd = 0;
HINSTANCE ghAppInst = 0;
RECT winRect;
2016-07-14 04:54:46 +00:00
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),
2016-07-14 04:54:46 +00:00
{0},
};
return QISearch(this, qit, riid, ppv);
}
2016-07-14 04:54:46 +00:00
IFACEMETHODIMP_(ULONG) AddRef() { return InterlockedIncrement(&_cRef); }
IFACEMETHODIMP_(ULONG) Release()
{
long cRef = InterlockedDecrement(&_cRef);
if (!cRef)
delete this;
return cRef;
}
// IFileDialogEvents methods
2016-07-14 04:54:46 +00:00
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);
IFACEMETHODIMP OnOverwrite(IFileDialog*, IShellItem*, FDE_OVERWRITE_RESPONSE*) { return S_OK; };
// IFileDialogControlEvents methods
2016-07-14 04:54:46 +00:00
IFACEMETHODIMP OnItemSelected(IFileDialogCustomize* pfdc, DWORD dwIDCtl, DWORD dwIDItem);
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:
2016-07-14 04:54:46 +00:00
~CDialogEventHandler(){};
long _cRef;
};
2016-07-14 04:54:46 +00:00
HRESULT CDialogEventHandler_CreateInstance(REFIID riid, void** ppv);
std::wstring openDB()
{
std::wstring ret;
2016-06-17 20:41:39 +00:00
CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
2016-07-14 04:54:46 +00:00
// Cocreate the file open dialog object
IFileDialog* pfd = NULL;
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
// Stuff needed for later
const COMDLG_FILTERSPEC rgFExt[] = {{L"Audio Group Archive (*.*)", L"*.*"}};
2016-07-14 04:54:46 +00:00
// Create event handling
IFileDialogEvents* pfde = NULL;
hr = CDialogEventHandler_CreateInstance(IID_PPV_ARGS(&pfde));
2016-07-14 04:54:46 +00:00
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
// Hook the event handler
DWORD dwCookie;
hr = pfd->Advise(pfde, &dwCookie);
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
// Set options for the dialog
DWORD dwFlags;
2016-07-14 04:54:46 +00:00
// Get options first so we do not override
hr = pfd->GetOptions(&dwFlags);
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
// Get shell items only
hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
// Types of files to display (not default)
hr = pfd->SetFileTypes(ARRAYSIZE(rgFExt), rgFExt);
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
// Set default file type to display
// hr = pfd->SetDefaultExtension(L"sqlite");
2016-07-14 04:54:46 +00:00
// if (SUCCEEDED(hr))
//{
2016-07-14 04:54:46 +00:00
// Show dialog
hr = pfd->Show(NULL);
if (SUCCEEDED(hr))
{
// Get the result once the user clicks on open
IShellItem* result;
hr = pfd->GetResult(&result);
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
// Print out the file name
PWSTR fName = NULL;
2016-07-14 04:54:46 +00:00
hr = result->GetDisplayName(SIGDN_FILESYSPATH, &fName);
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
ret.assign(fName);
CoTaskMemFree(fName);
}
2016-07-14 04:54:46 +00:00
result->Release();
}
2016-07-14 04:54:46 +00:00
}
//}
}
}
}
}
pfd->Unadvise(dwCookie);
}
pfde->Release();
}
pfd->Release();
return ret;
}
2016-07-14 04:54:46 +00:00
HRESULT CDialogEventHandler_CreateInstance(REFIID riid, void** ppv)
{
*ppv = NULL;
2016-07-14 04:54:46 +00:00
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;
}
2016-07-14 04:54:46 +00:00
HRESULT CDialogEventHandler::OnTypeChange(IFileDialog* pfd)
{
2016-07-14 04:54:46 +00:00
IFileSaveDialog* pfsd;
HRESULT hr = pfd->QueryInterface(&pfsd);
if (SUCCEEDED(hr))
{
UINT uIndex;
2016-07-14 04:54:46 +00:00
hr = pfsd->GetFileTypeIndex(&uIndex); // index of current file-type
if (SUCCEEDED(hr))
{
2016-07-14 04:54:46 +00:00
IPropertyDescriptionList* pdl = NULL;
}
pfsd->Release();
}
return hr;
}
// IFileDialogControlEvents
// This method gets called when an dialog control item selection happens (radio-button selection. etc).
// For sample sake, let's react to this event by changing the dialog title.
2016-07-14 04:54:46 +00:00
HRESULT CDialogEventHandler::OnItemSelected(IFileDialogCustomize* pfdc, DWORD dwIDCtl, DWORD dwIDItem)
{
2016-07-14 04:54:46 +00:00
IFileDialog* pfd = NULL;
HRESULT hr = pfdc->QueryInterface(&pfd);
if (SUCCEEDED(hr))
{
if (dwIDCtl == CONTROL_RADIOBUTTONLIST)
{
switch (dwIDItem)
{
case CONTROL_RADIOBUTTON1:
hr = pfd->SetTitle(L"Longhorn Dialog");
break;
case CONTROL_RADIOBUTTON2:
hr = pfd->SetTitle(L"Vista Dialog");
break;
}
}
pfd->Release();
}
return hr;
}