080a8ebc6fdce038a23776c13e31e4dfe6f02b11
[dcpomatic.git] / src / wx / font_files_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 "font_files_dialog.h"
21 #include "system_font_dialog.h"
22
23 using boost::bind;
24
25 FontFilesDialog::FontFilesDialog (wxWindow* parent, FontFiles files)
26 #ifdef DCPOMATIC_WINDOWS
27         : TableDialog (parent, _("Fonts"), 4, 1, true)
28 #else
29         : TableDialog (parent, _("Fonts"), 3, 1, true)
30 #endif
31         , _files (files)
32 {
33         wxString labels[] = {
34                 _("Normal font"),
35                 _("Italic font"),
36                 _("Bold font")
37         };
38
39         DCPOMATIC_ASSERT (FontFiles::VARIANTS == 3);
40
41         for (int i = 0; i < FontFiles::VARIANTS; ++i) {
42                 add (labels[i], true);
43                 _name[i] = new wxStaticText (
44                         this, wxID_ANY,
45                         std_to_wx(_files.get(static_cast<FontFiles::Variant>(i)).get_value_or("").string()),
46                         wxDefaultPosition,
47                         wxSize (200, -1)
48                         );
49                 _table->Add (_name[i], 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
50                 add (_set_file[i] = new wxButton (this, wxID_ANY, _("Set from file...")));
51                 _set_file[i]->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&FontFilesDialog::set_from_file_clicked, this, static_cast<FontFiles::Variant>(i)));
52 #ifdef DCPOMATIC_WINDOWS
53                 add (_set_system[i] = new wxButton (this, wxID_ANY, _("Set from system font...")));
54                 _set_system[i]->Bind (wxEVT_COMMAND_BUTTON_CLICKED, bind (&FontFilesDialog::set_from_system_clicked, this, static_cast<FontFiles::Variant>(i)));
55 #endif
56         }
57
58         layout ();
59 }
60
61 void
62 FontFilesDialog::set_from_file_clicked (FontFiles::Variant variant)
63 {
64         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
65            non-Latin filenames or paths.
66         */
67         wxString default_dir = "";
68 #ifdef DCPOMATIC_LINUX
69         if (boost::filesystem::exists ("/usr/share/fonts/truetype")) {
70                 default_dir = "/usr/share/fonts/truetype";
71         } else {
72                 default_dir = "/usr/share/fonts";
73         }
74 #endif
75 #ifdef DCPOMATIC_OSX
76         default_dir = "/System/Library/Fonts";
77 #endif
78
79         wxFileDialog* d = new wxFileDialog (this, _("Choose a font file"), default_dir, wxT (""), wxT ("*.ttf"), wxFD_CHANGE_DIR);
80         int const r = d->ShowModal ();
81
82         if (r != wxID_OK) {
83                 d->Destroy ();
84                 return;
85         }
86
87         set (variant, wx_to_std (d->GetPath ()));
88         d->Destroy ();
89 }
90
91 #ifdef DCPOMATIC_WINDOWS
92 void
93 FontFilesDialog::set_from_system_clicked (FontFiles::Variant variant)
94 {
95         SystemFontDialog* d = new SystemFontDialog (this);
96         int const r = d->ShowModal ();
97
98         if (r != wxID_OK) {
99                 d->Destroy ();
100                 return;
101         }
102
103         set (variant, d->get_font().get());
104         d->Destroy ();
105 }
106 #endif
107
108 void
109 FontFilesDialog::set (FontFiles::Variant variant, boost::filesystem::path path)
110 {
111         _files.set (variant, path);
112         _name[variant]->SetLabel (std_to_wx (path.leaf().string()));
113 }