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 enum class EditableListTitle
71 /** @param T type of things being edited.
72 * @param S dialog to edit a thing.
73 * @param get Function to get a std::vector of the things being edited.
74 * @param set Function set the things from a a std::vector.
75 * @param column Function to get the display string for a given column in a given item.
77 template<class T, class S>
78 class EditableList : public wxPanel
83 std::vector<EditableListColumn> columns,
84 std::function<std::vector<T> ()> get,
85 std::function<void (std::vector<T>)> set,
86 std::function<std::string (T, int)> column,
87 EditableListTitle title,
95 , _default_width (200)
97 _sizer = new wxBoxSizer (wxHORIZONTAL);
100 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
101 if (title == EditableListTitle::INVISIBLE) {
102 style |= wxLC_NO_HEADER;
106 for (auto i: _columns) {
107 total_width += i.width.get_value_or (_default_width);
111 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
112 * window, so put a border in to help.
114 auto border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
115 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
116 auto border_sizer = new wxBoxSizer (wxHORIZONTAL);
117 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
118 border->SetSizer (border_sizer);
120 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
124 for (auto i: _columns) {
128 _list->InsertColumn (j, ip);
133 _sizer->Add (border, 1, wxEXPAND);
135 _sizer->Add (_list, 1, wxEXPAND);
139 auto s = new wxBoxSizer (wxVERTICAL);
140 if (buttons & EditableListButton::NEW) {
141 _add = new Button (this, _("Add..."));
142 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
144 if (buttons & EditableListButton::EDIT) {
145 _edit = new Button (this, _("Edit..."));
146 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
148 if (buttons & EditableListButton::REMOVE) {
149 _remove = new Button (this, _("Remove"));
150 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
152 _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
156 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
159 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
162 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
165 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
166 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
167 #if BOOST_VERSION >= 106100
168 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
170 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
174 selection_changed ();
179 _list->DeleteAllItems ();
181 auto current = _get ();
182 for (auto const& i: current) {
187 boost::optional<T> selection () const
189 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
195 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
204 boost::signals2::signal<void ()> SelectionChanged;
208 void add_to_control (T item)
210 wxListItem list_item;
211 int const n = _list->GetItemCount ();
213 _list->InsertItem (list_item);
215 for (size_t i = 0; i < _columns.size(); ++i) {
216 _list->SetItem (n, i, std_to_wx (_column (item, i)));
220 void selection_changed ()
222 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
224 _edit->Enable (i >= 0);
227 _remove->Enable (i >= 0);
235 auto dialog = make_wx<S>(this);
237 if (dialog->ShowModal() == wxID_OK) {
238 auto const v = dialog->get ();
239 static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
241 add_to_control (v.get ());
243 all.push_back (v.get ());
251 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
256 std::vector<T> all = _get ();
257 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
259 auto dialog = make_wx<S>(this);
260 dialog->set (all[item]);
261 if (dialog->ShowModal() == wxID_OK) {
262 auto const v = dialog->get ();
263 static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
268 all[item] = v.get ();
271 for (size_t i = 0; i < _columns.size(); ++i) {
272 _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
278 void remove_clicked ()
280 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
285 _list->DeleteItem (i);
287 all.erase (all.begin() + i);
290 selection_changed ();
293 void resized (wxSizeEvent& ev)
295 int const w = _list->GetSize().GetWidth() - 2;
300 for (auto i: _columns) {
301 fixed_width += i.width.get_value_or (_default_width);
303 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
311 for (auto i: _columns) {
313 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
321 std::function <std::vector<T> ()> _get;
322 std::function <void (std::vector<T>)> _set;
323 std::vector<EditableListColumn> _columns;
324 std::function<std::string (T, int)> _column;
326 wxButton* _add = nullptr;
327 wxButton* _edit = nullptr;
328 wxButton* _remove = nullptr;