2022-03-13 23:32:35 +00:00
|
|
|
#include "fileDialog.h"
|
|
|
|
#include "ImGuiFileDialog.h"
|
2022-03-14 04:10:43 +00:00
|
|
|
#include "../ta-log.h"
|
2022-03-14 02:19:52 +00:00
|
|
|
|
2022-06-17 06:28:22 +00:00
|
|
|
#ifdef USE_NFD
|
|
|
|
#include <nfd.h>
|
|
|
|
#else
|
2022-03-14 03:02:50 +00:00
|
|
|
#include "../../extern/pfd-fixed/portable-file-dialogs.h"
|
2022-06-17 06:28:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_NFD
|
|
|
|
struct NFDState {
|
|
|
|
bool isSave;
|
|
|
|
String header;
|
|
|
|
std::vector<String> filter;
|
|
|
|
String path;
|
|
|
|
FileDialogSelectCallback clickCallback;
|
|
|
|
NFDState(bool save, String h, std::vector<String> filt, String pa, FileDialogSelectCallback cc):
|
|
|
|
isSave(save),
|
|
|
|
header(h),
|
|
|
|
filter(filt),
|
|
|
|
path(pa),
|
|
|
|
clickCallback(cc) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: filter
|
|
|
|
void _nfdThread(const NFDState state, std::atomic<bool>* ok, String* result) {
|
|
|
|
nfdchar_t* out=NULL;
|
|
|
|
nfdresult_t ret=NFD_CANCEL;
|
|
|
|
|
|
|
|
if (state.isSave) {
|
2022-06-24 08:11:16 +00:00
|
|
|
ret=NFD_SaveDialog(state.filter,state.path.c_str(),&out,state.clickCallback);
|
2022-06-17 06:28:22 +00:00
|
|
|
} else {
|
2022-06-24 08:11:16 +00:00
|
|
|
ret=NFD_OpenDialog(state.filter,state.path.c_str(),&out,state.clickCallback);
|
2022-06-17 06:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case NFD_OKAY:
|
|
|
|
if (out!=NULL) {
|
|
|
|
(*result)=out;
|
|
|
|
} else {
|
|
|
|
(*result)="";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NFD_CANCEL:
|
|
|
|
(*result)="";
|
|
|
|
break;
|
|
|
|
case NFD_ERROR:
|
|
|
|
(*result)="";
|
|
|
|
logE("NFD error! %s\n",NFD_GetError());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logE("NFD unknown return code %d!\n",ret);
|
|
|
|
(*result)="";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(*ok)=true;
|
|
|
|
}
|
|
|
|
#endif
|
2022-03-13 23:32:35 +00:00
|
|
|
|
2022-04-25 23:58:17 +00:00
|
|
|
bool FurnaceGUIFileDialog::openLoad(String header, std::vector<String> filter, const char* noSysFilter, String path, double dpiScale, FileDialogSelectCallback clickCallback) {
|
2022-03-13 23:32:35 +00:00
|
|
|
if (opened) return false;
|
|
|
|
saving=false;
|
|
|
|
curPath=path;
|
2022-04-11 09:07:01 +00:00
|
|
|
logD("opening load file dialog with curPath %s",curPath.c_str());
|
2022-03-13 23:32:35 +00:00
|
|
|
if (sysDialog) {
|
2022-06-17 06:28:22 +00:00
|
|
|
#ifdef USE_NFD
|
|
|
|
dialogOK=false;
|
2022-06-19 23:51:51 +00:00
|
|
|
#ifdef NFD_NON_THREADED
|
|
|
|
_nfdThread(NFDState(false,header,filter,path,clickCallback),&dialogOK,&nfdResult);
|
|
|
|
#else
|
2022-06-17 06:28:22 +00:00
|
|
|
dialogO=new std::thread(_nfdThread,NFDState(false,header,filter,path,clickCallback),&dialogOK,&nfdResult);
|
2022-06-19 23:51:51 +00:00
|
|
|
#endif
|
2022-06-17 06:28:22 +00:00
|
|
|
#else
|
2022-03-13 23:32:35 +00:00
|
|
|
dialogO=new pfd::open_file(header,path,filter);
|
2022-06-17 06:28:22 +00:00
|
|
|
#endif
|
2022-03-13 23:32:35 +00:00
|
|
|
} else {
|
|
|
|
ImGuiFileDialog::Instance()->DpiScale=dpiScale;
|
2022-04-25 23:58:17 +00:00
|
|
|
ImGuiFileDialog::Instance()->OpenModal("FileDialog",header,noSysFilter,path,1,nullptr,0,clickCallback);
|
2022-03-13 23:32:35 +00:00
|
|
|
}
|
2022-03-14 04:10:43 +00:00
|
|
|
opened=true;
|
2022-03-13 23:32:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-14 04:10:43 +00:00
|
|
|
bool FurnaceGUIFileDialog::openSave(String header, std::vector<String> filter, const char* noSysFilter, String path, double dpiScale) {
|
|
|
|
if (opened) return false;
|
|
|
|
saving=true;
|
2022-03-13 23:32:35 +00:00
|
|
|
curPath=path;
|
2022-04-11 09:07:01 +00:00
|
|
|
logD("opening save file dialog with curPath %s",curPath.c_str());
|
2022-03-13 23:32:35 +00:00
|
|
|
if (sysDialog) {
|
2022-06-17 06:28:22 +00:00
|
|
|
#ifdef USE_NFD
|
|
|
|
dialogOK=false;
|
2022-06-19 23:51:51 +00:00
|
|
|
#ifdef NFD_NON_THREADED
|
|
|
|
_nfdThread(NFDState(true,header,filter,path,NULL),&dialogOK,&nfdResult);
|
|
|
|
#else
|
2022-06-17 06:28:22 +00:00
|
|
|
dialogS=new std::thread(_nfdThread,NFDState(true,header,filter,path,NULL),&dialogOK,&nfdResult);
|
2022-06-19 23:51:51 +00:00
|
|
|
#endif
|
2022-06-17 06:28:22 +00:00
|
|
|
#else
|
2022-03-14 04:10:43 +00:00
|
|
|
dialogS=new pfd::save_file(header,path,filter);
|
2022-06-17 06:28:22 +00:00
|
|
|
#endif
|
2022-03-13 23:32:35 +00:00
|
|
|
} else {
|
|
|
|
ImGuiFileDialog::Instance()->DpiScale=dpiScale;
|
2022-03-14 04:10:43 +00:00
|
|
|
ImGuiFileDialog::Instance()->OpenModal("FileDialog",header,noSysFilter,path,1,nullptr,ImGuiFileDialogFlags_ConfirmOverwrite);
|
2022-03-13 23:32:35 +00:00
|
|
|
}
|
2022-03-14 04:10:43 +00:00
|
|
|
opened=true;
|
2022-03-13 23:32:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FurnaceGUIFileDialog::accepted() {
|
|
|
|
if (sysDialog) {
|
|
|
|
return (fileName!="");
|
|
|
|
} else {
|
|
|
|
return ImGuiFileDialog::Instance()->IsOk();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FurnaceGUIFileDialog::close() {
|
|
|
|
if (sysDialog) {
|
|
|
|
if (saving) {
|
|
|
|
if (dialogS!=NULL) {
|
2022-06-17 06:28:22 +00:00
|
|
|
#ifdef USE_NFD
|
|
|
|
dialogS->join();
|
|
|
|
#endif
|
2022-03-13 23:32:35 +00:00
|
|
|
delete dialogS;
|
|
|
|
dialogS=NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dialogO!=NULL) {
|
2022-06-17 06:28:22 +00:00
|
|
|
#ifdef USE_NFD
|
|
|
|
dialogO->join();
|
|
|
|
#endif
|
2022-03-13 23:32:35 +00:00
|
|
|
delete dialogO;
|
|
|
|
dialogO=NULL;
|
|
|
|
}
|
|
|
|
}
|
2022-06-17 06:28:22 +00:00
|
|
|
#ifdef USE_NFD
|
|
|
|
dialogOK=false;
|
|
|
|
#endif
|
2022-03-13 23:32:35 +00:00
|
|
|
} else {
|
|
|
|
ImGuiFileDialog::Instance()->Close();
|
|
|
|
}
|
2022-03-14 04:10:43 +00:00
|
|
|
opened=false;
|
2022-03-13 23:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FurnaceGUIFileDialog::render(const ImVec2& min, const ImVec2& max) {
|
|
|
|
if (sysDialog) {
|
2022-06-17 06:28:22 +00:00
|
|
|
#ifdef USE_NFD
|
|
|
|
if (dialogOK) {
|
|
|
|
fileName=nfdResult;
|
|
|
|
logD("returning %s",fileName.c_str());
|
|
|
|
dialogOK=false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
#else
|
2022-03-13 23:32:35 +00:00
|
|
|
if (saving) {
|
|
|
|
if (dialogS!=NULL) {
|
2022-03-29 21:18:38 +00:00
|
|
|
if (dialogS->ready(0)) {
|
2022-03-13 23:32:35 +00:00
|
|
|
fileName=dialogS->result();
|
2022-04-11 08:14:12 +00:00
|
|
|
size_t dsPos=fileName.rfind(DIR_SEPARATOR);
|
|
|
|
if (dsPos!=String::npos) curPath=fileName.substr(0,dsPos);
|
2022-04-11 03:12:02 +00:00
|
|
|
logD("returning %s",fileName.c_str());
|
2022-03-13 23:32:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dialogO!=NULL) {
|
2022-03-29 21:18:38 +00:00
|
|
|
if (dialogO->ready(0)) {
|
2022-03-13 23:32:35 +00:00
|
|
|
if (dialogO->result().empty()) {
|
|
|
|
fileName="";
|
2022-04-11 03:12:02 +00:00
|
|
|
logD("returning nothing");
|
2022-03-13 23:32:35 +00:00
|
|
|
} else {
|
|
|
|
fileName=dialogO->result()[0];
|
2022-04-11 08:14:12 +00:00
|
|
|
size_t dsPos=fileName.rfind(DIR_SEPARATOR);
|
|
|
|
if (dsPos!=String::npos) curPath=fileName.substr(0,dsPos);
|
2022-04-11 03:12:02 +00:00
|
|
|
logD("returning %s",fileName.c_str());
|
2022-03-13 23:32:35 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2022-06-17 06:28:22 +00:00
|
|
|
#endif
|
2022-03-13 23:32:35 +00:00
|
|
|
} else {
|
|
|
|
return ImGuiFileDialog::Instance()->Display("FileDialog",ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove,min,max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 07:02:55 +00:00
|
|
|
bool FurnaceGUIFileDialog::isOpen() {
|
|
|
|
return opened;
|
|
|
|
}
|
|
|
|
|
2022-03-13 23:32:35 +00:00
|
|
|
String FurnaceGUIFileDialog::getPath() {
|
|
|
|
if (sysDialog) {
|
2022-04-11 09:07:01 +00:00
|
|
|
if (curPath.size()>1) {
|
2022-04-11 08:14:12 +00:00
|
|
|
if (curPath[curPath.size()-1]==DIR_SEPARATOR) {
|
|
|
|
curPath=curPath.substr(0,curPath.size()-1);
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 09:07:01 +00:00
|
|
|
logD("curPath: %s",curPath.c_str());
|
2022-03-13 23:32:35 +00:00
|
|
|
return curPath;
|
|
|
|
} else {
|
|
|
|
return ImGuiFileDialog::Instance()->GetCurrentPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String FurnaceGUIFileDialog::getFileName() {
|
|
|
|
if (sysDialog) {
|
|
|
|
return fileName;
|
|
|
|
} else {
|
|
|
|
return ImGuiFileDialog::Instance()->GetFilePathName();
|
|
|
|
}
|
2022-03-14 02:19:52 +00:00
|
|
|
}
|