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