std::shared_ptr
[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_changed Function called when the view has changed; useful for linking controls.
56          *  @param view_to_model Function to convert a view value to a model value.
57          *  @param model_to_view Function to convert a model value to a view value.
58          */
59         ContentWidget (
60                 wxWindow* parent,
61                 T* wrapped,
62                 int property,
63                 boost::function<std::shared_ptr<S> (Content*)> part,
64                 boost::function<U (S*)> model_getter,
65                 boost::function<void (S*, U)> model_setter,
66                 boost::function<void ()> view_changed,
67                 boost::function<U (V)> view_to_model,
68                 boost::function<V (U)> model_to_view
69                 )
70                 : _wrapped (wrapped)
71                 , _sizer (0)
72                 , _button (new wxButton (parent, wxID_ANY, _("Multiple values")))
73                 , _property (property)
74                 , _part (part)
75                 , _model_getter (model_getter)
76                 , _model_setter (model_setter)
77                 , _view_changed (view_changed)
78                 , _view_to_model (view_to_model)
79                 , _model_to_view (model_to_view)
80                 , _ignore_model_changes (false)
81         {
82                 _button->SetToolTip (_("Click the button to set all selected content to the same value."));
83                 _button->Hide ();
84                 _button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentWidget::button_clicked, this));
85         }
86
87         /** @return the widget that we are wrapping */
88         T* wrapped () const
89         {
90                 return _wrapped;
91         }
92
93         typedef std::vector<std::shared_ptr<Content> > List;
94
95         /** Set the content that this control is working on (i.e. the selected content) */
96         void set_content (List content)
97         {
98                 for (typename std::list<boost::signals2::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
99                         i->disconnect ();
100                 }
101
102                 _connections.clear ();
103
104                 _content = content;
105
106                 _wrapped->Enable (!_content.empty ());
107
108                 update_from_model ();
109
110                 for (typename List::iterator i = _content.begin(); i != _content.end(); ++i) {
111 #if BOOST_VERSION >= 106100
112                         _connections.push_back ((*i)->Change.connect (boost::bind (&ContentWidget::model_changed, this, boost::placeholders::_1, boost::placeholders::_3)));
113 #else
114                         _connections.push_back ((*i)->Change.connect (boost::bind (&ContentWidget::model_changed, this, _1, _3)));
115 #endif
116                 }
117         }
118
119         /** Add this widget to a wxGridBagSizer */
120         void add (wxGridBagSizer* sizer, wxGBPosition position, wxGBSpan span = wxDefaultSpan, int flag = 0)
121         {
122                 _sizer = sizer;
123                 _position = position;
124                 _span = span;
125                 _sizer->Add (_wrapped, _position, _span, flag);
126         }
127
128         /** Update the view from the model */
129         void update_from_model ()
130         {
131                 if (_content.empty ()) {
132                         set_single ();
133                         return;
134                 }
135
136                 typename List::iterator i = _content.begin ();
137                 U const v = boost::bind (_model_getter, _part(_content.front().get()).get())();
138                 while (i != _content.end() && boost::bind (_model_getter, _part(i->get()).get())() == v) {
139                         ++i;
140                 }
141
142                 if (i == _content.end ()) {
143                         set_single ();
144                         checked_set (_wrapped, _model_to_view (v));
145                 } else {
146                         set_multiple ();
147                 }
148         }
149
150         void view_changed ()
151         {
152                 _ignore_model_changes = true;
153                 for (size_t i = 0; i < _content.size(); ++i) {
154                         boost::bind (_model_setter, _part (_content[i].get()).get(), _view_to_model (wx_get (_wrapped))) ();
155                 }
156                 if (_view_changed) {
157                         _view_changed ();
158                 }
159                 _ignore_model_changes = false;
160         }
161
162         void show (bool s)
163         {
164                 _wrapped->Show (s);
165         }
166
167 private:
168
169         void set_single ()
170         {
171                 if (_wrapped->IsShown() || !_sizer) {
172                         return;
173                 }
174
175                 _sizer->Detach (_button);
176                 _button->Hide ();
177                 _sizer->Add (_wrapped, _position, _span);
178                 _wrapped->Show ();
179                 _sizer->Layout ();
180         }
181
182         void set_multiple ()
183         {
184                 if (_button->IsShown() || !_sizer) {
185                         return;
186                 }
187
188                 _wrapped->Hide ();
189                 _sizer->Detach (_wrapped);
190                 _button->Show ();
191                 _sizer->Add (_button, _position, _span);
192                 _sizer->Layout ();
193         }
194
195         void button_clicked ()
196         {
197                 U const v = boost::bind (_model_getter, _part(_content.front().get()).get())();
198                 for (typename List::iterator i = _content.begin (); i != _content.end(); ++i) {
199                         boost::bind (_model_setter, _part(i->get()).get(), v) ();
200                 }
201         }
202
203         void model_changed (ChangeType type, int property)
204         {
205                 if (type == CHANGE_TYPE_DONE && property == _property && !_ignore_model_changes) {
206                         update_from_model ();
207                 }
208         }
209
210         T* _wrapped;
211         wxGridBagSizer* _sizer;
212         wxGBPosition _position;
213         wxGBSpan _span;
214         wxButton* _button;
215         List _content;
216         int _property;
217         boost::function<std::shared_ptr<S> (Content *)> _part;
218         boost::function<U (S*)> _model_getter;
219         boost::function<void (S*, U)> _model_setter;
220         boost::function<void ()> _view_changed;
221         boost::function<U (V)> _view_to_model;
222         boost::function<V (U)> _model_to_view;
223         std::list<boost::signals2::connection> _connections;
224         bool _ignore_model_changes;
225 };
226
227 template <typename U, typename V>
228 V caster (U x)
229 {
230         return static_cast<V> (x);
231 }
232
233 template <class S>
234 class ContentSpinCtrl : public ContentWidget<S, wxSpinCtrl, int, int>
235 {
236 public:
237         ContentSpinCtrl (
238                 wxWindow* parent,
239                 wxSpinCtrl* wrapped,
240                 int property,
241                 boost::function<std::shared_ptr<S> (Content *)> part,
242                 boost::function<int (S*)> getter,
243                 boost::function<void (S*, int)> setter,
244                 boost::function<void ()> view_changed = boost::function<void ()>()
245                 )
246                 : ContentWidget<S, wxSpinCtrl, int, int> (
247                         parent,
248                         wrapped,
249                         property,
250                         part,
251                         getter, setter,
252                         view_changed,
253                         &caster<int, int>,
254                         &caster<int, int>
255                         )
256         {
257                 wrapped->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrl, int, int>::view_changed, this));
258         }
259 };
260
261 template <class S>
262 class ContentSpinCtrlDouble : public ContentWidget<S, wxSpinCtrlDouble, double, double>
263 {
264 public:
265         ContentSpinCtrlDouble (
266                 wxWindow* parent,
267                 wxSpinCtrlDouble* wrapped,
268                 int property,
269                 boost::function<std::shared_ptr<S> (Content *)> part,
270                 boost::function<double (S*)> getter,
271                 boost::function<void (S*, double)> setter,
272                 boost::function<void ()> view_changed = boost::function<void ()>()
273                 )
274                 : ContentWidget<S, wxSpinCtrlDouble, double, double> (
275                         parent,
276                         wrapped,
277                         property,
278                         part,
279                         getter, setter,
280                         view_changed,
281                         &caster<double, double>,
282                         &caster<double, double>
283                         )
284         {
285                 wrapped->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrlDouble, double, double>::view_changed, this));
286         }
287 };
288
289 template <class S, class U>
290 class ContentChoice : public ContentWidget<S, wxChoice, U, int>
291 {
292 public:
293         ContentChoice (
294                 wxWindow* parent,
295                 wxChoice* wrapped,
296                 int property,
297                 boost::function<std::shared_ptr<S> (Content *)> part,
298                 boost::function<U (S*)> getter,
299                 boost::function<void (S*, U)> setter,
300                 boost::function<U (int)> view_to_model,
301                 boost::function<int (U)> model_to_view,
302                 boost::function<void ()> view_changed = boost::function<void()>()
303                 )
304                 : ContentWidget<S, wxChoice, U, int> (
305                         parent,
306                         wrapped,
307                         property,
308                         part,
309                         getter,
310                         setter,
311                         view_changed,
312                         view_to_model,
313                         model_to_view
314                         )
315         {
316                 wrapped->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ContentWidget<S, wxChoice, U, int>::view_changed, this));
317         }
318
319 };
320
321 #endif