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/screen.h"
30 #include <unicode/putil.h>
31 #include <unicode/ucol.h>
32 #include <unicode/uiter.h>
33 #include <unicode/utypes.h>
34 #include <unicode/ustring.h>
39 using std::make_shared;
42 using std::shared_ptr;
45 using boost::optional;
46 using namespace dcpomatic;
49 ScreensPanel::ScreensPanel (wxWindow* parent)
50 : wxPanel (parent, wxID_ANY)
51 , _ignore_selection_change (false)
53 auto sizer = new wxBoxSizer (wxVERTICAL);
55 _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
57 /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
58 _search->ShowCancelButton (true);
60 sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
62 auto targets = new wxBoxSizer (wxHORIZONTAL);
63 _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
64 _targets->AppendColumn (wxT("foo"));
65 _targets->SetSortColumn (0);
66 _targets->SetItemComparator (&_comparator);
68 targets->Add (_targets, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_GAP);
72 auto target_buttons = new wxBoxSizer (wxVERTICAL);
74 _add_cinema = new Button (this, _("Add Cinema..."));
75 target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
76 _edit_cinema = new Button (this, _("Edit Cinema..."));
77 target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
78 _remove_cinema = new Button (this, _("Remove Cinema"));
79 target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
80 _add_screen = new Button (this, _("Add Screen..."));
81 target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
82 _edit_screen = new Button (this, _("Edit Screen..."));
83 target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
84 _remove_screen = new Button (this, _("Remove Screen"));
85 target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
87 targets->Add (target_buttons, 0, 0);
89 sizer->Add (targets, 1, wxEXPAND);
91 _search->Bind (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
92 _targets->Bind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
93 _targets->Bind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
95 _add_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
96 _edit_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
97 _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
99 _add_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
100 _edit_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
101 _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
107 ScreensPanel::~ScreensPanel ()
109 _targets->Unbind (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
110 _targets->Unbind (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
115 ScreensPanel::setup_sensitivity ()
117 bool const sc = _selected_cinemas.size() == 1;
118 bool const ss = _selected_screens.size() == 1;
120 _edit_cinema->Enable (sc || ss);
121 _remove_cinema->Enable (_selected_cinemas.size() >= 1);
123 _add_screen->Enable (sc || ss);
124 _edit_screen->Enable (ss);
125 _remove_screen->Enable (_selected_screens.size() >= 1);
129 optional<wxTreeListItem>
130 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema)
132 auto search = wx_to_std (_search->GetValue ());
133 transform (search.begin(), search.end(), search.begin(), ::tolower);
135 if (!search.empty ()) {
136 auto name = cinema->name;
137 transform (name.begin(), name.end(), name.begin(), ::tolower);
138 if (name.find (search) == string::npos) {
143 auto id = _targets->AppendItem(_targets->GetRootItem(), std_to_wx(cinema->name));
145 _cinemas.push_back(make_pair(id, cinema));
146 _item_to_cinema[id] = cinema;
147 _cinema_to_item[cinema] = id;
149 for (auto screen: cinema->screens()) {
150 add_screen (cinema, screen);
157 optional<wxTreeListItem>
158 ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
160 auto item = cinema_to_item(cinema);
165 auto id = _targets->AppendItem(*item, std_to_wx(screen->name));
167 _screens.push_back(make_pair(id, screen));
168 _item_to_screen[id] = screen;
169 _screen_to_item[screen] = id;
176 ScreensPanel::add_cinema_clicked ()
178 auto d = new CinemaDialog (GetParent(), _("Add Cinema"));
179 if (d->ShowModal () == wxID_OK) {
180 auto cinema = make_shared<Cinema>(d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute());
181 Config::instance()->add_cinema (cinema);
182 auto id = add_cinema (cinema);
184 _targets->UnselectAll ();
185 _targets->Select (*id);
193 optional<pair<wxTreeListItem, shared_ptr<Cinema>>>
194 ScreensPanel::cinema_for_operation () const
196 if (_selected_cinemas.size() == 1) {
197 return make_pair(_selected_cinemas.begin()->first, _selected_cinemas.begin()->second);
198 } else if (_selected_screens.size() == 1) {
199 return make_pair(_targets->GetItemParent(_selected_screens.begin()->first), _selected_screens.begin()->second->cinema);
207 ScreensPanel::edit_cinema_clicked ()
209 auto cinema = cinema_for_operation ();
214 auto d = new CinemaDialog (
215 GetParent(), _("Edit cinema"), cinema->second->name, cinema->second->emails, cinema->second->notes, cinema->second->utc_offset_hour(), cinema->second->utc_offset_minute()
218 if (d->ShowModal() == wxID_OK) {
219 cinema->second->name = d->name ();
220 cinema->second->emails = d->emails ();
221 cinema->second->notes = d->notes ();
222 cinema->second->set_utc_offset_hour (d->utc_offset_hour ());
223 cinema->second->set_utc_offset_minute (d->utc_offset_minute ());
224 _targets->SetItemText (cinema->first, std_to_wx(d->name()));
225 Config::instance()->changed (Config::CINEMAS);
233 ScreensPanel::remove_cinema_clicked ()
235 if (_selected_cinemas.size() == 1) {
236 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
240 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
245 for (auto const& i: _selected_cinemas) {
246 Config::instance()->remove_cinema (i.second);
247 _targets->DeleteItem (i.first);
250 selection_changed ();
255 ScreensPanel::add_screen_clicked ()
257 auto cinema = cinema_for_operation ();
262 auto d = new ScreenDialog (GetParent(), _("Add Screen"));
263 if (d->ShowModal () != wxID_OK) {
268 for (auto screen: cinema->second->screens()) {
269 if (screen->name == d->name()) {
273 _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
274 std_to_wx(d->name()).data()
281 auto screen = std::make_shared<Screen>(d->name(), d->notes(), d->recipient(), d->recipient_file(), d->trusted_devices());
282 cinema->second->add_screen (screen);
283 auto id = add_screen (cinema->second, screen);
285 _targets->Expand (id.get ());
288 Config::instance()->changed (Config::CINEMAS);
295 ScreensPanel::edit_screen_clicked ()
297 if (_selected_screens.size() != 1) {
301 auto edit_screen = *_selected_screens.begin();
303 auto d = new ScreenDialog (
304 GetParent(), _("Edit screen"),
305 edit_screen.second->name,
306 edit_screen.second->notes,
307 edit_screen.second->recipient,
308 edit_screen.second->recipient_file,
309 edit_screen.second->trusted_devices
312 if (d->ShowModal() != wxID_OK) {
317 auto cinema = edit_screen.second->cinema;
318 for (auto screen: cinema->screens()) {
319 if (screen != edit_screen.second && screen->name == d->name()) {
323 _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
324 std_to_wx(d->name()).data()
331 edit_screen.second->name = d->name ();
332 edit_screen.second->notes = d->notes ();
333 edit_screen.second->recipient = d->recipient ();
334 edit_screen.second->recipient_file = d->recipient_file ();
335 edit_screen.second->trusted_devices = d->trusted_devices ();
336 _targets->SetItemText (edit_screen.first, std_to_wx(d->name()));
337 Config::instance()->changed (Config::CINEMAS);
344 ScreensPanel::remove_screen_clicked ()
346 if (_selected_screens.size() == 1) {
347 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
351 if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
356 for (auto const& i: _selected_screens) {
357 auto j = _cinemas.begin ();
358 while (j != _cinemas.end ()) {
359 auto sc = j->second->screens ();
360 if (find (sc.begin(), sc.end(), i.second) != sc.end ()) {
367 if (j == _cinemas.end()) {
371 j->second->remove_screen (i.second);
372 _targets->DeleteItem (i.first);
375 Config::instance()->changed (Config::CINEMAS);
379 vector<shared_ptr<Screen>>
380 ScreensPanel::screens () const
382 vector<shared_ptr<Screen>> output;
384 for (auto item = _targets->GetFirstItem(); item.IsOk(); item = _targets->GetNextItem(item)) {
385 if (_targets->GetCheckedState(item) == wxCHK_CHECKED) {
386 if (auto screen = item_to_screen(item)) {
387 output.push_back (screen);
397 ScreensPanel::selection_changed_shim (wxTreeListEvent &)
399 selection_changed ();
404 ScreensPanel::selection_changed ()
406 if (_ignore_selection_change) {
410 wxTreeListItems selection;
411 _targets->GetSelections (selection);
413 _selected_cinemas.clear ();
414 _selected_screens.clear ();
416 for (size_t i = 0; i < selection.size(); ++i) {
417 if (auto cinema = item_to_cinema(selection[i])) {
418 _selected_cinemas.push_back(make_pair(selection[i], cinema));
420 if (auto screen = item_to_screen(selection[i])) {
421 _selected_screens.push_back(make_pair(selection[i], screen));
425 setup_sensitivity ();
430 ScreensPanel::add_cinemas ()
432 for (auto cinema: Config::instance()->cinemas()) {
439 ScreensPanel::search_changed ()
441 _targets->DeleteAllItems ();
445 _item_to_cinema.clear ();
446 _cinema_to_item.clear ();
447 _item_to_screen.clear ();
448 _screen_to_item.clear ();
452 _ignore_selection_change = true;
454 for (auto const& selection: _selected_cinemas) {
455 /* The wxTreeListItems will now be different, so we must search by cinema */
456 if (auto item = cinema_to_item(selection.second)) {
457 _targets->Select (*item);
461 for (auto const& selection: _selected_screens) {
462 /* Likewise by screen */
463 if (auto item = screen_to_item(selection.second)) {
464 _targets->Select (*item);
468 _ignore_selection_change = false;
473 ScreensPanel::checkbox_changed (wxTreeListEvent& ev)
475 if (item_to_cinema(ev.GetItem())) {
476 /* Cinema: check/uncheck all children */
477 auto const checked = _targets->GetCheckedState(ev.GetItem());
478 for (auto child = _targets->GetFirstChild(ev.GetItem()); child.IsOk(); child = _targets->GetNextSibling(child)) {
479 _targets->CheckItem(child, checked);
482 /* Screen: set cinema to checked/unchecked/3state */
483 auto parent = _targets->GetItemParent(ev.GetItem());
484 DCPOMATIC_ASSERT (parent.IsOk());
487 for (auto child = _targets->GetFirstChild(parent); child.IsOk(); child = _targets->GetNextSibling(child)) {
488 if (_targets->GetCheckedState(child) == wxCHK_CHECKED) {
495 _targets->CheckItem(parent, wxCHK_UNCHECKED);
496 } else if (unchecked == 0) {
497 _targets->CheckItem(parent, wxCHK_CHECKED);
499 _targets->CheckItem(parent, wxCHK_UNDETERMINED);
508 ScreensPanel::item_to_cinema (wxTreeListItem item) const
510 auto iter = _item_to_cinema.find (item);
511 if (iter == _item_to_cinema.end()) {
520 ScreensPanel::item_to_screen (wxTreeListItem item) const
522 auto iter = _item_to_screen.find (item);
523 if (iter == _item_to_screen.end()) {
531 optional<wxTreeListItem>
532 ScreensPanel::cinema_to_item (shared_ptr<Cinema> cinema) const
534 auto iter = _cinema_to_item.find (cinema);
535 if (iter == _cinema_to_item.end()) {
543 optional<wxTreeListItem>
544 ScreensPanel::screen_to_item (shared_ptr<Screen> screen) const
546 auto iter = _screen_to_item.find (screen);
547 if (iter == _screen_to_item.end()) {
555 ScreensPanel::Comparator::Comparator ()
557 UErrorCode status = U_ZERO_ERROR;
558 _collator = ucol_open(nullptr, &status);
560 ucol_setAttribute(_collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
561 ucol_setAttribute(_collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
562 ucol_setAttribute(_collator, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
566 ScreensPanel::Comparator::~Comparator ()
569 ucol_close (_collator);
574 ScreensPanel::Comparator::Compare (wxTreeListCtrl* tree_list, unsigned, wxTreeListItem a, wxTreeListItem b)
576 auto utf8_a = wx_to_std(tree_list->GetItemText(a));
577 auto utf8_b = wx_to_std(tree_list->GetItemText(b));
579 UErrorCode error = U_ZERO_ERROR;
580 boost::scoped_array<uint16_t> utf16_a(new uint16_t[utf8_a.size() + 1]);
581 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_a.get()), utf8_a.size() + 1, nullptr, utf8_a.c_str(), -1, &error);
582 boost::scoped_array<uint16_t> utf16_b(new uint16_t[utf8_b.size() + 1]);
583 u_strFromUTF8(reinterpret_cast<UChar*>(utf16_b.get()), utf8_b.size() + 1, nullptr, utf8_b.c_str(), -1, &error);
584 return ucol_strcoll(_collator, reinterpret_cast<UChar*>(utf16_a.get()), -1, reinterpret_cast<UChar*>(utf16_b.get()), -1);
586 return strcoll(utf8_a.c_str(), utf8_b.c_str());