C++11 tidying.
[dcpomatic.git] / src / wx / templates_dialog.cc
1 /*
2     Copyright (C) 2016-2021 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
22 #include "templates_dialog.h"
23 #include "wx_util.h"
24 #include "rename_template_dialog.h"
25 #include "dcpomatic_button.h"
26 #include "lib/config.h"
27 #include <wx/wx.h>
28
29
30 using std::string;
31 using boost::bind;
32 #if BOOST_VERSION >= 106100
33 using namespace boost::placeholders;
34 #endif
35
36
37 TemplatesDialog::TemplatesDialog (wxWindow* parent)
38         : wxDialog (parent, wxID_ANY, _("Templates"))
39 {
40         _sizer = new wxBoxSizer (wxVERTICAL);
41         SetSizer (_sizer);
42
43         auto hs = new wxBoxSizer (wxHORIZONTAL);
44         _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (200, 100), wxLC_REPORT | wxLC_SINGLE_SEL);
45
46         wxListItem ip;
47         ip.SetId (0);
48         ip.SetText (_("Template"));
49         ip.SetWidth (200);
50         _list->InsertColumn (0, ip);
51
52         hs->Add (_list, 1, wxEXPAND, DCPOMATIC_SIZER_GAP);
53
54         {
55                 auto s = new wxBoxSizer (wxVERTICAL);
56                 _rename = new Button (this, _("Rename..."));
57                 s->Add (_rename, 0, wxTOP | wxBOTTOM, 2);
58                 _remove = new Button (this, _("Remove"));
59                 s->Add (_remove, 0, wxTOP | wxBOTTOM, 2);
60                 hs->Add (s, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
61         }
62
63         _sizer->Add (hs, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
64
65         auto buttons = CreateSeparatedButtonSizer (wxCLOSE);
66         if (buttons) {
67                 _sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
68         }
69
70         _rename->Bind (wxEVT_BUTTON, bind(&TemplatesDialog::rename_clicked, this));
71         _remove->Bind (wxEVT_BUTTON, bind(&TemplatesDialog::remove_clicked, this));
72
73         _list->Bind (wxEVT_LIST_ITEM_SELECTED, bind(&TemplatesDialog::selection_changed, this));
74         _list->Bind (wxEVT_LIST_ITEM_DESELECTED, bind(&TemplatesDialog::selection_changed, this));
75         _list->Bind (wxEVT_SIZE, bind(&TemplatesDialog::resized, this, _1));
76         _config_connection = Config::instance()->Changed.connect (bind(&TemplatesDialog::refresh, this));
77
78         refresh ();
79         selection_changed ();
80 }
81
82
83 void
84 TemplatesDialog::refresh ()
85 {
86         _list->DeleteAllItems ();
87
88         for (auto i: Config::instance()->templates()) {
89                 wxListItem list_item;
90                 int const n = _list->GetItemCount ();
91                 list_item.SetId (n);
92                 _list->InsertItem (list_item);
93                 _list->SetItem (n, 0, std_to_wx (i));
94         }
95 }
96
97
98 void
99 TemplatesDialog::layout ()
100 {
101         _sizer->Layout ();
102 }
103
104
105 void
106 TemplatesDialog::selection_changed ()
107 {
108         int const i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
109         _rename->Enable (i >= 0);
110         _remove->Enable (i >= 0);
111 }
112
113
114 void
115 TemplatesDialog::rename_clicked ()
116 {
117         int item = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
118         if (item == -1) {
119                 return;
120         }
121
122         wxListItem li;
123         li.m_itemId = item;
124         li.m_col = 0;
125         li.m_mask = wxLIST_MASK_TEXT;
126         _list->GetItem (li);
127
128         auto d = new RenameTemplateDialog (this);
129         d->set (li.m_text);
130         if (d->ShowModal() == wxID_OK) {
131                 if (!d->get().IsEmpty()) {
132                         Config::instance()->rename_template (wx_to_std (li.m_text), wx_to_std (d->get ()));
133                         _list->SetItem (item, 0, d->get());
134                 } else {
135                         error_dialog (this, _("Template names must not be empty."));
136                 }
137         }
138         d->Destroy ();
139 }
140
141
142 void
143 TemplatesDialog::remove_clicked ()
144 {
145         int i = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
146         if (i == -1) {
147                 return;
148         }
149
150         wxListItem li;
151         li.m_itemId = i;
152         li.m_col = 0;
153         li.m_mask = wxLIST_MASK_TEXT;
154         _list->GetItem (li);
155
156         Config::instance()->delete_template (wx_to_std(li.m_text));
157         _list->DeleteItem (i);
158
159         selection_changed ();
160 }
161
162
163 void
164 TemplatesDialog::resized (wxSizeEvent& ev)
165 {
166         _list->SetColumnWidth (0, GetSize().GetWidth());
167         ev.Skip ();
168 }