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