call after AddFiltersToDialog */
| 177 | |
| 178 | /* call after AddFiltersToDialog */ |
| 179 | nfdresult_t SetDefaultExtension(::IFileDialog* fileOpenDialog, |
| 180 | const nfdnfilteritem_t* filterList, |
| 181 | nfdfiltersize_t filterCount) { |
| 182 | // if there are no filters, then don't set default extensions |
| 183 | if (!filterCount) { |
| 184 | return NFD_OKAY; |
| 185 | } |
| 186 | |
| 187 | assert(filterList); |
| 188 | |
| 189 | // set the first item as the default index, and set the default extension |
| 190 | if (!SUCCEEDED(fileOpenDialog->SetFileTypeIndex(1))) { |
| 191 | NFDi_SetError("Failed to set the selected file type index."); |
| 192 | return NFD_ERROR; |
| 193 | } |
| 194 | |
| 195 | // set the first item as the default file extension |
| 196 | const nfdnchar_t* p_spec = filterList[0].spec; |
| 197 | for (; *p_spec; ++p_spec) { |
| 198 | if (*p_spec == ',') { |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | if (*p_spec) { |
| 203 | // multiple file extensions for this type (need to allocate memory) |
| 204 | size_t numChars = p_spec - filterList[0].spec; |
| 205 | // allocate one more char space for the '\0' |
| 206 | nfdnchar_t* extnBuf = NFDi_Malloc<nfdnchar_t>(sizeof(nfdnchar_t) * (numChars + 1)); |
| 207 | if (!extnBuf) { |
| 208 | return NFD_ERROR; |
| 209 | } |
| 210 | Free_Guard<nfdnchar_t> extnBufGuard(extnBuf); |
| 211 | |
| 212 | // copy the extension |
| 213 | for (size_t i = 0; i != numChars; ++i) { |
| 214 | extnBuf[i] = filterList[0].spec[i]; |
| 215 | } |
| 216 | // pad with trailing '\0' |
| 217 | extnBuf[numChars] = L'\0'; |
| 218 | |
| 219 | if (!SUCCEEDED(fileOpenDialog->SetDefaultExtension(extnBuf))) { |
| 220 | NFDi_SetError("Failed to set default extension."); |
| 221 | return NFD_ERROR; |
| 222 | } |
| 223 | } else { |
| 224 | // single file extension for this type (no need to allocate memory) |
| 225 | if (!SUCCEEDED(fileOpenDialog->SetDefaultExtension(filterList[0].spec))) { |
| 226 | NFDi_SetError("Failed to set default extension."); |
| 227 | return NFD_ERROR; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return NFD_OKAY; |
| 232 | } |
| 233 | |
| 234 | nfdresult_t SetDefaultPath(IFileDialog* dialog, const nfdnchar_t* defaultPath) { |
| 235 | if (!defaultPath || !*defaultPath) return NFD_OKAY; |
no test coverage detected