768d250923f7d302973981da9f53a1453d3013d3
[dcpomatic.git] / src / wx / screens_panel.cc
1 /*
2     Copyright (C) 2015-2022 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
22 #include "check_box.h"
23 #include "cinema_dialog.h"
24 #include "dcpomatic_button.h"
25 #include "screen_dialog.h"
26 #include "screens_panel.h"
27 #include "wx_util.h"
28 #include "wx_variant.h"
29 #include "lib/cinema.h"
30 #include "lib/config.h"
31 #include "lib/screen.h"
32 #include "lib/timer.h"
33 #include <dcp/scope_guard.h>
34
35
36 using std::cout;
37 using std::list;
38 using std::make_pair;
39 using std::make_shared;
40 using std::map;
41 using std::pair;
42 using std::shared_ptr;
43 using std::string;
44 using std::vector;
45 using boost::optional;
46 #if BOOST_VERSION >= 106100
47 using namespace boost::placeholders;
48 #endif
49 using namespace dcpomatic;
50
51
52 ScreensPanel::ScreensPanel (wxWindow* parent)
53         : wxPanel (parent, wxID_ANY)
54 {
55         _overall_sizer = new wxBoxSizer(wxVERTICAL);
56
57         auto search_sizer = new wxBoxSizer(wxHORIZONTAL);
58
59         _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
60 #ifndef __WXGTK3__
61         /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
62         _search->ShowCancelButton (true);
63 #endif
64         search_sizer->Add(_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
65
66         _show_only_checked = new CheckBox(this, _("Show only checked"));
67         search_sizer->Add(_show_only_checked, 1, wxEXPAND | wxLEFT | wxBOTTOM, DCPOMATIC_SIZER_GAP);
68
69         _overall_sizer->Add(search_sizer);
70
71         auto targets = new wxBoxSizer (wxHORIZONTAL);
72         _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
73         _targets->AppendColumn (wxT("foo"));
74
75         targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
76
77         add_cinemas ();
78
79         auto side_buttons = new wxBoxSizer (wxVERTICAL);
80
81         auto target_buttons = new wxBoxSizer (wxVERTICAL);
82
83         _add_cinema = new Button (this, _("Add Cinema..."));
84         target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
85         _edit_cinema = new Button (this, _("Edit Cinema..."));
86         target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
87         _remove_cinema = new Button (this, _("Remove Cinema"));
88         target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
89         _add_screen = new Button (this, _("Add Screen..."));
90         target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
91         _edit_screen = new Button (this, _("Edit Screen..."));
92         target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
93         _remove_screen = new Button (this, _("Remove Screen"));
94         target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
95
96         side_buttons->Add (target_buttons, 0, 0);
97
98         auto check_buttons = new wxBoxSizer (wxVERTICAL);
99
100         _check_all = new Button (this, _("Check all"));
101         check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
102         _uncheck_all = new Button (this, _("Uncheck all"));
103         check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
104
105         side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
106
107         targets->Add (side_buttons, 0, 0);
108
109         _overall_sizer->Add (targets, 1, wxEXPAND);
110
111         _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::display_filter_changed, this));
112         _show_only_checked->Bind(wxEVT_CHECKBOX, boost::bind(&ScreensPanel::display_filter_changed, this));
113         _targets->Bind       (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
114         _targets->Bind       (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
115         _targets->Bind       (wxEVT_TREELIST_ITEM_ACTIVATED, &ScreensPanel::item_activated, this);
116
117         _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
118         _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
119         _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
120
121         _add_screen->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
122         _edit_screen->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
123         _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
124
125         _check_all->Bind     (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
126         _uncheck_all->Bind   (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
127
128         SetSizer(_overall_sizer);
129
130         _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
131 }
132
133
134 ScreensPanel::~ScreensPanel ()
135 {
136         _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
137         _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
138 }
139
140
141 void
142 ScreensPanel::check_all ()
143 {
144         for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk();  cinema = _targets->GetNextSibling(cinema)) {
145                 _targets->CheckItem(cinema, wxCHK_CHECKED);
146                 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
147                         _targets->CheckItem(screen, wxCHK_CHECKED);
148                         set_screen_checked(screen, true);
149                 }
150         }
151 }
152
153
154 void
155 ScreensPanel::uncheck_all ()
156 {
157         for (auto cinema = _targets->GetFirstChild(_targets->GetRootItem()); cinema.IsOk();  cinema = _targets->GetNextSibling(cinema)) {
158                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
159                 for (auto screen = _targets->GetFirstChild(cinema); screen.IsOk(); screen = _targets->GetNextSibling(screen)) {
160                         _targets->CheckItem(screen, wxCHK_UNCHECKED);
161                         set_screen_checked(screen, false);
162                 }
163         }
164 }
165
166
167 void
168 ScreensPanel::setup_sensitivity ()
169 {
170         bool const sc = _selected_cinemas.size() == 1;
171         bool const ss = _selected_screens.size() == 1;
172
173         _edit_cinema->Enable (sc || ss);
174         _remove_cinema->Enable (_selected_cinemas.size() >= 1);
175
176         _add_screen->Enable (sc || ss);
177         _edit_screen->Enable (ss);
178         _remove_screen->Enable (_selected_screens.size() >= 1);
179
180         _show_only_checked->Enable(!_checked_screens.empty());
181 }
182
183
184 void
185 ScreensPanel::convert_to_lower(string& s)
186 {
187         transform(s.begin(), s.end(), s.begin(), ::tolower);
188 }
189
190
191 bool
192 ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string search)
193 {
194         if (search.empty()) {
195                 return true;
196         }
197
198         return _collator.find(search, cinema->name);
199 }
200
201
202 optional<wxTreeListItem>
203 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
204 {
205         auto const search = wx_to_std(_search->GetValue());
206         if (!matches_search(cinema, search)) {
207                 return {};
208         }
209
210         if (_show_only_checked->get()) {
211                 auto screens = cinema->screens();
212                 auto iter = std::find_if(screens.begin(), screens.end(), [this](shared_ptr<dcpomatic::Screen> screen) {
213                         return _checked_screens.find(screen) != _checked_screens.end();
214                 });
215                 if (iter == screens.end()) {
216                         return {};
217                 }
218         }
219
220         auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
221
222         _item_to_cinema[id] = cinema;
223         _cinema_to_item[cinema] = id;
224
225         for (auto screen: cinema->screens()) {
226                 add_screen (cinema, screen);
227         }
228
229         return id;
230 }
231
232
233 optional<wxTreeListItem>
234 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
235 {
236         auto item = cinema_to_item(cinema);
237         if (!item) {
238                 return {};
239         }
240
241         auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
242
243         _item_to_screen[id] = screen;
244         _screen_to_item[screen] = id;
245
246         return item;
247 }
248
249
250 void
251 ScreensPanel::add_cinema_clicked ()
252 {
253         CinemaDialog dialog(GetParent(), _("Add Cinema"));
254
255         if (dialog.ShowModal() == wxID_OK) {
256                 auto cinema = make_shared<Cinema>(dialog.name(), dialog.emails(), dialog.notes(), dialog.utc_offset());
257
258                 auto cinemas = sorted_cinemas();
259
260                 try {
261                         _ignore_cinemas_changed = true;
262                         dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
263                         Config::instance()->add_cinema(cinema);
264                 } catch (FileError& e) {
265                         error_dialog(
266                                 GetParent(),
267                                 variant::wx::insert_dcpomatic(
268                                         _("Could not write cinema details to the cinemas.xml file.  Check that the location of "
269                                           "cinemas.xml is valid in %s's preferences.")
270                                         ),
271                                 std_to_wx(e.what())
272                                 );
273                         return;
274                 }
275
276                 wxTreeListItem previous = wxTLI_FIRST;
277                 bool found = false;
278                 auto const search = wx_to_std(_search->GetValue());
279                 for (auto existing_cinema: cinemas) {
280                         if (!matches_search(existing_cinema, search)) {
281                                 continue;
282                         }
283                         if (_collator.compare(dialog.name(), existing_cinema->name) < 0) {
284                                 /* existing_cinema should be after the one we're inserting */
285                                 found = true;
286                                 break;
287                         }
288                         auto item = cinema_to_item(existing_cinema);
289                         DCPOMATIC_ASSERT(item);
290                         previous = *item;
291                 }
292
293                 auto item = add_cinema(cinema, found ? previous : wxTLI_LAST);
294
295                 if (item) {
296                         _targets->UnselectAll ();
297                         _targets->Select (*item);
298                 }
299         }
300
301         selection_changed ();
302 }
303
304
305 shared_ptr<Cinema>
306 ScreensPanel::cinema_for_operation () const
307 {
308         if (_selected_cinemas.size() == 1) {
309                 return _selected_cinemas[0];
310         } else if (_selected_screens.size() == 1) {
311                 return _selected_screens[0]->cinema;
312         }
313
314         return {};
315 }
316
317
318 void
319 ScreensPanel::edit_cinema_clicked ()
320 {
321         auto cinema = cinema_for_operation ();
322         if (cinema) {
323                 edit_cinema(cinema);
324         }
325 }
326
327
328 void
329 ScreensPanel::edit_cinema(shared_ptr<Cinema> cinema)
330 {
331         CinemaDialog dialog(GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset);
332
333         if (dialog.ShowModal() == wxID_OK) {
334                 cinema->name = dialog.name();
335                 cinema->emails = dialog.emails();
336                 cinema->notes = dialog.notes();
337                 cinema->utc_offset = dialog.utc_offset();
338                 notify_cinemas_changed();
339                 auto item = cinema_to_item(cinema);
340                 DCPOMATIC_ASSERT(item);
341                 _targets->SetItemText (*item, std_to_wx(dialog.name()));
342         }
343 }
344
345
346 void
347 ScreensPanel::remove_cinema_clicked ()
348 {
349         if (_selected_cinemas.size() == 1) {
350                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
351                         return;
352                 }
353         } else {
354                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
355                         return;
356                 }
357         }
358
359         auto cinemas_to_remove = _selected_cinemas;
360
361         for (auto const& cinema: cinemas_to_remove) {
362                 _ignore_cinemas_changed = true;
363                 dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
364                 for (auto screen: cinema->screens()) {
365                         _checked_screens.erase(screen);
366                 }
367                 Config::instance()->remove_cinema(cinema);
368                 auto item = cinema_to_item(cinema);
369                 DCPOMATIC_ASSERT(item);
370                 _targets->DeleteItem(*item);
371         }
372
373         selection_changed ();
374         setup_show_only_checked();
375 }
376
377
378 void
379 ScreensPanel::add_screen_clicked ()
380 {
381         auto cinema = cinema_for_operation ();
382         if (!cinema) {
383                 return;
384         }
385
386         ScreenDialog dialog(GetParent(), _("Add Screen"));
387
388         if (dialog.ShowModal () != wxID_OK) {
389                 return;
390         }
391
392         for (auto screen: cinema->screens()) {
393                 if (screen->name == dialog.name()) {
394                         error_dialog (
395                                 GetParent(),
396                                 wxString::Format (
397                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
398                                         std_to_wx(dialog.name()).data()
399                                         )
400                                 );
401                         return;
402                 }
403         }
404
405         auto screen = std::make_shared<Screen>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices());
406         cinema->add_screen (screen);
407         notify_cinemas_changed();
408
409         auto id = add_screen (cinema, screen);
410         if (id) {
411                 _targets->Expand (id.get ());
412         }
413 }
414
415
416 void
417 ScreensPanel::edit_screen_clicked ()
418 {
419         if (_selected_screens.size() == 1) {
420                 edit_screen(_selected_screens[0]);
421         }
422 }
423
424
425 void
426 ScreensPanel::edit_screen(shared_ptr<Screen> edit_screen)
427 {
428         ScreenDialog dialog(
429                 GetParent(), _("Edit screen"),
430                 edit_screen->name,
431                 edit_screen->notes,
432                 edit_screen->recipient,
433                 edit_screen->recipient_file,
434                 edit_screen->trusted_devices
435                 );
436
437         if (dialog.ShowModal() != wxID_OK) {
438                 return;
439         }
440
441         auto cinema = edit_screen->cinema;
442         for (auto screen: cinema->screens()) {
443                 if (screen != edit_screen && screen->name == dialog.name()) {
444                         error_dialog (
445                                 GetParent(),
446                                 wxString::Format (
447                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
448                                         std_to_wx(dialog.name()).data()
449                                         )
450                                 );
451                         return;
452                 }
453         }
454
455         edit_screen->name = dialog.name();
456         edit_screen->notes = dialog.notes();
457         edit_screen->recipient = dialog.recipient();
458         edit_screen->recipient_file = dialog.recipient_file();
459         edit_screen->trusted_devices = dialog.trusted_devices();
460         notify_cinemas_changed();
461
462         auto item = screen_to_item(edit_screen);
463         DCPOMATIC_ASSERT (item);
464         _targets->SetItemText(*item, std_to_wx(dialog.name()));
465 }
466
467
468 void
469 ScreensPanel::remove_screen_clicked ()
470 {
471         if (_selected_screens.size() == 1) {
472                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
473                         return;
474                 }
475         } else {
476                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
477                         return;
478                 }
479         }
480
481         for (auto screen: _selected_screens) {
482                 _checked_screens.erase(screen);
483                 screen->cinema->remove_screen(screen);
484                 auto item = screen_to_item(screen);
485                 DCPOMATIC_ASSERT(item);
486                 _targets->DeleteItem(*item);
487         }
488
489         /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves
490          * as well.
491          */
492         selection_changed();
493         notify_cinemas_changed();
494         setup_show_only_checked();
495 }
496
497
498 vector<shared_ptr<Screen>>
499 ScreensPanel::screens () const
500 {
501         vector<shared_ptr<Screen>> output;
502         std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
503         return output;
504 }
505
506
507 void
508 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
509 {
510         selection_changed ();
511 }
512
513
514 void
515 ScreensPanel::selection_changed ()
516 {
517         if (_ignore_selection_change) {
518                 return;
519         }
520
521         wxTreeListItems selection;
522         _targets->GetSelections (selection);
523
524         _selected_cinemas.clear ();
525         _selected_screens.clear ();
526
527         for (size_t i = 0; i < selection.size(); ++i) {
528                 if (auto cinema = item_to_cinema(selection[i])) {
529                         _selected_cinemas.push_back(cinema);
530                 }
531                 if (auto screen = item_to_screen(selection[i])) {
532                         _selected_screens.push_back(screen);
533                 }
534         }
535
536         setup_sensitivity ();
537 }
538
539
540 list<shared_ptr<Cinema>>
541 ScreensPanel::sorted_cinemas() const
542 {
543         auto cinemas = Config::instance()->cinemas();
544
545         cinemas.sort(
546                 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
547                 );
548
549         return cinemas;
550 }
551
552
553 void
554 ScreensPanel::add_cinemas ()
555 {
556         for (auto cinema: sorted_cinemas()) {
557                 add_cinema (cinema, wxTLI_LAST);
558         }
559 }
560
561
562 void
563 ScreensPanel::clear_and_re_add()
564 {
565         _targets->DeleteAllItems ();
566
567         _item_to_cinema.clear ();
568         _cinema_to_item.clear ();
569         _item_to_screen.clear ();
570         _screen_to_item.clear ();
571
572         add_cinemas ();
573 }
574
575
576 /** Search and/or "show only checked" changed */
577 void
578 ScreensPanel::display_filter_changed()
579 {
580         clear_and_re_add();
581
582         _ignore_selection_change = true;
583
584         for (auto const& selection: _selected_cinemas) {
585                 if (auto item = cinema_to_item(selection)) {
586                         _targets->Select (*item);
587                 }
588         }
589
590         for (auto const& selection: _selected_screens) {
591                 if (auto item = screen_to_item(selection)) {
592                         _targets->Select (*item);
593                 }
594         }
595
596         _ignore_selection_change = false;
597
598         _ignore_check_change = true;
599
600         for (auto const& checked: _checked_screens) {
601                 if (auto item = screen_to_item(checked)) {
602                         _targets->CheckItem(*item, wxCHK_CHECKED);
603                         setup_cinema_checked_state(*item);
604                 }
605         }
606
607         _ignore_check_change = false;
608 }
609
610
611 void
612 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
613 {
614         auto screen = item_to_screen(item);
615         DCPOMATIC_ASSERT(screen);
616         if (checked) {
617                 _checked_screens.insert(screen);
618         } else {
619                 _checked_screens.erase(screen);
620         }
621
622         setup_show_only_checked();
623 }
624
625
626 void
627 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
628 {
629         auto cinema = _targets->GetItemParent(screen);
630         DCPOMATIC_ASSERT (cinema.IsOk());
631         int checked = 0;
632         int unchecked = 0;
633         for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
634                 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
635                     ++checked;
636                 } else {
637                     ++unchecked;
638                 }
639         }
640         if (checked == 0) {
641                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
642         } else if (unchecked == 0) {
643                 _targets->CheckItem(cinema, wxCHK_CHECKED);
644         } else {
645                 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
646         }
647 }
648
649
650 void
651 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
652 {
653         if (_ignore_check_change) {
654                 return;
655         }
656
657         if (item_to_cinema(ev.GetItem())) {
658                 /* Cinema: check/uncheck all children */
659                 auto const checked = _targets->GetCheckedState(ev.GetItem());
660                 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
661                         _targets->CheckItem(child, checked);
662                         set_screen_checked(child, checked);
663                 }
664         } else {
665                 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
666                 setup_cinema_checked_state(ev.GetItem());
667         }
668
669         ScreensChanged ();
670 }
671
672
673 shared_ptr<Cinema>
674 ScreensPanel::item_to_cinema (wxTreeListItem item) const
675 {
676         auto iter = _item_to_cinema.find (item);
677         if (iter == _item_to_cinema.end()) {
678                 return {};
679         }
680
681         return iter->second;
682 }
683
684
685 shared_ptr<Screen>
686 ScreensPanel::item_to_screen (wxTreeListItem item) const
687 {
688         auto iter = _item_to_screen.find (item);
689         if (iter == _item_to_screen.end()) {
690                 return {};
691         }
692
693         return iter->second;
694 }
695
696
697 optional<wxTreeListItem>
698 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
699 {
700         auto iter = _cinema_to_item.find (cinema);
701         if (iter == _cinema_to_item.end()) {
702                 return {};
703         }
704
705         return iter->second;
706 }
707
708
709 optional<wxTreeListItem>
710 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
711 {
712         auto iter = _screen_to_item.find (screen);
713         if (iter == _screen_to_item.end()) {
714                 return {};
715         }
716
717         return iter->second;
718 }
719
720
721 bool
722 ScreensPanel::notify_cinemas_changed()
723 {
724         _ignore_cinemas_changed = true;
725         dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
726
727         try {
728                 Config::instance()->changed(Config::CINEMAS);
729         } catch (FileError& e) {
730                 error_dialog(
731                         GetParent(),
732                         variant::wx::insert_dcpomatic(
733                                 _("Could not write cinema details to the cinemas.xml file.  Check that the location of "
734                                   "cinemas.xml is valid in %s's preferences.")
735                                 ),
736                         std_to_wx(e.what())
737                         );
738                 return false;
739         }
740
741         return true;
742 }
743
744
745 void
746 ScreensPanel::config_changed(Config::Property property)
747 {
748         if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
749                 clear_and_re_add();
750         }
751 }
752
753
754 void
755 ScreensPanel::item_activated(wxTreeListEvent& ev)
756 {
757         auto iter = _item_to_cinema.find(ev.GetItem());
758         if (iter != _item_to_cinema.end()) {
759                 edit_cinema(iter->second);
760         } else {
761                 auto iter = _item_to_screen.find(ev.GetItem());
762                 if (iter != _item_to_screen.end()) {
763                         edit_screen(iter->second);
764                 }
765         }
766 }
767
768
769 void
770 ScreensPanel::setup_show_only_checked()
771 {
772         if (_checked_screens.empty()) {
773                 _show_only_checked->set_text(_("Show only checked"));
774         } else {
775                 _show_only_checked->set_text(wxString::Format(_("Show only %d checked"), static_cast<int>(_checked_screens.size())));
776         }
777
778         _overall_sizer->Layout();
779         setup_sensitivity();
780 }
781
782
783 dcp::UTCOffset
784 ScreensPanel::best_utc_offset() const
785 {
786         auto all_screens = screens();
787         if (all_screens.empty()) {
788                 return {};
789         }
790
791         dcp::UTCOffset const first = all_screens[0]->cinema->utc_offset;
792
793         for (auto screen = std::next(all_screens.begin()); screen != all_screens.end(); ++screen) {
794                 if ((*screen)->cinema->utc_offset != first) {
795                         /* Not unique */
796                         return dcp::UTCOffset();
797                 }
798         }
799
800         return first;
801 }
802