Supporters update.
[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         auto full = new FullLanguageTagDialog (GetParent());
79         auto r = full->ShowModal ();
80         if (r == wxID_OK) {
81                 Config::instance()->add_custom_language (full->get());
82                 set (full->get());
83         }
84         full->Destroy ();
85 }
86
87
88 void
89 LanguageTagDialog::populate_list ()
90 {
91         _list->DeleteAllItems ();
92
93         auto add = [this](vector<dcp::LanguageTag> const& tags) {
94                 for (auto const& i: tags) {
95                         wxListItem it;
96                         it.SetId (_list->GetItemCount());
97                         it.SetColumn (0);
98                         it.SetText (std_to_wx(i.description()));
99                         _list->InsertItem (it);
100                         it.SetColumn (1);
101                         it.SetText (std_to_wx(i.to_string()));
102                         _list->SetItem (it);
103                 }
104         };
105
106         add (_presets);
107         add (_custom);
108 }
109
110
111 void
112 LanguageTagDialog::set (dcp::LanguageTag tag)
113 {
114         size_t selection = 0;
115
116         auto iter = find(_presets.begin(), _presets.end(), tag);
117         if (iter == _presets.end()) {
118                 iter = find(_custom.begin(), _custom.end(), tag);
119                 if (iter == _custom.end()) {
120                         _custom.push_back (tag);
121                         selection = _presets.size() + _custom.size() - 1;
122                         populate_list ();
123                         if (_list->GetItemCount() > 0) {
124                                 _list->EnsureVisible (_list->GetItemCount() - 1);
125                         }
126                 } else {
127                         selection = _presets.size() + std::distance(_custom.begin(), iter);
128                 }
129         } else {
130                 selection = std::distance(_presets.begin(), iter);
131         }
132
133         _list->SetItemState (selection, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
134         if (static_cast<int>(selection) < _list->GetItemCount()) {
135                 _list->EnsureVisible (selection);
136         }
137 }
138
139
140 dcp::LanguageTag
141 LanguageTagDialog::get () const
142 {
143         auto selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
144         DCPOMATIC_ASSERT (selected >= 0);
145
146         if (selected < static_cast<long>(_presets.size())) {
147                 return _presets[selected];
148         }
149
150         selected -= _presets.size();
151
152         DCPOMATIC_ASSERT (selected < static_cast<long>(_custom.size()));
153         return _custom[selected];
154 }
155