Simplification of name format stuff.
[dcpomatic.git] / src / wx / name_format_editor.h
1 /*
2     Copyright (C) 2016 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 #ifndef DCPOMATIC_NAME_FORMAT_EDITOR_H
22 #define DCPOMATIC_NAME_FORMAT_EDITOR_H
23
24 #include "lib/compose.hpp"
25 #include <dcp/name_format.h>
26 #include <wx/wx.h>
27 #include <boost/foreach.hpp>
28
29 template <class T>
30 class NameFormatEditor
31 {
32 public:
33         NameFormatEditor (wxWindow* parent, T name, dcp::NameFormat::Map titles, dcp::NameFormat::Map examples)
34                 : _panel (new wxPanel (parent))
35                 , _example (new wxStaticText (_panel, wxID_ANY, ""))
36                 , _sizer (new wxBoxSizer (wxVERTICAL))
37                 , _specification (new wxTextCtrl (_panel, wxID_ANY, ""))
38                 , _name (name)
39                 , _examples (examples)
40         {
41                 _sizer->Add (_specification, 0, wxEXPAND, DCPOMATIC_SIZER_Y_GAP);
42                 _sizer->Add (_example, 0, wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
43                 _panel->SetSizer (_sizer);
44
45                 BOOST_FOREACH (char c, name.components ()) {
46                         wxStaticText* t = new wxStaticText (_panel, wxID_ANY, std_to_wx (String::compose ("%%%1 %2", c, titles[c])));
47                         _sizer->Add (t);
48                         wxFont font = t->GetFont();
49                         font.SetStyle (wxFONTSTYLE_ITALIC);
50                         font.SetPointSize (font.GetPointSize() - 1);
51                         t->SetFont (font);
52                         t->SetForegroundColour (wxColour (0, 0, 204));
53                 }
54
55                 _specification->SetValue (std_to_wx (_name.specification ()));
56                 _specification->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&NameFormatEditor::changed, this));
57
58                 update_example ();
59         }
60
61         wxPanel* panel () const {
62                 return _panel;
63         }
64
65         T get () const {
66                 return _name;
67         }
68
69         boost::signals2::signal<void ()> Changed;
70
71 private:
72
73         void changed ()
74         {
75                 update_example ();
76                 Changed ();
77         }
78
79         virtual void update_example ()
80         {
81                 _name.set_specification (wx_to_std (_specification->GetValue ()));
82
83                 wxString example = wxString::Format (_("e.g. %s"), _name.get (_examples));
84                 wxString wrapped;
85                 for (size_t i = 0; i < example.Length(); ++i) {
86                         if (i > 0 && (i % 30) == 0) {
87                                 wrapped += "\n";
88                         }
89                         wrapped += example[i];
90                 }
91
92                 _example->SetLabel (wrapped);
93         }
94
95         wxPanel* _panel;
96         wxStaticText* _example;
97         wxSizer* _sizer;
98         wxTextCtrl* _specification;
99
100         T _name;
101         dcp::NameFormat::Map _examples;
102 };
103
104 #endif