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