Various small fixes.
[dcpomatic.git] / src / wx / screen_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 #include <wx/filepicker.h>
21 #include <wx/validate.h>
22 #include <dcp/exceptions.h>
23 #include "lib/compose.hpp"
24 #include "screen_dialog.h"
25 #include "wx_util.h"
26
27 using std::string;
28 using std::cout;
29 using boost::shared_ptr;
30
31 ScreenDialog::ScreenDialog (wxWindow* parent, string title, string name, shared_ptr<dcp::Certificate> certificate)
32         : wxDialog (parent, wxID_ANY, std_to_wx (title))
33         , _certificate (certificate)
34 {
35         wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6);
36         table->AddGrowableCol (1, 1);
37
38         add_label_to_sizer (table, this, "Name", true);
39         _name = new wxTextCtrl (this, wxID_ANY, std_to_wx (name), wxDefaultPosition, wxSize (320, -1));
40         table->Add (_name, 1, wxEXPAND);
41
42         add_label_to_sizer (table, this, "Certificate", true);
43         _certificate_load = new wxButton (this, wxID_ANY, wxT ("Load from file..."));
44         table->Add (_certificate_load, 1, wxEXPAND);
45
46         table->AddSpacer (0);
47         _certificate_text = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (320, 256), wxTE_MULTILINE | wxTE_READONLY);
48         if (certificate) {
49                 _certificate_text->SetValue (certificate->certificate ());
50         }
51         wxFont font = wxSystemSettings::GetFont (wxSYS_ANSI_FIXED_FONT);
52         font.SetPointSize (font.GetPointSize() / 2);
53         _certificate_text->SetFont (font);
54         table->Add (_certificate_text, 1, wxEXPAND);
55
56         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
57         overall_sizer->Add (table, 1, wxEXPAND | wxALL, 6);
58         
59         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
60         if (buttons) {
61                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
62         }
63
64         SetSizer (overall_sizer);
65         overall_sizer->Layout ();
66         overall_sizer->SetSizeHints (this);
67
68         _certificate_load->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&ScreenDialog::load_certificate, this));
69
70         setup_sensitivity ();
71 }
72
73 string
74 ScreenDialog::name () const
75 {
76         return wx_to_std (_name->GetValue());
77 }
78
79 shared_ptr<dcp::Certificate>
80 ScreenDialog::certificate () const
81 {
82         return _certificate;
83 }
84
85 void
86 ScreenDialog::load_certificate ()
87 {
88         wxFileDialog* d = new wxFileDialog (this, _("Select Certificate File"));
89
90         if (d->ShowModal () == wxID_OK) {
91                 try {
92                         _certificate.reset (new dcp::Certificate (boost::filesystem::path (wx_to_std (d->GetPath ()))));
93                         _certificate_text->SetValue (_certificate->certificate ());
94                 } catch (dcp::MiscError& e) {
95                         error_dialog (this, String::compose ("Could not read certificate file (%1)", e.what()));
96                 }
97         }
98         
99         d->Destroy ();
100
101         setup_sensitivity ();
102 }
103
104 void
105 ScreenDialog::setup_sensitivity ()
106 {
107         wxButton* ok = dynamic_cast<wxButton*> (FindWindowById (wxID_OK, this));
108         ok->Enable (_certificate.get ());
109 }