Revert "Remove wxWidgets' file overwrite checks in favour of our own (because of...
[dcpomatic.git] / src / wx / closed_captions_dialog.cc
index be520057b81a4b46cf543442b52aef7b30411daa..b81414bed9f72b37a0e638cf6545a19440685fae 100644 (file)
 
 */
 
-#include "closed_captions_view.h"
+#include "closed_captions_dialog.h"
+#include "lib/string_text.h"
+#include "lib/butler.h"
 #include <boost/bind.hpp>
 
 using std::list;
+using std::max;
 using std::cout;
+using std::pair;
 using std::make_pair;
-
-int const ClosedCaptionsDialog::_num_lines = 3;
-int const ClosedCaptionsDialog::_num_chars_per_line = 30;
+using boost::shared_ptr;
+using boost::weak_ptr;
+using boost::optional;
 
 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
-       : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
+        /* XXX: empirical and probably unhelpful default size here; needs to be related to font metrics */
+       : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxSize(640, (640 / 10) + 64),
 #ifdef DCPOMATIC_OSX
                /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
                   the window above all others (and not just our own) it's better than nothing for now.
@@ -39,9 +44,9 @@ ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent)
                wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
 #endif
                )
-
+       , _current_in_lines (false)
 {
-       _lines.resize (_num_lines);
+       _lines.resize (CLOSED_CAPTION_LINES);
        Bind (wxEVT_PAINT, boost::bind (&ClosedCaptionsDialog::paint, this));
 }
 
@@ -54,16 +59,20 @@ ClosedCaptionsDialog::paint ()
        dc.SetTextForeground (*wxWHITE);
 
        /* Choose a font which fits vertically */
-       int const line_height = dc.GetSize().GetHeight() / _num_lines;
+       int const line_height = max (8, dc.GetSize().GetHeight() / CLOSED_CAPTION_LINES);
        wxFont font (*wxNORMAL_FONT);
        font.SetPixelSize (wxSize (0, line_height * 0.8));
        dc.SetFont (font);
 
-       for (int i = 0; i < _num_lines; ++i) {
-               if (_lines[i].IsEmpty()) {
-                       dc.DrawText (wxString::Format("Line %d", i + 1), 8, line_height * i);
-               } else {
-                       dc.DrawText (_lines[i], 8, line_height * i);
+       for (int i = 0; i < CLOSED_CAPTION_LINES; ++i) {
+               wxString const good = _lines[i].Left (CLOSED_CAPTION_LENGTH);
+               dc.DrawText (good, 8, line_height * i);
+               if (_lines[i].Length() > CLOSED_CAPTION_LENGTH) {
+                       wxString const bad = _lines[i].Right (_lines[i].Length() - CLOSED_CAPTION_LENGTH);
+                       wxSize size = dc.GetTextExtent (good);
+                       dc.SetTextForeground (*wxRED);
+                       dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
+                       dc.SetTextForeground (*wxWHITE);
                }
        }
 }
@@ -71,13 +80,13 @@ ClosedCaptionsDialog::paint ()
 class ClosedCaptionSorter
 {
 public:
-       bool operator() (TextCaption const & a, TextCaption const & b)
+       bool operator() (StringText const & a, StringText const & b)
        {
                return from_top(a) < from_top(b);
        }
 
 private:
-       float from_top (TextCaption const & c) const
+       float from_top (StringText const & c) const
        {
                switch (c.v_align()) {
                case dcp::VALIGN_TOP:
@@ -93,51 +102,64 @@ private:
 };
 
 void
-ClosedCaptionsDialog::refresh (DCPTime time)
+ClosedCaptionsDialog::update (DCPTime time)
 {
-       list<TextCaption> to_show;
-       list<Caption>::iterator i = _captions.begin ();
-       while (i != _captions.end ()) {
-               if (time > i->second.to) {
-                       list<Caption>::iterator tmp = i;
-                       ++i;
-                       _captions.erase (tmp);
-               } else if (i->second.contains (time)) {
-                       BOOST_FOREACH (TextCaption j, i->first.text) {
-                               to_show.push_back (j);
-                       }
-                       ++i;
-               } else {
-                       ++i;
+       if (_current_in_lines && _current->period.to > time) {
+               /* Current one is fine */
+               return;
+       }
+
+       if (_current && _current->period.to < time) {
+               /* Current one has finished; clear out */
+               for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
+                       _lines[j] = "";
                }
+               Refresh ();
+               _current = optional<TextRingBuffers::Data>();
        }
 
-       for (int j = 0; j < _num_lines; ++j) {
-               _lines[j] = "";
+       if (!_current) {
+               /* We have no current one: get another */
+               shared_ptr<Butler> butler = _butler.lock ();
+               DCPOMATIC_ASSERT (butler);
+               _current = butler->get_closed_caption ();
+               _current_in_lines = false;
        }
 
-       to_show.sort (ClosedCaptionSorter());
+       if (_current && _current->period.contains(time)) {
+               /* We need to set this new one up */
 
-       list<TextCaption>::const_iterator j = to_show.begin();
-       int k = 0;
-       while (j != to_show.end() && k < _num_lines) {
-               _lines[k] = j->text();
-               ++j;
-               ++k;
-       }
+               list<StringText> to_show = _current->text.string;
 
-       Refresh ();
+               for (int j = 0; j < CLOSED_CAPTION_LINES; ++j) {
+                       _lines[j] = "";
+               }
+
+               to_show.sort (ClosedCaptionSorter());
+
+               list<StringText>::const_iterator j = to_show.begin();
+               int k = 0;
+               while (j != to_show.end() && k < CLOSED_CAPTION_LINES) {
+                       _lines[k] = j->text();
+                       ++j;
+                       ++k;
+               }
+
+               Refresh ();
+               _current_in_lines = true;
+       }
 }
 
 void
-ClosedCaptionsDialog::caption (PlayerCaption caption, DCPTimePeriod period)
+ClosedCaptionsDialog::clear ()
 {
-       _captions.push_back (make_pair (caption, period));
+       _current = optional<TextRingBuffers::Data>();
+       _current_in_lines = false;
+       Refresh ();
 }
 
 void
-ClosedCaptionsDialog::clear ()
+ClosedCaptionsDialog::set_butler (weak_ptr<Butler> butler)
 {
-       _captions.clear ();
-       Refresh ();
+       _butler = butler;
 }