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