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