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"
32 using boost::shared_ptr;
34 ScreensPanel::ScreensPanel (wxWindow* parent)
35 : wxPanel (parent, wxID_ANY)
37 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
39 wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
40 _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT);
41 targets->Add (_targets, 1, wxEXPAND | wxTOP | wxRIGHT, DCPOMATIC_SIZER_GAP);
43 _root = _targets->AddRoot ("Foo");
45 list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
46 for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
50 _targets->ExpandAll ();
52 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
54 _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
55 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
56 _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
57 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
58 _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
59 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
61 _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
62 target_buttons->Add (_add_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
63 _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
64 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
65 _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
66 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxALL, DCPOMATIC_BUTTON_STACK_GAP);
68 targets->Add (target_buttons, 0, 0);
70 sizer->Add (targets, 1, wxEXPAND);
72 _targets->Bind (wxEVT_COMMAND_TREE_SEL_CHANGED, boost::bind (&ScreensPanel::selection_changed, this));
74 _add_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
75 _edit_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
76 _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
78 _add_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
79 _edit_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
80 _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
85 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
86 ScreensPanel::selected_cinemas () const
89 _targets->GetSelections (s);
91 list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
92 for (size_t i = 0; i < s.GetCount(); ++i) {
93 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
94 if (j != _cinemas.end ()) {
95 c.push_back (make_pair (j->first, j->second));
102 list<pair<wxTreeItemId, shared_ptr<Screen> > >
103 ScreensPanel::selected_screens () const
105 wxArrayTreeItemIds s;
106 _targets->GetSelections (s);
108 list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
109 for (size_t i = 0; i < s.GetCount(); ++i) {
110 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
111 if (j != _screens.end ()) {
112 c.push_back (make_pair (j->first, j->second));
120 ScreensPanel::setup_sensitivity ()
122 bool const sc = selected_cinemas().size() == 1;
123 bool const ss = selected_screens().size() == 1;
125 _edit_cinema->Enable (sc);
126 _remove_cinema->Enable (sc);
128 _add_screen->Enable (sc);
129 _edit_screen->Enable (ss);
130 _remove_screen->Enable (ss);
135 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
137 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
139 list<shared_ptr<Screen> > sc = c->screens ();
140 for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
146 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
148 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
149 while (i != _cinemas.end() && i->second != c) {
153 if (i == _cinemas.end()) {
157 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
158 _targets->Expand (i->first);
162 ScreensPanel::add_cinema_clicked ()
164 CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
165 if (d->ShowModal () == wxID_OK) {
166 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
167 Config::instance()->add_cinema (c);
175 ScreensPanel::edit_cinema_clicked ()
177 if (selected_cinemas().size() != 1) {
181 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
183 CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
184 if (d->ShowModal () == wxID_OK) {
185 c.second->name = d->name ();
186 c.second->email = d->email ();
187 _targets->SetItemText (c.first, std_to_wx (d->name()));
188 Config::instance()->changed ();
195 ScreensPanel::remove_cinema_clicked ()
197 if (selected_cinemas().size() != 1) {
201 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
203 Config::instance()->remove_cinema (c.second);
204 _targets->Delete (c.first);
208 ScreensPanel::add_screen_clicked ()
210 if (selected_cinemas().size() != 1) {
214 shared_ptr<Cinema> c = selected_cinemas().front().second;
216 ScreenDialog* d = new ScreenDialog (this, "Add Screen");
217 if (d->ShowModal () != wxID_OK) {
221 shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
225 Config::instance()->changed ();
231 ScreensPanel::edit_screen_clicked ()
233 if (selected_screens().size() != 1) {
237 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
239 ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
240 if (d->ShowModal () == wxID_OK) {
241 s.second->name = d->name ();
242 s.second->certificate = d->certificate ();
243 _targets->SetItemText (s.first, std_to_wx (d->name()));
244 Config::instance()->changed ();
251 ScreensPanel::remove_screen_clicked ()
253 if (selected_screens().size() != 1) {
257 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
259 map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
260 while (i != _cinemas.end ()) {
261 list<shared_ptr<Screen> > sc = i->second->screens ();
262 if (find (sc.begin(), sc.end(), s.second) != sc.end ()) {
267 if (i == _cinemas.end()) {
271 i->second->remove_screen (s.second);
272 _targets->Delete (s.first);
274 Config::instance()->changed ();
277 list<shared_ptr<Screen> >
278 ScreensPanel::screens () const
280 list<shared_ptr<Screen> > s;
282 list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
283 for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
284 list<shared_ptr<Screen> > sc = i->second->screens ();
285 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
290 list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
291 for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
292 s.push_back (i->second);
302 ScreensPanel::selection_changed ()
304 setup_sensitivity ();