2 Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/wx/content_widget.h
22 * @brief ContentWidget class.
25 #ifndef DCPOMATIC_CONTENT_WIDGET_H
26 #define DCPOMATIC_CONTENT_WIDGET_H
29 #include "lib/content.h"
31 #include <wx/gbsizer.h>
32 #include <wx/spinctrl.h>
33 #include <boost/function.hpp>
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.
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.
45 template <class S, class T, typename U, typename V>
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.
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
72 , _button (new wxButton (parent, wxID_ANY, _("Multiple values")))
73 , _property (property)
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)
82 _button->SetToolTip (_("Click the button to set all selected content to the same value."));
84 _button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentWidget::button_clicked, this));
87 ContentWidget (ContentWidget const&) = delete;
88 ContentWidget& operator= (ContentWidget const&) = delete;
90 /** @return the widget that we are wrapping */
96 typedef std::vector<std::shared_ptr<Content> > List;
98 /** Set the content that this control is working on (i.e. the selected content) */
99 void set_content (List content)
101 for (typename std::list<boost::signals2::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
105 _connections.clear ();
109 _wrapped->Enable (!_content.empty ());
111 update_from_model ();
113 for (typename List::iterator i = _content.begin(); i != _content.end(); ++i) {
114 #if BOOST_VERSION >= 106100
115 _connections.push_back ((*i)->Change.connect (boost::bind (&ContentWidget::model_changed, this, boost::placeholders::_1, boost::placeholders::_3)));
117 _connections.push_back ((*i)->Change.connect (boost::bind (&ContentWidget::model_changed, this, _1, _3)));
122 /** Add this widget to a wxGridBagSizer */
123 void add (wxGridBagSizer* sizer, wxGBPosition position, wxGBSpan span = wxDefaultSpan, int flag = 0)
126 _position = position;
128 _sizer->Add (_wrapped, _position, _span, flag);
131 /** Update the view from the model */
132 void update_from_model ()
134 if (_content.empty ()) {
139 typename List::iterator i = _content.begin ();
140 U const v = boost::bind (_model_getter, _part(_content.front().get()).get())();
141 while (i != _content.end() && boost::bind (_model_getter, _part(i->get()).get())() == v) {
145 if (i == _content.end ()) {
147 checked_set (_wrapped, _model_to_view (v));
155 _ignore_model_changes = true;
156 for (size_t i = 0; i < _content.size(); ++i) {
157 boost::bind (_model_setter, _part (_content[i].get()).get(), _view_to_model (wx_get (_wrapped))) ();
162 _ignore_model_changes = false;
174 if (_wrapped->IsShown() || !_sizer) {
178 _sizer->Detach (_button);
180 _sizer->Add (_wrapped, _position, _span);
187 if (_button->IsShown() || !_sizer) {
192 _sizer->Detach (_wrapped);
194 _sizer->Add (_button, _position, _span);
198 void button_clicked ()
200 U const v = boost::bind (_model_getter, _part(_content.front().get()).get())();
201 for (auto const& i: _content) {
202 boost::bind (_model_setter, _part(i.get()).get(), v)();
206 void model_changed (ChangeType type, int property)
208 if (type == ChangeType::DONE && property == _property && !_ignore_model_changes) {
209 update_from_model ();
214 wxGridBagSizer* _sizer;
215 wxGBPosition _position;
220 boost::function<std::shared_ptr<S> (Content *)> _part;
221 boost::function<U (S*)> _model_getter;
222 boost::function<void (S*, U)> _model_setter;
223 boost::function<void ()> _view_changed;
224 boost::function<U (V)> _view_to_model;
225 boost::function<V (U)> _model_to_view;
226 std::list<boost::signals2::connection> _connections;
227 bool _ignore_model_changes;
230 template <typename U, typename V>
233 return static_cast<V> (x);
237 class ContentSpinCtrl : public ContentWidget<S, wxSpinCtrl, int, int>
244 boost::function<std::shared_ptr<S> (Content *)> part,
245 boost::function<int (S*)> getter,
246 boost::function<void (S*, int)> setter,
247 boost::function<void ()> view_changed = boost::function<void ()>()
249 : ContentWidget<S, wxSpinCtrl, int, int> (
260 wrapped->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrl, int, int>::view_changed, this));
265 class ContentSpinCtrlDouble : public ContentWidget<S, wxSpinCtrlDouble, double, double>
268 ContentSpinCtrlDouble (
270 wxSpinCtrlDouble* wrapped,
272 boost::function<std::shared_ptr<S> (Content *)> part,
273 boost::function<double (S*)> getter,
274 boost::function<void (S*, double)> setter,
275 boost::function<void ()> view_changed = boost::function<void ()>()
277 : ContentWidget<S, wxSpinCtrlDouble, double, double> (
284 &caster<double, double>,
285 &caster<double, double>
288 wrapped->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrlDouble, double, double>::view_changed, this));
292 template <class S, class U>
293 class ContentChoice : public ContentWidget<S, wxChoice, U, int>
300 boost::function<std::shared_ptr<S> (Content *)> part,
301 boost::function<U (S*)> getter,
302 boost::function<void (S*, U)> setter,
303 boost::function<U (int)> view_to_model,
304 boost::function<int (U)> model_to_view,
305 boost::function<void ()> view_changed = boost::function<void()>()
307 : ContentWidget<S, wxChoice, U, int> (
319 wrapped->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ContentWidget<S, wxChoice, U, int>::view_changed, this));