Basic config dialogue.
[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 "wx/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
80 void
81 maybe_save_then_delete_film ()
82 {
83         if (!film) {
84                 return;
85         }
86                         
87         if (film->dirty ()) {
88                 FilmChangedDialog d;
89                 switch (d.run ()) {
90                 case wxID_NO:
91                         break;
92                 case wxID_YES:
93                         film->write_metadata ();
94                         break;
95                 }
96         }
97         
98         delete film;
99         film = 0;
100 }
101
102 enum Sensitivity {
103         ALWAYS,
104         NEEDS_FILM
105 };
106
107 map<wxMenuItem*, Sensitivity> menu_items;
108         
109 void
110 add_item (wxMenu* menu, std::string text, int id, Sensitivity sens)
111 {
112         wxMenuItem* item = menu->Append (id, std_to_wx (text));
113         menu_items.insert (make_pair (item, sens));
114 }
115
116 void
117 set_menu_sensitivity ()
118 {
119         for (map<wxMenuItem*, Sensitivity>::iterator i = menu_items.begin(); i != menu_items.end(); ++i) {
120                 if (i->second == NEEDS_FILM) {
121                         i->first->Enable (film != 0);
122                 } else {
123                         i->first->Enable (true);
124                 }
125         }
126 }
127
128 enum {
129         ID_file_new = 1,
130         ID_file_open,
131         ID_file_save,
132         ID_file_quit,
133         ID_edit_preferences,
134         ID_jobs_make_dcp,
135         ID_jobs_send_dcp_to_tms,
136         ID_jobs_copy_from_dvd,
137         ID_jobs_examine_content,
138         ID_jobs_make_dcp_from_existing_transcode,
139         ID_help_about
140 };
141
142 void
143 setup_menu (wxMenuBar* m)
144 {
145         wxMenu* file = new wxMenu;
146         add_item (file, "New...", ID_file_new, ALWAYS);
147         add_item (file, "&Open...", ID_file_open, ALWAYS);
148         file->AppendSeparator ();
149         add_item (file, "&Save", ID_file_save, NEEDS_FILM);
150         file->AppendSeparator ();
151         add_item (file, "&Quit", ID_file_quit, ALWAYS);
152
153         wxMenu* edit = new wxMenu;
154         add_item (edit, "&Preferences...", ID_edit_preferences, ALWAYS);
155
156         wxMenu* jobs = new wxMenu;
157         add_item (jobs, "&Make DCP", ID_jobs_make_dcp, NEEDS_FILM);
158         add_item (jobs, "&Send DCP to TMS", ID_jobs_send_dcp_to_tms, NEEDS_FILM);
159         add_item (jobs, "Copy from &DVD...", ID_jobs_copy_from_dvd, NEEDS_FILM);
160         jobs->AppendSeparator ();
161         add_item (jobs, "&Examine content", ID_jobs_examine_content, NEEDS_FILM);
162         add_item (jobs, "Make DCP from existing &transcode", ID_jobs_make_dcp_from_existing_transcode, NEEDS_FILM);
163
164         wxMenu* help = new wxMenu;
165         add_item (help, "About", ID_help_about, ALWAYS);
166
167         m->Append (file, _("&File"));
168         m->Append (edit, _("&Edit"));
169         m->Append (jobs, _("&Jobs"));
170         m->Append (help, _("&Help"));
171 }
172
173 bool
174 window_closed (wxCommandEvent &)
175 {
176         maybe_save_then_delete_film ();
177         return false;
178 }
179
180 void
181 file_changed (string f)
182 {
183         stringstream s;
184         s << "DVD-o-matic";
185         if (!f.empty ()) {
186                 s << " — " << f;
187         }
188         
189         frame->SetTitle (std_to_wx (s.str()));
190 }
191
192 class Frame : public wxFrame
193 {
194 public:
195         Frame (wxString const & title)
196                 : wxFrame (NULL, -1, title)
197         {
198                 wxMenuBar* bar = new wxMenuBar;
199                 setup_menu (bar);
200                 SetMenuBar (bar);
201         }
202
203         void file_new (wxCommandEvent &)
204         {
205                 wxDirDialog* c = new wxDirDialog (frame, wxT ("New Film"));
206                 int const r = c->ShowModal ();
207                 c->Destroy ();
208                 
209                 if (r == wxID_OK) {
210                         maybe_save_then_delete_film ();
211                         film = new Film (wx_to_std (c->GetPath ()));
212 #if BOOST_FILESYSTEM_VERSION == 3               
213                         film->set_name (filesystem::path (wx_to_std (c->GetPath())).filename().generic_string());
214 #else           
215                         film->set_name (filesystem::path (wx_to_std (c->GetPath())).filename());
216 #endif          
217                         film_viewer->set_film (film);
218                         film_editor->set_film (film);
219                         set_menu_sensitivity ();
220                 }
221         }
222
223         void file_open (wxCommandEvent &)
224         {
225                 wxDirDialog* c = new wxDirDialog (frame, wxT ("Open Film"), wxT (""), wxDD_DIR_MUST_EXIST);
226                 int const r = c->ShowModal ();
227                 c->Destroy ();
228                 
229                 if (r == wxID_OK) {
230                         maybe_save_then_delete_film ();
231                         film = new Film (wx_to_std (c->GetPath ()));
232                         film_viewer->set_film (film);
233                         film_editor->set_film (film);
234                         set_menu_sensitivity ();
235                 }
236         }
237
238         void file_save (wxCommandEvent &)
239         {
240                 film->write_metadata ();
241         }
242         
243         void file_quit (wxCommandEvent &)
244         {
245                 maybe_save_then_delete_film ();
246                 frame->Close (true);
247         }
248
249         void edit_preferences (wxCommandEvent &)
250         {
251                 ConfigDialog* d = new ConfigDialog (this);
252                 d->ShowModal ();
253                 d->Destroy ();
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)