2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef DCPOMATIC_MULTIPLE_WIDGET_H
21 #define DCPOMATIC_MULTIPLE_WIDGET_H
25 #include <wx/gbsizer.h>
26 #include <boost/function.hpp>
29 /** A widget which represents some Content state and which can be used
30 * when multiple pieces of content are selected.
32 * @param S Type containing the content being represented (e.g. VideoContent)
33 * @param T Type of the widget (e.g. wxSpinCtrl)
34 * @param U Data type of state as used by the model.
35 * @param V Data type of state as used by the view.
37 template <class S, class T, typename U, typename V>
41 /** @param parent Parent window.
42 * @param wrapped Control widget that we are wrapping.
43 * @param property ContentProperty that the widget is handling.
44 * @param model_getter Function on the Content to get the value.
45 * @param model_setter Function on the Content to set the value.
51 boost::function<U (S*)> model_getter,
52 boost::function<void (S*, U)> model_setter,
53 boost::function<U (V)> view_to_model,
54 boost::function<V (U)> model_to_view
58 , _button (new wxButton (parent, wxID_ANY, _("Multiple values")))
59 , _property (property)
60 , _model_getter (model_getter)
61 , _model_setter (model_setter)
62 , _view_to_model (view_to_model)
63 , _model_to_view (model_to_view)
64 , _ignore_model_changes (false)
66 _button->SetToolTip (_("Click the button to set all selected content to the same value."));
68 _button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentWidget::button_clicked, this));
71 /** @return the widget that we are wrapping */
77 typedef std::vector<boost::shared_ptr<S> > List;
79 /** Set the content that this control is working on (i.e. the selected content) */
80 void set_content (List content)
82 for (typename std::list<boost::signals2::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
86 _connections.clear ();
90 _wrapped->Enable (!_content.empty ());
94 for (typename List::iterator i = _content.begin(); i != _content.end(); ++i) {
95 _connections.push_back ((*i)->Changed.connect (boost::bind (&ContentWidget::model_changed, this, _2)));
99 /** Add this widget to a wxGridBagSizer */
100 void add (wxGridBagSizer* sizer, wxGBPosition position)
103 _position = position;
104 _sizer->Add (_wrapped, _position);
107 /** Update the view from the model */
108 void update_from_model ()
110 if (_content.empty ()) {
115 typename List::iterator i = _content.begin ();
116 U const v = boost::bind (_model_getter, _content.front().get())();
117 while (i != _content.end() && boost::bind (_model_getter, i->get())() == v) {
121 if (i == _content.end ()) {
123 checked_set (_wrapped, _model_to_view (v));
131 _ignore_model_changes = true;
132 for (size_t i = 0; i < _content.size(); ++i) {
133 boost::bind (_model_setter, _content[i].get(), _view_to_model (wx_get (_wrapped))) ();
135 _ignore_model_changes = false;
142 if (_wrapped->IsShown ()) {
146 _sizer->Detach (_button);
148 _sizer->Add (_wrapped, _position);
155 if (_button->IsShown ()) {
160 _sizer->Detach (_wrapped);
162 _sizer->Add (_button, _position);
166 void button_clicked ()
168 U const v = boost::bind (_model_getter, _content.front().get())();
169 for (typename List::iterator i = _content.begin (); i != _content.end(); ++i) {
170 boost::bind (_model_setter, i->get(), v) ();
174 void model_changed (int property)
176 if (property == _property && !_ignore_model_changes) {
177 update_from_model ();
182 wxGridBagSizer* _sizer;
183 wxGBPosition _position;
187 boost::function<U (S*)> _model_getter;
188 boost::function<void (S*, U)> _model_setter;
189 boost::function<U (V)> _view_to_model;
190 boost::function<V (U)> _model_to_view;
191 std::list<boost::signals2::connection> _connections;
192 bool _ignore_model_changes;
195 template <typename U, typename V>
198 return static_cast<V> (x);
202 class ContentSpinCtrl : public ContentWidget<S, wxSpinCtrl, int, int>
209 boost::function<int (S*)> getter,
210 boost::function<void (S*, int)> setter
212 : ContentWidget<S, wxSpinCtrl, int, int> (
221 wrapped->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrl, int, int>::view_changed, this));
225 template <class S, class U>
226 class ContentChoice : public ContentWidget<S, wxChoice, U, int>
233 boost::function<U (S*)> getter,
234 boost::function<void (S*, U)> setter,
235 boost::function<U (int)> view_to_model,
236 boost::function<int (U)> model_to_view
238 : ContentWidget<S, wxChoice, U, int> (
248 wrapped->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ContentWidget<S, wxChoice, U, int>::view_changed, this));