|
|
|
@ -1,6 +1,5 @@ |
|
|
|
#include "dialogs.h" |
|
|
|
#include "dialogs.h" |
|
|
|
#include "singletons.h" |
|
|
|
#include "singletons.h" |
|
|
|
#include <boost/locale.hpp> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#undef NTDDI_VERSION |
|
|
|
#undef NTDDI_VERSION |
|
|
|
#define NTDDI_VERSION NTDDI_VISTA |
|
|
|
#define NTDDI_VERSION NTDDI_VISTA |
|
|
|
@ -14,26 +13,16 @@ |
|
|
|
#include <iostream> //TODO: remove |
|
|
|
#include <iostream> //TODO: remove |
|
|
|
using namespace std; //TODO: remove
|
|
|
|
using namespace std; //TODO: remove
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef check |
|
|
|
|
|
|
|
HRESULT __hr__; |
|
|
|
|
|
|
|
#define check(__fun__, error_message) \ |
|
|
|
|
|
|
|
__hr__ = __fun__; \
|
|
|
|
|
|
|
|
if (FAILED(__hr__)) { \
|
|
|
|
|
|
|
|
Singleton::terminal->print(error_message); \
|
|
|
|
|
|
|
|
throw std::runtime_error(error_message); \
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CHECK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommonDialog { |
|
|
|
class CommonDialog { |
|
|
|
public: |
|
|
|
public: |
|
|
|
CommonDialog(CLSID type); |
|
|
|
CommonDialog(CLSID type); |
|
|
|
/** available options are listed https://msdn.microsoft.com/en-gb/library/windows/desktop/dn457282(v=vs.85).aspx */ |
|
|
|
/** available options are listed at https://msdn.microsoft.com/en-gb/library/windows/desktop/dn457282(v=vs.85).aspx */ |
|
|
|
void add_option(unsigned option); |
|
|
|
void add_option(unsigned option); |
|
|
|
void set_title(const std::string &title); |
|
|
|
void set_title(const std::wstring &title); |
|
|
|
/** Sets the extensions the browser can find */ |
|
|
|
/** Sets the extensions the browser can find */ |
|
|
|
void set_default_file_extension(const std::string &file_extension); |
|
|
|
void set_default_file_extension(const std::wstring &file_extension); |
|
|
|
/** Sets the directory to start browsing */ |
|
|
|
/** Sets the directory to start browsing */ |
|
|
|
void set_default_folder(const std::string &directory_path); |
|
|
|
void set_default_folder(const std::wstring &directory_path); |
|
|
|
/** Returns the selected item's path as a string */ |
|
|
|
/** Returns the selected item's path as a string */ |
|
|
|
std::string show(); |
|
|
|
std::string show(); |
|
|
|
|
|
|
|
|
|
|
|
@ -45,55 +34,64 @@ protected: |
|
|
|
|
|
|
|
|
|
|
|
class OpenDialog : public CommonDialog { |
|
|
|
class OpenDialog : public CommonDialog { |
|
|
|
public: |
|
|
|
public: |
|
|
|
OpenDialog(const std::string &title, unsigned option); |
|
|
|
OpenDialog(const std::wstring &title, unsigned option=0); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class SaveDialog : public CommonDialog { |
|
|
|
class SaveDialog : public CommonDialog { |
|
|
|
public: |
|
|
|
public: |
|
|
|
SaveDialog(const std::string &title, unsigned option); |
|
|
|
SaveDialog(const std::wstring &title, const boost::filesystem::path &file_path="", unsigned option=0); |
|
|
|
private: |
|
|
|
private: |
|
|
|
std::vector<COMDLG_FILTERSPEC> extensions; |
|
|
|
std::vector<COMDLG_FILTERSPEC> extensions; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// { COMMON_DIALOG
|
|
|
|
|
|
|
|
CommonDialog::CommonDialog(CLSID type) : dialog(nullptr) { |
|
|
|
CommonDialog::CommonDialog(CLSID type) : dialog(nullptr) { |
|
|
|
if(CoCreateInstance(type, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog))!=S_OK) { |
|
|
|
if(CoCreateInstance(type, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog))!=S_OK) { |
|
|
|
error=true; |
|
|
|
error=true; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if(dialog->GetOptions(&options)!=S_OK) |
|
|
|
if(dialog->GetOptions(&options)!=S_OK) |
|
|
|
error=true; |
|
|
|
error=true; |
|
|
|
} |
|
|
|
} |
|
|
|
void CommonDialog::set_title(const std::string &title) { |
|
|
|
|
|
|
|
|
|
|
|
void CommonDialog::set_title(const std::wstring &title) { |
|
|
|
if(error) return; |
|
|
|
if(error) return; |
|
|
|
auto ptr = boost::locale::conv::utf_to_utf<wchar_t>(title).data(); |
|
|
|
if(dialog->SetTitle(title.c_str())!=S_OK) |
|
|
|
if(dialog->SetTitle(ptr)!=S_OK) |
|
|
|
|
|
|
|
error=true; |
|
|
|
error=true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void CommonDialog::add_option(unsigned option) { |
|
|
|
void CommonDialog::add_option(unsigned option) { |
|
|
|
if(error) return; |
|
|
|
if(error) return; |
|
|
|
if(dialog->SetOptions(options | option)!=S_OK) |
|
|
|
if(dialog->SetOptions(options | option)!=S_OK) |
|
|
|
error=true; |
|
|
|
error=true; |
|
|
|
} |
|
|
|
} |
|
|
|
void CommonDialog::set_default_file_extension(const std::string &file_extension) { |
|
|
|
|
|
|
|
|
|
|
|
void CommonDialog::set_default_file_extension(const std::wstring &file_extension) { |
|
|
|
if(error) return; |
|
|
|
if(error) return; |
|
|
|
auto ptr = boost::locale::conv::utf_to_utf<wchar_t>(file_extension).data(); |
|
|
|
if(dialog->SetDefaultExtension(file_extension.c_str())!=S_OK) |
|
|
|
if(dialog->SetDefaultExtension(ptr)!=S_OK) |
|
|
|
|
|
|
|
error=true; |
|
|
|
error=true; |
|
|
|
} |
|
|
|
} |
|
|
|
void CommonDialog::set_default_folder(const std::string &directory_path) { |
|
|
|
|
|
|
|
|
|
|
|
void CommonDialog::set_default_folder(const std::wstring &directory_path) { |
|
|
|
if(error) return; |
|
|
|
if(error) return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::wstring path=directory_path; |
|
|
|
|
|
|
|
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; |
|
|
|
IShellItem * folder = nullptr; |
|
|
|
auto ptr = boost::locale::conv::utf_to_utf<wchar_t>(directory_path).data(); |
|
|
|
if(SHCreateItemFromParsingName(path.c_str(), nullptr, IID_PPV_ARGS(&folder))!=S_OK) { |
|
|
|
if(SHCreateItemFromParsingName(ptr, nullptr, IID_PPV_ARGS(&folder))!=S_OK) { |
|
|
|
|
|
|
|
error=true; |
|
|
|
error=true; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
auto result=dialog->SetDefaultFolder(folder) |
|
|
|
auto hresult=dialog->SetDefaultFolder(folder); |
|
|
|
folder->Release(); |
|
|
|
folder->Release(); |
|
|
|
if(result!=S_OK) |
|
|
|
if(hresult!=S_OK) |
|
|
|
error=true; |
|
|
|
error=true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string CommonDialog::show() { |
|
|
|
std::string CommonDialog::show() { |
|
|
|
if(error) return ""; |
|
|
|
if(error) return ""; |
|
|
|
if(dialog->Show(nullptr)!=S_OK) { |
|
|
|
if(dialog->Show(nullptr)!=S_OK) { |
|
|
|
@ -107,54 +105,62 @@ std::string CommonDialog::show() { |
|
|
|
return ""; |
|
|
|
return ""; |
|
|
|
} |
|
|
|
} |
|
|
|
LPWSTR str = nullptr;
|
|
|
|
LPWSTR str = nullptr;
|
|
|
|
auto result=result->GetDisplayName(SIGDN_FILESYSPATH, &str); |
|
|
|
auto hresult=result->GetDisplayName(SIGDN_FILESYSPATH, &str); |
|
|
|
result->Release(); |
|
|
|
result->Release(); |
|
|
|
dialog->Release(); |
|
|
|
dialog->Release(); |
|
|
|
if(result!=S_OK) |
|
|
|
if(hresult!=S_OK) |
|
|
|
return ""; |
|
|
|
return ""; |
|
|
|
auto res = boost::locale::conv::utf_to_utf<char>(str); |
|
|
|
std::wstring wstr(str); |
|
|
|
|
|
|
|
std::string res(wstr.begin(), wstr.end()); |
|
|
|
CoTaskMemFree(str); |
|
|
|
CoTaskMemFree(str); |
|
|
|
return res; |
|
|
|
return res; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
OpenDialog::OpenDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { |
|
|
|
OpenDialog::OpenDialog(const std::wstring &title, unsigned option) : CommonDialog(CLSID_FileOpenDialog) { |
|
|
|
set_title(title); |
|
|
|
set_title(title); |
|
|
|
add_option(option); |
|
|
|
add_option(option); |
|
|
|
auto dirs = Singleton::directories()->current_path; |
|
|
|
auto dirs = Singleton::directories->current_path; |
|
|
|
set_default_folder(dirs.empty() ? boost::filesystem::current_path().string() : dirs.string()); |
|
|
|
set_default_folder(dirs.empty() ? boost::filesystem::current_path().native() : dirs.native()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SaveDialog::SaveDialog(const std::string &title, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { |
|
|
|
SaveDialog::SaveDialog(const std::wstring &title, const boost::filesystem::path &file_path, unsigned option) : CommonDialog(CLSID_FileSaveDialog) { |
|
|
|
set_title(title); |
|
|
|
set_title(title); |
|
|
|
add_option(option); |
|
|
|
add_option(option); |
|
|
|
auto dirs = Singleton::directories()->current_path; |
|
|
|
if(!file_path.empty()) { |
|
|
|
set_default_folder(dirs.empty() ? boost::filesystem::current_path().string() : dirs.string()); |
|
|
|
set_default_folder(file_path.parent_path().native()); |
|
|
|
//extensions.emplace_back(COMDLG_FILTERSPEC{L"Default", L"*.h;*.cpp"});
|
|
|
|
if(file_path.has_extension()) { |
|
|
|
//extensions.emplace_back(COMDLG_FILTERSPEC{L"GoogleStyle", L"*.cc;*.h"});
|
|
|
|
auto extension=(L"*"+file_path.extension().native()).c_str(); |
|
|
|
//extensions.emplace_back(COMDLG_FILTERSPEC{L"BoostStyle", L"*.hpp;*.cpp"});
|
|
|
|
extensions.emplace_back(COMDLG_FILTERSPEC{extension, extension}); |
|
|
|
//extensions.emplace_back(COMDLG_FILTERSPEC{L"Other", L"*.cxx;*.c"});
|
|
|
|
set_default_file_extension(extension); |
|
|
|
//check(dialog->SetFileTypes(extensions.size(), extensions.data()), "Failed to set extensions");
|
|
|
|
} |
|
|
|
//set_default_file_extension("Default");
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
auto dirs = Singleton::directories->current_path; |
|
|
|
|
|
|
|
set_default_folder(dirs.empty() ? boost::filesystem::current_path().native() : dirs.native()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
extensions.emplace_back(COMDLG_FILTERSPEC{L"All files", L"*.*"}); |
|
|
|
|
|
|
|
if(dialog->SetFileTypes(extensions.size(), extensions.data())!=S_OK) { |
|
|
|
|
|
|
|
error=true; |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DIALOGS }}
|
|
|
|
std::string Dialog::open_folder() { |
|
|
|
std::string Dialog::select_folder() { |
|
|
|
return (OpenDialog(L"Open Folder", FOS_PICKFOLDERS)).show(); |
|
|
|
return (OpenDialog("Select folder", FOS_PICKFOLDERS)).show(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string Dialog::new_file() { |
|
|
|
std::string Dialog::new_file() { |
|
|
|
return (SaveDialog("Please choose your destination", 0)).show(); |
|
|
|
return (SaveDialog(L"New File")).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string Dialog::new_folder() { |
|
|
|
std::string Dialog::new_folder() { |
|
|
|
return Dialog::select_folder(); |
|
|
|
return (OpenDialog(L"New Folder", FOS_PICKFOLDERS)).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string Dialog::select_file() { |
|
|
|
std::string Dialog::open_file() { |
|
|
|
return (OpenDialog("Open file", 0)).show(); |
|
|
|
return (OpenDialog(L"Open File")).show(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::string Dialog::save_file(const boost::filesystem::path file_path) { |
|
|
|
std::string Dialog::save_file_as(const boost::filesystem::path &file_path) { |
|
|
|
//TODO: use file_path
|
|
|
|
return (SaveDialog(L"Save File As", file_path)).show(); |
|
|
|
return (SaveDialog("Please choose your destination", 0)).show(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|