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/>.
22 #ifndef DCPOMATIC_EDITABLE_LIST_H
23 #define DCPOMATIC_EDITABLE_LIST_H
26 #include "dcpomatic_button.h"
28 #include <dcp/warnings.h>
29 LIBDCP_DISABLE_WARNINGS
30 #include <wx/listctrl.h>
32 LIBDCP_ENABLE_WARNINGS
36 class EditableListColumn
39 EditableListColumn (wxString name_)
44 EditableListColumn (wxString name_, boost::optional<int> width_, bool growable_)
47 , growable (growable_)
51 boost::optional<int> width;
56 namespace EditableListButton
58 static int constexpr NEW = 0x1;
59 static int constexpr EDIT = 0x2;
60 static int constexpr REMOVE = 0x4;
64 /** @param T type of things being edited.
65 * @param S dialog to edit a thing.
66 * @param get Function to get a std::vector of the things being edited.
67 * @param set Function set the things from a a std::vector.
68 * @param column Function to get the display string for a given column in a given item.
70 template<class T, class S>
71 class EditableList : public wxPanel
76 std::vector<EditableListColumn> columns,
77 std::function<std::vector<T> ()> get,
78 std::function<void (std::vector<T>)> set,
79 std::function<std::string (T, int)> column,
88 , _default_width (200)
90 _sizer = new wxBoxSizer (wxHORIZONTAL);
93 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
95 style |= wxLC_NO_HEADER;
99 for (auto i: _columns) {
100 total_width += i.width.get_value_or (_default_width);
104 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
105 * window, so put a border in to help.
107 auto border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
108 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
109 auto border_sizer = new wxBoxSizer (wxHORIZONTAL);
110 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
111 border->SetSizer (border_sizer);
113 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
117 for (auto i: _columns) {
121 _list->InsertColumn (j, ip);
126 _sizer->Add (border, 1, wxEXPAND);
128 _sizer->Add (_list, 1, wxEXPAND);
132 auto s = new wxBoxSizer (wxVERTICAL);
133 if (buttons & EditableListButton::NEW) {
134 _add = new Button (this, _("Add..."));
135 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
137 if (buttons & EditableListButton::EDIT) {
138 _edit = new Button (this, _("Edit..."));
139 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
141 if (buttons & EditableListButton::REMOVE) {
142 _remove = new Button (this, _("Remove"));
143 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
145 _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
149 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
152 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
155 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
158 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
159 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
160 #if BOOST_VERSION >= 106100
161 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
163 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
167 selection_changed ();
172 _list->DeleteAllItems ();
174 auto current = _get ();
175 for (auto const& i: current) {
180 boost::optional<T> selection () const
182 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
188 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
197 boost::signals2::signal<void ()> SelectionChanged;
201 void add_to_control (T item)
203 wxListItem list_item;
204 int const n = _list->GetItemCount ();
206 _list->InsertItem (list_item);
208 for (size_t i = 0; i < _columns.size(); ++i) {
209 _list->SetItem (n, i, std_to_wx (_column (item, i)));
213 void selection_changed ()
215 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
217 _edit->Enable (i >= 0);
220 _remove->Enable (i >= 0);
228 S* dialog = new S (this);
230 if (dialog->ShowModal() == wxID_OK) {
231 auto const v = dialog->get ();
232 static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
234 add_to_control (v.get ());
236 all.push_back (v.get ());
246 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
251 std::vector<T> all = _get ();
252 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
254 S* dialog = new S (this);
255 dialog->set (all[item]);
256 if (dialog->ShowModal() == wxID_OK) {
257 auto const v = dialog->get ();
258 static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
263 all[item] = v.get ();
267 for (size_t i = 0; i < _columns.size(); ++i) {
268 _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
274 void remove_clicked ()
276 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
281 _list->DeleteItem (i);
283 all.erase (all.begin() + i);
286 selection_changed ();
289 void resized (wxSizeEvent& ev)
291 int const w = _list->GetSize().GetWidth() - 2;
296 for (auto i: _columns) {
297 fixed_width += i.width.get_value_or (_default_width);
299 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
307 for (auto i: _columns) {
309 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
317 std::function <std::vector<T> ()> _get;
318 std::function <void (std::vector<T>)> _set;
319 std::vector<EditableListColumn> _columns;
320 std::function<std::string (T, int)> _column;
322 wxButton* _add = nullptr;
323 wxButton* _edit = nullptr;
324 wxButton* _remove = nullptr;