*/
-#include "lib/dcpomatic_assert.h"
#include "full_language_tag_dialog.h"
+#include "language_subtag_panel.h"
+#include "subtag_list_ctrl.h"
+#include "lib/dcpomatic_assert.h"
#include <dcp/language_tag.h>
+#include <dcp/warnings.h>
+LIBDCP_DISABLE_WARNINGS
#include <wx/listctrl.h>
#include <wx/srchctrl.h>
#include <wx/wx.h>
+LIBDCP_ENABLE_WARNINGS
#include <boost/algorithm/string.hpp>
#include <boost/bind/bind.hpp>
#include <boost/optional.hpp>
#include <boost/signals2.hpp>
-#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using std::min;
using std::pair;
+using std::shared_ptr;
using std::string;
using std::vector;
-using boost::optional;
-using std::shared_ptr;
using std::weak_ptr;
+using boost::optional;
#if BOOST_VERSION >= 106100
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<dcp::LanguageTag::SubtagData> subtag = optional<dcp::LanguageTag::SubtagData>())
- {
- _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<dcp::LanguageTag::SubtagData> selected_subtag () const
- {
- auto selected = GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
- if (selected == -1) {
- return {};
- }
-
- DCPOMATIC_ASSERT (static_cast<size_t>(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<dcp::LanguageTag::SubtagData> _all_subtags;
- std::vector<dcp::LanguageTag::SubtagData> _matching_subtags;
-};
-
-
-class LanguageSubtagPanel : public wxPanel
-{
-public:
- LanguageSubtagPanel (wxWindow* parent)
- : wxPanel (parent, wxID_ANY)
- {
-#ifdef __WXGTK3__
- int const height = 30;
-#else
- int const height = -1;
-#endif
-
- _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, height));
- _list = new SubtagListCtrl (this);
-
- auto sizer = new wxBoxSizer (wxVERTICAL);
- sizer->Add (_search, 0, wxALL, 8);
- sizer->Add (_list, 1, wxALL, 8);
- SetSizer (sizer);
-
- _search->Bind (wxEVT_TEXT, boost::bind(&LanguageSubtagPanel::search_changed, this));
- _list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
- _list->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind(&LanguageSubtagPanel::selection_changed, this));
- }
-
- void set (dcp::LanguageTag::SubtagType type, string search, optional<dcp::LanguageTag::SubtagData> subtag = optional<dcp::LanguageTag::SubtagData>())
- {
- _list->set (type, search, subtag);
- _search->SetValue (wxString(search));
- }
-
- optional<dcp::LanguageTag::RegionSubtag> get () const
- {
- if (!_list->selected_subtag()) {
- return {};
- }
-
- return dcp::LanguageTag::RegionSubtag(_list->selected_subtag()->subtag);
- }
-
- boost::signals2::signal<void (optional<dcp::LanguageTag::SubtagData>)> SelectionChanged;
- boost::signals2::signal<void (string)> SearchChanged;
-
-private:
- void search_changed ()
- {
- auto search = _search->GetValue();
- _list->set_search (search.ToStdString());
- if (search.Length() > 0 && _list->GetItemCount() > 0) {
- _list->EnsureVisible (0);
- }
- SearchChanged (_search->GetValue().ToStdString());
- }
-
- void selection_changed ()
- {
- SelectionChanged (_list->selected_subtag());
- }
-
- wxSearchCtrl* _search;
- SubtagListCtrl* _list;
-};
-
-
FullLanguageTagDialog::FullLanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
: wxDialog (parent, wxID_ANY, _("Language Tag"), wxDefaultPosition, wxSize(-1, 500))
{
_remove->Enable (selected > 0);
}
-
-RegionSubtagDialog::RegionSubtagDialog (wxWindow* parent, dcp::LanguageTag::RegionSubtag region)
- : wxDialog (parent, wxID_ANY, _("Region"), wxDefaultPosition, wxSize(-1, 500))
- , _panel (new LanguageSubtagPanel (this))
-{
- auto sizer = new wxBoxSizer (wxVERTICAL);
- sizer->Add (_panel, 1);
-
- auto buttons = CreateSeparatedButtonSizer (wxOK);
- if (buttons) {
- sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
- }
-
- SetSizer (sizer);
-
- _panel->set (dcp::LanguageTag::SubtagType::REGION, "", *dcp::LanguageTag::get_subtag_data(region));
-}
-
-
-optional<dcp::LanguageTag::RegionSubtag>
-RegionSubtagDialog::get () const
-{
- return _panel->get ();
-}
-
-