From d380b3237341ecd78ab292843e9746c4a12b3e0e Mon Sep 17 00:00:00 2001 From: Matt Hartley Date: Thu, 23 Jul 2020 13:10:53 +0100 Subject: [PATCH] created directory_dialog for windows and linux --- include/nanogui/common.h | 12 +++++++ src/common.cpp | 67 +++++++++++++++++++++++++++++++++++++++- src/example1.cpp | 8 +++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/include/nanogui/common.h b/include/nanogui/common.h index d3d2a5a7..05cc8e82 100644 --- a/include/nanogui/common.h +++ b/include/nanogui/common.h @@ -293,6 +293,18 @@ file_dialog(const std::vector> &filetypes, extern NANOGUI_EXPORT void chdir_to_bundle_parent(); #endif +/** +* \brief Open a native directory dialog. +* +* +* \param saved_path +* Optional parameter to specify a directory that the dialog should start in. +*/ +#if !defined(__APPLE__) + extern NANOGUI_EXPORT std::string + directory_dialog(std::string saved_path = ""); +#endif + /** * \brief Convert a single UTF32 character code to UTF8. * diff --git a/src/common.cpp b/src/common.cpp index f0ac3ce0..9842af32 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -14,6 +14,7 @@ #if defined(_WIN32) # define NOMINMAX # include +# include #endif #include @@ -416,7 +417,71 @@ std::vector file_dialog(const std::vector 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(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 + + + void Object::inc_ref() const { m_ref_count++; } diff --git a/src/example1.cpp b/src/example1.cpp index 9c6a6d09..f720855b 100644 --- a/src/example1.cpp +++ b/src/example1.cpp @@ -216,6 +216,14 @@ class ExampleApplication : public Screen { { {"png", "Portable Network Graphics"}, {"txt", "Text file"} }, true) << std::endl; }); + new Label(window, "Directory dialog", "sans-bold"); + tools = new Widget(window); + tools->set_layout(new BoxLayout(Orientation::Horizontal,Alignment::Middle, 0, 6)); + b = new Button(tools, "Browse"); + b->set_callback([&] { + std::cout << "directory dialog result: " << directory_dialog() << std::endl; + }); + new Label(window, "Combo box", "sans-bold"); new ComboBox(window, { "Combo box item 1", "Combo box item 2", "Combo box item 3"}); new Label(window, "Check box", "sans-bold");