diff --git a/src/data/field.cpp b/src/data/field.cpp index 7665358d..c1a41791 100644 --- a/src/data/field.cpp +++ b/src/data/field.cpp @@ -17,6 +17,8 @@ #include #include +DECLARE_TYPEOF_COLLECTION(StyleListener*); + // ----------------------------------------------------------------------------- : Field Field::Field() @@ -120,6 +122,35 @@ void Style::initDependencies(Context& ctx, const Dependency& dep) const { // visible.initDependencies(ctx,dep); } +// ----------------------------------------------------------------------------- : StyleListener + +void Style::addListener(StyleListener* listener) { + listeners.push_back(listener); +} +void Style::removeListener(StyleListener* listener) { + listeners.erase( + std::remove( + listeners.begin(), + listeners.end(), + listener + ), + listeners.end() + ); +} +void Style::tellListeners() { + FOR_EACH(l, listeners) l->onStyleChange(); +} + +StyleListener::StyleListener(const StyleP& style) + : styleP(style) +{ + style->addListener(this); +} +StyleListener::~StyleListener() { + styleP->removeListener(this); +} + + // ----------------------------------------------------------------------------- : Value Value::~Value() {} diff --git a/src/data/field.hpp b/src/data/field.hpp index c937d3cf..096a1049 100644 --- a/src/data/field.hpp +++ b/src/data/field.hpp @@ -22,6 +22,7 @@ DECLARE_POINTER_TYPE(Value); class Context; class Dependency; class Action; +class StyleListener; // for DataViewer/editor class DataViewer; class DataEditor; @@ -103,16 +104,26 @@ class Style { /** thisP is a smart pointer to this */ virtual ValueViewerP makeEditor(DataEditor& parent, const StyleP& thisP) = 0; - /// Update scripted values of this style, return true if anything has changed + /// Update scripted values of this style, return true if anything has changed. + /** The caller should tellListeners() */ virtual bool update(Context&); /// Add the given dependency to the dependent_scripts list for the variables this style depends on /** Only use for things that need invalidate() */ virtual void initDependencies(Context&, const Dependency&) const; /// Invalidate scripted images for this style - virtual void invalidate() {} + virtual void invalidate(Context&) {} + + /// Add a StyleListener + void addListener(StyleListener*); + /// Remove a StyleListener + void removeListener(StyleListener*); + /// Tell the StyleListeners that this style has changed + void tellListeners(); private: DECLARE_REFLECTION_VIRTUAL(); + /// Things that are listening to changes in this style + vector listeners; }; void init_object(const FieldP&, StyleP&); @@ -120,6 +131,20 @@ inline const FieldP& get_key (const StyleP& s) { return s->fieldP; } inline const String& get_key_name(const StyleP& s) { return s->fieldP->name; } template <> StyleP read_new