Use more ScopeGuards.
[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 "lib/scope_guard.h"
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <wx/listctrl.h>
32 #include <wx/wx.h>
33 LIBDCP_ENABLE_WARNINGS
34 #include <vector>
35
36
37 class EditableListColumn
38 {
39 public:
40         EditableListColumn (wxString name_)
41                 : name (name_)
42                 , growable (false)
43         {}
44
45         EditableListColumn (wxString name_, boost::optional<int> width_, bool growable_)
46                 : name (name_)
47                 , width (width_)
48                 , growable (growable_)
49         {}
50
51         wxString name;
52         boost::optional<int> width;
53         bool growable;
54 };
55
56
57 namespace EditableListButton
58 {
59         static int constexpr NEW = 0x1;
60         static int constexpr EDIT = 0x2;
61         static int constexpr REMOVE = 0x4;
62 };
63
64
65 enum class EditableListTitle
66 {
67         VISIBLE,
68         INVISIBLE
69 };
70
71
72 /** @param T type of things being edited.
73  *  @param S dialog to edit a thing.
74  *  @param get Function to get a std::vector of the things being edited.
75  *  @param set Function set the things from a a std::vector.
76  *  @param column Function to get the display string for a given column in a given item.
77  */
78 template<class T, class S>
79 class EditableList : public wxPanel
80 {
81 public:
82         EditableList (
83                 wxWindow* parent,
84                 std::vector<EditableListColumn> columns,
85                 std::function<std::vector<T> ()> get,
86                 std::function<void (std::vector<T>)> set,
87                 std::function<std::string (T, int)> column,
88                 EditableListTitle title,
89                 int buttons
90                 )
91                 : wxPanel (parent)
92                 , _get (get)
93                 , _set (set)
94                 , _columns (columns)
95                 , _column (column)
96                 , _default_width (200)
97         {
98                 _sizer = new wxBoxSizer (wxHORIZONTAL);
99                 SetSizer (_sizer);
100
101                 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
102                 if (title == EditableListTitle::INVISIBLE) {
103                         style |= wxLC_NO_HEADER;
104                 }
105
106                 int total_width = 0;
107                 for (auto i: _columns) {
108                         total_width += i.width.get_value_or (_default_width);
109                 }
110
111 #ifdef __WXGTK3__
112                 /* With the GTK3 backend wxListCtrls are hard to pick out from the background of the
113                  * window, so put a border in to help.
114                  */
115                 auto border = new wxPanel (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxBORDER_THEME);
116                 _list = new wxListCtrl (border, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
117                 auto border_sizer = new wxBoxSizer (wxHORIZONTAL);
118                 border_sizer->Add (_list, 1, wxALL | wxEXPAND, 2);
119                 border->SetSizer (border_sizer);
120 #else
121                 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
122 #endif
123
124                 int j = 0;
125                 for (auto i: _columns) {
126                         wxListItem ip;
127                         ip.SetId (j);
128                         ip.SetText (i.name);
129                         _list->InsertColumn (j, ip);
130                         ++j;
131                 }
132
133 #ifdef __WXGTK3__
134                 _sizer->Add (border, 1, wxEXPAND);
135 #else
136                 _sizer->Add (_list, 1, wxEXPAND);
137 #endif
138
139                 {
140                         auto s = new wxBoxSizer (wxVERTICAL);
141                         if (buttons & EditableListButton::NEW) {
142                                 _add = new Button (this, _("Add..."));
143                                 s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
144                         }
145                         if (buttons & EditableListButton::EDIT) {
146                                 _edit = new Button (this, _("Edit..."));
147                                 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
148                         }
149                         if (buttons & EditableListButton::REMOVE) {
150                                 _remove = new Button (this, _("Remove"));
151                                 s->Add (_remove, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
152                         }
153                         _sizer->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
154                 }
155
156                 if (_add) {
157                         _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
158                 }
159                 if (_edit) {
160                         _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
161                 }
162                 if (_remove) {
163                         _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
164                 }
165
166                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
167                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
168 #if BOOST_VERSION >= 106100
169                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, boost::placeholders::_1));
170 #else
171                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
172 #endif
173
174                 refresh ();
175                 selection_changed ();
176         }
177
178         void refresh ()
179         {
180                 _list->DeleteAllItems ();
181
182                 auto current = _get ();
183                 for (auto const& i: current) {
184                         add_to_control (i);
185                 }
186         }
187
188         boost::optional<T> selection () const
189         {
190                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
191                 if (item == -1) {
192                         return {};
193                 }
194
195                 auto all = _get ();
196                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
197                 return all[item];
198         }
199
200         void layout ()
201         {
202                 _sizer->Layout ();
203         }
204
205         boost::signals2::signal<void ()> SelectionChanged;
206
207 private:
208
209         void add_to_control (T item)
210         {
211                 wxListItem list_item;
212                 int const n = _list->GetItemCount ();
213                 list_item.SetId (n);
214                 _list->InsertItem (list_item);
215
216                 for (size_t i = 0; i < _columns.size(); ++i) {
217                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
218                 }
219         }
220
221         void selection_changed ()
222         {
223                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
224                 if (_edit) {
225                         _edit->Enable (i >= 0);
226                 }
227                 if (_remove) {
228                         _remove->Enable (i >= 0);
229                 }
230
231                 SelectionChanged ();
232         }
233
234         void add_clicked ()
235         {
236                 S* dialog = new S (this);
237                 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
238
239                 if (dialog->ShowModal() == wxID_OK) {
240                         auto const v = dialog->get ();
241                         static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
242                         if (v) {
243                                 add_to_control (v.get ());
244                                 auto all = _get ();
245                                 all.push_back (v.get ());
246                                 _set (all);
247                         }
248                 }
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                 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
263                 dialog->set (all[item]);
264                 if (dialog->ShowModal() == wxID_OK) {
265                         auto const v = dialog->get ();
266                         static_assert(std::is_same<typename std::remove_const<decltype(v)>::type, boost::optional<T>>::value, "get() must return boost::optional<T>");
267                         if (!v) {
268                                 return;
269                         }
270
271                         all[item] = v.get ();
272                 }
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