summaryrefslogtreecommitdiff
path: root/src/wx/content_widget.h
blob: 302436e55385f5f0e75eb4844f8b253c5791f168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
    Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


/** @file  src/wx/content_widget.h
 *  @brief ContentWidget class.
 */


#ifndef DCPOMATIC_CONTENT_WIDGET_H
#define DCPOMATIC_CONTENT_WIDGET_H


#include "wx_util.h"
#include "lib/content.h"
#include <dcp/warnings.h>
LIBDCP_DISABLE_WARNINGS
#include <wx/gbsizer.h>
#include <wx/spinctrl.h>
#include <wx/wx.h>
LIBDCP_ENABLE_WARNINGS
#include <vector>


/** @class ContentWidget
 *  @brief A widget which represents some Content state and which can be used
 *  when multiple pieces of content are selected.
 *
 *  @param S Type of ContentPart being manipulated (e.g. VideoContent)
 *  @param T Type of the widget (e.g. wxSpinCtrl)
 *  @param U Data type of state as used by the model.
 *  @param V Data type of state as used by the view.
 */
template <class S, class T, typename U, typename V>
class ContentWidget
{
public:
	/** @param parent Parent window.
	 *  @param wrapped Control widget that we are wrapping.
	 *  @param property ContentProperty that the widget is handling.
	 *  @param part Part of Content that the property is in (e.g. &Content::video)
	 *  @param model_getter Function on the Content to get the value.
	 *  @param model_setter Function on the Content to set the value.
	 *  @param view_changed Function called when the view has changed; useful for linking controls.
	 *  @param view_to_model Function to convert a view value to a model value.
	 *  @param model_to_view Function to convert a model value to a view value.
	 */
	ContentWidget (
		wxWindow* parent,
		T* wrapped,
		int property,
		std::function<std::shared_ptr<S> (Content*)> part,
		std::function<U (S*)> model_getter,
		std::function<void (S*, U)> model_setter,
		std::function<void ()> view_changed,
		std::function<U (V)> view_to_model,
		std::function<V (U)> model_to_view
		)
		: _wrapped (wrapped)
		, _sizer (0)
		, _button (new wxButton (parent, wxID_ANY, _("Multiple values")))
		, _property (property)
		, _part(std::move(part))
		, _model_getter(std::move(model_getter))
		, _model_setter(std::move(model_setter))
		, _view_changed(std::move(view_changed))
		, _view_to_model(std::move(view_to_model))
		, _model_to_view(std::move(model_to_view))
		, _ignore_model_changes (false)
	{
		_button->SetToolTip (_("Click the button to set all selected content to the same value."));
		_button->Hide ();
		_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ContentWidget::button_clicked, this));
	}

	ContentWidget (ContentWidget const&) = delete;
	ContentWidget& operator= (ContentWidget const&) = delete;

	/** @return the widget that we are wrapping */
	T* wrapped () const
	{
		return _wrapped;
	}

	typedef std::vector<std::shared_ptr<Content> > List;

	/** Set the content that this control is working on (i.e. the selected content) */
	void set_content (List content)
	{
		for (typename std::list<boost::signals2::connection>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
			i->disconnect ();
		}

		_connections.clear ();

		_content = content;

		_wrapped->Enable (!_content.empty ());

		update_from_model ();

		for (typename List::iterator i = _content.begin(); i != _content.end(); ++i) {
#if BOOST_VERSION >= 106100
			_connections.push_back ((*i)->Change.connect (boost::bind (&ContentWidget::model_changed, this, boost::placeholders::_1, boost::placeholders::_3)));
#else
			_connections.push_back ((*i)->Change.connect (boost::bind (&ContentWidget::model_changed, this, _1, _3)));
#endif
		}
	}

	/** Add this widget to a wxGridBagSizer */
	void add (wxGridBagSizer* sizer, wxGBPosition position, wxGBSpan span = wxDefaultSpan, int flag = 0)
	{
		_sizer = sizer;
		_position = position;
		_span = span;
		_sizer->Add (_wrapped, _position, _span, flag);
	}

	/** Update the view from the model */
	void update_from_model ()
	{
		if (_content.empty ()) {
			set_single ();
			return;
		}

		typename List::iterator i = _content.begin ();
		U const v = boost::bind (_model_getter, _part(_content.front().get()).get())();
		while (i != _content.end() && boost::bind (_model_getter, _part(i->get()).get())() == v) {
			++i;
		}

		if (i == _content.end ()) {
			set_single ();
			checked_set (_wrapped, _model_to_view (v));
		} else {
			set_multiple ();
		}
	}

	void view_changed ()
	{
		_ignore_model_changes = true;
		for (size_t i = 0; i < _content.size(); ++i) {
			boost::bind (_model_setter, _part (_content[i].get()).get(), _view_to_model (wx_get (_wrapped))) ();
		}
		if (_view_changed) {
			_view_changed ();
		}
		_ignore_model_changes = false;
	}

	void show (bool s)
	{
		_wrapped->Show (s);
	}

