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