From 28651e9615c0728c6d9529a172d0e624887e6bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 2 Nov 2015 20:52:54 +0100 Subject: [PATCH] Remove usage of codecvt since gcc has poor support. Use boost locale instead --- src/CMakeLists.txt | 2 -- src/dialogs.h | 24 +++++------------- src/dialogs_win.cc | 61 ++++++++++++---------------------------------- 3 files changed, 21 insertions(+), 66 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6df0f15..0be9bf3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -107,7 +107,6 @@ include_directories( ${LIBCLANG_INCLUDE_DIRS} ${ASPELL_INCLUDE_DIR} ../libclangmm/src - "/usr/x86_64-w64-mingw32/include" ) link_directories( @@ -116,7 +115,6 @@ link_directories( ${Boost_LIBRARY_DIRS} # ${PYTHON_INCLUDE_DIRS} ${LIBCLANG_LIBRARY_DIRS} - "/usr/x86_64-w64-mingw32/lib" ) # set_target_properties(${module} diff --git a/src/dialogs.h b/src/dialogs.h index fe64b2b..be61bad 100644 --- a/src/dialogs.h +++ b/src/dialogs.h @@ -23,28 +23,14 @@ class Dialog { #include #include -class WinString { -public: - WinString() : str(nullptr) { } - WinString(const std::string &string); - ~WinString() { CoTaskMemFree(static_cast(str)); } - std::string operator()(); - wchar_t** operator&() { return &str; } - static std::wstring s2ws(const std::string& str); - static std::string ws2s(const std::wstring& wstr); - -private: - wchar_t* str; -}; - class CommonDialog { public: CommonDialog(CLSID type); /** available options are listed https://msdn.microsoft.com/en-gb/library/windows/desktop/dn457282(v=vs.85).aspx */ void add_option(unsigned option); - void set_title(const std::string &title); + void set_title(std::string &&title); /** Sets the extensions the browser can find */ - void set_file_extensions(const std::vector &file_extensions); + void set_default_file_extension(std::string &&file_extension); /** Sets the directory to start browsing */ void set_default_folder(const std::string &directory_path); /** Returns the selected item's path as a string */ @@ -53,15 +39,17 @@ public: private: IFileDialog * dialog; DWORD options; + const std::vector text_files = {L"Text files", L"*.txt;*.c"}; + const std::vector all_files = {L"", L""}; }; class OpenDialog : public CommonDialog { public: - OpenDialog(const std::string &title, unsigned option); + OpenDialog(std::string &&title, unsigned option); }; class SaveDialog : public CommonDialog { public: - SaveDialog(const std::string &title, unsigned option); + SaveDialog(std::string &&title, unsigned option); }; #endif // __WIN32 diff --git a/src/dialogs_win.cc b/src/dialogs_win.cc index b74338a..5ea7e57 100644 --- a/src/dialogs_win.cc +++ b/src/dialogs_win.cc @@ -12,84 +12,53 @@ HRESULT __hr__; } #endif // CHECK - -// { WINSTRING -WinString::WinString(const std::string &string) { - std::wstringstream ss; - ss << s2ws(string); - ss >> str; -} - -std::string WinString::operator()() { - std::string res; - if (str != nullptr) { - std::wstring ss(str); - res = ws2s(ss); - } - return res; -} - -std::wstring WinString::s2ws(const std::string& str) { - return boost::locale::conv::utf_to_utf(str); -} - -std::string WinString::ws2s(const std::wstring& wstr) { - return boost::locale::conv::utf_to_utf(wstr); -} - -// WINSTRING } - // { COMMON_DIALOG CommonDialog::CommonDialog(CLSID type) : dialog(nullptr) { check(CoCreateInstance(type, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog)), "Failed to create instance"); check(dialog->GetOptions(&options), "Failed to get options from instance"); } - -void CommonDialog::set_title(const std::string &title) { - check(dialog->SetTitle(), "Failed to set dialog title"); +void CommonDialog::set_title(std::string &&title) { + auto ptr = boost::locale::conv::utf_to_utf(title).data(); + check(dialog->SetTitle(ptr), "Failed to set dialog title"); } - void CommonDialog::add_option(unsigned option) { check(dialog->SetOptions(options | option), "Failed to set options"); } - -void CommonDialog::set_file_extensions(const std::vector &file_extensions) { - return; +void CommonDialog::set_default_file_extension(std::string &&file_extensions) { + } - void CommonDialog::set_default_folder(const std::string &directory_path) { - std::cout << directory_path << std::endl; IShellItem * folder = nullptr; - auto dir = WinString::s2ws(directory_path); - std::wcout << dir << std::endl; - check(SHCreateItemFromParsingName(dir.data(), nullptr, IID_PPV_ARGS(&folder)), "Failed to create string"); + auto ptr = boost::locale::conv::utf_to_utf(directory_path).data(); + check(SHCreateItemFromParsingName(ptr, nullptr, IID_PPV_ARGS(&folder)), "Failed to create string"); check(dialog->SetDefaultFolder(folder), "Failed to set default folder"); folder->Release(); } - std::string CommonDialog::show() { try { check(dialog->Show(nullptr), "Failed to show dialog"); IShellItem *result = nullptr; check(dialog->GetResult(&result), "Failed to get result from dialog"); - WinString str; + LPWSTR str = nullptr; check(result->GetDisplayName(SIGDN_FILESYSPATH, &str), "Failed to get display name from dialog"); result->Release(); - return str(); + auto res = boost::locale::conv::utf_to_utf(str); + CoTaskMemFree(str); + return res; } catch (std::exception e) { return ""; } } -OpenDialog::OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { - set_title(title); +OpenDialog::OpenDialog(std::string &&title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { + set_title(std::move(title)); add_option(option); auto dirs = Singleton::directories()->current_path; set_default_folder(dirs.empty() ? boost::filesystem::current_path().string() : dirs.string()); } -SaveDialog::SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { - set_title(title); +SaveDialog::SaveDialog(std::string &&title, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { + set_title(std::move(title)); add_option(option); auto dirs = Singleton::directories()->current_path; set_default_folder(dirs.empty() ? boost::filesystem::current_path().string() : dirs.string());