diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2017-07-26 21:50:41 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2017-07-26 22:06:04 +0200 |
| commit | ba31faa55abedea506443df821e32aff93378c15 (patch) | |
| tree | ef192cd7b1c1fcc87ed8238353ea40c5f3e1dc32 /src/gui_gtk.cc | |
| parent | 629bd9dbd099af60812e80f851afb7902fc28c37 (diff) | |
Add GuiForm::Listener::changed which is called whenever a value is changed
Diffstat (limited to 'src/gui_gtk.cc')
| -rw-r--r-- | src/gui_gtk.cc | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/src/gui_gtk.cc b/src/gui_gtk.cc index 7510f09..c7031a8 100644 --- a/src/gui_gtk.cc +++ b/src/gui_gtk.cc @@ -914,12 +914,12 @@ public: void add_string(std::string const& id, std::string const& label, std::string const& value, std::string const& description) override { - values_.emplace_back(new StringValue(id, label, description, value)); + values_.emplace_back(new StringValue(this, id, label, description, value)); } void add_number(std::string const& id, std::string const& label, uint64_t value, std::string const& description) override { - values_.emplace_back(new NumberValue(id, label, description, value)); + values_.emplace_back(new NumberValue(this, id, label, description, value)); } void add_file(std::string const& id, std::string const& label, @@ -927,7 +927,7 @@ public: std::string const& description, uint8_t flags, std::vector<Filter> const& filter) override { - values_.emplace_back(new FileValue(id, label, description, value, + values_.emplace_back(new FileValue(this, id, label, description, value, flags, filter)); } @@ -1027,6 +1027,8 @@ public: case STRING: gtk_entry_set_text(GTK_ENTRY(value->entry_), static_cast<StringValue*>(value.get())->value_.c_str()); + g_signal_connect(G_OBJECT(value->entry_), "changed", + G_CALLBACK(changed), value.get()); break; case NUMBER: { char tmp[20]; @@ -1035,6 +1037,8 @@ public: gtk_entry_set_text(GTK_ENTRY(value->entry_), tmp); gtk_entry_set_input_purpose(GTK_ENTRY(value->entry_), GTK_INPUT_PURPOSE_DIGITS); + g_signal_connect(G_OBJECT(value->entry_), "changed", + G_CALLBACK(changed), value.get()); break; } case FILE: { @@ -1045,6 +1049,8 @@ public: v->label_.c_str(), GTK_FILE_CHOOSER_ACTION_OPEN); v->chooser_ = GTK_FILE_CHOOSER(button); value->entry_ = button; + g_signal_connect(G_OBJECT(v->chooser_), "file-set", + G_CALLBACK(file_set), value.get()); } else { auto chooser = gtk_file_chooser_native_new( v->label_.c_str(), @@ -1057,6 +1063,8 @@ public: v->chooser_ = GTK_FILE_CHOOSER(chooser); gtk_editable_set_editable(GTK_EDITABLE(value->entry_), false); gtk_file_chooser_set_do_overwrite_confirmation(v->chooser_, true); + g_signal_connect(G_OBJECT(value->entry_), "changed", + G_CALLBACK(changed), value.get()); } if (!v->value_.empty()) { gtk_file_chooser_set_filename(v->chooser_, v->value_.c_str()); @@ -1148,15 +1156,16 @@ protected: }; struct Value { + GtkGuiForm* me_; Type const type_; std::string const id_; std::string const label_; std::string const description_; GtkWidget* entry_; - Value(Type type, std::string const& id, std::string const& label, - std::string const& description) - : type_(type), id_(id), label_(label), description_(description), + Value(GtkGuiForm* me, Type type, std::string const& id, + std::string const& label, std::string const& description) + : me_(me), type_(type), id_(id), label_(label), description_(description), entry_(nullptr) { } }; @@ -1164,18 +1173,18 @@ protected: struct StringValue : public Value { std::string value_; - StringValue(std::string const& id, std::string const& label, + StringValue(GtkGuiForm* me, std::string const& id, std::string const& label, std::string const& description, std::string const& value) - : Value(STRING, id, label, description), value_(value) { + : Value(me, STRING, id, label, description), value_(value) { } }; struct NumberValue : public Value { uint64_t value_; - NumberValue(std::string const& id, std::string const& label, + NumberValue(GtkGuiForm* me, std::string const& id, std::string const& label, std::string const& description, uint64_t value) - : Value(NUMBER, id, label, description), value_(value) { + : Value(me, NUMBER, id, label, description), value_(value) { } }; @@ -1185,10 +1194,10 @@ protected: std::vector<Filter> filter_; GtkFileChooser* chooser_; - FileValue(std::string const& id, std::string const& label, + 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) - : Value(FILE, id, label, description), value_(value), + : Value(me, FILE, id, label, description), value_(value), flags_(flags), filter_(filter) { } }; @@ -1201,6 +1210,13 @@ protected: return true; } + void notify_changed(std::string const& id) { + auto it = observers_.notify(); + while (it.has_next()) { + it.next()->changed(this, id); + } + } + static bool get_number(GtkWidget* entry, uint64_t* number) { assert(entry && number); auto text = gtk_entry_get_text(GTK_ENTRY(entry)); @@ -1237,6 +1253,17 @@ protected: } } + static void changed(GtkEditable* UNUSED(editable), gpointer user_data) { + auto value = reinterpret_cast<Value*>(user_data); + value->me_->notify_changed(value->id_); + } + + static void file_set(GtkFileChooserButton* UNUSED(widget), + gpointer user_data) { + auto value = reinterpret_cast<Value*>(user_data); + value->me_->notify_changed(value->id_); + } + virtual gint add_extra_widgets(GtkGrid* UNUSED(grid), gint row, gint UNUSED(colspan)) { return row; |
