X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fwx%2Fcontent_widget.h;h=30501c1a95f513700289e31fe183874e473ade80;hb=2e504b33eb9f38cac629ad31b7c107fb0cf5efda;hp=07abc7cbdf8d1de81140b691d03d112be4534689;hpb=1415214ff18e05e5b609aab8c4efb89e5b9e0cc0;p=dcpomatic.git diff --git a/src/wx/content_widget.h b/src/wx/content_widget.h index 07abc7cbd..30501c1a9 100644 --- a/src/wx/content_widget.h +++ b/src/wx/content_widget.h @@ -50,7 +50,8 @@ public: int property, boost::function model_getter, boost::function model_setter, - boost::function view_getter + boost::function view_to_model, + boost::function model_to_view ) : _wrapped (wrapped) , _sizer (0) @@ -58,7 +59,8 @@ public: , _property (property) , _model_getter (model_getter) , _model_setter (model_setter) - , _view_getter (view_getter) + , _view_to_model (view_to_model) + , _model_to_view (model_to_view) , _ignore_model_changes (false) { _button->SetToolTip (_("Click the button to set all selected content to the same value.")); @@ -66,12 +68,15 @@ public: _button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentWidget::button_clicked, this)); } - T* wrapped () const { + /** @return the widget that we are wrapping */ + T* wrapped () const + { return _wrapped; } typedef std::vector > List; + /** Set the content that this control is working on (i.e. the selected content) */ void set_content (List content) { for (typename std::list::iterator i = _connections.begin(); i != _connections.end(); ++i) { @@ -91,6 +96,7 @@ public: } } + /** Add this widget to a wxGridBagSizer */ void add (wxGridBagSizer* sizer, wxGBPosition position) { _sizer = sizer; @@ -98,6 +104,7 @@ public: _sizer->Add (_wrapped, _position); } + /** Update the view from the model */ void update_from_model () { if (_content.empty ()) { @@ -113,7 +120,7 @@ public: if (i == _content.end ()) { set_single (); - checked_set (_wrapped, v); + checked_set (_wrapped, _model_to_view (v)); } else { set_multiple (); } @@ -121,11 +128,11 @@ public: void view_changed () { + _ignore_model_changes = true; for (size_t i = 0; i < _content.size(); ++i) { - /* Only update our view on the last time round this loop */ - _ignore_model_changes = i < (_content.size() - 1); - boost::bind (_model_setter, _content[i].get(), static_cast (boost::bind (_view_getter, _wrapped)()))(); + boost::bind (_model_setter, _content[i].get(), _view_to_model (wx_get (_wrapped))) (); } + _ignore_model_changes = false; } private: @@ -179,29 +186,64 @@ private: int _property; boost::function _model_getter; boost::function _model_setter; - boost::function _view_getter; + boost::function _view_to_model; + boost::function _model_to_view; std::list _connections; bool _ignore_model_changes; }; +template +V caster (U x) +{ + return static_cast (x); +} + template class ContentSpinCtrl : public ContentWidget { public: - ContentSpinCtrl (wxWindow* parent, wxSpinCtrl* wrapped, int property, boost::function getter, boost::function setter) - : ContentWidget (parent, wrapped, property, getter, setter, boost::mem_fn (&wxSpinCtrl::GetValue)) + ContentSpinCtrl ( + wxWindow* parent, + wxSpinCtrl* wrapped, + int property, + boost::function getter, + boost::function setter + ) + : ContentWidget ( + parent, + wrapped, + property, + getter, setter, + &caster, + &caster + ) { wrapped->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ContentWidget::view_changed, this)); } - }; template class ContentChoice : public ContentWidget { public: - ContentChoice (wxWindow* parent, wxChoice* wrapped, int property, boost::function getter, boost::function setter) - : ContentWidget (parent, wrapped, property, getter, setter, boost::mem_fn (&wxChoice::GetSelection)) + ContentChoice ( + wxWindow* parent, + wxChoice* wrapped, + int property, + boost::function getter, + boost::function setter, + boost::function view_to_model, + boost::function model_to_view + ) + : ContentWidget ( + parent, + wrapped, + property, + getter, + setter, + view_to_model, + model_to_view + ) { wrapped->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ContentWidget::view_changed, this)); }