2 Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef DCPOMATIC_MULTIPLE_WIDGET_H
21 #define DCPOMATIC_MULTIPLE_WIDGET_H
25 #include <wx/gbsizer.h>
26 #include <boost/function.hpp>
33 MultipleWidget (wxWindow* parent, T* wrapped)
36 , _button (new wxButton (parent, wxID_ANY, _("Multiple values")))
38 _button->SetToolTip (_("Click the button to set all selections to the same value"));
40 _button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&MultipleWidget::button_clicked, this));
48 void add (wxGridBagSizer* sizer, wxGBPosition position)
52 _sizer->Add (_wrapped, _position);
57 if (_wrapped->IsShown ()) {
61 _sizer->Detach (_button);
63 _sizer->Add (_wrapped, _position);
70 if (_button->IsShown ()) {
75 _sizer->Detach (_wrapped);
77 _sizer->Add (_button, _position);
81 boost::signals2::signal<void (void)> SetAllSame;
84 void button_clicked ()
90 wxGridBagSizer* _sizer;
91 wxGBPosition _position;
96 /** Set up some MultipleWidget<SpinCtrl> using a (possibly) multiple selection of objects of type T.
97 * The value is obtained from the T objects using getter.
101 set_multiple (std::vector<boost::shared_ptr<T> > data, MultipleWidget<wxSpinCtrl>* widget, boost::function<int (T*)> getter)
104 widget->set_single ();
105 widget->wrapped()->SetValue (0);
109 typename std::vector<boost::shared_ptr<T> >::iterator i = data.begin();
110 int first = boost::bind (getter, data.front().get()) ();
111 while (i != data.end() && boost::bind (getter, i->get())() == first) {
115 if (i == data.end ()) {
116 /* All values are the same */
117 widget->set_single ();
118 checked_set (widget->wrapped(), first);
120 /* At least one different value */
121 widget->set_multiple ();