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