2 Copyright (C) 2015-2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "cinema_dialog.h"
23 #include "dcpomatic_button.h"
24 #include "screen_dialog.h"
25 #include "screens_panel.h"
27 #include "lib/cinema.h"
28 #include "lib/config.h"
29 #include "lib/scope_guard.h"
30 #include "lib/screen.h"
31 #include "lib/timer.h"
32 #include <unicode/putil.h>
33 #include <unicode/ucol.h>
34 #include <unicode/uiter.h>
35 #include <unicode/utypes.h>
36 #include <unicode/ustring.h>
41 using std::make_shared;
44 using std::shared_ptr;
47 using boost::optional;
48 using namespace dcpomatic;
51 ScreensPanel::ScreensPanel (wxWindow* parent)
52 : wxPanel (parent, wxID_ANY)
54 auto sizer = new wxBoxSizer (wxVERTICAL);
56 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
58 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
59 _search->ShowCancelButton (true);
61 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
63 auto targets = new wxBoxSizer (wxHORIZONTAL);
64 _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
65 _targets->AppendColumn (wxT("foo"));
67 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
71 auto side_buttons = new wxBoxSizer (wxVERTICAL);
73 auto target_buttons = new wxBoxSizer (wxVERTICAL);
75 _add_cinema = new Button (this, _("Add Cinema..."));
76 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
77 _edit_cinema = new Button (this, _("Edit Cinema..."));
78 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
79 _remove_cinema = new Button (this, _("Remove Cinema"));
80 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
81 _add_screen = new Button (this, _("Add Screen..."));
82 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
83 _edit_screen = new Button (this, _("Edit Screen..."));
84 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
85 _remove_screen = new Button (this, _("Remove Screen"));
86 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
88 side_buttons->Add (target_buttons, 0, 0);
90 auto check_buttons = new wxBoxSizer (wxVERTICAL);
92 _check_all = new Button (this, _("Check all"));
93 check_buttons->Add (_check_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
94 _uncheck_all = new Button (this, _("Uncheck all"));
95 check_buttons->Add (_uncheck_all, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
97 side_buttons->Add (check_buttons, 1, wxEXPAND | wxTOP, DCPOMATIC_BUTTON_STACK_GAP * 8);
99 targets->Add (side_buttons, 0, 0);
101 sizer->Add (targets, 1, wxEXPAND);
103 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
104 _targets->Bind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
105 _targets->Bind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
107 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
108 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
109 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
111 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
112 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
113 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
115 _check_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
116 _uncheck_all->Bind (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
120 UErrorCode status = U_ZERO_ERROR;
121 _collator = ucol_open(nullptr, &status);
123 ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
124 ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
125 ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
130 ScreensPanel::~ScreensPanel ()
132 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
133 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
136 ucol_close (_collator);
142 ScreensPanel::check_all ()
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);
155 ScreensPanel::uncheck_all ()
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);
168 ScreensPanel::setup_sensitivity ()
170 bool const sc = _selected_cinemas.size() == 1;
171 bool const ss = _selected_screens.size() == 1;
173 _edit_cinema->Enable (sc || ss);
174 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
176 _add_screen->Enable (sc || ss);
177 _edit_screen->Enable (ss);
178 _remove_screen->Enable (_selected_screens.size() >= 1);
182 optional<wxTreeListItem>
183 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
185 auto search = wx_to_std (_search->GetValue ());
186 transform (search.begin(), search.end(), search.begin(), ::tolower);
188 if (!search.empty ()) {
189 auto name = cinema->name;
190 transform (name.begin(), name.end(), name.begin(), ::tolower);
191 if (name.find (search) == string::npos) {
196 auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
198 _item_to_cinema[id] = cinema;
199 _cinema_to_item[cinema] = id;
201 for (auto screen: cinema->screens()) {
202 add_screen (cinema, screen);
209 optional<wxTreeListItem>
210 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
212 auto item = cinema_to_item(cinema);
217 auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
219 _item_to_screen[id] = screen;
220 _screen_to_item[screen] = id;
227 ScreensPanel::add_cinema_clicked ()
229 auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
230 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
232 if (dialog->ShowModal() == wxID_OK) {
233 auto cinema = make_shared<Cinema>(dialog->name(), dialog->emails(), dialog->notes(), dialog->utc_offset_hour(), dialog->utc_offset_minute());
235 auto cinemas = Config::instance()->cinemas();
237 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
240 optional<wxTreeListItem> item;
241 for (auto existing_cinema: cinemas) {
242 if (!item && compare(dialog->name(), existing_cinema->name) < 0) {
243 if (auto existing_item = cinema_to_item(existing_cinema)) {
244 item = add_cinema (cinema, *existing_item);
250 item = add_cinema (cinema, wxTLI_LAST);
254 _targets->UnselectAll ();
255 _targets->Select (*item);
258 Config::instance()->add_cinema (cinema);
261 selection_changed ();
266 ScreensPanel::cinema_for_operation () const
268 if (_selected_cinemas.size() == 1) {
269 return _selected_cinemas[0];
270 } else if (_selected_screens.size() == 1) {
271 return _selected_screens[0]->cinema;
279 ScreensPanel::edit_cinema_clicked ()
281 auto cinema = cinema_for_operation ();
286 auto dialog = new CinemaDialog(
287 GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
289 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
291 if (dialog->ShowModal() == wxID_OK) {
292 cinema->name = dialog->name();
293 cinema->emails = dialog->emails();
294 cinema->notes = dialog->notes();
295 cinema->set_utc_offset_hour(dialog->utc_offset_hour());
296 cinema->set_utc_offset_minute(dialog->utc_offset_minute());
297 auto item = cinema_to_item(cinema);
298 DCPOMATIC_ASSERT(item);
299 _targets->SetItemText (*item, std_to_wx(dialog->name()));
300 Config::instance()->changed (Config::CINEMAS);
306 ScreensPanel::remove_cinema_clicked ()
308 if (_selected_cinemas.size() == 1) {
309 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas[0]->name)))) {
313 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
318 for (auto const& cinema: _selected_cinemas) {
319 Config::instance()->remove_cinema(cinema);
320 auto item = cinema_to_item(cinema);
321 DCPOMATIC_ASSERT(item);
322 _targets->DeleteItem(*item);
325 selection_changed ();
330 ScreensPanel::add_screen_clicked ()
332 auto cinema = cinema_for_operation ();
337 auto dialog = new ScreenDialog(GetParent(), _("Add Screen"));
338 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
340 if (dialog->ShowModal () != wxID_OK) {
344 for (auto screen: cinema->screens()) {
345 if (screen->name == dialog->name()) {
349 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
350 std_to_wx(dialog->name()).data()
357 auto screen = std::make_shared<Screen>(dialog->name(), dialog->notes(), dialog->recipient(), dialog->recipient_file(), dialog->trusted_devices());
358 cinema->add_screen (screen);
359 auto id = add_screen (cinema, screen);
361 _targets->Expand (id.get ());
364 Config::instance()->changed (Config::CINEMAS);
369 ScreensPanel::edit_screen_clicked ()
371 if (_selected_screens.size() != 1) {
375 auto edit_screen = _selected_screens[0];
377 auto dialog = new ScreenDialog(
378 GetParent(), _("Edit screen"),
381 edit_screen->recipient,
382 edit_screen->recipient_file,
383 edit_screen->trusted_devices
385 ScopeGuard sg = [dialog]() { dialog->Destroy(); };
387 if (dialog->ShowModal() != wxID_OK) {
391 auto cinema = edit_screen->cinema;
392 for (auto screen: cinema->screens()) {
393 if (screen != edit_screen && screen->name == dialog->name()) {
397 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
398 std_to_wx(dialog->name()).data()
405 edit_screen->name = dialog->name();
406 edit_screen->notes = dialog->notes();
407 edit_screen->recipient = dialog->recipient();
408 edit_screen->recipient_file = dialog->recipient_file();
409 edit_screen->trusted_devices = dialog->trusted_devices();
410 auto item = screen_to_item(edit_screen);
411 DCPOMATIC_ASSERT (item);
412 _targets->SetItemText (*item, std_to_wx(dialog->name()));
413 Config::instance()->changed (Config::CINEMAS);
418 ScreensPanel::remove_screen_clicked ()
420 if (_selected_screens.size() == 1) {
421 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens[0]->name)))) {
425 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
430 for (auto const& screen: _selected_screens) {
431 screen->cinema->remove_screen(screen);
432 auto item = screen_to_item(screen);
433 DCPOMATIC_ASSERT(item);
434 _targets->DeleteItem(*item);
437 Config::instance()->changed (Config::CINEMAS);
441 vector<shared_ptr<Screen>>
442 ScreensPanel::screens () const
444 vector<shared_ptr<Screen>> output;
445 std::copy (_checked_screens.begin(), _checked_screens.end(), std::back_inserter(output));
451 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
453 selection_changed ();
458 ScreensPanel::selection_changed ()
460 if (_ignore_selection_change) {
464 wxTreeListItems selection;
465 _targets->GetSelections (selection);
467 _selected_cinemas.clear ();
468 _selected_screens.clear ();
470 for (size_t i = 0; i < selection.size(); ++i) {
471 if (auto cinema = item_to_cinema(selection[i])) {
472 _selected_cinemas.push_back(cinema);
474 if (auto screen = item_to_screen(selection[i])) {
475 _selected_screens.push_back(screen);
479 setup_sensitivity ();
484 ScreensPanel::add_cinemas ()
486 auto cinemas = Config::instance()->cinemas();
488 [this](shared_ptr<Cinema> a, shared_ptr<Cinema> b) { return compare(a->name, b->name) < 0; }
491 for (auto cinema: cinemas) {
492 add_cinema (cinema, wxTLI_LAST);
498 ScreensPanel::search_changed ()
500 _targets->DeleteAllItems ();
502 _item_to_cinema.clear ();
503 _cinema_to_item.clear ();
504 _item_to_screen.clear ();
505 _screen_to_item.clear ();
509 _ignore_selection_change = true;
511 for (auto const& selection: _selected_cinemas) {
512 if (auto item = cinema_to_item(selection)) {
513 _targets->Select (*item);
517 for (auto const& selection: _selected_screens) {
518 if (auto item = screen_to_item(selection)) {
519 _targets->Select (*item);
523 _ignore_selection_change = false;
525 _ignore_check_change = true;
527 for (auto const& checked: _checked_screens) {
528 if (auto item = screen_to_item(checked)) {
529 _targets->CheckItem(*item, wxCHK_CHECKED);
530 setup_cinema_checked_state(*item);
534 _ignore_check_change = false;
539 ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
541 auto screen = item_to_screen(item);
542 DCPOMATIC_ASSERT(screen);
544 _checked_screens.insert(screen);
546 _checked_screens.erase(screen);
552 ScreensPanel::setup_cinema_checked_state (wxTreeListItem screen)
554 auto cinema = _targets->GetItemParent(screen);
555 DCPOMATIC_ASSERT (cinema.IsOk());
558 for (auto child = _targets->GetFirstChild(cinema); child.IsOk(); child = _targets->GetNextSibling(child)) {
559 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
566 _targets->CheckItem(cinema, wxCHK_UNCHECKED);
567 } else if (unchecked == 0) {
568 _targets->CheckItem(cinema, wxCHK_CHECKED);
570 _targets->CheckItem(cinema, wxCHK_UNDETERMINED);
576 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
578 if (_ignore_check_change) {
582 if (item_to_cinema(ev.GetItem())) {
583 /* Cinema: check/uncheck all children */
584 auto const checked = _targets->GetCheckedState(ev.GetItem());
585 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
586 _targets->CheckItem(child, checked);
587 set_screen_checked(child, checked);
590 set_screen_checked(ev.GetItem(), _targets->GetCheckedState(ev.GetItem()));
591 setup_cinema_checked_state(ev.GetItem());
599 ScreensPanel::item_to_cinema (wxTreeListItem item) const
601 auto iter = _item_to_cinema.find (item);
602 if (iter == _item_to_cinema.end()) {
611 ScreensPanel::item_to_screen (wxTreeListItem item) const
613 auto iter = _item_to_screen.find (item);
614 if (iter == _item_to_screen.end()) {
622 optional<wxTreeListItem>
623 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
625 auto iter = _cinema_to_item.find (cinema);
626 if (iter == _cinema_to_item.end()) {
634 optional<wxTreeListItem>
635 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
637 auto iter = _screen_to_item.find (screen);
638 if (iter == _screen_to_item.end()) {
647 ScreensPanel::compare (string const& utf8_a, string const& utf8_b)
650 UErrorCode error = U_ZERO_ERROR;
651 boost::scoped_array<uint16_t> utf16_a(new uint16_t[utf8_a.size() + 1]);
652 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error);
653 boost::scoped_array<uint16_t> utf16_b(new uint16_t[utf8_b.size() + 1]);
654 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error);
655 return ucol_strcoll(_collator, reinterpret_cast<UChar*>(utf16_a.get()), -1, reinterpret_cast<UChar*>(utf16_b.get()), -1);
657 return strcoll(utf8_a.c_str(), utf8_b.c_str());