From e00ce472928df6f52c669431e1a4199e80b781ce Mon Sep 17 00:00:00 2001 From: "U-ole-PC\\ole" Date: Tue, 3 Nov 2015 08:23:07 +0100 Subject: [PATCH] Fixed a crash in MSYS2 when canceling a dialog. Some cleanup as well. --- src/dialogs.cc | 4 +-- src/dialogs.h | 54 +------------------------------- src/dialogs_win.cc | 77 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 70 insertions(+), 65 deletions(-) diff --git a/src/dialogs.cc b/src/dialogs.cc index a386595..cb9b8b3 100644 --- a/src/dialogs.cc +++ b/src/dialogs.cc @@ -10,9 +10,9 @@ std::string open_dialog(const std::string &title, const std::string &file_name = "") { Gtk::FileChooserDialog dialog(title, gtk_options); if(!Singleton::directories->current_path.empty()) - gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), Singleton::directories->current_path.c_str()); + gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), Singleton::directories->current_path.string().c_str()); else - gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), boost::filesystem::current_path().c_str()); + gtk_file_chooser_set_current_folder((GtkFileChooser*)dialog.gobj(), boost::filesystem::current_path().string().c_str()); if (!file_name.empty()) gtk_file_chooser_set_filename((GtkFileChooser*)dialog.gobj(), file_name.c_str()); dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS); diff --git a/src/dialogs.h b/src/dialogs.h index 378238c..1dfeec2 100644 --- a/src/dialogs.h +++ b/src/dialogs.h @@ -10,58 +10,6 @@ public: static std::string new_file(); static std::string new_folder(); static std::string save_file(const boost::filesystem::path file_path); -}; // Dialog - -#ifdef _WIN32 -#define NTDDI_VERSION NTDDI_VISTA -#define _WIN32_WINNT _WIN32_WINNT_VISTA - -#include -#include - -#include -#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; } -private: - wchar_t* str; - std::wstring s2ws(const std::string& str); - std::string ws2s(const std::wstring& wstr); -}; - -class CommonDialog { -public: - CommonDialog(CLSID type); - void add_option(unsigned option); - void set_title(const std::string &title); - std::string show(); - -private: - IFileDialog * dialog; - DWORD options; }; -class OpenDialog : public CommonDialog { -public: - OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { - set_title(title); - add_option(option); - } -}; - -class SaveDialog : public CommonDialog { -public: - SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { - set_title(title); - add_option(option); - } -}; -#endif // __WIN32 -#endif // JUCI_DIALOG_H_ +#endif //JUCI_DIALOG_H_ \ No newline at end of file diff --git a/src/dialogs_win.cc b/src/dialogs_win.cc index 4e36682..d65bbd6 100644 --- a/src/dialogs_win.cc +++ b/src/dialogs_win.cc @@ -1,7 +1,9 @@ -#ifdef _WIN32 #include "dialogs.h" #include "singletons.h" +#include //TODO: remove +using namespace std; //TODO: remove + #ifndef check HRESULT __hr__; #define check(__fun__, error_message) \ @@ -12,6 +14,58 @@ HRESULT __hr__; } #endif // CHECK +#undef NTDDI_VERSION +#define NTDDI_VERSION NTDDI_VISTA +#undef _WIN32_WINNT +#define _WIN32_WINNT _WIN32_WINNT_VISTA + +#include +#include + +#include +#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; } +private: + wchar_t* str; + std::wstring s2ws(const std::string& str); + std::string ws2s(const std::wstring& wstr); +}; + +class CommonDialog { +public: + CommonDialog(CLSID type); + void add_option(unsigned option); + void set_title(const std::string &title); + std::string show(); + +private: + IFileDialog * dialog; + DWORD options; +}; + +class OpenDialog : public CommonDialog { +public: + OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { + set_title(title); + add_option(option); + } +}; + +class SaveDialog : public CommonDialog { +public: + SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { + set_title(title); + add_option(option); + } +}; // { WINSTRING WinString::WinString(const std::string &string) { @@ -61,17 +115,21 @@ void CommonDialog::add_option(unsigned option) { } 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; - check(result->GetDisplayName(SIGDN_FILESYSPATH, &str), "Failed to get display name from dialog"); + if(dialog->Show(nullptr)!=S_OK) { + dialog->Release(); + return ""; + } + IShellItem *result = nullptr; + if(dialog->GetResult(&result)!=S_OK) { result->Release(); - return str(); - } catch (std::exception e) { return ""; } + WinString str; + if(result->GetDisplayName(SIGDN_FILESYSPATH, &str)!=S_OK) + return ""; + result->Release(); + dialog->Release(); + return str(); } // COMMON_DIALOG }} std::string Dialog::select_folder() { @@ -94,4 +152,3 @@ std::string Dialog::save_file(const boost::filesystem::path file_path) { //TODO: use file_path return (SaveDialog("Please choose your destination", 0)).show(); } -#endif