From: Carl Hetherington Date: Thu, 24 Nov 2022 22:30:01 +0000 (+0100) Subject: Extract SubtagListCtrl to its own files. X-Git-Tag: v2.16.34~19 X-Git-Url: https://git.carlh.net/gitweb/?p=dcpomatic.git;a=commitdiff_plain;h=62b4857f5906b2a05cab66fb3b14445042d1d8cf Extract SubtagListCtrl to its own files. --- diff --git a/src/wx/full_language_tag_dialog.cc b/src/wx/full_language_tag_dialog.cc index f597e82f7..c181e53fb 100644 --- a/src/wx/full_language_tag_dialog.cc +++ b/src/wx/full_language_tag_dialog.cc @@ -20,6 +20,7 @@ #include "full_language_tag_dialog.h" +#include "subtag_list_ctrl.h" #include "lib/dcpomatic_assert.h" #include #include @@ -49,84 +50,6 @@ using namespace boost::placeholders; #endif -class SubtagListCtrl : public wxListCtrl -{ -public: - SubtagListCtrl (wxWindow* parent) - : wxListCtrl (parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxLC_VIRTUAL) - { - AppendColumn ("", wxLIST_FORMAT_LEFT, 80); - AppendColumn ("", wxLIST_FORMAT_LEFT, 400); - } - - void set (dcp::LanguageTag::SubtagType type, string search, optional subtag = optional()) - { - _all_subtags = dcp::LanguageTag::get_all(type); - set_search (search); - if (subtag) { - auto i = find(_matching_subtags.begin(), _matching_subtags.end(), *subtag); - if (i != _matching_subtags.end()) { - auto item = std::distance(_matching_subtags.begin(), i); - SetItemState (item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); - EnsureVisible (item); - } - } else { - if (GetItemCount() > 0) { - /* The new list sometimes isn't visible without this */ - EnsureVisible (0); - } - } - } - - void set_search (string search) - { - if (search == "") { - _matching_subtags = _all_subtags; - } else { - _matching_subtags.clear (); - - boost::algorithm::to_lower(search); - for (auto const& i: _all_subtags) { - if ( - (boost::algorithm::to_lower_copy(i.subtag).find(search) != string::npos) || - (boost::algorithm::to_lower_copy(i.description).find(search) != string::npos)) { - _matching_subtags.push_back (i); - } - } - } - - SetItemCount (_matching_subtags.size()); - if (GetItemCount() > 0) { - RefreshItems (0, GetItemCount() - 1); - } - } - - optional selected_subtag () const - { - auto selected = GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); - if (selected == -1) { - return {}; - } - - DCPOMATIC_ASSERT (static_cast(selected) < _matching_subtags.size()); - return _matching_subtags[selected]; - } - -private: - wxString OnGetItemText (long item, long column) const override - { - if (column == 0) { - return _matching_subtags[item].subtag; - } else { - return _matching_subtags[item].description; - } - } - - std::vector _all_subtags; - std::vector _matching_subtags; -}; - - class LanguageSubtagPanel : public wxPanel { public: diff --git a/src/wx/subtag_list_ctrl.cc b/src/wx/subtag_list_ctrl.cc new file mode 100644 index 000000000..436b9525d --- /dev/null +++ b/src/wx/subtag_list_ctrl.cc @@ -0,0 +1,108 @@ +/* + Copyright (C) 2020-2021 Carl Hetherington + + 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. + + 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 DCP-o-matic. If not, see . + +*/ + + +#include "subtag_list_ctrl.h" +#include "lib/dcpomatic_assert.h" +#include +#include + + +using std::string; +using boost::optional; + + +SubtagListCtrl::SubtagListCtrl(wxWindow* parent) + : wxListCtrl(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER | wxLC_VIRTUAL) +{ + AppendColumn("", wxLIST_FORMAT_LEFT, 80); + AppendColumn("", wxLIST_FORMAT_LEFT, 400); +} + + +void +SubtagListCtrl::set(dcp::LanguageTag::SubtagType type, string search, optional subtag) +{ + _all_subtags = dcp::LanguageTag::get_all(type); + set_search(search); + if (subtag) { + auto i = find(_matching_subtags.begin(), _matching_subtags.end(), *subtag); + if (i != _matching_subtags.end()) { + auto item = std::distance(_matching_subtags.begin(), i); + SetItemState(item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); + EnsureVisible(item); + } + } else { + if (GetItemCount() > 0) { + /* The new list sometimes isn't visible without this */ + EnsureVisible(0); + } + } +} + + +void +SubtagListCtrl::set_search(string search) +{ + if (search == "") { + _matching_subtags = _all_subtags; + } else { + _matching_subtags.clear(); + + boost::algorithm::to_lower(search); + for (auto const& i: _all_subtags) { + if ( + (boost::algorithm::to_lower_copy(i.subtag).find(search) != string::npos) || + (boost::algorithm::to_lower_copy(i.description).find(search) != string::npos)) { + _matching_subtags.push_back (i); + } + } + } + + SetItemCount(_matching_subtags.size()); + if (GetItemCount() > 0) { + RefreshItems(0, GetItemCount() - 1); + } +} + + +optional +SubtagListCtrl::selected_subtag() const +{ + auto selected = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); + if (selected == -1) { + return {}; + } + + DCPOMATIC_ASSERT(static_cast(selected) < _matching_subtags.size()); + return _matching_subtags[selected]; +} + + +wxString +SubtagListCtrl::OnGetItemText(long item, long column) const +{ + if (column == 0) { + return _matching_subtags[item].subtag; + } else { + return _matching_subtags[item].description; + } +} + diff --git a/src/wx/subtag_list_ctrl.h b/src/wx/subtag_list_ctrl.h new file mode 100644 index 000000000..b1413b361 --- /dev/null +++ b/src/wx/subtag_list_ctrl.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2020-2021 Carl Hetherington + + 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. + + 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 DCP-o-matic. If not, see . + +*/ + + +#include +#include +#include + + +class SubtagListCtrl : public wxListCtrl +{ +public: + SubtagListCtrl(wxWindow* parent); + + void set(dcp::LanguageTag::SubtagType type, std::string search, boost::optional subtag = boost::optional()); + void set_search(std::string search); + boost::optional selected_subtag() const; + +private: + wxString OnGetItemText(long item, long column) const override; + + std::vector _all_subtags; + std::vector _matching_subtags; +}; + + + + diff --git a/src/wx/wscript b/src/wx/wscript index ba51aec57..6bbae6b74 100644 --- a/src/wx/wscript +++ b/src/wx/wscript @@ -141,6 +141,7 @@ sources = """ smpte_metadata_dialog.cc standard_controls.cc static_text.cc + subtag_list_ctrl.cc subtitle_appearance_dialog.cc suspender.cc system_font_dialog.cc