Sort-of open directory dialogs in My Documents.
[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/stdpaths.h>
24 #include "wx/film_viewer.h"
25 #include "wx/film_editor.h"
26 #ifndef DVDOMATIC_DISABLE_PLAYER
27 #include "wx/film_player.h"
28 #endif
29 #include "wx/job_manager_view.h"
30 #include "wx/config_dialog.h"
31 #include "wx/job_wrapper.h"
32 //#include "gtk/dvd_title_dialog.h"
33 #include "wx/wx_util.h"
34 #include "lib/film.h"
35 #include "lib/format.h"
36 #include "lib/config.h"
37 #include "lib/filter.h"
38 #include "lib/util.h"
39 #include "lib/scaler.h"
40 #include "lib/exceptions.h"
41
42 using namespace std;
43 using namespace boost;
44
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 (0, 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 class Frame : public wxFrame
181 {
182 public:
183         Frame (wxString const & title)
184                 : wxFrame (NULL, -1, title)
185         {
186                 wxMenuBar* bar = new wxMenuBar;
187                 setup_menu (bar);
188                 SetMenuBar (bar);
189
190                 Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
191                 Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
192                 Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
193                 Connect (ID_file_quit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_quit));
194                 Connect (ID_edit_preferences, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
195                 Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
196                 Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
197                 Connect (ID_jobs_copy_from_dvd, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_copy_from_dvd));
198                 Connect (ID_jobs_examine_content, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_examine_content));
199                 Connect (ID_jobs_make_dcp_from_existing_transcode, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp_from_existing_transcode));
200                 Connect (ID_help_about, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
201
202                 wxPanel* panel = new wxPanel (this);
203                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
204                 s->Add (panel, 1, wxEXPAND);
205                 SetSizer (s);
206
207                 film_editor = new FilmEditor (film, panel);
208                 film_viewer = new FilmViewer (film, panel);
209 #ifndef DVDOMATIC_DISABLE_PLAYER
210                 film_player = new FilmPlayer (film, panel);
211 #endif
212                 JobManagerView* job_manager_view = new JobManagerView (panel);
213
214                 wxSizer* rhs_sizer = new wxBoxSizer (wxVERTICAL);
215                 rhs_sizer->Add (film_viewer, 3, wxEXPAND | wxALL);
216                 rhs_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL);
217
218                 wxBoxSizer* main_sizer = new wxBoxSizer (wxHORIZONTAL);
219                 main_sizer->Add (film_editor, 0, wxALL, 6);
220                 main_sizer->Add (rhs_sizer, 1, wxEXPAND | wxALL, 6);
221                 panel->SetSizer (main_sizer);
222
223                 set_menu_sensitivity ();
224
225                 /* XXX: calling these here is a bit of a hack */
226                 film_editor->setup_visibility ();
227 #ifndef DVDOMATIC_DISABLE_PLAYER        
228                 film_player->setup_visibility ();
229 #endif  
230                 film_viewer->setup_visibility ();
231                 
232                 film_editor->FileChanged.connect (sigc::mem_fun (*this, &Frame::file_changed));
233                 if (film) {
234                         file_changed (film->directory ());
235                 } else {
236                         file_changed ("");
237                 }
238         }
239
240         void
241         file_changed (string f)
242         {
243                 stringstream s;
244                 s << "DVD-o-matic";
245                 if (!f.empty ()) {
246                         s << " - " << f;
247                 }
248                 
249                 SetTitle (std_to_wx (s.str()));
250         }
251         
252         void file_new (wxCommandEvent &)
253         {
254                 wxDirDialog* c = new wxDirDialog (this, wxT ("New Film"), wxStandardPaths::Get().GetDocumentsDir());
255                 int const r = c->ShowModal ();
256                 c->Destroy ();
257                 
258                 if (r == wxID_OK) {
259                         maybe_save_then_delete_film ();
260                         film = new Film (wx_to_std (c->GetPath ()));
261 #if BOOST_FILESYSTEM_VERSION == 3               
262                         film->set_name (filesystem::path (wx_to_std (c->GetPath())).filename().generic_string());
263 #else           
264                         film->set_name (filesystem::path (wx_to_std (c->GetPath())).filename());
265 #endif          
266                         film_viewer->set_film (film);
267                         film_editor->set_film (film);
268                         set_menu_sensitivity ();
269                 }
270         }
271
272         void file_open (wxCommandEvent &)
273         {
274                 wxDirDialog* c = new wxDirDialog (this, wxT ("Open Film"), wxStandardPaths::Get().GetDocumentsDir(), wxDD_DIR_MUST_EXIST);
275                 int const r = c->ShowModal ();
276                 c->Destroy ();
277                 
278                 if (r == wxID_OK) {
279                         maybe_save_then_delete_film ();
280                         film = new Film (wx_to_std (c->GetPath ()));
281                         film_viewer->set_film (film);
282                         film_editor->set_film (film);
283                         set_menu_sensitivity ();
284                 }
285         }
286
287         void file_save (wxCommandEvent &)
288         {
289                 film->write_metadata ();
290         }
291         
292         void file_quit (wxCommandEvent &)
293         {
294                 maybe_save_then_delete_film ();
295                 Close (true);
296         }
297
298         void edit_preferences (wxCommandEvent &)
299         {
300                 ConfigDialog* d = new ConfigDialog (this);
301                 d->ShowModal ();
302                 d->Destroy ();
303                 Config::instance()->write ();
304         }
305
306         void jobs_make_dcp (wxCommandEvent &)
307         {
308                 JobWrapper::make_dcp (this, film, true);
309         }
310         
311         void jobs_make_dcp_from_existing_transcode (wxCommandEvent &)
312         {
313                 JobWrapper::make_dcp (this, film, false);
314         }
315         
316         void jobs_copy_from_dvd (wxCommandEvent &)
317         {
318 //      try {
319 //              DVDTitleDialog d;
320 //              if (d.run () != Gtk::RESPONSE_OK) {
321 //                      return;
322 //              }
323 //              film->copy_from_dvd ();
324 //      } catch (DVDError& e) {
325 //              error_dialog (e.what ());
326 //      }
327         }
328         
329         void jobs_send_dcp_to_tms (wxCommandEvent &)
330         {
331                 film->send_dcp_to_tms ();
332         }
333         
334         void jobs_examine_content (wxCommandEvent &)
335         {
336                 film->examine_content ();
337         }
338         
339         void help_about (wxCommandEvent &)
340         {
341                 wxAboutDialogInfo info;
342                 info.SetName (_("DVD-o-matic"));
343                 info.SetVersion (wxT (DVDOMATIC_VERSION));
344                 info.SetDescription (_("DCP generation from arbitrary formats"));
345                 info.SetCopyright (_("(C) Carl Hetherington, Terrence Meiczinger, Paul Davis"));
346                 wxArrayString authors;
347                 authors.Add (wxT ("Carl Hetherington"));
348                 authors.Add (wxT ("Terrence Meiczinger"));
349                 authors.Add (wxT ("Paul Davis"));
350                 info.SetDevelopers (authors);
351                 info.SetWebSite (wxT ("http://carlh.net/software/dvdomatic"));
352                 wxAboutBox (info);
353         }
354 };
355
356 class App : public wxApp
357 {
358         bool OnInit ()
359         {
360                 if (!wxApp::OnInit ()) {
361                         return false;
362                 }
363
364                 wxInitAllImageHandlers ();
365                 
366                 dvdomatic_setup ();
367
368 //              if (argc == 2 && boost::filesystem::is_directory (argv[1])) {
369 //                      film = new Film (argv[1]);
370 //              }
371
372                 Frame* f = new Frame (_("DVD-o-matic"));
373                 SetTopWindow (f);
374                 f->Maximize ();
375                 f->Show ();
376                 return true;
377         }
378 };
379
380 IMPLEMENT_APP (App)