From daaa1a958e69e83cf84fa74093be52658a06ee0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Sun, 1 Nov 2015 20:24:39 +0100 Subject: [PATCH 1/5] Attempt to set default folder for win dialogs --- src/CMakeLists.txt | 2 ++ src/dialogs.h | 18 +++++++++--------- src/dialogs_win.cc | 30 +++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2da1c6f..e66417f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -106,6 +106,7 @@ include_directories( ${LIBCLANG_INCLUDE_DIRS} ${ASPELL_INCLUDE_DIR} ../libclangmm/src + "/usr/x86_64-w64-mingw32/include" ) link_directories( @@ -114,6 +115,7 @@ 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 086a93b..92638f5 100644 --- a/src/dialogs.h +++ b/src/dialogs.h @@ -38,8 +38,14 @@ private: 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); + /** Sets the extensions the browser can find */ + void set_file_extensions(const std::vector file_extensions); + /** Sets the directory to start browsing */ + void set_default_folder(const std::string &directory_path); + /** Returns the selected item's path as a string */ std::string show(); private: @@ -49,18 +55,12 @@ private: class OpenDialog : public CommonDialog { public: - OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { - set_title(title); - add_option(option); - } + OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog); }; - class SaveDialog : public CommonDialog { public: - SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { - set_title(title); - add_option(option); - } + SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog); }; + #endif // __WIN32 #endif // JUCI_DIALOG_H_ diff --git a/src/dialogs_win.cc b/src/dialogs_win.cc index d7b2791..22db8fa 100644 --- a/src/dialogs_win.cc +++ b/src/dialogs_win.cc @@ -60,6 +60,19 @@ 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_folder(const std::string &directory_path) { + IShellItem * folder = nullptr; + WinString str; + &str = (str.s2ws(directory_path)).data(); + check(SHCreateItemFromParsingName(&str, NULL, 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"); @@ -73,7 +86,22 @@ std::string CommonDialog::show() { return ""; } } -// COMMON_DIALOG }} + +OpenDialog::OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog); { + set_title(title); + add_option(option); + auto dirs = Singleton::directories()->current_path; + set_default_folder(dirs.empty() ? boost::filesystem::current_path() : dirs); +} + +SaveDialog::SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { + set_title(title); + add_option(option); + auto dirs = Singleton::directories()->current_path; + set_default_folder(dirs.empty() ? boost::filesystem::current_path() : dirs); +} + +// DIALOGS }} std::string Dialog::select_folder() { return (OpenDialog("Select folder", FOS_PICKFOLDERS)).show(); } From 7f88c51d6788c9eabddaa44161b1b449ba50f53d Mon Sep 17 00:00:00 2001 From: zalox Date: Mon, 2 Nov 2015 14:39:00 +0100 Subject: [PATCH 2/5] Intermediate commit, please ignore --- src/CMakeLists.txt | 3 ++- src/dialogs.h | 12 +++++++----- src/dialogs_win.cc | 28 +++++++++++----------------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e66417f..6df0f15 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,7 +28,8 @@ find_package(LibClang REQUIRED) #find_package(PythonLibs 2.7) #find_package(Boost 1.55 COMPONENTS python thread log system filesystem REQUIRED) -find_package(Boost 1.55 COMPONENTS thread log system filesystem REQUIRED) + +find_package(Boost 1.55 COMPONENTS thread log system filesystem locale REQUIRED) pkg_check_modules(GTKMM gtkmm-3.0 REQUIRED) # The name GTKMM is set here for the variables abouve diff --git a/src/dialogs.h b/src/dialogs.h index 92638f5..fe64b2b 100644 --- a/src/dialogs.h +++ b/src/dialogs.h @@ -21,6 +21,7 @@ class Dialog { #include #include #include +#include class WinString { public: @@ -29,10 +30,11 @@ public: ~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; - std::wstring s2ws(const std::string& str); - std::string ws2s(const std::wstring& wstr); }; class CommonDialog { @@ -42,7 +44,7 @@ public: void add_option(unsigned option); void set_title(const std::string &title); /** Sets the extensions the browser can find */ - void set_file_extensions(const std::vector file_extensions); + void set_file_extensions(const std::vector &file_extensions); /** Sets the directory to start browsing */ void set_default_folder(const std::string &directory_path); /** Returns the selected item's path as a string */ @@ -55,11 +57,11 @@ private: class OpenDialog : public CommonDialog { public: - OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog); + OpenDialog(const std::string &title, unsigned option); }; class SaveDialog : public CommonDialog { public: - SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog); + SaveDialog(const std::string &title, unsigned option); }; #endif // __WIN32 diff --git a/src/dialogs_win.cc b/src/dialogs_win.cc index 22db8fa..b74338a 100644 --- a/src/dialogs_win.cc +++ b/src/dialogs_win.cc @@ -1,7 +1,7 @@ #ifdef _WIN32 #include "dialogs.h" #include "singletons.h" - +#include #ifndef check HRESULT __hr__; #define check(__fun__, error_message) \ @@ -29,17 +29,12 @@ std::string WinString::operator()() { return res; } -// http://stackoverflow.com/questions/4804298/how-to-convert-wstring-into-string std::wstring WinString::s2ws(const std::string& str) { - typedef std::codecvt_utf8 convert_typeX; - std::wstring_convert converterX; - return converterX.from_bytes(str); + return boost::locale::conv::utf_to_utf(str); } std::string WinString::ws2s(const std::wstring& wstr) { - typedef std::codecvt_utf8 convert_typeX; - std::wstring_convert converterX; - return converterX.to_bytes(wstr); + return boost::locale::conv::utf_to_utf(wstr); } // WINSTRING } @@ -51,9 +46,7 @@ CommonDialog::CommonDialog(CLSID type) : dialog(nullptr) { } void CommonDialog::set_title(const std::string &title) { - auto tmp = std::wstring(title.begin(), title.end()); - auto t = tmp.data(); - check(dialog->SetTitle(t), "Failed to set dialog title"); + check(dialog->SetTitle(), "Failed to set dialog title"); } void CommonDialog::add_option(unsigned option) { @@ -65,10 +58,11 @@ void CommonDialog::set_file_extensions(const std::vector &file_exte } void CommonDialog::set_default_folder(const std::string &directory_path) { + std::cout << directory_path << std::endl; IShellItem * folder = nullptr; - WinString str; - &str = (str.s2ws(directory_path)).data(); - check(SHCreateItemFromParsingName(&str, NULL, IID_PPV_ARGS(&folder)), "Failed to create string"); + auto dir = WinString::s2ws(directory_path); + std::wcout << dir << std::endl; + check(SHCreateItemFromParsingName(dir.data(), nullptr, IID_PPV_ARGS(&folder)), "Failed to create string"); check(dialog->SetDefaultFolder(folder), "Failed to set default folder"); folder->Release(); } @@ -87,18 +81,18 @@ std::string CommonDialog::show() { } } -OpenDialog::OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog); { +OpenDialog::OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { set_title(title); add_option(option); auto dirs = Singleton::directories()->current_path; - set_default_folder(dirs.empty() ? boost::filesystem::current_path() : dirs); + 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); add_option(option); auto dirs = Singleton::directories()->current_path; - set_default_folder(dirs.empty() ? boost::filesystem::current_path() : dirs); + set_default_folder(dirs.empty() ? boost::filesystem::current_path().string() : dirs.string()); } // DIALOGS }} 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 3/5] 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()); From eb1836b9ecab4a8bf7b5591d7b2638e160504735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 2 Nov 2015 21:51:52 +0100 Subject: [PATCH 4/5] Add default file extensions --- src/dialogs.h | 6 +++--- src/dialogs_win.cc | 11 +++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/dialogs.h b/src/dialogs.h index be61bad..5d4420e 100644 --- a/src/dialogs.h +++ b/src/dialogs.h @@ -36,11 +36,9 @@ public: /** Returns the selected item's path as a string */ std::string show(); -private: +protected: 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 { @@ -50,6 +48,8 @@ public: class SaveDialog : public CommonDialog { public: SaveDialog(std::string &&title, unsigned option); +private: + std::vector extensions; }; #endif // __WIN32 diff --git a/src/dialogs_win.cc b/src/dialogs_win.cc index 5ea7e57..ecee64c 100644 --- a/src/dialogs_win.cc +++ b/src/dialogs_win.cc @@ -24,8 +24,9 @@ void CommonDialog::set_title(std::string &&title) { void CommonDialog::add_option(unsigned option) { check(dialog->SetOptions(options | option), "Failed to set options"); } -void CommonDialog::set_default_file_extension(std::string &&file_extensions) { - +void CommonDialog::set_default_file_extension(std::string &&file_extension) { + auto ptr = boost::locale::conv::utf_to_utf(file_extension).data(); + check(dialog->SetDefaultExtension(ptr), "Failed to set file extension"); } void CommonDialog::set_default_folder(const std::string &directory_path) { IShellItem * folder = nullptr; @@ -62,6 +63,12 @@ SaveDialog::SaveDialog(std::string &&title, unsigned option) : CommonDialog(CLSI add_option(option); auto dirs = Singleton::directories()->current_path; set_default_folder(dirs.empty() ? boost::filesystem::current_path().string() : dirs.string()); + extensions.emplace_back(COMDLG_FILTERSPEC{L"Default", L"*.h;*.cpp"}); + extensions.emplace_back(COMDLG_FILTERSPEC{L"GoogleStyle", L"*.cc;*.h"}); + extensions.emplace_back(COMDLG_FILTERSPEC{L"BoostStyle", L"*.hpp;*.cpp"}); + extensions.emplace_back(COMDLG_FILTERSPEC{L"Other", L"*.cxx;*.c"}); + check(dialog->SetFileTypes(extensions.size(), extensions.data()), "Failed to set extensions"); + set_default_file_extension("Default"); } // DIALOGS }} From d6985aa3dd028e218aa82c3052ec1fe87084ffc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Mon, 2 Nov 2015 22:01:12 +0100 Subject: [PATCH 5/5] Remove some includes and remove locale dependency from unix --- src/CMakeLists.txt | 9 ++++++++- src/dialogs.h | 4 ---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0be9bf3..e1e7ef9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,7 +29,14 @@ find_package(LibClang REQUIRED) #find_package(Boost 1.55 COMPONENTS python thread log system filesystem REQUIRED) -find_package(Boost 1.55 COMPONENTS thread log system filesystem locale REQUIRED) +set(BOOST_DEP thread log system filesystem) + +if(MSYS) + list(APPEND BOOST_DEP locale) +endif() + + +find_package(Boost 1.55 COMPONENTS ${BOOST_DEP} REQUIRED) pkg_check_modules(GTKMM gtkmm-3.0 REQUIRED) # The name GTKMM is set here for the variables abouve diff --git a/src/dialogs.h b/src/dialogs.h index 5d4420e..5b71b0c 100644 --- a/src/dialogs.h +++ b/src/dialogs.h @@ -17,10 +17,6 @@ class Dialog { #include #include - -#include -#include -#include #include class CommonDialog {