amuse/VST/FileOpenDialog.cpp

213 lines
6.6 KiB
C++
Raw Permalink 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
2018-12-08 05:20:09 +00:00
#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;
2018-12-08 05:20:09 +00:00
class CDialogEventHandler : public IFileDialogEvents, public IFileDialogControlEvents {
public:
2018-12-08 05:20:09 +00:00
// 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);
IFACEMETHODIMP OnOverwrite(IFileDialog*, IShellItem*, FDE_OVERWRITE_RESPONSE*) { return S_OK; };
// IFileDialogControlEvents methods
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){};
2018-12-08 05:20:09 +00:00
private:
~CDialogEventHandler(){};
long _cRef;
};
2018-12-08 05:20:09 +00:00
HRESULT CDialogEventHandler_CreateInstance(REFIID riid, void** ppv);
2018-12-08 05:20:09 +00:00
std::wstring openDB() {
std::wstring ret;
CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
2018-12-08 05:20:09 +00:00
// Cocreate the file open dialog object
IFileDialog* pfd = NULL;
2016-07-14 04:54:46 +00:00
2018-12-08 05:20:09 +00:00
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd));
2018-12-08 05:20:09 +00:00
if (SUCCEEDED(hr)) {
// Stuff needed for later
const COMDLG_FILTERSPEC rgFExt[] = {{L"Audio Group Archive (*.*)", L"*.*"}};
2018-12-08 05:20:09 +00:00
// Create event handling
IFileDialogEvents* pfde = NULL;
hr = CDialogEventHandler_CreateInstance(IID_PPV_ARGS(&pfde));
if (SUCCEEDED(hr)) {
// Hook the event handler
DWORD dwCookie;
hr = pfd->Advise(pfde, &dwCookie);
if (SUCCEEDED(hr)) {
// Set options for the dialog
DWORD dwFlags;
// Get options first so we do not override
hr = pfd->GetOptions(&dwFlags);
if (SUCCEEDED(hr)) {
// Get shell items only
hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
if (SUCCEEDED(hr)) {
// Types of files to display (not default)
hr = pfd->SetFileTypes(ARRAYSIZE(rgFExt), rgFExt);
if (SUCCEEDED(hr)) {
// Set default file type to display
// hr = pfd->SetDefaultExtension(L"sqlite");
2018-12-08 05:20:09 +00:00
// if (SUCCEEDED(hr))
//{
// 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)) {
// Print out the file name
PWSTR fName = NULL;
hr = result->GetDisplayName(SIGDN_FILESYSPATH, &fName);
if (SUCCEEDED(hr)) {
ret.assign(fName);
CoTaskMemFree(fName);
}
result->Release();
}
2018-12-08 05:20:09 +00:00
}
//}
}
2018-12-08 05:20:09 +00:00
}
}
2018-12-08 05:20:09 +00:00
}
2018-12-08 05:20:09 +00:00
pfd->Unadvise(dwCookie);
}
2018-12-08 05:20:09 +00:00
pfde->Release();
}
pfd->Release();
return ret;
}
2018-12-08 05:20:09 +00:00
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;
}
2018-12-08 05:20:09 +00:00
HRESULT CDialogEventHandler::OnTypeChange(IFileDialog* pfd) {
IFileSaveDialog* pfsd;
HRESULT hr = pfd->QueryInterface(&pfsd);
if (SUCCEEDED(hr)) {
UINT uIndex;
hr = pfsd->GetFileTypeIndex(&uIndex); // index of current file-type
if (SUCCEEDED(hr)) {
IPropertyDescriptionList* pdl = NULL;
}
2018-12-08 05:20:09 +00:00
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.
2018-12-08 05:20:09 +00:00
HRESULT CDialogEventHandler::OnItemSelected(IFileDialogCustomize* pfdc, DWORD dwIDCtl, DWORD dwIDItem) {
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;
}
}
2018-12-08 05:20:09 +00:00
pfd->Release();
}
return hr;
}