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