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