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"
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <wx/listctrl.h>
33 LIBDCP_ENABLE_WARNINGS
37 class EditableListColumn
40 EditableListColumn (wxString name_)
45 EditableListColumn (wxString name_, boost::optional<int> width_, bool growable_)
48 , growable (growable_)
52 boost::optional<int> width;
57 namespace EditableListButton
59 static int constexpr NEW = 0x1;
60 static int constexpr EDIT = 0x2;
61 static int constexpr REMOVE = 0x4;
65 enum class EditableListTitle
72 /** @param T type of things being edited.
73 * @param S dialog to edit a thing.
74 * @param get Function to get a std::vector of the things being edited.
75 * @param set Function set the things from a a std::vector.
76 * @param column Function to get the display string for a given column in a given item.
78 template<class T, class S>
79 class EditableList : public wxPanel
84 std::vector<EditableListColumn> columns,
85 std::function<std::vector<T> ()> get,
86 std::function<void (std::vector<T>)> set,
87 std::function<std::string (T, int)> column,
88 EditableListTitle title,
96 , _default_width (200)
98 _sizer = new wxBoxSizer (wxHORIZONTAL);
101 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
102 if (title == EditableListTitle::INVISIBLE) {
103 style |= wxLC_NO_HEADER;
107 for (auto i: _columns) {
108 total_width += i.width.get_value_or (_default_width);
112 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
113 * window, so put a border in to help.
115 auto border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
116 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
117 auto border_sizer = new wxBoxSizer (wxHORIZONTAL);
118 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
119 border->SetSizer (border_sizer);
121 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
125 for (auto i: _columns) {
129 _list->InsertColumn (j, ip);
134 _sizer->Add (border, 1, wxEXPAND);
136 _sizer->Add (_list, 1, wxEXPAND);
140 auto s = new wxBoxSizer (wxVERTICAL);
141 if (buttons & EditableListButton::NEW) {
142 _add = new Button (this, _("Add..."));
143 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
145 if (buttons & EditableListButton::EDIT) {
146 _edit = new Button (this, _("Edit..."));
147 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
149 if (buttons & EditableListButton::REMOVE) {
150 _remove = new Button (this, _("Remove"));
151 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
153 _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
157 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
160 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
163 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
166 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
167 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
168 #if BOOST_VERSION >= 106100
169 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
171 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
175 selection_changed ();
180 _list->DeleteAllItems ();
182 auto current = _get ();
183 for (auto const& i: current) {
188 boost::optional<T> selection () const
190 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
196 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
205 boost::signals2::signal<void ()> SelectionChanged;
209 void add_to_control (T item)
211 wxListItem list_item;
212 int const n = _list->GetItemCount ();
214 _list->InsertItem (list_item);
216 for (size_t i = 0; i < _columns.size(); ++i) {
217 _list->SetItem (n, i, std_to_wx (_column (item, i)));
221 void selection_changed ()
223 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
225 _edit->Enable (i >= 0);
228 _remove->Enable (i >= 0);
236 auto dialog = make_wx<S>(this);
238 if (dialog->ShowModal() == wxID_OK) {
239 auto const v = dialog->get ();
240 static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
242 add_to_control (v.get ());
244 all.push_back (v.get ());
252 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
257 std::vector<T> all = _get ();
258 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
260 auto dialog = make_wx<S>(this);
261 dialog->set (all[item]);
262 if (dialog->ShowModal() == wxID_OK) {
263 auto const v = dialog->get ();
264 static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
269 all[item] = v.get ();
272 for (size_t i = 0; i < _columns.size(); ++i) {
273 _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
279 void remove_clicked ()
281 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
286 _list->DeleteItem (i);
288 all.erase (all.begin() + i);
291 selection_changed ();
294 void resized (wxSizeEvent& ev)
296 int const w = _list->GetSize().GetWidth() - 2;
301 for (auto i: _columns) {
302 fixed_width += i.width.get_value_or (_default_width);
304 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
312 for (auto i: _columns) {
314 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
322 std::function <std::vector<T> ()> _get;
323 std::function <void (std::vector<T>)> _set;
324 std::vector<EditableListColumn> _columns;
325 std::function<std::string (T, int)> _column;
327 wxButton* _add = nullptr;
328 wxButton* _edit = nullptr;
329 wxButton* _remove = nullptr;