Merge master.
[dcpomatic.git] / src / wx / editable_list.h
1 /*
2     Copyright (C) 2012 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                 std::vector<T> current = _get ();
74                 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
75                         add_to_control (*i);
76                 }
77
78                 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
79                 _copy->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::copy_clicked, this));
80                 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
81                 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
82
83                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
84                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
85                 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
86                 selection_changed ();
87
88         }
89
90 private:        
91
92         void add_to_control (T item)
93         {
94                 wxListItem list_item;
95                 int const n = _list->GetItemCount ();
96                 list_item.SetId (n);
97                 _list->InsertItem (list_item);
98
99                 for (int i = 0; i < _columns; ++i) {
100                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
101                 }
102         }
103
104         void selection_changed ()
105         {
106                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
107                 _edit->Enable (i >= 0);
108                 _remove->Enable (i >= 0);
109         }
110
111         void add_clicked ()
112         {
113                 T new_item;
114                 S* dialog = new S (this);
115                 dialog->set (new_item);
116                 dialog->ShowModal ();
117
118                 add_to_control (dialog->get ());
119                 
120                 std::vector<T> all = _get ();
121                 all.push_back (dialog->get ());
122                 _set (all);
123                 
124                 dialog->Destroy ();
125         }
126
127         void copy_clicked ()
128         {
129                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
130                 if (item == -1) {
131                         return;
132                 }
133
134                 std::vector<T> all = _get ();
135                 assert (item >= 0 && item < int (all.size ()));
136
137                 T copy (all[item]);
138                 add_to_control (copy);
139                 
140                 all.push_back (copy);
141                 _set (all);
142         }
143
144         void edit_clicked ()
145         {
146                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
147                 if (item == -1) {
148                         return;
149                 }
150
151                 std::vector<T> all = _get ();
152                 assert (item >= 0 && item < int (all.size ()));
153
154                 S* dialog = new S (this);
155                 dialog->set (all[item]);
156                 dialog->ShowModal ();
157                 all[item] = dialog->get ();
158                 dialog->Destroy ();
159                 
160                 for (int i = 0; i < _columns; ++i) {
161                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
162                 }
163
164                 _set (all);
165         }
166
167         void remove_clicked ()
168         {
169                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
170                 if (i == -1) {
171                         return;
172                 }
173                 
174                 _list->DeleteItem (i);
175                 std::vector<T> all = _get ();
176                 all.erase (all.begin() + i);
177                 _set (all);
178
179                 selection_changed ();
180         }
181
182         void resized (wxSizeEvent& ev)
183         {
184                 int const w = GetSize().GetWidth() / _columns;
185                 for (int i = 0; i < _columns; ++i) {
186                         _list->SetColumnWidth (i, w);
187                 }
188                 ev.Skip ();
189         }
190
191         boost::function <std::vector<T> ()> _get;
192         boost::function <void (std::vector<T>)> _set;
193         int _columns;
194         boost::function<std::string (T, int)> _column;
195
196         wxButton* _add;
197         wxButton* _copy;
198         wxButton* _edit;
199         wxButton* _remove;
200         wxListCtrl* _list;
201 };