2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include <wx/listctrl.h>
23 template<class T, class S>
24 class EditableList : public wxPanel
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,
38 , _columns (columns.size ())
41 wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
44 wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
45 table->AddGrowableCol (0, 1);
46 s->Add (table, 1, wxALL | wxEXPAND, 8);
48 _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (columns.size() * 200, height), wxLC_REPORT | wxLC_SINGLE_SEL);
50 for (size_t i = 0; i < columns.size(); ++i) {
53 ip.SetText (std_to_wx (columns[i]));
55 _list->InsertColumn (i, ip);
58 table->Add (_list, 1, wxEXPAND | wxALL);
61 wxSizer* s = new wxBoxSizer (wxVERTICAL);
62 _add = new wxButton (this, wxID_ANY, _("Add..."));
63 s->Add (_add, 0, wxTOP | wxBOTTOM, 2);
64 _edit = new wxButton (this, wxID_ANY, _("Edit..."));
65 s->Add (_edit, 0, wxTOP | wxBOTTOM, 2);
66 _remove = new wxButton (this, wxID_ANY, _("Remove"));
67 s->Add (_remove, 0, wxTOP | wxBOTTOM, 2);
71 std::vector<T> current = _get ();
72 for (typename std::vector<T>::iterator i = current.begin (); i != current.end(); ++i) {
76 _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this));
77 _edit->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::edit_clicked, this));
78 _remove->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::remove_clicked, this));
80 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&EditableList::selection_changed, this));
81 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&EditableList::selection_changed, this));
82 _list->Bind (wxEVT_SIZE, boost::bind (&EditableList::resized, this, _1));
89 void add_to_control (T item)
92 int const n = _list->GetItemCount ();
94 _list->InsertItem (list_item);
96 for (int i = 0; i < _columns; ++i) {
97 _list->SetItem (n, i, std_to_wx (_column (item, i)));
101 void selection_changed ()
103 int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
104 _edit->Enable (i >= 0);
105 _remove->Enable (i >= 0);
111 S* dialog = new S (this);
112 dialog->set (new_item);
113 dialog->ShowModal ();
115 add_to_control (dialog->get ());
117 std::vector<T> all = _get ();
118 all.push_back (dialog->get ());
126 int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
131 std::vector<T> all = _get ();
132 assert (item >= 0 && item < int (all.size ()));
134 S* dialog = new S (this);
135 dialog->set (all[item]);
136 dialog->ShowModal ();
137 all[item] = dialog->get ();
140 for (int i = 0; i < _columns; ++i) {
141 _list->SetItem (item, i, std_to_wx (_column (all[item], i)));
147 void remove_clicked ()
149 int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
154 _list->DeleteItem (i);
155 std::vector<T> all = _get ();
156 all.erase (all.begin() + i);
159 selection_changed ();
162 void resized (wxSizeEvent& ev)
164 int const w = GetSize().GetWidth() / _columns;
165 for (int i = 0; i < _columns; ++i) {
166 _list->SetColumnWidth (i, w);
171 boost::function <std::vector<T> ()> _get;
172 boost::function <void (std::vector<T>)> _set;
174 boost::function<std::string (T, int)> _column;