52545e5f6cd0734a93ae2e7745812ce84449d865
[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());
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);
332
333         if (dialog.ShowModal() == wxID_OK) {
334                 cinema->name = dialog.name();
335                 cinema->emails = dialog.emails();
336                 cinema->notes = dialog.notes();
337                 notify_cinemas_changed();
338                 auto item = cinema_to_item(cinema);
339                 DCPOMATIC_ASSERT(item);
340                 _targets->SetItemText (*item, std_to_wx(dialog.name()));
341         }
342 }
343
344
345 void
346 ScreensPanel::remove_cinema_clicked ()
347 {
348         if (_selected_cinemas.size() == 1) {
349                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
350                         return;
351                 }
352         } else {
353                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
354                         return;
355                 }
356         }
357
358         auto cinemas_to_remove = _selected_cinemas;
359
360         for (auto const& cinema: cinemas_to_remove) {
361                 _ignore_cinemas_changed = true;
362                 dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
363                 for (auto screen: cinema->screens()) {
364                         _checked_screens.erase(screen);
365                 }
366                 Config::instance()->remove_cinema(cinema);
367                 auto item = cinema_to_item(cinema);
368                 DCPOMATIC_ASSERT(item);
369                 _targets->DeleteItem(*item);
370         }
371
372         selection_changed ();
373         setup_show_only_checked();
374 }
375
376
377 void
378 ScreensPanel::add_screen_clicked ()
379 {
380         auto cinema = cinema_for_operation ();
381         if (!cinema) {
382                 return;
383         }
384
385         ScreenDialog dialog(GetParent(), _("Add Screen"));
386
387         if (dialog.ShowModal () != wxID_OK) {
388                 return;
389         }
390
391         for (auto screen: cinema->screens()) {
392                 if (screen->name == dialog.name()) {
393                         error_dialog (
394                                 GetParent(),
395                                 wxString::Format (
396                                         _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
397                                         std_to_wx(dialog.name()).data()
398                                         )
399                                 );
400                         return;
401                 }
402         }
403
404         auto screen = std::make_shared<Screen>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices());
405         cinema->add_screen (screen);
406         notify_cinemas_changed();
407
408         auto id = add_screen (cinema, screen);
409         if (id) {
410                 _targets->Expand (id.get ());
411         }
412 }
413
414
415 void
416 ScreensPanel::edit_screen_clicked ()
417 {
418         if (_selected_screens.size() == 1) {
419                 edit_screen(_selected_screens[0]);
420         }
421 }
422
423
424 void
425 ScreensPanel::edit_screen(shared_ptr<Screen> edit_screen)
426 {
427         ScreenDialog dialog(
428                 GetParent(), _("Edit screen"),
429                 edit_screen->name,
430                 edit_screen->notes,
431                 edit_screen->recipient,
432                 edit_screen->recipient_file,
433                 edit_screen->trusted_devices
434                 );
435
436         if (dialog.ShowModal() != wxID_OK) {
437                 return;
438         }
439
440         auto cinema = edit_screen->cinema;
441         for (auto screen: cinema->screens()) {
442                 if (screen != edit_screen && screen->name == dialog.name()) {
443                         error_dialog (
444                                 GetParent(),
445                                 wxString::Format (
446                                         _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
447                                         std_to_wx(dialog.name()).data()
448                                         )
449                                 );
450                         return;
451                 }
452         }
453
454         edit_screen->name = dialog.name();
455         edit_screen->notes = dialog.notes();
456         edit_screen->recipient = dialog.recipient();
457         edit_screen->recipient_file = dialog.recipient_file();
458         edit_screen->trusted_devices = dialog.trusted_devices();
459         notify_cinemas_changed();
460
461         auto item = screen_to_item(edit_screen);
462         DCPOMATIC_ASSERT (item);
463         _targets->SetItemText(*item, std_to_wx(dialog.name()));
464 }
465
466
467 void
468 ScreensPanel::remove_screen_clicked ()
469 {
470         if (_selected_screens.size() == 1) {
471                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
472                         return;
473                 }
474         } else {
475                 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
476                         return;
477                 }
478         }
479
480         for (auto screen: _selected_screens) {
481                 _checked_screens.erase(screen);
482                 screen->cinema->remove_screen(screen);
483                 auto item = screen_to_item(screen);
484                 DCPOMATIC_ASSERT(item);
485                 _targets->DeleteItem(*item);
486         }
487
488         /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves
489          * as well.
490          */
491         selection_changed();
492         notify_cinemas_changed();
493         setup_show_only_checked();
494 }
495
496
497 vector<shared_ptr<Screen>>
498 ScreensPanel::screens () const
499 {
500         vector<shared_ptr<Screen>> output;
501         std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
502         return output;
503 }
504
505
506 void
507 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
508 {
509         selection_changed ();
510 }
511
512
513 void
514 ScreensPanel::selection_changed ()
515 {
516         if (_ignore_selection_change) {
517                 return;
518         }
519
520         wxTreeListItems selection;
521         _targets->GetSelections (selection);
522
523         _selected_cinemas.clear ();
524         _selected_screens.clear ();
525
526         for (size_t i = 0; i < selection.size(); ++i) {
527                 if (auto cinema = item_to_cinema(selection[i])) {
528                         _selected_cinemas.push_back(cinema);
529                 }
530                 if (auto screen = item_to_screen(selection[i])) {
531                         _selected_screens.push_back(screen);
532                 }
533         }
534
535         setup_sensitivity ();
536 }
537
538
539 list<shared_ptr<Cinema>>
540 ScreensPanel::sorted_cinemas() const
541 {
542         auto cinemas = Config::instance()->cinemas();
543
544         cinemas.sort(
545                 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return _collator.compare(a->name, b->name) < 0; }
546                 );
547
548         return cinemas;
549 }
550
551
552 void
553 ScreensPanel::add_cinemas ()
554 {
555         for (auto cinema: sorted_cinemas()) {
556                 add_cinema (cinema, wxTLI_LAST);
557         }
558 }
559
560
561 void
562 ScreensPanel::clear_and_re_add()
563 {
564         _targets->DeleteAllItems ();
565
566         _item_to_cinema.clear ();
567         _cinema_to_item.clear ();
568         _item_to_screen.clear ();
569         _screen_to_item.clear ();
570
571         add_cinemas ();
572 }
573
574
575 /** Search and/or "show only checked" changed */
576 void
577 ScreensPanel::display_filter_changed()
578 {
579         clear_and_re_add();
580
581         _ignore_selection_change = true;
582
583         for (auto const& selection: _selected_cinemas) {
584                 if (auto item = cinema_to_item(selection)) {
585                         _targets->Select (*item);
586                 }
587         }
588
589         for (auto const& selection: _selected_screens) {
590                 if (auto item = screen_to_item(selection)) {
591                         _targets->Select (*item);
592                 }
593         }
594
595         _ignore_selection_change = false;
596
597         _ignore_check_change = true;
598
599         for (auto const& checked: _checked_screens) {
600                 if (auto item = screen_to_item(checked)) {
601                         _targets->CheckItem(*item, wxCHK_CHECKED);
602                         setup_cinema_checked_state(*item);
603                 }
604         }
605
606         _ignore_check_change = false;
607 }
608
609
610 void
611 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
612 {
613         auto screen = item_to_screen(item);
614         DCPOMATIC_ASSERT(screen);
615         if (checked) {
616                 _checked_screens.insert(screen);
617         } else {
618                 _checked_screens.erase(screen);
619         }
620
621         setup_show_only_checked();
622 }
623
624
625 void
626 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
627 {
628         auto cinema = _targets->GetItemParent(screen);
629         DCPOMATIC_ASSERT (cinema.IsOk());
630         int checked = 0;
631         int unchecked = 0;
632         for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
633                 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
634                     ++checked;
635                 } else {
636                     ++unchecked;
637                 }
638         }
639         if (checked == 0) {
640                 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
641         } else if (unchecked == 0) {
642                 _targets->CheckItem(cinema, wxCHK_CHECKED);
643         } else {
644                 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
645         }
646 }
647
648
649 void
650 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
651 {
652         if (_ignore_check_change) {
653                 return;
654         }
655
656         if (item_to_cinema(ev.GetItem())) {
657                 /* Cinema: check/uncheck all children */
658                 auto const checked = _targets->GetCheckedState(ev.GetItem());
659                 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
660                         _targets->CheckItem(child, checked);
661                         set_screen_checked(child, checked);
662                 }
663         } else {
664                 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
665                 setup_cinema_checked_state(ev.GetItem());
666         }
667
668         ScreensChanged ();
669 }
670
671
672 shared_ptr<Cinema>
673 ScreensPanel::item_to_cinema (wxTreeListItem item) const
674 {
675         auto iter = _item_to_cinema.find (item);
676         if (iter == _item_to_cinema.end()) {
677                 return {};
678         }
679
680         return iter->second;
681 }
682
683
684 shared_ptr<Screen>
685 ScreensPanel::item_to_screen (wxTreeListItem item) const
686 {
687         auto iter = _item_to_screen.find (item);
688         if (iter == _item_to_screen.end()) {
689                 return {};
690         }
691
692         return iter->second;
693 }
694
695
696 optional<wxTreeListItem>
697 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
698 {
699         auto iter = _cinema_to_item.find (cinema);
700         if (iter == _cinema_to_item.end()) {
701                 return {};
702         }
703
704         return iter->second;
705 }
706
707
708 optional<wxTreeListItem>
709 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
710 {
711         auto iter = _screen_to_item.find (screen);
712         if (iter == _screen_to_item.end()) {
713                 return {};
714         }
715
716         return iter->second;
717 }
718
719
720 bool
721 ScreensPanel::notify_cinemas_changed()
722 {
723         _ignore_cinemas_changed = true;
724         dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
725
726         try {
727                 Config::instance()->changed(Config::CINEMAS);
728         } catch (FileError& e) {
729                 error_dialog(
730                         GetParent(),
731                         variant::wx::insert_dcpomatic(
732                                 _("Could not write cinema details to the cinemas.xml file.  Check that the location of "
733                                   "cinemas.xml is valid in %s's preferences.")
734                                 ),
735                         std_to_wx(e.what())
736                         );
737                 return false;
738         }
739
740         return true;
741 }
742
743
744 void
745 ScreensPanel::config_changed(Config::Property property)
746 {
747         if (property == Config::Property::CINEMAS && !_ignore_cinemas_changed) {
748                 clear_and_re_add();
749         }
750 }
751
752
753 void
754 ScreensPanel::item_activated(wxTreeListEvent& ev)
755 {
756         auto iter = _item_to_cinema.find(ev.GetItem());
757         if (iter != _item_to_cinema.end()) {
758                 edit_cinema(iter->second);
759         } else {
760                 auto iter = _item_to_screen.find(ev.GetItem());
761                 if (iter != _item_to_screen.end()) {
762                         edit_screen(iter->second);
763                 }
764         }
765 }
766
767
768 void
769 ScreensPanel::setup_show_only_checked()
770 {
771         if (_checked_screens.empty()) {
772                 _show_only_checked->set_text(_("Show only checked"));
773         } else {
774                 _show_only_checked->set_text(wxString::Format(_("Show only %d checked"), static_cast<int>(_checked_screens.size())));
775         }
776
777         _overall_sizer->Layout();
778         setup_sensitivity();
779 }
780