| 106 | |
| 107 | #if defined( _WIN32 ) |
| 108 | std::vector<std::filesystem::path> windowsDialog( const MR::FileDialog::Parameters& params = {} ) |
| 109 | { |
| 110 | std::vector<std::filesystem::path> res; |
| 111 | //<SnippetRefCounts> |
| 112 | HRESULT hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED | |
| 113 | COINIT_DISABLE_OLE1DDE ); |
| 114 | |
| 115 | std::vector<COMDLG_FILTERSPEC> filters; |
| 116 | std::vector<std::pair<std::wstring, std::wstring>> filtersWCopy; |
| 117 | |
| 118 | if( SUCCEEDED( hr ) ) |
| 119 | { |
| 120 | IFileDialog* pFileOpen; |
| 121 | |
| 122 | // Create the FileOpenDialog object. |
| 123 | hr = CoCreateInstance( params.saveDialog ? CLSID_FileSaveDialog : CLSID_FileOpenDialog, |
| 124 | NULL, CLSCTX_ALL, |
| 125 | params.saveDialog ? IID_IFileSaveDialog : IID_IFileOpenDialog, |
| 126 | reinterpret_cast<void**>(&pFileOpen) ); |
| 127 | |
| 128 | if( SUCCEEDED( hr ) ) |
| 129 | { |
| 130 | auto baseFolder = params.baseFolder; |
| 131 | if ( baseFolder.empty() ) |
| 132 | baseFolder = MR::FileDialog::getLastUsedDir(); |
| 133 | if( !baseFolder.empty() ) |
| 134 | { |
| 135 | IShellItem* pItem; |
| 136 | hr = SHCreateItemFromParsingName( baseFolder.c_str(), NULL, IID_PPV_ARGS( &pItem ) ); |
| 137 | if( SUCCEEDED( hr ) ) |
| 138 | { |
| 139 | pFileOpen->SetFolder( pItem ); |
| 140 | pItem->Release(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Set the options on the dialog. |
| 145 | DWORD dwFlags; |
| 146 | // Before setting, always get the options first in order |
| 147 | // not to override existing options. |
| 148 | pFileOpen->GetOptions( &dwFlags ); |
| 149 | pFileOpen->SetOptions( dwFlags | (params.multiselect ? FOS_ALLOWMULTISELECT : 0) | |
| 150 | (params.folderDialog ? FOS_PICKFOLDERS : 0) ); |
| 151 | |
| 152 | if( !params.filters.empty() ) |
| 153 | { |
| 154 | unsigned filtersSize = unsigned( params.filters.size() ); |
| 155 | filtersWCopy.resize( filtersSize ); |
| 156 | filters.resize( filtersSize ); |
| 157 | |
| 158 | for( unsigned i = 0; i < filtersSize; ++i ) |
| 159 | { |
| 160 | const auto& [nameU8, filterU8] = params.filters[i]; |
| 161 | auto& [nameW, filterW] = filtersWCopy[i]; |
| 162 | nameW = MR::utf8ToWide( nameU8.c_str() ); |
| 163 | filterW = MR::utf8ToWide( filterU8.c_str() ); |
| 164 | |
| 165 | filters[i].pszName = nameW.c_str(); |
no test coverage detected