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