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