Supporters update.
[dcpomatic.git] / src / wx / fonts_dialog.cc
1 /*
2     Copyright (C) 2014-2021 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
22 #include "dcpomatic_button.h"
23 #include "fonts_dialog.h"
24 #include "system_font_dialog.h"
25 #include "wx_util.h"
26 #include "lib/content.h"
27 #include "lib/font.h"
28 #include "lib/text_content.h"
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <wx/wx.h>
32 LIBDCP_ENABLE_WARNINGS
33 #include <memory>
34
35
36 using std::list;
37 using std::shared_ptr;
38 using std::string;
39 using namespace dcpomatic;
40
41
42 FontsDialog::FontsDialog (wxWindow* parent, shared_ptr<Content> content, shared_ptr<TextContent> caption)
43         : wxDialog (parent, wxID_ANY, _("Fonts"))
44         , _content (content)
45         , _caption (caption)
46 {
47         _fonts = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (550, 200), wxLC_REPORT | wxLC_SINGLE_SEL);
48
49         {
50                 wxListItem ip;
51                 ip.SetId (0);
52                 ip.SetText (_("ID"));
53                 ip.SetWidth (100);
54                 _fonts->InsertColumn (0, ip);
55         }
56
57         {
58                 wxListItem ip;
59                 ip.SetId (1);
60                 ip.SetText (_("File"));
61                 ip.SetWidth (450);
62                 _fonts->InsertColumn (1, ip);
63         }
64
65         auto sizer = new wxBoxSizer (wxHORIZONTAL);
66         sizer->Add (_fonts, 1, wxEXPAND | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
67
68         auto buttons_panel = new wxPanel(this);
69         auto buttons_sizer = new wxBoxSizer(wxVERTICAL);
70
71         _set_from_file = new Button(buttons_panel, _("Set from file..."));
72         buttons_sizer->Add (_set_from_file, 0, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
73
74 #ifdef DCPOMATIC_WINDOWS
75         _set_from_system_font = new Button(buttons_panel, _("Set from system font..."));
76         buttons_sizer->Add (_set_from_system_font, 0, wxEXPAND | wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
77 #endif
78
79         buttons_panel->SetSizer(buttons_sizer);
80         sizer->Add(buttons_panel);
81
82         auto overall_sizer = new wxBoxSizer (wxVERTICAL);
83         overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
84
85         auto buttons = CreateSeparatedButtonSizer (wxOK);
86         if (buttons) {
87                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
88         }
89
90         SetSizerAndFit (overall_sizer);
91
92         _set_from_file->Bind(wxEVT_BUTTON, boost::bind(&FontsDialog::set_from_file_clicked, this));
93         if (_set_from_system_font) {
94                 _set_from_system_font->Bind(wxEVT_BUTTON, boost::bind(&FontsDialog::set_from_system_font_clicked, this));
95         }
96         _fonts->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&FontsDialog::selection_changed, this));
97         _fonts->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&FontsDialog::selection_changed, this));
98
99         setup ();
100 }
101
102
103 void
104 FontsDialog::setup ()
105 {
106         auto content = _content.lock ();
107         auto caption = _caption.lock ();
108         if (!content || !caption) {
109                 return;
110         }
111
112         _fonts->DeleteAllItems ();
113         size_t n = 0;
114         for (auto i: caption->fonts ()) {
115                 wxListItem item;
116                 item.SetId (n);
117                 _fonts->InsertItem (item);
118                 auto const id = i->id().empty() ? _("Unspecified") : std_to_wx(i->id());
119                 _fonts->SetItem(n, 0, id);
120                 _fonts->SetItemData(n, i->id().empty());
121                 if (i->file()) {
122                         _fonts->SetItem(n, 1, i->file()->leaf().string());
123                 }
124                 ++n;
125         }
126
127         setup_sensitivity ();
128 }
129
130
131 void
132 FontsDialog::selection_changed ()
133 {
134         setup_sensitivity ();
135 }
136
137
138 void
139 FontsDialog::setup_sensitivity ()
140 {
141         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
142         _set_from_file->Enable (item != -1);
143         if (_set_from_system_font) {
144                 _set_from_system_font->Enable (item != -1);
145         }
146 }
147
148
149 shared_ptr<Font>
150 FontsDialog::get_selection ()
151 {
152         auto caption = _caption.lock();
153         if (!caption) {
154                 return {};
155         }
156
157         int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
158         auto const id = _fonts->GetItemData(item) ? "" : wx_to_std(_fonts->GetItemText(item, 0));
159         return caption->get_font(id);
160 }
161
162
163 void
164 FontsDialog::set_from_file_clicked ()
165 {
166         auto font = get_selection();
167         if (!font) {
168                 return;
169         }
170
171         /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
172            non-Latin filenames or paths.
173         */
174         wxString default_dir = "";
175 #ifdef DCPOMATIC_LINUX
176         if (boost::filesystem::exists ("/usr/share/fonts/truetype")) {
177                 default_dir = "/usr/share/fonts/truetype";
178         } else {
179                 default_dir = "/usr/share/fonts";
180         }
181 #endif
182 #ifdef DCPOMATIC_OSX
183         default_dir = "/System/Library/Fonts";
184 #endif
185
186         auto d = new wxFileDialog (this, _("Choose a font file"), default_dir, wxT(""), wxT("*.ttf;*.otf;*.ttc"), wxFD_CHANGE_DIR);
187         int const r = d->ShowModal ();
188
189         if (r != wxID_OK) {
190                 d->Destroy ();
191                 return;
192         }
193
194         font->set_file (wx_to_std(d->GetPath()));
195         d->Destroy ();
196
197         setup ();
198 }
199
200
201 void
202 FontsDialog::set_from_system_font_clicked()
203 {
204         auto font = get_selection();
205         if (!font) {
206                 return;
207         }
208
209         auto dialog = new SystemFontDialog(this);
210         auto const r = dialog->ShowModal();
211         if (r == wxID_OK) {
212                 auto font_file = dialog->get_font();
213                 if (font_file) {
214                         font->set_file(*font_file);
215                 }
216         }
217
218         dialog->Destroy();
219         setup ();
220 }