54c6d9d4303f98f082c52d20659a0c9b71259cfc
[dcpomatic.git] / src / wx / region_subtag_widget.cc
1 /*
2     Copyright (C) 2020-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 "dcpomatic_button.h"
23 #include "full_language_tag_dialog.h"
24 #include "region_subtag_dialog.h"
25 #include "region_subtag_widget.h"
26 #include "wx_ptr.h"
27 #include "wx_util.h"
28 #include "lib/scope_guard.h"
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <wx/wx.h>
32 LIBDCP_ENABLE_WARNINGS
33
34
35 using boost::optional;
36
37
38 RegionSubtagWidget::RegionSubtagWidget(wxWindow* parent, wxString tooltip, optional<dcp::LanguageTag::RegionSubtag> tag, optional<wxString> size_to_fit)
39         : _parent(parent)
40         , _sizer(new wxBoxSizer(wxHORIZONTAL))
41 {
42         _region = new wxStaticText(parent, wxID_ANY, wxT(""), wxDefaultPosition, wxDefaultSize, wxST_ELLIPSIZE_END);
43         _region->SetToolTip(tooltip);
44         set(tag);
45
46         if (size_to_fit) {
47                 int w;
48                 int h;
49                 _region->GetTextExtent(*size_to_fit, &w, &h);
50                 _region->SetMinSize(wxSize(w, -1));
51         }
52
53         _sizer->Add(_region, 1, wxLEFT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_X_GAP);
54         _edit = new Button(parent, _("Edit..."));
55         _sizer->Add(_edit, 0, wxLEFT, DCPOMATIC_SIZER_GAP);
56
57         _edit->Bind(wxEVT_BUTTON, boost::bind(&RegionSubtagWidget::edit, this));
58 }
59
60
61 RegionSubtagWidget::~RegionSubtagWidget()
62 {
63         _region->Destroy();
64         _edit->Destroy();
65 }
66
67
68 void
69 RegionSubtagWidget::edit()
70 {
71         auto d = make_wx<RegionSubtagDialog>(_parent, _tag.get_value_or(dcp::LanguageTag::RegionSubtag("US")));
72
73         if (d->ShowModal() == wxID_OK) {
74                 set(d->get());
75                 Changed(d->get());
76         }
77 }
78
79
80 void
81 RegionSubtagWidget::set(optional<dcp::LanguageTag::RegionSubtag> tag)
82 {
83         _tag = tag;
84         if (tag) {
85                 checked_set(_region, std_to_wx(tag->subtag()));
86         } else {
87                 checked_set(_region, wxT(""));
88         }
89 }
90
91
92 void
93 RegionSubtagWidget::enable(bool e)
94 {
95         _region->Enable(e);
96         _edit->Enable(e);
97 }