Browse Source

More fixes to dialogs_win.cc

merge-requests/365/head
U-olece-PC\olece 10 years ago
parent
commit
954d50e988
  1. 25
      src/dialogs_win.cc

25
src/dialogs_win.cc

@ -16,18 +16,22 @@ using namespace std; //TODO: remove
class CommonDialog { class CommonDialog {
public: public:
CommonDialog(CLSID type); CommonDialog(CLSID type);
~CommonDialog() {
if(dialog!=nullptr)
dialog->Release();
}
/** available options are listed at 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::wstring &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::wstring &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::wstring &directory_path); void set_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();
protected: protected:
IFileDialog * dialog; IFileDialog *dialog=nullptr;
DWORD options; DWORD options;
bool error=false; bool error=false;
}; };
@ -44,7 +48,7 @@ private:
std::vector<COMDLG_FILTERSPEC> extensions; std::vector<COMDLG_FILTERSPEC> extensions;
}; };
CommonDialog::CommonDialog(CLSID type) : dialog(nullptr) { CommonDialog::CommonDialog(CLSID type) {
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;
@ -71,7 +75,7 @@ void CommonDialog::set_default_file_extension(const std::wstring &file_extension
error=true; error=true;
} }
void CommonDialog::set_default_folder(const std::wstring &directory_path) { void CommonDialog::set_folder(const std::wstring &directory_path) {
if(error) return; if(error) return;
std::wstring path=directory_path; std::wstring path=directory_path;
@ -86,7 +90,7 @@ void CommonDialog::set_default_folder(const std::wstring &directory_path) {
error=true; error=true;
return; return;
} }
auto hresult=dialog->SetDefaultFolder(folder); auto hresult=dialog->SetFolder(folder);
folder->Release(); folder->Release();
if(hresult!=S_OK) if(hresult!=S_OK)
error=true; error=true;
@ -95,19 +99,16 @@ void CommonDialog::set_default_folder(const std::wstring &directory_path) {
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) {
dialog->Release();
return ""; return "";
} }
IShellItem *result = nullptr; IShellItem *result = nullptr;
if(dialog->GetResult(&result)!=S_OK) { if(dialog->GetResult(&result)!=S_OK) {
result->Release(); result->Release();
dialog->Release();
return ""; return "";
} }
LPWSTR str = nullptr; LPWSTR str = nullptr;
auto hresult=result->GetDisplayName(SIGDN_FILESYSPATH, &str); auto hresult=result->GetDisplayName(SIGDN_FILESYSPATH, &str);
result->Release(); result->Release();
dialog->Release();
if(hresult!=S_OK) if(hresult!=S_OK)
return ""; return "";
std::wstring wstr(str); std::wstring wstr(str);
@ -120,15 +121,15 @@ OpenDialog::OpenDialog(const std::wstring &title, unsigned option) : CommonDialo
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().native() : dirs.native()); set_folder(dirs.empty() ? boost::filesystem::current_path().native() : dirs.native());
} }
SaveDialog::SaveDialog(const std::wstring &title, const boost::filesystem::path &file_path, 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);
if(!file_path.empty()) { if(!file_path.empty()) {
set_default_folder(file_path.parent_path().native()); set_folder(file_path.parent_path().native());
if(file_path.has_extension()) { if(file_path.has_extension() && file_path.filename()!=file_path.extension()) {
auto extension=(L"*"+file_path.extension().native()).c_str(); auto extension=(L"*"+file_path.extension().native()).c_str();
extensions.emplace_back(COMDLG_FILTERSPEC{extension, extension}); extensions.emplace_back(COMDLG_FILTERSPEC{extension, extension});
set_default_file_extension(extension); set_default_file_extension(extension);
@ -136,7 +137,7 @@ SaveDialog::SaveDialog(const std::wstring &title, const boost::filesystem::path
} }
else { else {
auto dirs = Singleton::directories->current_path; auto dirs = Singleton::directories->current_path;
set_default_folder(dirs.empty() ? boost::filesystem::current_path().native() : dirs.native()); set_folder(dirs.empty() ? boost::filesystem::current_path().native() : dirs.native());
} }
extensions.emplace_back(COMDLG_FILTERSPEC{L"All files", L"*.*"}); extensions.emplace_back(COMDLG_FILTERSPEC{L"All files", L"*.*"});
if(dialog->SetFileTypes(extensions.size(), extensions.data())!=S_OK) { if(dialog->SetFileTypes(extensions.size(), extensions.data())!=S_OK) {

Loading…
Cancel
Save