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 <wx/listctrl.h>
33 class EditableListColumn
36 EditableListColumn (wxString name_)
41 EditableListColumn (wxString name_, boost::optional<int> width_, bool growable_)
44 , growable (growable_)
48 boost::optional<int> width;
52 /** @param T type of things being edited.
53 * @param S dialog to edit a thing.
54 * @param get Function to get a std::vector of the things being edited.
55 * @param set Function set the things from a a std::vector.
56 * @param column Function to get the display string for a given column in a given item.
58 template<class T, class S>
59 class EditableList : public wxPanel
64 std::vector<EditableListColumn> columns,
65 std::function<std::vector<T> ()> get,
66 std::function<void (std::vector<T>)> set,
67 std::function<std::string (T, int)> column,
77 , _default_width (200)
79 _sizer = new wxBoxSizer (wxHORIZONTAL);
82 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
84 style |= wxLC_NO_HEADER;
88 for (auto i: _columns) {
89 total_width += i.width.get_value_or (_default_width);
93 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
94 * window, so put a border in to help.
96 wxPanel* border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
97 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
98 wxBoxSizer* border_sizer = new wxBoxSizer (wxHORIZONTAL);
99 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
100 border->SetSizer (border_sizer);
102 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
106 for (auto i: _columns) {
110 _list->InsertColumn (j, ip);
115 _sizer->Add (border, 1, wxEXPAND);
117 _sizer->Add (_list, 1, wxEXPAND);
121 wxSizer* s = new wxBoxSizer (wxVERTICAL);
122 _add = new Button (this, _("Add..."));
123 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
125 _edit = new Button (this, _("Edit..."));
126 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
128 _remove = new Button (this, _("Remove"));
129 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
130 _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
133 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
135 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
137 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
139 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
140 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
141 #if BOOST_VERSION >= 106100
142 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
144 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
148 selection_changed ();
153 _list->DeleteAllItems ();
155 std::vector<T> current = _get ();
156 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
161 boost::optional<T> selection () const
163 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
165 return boost::optional<T> ();
168 std::vector<T> all = _get ();
169 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
178 boost::signals2::signal<void ()> SelectionChanged;
182 void add_to_control (T item)
184 wxListItem list_item;
185 int const n = _list->GetItemCount ();
187 _list->InsertItem (list_item);
189 for (size_t i = 0; i < _columns.size(); ++i) {
190 _list->SetItem (n, i, std_to_wx (_column (item, i)));
194 void selection_changed ()
196 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
198 _edit->Enable (i >= 0);
200 _remove->Enable (i >= 0);
207 S* dialog = new S (this);
209 if (dialog->ShowModal() == wxID_OK) {
210 boost::optional<T> const v = dialog->get ();
212 add_to_control (v.get ());
213 std::vector<T> all = _get ();
214 all.push_back (v.get ());
224 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
229 std::vector<T> all = _get ();
230 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
232 S* dialog = new S (this);
233 dialog->set (all[item]);
234 if (dialog->ShowModal() == wxID_OK) {
235 boost::optional<T> const v = dialog->get ();
240 all[item] = v.get ();
244 for (size_t i = 0; i < _columns.size(); ++i) {
245 _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
251 void remove_clicked ()
253 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
258 _list->DeleteItem (i);
259 std::vector<T> all = _get ();
260 all.erase (all.begin() + i);
263 selection_changed ();
266 void resized (wxSizeEvent& ev)
268 int const w = _list->GetSize().GetWidth() - 2;
273 for (auto i: _columns) {
274 fixed_width += i.width.get_value_or (_default_width);
276 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
284 for (auto i: _columns) {
286 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
294 std::function <std::vector<T> ()> _get;
295 std::function <void (std::vector<T>)> _set;
296 std::vector<EditableListColumn> _columns;
297 std::function<std::string (T, int)> _column;