Basics of custom DCP filename components.
[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)
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         {
40                 _sizer->Add (_specification, 0, wxEXPAND, DCPOMATIC_SIZER_Y_GAP);
41                 _sizer->Add (_example, 0, wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
42                 _panel->SetSizer (_sizer);
43
44                 BOOST_FOREACH (dcp::NameFormat::Component c, name.components ()) {
45                         wxStaticText* t = new wxStaticText (_panel, wxID_ANY, std_to_wx (String::compose ("%%%1 %2", c.placeholder, c.title)));
46                         _sizer->Add (t);
47                         wxFont font = t->GetFont();
48                         font.SetStyle (wxFONTSTYLE_ITALIC);
49                         font.SetPointSize (font.GetPointSize() - 1);
50                         t->SetFont (font);
51                         t->SetForegroundColour (wxColour (0, 0, 204));
52                 }
53
54                 _specification->SetValue (std_to_wx (_name.specification ()));
55                 _specification->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&NameFormatEditor::changed, this));
56
57                 update_example ();
58         }
59
60         wxPanel* panel () const
61         {
62                 return _panel;
63         }
64
65         void set_example (dcp::NameFormat::Map v) {
66                 _example_values = v;
67                 update_example ();
68         }
69
70         T get () const {
71                 return _name;
72         }
73
74         boost::signals2::signal<void ()> Changed;
75
76 private:
77
78         void changed ()
79         {
80                 update_example ();
81                 Changed ();
82         }
83
84         virtual void update_example ()
85         {
86                 _name.set_specification (wx_to_std (_specification->GetValue ()));
87
88                 wxString example = wxString::Format (_("e.g. %s"), _name.get (_example_values));
89                 wxString wrapped;
90                 for (size_t i = 0; i < example.Length(); ++i) {
91                         if (i > 0 && (i % 30) == 0) {
92                                 wrapped += "\n";
93                         }
94                         wrapped += example[i];
95                 }
96
97                 _example->SetLabel (wrapped);
98         }
99
100         wxPanel* _panel;
101         wxStaticText* _example;
102         wxSizer* _sizer;
103         wxTextCtrl* _specification;
104
105         T _name;
106         dcp::NameFormat::Map _example_values;
107 };
108
109 #endif