mirror of https://gitlab.com/cppit/jucipp
2 changed files with 60 additions and 27 deletions
@ -1,45 +1,78 @@ |
|||||||
#include "dialogs.h" |
#include "dialogs.h" |
||||||
|
#include <windows.h> |
||||||
#include <shobjidl.h> |
#include <shobjidl.h> |
||||||
#include <memory> |
#include <memory> |
||||||
#include <exception> |
#include <exception> |
||||||
#include <singletons.h> |
#include "singletons.h" |
||||||
|
#include <sstream> |
||||||
|
|
||||||
#ifndef check(__fun__) |
#ifndef check |
||||||
#define MESSAGE "An error occurred when trying open windows dialog" |
#define MESSAGE "An error occurred when trying open windows dialog" |
||||||
#define check(__fun__) auto __hr__ = __fun__; if(FAILED(__hr__)) Singleton::terminal()->print(MESSAGE) |
HRESULT __hr__; |
||||||
|
#define check(__fun__) __hr__ = __fun__; if(FAILED(__hr__)) Singleton::terminal()->print(MESSAGE) |
||||||
#endif |
#endif |
||||||
|
|
||||||
|
class win_string { |
||||||
|
public: |
||||||
|
win_string() : str(nullptr) { } |
||||||
|
~win_string() { CoTaskMemFree(static_cast<void*>(str)); } |
||||||
|
std::string operator()(){ |
||||||
|
std::string res; |
||||||
|
if (str != nullptr) { |
||||||
|
std::wstringstream ss; |
||||||
|
ss << str; |
||||||
|
res = std::string(ss.str().begin(), ss.str().end()); |
||||||
|
} |
||||||
|
return res; |
||||||
|
} |
||||||
|
wchar_t** operator&() { return &str; } |
||||||
|
private: |
||||||
|
wchar_t* str; |
||||||
|
}; |
||||||
|
|
||||||
std::string Dialog::select_folder() { |
class CommonDialog { |
||||||
IFileOpenDialog *dialog = nullptr; |
public: |
||||||
check(CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog))); |
CommonDialog() : dialog(nullptr) { |
||||||
DWORD options; |
check(CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&dialog))); |
||||||
check(dialog->GetOptions(&options)); |
check(dialog->GetOptions(&options)); |
||||||
|
} |
||||||
|
CommonDialog(const std::string &title, unsigned option) : CommonDialog() { |
||||||
|
set_title(title); |
||||||
|
add_option(option); |
||||||
|
} |
||||||
|
void set_title(const std::string &title) { check(dialog->SetTitle(title)); } |
||||||
|
void add_option(unsigned option) { check(dialog->SetOptions(options | option)); } |
||||||
|
std::string show() { |
||||||
|
check(dialog->Show(nullptr)); |
||||||
|
IShellItem *result = nullptr; |
||||||
|
check(dialog->GetResult(&result)); |
||||||
|
win_string str; |
||||||
|
check(result->GetDisplayName(SIGDN_FILESYSPATH, &str));; |
||||||
|
result->Release(); |
||||||
|
return str(); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
IFileOpenDialog * dialog; |
||||||
|
DWORD options; |
||||||
|
}; |
||||||
|
|
||||||
|
std::string Dialog::select_folder() { |
||||||
|
return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); |
||||||
} |
} |
||||||
/*
|
|
||||||
std::string Dialog::new_file() { |
std::string Dialog::new_file() { |
||||||
return open_dialog("Please create a new file", |
return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); |
||||||
{std::make_pair("Cancel", Gtk::RESPONSECANCEL), std::make_pair("Save", Gtk::RESPONSE_OK)}, |
|
||||||
Gtk::FILE_CHOOSER_ACTION_SAVE); |
|
||||||
} |
} |
||||||
|
|
||||||
std::string Dialog::new_folder() { |
std::string Dialog::new_folder() { |
||||||
return open_dialog("Please create a new folder", |
return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); |
||||||
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Create", Gtk::RESPONSE_OK)}, |
|
||||||
Gtk::FILE_CHOOSER_ACTION_CREATE_FOLDER); |
|
||||||
} |
} |
||||||
|
|
||||||
std::string Dialog::select_file() { |
std::string Dialog::select_file() { |
||||||
return open_dialog("Please choose a folder", |
return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); |
||||||
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Select", Gtk::RESPONSE_OK)}, |
|
||||||
Gtk::FILE_CHOOSER_ACTION_OPEN); |
|
||||||
} |
} |
||||||
|
|
||||||
std::string Dialog::save_file() { |
std::string Dialog::save_file() { |
||||||
return open_dialog("Please choose a file", |
return (CommonDialog("Please select a folder", FOS_PICKFOLDERS)).show(); |
||||||
{std::make_pair("Cancel", Gtk::RESPONSE_CANCEL),std::make_pair("Save", Gtk::RESPONSE_OK)}, |
} |
||||||
Gtk::FILE_CHOOSER_ACTION_OPEN); |
|
||||||
} |
|
||||||
|
|
||||||
*/ |
|
||||||
Loading…
Reference in new issue