Supporters update.
[dcpomatic.git] / src / wx / name_format_editor.cc
1 /*
2     Copyright (C) 2016-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 "name_format_editor.h"
23 #include "static_text.h"
24 #include "wx_util.h"
25 #include "lib/util.h"
26
27
28 using std::string;
29
30
31 NameFormatEditor::NameFormatEditor (wxWindow* parent, dcp::NameFormat name, dcp::NameFormat::Map titles, dcp::NameFormat::Map examples, string suffix)
32         : _panel (new wxPanel(parent))
33         , _example (new StaticText(_panel, ""))
34         , _sizer (new wxBoxSizer(wxVERTICAL))
35         , _specification (new wxTextCtrl(_panel, wxID_ANY, ""))
36         , _name (name)
37         , _examples (examples)
38         , _suffix (suffix)
39 {
40         _sizer->Add (_specification, 0, wxEXPAND, DCPOMATIC_SIZER_Y_GAP);
41         if (!_examples.empty ()) {
42                 _sizer->Add (_example, 0, wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
43         }
44         _panel->SetSizer (_sizer);
45
46         auto titles_sizer = new wxFlexGridSizer (2);
47         for (auto const& i: titles) {
48                 auto t = new StaticText (_panel, std_to_wx (String::compose ("%%%1 %2", i.first, i.second)));
49                 titles_sizer->Add(t, 1, wxRIGHT, DCPOMATIC_SIZER_X_GAP);
50                 auto font = t->GetFont();
51                 font.SetStyle (wxFONTSTYLE_ITALIC);
52                 font.SetPointSize (font.GetPointSize() - 1);
53                 t->SetFont (font);
54                 t->SetForegroundColour (wxColour (0, 0, 204));
55         }
56         _sizer->Add (titles_sizer);
57
58         _specification->SetValue (std_to_wx (_name.specification()));
59         _specification->Bind (wxEVT_TEXT, boost::bind(&NameFormatEditor::changed, this));
60
61         update_example ();
62 }
63
64
65 void
66 NameFormatEditor::changed ()
67 {
68         update_example ();
69         Changed ();
70 }
71
72
73 void
74 NameFormatEditor::update_example ()
75 {
76         if (_examples.empty ()) {
77                 return;
78         }
79
80         _name.set_specification(wx_to_std(_specification->GetValue()));
81
82         auto example = wxString::Format(_("e.g. %s"), std_to_wx(careful_string_filter(_name.get(_examples, _suffix))));
83         wxString wrapped;
84         for (size_t i = 0; i < example.Length(); ++i) {
85                 if (i > 0 && (i % 40) == 0) {
86                         wrapped += "\n";
87                 }
88                 wrapped += example[i];
89         }
90
91         _example->SetLabel (wrapped);
92 }