Skip to content

Commit

Permalink
directory dialog based on mitsuba-renderer#48
Browse files Browse the repository at this point in the history
  • Loading branch information
arpit15 committed Sep 12, 2023
1 parent 2ee903c commit 8e5016e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/nanogui/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ extern NANOGUI_EXPORT std::string
file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes,
bool save);

/**
* \brief Open a native directory dialog.
*
*
* \param saved_path
* Optional parameter to specify a directory that the dialog should start in.
*/
extern NANOGUI_EXPORT std::string
directory_dialog(std::string saved_path = "");

/**
* \brief Check for the availability of displays with 10-bit color and/or
Expand Down
62 changes: 62 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# define NOMINMAX 1
# endif
# include <windows.h>
# include <shlobj.h>
#endif

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

#if !defined(__APPLE__)
#if !defined(_WIN32)
//linux
std::string directory_dialog(const std::string save_dir){
static const int DIRECTORY_DIALOG_MAX_BUFFER = 16384;
char buffer[DIRECTORY_DIALOG_MAX_BUFFER];
buffer[0] = '\0';

std::string cmd = "zenity --file-selection --directory ";
if(save_dir.length() > 0 ){
cmd += "--filename="+save_dir;
}

FILE *output = popen(cmd.c_str(), "r");
if (output == nullptr)
throw std::runtime_error("popen() failed -- could not launch zenity!");
while (fgets(buffer, DIRECTORY_DIALOG_MAX_BUFFER, output) != NULL);
pclose(output);
std::string path(buffer);

path.erase(std::remove(path.begin(), path.end(), '\n'), path.end());

return path;

};
#else


std::string directory_dialog(const std::string saved_path){
TCHAR path[MAX_PATH];

const char * path_param = saved_path.c_str();

BROWSEINFO browseInfo = { 0 };
browseInfo.lpszTitle = ("Select directory");
browseInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_EDITBOX | BIF_BROWSEINCLUDEURLS;
browseInfo.lParam = (LPARAM) path_param;

LPITEMIDLIST pidl = SHBrowseForFolder ( &browseInfo );

if( pidl == 0 ){
return "";
}else{
//get path
SHGetPathFromIDList ( pidl, path );

//free memory
IMalloc * imalloc = 0;

if ( SUCCEEDED( SHGetMalloc ( &imalloc )) ){
imalloc->Free(pidl);
imalloc->Release();
}

return path;
}
}

#endif
#endif

static void (*object_inc_ref_py)(PyObject *) noexcept = nullptr;
static void (*object_dec_ref_py)(PyObject *) noexcept = nullptr;

Expand Down
18 changes: 18 additions & 0 deletions src/darwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ @interface NSScreen (ForwardDeclare)
return result;
}

std::string directory_dialog(const std::string saved_path) {
NSOpenPanel *openDlg = [NSOpenPanel openPanel];

if(saved_path.length() > 0 ){
std::string urlString = "folder"+saved_path;
[openDlg setDirectoryURL: [NSURL URLWithString: [NSString stringWithUTF8String: urlString.c_str()]]];
}
[openDlg setCanChooseFiles:NO];
[openDlg setCanChooseDirectories:YES];
[openDlg setAllowsMultipleSelection:NO];

if ([openDlg runModal] == NSModalResponseOK) {
NSURL* url = [openDlg URL];
return ((char*) [[url path] UTF8String]);
}
return "";
}

void chdir_to_bundle_parent() {
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
chdir([path fileSystemRepresentation]);
Expand Down
1 change: 1 addition & 0 deletions src/python/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ NB_MODULE(nanogui_ext, m_) {
bool)) &
nanogui::file_dialog,
D(file_dialog, 2));
m.def("directory_dialog", &nanogui::directory_dialog);
#if defined(__APPLE__)
m.def("chdir_to_bundle_parent", &nanogui::chdir_to_bundle_parent);
#endif
Expand Down

0 comments on commit 8e5016e

Please sign in to comment.