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 /** @param T type of things being edited.
65  *  @param S dialog to edit a thing.
66  *  @param get Function to get a std::vector of the things being edited.
67  *  @param set Function set the things from a a std::vector.
68  *  @param column Function to get the display string for a given column in a given item.
69  */
70 template<class T, class S>
71 class EditableList : public wxPanel
72 {
73 public:
74         EditableList (
75                 wxWindow* parent,
76                 std::vector<EditableListColumn> columns,
77                 std::function<std::vector<T> ()> get,
78                 std::function<void (std::vector<T>)> set,
79                 std::function<std::string (T, int)> column,
80                 bool title,
81                 int buttons
82                 )
83                 : wxPanel (parent)
84                 , _get (get)
85                 , _set (set)
86                 , _columns (columns)
87                 , _column (column)
88                 , _default_width (200)
89         {
90                 _sizer = new wxBoxSizer (wxHORIZONTAL);
91                 SetSizer (_sizer);
92
93                 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
94                 if (!title) {
95                         style |= wxLC_NO_HEADER;
96                 }
97
98                 int total_width = 0;
99                 for (auto i: _columns) {
100                         total_width += i.width.get_value_or (_default_width);
101                 }
102
103 #ifdef __WXGTK3__
104                 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
105                  * window, so put a border in to help.
106                  */
107                 auto border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
108                 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
109                 auto border_sizer = new wxBoxSizer (wxHORIZONTAL);
110                 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
111                 border->SetSizer (border_sizer);
112 #else
113                 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
114 #endif
115
116                 int j = 0;
117                 for (auto i: _columns) {
118                         wxListItem ip;
119                         ip.SetId (j);
120                         ip.SetText (i.name);
121                         _list->InsertColumn (j, ip);
122                         ++j;
123                 }
124
125 #ifdef __WXGTK3__
126                 _sizer->Add (border, 1, wxEXPAND);
127 #else
128                 _sizer->Add (_list, 1, wxEXPAND);
129 #endif
130
131                 {
132                         auto s = new wxBoxSizer (wxVERTICAL);
133                         if (buttons & EditableListButton::NEW) {
134                                 _add = new Button (this, _("Add..."));
135                                 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
136                         }
137                         if (buttons & EditableListButton::EDIT) {
138                                 _edit = new Button (this, _("Edit..."));
139                                 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
140                         }
141                         if (buttons & EditableListButton::REMOVE) {
142                                 _remove = new Button (this, _("Remove"));
143                                 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
144                         }
145                         _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
146                 }
147
148                 if (_add) {
149                         _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
150                 }
151                 if (_edit) {
152                         _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
153                 }
154                 if (_remove) {
155                         _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
156                 }
157
158                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
159                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
160 #if BOOST_VERSION >= 106100
161                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
162 #else
163                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
164 #endif
165
166                 refresh ();
167                 selection_changed ();
168         }
169
170         void refresh ()
171         {
172                 _list->DeleteAllItems ();
173
174                 auto current = _get ();
175                 for (auto const& i: current) {
176                         add_to_control (i);
177                 }
178         }
179
180         boost::optional<T> selection () const
181         {
182                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
183                 if (item == -1) {
184                         return {};
185                 }
186
187                 auto all = _get ();
188                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
189                 return all[item];
190         }
191
192         void layout ()
193         {
194                 _sizer->Layout ();
195         }
196
197         boost::signals2::signal<void ()> SelectionChanged;
198
199 private:
200
201         void add_to_control (T item)
202         {
203                 wxListItem list_item;
204                 int const n = _list->GetItemCount ();
205                 list_item.SetId (n);
206                 _list->InsertItem (list_item);
207
208                 for (size_t i = 0; i < _columns.size(); ++i) {
209                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
210                 }
211         }
212
213         void selection_changed ()
214         {
215                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
216                 if (_edit) {
217                         _edit->Enable (i >= 0);
218                 }
219                 if (_remove) {
220                         _remove->Enable (i >= 0);
221                 }
222
223                 SelectionChanged ();
224         }
225
226         void add_clicked ()
227         {
228                 S* dialog = new S (this);
229
230                 if (dialog->ShowModal() == wxID_OK) {
231                         auto const v = dialog->get ();
232                         static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
233                         if (v) {
234                                 add_to_control (v.get ());
235                                 auto all = _get ();
236                                 all.push_back (v.get ());
237                                 _set (all);
238                         }
239                 }
240
241                 dialog->Destroy ();
242         }
243
244         void edit_clicked ()
245         {
246                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
247                 if (item == -1) {
248                         return;
249                 }
250
251                 std::vector<T> all = _get ();
252                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
253
254                 S* dialog = new S (this);
255                 dialog->set (all[item]);
256                 if (dialog->ShowModal() == wxID_OK) {
257                         auto const v = dialog->get ();
258                         static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
259                         if (!v) {
260                                 return;
261                         }
262
263                         all[item] = v.get ();
264                 }
265                 dialog->Destroy ();
266
267                 for (size_t i = 0; i < _columns.size(); ++i) {
268                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
269                 }
270
271                 _set (all);
272         }
273
274         void remove_clicked ()
275         {
276                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
277                 if (i == -1) {
278                         return;
279                 }
280
281                 _list->DeleteItem (i);
282                 auto all = _get ();
283                 all.erase (all.begin() + i);
284                 _set (all);
285
286                 selection_changed ();
287         }
288
289         void resized (wxSizeEvent& ev)
290         {
291                 int const w = _list->GetSize().GetWidth() - 2;
292
293                 int fixed_width = 0;
294                 int growable = 0;
295                 int j = 0;
296                 for (auto i: _columns) {
297                         fixed_width += i.width.get_value_or (_default_width);
298                         if (!i.growable) {
299                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
300                         } else {
301                                 ++growable;
302                         }
303                         ++j;
304                 }
305
306                 j = 0;
307                 for (auto i: _columns) {
308                         if (i.growable) {
309                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
310                         }
311                         ++j;
312                 }
313
314                 ev.Skip ();
315         }
316
317         std::function <std::vector<T> ()> _get;
318         std::function <void (std::vector<T>)> _set;
319         std::vector<EditableListColumn> _columns;
320         std::function<std::string (T, int)> _column;
321
322         wxButton* _add = nullptr;
323         wxButton* _edit = nullptr;
324         wxButton* _remove = nullptr;
325         wxListCtrl* _list;
326         wxBoxSizer* _sizer;
327         int _default_width;
328 };
329
330 #endif