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