Give content menu on both main control and timeline. Fix silly bug on updating edito...
[dcpomatic.git] / src / wx / config_dialog.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 /** @file src/config_dialog.cc
21  *  @brief A dialogue to edit DCP-o-matic configuration.
22  */
23
24 #include <iostream>
25 #include <boost/lexical_cast.hpp>
26 #include <boost/filesystem.hpp>
27 #include <wx/stdpaths.h>
28 #include <wx/notebook.h>
29 #include "lib/config.h"
30 #include "lib/server.h"
31 #include "lib/ratio.h"
32 #include "lib/scaler.h"
33 #include "lib/filter.h"
34 #include "lib/dcp_content_type.h"
35 #include "config_dialog.h"
36 #include "wx_util.h"
37 #include "filter_dialog.h"
38 #include "server_dialog.h"
39 #include "dir_picker_ctrl.h"
40 #include "dci_metadata_dialog.h"
41
42 using namespace std;
43 using boost::bind;
44
45 ConfigDialog::ConfigDialog (wxWindow* parent)
46         : wxDialog (parent, wxID_ANY, _("DCP-o-matic Preferences"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
47 {
48         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
49         _notebook = new wxNotebook (this, wxID_ANY);
50         s->Add (_notebook, 1);
51
52         make_misc_panel ();
53         _notebook->AddPage (_misc_panel, _("Miscellaneous"), true);
54         make_servers_panel ();
55         _notebook->AddPage (_servers_panel, _("Encoding servers"), false);
56         make_metadata_panel ();
57         _notebook->AddPage (_metadata_panel, _("Metadata"), false);
58         make_tms_panel ();
59         _notebook->AddPage (_tms_panel, _("TMS"), false);
60
61         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
62         overall_sizer->Add (s, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
63
64         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
65         if (buttons) {
66                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
67         }
68
69         SetSizer (overall_sizer);
70         overall_sizer->Layout ();
71         overall_sizer->SetSizeHints (this);
72 }
73
74 void
75 ConfigDialog::make_misc_panel ()
76 {
77         _misc_panel = new wxPanel (_notebook);
78         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
79         _misc_panel->SetSizer (s);
80
81         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
82         table->AddGrowableCol (1, 1);
83         s->Add (table, 1, wxALL | wxEXPAND, 8);
84
85         _set_language = new wxCheckBox (_misc_panel, wxID_ANY, _("Set language"));
86         table->Add (_set_language, 1);
87         _language = new wxChoice (_misc_panel, wxID_ANY);
88         _language->Append (wxT ("English"));
89         _language->Append (wxT ("Français"));
90         _language->Append (wxT ("Italiano"));
91         _language->Append (wxT ("Español"));
92         _language->Append (wxT ("Svenska"));
93         table->Add (_language);
94
95         wxStaticText* restart = add_label_to_sizer (table, _misc_panel, _("(restart DCP-o-matic to see language changes)"), false);
96         wxFont font = restart->GetFont();
97         font.SetStyle (wxFONTSTYLE_ITALIC);
98         font.SetPointSize (font.GetPointSize() - 1);
99         restart->SetFont (font);
100         table->AddSpacer (0);
101
102         add_label_to_sizer (table, _misc_panel, _("Threads to use for encoding on this host"), true);
103         _num_local_encoding_threads = new wxSpinCtrl (_misc_panel);
104         table->Add (_num_local_encoding_threads, 1);
105
106         {
107                 add_label_to_sizer (table, _misc_panel, _("Default duration of still images"), true);
108                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
109                 _default_still_length = new wxSpinCtrl (_misc_panel);
110                 s->Add (_default_still_length);
111                 add_label_to_sizer (s, _misc_panel, _("s"), false);
112                 table->Add (s, 1);
113         }
114
115         add_label_to_sizer (table, _misc_panel, _("Default directory for new films"), true);
116 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
117         _default_directory = new DirPickerCtrl (_misc_panel);
118 #else   
119         _default_directory = new wxDirPickerCtrl (_misc_panel, wxDD_DIR_MUST_EXIST);
120 #endif
121         table->Add (_default_directory, 1, wxEXPAND);
122
123         add_label_to_sizer (table, _misc_panel, _("Default DCI name details"), true);
124         _default_dci_metadata_button = new wxButton (_misc_panel, wxID_ANY, _("Edit..."));
125         table->Add (_default_dci_metadata_button);
126
127         add_label_to_sizer (table, _misc_panel, _("Default container"), true);
128         _default_container = new wxChoice (_misc_panel, wxID_ANY);
129         table->Add (_default_container);
130
131         add_label_to_sizer (table, _misc_panel, _("Default content type"), true);
132         _default_dcp_content_type = new wxChoice (_misc_panel, wxID_ANY);
133         table->Add (_default_dcp_content_type);
134
135         {
136                 add_label_to_sizer (table, _misc_panel, _("Default JPEG2000 bandwidth"), true);
137                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
138                 _default_j2k_bandwidth = new wxSpinCtrl (_misc_panel);
139                 s->Add (_default_j2k_bandwidth);
140                 add_label_to_sizer (s, _misc_panel, _("MBps"), false);
141                 table->Add (s, 1);
142         }
143         
144         Config* config = Config::instance ();
145
146         _set_language->SetValue (config->language ());
147
148         if (config->language().get_value_or ("") == "fr") {
149                 _language->SetSelection (1);
150         } else if (config->language().get_value_or ("") == "it") {
151                 _language->SetSelection (2);
152         } else if (config->language().get_value_or ("") == "es") {
153                 _language->SetSelection (3);
154         } else if (config->language().get_value_or ("") == "sv") {
155                 _language->SetSelection (4);
156         } else {
157                 _language->SetSelection (0);
158         }
159
160         setup_language_sensitivity ();
161
162         _set_language->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (ConfigDialog::set_language_changed), 0, this);
163         _language->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::language_changed), 0, this);
164
165         _num_local_encoding_threads->SetRange (1, 128);
166         _num_local_encoding_threads->SetValue (config->num_local_encoding_threads ());
167         _num_local_encoding_threads->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::num_local_encoding_threads_changed), 0, this);
168
169         _default_still_length->SetRange (1, 3600);
170         _default_still_length->SetValue (config->default_still_length ());
171         _default_still_length->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::default_still_length_changed), 0, this);
172
173         _default_directory->SetPath (std_to_wx (config->default_directory_or (wx_to_std (wxStandardPaths::Get().GetDocumentsDir()))));
174         _default_directory->Connect (wxID_ANY, wxEVT_COMMAND_DIRPICKER_CHANGED, wxCommandEventHandler (ConfigDialog::default_directory_changed), 0, this);
175
176         _default_dci_metadata_button->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_default_dci_metadata_clicked), 0, this);
177
178         vector<Ratio const *> ratio = Ratio::all ();
179         int n = 0;
180         for (vector<Ratio const *>::iterator i = ratio.begin(); i != ratio.end(); ++i) {
181                 _default_container->Append (std_to_wx ((*i)->nickname ()));
182                 if (*i == config->default_container ()) {
183                         _default_container->SetSelection (n);
184                 }
185                 ++n;
186         }
187
188         _default_container->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::default_container_changed), 0, this);
189         
190         vector<DCPContentType const *> const ct = DCPContentType::all ();
191         n = 0;
192         for (vector<DCPContentType const *>::const_iterator i = ct.begin(); i != ct.end(); ++i) {
193                 _default_dcp_content_type->Append (std_to_wx ((*i)->pretty_name ()));
194                 if (*i == config->default_dcp_content_type ()) {
195                         _default_dcp_content_type->SetSelection (n);
196                 }
197                 ++n;
198         }
199
200         _default_dcp_content_type->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (ConfigDialog::default_dcp_content_type_changed), 0, this);
201
202         _default_j2k_bandwidth->SetRange (50, 250);
203         _default_j2k_bandwidth->SetValue (config->default_j2k_bandwidth() / 1e6);
204         _default_j2k_bandwidth->Connect (wxID_ANY, wxEVT_COMMAND_SPINCTRL_UPDATED, wxCommandEventHandler (ConfigDialog::default_j2k_bandwidth_changed), 0, this);
205 }
206
207 void
208 ConfigDialog::make_tms_panel ()
209 {
210         _tms_panel = new wxPanel (_notebook);
211         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
212         _tms_panel->SetSizer (s);
213
214         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
215         table->AddGrowableCol (1, 1);
216         s->Add (table, 1, wxALL | wxEXPAND, 8);
217
218         add_label_to_sizer (table, _tms_panel, _("IP address"), true);
219         _tms_ip = new wxTextCtrl (_tms_panel, wxID_ANY);
220         table->Add (_tms_ip, 1, wxEXPAND);
221
222         add_label_to_sizer (table, _tms_panel, _("Target path"), true);
223         _tms_path = new wxTextCtrl (_tms_panel, wxID_ANY);
224         table->Add (_tms_path, 1, wxEXPAND);
225
226         add_label_to_sizer (table, _tms_panel, _("User name"), true);
227         _tms_user = new wxTextCtrl (_tms_panel, wxID_ANY);
228         table->Add (_tms_user, 1, wxEXPAND);
229
230         add_label_to_sizer (table, _tms_panel, _("Password"), true);
231         _tms_password = new wxTextCtrl (_tms_panel, wxID_ANY);
232         table->Add (_tms_password, 1, wxEXPAND);
233
234         Config* config = Config::instance ();
235         
236         _tms_ip->SetValue (std_to_wx (config->tms_ip ()));
237         _tms_ip->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_ip_changed), 0, this);
238         _tms_path->SetValue (std_to_wx (config->tms_path ()));
239         _tms_path->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_path_changed), 0, this);
240         _tms_user->SetValue (std_to_wx (config->tms_user ()));
241         _tms_user->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_user_changed), 0, this);
242         _tms_password->SetValue (std_to_wx (config->tms_password ()));
243         _tms_password->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::tms_password_changed), 0, this);
244 }
245
246 void
247 ConfigDialog::make_metadata_panel ()
248 {
249         _metadata_panel = new wxPanel (_notebook);
250         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
251         _metadata_panel->SetSizer (s);
252
253         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
254         table->AddGrowableCol (1, 1);
255         s->Add (table, 1, wxALL | wxEXPAND, 8);
256
257         add_label_to_sizer (table, _metadata_panel, _("Issuer"), true);
258         _issuer = new wxTextCtrl (_metadata_panel, wxID_ANY);
259         table->Add (_issuer, 1, wxEXPAND);
260
261         add_label_to_sizer (table, _metadata_panel, _("Creator"), true);
262         _creator = new wxTextCtrl (_metadata_panel, wxID_ANY);
263         table->Add (_creator, 1, wxEXPAND);
264
265         Config* config = Config::instance ();
266
267         _issuer->SetValue (std_to_wx (config->dcp_metadata().issuer));
268         _issuer->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::issuer_changed), 0, this);
269         _creator->SetValue (std_to_wx (config->dcp_metadata().creator));
270         _creator->Connect (wxID_ANY, wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler (ConfigDialog::creator_changed), 0, this);
271 }
272
273 void
274 ConfigDialog::make_servers_panel ()
275 {
276         _servers_panel = new wxPanel (_notebook);
277         wxBoxSizer* s = new wxBoxSizer (wxVERTICAL);
278         _servers_panel->SetSizer (s);
279
280         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
281         table->AddGrowableCol (0, 1);
282         s->Add (table, 1, wxALL | wxEXPAND, 8);
283
284         Config* config = Config::instance ();
285
286         _servers = new wxListCtrl (_servers_panel, wxID_ANY, wxDefaultPosition, wxSize (220, 100), wxLC_REPORT | wxLC_SINGLE_SEL);
287         wxListItem ip;
288         ip.SetId (0);
289         ip.SetText (_("IP address"));
290         ip.SetWidth (120);
291         _servers->InsertColumn (0, ip);
292         ip.SetId (1);
293         ip.SetText (_("Threads"));
294         ip.SetWidth (80);
295         _servers->InsertColumn (1, ip);
296         table->Add (_servers, 1, wxEXPAND | wxALL);
297
298         {
299                 wxSizer* s = new wxBoxSizer (wxVERTICAL);
300                 _add_server = new wxButton (_servers_panel, wxID_ANY, _("Add"));
301                 s->Add (_add_server, 0, wxTOP | wxBOTTOM, 2);
302                 _edit_server = new wxButton (_servers_panel, wxID_ANY, _("Edit"));
303                 s->Add (_edit_server, 0, wxTOP | wxBOTTOM, 2);
304                 _remove_server = new wxButton (_servers_panel, wxID_ANY, _("Remove"));
305                 s->Add (_remove_server, 0, wxTOP | wxBOTTOM, 2);
306                 table->Add (s, 0);
307         }
308
309         vector<ServerDescription*> servers = config->servers ();
310         for (vector<ServerDescription*>::iterator i = servers.begin(); i != servers.end(); ++i) {
311                 add_server_to_control (*i);
312         }
313         
314         _add_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::add_server_clicked), 0, this);
315         _edit_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::edit_server_clicked), 0, this);
316         _remove_server->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (ConfigDialog::remove_server_clicked), 0, this);
317
318         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_SELECTED, wxListEventHandler (ConfigDialog::server_selection_changed), 0, this);
319         _servers->Connect (wxID_ANY, wxEVT_COMMAND_LIST_ITEM_DESELECTED, wxListEventHandler (ConfigDialog::server_selection_changed), 0, this);
320         wxListEvent ev;
321         server_selection_changed (ev);
322 }
323
324 void
325 ConfigDialog::language_changed (wxCommandEvent &)
326 {
327         switch (_language->GetSelection ()) {
328         case 0:
329                 Config::instance()->set_language ("en");
330                 break;
331         case 1:
332                 Config::instance()->set_language ("fr");
333                 break;
334         case 2:
335                 Config::instance()->set_language ("it");
336                 break;
337         case 3:
338                 Config::instance()->set_language ("es");
339                 break;
340         case 4:
341                 Config::instance()->set_language ("sv");
342                 break;
343         }
344 }
345
346 void
347 ConfigDialog::tms_ip_changed (wxCommandEvent &)
348 {
349         Config::instance()->set_tms_ip (wx_to_std (_tms_ip->GetValue ()));
350 }
351
352 void
353 ConfigDialog::tms_path_changed (wxCommandEvent &)
354 {
355         Config::instance()->set_tms_path (wx_to_std (_tms_path->GetValue ()));
356 }
357
358 void
359 ConfigDialog::tms_user_changed (wxCommandEvent &)
360 {
361         Config::instance()->set_tms_user (wx_to_std (_tms_user->GetValue ()));
362 }
363
364 void
365 ConfigDialog::tms_password_changed (wxCommandEvent &)
366 {
367         Config::instance()->set_tms_password (wx_to_std (_tms_password->GetValue ()));
368 }
369
370 void
371 ConfigDialog::num_local_encoding_threads_changed (wxCommandEvent &)
372 {
373         Config::instance()->set_num_local_encoding_threads (_num_local_encoding_threads->GetValue ());
374 }
375
376 void
377 ConfigDialog::default_directory_changed (wxCommandEvent &)
378 {
379         Config::instance()->set_default_directory (wx_to_std (_default_directory->GetPath ()));
380 }
381
382 void
383 ConfigDialog::add_server_to_control (ServerDescription* s)
384 {
385         wxListItem item;
386         int const n = _servers->GetItemCount ();
387         item.SetId (n);
388         _servers->InsertItem (item);
389         _servers->SetItem (n, 0, std_to_wx (s->host_name ()));
390         _servers->SetItem (n, 1, std_to_wx (boost::lexical_cast<string> (s->threads ())));
391 }
392
393 void
394 ConfigDialog::add_server_clicked (wxCommandEvent &)
395 {
396         ServerDialog* d = new ServerDialog (this, 0);
397         d->ShowModal ();
398         ServerDescription* s = d->server ();
399         d->Destroy ();
400         
401         add_server_to_control (s);
402         vector<ServerDescription*> o = Config::instance()->servers ();
403         o.push_back (s);
404         Config::instance()->set_servers (o);
405 }
406
407 void
408 ConfigDialog::edit_server_clicked (wxCommandEvent &)
409 {
410         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
411         if (i == -1) {
412                 return;
413         }
414
415         wxListItem item;
416         item.SetId (i);
417         item.SetColumn (0);
418         _servers->GetItem (item);
419
420         vector<ServerDescription*> servers = Config::instance()->servers ();
421         assert (i >= 0 && i < int (servers.size ()));
422
423         ServerDialog* d = new ServerDialog (this, servers[i]);
424         d->ShowModal ();
425         d->Destroy ();
426
427         _servers->SetItem (i, 0, std_to_wx (servers[i]->host_name ()));
428         _servers->SetItem (i, 1, std_to_wx (boost::lexical_cast<string> (servers[i]->threads ())));
429 }
430
431 void
432 ConfigDialog::remove_server_clicked (wxCommandEvent &)
433 {
434         int i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
435         if (i >= 0) {
436                 _servers->DeleteItem (i);
437         }
438
439         vector<ServerDescription*> o = Config::instance()->servers ();
440         o.erase (o.begin() + i);
441         Config::instance()->set_servers (o);
442 }
443
444 void
445 ConfigDialog::server_selection_changed (wxListEvent &)
446 {
447         int const i = _servers->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
448         _edit_server->Enable (i >= 0);
449         _remove_server->Enable (i >= 0);
450 }
451
452 void
453 ConfigDialog::edit_default_dci_metadata_clicked (wxCommandEvent &)
454 {
455         DCIMetadataDialog* d = new DCIMetadataDialog (this, Config::instance()->default_dci_metadata ());
456         d->ShowModal ();
457         Config::instance()->set_default_dci_metadata (d->dci_metadata ());
458         d->Destroy ();
459 }
460
461 void
462 ConfigDialog::set_language_changed (wxCommandEvent& ev)
463 {
464         setup_language_sensitivity ();
465         if (_set_language->GetValue ()) {
466                 language_changed (ev);
467         } else {
468                 Config::instance()->unset_language ();
469         }
470 }
471
472 void
473 ConfigDialog::setup_language_sensitivity ()
474 {
475         _language->Enable (_set_language->GetValue ());
476 }
477
478 void
479 ConfigDialog::default_still_length_changed (wxCommandEvent &)
480 {
481         Config::instance()->set_default_still_length (_default_still_length->GetValue ());
482 }
483
484 void
485 ConfigDialog::default_container_changed (wxCommandEvent &)
486 {
487         vector<Ratio const *> ratio = Ratio::all ();
488         Config::instance()->set_default_container (ratio[_default_container->GetSelection()]);
489 }
490
491 void
492 ConfigDialog::default_dcp_content_type_changed (wxCommandEvent &)
493 {
494         vector<DCPContentType const *> ct = DCPContentType::all ();
495         Config::instance()->set_default_dcp_content_type (ct[_default_dcp_content_type->GetSelection()]);
496 }
497
498 void
499 ConfigDialog::issuer_changed (wxCommandEvent &)
500 {
501         libdcp::XMLMetadata m = Config::instance()->dcp_metadata ();
502         m.issuer = wx_to_std (_issuer->GetValue ());
503         Config::instance()->set_dcp_metadata (m);
504 }
505
506 void
507 ConfigDialog::creator_changed (wxCommandEvent &)
508 {
509         libdcp::XMLMetadata m = Config::instance()->dcp_metadata ();
510         m.creator = wx_to_std (_creator->GetValue ());
511         Config::instance()->set_dcp_metadata (m);
512 }
513
514 void
515 ConfigDialog::default_j2k_bandwidth_changed (wxCommandEvent &)
516 {
517         Config::instance()->set_default_j2k_bandwidth (_default_j2k_bandwidth->GetValue() * 1e6);
518 }