e6e63efffdcf862be274b590ed6b4100ee715612
[dcpomatic.git] / src / wx / caption_view.cc
1 /*
2     Copyright (C) 2014-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "lib/text_caption_file_decoder.h"
22 #include "lib/content_caption.h"
23 #include "lib/video_decoder.h"
24 #include "lib/audio_decoder.h"
25 #include "lib/film.h"
26 #include "lib/config.h"
27 #include "lib/text_caption_file_content.h"
28 #include "lib/caption_decoder.h"
29 #include "caption_view.h"
30 #include "film_viewer.h"
31 #include "wx_util.h"
32
33 using std::list;
34 using boost::shared_ptr;
35 using boost::bind;
36 using boost::dynamic_pointer_cast;
37
38 CaptionView::CaptionView (wxWindow* parent, shared_ptr<Film> film, shared_ptr<Content> content, shared_ptr<CaptionContent> caption, shared_ptr<Decoder> decoder, FilmViewer* viewer)
39         : wxDialog (parent, wxID_ANY, _("Captions"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
40         , _content (content)
41         , _film_viewer (viewer)
42 {
43         _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL);
44
45         {
46                 wxListItem ip;
47                 ip.SetId (0);
48                 ip.SetText (_("Start"));
49                 ip.SetWidth (100);
50                 _list->InsertColumn (0, ip);
51         }
52
53         {
54                 wxListItem ip;
55                 ip.SetId (1);
56                 ip.SetText (_("End"));
57                 ip.SetWidth (100);
58                 _list->InsertColumn (1, ip);
59         }
60
61         {
62                 wxListItem ip;
63                 ip.SetId (2);
64                 ip.SetText (_("Caption"));
65                 ip.SetWidth (640);
66                 _list->InsertColumn (2, ip);
67         }
68
69         wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
70         sizer->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
71
72         _list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&CaptionView::subtitle_selected, this, _1));
73
74         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
75         if (buttons) {
76                 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
77         }
78
79         if (decoder->video) {
80                 decoder->video->set_ignore (true);
81         }
82         if (decoder->audio) {
83                 decoder->audio->set_ignore (true);
84         }
85
86         _subs = 0;
87         _frc = film->active_frame_rate_change (content->position());
88
89         /* Find the decoder that is being used for our CaptionContent and attach to it */
90         BOOST_FOREACH (shared_ptr<CaptionDecoder> i, decoder->caption) {
91                 if (i->content() == caption) {
92                         i->PlainStart.connect (bind (&CaptionView::data_start, this, _1));
93                         i->Stop.connect (bind (&CaptionView::data_stop, this, _1));
94                 }
95         }
96         while (!decoder->pass ()) {}
97         SetSizerAndFit (sizer);
98 }
99
100 void
101 CaptionView::data_start (ContentTextCaption cts)
102 {
103         BOOST_FOREACH (dcp::SubtitleString const & i, cts.subs) {
104                 wxListItem list_item;
105                 list_item.SetId (_subs);
106                 _list->InsertItem (list_item);
107                 _list->SetItem (_subs, 0, std_to_wx (cts.from().timecode (_frc->source)));
108                 _list->SetItem (_subs, 2, std_to_wx (i.text ()));
109                 _start_times.push_back (cts.from ());
110                 ++_subs;
111         }
112
113         _last_count = cts.subs.size ();
114 }
115
116 void
117 CaptionView::data_stop (ContentTime time)
118 {
119         if (!_last_count) {
120                 return;
121         }
122
123         for (int i = _subs - *_last_count; i < _subs; ++i) {
124                 _list->SetItem (i, 1, std_to_wx (time.timecode (_frc->source)));
125         }
126 }
127
128 void
129 CaptionView::subtitle_selected (wxListEvent& ev)
130 {
131         if (!Config::instance()->jump_to_selected ()) {
132                 return;
133         }
134
135         DCPOMATIC_ASSERT (ev.GetIndex() < int(_start_times.size()));
136         shared_ptr<Content> locked = _content.lock ();
137         DCPOMATIC_ASSERT (locked);
138         _film_viewer->set_position (locked, _start_times[ev.GetIndex()]);
139 }