finally implement file name hint on NFD (Windows)

for "auto-fill file name when saving"
This commit is contained in:
tildearrow 2024-09-28 00:30:16 -05:00
parent 0f5adae785
commit ab006d5d78
2 changed files with 39 additions and 10 deletions

View file

@ -466,6 +466,15 @@ nfdresult_t NFD_OpenDialog( const std::vector<std::string>& filterList,
goto end; goto end;
} }
// Set the file name
if (defaultFileName!=NULL) {
std::wstring defFileName=utf8To16(defaultFileName);
result = fileOpenDialog->SetFileName(defFileName.c_str());
if ( !SUCCEEDED(result) ) {
// ignore
}
}
// Pass the callback // Pass the callback
winEvents=new NFDWinEvents(selCallback); winEvents=new NFDWinEvents(selCallback);
if ( !SUCCEEDED(fileOpenDialog->Advise(winEvents,&eventID)) ) { if ( !SUCCEEDED(fileOpenDialog->Advise(winEvents,&eventID)) ) {
@ -577,6 +586,15 @@ nfdresult_t NFD_OpenDialogMultiple( const std::vector<std::string>& filterList,
goto end; goto end;
} }
// Set the file name
if (defaultFileName!=NULL) {
std::wstring defFileName=utf8To16(defaultFileName);
result = fileOpenDialog->SetFileName(defFileName.c_str());
if ( !SUCCEEDED(result) ) {
// ignore
}
}
// Pass the callback // Pass the callback
winEvents=new NFDWinEvents(selCallback); winEvents=new NFDWinEvents(selCallback);
if ( !SUCCEEDED(fileOpenDialog->Advise(winEvents,&eventID)) ) { if ( !SUCCEEDED(fileOpenDialog->Advise(winEvents,&eventID)) ) {
@ -686,6 +704,15 @@ nfdresult_t NFD_SaveDialog( const std::vector<std::string>& filterList,
goto end; goto end;
} }
// Set the file name
if (defaultFileName!=NULL) {
std::wstring defFileName=utf8To16(defaultFileName);
result = fileSaveDialog->SetFileName(defFileName.c_str());
if ( !SUCCEEDED(result) ) {
// ignore
}
}
// Set a flag for no history // Set a flag for no history
DWORD dwFlags; DWORD dwFlags;
result = fileSaveDialog->GetOptions(&dwFlags); result = fileSaveDialog->GetOptions(&dwFlags);

View file

@ -18,13 +18,15 @@ struct NFDState {
String header; String header;
std::vector<String> filter; std::vector<String> filter;
String path; String path;
String defFileName;
FileDialogSelectCallback clickCallback; FileDialogSelectCallback clickCallback;
NFDState(unsigned char save, String h, std::vector<String> filt, String pa, FileDialogSelectCallback cc, bool multi): NFDState(unsigned char save, String h, std::vector<String> filt, String pa, FileDialogSelectCallback cc, bool multi, String defFN):
isSave(save), isSave(save),
allowMultiple(multi), allowMultiple(multi),
header(h), header(h),
filter(filt), filter(filt),
path(pa), path(pa),
defFileName(defFN),
clickCallback(cc) { clickCallback(cc) {
} }
}; };
@ -40,12 +42,12 @@ void _nfdThread(const NFDState state, std::atomic<bool>* ok, std::vector<String>
if (state.isSave==2) { if (state.isSave==2) {
ret=NFD_PickFolder(state.path.c_str(),&out); ret=NFD_PickFolder(state.path.c_str(),&out);
} else if (state.isSave==1) { } else if (state.isSave==1) {
ret=NFD_SaveDialog(state.filter,state.path.c_str(),&out,state.clickCallback); ret=NFD_SaveDialog(state.filter,state.path.c_str(),&out,state.clickCallback,state.defFileName.empty()?NULL:state.defFileName.c_str());
} else { } else {
if (state.allowMultiple) { if (state.allowMultiple) {
ret=NFD_OpenDialogMultiple(state.filter,state.path.c_str(),&paths,state.clickCallback); ret=NFD_OpenDialogMultiple(state.filter,state.path.c_str(),&paths,state.clickCallback,state.defFileName.empty()?NULL:state.defFileName.c_str());
} else { } else {
ret=NFD_OpenDialog(state.filter,state.path.c_str(),&out,state.clickCallback); ret=NFD_OpenDialog(state.filter,state.path.c_str(),&out,state.clickCallback,state.defFileName.empty()?NULL:state.defFileName.c_str());
} }
} }
@ -131,9 +133,9 @@ bool FurnaceGUIFileDialog::openLoad(String header, std::vector<String> filter, S
#ifdef USE_NFD #ifdef USE_NFD
dialogOK=false; dialogOK=false;
#ifdef NFD_NON_THREADED #ifdef NFD_NON_THREADED
_nfdThread(NFDState(0,header,filter,path,clickCallback,allowMultiple),&dialogOK,&nfdResult,&hasError); _nfdThread(NFDState(0,header,filter,path,clickCallback,allowMultiple,hint),&dialogOK,&nfdResult,&hasError);
#else #else
dialogO=new std::thread(_nfdThread,NFDState(0,header,filter,path,clickCallback,allowMultiple),&dialogOK,&nfdResult,&hasError); dialogO=new std::thread(_nfdThread,NFDState(0,header,filter,path,clickCallback,allowMultiple,hint),&dialogOK,&nfdResult,&hasError);
#endif #endif
#elif defined(ANDROID) #elif defined(ANDROID)
hasError=false; hasError=false;
@ -223,9 +225,9 @@ bool FurnaceGUIFileDialog::openSave(String header, std::vector<String> filter, S
#ifdef USE_NFD #ifdef USE_NFD
dialogOK=false; dialogOK=false;
#ifdef NFD_NON_THREADED #ifdef NFD_NON_THREADED
_nfdThread(NFDState(1,header,filter,path,NULL,false),&dialogOK,&nfdResult,&hasError); _nfdThread(NFDState(1,header,filter,path,NULL,false,hint),&dialogOK,&nfdResult,&hasError);
#else #else
dialogS=new std::thread(_nfdThread,NFDState(1,header,filter,path,NULL,false),&dialogOK,&nfdResult,&hasError); dialogS=new std::thread(_nfdThread,NFDState(1,header,filter,path,NULL,false,hint),&dialogOK,&nfdResult,&hasError);
#endif #endif
#elif defined(ANDROID) #elif defined(ANDROID)
hasError=false; hasError=false;
@ -303,9 +305,9 @@ bool FurnaceGUIFileDialog::openSelectDir(String header, String path, double dpiS
#ifdef USE_NFD #ifdef USE_NFD
dialogOK=false; dialogOK=false;
#ifdef NFD_NON_THREADED #ifdef NFD_NON_THREADED
_nfdThread(NFDState(2,header,std::vector<String>(),path,NULL,false),&dialogOK,&nfdResult,&hasError); _nfdThread(NFDState(2,header,std::vector<String>(),path,NULL,false,""),&dialogOK,&nfdResult,&hasError);
#else #else
dialogF=new std::thread(_nfdThread,NFDState(2,header,std::vector<String>(),path,NULL,false),&dialogOK,&nfdResult,&hasError); dialogF=new std::thread(_nfdThread,NFDState(2,header,std::vector<String>(),path,NULL,false,""),&dialogOK,&nfdResult,&hasError);
#endif #endif
#elif defined(ANDROID) #elif defined(ANDROID)
hasError=true; hasError=true;