private:

	void set_single ()
	{
		if (_wrapped->IsShown() || !_sizer) {
			return;
		}

		_sizer->Detach (_button);
		_button->Hide ();
		_sizer->Add (_wrapped, _position, _span);
		_wrapped->Show ();
		_sizer->Layout ();
	}

	void set_multiple ()
	{
		if (_button->IsShown() || !_sizer) {
			return;
		}

		_wrapped->Hide ();
		_sizer->Detach (_wrapped);
		_button->Show ();
		_sizer->Add (_button, _position, _span);
		_sizer->Layout ();
	}

	void button_clicked ()
	{
		U const v = boost::bind (_model_getter, _part(_content.front().get()).get())();
		for (auto const& i: _content) {
			boost::bind (_model_setter, _part(i.get()).get(), v)();
		}
	}

	void model_changed (ChangeType type, int property)
	{
		if (type == ChangeType::DONE && property == _property && !_ignore_model_changes) {
			update_from_model ();
		}
	}

	T* _wrapped;
	wxGridBagSizer* _sizer;
	wxGBPosition _position;
	wxGBSpan _span;
	wxButton* _button;
	List _content;
	int _property;
	std::function<std::shared_ptr<S> (Content *)> _part;
	std::function<U (S*)> _model_getter;
	std::function<void (S*, U)> _model_setter;
	std::function<void ()> _view_changed;
	std::function<U (V)> _view_to_model;
	std::function<V (U)> _model_to_view;
	std::list<boost::signals2::connection> _connections;
	bool _ignore_model_changes;
};

template <typename U, typename V>
V caster (U x)
{
	return static_cast<V> (x);
}

template <class S>
class ContentSpinCtrl : public ContentWidget<S, wxSpinCtrl, int, int>
{
public:
	ContentSpinCtrl (
		wxWindow* parent,
		wxSpinCtrl* wrapped,
		int property,
		std::function<std::shared_ptr<S> (Content *)> part,
		std::function<int (S*)> getter,
		std::function<void (S*, int)> setter,
		std::function<void ()> view_changed = std::function<void ()>()
		)
		: ContentWidget<S, wxSpinCtrl, int, int> (
			parent,
			wrapped,
			property,
			part,
			getter, setter,
			view_changed,
			&caster<int, int>,
			&caster<int, int>
			)
	{
		wrapped->Bind (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrl, int, int>::view_changed, this));
	}
};

template <class S>
class ContentSpinCtrlDouble : public ContentWidget<S, wxSpinCtrlDouble, double, double>
{
public:
	ContentSpinCtrlDouble (
		wxWindow* parent,
		wxSpinCtrlDouble* wrapped,
		int property,
		std::function<std::shared_ptr<S> (Content *)> part,
		std::function<double (S*)> getter,
		std::function<void (S*, double)> setter,
		std::function<void ()> view_changed = std::function<void ()>()
		)
		: ContentWidget<S, wxSpinCtrlDouble, double, double> (
			parent,
			wrapped,
			property,
			part,
			getter, setter,
			view_changed,
			&caster<double, double>,
			&caster<double, double>
			)
	{
		wrapped->Bind (wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, boost::bind (&ContentWidget<S, wxSpinCtrlDouble, double, double>::view_changed, this));
	}
};

template <class S, class U>
class ContentChoice : public ContentWidget<S, wxChoice, U, int>
{
public:
	ContentChoice (
		wxWindow* parent,
		wxChoice* wrapped,
		int property,
		std::function<std::shared_ptr<S> (Content *)> part,
		std::function<U (S*)> getter,
		std::function<void (S*, U)> setter,
		std::function<U (int)> view_to_model,
		std::function<int (U)> model_to_view,
		std::function<void ()> view_changed = std::function<void()>()
		)
		: ContentWidget<S, wxChoice, U, int> (
			parent,
			wrapped,
			property,
			part,
			getter,
			setter,
			view_changed,
			view_to_model,
			model_to_view
			)
	{
		wrapped->Bind (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&ContentWidget<S, wxChoice, U, int>::view_changed, this));
	}

};

#endif