Emit no audio from DCPs if none is mapped
[dcpomatic.git] / src / wx / screens_panel.cc
index 1d94d1acb9f8027a571403d1811698bdbb73f2ac..d3b1db77d4e36da0a04323244ac368922ec8789b 100644 (file)
@@ -19,6 +19,7 @@
 */
 
 
+#include "check_box.h"
 #include "cinema_dialog.h"
 #include "dcpomatic_button.h"
 #include "screen_dialog.h"
@@ -26,9 +27,9 @@
 #include "wx_util.h"
 #include "lib/cinema.h"
 #include "lib/config.h"
-#include "lib/scope_guard.h"
 #include "lib/screen.h"
 #include "lib/timer.h"
+#include <dcp/scope_guard.h>
 
 
 using std::cout;
@@ -50,14 +51,21 @@ using namespace dcpomatic;
 ScreensPanel::ScreensPanel (wxWindow* parent)
        : wxPanel (parent, wxID_ANY)
 {
-       auto sizer = new wxBoxSizer (wxVERTICAL);
+       _overall_sizer = new wxBoxSizer(wxVERTICAL);
+
+       auto search_sizer = new wxBoxSizer(wxHORIZONTAL);
 
        _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, search_ctrl_height()));
 #ifndef __WXGTK3__
        /* The cancel button seems to be strangely broken in GTK3; clicking on it twice sometimes works */
        _search->ShowCancelButton (true);
 #endif
-       sizer->Add (_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
+       search_sizer->Add(_search, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
+
+       _show_only_checked = new CheckBox(this, _("Show only checked"));
+       search_sizer->Add(_show_only_checked, 1, wxEXPAND | wxLEFT | wxBOTTOM, DCPOMATIC_SIZER_GAP);
+
+       _overall_sizer->Add(search_sizer);
 
        auto targets = new wxBoxSizer (wxHORIZONTAL);
        _targets = new wxTreeListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_MULTIPLE | wxTL_3STATE | wxTL_NO_HEADER);
@@ -97,11 +105,13 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
 
        targets->Add (side_buttons, 0, 0);
 
-       sizer->Add (targets, 1, wxEXPAND);
+       _overall_sizer->Add (targets, 1, wxEXPAND);
 
-       _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
+       _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::display_filter_changed, this));
+       _show_only_checked->Bind(wxEVT_CHECKBOX, boost::bind(&ScreensPanel::display_filter_changed, this));
        _targets->Bind       (wxEVT_TREELIST_SELECTION_CHANGED, &ScreensPanel::selection_changed_shim, this);
        _targets->Bind       (wxEVT_TREELIST_ITEM_CHECKED, &ScreensPanel::checkbox_changed, this);
+       _targets->Bind       (wxEVT_TREELIST_ITEM_ACTIVATED, &ScreensPanel::item_activated, this);
 
        _add_cinema->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_cinema_clicked, this));
        _edit_cinema->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
@@ -114,7 +124,7 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
        _check_all->Bind     (wxEVT_BUTTON, boost::bind(&ScreensPanel::check_all, this));
        _uncheck_all->Bind   (wxEVT_BUTTON, boost::bind(&ScreensPanel::uncheck_all, this));
 
-       SetSizer (sizer);
+       SetSizer(_overall_sizer);
 
        _config_connection = Config::instance()->Changed.connect(boost::bind(&ScreensPanel::config_changed, this, _1));
 }
@@ -165,6 +175,8 @@ ScreensPanel::setup_sensitivity ()
        _add_screen->Enable (sc || ss);
        _edit_screen->Enable (ss);
        _remove_screen->Enable (_selected_screens.size() >= 1);
+
+       _show_only_checked->Enable(!_checked_screens.empty());
 }
 
 
@@ -176,27 +188,34 @@ ScreensPanel::convert_to_lower(string& s)
 
 
 bool
-ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string lower_case_search)
+ScreensPanel::matches_search(shared_ptr<const Cinema> cinema, string search)
 {
-       if (lower_case_search.empty()) {
+       if (search.empty()) {
                return true;
        }
 
-       auto name = cinema->name;
-       convert_to_lower(name);
-       return name.find(lower_case_search) != string::npos;
+       return _collator.find(search, cinema->name);
 }
 
 
 optional<wxTreeListItem>
 ScreensPanel::add_cinema (shared_ptr<Cinema> cinema, wxTreeListItem previous)
 {
-       auto search = wx_to_std (_search->GetValue ());
-       convert_to_lower(search);
+       auto const search = wx_to_std(_search->GetValue());
        if (!matches_search(cinema, search)) {
                return {};
        }
 
+       if (_show_only_checked->get()) {
+               auto screens = cinema->screens();
+               auto iter = std::find_if(screens.begin(), screens.end(), [this](shared_ptr<dcpomatic::Screen> screen) {
+                       return _checked_screens.find(screen) != _checked_screens.end();
+               });
+               if (iter == screens.end()) {
+                       return {};
+               }
+       }
+
        auto id = _targets->InsertItem(_targets->GetRootItem(), previous, std_to_wx(cinema->name));
 
        _item_to_cinema[id] = cinema;
@@ -230,17 +249,16 @@ ScreensPanel::add_screen (shared_ptr<Cinema> cinema, shared_ptr<Screen> screen)
 void
 ScreensPanel::add_cinema_clicked ()
 {
-       auto dialog = new CinemaDialog (GetParent(), _("Add Cinema"));
-       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
+       CinemaDialog dialog(GetParent(), _("Add Cinema"));
 
-       if (dialog->ShowModal() == wxID_OK) {
-               auto cinema = make_shared<Cinema>(dialog->name(), dialog->emails(), dialog->notes(), dialog->utc_offset_hour(), dialog->utc_offset_minute());
+       if (dialog.ShowModal() == wxID_OK) {
+               auto cinema = make_shared<Cinema>(dialog.name(), dialog.emails(), dialog.notes(), dialog.utc_offset_hour(), dialog.utc_offset_minute());
 
                auto cinemas = sorted_cinemas();
 
                try {
                        _ignore_cinemas_changed = true;
-                       ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
+                       dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
                        Config::instance()->add_cinema(cinema);
                } catch (FileError& e) {
                        error_dialog(GetParent(), _("Could not write cinema details to the cinemas.xml file.  Check that the location of cinemas.xml is valid in DCP-o-matic's preferences."), std_to_wx(e.what()));
@@ -249,13 +267,12 @@ ScreensPanel::add_cinema_clicked ()
 
                wxTreeListItem previous = wxTLI_FIRST;
                bool found = false;
-               auto search = wx_to_std(_search->GetValue());
-               convert_to_lower(search);
+               auto const search = wx_to_std(_search->GetValue());
                for (auto existing_cinema: cinemas) {
                        if (!matches_search(existing_cinema, search)) {
                                continue;
                        }
-                       if (_collator.compare(dialog->name(), existing_cinema->name) < 0) {
+                       if (_collator.compare(dialog.name(), existing_cinema->name) < 0) {
                                /* existing_cinema should be after the one we're inserting */
                                found = true;
                                break;
@@ -294,25 +311,29 @@ void
 ScreensPanel::edit_cinema_clicked ()
 {
        auto cinema = cinema_for_operation ();
-       if (!cinema) {
-               return;
+       if (cinema) {
+               edit_cinema(cinema);
        }
+}
 
-       auto dialog = new CinemaDialog(
+
+void
+ScreensPanel::edit_cinema(shared_ptr<Cinema> cinema)
+{
+       CinemaDialog dialog(
                GetParent(), _("Edit cinema"), cinema->name, cinema->emails, cinema->notes, cinema->utc_offset_hour(), cinema->utc_offset_minute()
                );
-       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
-
-       if (dialog->ShowModal() == wxID_OK) {
-               cinema->name = dialog->name();
-               cinema->emails = dialog->emails();
-               cinema->notes = dialog->notes();
-               cinema->set_utc_offset_hour(dialog->utc_offset_hour());
-               cinema->set_utc_offset_minute(dialog->utc_offset_minute());
+
+       if (dialog.ShowModal() == wxID_OK) {
+               cinema->name = dialog.name();
+               cinema->emails = dialog.emails();
+               cinema->notes = dialog.notes();
+               cinema->set_utc_offset_hour(dialog.utc_offset_hour());
+               cinema->set_utc_offset_minute(dialog.utc_offset_minute());
                notify_cinemas_changed();
                auto item = cinema_to_item(cinema);
                DCPOMATIC_ASSERT(item);
-               _targets->SetItemText (*item, std_to_wx(dialog->name()));
+               _targets->SetItemText (*item, std_to_wx(dialog.name()));
        }
 }
 
@@ -330,9 +351,14 @@ ScreensPanel::remove_cinema_clicked ()
                }
        }
 
-       for (auto const& cinema: _selected_cinemas) {
+       auto cinemas_to_remove = _selected_cinemas;
+
+       for (auto const& cinema: cinemas_to_remove) {
                _ignore_cinemas_changed = true;
-               ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
+               dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
+               for (auto screen: cinema->screens()) {
+                       _checked_screens.erase(screen);
+               }
                Config::instance()->remove_cinema(cinema);
                auto item = cinema_to_item(cinema);
                DCPOMATIC_ASSERT(item);
@@ -340,6 +366,7 @@ ScreensPanel::remove_cinema_clicked ()
        }
 
        selection_changed ();
+       setup_show_only_checked();
 }
 
 
@@ -351,27 +378,26 @@ ScreensPanel::add_screen_clicked ()
                return;
        }
 
-       auto dialog = new ScreenDialog(GetParent(), _("Add Screen"));
-       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
+       ScreenDialog dialog(GetParent(), _("Add Screen"));
 
-       if (dialog->ShowModal () != wxID_OK) {
+       if (dialog.ShowModal () != wxID_OK) {
                return;
        }
 
        for (auto screen: cinema->screens()) {
-               if (screen->name == dialog->name()) {
+               if (screen->name == dialog.name()) {
                        error_dialog (
                                GetParent(),
                                wxString::Format (
                                        _("You cannot add a screen called '%s' as the cinema already has a screen with this name."),
-                                       std_to_wx(dialog->name()).data()
+                                       std_to_wx(dialog.name()).data()
                                        )
                                );
                        return;
                }
        }
 
-       auto screen = std::make_shared<Screen>(dialog->name(), dialog->notes(), dialog->recipient(), dialog->recipient_file(), dialog->trusted_devices());
+       auto screen = std::make_shared<Screen>(dialog.name(), dialog.notes(), dialog.recipient(), dialog.recipient_file(), dialog.trusted_devices());
        cinema->add_screen (screen);
        notify_cinemas_changed();
 
@@ -385,13 +411,16 @@ ScreensPanel::add_screen_clicked ()
 void
 ScreensPanel::edit_screen_clicked ()
 {
-       if (_selected_screens.size() != 1) {
-               return;
+       if (_selected_screens.size() == 1) {
+               edit_screen(_selected_screens[0]);
        }
+}
 
-       auto edit_screen = _selected_screens[0];
 
-       auto dialog = new ScreenDialog(
+void
+ScreensPanel::edit_screen(shared_ptr<Screen> edit_screen)
+{
+       ScreenDialog dialog(
                GetParent(), _("Edit screen"),
                edit_screen->name,
                edit_screen->notes,
@@ -399,36 +428,35 @@ ScreensPanel::edit_screen_clicked ()
                edit_screen->recipient_file,
                edit_screen->trusted_devices
                );
-       ScopeGuard sg = [dialog]() { dialog->Destroy(); };
 
-       if (dialog->ShowModal() != wxID_OK) {
+       if (dialog.ShowModal() != wxID_OK) {
                return;
        }
 
        auto cinema = edit_screen->cinema;
        for (auto screen: cinema->screens()) {
-               if (screen != edit_screen && screen->name == dialog->name()) {
+               if (screen != edit_screen && screen->name == dialog.name()) {
                        error_dialog (
                                GetParent(),
                                wxString::Format (
                                        _("You cannot change this screen's name to '%s' as the cinema already has a screen with this name."),
-                                       std_to_wx(dialog->name()).data()
+                                       std_to_wx(dialog.name()).data()
                                        )
                                );
                        return;
                }
        }
 
-       edit_screen->name = dialog->name();
-       edit_screen->notes = dialog->notes();
-       edit_screen->recipient = dialog->recipient();
-       edit_screen->recipient_file = dialog->recipient_file();
-       edit_screen->trusted_devices = dialog->trusted_devices();
+       edit_screen->name = dialog.name();
+       edit_screen->notes = dialog.notes();
+       edit_screen->recipient = dialog.recipient();
+       edit_screen->recipient_file = dialog.recipient_file();
+       edit_screen->trusted_devices = dialog.trusted_devices();
        notify_cinemas_changed();
 
        auto item = screen_to_item(edit_screen);
        DCPOMATIC_ASSERT (item);
-       _targets->SetItemText (*item, std_to_wx(dialog->name()));
+       _targets->SetItemText(*item, std_to_wx(dialog.name()));
 }
 
 
@@ -445,14 +473,20 @@ ScreensPanel::remove_screen_clicked ()
                }
        }
 
-       for (auto const& screen: _selected_screens) {
+       for (auto screen: _selected_screens) {
+               _checked_screens.erase(screen);
                screen->cinema->remove_screen(screen);
                auto item = screen_to_item(screen);
                DCPOMATIC_ASSERT(item);
                _targets->DeleteItem(*item);
        }
 
+       /* This is called by the signal on Linux, but not it seems on Windows, so we call it ourselves
+        * as well.
+        */
+       selection_changed();
        notify_cinemas_changed();
+       setup_show_only_checked();
 }
 
 
@@ -534,8 +568,9 @@ ScreensPanel::clear_and_re_add()
 }
 
 
+/** Search and/or "show only checked" changed */
 void
-ScreensPanel::search_changed ()
+ScreensPanel::display_filter_changed()
 {
        clear_and_re_add();
 
@@ -578,6 +613,8 @@ ScreensPanel::set_screen_checked (wxTreeListItem item, bool checked)
        } else {
                _checked_screens.erase(screen);
        }
+
+       setup_show_only_checked();
 }
 
 
@@ -680,7 +717,7 @@ bool
 ScreensPanel::notify_cinemas_changed()
 {
        _ignore_cinemas_changed = true;
-       ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
+       dcp::ScopeGuard sg = [this]() { _ignore_cinemas_changed = false; };
 
        try {
                Config::instance()->changed(Config::CINEMAS);
@@ -700,3 +737,33 @@ ScreensPanel::config_changed(Config::Property property)
                clear_and_re_add();
        }
 }
+
+
+void
+ScreensPanel::item_activated(wxTreeListEvent& ev)
+{
+       auto iter = _item_to_cinema.find(ev.GetItem());
+       if (iter != _item_to_cinema.end()) {
+               edit_cinema(iter->second);
+       } else {
+               auto iter = _item_to_screen.find(ev.GetItem());
+               if (iter != _item_to_screen.end()) {
+                       edit_screen(iter->second);
+               }
+       }
+}
+
+
+void
+ScreensPanel::setup_show_only_checked()
+{
+       if (_checked_screens.empty()) {
+               _show_only_checked->set_text(_("Show only checked"));
+       } else {
+               _show_only_checked->set_text(wxString::Format(_("Show only %d checked"), static_cast<int>(_checked_screens.size())));
+       }
+
+       _overall_sizer->Layout();
+       setup_sensitivity();
+}
+