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