Supporters update.
[dcpomatic.git] / src / wx / editable_list.h
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #ifndef DCPOMATIC_EDITABLE_LIST_H
23 #define DCPOMATIC_EDITABLE_LIST_H
24
25
26 #include "dcpomatic_button.h"
27 #include "wx_util.h"
28 #include <dcp/warnings.h>
29 LIBDCP_DISABLE_WARNINGS
30 #include <wx/listctrl.h>
31 #include <wx/wx.h>
32 LIBDCP_ENABLE_WARNINGS
33 #include <vector>
34
35
36 class EditableListColumn
37 {
38 public:
39         EditableListColumn (wxString name_)
40                 : name (name_)
41                 , growable (false)
42         {}
43
44         EditableListColumn (wxString name_, boost::optional<int> width_, bool growable_)
45                 : name (name_)
46                 , width (width_)
47                 , growable (growable_)
48         {}
49
50         wxString name;
51         boost::optional<int> width;
52         bool growable;
53 };
54
55
56 namespace EditableListButton
57 {
58         static int constexpr NEW = 0x1;
59         static int constexpr EDIT = 0x2;
60         static int constexpr REMOVE = 0x4;
61 };
62
63
64 enum class EditableListTitle
65 {
66         VISIBLE,
67         INVISIBLE
68 };
69
70
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.
76  */
77 template<class T, class S>
78 class EditableList : public wxPanel
79 {
80 public:
81         EditableList (
82                 wxWindow* parent,
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,
88                 int buttons
89                 )
90                 : wxPanel (parent)
91                 , _get (get)
92                 , _set (set)
93                 , _columns (columns)
94                 , _column (column)
95                 , _default_width (200)
96         {
97                 _sizer = new wxBoxSizer (wxHORIZONTAL);
98                 SetSizer (_sizer);
99
100                 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
101                 if (title == EditableListTitle::INVISIBLE) {
102                         style |= wxLC_NO_HEADER;
103                 }
104
105                 int total_width = 0;
106                 for (auto i: _columns) {
107                         total_width += i.width.get_value_or (_default_width);
108                 }
109
110 #ifdef __WXGTK3__
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.
113                  */
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);
119 #else
120                 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
121 #endif
122
123                 int j = 0;
124                 for (auto i: _columns) {
125                         wxListItem ip;
126                         ip.SetId (j);
127                         ip.SetText (i.name);
128                         _list->InsertColumn (j, ip);
129                         ++j;
130                 }
131
132 #ifdef __WXGTK3__
133                 _sizer->Add (border, 1, wxEXPAND);
134 #else
135                 _sizer->Add (_list, 1, wxEXPAND);
136 #endif
137
138                 {
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);
143                         }
144                         if (buttons & EditableListButton::EDIT) {
145                                 _edit = new Button (this, _("Edit..."));
146                                 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
147                         }
148                         if (buttons & EditableListButton::REMOVE) {
149                                 _remove = new Button (this, _("Remove"));
150                                 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
151                         }
152                         _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
153                 }
154
155                 if (_add) {
156                         _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
157                 }
158                 if (_edit) {
159                         _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
160                 }
161                 if (_remove) {
162                         _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
163                 }
164
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));
169 #else
170                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
171 #endif
172
173                 refresh ();
174                 selection_changed ();
175         }
176
177         void refresh ()
178         {
179                 _list->DeleteAllItems ();
180
181                 auto current = _get ();
182                 for (auto const& i: current) {
183                         add_to_control (i);
184                 }
185         }
186
187         boost::optional<T> selection () const
188         {
189                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
190                 if (item == -1) {
191                         return {};
192                 }
193
194                 auto all = _get ();
195                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
196                 return all[item];
197         }
198
199         void layout ()
200         {
201                 _sizer->Layout ();
202         }
203
204         boost::signals2::signal<void ()> SelectionChanged;
205
206 private:
207
208         void add_to_control (T item)
209         {
210                 wxListItem list_item;
211                 int const n = _list->GetItemCount ();
212                 list_item.SetId (n);
213                 _list->InsertItem (list_item);
214
215                 for (size_t i = 0; i < _columns.size(); ++i) {
216                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
217                 }
218         }
219
220         void selection_changed ()
221         {
222                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
223                 if (_edit) {
224                         _edit->Enable (i >= 0);
225                 }
226                 if (_remove) {
227                         _remove->Enable (i >= 0);
228                 }
229
230                 SelectionChanged ();
231         }
232
233         void add_clicked ()
234         {
235                 S* dialog = new S (this);
236
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>");
240                         if (v) {
241                                 add_to_control (v.get ());
242                                 auto all = _get ();
243                                 all.push_back (v.get ());
244                                 _set (all);
245                         }
246                 }
247
248                 dialog->Destroy ();
249         }
250
251         void edit_clicked ()
252         {
253                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
254                 if (item == -1) {
255                         return;
256                 }
257
258                 std::vector<T> all = _get ();
259                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
260
261                 S* dialog = new S (this);
262                 dialog->set (all[item]);
263                 if (dialog->ShowModal() == wxID_OK) {
264                         auto const v = dialog->get ();
265                         static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
266                         if (!v) {
267                                 return;
268                         }
269
270                         all[item] = v.get ();
271                 }
272                 dialog->Destroy ();
273
274                 for (size_t i = 0; i < _columns.size(); ++i) {
275                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
276                 }
277
278                 _set (all);
279         }
280
281         void remove_clicked ()
282         {
283                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
284                 if (i == -1) {
285                         return;
286                 }
287
288                 _list->DeleteItem (i);
289                 auto all = _get ();
290                 all.erase (all.begin() + i);
291                 _set (all);
292
293                 selection_changed ();
294         }
295
296         void resized (wxSizeEvent& ev)
297         {
298                 int const w = _list->GetSize().GetWidth() - 2;
299
300                 int fixed_width = 0;
301                 int growable = 0;
302                 int j = 0;
303                 for (auto i: _columns) {
304                         fixed_width += i.width.get_value_or (_default_width);
305                         if (!i.growable) {
306                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
307                         } else {
308                                 ++growable;
309                         }
310                         ++j;
311                 }
312
313                 j = 0;
314                 for (auto i: _columns) {
315                         if (i.growable) {
316                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
317                         }
318                         ++j;
319                 }
320
321                 ev.Skip ();
322         }
323
324         std::function <std::vector<T> ()> _get;
325         std::function <void (std::vector<T>)> _set;
326         std::vector<EditableListColumn> _columns;
327         std::function<std::string (T, int)> _column;
328
329         wxButton* _add = nullptr;
330         wxButton* _edit = nullptr;
331         wxButton* _remove = nullptr;
332         wxListCtrl* _list;
333         wxBoxSizer* _sizer;
334         int _default_width;
335 };
336
337 #endif