std::shared_ptr
[dcpomatic.git] / src / wx / screens_panel.cc
index 042fcc5ab58184b9f39041c800b03b4daa81f275..ee88d01789d299602d10f9a1177867506cf9ed0d 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2015-2016 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2015-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 
 */
 
-#include "lib/config.h"
-#include "lib/cinema.h"
-#include "lib/screen.h"
 #include "screens_panel.h"
 #include "wx_util.h"
 #include "cinema_dialog.h"
 #include "screen_dialog.h"
+#include "dcpomatic_button.h"
+#include "lib/config.h"
+#include "lib/cinema.h"
+#include "lib/screen.h"
 #include <boost/foreach.hpp>
 
 using std::list;
@@ -33,8 +34,9 @@ using std::cout;
 using std::map;
 using std::string;
 using std::make_pair;
-using boost::shared_ptr;
+using std::shared_ptr;
 using boost::optional;
+using namespace dcpomatic;
 
 ScreensPanel::ScreensPanel (wxWindow* parent)
        : wxPanel (parent, wxID_ANY)
@@ -42,8 +44,17 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
 {
        wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
 
-       _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (200, -1));
+#ifdef __WXGTK3__
+       int const height = 30;
+#else
+       int const height = -1;
+#endif
+
+       _search = new wxSearchCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, 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);
 
        wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
@@ -54,40 +65,40 @@ ScreensPanel::ScreensPanel (wxWindow* parent)
 
        wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
 
-       _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
+       _add_cinema = new Button (this, _("Add Cinema..."));
        target_buttons->Add (_add_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
-       _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
+       _edit_cinema = new Button (this, _("Edit Cinema..."));
        target_buttons->Add (_edit_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
-       _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
+       _remove_cinema = new Button (this, _("Remove Cinema"));
        target_buttons->Add (_remove_cinema, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
-       _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
+       _add_screen = new Button (this, _("Add Screen..."));
        target_buttons->Add (_add_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
-       _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
+       _edit_screen = new Button (this, _("Edit Screen..."));
        target_buttons->Add (_edit_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
-       _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
+       _remove_screen = new Button (this, _("Remove Screen"));
        target_buttons->Add (_remove_screen, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
 
        targets->Add (target_buttons, 0, 0);
 
        sizer->Add (targets, 1, wxEXPAND);
 
-       _search->Bind        (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&ScreensPanel::search_changed, this));
-       _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
+       _search->Bind        (wxEVT_TEXT, boost::bind (&ScreensPanel::search_changed, this));
+       _targets->Bind       (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
 
-       _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_cinema_clicked, this));
-       _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_cinema_clicked, this));
-       _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_cinema_clicked, 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));
+       _remove_cinema->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_cinema_clicked, this));
 
-       _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::add_screen_clicked, this));
-       _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::edit_screen_clicked, this));
-       _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreensPanel::remove_screen_clicked, this));
+       _add_screen->Bind    (wxEVT_BUTTON, boost::bind (&ScreensPanel::add_screen_clicked, this));
+       _edit_screen->Bind   (wxEVT_BUTTON, boost::bind (&ScreensPanel::edit_screen_clicked, this));
+       _remove_screen->Bind (wxEVT_BUTTON, boost::bind (&ScreensPanel::remove_screen_clicked, this));
 
        SetSizer (sizer);
 }
 
 ScreensPanel::~ScreensPanel ()
 {
-       _targets->Unbind (wxEVT_COMMAND_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
+       _targets->Unbind (wxEVT_TREE_SEL_CHANGED, &ScreensPanel::selection_changed_shim, this);
 }
 
 void
@@ -104,7 +115,8 @@ ScreensPanel::setup_sensitivity ()
        _remove_screen->Enable (_selected_screens.size() >= 1);
 }
 
-void
+
+optional<wxTreeItemId>
 ScreensPanel::add_cinema (shared_ptr<Cinema> c)
 {
        string search = wx_to_std (_search->GetValue ());
@@ -114,20 +126,24 @@ ScreensPanel::add_cinema (shared_ptr<Cinema> c)
                string name = c->name;
                transform (name.begin(), name.end(), name.begin(), ::tolower);
                if (name.find (search) == string::npos) {
-                       return;
+                       return optional<wxTreeItemId>();
                }
        }
 
-       _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
+       wxTreeItemId id = _targets->AppendItem(_root, std_to_wx(c->name));
+
+       _cinemas[id] = c;
 
-       list<shared_ptr<Screen> > sc = c->screens ();
-       for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
-               add_screen (c, *i);
+       BOOST_FOREACH (shared_ptr<Screen> i, c->screens()) {
+               add_screen (c, i);
        }
 
        _targets->SortChildren (_root);
+
+       return id;
 }
 
