Skip to content

Commit 8e5016e

Browse files
committed
directory dialog based on mitsuba-renderer#48
1 parent 2ee903c commit 8e5016e

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

include/nanogui/common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ extern NANOGUI_EXPORT std::string
274274
file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes,
275275
bool save);
276276

277+
/**
278+
* \brief Open a native directory dialog.
279+
*
280+
*
281+
* \param saved_path
282+
* Optional parameter to specify a directory that the dialog should start in.
283+
*/
284+
extern NANOGUI_EXPORT std::string
285+
directory_dialog(std::string saved_path = "");
277286

278287
/**
279288
* \brief Check for the availability of displays with 10-bit color and/or

src/common.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# define NOMINMAX 1
1717
# endif
1818
# include <windows.h>
19+
# include <shlobj.h>
1920
#endif
2021

2122
#include <nanogui/opengl.h>
@@ -441,6 +442,67 @@ std::vector<std::string> file_dialog(const std::vector<std::pair<std::string, st
441442
}
442443
#endif
443444

445+
#if !defined(__APPLE__)
446+
#if !defined(_WIN32)
447+
//linux
448+
std::string directory_dialog(const std::string save_dir){
449+
static const int DIRECTORY_DIALOG_MAX_BUFFER = 16384;
450+
char buffer[DIRECTORY_DIALOG_MAX_BUFFER];
451+
buffer[0] = '\0';
452+
453+
std::string cmd = "zenity --file-selection --directory ";
454+
if(save_dir.length() > 0 ){
455+
cmd += "--filename="+save_dir;
456+
}
457+
458+
FILE *output = popen(cmd.c_str(), "r");
459+
if (output == nullptr)
460+
throw std::runtime_error("popen() failed -- could not launch zenity!");
461+
while (fgets(buffer, DIRECTORY_DIALOG_MAX_BUFFER, output) != NULL);
462+
pclose(output);
463+
std::string path(buffer);
464+
465+
path.erase(std::remove(path.begin(), path.end(), '\n'), path.end());
466+
467+
return path;
468+
469+
};
470+
#else
471+
472+
473+
std::string directory_dialog(const std::string saved_path){
474+
TCHAR path[MAX_PATH];
475+
476+
const char * path_param = saved_path.c_str();
477+
478+
BROWSEINFO browseInfo = { 0 };
479+
browseInfo.lpszTitle = ("Select directory");
480+
browseInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_EDITBOX | BIF_BROWSEINCLUDEURLS;
481+
browseInfo.lParam = (LPARAM) path_param;
482+
483+
LPITEMIDLIST pidl = SHBrowseForFolder ( &browseInfo );
484+
485+
if( pidl == 0 ){
486+
return "";
487+
}else{
488+
//get path
489+
SHGetPathFromIDList ( pidl, path );
490+
491+
//free memory
492+
IMalloc * imalloc = 0;
493+
494+
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) ){
495+
imalloc->Free(pidl);
496+
imalloc->Release();
497+
}
498+
499+
return path;
500+
}
501+
}
502+
503+
#endif
504+
#endif
505+
444506
static void (*object_inc_ref_py)(PyObject *) noexcept = nullptr;
445507
static void (*object_dec_ref_py)(PyObject *) noexcept = nullptr;
446508

src/darwin.mm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ @interface NSScreen (ForwardDeclare)
5656
return result;
5757
}
5858

59+
std::string directory_dialog(const std::string saved_path) {
60+
NSOpenPanel *openDlg = [NSOpenPanel openPanel];
61+
62+
if(saved_path.length() > 0 ){
63+
std::string urlString = "folder"+saved_path;
64+
[openDlg setDirectoryURL: [NSURL URLWithString: [NSString stringWithUTF8String: urlString.c_str()]]];
65+
}
66+
[openDlg setCanChooseFiles:NO];
67+
[openDlg setCanChooseDirectories:YES];
68+
[openDlg setAllowsMultipleSelection:NO];
69+
70+
if ([openDlg runModal] == NSModalResponseOK) {
71+
NSURL* url = [openDlg URL];
72+
return ((char*) [[url path] UTF8String]);
73+
}
74+
return "";
75+
}
76+
5977
void chdir_to_bundle_parent() {
6078
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
6179
chdir([path fileSystemRepresentation]);

src/python/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ NB_MODULE(nanogui_ext, m_) {
111111
bool)) &
112112
nanogui::file_dialog,
113113
D(file_dialog, 2));
114+
m.def("directory_dialog", &nanogui::directory_dialog);
114115
#if defined(__APPLE__)
115116
m.def("chdir_to_bundle_parent", &nanogui::chdir_to_bundle_parent);
116117
#endif

0 commit comments

Comments
 (0)