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