2 Copyright (C) 2012-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 #ifndef DCPOMATIC_EDITABLE_LIST_H
22 #define DCPOMATIC_EDITABLE_LIST_H
25 #include "dcpomatic_button.h"
27 #include <wx/listctrl.h>
28 #include <boost/function.hpp>
31 class EditableListColumn
34 EditableListColumn (wxString name_)
39 EditableListColumn (wxString name_, boost::optional<int> width_, bool growable_)
42 , growable (growable_)
46 boost::optional<int> width;
50 /** @param T type of things being edited.
51 * @param S dialog to edit a thing.
52 * @param get Function to get a std::vector of the things being edited.
53 * @param set Function set the things from a a std::vector.
54 * @param column Function to get the display string for a given column in a given item.
56 template<class T, class S>
57 class EditableList : public wxPanel
62 std::vector<EditableListColumn> columns,
63 boost::function<std::vector<T> ()> get,
64 boost::function<void (std::vector<T>)> set,
65 boost::function<std::string (T, int)> column,
75 , _default_width (200)
77 _sizer = new wxBoxSizer (wxHORIZONTAL);
80 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
82 style |= wxLC_NO_HEADER;
86 for (auto i: _columns) {
87 total_width += i.width.get_value_or (_default_width);
91 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
92 * window, so put a border in to help.
94 wxPanel* border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
95 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
96 wxBoxSizer* border_sizer = new wxBoxSizer (wxHORIZONTAL);
97 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
98 border->SetSizer (border_sizer);
100 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
104 for (auto i: _columns) {
108 _list->InsertColumn (j, ip);
113 _sizer->Add (border, 1, wxEXPAND);
115 _sizer->Add (_list, 1, wxEXPAND);
119 wxSizer* s = new wxBoxSizer (wxVERTICAL);
120 _add = new Button (this, _("Add..."));
121 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
123 _edit = new Button (this, _("Edit..."));
124 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
126 _remove = new Button (this, _("Remove"));
127 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
128 _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
131 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
133 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
135 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
137 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
138 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
139 #if BOOST_VERSION >= 106100
140 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
142 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
146 selection_changed ();
151 _list->DeleteAllItems ();
153 std::vector<T> current = _get ();
154 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
159 boost::optional<T> selection () const
161 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
163 return boost::optional<T> ();
166 std::vector<T> all = _get ();
167 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
176 boost::signals2::signal<void ()> SelectionChanged;
180 void add_to_control (T item)
182 wxListItem list_item;
183 int const n = _list->GetItemCount ();
185 _list->InsertItem (list_item);
187 for (size_t i = 0; i < _columns.size(); ++i) {
188 _list->SetItem (n, i, std_to_wx (_column (item, i)));
192 void selection_changed ()
194 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
196 _edit->Enable (i >= 0);
198 _remove->Enable (i >= 0);
205 S* dialog = new S (this);
207 if (dialog->ShowModal() == wxID_OK) {
208 boost::optional<T> const v = dialog->get ();
210 add_to_control (v.get ());
211 std::vector<T> all = _get ();
212 all.push_back (v.get ());
222 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
227 std::vector<T> all = _get ();
228 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
230 S* dialog = new S (this);
231 dialog->set (all[item]);
232 if (dialog->ShowModal() == wxID_OK) {
233 boost::optional<T> const v = dialog->get ();
238 all[item] = v.get ();
242 for (size_t i = 0; i < _columns.size(); ++i) {
243 _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
249 void remove_clicked ()
251 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
256 _list->DeleteItem (i);
257 std::vector<T> all = _get ();
258 all.erase (all.begin() + i);
261 selection_changed ();
264 void resized (wxSizeEvent& ev)
266 int const w = _list->GetSize().GetWidth() - 2;
271 for (auto i: _columns) {
272 fixed_width += i.width.get_value_or (_default_width);
274 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
282 for (auto i: _columns) {
284 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
292 boost::function <std::vector<T> ()> _get;
293 boost::function <void (std::vector<T>)> _set;
294 std::vector<EditableListColumn> _columns;
295 boost::function<std::string (T, int)> _column;