13481271c74d53b5a1031e05746ec1713aad24b9
[dcpomatic.git] / src / wx / save_template_dialog.cc
1 /*
2     Copyright (C) 2016-2020 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 #include "check_box.h"
22 #include "dcpomatic_choice.h"
23 #include "save_template_dialog.h"
24 #include "wx_util.h"
25 #include "lib/config.h"
26
27 using std::string;
28 using boost::optional;
29 #if BOOST_VERSION >= 106100
30 using namespace boost::placeholders;
31 #endif
32
33
34 SaveTemplateDialog::SaveTemplateDialog(wxWindow* parent, optional<string> last_template)
35         : TableDialog (parent, _("Save template"), 2, 1, true)
36 {
37         _as_existing = new wxRadioButton(this, wxID_ANY, "Save over existing template");
38         add(_as_existing, true);
39         _existing = new Choice(this);
40         add(_existing);
41
42         auto const existing_templates = Config::instance()->templates();
43         for (auto i: existing_templates) {
44                 _existing->add(i);
45         }
46
47         _new = new wxRadioButton(this, wxID_ANY, _("Make new template with name"));
48         add(_new, true);
49         _name = add (new wxTextCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (300, -1)));
50         _name->SetFocus ();
51         layout ();
52
53         _as_existing->Enable(!existing_templates.empty());
54         _existing->Enable(!existing_templates.empty());
55
56         if (last_template) {
57                 auto iter = std::find(existing_templates.begin(), existing_templates.end(), *last_template);
58                 if (iter != existing_templates.end()) {
59                         _as_existing->SetValue(true);
60                         _existing->set(std::distance(existing_templates.begin(), iter));
61                 }
62         } else if (!existing_templates.empty()) {
63                 _new->SetValue(true);
64         }
65
66
67         auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this));
68         if (ok) {
69                 ok->Bind(wxEVT_BUTTON, boost::bind(&SaveTemplateDialog::check, this, _1));
70         }
71
72         _as_existing->Bind(wxEVT_RADIOBUTTON, boost::bind(&SaveTemplateDialog::setup_sensitivity, this));
73         _new->Bind(wxEVT_RADIOBUTTON, boost::bind(&SaveTemplateDialog::setup_sensitivity, this));
74         _name->Bind(wxEVT_TEXT, boost::bind(&SaveTemplateDialog::setup_sensitivity, this));
75
76         setup_sensitivity ();
77 }
78
79
80 void
81 SaveTemplateDialog::setup_sensitivity ()
82 {
83         _existing->Enable(_as_existing->GetValue());
84         _name->Enable(_new->GetValue());
85         if (_new->GetValue()) {
86                 _name->SetFocus();
87         }
88         if (auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this))) {
89                 ok->Enable(_as_existing->GetValue() || (_new->GetValue() && !_name->GetValue().IsEmpty()));
90         }
91 }
92
93
94 string
95 SaveTemplateDialog::name () const
96 {
97         if (_as_existing->GetValue()) {
98                 auto const templates = Config::instance()->templates();
99                 auto selection = _existing->get();
100                 DCPOMATIC_ASSERT(selection && *selection < int(templates.size()));
101                 return templates[*selection];
102         } else {
103                 return wx_to_std(_name->GetValue());
104         }
105 }
106
107
108 void
109 SaveTemplateDialog::check (wxCommandEvent& ev)
110 {
111         bool ok = true;
112
113         if (_new->GetValue() && Config::instance()->existing_template(wx_to_std(_name->GetValue()))) {
114                 ok = confirm_dialog (this, _("There is already a template with this name.  Do you want to overwrite it?"));
115         }
116
117         if (ok) {
118                 ev.Skip ();
119         }
120 }