X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fwx%2Feditable_list.h;h=4b203c0e2a4c802a75d5ac9cf580fde5fcbefce6;hb=3828baf56467224f5d44049bf1e7a7ed11f43a05;hp=f895e4a0a3c0669adeb873ea18579a3ceeea9a0b;hpb=3a67f024a614687a811ecb5c96a56fc664f04c3a;p=dcpomatic.git diff --git a/src/wx/editable_list.h b/src/wx/editable_list.h index f895e4a0a..4b203c0e2 100644 --- a/src/wx/editable_list.h +++ b/src/wx/editable_list.h @@ -1,19 +1,20 @@ /* - Copyright (C) 2012-2015 Carl Hetherington + Copyright (C) 2012-2016 Carl Hetherington - This program is free software; you can redistribute it and/or modify + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - This program is distributed in the hope that it will be useful, + DCP-o-matic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with DCP-o-matic. If not, see . */ @@ -26,6 +27,8 @@ #include #include +bool always_valid (); + /** @param T type of things being edited. * @param S dialog to edit a thing. */ @@ -38,12 +41,15 @@ public: std::vector columns, boost::function ()> get, boost::function)> set, + boost::function valid, boost::function column, - bool can_edit = true + bool can_edit = true, + bool title = true ) : wxPanel (parent) , _get (get) , _set (set) + , _valid (valid) , _columns (columns.size ()) , _column (column) , _edit (0) @@ -51,11 +57,15 @@ public: wxBoxSizer* s = new wxBoxSizer (wxVERTICAL); SetSizer (s); - wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP); - table->AddGrowableCol (0, 1); - s->Add (table, 1, wxEXPAND); + _table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP); + _table->AddGrowableCol (0, 1); + s->Add (_table, 1, wxEXPAND); - _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (columns.size() * 200, 100), wxLC_REPORT | wxLC_SINGLE_SEL); + long style = wxLC_REPORT | wxLC_SINGLE_SEL; + if (title) { + style |= wxLC_NO_HEADER; + } + _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (columns.size() * 200, 100), style); for (size_t i = 0; i < columns.size(); ++i) { wxListItem ip; @@ -65,7 +75,7 @@ public: _list->InsertColumn (i, ip); } - table->Add (_list, 1, wxEXPAND | wxALL); + _table->Add (_list, 1, wxEXPAND | wxALL); { wxSizer* s = new wxBoxSizer (wxVERTICAL); @@ -77,7 +87,7 @@ public: } _remove = new wxButton (this, wxID_ANY, _("Remove")); s->Add (_remove, 0, wxTOP | wxBOTTOM, 2); - table->Add (s, 0); + _table->Add (s, 0); } _add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&EditableList::add_clicked, this)); @@ -104,6 +114,25 @@ public: } } + boost::optional selection () const + { + int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); + if (item == -1) { + return boost::optional (); + } + + std::vector all = _get (); + DCPOMATIC_ASSERT (item >= 0 && item < int (all.size ())); + return all[item]; + } + + void layout () + { + _table->Layout (); + } + + boost::signals2::signal SelectionChanged; + private: void add_to_control (T item) @@ -125,19 +154,22 @@ private: _edit->Enable (i >= 0); } _remove->Enable (i >= 0); + + SelectionChanged (); } void add_clicked () { - T new_item; S* dialog = new S (this); - dialog->set (new_item); if (dialog->ShowModal() == wxID_OK) { - add_to_control (dialog->get ()); - std::vector all = _get (); - all.push_back (dialog->get ()); - _set (all); + T const v = dialog->get (); + if (_valid (v)) { + add_to_control (v); + std::vector all = _get (); + all.push_back (v); + _set (all); + } } dialog->Destroy (); @@ -156,7 +188,12 @@ private: S* dialog = new S (this); dialog->set (all[item]); if (dialog->ShowModal() == wxID_OK) { - all[item] = dialog->get (); + T const v = dialog->get (); + if (!_valid (v)) { + return; + } + + all[item] = v; } dialog->Destroy (); @@ -193,6 +230,7 @@ private: boost::function ()> _get; boost::function )> _set; + boost::function _valid; int _columns; boost::function _column; @@ -200,6 +238,7 @@ private: wxButton* _edit; wxButton* _remove; wxListCtrl* _list; + wxFlexGridSizer* _table; }; #endif