summaryrefslogtreecommitdiff
path: root/src/gui_gtk.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2017-07-28 22:05:37 +0200
committerJoel Klinghed <the_jk@yahoo.com>2017-07-28 22:11:23 +0200
commit29d3d1cef5272f5cba1fe573b664ea6059f05eda (patch)
tree9a3b346b75cf6464def9c73b8efddc74ae955060 /src/gui_gtk.cc
parent0898066430e0f2908565a1b4588e50de2d41a256 (diff)
Add GuiMain::file_dialog
Diffstat (limited to 'src/gui_gtk.cc')
-rw-r--r--src/gui_gtk.cc66
1 files changed, 49 insertions, 17 deletions
diff --git a/src/gui_gtk.cc b/src/gui_gtk.cc
index 301b92f..12972cf 100644
--- a/src/gui_gtk.cc
+++ b/src/gui_gtk.cc
@@ -573,6 +573,18 @@ private:
std::unordered_map<std::string, std::string> memory_;
};
+void set_filter(GtkFileChooser* chooser,
+ std::vector<GuiFile::Filter> const& filters) {
+ for (auto const& filter : filters) {
+ auto f = gtk_file_filter_new();
+ gtk_file_filter_set_name(f, filter.name.c_str());
+ for (auto const& mask : filter.masks) {
+ gtk_file_filter_add_pattern(f, mask.c_str());
+ }
+ gtk_file_chooser_add_filter(chooser, f);
+ }
+}
+
class GtkGuiMain : public virtual GuiMain, public GtkGuiWindow {
public:
GtkGuiMain(std::string const& title, uint32_t width, uint32_t height)
@@ -697,6 +709,32 @@ public:
gtk_target_entry_free(target);
}
+ std::string file_dialog(std::string const& title, std::string const& file,
+ uint8_t flags,
+ std::vector<GuiFile::Filter> const& filter) override {
+ auto chooser = gtk_file_chooser_native_new(
+ title.c_str(),
+ reinterpret_cast<GtkWindow*>(impl()),
+ flags & GuiFile::FILE_SAVE ? GTK_FILE_CHOOSER_ACTION_SAVE
+ : GTK_FILE_CHOOSER_ACTION_OPEN,
+ nullptr, nullptr);
+ if (flags & GuiFile::FILE_SAVE) {
+ gtk_file_chooser_set_do_overwrite_confirmation(
+ GTK_FILE_CHOOSER(chooser), true);
+ }
+ if (file.empty()) {
+ gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(chooser), file.c_str());
+ }
+ set_filter(GTK_FILE_CHOOSER(chooser), filter);
+ std::string ret_file;
+ auto ret = gtk_native_dialog_run(GTK_NATIVE_DIALOG(chooser));
+ if (ret == GTK_RESPONSE_ACCEPT) {
+ ret_file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser));
+ }
+ gtk_widget_destroy(GTK_WIDGET(chooser));
+ return ret_file;
+ }
+
private:
bool notify_about_to_exit() {
auto it = observers_.notify();
@@ -935,7 +973,7 @@ public:
std::string const& value,
std::string const& description,
uint8_t flags,
- std::vector<Filter> const& filter) override {
+ std::vector<GuiFile::Filter> const& filter) override {
values_.emplace_back(new FileValue(this, id, label, description, value,
flags, filter));
}
@@ -1004,7 +1042,7 @@ public:
for (auto const& value : values_) {
if (value->id_ == id && value->type_ == FILE) {
auto v = static_cast<FileValue*>(value.get());
- if ((v->flags_ & FILE_SAVE) == 0 && v->chooser_) {
+ if ((v->flags_ & GuiFile::FILE_SAVE) == 0 && v->chooser_) {
std::string ret;
auto file = gtk_file_chooser_get_filename(v->chooser_);
if (file) {
@@ -1079,7 +1117,8 @@ public:
label = nullptr;
}
if (value->type_ != FILE
- || (static_cast<FileValue*>(value.get())->flags_ & FILE_SAVE)) {
+ || (static_cast<FileValue*>(value.get())
+ ->flags_ & GuiFile::FILE_SAVE)) {
value->entry_ = gtk_entry_new();
gtk_entry_set_activates_default(GTK_ENTRY(value->entry_), true);
}
@@ -1104,7 +1143,7 @@ public:
}
case FILE: {
auto v = static_cast<FileValue*>(value.get());
- if ((v->flags_ & FILE_SAVE) == 0) {
+ if ((v->flags_ & GuiFile::FILE_SAVE) == 0) {
assert(!value->entry_);
value->entry_ = gtk_file_chooser_button_new(
v->label_.c_str(), GTK_FILE_CHOOSER_ACTION_OPEN);
@@ -1130,14 +1169,7 @@ public:
if (!v->value_.empty()) {
gtk_file_chooser_set_filename(v->chooser_, v->value_.c_str());
}
- for (auto const& filter : v->filter_) {
- auto f = gtk_file_filter_new();
- gtk_file_filter_set_name(f, filter.name.c_str());
- for (auto const& mask : filter.masks) {
- gtk_file_filter_add_pattern(f, mask.c_str());
- }
- gtk_file_chooser_add_filter(v->chooser_, f);
- }
+ set_filter(v->chooser_, v->filter_);
break;
}
case BOOLEAN:
@@ -1200,7 +1232,7 @@ public:
break;
case FILE: {
auto v = static_cast<FileValue*>(value.get());
- if ((v->flags_ & FILE_SAVE) == 0) {
+ if ((v->flags_ & GuiFile::FILE_SAVE) == 0) {
auto file = gtk_file_chooser_get_filename(v->chooser_);
if (file) {
v->value_ = file;
@@ -1273,13 +1305,13 @@ protected:
struct FileValue : public Value {
std::string value_;
uint8_t flags_;
- std::vector<Filter> filter_;
+ std::vector<GuiFile::Filter> filter_;
GtkFileChooser* chooser_;
GtkWidget* button_;
FileValue(GtkGuiForm* me, std::string const& id, std::string const& label,
std::string const& description, std::string const& value,
- uint8_t flags, std::vector<Filter> const& filter)
+ uint8_t flags, std::vector<GuiFile::Filter> const& filter)
: Value(me, FILE, id, label, description), value_(value),
flags_(flags), filter_(filter), chooser_(nullptr), button_(nullptr) {
}
@@ -2079,8 +2111,8 @@ GuiForm* GuiForm::create(std::string const& title,
}
// static
-uint8_t const GuiForm::FILE_OPEN = 0;
-uint8_t const GuiForm::FILE_SAVE = 1;
+uint8_t const GuiFile::FILE_OPEN = 0;
+uint8_t const GuiFile::FILE_SAVE = 1;
// static
GuiFormApply* GuiFormApply::create(std::string const& title,