Build fixes for Boost >= 1.73
[dcpomatic.git] / src / wx / content_widget.h
1 /*
2     Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/wx/content_widget.h
22  *  @brief ContentWidget class.
23  */
24
25 #ifndef DCPOMATIC_CONTENT_WIDGET_H
26 #define DCPOMATIC_CONTENT_WIDGET_H
27
28 #include "wx_util.h"
29 #include "lib/content.h"
30 #include <wx/wx.h>
31 #include <wx/gbsizer.h>
32 #include <wx/spinctrl.h>
33 #include <boost/function.hpp>
34 #include <vector>
35
36 /** @class ContentWidget
37  *  @brief A widget which represents some Content state and which can be used
38  *  when multiple pieces of content are selected.
39  *
40  *  @param S Type of ContentPart being manipulated (e.g. VideoContent)
41  *  @param T Type of the widget (e.g. wxSpinCtrl)
42  *  @param U Data type of state as used by the model.
43  *  @param V Data type of state as used by the view.
44  */
45 template <class S, class T, typename U, typename V>
46 class ContentWidget : public boost::noncopyable
47 {
48 public:
49         /** @param parent Parent window.
50          *  @param wrapped Control widget that we are wrapping.
51          *  @param property ContentProperty that the widget is handling.
52          *  @param part Part of Content that the property is in (e.g. &Content::video)
53          *  @param model_getter Function on the Content to get the value.
54          *  @param model_setter Function on the Content to set the value.
55          *  @param view_to_model Function to convert a view value to a model value.
56          *  @param model_to_view Function to convert a model value to a view value.
57          */
58         ContentWidget (
59                 wxWindow* parent,
60                 T* wrapped,
61                 int property,
62                 boost::function<boost::shared_ptr<S> (Content*)> part,
63                 boost::function<U (S*)> model_getter,
64                 boost::function<void (S*, U)> model_setter,
65                 boost::function<U (V)> view_to_model,
66                 boost::function<V (U)> model_to_view
67                 )
68                 : _wrapped (wrapped)
69                 , _sizer (0)
70                 , _button (new wxButton (parent, wxID_ANY, _("Multiple values")))
71                 , _property (property)
72                 , _part (part)
73                 , _model_getter (model_getter)
74                 , _model_setter (model_setter)
75                 , _view_to_model (view_to_model)
76                 , _model_to_view (model_to_view)
77                 , _ignore_model_changes (false)
78         {
79                 _button->SetToolTip (_("Click the button to set all selected content to the same value."));
80                 _button->Hide ();
81                 _button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentWidget::button_clicked, this));
82         }
83
84         /** @return the widget that we are wrapping */
85         T* wrapped () const
86         {
87                 return _wrapped;
88         }
89
90         typedef std::vector<boost::shared_ptr<Content> > List;
91
92         /** Set the content that this control is working on (i.e. the selected content) */
93         void set_content (List content)
94         {
95                 for (typename std::list<boost::signals2::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
96                         i->disconnect ();
97                 }
98
99                 _connections.clear ();
100
101                 _content = content;
102
103                 _wrapped->Enable (!_content.empty ());
104
105                 update_from_model ();
106
107                 for (typename List::iterator i = _content.begin(); i != _content.end(); ++i) {
108 #if BOOST_VERSION >= 106100
109                         _connections.push_back ((*i)->Change.connect (boost::bind (&ContentWidget::model_changed, this, boost::placeholders::_1, boost::placeholders::_3)));
110 #else
111                         _connections.push_back ((*i)->Change.connect (boost::bind (&ContentWidget::model_changed, this, _1, _3)));
112 #endif
113                 }
114         }
115
116         /** Add this widget to a wxGridBagSizer */
117         void add (wxGridBagSizer* sizer, wxGBPosition position, wxGBSpan span = wxDefaultSpan)
118         {
119                 _sizer = sizer;
120                 _position = position;
121                 _span = span;
122                 _sizer->Add (_wrapped, _position, _span);
123         }
124
125         /** Update the view from the model */
126         void update_from_model ()
127         {
128                 if (_content.empty ()) {
129                         set_single ();
130                         return;
131                 }
132
133                 typename List::iterator i = _content.begin ();
134                 U const v = boost::bind (_model_getter, _part(_content.front().get()).get())();
135                 while (i != _content.end() && boost::bind (_model_getter, _part(i->get()).get())() == v) {
136                         ++i;
137                 }
138
139                 if (i == _content.end ()) {
140                         set_single ();
141                         checked_set (_wrapped, _model_to_view (v));
142                 } else {
143                         set_multiple ();
144                 }
145         }
146
147         void view_changed ()
148         {
149                 _ignore_model_changes = true;
150                 for (size_t i = 0; i < _content.size(); ++i) {
151                         boost::bind (_model_setter, _part (_content[i].get()).get(), _view_to_model (wx_get (_wrapped))) ();
152                 }
153                 _ignore_model_changes = false;
154         }
155
156         void show (bool s)
157         {
158                 _wrapped->Show (s);
159         }
160
161 private:
162
163         void set_single ()
164         {
165                 if (_wrapped->IsShown() || !_sizer) {
166                         return;
167                 }
168
169                 _sizer->Detach (_button);
170                 _button->Hide ();
171                 _sizer->Add (_wrapped, _position, _span);
172                 _wrapped->Show ();
173                 _sizer->Layout ();
174         }
175
176         void set_multiple ()
177         {
178                 if (_button->IsShown() || !_sizer) {
179                         return;
180                 }
181
182                 _wrapped->Hide ();
183                 _sizer->Detach (_wrapped);
184                 _button->Show ();
185                 _sizer->Add (_button, _position, _span);
186                 _sizer->Layout ();
187         }
188
189         void button_clicked ()
190         {
191                 U const v = boost::bind (_model_getter, _part(_content.front().get()).get())();
192                 for (typename List::iterator i = _content.begin (); i != _content.end(); ++i) {
193                         boost::bind (_model_setter, _part(i->get()).get(), v) ();
194                 }
195         }
196
197         void model_changed (ChangeType type, int property)
198         {
199                 if (type == CHANGE_TYPE_DONE && property == _property && !_ignore_model_changes) {
200                         update_from_model ();
201                 }
202         }
203
204         T* _wrapped;
205         wxGridBagSizer* _sizer;
206         wxGBPosition _position;
207         wxGBSpan _span;
208         wxButton* _button;
209         List _content;
210         int _property;
211         boost::function<boost::shared_ptr<S> (Content *)> _part;
212         boost::function<U (S*)> _model_getter;
213         boost::function<void (S*, U)> _model_setter;
214         boost::function<U (V)> _view_to_model;
215         boost::function<V (U)> _model_to_view;
216         std::list<boost::signals2::connection> _connections;
217         bool _ignore_model_changes;
218 };
219
220 template <typename U, typename V>
221 V caster (U x)
222 {
223         return static_cast<V> (x);
224 }
225
226 template <class S>
227 class ContentSpinCtrl : public ContentWidget<S, wxSpinCtrl, int, int>
228 {
229 public:
230         ContentSpinCtrl (
231                 wxWindow* parent,
232                 wxSpinCtrl* wrapped,
233                 int property,
234                 boost::function<boost::shared_ptr<S> (Content *)> part,
235                 boost::function<int (S*)> getter,
236                 boost::function<void (S*, int)> setter
237                 )
238                 : ContentWidget<S, wxSpinCtrl, int, int> (
239                         parent,
240                         wrapped,
241                         property,
242                         part,
243                         getter, setter,
244                         &caster<int, int>,
245                         &caster<int, int>
246                         )
247         {
248                 wrapped->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrl, int, int>::view_changed, this));
249         }
250 };
251
252 template <class S>
253 class ContentSpinCtrlDouble : public ContentWidget<S, wxSpinCtrlDouble, double, double>
254 {
255 public:
256         ContentSpinCtrlDouble (
257                 wxWindow* parent,
258                 wxSpinCtrlDouble* wrapped,
259                 int property,
260                 boost::function<boost::shared_ptr<S> (Content *)> part,
261                 boost::function<double (S*)> getter,
262                 boost::function<void (S*, double)> setter
263                 )
264                 : ContentWidget<S, wxSpinCtrlDouble, double, double> (
265                         parent,
266                         wrapped,
267                         property,
268                         part,
269                         getter, setter,
270                         &caster<double, double>,
271                         &caster<double, double>
272                         )
273         {
274                 wrapped->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrlDouble, double, double>::view_changed, this));
275         }
276 };
277
278 template <class S, class U>
279 class ContentChoice : public ContentWidget<S, wxChoice, U, int>
280 {
281 public:
282         ContentChoice (
283                 wxWindow* parent,
284                 wxChoice* wrapped,
285                 int property,
286                 boost::function<boost::shared_ptr<S> (Content *)> part,
287                 boost::function<U (S*)> getter,
288                 boost::function<void (S*, U)> setter,
289                 boost::function<U (int)> view_to_model,
290                 boost::function<int (U)> model_to_view
291                 )
292                 : ContentWidget<S, wxChoice, U, int> (
293                         parent,
294                         wrapped,
295                         property,
296                         part,
297                         getter,
298                         setter,
299                         view_to_model,
300                         model_to_view
301                         )
302         {
303                 wrapped->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ContentWidget<S, wxChoice, U, int>::view_changed, this));
304         }
305
306 };
307
308 #endif