Skip to content

Instantly share code, notes, and snippets.

@fodinabor
Created September 13, 2014 21:19
Show Gist options
  • Save fodinabor/982c75a4fad651abb28e to your computer and use it in GitHub Desktop.
Save fodinabor/982c75a4fad651abb28e to your computer and use it in GitHub Desktop.
If the OFN_ALLOWMULTISELECT flag is set and the user selects multiple files, the buffer contains the current directory followed by the file names of the selected files. For Explorer-style dialog boxes, the directory and file name strings are NULL separated, with an extra NULL character after the last file name. For old-style dialog boxes, the st…
std::vector<String> Win32Core::openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple) {
OPENFILENAME ofn;
wchar_t fBuffer[2048];
wchar_t filterString[2048];
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof ( ofn );
ofn.hwndOwner = hWnd ;
ofn.lpstrFile = fBuffer;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof( fBuffer );
if(extensions.size() > 0) {
int offset = 0;
for(int i =0; i < extensions.size(); i++) {
// filterString += extensions[i].description+"\0*."+extensions[i].extension+"\0";
memcpy(filterString+offset, extensions[i].description.getWDataWithEncoding(String::ENCODING_UTF8), extensions[i].description.length() * sizeof(wchar_t));
offset += extensions[i].description.length();
filterString[offset] = '\0';
offset++;
filterString[offset] = '*';
offset++;
filterString[offset] = '.';
offset++;
memcpy(filterString+offset, extensions[i].extension.getWDataWithEncoding(String::ENCODING_UTF8), extensions[i].extension.length() * sizeof(wchar_t));
offset += extensions[i].extension.length();
filterString[offset] = '\0';
offset++;
}
filterString[offset] = '\0';
ofn.lpstrFilter = filterString;
ofn.nFilterIndex = 1;
} else {
ofn.lpstrFilter = NULL;
}
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir=NULL;
if(!allowMultiple) {
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_EXPLORER;
} else {
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT|OFN_EXPLORER;
}
std::vector<String> retVec;
if(GetOpenFileName(&ofn)) {
if(allowMultiple) {
//hier wird die Sache spannend..
String dir = fBuffer;
size_t pos = dir.length(), last = 0;
while (true){
if (fBuffer[pos + 1] == NULL){
if (retVec.size() == 0){
retVec.push_back(dir);
}
break;
} else {
String fileName;
for (last = pos + 1; fBuffer[pos + 1] != NULL; pos++){
fileName.append(fBuffer[pos + 1]);
}
retVec.push_back(dir + "/" + fileName);
pos++;
}
}
} else {
retVec.push_back(String(fBuffer));
}
}
SetCurrentDirectory(defaultWorkingDirectory.getWDataWithEncoding(String::ENCODING_UTF8));
for(int i=0; i < retVec.size(); i++) {
retVec[i] = retVec[i].replace("\\", "/");
}
return retVec;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment