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