+
 optional<wxTreeItemId>
 ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
 {
@@ -147,11 +163,15 @@ ScreensPanel::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
 void
 ScreensPanel::add_cinema_clicked ()
 {
-       CinemaDialog* d = new CinemaDialog (this, _("Add Cinema"));
+       CinemaDialog* d = new CinemaDialog (GetParent(), _("Add Cinema"));
        if (d->ShowModal () == wxID_OK) {
                shared_ptr<Cinema> c (new Cinema (d->name(), d->emails(), d->notes(), d->utc_offset_hour(), d->utc_offset_minute()));
                Config::instance()->add_cinema (c);
-               add_cinema (c);
+               optional<wxTreeItemId> id = add_cinema (c);
+               if (id) {
+                       _targets->Unselect ();
+                       _targets->SelectItem (*id);
+               }
        }
 
        d->Destroy ();
@@ -167,7 +187,7 @@ ScreensPanel::edit_cinema_clicked ()
        pair<wxTreeItemId, shared_ptr<Cinema> > c = *_selected_cinemas.begin();
 
        CinemaDialog* d = new CinemaDialog (
-               this, _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
+               GetParent(), _("Edit cinema"), c.second->name, c.second->emails, c.second->notes, c.second->utc_offset_hour(), c.second->utc_offset_minute()
                );
 
        if (d->ShowModal () == wxID_OK) {
@@ -177,7 +197,7 @@ ScreensPanel::edit_cinema_clicked ()
                c.second->set_utc_offset_hour (d->utc_offset_hour ());
                c.second->set_utc_offset_minute (d->utc_offset_minute ());
                _targets->SetItemText (c.first, std_to_wx (d->name()));
-               Config::instance()->changed ();
+               Config::instance()->changed (Config::CINEMAS);
        }
 
        d->Destroy ();
@@ -186,6 +206,16 @@ ScreensPanel::edit_cinema_clicked ()
 void
 ScreensPanel::remove_cinema_clicked ()
 {
+       if (_selected_cinemas.size() == 1) {
+               if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the cinema '%s'?"), std_to_wx(_selected_cinemas.begin()->second->name)))) {
+                       return;
+               }
+       } else {
+               if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d cinemas?"), int(_selected_cinemas.size())))) {
+                       return;
+               }
+       }
+
        for (CinemaMap::iterator i = _selected_cinemas.begin(); i != _selected_cinemas.end(); ++i) {
                Config::instance()->remove_cinema (i->second);
                _targets->Delete (i->first);
@@ -203,19 +233,33 @@ ScreensPanel::add_screen_clicked ()
 
        shared_ptr<Cinema> c = _selected_cinemas.begin()->second;
 
-       ScreenDialog* d = new ScreenDialog (this, _("Add Screen"));
+       ScreenDialog* d = new ScreenDialog (GetParent(), _("Add Screen"));
        if (d->ShowModal () != wxID_OK) {
+               d->Destroy ();
                return;
        }
 
-       shared_ptr<Screen> s (new Screen (d->name(), d->recipient(), d->trusted_devices()));
+       BOOST_FOREACH (shared_ptr<Screen> i, c->screens ()) {
+               if (i->name == d->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(d->name()).data()
+                                       )
+                               );
+                       return;
+               }
+       }
+
+       shared_ptr<Screen> s (new Screen (d->name(), d->notes(), d->recipient(), d->trusted_devices()));
        c->add_screen (s);
        optional<wxTreeItemId> id = add_screen (c, s);
        if (id) {
                _targets->Expand (id.get ());
        }
 
-       Config::instance()->changed ();
+       Config::instance()->changed (Config::CINEMAS);
 
        d->Destroy ();
 }
@@ -229,22 +273,49 @@ ScreensPanel::edit_screen_clicked ()
 
        pair<wxTreeItemId, shared_ptr<Screen> > s = *_selected_screens.begin();
 
-       ScreenDialog* d = new ScreenDialog (this, _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->trusted_devices);
-       if (d->ShowModal () == wxID_OK) {
-               s.second->name = d->name ();
-               s.second->notes = d->notes ();
-               s.second->recipient = d->recipient ();
-               s.second->trusted_devices = d->trusted_devices ();
-               _targets->SetItemText (s.first, std_to_wx (d->name()));
-               Config::instance()->changed ();
+       ScreenDialog* d = new ScreenDialog (GetParent(), _("Edit screen"), s.second->name, s.second->notes, s.second->recipient, s.second->trusted_devices);
+       if (d->ShowModal () != wxID_OK) {
+               d->Destroy ();
+               return;
        }
 
+       shared_ptr<Cinema> c = s.second->cinema;
+       BOOST_FOREACH (shared_ptr<Screen> i, c->screens ()) {
+               if (i != s.second && i->name == d->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(d->name()).data()
+                                       )
+                               );
+                       return;
+               }
+       }
+
+       s.second->name = d->name ();
+       s.second->notes = d->notes ();
+       s.second->recipient = d->recipient ();
+       s.second->trusted_devices = d->trusted_devices ();
+       _targets->SetItemText (s.first, std_to_wx (d->name()));
+       Config::instance()->changed (Config::CINEMAS);
+
        d->Destroy ();
 }
 
 void
 ScreensPanel::remove_screen_clicked ()
 {
+       if (_selected_screens.size() == 1) {
+               if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove the screen '%s'?"), std_to_wx(_selected_screens.begin()->second->name)))) {
+                       return;
+               }
+       } else {
+               if (!confirm_dialog(this, wxString::Format(_("Are you sure you want to remove %d screens?"), int(_selected_screens.size())))) {
+                       return;
+               }
+       }
+
        for (ScreenMap::iterator i = _selected_screens.begin(); i != _selected_screens.end(); ++i) {
                CinemaMap::iterator j = _cinemas.begin ();
                while (j != _cinemas.end ()) {
@@ -264,7 +335,7 @@ ScreensPanel::remove_screen_clicked ()
                _targets->Delete (i->first);
        }
 
-       Config::instance()->changed ();
+       Config::instance()->changed (Config::CINEMAS);
 }
 
 list<shared_ptr<Screen> >