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