2 Copyright (C) 2016-2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "check_box.h"
22 #include "dcpomatic_choice.h"
23 #include "save_template_dialog.h"
25 #include "lib/config.h"
28 using boost::optional;
29 #if BOOST_VERSION >= 106100
30 using namespace boost::placeholders;
34 SaveTemplateDialog::SaveTemplateDialog(wxWindow* parent, optional<string> last_template)
35 : TableDialog (parent, _("Save template"), 2, 1, true)
37 _as_existing = new wxRadioButton(this, wxID_ANY, "Save over existing template");
38 add(_as_existing, true);
39 _existing = new Choice(this);
42 auto const existing_templates = Config::instance()->templates();
43 for (auto i: existing_templates) {
47 _new = new wxRadioButton(this, wxID_ANY, _("Make new template with name"));
49 _name = add (new wxTextCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (300, -1)));
53 _as_existing->Enable(!existing_templates.empty());
54 _existing->Enable(!existing_templates.empty());
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));
62 } else if (!existing_templates.empty()) {
67 auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this));
69 ok->Bind(wxEVT_BUTTON, boost::bind(&SaveTemplateDialog::check, this, _1));
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));
81 SaveTemplateDialog::setup_sensitivity ()
83 _existing->Enable(_as_existing->GetValue());
84 _name->Enable(_new->GetValue());
85 if (_new->GetValue()) {
88 if (auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this))) {
89 ok->Enable(_as_existing->GetValue() || (_new->GetValue() && !_name->GetValue().IsEmpty()));
95 SaveTemplateDialog::name () const
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];
103 return wx_to_std(_name->GetValue());
109 SaveTemplateDialog::check (wxCommandEvent& ev)
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?"));