0949d02a6c7e38cb196e28a97dab60671615bca9
[dcpomatic.git] / src / wx / ffmpeg_content_dialog.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/lexical_cast.hpp>
21 #include "lib/ffmpeg_content.h"
22 #include "ffmpeg_content_dialog.h"
23 #include "wx_util.h"
24
25 using std::vector;
26 using std::string;
27 using boost::shared_ptr;
28 using boost::lexical_cast;
29
30 FFmpegContentDialog::FFmpegContentDialog (wxWindow* parent, shared_ptr<FFmpegContent> content)
31         : wxDialog (parent, wxID_ANY, _("Video"))
32 {
33         wxFlexGridSizer* grid = new wxFlexGridSizer (3, 6, 6);
34         grid->AddGrowableCol (1, 1);
35
36         add_label_to_sizer (grid, this, _("Audio Stream"));
37         _audio_stream = new wxChoice (this, wxID_ANY);
38         grid->Add (_audio_stream, 1, wxEXPAND | wxALL, 6);
39         _audio_description = new wxStaticText (this, wxID_ANY, wxT (""));
40         grid->Add (_audio_description, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
41         
42         add_label_to_sizer (grid, this, _("Subtitle stream"));
43         _subtitle_stream = new wxChoice (this, wxID_ANY);
44         grid->Add (_subtitle_stream, 1, wxEXPAND | wxALL, 6);
45         grid->AddSpacer (0);
46
47         _audio_stream->Clear ();
48         vector<FFmpegAudioStream> a = content->audio_streams ();
49         for (vector<FFmpegAudioStream>::iterator i = a.begin(); i != a.end(); ++i) {
50                 _audio_stream->Append (std_to_wx (i->name), new wxStringClientData (std_to_wx (lexical_cast<string> (i->id))));
51         }
52         
53         if (content->audio_stream()) {
54                 checked_set (_audio_stream, lexical_cast<string> (content->audio_stream()->id));
55         }
56
57         _subtitle_stream->Clear ();
58         vector<FFmpegSubtitleStream> s = content->subtitle_streams ();
59         if (s.empty ()) {
60                 _subtitle_stream->Enable (false);
61         }
62         for (vector<FFmpegSubtitleStream>::iterator i = s.begin(); i != s.end(); ++i) {
63                 _subtitle_stream->Append (std_to_wx (i->name), new wxStringClientData (std_to_wx (lexical_cast<string> (i->id))));
64         }
65         
66         if (content->subtitle_stream()) {
67                 checked_set (_subtitle_stream, lexical_cast<string> (content->subtitle_stream()->id));
68         } else {
69                 _subtitle_stream->SetSelection (wxNOT_FOUND);
70         }
71
72         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
73         overall_sizer->Add (grid, 1, wxEXPAND | wxALL, 6);
74
75         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
76         if (buttons) {
77                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
78         }
79
80         SetSizer (overall_sizer);
81         overall_sizer->Layout ();
82         overall_sizer->SetSizeHints (this);
83
84         _audio_stream->Connect    (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FFmpegContentDialog::audio_stream_changed), 0, this);
85         _subtitle_stream->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FFmpegContentDialog::subtitle_stream_changed), 0, this);
86 }
87
88 void
89 FFmpegContentDialog::audio_stream_changed (wxCommandEvent &)
90 {
91         shared_ptr<FFmpegContent> c = _content.lock ();
92         if (!c) {
93                 return;
94         }
95         
96         vector<FFmpegAudioStream> a = c->audio_streams ();
97         vector<FFmpegAudioStream>::iterator i = a.begin ();
98         string const s = string_client_data (_audio_stream->GetClientObject (_audio_stream->GetSelection ()));
99         while (i != a.end() && lexical_cast<string> (i->id) != s) {
100                 ++i;
101         }
102
103         if (i != a.end ()) {
104                 c->set_audio_stream (*i);
105         }
106
107         if (!c->audio_stream ()) {
108                 _audio_description->SetLabel (wxT (""));
109         } else {
110                 wxString s;
111                 if (c->audio_channels() == 1) {
112                         s << _("1 channel");
113                 } else {
114                         s << c->audio_channels() << wxT (" ") << _("channels");
115                 }
116                 s << wxT (", ") << c->content_audio_frame_rate() << _("Hz");
117                 _audio_description->SetLabel (s);
118         }
119 }
120
121
122
123 void
124 FFmpegContentDialog::subtitle_stream_changed (wxCommandEvent &)
125 {
126         shared_ptr<FFmpegContent> c = _content.lock ();
127         if (!c) {
128                 return;
129         }
130         
131         vector<FFmpegSubtitleStream> a = c->subtitle_streams ();
132         vector<FFmpegSubtitleStream>::iterator i = a.begin ();
133         string const s = string_client_data (_subtitle_stream->GetClientObject (_subtitle_stream->GetSelection ()));
134         while (i != a.end() && lexical_cast<string> (i->id) != s) {
135                 ++i;
136         }
137
138         if (i != a.end ()) {
139                 c->set_subtitle_stream (*i);
140         }
141 }