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