Include subscribers / supporters in git.
[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 <wx/listctrl.h>
27 #include <wx/wx.h>
28 #include <dcp/language_tag.h>
29
30
31 using std::vector;
32
33
34 LanguageTagDialog::LanguageTagDialog (wxWindow* parent, dcp::LanguageTag tag)
35         : wxDialog (parent, wxID_ANY, _("Language Tag"))
36 {
37         _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize(600, 700), wxLC_REPORT | wxLC_SINGLE_SEL | wxLC_NO_HEADER);
38         _list->AppendColumn ("", wxLIST_FORMAT_LEFT, 400);
39         _list->AppendColumn ("", wxLIST_FORMAT_LEFT, 150);
40         auto add = new wxButton (this, wxID_ANY, _("Add language..."));
41
42         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
43         overall_sizer->Add (_list, 0, wxALL, DCPOMATIC_SIZER_GAP);
44         overall_sizer->Add (add, 0, wxALL, DCPOMATIC_SIZER_GAP);
45
46         auto buttons = CreateSeparatedButtonSizer (wxOK);
47         if (buttons) {
48                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
49         }
50
51         SetSizerAndFit (overall_sizer);
52
53         for (auto const& i: dcp::dcnc_tags()) {
54                 _presets.push_back (dcp::LanguageTag(i.first));
55         }
56
57         std::sort (_presets.begin(), _presets.end(), [](dcp::LanguageTag const& i, dcp::LanguageTag const& j) {
58                 return i.description() < j.description();
59         });
60
61         _custom = Config::instance()->custom_languages ();
62
63         populate_list ();
64
65         set (tag);
66
67         add->Bind (wxEVT_BUTTON, boost::bind(&LanguageTagDialog::add_language, this));
68 }
69
70
71 void
72 LanguageTagDialog::add_language ()
73 {
74         auto full = new FullLanguageTagDialog (GetParent());
75         auto r = full->ShowModal ();
76         if (r == wxID_OK) {
77                 Config::instance()->add_custom_language (full->get());
78                 set (full->get());
79         }
80         full->Destroy ();
81 }
82
83
84 void
85 LanguageTagDialog::populate_list ()
86 {
87         _list->DeleteAllItems ();
88
89         auto add = [this](vector<dcp::LanguageTag> const& tags) {
90                 for (auto const& i: tags) {
91                         wxListItem it;
92                         it.SetId (_list->GetItemCount());
93                         it.SetColumn (0);
94                         it.SetText (std_to_wx(i.description()));
95                         _list->InsertItem (it);
96                         it.SetColumn (1);
97                         it.SetText (std_to_wx(i.to_string()));
98                         _list->SetItem (it);
99                 }
100         };
101
102         add (_presets);
103         add (_custom);
104 }
105
106
107 void
108 LanguageTagDialog::set (dcp::LanguageTag tag)
109 {
110         size_t selection = 0;
111
112         auto iter = find(_presets.begin(), _presets.end(), tag);
113         if (iter == _presets.end()) {
114                 iter = find(_custom.begin(), _custom.end(), tag);
115                 if (iter == _custom.end()) {
116                         _custom.push_back (tag);
117                         selection = _presets.size() + _custom.size() - 1;
118                         populate_list ();
119                         if (_list->GetItemCount() > 0) {
120                                 _list->EnsureVisible (_list->GetItemCount() - 1);
121                         }
122                 } else {
123                         selection = _presets.size() + std::distance(_custom.begin(), iter);
124                 }
125         } else {
126                 selection = std::distance(_presets.begin(), iter);
127         }
128
129         _list->SetItemState (selection, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
130         if (static_cast<int>(selection) < _list->GetItemCount()) {
131                 _list->EnsureVisible (selection);
132         }
133 }
134
135
136 dcp::LanguageTag
137 LanguageTagDialog::get () const
138 {
139         auto selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
140         DCPOMATIC_ASSERT (selected >= 0);
141
142         if (selected < static_cast<long>(_presets.size())) {
143                 return _presets[selected];
144         }
145
146         selected -= _presets.size();
147
148         DCPOMATIC_ASSERT (selected < static_cast<long>(_custom.size()));
149         return _custom[selected];
150 }
151