Supporters update.
[dcpomatic.git] / src / wx / text_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
22 #include "film_viewer.h"
23 #include "text_view.h"
24 #include "wx_util.h"
25 #include "lib/audio_decoder.h"
26 #include "lib/config.h"
27 #include "lib/content_text.h"
28 #include "lib/film.h"
29 #include "lib/string_text_file_content.h"
30 #include "lib/string_text_file_decoder.h"
31 #include "lib/text_decoder.h"
32 #include "lib/video_decoder.h"
33
34
35 using std::dynamic_pointer_cast;
36 using std::list;
37 using std::shared_ptr;
38 using boost::bind;
39 using namespace dcpomatic;
40 #if BOOST_VERSION >= 106100
41 using namespace boost::placeholders;
42 #endif
43
44
45 TextView::TextView (
46         wxWindow* parent, shared_ptr<Film> film, shared_ptr<Content> content, shared_ptr<TextContent> text, shared_ptr<Decoder> decoder, FilmViewer& viewer
47         )
48         : wxDialog (parent, wxID_ANY, _("Captions"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
49         , _content (content)
50         , _film_viewer (viewer)
51 {
52         _list = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL);
53
54         {
55                 wxListItem ip;
56                 ip.SetId (0);
57                 ip.SetText (_("Start"));
58                 ip.SetWidth (100);
59                 _list->InsertColumn (0, ip);
60         }
61
62         {
63                 wxListItem ip;
64                 ip.SetId (1);
65                 ip.SetText (_("End"));
66                 ip.SetWidth (100);
67                 _list->InsertColumn (1, ip);
68         }
69
70         {
71                 wxListItem ip;
72                 ip.SetId (2);
73                 ip.SetText (_("Caption"));
74                 ip.SetWidth (640);
75                 _list->InsertColumn (2, ip);
76         }
77
78         auto sizer = new wxBoxSizer (wxVERTICAL);
79         sizer->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
80
81         _list->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&TextView::subtitle_selected, this, _1));
82
83         auto buttons = CreateSeparatedButtonSizer (wxOK);
84         if (buttons) {
85                 sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
86         }
87
88         if (decoder->video) {
89                 decoder->video->set_ignore (true);
90         }
91         if (decoder->audio) {
92                 decoder->audio->set_ignore (true);
93         }
94
95         _subs = 0;
96         _frc = film->active_frame_rate_change (content->position());
97
98         /* Find the decoder that is being used for our TextContent and attach to it */
99         for (auto i: decoder->text) {
100                 if (i->content() == text) {
101                         i->PlainStart.connect (bind (&TextView::data_start, this, _1));
102                         i->Stop.connect (bind (&TextView::data_stop, this, _1));
103                 }
104         }
105         while (!decoder->pass ()) {}
106         SetSizerAndFit (sizer);
107 }
108
109
110 void
111 TextView::data_start (ContentStringText cts)
112 {
113         for (auto const& i: cts.subs) {
114                 wxListItem list_item;
115                 list_item.SetId (_subs);
116                 _list->InsertItem (list_item);
117                 _list->SetItem (_subs, 0, std_to_wx (cts.from().timecode (_frc->source)));
118                 _list->SetItem (_subs, 2, std_to_wx (i.text ()));
119                 _start_times.push_back (cts.from ());
120                 ++_subs;
121         }
122
123         _last_count = cts.subs.size ();
124 }
125
126
127 void
128 TextView::data_stop (ContentTime time)
129 {
130         if (!_last_count) {
131                 return;
132         }
133
134         for (int i = _subs - *_last_count; i < _subs; ++i) {
135                 _list->SetItem (i, 1, std_to_wx (time.timecode (_frc->source)));
136         }
137 }
138
139
140 void
141 TextView::subtitle_selected (wxListEvent& ev)
142 {
143         if (!Config::instance()->jump_to_selected ()) {
144                 return;
145         }
146
147         DCPOMATIC_ASSERT (ev.GetIndex() < int(_start_times.size()));
148         auto lc = _content.lock ();
149         DCPOMATIC_ASSERT (lc);
150         /* Add on a frame here to work around any rounding errors and make sure land in the subtitle */
151         _film_viewer.seek(lc, _start_times[ev.GetIndex()] + ContentTime::from_frames(1, _frc->source), true);
152 }