Missed update to private test repo version.
[dcpomatic.git] / src / wx / language_tag_dialog.cc
1 /*
2     Copyright (C) 2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "full_language_tag_dialog.h"
23 #include "language_tag_dialog.h"
24 #include "wx_util.h"
25 #include "lib/config.h"
26 #include <dcp/language_tag.h>
27 #include <dcp/warnings.h>
28 LIBDCP_DISABLE_WARNINGS
29 #include <wx/listctrl.h>
30 #include <wx/wx.h>
31 LIBDCP_ENABLE_WARNINGS
32
33
34 using std::vector;
35
36
37 LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
38         : wxDialog (parent, wxID_ANY, _("Language Tag"))
39 {
40         _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(600, 700), wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER);
41         _list->AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
42         _list->AppendColumn ("", wxLIST_FORMAT_LEFT, 150);
43         auto add = new wxButton (this, wxID_ANY, _("Add language..."));
44
45         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
46         overall_sizer->Add (_list, 0, wxALL, DCPOMATIC_SIZER_GAP);
47         overall_sizer->Add (add, 0, wxALL, DCPOMATIC_SIZER_GAP);
48
49         auto buttons = CreateSeparatedButtonSizer(wxOK | wxCANCEL);
50         if (buttons) {
51                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
52         }
53
54         SetSizerAndFit (overall_sizer);
55
56         for (auto const& i: dcp::dcnc_tags()) {
57                 _presets.push_back (dcp::LanguageTag(i.first));
58         }
59
60         std::sort (_presets.begin(), _presets.end(), [](dcp::LanguageTag const& i, dcp::LanguageTag const& j) {
61                 return i.description() < j.description();
62         });
63
64         _custom = Config::instance()->custom_languages ();
65
66         populate_list ();
67
68         set (tag);
69
70         add->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_language, this));
71         _list->Bind(wxEVT_LIST_ITEM_ACTIVATED, boost::bind(&LanguageTagDialog::EndModal, this, wxID_OK));
72 }
73
74
75 void
76 LanguageTagDialog::add_language ()
77 {
78         FullLanguageTagDialog full(GetParent());
79         if (full.ShowModal() == wxID_OK) {
80                 Config::instance()->add_custom_language(full.get());
81                 set(full.get());
82         }
83 }
84
85
86 void
87 LanguageTagDialog::populate_list ()
88 {
89         _list->DeleteAllItems ();
90
91         auto add = [this](vector<dcp::LanguageTag> const& tags) {
92                 for (auto const& i: tags) {
93                         wxListItem it;
94                         it.SetId (_list->GetItemCount());
95                         it.SetColumn (0);
96                         it.SetText (std_to_wx(i.description()));
97                         _list->InsertItem (it);
98                         it.SetColumn (1);
99                         it.SetText (std_to_wx(i.to_string()));
100                         _list->SetItem (it);
101                 }
102         };
103
104         add (_presets);
105         add (_custom);
106 }
107
108
109 void
110 LanguageTagDialog::set (dcp::LanguageTag tag)
111 {
112         size_t selection = 0;
113
114         auto iter = find(_presets.begin(), _presets.end(), tag);
115         if (iter == _presets.end()) {
116                 iter = find(_custom.begin(), _custom.end(), tag);
117                 if (iter == _custom.end()) {
118                         _custom.push_back (tag);
119                         selection = _presets.size() + _custom.size() - 1;
120                         populate_list ();
121                         if (_list->GetItemCount() > 0) {
122                                 _list->EnsureVisible (_list->GetItemCount() - 1);
123                         }
124                 } else {
125                         selection = _presets.size() + std::distance(_custom.begin(), iter);
126                 }
127         } else {
128                 selection = std::distance(_presets.begin(), iter);
129         }
130
131         _list->SetItemState (selection, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
132         if (static_cast<int>(selection) < _list->GetItemCount()) {
133                 _list->EnsureVisible (selection);
134         }
135 }
136
137
138 dcp::LanguageTag
139 LanguageTagDialog::get () const
140 {
141         auto selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
142         DCPOMATIC_ASSERT (selected >= 0);
143
144         if (selected < static_cast<long>(_presets.size())) {
145                 return _presets[selected];
146         }
147
148         selected -= _presets.size();
149
150         DCPOMATIC_ASSERT (selected < static_cast<long>(_custom.size()));
151         return _custom[selected];
152 }
153