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