diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index deec0b9..7bf09cc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -93,18 +93,18 @@ set(source_files juci.h cmake.cc) if(MSYS) - list(APPEND source_files terminal_win.cc dialogs_win.cc) + list(APPEND source_files terminal_win.cc) list(APPEND source_files dialogs_win.cc) message("MSYS detected") else() - list(APPEND source_files terminal.cc dialogs.cc) + list(APPEND source_files terminal.cc) list(APPEND source_files dialogs.cc) message("UNIX detected") endif() if(${validation}) add_executable(${project_name} ${source_files}) - + # add_library(${module} SHARED # api # api_ext) diff --git a/src/dialogs_win.cc b/src/dialogs_win.cc index c9e6f57..acf03bd 100644 --- a/src/dialogs_win.cc +++ b/src/dialogs_win.cc @@ -1,45 +1,78 @@ #include "dialogs.h" +#include #include #include #include -#include +#include "singletons.h" +#include -#ifndef check(__fun__) +#ifndef check #define MESSAGE "An error occurred when trying open windows dialog" -#define check(__fun__) auto __hr__ = __fun__; if(FAILED(__hr__)) Singleton::terminal()->print(MESSAGE) +HRESULT __hr__; +#define check(__fun__) __hr__ = __fun__; if(FAILED(__hr__)) Singleton::terminal()->print(MESSAGE) #endif +class win_string { + public: + win_string() : str(nullptr) { } + ~win_string() { CoTaskMemFree(static_cast(str)); } + std::string operator()(){ + std::string res; + if (str != nullptr) { + std::wstringstream ss; + ss << str; + res = std::string(ss.str().begin(), ss.str().end()); + } + return res; + } + wchar_t** operator&() { return &str; } + private: + wchar_t* str; +}; -std::string Dialog::select_folder() { - IFileOpenDialog *dialog = nullptr; - check(CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog))); - DWORD options; - check(dialog->GetOptions(&options)); +class CommonDialog { + public: + CommonDialog() : dialog(nullptr) { + check(CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog))); + check(dialog->GetOptions(&options)); + } + CommonDialog(const std::string &title, unsigned option) : CommonDialog() { + set_title(title); + add_option(option); + } + void set_title(const std::string &title) { check(dialog->SetTitle(title)); } + void add_option(unsigned option) { check(dialog->SetOptions(options | option)); } + std::string show() { + check(dialog->Show(nullptr)); + IShellItem *result = nullptr; + check(dialog->GetResult(&result)); + win_string str; + check(result->GetDisplayName(SIGDN_FILESYSPATH, &str));; + result->Release(); + return str(); + } + private: + IFileOpenDialog * dialog; + DWORD options; +}; + +std::string Dialog::select_folder() { + return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); } -/* + std::string Dialog::new_file() { - return open_dialog("Please create a new file", - {std::make_pair("Cancel", Gtk::RESPONSECANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_SAVE); + return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); } std::string Dialog::new_folder() { - return open_dialog("Please create a new folder", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Create", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER); + return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); } std::string Dialog::select_file() { - return open_dialog("Please choose a folder", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Select", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_OPEN); + return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); } std::string Dialog::save_file() { - return open_dialog("Please choose a file", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Save", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_OPEN); -} - -*/ \ No newline at end of file + return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); +} \ No newline at end of file