2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "lib/config.h"
21 #include "lib/cinema.h"
22 #include "lib/screen.h"
23 #include "screens_panel.h"
25 #include "cinema_dialog.h"
26 #include "screen_dialog.h"
33 using boost::shared_ptr;
35 ScreensPanel::ScreensPanel (wxWindow* parent)
36 : wxPanel (parent, wxID_ANY)
38 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
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);
44 _root = _targets->AddRoot ("Foo");
46 list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
47 for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
51 _targets->ExpandAll ();
53 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
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);
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);
69 targets->Add (target_buttons, 0, 0);
71 sizer->Add (targets, 1, wxEXPAND);
73 _targets->Bind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this);
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));
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));
86 ScreensPanel::~ScreensPanel ()
88 _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed, this);
91 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
92 ScreensPanel::selected_cinemas () const
95 _targets->GetSelections (s);
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));
108 list<pair<wxTreeItemId, shared_ptr<Screen> > >
109 ScreensPanel::selected_screens () const
111 wxArrayTreeItemIds s;
112 _targets->GetSelections (s);
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));
126 ScreensPanel::setup_sensitivity ()
128 bool const sc = selected_cinemas().size() == 1;
129 bool const ss = selected_screens().size() == 1;
131 _edit_cinema->Enable (sc);
132 _remove_cinema->Enable (sc);
134 _add_screen->Enable (sc);
135 _edit_screen->Enable (ss);
136 _remove_screen->Enable (ss);
141 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
143 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
145 list<shared_ptr<Screen> > sc = c->screens ();
146 for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
150 _targets->SortChildren (_root);
154 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
156 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
157 while (i != _cinemas.end() && i->second != c) {
161 if (i == _cinemas.end()) {
165 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
166 _targets->Expand (i->first);
170 ScreensPanel::add_cinema_clicked ()
172 CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
173 if (d->ShowModal () == wxID_OK) {
174 shared_ptr<Cinema> c (new Cinema (d->name(), d->emails()));
175 Config::instance()->add_cinema (c);
183 ScreensPanel::edit_cinema_clicked ()
185 if (selected_cinemas().size() != 1) {
189 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
191 CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->emails);
192 if (d->ShowModal () == wxID_OK) {
193 c.second->name = d->name ();
194 c.second->emails = d->emails ();
195 _targets->SetItemText (c.first, std_to_wx (d->name()));
196 Config::instance()->changed ();
203 ScreensPanel::remove_cinema_clicked ()
205 if (selected_cinemas().size() != 1) {
209 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
211 Config::instance()->remove_cinema (c.second);
212 _targets->Delete (c.first);
216 ScreensPanel::add_screen_clicked ()
218 if (selected_cinemas().size() != 1) {
222 shared_ptr<Cinema> c = selected_cinemas().front().second;
224 ScreenDialog* d = new ScreenDialog (this, "Add Screen");
225 if (d->ShowModal () != wxID_OK) {
229 shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
233 Config::instance()->changed ();
239 ScreensPanel::edit_screen_clicked ()
241 if (selected_screens().size() != 1) {
245 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
247 ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->recipient, s.second->trusted_devices);
248 if (d->ShowModal () == wxID_OK) {
249 s.second->name = d->name ();
250 s.second->recipient = d->recipient ();
251 s.second->trusted_devices = d->trusted_devices ();
252 _targets->SetItemText (s.first, std_to_wx (d->name()));
253 Config::instance()->changed ();
260 ScreensPanel::remove_screen_clicked ()
262 if (selected_screens().size() != 1) {
266 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
268 map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
269 while (i != _cinemas.end ()) {
270 list<shared_ptr<Screen> > sc = i->second->screens ();
271 if (find (sc.begin(), sc.end(), s.second) != sc.end ()) {
276 if (i == _cinemas.end()) {
280 i->second->remove_screen (s.second);
281 _targets->Delete (s.first);
283 Config::instance()->changed ();
286 list<shared_ptr<Screen> >
287 ScreensPanel::screens () const
289 list<shared_ptr<Screen> > s;
291 list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
292 for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
293 list<shared_ptr<Screen> > sc = i->second->screens ();
294 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
299 list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
300 for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
301 s.push_back (i->second);
311 ScreensPanel::selection_changed (wxTreeEvent &)
313 setup_sensitivity ();