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