Basics of in-place i18n with support for wxStaticText and wxCheckBox.
[dcpomatic.git] / src / wx / font_files_dialog.cc
1 /*
2     Copyright (C) 2015-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "font_files_dialog.h"
22 #include "system_font_dialog.h"
23 #include "static_text.h"
24
25 using boost::bind;
26
27 FontFilesDialog::FontFilesDialog (wxWindow* parent, FontFiles files)
28 #ifdef DCPOMATIC_WINDOWS
29         : TableDialog (parent, _("Fonts"), 4, 1, true)
30 #else
31         : TableDialog (parent, _("Fonts"), 3, 1, true)
32 #endif
33         , _files (files)
34 {
35         wxString labels[] = {
36                 _("Normal font"),
37                 _("Italic font"),
38                 _("Bold font")
39         };
40
41         DCPOMATIC_ASSERT (FontFiles::VARIANTS == 3);
42
43         for (int i = 0; i < FontFiles::VARIANTS; ++i) {
44                 add (labels[i], true);
45                 _name[i] = new StaticText (
46                         this,
47                         std_to_wx(_files.get(static_cast<FontFiles::Variant>(i)).get_value_or("").string()),
48                         wxDefaultPosition,
49                         wxSize (200, -1)
50                         );
51                 _table->Add (_name[i], 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
52                 add (_set_file[i] = new wxButton (this, wxID_ANY, _("Set from file...")));
53                 _set_file[i]->Bind (wxEVT_BUTTON, bind (&FontFilesDialog::set_from_file_clicked, this, static_cast<FontFiles::Variant>(i)));
54 #ifdef DCPOMATIC_WINDOWS
55                 add (_set_system[i] = new wxButton (this, wxID_ANY, _("Set from system font...")));
56                 _set_system[i]->Bind (wxEVT_BUTTON, bind (&FontFilesDialog::set_from_system_clicked, this, static_cast<FontFiles::Variant>(i)));
57 #endif
58         }
59
60         layout ();
61 }
62
63 void
64 FontFilesDialog::set_from_file_clicked (FontFiles::Variant variant)
65 {
66         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
67            non-Latin filenames or paths.
68         */
69         wxString default_dir = "";
70 #ifdef DCPOMATIC_LINUX
71         if (boost::filesystem::exists ("/usr/share/fonts/truetype")) {
72                 default_dir = "/usr/share/fonts/truetype";
73         } else {
74                 default_dir = "/usr/share/fonts";
75         }
76 #endif
77 #ifdef DCPOMATIC_OSX
78         default_dir = "/System/Library/Fonts";
79 #endif
80
81         wxFileDialog* d = new wxFileDialog (this, _("Choose a font file"), default_dir, wxT (""), wxT ("*.ttf"), wxFD_CHANGE_DIR);
82         int const r = d->ShowModal ();
83
84         if (r != wxID_OK) {
85                 d->Destroy ();
86                 return;
87         }
88
89         set (variant, wx_to_std (d->GetPath ()));
90         d->Destroy ();
91 }
92
93 #ifdef DCPOMATIC_WINDOWS
94 void
95 FontFilesDialog::set_from_system_clicked (FontFiles::Variant variant)
96 {
97         SystemFontDialog* d = new SystemFontDialog (this);
98         int const r = d->ShowModal ();
99
100         if (r != wxID_OK) {
101                 d->Destroy ();
102                 return;
103         }
104
105         set (variant, d->get_font().get());
106         d->Destroy ();
107 }
108 #endif
109
110 void
111 FontFilesDialog::set (FontFiles::Variant variant, boost::filesystem::path path)
112 {
113         _files.set (variant, path);
114         _name[variant]->SetLabel (std_to_wx (path.leaf().string()));
115 }