PlainText -> PlainTextFile.
[dcpomatic.git] / src / wx / subtitle_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/plain_text_file_decoder.h"
22 #include "lib/content_text.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/plain_text_file_content.h"
28 #include "lib/text_decoder.h"
29 #include "subtitle_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 SubtitleView::SubtitleView (wxWindow* parent, shared_ptr<Film> film, shared_ptr<Content> content, shared_ptr<Decoder> decoder, FilmViewer* viewer)
39         : wxDialog (parent, wxID_ANY, _("Subtitles"), 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 (_("Subtitle"));
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 (&SubtitleView::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         decoder->subtitle->PlainStart.connect (bind (&SubtitleView::data_start, this, _1));
89         decoder->subtitle->Stop.connect (bind (&SubtitleView::data_stop, this, _1));
90         while (!decoder->pass ()) {}
91         SetSizerAndFit (sizer);
92 }
93
94 void
95 SubtitleView::data_start (ContentPlainText cts)
96 {
97         BOOST_FOREACH (dcp::SubtitleString const & i, cts.subs) {
98                 wxListItem list_item;
99                 list_item.SetId (_subs);
100                 _list->InsertItem (list_item);
101                 _list->SetItem (_subs, 0, std_to_wx (cts.from().timecode (_frc->source)));
102                 _list->SetItem (_subs, 2, std_to_wx (i.text ()));
103                 _start_times.push_back (cts.from ());
104                 ++_subs;
105         }
106
107         _last_count = cts.subs.size ();
108 }
109
110 void
111 SubtitleView::data_stop (ContentTime time)
112 {
113         if (!_last_count) {
114                 return;
115         }
116
117         for (int i = _subs - *_last_count; i < _subs; ++i) {
118                 _list->SetItem (i, 1, std_to_wx (time.timecode (_frc->source)));
119         }
120 }
121
122 void
123 SubtitleView::subtitle_selected (wxListEvent& ev)
124 {
125         if (!Config::instance()->jump_to_selected ()) {
126                 return;
127         }
128
129         DCPOMATIC_ASSERT (ev.GetIndex() < int(_start_times.size()));
130         shared_ptr<Content> locked = _content.lock ();
131         DCPOMATIC_ASSERT (locked);
132         _film_viewer->set_position (locked, _start_times[ev.GetIndex()]);
133 }