Enable some more bits.
[dcpomatic.git] / src / tools / dvdomatic.cc
1 /*
2     Copyright (C) 2012 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 <iostream>
21 #include <boost/filesystem.hpp>
22 #include <wx/aboutdlg.h>
23 #include "wx/film_viewer.h"
24 #include "wx/film_editor.h"
25 #ifndef DVDOMATIC_DISABLE_PLAYER
26 #include "wx/film_player.h"
27 #endif
28 #include "wx/job_manager_view.h"
29 //#include "gtk/config_dialog.h"
30 #include "wx/job_wrapper.h"
31 //#include "gtk/dvd_title_dialog.h"
32 #include "wx/wx_util.h"
33 #include "lib/film.h"
34 #include "lib/format.h"
35 #include "lib/config.h"
36 #include "lib/filter.h"
37 #include "lib/util.h"
38 #include "lib/scaler.h"
39 #include "lib/exceptions.h"
40
41 using namespace std;
42 using namespace boost;
43
44 static wxFrame* frame = 0;
45 static FilmEditor* film_editor = 0;
46 static FilmViewer* film_viewer = 0;
47
48 #ifndef DVDOMATIC_DISABLE_PLAYER
49 static FilmPlayer* film_player = 0;
50 #endif
51 static Film* film = 0;
52
53 static void set_menu_sensitivity ();
54
55 class FilmChangedDialog
56 {
57 public:
58         FilmChangedDialog ()
59         {
60                 stringstream s;
61                 s << "Save changes to film \"" << film->name() << "\" before closing?";
62                 _dialog = new wxMessageDialog (frame, std_to_wx (s.str()), wxT ("Film changed"), wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION);
63         }
64
65         ~FilmChangedDialog ()
66         {
67                 _dialog->Destroy ();
68         }
69
70         int run ()
71         {
72                 return _dialog->ShowModal ();
73         }
74
75 private:        
76         wxMessageDialog* _dialog;
77 };
78
79 void
80 maybe_save_then_delete_film ()
81 {
82         if (!film) {
83                 return;
84         }
85                         
86         if (film->dirty ()) {
87                 FilmChangedDialog d;
88                 switch (d.run ()) {
89                 case wxID_NO:
90                         break;
91                 case wxID_YES:
92                         film->write_metadata ();
93                         break;
94                 }
95         }
96         
97         delete film;
98         film = 0;
99 }
100
101
102
103 enum Sensitivity {
104         ALWAYS,
105         NEEDS_FILM
106 };
107
108 map<wxMenuItem*, Sensitivity> menu_items;
109         
110 void
111 add_item (wxMenu* menu, std::string text, int id, Sensitivity sens)
112 {
113         wxMenuItem* item = menu->Append (id, std_to_wx (text));
114         menu_items.insert (make_pair (item, sens));
115 }
116
117 void
118 set_menu_sensitivity ()
119 {
120         for (map<wxMenuItem*, Sensitivity>::iterator i = menu_items.begin(); i != menu_items.end(); ++i) {
121                 if (i->second == NEEDS_FILM) {
122                         i->first->Enable (film != 0);
123                 } else {
124                         i->first->Enable (true);
125                 }
126         }
127 }
128
129 enum {
130         ID_file_new = 1,
131         ID_file_open,
132         ID_file_save,
133         ID_file_quit,
134         ID_edit_preferences,
135         ID_jobs_make_dcp,
136         ID_jobs_send_dcp_to_tms,
137         ID_jobs_copy_from_dvd,
138         ID_jobs_examine_content,
139         ID_jobs_make_dcp_from_existing_transcode,
140         ID_help_about
141 };
142
143 void
144 setup_menu (wxMenuBar* m)
145 {
146         wxMenu* file = new wxMenu;
147         add_item (file, "New...", ID_file_new, ALWAYS);
148         add_item (file, "&Open...", ID_file_open, ALWAYS);
149         file->AppendSeparator ();
150         add_item (file, "&Save", ID_file_save, NEEDS_FILM);
151         file->AppendSeparator ();
152         add_item (file, "&Quit", ID_file_quit, ALWAYS);
153
154         wxMenu* edit = new wxMenu;
155         add_item (edit, "&Preferences...", ID_edit_preferences, ALWAYS);
156
157         wxMenu* jobs = new wxMenu;
158         add_item (jobs, "&Make DCP", ID_jobs_make_dcp, NEEDS_FILM);
159         add_item (jobs, "&Send DCP to TMS", ID_jobs_send_dcp_to_tms, NEEDS_FILM);
160         add_item (jobs, "Copy from &DVD...", ID_jobs_copy_from_dvd, NEEDS_FILM);
161         jobs->AppendSeparator ();
162         add_item (jobs, "&Examine content", ID_jobs_examine_content, NEEDS_FILM);
163         add_item (jobs, "Make DCP from existing &transcode", ID_jobs_make_dcp_from_existing_transcode, NEEDS_FILM);
164
165         wxMenu* help = new wxMenu;
166         add_item (help, "About", ID_help_about, ALWAYS);
167
168         m->Append (file, _("&File"));
169         m->Append (edit, _("&Edit"));
170         m->Append (jobs, _("&Jobs"));
171         m->Append (help, _("&Help"));
172 }
173
174 bool
175 window_closed (wxCommandEvent &)
176 {
177         maybe_save_then_delete_film ();
178         return false;
179 }
180
181 void
182 file_changed (string f)
183 {
184         stringstream s;
185         s << "DVD-o-matic";
186         if (!f.empty ()) {
187                 s << " — " << f;
188         }
189         
190         frame->SetTitle (std_to_wx (s.str()));
191 }
192
193 class Frame : public wxFrame
194 {
195 public:
196         Frame (wxString const & title)
197                 : wxFrame (NULL, -1, title)
198         {
199                 wxMenuBar* bar = new wxMenuBar;
200                 setup_menu (bar);
201                 SetMenuBar (bar);
202         }
203
204         void file_new (wxCommandEvent &)
205         {
206                 wxDirDialog* c = new wxDirDialog (frame, wxT ("New Film"));
207                 int const r = c->ShowModal ();
208                 c->Destroy ();
209                 
210                 if (r == wxID_OK) {
211                         maybe_save_then_delete_film ();
212                         film = new Film (wx_to_std (c->GetPath ()));
213 #if BOOST_FILESYSTEM_VERSION == 3               
214                         film->set_name (filesystem::path (wx_to_std (c->GetPath())).filename().generic_string());
215 #else           
216                         film->set_name (filesystem::path (wx_to_std (c->GetPath())).filename());
217 #endif          
218                         film_viewer->set_film (film);
219                         film_editor->set_film (film);
220                         set_menu_sensitivity ();
221                 }
222         }
223
224         void file_open (wxCommandEvent &)
225         {
226                 wxDirDialog* c = new wxDirDialog (frame, wxT ("Open Film"), wxT (""), wxDD_DIR_MUST_EXIST);
227                 int const r = c->ShowModal ();
228                 c->Destroy ();
229                 
230                 if (r == wxID_OK) {
231                         maybe_save_then_delete_film ();
232                         film = new Film (wx_to_std (c->GetPath ()));
233                         film_viewer->set_film (film);
234                         film_editor->set_film (film);
235                         set_menu_sensitivity ();
236                 }
237         }
238
239         void file_save (wxCommandEvent &)
240         {
241                 film->write_metadata ();
242         }
243         
244         void file_quit (wxCommandEvent &)
245         {
246                 maybe_save_then_delete_film ();
247                 frame->Close (true);
248         }
249
250         void edit_preferences (wxCommandEvent &)
251         {
252 //      ConfigDialog d;
253 //      d.run ();
254 //      Config::instance()->write ();
255         }
256
257         void jobs_make_dcp (wxCommandEvent &)
258         {
259                 JobWrapper::make_dcp (this, film, true);
260         }
261         
262         void jobs_make_dcp_from_existing_transcode (wxCommandEvent &)
263         {
264                 JobWrapper::make_dcp (this, film, false);
265         }
266         
267         void jobs_copy_from_dvd (wxCommandEvent &)
268         {
269 //      try {
270 //              DVDTitleDialog d;
271 //              if (d.run () != Gtk::RESPONSE_OK) {
272 //                      return;
273 //              }
274 //              film->copy_from_dvd ();
275 //      } catch (DVDError& e) {
276 //              error_dialog (e.what ());
277 //      }
278         }
279         
280         void jobs_send_dcp_to_tms (wxCommandEvent &)
281         {
282                 film->send_dcp_to_tms ();
283         }
284         
285         void jobs_examine_content (wxCommandEvent &)
286         {
287                 film->examine_content ();
288         }
289         
290         void help_about (wxCommandEvent &)
291         {
292                 wxAboutDialogInfo info;
293                 info.SetName (_("DVD-o-matic"));
294                 info.SetVersion (wxT (DVDOMATIC_VERSION));
295                 info.SetDescription (_("DCP generation from arbitrary formats"));
296                 info.SetCopyright (_("(C) Carl Hetherington, Terrence Meiczinger, Paul Davis"));
297                 wxArrayString authors;
298                 authors.Add (wxT ("Carl Hetherington"));
299                 authors.Add (wxT ("Terrence Meiczinger"));
300                 authors.Add (wxT ("Paul Davis"));
301                 info.SetDevelopers (authors);
302                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
303                 wxAboutBox (info);
304         }
305 };
306
307 class App : public wxApp
308 {
309         bool OnInit ()
310         {
311                 if (!wxApp::OnInit ()) {
312                         return false;
313                 }
314
315                 wxInitAllImageHandlers ();
316                 
317                 dvdomatic_setup ();
318
319 //              if (argc == 2 && boost::filesystem::is_directory (argv[1])) {
320 //                      film = new Film (argv[1]);
321 //              }
322                 
323                 frame = new Frame (_("DVD-o-matic"));
324                 frame->Show (true);
325                 
326                 frame->Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
327                 frame->Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
328                 frame->Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
329                 frame->Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
330                 frame->Connect (ID_edit_preferences, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
331                 frame->Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
332                 frame->Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
333                 frame->Connect (ID_jobs_copy_from_dvd, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_copy_from_dvd));
334                 frame->Connect (ID_jobs_examine_content, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_examine_content));
335                 frame->Connect (ID_jobs_make_dcp_from_existing_transcode, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp_from_existing_transcode));
336                 frame->Connect (ID_help_about, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
337
338                 wxPanel* rhs = new wxPanel (frame);
339                 
340                 film_editor = new FilmEditor (film, frame);
341                 film_viewer = new FilmViewer (film, rhs);
342 #ifndef DVDOMATIC_DISABLE_PLAYER
343                 film_player = new FilmPlayer (film, frame);
344 #endif
345                 JobManagerView* job_manager_view = new JobManagerView (rhs);
346
347                 wxSizer* rhs_sizer = new wxBoxSizer (wxVERTICAL);
348                 rhs_sizer->Add (film_viewer, 3, wxEXPAND | wxALL);
349                 rhs_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL);
350                 rhs->SetSizer (rhs_sizer);
351
352                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
353                 main_sizer->Add (film_editor, 0, wxALL, 6);
354                 main_sizer->Add (rhs, 1, wxEXPAND | wxALL, 6);
355                 frame->SetSizer (main_sizer);
356
357                 SetTopWindow (frame);
358                 frame->Maximize ();
359
360                 set_menu_sensitivity ();
361
362                 /* XXX: calling these here is a bit of a hack */
363                 film_editor->setup_visibility ();
364 #ifndef DVDOMATIC_DISABLE_PLAYER        
365                 film_player->setup_visibility ();
366 #endif  
367                 film_viewer->setup_visibility ();
368                 
369                 film_editor->FileChanged.connect (ptr_fun (file_changed));
370                 if (film) {
371                         file_changed (film->directory ());
372                 } else {
373                         file_changed ("");
374                 }
375                 
376                 return true;
377         }
378 };
379
380 IMPLEMENT_APP (App)