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