diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 86eadb9..14a0229 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,8 +43,7 @@ target_link_libraries(juci_shared set(JUCI_FILES config.cpp - dialogs.cpp - dialogs_unix.cpp + dialog.cpp directories.cpp entrybox.cpp info.cpp diff --git a/src/cmake.cpp b/src/cmake.cpp index 8cf8cb8..eb00c17 100644 --- a/src/cmake.cpp +++ b/src/cmake.cpp @@ -1,7 +1,7 @@ #include "cmake.hpp" #include "compile_commands.hpp" #include "config.hpp" -#include "dialogs.hpp" +#include "dialog.hpp" #include "filesystem.hpp" #include "terminal.hpp" #include "utility.hpp" diff --git a/src/dialogs.cpp b/src/dialog.cpp similarity index 72% rename from src/dialogs.cpp rename to src/dialog.cpp index 6d1e90f..f647098 100644 --- a/src/dialogs.cpp +++ b/src/dialog.cpp @@ -1,4 +1,4 @@ -#include "dialogs.hpp" +#include "dialog.hpp" #include "filesystem.hpp" #include @@ -94,3 +94,33 @@ std::string Dialog::gtk_dialog(const boost::filesystem::path &path, const std::s dialog.add_button(button.first, button.second); return dialog.run() == Gtk::RESPONSE_OK ? dialog.get_filename() : ""; } + +std::string Dialog::open_folder(const boost::filesystem::path &path) { + return gtk_dialog(path, "Open Folder", + {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Open", Gtk::RESPONSE_OK)}, + Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); +} + +std::string Dialog::new_file(const boost::filesystem::path &path) { + return gtk_dialog(path, "New File", + {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)}, + Gtk::FILE_CHOOSER_ACTION_SAVE); +} + +std::string Dialog::new_folder(const boost::filesystem::path &path) { + return gtk_dialog(path, "New Folder", + {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Create", Gtk::RESPONSE_OK)}, + Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER); +} + +std::string Dialog::open_file(const boost::filesystem::path &path) { + return gtk_dialog(path, "Open File", + {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Select", Gtk::RESPONSE_OK)}, + Gtk::FILE_CHOOSER_ACTION_OPEN); +} + +std::string Dialog::save_file_as(const boost::filesystem::path &path) { + return gtk_dialog(path, "Save File As", + {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)}, + Gtk::FILE_CHOOSER_ACTION_SAVE); +} diff --git a/src/dialogs.hpp b/src/dialog.hpp similarity index 99% rename from src/dialogs.hpp rename to src/dialog.hpp index b4be549..5ea0230 100644 --- a/src/dialogs.hpp +++ b/src/dialog.hpp @@ -6,12 +6,6 @@ class Dialog { public: - static std::string open_folder(const boost::filesystem::path &path); - static std::string open_file(const boost::filesystem::path &path); - static std::string new_file(const boost::filesystem::path &path); - static std::string new_folder(const boost::filesystem::path &path); - static std::string save_file_as(const boost::filesystem::path &path); - class Message : public Gtk::Window { public: Message(const std::string &text, std::function &&on_cancel = {}, bool show_progrss_bar = false); @@ -28,4 +22,11 @@ private: static std::string gtk_dialog(const boost::filesystem::path &path, const std::string &title, const std::vector> &buttons, Gtk::FileChooserAction gtk_options); + +public: + static std::string open_folder(const boost::filesystem::path &path); + static std::string open_file(const boost::filesystem::path &path); + static std::string new_file(const boost::filesystem::path &path); + static std::string new_folder(const boost::filesystem::path &path); + static std::string save_file_as(const boost::filesystem::path &path); }; diff --git a/src/dialogs_unix.cpp b/src/dialogs_unix.cpp deleted file mode 100644 index fe2dc94..0000000 --- a/src/dialogs_unix.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "dialogs.hpp" - -std::string Dialog::open_folder(const boost::filesystem::path &path) { - return gtk_dialog(path, "Open Folder", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Open", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER); -} - -std::string Dialog::new_file(const boost::filesystem::path &path) { - return gtk_dialog(path, "New File", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_SAVE); -} - -std::string Dialog::new_folder(const boost::filesystem::path &path) { - return gtk_dialog(path, "New Folder", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Create", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER); -} - -std::string Dialog::open_file(const boost::filesystem::path &path) { - return gtk_dialog(path, "Open File", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Select", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_OPEN); -} - -std::string Dialog::save_file_as(const boost::filesystem::path &path) { - return gtk_dialog(path, "Save File As", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_SAVE); -} diff --git a/src/dialogs_win.cpp b/src/dialogs_win.cpp deleted file mode 100644 index dc33d79..0000000 --- a/src/dialogs_win.cpp +++ /dev/null @@ -1,163 +0,0 @@ -#include "dialogs.hpp" -#include "juci.hpp" -#include "singletons.hpp" - -#undef NTDDI_VERSION -#define NTDDI_VERSION NTDDI_VISTA -#undef _WIN32_WINNT -#define _WIN32_WINNT _WIN32_WINNT_VISTA - -#include -#include -#include - -#include //TODO: remove -using namespace std; //TODO: remove - -class Win32Dialog { -public: - Win32Dialog(){}; - - ~Win32Dialog() { - if(dialog) - dialog->Release(); - } - - /** Returns the selected item's path as a string */ - std::string open(const std::wstring &title, unsigned option = 0) { - if(!init(CLSID_FileOpenDialog)) - return ""; - - if(!set_title(title) || !add_option(option)) - return ""; - if(!set_folder()) - return ""; - - return show(); - } - - std::string save(const std::wstring &title, const boost::filesystem::path &file_path = "", unsigned option = 0) { - if(!init(CLSID_FileSaveDialog)) - return ""; - - if(!set_title(title) || !add_option(option)) - return ""; - if(!set_folder()) - return ""; - std::vector extensions; - if(!file_path.empty()) { - if(file_path.has_extension() && file_path.filename() != file_path.extension()) { - auto extension = (L"*" + file_path.extension().native()).c_str(); - extensions.emplace_back(COMDLG_FILTERSPEC{extension, extension}); - if(!set_default_file_extension(extension)) - return ""; - } - } - extensions.emplace_back(COMDLG_FILTERSPEC{L"All files", L"*.*"}); - if(dialog->SetFileTypes(extensions.size(), extensions.data()) != S_OK) - return ""; - - return show(); - } - -private: - IFileDialog *dialog = nullptr; - DWORD options; - - bool init(CLSID type) { - if(CoCreateInstance(type, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog)) != S_OK) - return false; - if(dialog->GetOptions(&options) != S_OK) - return false; - return true; - } - - /** available options are listed at https://msdn.microsoft.com/en-gb/library/windows/desktop/dn457282(v=vs.85).aspx */ - bool add_option(unsigned option) { - if(dialog->SetOptions(options | option) != S_OK) - return false; - return true; - } - - bool set_title(const std::wstring &title) { - if(dialog->SetTitle(title.c_str()) != S_OK) - return false; - return true; - } - - /** Sets the extensions the browser can find */ - bool set_default_file_extension(const std::wstring &file_extension) { - if(dialog->SetDefaultExtension(file_extension.c_str()) != S_OK) - return false; - return true; - } - - /** Sets the directory to start browsing */ - bool set_folder() { - auto g_application = g_application_get_default(); //TODO: Post issue that Gio::Application::get_default should return pointer and not Glib::RefPtr - auto gio_application = Glib::wrap(g_application, true); - auto application = Glib::RefPtr::cast_static(gio_application); - - auto current_path = application->window->notebook.get_current_folder(); - boost::system::error_code ec; - if(current_path.empty()) - current_path = boost::filesystem::current_path(ec); - if(ec) - return false; - - std::wstring path = current_path.native(); - size_t pos = 0; - while((pos = path.find(L'/', pos)) != std::wstring::npos) { //TODO: issue bug report on boost::filesystem::path::native on MSYS2 - path.replace(pos, 1, L"\\"); - pos++; - } - - IShellItem *folder = nullptr; - if(SHCreateItemFromParsingName(path.c_str(), nullptr, IID_PPV_ARGS(&folder)) != S_OK) - return false; - if(dialog->SetFolder(folder) != S_OK) - return false; - folder->Release(); - return true; - } - - std::string show() { - if(dialog->Show(nullptr) != S_OK) - return ""; - IShellItem *result = nullptr; - if(dialog->GetResult(&result) != S_OK) - return ""; - LPWSTR file_path = nullptr; - auto hresult = result->GetDisplayName(SIGDN_FILESYSPATH, &file_path); - result->Release(); - if(hresult != S_OK) - return ""; - std::wstring file_path_wstring(file_path); - std::string file_path_string(file_path_wstring.begin(), file_path_wstring.end()); - CoTaskMemFree(file_path); - return file_path_string; - } -}; - -std::string Dialog::open_folder() { - return Win32Dialog().open(L"Open Folder", FOS_PICKFOLDERS); -} - -std::string Dialog::new_file() { - return Win32Dialog().save(L"New File"); -} - -std::string Dialog::new_folder() { - //Win32 (IFileDialog) does not support create folder... - return gtk_dialog("New Folder", - {std::make_pair("Cancel", Gtk::RESPONSE_CANCEL), std::make_pair("Create", Gtk::RESPONSE_OK)}, - Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER); -} - -std::string Dialog::open_file() { - return Win32Dialog().open(L"Open File"); -} - -std::string Dialog::save_file_as(const boost::filesystem::path &file_path) { - return Win32Dialog().save(L"Save File As", file_path); -} diff --git a/src/meson.cpp b/src/meson.cpp index 67f2a71..22de0cb 100644 --- a/src/meson.cpp +++ b/src/meson.cpp @@ -1,7 +1,7 @@ #include "meson.hpp" #include "compile_commands.hpp" #include "config.hpp" -#include "dialogs.hpp" +#include "dialog.hpp" #include "filesystem.hpp" #include "terminal.hpp" #include "utility.hpp" diff --git a/src/source_clang.cpp b/src/source_clang.cpp index ee9739a..c0bb4f7 100644 --- a/src/source_clang.cpp +++ b/src/source_clang.cpp @@ -7,7 +7,7 @@ #endif #include "compile_commands.hpp" #include "ctags.hpp" -#include "dialogs.hpp" +#include "dialog.hpp" #include "documentation.hpp" #include "filesystem.hpp" #include "info.hpp" diff --git a/src/usages_clang.cpp b/src/usages_clang.cpp index b6af6a7..11150d3 100644 --- a/src/usages_clang.cpp +++ b/src/usages_clang.cpp @@ -1,7 +1,7 @@ #include "usages_clang.hpp" #include "compile_commands.hpp" #include "config.hpp" -#include "dialogs.hpp" +#include "dialog.hpp" #include "filesystem.hpp" #include "utility.hpp" #include diff --git a/src/window.cpp b/src/window.cpp index 7685aea..33c79af 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -4,7 +4,7 @@ #include "debug_lldb.hpp" #endif #include "compile_commands.hpp" -#include "dialogs.hpp" +#include "dialog.hpp" #include "directories.hpp" #include "entrybox.hpp" #include "filesystem.hpp" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5428c91..9d1c817 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,7 +9,7 @@ include_directories(${CMAKE_SOURCE_DIR}/src) add_library(test_stubs OBJECT stubs/config.cpp - stubs/dialogs.cpp + stubs/dialog.cpp stubs/directories.cpp stubs/info.cpp stubs/notebook.cpp diff --git a/tests/stubs/dialogs.cpp b/tests/stubs/dialog.cpp similarity index 92% rename from tests/stubs/dialogs.cpp rename to tests/stubs/dialog.cpp index 50255e4..404ef76 100644 --- a/tests/stubs/dialogs.cpp +++ b/tests/stubs/dialog.cpp @@ -1,4 +1,4 @@ -#include "dialogs.hpp" +#include "dialog.hpp" Dialog::Message::Message(const std::string &text, std::function &&on_cancel, bool show_progress_bar) : Gtk::Window(Gtk::WindowType::WINDOW_POPUP) {}