56c3a946379cedfa99ef19da832327eda7597291
[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                 wxPanel* 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                 wxBoxSizer* 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                 std::vector<T> current = _get ();
159                 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
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 boost::optional<T> ();
169                 }
170
171                 std::vector<T> 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                         boost::optional<T> const v = dialog->get ();
214                         if (v) {
215                                 add_to_control (v.get ());
216                                 std::vector<T> all = _get ();
217                                 all.push_back (v.get ());
218                                 _set (all);
219                         }
220                 }
221
222                 dialog->Destroy ();
223         }
224
225         void edit_clicked ()
226         {
227                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
228                 if (item == -1) {
229                         return;
230                 }
231
232                 std::vector<T> all = _get ();
233                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
234
235                 S* dialog = new S (this);
236                 dialog->set (all[item]);
237                 if (dialog->ShowModal() == wxID_OK) {
238                         boost::optional<T> const v = dialog->get ();
239                         if (!v) {
240                                 return;
241                         }
242
243                         all[item] = v.get ();
244                 }
245                 dialog->Destroy ();
246
247                 for (size_t i = 0; i < _columns.size(); ++i) {
248                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
249                 }
250
251                 _set (all);
252         }
253
254         void remove_clicked ()
255         {
256                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
257                 if (i == -1) {
258                         return;
259                 }
260
261                 _list->DeleteItem (i);
262                 std::vector<T> all = _get ();
263                 all.erase (all.begin() + i);
264                 _set (all);
265
266                 selection_changed ();
267         }
268
269         void resized (wxSizeEvent& ev)
270         {
271                 int const w = _list->GetSize().GetWidth() - 2;
272
273                 int fixed_width = 0;
274                 int growable = 0;
275                 int j = 0;
276                 for (auto i: _columns) {
277                         fixed_width += i.width.get_value_or (_default_width);
278                         if (!i.growable) {
279                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
280                         } else {
281                                 ++growable;
282                         }
283                         ++j;
284                 }
285
286                 j = 0;
287                 for (auto i: _columns) {
288                         if (i.growable) {
289                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
290                         }
291                         ++j;
292                 }
293
294                 ev.Skip ();
295         }
296
297         std::function <std::vector<T> ()> _get;
298         std::function <void (std::vector<T>)> _set;
299         std::vector<EditableListColumn> _columns;
300         std::function<std::string (T, int)> _column;
301
302         wxButton* _add;
303         wxButton* _edit;
304         wxButton* _remove;
305         wxListCtrl* _list;
306         wxBoxSizer* _sizer;
307         int _default_width;
308 };
309
310 #endif