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;
55 /** @param T type of things being edited.
56 * @param S dialog to edit a thing.
57 * @param get Function to get a std::vector of the things being edited.
58 * @param set Function set the things from a a std::vector.
59 * @param column Function to get the display string for a given column in a given item.
61 template<class T, class S>
62 class EditableList : public wxPanel
67 std::vector<EditableListColumn> columns,
68 std::function<std::vector<T> ()> get,
69 std::function<void (std::vector<T>)> set,
70 std::function<std::string (T, int)> column,
80 , _default_width (200)
82 _sizer = new wxBoxSizer (wxHORIZONTAL);
85 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
87 style |= wxLC_NO_HEADER;
91 for (auto i: _columns) {
92 total_width += i.width.get_value_or (_default_width);
96 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
97 * window, so put a border in to help.
99 wxPanel* border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
100 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
101 wxBoxSizer* border_sizer = new wxBoxSizer (wxHORIZONTAL);
102 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
103 border->SetSizer (border_sizer);
105 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
109 for (auto i: _columns) {
113 _list->InsertColumn (j, ip);
118 _sizer->Add (border, 1, wxEXPAND);
120 _sizer->Add (_list, 1, wxEXPAND);
124 wxSizer* s = new wxBoxSizer (wxVERTICAL);
125 _add = new Button (this, _("Add..."));
126 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
128 _edit = new Button (this, _("Edit..."));
129 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
131 _remove = new Button (this, _("Remove"));
132 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
133 _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
136 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
138 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
140 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
142 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
143 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
144 #if BOOST_VERSION >= 106100
145 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
147 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
151 selection_changed ();
156 _list->DeleteAllItems ();
158 std::vector<T> current = _get ();
159 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
164 boost::optional<T> selection () const
166 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
168 return boost::optional<T> ();
171 std::vector<T> all = _get ();
172 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
181 boost::signals2::signal<void ()> SelectionChanged;
185 void add_to_control (T item)
187 wxListItem list_item;
188 int const n = _list->GetItemCount ();
190 _list->InsertItem (list_item);
192 for (size_t i = 0; i < _columns.size(); ++i) {
193 _list->SetItem (n, i, std_to_wx (_column (item, i)));
197 void selection_changed ()
199 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
201 _edit->Enable (i >= 0);
203 _remove->Enable (i >= 0);
210 S* dialog = new S (this);
212 if (dialog->ShowModal() == wxID_OK) {
213 boost::optional<T> const v = dialog->get ();
215 add_to_control (v.get ());
216 std::vector<T> all = _get ();
217 all.push_back (v.get ());
227 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
232 std::vector<T> all = _get ();
233 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
235 S* dialog = new S (this);
236 dialog->set (all[item]);
237 if (dialog->ShowModal() == wxID_OK) {
238 boost::optional<T> const v = dialog->get ();
243 all[item] = v.get ();
247 for (size_t i = 0; i < _columns.size(); ++i) {
248 _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
254 void remove_clicked ()
256 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
261 _list->DeleteItem (i);
262 std::vector<T> all = _get ();
263 all.erase (all.begin() + i);
266 selection_changed ();
269 void resized (wxSizeEvent& ev)
271 int const w = _list->GetSize().GetWidth() - 2;
276 for (auto i: _columns) {
277 fixed_width += i.width.get_value_or (_default_width);
279 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
287 for (auto i: _columns) {
289 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
297 std::function <std::vector<T> ()> _get;
298 std::function <void (std::vector<T>)> _set;
299 std::vector<EditableListColumn> _columns;
300 std::function<std::string (T, int)> _column;