summaryrefslogtreecommitdiff
path: root/src/wx/multiple_widget.h
blob: 7c0b4a6142669c5b0c89bf76cff5261a4f335f07 (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
/*
    Copyright (C) 2013 Carl Hetherington <cth@carlh.net>

    This program 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.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef DCPOMATIC_MULTIPLE_WIDGET_H
#define DCPOMATIC_MULTIPLE_WIDGET_H

#include <vector>
#include <wx/wx.h>
#include <wx/gbsizer.h>
#include <boost/function.hpp>
#include "wx_util.h"

template <class T>
class MultipleWidget
{
public:
	MultipleWidget (wxWindow* parent, T* wrapped)
		: _wrapped (wrapped)
		, _sizer (0)
		, _button (new wxButton (parent, wxID_ANY, _("Multiple values")))
	{
		_button->SetToolTip (_("Click the button to set all selections to the same value"));
		_button->Hide ();
		_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&MultipleWidget::button_clicked, this));
	}

	T* wrapped () const
	{
		return _wrapped;
	}

	void add (wxGridBagSizer* sizer, wxGBPosition position)
	{
		_sizer = sizer;
		_position = position;
		_sizer->Add (_wrapped, _position);
	}

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

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

	void set_multiple ()
	{
		if (_button->IsShown ()) {
			return;
		}
		
		_wrapped->Hide ();
		_sizer->Detach (_wrapped);
		_button->Show ();
		_sizer->Add (_button, _position);
		_sizer->Layout ();
	}

	boost::signals2::signal<void (void)> SetAllSame;

private:
	void button_clicked ()
	{
		SetAllSame ();
	}
	
	T* _wrapped;
	wxGridBagSizer* _sizer;
	wxGBPosition _position;
	wxButton* _button;
};


/** Set up some MultipleWidget<SpinCtrl> using a (possibly) multiple selection of objects of type T.
 *  The value is obtained from the T objects using getter.
 */
template <class T>
void
set_multiple (std::vector<boost::shared_ptr<T> > data, MultipleWidget<wxSpinCtrl>* widget, boost::function<int (T*)> getter)
{
	if (data.empty ()) {
		widget->set_single ();
		widget->wrapped()->SetValue (0);
		return;
	}
	
	typename std::vector<boost::shared_ptr<T> >::iterator i = data.begin();
	int first = boost::bind (getter, data.front().get()) ();
	while (i != data.end() && boost::bind (getter, i->get())() == first) {
		++i;
	}

	if (i == data.end ()) {
		/* All values are the same */
		widget->set_single ();
		checked_set (widget->wrapped(), first);
	} else {
		/* At least one different value */
		widget->set_multiple ();
	}
}

#endif