ca58009a4b7059027700b9fde7181380742f26a1
[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 (std::string name_)
35                 : name (name_)
36                 , growable (false)
37         {}
38
39         EditableListColumn (std::string name_, boost::optional<int> width_, bool growable_)
40                 : name (name_)
41                 , width (width_)
42                 , growable (growable_)
43         {}
44
45         std::string 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 (std_to_wx(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, 0, wxTOP | wxBOTTOM, 2);
122                         if (can_edit) {
123                                 _edit = new Button (this, _("Edit..."));
124                                 s->Add (_edit, 0, wxTOP | wxBOTTOM, 2);
125                         }
126                         _remove = new Button (this, _("Remove"));
127                         s->Add (_remove, 0, wxTOP | wxBOTTOM, 2);
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                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
140
141                 refresh ();
142                 selection_changed ();
143         }
144
145         void refresh ()
146         {
147                 _list->DeleteAllItems ();
148
149                 std::vector<T> current = _get ();
150                 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
151                         add_to_control (*i);
152                 }
153         }
154
155         boost::optional<T> selection () const
156         {
157                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
158                 if (item == -1) {
159                         return boost::optional<T> ();
160                 }
161
162                 std::vector<T> all = _get ();
163                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
164                 return all[item];
165         }
166
167         void layout ()
168         {
169                 _sizer->Layout ();
170         }
171
172         boost::signals2::signal<void ()> SelectionChanged;
173
174 private:
175
176         void add_to_control (T item)
177         {
178                 wxListItem list_item;
179                 int const n = _list->GetItemCount ();
180                 list_item.SetId (n);
181                 _list->InsertItem (list_item);
182
183                 for (size_t i = 0; i < _columns.size(); ++i) {
184                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
185                 }
186         }
187
188         void selection_changed ()
189         {
190                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
191                 if (_edit) {
192                         _edit->Enable (i >= 0);
193                 }
194                 _remove->Enable (i >= 0);
195
196                 SelectionChanged ();
197         }
198
199         void add_clicked ()
200         {
201                 S* dialog = new S (this);
202
203                 if (dialog->ShowModal() == wxID_OK) {
204                         boost::optional<T> const v = dialog->get ();
205                         if (v) {
206                                 add_to_control (v.get ());
207                                 std::vector<T> all = _get ();
208                                 all.push_back (v.get ());
209                                 _set (all);
210                         }
211                 }
212
213                 dialog->Destroy ();
214         }
215
216         void edit_clicked ()
217         {
218                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
219                 if (item == -1) {
220                         return;
221                 }
222
223                 std::vector<T> all = _get ();
224                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
225
226                 S* dialog = new S (this);
227                 dialog->set (all[item]);
228                 if (dialog->ShowModal() == wxID_OK) {
229                         boost::optional<T> const v = dialog->get ();
230                         if (!v) {
231                                 return;
232                         }
233
234                         all[item] = v.get ();
235                 }
236                 dialog->Destroy ();
237
238                 for (size_t i = 0; i < _columns.size(); ++i) {
239                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
240                 }
241
242                 _set (all);
243         }
244
245         void remove_clicked ()
246         {
247                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
248                 if (i == -1) {
249                         return;
250                 }
251
252                 _list->DeleteItem (i);
253                 std::vector<T> all = _get ();
254                 all.erase (all.begin() + i);
255                 _set (all);
256
257                 selection_changed ();
258         }
259
260         void resized (wxSizeEvent& ev)
261         {
262                 int const w = _list->GetSize().GetWidth() - 2;
263
264                 int fixed_width = 0;
265                 int growable = 0;
266                 int j = 0;
267                 BOOST_FOREACH (EditableListColumn i, _columns) {
268                         fixed_width += i.width.get_value_or (_default_width);
269                         if (!i.growable) {
270                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width));
271                         } else {
272                                 ++growable;
273                         }
274                         ++j;
275                 }
276
277                 j = 0;
278                 BOOST_FOREACH (EditableListColumn i, _columns) {
279                         if (i.growable) {
280                                 _list->SetColumnWidth (j, i.width.get_value_or(_default_width) + (w - fixed_width) / growable);
281                         }
282                         ++j;
283                 }
284
285                 ev.Skip ();
286         }
287
288         boost::function <std::vector<T> ()> _get;
289         boost::function <void (std::vector<T>)> _set;
290         std::vector<EditableListColumn> _columns;
291         boost::function<std::string (T, int)> _column;
292
293         wxButton* _add;
294         wxButton* _edit;
295         wxButton* _remove;
296         wxListCtrl* _list;
297         wxBoxSizer* _sizer;
298         int _default_width;
299 };
300
301 #endif