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 #ifndef DCPOMATIC_EDITABLE_LIST_H
22 #define DCPOMATIC_EDITABLE_LIST_H
23
24 #include "wx_util.h"
25 #include "dcpomatic_button.h"
26 #include <wx/wx.h>
27 #include <wx/listctrl.h>
28 #include <vector>
29
30 class EditableListColumn
31 {
32 public:
33         EditableListColumn (wxString name_)
34                 : name (name_)
35                 , growable (false)
36         {}
37
38         EditableListColumn (wxString name_, boost::optional<int> width_, bool growable_)
39                 : name (name_)
40                 , width (width_)
41                 , growable (growable_)
42         {}
43
44         wxString name;
45         boost::optional<int> width;
46         bool growable;
47 };
48
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.
54  */
55 template<class T, class S>
56 class EditableList : public wxPanel
57 {
58 public:
59         EditableList (
60                 wxWindow* parent,
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,
65                 bool can_edit = true,
66                 bool title = true
67                 )
68                 : wxPanel (parent)
69                 , _get (get)
70                 , _set (set)
71                 , _columns (columns)
72                 , _column (column)
73                 , _edit (0)
74                 , _default_width (200)
75         {
76                 _sizer = new wxBoxSizer (wxHORIZONTAL);
77                 SetSizer (_sizer);
78
79                 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
80                 if (!title) {
81                         style |= wxLC_NO_HEADER;
82                 }
83
84                 int total_width = 0;
85                 for (auto i: _columns) {
86                         total_width += i.width.get_value_or (_default_width);
87                 }
88
89 #ifdef __WXGTK3__
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.
92                  */
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);
98 #else
99                 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(total_width, 100), style);
100 #endif
101
102                 int j = 0;
103                 for (auto i: _columns) {
104                         wxListItem ip;
105                         ip.SetId (j);
106                         ip.SetText (i.name);
107                         _list->InsertColumn (j, ip);
108                         ++j;
109                 }
110
111 #ifdef __WXGTK3__
112                 _sizer->Add (border, 1, wxEXPAND);
113 #else
114                 _sizer->Add (_list, 1, wxEXPAND);
115 #endif
116
117                 {
118                         wxSizer* s = new wxBoxSizer (wxVERTICAL);
119                         _add = new Button (this, _("Add..."));
120                         s->Add (_add, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
121                         if (can_edit) {
122                                 _edit = new Button (this, _("Edit..."));
123                                 s->Add (_edit, 1, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
124                         }
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);
128                 }
129
130                 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
131                 if (_edit) {
132                         _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
133                 }
134                 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
135
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));
140 #else
141                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
142 #endif
143
144                 refresh ();
145                 selection_changed ();
146         }
147
148         void refresh ()
149         {
150                 _list->DeleteAllItems ();
151
152                 std::vector<T> current = _get ();
153                 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
154                         add_to_control (*i);
155                 }
156         }
157
158         boost::optional<T> selection () const
159         {
160                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
161                 if (item == -1) {
162                         return boost::optional<T> ();
163                 }
164
165                 std::vector<T> all = _get ();
166                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
167                 return all[item];
168         }
169
170         void layout ()
171         {
172                 _sizer->Layout ();
173         }
174
175         boost::signals2::signal<void ()> SelectionChanged;
176
177 private:
178
179         void add_to_control (T item)
180         {
181                 wxListItem list_item;
182                 int const n = _list->GetItemCount ();
183                 list_item.SetId (n);
184                 _list->InsertItem (list_item);
185
186                 for (size_t i = 0; i < _columns.size(); ++i) {
187                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
188                 }
189         }
190
191         void selection_changed ()
192         {
193                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
194                 if (_edit) {
195                         _edit->Enable (i >= 0);
196                 }
197                 _remove->Enable (i >= 0);
198
199                 SelectionChanged ();
200         }
201
202         void add_clicked ()
203         {
204                 S* dialog = new S (this);
205
206                 if (dialog->ShowModal() == wxID_OK) {
207                         boost::optional<T> const v = dialog->get ();
208                         if (v) {
209                                 add_to_control (v.get ());
210                                 std::vector<T> all = _get ();
211                                 all.push_back (v.get ());
212                                 _set (all);
213                         }
214                 }
215
216                 dialog->Destroy ();
217         }
218
219         void edit_clicked ()
220         {
221                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
222                 if (item == -1) {
223                         return;
224                 }
225
226                 std::vector<T> all = _get ();
227                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
228
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 ();
233                         if (!v) {
234                                 return;
235                         }
236
237                         all[item] = v.get ();
238                 }
239                 dialog->Destroy ();
240
241                 for (size_t i = 0; i < _columns.size(); ++i) {
242                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
243                 }
244
245                 _set (all);
246         }
247
248         void remove_clicked ()
249         {
250                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
251                 if (i == -1) {
252                         return;
253                 }
254
255                 _list->DeleteItem (i);
256                 std::vector<T> all = _get ();
257                 all.erase (all.begin() + i);
258                 _set (all);
259
260                 selection_changed ();
261         }
262
263         void resized (wxSizeEvent& ev)
264         {
265                 int const w = _list->GetSize().GetWidth() - 2;
266
267                 int fixed_width = 0;
268                 int growable = 0;
269                 int j = 0;
270                 for (auto i: _columns) {
271                         fixed_width += i.width.get_value_or (_default_width);
272                         if (!i.growable) {
273                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
274                         } else {
275                                 ++growable;
276                         }
277                         ++j;
278                 }
279
280                 j = 0;
281                 for (auto i: _columns) {
282                         if (i.growable) {
283                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
284                         }
285                         ++j;
286                 }
287
288                 ev.Skip ();
289         }
290
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;
295
296         wxButton* _add;
297         wxButton* _edit;
298         wxButton* _remove;
299         wxListCtrl* _list;
300         wxBoxSizer* _sizer;
301         int _default_width;
302 };
303
304 #endif