2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
22 #include <boost/filesystem.hpp>
27 #include <ApplicationServices/ApplicationServices.h>
29 #include <wx/generic/aboutdlgg.h>
30 #include <wx/stdpaths.h>
31 #include <wx/cmdline.h>
32 #include "wx/film_viewer.h"
33 #include "wx/film_editor.h"
34 #include "wx/job_manager_view.h"
35 #include "wx/config_dialog.h"
36 #include "wx/job_wrapper.h"
37 #include "wx/wx_util.h"
38 #include "wx/new_film_dialog.h"
39 #include "wx/properties_dialog.h"
40 #include "wx/wx_ui_signaller.h"
41 #include "wx/about_dialog.h"
43 #include "lib/format.h"
44 #include "lib/config.h"
45 #include "lib/filter.h"
47 #include "lib/scaler.h"
48 #include "lib/exceptions.h"
49 #include "lib/ui_signaller.h"
55 using std::stringstream;
60 using boost::shared_ptr;
62 static FilmEditor* film_editor = 0;
63 static FilmViewer* film_viewer = 0;
64 static shared_ptr<Film> film;
65 static std::string log_level;
66 static std::string film_to_load;
67 static wxMenu* jobs_menu = 0;
69 static void set_menu_sensitivity ();
71 class FilmChangedDialog
76 _dialog = new wxMessageDialog (
78 wxString::Format (_("Save changes to film \"%s\" before closing?"), std_to_wx (film->name ()).data()),
80 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
91 return _dialog->ShowModal ();
95 wxMessageDialog* _dialog;
100 maybe_save_then_delete_film ()
106 if (film->dirty ()) {
112 film->write_metadata ();
125 map<wxMenuItem*, Sensitivity> menu_items;
128 add_item (wxMenu* menu, wxString text, int id, Sensitivity sens)
130 wxMenuItem* item = menu->Append (id, text);
131 menu_items.insert (make_pair (item, sens));
135 set_menu_sensitivity ()
137 for (map<wxMenuItem*, Sensitivity>::iterator i = menu_items.begin(); i != menu_items.end(); ++i) {
138 if (i->second == NEEDS_FILM) {
139 i->first->Enable (film != 0);
141 i->first->Enable (true);
152 ID_jobs_send_dcp_to_tms,
154 ID_jobs_analyse_audio,
158 setup_menu (wxMenuBar* m)
160 wxMenu* file = new wxMenu;
161 add_item (file, _("New..."), ID_file_new, ALWAYS);
162 add_item (file, _("&Open..."), ID_file_open, ALWAYS);
163 file->AppendSeparator ();
164 add_item (file, _("&Save"), ID_file_save, NEEDS_FILM);
165 file->AppendSeparator ();
166 add_item (file, _("&Properties..."), ID_file_properties, NEEDS_FILM);
168 file->AppendSeparator ();
170 add_item (file, _("&Exit"), wxID_EXIT, ALWAYS);
173 add_item (file, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
175 wxMenu* edit = new wxMenu;
176 add_item (edit, _("&Preferences..."), wxID_PREFERENCES, ALWAYS);
179 jobs_menu = new wxMenu;
180 add_item (jobs_menu, _("&Make DCP"), ID_jobs_make_dcp, NEEDS_FILM);
181 add_item (jobs_menu, _("&Send DCP to TMS"), ID_jobs_send_dcp_to_tms, NEEDS_FILM);
182 add_item (jobs_menu, _("S&how DCP"), ID_jobs_show_dcp, NEEDS_FILM);
183 jobs_menu->AppendSeparator ();
184 add_item (jobs_menu, _("&Analyse audio"), ID_jobs_analyse_audio, NEEDS_FILM);
186 wxMenu* help = new wxMenu;
188 add_item (help, _("About DVD-o-matic"), wxID_ABOUT, ALWAYS);
190 add_item (help, _("About"), wxID_ABOUT, ALWAYS);
193 m->Append (file, _("&File"));
195 m->Append (edit, _("&Edit"));
197 m->Append (jobs_menu, _("&Jobs"));
198 m->Append (help, _("&Help"));
202 window_closed (wxCommandEvent &)
204 maybe_save_then_delete_film ();
208 class Frame : public wxFrame
211 Frame (wxString const & title)
212 : wxFrame (NULL, -1, title)
214 wxMenuBar* bar = new wxMenuBar;
218 Connect (ID_file_new, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_new));
219 Connect (ID_file_open, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_open));
220 Connect (ID_file_save, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_save));
221 Connect (ID_file_properties, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_properties));
222 Connect (wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::file_exit));
223 Connect (wxID_PREFERENCES, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::edit_preferences));
224 Connect (ID_jobs_make_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_make_dcp));
225 Connect (ID_jobs_send_dcp_to_tms, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_send_dcp_to_tms));
226 Connect (ID_jobs_show_dcp, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_show_dcp));
227 Connect (ID_jobs_analyse_audio, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::jobs_analyse_audio));
228 Connect (wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler (Frame::help_about));
230 Connect (wxID_ANY, wxEVT_MENU_OPEN, wxMenuEventHandler (Frame::menu_opened));
232 wxPanel* panel = new wxPanel (this);
233 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
234 s->Add (panel, 1, wxEXPAND);
237 film_editor = new FilmEditor (film, panel);
238 film_viewer = new FilmViewer (film, panel);
239 JobManagerView* job_manager_view = new JobManagerView (panel, static_cast<JobManagerView::Buttons> (0));
241 _top_sizer = new wxBoxSizer (wxHORIZONTAL);
242 _top_sizer->Add (film_editor, 0, wxALL, 6);
243 _top_sizer->Add (film_viewer, 1, wxEXPAND | wxALL, 6);
245 wxBoxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
246 main_sizer->Add (_top_sizer, 2, wxEXPAND | wxALL, 6);
247 main_sizer->Add (job_manager_view, 1, wxEXPAND | wxALL, 6);
248 panel->SetSizer (main_sizer);
250 set_menu_sensitivity ();
252 /* XXX: calling these here is a bit of a hack */
253 film_editor->setup_visibility ();
255 film_editor->FileChanged.connect (bind (&Frame::file_changed, this, _1));
257 file_changed (film->directory ());
264 film_editor->Connect (wxID_ANY, wxEVT_SIZE, wxSizeEventHandler (Frame::film_editor_sized), 0, this);
269 void film_editor_sized (wxSizeEvent &)
271 static bool in_layout = false;
274 _top_sizer->Layout ();
279 void menu_opened (wxMenuEvent& ev)
281 if (ev.GetMenu() != jobs_menu) {
285 bool const have_dcp = film && film->have_dcp();
286 jobs_menu->Enable (ID_jobs_send_dcp_to_tms, have_dcp);
287 jobs_menu->Enable (ID_jobs_show_dcp, have_dcp);
292 film_viewer->set_film (film);
293 film_editor->set_film (film);
294 set_menu_sensitivity ();
297 void file_changed (string f)
300 s << wx_to_std (_("DVD-o-matic"));
305 SetTitle (std_to_wx (s.str()));
308 void file_new (wxCommandEvent &)
310 NewFilmDialog* d = new NewFilmDialog (this);
311 int const r = d->ShowModal ();
315 if (boost::filesystem::exists (d->get_path()) && !boost::filesystem::is_empty(d->get_path())) {
316 if (!confirm_dialog (
319 String::compose (wx_to_std (_("The directory %1 already exists and is not empty. "
320 "Are you sure you want to use it?")),
321 d->get_path().c_str())
328 maybe_save_then_delete_film ();
329 film.reset (new Film (d->get_path (), false));
330 film->log()->set_level (log_level);
331 film->set_name (boost::filesystem::path (d->get_path()).filename().generic_string());
338 void file_open (wxCommandEvent &)
340 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
344 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
345 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
352 maybe_save_then_delete_film ();
354 film.reset (new Film (wx_to_std (c->GetPath ())));
355 film->log()->set_level (log_level);
357 } catch (std::exception& e) {
358 wxString p = c->GetPath ();
359 wxCharBuffer b = p.ToUTF8 ();
360 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
367 void file_save (wxCommandEvent &)
369 film->write_metadata ();
372 void file_properties (wxCommandEvent &)
374 PropertiesDialog* d = new PropertiesDialog (this, film);
379 void file_exit (wxCommandEvent &)
381 maybe_save_then_delete_film ();
385 void edit_preferences (wxCommandEvent &)
387 ConfigDialog* d = new ConfigDialog (this);
390 Config::instance()->write ();
393 void jobs_make_dcp (wxCommandEvent &)
395 JobWrapper::make_dcp (this, film);
398 void jobs_send_dcp_to_tms (wxCommandEvent &)
400 film->send_dcp_to_tms ();
403 void jobs_show_dcp (wxCommandEvent &)
406 string d = film->directory();
408 w.assign (d.begin(), d.end());
409 ShellExecute (0, L"open", w.c_str(), 0, 0, SW_SHOWDEFAULT);
411 int r = system ("which nautilus");
412 if (WEXITSTATUS (r) == 0) {
413 system (string ("nautilus " + film->directory()).c_str ());
415 int r = system ("which konqueror");
416 if (WEXITSTATUS (r) == 0) {
417 system (string ("konqueror " + film->directory()).c_str ());
423 void jobs_analyse_audio (wxCommandEvent &)
425 film->analyse_audio ();
428 void help_about (wxCommandEvent &)
430 AboutDialog* d = new AboutDialog (this);
438 #if wxMINOR_VERSION == 9
439 static const wxCmdLineEntryDesc command_line_description[] = {
440 { wxCMD_LINE_OPTION, "l", "log", "set log level (silent, verbose or timing)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
441 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
442 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
445 static const wxCmdLineEntryDesc command_line_description[] = {
446 { wxCMD_LINE_OPTION, wxT("l"), wxT("log"), wxT("set log level (silent, verbose or timing)"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
447 { wxCMD_LINE_PARAM, 0, 0, wxT("film to load"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
448 { wxCMD_LINE_NONE, wxT(""), wxT(""), wxT(""), wxCmdLineParamType (0), 0 }
452 class App : public wxApp
456 if (!wxApp::OnInit()) {
460 #ifdef DVDOMATIC_LINUX
461 unsetenv ("UBUNTU_MENUPROXY");
465 ProcessSerialNumber serial;
466 GetCurrentProcess (&serial);
467 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
470 wxInitAllImageHandlers ();
472 /* Enable i18n; this will create a Config object
473 to look for a force-configured language. This Config
474 object will be wrong, however, because dvdomatic_setup
475 hasn't yet been called and there aren't any scalers, filters etc.
478 dvdomatic_setup_i18n ();
480 /* Set things up, including scalers / filters etc.
481 which will now be internationalised correctly.
485 /* Force the configuration to be re-loaded correctly next
490 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
492 film.reset (new Film (film_to_load));
493 film->log()->set_level (log_level);
494 } catch (exception& e) {
495 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
499 Frame* f = new Frame (_("DVD-o-matic"));
504 ui_signaller = new wxUISignaller (this);
505 this->Connect (-1, wxEVT_IDLE, wxIdleEventHandler (App::idle));
510 void OnInitCmdLine (wxCmdLineParser& parser)
512 parser.SetDesc (command_line_description);
513 parser.SetSwitchChars (wxT ("-"));
516 bool OnCmdLineParsed (wxCmdLineParser& parser)
518 if (parser.GetParamCount() > 0) {
519 film_to_load = wx_to_std (parser.GetParam(0));
523 if (parser.Found(wxT("log"), &log)) {
524 log_level = wx_to_std (log);
530 void idle (wxIdleEvent &)
532 ui_signaller->ui_idle ();