Merge.
[dcpomatic.git] / src / wx / editable_list.h
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef DCPOMATIC_EDITABLE_LIST_H
21 #define DCPOMATIC_EDITABLE_LIST_H
22
23 #include "wx_util.h"
24 #include <wx/wx.h>
25 #include <wx/listctrl.h>
26 #include <boost/function.hpp>
27 #include <vector>
28
29 /** @param T type of things being edited.
30  *  @param S dialog to edit a thing.
31  */
32 template<class T, class S>
33 class EditableList : public wxPanel
34 {
35 public:
36         EditableList (
37                 wxWindow* parent,
38                 std::vector<std::string> columns,
39                 boost::function<std::vector<T> ()> get,
40                 boost::function<void (std::vector<T>)> set,
41                 boost::function<std::string (T, int)> column,
42                 bool can_edit = true,
43                 bool title = true
44                 )
45                 : wxPanel (parent)
46                 , _get (get)
47                 , _set (set)
48                 , _columns (columns.size ())
49                 , _column (column)
50                 , _edit (0)
51         {
52                 wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
53                 SetSizer (s);
54
55                 wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
56                 table->AddGrowableCol (0, 1);
57                 s->Add (table, 1, wxEXPAND);
58
59                 long style = wxLC_REPORT | wxLC_SINGLE_SEL;
60                 if (title) {
61                         style |= wxLC_NO_HEADER;
62                 }
63                 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (columns.size() * 200, 100), style);
64
65                 for (size_t i = 0; i < columns.size(); ++i) {
66                         wxListItem ip;
67                         ip.SetId (i);
68                         ip.SetText (std_to_wx (columns[i]));
69                         ip.SetWidth (200);
70                         _list->InsertColumn (i, ip);
71                 }
72
73                 table->Add (_list, 1, wxEXPAND | wxALL);
74
75                 {
76                         wxSizer* s = new wxBoxSizer (wxVERTICAL);
77                         _add = new wxButton (this, wxID_ANY, _("Add..."));
78                         s->Add (_add, 0, wxTOP | wxBOTTOM, 2);
79                         if (can_edit) {
80                                 _edit = new wxButton (this, wxID_ANY, _("Edit..."));
81                                 s->Add (_edit, 0, wxTOP | wxBOTTOM, 2);
82                         }
83                         _remove = new wxButton (this, wxID_ANY, _("Remove"));
84                         s->Add (_remove, 0, wxTOP | wxBOTTOM, 2);
85                         table->Add (s, 0);
86                 }
87
88                 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
89                 if (_edit) {
90                         _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
91                 }
92                 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
93
94                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
95                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
96                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
97
98                 refresh ();
99                 selection_changed ();
100         }
101
102         void refresh ()
103         {
104                 _list->DeleteAllItems ();
105
106                 std::vector<T> current = _get ();
107                 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
108                         add_to_control (*i);
109                 }
110         }
111
112         boost::optional<T> selection () const
113         {
114                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
115                 if (item == -1) {
116                         return boost::optional<T> ();
117                 }
118
119                 std::vector<T> all = _get ();
120                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
121                 return all[item];
122         }
123
124         boost::signals2::signal<void ()> SelectionChanged;
125
126 private:
127
128         void add_to_control (T item)
129         {
130                 wxListItem list_item;
131                 int const n = _list->GetItemCount ();
132                 list_item.SetId (n);
133                 _list->InsertItem (list_item);
134
135                 for (int i = 0; i < _columns; ++i) {
136                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
137                 }
138         }
139
140         void selection_changed ()
141         {
142                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
143                 if (_edit) {
144                         _edit->Enable (i >= 0);
145                 }
146                 _remove->Enable (i >= 0);
147
148                 SelectionChanged ();
149         }
150
151         void add_clicked ()
152         {
153                 S* dialog = new S (this);
154
155                 if (dialog->ShowModal() == wxID_OK) {
156                         add_to_control (dialog->get ());
157                         std::vector<T> all = _get ();
158                         all.push_back (dialog->get ());
159                         _set (all);
160                 }
161
162                 dialog->Destroy ();
163         }
164
165         void edit_clicked ()
166         {
167                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
168                 if (item == -1) {
169                         return;
170                 }
171
172                 std::vector<T> all = _get ();
173                 DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ()));
174
175                 S* dialog = new S (this);
176                 dialog->set (all[item]);
177                 if (dialog->ShowModal() == wxID_OK) {
178                         all[item] = dialog->get ();
179                 }
180                 dialog->Destroy ();
181
182                 for (int i = 0; i < _columns; ++i) {
183                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
184                 }
185
186                 _set (all);
187         }
188
189         void remove_clicked ()
190         {
191                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
192                 if (i == -1) {
193                         return;
194                 }
195
196                 _list->DeleteItem (i);
197                 std::vector<T> all = _get ();
198                 all.erase (all.begin() + i);
199                 _set (all);
200
201                 selection_changed ();
202         }
203
204         void resized (wxSizeEvent& ev)
205         {
206                 int const w = GetSize().GetWidth() / _columns;
207                 for (int i = 0; i < _columns; ++i) {
208                         _list->SetColumnWidth (i, w);
209                 }
210                 ev.Skip ();
211         }
212
213         boost::function <std::vector<T> ()> _get;
214         boost::function <void (std::vector<T>)> _set;
215         int _columns;
216         boost::function<std::string (T, int)> _column;
217
218         wxButton* _add;
219         wxButton* _edit;
220         wxButton* _remove;
221         wxListCtrl* _list;
222 };
223
224 #endif