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