Rearrange KDM window and replace OK/Cancel with Make KDM button.
[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/name_format.h"
25 #include "lib/compose.hpp"
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 (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::update_example, this));
56
57                 update_example ();
58         }
59
60         wxPanel* panel () const
61         {
62                 return _panel;
63         }
64
65         void set_example (NameFormat::Map v) {
66                 _example_values = v;
67                 update_example ();
68         }
69
70         T get () const {
71                 return _name;
72         }
73
74 private:
75
76         virtual void update_example ()
77         {
78                 _name.set_specification (wx_to_std (_specification->GetValue ()));
79
80                 wxString example = wxString::Format (_("e.g. %s"), _name.get (_example_values));
81                 wxString wrapped;
82                 for (size_t i = 0; i < example.Length(); ++i) {
83                         if (i > 0 && (i % 30) == 0) {
84                                 wrapped += "\n";
85                         }
86                         wrapped += example[i];
87                 }
88
89                 _example->SetLabel (wrapped);
90         }
91
92         wxPanel* _panel;
93         wxStaticText* _example;
94         wxSizer* _sizer;
95         wxTextCtrl* _specification;
96
97         T _name;
98         NameFormat::Map _example_values;
99 };
100
101 #endif