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/>.
21 #ifndef DCPOMATIC_EDITABLE_LIST_H
22 #define DCPOMATIC_EDITABLE_LIST_H
25 #include "dcpomatic_button.h"
27 #include <wx/listctrl.h>
30 class EditableListColumn
33 EditableListColumn (wxString name_)
38 EditableListColumn (wxString name_, boost::optional<int> width_, bool growable_)
41 , growable (growable_)
45 boost::optional<int> width;
49 /** @param T type of things being edited.
50 * @param S dialog to edit a thing.
51 * @param get Function to get a std::vector of the things being edited.
52 * @param set Function set the things from a a std::vector.
53 * @param column Function to get the display string for a given column in a given item.
55 template<class T, class S>
56 class EditableList : public wxPanel
61 std::vector<EditableListColumn> columns,
62 std::function<std::vector<T> ()> get,
63 std::function<void (std::vector<T>)> set,
64 std::function<std::string (T, int)> column,
74 , _default_width (200)
76 _sizer = new wxBoxSizer (wxHORIZONTAL);
79 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
81 style |= wxLC_NO_HEADER;
85 for (auto i: _columns) {
86 total_width += i.width.get_value_or (_default_width);
90 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
91 * window, so put a border in to help.
93 wxPanel* border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
94 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
95 wxBoxSizer* border_sizer = new wxBoxSizer (wxHORIZONTAL);
96 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
97 border->SetSizer (border_sizer);
99 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
103 for (auto i: _columns) {
107 _list->InsertColumn (j, ip);
112 _sizer->Add (border, 1, wxEXPAND);
114 _sizer->Add (_list, 1, wxEXPAND);
118 wxSizer* s = new wxBoxSizer (wxVERTICAL);
119 _add = new Button (this, _("Add..."));
120 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
122 _edit = new Button (this, _("Edit..."));
123 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
125 _remove = new Button (this, _("Remove"));
126 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
127 _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
130 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
132 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
134 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
136 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
137 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
138 #if BOOST_VERSION >= 106100
139 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
141 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
145 selection_changed ();
150 _list->DeleteAllItems ();
152 std::vector<T> current = _get ();
153 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
158 boost::optional<T> selection () const
160 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
162 return boost::optional<T> ();
165 std::vector<T> all = _get ();
166 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
175 boost::signals2::signal<void ()> SelectionChanged;
179 void add_to_control (T item)
181 wxListItem list_item;
182 int const n = _list->GetItemCount ();
184 _list->InsertItem (list_item);
186 for (size_t i = 0; i < _columns.size(); ++i) {
187 _list->SetItem (n, i, std_to_wx (_column (item, i)));
191 void selection_changed ()
193 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
195 _edit->Enable (i >= 0);
197 _remove->Enable (i >= 0);
204 S* dialog = new S (this);
206 if (dialog->ShowModal() == wxID_OK) {
207 boost::optional<T> const v = dialog->get ();
209 add_to_control (v.get ());
210 std::vector<T> all = _get ();
211 all.push_back (v.get ());
221 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
226 std::vector<T> all = _get ();
227 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
229 S* dialog = new S (this);
230 dialog->set (all[item]);
231 if (dialog->ShowModal() == wxID_OK) {
232 boost::optional<T> const v = dialog->get ();
237 all[item] = v.get ();
241 for (size_t i = 0; i < _columns.size(); ++i) {
242 _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
248 void remove_clicked ()
250 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
255 _list->DeleteItem (i);
256 std::vector<T> all = _get ();
257 all.erase (all.begin() + i);
260 selection_changed ();
263 void resized (wxSizeEvent& ev)
265 int const w = _list->GetSize().GetWidth() - 2;
270 for (auto i: _columns) {
271 fixed_width += i.width.get_value_or (_default_width);
273 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
281 for (auto i: _columns) {
283 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
291 std::function <std::vector<T> ()> _get;
292 std::function <void (std::vector<T>)> _set;
293 std::vector<EditableListColumn> _columns;
294 std::function<std::string (T, int)> _column;