2 Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "fonts_dialog.h"
24 #include "system_font_dialog.h"
25 #include "dcpomatic_button.h"
27 #include "lib/content.h"
28 #include "lib/text_content.h"
36 using std::shared_ptr;
37 using namespace dcpomatic;
40 FontsDialog::FontsDialog (wxWindow* parent, shared_ptr<Content> content, shared_ptr<TextContent> caption)
41 : wxDialog (parent, wxID_ANY, _("Fonts"))
45 _fonts = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxSize (550, 200), wxLC_REPORT | wxLC_SINGLE_SEL);
52 _fonts->InsertColumn (0, ip);
58 ip.SetText (_("File"));
60 _fonts->InsertColumn (1, ip);
63 auto sizer = new wxBoxSizer (wxHORIZONTAL);
64 sizer->Add (_fonts, 1, wxEXPAND | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
66 _edit = new Button (this, _("Edit..."));
67 sizer->Add (_edit, 0, wxTOP | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
69 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
70 overall_sizer->Add (sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_X_GAP);
72 auto buttons = CreateSeparatedButtonSizer (wxOK);
74 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
77 SetSizerAndFit (overall_sizer);
79 _edit->Bind (wxEVT_BUTTON, boost::bind (&FontsDialog::edit_clicked, this));
80 _fonts->Bind (wxEVT_LIST_ITEM_SELECTED, boost::bind (&FontsDialog::selection_changed, this));
81 _fonts->Bind (wxEVT_LIST_ITEM_DESELECTED, boost::bind (&FontsDialog::selection_changed, this));
90 auto content = _content.lock ();
91 auto caption = _caption.lock ();
92 if (!content || !caption) {
96 _fonts->DeleteAllItems ();
98 for (auto i: caption->fonts ()) {
101 _fonts->InsertItem (item);
102 _fonts->SetItem (n, 0, std_to_wx (i->id ()));
104 _fonts->SetItem (n, 1, i->file()->leaf().string ());
109 setup_sensitivity ();
114 FontsDialog::selection_changed ()
116 setup_sensitivity ();
121 FontsDialog::setup_sensitivity ()
123 int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
124 _edit->Enable (item != -1);
129 FontsDialog::edit_clicked ()
131 auto content = _content.lock ();
132 auto caption = _caption.lock ();
133 if (!content || !caption) {
137 int const item = _fonts->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
138 auto const id = wx_to_std (_fonts->GetItemText(item, 0));
139 shared_ptr<Font> font;
140 for (auto i: caption->fonts()) {
150 /* The wxFD_CHANGE_DIR here prevents a `could not set working directory' error 123 on Windows when using
151 non-Latin filenames or paths.
153 wxString default_dir = "";
154 #ifdef DCPOMATIC_LINUX
155 if (boost::filesystem::exists ("/usr/share/fonts/truetype")) {
156 default_dir = "/usr/share/fonts/truetype";
158 default_dir = "/usr/share/fonts";
162 default_dir = "/System/Library/Fonts";
165 auto d = new wxFileDialog (this, _("Choose a font file"), default_dir, wxT(""), wxT("*.ttf;*.otf"), wxFD_CHANGE_DIR);
166 int const r = d->ShowModal ();
173 font->set_file (wx_to_std(d->GetPath()));