Very basics of colour conversion configuration.
[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<boost::shared_ptr<T> > ()> get,
30                 boost::function<void (std::vector<boost::shared_ptr<T> >)> set,
31                 boost::function<std::string (boost::shared_ptr<T>, int)> column
32                 )
33                 : wxPanel (parent)
34                 , _get (get)
35                 , _set (set)
36                 , _columns (columns.size ())
37                 , _column (column)
38         {
39                 wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
40                 SetSizer (s);
41
42                 wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
43                 table->AddGrowableCol (0, 1);
44                 s->Add (table, 1, wxALL | wxEXPAND, 8);
45
46                 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (columns.size() * 200, 100), wxLC_REPORT | wxLC_SINGLE_SEL);
47
48                 for (size_t i = 0; i < columns.size(); ++i) {
49                         wxListItem ip;
50                         ip.SetId (i);
51                         ip.SetText (std_to_wx (columns[i]));
52                         ip.SetWidth (200);
53                         _list->InsertColumn (i, ip);
54                 }
55
56                 table->Add (_list, 1, wxEXPAND | wxALL);
57
58                 {
59                         wxSizer* s = new wxBoxSizer (wxVERTICAL);
60                         _add = new wxButton (this, wxID_ANY, _("Add..."));
61                         s->Add (_add, 0, wxTOP | wxBOTTOM, 2);
62                         _edit = new wxButton (this, wxID_ANY, _("Edit..."));
63                         s->Add (_edit, 0, wxTOP | wxBOTTOM, 2);
64                         _remove = new wxButton (this, wxID_ANY, _("Remove"));
65                         s->Add (_remove, 0, wxTOP | wxBOTTOM, 2);
66                         table->Add (s, 0);
67                 }
68
69                 std::vector<boost::shared_ptr<T> > current = _get ();
70                 for (typename std::vector<boost::shared_ptr<T> >::iterator i = current.begin (); i != current.end(); ++i) {
71                         add_to_control (*i);
72                 }
73
74                 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_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                 selection_changed ();
81         }
82
83         void add_to_control (boost::shared_ptr<T> item)
84         {
85                 wxListItem list_item;
86                 int const n = _list->GetItemCount ();
87                 list_item.SetId (n);
88                 _list->InsertItem (list_item);
89
90                 for (int i = 0; i < _columns; ++i) {
91                         _list->SetItem (n, i, std_to_wx (_column (item, i)));
92                 }
93         }
94
95         void selection_changed ()
96         {
97                 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
98                 _edit->Enable (i >= 0);
99                 _remove->Enable (i >= 0);
100         }
101
102         void add_clicked ()
103         {
104                 boost::shared_ptr<T> new_item (new T);
105                 S* dialog = new S (this, new_item);
106                 dialog->ShowModal ();
107                 dialog->Destroy ();
108
109                 add_to_control (new_item);
110                 std::vector<boost::shared_ptr<T> > all = _get ();
111                 all.push_back (new_item);
112                 _set (all);
113         }
114
115         void edit_clicked ()
116         {
117                 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
118                 if (item == -1) {
119                         return;
120                 }
121
122                 std::vector<boost::shared_ptr<T> > all = _get ();
123                 assert (item >= 0 && item < int (all.size ()));
124
125                 S* dialog = new S (this, all[item]);
126                 dialog->ShowModal ();
127                 dialog->Destroy ();
128
129                 for (int i = 0; i < _columns; ++i) {
130                         _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
131                 }
132         }
133
134         void remove_clicked ()
135         {
136                 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
137                 if (i >= 0) {
138                         _list->DeleteItem (i);
139                 }
140                 
141                 std::vector<boost::shared_ptr<T> > all = _get ();
142                 all.erase (all.begin() + i);
143                 _set (all);
144         }
145
146 private:        
147         boost::function <std::vector<boost::shared_ptr<T> > ()> _get;
148         boost::function <void (std::vector<boost::shared_ptr<T> >)> _set;
149         int _columns;
150         boost::function<std::string (boost::shared_ptr<T>, int)> _column;
151
152         wxButton* _add;
153         wxButton* _edit;
154         wxButton* _remove;
155         wxListCtrl* _list;
156 };