Add console to dcpomatic_kdm on Windows. Tweak layout a bit more.
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/config.h"
21 #include "lib/cinema.h"
22 #include "lib/screen.h"
23 #include "screens_panel.h"
24 #include "wx_util.h"
25 #include "cinema_dialog.h"
26 #include "screen_dialog.h"
27
28 using std::list;
29 using std::pair;
30 using std::cout;
31 using std::map;
32 using std::make_pair;
33 using boost::shared_ptr;
34
35 ScreensPanel::ScreensPanel (wxWindow* parent)
36         : wxPanel (parent, wxID_ANY)
37 {
38         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
39
40         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
41         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
42         targets->Add (_targets, 1, wxEXPAND | wxTOP | wxRIGHT, DCPOMATIC_SIZER_GAP);
43
44         _root = _targets->AddRoot ("Foo");
45
46         list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
47         for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
48                 add_cinema (*i);
49         }
50
51         _targets->ExpandAll ();
52
53         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
54
55         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
56         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
57         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
58         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
59         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
60         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
61
62         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
63         target_buttons->Add (_add_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
64         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
65         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
66         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
67         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
68
69         targets->Add (target_buttons, 0, 0);
70
71         sizer->Add (targets, 1, wxEXPAND);
72
73         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this);
74
75         _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
76         _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
77         _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
78
79         _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
80         _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
81         _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
82
83         SetSizer (sizer);
84 }
85
86 ScreensPanel::~ScreensPanel ()
87 {
88         _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this);
89 }
90
91 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
92 ScreensPanel::selected_cinemas () const
93 {
94         wxArrayTreeItemIds s;
95         _targets->GetSelections (s);
96
97         list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
98         for (size_t i = 0; i < s.GetCount(); ++i) {
99                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
100                 if (j != _cinemas.end ()) {
101                         c.push_back (make_pair (j->first, j->second));
102                 }
103         }
104
105         return c;
106 }
107
108 list<pair<wxTreeItemId, shared_ptr<Screen> > >
109 ScreensPanel::selected_screens () const
110 {
111         wxArrayTreeItemIds s;
112         _targets->GetSelections (s);
113
114         list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
115         for (size_t i = 0; i < s.GetCount(); ++i) {
116                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
117                 if (j != _screens.end ()) {
118                         c.push_back (make_pair (j->first, j->second));
119                 }
120         }
121
122         return c;
123 }
124
125 void
126 ScreensPanel::setup_sensitivity ()
127 {
128         bool const sc = selected_cinemas().size() == 1;
129         bool const ss = selected_screens().size() == 1;
130
131         _edit_cinema->Enable (sc);
132         _remove_cinema->Enable (sc);
133
134         _add_screen->Enable (sc);
135         _edit_screen->Enable (ss);
136         _remove_screen->Enable (ss);
137 }
138
139
140 void
141 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
142 {
143         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
144
145         list<shared_ptr<Screen> > sc = c->screens ();
146         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
147                 add_screen (c, *i);
148         }
149 }
150
151 void
152 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
153 {
154         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
155         while (i != _cinemas.end() && i->second != c) {
156                 ++i;
157         }
158
159         if (i == _cinemas.end()) {
160                 return;
161         }
162
163         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
164         _targets->Expand (i->first);
165 }
166
167 void
168 ScreensPanel::add_cinema_clicked ()
169 {
170         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
171         if (d->ShowModal () == wxID_OK) {
172                 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
173                 Config::instance()->add_cinema (c);
174                 add_cinema (c);
175         }
176
177         d->Destroy ();
178 }
179
180 void
181 ScreensPanel::edit_cinema_clicked ()
182 {
183         if (selected_cinemas().size() != 1) {
184                 return;
185         }
186
187         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
188
189         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
190         if (d->ShowModal () == wxID_OK) {
191                 c.second->name = d->name ();
192                 c.second->email = d->email ();
193                 _targets->SetItemText (c.first, std_to_wx (d->name()));
194                 Config::instance()->changed ();
195         }
196
197         d->Destroy ();
198 }
199
200 void
201 ScreensPanel::remove_cinema_clicked ()
202 {
203         if (selected_cinemas().size() != 1) {
204                 return;
205         }
206
207         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
208
209         Config::instance()->remove_cinema (c.second);
210         _targets->Delete (c.first);
211 }
212
213 void
214 ScreensPanel::add_screen_clicked ()
215 {
216         if (selected_cinemas().size() != 1) {
217                 return;
218         }
219
220         shared_ptr<Cinema> c = selected_cinemas().front().second;
221
222         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
223         if (d->ShowModal () != wxID_OK) {
224                 return;
225         }
226
227         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
228         c->add_screen (s);
229         add_screen (c, s);
230
231         Config::instance()->changed ();
232
233         d->Destroy ();
234 }
235
236 void
237 ScreensPanel::edit_screen_clicked ()
238 {
239         if (selected_screens().size() != 1) {
240                 return;
241         }
242
243         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
244
245         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
246         if (d->ShowModal () == wxID_OK) {
247                 s.second->name = d->name ();
248                 s.second->certificate = d->certificate ();
249                 _targets->SetItemText (s.first, std_to_wx (d->name()));
250                 Config::instance()->changed ();
251         }
252
253         d->Destroy ();
254 }
255
256 void
257 ScreensPanel::remove_screen_clicked ()
258 {
259         if (selected_screens().size() != 1) {
260                 return;
261         }
262
263         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
264
265         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
266         while (i != _cinemas.end ()) {
267                 list<shared_ptr<Screen> > sc = i->second->screens ();
268                 if (find (sc.begin(), sc.end(), s.second) != sc.end ()) {
269                         break;
270                 }
271         }
272
273         if (i == _cinemas.end()) {
274                 return;
275         }
276
277         i->second->remove_screen (s.second);
278         _targets->Delete (s.first);
279
280         Config::instance()->changed ();
281 }
282
283 list<shared_ptr<Screen> >
284 ScreensPanel::screens () const
285 {
286         list<shared_ptr<Screen> > s;
287
288         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
289         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
290                 list<shared_ptr<Screen> > sc = i->second->screens ();
291                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
292                         s.push_back (*j);
293                 }
294         }
295
296         list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
297         for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
298                 s.push_back (i->second);
299         }
300
301         s.sort ();
302         s.unique ();
303
304         return s;
305 }
306
307 void
308 ScreensPanel::selection_changed (wxTreeEvent &)
309 {
310         setup_sensitivity ();
311         ScreensChanged ();
312 }