Debian Codegraph @

src/gui/fileDialog.cpp

raw
1 #include "fileDialog.h"
2 #include "ImGuiFileDialog.h"
3 #include "util.h"
4 #include "../ta-log.h"
5
6 #ifdef USE_NFD
7 #include <nfd.h>
8 #elif defined(ANDROID)
9 #include <SDL.h>
10 #elif (!defined(SUPPORT_XP) || !defined(_WIN32))
11 #include "../../extern/pfd-fixed/portable-file-dialogs.h"
12 #endif
13
14 #ifdef USE_NFD
15 struct NFDState {
16 unsigned char isSave;
17 bool allowMultiple;
18 String header;
19 std::vector<String> filter;
20 String path;
21 String defFileName;
22 FileDialogSelectCallback clickCallback;
23 NFDState(unsigned char save, String h, std::vector<String> filt, String pa, FileDialogSelectCallback cc, bool multi, String defFN):
24 isSave(save),
25 allowMultiple(multi),
26 header(h),
27 filter(filt),
28 path(pa),
29 defFileName(defFN),
30 clickCallback(cc) {
31 }
32 };
33
34 void _nfdThread(const NFDState state, std::atomic<bool>* ok, std::vector<String>* result, bool* errorOutput) {
35 nfdchar_t* out=NULL;
36 nfdresult_t ret=NFD_CANCEL;
37 (*errorOutput)=false;
38 nfdpathset_t paths;
39
40 result->clear();
41
42 if (state.isSave==2) {
43 ret=NFD_PickFolder(state.path.c_str(),&out);
44 } else if (state.isSave==1) {
45 ret=NFD_SaveDialog(state.filter,state.path.c_str(),&out,state.clickCallback,state.defFileName.empty()?NULL:state.defFileName.c_str());
46 } else {
47 if (state.allowMultiple) {
48 ret=NFD_OpenDialogMultiple(state.filter,state.path.c_str(),&paths,state.clickCallback,state.defFileName.empty()?NULL:state.defFileName.c_str());
49 } else {
50 ret=NFD_OpenDialog(state.filter,state.path.c_str(),&out,state.clickCallback,state.defFileName.empty()?NULL:state.defFileName.c_str());
51 }
52 }
53
54 switch (ret) {
55 case NFD_OKAY:
56 if (state.allowMultiple) {
57 logD("pushing multi path");
58 for (size_t i=0; i<NFD_PathSet_GetCount(&paths); i++) {
59 result->push_back(String(NFD_PathSet_GetPath(&paths,i)));
60 }
61 NFD_PathSet_Free(&paths);
62 } else {
63 logD("pushing single path");
64 if (out!=NULL) {
65 logD("we have it");
66 result->push_back(String(out));
67 }
68 }
69 break;
70 case NFD_CANCEL:
71 break;
72 case NFD_ERROR:
73 logE("NFD error! %s\n",NFD_GetError());
74 (*errorOutput)=true;
75 break;
76 default:
77 logE("NFD unknown return code %d!\n",(int)ret);
78 break;
79 }
80 (*ok)=true;
81 }
82 #endif
83
84 void FurnaceGUIFileDialog::convertFilterList(std::vector<String>& filter) {
85 memset(noSysFilter,0,4096);
86
87 String result;
88 char sprintfBuf[4096];
89
90 for (size_t i=0; (i+1)<filter.size(); i+=2) {
91 String label=filter[i];
92 String ext;
93
94 if (filter[i+1]=="*") {
95 ext=".*";
96 } else for (char j: filter[i+1]) {
97 switch (j) {
98 case '*':
99 break;
100 case ' ':
101 ext+=',';
102 break;
103 default:
104 ext+=j;
105 break;
106 }
107 }
108
109 if (!result.empty()) result+=',';
110 // what the heck? fmt::sprintf not working?!
111 snprintf(sprintfBuf,4095,"%s{%s}",label.c_str(),ext.c_str());
112 result+=sprintfBuf;
113 }
114
115 strncpy(noSysFilter,result.c_str(),4095);
116 }
117
118 bool FurnaceGUIFileDialog::openLoad(String header, std::vector<String> filter, String path, double dpiScale, FileDialogSelectCallback clickCallback, bool allowMultiple, String hint) {
119 if (opened) return false;
120 dialogType=0;
121 curPath=path;
122
123 // strip excess directory separators
124 while (!curPath.empty()) {
125 if (curPath[curPath.size()-1]!=DIR_SEPARATOR) break;
126 curPath.erase(curPath.size()-1);
127 }
128 curPath+=DIR_SEPARATOR;
129
130 logD("opening load file dialog with curPath %s",curPath.c_str());
131 if (sysDialog) {
132 curPath+=hint;
133 #ifdef USE_NFD
134 dialogOK=false;
135 #ifdef NFD_NON_THREADED
136 _nfdThread(NFDState(0,header,filter,path,clickCallback,allowMultiple,hint),&dialogOK,&nfdResult,&hasError);
137 #else
138 dialogO=new std::thread(_nfdThread,NFDState(0,header,filter,path,clickCallback,allowMultiple,hint),&dialogOK,&nfdResult,&hasError);
139 #endif
140 #elif defined(ANDROID)
141 hasError=false;
142 if (jniEnv==NULL) {
143 jniEnv=(JNIEnv*)SDL_AndroidGetJNIEnv();
144 if (jniEnv==NULL) {
145 hasError=true;
146 logE("could not acquire JNI env!");
147 return false;
148 }
149 }
150
151 jobject activity=(jobject)SDL_AndroidGetActivity();
152 if (activity==NULL) {
153 hasError=true;
154 logE("the Activity is NULL!");
155 return false;
156 }
157
158 jclass class_=jniEnv->GetObjectClass(activity);
159 jmethodID showFileDialog=jniEnv->GetMethodID(class_,"showFileDialog","()V");
160
161 if (showFileDialog==NULL) {
162 logE("method showFileDialog not found!");
163 hasError=true;
164 jniEnv->DeleteLocalRef(class_);
165 jniEnv->DeleteLocalRef(activity);
166 return false;
167 }
168
169 jniEnv->CallVoidMethod(activity,showFileDialog);
170
171 /*if (!(bool)mret) {
172 hasError=true;
173 logW("could not open Android file picker...");
174 }*/
175
176 jniEnv->DeleteLocalRef(class_);
177 jniEnv->DeleteLocalRef(activity);
178 return true;
179 #elif (!defined(SUPPORT_XP) || !defined(_WIN32))
180 dialogO=new pfd::open_file(header,path,filter,allowMultiple?(pfd::opt::multiselect):(pfd::opt::none));
181 hasError=!pfd::settings::available();
182 #else
183 hasError=true;
184 return false;
185 #endif
186 } else {
187 hasError=false;
188
189 #ifdef ANDROID
190 if (!SDL_AndroidRequestPermission("android.permission.READ_EXTERNAL_STORAGE")) {
191 return false;
192 }
193 #endif
194
195 convertFilterList(filter);
196
197 ImGuiFileDialog::Instance()->singleClickSel=mobileUI;
198 ImGuiFileDialog::Instance()->DpiScale=dpiScale;
199 ImGuiFileDialog::Instance()->mobileMode=mobileUI;
200 ImGuiFileDialog::Instance()->homePath=getHomeDir();
201 ImGuiFileDialog::Instance()->OpenModal("FileDialog",header,filter.empty()?NULL:noSysFilter,path,hint,allowMultiple?999:1,nullptr,0,clickCallback);
202 }
203 opened=true;
204 return true;
205 }
206
207 bool FurnaceGUIFileDialog::openSave(String header, std::vector<String> filter, String path, double dpiScale, String hint) {
208 if (opened) return false;
209
210 #ifdef ANDROID
211 if (!SDL_AndroidRequestPermission("android.permission.WRITE_EXTERNAL_STORAGE")) {
212 return false;
213 }
214 #endif
215
216 dialogType=1;
217 curPath=path;
218
219 // strip excess directory separators
220 while (!curPath.empty()) {
221 if (curPath[curPath.size()-1]!=DIR_SEPARATOR) break;
222 curPath.erase(curPath.size()-1);
223 }
224 curPath+=DIR_SEPARATOR;
225
226 logD("opening save file dialog with curPath %s",curPath.c_str());
227 if (sysDialog) {
228 #ifdef USE_NFD
229 dialogOK=false;
230 #ifdef NFD_NON_THREADED
231 _nfdThread(NFDState(1,header,filter,path,NULL,false,hint),&dialogOK,&nfdResult,&hasError);
232 #else
233 dialogS=new std::thread(_nfdThread,NFDState(1,header,filter,path,NULL,false,hint),&dialogOK,&nfdResult,&hasError);
234 #endif
235 #elif defined(ANDROID)
236 hasError=false;
237 if (jniEnv==NULL) {
238 jniEnv=(JNIEnv*)SDL_AndroidGetJNIEnv();
239 if (jniEnv==NULL) {
240 hasError=true;
241 logE("could not acquire JNI env!");
242 return false;
243 }
244 }
245
246 jobject activity=(jobject)SDL_AndroidGetActivity();
247 if (activity==NULL) {
248 hasError=true;
249 logE("the Activity is NULL!");
250 return false;
251 }
252
253 jclass class_=jniEnv->GetObjectClass(activity);
254 jmethodID showSaveFileDialog=jniEnv->GetMethodID(class_,"showSaveFileDialog","()V");
255
256 if (showSaveFileDialog==NULL) {
257 logE("method showSaveFileDialog not found!");
258 hasError=true;
259 jniEnv->DeleteLocalRef(class_);
260 jniEnv->DeleteLocalRef(activity);
261 return false;
262 }
263
264 jniEnv->CallVoidMethod(activity,showSaveFileDialog);
265
266 /*if (!(bool)mret) {
267 hasError=true;
268 logW("could not open Android file picker...");
269 }*/
270
271 jniEnv->DeleteLocalRef(class_);
272 jniEnv->DeleteLocalRef(activity);
273 return true;
274 #elif (!defined(SUPPORT_XP) || !defined(_WIN32))
275 dialogS=new pfd::save_file(header,path,filter);
276 hasError=!pfd::settings::available();
277 #else
278 hasError=true;
279 return false;
280 #endif
281 } else {
282 hasError=false;
283
284 convertFilterList(filter);
285
286 ImGuiFileDialog::Instance()->singleClickSel=false;
287 ImGuiFileDialog::Instance()->DpiScale=dpiScale;
288 ImGuiFileDialog::Instance()->mobileMode=mobileUI;
289 ImGuiFileDialog::Instance()->homePath=getHomeDir();
290 ImGuiFileDialog::Instance()->OpenModal("FileDialog",header,noSysFilter,path,hint,1,nullptr,ImGuiFileDialogFlags_ConfirmOverwrite);
291 }
292 opened=true;
293 return true;
294 }
295
296 bool FurnaceGUIFileDialog::openSelectDir(String header, String path, double dpiScale, String hint) {
297 if (opened) return false;
298 dialogType=2;
299 curPath=path;
300
301 // strip excess directory separators
302 while (!curPath.empty()) {
303 if (curPath[curPath.size()-1]!=DIR_SEPARATOR) break;
304 curPath.erase(curPath.size()-1);
305 }
306 curPath+=DIR_SEPARATOR;
307
308 logD("opening select dir dialog with curPath %s",curPath.c_str());
309 if (sysDialog) {
310 curPath+=hint;
311 #ifdef USE_NFD
312 dialogOK=false;
313 #ifdef NFD_NON_THREADED
314 _nfdThread(NFDState(2,header,std::vector<String>(),path,NULL,false,""),&dialogOK,&nfdResult,&hasError);
315 #else
316 dialogF=new std::thread(_nfdThread,NFDState(2,header,std::vector<String>(),path,NULL,false,""),&dialogOK,&nfdResult,&hasError);
317 #endif
318 #elif defined(ANDROID)
319 hasError=true;
320 return false;
321 #elif (!defined(SUPPORT_XP) || !defined(_WIN32))
322 dialogF=new pfd::select_folder(header,path);
323 hasError=!pfd::settings::available();
324 #else
325 hasError=true;
326 return false;
327 #endif
328 } else {
329 hasError=false;
330
331 #ifdef ANDROID
332 if (!SDL_AndroidRequestPermission("android.permission.READ_EXTERNAL_STORAGE")) {
333 return false;
334 }
335 #endif
336
337 ImGuiFileDialog::Instance()->singleClickSel=mobileUI;
338 ImGuiFileDialog::Instance()->DpiScale=dpiScale;
339 ImGuiFileDialog::Instance()->mobileMode=mobileUI;
340 ImGuiFileDialog::Instance()->homePath=getHomeDir();
341 ImGuiFileDialog::Instance()->OpenModal("FileDialog",header,NULL,path,hint,1,nullptr,0);
342 }
343 opened=true;
344 return true;
345 }
346
347 bool FurnaceGUIFileDialog::accepted() {
348 if (sysDialog) {
349 return (!fileName.empty());
350 } else {
351 return ImGuiFileDialog::Instance()->IsOk();
352 }
353 }
354
355 void FurnaceGUIFileDialog::close() {
356 if (sysDialog) {
357 if (dialogType==2) {
358 if (dialogF!=NULL) {
359 #ifdef USE_NFD
360 dialogF->join();
361 #endif
362 #ifndef ANDROID
363 delete dialogF;
364 #endif
365 dialogF=NULL;
366 }
367 } else if (dialogType==1) {
368 if (dialogS!=NULL) {
369 #ifdef USE_NFD
370 dialogS->join();
371 #endif
372 #ifndef ANDROID
373 delete dialogS;
374 #endif
375 dialogS=NULL;
376 }
377 } else if (dialogType==0) {
378 if (dialogO!=NULL) {
379 #ifdef USE_NFD
380 dialogO->join();
381 #endif
382 #ifndef ANDROID
383 delete dialogO;
384 #endif
385 dialogO=NULL;
386 }
387 } else {
388 logE("what...");
389 }
390 #ifdef USE_NFD
391 dialogOK=false;
392 #endif
393 } else {
394 ImGuiFileDialog::Instance()->Close();
395 }
396 opened=false;
397 }
398
399 bool FurnaceGUIFileDialog::render(const ImVec2& min, const ImVec2& max) {
400 if (sysDialog) {
401 #ifdef USE_NFD
402 if (dialogOK) {
403 fileName.clear();
404 fileName=nfdResult;
405 if (!fileName.empty()) {
406 size_t dsPos=fileName[0].rfind(DIR_SEPARATOR);
407 if (dsPos!=String::npos) curPath=fileName[0].substr(0,dsPos);
408 }
409 for (String& i: fileName) {
410 logD("- returning %s",i);
411 }
412 dialogOK=false;
413 return true;
414 }
415 return false;
416 #elif defined(ANDROID)
417 // TODO: detect when file picker is closed
418 return false;
419 #elif (!defined(SUPPORT_XP) || !defined(_WIN32))
420 if (dialogType==2) {
421 if (dialogF!=NULL) {
422 if (dialogF->ready(0)) {
423 fileName.clear();
424 fileName.push_back(dialogF->result());
425 size_t dsPos=fileName[0].rfind(DIR_SEPARATOR);
426 if (dsPos!=String::npos) curPath=fileName[0].substr(0,dsPos);
427 logD("returning %s",fileName[0]);
428 return true;
429 }
430 }
431 } else if (dialogType==1) {
432 if (dialogS!=NULL) {
433 if (dialogS->ready(0)) {
434 fileName.clear();
435 fileName.push_back(dialogS->result());
436 size_t dsPos=fileName[0].rfind(DIR_SEPARATOR);
437 if (dsPos!=String::npos) curPath=fileName[0].substr(0,dsPos);
438 logD("returning %s",fileName[0]);
439 return true;
440 }
441 }
442 } else if (dialogType==0) {
443 if (dialogO!=NULL) {
444 if (dialogO->ready(0)) {
445 if (dialogO->result().empty()) {
446 fileName.clear();
447 logD("returning nothing");
448 } else {
449 fileName=dialogO->result();
450 if (fileName.empty()) {
451 // don't touch
452 } else {
453 size_t dsPos=fileName[0].rfind(DIR_SEPARATOR);
454 if (dsPos!=String::npos) curPath=fileName[0].substr(0,dsPos);
455 for (String& i: fileName) {
456 logD("- returning %s",i);
457 }
458 }
459 }
460 return true;
461 }
462 }
463 } else {
464 logE("what!");
465 }
466 return false;
467 #else
468 return false;
469 #endif
470 } else {
471 return ImGuiFileDialog::Instance()->Display("FileDialog",ImGuiWindowFlags_NoCollapse|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoScrollWithMouse,min,max);
472 }
473 }
474
475 bool FurnaceGUIFileDialog::isOpen() {
476 return opened;
477 }
478
479 bool FurnaceGUIFileDialog::isError() {
480 return hasError;
481 }
482
483 String FurnaceGUIFileDialog::getPath() {
484 if (sysDialog) {
485 if (curPath.size()>1) {
486 if (curPath[curPath.size()-1]==DIR_SEPARATOR) {
487 curPath=curPath.substr(0,curPath.size()-1);
488 }
489 }
490 logD("curPath: %s",curPath.c_str());
491 return curPath;
492 } else {
493 return ImGuiFileDialog::Instance()->GetCurrentPath();
494 }
495 }
496
497 std::vector<String>& FurnaceGUIFileDialog::getFileName() {
498 if (sysDialog) {
499 return fileName;
500 } else {
501 fileName.clear();
502 if (dialogType!=0) {
503 fileName.push_back(ImGuiFileDialog::Instance()->GetFilePathName());
504 } else {
505 for (auto& i: ImGuiFileDialog::Instance()->GetSelection()) {
506 fileName.push_back(i.second);
507 }
508 }
509 //
510 return fileName;
511 }
512 }