Browse Source

Fixed a crash in MSYS2 when canceling a dialog. Some cleanup as well.

merge-requests/365/head
U-ole-PC\ole 10 years ago
parent
commit
e00ce47292
  1. 4
      src/dialogs.cc
  2. 54
      src/dialogs.h
  3. 77
      src/dialogs_win.cc

4
src/dialogs.cc

@ -10,9 +10,9 @@ std::string open_dialog(const std::string &title,
const std::string &file_name = "") { const std::string &file_name = "") {
Gtk::FileChooserDialog dialog(title, gtk_options); Gtk::FileChooserDialog dialog(title, gtk_options);
if(!Singleton::directories->current_path.empty()) 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 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()) if (!file_name.empty())
gtk_file_chooser_set_filename((GtkFileChooser*)dialog.gobj(), file_name.c_str()); gtk_file_chooser_set_filename((GtkFileChooser*)dialog.gobj(), file_name.c_str());
dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS); dialog.set_position(Gtk::WindowPosition::WIN_POS_CENTER_ALWAYS);

54
src/dialogs.h

@ -10,58 +10,6 @@ public:
static std::string new_file(); static std::string new_file();
static std::string new_folder(); static std::string new_folder();
static std::string save_file(const boost::filesystem::path file_path); 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 <windows.h>
#include <shobjidl.h>
#include <memory>
#include <sstream>
#include <codecvt>
class WinString {
public:
WinString() : str(nullptr) { }
WinString(const std::string &string);
~WinString() { CoTaskMemFree(static_cast<void*>(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 { #endif //JUCI_DIALOG_H_
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_

77
src/dialogs_win.cc

@ -1,7 +1,9 @@
#ifdef _WIN32
#include "dialogs.h" #include "dialogs.h"
#include "singletons.h" #include "singletons.h"
#include <iostream> //TODO: remove
using namespace std; //TODO: remove
#ifndef check #ifndef check
HRESULT __hr__; HRESULT __hr__;
#define check(__fun__, error_message) \ #define check(__fun__, error_message) \
@ -12,6 +14,58 @@ HRESULT __hr__;
} }
#endif // CHECK #endif // CHECK
#undef NTDDI_VERSION
#define NTDDI_VERSION NTDDI_VISTA
#undef _WIN32_WINNT
#define _WIN32_WINNT _WIN32_WINNT_VISTA
#include <windows.h>
#include <shobjidl.h>
#include <memory>
#include <sstream>
#include <codecvt>
class WinString {
public:
WinString() : str(nullptr) { }
WinString(const std::string &string);
~WinString() { CoTaskMemFree(static_cast<void*>(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::WinString(const std::string &string) { WinString::WinString(const std::string &string) {
@ -61,17 +115,21 @@ void CommonDialog::add_option(unsigned option) {
} }
std::string CommonDialog::show() { std::string CommonDialog::show() {
try { if(dialog->Show(nullptr)!=S_OK) {
check(dialog->Show(nullptr), "Failed to show dialog"); dialog->Release();
IShellItem *result = nullptr; return "";
check(dialog->GetResult(&result), "Failed to get result from dialog"); }
WinString str; IShellItem *result = nullptr;
check(result->GetDisplayName(SIGDN_FILESYSPATH, &str), "Failed to get display name from dialog"); if(dialog->GetResult(&result)!=S_OK) {
result->Release(); result->Release();
return str();
} catch (std::exception e) {
return ""; return "";
} }
WinString str;
if(result->GetDisplayName(SIGDN_FILESYSPATH, &str)!=S_OK)
return "";
result->Release();
dialog->Release();
return str();
} }
// COMMON_DIALOG }} // COMMON_DIALOG }}
std::string Dialog::select_folder() { std::string Dialog::select_folder() {
@ -94,4 +152,3 @@ std::string Dialog::save_file(const boost::filesystem::path file_path) {
//TODO: use file_path //TODO: use file_path
return (SaveDialog("Please choose your destination", 0)).show(); return (SaveDialog("Please choose your destination", 0)).show();
} }
#endif

Loading…
Cancel
Save