No-op; fix GPL address and use the explicit-program-name version.
[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 <wx/wx.h>
26 #include <wx/listctrl.h>
27 #include <boost/function.hpp>
28 #include <vector>
29
30 bool always_valid ();
31
32 /** @param T type of things being edited.
33  *  @param S dialog to edit a thing.
34  */
35 template<class T, class S>
36 class EditableList : public wxPanel
37 {
38 public:
39         EditableList (
40                 wxWindow* parent,
41                 std::vector<std::string> columns,
42                 boost::function<std::vector<T> ()> get,
43                 boost::function<void (std::vector<T>)> set,
44                 boost::function<bool (T)> valid,
45                 boost::function<std::string (T, int)> column,
46                 bool can_edit = true,
47                 bool title = true
48                 )
49                 : wxPanel (parent)
50                 , _get (get)
51                 , _set (set)
52                 , _valid (valid)
53                 , _columns (columns.size ())
54                 , _column (column)
55                 , _edit (0)
56         {
57                 wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
58                 SetSizer (s);
59
60                 _table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
61                 _table->AddGrowableCol (0, 1);
62                 s->Add (_table, 1, wxEXPAND);
63
64                 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
65                 if (title) {
66                         style |= wxLC_NO_HEADER;
67                 }
68                 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (columns.size() * 200, 100), style);
69
70                 for (size_t i = 0; i < columns.size(); ++i) {
71                         wxListItem ip;
72                         ip.SetId (i);
73                         ip.SetText (std_to_wx (columns[i]));
74                         ip.SetWidth (200);
75                         _list->InsertColumn (i, ip);
76                 }
77
78                 _table->Add (_list, 1, wxEXPAND | wxALL);
79
80                 {
81                         wxSizer* s = new wxBoxSizer (wxVERTICAL);
82                         _add = new wxButton (this, wxID_ANY, _("Add..."));
83                         s->Add (_add, 0, wxTOP | wxBOTTOM, 2);
84                         if (can_edit) {
85                                 _edit = new wxButton (this, wxID_ANY, _("Edit..."));
86                                 s->Add (_edit, 0, wxTOP | wxBOTTOM, 2);
87                         }
88                         _remove = new wxButton (this, wxID_ANY, _("Remove"));
89                         s->Add (_remove, 0, wxTOP | wxBOTTOM, 2);
90                         _table->Add (s, 0);
91                 }
92
93                 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
94                 if (_edit) {
95                         _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
96                 }
97                 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
98
99                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
100                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
101                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
102
103                 refresh ();
104                 selection_changed ();
105         }
106
107         void refresh ()
108         {
109                 _list->DeleteAllItems ();
110
111                 std::vector<T> current = _get ();
112                 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
113                         add_to_control (*i);
114                 }
115         }
116
117         boost::optional<T> selection () const
118         {
119                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
120                 if (item == -1) {
121                         return boost::optional<T> ();
122                 }
123
124                 std::vector<T> all = _get ();
125                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
126                 return all[item];
127         }
128
129         void layout ()
130         {
131                 _table->Layout ();
132         }
133
134         boost::signals2::signal<void ()> SelectionChanged;
135
136 private:
137
138         void add_to_control (T item)
139         {
140                 wxListItem list_item;
141                 int const n = _list->GetItemCount ();
142                 list_item.SetId (n);
143                 _list->InsertItem (list_item);
144
145                 for (int i = 0; i < _columns; ++i) {
146                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
147                 }
148         }
149
150         void selection_changed ()
151         {
152                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
153                 if (_edit) {
154                         _edit->Enable (i >= 0);
155                 }
156                 _remove->Enable (i >= 0);
157
158                 SelectionChanged ();
159         }
160
161         void add_clicked ()
162         {
163                 S* dialog = new S (this);
164
165                 if (dialog->ShowModal() == wxID_OK) {
166                         T const v = dialog->get ();
167                         if (_valid (v)) {
168                                 add_to_control (v);
169                                 std::vector<T> all = _get ();
170                                 all.push_back (v);
171                                 _set (all);
172                         }
173                 }
174
175                 dialog->Destroy ();
176         }
177
178         void edit_clicked ()
179         {
180                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
181                 if (item == -1) {
182                         return;
183                 }
184
185                 std::vector<T> all = _get ();
186                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
187
188                 S* dialog = new S (this);
189                 dialog->set (all[item]);
190                 if (dialog->ShowModal() == wxID_OK) {
191                         T const v = dialog->get ();
192                         if (!_valid (v)) {
193                                 return;
194                         }
195
196                         all[item] = v;
197                 }
198                 dialog->Destroy ();
199
200                 for (int i = 0; i < _columns; ++i) {
201                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
202                 }
203
204                 _set (all);
205         }
206
207         void remove_clicked ()
208         {
209                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
210                 if (i == -1) {
211                         return;
212                 }
213
214                 _list->DeleteItem (i);
215                 std::vector<T> all = _get ();
216                 all.erase (all.begin() + i);
217                 _set (all);
218
219                 selection_changed ();
220         }
221
222         void resized (wxSizeEvent& ev)
223         {
224                 int const w = GetSize().GetWidth() / _columns;
225                 for (int i = 0; i < _columns; ++i) {
226                         _list->SetColumnWidth (i, w);
227                 }
228                 ev.Skip ();
229         }
230
231         boost::function <std::vector<T> ()> _get;
232         boost::function <void (std::vector<T>)> _set;
233         boost::function <bool (T)> _valid;
234         int _columns;
235         boost::function<std::string (T, int)> _column;
236
237         wxButton* _add;
238         wxButton* _edit;
239         wxButton* _remove;
240         wxListCtrl* _list;
241         wxFlexGridSizer* _table;
242 };
243
244 #endif