2062a2341aa060f75aa4fac62b029b880b4ae487
[dcpomatic.git] / src / wx / key_dialog.cc
1 /*
2     Copyright (C) 2015 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 "key_dialog.h"
21 #include "wx_util.h"
22 #include <iostream>
23
24 using std::cout;
25
26 KeyDialog::KeyDialog (wxWindow* parent, dcp::Key key)
27         : TableDialog (parent, _("Key"), 3, 1, true)
28 {
29         add (_("Key"), true);
30
31         wxClientDC dc (parent);
32         wxSize size = dc.GetTextExtent (wxT ("0123456790ABCDEF0123456790ABCDEF"));
33         size.SetHeight (-1);
34
35         wxTextValidator validator (wxFILTER_INCLUDE_CHAR_LIST);
36         wxArrayString list;
37
38         wxString n (wxT ("0123456789abcdefABCDEF"));
39         for (size_t i = 0; i < n.Length(); ++i) {
40                 list.Add (n[i]);
41         }
42
43         validator.SetIncludes (list);
44
45         _key = add (new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, size, 0, validator));
46         _key->SetValue (std_to_wx (key.hex ()));
47         _key->SetMaxLength (32);
48
49         _random = add (new wxButton (this, wxID_ANY, _("Random")));
50
51         _key->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&KeyDialog::key_changed, this));
52         _random->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KeyDialog::random, this));
53
54         layout ();
55 }
56
57 dcp::Key
58 KeyDialog::key () const
59 {
60         return dcp::Key (wx_to_std (_key->GetValue ()));
61 }
62
63 void
64 KeyDialog::key_changed ()
65 {
66         wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK, this));
67         ok->Enable (_key->GetValue().Length() == 32);
68 }
69
70 void
71 KeyDialog::random ()
72 {
73         _key->SetValue (dcp::Key().hex ());
74 }